Advertisement
Guest User

chaitin.c

a guest
Aug 28th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.38 KB | None | 0 0
  1. // chaitin.c
  2. //
  3. // Author: Clayton Bauman, 2014
  4. // License: BSD License
  5. //
  6. // This cipher is named after Gregory Chaitin, a mathematical hero of mine.
  7. // The ideas of Algorithmic Information Theory have affected every aspect of
  8. // my thinking, including my ideas about encryption. The Chaitin cipher seeks
  9. // to build substantial entropy into the operation of the cipher, without
  10. // introducing the trust issues that arise when encoding arbitrary constants
  11. // into a cipher, as well as avoiding common cipher mistakes, such as
  12. // vulnerability to linear and differential cryptanalysis and side-channel
  13. // attacks.
  14. //
  15. // "Anyone who considers arithmetical methods of producing random digits is, of
  16. // course, in a state of sin."
  17. // - John von Neumann
  18. //
  19. // "But when a rule is very complex what conforms to it passes for random."
  20. // - Gottfried Leibniz
  21.  
  22.  
  23. // Get user key, size N bytes
  24. // Key warm-up:
  25. // for i in 0 to N-1:
  26. // keyed_muxrand_stir(N_i)
  27. // muxrand_stir 1000 times
  28. // Key schedule
  29. // Create Chaitin key (12,288 bytes, with muxrand8)
  30. // key[0]->perm_key = shuffle(perm_table, key[0]->perm_key)
  31. // key[0]->subst_key = shuffle(subst_table, key[0]->subst_key)
  32. // For round in 1 to 15:
  33. // key[round]->perm_key = shuffle(key[round-1]->perm_key, key[round]->perm_key)
  34. // key[round]->subst_key = shuffle(key[round-1]->subst_key, key[round]->subst_key)
  35. //
  36. // for input:
  37. // enc:
  38. // For round in 0 to 15:
  39. // masked = input XOR key[round]->mask
  40. // permed = permute(masked, key[round]->perm_key)
  41. // substd = subst(permed, key[round]->subst_key)
  42. // input = substd
  43. //
  44. // dec:
  45. // For round in 15 to 0:
  46. // substd = subst(input, inv(key[round]->subst_key))
  47. // permed = permute(substd, inv(key[round]->perm_key))
  48. // masked = permed XOR key[round]->mask
  49. // intput = masked
  50.  
  51. #include "stdint.h"
  52. #include "stdio.h"
  53. #include "stdlib.h"
  54. #include "string.h"
  55.  
  56. #define _die fprintf(stderr, "Died at %s line %d\n", __FILE__, __LINE__); exit(1); // die#
  57. #define _trace fprintf(stderr, "%s() in %s line %d\n", __func__, __FILE__, __LINE__); // trace#
  58.  
  59. #define mux8(A,B,S) ((A&S)|(B&~S))
  60.  
  61. char muxrand_array[256] = {
  62. 0xC4, 0x83, 0x14, 0x37, 0xB8, 0xF8, 0xBA, 0xCC, 0xE3, 0xC1, 0x32, 0xEC, 0x13, 0x34, 0xF1, 0xB4,
  63. 0x18, 0xE0, 0x26, 0x8E, 0x48, 0xA0, 0x4E, 0xCA, 0x54, 0xBB, 0xDA, 0x3C, 0x74, 0x05, 0x95, 0x88,
  64. 0xCA, 0x7E, 0xC2, 0x36, 0xF6, 0x87, 0x8C, 0x03, 0xAF, 0x4F, 0xB5, 0x1E, 0x79, 0xB0, 0x73, 0x4C,
  65. 0x17, 0x40, 0xB7, 0xFF, 0xA7, 0xF2, 0xF7, 0x74, 0x28, 0x52, 0xCC, 0xB2, 0xC0, 0xC8, 0x17, 0x4A,
  66. 0xC6, 0xB9, 0xFB, 0x3C, 0xD5, 0x99, 0x2F, 0xD1, 0x00, 0x74, 0xD1, 0x77, 0x66, 0x47, 0xBF, 0x3F,
  67. 0xD4, 0xC7, 0x3B, 0x93, 0x52, 0x19, 0xD9, 0xD2, 0xFD, 0xA9, 0x60, 0xEE, 0x68, 0xDD, 0xD9, 0xB3,
  68. 0xD3, 0xC0, 0xBC, 0x8F, 0x8B, 0xDB, 0xEA, 0x52, 0xB6, 0x0C, 0x7F, 0x16, 0xB2, 0x5E, 0xFA, 0x7B,
  69. 0x8D, 0x18, 0xC8, 0x49, 0x7D, 0xD7, 0x2D, 0xD3, 0x19, 0x82, 0x19, 0x35, 0xEE, 0x1E, 0x5E, 0x7B,
  70. 0x04, 0xC6, 0xB1, 0xF3, 0xAE, 0x11, 0x7B, 0xF4, 0xA9, 0xA3, 0x31, 0x3E, 0x3B, 0x7E, 0x44, 0x01,
  71. 0x4A, 0x62, 0x0C, 0x20, 0xFA, 0x0C, 0x7F, 0xC0, 0xFC, 0x82, 0x14, 0xAC, 0x64, 0x0A, 0xA3, 0x9E,
  72. 0x21, 0x89, 0x51, 0x37, 0x7C, 0x2D, 0x4D, 0xAB, 0x7D, 0x01, 0xBD, 0xA5, 0xDC, 0x03, 0x8B, 0xC1,
  73. 0x40, 0xD0, 0x88, 0x64, 0x8D, 0x1F, 0xE8, 0xCF, 0x7C, 0x93, 0x4F, 0xF8, 0xD8, 0x22, 0xEB, 0xD2,
  74. 0xCE, 0x2B, 0x5C, 0x7C, 0x07, 0xC9, 0xBC, 0x16, 0x6C, 0xD5, 0x3F, 0x1B, 0x03, 0xF5, 0xCD, 0x61,
  75. 0x6B, 0x29, 0x40, 0x5D, 0x99, 0xAF, 0xD1, 0x7D, 0x3B, 0x86, 0x63, 0x7C, 0x72, 0x71, 0x1F, 0x30,
  76. 0x73, 0x85, 0x8A, 0x6D, 0x4E, 0x18, 0xA1, 0xC1, 0xFE, 0x37, 0xC2, 0x63, 0xAC, 0x57, 0x78, 0x6E,
  77. 0x12, 0x5D, 0xBE, 0xDA, 0xF0, 0x46, 0x07, 0x19, 0x90, 0x1A, 0x5E, 0x8F, 0x97, 0x97, 0x79, 0x99,
  78. };
  79.  
  80. void muxrand_stir(char *array);
  81. void muxrand_keyed_stir(char *array, char key);
  82.  
  83. uint8_t muxrand8(void);
  84. uint16_t muxrand16(void);
  85. uint32_t muxrand32(void);
  86. uint64_t muxrand64(void);
  87.  
  88. #define ROUNDS 16
  89. #define ROUND_WARMUP_CYCLES 9973 // largest prime < 10,000
  90. #define KEY_WARMUP_CYCLES 997 // largest prime < 1,000
  91.  
  92. #define copy(x,y) memcpy(x, y, 256);
  93.  
  94. typedef struct {
  95. char *mask_key;
  96. char *perm_key;
  97. char *subst_key;
  98. } chaitin_round_key;
  99.  
  100. typedef struct {
  101. char *bytes;
  102. int size;
  103. } user_key;
  104.  
  105. void muxrand_warmup(char *array, int cycles);
  106.  
  107. // Cipher:
  108. chaitin_round_key *chaitin_init(user_key u);
  109. void chaitin_mask(char *array, char *mask_key);
  110. void chaitin_subst(char *array, char *subst_key);
  111. void chaitin_perm(char *array, char *perm_key);
  112. void chaitin_enc(char *input_msg, chaitin_round_key *full_key);
  113. void chaitin_dec(char *input_msg, chaitin_round_key *full_key);
  114. void chaitin_enc_round(char *input_msg, char *mask_key, char *perm_key, char *subst_key);
  115. void chaitin_dec_round(char *input_msg, char *mask_key, char *perm_key, char *subst_key);
  116. void chaitin_inv(char *perm);
  117.  
  118. void enc_key_gen(void);
  119. void dec_key_gen(void);
  120.  
  121. char *enc_round(char *input_msg, char *mask_key, char *perm_key, char *subst_key);
  122. char *dec_round(char *input_msg, char *mask_key, char *perm_key, char *subst_key);
  123.  
  124. char *enc(char *input_msg, char *mask_key, char *perm_key, char *subst_key);
  125. char *dec(char *input_msg, char *mask_key, char *perm_key, char *subst_key);
  126.  
  127. void shuffle256(char *array, char *shuffle_array);
  128.  
  129. // user key width
  130. // user key
  131. //
  132. // invert permutation
  133. // round_mask
  134. // round_permute
  135. // round_substitute
  136.  
  137. extern char perm_table[256];
  138. extern char subst_table[256];
  139.  
  140. char perm_table[] = {
  141. 0x01, 0x57, 0x31, 0x0c, 0xb0, 0xb2, 0x66, 0xa6, 0x79, 0xc1, 0x06, 0x54, 0xf9, 0xe6, 0x2c, 0xa3,
  142. 0x0e, 0xc5, 0xd5, 0xb5, 0xa1, 0x55, 0xda, 0x50, 0x40, 0xef, 0x18, 0xe2, 0xec, 0x8e, 0x26, 0xc8,
  143. 0x6e, 0xb1, 0x68, 0x67, 0x8d, 0xfd, 0xff, 0x32, 0x4d, 0x65, 0x51, 0x12, 0x2d, 0x60, 0x1f, 0xde,
  144. 0x19, 0x6b, 0xbe, 0x46, 0x56, 0xed, 0xf0, 0x22, 0x48, 0xf2, 0x14, 0xd6, 0xf4, 0xe3, 0x95, 0xeb,
  145. 0x61, 0xea, 0x39, 0x16, 0x3c, 0xfa, 0x52, 0xaf, 0xd0, 0x05, 0x7f, 0xc7, 0x6f, 0x3e, 0x87, 0xf8,
  146. 0xae, 0xa9, 0xd3, 0x3a, 0x42, 0x9a, 0x6a, 0xc3, 0xf5, 0xab, 0x11, 0xbb, 0xb6, 0xb3, 0x00, 0xf3,
  147. 0x84, 0x38, 0x94, 0x4b, 0x80, 0x85, 0x9e, 0x64, 0x82, 0x7e, 0x5b, 0x0d, 0x99, 0xf6, 0xd8, 0xdb,
  148. 0x77, 0x44, 0xdf, 0x4e, 0x53, 0x58, 0xc9, 0x63, 0x7a, 0x0b, 0x5c, 0x20, 0x88, 0x72, 0x34, 0x0a,
  149. 0x8a, 0x1e, 0x30, 0xb7, 0x9c, 0x23, 0x3d, 0x1a, 0x8f, 0x4a, 0xfb, 0x5e, 0x81, 0xa2, 0x3f, 0x98,
  150. 0xaa, 0x07, 0x73, 0xa7, 0xf1, 0xce, 0x03, 0x96, 0x37, 0x3b, 0x97, 0xdc, 0x5a, 0x35, 0x17, 0x83,
  151. 0x7d, 0xad, 0x0f, 0xee, 0x4f, 0x5f, 0x59, 0x10, 0x69, 0x89, 0xe1, 0xe0, 0xd9, 0xa0, 0x25, 0x7b,
  152. 0x76, 0x49, 0x02, 0x9d, 0x2e, 0x74, 0x09, 0x91, 0x86, 0xe4, 0xcf, 0xd4, 0xca, 0xd7, 0x45, 0xe5,
  153. 0x1b, 0xbc, 0x43, 0x7c, 0xa8, 0xfc, 0x2a, 0x04, 0x1d, 0x6c, 0x15, 0xf7, 0x13, 0xcd, 0x27, 0xcb,
  154. 0xe9, 0x28, 0xba, 0x93, 0xc6, 0xc0, 0x9b, 0x21, 0xa4, 0xbf, 0x62, 0xcc, 0xa5, 0xb4, 0x75, 0x4c,
  155. 0x8c, 0x24, 0xd2, 0xac, 0x29, 0x36, 0x9f, 0x08, 0xb9, 0xe8, 0x71, 0xc4, 0xe7, 0x2f, 0x92, 0x78,
  156. 0x33, 0x41, 0x1c, 0x90, 0xfe, 0xdd, 0x5d, 0xbd, 0xc2, 0x8b, 0x70, 0x2b, 0x47, 0x6d, 0xb8, 0xd1,
  157. };
  158.  
  159.  
  160. char subst_table[] = {
  161. 0x5b, 0x32, 0x81, 0x52, 0x37, 0xfe, 0xec, 0xa9, 0x49, 0x29, 0xfd, 0x4a, 0xe4, 0x20, 0x19, 0xd4,
  162. 0x6c, 0xe9, 0x70, 0x4d, 0x99, 0x13, 0x95, 0xb2, 0xb4, 0x72, 0xbb, 0x7b, 0x5e, 0x77, 0x11, 0x31,
  163. 0xa8, 0xef, 0x3d, 0xaf, 0xc6, 0x98, 0xc7, 0x92, 0x5c, 0x39, 0xa2, 0xc4, 0x7d, 0xf6, 0x8a, 0x0a,
  164. 0xd8, 0xb0, 0xca, 0xf7, 0x47, 0xa3, 0x56, 0xea, 0x75, 0x9d, 0x59, 0x93, 0x44, 0x8c, 0xf9, 0xad,
  165. 0x21, 0xfc, 0x7e, 0xe6, 0x40, 0xe1, 0x3b, 0xed, 0x60, 0x06, 0x25, 0x73, 0x2d, 0x3e, 0x09, 0x76,
  166. 0xda, 0x5d, 0x48, 0xe0, 0xbf, 0x0d, 0x03, 0x1a, 0x9f, 0xb6, 0xdb, 0x7f, 0x91, 0x94, 0xc9, 0xde,
  167. 0xeb, 0xd3, 0x10, 0x84, 0x7c, 0x36, 0xb3, 0xcb, 0x22, 0xb1, 0x14, 0x61, 0x01, 0xe2, 0xac, 0x74,
  168. 0xa1, 0xd5, 0xa4, 0xf0, 0x3c, 0x53, 0x1b, 0xba, 0x6f, 0x0e, 0xae, 0x78, 0x08, 0x88, 0xd0, 0xdf,
  169. 0xc1, 0x12, 0x85, 0x05, 0x86, 0xf1, 0x97, 0x0c, 0x5f, 0x38, 0x18, 0xee, 0xbe, 0xe5, 0x9b, 0x68,
  170. 0xcf, 0x2e, 0x4e, 0x1c, 0xfb, 0x1f, 0x16, 0x3f, 0x1e, 0x54, 0xc8, 0xd7, 0xaa, 0x8d, 0x6b, 0xf5,
  171. 0x00, 0xab, 0x28, 0xc2, 0x2c, 0xa6, 0x4b, 0xa5, 0x2a, 0x3a, 0xe7, 0xa7, 0x8e, 0xe8, 0x58, 0xf2,
  172. 0x80, 0x87, 0xbc, 0x6a, 0x33, 0x82, 0x9a, 0x67, 0x4f, 0xb8, 0x43, 0xf8, 0x27, 0x9e, 0x41, 0xc5,
  173. 0x65, 0xf4, 0xd1, 0x1d, 0x63, 0x45, 0x8b, 0x9c, 0x90, 0xcd, 0x34, 0x51, 0x17, 0xc3, 0x24, 0x46,
  174. 0xcc, 0x07, 0xdc, 0x15, 0xd9, 0xa0, 0xd6, 0x71, 0xd2, 0x66, 0xce, 0x79, 0x42, 0x6d, 0x04, 0x0b,
  175. 0xc0, 0x4c, 0x96, 0x30, 0xfa, 0x83, 0x69, 0xb7, 0xbd, 0xf3, 0xb5, 0x8f, 0xb9, 0x2f, 0x02, 0x7a,
  176. 0x23, 0x64, 0x35, 0x0f, 0x6e, 0x55, 0x89, 0xe3, 0x5a, 0x62, 0x50, 0xdd, 0x2b, 0x57, 0x26, 0xff,
  177. };
  178.  
  179. //char *full_key;
  180. //chaitin_key full_key;
  181. user_key u;
  182.  
  183. extern char test_key[16];
  184.  
  185. //char test_key[] = {
  186. // 0x00, 0x01, 0x02, 0x03,
  187. // 0x04, 0x05, 0x06, 0x07,
  188. // 0x08, 0x09, 0x0a, 0x0b,
  189. // 0x0c, 0x0d, 0x0e, 0x0f, };
  190.  
  191. char test_key[] = {
  192. 0x02, 0x00, 0x00, 0x00,
  193. 0x00, 0x00, 0x00, 0x00,
  194. 0x00, 0x00, 0x00, 0x00,
  195. 0x00, 0x00, 0x00, 0x00,};
  196.  
  197. char test_plaintext[256] = {
  198.  
  199. // 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
  200. // 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  201. // 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  202. // 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  203. // 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  204. // 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  205. // 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  206. // 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
  207. // 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  208. // 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  209. // 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
  210. // 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  211. // 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  212. // 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
  213. // 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  214. // 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
  215.  
  216. 0x4C, 0x65, 0x74, 0x20, 0x6D, 0x65, 0x20, 0x6E, 0x6F, 0x74, 0x20, 0x74, 0x6F, 0x20, 0x74, 0x68,
  217. 0x65, 0x20, 0x6D, 0x61, 0x72, 0x72, 0x69, 0x61, 0x67, 0x65, 0x20, 0x6F, 0x66, 0x20, 0x74, 0x72,
  218. 0x75, 0x65, 0x20, 0x6D, 0x69, 0x6E, 0x64, 0x73, 0x0A, 0x41, 0x64, 0x6D, 0x69, 0x74, 0x20, 0x69,
  219. 0x6D, 0x70, 0x65, 0x64, 0x69, 0x6D, 0x65, 0x6E, 0x74, 0x73, 0x2E, 0x20, 0x4C, 0x6F, 0x76, 0x65,
  220. 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F, 0x74, 0x20, 0x6C, 0x6F, 0x76, 0x65, 0x0A, 0x57, 0x68, 0x69,
  221. 0x63, 0x68, 0x20, 0x61, 0x6C, 0x74, 0x65, 0x72, 0x73, 0x20, 0x77, 0x68, 0x65, 0x6E, 0x20, 0x69,
  222. 0x74, 0x20, 0x61, 0x6C, 0x74, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x66, 0x69, 0x6E,
  223. 0x64, 0x73, 0x2C, 0x0A, 0x4F, 0x72, 0x20, 0x62, 0x65, 0x6E, 0x64, 0x73, 0x20, 0x77, 0x69, 0x74,
  224. 0x68, 0x20, 0x74, 0x68, 0x65, 0x20, 0x72, 0x65, 0x6D, 0x6F, 0x76, 0x65, 0x72, 0x20, 0x74, 0x6F,
  225. 0x20, 0x72, 0x65, 0x6D, 0x6F, 0x76, 0x65, 0x3A, 0x0A, 0x4F, 0x20, 0x6E, 0x6F, 0x3B, 0x20, 0x69,
  226. 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x6E, 0x20, 0x65, 0x76, 0x65, 0x72, 0x2D, 0x66, 0x69, 0x78,
  227. 0x65, 0x64, 0x20, 0x6D, 0x61, 0x72, 0x6B, 0x2C, 0x20, 0x0A, 0x54, 0x68, 0x61, 0x74, 0x20, 0x6C,
  228. 0x6F, 0x6F, 0x6B, 0x73, 0x20, 0x6F, 0x6E, 0x20, 0x74, 0x65, 0x6D, 0x70, 0x65, 0x73, 0x74, 0x73,
  229. 0x2C, 0x20, 0x61, 0x6E, 0x64, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x65, 0x76, 0x65, 0x72, 0x20, 0x73,
  230. 0x68, 0x61, 0x6B, 0x65, 0x6E, 0x3B, 0x0A, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65,
  231. 0x20, 0x73, 0x74, 0x61, 0x72, 0x20, 0x74, 0x6F, 0x20, 0x65, 0x76, 0x65, 0x72, 0x79, 0x20, 0x77,
  232.  
  233. };
  234.  
  235.  
  236. //
  237. //
  238. chaitin_round_key *chaitin_init(user_key u){
  239.  
  240. chaitin_round_key *full_key = malloc(ROUNDS * sizeof(chaitin_round_key));
  241.  
  242. int i,j;
  243.  
  244. for(i=0;i<ROUNDS;i++){
  245. full_key[i].mask_key = malloc(256);
  246. full_key[i].perm_key = malloc(256);
  247. full_key[i].subst_key = malloc(256);
  248. }
  249.  
  250. for(i=0;i<u.size;i++){
  251. muxrand_keyed_stir(muxrand_array, u.bytes[i]);
  252. muxrand_warmup(muxrand_array, KEY_WARMUP_CYCLES);
  253. }
  254.  
  255. for(i=0; i<ROUNDS; i++){
  256. for(j=0; j<256; j++){
  257. full_key[i].mask_key[j] = muxrand8();
  258. full_key[i].perm_key[j] = muxrand8();
  259. full_key[i].subst_key[j] = muxrand8();
  260. }
  261. }
  262.  
  263. for(i=0; i<ROUNDS; i++){
  264. shuffle256(perm_table, full_key[i].perm_key);
  265. copy(full_key[i].perm_key, perm_table);
  266. }
  267.  
  268. for(i=0; i<ROUNDS; i++){
  269. shuffle256(subst_table, full_key[i].subst_key);
  270. copy(full_key[i].subst_key, subst_table);
  271. }
  272.  
  273. return full_key;
  274.  
  275. }
  276.  
  277.  
  278. //
  279. //
  280. void shuffle256(char *array, char *shuffle_array){
  281.  
  282. int i;
  283. unsigned char swapA, swapB;
  284.  
  285. for(i=0;i<256;i++){
  286.  
  287. swapA = array[i];
  288. swapB = shuffle_array[i];
  289.  
  290. array[i] = array[swapB];
  291. array[swapB] = swapA;
  292.  
  293. }
  294.  
  295. }
  296.  
  297.  
  298. //////////////////////////////////////////////////////////////////////////////
  299.  
  300. void main(void){
  301.  
  302. int i,j,k;
  303.  
  304. user_key u;
  305. u.size = 16;
  306. u.bytes = test_key;
  307.  
  308. // shuffle256(test_plaintext, muxrand_array);
  309.  
  310. chaitin_round_key *full_key = chaitin_init(u);
  311.  
  312. // for(i=0;i<16;i++){
  313. // for(j=0;j<16;j++){
  314. // printf("%02x ", (unsigned char)full_key[0].perm_key[i*16 + j]);
  315. // }
  316. // printf("\n");
  317. // }
  318. //_die;
  319.  
  320. chaitin_enc(test_plaintext, full_key);
  321. // chaitin_enc(test_plaintext, full_key);
  322. chaitin_dec(test_plaintext, full_key);
  323.  
  324. for(i=0;i<16;i++){
  325. for(j=0;j<16;j++){
  326. printf("%02x ", (unsigned char)test_plaintext[i*16 + j]);
  327. }
  328. printf("\n");
  329. }
  330.  
  331. // for(i=0;i<ROUNDS;i++){
  332. // for(j=0;j<16;j++){
  333. // for(k=0;k<16;k++){
  334. // printf("%02x ", (unsigned char)full_key[i].mask_key[j*16 + k]);
  335. // }
  336. // printf("\n");
  337. // }
  338. // printf("\n");
  339. // }
  340.  
  341. // //keyed_muxrand_stir(muxrand_array, 0x73);
  342. //// for(i=0;i<1000;i++){
  343. //// muxrand_stir(muxrand_array);
  344. //// }
  345. ////
  346. //// for(i=0;i<16;i++){
  347. //// for(j=0;j<16;j++){
  348. //// printf("%02x ", (unsigned char)muxrand_array[i*16 + j]);
  349. //// }
  350. //// printf("\n");
  351. //// }
  352. //
  353. // for(i=0;i<256;i++){
  354. // printf("%02x ", muxrand8());
  355. // }
  356.  
  357. }
  358.  
  359.  
  360. //char *mask_key, char *perm_key, char *subst_key){
  361. void chaitin_enc(char *input_msg, chaitin_round_key *full_key){
  362. int i;
  363. for(i=0;i<ROUNDS;i++){
  364. chaitin_enc_round(input_msg, full_key[i].mask_key, full_key[i].perm_key, full_key[i].subst_key);
  365. }
  366. }
  367.  
  368.  
  369. void chaitin_dec(char *input_msg, chaitin_round_key *full_key){
  370. int i;
  371. for(i=0;i<ROUNDS;i++){
  372. chaitin_inv(full_key[i].perm_key);
  373. chaitin_inv(full_key[i].subst_key);
  374. }
  375. for(i=ROUNDS-1;i>=0;i--){
  376. chaitin_dec_round(input_msg, full_key[i].mask_key, full_key[i].perm_key, full_key[i].subst_key);
  377. }
  378. }
  379.  
  380.  
  381. void chaitin_enc_round(char *input_msg, char *mask_key, char *perm_key, char *subst_key){
  382. chaitin_mask(input_msg, mask_key);
  383. chaitin_perm(input_msg, perm_key);
  384. chaitin_subst(input_msg, subst_key);
  385. }
  386.  
  387. void chaitin_dec_round(char *input_msg, char *mask_key, char *perm_key, char *subst_key){
  388. chaitin_subst(input_msg, subst_key);
  389. chaitin_perm(input_msg, perm_key);
  390. chaitin_mask(input_msg, mask_key);
  391. }
  392.  
  393.  
  394. void chaitin_mask(char *array, char *mask_key){
  395. int i;
  396. for(i=0;i<256;i++){
  397. array[i] ^= mask_key[i];
  398. }
  399. }
  400.  
  401. void chaitin_perm(char *array, char *perm_key){
  402. unsigned char temp_array[256];
  403. int i;
  404. for(i=0;i<256;i++){
  405. temp_array[i] = array[(unsigned char)perm_key[i]];
  406. }
  407. for(i=0;i<256;i++){
  408. array[i] = temp_array[i];
  409. }
  410. }
  411.  
  412. void chaitin_subst(char *array, char *subst_key){
  413. int i;
  414. for(i=0;i<256;i++){
  415. array[i] = subst_key[(unsigned char)array[i]];
  416. }
  417. }
  418.  
  419. void chaitin_inv(char *perm){
  420. unsigned char temp_perm[256];
  421. int i;
  422. for(i=0;i<256;i++){
  423. temp_perm[(unsigned char)perm[i]] = (unsigned char)i;
  424. }
  425. for(i=0;i<256;i++){
  426. perm[i] = temp_perm[i];
  427. }
  428. }
  429.  
  430.  
  431. uint8_t muxrand8(void){
  432. static p=0;
  433. p=p+1 & 0xff;
  434. muxrand_stir(muxrand_array);
  435. return (uint8_t)muxrand_array[p];
  436. }
  437.  
  438. uint16_t muxrand16(void){
  439. static p=0;
  440. p=p+1 & 0x7f;
  441. muxrand_stir(muxrand_array);
  442. return *((uint16_t*)muxrand_array+p);
  443. }
  444.  
  445. uint32_t muxrand32(void){
  446. static p=0;
  447. p=p+1 & 0x3f;
  448. muxrand_stir(muxrand_array);
  449. return *((uint32_t*)muxrand_array+p);
  450. }
  451.  
  452. uint64_t muxrand64(void){
  453. static p=0;
  454. p=p+1 & 0x1f;
  455. muxrand_stir(muxrand_array);
  456. return *((uint64_t*)muxrand_array+p);
  457. }
  458.  
  459. void muxrand_warmup(char *array, int cycles){
  460. while(cycles--){
  461. muxrand_stir(array);
  462. }
  463. }
  464.  
  465. void muxrand_stir(char *array){
  466.  
  467. int i,j,k;
  468. unsigned char a[3];
  469. unsigned char b[3];
  470. unsigned char c;
  471. unsigned char temp_array[256];
  472.  
  473. for(i=0;i<256;i++){
  474.  
  475. a[0] = array[(i+255)&0xff];
  476. a[1] = array[(i+127)&0xff];
  477. a[2] = array[(i+63 )&0xff];
  478. b[0] = mux8(a[0],a[1],a[2]);
  479.  
  480. a[0] = array[(i+31 )&0xff];
  481. a[1] = array[(i+15 )&0xff];
  482. a[2] = array[(i+7 )&0xff];
  483. b[1] = mux8(a[0],a[1],a[2]);
  484.  
  485. a[0] = array[(i+3 )&0xff];
  486. a[1] = array[(i+2 )&0xff];
  487. a[2] = array[(i+1 )&0xff];
  488. b[2] = mux8(a[0],a[1],a[2]);
  489.  
  490. temp_array[i] = array[i] ^ mux8(b[0],b[1],b[2]);
  491.  
  492. }
  493.  
  494. for(i=0;i<256;i++){
  495. array[i]=temp_array[i];
  496. }
  497.  
  498. }
  499.  
  500. void muxrand_keyed_stir(char *array, char key){
  501.  
  502. int i,j,k;
  503. unsigned char a[3];
  504. unsigned char b[3];
  505. unsigned char c;
  506. unsigned char temp_array[256];
  507.  
  508. for(i=0;i<256;i++){
  509.  
  510. a[0] = array[(i+255)&0xff];
  511. a[1] = array[(i+127)&0xff];
  512. a[2] = array[(i+63 )&0xff];
  513. b[0] = mux8(a[0],a[1],a[2]);
  514.  
  515. a[0] = array[(i+31 )&0xff];
  516. a[1] = array[(i+15 )&0xff];
  517. a[2] = array[(i+7 )&0xff];
  518. b[1] = mux8(a[0],a[1],a[2]);
  519.  
  520. a[0] = array[(i+3 )&0xff];
  521. a[1] = array[(i+2 )&0xff];
  522. a[2] = array[(i+1 )&0xff];
  523. b[2] = mux8(a[0],a[1],a[2]);
  524.  
  525. temp_array[i] = array[i] ^ mux8(b[0],b[1],b[2]);
  526. temp_array[i] ^= key;
  527.  
  528. //temp_array[i] = key ^ mux8(b[0],b[1],b[2]);
  529. //temp_array[i] = array[i] ^ mux8(b[0],b[1],key);
  530.  
  531. }
  532.  
  533. for(i=0;i<256;i++){
  534. array[i]=temp_array[i];
  535. }
  536.  
  537. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement