rc2.c 10 KB

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