rc5.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "mycrypt.h"
  2. #ifdef RC5
  3. const struct _cipher_descriptor rc5_desc =
  4. {
  5. "rc5",
  6. 2,
  7. 8, 128, 8, 12,
  8. &rc5_setup,
  9. &rc5_ecb_encrypt,
  10. &rc5_ecb_decrypt,
  11. &rc5_test,
  12. &rc5_keysize
  13. };
  14. static const ulong32 stab[50] = {
  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, 0xe96a3d2fUL, 0x87a1b6e8UL, 0x25d930a1UL, 0xc410aa5aUL,
  21. 0x62482413UL, 0x007f9dccUL
  22. };
  23. #ifdef CLEAN_STACK
  24. static int _rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
  25. #else
  26. int rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
  27. #endif
  28. {
  29. ulong32 L[64], *S, A, B, i, j, v, s, t, l;
  30. _ARGCHK(skey != NULL);
  31. _ARGCHK(key != NULL);
  32. /* test parameters */
  33. if (num_rounds == 0) {
  34. num_rounds = rc5_desc.default_rounds;
  35. }
  36. if (num_rounds < 12 || num_rounds > 24) {
  37. return CRYPT_INVALID_ROUNDS;
  38. }
  39. /* key must be between 64 and 1024 bits */
  40. if (keylen < 8 || keylen > 128) {
  41. return CRYPT_INVALID_KEYSIZE;
  42. }
  43. skey->rc5.rounds = num_rounds;
  44. S = skey->rc5.K;
  45. /* copy the key into the L array */
  46. for (A = i = j = 0; i < (ulong32)keylen; ) {
  47. A = (A << 8) | ((ulong32)(key[i++] & 255));
  48. if ((i & 3) == 0) {
  49. L[j++] = BSWAP(A);
  50. A = 0;
  51. }
  52. }
  53. if ((keylen & 3) != 0) {
  54. A <<= (ulong32)((8 * (4 - (keylen&3))));
  55. L[j++] = BSWAP(A);
  56. }
  57. /* setup the S array */
  58. t = (ulong32)(2 * (num_rounds + 1));
  59. memcpy(S, stab, t * sizeof(stab[0]));
  60. /* mix buffer */
  61. s = 3 * MAX(t, j);
  62. l = j;
  63. for (A = B = i = j = v = 0; v < s; v++) {
  64. A = S[i] = ROL(S[i] + A + B, 3);
  65. B = L[j] = ROL(L[j] + A + B, (A+B));
  66. if (++i == t) { i = 0; }
  67. if (++j == l) { j = 0; }
  68. }
  69. return CRYPT_OK;
  70. }
  71. #ifdef CLEAN_STACK
  72. int rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
  73. {
  74. int x;
  75. x = _rc5_setup(key, keylen, num_rounds, skey);
  76. burn_stack(sizeof(ulong32) * 122 + sizeof(int));
  77. return x;
  78. }
  79. #endif
  80. #ifdef CLEAN_STACK
  81. static void _rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
  82. #else
  83. void rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
  84. #endif
  85. {
  86. ulong32 A, B, *K;
  87. int r;
  88. _ARGCHK(key != NULL);
  89. _ARGCHK(pt != NULL);
  90. _ARGCHK(ct != NULL);
  91. LOAD32L(A, &pt[0]);
  92. LOAD32L(B, &pt[4]);
  93. A += key->rc5.K[0];
  94. B += key->rc5.K[1];
  95. K = key->rc5.K + 2;
  96. if ((key->rc5.rounds & 1) == 0) {
  97. for (r = 0; r < key->rc5.rounds; r += 2) {
  98. A = ROL(A ^ B, B) + K[0];
  99. B = ROL(B ^ A, A) + K[1];
  100. A = ROL(A ^ B, B) + K[2];
  101. B = ROL(B ^ A, A) + K[3];
  102. K += 4;
  103. }
  104. } else {
  105. for (r = 0; r < key->rc5.rounds; r++) {
  106. A = ROL(A ^ B, B) + K[0];
  107. B = ROL(B ^ A, A) + K[1];
  108. K += 2;
  109. }
  110. }
  111. STORE32L(A, &ct[0]);
  112. STORE32L(B, &ct[4]);
  113. }
  114. #ifdef CLEAN_STACK
  115. void rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
  116. {
  117. _rc5_ecb_encrypt(pt, ct, key);
  118. burn_stack(sizeof(ulong32) * 2 + sizeof(int));
  119. }
  120. #endif
  121. #ifdef CLEAN_STACK
  122. static void _rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
  123. #else
  124. void rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
  125. #endif
  126. {
  127. ulong32 A, B, *K;
  128. int r;
  129. _ARGCHK(key != NULL);
  130. _ARGCHK(pt != NULL);
  131. _ARGCHK(ct != NULL);
  132. LOAD32L(A, &ct[0]);
  133. LOAD32L(B, &ct[4]);
  134. K = key->rc5.K + (key->rc5.rounds << 1);
  135. if ((key->rc5.rounds & 1) == 0) {
  136. K -= 2;
  137. for (r = key->rc5.rounds - 1; r >= 0; r -= 2) {
  138. B = ROR(B - K[3], A) ^ A;
  139. A = ROR(A - K[2], B) ^ B;
  140. B = ROR(B - K[1], A) ^ A;
  141. A = ROR(A - K[0], B) ^ B;
  142. K -= 4;
  143. }
  144. } else {
  145. for (r = key->rc5.rounds - 1; r >= 0; r--) {
  146. B = ROR(B - K[1], A) ^ A;
  147. A = ROR(A - K[0], B) ^ B;
  148. K -= 2;
  149. }
  150. }
  151. A -= key->rc5.K[0];
  152. B -= key->rc5.K[1];
  153. STORE32L(A, &pt[0]);
  154. STORE32L(B, &pt[4]);
  155. }
  156. #ifdef CLEAN_STACK
  157. void rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
  158. {
  159. _rc5_ecb_decrypt(ct, pt, key);
  160. burn_stack(sizeof(ulong32) * 2 + sizeof(int));
  161. }
  162. #endif
  163. int rc5_test(void)
  164. {
  165. #ifndef LTC_TEST
  166. return CRYPT_NOP;
  167. #else
  168. static const struct {
  169. unsigned char key[16], pt[8], ct[8];
  170. } tests[] = {
  171. {
  172. { 0x91, 0x5f, 0x46, 0x19, 0xbe, 0x41, 0xb2, 0x51,
  173. 0x63, 0x55, 0xa5, 0x01, 0x10, 0xa9, 0xce, 0x91 },
  174. { 0x21, 0xa5, 0xdb, 0xee, 0x15, 0x4b, 0x8f, 0x6d },
  175. { 0xf7, 0xc0, 0x13, 0xac, 0x5b, 0x2b, 0x89, 0x52 }
  176. },
  177. {
  178. { 0x78, 0x33, 0x48, 0xe7, 0x5a, 0xeb, 0x0f, 0x2f,
  179. 0xd7, 0xb1, 0x69, 0xbb, 0x8d, 0xc1, 0x67, 0x87 },
  180. { 0xF7, 0xC0, 0x13, 0xAC, 0x5B, 0x2B, 0x89, 0x52 },
  181. { 0x2F, 0x42, 0xB3, 0xB7, 0x03, 0x69, 0xFC, 0x92 }
  182. },
  183. {
  184. { 0xDC, 0x49, 0xdb, 0x13, 0x75, 0xa5, 0x58, 0x4f,
  185. 0x64, 0x85, 0xb4, 0x13, 0xb5, 0xf1, 0x2b, 0xaf },
  186. { 0x2F, 0x42, 0xB3, 0xB7, 0x03, 0x69, 0xFC, 0x92 },
  187. { 0x65, 0xc1, 0x78, 0xb2, 0x84, 0xd1, 0x97, 0xcc }
  188. }
  189. };
  190. unsigned char buf[2][8];
  191. int x, err;
  192. symmetric_key key;
  193. for (x = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) {
  194. /* setup key */
  195. if ((err = rc5_setup(tests[x].key, 16, 12, &key)) != CRYPT_OK) {
  196. return err;
  197. }
  198. /* encrypt and decrypt */
  199. rc5_ecb_encrypt(tests[x].pt, buf[0], &key);
  200. rc5_ecb_decrypt(buf[0], buf[1], &key);
  201. /* compare */
  202. if (memcmp(buf[0], tests[x].ct, 8) != 0 || memcmp(buf[1], tests[x].pt, 8) != 0) {
  203. return CRYPT_FAIL_TESTVECTOR;
  204. }
  205. }
  206. return CRYPT_OK;
  207. #endif
  208. }
  209. int rc5_keysize(int *desired_keysize)
  210. {
  211. _ARGCHK(desired_keysize != NULL);
  212. if (*desired_keysize < 8) {
  213. return CRYPT_INVALID_KEYSIZE;
  214. } else if (*desired_keysize > 128) {
  215. *desired_keysize = 128;
  216. }
  217. return CRYPT_OK;
  218. }
  219. #endif