test-aes.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. static int test_eax(void)
  47. {
  48. u8 msg[] = { 0xF7, 0xFB };
  49. u8 key[] = { 0x91, 0x94, 0x5D, 0x3F, 0x4D, 0xCB, 0xEE, 0x0B,
  50. 0xF4, 0x5E, 0xF5, 0x22, 0x55, 0xF0, 0x95, 0xA4 };
  51. u8 nonce[] = { 0xBE, 0xCA, 0xF0, 0x43, 0xB0, 0xA2, 0x3D, 0x84,
  52. 0x31, 0x94, 0xBA, 0x97, 0x2C, 0x66, 0xDE, 0xBD };
  53. u8 hdr[] = { 0xFA, 0x3B, 0xFD, 0x48, 0x06, 0xEB, 0x53, 0xFA };
  54. u8 cipher[] = { 0x19, 0xDD, 0x5C, 0x4C, 0x93, 0x31, 0x04, 0x9D,
  55. 0x0B, 0xDA, 0xB0, 0x27, 0x74, 0x08, 0xF6, 0x79,
  56. 0x67, 0xE5 };
  57. u8 data[sizeof(msg)], tag[BLOCK_SIZE];
  58. memcpy(data, msg, sizeof(msg));
  59. if (aes_128_eax_encrypt(key, nonce, sizeof(nonce), hdr, sizeof(hdr),
  60. data, sizeof(data), tag)) {
  61. printf("AES-128 EAX mode encryption failed\n");
  62. return 1;
  63. }
  64. if (memcmp(data, cipher, sizeof(data)) != 0) {
  65. printf("AES-128 EAX mode encryption returned invalid cipher "
  66. "text\n");
  67. return 1;
  68. }
  69. if (memcmp(tag, cipher + sizeof(data), BLOCK_SIZE) != 0) {
  70. printf("AES-128 EAX mode encryption returned invalid tag\n");
  71. return 1;
  72. }
  73. if (aes_128_eax_decrypt(key, nonce, sizeof(nonce), hdr, sizeof(hdr),
  74. data, sizeof(data), tag)) {
  75. printf("AES-128 EAX mode decryption failed\n");
  76. return 1;
  77. }
  78. if (memcmp(data, msg, sizeof(data)) != 0) {
  79. printf("AES-128 EAX mode decryption returned invalid plain "
  80. "text\n");
  81. return 1;
  82. }
  83. return 0;
  84. }
  85. static int test_cbc(void)
  86. {
  87. struct cbc_test_vector {
  88. u8 key[16];
  89. u8 iv[16];
  90. u8 plain[32];
  91. u8 cipher[32];
  92. size_t len;
  93. } vectors[] = {
  94. {
  95. { 0x06, 0xa9, 0x21, 0x40, 0x36, 0xb8, 0xa1, 0x5b,
  96. 0x51, 0x2e, 0x03, 0xd5, 0x34, 0x12, 0x00, 0x06 },
  97. { 0x3d, 0xaf, 0xba, 0x42, 0x9d, 0x9e, 0xb4, 0x30,
  98. 0xb4, 0x22, 0xda, 0x80, 0x2c, 0x9f, 0xac, 0x41 },
  99. "Single block msg",
  100. { 0xe3, 0x53, 0x77, 0x9c, 0x10, 0x79, 0xae, 0xb8,
  101. 0x27, 0x08, 0x94, 0x2d, 0xbe, 0x77, 0x18, 0x1a },
  102. 16
  103. },
  104. {
  105. { 0xc2, 0x86, 0x69, 0x6d, 0x88, 0x7c, 0x9a, 0xa0,
  106. 0x61, 0x1b, 0xbb, 0x3e, 0x20, 0x25, 0xa4, 0x5a },
  107. { 0x56, 0x2e, 0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28,
  108. 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58 },
  109. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  110. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  111. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  112. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
  113. { 0xd2, 0x96, 0xcd, 0x94, 0xc2, 0xcc, 0xcf, 0x8a,
  114. 0x3a, 0x86, 0x30, 0x28, 0xb5, 0xe1, 0xdc, 0x0a,
  115. 0x75, 0x86, 0x60, 0x2d, 0x25, 0x3c, 0xff, 0xf9,
  116. 0x1b, 0x82, 0x66, 0xbe, 0xa6, 0xd6, 0x1a, 0xb1 },
  117. 32
  118. }
  119. };
  120. int ret = 0;
  121. u8 *buf;
  122. unsigned int i;
  123. for (i = 0; i < ARRAY_SIZE(vectors); i++) {
  124. struct cbc_test_vector *tv = &vectors[i];
  125. buf = malloc(tv->len);
  126. if (buf == NULL) {
  127. ret++;
  128. break;
  129. }
  130. memcpy(buf, tv->plain, tv->len);
  131. if (aes_128_cbc_encrypt(tv->key, tv->iv, buf, tv->len) ||
  132. memcmp(buf, tv->cipher, tv->len) != 0) {
  133. printf("AES-CBC encrypt %d failed\n", i);
  134. ret++;
  135. }
  136. memcpy(buf, tv->cipher, tv->len);
  137. if (aes_128_cbc_decrypt(tv->key, tv->iv, buf, tv->len) ||
  138. memcmp(buf, tv->plain, tv->len) != 0) {
  139. printf("AES-CBC decrypt %d failed\n", i);
  140. ret++;
  141. }
  142. free(buf);
  143. }
  144. return ret;
  145. }
  146. /*
  147. * GCM test vectors from
  148. * http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-spec.pdf
  149. */
  150. struct gcm_test_vector {
  151. char *k;
  152. char *p;
  153. char *aad;
  154. char *iv;
  155. char *c;
  156. char *t;
  157. };
  158. static const struct gcm_test_vector gcm_tests[] = {
  159. {
  160. /* Test Case 1 */
  161. "00000000000000000000000000000000",
  162. "",
  163. "",
  164. "000000000000000000000000",
  165. "",
  166. "58e2fccefa7e3061367f1d57a4e7455a"
  167. },
  168. {
  169. /* Test Case 2 */
  170. "00000000000000000000000000000000",
  171. "00000000000000000000000000000000",
  172. "",
  173. "000000000000000000000000",
  174. "0388dace60b6a392f328c2b971b2fe78",
  175. "ab6e47d42cec13bdf53a67b21257bddf"
  176. },
  177. {
  178. /* Test Case 3 */
  179. "feffe9928665731c6d6a8f9467308308",
  180. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
  181. "",
  182. "cafebabefacedbaddecaf888",
  183. "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f5985",
  184. "4d5c2af327cd64a62cf35abd2ba6fab4"
  185. },
  186. {
  187. /* Test Case 4 */
  188. "feffe9928665731c6d6a8f9467308308",
  189. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  190. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  191. "cafebabefacedbaddecaf888",
  192. "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091",
  193. "5bc94fbc3221a5db94fae95ae7121a47"
  194. },
  195. {
  196. /* Test Case 5 */
  197. "feffe9928665731c6d6a8f9467308308",
  198. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  199. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  200. "cafebabefacedbad",
  201. "61353b4c2806934a777ff51fa22a4755699b2a714fcdc6f83766e5f97b6c742373806900e49f24b22b097544d4896b424989b5e1ebac0f07c23f4598",
  202. "3612d2e79e3b0785561be14aaca2fccb"
  203. },
  204. {
  205. /* Test Case 6 */
  206. "feffe9928665731c6d6a8f9467308308",
  207. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  208. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  209. "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
  210. "8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca701e4a9a4fba43c90ccdcb281d48c7c6fd62875d2aca417034c34aee5",
  211. "619cc5aefffe0bfa462af43c1699d050"
  212. },
  213. {
  214. /* Test Case 7 */
  215. "000000000000000000000000000000000000000000000000",
  216. "",
  217. "",
  218. "000000000000000000000000",
  219. "",
  220. "cd33b28ac773f74ba00ed1f312572435"
  221. },
  222. {
  223. /* Test Case 8 */
  224. "000000000000000000000000000000000000000000000000",
  225. "00000000000000000000000000000000",
  226. "",
  227. "000000000000000000000000",
  228. "98e7247c07f0fe411c267e4384b0f600",
  229. "2ff58d80033927ab8ef4d4587514f0fb"
  230. },
  231. {
  232. /* Test Case 9 */
  233. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  234. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
  235. "",
  236. "cafebabefacedbaddecaf888",
  237. "3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710acade256",
  238. "9924a7c8587336bfb118024db8674a14"
  239. },
  240. {
  241. /* Test Case 10 */
  242. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  243. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  244. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  245. "cafebabefacedbaddecaf888",
  246. "3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710",
  247. "2519498e80f1478f37ba55bd6d27618c"
  248. },
  249. {
  250. /* Test Case 11 */
  251. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  252. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  253. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  254. "cafebabefacedbad",
  255. "0f10f599ae14a154ed24b36e25324db8c566632ef2bbb34f8347280fc4507057fddc29df9a471f75c66541d4d4dad1c9e93a19a58e8b473fa0f062f7",
  256. "65dcc57fcf623a24094fcca40d3533f8"
  257. },
  258. {
  259. /* Test Case 12 */
  260. "feffe9928665731c6d6a8f9467308308feffe9928665731c",
  261. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  262. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  263. "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
  264. "d27e88681ce3243c4830165a8fdcf9ff1de9a1d8e6b447ef6ef7b79828666e4581e79012af34ddd9e2f037589b292db3e67c036745fa22e7e9b7373b",
  265. "dcf566ff291c25bbb8568fc3d376a6d9"
  266. },
  267. {
  268. /* Test Case 13 */
  269. "0000000000000000000000000000000000000000000000000000000000000000",
  270. "",
  271. "",
  272. "000000000000000000000000",
  273. "",
  274. "530f8afbc74536b9a963b4f1c4cb738b"
  275. },
  276. {
  277. /* Test Case 14 */
  278. "0000000000000000000000000000000000000000000000000000000000000000",
  279. "00000000000000000000000000000000",
  280. "",
  281. "000000000000000000000000",
  282. "cea7403d4d606b6e074ec5d3baf39d18",
  283. "d0d1c8a799996bf0265b98b5d48ab919"
  284. },
  285. {
  286. /* Test Case 15 */
  287. "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
  288. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
  289. "",
  290. "cafebabefacedbaddecaf888",
  291. "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662898015ad",
  292. "b094dac5d93471bdec1a502270e3cc6c"
  293. },
  294. {
  295. /* Test Case 16 */
  296. "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
  297. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  298. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  299. "cafebabefacedbaddecaf888",
  300. "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662",
  301. "76fc6ece0f4e1768cddf8853bb2d551b"
  302. },
  303. {
  304. /* Test Case 17 */
  305. "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
  306. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  307. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  308. "cafebabefacedbad",
  309. "c3762df1ca787d32ae47c13bf19844cbaf1ae14d0b976afac52ff7d79bba9de0feb582d33934a4f0954cc2363bc73f7862ac430e64abe499f47c9b1f",
  310. "3a337dbf46a792c45e454913fe2ea8f2"
  311. },
  312. {
  313. /* Test Case 18 */
  314. "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
  315. "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
  316. "feedfacedeadbeeffeedfacedeadbeefabaddad2",
  317. "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
  318. "5a8def2f0c9e53f1f75d7853659e2a20eeb2b22aafde6419a058ab4f6f746bf40fc0c3b780f244452da3ebf1c5d82cdea2418997200ef82e44ae7e3f",
  319. "a44a8266ee1c8eb0c8b5d4cf5ae9f19a"
  320. }
  321. };
  322. static int test_gcm(void)
  323. {
  324. int ret = 0;
  325. int i;
  326. u8 k[32], aad[32], iv[64], t[16], tag[16];
  327. u8 p[64], c[64], tmp[64];
  328. size_t k_len, p_len, aad_len, iv_len;
  329. for (i = 0; i < ARRAY_SIZE(gcm_tests); i++) {
  330. const struct gcm_test_vector *tc = &gcm_tests[i];
  331. k_len = os_strlen(tc->k) / 2;
  332. if (hexstr2bin(tc->k, k, k_len)) {
  333. printf("Invalid GCM test vector %d (k)\n", i);
  334. ret++;
  335. continue;
  336. }
  337. p_len = os_strlen(tc->p) / 2;
  338. if (hexstr2bin(tc->p, p, p_len)) {
  339. printf("Invalid GCM test vector %d (p)\n", i);
  340. ret++;
  341. continue;
  342. }
  343. aad_len = os_strlen(tc->aad) / 2;
  344. if (hexstr2bin(tc->aad, aad, aad_len)) {
  345. printf("Invalid GCM test vector %d (aad)\n", i);
  346. ret++;
  347. continue;
  348. }
  349. iv_len = os_strlen(tc->iv) / 2;
  350. if (hexstr2bin(tc->iv, iv, iv_len)) {
  351. printf("Invalid GCM test vector %d (iv)\n", i);
  352. ret++;
  353. continue;
  354. }
  355. if (hexstr2bin(tc->c, c, p_len)) {
  356. printf("Invalid GCM test vector %d (c)\n", i);
  357. ret++;
  358. continue;
  359. }
  360. if (hexstr2bin(tc->t, t, sizeof(t))) {
  361. printf("Invalid GCM test vector %d (t)\n", i);
  362. ret++;
  363. continue;
  364. }
  365. if (aes_gcm_ae(k, k_len, iv, iv_len, p, p_len, aad, aad_len,
  366. tmp, tag) < 0) {
  367. printf("GCM-AE failed (test case %d)\n", i);
  368. ret++;
  369. continue;
  370. }
  371. if (os_memcmp(c, tmp, p_len) != 0) {
  372. printf("GCM-AE mismatch (test case %d)\n", i);
  373. ret++;
  374. }
  375. if (os_memcmp(tag, t, sizeof(tag)) != 0) {
  376. printf("GCM-AE tag mismatch (test case %d)\n", i);
  377. ret++;
  378. }
  379. if (p_len == 0) {
  380. if (aes_gmac(k, k_len, iv, iv_len, aad, aad_len, tag) <
  381. 0) {
  382. printf("GMAC failed (test case %d)\n", i);
  383. ret++;
  384. continue;
  385. }
  386. if (os_memcmp(tag, t, sizeof(tag)) != 0) {
  387. printf("GMAC tag mismatch (test case %d)\n", i);
  388. ret++;
  389. }
  390. }
  391. if (aes_gcm_ad(k, k_len, iv, iv_len, c, p_len, aad, aad_len,
  392. t, tmp) < 0) {
  393. printf("GCM-AD failed (test case %d)\n", i);
  394. ret++;
  395. continue;
  396. }
  397. if (os_memcmp(p, tmp, p_len) != 0) {
  398. printf("GCM-AD mismatch (test case %d)\n", i);
  399. ret++;
  400. }
  401. }
  402. return ret;
  403. }
  404. /* OMAC1 AES-128 test vectors from
  405. * http://csrc.nist.gov/CryptoToolkit/modes/proposedmodes/omac/omac-ad.pdf
  406. * which are same as the examples from NIST SP800-38B
  407. * http://csrc.nist.gov/CryptoToolkit/modes/800-38_Series_Publications/SP800-38B.pdf
  408. */
  409. struct omac1_test_vector {
  410. u8 k[16];
  411. u8 msg[64];
  412. int msg_len;
  413. u8 tag[16];
  414. };
  415. static struct omac1_test_vector test_vectors[] =
  416. {
  417. {
  418. { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  419. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
  420. { },
  421. 0,
  422. { 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  423. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46 }
  424. },
  425. {
  426. { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  427. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
  428. { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  429. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a},
  430. 16,
  431. { 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  432. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c }
  433. },
  434. {
  435. { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  436. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
  437. { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  438. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  439. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  440. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  441. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11 },
  442. 40,
  443. { 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  444. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27 }
  445. },
  446. {
  447. { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  448. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
  449. { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  450. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  451. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  452. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  453. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  454. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  455. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  456. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 },
  457. 64,
  458. { 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  459. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe }
  460. },
  461. };
  462. static int test_key_wrap(void)
  463. {
  464. unsigned int i;
  465. int ret = 0;
  466. /* RFC 3394 - Test vector 4.1 */
  467. u8 kek41[] = {
  468. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  469. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  470. };
  471. u8 plain41[] = {
  472. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  473. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  474. };
  475. u8 crypt41[] = {
  476. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  477. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  478. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  479. };
  480. /* RFC 3394 - Test vector 4.2 */
  481. u8 kek42[] = {
  482. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  483. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  484. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  485. };
  486. u8 plain42[] = {
  487. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  488. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  489. };
  490. u8 crypt42[] = {
  491. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  492. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  493. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  494. };
  495. /* RFC 3394 - Test vector 4.3 */
  496. u8 kek43[] = {
  497. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  498. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  499. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  500. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  501. };
  502. u8 plain43[] = {
  503. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  504. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  505. };
  506. u8 crypt43[] = {
  507. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  508. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  509. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7,
  510. };
  511. /* RFC 3394 - Test vector 4.4 */
  512. u8 kek44[] = {
  513. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  514. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  515. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  516. };
  517. u8 plain44[] = {
  518. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  519. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  520. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  521. };
  522. u8 crypt44[] = {
  523. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  524. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  525. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  526. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  527. };
  528. /* RFC 3394 - Test vector 4.5 */
  529. u8 kek45[] = {
  530. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  531. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  532. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  533. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  534. };
  535. u8 plain45[] = {
  536. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  537. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  538. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  539. };
  540. u8 crypt45[] = {
  541. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  542. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  543. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  544. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1,
  545. };
  546. /* RFC 3394 - Test vector 4.6 */
  547. u8 kek46[] = {
  548. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  549. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  550. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  551. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  552. };
  553. u8 plain46[] = {
  554. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  555. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  556. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  557. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  558. };
  559. u8 crypt46[] = {
  560. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  561. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  562. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  563. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  564. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  565. };
  566. u8 result[40];
  567. printf("RFC 3394 - Test vector 4.1\n");
  568. if (aes_wrap(kek41, sizeof(kek41), sizeof(plain41) / 8, plain41,
  569. result)) {
  570. printf("AES-WRAP-128 reported failure\n");
  571. ret++;
  572. }
  573. if (memcmp(result, crypt41, sizeof(crypt41)) != 0) {
  574. printf("AES-WRAP-128 failed\n");
  575. ret++;
  576. }
  577. if (aes_unwrap(kek41, sizeof(kek41), sizeof(plain41) / 8, crypt41,
  578. result)) {
  579. printf("AES-UNWRAP-128 reported failure\n");
  580. ret++;
  581. }
  582. if (memcmp(result, plain41, sizeof(plain41)) != 0) {
  583. printf("AES-UNWRAP-128 failed\n");
  584. ret++;
  585. for (i = 0; i < sizeof(plain41); i++)
  586. printf(" %02x", result[i]);
  587. printf("\n");
  588. }
  589. printf("RFC 3394 - Test vector 4.2\n");
  590. if (aes_wrap(kek42, sizeof(kek42), sizeof(plain42) / 8, plain42,
  591. result)) {
  592. printf("AES-WRAP-192 reported failure\n");
  593. ret++;
  594. }
  595. if (memcmp(result, crypt42, sizeof(crypt42)) != 0) {
  596. printf("AES-WRAP-192 failed\n");
  597. ret++;
  598. }
  599. if (aes_unwrap(kek42, sizeof(kek42), sizeof(plain42) / 8, crypt42,
  600. result)) {
  601. printf("AES-UNWRAP-192 reported failure\n");
  602. ret++;
  603. }
  604. if (memcmp(result, plain42, sizeof(plain42)) != 0) {
  605. printf("AES-UNWRAP-192 failed\n");
  606. ret++;
  607. for (i = 0; i < sizeof(plain42); i++)
  608. printf(" %02x", result[i]);
  609. printf("\n");
  610. }
  611. printf("RFC 3394 - Test vector 4.3\n");
  612. if (aes_wrap(kek43, sizeof(kek43), sizeof(plain43) / 8, plain43,
  613. result)) {
  614. printf("AES-WRAP-256 reported failure\n");
  615. ret++;
  616. }
  617. if (memcmp(result, crypt43, sizeof(crypt43)) != 0) {
  618. printf("AES-WRAP-256 failed\n");
  619. ret++;
  620. }
  621. if (aes_unwrap(kek43, sizeof(kek43), sizeof(plain43) / 8, crypt43,
  622. result)) {
  623. printf("AES-UNWRAP-256 reported failure\n");
  624. ret++;
  625. }
  626. if (memcmp(result, plain43, sizeof(plain43)) != 0) {
  627. printf("AES-UNWRAP-256 failed\n");
  628. ret++;
  629. for (i = 0; i < sizeof(plain43); i++)
  630. printf(" %02x", result[i]);
  631. printf("\n");
  632. }
  633. printf("RFC 3394 - Test vector 4.4\n");
  634. if (aes_wrap(kek44, sizeof(kek44), sizeof(plain44) / 8, plain44,
  635. result)) {
  636. printf("AES-WRAP-192 reported failure\n");
  637. ret++;
  638. }
  639. if (memcmp(result, crypt44, sizeof(crypt44)) != 0) {
  640. printf("AES-WRAP-192 failed\n");
  641. ret++;
  642. }
  643. if (aes_unwrap(kek44, sizeof(kek44), sizeof(plain44) / 8, crypt44,
  644. result)) {
  645. printf("AES-UNWRAP-192 reported failure\n");
  646. ret++;
  647. }
  648. if (memcmp(result, plain44, sizeof(plain44)) != 0) {
  649. printf("AES-UNWRAP-192 failed\n");
  650. ret++;
  651. for (i = 0; i < sizeof(plain44); i++)
  652. printf(" %02x", result[i]);
  653. printf("\n");
  654. }
  655. printf("RFC 3394 - Test vector 4.5\n");
  656. if (aes_wrap(kek45, sizeof(kek45), sizeof(plain45) / 8, plain45,
  657. result)) {
  658. printf("AES-WRAP-256 reported failure\n");
  659. ret++;
  660. }
  661. if (memcmp(result, crypt45, sizeof(crypt45)) != 0) {
  662. printf("AES-WRAP-256 failed\n");
  663. ret++;
  664. for (i = 0; i < sizeof(crypt45); i++)
  665. printf(" %02x", result[i]);
  666. printf("\n");
  667. }
  668. if (aes_unwrap(kek45, sizeof(kek45), sizeof(plain45) / 8, crypt45,
  669. result)) {
  670. printf("AES-UNWRAP-256 reported failure\n");
  671. ret++;
  672. }
  673. if (memcmp(result, plain45, sizeof(plain45)) != 0) {
  674. printf("AES-UNWRAP-256 failed\n");
  675. ret++;
  676. for (i = 0; i < sizeof(plain45); i++)
  677. printf(" %02x", result[i]);
  678. printf("\n");
  679. }
  680. printf("RFC 3394 - Test vector 4.6\n");
  681. if (aes_wrap(kek46, sizeof(kek46), sizeof(plain46) / 8, plain46,
  682. result)) {
  683. printf("AES-WRAP-256 reported failure\n");
  684. ret++;
  685. }
  686. if (memcmp(result, crypt46, sizeof(crypt46)) != 0) {
  687. printf("AES-WRAP-256 failed\n");
  688. ret++;
  689. }
  690. if (aes_unwrap(kek46, sizeof(kek46), sizeof(plain46) / 8, crypt46,
  691. result)) {
  692. printf("AES-UNWRAP-256 reported failure\n");
  693. ret++;
  694. }
  695. if (memcmp(result, plain46, sizeof(plain46)) != 0) {
  696. printf("AES-UNWRAP-256 failed\n");
  697. ret++;
  698. for (i = 0; i < sizeof(plain46); i++)
  699. printf(" %02x", result[i]);
  700. printf("\n");
  701. }
  702. return ret;
  703. }
  704. static int test_nist_key_wrap_ae(const char *fname)
  705. {
  706. FILE *f;
  707. int ret = 0;
  708. char buf[15000], *pos, *pos2;
  709. u8 bin[2000], k[32], p[1024], c[1024 + 8], result[1024 + 8];
  710. size_t bin_len, k_len = 0, p_len = 0, c_len = 0;
  711. int ok = 0;
  712. printf("NIST KW AE tests from %s\n", fname);
  713. f = fopen(fname, "r");
  714. if (f == NULL) {
  715. printf("%s does not exist - cannot validate test vectors\n",
  716. fname);
  717. return 1;
  718. }
  719. while (fgets(buf, sizeof(buf), f)) {
  720. if (buf[0] == '#')
  721. continue;
  722. pos = os_strchr(buf, '=');
  723. if (pos == NULL)
  724. continue;
  725. pos2 = pos - 1;
  726. while (pos2 >= buf && *pos2 == ' ')
  727. *pos2-- = '\0';
  728. *pos++ = '\0';
  729. while (*pos == ' ')
  730. *pos++ = '\0';
  731. pos2 = os_strchr(pos, '\r');
  732. if (!pos2)
  733. pos2 = os_strchr(pos, '\n');
  734. if (pos2)
  735. *pos2 = '\0';
  736. else
  737. pos2 = pos + os_strlen(pos);
  738. if (buf[0] == '[') {
  739. printf("%s = %s\n", buf, pos);
  740. continue;
  741. }
  742. if (os_strcmp(buf, "COUNT") == 0) {
  743. printf("Test %s - ", pos);
  744. continue;
  745. }
  746. bin_len = os_strlen(pos);
  747. if (bin_len > sizeof(bin) * 2) {
  748. printf("Too long binary data (%s)\n", buf);
  749. return 1;
  750. }
  751. if (bin_len & 0x01) {
  752. printf("Odd number of hexstring values (%s)\n",
  753. buf);
  754. return 1;
  755. }
  756. bin_len /= 2;
  757. if (hexstr2bin(pos, bin, bin_len) < 0) {
  758. printf("Invalid hex string '%s' (%s)\n", pos, buf);
  759. return 1;
  760. }
  761. if (os_strcmp(buf, "K") == 0) {
  762. if (bin_len > sizeof(k)) {
  763. printf("Too long K (%u)\n", (unsigned) bin_len);
  764. return 1;
  765. }
  766. os_memcpy(k, bin, bin_len);
  767. k_len = bin_len;
  768. continue;
  769. }
  770. if (os_strcmp(buf, "P") == 0) {
  771. if (bin_len > sizeof(p)) {
  772. printf("Too long P (%u)\n", (unsigned) bin_len);
  773. return 1;
  774. }
  775. os_memcpy(p, bin, bin_len);
  776. p_len = bin_len;
  777. continue;
  778. }
  779. if (os_strcmp(buf, "C") != 0) {
  780. printf("Unexpected field '%s'\n", buf);
  781. continue;
  782. }
  783. if (bin_len > sizeof(c)) {
  784. printf("Too long C (%u)\n", (unsigned) bin_len);
  785. return 1;
  786. }
  787. os_memcpy(c, bin, bin_len);
  788. c_len = bin_len;
  789. if (p_len % 8 != 0 || c_len % 8 != 0 || c_len - p_len != 8) {
  790. printf("invalid parameter length (p_len=%u c_len=%u)\n",
  791. (unsigned) p_len, (unsigned) c_len);
  792. continue;
  793. }
  794. if (aes_wrap(k, k_len, p_len / 8, p, result)) {
  795. printf("aes_wrap() failed\n");
  796. ret++;
  797. continue;
  798. }
  799. if (os_memcmp(c, result, c_len) == 0) {
  800. printf("OK\n");
  801. ok++;
  802. } else {
  803. printf("FAIL\n");
  804. ret++;
  805. }
  806. }
  807. fclose(f);
  808. if (ret)
  809. printf("Test case failed\n");
  810. else
  811. printf("%d test vectors OK\n", ok);
  812. return ret;
  813. }
  814. static int test_nist_key_wrap_ad(const char *fname)
  815. {
  816. FILE *f;
  817. int ret = 0;
  818. char buf[15000], *pos, *pos2;
  819. u8 bin[2000], k[32], p[1024], c[1024 + 8], result[1024 + 8];
  820. size_t bin_len, k_len = 0, p_len = 0, c_len = 0;
  821. int ok = 0;
  822. int fail;
  823. printf("NIST KW AD tests from %s\n", fname);
  824. f = fopen(fname, "r");
  825. if (f == NULL) {
  826. printf("%s does not exist - cannot validate test vectors\n",
  827. fname);
  828. return 1;
  829. }
  830. while (fgets(buf, sizeof(buf), f)) {
  831. if (buf[0] == '#')
  832. continue;
  833. fail = 0;
  834. pos = os_strchr(buf, '=');
  835. if (pos == NULL) {
  836. if (os_strncmp(buf, "FAIL", 4) == 0) {
  837. fail = 1;
  838. goto skip_val_parse;
  839. }
  840. continue;
  841. }
  842. pos2 = pos - 1;
  843. while (pos2 >= buf && *pos2 == ' ')
  844. *pos2-- = '\0';
  845. *pos++ = '\0';
  846. while (*pos == ' ')
  847. *pos++ = '\0';
  848. pos2 = os_strchr(pos, '\r');
  849. if (!pos2)
  850. pos2 = os_strchr(pos, '\n');
  851. if (pos2)
  852. *pos2 = '\0';
  853. else
  854. pos2 = pos + os_strlen(pos);
  855. if (buf[0] == '[') {
  856. printf("%s = %s\n", buf, pos);
  857. continue;
  858. }
  859. if (os_strcmp(buf, "COUNT") == 0) {
  860. printf("Test %s - ", pos);
  861. continue;
  862. }
  863. bin_len = os_strlen(pos);
  864. if (bin_len > sizeof(bin) * 2) {
  865. printf("Too long binary data (%s)\n", buf);
  866. return 1;
  867. }
  868. if (bin_len & 0x01) {
  869. printf("Odd number of hexstring values (%s)\n",
  870. buf);
  871. return 1;
  872. }
  873. bin_len /= 2;
  874. if (hexstr2bin(pos, bin, bin_len) < 0) {
  875. printf("Invalid hex string '%s' (%s)\n", pos, buf);
  876. return 1;
  877. }
  878. if (os_strcmp(buf, "K") == 0) {
  879. if (bin_len > sizeof(k)) {
  880. printf("Too long K (%u)\n", (unsigned) bin_len);
  881. return 1;
  882. }
  883. os_memcpy(k, bin, bin_len);
  884. k_len = bin_len;
  885. continue;
  886. }
  887. if (os_strcmp(buf, "C") == 0) {
  888. if (bin_len > sizeof(c)) {
  889. printf("Too long C (%u)\n", (unsigned) bin_len);
  890. return 1;
  891. }
  892. os_memcpy(c, bin, bin_len);
  893. c_len = bin_len;
  894. continue;
  895. }
  896. skip_val_parse:
  897. if (!fail) {
  898. if (os_strcmp(buf, "P") != 0) {
  899. printf("Unexpected field '%s'\n", buf);
  900. continue;
  901. }
  902. if (bin_len > sizeof(p)) {
  903. printf("Too long P (%u)\n", (unsigned) bin_len);
  904. return 1;
  905. }
  906. os_memcpy(p, bin, bin_len);
  907. p_len = bin_len;
  908. if (p_len % 8 != 0 || c_len % 8 != 0 ||
  909. c_len - p_len != 8) {
  910. printf("invalid parameter length (p_len=%u c_len=%u)\n",
  911. (unsigned) p_len, (unsigned) c_len);
  912. continue;
  913. }
  914. }
  915. if (aes_unwrap(k, k_len, (c_len / 8) - 1, c, result)) {
  916. if (fail) {
  917. printf("OK (fail reported)\n");
  918. ok++;
  919. continue;
  920. }
  921. printf("aes_unwrap() failed\n");
  922. ret++;
  923. continue;
  924. }
  925. if (fail) {
  926. printf("FAIL (mismatch not reported)\n");
  927. ret++;
  928. } else if (os_memcmp(p, result, p_len) == 0) {
  929. printf("OK\n");
  930. ok++;
  931. } else {
  932. printf("FAIL\n");
  933. ret++;
  934. }
  935. }
  936. fclose(f);
  937. if (ret)
  938. printf("Test case failed\n");
  939. else
  940. printf("%d test vectors OK\n", ok);
  941. return ret;
  942. }
  943. int main(int argc, char *argv[])
  944. {
  945. u8 result[24];
  946. int ret = 0;
  947. unsigned int i;
  948. struct omac1_test_vector *tv;
  949. if (argc >= 3 && os_strcmp(argv[1], "NIST-KW-AE") == 0)
  950. ret += test_nist_key_wrap_ae(argv[2]);
  951. else if (argc >= 3 && os_strcmp(argv[1], "NIST-KW-AD") == 0)
  952. ret += test_nist_key_wrap_ad(argv[2]);
  953. ret += test_key_wrap();
  954. test_aes_perf();
  955. for (i = 0; i < ARRAY_SIZE(test_vectors); i++) {
  956. tv = &test_vectors[i];
  957. if (omac1_aes_128(tv->k, tv->msg, tv->msg_len, result) ||
  958. memcmp(result, tv->tag, 16) != 0) {
  959. printf("OMAC1-AES-128 test vector %d failed\n", i);
  960. ret++;
  961. }
  962. if (tv->msg_len > 1) {
  963. const u8 *addr[2];
  964. size_t len[2];
  965. addr[0] = tv->msg;
  966. len[0] = 1;
  967. addr[1] = tv->msg + 1;
  968. len[1] = tv->msg_len - 1;
  969. if (omac1_aes_128_vector(tv->k, 2, addr, len,
  970. result) ||
  971. memcmp(result, tv->tag, 16) != 0) {
  972. printf("OMAC1-AES-128(vector) test vector %d "
  973. "failed\n", i);
  974. ret++;
  975. }
  976. }
  977. }
  978. ret += test_eax();
  979. ret += test_cbc();
  980. ret += test_gcm();
  981. if (ret)
  982. printf("FAILED!\n");
  983. return ret;
  984. }