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