rc6.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. /* RC6 code by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef RC6
  14. const struct _cipher_descriptor rc6_desc =
  15. {
  16. "rc6",
  17. 3,
  18. 8, 128, 16, 20,
  19. &rc6_setup,
  20. &rc6_ecb_encrypt,
  21. &rc6_ecb_decrypt,
  22. &rc6_test,
  23. &rc6_keysize
  24. };
  25. static const ulong32 stab[44] = {
  26. 0xb7e15163UL, 0x5618cb1cUL, 0xf45044d5UL, 0x9287be8eUL, 0x30bf3847UL, 0xcef6b200UL, 0x6d2e2bb9UL, 0x0b65a572UL,
  27. 0xa99d1f2bUL, 0x47d498e4UL, 0xe60c129dUL, 0x84438c56UL, 0x227b060fUL, 0xc0b27fc8UL, 0x5ee9f981UL, 0xfd21733aUL,
  28. 0x9b58ecf3UL, 0x399066acUL, 0xd7c7e065UL, 0x75ff5a1eUL, 0x1436d3d7UL, 0xb26e4d90UL, 0x50a5c749UL, 0xeedd4102UL,
  29. 0x8d14babbUL, 0x2b4c3474UL, 0xc983ae2dUL, 0x67bb27e6UL, 0x05f2a19fUL, 0xa42a1b58UL, 0x42619511UL, 0xe0990ecaUL,
  30. 0x7ed08883UL, 0x1d08023cUL, 0xbb3f7bf5UL, 0x5976f5aeUL, 0xf7ae6f67UL, 0x95e5e920UL, 0x341d62d9UL, 0xd254dc92UL,
  31. 0x708c564bUL, 0x0ec3d004UL, 0xacfb49bdUL, 0x4b32c376UL };
  32. #ifdef CLEAN_STACK
  33. static int _rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
  34. #else
  35. int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
  36. #endif
  37. {
  38. ulong32 L[64], S[50], A, B, i, j, v, s, l;
  39. _ARGCHK(key != NULL);
  40. _ARGCHK(skey != NULL);
  41. /* test parameters */
  42. if (num_rounds != 0 && num_rounds != 20) {
  43. return CRYPT_INVALID_ROUNDS;
  44. }
  45. /* key must be between 64 and 1024 bits */
  46. if (keylen < 8 || keylen > 128) {
  47. return CRYPT_INVALID_KEYSIZE;
  48. }
  49. /* copy the key into the L array */
  50. for (A = i = j = 0; i < (ulong32)keylen; ) {
  51. A = (A << 8) | ((ulong32)(key[i++] & 255));
  52. if (!(i & 3)) {
  53. L[j++] = BSWAP(A);
  54. A = 0;
  55. }
  56. }
  57. /* handle odd sized keys */
  58. if (keylen & 3) {
  59. A <<= (8 * (4 - (keylen&3)));
  60. L[j++] = BSWAP(A);
  61. }
  62. /* setup the S array */
  63. XMEMCPY(S, stab, 44 * sizeof(stab[0]));
  64. /* mix buffer */
  65. s = 3 * MAX(44, j);
  66. l = j;
  67. for (A = B = i = j = v = 0; v < s; v++) {
  68. A = S[i] = ROL(S[i] + A + B, 3);
  69. B = L[j] = ROL(L[j] + A + B, (A+B));
  70. if (++i == 44) { i = 0; }
  71. if (++j == l) { j = 0; }
  72. }
  73. /* copy to key */
  74. for (i = 0; i < 44; i++) {
  75. skey->rc6.K[i] = S[i];
  76. }
  77. return CRYPT_OK;
  78. }
  79. #ifdef CLEAN_STACK
  80. int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
  81. {
  82. int x;
  83. x = _rc6_setup(key, keylen, num_rounds, skey);
  84. burn_stack(sizeof(ulong32) * 122);
  85. return x;
  86. }
  87. #endif
  88. #ifdef CLEAN_STACK
  89. static void _rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
  90. #else
  91. void rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
  92. #endif
  93. {
  94. ulong32 a,b,c,d,t,u, *K;
  95. int r;
  96. _ARGCHK(key != NULL);
  97. _ARGCHK(pt != NULL);
  98. _ARGCHK(ct != NULL);
  99. LOAD32L(a,&pt[0]);LOAD32L(b,&pt[4]);LOAD32L(c,&pt[8]);LOAD32L(d,&pt[12]);
  100. b += key->rc6.K[0];
  101. d += key->rc6.K[1];
  102. #define RND(a,b,c,d) \
  103. t = (b * (b + b + 1)); t = ROL(t, 5); \
  104. u = (d * (d + d + 1)); u = ROL(u, 5); \
  105. a = ROL(a^t,u) + K[0]; \
  106. c = ROL(c^u,t) + K[1]; K += 2;
  107. K = key->rc6.K + 2;
  108. for (r = 0; r < 20; r += 4) {
  109. RND(a,b,c,d);
  110. RND(b,c,d,a);
  111. RND(c,d,a,b);
  112. RND(d,a,b,c);
  113. }
  114. #undef RND
  115. a += key->rc6.K[42];
  116. c += key->rc6.K[43];
  117. STORE32L(a,&ct[0]);STORE32L(b,&ct[4]);STORE32L(c,&ct[8]);STORE32L(d,&ct[12]);
  118. }
  119. #ifdef CLEAN_STACK
  120. void rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
  121. {
  122. _rc6_ecb_encrypt(pt, ct, key);
  123. burn_stack(sizeof(ulong32) * 6 + sizeof(int));
  124. }
  125. #endif
  126. #ifdef CLEAN_STACK
  127. static void _rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
  128. #else
  129. void rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
  130. #endif
  131. {
  132. ulong32 a,b,c,d,t,u, *K;
  133. int r;
  134. _ARGCHK(key != NULL);
  135. _ARGCHK(pt != NULL);
  136. _ARGCHK(ct != NULL);
  137. LOAD32L(a,&ct[0]);LOAD32L(b,&ct[4]);LOAD32L(c,&ct[8]);LOAD32L(d,&ct[12]);
  138. a -= key->rc6.K[42];
  139. c -= key->rc6.K[43];
  140. #define RND(a,b,c,d) \
  141. t = (b * (b + b + 1)); t = ROL(t, 5); \
  142. u = (d * (d + d + 1)); u = ROL(u, 5); \
  143. c = ROR(c - K[1], t) ^ u; \
  144. a = ROR(a - K[0], u) ^ t; K -= 2;
  145. K = key->rc6.K + 40;
  146. for (r = 0; r < 20; r += 4) {
  147. RND(d,a,b,c);
  148. RND(c,d,a,b);
  149. RND(b,c,d,a);
  150. RND(a,b,c,d);
  151. }
  152. #undef RND
  153. b -= key->rc6.K[0];
  154. d -= key->rc6.K[1];
  155. STORE32L(a,&pt[0]);STORE32L(b,&pt[4]);STORE32L(c,&pt[8]);STORE32L(d,&pt[12]);
  156. }
  157. #ifdef CLEAN_STACK
  158. void rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
  159. {
  160. _rc6_ecb_decrypt(ct, pt, key);
  161. burn_stack(sizeof(ulong32) * 6 + sizeof(int));
  162. }
  163. #endif
  164. int rc6_test(void)
  165. {
  166. #ifndef LTC_TEST
  167. return CRYPT_NOP;
  168. #else
  169. static const struct {
  170. int keylen;
  171. unsigned char key[32], pt[16], ct[16];
  172. } tests[] = {
  173. {
  174. 16,
  175. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  176. 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
  177. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  178. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  179. { 0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79,
  180. 0x8a, 0x9b, 0xac, 0xbd, 0xce, 0xdf, 0xe0, 0xf1 },
  181. { 0x52, 0x4e, 0x19, 0x2f, 0x47, 0x15, 0xc6, 0x23,
  182. 0x1f, 0x51, 0xf6, 0x36, 0x7e, 0xa4, 0x3f, 0x18 }
  183. },
  184. {
  185. 24,
  186. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  187. 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
  188. 0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0,
  189. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  190. { 0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79,
  191. 0x8a, 0x9b, 0xac, 0xbd, 0xce, 0xdf, 0xe0, 0xf1 },
  192. { 0x68, 0x83, 0x29, 0xd0, 0x19, 0xe5, 0x05, 0x04,
  193. 0x1e, 0x52, 0xe9, 0x2a, 0xf9, 0x52, 0x91, 0xd4 }
  194. },
  195. {
  196. 32,
  197. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  198. 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
  199. 0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0,
  200. 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe },
  201. { 0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79,
  202. 0x8a, 0x9b, 0xac, 0xbd, 0xce, 0xdf, 0xe0, 0xf1 },
  203. { 0xc8, 0x24, 0x18, 0x16, 0xf0, 0xd7, 0xe4, 0x89,
  204. 0x20, 0xad, 0x16, 0xa1, 0x67, 0x4e, 0x5d, 0x48 }
  205. }
  206. };
  207. unsigned char tmp[2][16];
  208. int x, y, err;
  209. symmetric_key key;
  210. for (x = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) {
  211. /* setup key */
  212. if ((err = rc6_setup(tests[x].key, tests[x].keylen, 0, &key)) != CRYPT_OK) {
  213. return err;
  214. }
  215. /* encrypt and decrypt */
  216. rc6_ecb_encrypt(tests[x].pt, tmp[0], &key);
  217. rc6_ecb_decrypt(tmp[0], tmp[1], &key);
  218. /* compare */
  219. if (memcmp(tmp[0], tests[x].ct, 16) || memcmp(tmp[1], tests[x].pt, 16)) {
  220. #if 0
  221. printf("\n\nFailed test %d\n", x);
  222. if (memcmp(tmp[0], tests[x].ct, 16)) {
  223. printf("Ciphertext: ");
  224. for (y = 0; y < 16; y++) printf("%02x ", tmp[0][y]);
  225. printf("\nExpected : ");
  226. for (y = 0; y < 16; y++) printf("%02x ", tests[x].ct[y]);
  227. printf("\n");
  228. }
  229. if (memcmp(tmp[1], tests[x].pt, 16)) {
  230. printf("Plaintext: ");
  231. for (y = 0; y < 16; y++) printf("%02x ", tmp[0][y]);
  232. printf("\nExpected : ");
  233. for (y = 0; y < 16; y++) printf("%02x ", tests[x].pt[y]);
  234. printf("\n");
  235. }
  236. #endif
  237. return CRYPT_FAIL_TESTVECTOR;
  238. }
  239. /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
  240. for (y = 0; y < 16; y++) tmp[0][y] = 0;
  241. for (y = 0; y < 1000; y++) rc6_ecb_encrypt(tmp[0], tmp[0], &key);
  242. for (y = 0; y < 1000; y++) rc6_ecb_decrypt(tmp[0], tmp[0], &key);
  243. for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
  244. }
  245. return CRYPT_OK;
  246. #endif
  247. }
  248. int rc6_keysize(int *desired_keysize)
  249. {
  250. _ARGCHK(desired_keysize != NULL);
  251. if (*desired_keysize < 8) {
  252. return CRYPT_INVALID_KEYSIZE;
  253. } else if (*desired_keysize > 128) {
  254. *desired_keysize = 128;
  255. }
  256. return CRYPT_OK;
  257. }
  258. #endif /*RC6*/