Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // chaitin.c
- //
- // Author: Clayton Bauman, 2014
- // License: BSD License
- //
- // This cipher is named after Gregory Chaitin, a mathematical hero of mine.
- // The ideas of Algorithmic Information Theory have affected every aspect of
- // my thinking, including my ideas about encryption. The Chaitin cipher seeks
- // to build substantial entropy into the operation of the cipher, without
- // introducing the trust issues that arise when encoding arbitrary constants
- // into a cipher, as well as avoiding common cipher mistakes, such as
- // vulnerability to linear and differential cryptanalysis and side-channel
- // attacks.
- //
- // "Anyone who considers arithmetical methods of producing random digits is, of
- // course, in a state of sin."
- // - John von Neumann
- //
- // "But when a rule is very complex what conforms to it passes for random."
- // - Gottfried Leibniz
- // Get user key, size N bytes
- // Key warm-up:
- // for i in 0 to N-1:
- // keyed_muxrand_stir(N_i)
- // muxrand_stir 1000 times
- // Key schedule
- // Create Chaitin key (12,288 bytes, with muxrand8)
- // key[0]->perm_key = shuffle(perm_table, key[0]->perm_key)
- // key[0]->subst_key = shuffle(subst_table, key[0]->subst_key)
- // For round in 1 to 15:
- // key[round]->perm_key = shuffle(key[round-1]->perm_key, key[round]->perm_key)
- // key[round]->subst_key = shuffle(key[round-1]->subst_key, key[round]->subst_key)
- //
- // for input:
- // enc:
- // For round in 0 to 15:
- // masked = input XOR key[round]->mask
- // permed = permute(masked, key[round]->perm_key)
- // substd = subst(permed, key[round]->subst_key)
- // input = substd
- //
- // dec:
- // For round in 15 to 0:
- // substd = subst(input, inv(key[round]->subst_key))
- // permed = permute(substd, inv(key[round]->perm_key))
- // masked = permed XOR key[round]->mask
- // intput = masked
- #include "stdint.h"
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
- #define _die fprintf(stderr, "Died at %s line %d\n", __FILE__, __LINE__); exit(1); // die#
- #define _trace fprintf(stderr, "%s() in %s line %d\n", __func__, __FILE__, __LINE__); // trace#
- #define mux8(A,B,S) ((A&S)|(B&~S))
- char muxrand_array[256] = {
- 0xC4, 0x83, 0x14, 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x8D, 0x1F, 0xE8, 0xCF, 0x7C, 0x93, 0x4F, 0xF8, 0xD8, 0x22, 0xEB, 0xD2,
- 0xCE, 0x2B, 0x5C, 0x7C, 0x07, 0xC9, 0xBC, 0x16, 0x6C, 0xD5, 0x3F, 0x1B, 0x03, 0xF5, 0xCD, 0x61,
- 0x6B, 0x29, 0x40, 0x5D, 0x99, 0xAF, 0xD1, 0x7D, 0x3B, 0x86, 0x63, 0x7C, 0x72, 0x71, 0x1F, 0x30,
- 0x73, 0x85, 0x8A, 0x6D, 0x4E, 0x18, 0xA1, 0xC1, 0xFE, 0x37, 0xC2, 0x63, 0xAC, 0x57, 0x78, 0x6E,
- 0x12, 0x5D, 0xBE, 0xDA, 0xF0, 0x46, 0x07, 0x19, 0x90, 0x1A, 0x5E, 0x8F, 0x97, 0x97, 0x79, 0x99,
- };
- void muxrand_stir(char *array);
- void muxrand_keyed_stir(char *array, char key);
- uint8_t muxrand8(void);
- uint16_t muxrand16(void);
- uint32_t muxrand32(void);
- uint64_t muxrand64(void);
- #define ROUNDS 16
- #define ROUND_WARMUP_CYCLES 9973 // largest prime < 10,000
- #define KEY_WARMUP_CYCLES 997 // largest prime < 1,000
- #define copy(x,y) memcpy(x, y, 256);
- typedef struct {
- char *mask_key;
- char *perm_key;
- char *subst_key;
- } chaitin_round_key;
- typedef struct {
- char *bytes;
- int size;
- } user_key;
- void muxrand_warmup(char *array, int cycles);
- // Cipher:
- chaitin_round_key *chaitin_init(user_key u);
- void chaitin_mask(char *array, char *mask_key);
- void chaitin_subst(char *array, char *subst_key);
- void chaitin_perm(char *array, char *perm_key);
- void chaitin_enc(char *input_msg, chaitin_round_key *full_key);
- void chaitin_dec(char *input_msg, chaitin_round_key *full_key);
- void chaitin_enc_round(char *input_msg, char *mask_key, char *perm_key, char *subst_key);
- void chaitin_dec_round(char *input_msg, char *mask_key, char *perm_key, char *subst_key);
- void chaitin_inv(char *perm);
- void enc_key_gen(void);
- void dec_key_gen(void);
- char *enc_round(char *input_msg, char *mask_key, char *perm_key, char *subst_key);
- char *dec_round(char *input_msg, char *mask_key, char *perm_key, char *subst_key);
- char *enc(char *input_msg, char *mask_key, char *perm_key, char *subst_key);
- char *dec(char *input_msg, char *mask_key, char *perm_key, char *subst_key);
- void shuffle256(char *array, char *shuffle_array);
- // user key width
- // user key
- //
- // invert permutation
- // round_mask
- // round_permute
- // round_substitute
- extern char perm_table[256];
- extern char subst_table[256];
- char perm_table[] = {
- 0x01, 0x57, 0x31, 0x0c, 0xb0, 0xb2, 0x66, 0xa6, 0x79, 0xc1, 0x06, 0x54, 0xf9, 0xe6, 0x2c, 0xa3,
- 0x0e, 0xc5, 0xd5, 0xb5, 0xa1, 0x55, 0xda, 0x50, 0x40, 0xef, 0x18, 0xe2, 0xec, 0x8e, 0x26, 0xc8,
- 0x6e, 0xb1, 0x68, 0x67, 0x8d, 0xfd, 0xff, 0x32, 0x4d, 0x65, 0x51, 0x12, 0x2d, 0x60, 0x1f, 0xde,
- 0x19, 0x6b, 0xbe, 0x46, 0x56, 0xed, 0xf0, 0x22, 0x48, 0xf2, 0x14, 0xd6, 0xf4, 0xe3, 0x95, 0xeb,
- 0x61, 0xea, 0x39, 0x16, 0x3c, 0xfa, 0x52, 0xaf, 0xd0, 0x05, 0x7f, 0xc7, 0x6f, 0x3e, 0x87, 0xf8,
- 0xae, 0xa9, 0xd3, 0x3a, 0x42, 0x9a, 0x6a, 0xc3, 0xf5, 0xab, 0x11, 0xbb, 0xb6, 0xb3, 0x00, 0xf3,
- 0x84, 0x38, 0x94, 0x4b, 0x80, 0x85, 0x9e, 0x64, 0x82, 0x7e, 0x5b, 0x0d, 0x99, 0xf6, 0xd8, 0xdb,
- 0x77, 0x44, 0xdf, 0x4e, 0x53, 0x58, 0xc9, 0x63, 0x7a, 0x0b, 0x5c, 0x20, 0x88, 0x72, 0x34, 0x0a,
- 0x8a, 0x1e, 0x30, 0xb7, 0x9c, 0x23, 0x3d, 0x1a, 0x8f, 0x4a, 0xfb, 0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 0x64, 0x35, 0x0f, 0x6e, 0x55, 0x89, 0xe3, 0x5a, 0x62, 0x50, 0xdd, 0x2b, 0x57, 0x26, 0xff,
- };
- //char *full_key;
- //chaitin_key full_key;
- user_key u;
- extern char test_key[16];
- //char test_key[] = {
- // 0x00, 0x01, 0x02, 0x03,
- // 0x04, 0x05, 0x06, 0x07,
- // 0x08, 0x09, 0x0a, 0x0b,
- // 0x0c, 0x0d, 0x0e, 0x0f, };
- char test_key[] = {
- 0x02, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00,};
- char test_plaintext[256] = {
- // 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
- // 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
- // 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
- // 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
- // 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
- // 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
- // 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
- // 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 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0x69, 0x74, 0x20, 0x69,
- 0x6D, 0x70, 0x65, 0x64, 0x69, 0x6D, 0x65, 0x6E, 0x74, 0x73, 0x2E, 0x20, 0x4C, 0x6F, 0x76, 0x65,
- 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F, 0x74, 0x20, 0x6C, 0x6F, 0x76, 0x65, 0x0A, 0x57, 0x68, 0x69,
- 0x63, 0x68, 0x20, 0x61, 0x6C, 0x74, 0x65, 0x72, 0x73, 0x20, 0x77, 0x68, 0x65, 0x6E, 0x20, 0x69,
- 0x74, 0x20, 0x61, 0x6C, 0x74, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x66, 0x69, 0x6E,
- 0x64, 0x73, 0x2C, 0x0A, 0x4F, 0x72, 0x20, 0x62, 0x65, 0x6E, 0x64, 0x73, 0x20, 0x77, 0x69, 0x74,
- 0x68, 0x20, 0x74, 0x68, 0x65, 0x20, 0x72, 0x65, 0x6D, 0x6F, 0x76, 0x65, 0x72, 0x20, 0x74, 0x6F,
- 0x20, 0x72, 0x65, 0x6D, 0x6F, 0x76, 0x65, 0x3A, 0x0A, 0x4F, 0x20, 0x6E, 0x6F, 0x3B, 0x20, 0x69,
- 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x6E, 0x20, 0x65, 0x76, 0x65, 0x72, 0x2D, 0x66, 0x69, 0x78,
- 0x65, 0x64, 0x20, 0x6D, 0x61, 0x72, 0x6B, 0x2C, 0x20, 0x0A, 0x54, 0x68, 0x61, 0x74, 0x20, 0x6C,
- 0x6F, 0x6F, 0x6B, 0x73, 0x20, 0x6F, 0x6E, 0x20, 0x74, 0x65, 0x6D, 0x70, 0x65, 0x73, 0x74, 0x73,
- 0x2C, 0x20, 0x61, 0x6E, 0x64, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x65, 0x76, 0x65, 0x72, 0x20, 0x73,
- 0x68, 0x61, 0x6B, 0x65, 0x6E, 0x3B, 0x0A, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65,
- 0x20, 0x73, 0x74, 0x61, 0x72, 0x20, 0x74, 0x6F, 0x20, 0x65, 0x76, 0x65, 0x72, 0x79, 0x20, 0x77,
- };
- //
- //
- chaitin_round_key *chaitin_init(user_key u){
- chaitin_round_key *full_key = malloc(ROUNDS * sizeof(chaitin_round_key));
- int i,j;
- for(i=0;i<ROUNDS;i++){
- full_key[i].mask_key = malloc(256);
- full_key[i].perm_key = malloc(256);
- full_key[i].subst_key = malloc(256);
- }
- for(i=0;i<u.size;i++){
- muxrand_keyed_stir(muxrand_array, u.bytes[i]);
- muxrand_warmup(muxrand_array, KEY_WARMUP_CYCLES);
- }
- for(i=0; i<ROUNDS; i++){
- for(j=0; j<256; j++){
- full_key[i].mask_key[j] = muxrand8();
- full_key[i].perm_key[j] = muxrand8();
- full_key[i].subst_key[j] = muxrand8();
- }
- }
- for(i=0; i<ROUNDS; i++){
- shuffle256(perm_table, full_key[i].perm_key);
- copy(full_key[i].perm_key, perm_table);
- }
- for(i=0; i<ROUNDS; i++){
- shuffle256(subst_table, full_key[i].subst_key);
- copy(full_key[i].subst_key, subst_table);
- }
- return full_key;
- }
- //
- //
- void shuffle256(char *array, char *shuffle_array){
- int i;
- unsigned char swapA, swapB;
- for(i=0;i<256;i++){
- swapA = array[i];
- swapB = shuffle_array[i];
- array[i] = array[swapB];
- array[swapB] = swapA;
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- void main(void){
- int i,j,k;
- user_key u;
- u.size = 16;
- u.bytes = test_key;
- // shuffle256(test_plaintext, muxrand_array);
- chaitin_round_key *full_key = chaitin_init(u);
- // for(i=0;i<16;i++){
- // for(j=0;j<16;j++){
- // printf("%02x ", (unsigned char)full_key[0].perm_key[i*16 + j]);
- // }
- // printf("\n");
- // }
- //_die;
- chaitin_enc(test_plaintext, full_key);
- // chaitin_enc(test_plaintext, full_key);
- chaitin_dec(test_plaintext, full_key);
- for(i=0;i<16;i++){
- for(j=0;j<16;j++){
- printf("%02x ", (unsigned char)test_plaintext[i*16 + j]);
- }
- printf("\n");
- }
- // for(i=0;i<ROUNDS;i++){
- // for(j=0;j<16;j++){
- // for(k=0;k<16;k++){
- // printf("%02x ", (unsigned char)full_key[i].mask_key[j*16 + k]);
- // }
- // printf("\n");
- // }
- // printf("\n");
- // }
- // //keyed_muxrand_stir(muxrand_array, 0x73);
- //// for(i=0;i<1000;i++){
- //// muxrand_stir(muxrand_array);
- //// }
- ////
- //// for(i=0;i<16;i++){
- //// for(j=0;j<16;j++){
- //// printf("%02x ", (unsigned char)muxrand_array[i*16 + j]);
- //// }
- //// printf("\n");
- //// }
- //
- // for(i=0;i<256;i++){
- // printf("%02x ", muxrand8());
- // }
- }
- //char *mask_key, char *perm_key, char *subst_key){
- void chaitin_enc(char *input_msg, chaitin_round_key *full_key){
- int i;
- for(i=0;i<ROUNDS;i++){
- chaitin_enc_round(input_msg, full_key[i].mask_key, full_key[i].perm_key, full_key[i].subst_key);
- }
- }
- void chaitin_dec(char *input_msg, chaitin_round_key *full_key){
- int i;
- for(i=0;i<ROUNDS;i++){
- chaitin_inv(full_key[i].perm_key);
- chaitin_inv(full_key[i].subst_key);
- }
- for(i=ROUNDS-1;i>=0;i--){
- chaitin_dec_round(input_msg, full_key[i].mask_key, full_key[i].perm_key, full_key[i].subst_key);
- }
- }
- void chaitin_enc_round(char *input_msg, char *mask_key, char *perm_key, char *subst_key){
- chaitin_mask(input_msg, mask_key);
- chaitin_perm(input_msg, perm_key);
- chaitin_subst(input_msg, subst_key);
- }
- void chaitin_dec_round(char *input_msg, char *mask_key, char *perm_key, char *subst_key){
- chaitin_subst(input_msg, subst_key);
- chaitin_perm(input_msg, perm_key);
- chaitin_mask(input_msg, mask_key);
- }
- void chaitin_mask(char *array, char *mask_key){
- int i;
- for(i=0;i<256;i++){
- array[i] ^= mask_key[i];
- }
- }
- void chaitin_perm(char *array, char *perm_key){
- unsigned char temp_array[256];
- int i;
- for(i=0;i<256;i++){
- temp_array[i] = array[(unsigned char)perm_key[i]];
- }
- for(i=0;i<256;i++){
- array[i] = temp_array[i];
- }
- }
- void chaitin_subst(char *array, char *subst_key){
- int i;
- for(i=0;i<256;i++){
- array[i] = subst_key[(unsigned char)array[i]];
- }
- }
- void chaitin_inv(char *perm){
- unsigned char temp_perm[256];
- int i;
- for(i=0;i<256;i++){
- temp_perm[(unsigned char)perm[i]] = (unsigned char)i;
- }
- for(i=0;i<256;i++){
- perm[i] = temp_perm[i];
- }
- }
- uint8_t muxrand8(void){
- static p=0;
- p=p+1 & 0xff;
- muxrand_stir(muxrand_array);
- return (uint8_t)muxrand_array[p];
- }
- uint16_t muxrand16(void){
- static p=0;
- p=p+1 & 0x7f;
- muxrand_stir(muxrand_array);
- return *((uint16_t*)muxrand_array+p);
- }
- uint32_t muxrand32(void){
- static p=0;
- p=p+1 & 0x3f;
- muxrand_stir(muxrand_array);
- return *((uint32_t*)muxrand_array+p);
- }
- uint64_t muxrand64(void){
- static p=0;
- p=p+1 & 0x1f;
- muxrand_stir(muxrand_array);
- return *((uint64_t*)muxrand_array+p);
- }
- void muxrand_warmup(char *array, int cycles){
- while(cycles--){
- muxrand_stir(array);
- }
- }
- void muxrand_stir(char *array){
- int i,j,k;
- unsigned char a[3];
- unsigned char b[3];
- unsigned char c;
- unsigned char temp_array[256];
- for(i=0;i<256;i++){
- a[0] = array[(i+255)&0xff];
- a[1] = array[(i+127)&0xff];
- a[2] = array[(i+63 )&0xff];
- b[0] = mux8(a[0],a[1],a[2]);
- a[0] = array[(i+31 )&0xff];
- a[1] = array[(i+15 )&0xff];
- a[2] = array[(i+7 )&0xff];
- b[1] = mux8(a[0],a[1],a[2]);
- a[0] = array[(i+3 )&0xff];
- a[1] = array[(i+2 )&0xff];
- a[2] = array[(i+1 )&0xff];
- b[2] = mux8(a[0],a[1],a[2]);
- temp_array[i] = array[i] ^ mux8(b[0],b[1],b[2]);
- }
- for(i=0;i<256;i++){
- array[i]=temp_array[i];
- }
- }
- void muxrand_keyed_stir(char *array, char key){
- int i,j,k;
- unsigned char a[3];
- unsigned char b[3];
- unsigned char c;
- unsigned char temp_array[256];
- for(i=0;i<256;i++){
- a[0] = array[(i+255)&0xff];
- a[1] = array[(i+127)&0xff];
- a[2] = array[(i+63 )&0xff];
- b[0] = mux8(a[0],a[1],a[2]);
- a[0] = array[(i+31 )&0xff];
- a[1] = array[(i+15 )&0xff];
- a[2] = array[(i+7 )&0xff];
- b[1] = mux8(a[0],a[1],a[2]);
- a[0] = array[(i+3 )&0xff];
- a[1] = array[(i+2 )&0xff];
- a[2] = array[(i+1 )&0xff];
- b[2] = mux8(a[0],a[1],a[2]);
- temp_array[i] = array[i] ^ mux8(b[0],b[1],b[2]);
- temp_array[i] ^= key;
- //temp_array[i] = key ^ mux8(b[0],b[1],b[2]);
- //temp_array[i] = array[i] ^ mux8(b[0],b[1],key);
- }
- for(i=0;i<256;i++){
- array[i]=temp_array[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement