test-aes.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Test program for AES
  3. * Copyright (c) 2003-2006, 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 < sizeof(vectors) / sizeof(vectors[0]); 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. /* OMAC1 AES-128 test vectors from
  147. * http://csrc.nist.gov/CryptoToolkit/modes/proposedmodes/omac/omac-ad.pdf
  148. * which are same as the examples from NIST SP800-38B
  149. * http://csrc.nist.gov/CryptoToolkit/modes/800-38_Series_Publications/SP800-38B.pdf
  150. */
  151. struct omac1_test_vector {
  152. u8 k[16];
  153. u8 msg[64];
  154. int msg_len;
  155. u8 tag[16];
  156. };
  157. static struct omac1_test_vector test_vectors[] =
  158. {
  159. {
  160. { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  161. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
  162. { },
  163. 0,
  164. { 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  165. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46 }
  166. },
  167. {
  168. { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  169. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
  170. { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  171. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a},
  172. 16,
  173. { 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  174. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c }
  175. },
  176. {
  177. { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  178. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
  179. { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  180. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  181. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  182. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  183. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11 },
  184. 40,
  185. { 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  186. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27 }
  187. },
  188. {
  189. { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  190. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
  191. { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  192. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  193. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  194. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  195. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  196. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  197. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  198. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 },
  199. 64,
  200. { 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  201. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe }
  202. },
  203. };
  204. int main(int argc, char *argv[])
  205. {
  206. u8 kek[] = {
  207. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  208. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  209. };
  210. u8 plain[] = {
  211. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  212. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  213. };
  214. u8 crypt[] = {
  215. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  216. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  217. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  218. };
  219. u8 result[24];
  220. int ret = 0;
  221. unsigned int i;
  222. struct omac1_test_vector *tv;
  223. if (aes_wrap(kek, 2, plain, result)) {
  224. printf("AES-WRAP-128-128 reported failure\n");
  225. ret++;
  226. }
  227. if (memcmp(result, crypt, 24) != 0) {
  228. printf("AES-WRAP-128-128 failed\n");
  229. ret++;
  230. }
  231. if (aes_unwrap(kek, 2, crypt, result)) {
  232. printf("AES-UNWRAP-128-128 reported failure\n");
  233. ret++;
  234. }
  235. if (memcmp(result, plain, 16) != 0) {
  236. printf("AES-UNWRAP-128-128 failed\n");
  237. ret++;
  238. for (i = 0; i < 16; i++)
  239. printf(" %02x", result[i]);
  240. printf("\n");
  241. }
  242. test_aes_perf();
  243. for (i = 0; i < sizeof(test_vectors) / sizeof(test_vectors[0]); i++) {
  244. tv = &test_vectors[i];
  245. if (omac1_aes_128(tv->k, tv->msg, tv->msg_len, result) ||
  246. memcmp(result, tv->tag, 16) != 0) {
  247. printf("OMAC1-AES-128 test vector %d failed\n", i);
  248. ret++;
  249. }
  250. if (tv->msg_len > 1) {
  251. const u8 *addr[2];
  252. size_t len[2];
  253. addr[0] = tv->msg;
  254. len[0] = 1;
  255. addr[1] = tv->msg + 1;
  256. len[1] = tv->msg_len - 1;
  257. if (omac1_aes_128_vector(tv->k, 2, addr, len,
  258. result) ||
  259. memcmp(result, tv->tag, 16) != 0) {
  260. printf("OMAC1-AES-128(vector) test vector %d "
  261. "failed\n", i);
  262. ret++;
  263. }
  264. }
  265. }
  266. ret += test_eax();
  267. ret += test_cbc();
  268. if (ret)
  269. printf("FAILED!\n");
  270. return ret;
  271. }