aes.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. /* AES implementation by Tom St Denis
  12. *
  13. * Derived from the Public Domain source code by
  14. ---
  15. * rijndael-alg-fst.c
  16. *
  17. * @version 3.0 (December 2000)
  18. *
  19. * Optimised ANSI C code for the Rijndael cipher (now AES)
  20. *
  21. * @author Vincent Rijmen <[email protected]>
  22. * @author Antoon Bosselaers <[email protected]>
  23. * @author Paulo Barreto <[email protected]>
  24. ---
  25. */
  26. #include "mycrypt.h"
  27. #ifdef RIJNDAEL
  28. #ifndef ENCRYPT_ONLY
  29. #define SETUP rijndael_setup
  30. #define ECB_ENC rijndael_ecb_encrypt
  31. #define ECB_DEC rijndael_ecb_decrypt
  32. #define ECB_TEST rijndael_test
  33. #define ECB_KS rijndael_keysize
  34. const struct _cipher_descriptor rijndael_desc =
  35. {
  36. "rijndael",
  37. 6,
  38. 16, 32, 16, 10,
  39. SETUP, ECB_ENC, ECB_DEC, ECB_TEST, ECB_KS
  40. };
  41. const struct _cipher_descriptor aes_desc =
  42. {
  43. "aes",
  44. 6,
  45. 16, 32, 16, 10,
  46. SETUP, ECB_ENC, ECB_DEC, ECB_TEST, ECB_KS
  47. };
  48. #else
  49. #define SETUP rijndael_enc_setup
  50. #define ECB_ENC rijndael_enc_ecb_encrypt
  51. #define ECB_KS rijndael_enc_keysize
  52. const struct _cipher_descriptor rijndael_enc_desc =
  53. {
  54. "rijndael",
  55. 6,
  56. 16, 32, 16, 10,
  57. SETUP, ECB_ENC, NULL, NULL, ECB_KS
  58. };
  59. const struct _cipher_descriptor aes_enc_desc =
  60. {
  61. "aes",
  62. 6,
  63. 16, 32, 16, 10,
  64. SETUP, ECB_ENC, NULL, NULL, ECB_KS
  65. };
  66. #endif
  67. #include "aes_tab.c"
  68. static ulong32 setup_mix(ulong32 temp)
  69. {
  70. return (Te4_3[byte(temp, 2)]) ^
  71. (Te4_2[byte(temp, 1)]) ^
  72. (Te4_1[byte(temp, 0)]) ^
  73. (Te4_0[byte(temp, 3)]);
  74. }
  75. #ifndef ENCRYPT_ONLY
  76. #ifdef SMALL_CODE
  77. static ulong32 setup_mix2(ulong32 temp)
  78. {
  79. return Td0(255 & Te4[byte(temp, 3)]) ^
  80. Td1(255 & Te4[byte(temp, 2)]) ^
  81. Td2(255 & Te4[byte(temp, 1)]) ^
  82. Td3(255 & Te4[byte(temp, 0)]);
  83. }
  84. #endif
  85. #endif
  86. int SETUP(const unsigned char *key, int keylen, int rounds, symmetric_key *skey)
  87. {
  88. int i, j;
  89. ulong32 temp, *rk;
  90. #ifndef ENCRYPT_ONLY
  91. ulong32 *rrk;
  92. #endif
  93. _ARGCHK(key != NULL);
  94. _ARGCHK(skey != NULL);
  95. if (keylen != 16 && keylen != 24 && keylen != 32) {
  96. return CRYPT_INVALID_KEYSIZE;
  97. }
  98. if (rounds != 0 && rounds != (10 + ((keylen/8)-2)*2)) {
  99. return CRYPT_INVALID_ROUNDS;
  100. }
  101. skey->rijndael.Nr = 10 + ((keylen/8)-2)*2;
  102. /* setup the forward key */
  103. i = 0;
  104. rk = skey->rijndael.eK;
  105. LOAD32H(rk[0], key );
  106. LOAD32H(rk[1], key + 4);
  107. LOAD32H(rk[2], key + 8);
  108. LOAD32H(rk[3], key + 12);
  109. if (keylen == 16) {
  110. j = 44;
  111. for (;;) {
  112. temp = rk[3];
  113. rk[4] = rk[0] ^ setup_mix(temp) ^ rcon[i];
  114. rk[5] = rk[1] ^ rk[4];
  115. rk[6] = rk[2] ^ rk[5];
  116. rk[7] = rk[3] ^ rk[6];
  117. if (++i == 10) {
  118. break;
  119. }
  120. rk += 4;
  121. }
  122. } else if (keylen == 24) {
  123. j = 52;
  124. LOAD32H(rk[4], key + 16);
  125. LOAD32H(rk[5], key + 20);
  126. for (;;) {
  127. #ifdef _MSC_VER
  128. temp = skey->rijndael.eK[rk - skey->rijndael.eK + 5];
  129. #else
  130. temp = rk[5];
  131. #endif
  132. rk[ 6] = rk[ 0] ^ setup_mix(temp) ^ rcon[i];
  133. rk[ 7] = rk[ 1] ^ rk[ 6];
  134. rk[ 8] = rk[ 2] ^ rk[ 7];
  135. rk[ 9] = rk[ 3] ^ rk[ 8];
  136. if (++i == 8) {
  137. break;
  138. }
  139. rk[10] = rk[ 4] ^ rk[ 9];
  140. rk[11] = rk[ 5] ^ rk[10];
  141. rk += 6;
  142. }
  143. } else if (keylen == 32) {
  144. j = 60;
  145. LOAD32H(rk[4], key + 16);
  146. LOAD32H(rk[5], key + 20);
  147. LOAD32H(rk[6], key + 24);
  148. LOAD32H(rk[7], key + 28);
  149. for (;;) {
  150. #ifdef _MSC_VER
  151. temp = skey->rijndael.eK[rk - skey->rijndael.eK + 7];
  152. #else
  153. temp = rk[7];
  154. #endif
  155. rk[ 8] = rk[ 0] ^ setup_mix(temp) ^ rcon[i];
  156. rk[ 9] = rk[ 1] ^ rk[ 8];
  157. rk[10] = rk[ 2] ^ rk[ 9];
  158. rk[11] = rk[ 3] ^ rk[10];
  159. if (++i == 7) {
  160. break;
  161. }
  162. temp = rk[11];
  163. rk[12] = rk[ 4] ^ setup_mix(ROR(temp, 8));
  164. rk[13] = rk[ 5] ^ rk[12];
  165. rk[14] = rk[ 6] ^ rk[13];
  166. rk[15] = rk[ 7] ^ rk[14];
  167. rk += 8;
  168. }
  169. } else {
  170. /* this can't happen */
  171. j = 4;
  172. }
  173. #ifndef ENCRYPT_ONLY
  174. /* setup the inverse key now */
  175. rk = skey->rijndael.dK;
  176. rrk = skey->rijndael.eK + j - 4;
  177. /* apply the inverse MixColumn transform to all round keys but the first and the last: */
  178. /* copy first */
  179. *rk++ = *rrk++;
  180. *rk++ = *rrk++;
  181. *rk++ = *rrk++;
  182. *rk = *rrk;
  183. rk -= 3; rrk -= 3;
  184. for (i = 1; i < skey->rijndael.Nr; i++) {
  185. rrk -= 4;
  186. rk += 4;
  187. #ifdef SMALL_CODE
  188. temp = rrk[0];
  189. rk[0] = setup_mix2(temp);
  190. temp = rrk[1];
  191. rk[1] = setup_mix2(temp);
  192. temp = rrk[2];
  193. rk[2] = setup_mix2(temp);
  194. temp = rrk[3];
  195. rk[3] = setup_mix2(temp);
  196. #else
  197. temp = rrk[0];
  198. rk[0] =
  199. Tks0[byte(temp, 3)] ^
  200. Tks1[byte(temp, 2)] ^
  201. Tks2[byte(temp, 1)] ^
  202. Tks3[byte(temp, 0)];
  203. temp = rrk[1];
  204. rk[1] =
  205. Tks0[byte(temp, 3)] ^
  206. Tks1[byte(temp, 2)] ^
  207. Tks2[byte(temp, 1)] ^
  208. Tks3[byte(temp, 0)];
  209. temp = rrk[2];
  210. rk[2] =
  211. Tks0[byte(temp, 3)] ^
  212. Tks1[byte(temp, 2)] ^
  213. Tks2[byte(temp, 1)] ^
  214. Tks3[byte(temp, 0)];
  215. temp = rrk[3];
  216. rk[3] =
  217. Tks0[byte(temp, 3)] ^
  218. Tks1[byte(temp, 2)] ^
  219. Tks2[byte(temp, 1)] ^
  220. Tks3[byte(temp, 0)];
  221. #endif
  222. }
  223. /* copy last */
  224. rrk -= 4;
  225. rk += 4;
  226. *rk++ = *rrk++;
  227. *rk++ = *rrk++;
  228. *rk++ = *rrk++;
  229. *rk = *rrk;
  230. #endif /* ENCRYPT_ONLY */
  231. return CRYPT_OK;
  232. }
  233. #ifdef CLEAN_STACK
  234. static void _rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
  235. #else
  236. void ECB_ENC(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
  237. #endif
  238. {
  239. ulong32 s0, s1, s2, s3, t0, t1, t2, t3, *rk;
  240. int Nr, r;
  241. _ARGCHK(pt != NULL);
  242. _ARGCHK(ct != NULL);
  243. _ARGCHK(skey != NULL);
  244. Nr = skey->rijndael.Nr;
  245. rk = skey->rijndael.eK;
  246. /*
  247. * map byte array block to cipher state
  248. * and add initial round key:
  249. */
  250. LOAD32H(s0, pt ); s0 ^= rk[0];
  251. LOAD32H(s1, pt + 4); s1 ^= rk[1];
  252. LOAD32H(s2, pt + 8); s2 ^= rk[2];
  253. LOAD32H(s3, pt + 12); s3 ^= rk[3];
  254. #ifdef SMALL_CODE
  255. for (r = 0; ; r++) {
  256. rk += 4;
  257. t0 =
  258. Te0(byte(s0, 3)) ^
  259. Te1(byte(s1, 2)) ^
  260. Te2(byte(s2, 1)) ^
  261. Te3(byte(s3, 0)) ^
  262. rk[0];
  263. t1 =
  264. Te0(byte(s1, 3)) ^
  265. Te1(byte(s2, 2)) ^
  266. Te2(byte(s3, 1)) ^
  267. Te3(byte(s0, 0)) ^
  268. rk[1];
  269. t2 =
  270. Te0(byte(s2, 3)) ^
  271. Te1(byte(s3, 2)) ^
  272. Te2(byte(s0, 1)) ^
  273. Te3(byte(s1, 0)) ^
  274. rk[2];
  275. t3 =
  276. Te0(byte(s3, 3)) ^
  277. Te1(byte(s0, 2)) ^
  278. Te2(byte(s1, 1)) ^
  279. Te3(byte(s2, 0)) ^
  280. rk[3];
  281. if (r == Nr-2) {
  282. break;
  283. }
  284. s0 = t0; s1 = t1; s2 = t2; s3 = t3;
  285. }
  286. rk += 4;
  287. #else
  288. /*
  289. * Nr - 1 full rounds:
  290. */
  291. r = Nr >> 1;
  292. for (;;) {
  293. t0 =
  294. Te0(byte(s0, 3)) ^
  295. Te1(byte(s1, 2)) ^
  296. Te2(byte(s2, 1)) ^
  297. Te3(byte(s3, 0)) ^
  298. rk[4];
  299. t1 =
  300. Te0(byte(s1, 3)) ^
  301. Te1(byte(s2, 2)) ^
  302. Te2(byte(s3, 1)) ^
  303. Te3(byte(s0, 0)) ^
  304. rk[5];
  305. t2 =
  306. Te0(byte(s2, 3)) ^
  307. Te1(byte(s3, 2)) ^
  308. Te2(byte(s0, 1)) ^
  309. Te3(byte(s1, 0)) ^
  310. rk[6];
  311. t3 =
  312. Te0(byte(s3, 3)) ^
  313. Te1(byte(s0, 2)) ^
  314. Te2(byte(s1, 1)) ^
  315. Te3(byte(s2, 0)) ^
  316. rk[7];
  317. rk += 8;
  318. if (--r == 0) {
  319. break;
  320. }
  321. s0 =
  322. Te0(byte(t0, 3)) ^
  323. Te1(byte(t1, 2)) ^
  324. Te2(byte(t2, 1)) ^
  325. Te3(byte(t3, 0)) ^
  326. rk[0];
  327. s1 =
  328. Te0(byte(t1, 3)) ^
  329. Te1(byte(t2, 2)) ^
  330. Te2(byte(t3, 1)) ^
  331. Te3(byte(t0, 0)) ^
  332. rk[1];
  333. s2 =
  334. Te0(byte(t2, 3)) ^
  335. Te1(byte(t3, 2)) ^
  336. Te2(byte(t0, 1)) ^
  337. Te3(byte(t1, 0)) ^
  338. rk[2];
  339. s3 =
  340. Te0(byte(t3, 3)) ^
  341. Te1(byte(t0, 2)) ^
  342. Te2(byte(t1, 1)) ^
  343. Te3(byte(t2, 0)) ^
  344. rk[3];
  345. }
  346. #endif
  347. /*
  348. * apply last round and
  349. * map cipher state to byte array block:
  350. */
  351. s0 =
  352. (Te4_3[byte(t0, 3)]) ^
  353. (Te4_2[byte(t1, 2)]) ^
  354. (Te4_1[byte(t2, 1)]) ^
  355. (Te4_0[byte(t3, 0)]) ^
  356. rk[0];
  357. STORE32H(s0, ct);
  358. s1 =
  359. (Te4_3[byte(t1, 3)]) ^
  360. (Te4_2[byte(t2, 2)]) ^
  361. (Te4_1[byte(t3, 1)]) ^
  362. (Te4_0[byte(t0, 0)]) ^
  363. rk[1];
  364. STORE32H(s1, ct+4);
  365. s2 =
  366. (Te4_3[byte(t2, 3)]) ^
  367. (Te4_2[byte(t3, 2)]) ^
  368. (Te4_1[byte(t0, 1)]) ^
  369. (Te4_0[byte(t1, 0)]) ^
  370. rk[2];
  371. STORE32H(s2, ct+8);
  372. s3 =
  373. (Te4_3[byte(t3, 3)]) ^
  374. (Te4_2[byte(t0, 2)]) ^
  375. (Te4_1[byte(t1, 1)]) ^
  376. (Te4_0[byte(t2, 0)]) ^
  377. rk[3];
  378. STORE32H(s3, ct+12);
  379. }
  380. #ifdef CLEAN_STACK
  381. void ECB_ENC(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
  382. {
  383. _rijndael_ecb_encrypt(pt, ct, skey);
  384. burn_stack(sizeof(unsigned long)*8 + sizeof(unsigned long*) + sizeof(int)*2);
  385. }
  386. #endif
  387. #ifndef ENCRYPT_ONLY
  388. #ifdef CLEAN_STACK
  389. static void _rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
  390. #else
  391. void ECB_DEC(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
  392. #endif
  393. {
  394. ulong32 s0, s1, s2, s3, t0, t1, t2, t3, *rk;
  395. int Nr, r;
  396. _ARGCHK(pt != NULL);
  397. _ARGCHK(ct != NULL);
  398. _ARGCHK(skey != NULL);
  399. Nr = skey->rijndael.Nr;
  400. rk = skey->rijndael.dK;
  401. /*
  402. * map byte array block to cipher state
  403. * and add initial round key:
  404. */
  405. LOAD32H(s0, ct ); s0 ^= rk[0];
  406. LOAD32H(s1, ct + 4); s1 ^= rk[1];
  407. LOAD32H(s2, ct + 8); s2 ^= rk[2];
  408. LOAD32H(s3, ct + 12); s3 ^= rk[3];
  409. #ifdef SMALL_CODE
  410. for (r = 0; ; r++) {
  411. rk += 4;
  412. t0 =
  413. Td0(byte(s0, 3)) ^
  414. Td1(byte(s3, 2)) ^
  415. Td2(byte(s2, 1)) ^
  416. Td3(byte(s1, 0)) ^
  417. rk[0];
  418. t1 =
  419. Td0(byte(s1, 3)) ^
  420. Td1(byte(s0, 2)) ^
  421. Td2(byte(s3, 1)) ^
  422. Td3(byte(s2, 0)) ^
  423. rk[1];
  424. t2 =
  425. Td0(byte(s2, 3)) ^
  426. Td1(byte(s1, 2)) ^
  427. Td2(byte(s0, 1)) ^
  428. Td3(byte(s3, 0)) ^
  429. rk[2];
  430. t3 =
  431. Td0(byte(s3, 3)) ^
  432. Td1(byte(s2, 2)) ^
  433. Td2(byte(s1, 1)) ^
  434. Td3(byte(s0, 0)) ^
  435. rk[3];
  436. if (r == Nr-2) {
  437. break;
  438. }
  439. s0 = t0; s1 = t1; s2 = t2; s3 = t3;
  440. }
  441. rk += 4;
  442. #else
  443. /*
  444. * Nr - 1 full rounds:
  445. */
  446. r = Nr >> 1;
  447. for (;;) {
  448. t0 =
  449. Td0(byte(s0, 3)) ^
  450. Td1(byte(s3, 2)) ^
  451. Td2(byte(s2, 1)) ^
  452. Td3(byte(s1, 0)) ^
  453. rk[4];
  454. t1 =
  455. Td0(byte(s1, 3)) ^
  456. Td1(byte(s0, 2)) ^
  457. Td2(byte(s3, 1)) ^
  458. Td3(byte(s2, 0)) ^
  459. rk[5];
  460. t2 =
  461. Td0(byte(s2, 3)) ^
  462. Td1(byte(s1, 2)) ^
  463. Td2(byte(s0, 1)) ^
  464. Td3(byte(s3, 0)) ^
  465. rk[6];
  466. t3 =
  467. Td0(byte(s3, 3)) ^
  468. Td1(byte(s2, 2)) ^
  469. Td2(byte(s1, 1)) ^
  470. Td3(byte(s0, 0)) ^
  471. rk[7];
  472. rk += 8;
  473. if (--r == 0) {
  474. break;
  475. }
  476. s0 =
  477. Td0(byte(t0, 3)) ^
  478. Td1(byte(t3, 2)) ^
  479. Td2(byte(t2, 1)) ^
  480. Td3(byte(t1, 0)) ^
  481. rk[0];
  482. s1 =
  483. Td0(byte(t1, 3)) ^
  484. Td1(byte(t0, 2)) ^
  485. Td2(byte(t3, 1)) ^
  486. Td3(byte(t2, 0)) ^
  487. rk[1];
  488. s2 =
  489. Td0(byte(t2, 3)) ^
  490. Td1(byte(t1, 2)) ^
  491. Td2(byte(t0, 1)) ^
  492. Td3(byte(t3, 0)) ^
  493. rk[2];
  494. s3 =
  495. Td0(byte(t3, 3)) ^
  496. Td1(byte(t2, 2)) ^
  497. Td2(byte(t1, 1)) ^
  498. Td3(byte(t0, 0)) ^
  499. rk[3];
  500. }
  501. #endif
  502. /*
  503. * apply last round and
  504. * map cipher state to byte array block:
  505. */
  506. s0 =
  507. (Td4[byte(t0, 3)] & 0xff000000) ^
  508. (Td4[byte(t3, 2)] & 0x00ff0000) ^
  509. (Td4[byte(t2, 1)] & 0x0000ff00) ^
  510. (Td4[byte(t1, 0)] & 0x000000ff) ^
  511. rk[0];
  512. STORE32H(s0, pt);
  513. s1 =
  514. (Td4[byte(t1, 3)] & 0xff000000) ^
  515. (Td4[byte(t0, 2)] & 0x00ff0000) ^
  516. (Td4[byte(t3, 1)] & 0x0000ff00) ^
  517. (Td4[byte(t2, 0)] & 0x000000ff) ^
  518. rk[1];
  519. STORE32H(s1, pt+4);
  520. s2 =
  521. (Td4[byte(t2, 3)] & 0xff000000) ^
  522. (Td4[byte(t1, 2)] & 0x00ff0000) ^
  523. (Td4[byte(t0, 1)] & 0x0000ff00) ^
  524. (Td4[byte(t3, 0)] & 0x000000ff) ^
  525. rk[2];
  526. STORE32H(s2, pt+8);
  527. s3 =
  528. (Td4[byte(t3, 3)] & 0xff000000) ^
  529. (Td4[byte(t2, 2)] & 0x00ff0000) ^
  530. (Td4[byte(t1, 1)] & 0x0000ff00) ^
  531. (Td4[byte(t0, 0)] & 0x000000ff) ^
  532. rk[3];
  533. STORE32H(s3, pt+12);
  534. }
  535. #ifdef CLEAN_STACK
  536. void ECB_DEC(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
  537. {
  538. _rijndael_ecb_decrypt(ct, pt, skey);
  539. burn_stack(sizeof(unsigned long)*8 + sizeof(unsigned long*) + sizeof(int)*2);
  540. }
  541. #endif
  542. int ECB_TEST(void)
  543. {
  544. #ifndef LTC_TEST
  545. return CRYPT_NOP;
  546. #else
  547. int err;
  548. static const struct {
  549. int keylen;
  550. unsigned char key[32], pt[16], ct[16];
  551. } tests[] = {
  552. { 16,
  553. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  554. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  555. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  556. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
  557. { 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30,
  558. 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a }
  559. }, {
  560. 24,
  561. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  562. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  563. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 },
  564. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  565. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
  566. { 0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0,
  567. 0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91 }
  568. }, {
  569. 32,
  570. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  571. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  572. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  573. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
  574. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  575. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
  576. { 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf,
  577. 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 }
  578. }
  579. };
  580. symmetric_key key;
  581. unsigned char tmp[2][16];
  582. int i, y;
  583. for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
  584. zeromem(&key, sizeof(key));
  585. if ((err = rijndael_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
  586. return err;
  587. }
  588. rijndael_ecb_encrypt(tests[i].pt, tmp[0], &key);
  589. rijndael_ecb_decrypt(tmp[0], tmp[1], &key);
  590. if (memcmp(tmp[0], tests[i].ct, 16) || memcmp(tmp[1], tests[i].pt, 16)) {
  591. #if 0
  592. printf("\n\nTest %d failed\n", i);
  593. if (memcmp(tmp[0], tests[i].ct, 16)) {
  594. printf("CT: ");
  595. for (i = 0; i < 16; i++) {
  596. printf("%02x ", tmp[0][i]);
  597. }
  598. printf("\n");
  599. } else {
  600. printf("PT: ");
  601. for (i = 0; i < 16; i++) {
  602. printf("%02x ", tmp[1][i]);
  603. }
  604. printf("\n");
  605. }
  606. #endif
  607. return CRYPT_FAIL_TESTVECTOR;
  608. }
  609. /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
  610. for (y = 0; y < 16; y++) tmp[0][y] = 0;
  611. for (y = 0; y < 1000; y++) rijndael_ecb_encrypt(tmp[0], tmp[0], &key);
  612. for (y = 0; y < 1000; y++) rijndael_ecb_decrypt(tmp[0], tmp[0], &key);
  613. for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
  614. }
  615. return CRYPT_OK;
  616. #endif
  617. }
  618. #endif /* ENCRYPT_ONLY */
  619. int ECB_KS(int *desired_keysize)
  620. {
  621. _ARGCHK(desired_keysize != NULL);
  622. if (*desired_keysize < 16)
  623. return CRYPT_INVALID_KEYSIZE;
  624. if (*desired_keysize < 24) {
  625. *desired_keysize = 16;
  626. return CRYPT_OK;
  627. } else if (*desired_keysize < 32) {
  628. *desired_keysize = 24;
  629. return CRYPT_OK;
  630. } else {
  631. *desired_keysize = 32;
  632. return CRYPT_OK;
  633. }
  634. }
  635. #endif