rc6.c 7.1 KB

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