test-aes.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * Test program for AES
  3. * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "crypto/crypto.h"
  11. #include "crypto/aes_wrap.h"
  12. #define BLOCK_SIZE 16
  13. static void test_aes_perf(void)
  14. {
  15. #if 0 /* this did not seem to work with new compiler?! */
  16. #ifdef __i386__
  17. #define rdtscll(val) \
  18. __asm__ __volatile__("rdtsc" : "=A" (val))
  19. const int num_iters = 10;
  20. int i;
  21. unsigned int start, end;
  22. u8 key[16], pt[16], ct[16];
  23. void *ctx;
  24. printf("keySetupEnc:");
  25. for (i = 0; i < num_iters; i++) {
  26. rdtscll(start);
  27. ctx = aes_encrypt_init(key, 16);
  28. rdtscll(end);
  29. aes_encrypt_deinit(ctx);
  30. printf(" %d", end - start);
  31. }
  32. printf("\n");
  33. printf("Encrypt:");
  34. ctx = aes_encrypt_init(key, 16);
  35. for (i = 0; i < num_iters; i++) {
  36. rdtscll(start);
  37. aes_encrypt(ctx, pt, ct);
  38. rdtscll(end);
  39. printf(" %d", end - start);
  40. }
  41. aes_encrypt_deinit(ctx);
  42. printf("\n");
  43. #endif /* __i386__ */
  44. #endif
  45. }
  46. /*
  47. * GCM test vectors from
  48. * http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-spec.pdf
  49. */
  50. struct gcm_test_vector {
  51. char *k;
  52. char *p;
  53. char *aad;
  54. char *iv;
  55. char *c;
  56. char *t;
  57. };
  58. static const struct gcm_test_vector gcm_tests[] = {
  59. {
  60. /* Test Case 1 */
  61. "00000000000000000000000000000000",
  62. "",
  63. "",
  64. "000000000000000000000000",
  65. "",
  66. "58e2fccefa7e3061367f1d57a4e7455a"
  67. },
  68. {
  69. /* Test Case 2 */
  70. "00000000000000000000000000000000",
  71. "00000000000000000000000000000000",
  72. "",
  73. "000000000000000000000000",
  74. "0388dace60b6a392f328c2b971b2fe78",
  75. "ab6e47d42cec13bdf53a67b21257bddf"
  76. },
  77. {
  78. /* Test Case 3 */
  79. "feffe9928665731c6d6a8f9467308308",
  80. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
  81. "",
  82. "cafebabefacedbaddecaf888",
  83. "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f5985",
  84. "4d5c2af327cd64a62cf35abd2ba6fab4"
  85. },
  86. {
  87. /* Test Case 4 */
  88. "feffe9928665731c6d6a8f9467308308",
  89. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  90. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  91. "cafebabefacedbaddecaf888",
  92. "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091",
  93. "5bc94fbc3221a5db94fae95ae7121a47"
  94. },
  95. {
  96. /* Test Case 5 */
  97. "feffe9928665731c6d6a8f9467308308",
  98. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  99. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  100. "cafebabefacedbad",
  101. "61353b4c2806934a777ff51fa22a4755699b2a714fcdc6f83766e5f97b6c742373806900e49f24b22b097544d4896b424989b5e1ebac0f07c23f4598",
  102. "3612d2e79e3b0785561be14aaca2fccb"
  103. },
  104. {
  105. /* Test Case 6 */
  106. "feffe9928665731c6d6a8f9467308308",
  107. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  108. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  109. "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
  110. "8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca701e4a9a4fba43c90ccdcb281d48c7c6fd62875d2aca417034c34aee5",
  111. "619cc5aefffe0bfa462af43c1699d050"
  112. },
  113. {
  114. /* Test Case 7 */
  115. "000000000000000000000000000000000000000000000000",
  116. "",
  117. "",
  118. "000000000000000000000000",
  119. "",
  120. "cd33b28ac773f74ba00ed1f312572435"
  121. },
  122. {
  123. /* Test Case 8 */
  124. "000000000000000000000000000000000000000000000000",
  125. "00000000000000000000000000000000",
  126. "",
  127. "000000000000000000000000",
  128. "98e7247c07f0fe411c267e4384b0f600",
  129. "2ff58d80033927ab8ef4d4587514f0fb"
  130. },
  131. {
  132. /* Test Case 9 */
  133. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  134. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
  135. "",
  136. "cafebabefacedbaddecaf888",
  137. "3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710acade256",
  138. "9924a7c8587336bfb118024db8674a14"
  139. },
  140. {
  141. /* Test Case 10 */
  142. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  143. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  144. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  145. "cafebabefacedbaddecaf888",
  146. "3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710",
  147. "2519498e80f1478f37ba55bd6d27618c"
  148. },
  149. {
  150. /* Test Case 11 */
  151. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  152. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  153. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  154. "cafebabefacedbad",
  155. "0f10f599ae14a154ed24b36e25324db8c566632ef2bbb34f8347280fc4507057fddc29df9a471f75c66541d4d4dad1c9e93a19a58e8b473fa0f062f7",
  156. "65dcc57fcf623a24094fcca40d3533f8"
  157. },
  158. {
  159. /* Test Case 12 */
  160. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  161. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  162. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  163. "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
  164. "d27e88681ce3243c4830165a8fdcf9ff1de9a1d8e6b447ef6ef7b79828666e4581e79012af34ddd9e2f037589b292db3e67c036745fa22e7e9b7373b",
  165. "dcf566ff291c25bbb8568fc3d376a6d9"
  166. },
  167. {
  168. /* Test Case 13 */
  169. "0000000000000000000000000000000000000000000000000000000000000000",
  170. "",
  171. "",
  172. "000000000000000000000000",
  173. "",
  174. "530f8afbc74536b9a963b4f1c4cb738b"
  175. },
  176. {
  177. /* Test Case 14 */
  178. "0000000000000000000000000000000000000000000000000000000000000000",
  179. "00000000000000000000000000000000",
  180. "",
  181. "000000000000000000000000",
  182. "cea7403d4d606b6e074ec5d3baf39d18",
  183. "d0d1c8a799996bf0265b98b5d48ab919"
  184. },
  185. {
  186. /* Test Case 15 */
  187. "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
  188. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
  189. "",
  190. "cafebabefacedbaddecaf888",
  191. "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662898015ad",
  192. "b094dac5d93471bdec1a502270e3cc6c"
  193. },
  194. {
  195. /* Test Case 16 */
  196. "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
  197. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  198. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  199. "cafebabefacedbaddecaf888",
  200. "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662",
  201. "76fc6ece0f4e1768cddf8853bb2d551b"
  202. },
  203. {
  204. /* Test Case 17 */
  205. "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
  206. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  207. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  208. "cafebabefacedbad",
  209. "c3762df1ca787d32ae47c13bf19844cbaf1ae14d0b976afac52ff7d79bba9de0feb582d33934a4f0954cc2363bc73f7862ac430e64abe499f47c9b1f",
  210. "3a337dbf46a792c45e454913fe2ea8f2"
  211. },
  212. {
  213. /* Test Case 18 */
  214. "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
  215. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  216. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  217. "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
  218. "5a8def2f0c9e53f1f75d7853659e2a20eeb2b22aafde6419a058ab4f6f746bf40fc0c3b780f244452da3ebf1c5d82cdea2418997200ef82e44ae7e3f",
  219. "a44a8266ee1c8eb0c8b5d4cf5ae9f19a"
  220. }
  221. };
  222. static int test_gcm(void)
  223. {
  224. int ret = 0;
  225. int i;
  226. u8 k[32], aad[32], iv[64], t[16], tag[16];
  227. u8 p[64], c[64], tmp[64];
  228. size_t k_len, p_len, aad_len, iv_len;
  229. for (i = 0; i < ARRAY_SIZE(gcm_tests); i++) {
  230. const struct gcm_test_vector *tc = &gcm_tests[i];
  231. k_len = os_strlen(tc->k) / 2;
  232. if (hexstr2bin(tc->k, k, k_len)) {
  233. printf("Invalid GCM test vector %d (k)\n", i);
  234. ret++;
  235. continue;
  236. }
  237. p_len = os_strlen(tc->p) / 2;
  238. if (hexstr2bin(tc->p, p, p_len)) {
  239. printf("Invalid GCM test vector %d (p)\n", i);
  240. ret++;
  241. continue;
  242. }
  243. aad_len = os_strlen(tc->aad) / 2;
  244. if (hexstr2bin(tc->aad, aad, aad_len)) {
  245. printf("Invalid GCM test vector %d (aad)\n", i);
  246. ret++;
  247. continue;
  248. }
  249. iv_len = os_strlen(tc->iv) / 2;
  250. if (hexstr2bin(tc->iv, iv, iv_len)) {
  251. printf("Invalid GCM test vector %d (iv)\n", i);
  252. ret++;
  253. continue;
  254. }
  255. if (hexstr2bin(tc->c, c, p_len)) {
  256. printf("Invalid GCM test vector %d (c)\n", i);
  257. ret++;
  258. continue;
  259. }
  260. if (hexstr2bin(tc->t, t, sizeof(t))) {
  261. printf("Invalid GCM test vector %d (t)\n", i);
  262. ret++;
  263. continue;
  264. }
  265. if (aes_gcm_ae(k, k_len, iv, iv_len, p, p_len, aad, aad_len,
  266. tmp, tag) < 0) {
  267. printf("GCM-AE failed (test case %d)\n", i);
  268. ret++;
  269. continue;
  270. }
  271. if (os_memcmp(c, tmp, p_len) != 0) {
  272. printf("GCM-AE mismatch (test case %d)\n", i);
  273. ret++;
  274. }
  275. if (os_memcmp(tag, t, sizeof(tag)) != 0) {
  276. printf("GCM-AE tag mismatch (test case %d)\n", i);
  277. ret++;
  278. }
  279. if (p_len == 0) {
  280. if (aes_gmac(k, k_len, iv, iv_len, aad, aad_len, tag) <
  281. 0) {
  282. printf("GMAC failed (test case %d)\n", i);
  283. ret++;
  284. continue;
  285. }
  286. if (os_memcmp(tag, t, sizeof(tag)) != 0) {
  287. printf("GMAC tag mismatch (test case %d)\n", i);
  288. ret++;
  289. }
  290. }
  291. if (aes_gcm_ad(k, k_len, iv, iv_len, c, p_len, aad, aad_len,
  292. t, tmp) < 0) {
  293. printf("GCM-AD failed (test case %d)\n", i);
  294. ret++;
  295. continue;
  296. }
  297. if (os_memcmp(p, tmp, p_len) != 0) {
  298. printf("GCM-AD mismatch (test case %d)\n", i);
  299. ret++;
  300. }
  301. }
  302. return ret;
  303. }
  304. static int test_nist_key_wrap_ae(const char *fname)
  305. {
  306. FILE *f;
  307. int ret = 0;
  308. char buf[15000], *pos, *pos2;
  309. u8 bin[2000], k[32], p[1024], c[1024 + 8], result[1024 + 8];
  310. size_t bin_len, k_len = 0, p_len = 0, c_len = 0;
  311. int ok = 0;
  312. printf("NIST KW AE tests from %s\n", fname);
  313. f = fopen(fname, "r");
  314. if (f == NULL) {
  315. printf("%s does not exist - cannot validate test vectors\n",
  316. fname);
  317. return 1;
  318. }
  319. while (fgets(buf, sizeof(buf), f)) {
  320. if (buf[0] == '#')
  321. continue;
  322. pos = os_strchr(buf, '=');
  323. if (pos == NULL)
  324. continue;
  325. pos2 = pos - 1;
  326. while (pos2 >= buf && *pos2 == ' ')
  327. *pos2-- = '\0';
  328. *pos++ = '\0';
  329. while (*pos == ' ')
  330. *pos++ = '\0';
  331. pos2 = os_strchr(pos, '\r');
  332. if (!pos2)
  333. pos2 = os_strchr(pos, '\n');
  334. if (pos2)
  335. *pos2 = '\0';
  336. else
  337. pos2 = pos + os_strlen(pos);
  338. if (buf[0] == '[') {
  339. printf("%s = %s\n", buf, pos);
  340. continue;
  341. }
  342. if (os_strcmp(buf, "COUNT") == 0) {
  343. printf("Test %s - ", pos);
  344. continue;
  345. }
  346. bin_len = os_strlen(pos);
  347. if (bin_len > sizeof(bin) * 2) {
  348. printf("Too long binary data (%s)\n", buf);
  349. return 1;
  350. }
  351. if (bin_len & 0x01) {
  352. printf("Odd number of hexstring values (%s)\n",
  353. buf);
  354. return 1;
  355. }
  356. bin_len /= 2;
  357. if (hexstr2bin(pos, bin, bin_len) < 0) {
  358. printf("Invalid hex string '%s' (%s)\n", pos, buf);
  359. return 1;
  360. }
  361. if (os_strcmp(buf, "K") == 0) {
  362. if (bin_len > sizeof(k)) {
  363. printf("Too long K (%u)\n", (unsigned) bin_len);
  364. return 1;
  365. }
  366. os_memcpy(k, bin, bin_len);
  367. k_len = bin_len;
  368. continue;
  369. }
  370. if (os_strcmp(buf, "P") == 0) {
  371. if (bin_len > sizeof(p)) {
  372. printf("Too long P (%u)\n", (unsigned) bin_len);
  373. return 1;
  374. }
  375. os_memcpy(p, bin, bin_len);
  376. p_len = bin_len;
  377. continue;
  378. }
  379. if (os_strcmp(buf, "C") != 0) {
  380. printf("Unexpected field '%s'\n", buf);
  381. continue;
  382. }
  383. if (bin_len > sizeof(c)) {
  384. printf("Too long C (%u)\n", (unsigned) bin_len);
  385. return 1;
  386. }
  387. os_memcpy(c, bin, bin_len);
  388. c_len = bin_len;
  389. if (p_len % 8 != 0 || c_len % 8 != 0 || c_len - p_len != 8) {
  390. printf("invalid parameter length (p_len=%u c_len=%u)\n",
  391. (unsigned) p_len, (unsigned) c_len);
  392. continue;
  393. }
  394. if (aes_wrap(k, k_len, p_len / 8, p, result)) {
  395. printf("aes_wrap() failed\n");
  396. ret++;
  397. continue;
  398. }
  399. if (os_memcmp(c, result, c_len) == 0) {
  400. printf("OK\n");
  401. ok++;
  402. } else {
  403. printf("FAIL\n");
  404. ret++;
  405. }
  406. }
  407. fclose(f);
  408. if (ret)
  409. printf("Test case failed\n");
  410. else
  411. printf("%d test vectors OK\n", ok);
  412. return ret;
  413. }
  414. static int test_nist_key_wrap_ad(const char *fname)
  415. {
  416. FILE *f;
  417. int ret = 0;
  418. char buf[15000], *pos, *pos2;
  419. u8 bin[2000], k[32], p[1024], c[1024 + 8], result[1024 + 8];
  420. size_t bin_len, k_len = 0, p_len = 0, c_len = 0;
  421. int ok = 0;
  422. int fail;
  423. printf("NIST KW AD tests from %s\n", fname);
  424. f = fopen(fname, "r");
  425. if (f == NULL) {
  426. printf("%s does not exist - cannot validate test vectors\n",
  427. fname);
  428. return 1;
  429. }
  430. while (fgets(buf, sizeof(buf), f)) {
  431. if (buf[0] == '#')
  432. continue;
  433. fail = 0;
  434. pos = os_strchr(buf, '=');
  435. if (pos == NULL) {
  436. if (os_strncmp(buf, "FAIL", 4) == 0) {
  437. fail = 1;
  438. goto skip_val_parse;
  439. }
  440. continue;
  441. }
  442. pos2 = pos - 1;
  443. while (pos2 >= buf && *pos2 == ' ')
  444. *pos2-- = '\0';
  445. *pos++ = '\0';
  446. while (*pos == ' ')
  447. *pos++ = '\0';
  448. pos2 = os_strchr(pos, '\r');
  449. if (!pos2)
  450. pos2 = os_strchr(pos, '\n');
  451. if (pos2)
  452. *pos2 = '\0';
  453. else
  454. pos2 = pos + os_strlen(pos);
  455. if (buf[0] == '[') {
  456. printf("%s = %s\n", buf, pos);
  457. continue;
  458. }
  459. if (os_strcmp(buf, "COUNT") == 0) {
  460. printf("Test %s - ", pos);
  461. continue;
  462. }
  463. bin_len = os_strlen(pos);
  464. if (bin_len > sizeof(bin) * 2) {
  465. printf("Too long binary data (%s)\n", buf);
  466. return 1;
  467. }
  468. if (bin_len & 0x01) {
  469. printf("Odd number of hexstring values (%s)\n",
  470. buf);
  471. return 1;
  472. }
  473. bin_len /= 2;
  474. if (hexstr2bin(pos, bin, bin_len) < 0) {
  475. printf("Invalid hex string '%s' (%s)\n", pos, buf);
  476. return 1;
  477. }
  478. if (os_strcmp(buf, "K") == 0) {
  479. if (bin_len > sizeof(k)) {
  480. printf("Too long K (%u)\n", (unsigned) bin_len);
  481. return 1;
  482. }
  483. os_memcpy(k, bin, bin_len);
  484. k_len = bin_len;
  485. continue;
  486. }
  487. if (os_strcmp(buf, "C") == 0) {
  488. if (bin_len > sizeof(c)) {
  489. printf("Too long C (%u)\n", (unsigned) bin_len);
  490. return 1;
  491. }
  492. os_memcpy(c, bin, bin_len);
  493. c_len = bin_len;
  494. continue;
  495. }
  496. skip_val_parse:
  497. if (!fail) {
  498. if (os_strcmp(buf, "P") != 0) {
  499. printf("Unexpected field '%s'\n", buf);
  500. continue;
  501. }
  502. if (bin_len > sizeof(p)) {
  503. printf("Too long P (%u)\n", (unsigned) bin_len);
  504. return 1;
  505. }
  506. os_memcpy(p, bin, bin_len);
  507. p_len = bin_len;
  508. if (p_len % 8 != 0 || c_len % 8 != 0 ||
  509. c_len - p_len != 8) {
  510. printf("invalid parameter length (p_len=%u c_len=%u)\n",
  511. (unsigned) p_len, (unsigned) c_len);
  512. continue;
  513. }
  514. }
  515. if (aes_unwrap(k, k_len, (c_len / 8) - 1, c, result)) {
  516. if (fail) {
  517. printf("OK (fail reported)\n");
  518. ok++;
  519. continue;
  520. }
  521. printf("aes_unwrap() failed\n");
  522. ret++;
  523. continue;
  524. }
  525. if (fail) {
  526. printf("FAIL (mismatch not reported)\n");
  527. ret++;
  528. } else if (os_memcmp(p, result, p_len) == 0) {
  529. printf("OK\n");
  530. ok++;
  531. } else {
  532. printf("FAIL\n");
  533. ret++;
  534. }
  535. }
  536. fclose(f);
  537. if (ret)
  538. printf("Test case failed\n");
  539. else
  540. printf("%d test vectors OK\n", ok);
  541. return ret;
  542. }
  543. int main(int argc, char *argv[])
  544. {
  545. int ret = 0;
  546. if (argc >= 3 && os_strcmp(argv[1], "NIST-KW-AE") == 0)
  547. ret += test_nist_key_wrap_ae(argv[2]);
  548. else if (argc >= 3 && os_strcmp(argv[1], "NIST-KW-AD") == 0)
  549. ret += test_nist_key_wrap_ad(argv[2]);
  550. test_aes_perf();
  551. ret += test_gcm();
  552. if (ret)
  553. printf("FAILED!\n");
  554. return ret;
  555. }