crypto_module_tests.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*
  2. * crypto module tests
  3. * Copyright (c) 2014-2015, 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 "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "crypto/aes_siv.h"
  11. #include "crypto/aes_wrap.h"
  12. #include "crypto/aes.h"
  13. static int test_siv(void)
  14. {
  15. #ifdef CONFIG_MESH
  16. /* RFC 5297, A.1. Deterministic Authenticated Encryption Example */
  17. u8 key[] = {
  18. 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8,
  19. 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
  20. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  21. 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
  22. };
  23. u8 ad[] = {
  24. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  25. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  26. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27
  27. };
  28. u8 plaintext[] = {
  29. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  30. 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee
  31. };
  32. u8 iv_c[] = {
  33. 0x85, 0x63, 0x2d, 0x07, 0xc6, 0xe8, 0xf3, 0x7f,
  34. 0x95, 0x0a, 0xcd, 0x32, 0x0a, 0x2e, 0xcc, 0x93,
  35. 0x40, 0xc0, 0x2b, 0x96, 0x90, 0xc4, 0xdc, 0x04,
  36. 0xda, 0xef, 0x7f, 0x6a, 0xfe, 0x5c
  37. };
  38. /* RFC 5297, A.2. Nonce-Based Authenticated Encryption Example */
  39. u8 key_2[] = {
  40. 0x7f, 0x7e, 0x7d, 0x7c, 0x7b, 0x7a, 0x79, 0x78,
  41. 0x77, 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x70,
  42. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  43. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
  44. };
  45. u8 ad1_2[] = {
  46. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  47. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  48. 0xde, 0xad, 0xda, 0xda, 0xde, 0xad, 0xda, 0xda,
  49. 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
  50. 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00
  51. };
  52. u8 ad2_2[] = {
  53. 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80,
  54. 0x90, 0xa0
  55. };
  56. u8 nonce_2[] = {
  57. 0x09, 0xf9, 0x11, 0x02, 0x9d, 0x74, 0xe3, 0x5b,
  58. 0xd8, 0x41, 0x56, 0xc5, 0x63, 0x56, 0x88, 0xc0
  59. };
  60. u8 plaintext_2[] = {
  61. 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  62. 0x73, 0x6f, 0x6d, 0x65, 0x20, 0x70, 0x6c, 0x61,
  63. 0x69, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x20, 0x74,
  64. 0x6f, 0x20, 0x65, 0x6e, 0x63, 0x72, 0x79, 0x70,
  65. 0x74, 0x20, 0x75, 0x73, 0x69, 0x6e, 0x67, 0x20,
  66. 0x53, 0x49, 0x56, 0x2d, 0x41, 0x45, 0x53
  67. };
  68. u8 iv_c_2[] = {
  69. 0x7b, 0xdb, 0x6e, 0x3b, 0x43, 0x26, 0x67, 0xeb,
  70. 0x06, 0xf4, 0xd1, 0x4b, 0xff, 0x2f, 0xbd, 0x0f,
  71. 0xcb, 0x90, 0x0f, 0x2f, 0xdd, 0xbe, 0x40, 0x43,
  72. 0x26, 0x60, 0x19, 0x65, 0xc8, 0x89, 0xbf, 0x17,
  73. 0xdb, 0xa7, 0x7c, 0xeb, 0x09, 0x4f, 0xa6, 0x63,
  74. 0xb7, 0xa3, 0xf7, 0x48, 0xba, 0x8a, 0xf8, 0x29,
  75. 0xea, 0x64, 0xad, 0x54, 0x4a, 0x27, 0x2e, 0x9c,
  76. 0x48, 0x5b, 0x62, 0xa3, 0xfd, 0x5c, 0x0d
  77. };
  78. u8 out[2 * AES_BLOCK_SIZE + sizeof(plaintext_2)];
  79. const u8 *addr[3];
  80. size_t len[3];
  81. /* RFC 5297, A.1. Deterministic Authenticated Encryption Example */
  82. addr[0] = ad;
  83. len[0] = sizeof(ad);
  84. if (aes_siv_encrypt(key, plaintext, sizeof(plaintext),
  85. 1, addr, len, out)) {
  86. wpa_printf(MSG_ERROR, "AES-SIV mode encryption failed");
  87. return 1;
  88. }
  89. if (os_memcmp(out, iv_c, sizeof(iv_c)) != 0) {
  90. wpa_printf(MSG_ERROR,
  91. "AES-SIV mode encryption returned invalid cipher text");
  92. return 1;
  93. }
  94. if (aes_siv_decrypt(key, iv_c, sizeof(iv_c), 1, addr, len, out)) {
  95. wpa_printf(MSG_ERROR, "AES-SIV mode decryption failed");
  96. return 1;
  97. }
  98. if (os_memcmp(out, plaintext, sizeof(plaintext)) != 0) {
  99. wpa_printf(MSG_ERROR,
  100. "AES-SIV mode decryption returned invalid plain text");
  101. return 1;
  102. }
  103. /* RFC 5297, A.2. Nonce-Based Authenticated Encryption Example */
  104. addr[0] = ad1_2;
  105. len[0] = sizeof(ad1_2);
  106. addr[1] = ad2_2;
  107. len[1] = sizeof(ad2_2);
  108. addr[2] = nonce_2;
  109. len[2] = sizeof(nonce_2);
  110. if (aes_siv_encrypt(key_2, plaintext_2, sizeof(plaintext_2),
  111. 3, addr, len, out)) {
  112. wpa_printf(MSG_ERROR, "AES-SIV mode encryption failed");
  113. return 1;
  114. }
  115. if (os_memcmp(out, iv_c_2, sizeof(iv_c_2)) != 0) {
  116. wpa_printf(MSG_ERROR,
  117. "AES-SIV mode encryption returned invalid cipher text");
  118. return 1;
  119. }
  120. if (aes_siv_decrypt(key_2, iv_c_2, sizeof(iv_c_2), 3, addr, len, out)) {
  121. wpa_printf(MSG_ERROR, "AES-SIV mode decryption failed");
  122. return 1;
  123. }
  124. if (os_memcmp(out, plaintext_2, sizeof(plaintext_2)) != 0) {
  125. wpa_printf(MSG_ERROR,
  126. "AES-SIV mode decryption returned invalid plain text");
  127. return 1;
  128. }
  129. wpa_printf(MSG_INFO, "AES-SIV test cases passed");
  130. #endif /* CONFIG_MESH */
  131. return 0;
  132. }
  133. /* OMAC1 AES-128 test vectors from
  134. * http://csrc.nist.gov/CryptoToolkit/modes/proposedmodes/omac/omac-ad.pdf
  135. * which are same as the examples from NIST SP800-38B
  136. * http://csrc.nist.gov/CryptoToolkit/modes/800-38_Series_Publications/SP800-38B.pdf
  137. */
  138. struct omac1_test_vector {
  139. u8 k[16];
  140. u8 msg[64];
  141. int msg_len;
  142. u8 tag[16];
  143. };
  144. static struct omac1_test_vector omac1_test_vectors[] =
  145. {
  146. {
  147. { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  148. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
  149. { },
  150. 0,
  151. { 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  152. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46 }
  153. },
  154. {
  155. { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  156. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
  157. { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  158. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a},
  159. 16,
  160. { 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  161. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c }
  162. },
  163. {
  164. { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  165. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
  166. { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  167. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  168. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  169. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  170. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11 },
  171. 40,
  172. { 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  173. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27 }
  174. },
  175. {
  176. { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  177. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
  178. { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  179. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  180. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  181. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  182. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  183. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  184. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  185. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 },
  186. 64,
  187. { 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  188. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe }
  189. },
  190. };
  191. static int test_omac1_vector(struct omac1_test_vector *tv, unsigned int i)
  192. {
  193. u8 key[] = {
  194. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  195. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  196. };
  197. u8 msg[] = { 0x12, 0x34, 0x56 };
  198. u8 result[24], result2[24];
  199. const u8 *addr[3];
  200. size_t len[3];
  201. if (omac1_aes_128(tv->k, tv->msg, tv->msg_len, result) ||
  202. os_memcmp(result, tv->tag, 16) != 0) {
  203. wpa_printf(MSG_ERROR, "OMAC1-AES-128 test vector %u failed", i);
  204. return 1;
  205. }
  206. if (tv->msg_len > 1) {
  207. addr[0] = tv->msg;
  208. len[0] = 1;
  209. addr[1] = tv->msg + 1;
  210. len[1] = tv->msg_len - 1;
  211. if (omac1_aes_128_vector(tv->k, 2, addr, len, result) ||
  212. os_memcmp(result, tv->tag, 16) != 0) {
  213. wpa_printf(MSG_ERROR,
  214. "OMAC1-AES-128(vector) test vector %u failed",
  215. i);
  216. return 1;
  217. }
  218. addr[0] = tv->msg;
  219. len[0] = tv->msg_len - 2;
  220. addr[1] = tv->msg + tv->msg_len - 2;
  221. len[1] = 1;
  222. addr[2] = tv->msg + tv->msg_len - 1;
  223. len[2] = 1;
  224. if (omac1_aes_128_vector(tv->k, 3, addr, len, result) ||
  225. os_memcmp(result, tv->tag, 16) != 0) {
  226. wpa_printf(MSG_ERROR,
  227. "OMAC1-AES-128(vector2) test vector %u failed",
  228. i);
  229. return 1;
  230. }
  231. }
  232. addr[0] = &msg[0];
  233. len[0] = 1;
  234. addr[1] = &msg[1];
  235. len[1] = 1;
  236. addr[2] = &msg[2];
  237. len[2] = 1;
  238. if (omac1_aes_128(key, msg, sizeof(msg), result) ||
  239. omac1_aes_128_vector(key, 3, addr, len, result2) ||
  240. os_memcmp(result, result2, 16) != 0) {
  241. wpa_printf(MSG_ERROR, "OMAC1-AES-128 short test mismatch");
  242. return 1;
  243. }
  244. return 0;
  245. }
  246. static int test_omac1(void)
  247. {
  248. unsigned int i;
  249. for (i = 0; i < ARRAY_SIZE(omac1_test_vectors); i++) {
  250. if (test_omac1_vector(&omac1_test_vectors[i], i))
  251. return 1;
  252. }
  253. wpa_printf(MSG_INFO, "OMAC1-AES-128 test cases passed");
  254. return 0;
  255. }
  256. static int test_eax(void)
  257. {
  258. #ifdef EAP_PSK
  259. u8 msg[] = { 0xF7, 0xFB };
  260. u8 key[] = { 0x91, 0x94, 0x5D, 0x3F, 0x4D, 0xCB, 0xEE, 0x0B,
  261. 0xF4, 0x5E, 0xF5, 0x22, 0x55, 0xF0, 0x95, 0xA4 };
  262. u8 nonce[] = { 0xBE, 0xCA, 0xF0, 0x43, 0xB0, 0xA2, 0x3D, 0x84,
  263. 0x31, 0x94, 0xBA, 0x97, 0x2C, 0x66, 0xDE, 0xBD };
  264. u8 hdr[] = { 0xFA, 0x3B, 0xFD, 0x48, 0x06, 0xEB, 0x53, 0xFA };
  265. u8 cipher[] = { 0x19, 0xDD, 0x5C, 0x4C, 0x93, 0x31, 0x04, 0x9D,
  266. 0x0B, 0xDA, 0xB0, 0x27, 0x74, 0x08, 0xF6, 0x79,
  267. 0x67, 0xE5 };
  268. u8 data[sizeof(msg)], tag[AES_BLOCK_SIZE];
  269. os_memcpy(data, msg, sizeof(msg));
  270. if (aes_128_eax_encrypt(key, nonce, sizeof(nonce), hdr, sizeof(hdr),
  271. data, sizeof(data), tag)) {
  272. wpa_printf(MSG_ERROR, "AES-128 EAX mode encryption failed");
  273. return 1;
  274. }
  275. if (os_memcmp(data, cipher, sizeof(data)) != 0) {
  276. wpa_printf(MSG_ERROR,
  277. "AES-128 EAX mode encryption returned invalid cipher text");
  278. return 1;
  279. }
  280. if (os_memcmp(tag, cipher + sizeof(data), AES_BLOCK_SIZE) != 0) {
  281. wpa_printf(MSG_ERROR,
  282. "AES-128 EAX mode encryption returned invalid tag");
  283. return 1;
  284. }
  285. if (aes_128_eax_decrypt(key, nonce, sizeof(nonce), hdr, sizeof(hdr),
  286. data, sizeof(data), tag)) {
  287. wpa_printf(MSG_ERROR, "AES-128 EAX mode decryption failed");
  288. return 1;
  289. }
  290. if (os_memcmp(data, msg, sizeof(data)) != 0) {
  291. wpa_printf(MSG_ERROR,
  292. "AES-128 EAX mode decryption returned invalid plain text");
  293. return 1;
  294. }
  295. wpa_printf(MSG_INFO, "AES-128 EAX mode test cases passed");
  296. #endif /* EAP_PSK */
  297. return 0;
  298. }
  299. static int test_cbc(void)
  300. {
  301. struct cbc_test_vector {
  302. u8 key[16];
  303. u8 iv[16];
  304. u8 plain[32];
  305. u8 cipher[32];
  306. size_t len;
  307. } vectors[] = {
  308. {
  309. { 0x06, 0xa9, 0x21, 0x40, 0x36, 0xb8, 0xa1, 0x5b,
  310. 0x51, 0x2e, 0x03, 0xd5, 0x34, 0x12, 0x00, 0x06 },
  311. { 0x3d, 0xaf, 0xba, 0x42, 0x9d, 0x9e, 0xb4, 0x30,
  312. 0xb4, 0x22, 0xda, 0x80, 0x2c, 0x9f, 0xac, 0x41 },
  313. "Single block msg",
  314. { 0xe3, 0x53, 0x77, 0x9c, 0x10, 0x79, 0xae, 0xb8,
  315. 0x27, 0x08, 0x94, 0x2d, 0xbe, 0x77, 0x18, 0x1a },
  316. 16
  317. },
  318. {
  319. { 0xc2, 0x86, 0x69, 0x6d, 0x88, 0x7c, 0x9a, 0xa0,
  320. 0x61, 0x1b, 0xbb, 0x3e, 0x20, 0x25, 0xa4, 0x5a },
  321. { 0x56, 0x2e, 0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28,
  322. 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58 },
  323. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  324. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  325. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  326. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
  327. { 0xd2, 0x96, 0xcd, 0x94, 0xc2, 0xcc, 0xcf, 0x8a,
  328. 0x3a, 0x86, 0x30, 0x28, 0xb5, 0xe1, 0xdc, 0x0a,
  329. 0x75, 0x86, 0x60, 0x2d, 0x25, 0x3c, 0xff, 0xf9,
  330. 0x1b, 0x82, 0x66, 0xbe, 0xa6, 0xd6, 0x1a, 0xb1 },
  331. 32
  332. }
  333. };
  334. int ret = 0;
  335. u8 *buf;
  336. unsigned int i;
  337. for (i = 0; i < ARRAY_SIZE(vectors); i++) {
  338. struct cbc_test_vector *tv = &vectors[i];
  339. buf = os_malloc(tv->len);
  340. if (buf == NULL) {
  341. ret++;
  342. break;
  343. }
  344. os_memcpy(buf, tv->plain, tv->len);
  345. if (aes_128_cbc_encrypt(tv->key, tv->iv, buf, tv->len) ||
  346. os_memcmp(buf, tv->cipher, tv->len) != 0) {
  347. wpa_printf(MSG_ERROR, "AES-CBC encrypt %d failed", i);
  348. ret++;
  349. }
  350. os_memcpy(buf, tv->cipher, tv->len);
  351. if (aes_128_cbc_decrypt(tv->key, tv->iv, buf, tv->len) ||
  352. os_memcmp(buf, tv->plain, tv->len) != 0) {
  353. wpa_printf(MSG_ERROR, "AES-CBC decrypt %d failed", i);
  354. ret++;
  355. }
  356. os_free(buf);
  357. }
  358. return ret;
  359. }
  360. static int test_ecb(void)
  361. {
  362. #ifdef EAP_PSK
  363. struct ecb_test_vector {
  364. char *key;
  365. char *plaintext;
  366. char *ciphertext;
  367. } vectors[] = {
  368. /* CAVS 11.1 - ECBGFSbox128.rsp */
  369. {
  370. "00000000000000000000000000000000",
  371. "f34481ec3cc627bacd5dc3fb08f273e6",
  372. "0336763e966d92595a567cc9ce537f5e"
  373. },
  374. {
  375. "00000000000000000000000000000000",
  376. "9798c4640bad75c7c3227db910174e72",
  377. "a9a1631bf4996954ebc093957b234589"
  378. },
  379. {
  380. "00000000000000000000000000000000",
  381. "96ab5c2ff612d9dfaae8c31f30c42168",
  382. "ff4f8391a6a40ca5b25d23bedd44a597"
  383. },
  384. {
  385. "00000000000000000000000000000000",
  386. "6a118a874519e64e9963798a503f1d35",
  387. "dc43be40be0e53712f7e2bf5ca707209"
  388. },
  389. {
  390. "00000000000000000000000000000000",
  391. "cb9fceec81286ca3e989bd979b0cb284",
  392. "92beedab1895a94faa69b632e5cc47ce"
  393. },
  394. {
  395. "00000000000000000000000000000000",
  396. "b26aeb1874e47ca8358ff22378f09144",
  397. "459264f4798f6a78bacb89c15ed3d601"
  398. },
  399. {
  400. "00000000000000000000000000000000",
  401. "58c8e00b2631686d54eab84b91f0aca1",
  402. "08a4e2efec8a8e3312ca7460b9040bbf"
  403. },
  404. /* CAVS 11.1 - ECBKeySbox128.rsp */
  405. {
  406. "10a58869d74be5a374cf867cfb473859",
  407. "00000000000000000000000000000000",
  408. "6d251e6944b051e04eaa6fb4dbf78465"
  409. },
  410. {
  411. "caea65cdbb75e9169ecd22ebe6e54675",
  412. "00000000000000000000000000000000",
  413. "6e29201190152df4ee058139def610bb",
  414. }
  415. };
  416. int ret = 0;
  417. unsigned int i;
  418. u8 key[16], plain[16], cipher[16], out[16];
  419. for (i = 0; i < ARRAY_SIZE(vectors); i++) {
  420. struct ecb_test_vector *tv = &vectors[i];
  421. if (hexstr2bin(tv->key, key, sizeof(key)) ||
  422. hexstr2bin(tv->plaintext, plain, sizeof(plain)) ||
  423. hexstr2bin(tv->ciphertext, cipher, sizeof(cipher))) {
  424. wpa_printf(MSG_ERROR, "Invalid AES-ECB test vector %u",
  425. i);
  426. ret++;
  427. continue;
  428. }
  429. if (aes_128_encrypt_block(key, plain, out) < 0 ||
  430. os_memcmp(out, cipher, 16) != 0) {
  431. wpa_printf(MSG_ERROR, "AES-ECB encrypt %u failed", i);
  432. ret++;
  433. }
  434. }
  435. if (!ret)
  436. wpa_printf(MSG_INFO, "AES ECB mode test cases passed");
  437. return ret;
  438. #endif /* EAP_PSK */
  439. return 0;
  440. }
  441. static int test_key_wrap(void)
  442. {
  443. int ret = 0;
  444. /* RFC 3394 - Test vector 4.1 */
  445. u8 kek41[] = {
  446. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  447. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  448. };
  449. u8 plain41[] = {
  450. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  451. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  452. };
  453. u8 crypt41[] = {
  454. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  455. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  456. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  457. };
  458. /* RFC 3394 - Test vector 4.2 */
  459. u8 kek42[] = {
  460. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  461. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  462. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  463. };
  464. u8 plain42[] = {
  465. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  466. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  467. };
  468. u8 crypt42[] = {
  469. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  470. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  471. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  472. };
  473. /* RFC 3394 - Test vector 4.3 */
  474. u8 kek43[] = {
  475. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  476. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  477. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  478. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  479. };
  480. u8 plain43[] = {
  481. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  482. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  483. };
  484. u8 crypt43[] = {
  485. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  486. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  487. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7,
  488. };
  489. /* RFC 3394 - Test vector 4.4 */
  490. u8 kek44[] = {
  491. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  492. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  493. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  494. };
  495. u8 plain44[] = {
  496. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  497. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  498. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  499. };
  500. u8 crypt44[] = {
  501. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  502. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  503. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  504. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  505. };
  506. /* RFC 3394 - Test vector 4.5 */
  507. u8 kek45[] = {
  508. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  509. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  510. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  511. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  512. };
  513. u8 plain45[] = {
  514. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  515. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  516. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  517. };
  518. u8 crypt45[] = {
  519. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  520. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  521. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  522. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1,
  523. };
  524. /* RFC 3394 - Test vector 4.6 */
  525. u8 kek46[] = {
  526. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  527. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  528. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  529. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  530. };
  531. u8 plain46[] = {
  532. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  533. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  534. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  535. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  536. };
  537. u8 crypt46[] = {
  538. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  539. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  540. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  541. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  542. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  543. };
  544. u8 result[40];
  545. wpa_printf(MSG_INFO, "RFC 3394 - Test vector 4.1");
  546. if (aes_wrap(kek41, sizeof(kek41), sizeof(plain41) / 8, plain41,
  547. result)) {
  548. wpa_printf(MSG_ERROR, "AES-WRAP-128 reported failure");
  549. ret++;
  550. }
  551. if (os_memcmp(result, crypt41, sizeof(crypt41)) != 0) {
  552. wpa_printf(MSG_ERROR, "AES-WRAP-128 failed");
  553. ret++;
  554. }
  555. if (aes_unwrap(kek41, sizeof(kek41), sizeof(plain41) / 8, crypt41,
  556. result)) {
  557. wpa_printf(MSG_ERROR, "AES-UNWRAP-128 reported failure");
  558. ret++;
  559. }
  560. if (os_memcmp(result, plain41, sizeof(plain41)) != 0) {
  561. wpa_printf(MSG_ERROR, "AES-UNWRAP-128 failed");
  562. ret++;
  563. }
  564. wpa_printf(MSG_INFO, "RFC 3394 - Test vector 4.2");
  565. if (aes_wrap(kek42, sizeof(kek42), sizeof(plain42) / 8, plain42,
  566. result)) {
  567. wpa_printf(MSG_ERROR, "AES-WRAP-192 reported failure");
  568. ret++;
  569. }
  570. if (os_memcmp(result, crypt42, sizeof(crypt42)) != 0) {
  571. wpa_printf(MSG_ERROR, "AES-WRAP-192 failed");
  572. ret++;
  573. }
  574. if (aes_unwrap(kek42, sizeof(kek42), sizeof(plain42) / 8, crypt42,
  575. result)) {
  576. wpa_printf(MSG_ERROR, "AES-UNWRAP-192 reported failure");
  577. ret++;
  578. }
  579. if (os_memcmp(result, plain42, sizeof(plain42)) != 0) {
  580. wpa_printf(MSG_ERROR, "AES-UNWRAP-192 failed");
  581. ret++;
  582. }
  583. wpa_printf(MSG_INFO, "RFC 3394 - Test vector 4.3");
  584. if (aes_wrap(kek43, sizeof(kek43), sizeof(plain43) / 8, plain43,
  585. result)) {
  586. wpa_printf(MSG_ERROR, "AES-WRAP-256 reported failure");
  587. ret++;
  588. }
  589. if (os_memcmp(result, crypt43, sizeof(crypt43)) != 0) {
  590. wpa_printf(MSG_ERROR, "AES-WRAP-256 failed");
  591. ret++;
  592. }
  593. if (aes_unwrap(kek43, sizeof(kek43), sizeof(plain43) / 8, crypt43,
  594. result)) {
  595. wpa_printf(MSG_ERROR, "AES-UNWRAP-256 reported failure");
  596. ret++;
  597. }
  598. if (os_memcmp(result, plain43, sizeof(plain43)) != 0) {
  599. wpa_printf(MSG_ERROR, "AES-UNWRAP-256 failed");
  600. ret++;
  601. }
  602. wpa_printf(MSG_INFO, "RFC 3394 - Test vector 4.4");
  603. if (aes_wrap(kek44, sizeof(kek44), sizeof(plain44) / 8, plain44,
  604. result)) {
  605. wpa_printf(MSG_ERROR, "AES-WRAP-192 reported failure");
  606. ret++;
  607. }
  608. if (os_memcmp(result, crypt44, sizeof(crypt44)) != 0) {
  609. wpa_printf(MSG_ERROR, "AES-WRAP-192 failed");
  610. ret++;
  611. }
  612. if (aes_unwrap(kek44, sizeof(kek44), sizeof(plain44) / 8, crypt44,
  613. result)) {
  614. wpa_printf(MSG_ERROR, "AES-UNWRAP-192 reported failure");
  615. ret++;
  616. }
  617. if (os_memcmp(result, plain44, sizeof(plain44)) != 0) {
  618. wpa_printf(MSG_ERROR, "AES-UNWRAP-192 failed");
  619. ret++;
  620. }
  621. wpa_printf(MSG_INFO, "RFC 3394 - Test vector 4.5");
  622. if (aes_wrap(kek45, sizeof(kek45), sizeof(plain45) / 8, plain45,
  623. result)) {
  624. wpa_printf(MSG_ERROR, "AES-WRAP-256 reported failure");
  625. ret++;
  626. }
  627. if (os_memcmp(result, crypt45, sizeof(crypt45)) != 0) {
  628. wpa_printf(MSG_ERROR, "AES-WRAP-256 failed");
  629. ret++;
  630. }
  631. if (aes_unwrap(kek45, sizeof(kek45), sizeof(plain45) / 8, crypt45,
  632. result)) {
  633. wpa_printf(MSG_ERROR, "AES-UNWRAP-256 reported failure");
  634. ret++;
  635. }
  636. if (os_memcmp(result, plain45, sizeof(plain45)) != 0) {
  637. wpa_printf(MSG_ERROR, "AES-UNWRAP-256 failed");
  638. ret++;
  639. }
  640. wpa_printf(MSG_INFO, "RFC 3394 - Test vector 4.6");
  641. if (aes_wrap(kek46, sizeof(kek46), sizeof(plain46) / 8, plain46,
  642. result)) {
  643. wpa_printf(MSG_ERROR, "AES-WRAP-256 reported failure");
  644. ret++;
  645. }
  646. if (os_memcmp(result, crypt46, sizeof(crypt46)) != 0) {
  647. wpa_printf(MSG_ERROR, "AES-WRAP-256 failed");
  648. ret++;
  649. }
  650. if (aes_unwrap(kek46, sizeof(kek46), sizeof(plain46) / 8, crypt46,
  651. result)) {
  652. wpa_printf(MSG_ERROR, "AES-UNWRAP-256 reported failure");
  653. ret++;
  654. }
  655. if (os_memcmp(result, plain46, sizeof(plain46)) != 0) {
  656. wpa_printf(MSG_ERROR, "AES-UNWRAP-256 failed");
  657. ret++;
  658. }
  659. if (!ret)
  660. wpa_printf(MSG_INFO, "AES key wrap/unwrap test cases passed");
  661. return ret;
  662. }
  663. int crypto_module_tests(void)
  664. {
  665. int ret = 0;
  666. wpa_printf(MSG_INFO, "crypto module tests");
  667. if (test_siv() ||
  668. test_omac1() ||
  669. test_eax() ||
  670. test_cbc() ||
  671. test_ecb() ||
  672. test_key_wrap())
  673. ret = -1;
  674. return ret;
  675. }