noekeon.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. *
  9. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. /* Implementation of the Noekeon block cipher by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef NOEKEON
  14. const struct _cipher_descriptor noekeon_desc =
  15. {
  16. "noekeon",
  17. 16,
  18. 16, 16, 16, 16,
  19. &noekeon_setup,
  20. &noekeon_ecb_encrypt,
  21. &noekeon_ecb_decrypt,
  22. &noekeon_test,
  23. &noekeon_keysize
  24. };
  25. static const ulong32 RC[] = {
  26. 0x00000080UL, 0x0000001bUL, 0x00000036UL, 0x0000006cUL,
  27. 0x000000d8UL, 0x000000abUL, 0x0000004dUL, 0x0000009aUL,
  28. 0x0000002fUL, 0x0000005eUL, 0x000000bcUL, 0x00000063UL,
  29. 0x000000c6UL, 0x00000097UL, 0x00000035UL, 0x0000006aUL,
  30. 0x000000d4UL
  31. };
  32. #define kTHETA(a, b, c, d) \
  33. temp = a^c; temp = temp ^ ROL(temp, 8) ^ ROR(temp, 8); \
  34. b ^= temp; d ^= temp; \
  35. temp = b^d; temp = temp ^ ROL(temp, 8) ^ ROR(temp, 8); \
  36. a ^= temp; c ^= temp;
  37. #define THETA(k, a, b, c, d) \
  38. temp = a^c; temp = temp ^ ROL(temp, 8) ^ ROR(temp, 8); \
  39. b ^= temp ^ k[1]; d ^= temp ^ k[3]; \
  40. temp = b^d; temp = temp ^ ROL(temp, 8) ^ ROR(temp, 8); \
  41. a ^= temp ^ k[0]; c ^= temp ^ k[2];
  42. #define GAMMA(a, b, c, d) \
  43. b ^= ~(d|c); \
  44. a ^= c&b; \
  45. temp = d; d = a; a = temp;\
  46. c ^= a ^ b ^ d; \
  47. b ^= ~(d|c); \
  48. a ^= c&b;
  49. #define PI1(a, b, c, d) \
  50. a = ROL(a, 1); c = ROL(c, 5); d = ROL(d, 2);
  51. #define PI2(a, b, c, d) \
  52. a = ROR(a, 1); c = ROR(c, 5); d = ROR(d, 2);
  53. int noekeon_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
  54. {
  55. ulong32 temp;
  56. _ARGCHK(key != NULL);
  57. _ARGCHK(skey != NULL);
  58. if (keylen != 16) {
  59. return CRYPT_INVALID_KEYSIZE;
  60. }
  61. if (num_rounds != 16 && num_rounds != 0) {
  62. return CRYPT_INVALID_ROUNDS;
  63. }
  64. LOAD32H(skey->noekeon.K[0],&key[0]);
  65. LOAD32H(skey->noekeon.K[1],&key[4]);
  66. LOAD32H(skey->noekeon.K[2],&key[8]);
  67. LOAD32H(skey->noekeon.K[3],&key[12]);
  68. LOAD32H(skey->noekeon.dK[0],&key[0]);
  69. LOAD32H(skey->noekeon.dK[1],&key[4]);
  70. LOAD32H(skey->noekeon.dK[2],&key[8]);
  71. LOAD32H(skey->noekeon.dK[3],&key[12]);
  72. kTHETA(skey->noekeon.dK[0], skey->noekeon.dK[1], skey->noekeon.dK[2], skey->noekeon.dK[3]);
  73. return CRYPT_OK;
  74. }
  75. #ifdef CLEAN_STACK
  76. static void _noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
  77. #else
  78. void noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
  79. #endif
  80. {
  81. ulong32 a,b,c,d,temp;
  82. int r;
  83. _ARGCHK(key != NULL);
  84. _ARGCHK(pt != NULL);
  85. _ARGCHK(ct != NULL);
  86. LOAD32H(a,&pt[0]); LOAD32H(b,&pt[4]);
  87. LOAD32H(c,&pt[8]); LOAD32H(d,&pt[12]);
  88. #define ROUND(i) \
  89. a ^= RC[i]; \
  90. THETA(key->noekeon.K, a,b,c,d); \
  91. PI1(a,b,c,d); \
  92. GAMMA(a,b,c,d); \
  93. PI2(a,b,c,d);
  94. for (r = 0; r < 16; ++r) {
  95. ROUND(r);
  96. }
  97. #undef ROUND
  98. a ^= RC[16];
  99. THETA(key->noekeon.K, a, b, c, d);
  100. STORE32H(a,&ct[0]); STORE32H(b,&ct[4]);
  101. STORE32H(c,&ct[8]); STORE32H(d,&ct[12]);
  102. }
  103. #ifdef CLEAN_STACK
  104. void noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
  105. {
  106. _noekeon_ecb_encrypt(pt, ct, key);
  107. burn_stack(sizeof(ulong32) * 5 + sizeof(int));
  108. }
  109. #endif
  110. #ifdef CLEAN_STACK
  111. static void _noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
  112. #else
  113. void noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
  114. #endif
  115. {
  116. ulong32 a,b,c,d, temp;
  117. int r;
  118. _ARGCHK(key != NULL);
  119. _ARGCHK(pt != NULL);
  120. _ARGCHK(ct != NULL);
  121. LOAD32H(a,&ct[0]); LOAD32H(b,&ct[4]);
  122. LOAD32H(c,&ct[8]); LOAD32H(d,&ct[12]);
  123. #define ROUND(i) \
  124. THETA(key->noekeon.dK, a,b,c,d); \
  125. a ^= RC[i]; \
  126. PI1(a,b,c,d); \
  127. GAMMA(a,b,c,d); \
  128. PI2(a,b,c,d);
  129. for (r = 16; r > 0; --r) {
  130. ROUND(r);
  131. }
  132. #undef ROUND
  133. THETA(key->noekeon.dK, a,b,c,d);
  134. a ^= RC[0];
  135. STORE32H(a,&pt[0]); STORE32H(b, &pt[4]);
  136. STORE32H(c,&pt[8]); STORE32H(d, &pt[12]);
  137. }
  138. #ifdef CLEAN_STACK
  139. void noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
  140. {
  141. _noekeon_ecb_decrypt(ct, pt, key);
  142. burn_stack(sizeof(ulong32) * 5 + sizeof(int));
  143. }
  144. #endif
  145. int noekeon_test(void)
  146. {
  147. #ifndef LTC_TEST
  148. return CRYPT_NOP;
  149. #else
  150. static const struct {
  151. int keylen;
  152. unsigned char key[16], pt[16], ct[16];
  153. } tests[] = {
  154. {
  155. 16,
  156. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
  157. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
  158. { 0x18, 0xa6, 0xec, 0xe5, 0x28, 0xaa, 0x79, 0x73,
  159. 0x28, 0xb2, 0xc0, 0x91, 0xa0, 0x2f, 0x54, 0xc5}
  160. }
  161. };
  162. symmetric_key key;
  163. unsigned char tmp[2][16];
  164. int err, i, y;
  165. for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
  166. zeromem(&key, sizeof(key));
  167. if ((err = noekeon_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
  168. return err;
  169. }
  170. noekeon_ecb_encrypt(tests[i].pt, tmp[0], &key);
  171. noekeon_ecb_decrypt(tmp[0], tmp[1], &key);
  172. if (memcmp(tmp[0], tests[i].ct, 16) || memcmp(tmp[1], tests[i].pt, 16)) {
  173. #if 0
  174. printf("\n\nTest %d failed\n", i);
  175. if (memcmp(tmp[0], tests[i].ct, 16)) {
  176. printf("CT: ");
  177. for (i = 0; i < 16; i++) {
  178. printf("%02x ", tmp[0][i]);
  179. }
  180. printf("\n");
  181. } else {
  182. printf("PT: ");
  183. for (i = 0; i < 16; i++) {
  184. printf("%02x ", tmp[1][i]);
  185. }
  186. printf("\n");
  187. }
  188. #endif
  189. return CRYPT_FAIL_TESTVECTOR;
  190. }
  191. /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
  192. for (y = 0; y < 16; y++) tmp[0][y] = 0;
  193. for (y = 0; y < 1000; y++) noekeon_ecb_encrypt(tmp[0], tmp[0], &key);
  194. for (y = 0; y < 1000; y++) noekeon_ecb_decrypt(tmp[0], tmp[0], &key);
  195. for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
  196. }
  197. return CRYPT_OK;
  198. #endif
  199. }
  200. int noekeon_keysize(int *desired_keysize)
  201. {
  202. _ARGCHK(desired_keysize != NULL);
  203. if (*desired_keysize < 16) {
  204. return CRYPT_INVALID_KEYSIZE;
  205. } else {
  206. *desired_keysize = 16;
  207. return CRYPT_OK;
  208. }
  209. }
  210. #endif