2
0

rc2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. /**********************************************************************\
  12. * To commemorate the 1996 RSA Data Security Conference, the following *
  13. * code is released into the public domain by its author. Prost! *
  14. * *
  15. * This cipher uses 16-bit words and little-endian byte ordering. *
  16. * I wonder which processor it was optimized for? *
  17. * *
  18. * Thanks to CodeView, SoftIce, and D86 for helping bring this code to *
  19. * the public. *
  20. \**********************************************************************/
  21. #include <mycrypt.h>
  22. #ifdef RC2
  23. const struct _cipher_descriptor rc2_desc = {
  24. "rc2",
  25. 12, 8, 128, 8, 16,
  26. &rc2_setup,
  27. &rc2_ecb_encrypt,
  28. &rc2_ecb_decrypt,
  29. &rc2_test,
  30. &rc2_keysize
  31. };
  32. /**********************************************************************\
  33. * Expand a variable-length user key (between 1 and 128 bytes) to a *
  34. * 64-short working rc2 key, of at most "bits" effective key bits. *
  35. * The effective key bits parameter looks like an export control hack. *
  36. * For normal use, it should always be set to 1024. For convenience, *
  37. * zero is accepted as an alias for 1024. *
  38. \**********************************************************************/
  39. /* 256-entry permutation table, probably derived somehow from pi */
  40. static const unsigned char permute[256] = {
  41. 217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157,
  42. 198,126, 55,131, 43,118, 83,142, 98, 76,100,136, 68,139,251,162,
  43. 23,154, 89,245,135,179, 79, 19, 97, 69,109,141, 9,129,125, 50,
  44. 189,143, 64,235,134,183,123, 11,240,149, 33, 34, 92,107, 78,130,
  45. 84,214,101,147,206, 96,178, 28,115, 86,192, 20,167,140,241,220,
  46. 18,117,202, 31, 59,190,228,209, 66, 61,212, 48,163, 60,182, 38,
  47. 111,191, 14,218, 70,105, 7, 87, 39,242, 29,155,188,148, 67, 3,
  48. 248, 17,199,246,144,239, 62,231, 6,195,213, 47,200,102, 30,215,
  49. 8,232,234,222,128, 82,238,247,132,170,114,172, 53, 77,106, 42,
  50. 150, 26,210,113, 90, 21, 73,116, 75,159,208, 94, 4, 24,164,236,
  51. 194,224, 65,110, 15, 81,203,204, 36,145,175, 80,161,244,112, 57,
  52. 153,124, 58,133, 35,184,180,122,252, 2, 54, 91, 37, 85,151, 49,
  53. 45, 93,250,152,227,138,146,174, 5,223, 41, 16,103,108,186,201,
  54. 211, 0,230,207,225,158,168, 44, 99, 22, 1, 63, 88,226,137,169,
  55. 13, 56, 52, 27,171, 51,255,176,187, 72, 12, 95,185,177,205, 46,
  56. 197,243,219, 71,229,165,156,119, 10,166, 32,104,254,127,193,173
  57. };
  58. int rc2_setup(const unsigned char *key, int keylen, int rounds, symmetric_key *skey)
  59. {
  60. unsigned *xkey = skey->rc2.xkey;
  61. unsigned char tmp[128];
  62. unsigned T8, TM;
  63. int i, bits;
  64. _ARGCHK(key != NULL);
  65. _ARGCHK(skey != NULL);
  66. if (keylen < 8 || keylen > 128) {
  67. return CRYPT_INVALID_KEYSIZE;
  68. }
  69. if (rounds != 0 && rounds != 16) {
  70. return CRYPT_INVALID_ROUNDS;
  71. }
  72. for (i = 0; i < keylen; i++) {
  73. tmp[i] = key[i] & 255;
  74. }
  75. /* Phase 1: Expand input key to 128 bytes */
  76. if (keylen < 128) {
  77. for (i = keylen; i < 128; i++) {
  78. tmp[i] = permute[(int)((tmp[i - 1] + tmp[i - keylen]) & 255)];
  79. }
  80. }
  81. /* Phase 2 - reduce effective key size to "bits" */
  82. bits = keylen*8;
  83. T8 = (unsigned)(bits+7)>>3;
  84. TM = (255 >> (unsigned)(7 & -bits));
  85. tmp[128 - T8] = permute[(int)(tmp[128 - T8] & TM)];
  86. for (i = 127 - T8; i >= 0; i--) {
  87. tmp[i] = permute[(int)(tmp[i + 1] ^ tmp[i + T8])];
  88. }
  89. /* Phase 3 - copy to xkey in little-endian order */
  90. i = 63;
  91. do {
  92. xkey[i] = (unsigned)tmp[2*i] + ((unsigned)tmp[2*i+1] << 8);
  93. } while (i-- > 0);
  94. #ifdef CLEAN_STACK
  95. zeromem(tmp, sizeof(tmp));
  96. #endif
  97. return CRYPT_OK;
  98. }
  99. /**********************************************************************\
  100. * Encrypt an 8-byte block of plaintext using the given key. *
  101. \**********************************************************************/
  102. #ifdef CLEAN_STACK
  103. static void _rc2_ecb_encrypt( const unsigned char *plain,
  104. unsigned char *cipher,
  105. symmetric_key *skey)
  106. #else
  107. void rc2_ecb_encrypt( const unsigned char *plain,
  108. unsigned char *cipher,
  109. symmetric_key *skey)
  110. #endif
  111. {
  112. unsigned *xkey;
  113. unsigned x76, x54, x32, x10, i;
  114. _ARGCHK(plain != NULL);
  115. _ARGCHK(cipher != NULL);
  116. _ARGCHK(skey != NULL);
  117. xkey = skey->rc2.xkey;
  118. x76 = ((unsigned)plain[7] << 8) + (unsigned)plain[6];
  119. x54 = ((unsigned)plain[5] << 8) + (unsigned)plain[4];
  120. x32 = ((unsigned)plain[3] << 8) + (unsigned)plain[2];
  121. x10 = ((unsigned)plain[1] << 8) + (unsigned)plain[0];
  122. for (i = 0; i < 16; i++) {
  123. x10 = (x10 + (x32 & ~x76) + (x54 & x76) + xkey[4*i+0]) & 0xFFFF;
  124. x10 = ((x10 << 1) | (x10 >> 15)) & 0xFFFF;
  125. x32 = (x32 + (x54 & ~x10) + (x76 & x10) + xkey[4*i+1]) & 0xFFFF;
  126. x32 = ((x32 << 2) | (x32 >> 14)) & 0xFFFF;
  127. x54 = (x54 + (x76 & ~x32) + (x10 & x32) + xkey[4*i+2]) & 0xFFFF;
  128. x54 = ((x54 << 3) | (x54 >> 13)) & 0xFFFF;
  129. x76 = (x76 + (x10 & ~x54) + (x32 & x54) + xkey[4*i+3]) & 0xFFFF;
  130. x76 = ((x76 << 5) | (x76 >> 11)) & 0xFFFF;
  131. if (i == 4 || i == 10) {
  132. x10 = (x10 + xkey[x76 & 63]) & 0xFFFF;
  133. x32 = (x32 + xkey[x10 & 63]) & 0xFFFF;
  134. x54 = (x54 + xkey[x32 & 63]) & 0xFFFF;
  135. x76 = (x76 + xkey[x54 & 63]) & 0xFFFF;
  136. }
  137. }
  138. cipher[0] = (unsigned char)x10;
  139. cipher[1] = (unsigned char)(x10 >> 8);
  140. cipher[2] = (unsigned char)x32;
  141. cipher[3] = (unsigned char)(x32 >> 8);
  142. cipher[4] = (unsigned char)x54;
  143. cipher[5] = (unsigned char)(x54 >> 8);
  144. cipher[6] = (unsigned char)x76;
  145. cipher[7] = (unsigned char)(x76 >> 8);
  146. }
  147. #ifdef CLEAN_STACK
  148. void rc2_ecb_encrypt( const unsigned char *plain,
  149. unsigned char *cipher,
  150. symmetric_key *skey)
  151. {
  152. _rc2_ecb_encrypt(plain, cipher, skey);
  153. burn_stack(sizeof(unsigned *) + sizeof(unsigned) * 5);
  154. }
  155. #endif
  156. /**********************************************************************\
  157. * Decrypt an 8-byte block of ciphertext using the given key. *
  158. \**********************************************************************/
  159. #ifdef CLEAN_STACK
  160. static void _rc2_ecb_decrypt( const unsigned char *cipher,
  161. unsigned char *plain,
  162. symmetric_key *skey)
  163. #else
  164. void rc2_ecb_decrypt( const unsigned char *cipher,
  165. unsigned char *plain,
  166. symmetric_key *skey)
  167. #endif
  168. {
  169. unsigned x76, x54, x32, x10;
  170. unsigned *xkey;
  171. int i;
  172. _ARGCHK(plain != NULL);
  173. _ARGCHK(cipher != NULL);
  174. _ARGCHK(skey != NULL);
  175. xkey = skey->rc2.xkey;
  176. x76 = ((unsigned)cipher[7] << 8) + (unsigned)cipher[6];
  177. x54 = ((unsigned)cipher[5] << 8) + (unsigned)cipher[4];
  178. x32 = ((unsigned)cipher[3] << 8) + (unsigned)cipher[2];
  179. x10 = ((unsigned)cipher[1] << 8) + (unsigned)cipher[0];
  180. for (i = 15; i >= 0; i--) {
  181. if (i == 4 || i == 10) {
  182. x76 = (x76 - xkey[x54 & 63]) & 0xFFFF;
  183. x54 = (x54 - xkey[x32 & 63]) & 0xFFFF;
  184. x32 = (x32 - xkey[x10 & 63]) & 0xFFFF;
  185. x10 = (x10 - xkey[x76 & 63]) & 0xFFFF;
  186. }
  187. x76 = ((x76 << 11) | (x76 >> 5)) & 0xFFFF;
  188. x76 = (x76 - ((x10 & ~x54) + (x32 & x54) + xkey[4*i+3])) & 0xFFFF;
  189. x54 = ((x54 << 13) | (x54 >> 3)) & 0xFFFF;
  190. x54 = (x54 - ((x76 & ~x32) + (x10 & x32) + xkey[4*i+2])) & 0xFFFF;
  191. x32 = ((x32 << 14) | (x32 >> 2)) & 0xFFFF;
  192. x32 = (x32 - ((x54 & ~x10) + (x76 & x10) + xkey[4*i+1])) & 0xFFFF;
  193. x10 = ((x10 << 15) | (x10 >> 1)) & 0xFFFF;
  194. x10 = (x10 - ((x32 & ~x76) + (x54 & x76) + xkey[4*i+0])) & 0xFFFF;
  195. }
  196. plain[0] = (unsigned char)x10;
  197. plain[1] = (unsigned char)(x10 >> 8);
  198. plain[2] = (unsigned char)x32;
  199. plain[3] = (unsigned char)(x32 >> 8);
  200. plain[4] = (unsigned char)x54;
  201. plain[5] = (unsigned char)(x54 >> 8);
  202. plain[6] = (unsigned char)x76;
  203. plain[7] = (unsigned char)(x76 >> 8);
  204. }
  205. #ifdef CLEAN_STACK
  206. void rc2_ecb_decrypt( const unsigned char *cipher,
  207. unsigned char *plain,
  208. symmetric_key *skey)
  209. {
  210. _rc2_ecb_decrypt(cipher, plain, skey);
  211. burn_stack(sizeof(unsigned *) + sizeof(unsigned) * 4 + sizeof(int));
  212. }
  213. #endif
  214. int rc2_test(void)
  215. {
  216. #ifndef LTC_TEST
  217. return CRYPT_NOP;
  218. #else
  219. static const struct {
  220. int keylen;
  221. unsigned char key[16], pt[8], ct[8];
  222. } tests[] = {
  223. { 8,
  224. { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  225. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  226. { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
  227. { 0x30, 0x64, 0x9e, 0xdf, 0x9b, 0xe7, 0xd2, 0xc2 }
  228. },
  229. { 16,
  230. { 0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x7f,
  231. 0x0f, 0x79, 0xc3, 0x84, 0x62, 0x7b, 0xaf, 0xb2 },
  232. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  233. { 0x22, 0x69, 0x55, 0x2a, 0xb0, 0xf8, 0x5c, 0xa6 }
  234. }
  235. };
  236. int x, y, err;
  237. symmetric_key skey;
  238. unsigned char tmp[2][8];
  239. for (x = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) {
  240. zeromem(tmp, sizeof(tmp));
  241. if ((err = rc2_setup(tests[x].key, tests[x].keylen, 0, &skey)) != CRYPT_OK) {
  242. return err;
  243. }
  244. rc2_ecb_encrypt(tests[x].pt, tmp[0], &skey);
  245. rc2_ecb_decrypt(tmp[0], tmp[1], &skey);
  246. if (memcmp(tmp[0], tests[x].ct, 8) != 0 || memcmp(tmp[1], tests[x].pt, 8) != 0) {
  247. return CRYPT_FAIL_TESTVECTOR;
  248. }
  249. /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
  250. for (y = 0; y < 8; y++) tmp[0][y] = 0;
  251. for (y = 0; y < 1000; y++) rc2_ecb_encrypt(tmp[0], tmp[0], &skey);
  252. for (y = 0; y < 1000; y++) rc2_ecb_decrypt(tmp[0], tmp[0], &skey);
  253. for (y = 0; y < 8; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
  254. }
  255. return CRYPT_OK;
  256. #endif
  257. }
  258. int rc2_keysize(int *keysize)
  259. {
  260. _ARGCHK(keysize != NULL);
  261. if (*keysize < 8) {
  262. return CRYPT_INVALID_KEYSIZE;
  263. } else if (*keysize > 128) {
  264. *keysize = 128;
  265. }
  266. return CRYPT_OK;
  267. }
  268. #endif