rc2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. /**********************************************************************\
  4. * To commemorate the 1996 RSA Data Security Conference, the following *
  5. * code is released into the public domain by its author. Prost! *
  6. * *
  7. * This cipher uses 16-bit words and little-endian byte ordering. *
  8. * I wonder which processor it was optimized for? *
  9. * *
  10. * Thanks to CodeView, SoftIce, and D86 for helping bring this code to *
  11. * the public. *
  12. \**********************************************************************/
  13. #include "tomcrypt_private.h"
  14. /**
  15. @file rc2.c
  16. Implementation of RC2 with fixed effective key length of 64bits
  17. */
  18. #ifdef LTC_RC2
  19. const struct ltc_cipher_descriptor rc2_desc = {
  20. "rc2",
  21. 12, 8, 128, 8, 16,
  22. &rc2_setup,
  23. &rc2_ecb_encrypt,
  24. &rc2_ecb_decrypt,
  25. &rc2_test,
  26. &rc2_done,
  27. &rc2_keysize,
  28. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
  29. };
  30. /* 256-entry permutation table, probably derived somehow from pi */
  31. static const unsigned char permute[256] = {
  32. 217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157,
  33. 198,126, 55,131, 43,118, 83,142, 98, 76,100,136, 68,139,251,162,
  34. 23,154, 89,245,135,179, 79, 19, 97, 69,109,141, 9,129,125, 50,
  35. 189,143, 64,235,134,183,123, 11,240,149, 33, 34, 92,107, 78,130,
  36. 84,214,101,147,206, 96,178, 28,115, 86,192, 20,167,140,241,220,
  37. 18,117,202, 31, 59,190,228,209, 66, 61,212, 48,163, 60,182, 38,
  38. 111,191, 14,218, 70,105, 7, 87, 39,242, 29,155,188,148, 67, 3,
  39. 248, 17,199,246,144,239, 62,231, 6,195,213, 47,200,102, 30,215,
  40. 8,232,234,222,128, 82,238,247,132,170,114,172, 53, 77,106, 42,
  41. 150, 26,210,113, 90, 21, 73,116, 75,159,208, 94, 4, 24,164,236,
  42. 194,224, 65,110, 15, 81,203,204, 36,145,175, 80,161,244,112, 57,
  43. 153,124, 58,133, 35,184,180,122,252, 2, 54, 91, 37, 85,151, 49,
  44. 45, 93,250,152,227,138,146,174, 5,223, 41, 16,103,108,186,201,
  45. 211, 0,230,207,225,158,168, 44, 99, 22, 1, 63, 88,226,137,169,
  46. 13, 56, 52, 27,171, 51,255,176,187, 72, 12, 95,185,177,205, 46,
  47. 197,243,219, 71,229,165,156,119, 10,166, 32,104,254,127,193,173
  48. };
  49. /**
  50. Initialize the RC2 block cipher
  51. @param key The symmetric key you wish to pass
  52. @param keylen The key length in bytes
  53. @param bits The effective key length in bits
  54. @param num_rounds The number of rounds desired (0 for default)
  55. @param skey The key in as scheduled by this function.
  56. @return CRYPT_OK if successful
  57. */
  58. int rc2_setup_ex(const unsigned char *key, int keylen, int bits, int num_rounds, symmetric_key *skey)
  59. {
  60. unsigned *xkey = skey->rc2.xkey;
  61. unsigned char tmp[128];
  62. unsigned T8, TM;
  63. int i;
  64. LTC_ARGCHK(key != NULL);
  65. LTC_ARGCHK(skey != NULL);
  66. if (keylen == 0 || keylen > 128 || bits > 1024) {
  67. return CRYPT_INVALID_KEYSIZE;
  68. }
  69. if (bits == 0) {
  70. bits = 1024;
  71. }
  72. if (num_rounds != 0 && num_rounds != 16) {
  73. return CRYPT_INVALID_ROUNDS;
  74. }
  75. for (i = 0; i < keylen; i++) {
  76. tmp[i] = key[i] & 255;
  77. }
  78. /* Phase 1: Expand input key to 128 bytes */
  79. if (keylen < 128) {
  80. for (i = keylen; i < 128; i++) {
  81. tmp[i] = permute[(tmp[i - 1] + tmp[i - keylen]) & 255];
  82. }
  83. }
  84. /* Phase 2 - reduce effective key size to "bits" */
  85. T8 = (unsigned)(bits+7)>>3;
  86. TM = (255 >> (unsigned)(7 & -bits));
  87. tmp[128 - T8] = permute[tmp[128 - T8] & TM];
  88. for (i = 127 - T8; i >= 0; i--) {
  89. tmp[i] = permute[tmp[i + 1] ^ tmp[i + T8]];
  90. }
  91. /* Phase 3 - copy to xkey in little-endian order */
  92. for (i = 0; i < 64; i++) {
  93. xkey[i] = (unsigned)tmp[2*i] + ((unsigned)tmp[2*i+1] << 8);
  94. }
  95. #ifdef LTC_CLEAN_STACK
  96. zeromem(tmp, sizeof(tmp));
  97. #endif
  98. return CRYPT_OK;
  99. }
  100. /**
  101. Initialize the RC2 block cipher
  102. The effective key length is here always keylen * 8
  103. @param key The symmetric key you wish to pass
  104. @param keylen The key length in bytes
  105. @param num_rounds The number of rounds desired (0 for default)
  106. @param skey The key in as scheduled by this function.
  107. @return CRYPT_OK if successful
  108. */
  109. int rc2_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
  110. {
  111. return rc2_setup_ex(key, keylen, keylen * 8, num_rounds, skey);
  112. }
  113. /**********************************************************************\
  114. * Encrypt an 8-byte block of plaintext using the given key. *
  115. \**********************************************************************/
  116. /**
  117. Encrypts a block of text with RC2
  118. @param pt The input plaintext (8 bytes)
  119. @param ct The output ciphertext (8 bytes)
  120. @param skey The key as scheduled
  121. @return CRYPT_OK if successful
  122. */
  123. #ifdef LTC_CLEAN_STACK
  124. static int s_rc2_ecb_encrypt( const unsigned char *pt,
  125. unsigned char *ct,
  126. const symmetric_key *skey)
  127. #else
  128. int rc2_ecb_encrypt( const unsigned char *pt,
  129. unsigned char *ct,
  130. const symmetric_key *skey)
  131. #endif
  132. {
  133. const unsigned *xkey;
  134. unsigned x76, x54, x32, x10, i;
  135. LTC_ARGCHK(pt != NULL);
  136. LTC_ARGCHK(ct != NULL);
  137. LTC_ARGCHK(skey != NULL);
  138. xkey = skey->rc2.xkey;
  139. x76 = ((unsigned)pt[7] << 8) + (unsigned)pt[6];
  140. x54 = ((unsigned)pt[5] << 8) + (unsigned)pt[4];
  141. x32 = ((unsigned)pt[3] << 8) + (unsigned)pt[2];
  142. x10 = ((unsigned)pt[1] << 8) + (unsigned)pt[0];
  143. for (i = 0; i < 16; i++) {
  144. x10 = (x10 + (x32 & ~x76) + (x54 & x76) + xkey[4*i+0]) & 0xFFFF;
  145. x10 = ((x10 << 1) | (x10 >> 15));
  146. x32 = (x32 + (x54 & ~x10) + (x76 & x10) + xkey[4*i+1]) & 0xFFFF;
  147. x32 = ((x32 << 2) | (x32 >> 14));
  148. x54 = (x54 + (x76 & ~x32) + (x10 & x32) + xkey[4*i+2]) & 0xFFFF;
  149. x54 = ((x54 << 3) | (x54 >> 13));
  150. x76 = (x76 + (x10 & ~x54) + (x32 & x54) + xkey[4*i+3]) & 0xFFFF;
  151. x76 = ((x76 << 5) | (x76 >> 11));
  152. if (i == 4 || i == 10) {
  153. x10 = (x10 + xkey[x76 & 63]) & 0xFFFF;
  154. x32 = (x32 + xkey[x10 & 63]) & 0xFFFF;
  155. x54 = (x54 + xkey[x32 & 63]) & 0xFFFF;
  156. x76 = (x76 + xkey[x54 & 63]) & 0xFFFF;
  157. }
  158. }
  159. ct[0] = (unsigned char)x10;
  160. ct[1] = (unsigned char)(x10 >> 8);
  161. ct[2] = (unsigned char)x32;
  162. ct[3] = (unsigned char)(x32 >> 8);
  163. ct[4] = (unsigned char)x54;
  164. ct[5] = (unsigned char)(x54 >> 8);
  165. ct[6] = (unsigned char)x76;
  166. ct[7] = (unsigned char)(x76 >> 8);
  167. return CRYPT_OK;
  168. }
  169. #ifdef LTC_CLEAN_STACK
  170. int rc2_ecb_encrypt( const unsigned char *pt,
  171. unsigned char *ct,
  172. const symmetric_key *skey)
  173. {
  174. int err = s_rc2_ecb_encrypt(pt, ct, skey);
  175. burn_stack(sizeof(unsigned *) + sizeof(unsigned) * 5);
  176. return err;
  177. }
  178. #endif
  179. /**********************************************************************\
  180. * Decrypt an 8-byte block of ciphertext using the given key. *
  181. \**********************************************************************/
  182. /**
  183. Decrypts a block of text with RC2
  184. @param ct The input ciphertext (8 bytes)
  185. @param pt The output plaintext (8 bytes)
  186. @param skey The key as scheduled
  187. @return CRYPT_OK if successful
  188. */
  189. #ifdef LTC_CLEAN_STACK
  190. static int s_rc2_ecb_decrypt( const unsigned char *ct,
  191. unsigned char *pt,
  192. const symmetric_key *skey)
  193. #else
  194. int rc2_ecb_decrypt( const unsigned char *ct,
  195. unsigned char *pt,
  196. const symmetric_key *skey)
  197. #endif
  198. {
  199. unsigned x76, x54, x32, x10;
  200. const unsigned *xkey;
  201. int i;
  202. LTC_ARGCHK(pt != NULL);
  203. LTC_ARGCHK(ct != NULL);
  204. LTC_ARGCHK(skey != NULL);
  205. xkey = skey->rc2.xkey;
  206. x76 = ((unsigned)ct[7] << 8) + (unsigned)ct[6];
  207. x54 = ((unsigned)ct[5] << 8) + (unsigned)ct[4];
  208. x32 = ((unsigned)ct[3] << 8) + (unsigned)ct[2];
  209. x10 = ((unsigned)ct[1] << 8) + (unsigned)ct[0];
  210. for (i = 15; i >= 0; i--) {
  211. if (i == 4 || i == 10) {
  212. x76 = (x76 - xkey[x54 & 63]) & 0xFFFF;
  213. x54 = (x54 - xkey[x32 & 63]) & 0xFFFF;
  214. x32 = (x32 - xkey[x10 & 63]) & 0xFFFF;
  215. x10 = (x10 - xkey[x76 & 63]) & 0xFFFF;
  216. }
  217. x76 = ((x76 << 11) | (x76 >> 5));
  218. x76 = (x76 - ((x10 & ~x54) + (x32 & x54) + xkey[4*i+3])) & 0xFFFF;
  219. x54 = ((x54 << 13) | (x54 >> 3));
  220. x54 = (x54 - ((x76 & ~x32) + (x10 & x32) + xkey[4*i+2])) & 0xFFFF;
  221. x32 = ((x32 << 14) | (x32 >> 2));
  222. x32 = (x32 - ((x54 & ~x10) + (x76 & x10) + xkey[4*i+1])) & 0xFFFF;
  223. x10 = ((x10 << 15) | (x10 >> 1));
  224. x10 = (x10 - ((x32 & ~x76) + (x54 & x76) + xkey[4*i+0])) & 0xFFFF;
  225. }
  226. pt[0] = (unsigned char)x10;
  227. pt[1] = (unsigned char)(x10 >> 8);
  228. pt[2] = (unsigned char)x32;
  229. pt[3] = (unsigned char)(x32 >> 8);
  230. pt[4] = (unsigned char)x54;
  231. pt[5] = (unsigned char)(x54 >> 8);
  232. pt[6] = (unsigned char)x76;
  233. pt[7] = (unsigned char)(x76 >> 8);
  234. return CRYPT_OK;
  235. }
  236. #ifdef LTC_CLEAN_STACK
  237. int rc2_ecb_decrypt( const unsigned char *ct,
  238. unsigned char *pt,
  239. const symmetric_key *skey)
  240. {
  241. int err = s_rc2_ecb_decrypt(ct, pt, skey);
  242. burn_stack(sizeof(unsigned *) + sizeof(unsigned) * 4 + sizeof(int));
  243. return err;
  244. }
  245. #endif
  246. /**
  247. Performs a self-test of the RC2 block cipher
  248. @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled
  249. */
  250. int rc2_test(void)
  251. {
  252. #ifndef LTC_TEST
  253. return CRYPT_NOP;
  254. #else
  255. static const struct {
  256. int keylen, bits;
  257. unsigned char key[16], pt[8], ct[8];
  258. } tests[] = {
  259. { 8, 63,
  260. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  261. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  262. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  263. { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff }
  264. },
  265. { 8, 64,
  266. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  267. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  268. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
  269. { 0x27, 0x8b, 0x27, 0xe4, 0x2e, 0x2f, 0x0d, 0x49 }
  270. },
  271. { 8, 64,
  272. { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  273. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  274. { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
  275. { 0x30, 0x64, 0x9e, 0xdf, 0x9b, 0xe7, 0xd2, 0xc2 }
  276. },
  277. { 1, 64,
  278. { 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  279. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  280. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  281. { 0x61, 0xa8, 0xa2, 0x44, 0xad, 0xac, 0xcc, 0xf0 }
  282. },
  283. { 7, 64,
  284. { 0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x00,
  285. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  286. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  287. { 0x6c, 0xcf, 0x43, 0x08, 0x97, 0x4c, 0x26, 0x7f }
  288. },
  289. { 16, 64,
  290. { 0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x7f,
  291. 0x0f, 0x79, 0xc3, 0x84, 0x62, 0x7b, 0xaf, 0xb2 },
  292. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  293. { 0x1a, 0x80, 0x7d, 0x27, 0x2b, 0xbe, 0x5d, 0xb1 }
  294. },
  295. { 16, 128,
  296. { 0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x7f,
  297. 0x0f, 0x79, 0xc3, 0x84, 0x62, 0x7b, 0xaf, 0xb2 },
  298. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  299. { 0x22, 0x69, 0x55, 0x2a, 0xb0, 0xf8, 0x5c, 0xa6 }
  300. }
  301. };
  302. int x, y, err;
  303. symmetric_key skey;
  304. unsigned char tmp[2][8];
  305. for (x = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) {
  306. zeromem(tmp, sizeof(tmp));
  307. if (tests[x].bits == (tests[x].keylen * 8)) {
  308. if ((err = rc2_setup(tests[x].key, tests[x].keylen, 0, &skey)) != CRYPT_OK) {
  309. return err;
  310. }
  311. }
  312. else {
  313. if ((err = rc2_setup_ex(tests[x].key, tests[x].keylen, tests[x].bits, 0, &skey)) != CRYPT_OK) {
  314. return err;
  315. }
  316. }
  317. rc2_ecb_encrypt(tests[x].pt, tmp[0], &skey);
  318. rc2_ecb_decrypt(tmp[0], tmp[1], &skey);
  319. if (ltc_compare_testvector(tmp[0], 8, tests[x].ct, 8, "RC2 CT", x) ||
  320. ltc_compare_testvector(tmp[1], 8, tests[x].pt, 8, "RC2 PT", x)) {
  321. return CRYPT_FAIL_TESTVECTOR;
  322. }
  323. /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
  324. for (y = 0; y < 8; y++) tmp[0][y] = 0;
  325. for (y = 0; y < 1000; y++) rc2_ecb_encrypt(tmp[0], tmp[0], &skey);
  326. for (y = 0; y < 1000; y++) rc2_ecb_decrypt(tmp[0], tmp[0], &skey);
  327. for (y = 0; y < 8; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
  328. }
  329. return CRYPT_OK;
  330. #endif
  331. }
  332. /** Terminate the context
  333. @param skey The scheduled key
  334. */
  335. void rc2_done(symmetric_key *skey)
  336. {
  337. LTC_UNUSED_PARAM(skey);
  338. }
  339. /**
  340. Gets suitable key size
  341. @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable.
  342. @return CRYPT_OK if the input key size is acceptable.
  343. */
  344. int rc2_keysize(int *keysize)
  345. {
  346. LTC_ARGCHK(keysize != NULL);
  347. if (*keysize < 1) {
  348. return CRYPT_INVALID_KEYSIZE;
  349. }
  350. if (*keysize > 128) {
  351. *keysize = 128;
  352. }
  353. return CRYPT_OK;
  354. }
  355. #endif