rc5.c 7.2 KB

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