aestest.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <gmssl/aes.h>
  13. #include <gmssl/hex.h>
  14. #include <gmssl/error.h>
  15. int test_aes(void)
  16. {
  17. AES_KEY aes_key;
  18. /* test 1 */
  19. uint8_t key128[16] = {
  20. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  21. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
  22. };
  23. uint32_t rk128[4 * 11] = {
  24. 0x2b7e1516, 0x28aed2a6, 0xabf71588, 0x09cf4f3c,
  25. 0xa0fafe17, 0x88542cb1, 0x23a33939, 0x2a6c7605,
  26. 0xf2c295f2, 0x7a96b943, 0x5935807a, 0x7359f67f,
  27. 0x3d80477d, 0x4716fe3e, 0x1e237e44, 0x6d7a883b,
  28. 0xef44a541, 0xa8525b7f, 0xb671253b, 0xdb0bad00,
  29. 0xd4d1c6f8, 0x7c839d87, 0xcaf2b8bc, 0x11f915bc,
  30. 0x6d88a37a, 0x110b3efd, 0xdbf98641, 0xca0093fd,
  31. 0x4e54f70e, 0x5f5fc9f3, 0x84a64fb2, 0x4ea6dc4f,
  32. 0xead27321, 0xb58dbad2, 0x312bf560, 0x7f8d292f,
  33. 0xac7766f3, 0x19fadc21, 0x28d12941, 0x575c006e,
  34. 0xd014f9a8, 0xc9ee2589, 0xe13f0cc8, 0xb6630ca6,
  35. };
  36. /* test 2 */
  37. uint8_t key192[24] = {
  38. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  39. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  40. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b,
  41. };
  42. uint32_t rk192[4 * 13] = {
  43. 0x8e73b0f7, 0xda0e6452, 0xc810f32b, 0x809079e5,
  44. 0x62f8ead2, 0x522c6b7b, 0xfe0c91f7, 0x2402f5a5,
  45. 0xec12068e, 0x6c827f6b, 0x0e7a95b9, 0x5c56fec2,
  46. 0x4db7b4bd, 0x69b54118, 0x85a74796, 0xe92538fd,
  47. 0xe75fad44, 0xbb095386, 0x485af057, 0x21efb14f,
  48. 0xa448f6d9, 0x4d6dce24, 0xaa326360, 0x113b30e6,
  49. 0xa25e7ed5, 0x83b1cf9a, 0x27f93943, 0x6a94f767,
  50. 0xc0a69407, 0xd19da4e1, 0xec1786eb, 0x6fa64971,
  51. 0x485f7032, 0x22cb8755, 0xe26d1352, 0x33f0b7b3,
  52. 0x40beeb28, 0x2f18a259, 0x6747d26b, 0x458c553e,
  53. 0xa7e1466c, 0x9411f1df, 0x821f750a, 0xad07d753,
  54. 0xca400538, 0x8fcc5006, 0x282d166a, 0xbc3ce7b5,
  55. 0xe98ba06f, 0x448c773c, 0x8ecc7204, 0x01002202,
  56. };
  57. /* test 3 */
  58. uint8_t key256[32] = {
  59. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  60. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  61. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  62. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4,
  63. };
  64. uint32_t rk256[4 * 15] = {
  65. 0x603deb10, 0x15ca71be, 0x2b73aef0, 0x857d7781,
  66. 0x1f352c07, 0x3b6108d7, 0x2d9810a3, 0x0914dff4,
  67. 0x9ba35411, 0x8e6925af, 0xa51a8b5f, 0x2067fcde,
  68. 0xa8b09c1a, 0x93d194cd, 0xbe49846e, 0xb75d5b9a,
  69. 0xd59aecb8, 0x5bf3c917, 0xfee94248, 0xde8ebe96,
  70. 0xb5a9328a, 0x2678a647, 0x98312229, 0x2f6c79b3,
  71. 0x812c81ad, 0xdadf48ba, 0x24360af2, 0xfab8b464,
  72. 0x98c5bfc9, 0xbebd198e, 0x268c3ba7, 0x09e04214,
  73. 0x68007bac, 0xb2df3316, 0x96e939e4, 0x6c518d80,
  74. 0xc814e204, 0x76a9fb8a, 0x5025c02d, 0x59c58239,
  75. 0xde136967, 0x6ccc5a71, 0xfa256395, 0x9674ee15,
  76. 0x5886ca5d, 0x2e2f31d7, 0x7e0af1fa, 0x27cf73c3,
  77. 0x749c47ab, 0x18501dda, 0xe2757e4f, 0x7401905a,
  78. 0xcafaaae3, 0xe4d59b34, 0x9adf6ace, 0xbd10190d,
  79. 0xfe4890d1, 0xe6188d0b, 0x046df344, 0x706c631e,
  80. };
  81. /* test 4 */
  82. unsigned char in1[16] = {
  83. 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
  84. 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34,
  85. };
  86. unsigned char out1[16] = {
  87. 0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb,
  88. 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32,
  89. };
  90. unsigned char buf[16] = {0};
  91. printf("aes test 1 ");
  92. aes_set_encrypt_key(&aes_key, key128, sizeof(key128));
  93. if (memcmp(&aes_key, rk128, sizeof(rk128)) != 0) {
  94. printf("failed\n");
  95. return -1;
  96. } else {
  97. printf("ok\n");
  98. }
  99. printf("aes test 2 ");
  100. aes_set_encrypt_key(&aes_key, key192, sizeof(key192));
  101. if (memcmp(&aes_key, rk192, sizeof(rk192)) != 0) {
  102. printf("failed\n");
  103. return -1;
  104. } else {
  105. printf("ok\n");
  106. }
  107. printf("aes test 3 ");
  108. aes_set_encrypt_key(&aes_key, key256, sizeof(key256));
  109. if (memcmp(&aes_key, rk256, sizeof(rk256)) != 0) {
  110. printf("failed\n");
  111. return -1;
  112. } else {
  113. printf("ok\n");
  114. }
  115. printf("aes test 4 ");
  116. aes_set_encrypt_key(&aes_key, key128, sizeof(key128));
  117. aes_encrypt(&aes_key, in1, buf);
  118. if (memcmp(buf, out1, sizeof(out1)) != 0) {
  119. printf("failed\n");
  120. return -1;
  121. } else {
  122. printf("ok\n");
  123. }
  124. printf("aes test 5 ");
  125. aes_set_decrypt_key(&aes_key, key128, sizeof(key128));
  126. aes_decrypt(&aes_key, buf, buf);
  127. if (memcmp(buf, in1, sizeof(in1)) != 0) {
  128. printf("failed\n");
  129. return -1;
  130. } else {
  131. printf("ok\n");
  132. }
  133. printf("%s() ok\n", __FUNCTION__);
  134. return 1;
  135. }
  136. int test_aes_ctr(void)
  137. {
  138. // NIST SP 800-38A F.5.1
  139. char *hex_key = "2b7e151628aed2a6abf7158809cf4f3c";
  140. char *hex_ctr = "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
  141. char *hex_msg = "6bc1bee22e409f96e93d7e117393172a"
  142. "ae2d8a571e03ac9c9eb76fac45af8e51"
  143. "30c81c46a35ce411e5fbc1191a0a52ef"
  144. "f69f2445df4f9b17ad2b417be66c3710";
  145. char *hex_out = "874d6191b620e3261bef6864990db6ce"
  146. "9806f66b7970fdff8617187bb9fffdff"
  147. "5ae4df3edbd5d35e5b4f09020db03eab"
  148. "1e031dda2fbe03d1792170a0f3009cee";
  149. AES_KEY aes_key;
  150. uint8_t key[32];
  151. uint8_t ctr[16];
  152. uint8_t msg[64];
  153. uint8_t out[64];
  154. uint8_t buf[64];
  155. size_t keylen, ctrlen, msglen, outlen, buflen;
  156. hex_to_bytes(hex_key, strlen(hex_key), key, &keylen);
  157. hex_to_bytes(hex_ctr, strlen(hex_ctr), ctr, &ctrlen);
  158. hex_to_bytes(hex_msg, strlen(hex_msg), msg, &msglen);
  159. hex_to_bytes(hex_out, strlen(hex_out), out, &outlen);
  160. aes_set_encrypt_key(&aes_key, key, keylen);
  161. aes_ctr_encrypt(&aes_key, ctr, msg, msglen, buf);
  162. buflen = msglen;
  163. printf("aes ctr test 1 ");
  164. if (memcmp(buf, out, outlen) != 0) {
  165. printf("failed\n");
  166. format_bytes(stdout, 0, 0, "aes_ctr(msg) = ", buf, buflen);
  167. format_bytes(stdout, 0, 0, " != ", out, outlen);
  168. return -1;
  169. } else {
  170. printf("ok\n");
  171. }
  172. printf("aes ctr test 2 ");
  173. hex_to_bytes(hex_ctr, strlen(hex_ctr), ctr, &ctrlen);
  174. aes_ctr_decrypt(&aes_key, ctr, buf, buflen, buf);
  175. if (memcmp(buf, msg, msglen) != 0) {
  176. printf("failed\n");
  177. format_bytes(stdout, 0, 0, "msg = ", msg, msglen);
  178. format_bytes(stdout, 0, 0, " = ", buf, buflen);
  179. return -1;
  180. } else {
  181. printf("ok\n");
  182. }
  183. printf("%s() ok\n", __FUNCTION__);
  184. return 1;
  185. }
  186. struct {
  187. char *K;
  188. char *P;
  189. char *A;
  190. char *IV;
  191. char *C;
  192. char *T;
  193. } aes_gcm_tests[] = {
  194. // test 1
  195. {
  196. "00000000000000000000000000000000",
  197. "",
  198. "",
  199. "000000000000000000000000",
  200. "",
  201. "58e2fccefa7e3061367f1d57a4e7455a",
  202. },
  203. // test 2
  204. {
  205. "00000000000000000000000000000000",
  206. "00000000000000000000000000000000",
  207. "",
  208. "000000000000000000000000",
  209. "0388dace60b6a392f328c2b971b2fe78",
  210. "ab6e47d42cec13bdf53a67b21257bddf",
  211. },
  212. // test 3
  213. {
  214. "feffe9928665731c6d6a8f9467308308",
  215. "d9313225f88406e5a55909c5aff5269a"
  216. "86a7a9531534f7da2e4c303d8a318a72"
  217. "1c3c0c95956809532fcf0e2449a6b525"
  218. "b16aedf5aa0de657ba637b391aafd255",
  219. "",
  220. "cafebabefacedbaddecaf888",
  221. "42831ec2217774244b7221b784d0d49c"
  222. "e3aa212f2c02a4e035c17e2329aca12e"
  223. "21d514b25466931c7d8f6a5aac84aa05"
  224. "1ba30b396a0aac973d58e091473f5985",
  225. "4d5c2af327cd64a62cf35abd2ba6fab4",
  226. },
  227. // test 4
  228. {
  229. "feffe9928665731c6d6a8f9467308308",
  230. "d9313225f88406e5a55909c5aff5269a"
  231. "86a7a9531534f7da2e4c303d8a318a72"
  232. "1c3c0c95956809532fcf0e2449a6b525"
  233. "b16aedf5aa0de657ba637b39",
  234. "feedfacedeadbeeffeedfacedeadbeef"
  235. "abaddad2",
  236. "cafebabefacedbaddecaf888",
  237. "42831ec2217774244b7221b784d0d49c"
  238. "e3aa212f2c02a4e035c17e2329aca12e"
  239. "21d514b25466931c7d8f6a5aac84aa05"
  240. "1ba30b396a0aac973d58e091",
  241. "5bc94fbc3221a5db94fae95ae7121a47",
  242. },
  243. // test 5
  244. {
  245. "feffe9928665731c6d6a8f9467308308",
  246. "d9313225f88406e5a55909c5aff5269a"
  247. "86a7a9531534f7da2e4c303d8a318a72"
  248. "1c3c0c95956809532fcf0e2449a6b525"
  249. "b16aedf5aa0de657ba637b39",
  250. "feedfacedeadbeeffeedfacedeadbeef"
  251. "abaddad2",
  252. "cafebabefacedbad",
  253. "61353b4c2806934a777ff51fa22a4755"
  254. "699b2a714fcdc6f83766e5f97b6c7423"
  255. "73806900e49f24b22b097544d4896b42"
  256. "4989b5e1ebac0f07c23f4598",
  257. "3612d2e79e3b0785561be14aaca2fccb",
  258. },
  259. // test 6
  260. {
  261. "feffe9928665731c6d6a8f9467308308",
  262. "d9313225f88406e5a55909c5aff5269a"
  263. "86a7a9531534f7da2e4c303d8a318a72"
  264. "1c3c0c95956809532fcf0e2449a6b525"
  265. "b16aedf5aa0de657ba637b39",
  266. "feedfacedeadbeeffeedfacedeadbeef"
  267. "abaddad2",
  268. "9313225df88406e555909c5aff5269aa"
  269. "6a7a9538534f7da1e4c303d2a318a728"
  270. "c3c0c95156809539fcf0e2429a6b5254"
  271. "16aedbf5a0de6a57a637b39b",
  272. "8ce24998625615b603a033aca13fb894"
  273. "be9112a5c3a211a8ba262a3cca7e2ca7"
  274. "01e4a9a4fba43c90ccdcb281d48c7c6f"
  275. "d62875d2aca417034c34aee5",
  276. "619cc5aefffe0bfa462af43c1699d050",
  277. },
  278. // test 7
  279. {
  280. "00000000000000000000000000000000"
  281. "0000000000000000",
  282. "",
  283. "",
  284. "000000000000000000000000",
  285. "",
  286. "cd33b28ac773f74ba00ed1f312572435",
  287. },
  288. };
  289. int test_aes_gcm(void)
  290. {
  291. int err = 0;
  292. uint8_t K[32];
  293. uint8_t P[64];
  294. uint8_t A[32];
  295. uint8_t IV[64];
  296. uint8_t C[64];
  297. uint8_t T[16];
  298. size_t Klen, Plen, Alen, IVlen, Clen, Tlen;
  299. AES_KEY aes_key;
  300. uint8_t out[64];
  301. uint8_t tag[16];
  302. uint8_t buf[64];
  303. int i;
  304. for (i = 0; i < sizeof(aes_gcm_tests)/sizeof(aes_gcm_tests[0]); i++) {
  305. hex_to_bytes(aes_gcm_tests[i].K, strlen(aes_gcm_tests[i].K), K, &Klen);
  306. hex_to_bytes(aes_gcm_tests[i].P, strlen(aes_gcm_tests[i].P), P, &Plen);
  307. hex_to_bytes(aes_gcm_tests[i].A, strlen(aes_gcm_tests[i].A), A, &Alen);
  308. hex_to_bytes(aes_gcm_tests[i].IV, strlen(aes_gcm_tests[i].IV), IV, &IVlen);
  309. hex_to_bytes(aes_gcm_tests[i].C, strlen(aes_gcm_tests[i].C), C, &Clen);
  310. hex_to_bytes(aes_gcm_tests[i].T, strlen(aes_gcm_tests[i].T), T, &Tlen);
  311. aes_set_encrypt_key(&aes_key, K, Klen);
  312. aes_gcm_encrypt(&aes_key, IV, IVlen, A, Alen, P, Plen, out, Tlen, tag);
  313. printf("aes gcm test %d ", i + 1);
  314. if (aes_gcm_decrypt(&aes_key, IV, IVlen, A, Alen, out, Plen, tag, Tlen, buf) != 1
  315. || memcmp(buf, P, Plen) != 0) {
  316. printf("failed\n");
  317. format_print(stdout, 0, 2, "K = %s\n", aes_gcm_tests[i].K);
  318. format_print(stdout, 0, 2, "P = %s\n", aes_gcm_tests[i].P);
  319. format_print(stdout, 0, 2, "A = %s\n", aes_gcm_tests[i].A);
  320. format_print(stdout, 0, 2, "IV = %s\n", aes_gcm_tests[i].IV);
  321. format_print(stdout, 0, 2, "C = %s\n", aes_gcm_tests[i].C);
  322. format_bytes(stdout, 0, 2, " = ", out, Plen);
  323. format_print(stdout, 0, 2, "T = %s\n", aes_gcm_tests[i].T);
  324. format_bytes(stdout, 0, 2, " = ", tag, Tlen);
  325. return -1;
  326. } else {
  327. printf("ok\n");
  328. }
  329. }
  330. printf("%s() ok\n", __FUNCTION__);
  331. return 1;
  332. }
  333. int main(void)
  334. {
  335. if (test_aes() != 1) goto err;
  336. if (test_aes_ctr() != 1) goto err;
  337. if (test_aes_gcm() != 1) goto err;
  338. printf("%s all tests passed!\n", __FILE__);
  339. return 0;
  340. err:
  341. error_print();
  342. return 1;
  343. }