test_aes_gcm.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /* mbedTLS GCM test
  2. *
  3. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <stdbool.h>
  10. #include <esp_system.h>
  11. #include "mbedtls/aes.h"
  12. #include "mbedtls/gcm.h"
  13. #include "unity.h"
  14. #include "sdkconfig.h"
  15. #include "esp_heap_caps.h"
  16. #include "test_utils.h"
  17. #include "ccomp_timer.h"
  18. #include "sys/param.h"
  19. #if CONFIG_MBEDTLS_HARDWARE_GCM
  20. /*
  21. Python example code for generating test vectors
  22. import os, binascii
  23. from cryptography.hazmat.primitives.ciphers.aead import AESGCM
  24. def as_c_array(byte_arr):
  25. hex_str = ''
  26. for idx, byte in enumerate(byte_arr):
  27. hex_str += "0x{:02x}, ".format(byte)
  28. bytes_per_line = 8
  29. if idx % bytes_per_line == bytes_per_line - 1:
  30. hex_str += '\n'
  31. return hex_str
  32. key = b'\x44' * 16
  33. iv = b'\xEE' * 16
  34. data = b'\xAA' * 3200
  35. aad = b'\x76' * 16
  36. aesgcm = AESGCM(key)
  37. ct = aesgcm.encrypt(iv, data, aad)
  38. print(as_c_array(ct))
  39. */
  40. TEST_CASE("mbedtls GCM stream test", "[aes-gcm]")
  41. {
  42. const unsigned SZ = 100;
  43. mbedtls_gcm_context ctx;
  44. uint8_t nonce[16];
  45. uint8_t key[16];
  46. uint8_t tag[16];
  47. mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
  48. const uint8_t expected_cipher[] = {
  49. 0x03, 0x92, 0x13, 0x49, 0x1f, 0x1f, 0x24, 0x41,
  50. 0xe8, 0xeb, 0x89, 0x47, 0x50, 0x0a, 0xce, 0xa3,
  51. 0xc7, 0x1c, 0x10, 0x70, 0xb0, 0x89, 0x82, 0x5e,
  52. 0x0f, 0x4a, 0x23, 0xee, 0xd2, 0xfc, 0xff, 0x45,
  53. 0x61, 0x4c, 0xd1, 0xfb, 0x6d, 0xe2, 0xbe, 0x67,
  54. 0x6f, 0x94, 0x72, 0xa3, 0xe7, 0x04, 0x99, 0xb3,
  55. 0x4a, 0x46, 0xf9, 0x2b, 0xaf, 0xac, 0xa9, 0x0e,
  56. 0x43, 0x7e, 0x8b, 0xc4, 0xbf, 0x49, 0xa4, 0x83,
  57. 0x9c, 0x31, 0x11, 0x1c, 0x09, 0xac, 0x90, 0xdf,
  58. 0x00, 0x34, 0x08, 0xe5, 0x70, 0xa3, 0x7e, 0x4b,
  59. 0x36, 0x48, 0x5a, 0x3f, 0x28, 0xc7, 0x1c, 0xd9,
  60. 0x1b, 0x1b, 0x49, 0x96, 0xe9, 0x7c, 0xea, 0x54,
  61. 0x7c, 0x71, 0x29, 0x0d
  62. };
  63. const uint8_t expected_tag[] = {
  64. 0x35, 0x1c, 0x21, 0xc6, 0xbc, 0x6b, 0x18, 0x52,
  65. 0x90, 0xe1, 0xf2, 0x5b, 0xe1, 0xf6, 0x15, 0xee,
  66. };
  67. memset(nonce, 0x89, 16);
  68. memset(key, 0x56, 16);
  69. // allocate internal memory
  70. uint8_t *ciphertext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  71. uint8_t *plaintext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  72. uint8_t *decryptedtext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  73. TEST_ASSERT_NOT_NULL(ciphertext);
  74. TEST_ASSERT_NOT_NULL(plaintext);
  75. TEST_ASSERT_NOT_NULL(decryptedtext);
  76. memset(plaintext, 0xAB, SZ);
  77. /* Test that all the end results are the same
  78. no matter how many bytes we encrypt each call
  79. */
  80. for (int bytes_to_process = 16; bytes_to_process < SZ; bytes_to_process = bytes_to_process + 16) {
  81. memset(nonce, 0x89, 16);
  82. memset(ciphertext, 0x0, SZ);
  83. memset(decryptedtext, 0x0, SZ);
  84. memset(tag, 0x0, 16);
  85. mbedtls_gcm_init(&ctx);
  86. mbedtls_gcm_setkey(&ctx, cipher, key, 128);
  87. mbedtls_gcm_starts( &ctx, MBEDTLS_AES_ENCRYPT, nonce, sizeof(nonce) );
  88. mbedtls_gcm_update_ad( &ctx, NULL, 0 );
  89. // Encrypt
  90. for (int idx = 0; idx < SZ; idx = idx + bytes_to_process) {
  91. // Limit length of last call to avoid exceeding buffer size
  92. size_t length = (idx + bytes_to_process > SZ) ? (SZ - idx) : bytes_to_process;
  93. mbedtls_gcm_update(&ctx, plaintext + idx, length, ciphertext + idx, 0, NULL);
  94. }
  95. size_t olen;
  96. mbedtls_gcm_finish( &ctx, NULL, 0, &olen, tag, sizeof(tag) );
  97. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher, ciphertext, SZ);
  98. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_tag, tag, sizeof(tag));
  99. // Decrypt
  100. memset(nonce, 0x89, 16);
  101. mbedtls_gcm_free( &ctx );
  102. mbedtls_gcm_init(&ctx);
  103. mbedtls_gcm_setkey(&ctx, cipher, key, 128);
  104. mbedtls_gcm_starts( &ctx, MBEDTLS_AES_DECRYPT, nonce, sizeof(nonce));
  105. mbedtls_gcm_update_ad( &ctx, NULL, 0 );
  106. for (int idx = 0; idx < SZ; idx = idx + bytes_to_process) {
  107. // Limit length of last call to avoid exceeding buffer size
  108. size_t length = (idx + bytes_to_process > SZ) ? (SZ - idx) : bytes_to_process;
  109. mbedtls_gcm_update(&ctx, ciphertext + idx, length, decryptedtext + idx, 0, NULL);
  110. }
  111. mbedtls_gcm_finish( &ctx, NULL, 0, &olen, tag, sizeof(tag) );
  112. TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, SZ);
  113. mbedtls_gcm_free( &ctx );
  114. }
  115. free(plaintext);
  116. free(ciphertext);
  117. free(decryptedtext);
  118. }
  119. TEST_CASE("mbedtls AES GCM self-tests", "[aes-gcm]")
  120. {
  121. TEST_ASSERT_FALSE_MESSAGE(mbedtls_gcm_self_test(1), "AES GCM self-test should pass.");
  122. }
  123. typedef struct {
  124. uint8_t *plaintext;
  125. size_t plaintext_length;
  126. uint32_t output_caps;
  127. uint8_t *add_buf;
  128. size_t add_length;
  129. uint8_t *iv;
  130. size_t iv_length;
  131. uint8_t *key;
  132. size_t key_bits;
  133. size_t tag_len;
  134. } aes_gcm_test_cfg_t;
  135. typedef struct {
  136. const uint8_t *expected_tag;
  137. const uint8_t *ciphertext_last_block; // Last block of the ciphertext
  138. } aes_gcm_test_expected_res_t;
  139. typedef enum {
  140. AES_GCM_TEST_CRYPT_N_TAG,
  141. AES_GCM_TEST_START_UPDATE_FINISH,
  142. } aes_gcm_test_type_t;
  143. static void aes_gcm_test(aes_gcm_test_cfg_t *cfg, aes_gcm_test_expected_res_t *res, aes_gcm_test_type_t aes_gcm_type)
  144. {
  145. mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
  146. mbedtls_gcm_context ctx;
  147. uint8_t tag_buf_encrypt[16] = {};
  148. uint8_t tag_buf_decrypt[16] = {};
  149. uint8_t iv_buf[16] = {};
  150. uint8_t *ciphertext = heap_caps_malloc(cfg->plaintext_length, cfg->output_caps);
  151. uint8_t *output = heap_caps_malloc(cfg->plaintext_length, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  152. if (cfg->plaintext_length != 0) {
  153. TEST_ASSERT_NOT_NULL(ciphertext);
  154. TEST_ASSERT_NOT_NULL(output);
  155. }
  156. memset(ciphertext, 0, cfg->plaintext_length);
  157. memset(output, 0, cfg->plaintext_length);
  158. memcpy(iv_buf, cfg->iv, cfg->iv_length);
  159. mbedtls_gcm_init(&ctx);
  160. mbedtls_gcm_setkey(&ctx, cipher, cfg->key, cfg->key_bits);
  161. size_t olen;
  162. /* Encrypt and tag */
  163. if (aes_gcm_type == AES_GCM_TEST_CRYPT_N_TAG) {
  164. mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_AES_ENCRYPT, cfg->plaintext_length, iv_buf, cfg->iv_length, cfg->add_buf, cfg->add_length, cfg->plaintext, ciphertext, cfg->tag_len, tag_buf_encrypt);
  165. } else if (aes_gcm_type == AES_GCM_TEST_START_UPDATE_FINISH) {
  166. TEST_ASSERT(mbedtls_gcm_starts( &ctx, MBEDTLS_AES_ENCRYPT, iv_buf, cfg->iv_length) == 0 );
  167. TEST_ASSERT(mbedtls_gcm_update_ad( &ctx, cfg->add_buf, cfg->add_length) == 0 );
  168. TEST_ASSERT(mbedtls_gcm_update( &ctx, cfg->plaintext, cfg->plaintext_length, ciphertext, 0, NULL) == 0 );
  169. TEST_ASSERT(mbedtls_gcm_finish( &ctx, NULL, 0, &olen, tag_buf_encrypt, cfg->tag_len) == 0 );
  170. }
  171. size_t offset = cfg->plaintext_length > 16 ? cfg->plaintext_length - 16 : 0;
  172. /* Sanity check: make sure the last ciphertext block matches what we expect to see. */
  173. TEST_ASSERT_EQUAL_HEX8_ARRAY(res->ciphertext_last_block, ciphertext + offset, MIN(16, cfg->plaintext_length));
  174. TEST_ASSERT_EQUAL_HEX8_ARRAY(res->expected_tag, tag_buf_encrypt, cfg->tag_len);
  175. /* Decrypt and authenticate */
  176. if (aes_gcm_type == AES_GCM_TEST_CRYPT_N_TAG) {
  177. TEST_ASSERT(mbedtls_gcm_auth_decrypt(&ctx, cfg->plaintext_length, iv_buf, cfg->iv_length, cfg->add_buf, cfg->add_length, res->expected_tag, cfg->tag_len, ciphertext, output) == 0);
  178. } else if (aes_gcm_type == AES_GCM_TEST_START_UPDATE_FINISH) {
  179. TEST_ASSERT(mbedtls_gcm_starts( &ctx, MBEDTLS_AES_DECRYPT, iv_buf, cfg->iv_length) == 0 );
  180. TEST_ASSERT(mbedtls_gcm_update_ad( &ctx, cfg->add_buf, cfg->add_length) == 0 );
  181. TEST_ASSERT(mbedtls_gcm_update( &ctx, ciphertext, cfg->plaintext_length, output, 0, NULL) == 0 );
  182. TEST_ASSERT(mbedtls_gcm_finish( &ctx, NULL, 0, &olen, tag_buf_decrypt, cfg->tag_len) == 0 );
  183. /* mbedtls_gcm_auth_decrypt already checks tag so only needed for AES_GCM_TEST_START_UPDATE_FINISH */
  184. TEST_ASSERT_EQUAL_HEX8_ARRAY(res->expected_tag, tag_buf_decrypt, cfg->tag_len);
  185. }
  186. TEST_ASSERT_EQUAL_HEX8_ARRAY(cfg->plaintext, output, cfg->plaintext_length);
  187. free(ciphertext);
  188. free(output);
  189. }
  190. TEST_CASE("mbedtls AES GCM", "[aes-gcm]")
  191. {
  192. uint8_t iv[16];
  193. uint8_t key[16];
  194. uint8_t add[30];
  195. memset(iv, 0xB1, sizeof(iv));
  196. memset(key, 0x27, sizeof(key));
  197. memset(add, 0x90, sizeof(add));
  198. size_t length[] = {10, 16, 500, 5000, 12345};
  199. const uint8_t expected_last_block[][16] = {
  200. {
  201. 0x37, 0x99, 0x4b, 0x16, 0x5f, 0x8d, 0x27, 0xb1,
  202. 0x60, 0x72
  203. },
  204. {
  205. 0x37, 0x99, 0x4b, 0x16, 0x5f, 0x8d, 0x27, 0xb1,
  206. 0x60, 0x72, 0x9a, 0x81, 0x8d, 0x3c, 0x69, 0x66
  207. },
  208. {
  209. 0x9d, 0x7a, 0xac, 0x84, 0xe3, 0x70, 0x43, 0x0f,
  210. 0xa7, 0x83, 0x43, 0xc9, 0x04, 0xf8, 0x7d, 0x48
  211. },
  212. {
  213. 0xee, 0xfd, 0xab, 0x2a, 0x09, 0x44, 0x41, 0x6a,
  214. 0x91, 0xb0, 0x74, 0x24, 0xee, 0x35, 0xb1, 0x39
  215. },
  216. {
  217. 0x51, 0xf7, 0x1f, 0x67, 0x1a, 0x4a, 0x12, 0x37,
  218. 0x60, 0x3b, 0x68, 0x01, 0x20, 0x4f, 0xf3, 0xd9
  219. },
  220. };
  221. const uint8_t expected_tag[][16] = {
  222. {
  223. 0x06, 0x4f, 0xb5, 0x91, 0x12, 0x24, 0xb4, 0x24,
  224. 0x0b, 0xc2, 0x85, 0x59, 0x6a, 0x7c, 0x1f, 0xc9
  225. },
  226. {
  227. 0x45, 0xc2, 0xa8, 0xfe, 0xff, 0x49, 0x1f, 0x45,
  228. 0x8e, 0x29, 0x74, 0x41, 0xed, 0x9b, 0x54, 0x28
  229. },
  230. {
  231. 0xe1, 0xf9, 0x40, 0xfa, 0x29, 0x6f, 0x30, 0xae,
  232. 0xb6, 0x9b, 0x33, 0xdb, 0x8a, 0xf9, 0x70, 0xc4
  233. },
  234. {
  235. 0x22, 0xe1, 0x22, 0x34, 0x0c, 0x91, 0x0b, 0xcf,
  236. 0xa3, 0x42, 0xe0, 0x48, 0xe6, 0xfe, 0x2e, 0x28
  237. },
  238. {
  239. 0xfb, 0xfe, 0x5a, 0xed, 0x26, 0x5c, 0x5e, 0x66,
  240. 0x4e, 0xb2, 0x48, 0xce, 0xe9, 0x88, 0x1c, 0xe0
  241. },
  242. };
  243. aes_gcm_test_cfg_t cfg = {
  244. .output_caps = MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL,
  245. .iv = iv,
  246. .iv_length = sizeof(iv),
  247. .key = key,
  248. .key_bits = 8 * sizeof(key),
  249. .add_buf = add,
  250. .add_length = sizeof(add),
  251. .tag_len = 16
  252. };
  253. aes_gcm_test_expected_res_t res = {
  254. };
  255. for (int i = 0; i < sizeof(length) / sizeof(length[0]); i++) {
  256. printf("Test AES-GCM with plaintext length = %d\n", length[i]);
  257. uint8_t *input = heap_caps_malloc(length[i], MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  258. TEST_ASSERT(input != NULL || length[i] == 0);
  259. memset(input, 0x36, length[i]);
  260. cfg.plaintext = input;
  261. cfg.plaintext_length = length[i];
  262. res.expected_tag = expected_tag[i];
  263. res.ciphertext_last_block = expected_last_block[i],
  264. aes_gcm_test(&cfg, &res, AES_GCM_TEST_CRYPT_N_TAG);
  265. aes_gcm_test(&cfg, &res, AES_GCM_TEST_START_UPDATE_FINISH);
  266. free(input);
  267. }
  268. }
  269. TEST_CASE("mbedtls AES GCM - Different add messages", "[aes-gcm]")
  270. {
  271. const unsigned CALL_SZ = 160;
  272. uint8_t iv[16];
  273. uint8_t key[16];
  274. uint8_t *input = heap_caps_malloc(CALL_SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  275. TEST_ASSERT_NOT_NULL(input);
  276. memset(input, 0x67, CALL_SZ);
  277. memset(iv, 0xA2, sizeof(iv));
  278. memset(key, 0x48, sizeof(key));
  279. const uint8_t expected_last_block[] = {
  280. 0xcd, 0xb9, 0xad, 0x6f, 0xc9, 0x35, 0x21, 0x0d,
  281. 0xc9, 0x5d, 0xea, 0xd9, 0xf7, 0x1d, 0x43, 0xed
  282. };
  283. size_t add_len[] = {0, 10, 16, 500, 5000};
  284. const uint8_t expected_tag[][16] = {
  285. {
  286. 0xe3, 0x91, 0xad, 0x40, 0x96, 0xb7, 0x8c, 0x53,
  287. 0x4d, 0x15, 0x7d, 0x55, 0x15, 0xdf, 0x10, 0x69
  288. },
  289. {
  290. 0xc2, 0x38, 0x36, 0xe9, 0x12, 0x72, 0x5b, 0x31,
  291. 0x0c, 0xde, 0xb5, 0xc9, 0x8c, 0xa3, 0xcb, 0xe7
  292. },
  293. {
  294. 0x57, 0x10, 0x22, 0x91, 0x65, 0xfa, 0x89, 0xba,
  295. 0x0a, 0x3e, 0xc1, 0x7c, 0x93, 0x6e, 0x35, 0xac
  296. },
  297. {
  298. 0x3c, 0x28, 0x03, 0xc2, 0x14, 0x40, 0xec, 0xb6,
  299. 0x25, 0xfb, 0xdd, 0x55, 0xa0, 0xb2, 0x47, 0x7b
  300. },
  301. {
  302. 0xfa, 0x66, 0x4a, 0x97, 0x2d, 0x02, 0x32, 0x5b,
  303. 0x92, 0x94, 0xf1, 0x00, 0x1c, 0xfa, 0xe3, 0x07
  304. }
  305. };
  306. aes_gcm_test_cfg_t cfg = {
  307. .plaintext = input,
  308. .plaintext_length = CALL_SZ,
  309. .output_caps = MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL,
  310. .iv = iv,
  311. .iv_length = sizeof(iv),
  312. .key = key,
  313. .key_bits = 8 * sizeof(key),
  314. .tag_len = 16
  315. };
  316. aes_gcm_test_expected_res_t res = {
  317. .ciphertext_last_block = expected_last_block,
  318. };
  319. for (int i = 0; i < sizeof(add_len) / sizeof(add_len[0]); i++) {
  320. printf("Test AES-GCM with add length = %d\n", add_len[i]);
  321. uint8_t *add = heap_caps_malloc(add_len[i], MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  322. TEST_ASSERT(add != NULL || add_len[i] == 0);
  323. memset(add, 0x12, add_len[i]);
  324. cfg.add_buf = add;
  325. cfg.add_length = add_len[i];
  326. res.expected_tag = expected_tag[i];
  327. aes_gcm_test(&cfg, &res, AES_GCM_TEST_CRYPT_N_TAG);
  328. aes_gcm_test(&cfg, &res, AES_GCM_TEST_START_UPDATE_FINISH);
  329. free(add);
  330. }
  331. free(input);
  332. }
  333. TEST_CASE("mbedtls AES GCM performance, start, update, ret", "[aes-gcm]")
  334. {
  335. const unsigned CALL_SZ = 16 * 3200;
  336. mbedtls_gcm_context ctx;
  337. float elapsed_usec;
  338. unsigned char tag_buf[16];
  339. mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
  340. uint8_t iv[16];
  341. uint8_t key[16];
  342. uint8_t aad[16];
  343. size_t olen;
  344. memset(iv, 0xEE, 16);
  345. memset(key, 0x44, 16);
  346. memset(aad, 0x76, 16);
  347. // allocate internal memory
  348. uint8_t *buf = heap_caps_malloc(CALL_SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  349. TEST_ASSERT_NOT_NULL(buf);
  350. mbedtls_gcm_init(&ctx);
  351. mbedtls_gcm_setkey( &ctx, cipher, key, 128);
  352. ccomp_timer_start();
  353. memset(buf, 0xAA, CALL_SZ);
  354. TEST_ASSERT(mbedtls_gcm_starts( &ctx, MBEDTLS_AES_ENCRYPT, iv, sizeof(iv) ) == 0 );
  355. TEST_ASSERT(mbedtls_gcm_update_ad( &ctx, aad, sizeof(aad)) == 0 );
  356. TEST_ASSERT(mbedtls_gcm_update( &ctx, buf, CALL_SZ, buf, 0, NULL) == 0 );
  357. TEST_ASSERT(mbedtls_gcm_finish( &ctx, NULL, 0, &olen, tag_buf, 16 ) == 0 );
  358. elapsed_usec = ccomp_timer_stop();
  359. /* Sanity check: make sure the last ciphertext block matches
  360. what we expect to see.
  361. */
  362. const uint8_t expected_last_block[] = {
  363. 0xd4, 0x25, 0x88, 0xd4, 0x32, 0x52, 0x3d, 0x6f,
  364. 0xae, 0x49, 0x19, 0xb5, 0x95, 0x01, 0xde, 0x7d,
  365. };
  366. const uint8_t expected_tag[] = {
  367. 0xf5, 0x10, 0x1f, 0x21, 0x5b, 0x07, 0x0d, 0x3f,
  368. 0xac, 0xc9, 0xd0, 0x42, 0x45, 0xef, 0xc7, 0xfa,
  369. };
  370. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_last_block, buf + CALL_SZ - 16, 16);
  371. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_tag, tag_buf, 16);
  372. free(buf);
  373. // bytes/usec = MB/sec
  374. float mb_sec = CALL_SZ / elapsed_usec;
  375. printf("GCM encryption rate %.3fMB/sec\n", mb_sec);
  376. #ifdef CONFIG_MBEDTLS_HARDWARE_GCM
  377. // Don't put a hard limit on software AES performance
  378. TEST_PERFORMANCE_GREATER_THAN(AES_GCM_UPDATE_THROUGHPUT_MBSEC, "%.3fMB/sec", mb_sec);
  379. #endif
  380. }
  381. TEST_CASE("mbedtls AES GCM performance, crypt-and-tag", "[aes-gcm]")
  382. {
  383. const unsigned CALL_SZ = 16 * 3200;
  384. mbedtls_gcm_context ctx;
  385. float elapsed_usec;
  386. unsigned char tag_buf[16] = {};
  387. mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
  388. uint8_t iv[16];
  389. uint8_t key[16];
  390. uint8_t aad[16];
  391. memset(iv, 0xEE, 16);
  392. memset(key, 0x44, 16);
  393. memset(aad, 0x76, 16);
  394. // allocate internal memory
  395. uint8_t *buf = heap_caps_malloc(CALL_SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  396. TEST_ASSERT_NOT_NULL(buf);
  397. mbedtls_gcm_init(&ctx);
  398. mbedtls_gcm_setkey( &ctx, cipher, key, 128);
  399. memset(buf, 0xAA, CALL_SZ);
  400. ccomp_timer_start();
  401. mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_AES_ENCRYPT, CALL_SZ, iv, sizeof(iv), aad, sizeof(aad), buf, buf, 16, tag_buf);
  402. elapsed_usec = ccomp_timer_stop();
  403. /* Sanity check: make sure the last ciphertext block matches
  404. what we expect to see.
  405. */
  406. const uint8_t expected_last_block[] = {
  407. 0xd4, 0x25, 0x88, 0xd4, 0x32, 0x52, 0x3d, 0x6f,
  408. 0xae, 0x49, 0x19, 0xb5, 0x95, 0x01, 0xde, 0x7d,
  409. };
  410. const uint8_t expected_tag[] = {
  411. 0xf5, 0x10, 0x1f, 0x21, 0x5b, 0x07, 0x0d, 0x3f,
  412. 0xac, 0xc9, 0xd0, 0x42, 0x45, 0xef, 0xc7, 0xfa,
  413. };
  414. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_last_block, buf + CALL_SZ - 16, 16);
  415. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_tag, tag_buf, 16);
  416. free(buf);
  417. // bytes/usec = MB/sec
  418. float mb_sec = CALL_SZ / elapsed_usec;
  419. printf("GCM encryption rate %.3fMB/sec\n", mb_sec);
  420. #ifdef CONFIG_MBEDTLS_HARDWARE_GCM
  421. // Don't put a hard limit on software AES performance
  422. TEST_PERFORMANCE_GREATER_THAN(AES_GCM_CRYPT_TAG_THROUGHPUT_MBSEC, "%.3fMB/sec", mb_sec);
  423. #endif
  424. }
  425. TEST_CASE("mbedtls AES GCM - Combine different IV/Key/Plaintext/AAD lengths", "[aes-gcm]")
  426. {
  427. #define IV_BYTES_VALUE 0xA2
  428. #define KEY_BYTES_VALUE 0x48
  429. #define INPUT_BYTES_VALUE 0x36
  430. #define ADD_BYTES_VALUE 0x12
  431. uint8_t iv[16];
  432. uint8_t key[32];
  433. memset(iv, IV_BYTES_VALUE, sizeof(iv));
  434. memset(key, KEY_BYTES_VALUE, sizeof(key));
  435. /* Key length is: 16 bytes, 32 bytes */
  436. size_t key_length[] = {16, 32};
  437. /* IV length is: 12 bytes (standard), 16 bytes */
  438. size_t iv_length[] = {12, 16};
  439. /* Plaintext length is: a multiple of 16 bytes, a non-multiple of 16 bytes */
  440. size_t length[] = {160, 321};
  441. /* Add len is: 0, a multiple of 16 bytes, a non-multiple of 16 bytes */
  442. size_t add_len[] = {0, 160, 321};
  443. /*indexes: Key - IV - Plaintext */
  444. const uint8_t expected_last_block[2][2][2][16] = {
  445. {
  446. /* 16 byte key */
  447. {
  448. {
  449. 0xa2, 0x1e, 0x23, 0x3c, 0xfc, 0x7c, 0xec, 0x9a,
  450. 0x91, 0xe5, 0xdb, 0x3a, 0xe5, 0x0c, 0x3f, 0xc2,
  451. },
  452. {
  453. 0xa8, 0xeb, 0x40, 0x9b, 0x7b, 0x87, 0x07,
  454. 0x68, 0x17, 0x5c, 0xc0, 0xb7, 0xb4, 0xb3, 0x81,
  455. 0xbe,
  456. }
  457. },
  458. {
  459. {
  460. 0x9c, 0xe8, 0xfc, 0x3e, 0x98, 0x64, 0x70, 0x5c,
  461. 0x98, 0x0c, 0xbb, 0x88, 0xa6, 0x4c, 0x12, 0xbc
  462. },
  463. {
  464. 0x8b, 0x66, 0xf5, 0xbc, 0x56, 0x59, 0xae,
  465. 0xf0, 0x9e, 0x5c, 0xdb, 0x6d, 0xfc, 0x1f, 0x2e,
  466. 0x00
  467. }
  468. },
  469. },
  470. {
  471. /* 32 byte key */
  472. {
  473. {
  474. 0xde, 0xc2, 0xd3, 0xeb, 0x5e, 0x03, 0x53, 0x4b,
  475. 0x04, 0x0d, 0x63, 0xf1, 0xd8, 0x5b, 0x1f, 0x85,
  476. },
  477. {
  478. 0xb5, 0x53, 0x8e, 0xd3, 0xab, 0x10, 0xf1,
  479. 0x77, 0x41, 0x92, 0xea, 0xdd, 0xdd, 0x9e, 0x5d,
  480. 0x40,
  481. }
  482. },
  483. {
  484. {
  485. 0x3b, 0xc7, 0xf0, 0x3f, 0xba, 0x97, 0xbd, 0xa0,
  486. 0xa5, 0x48, 0xf3, 0x7a, 0xde, 0x23, 0x19, 0x7a,
  487. },
  488. {
  489. 0x57, 0xc7, 0x4d, 0xe3, 0x79, 0x5e, 0xbd,
  490. 0x0d, 0xd7, 0x6a, 0xef, 0x1f, 0x54, 0x29, 0xa6,
  491. 0xd7,
  492. }
  493. },
  494. },
  495. };
  496. /*indexes: Key - IV - Plaintext - Add len*/
  497. const uint8_t expected_tag[2][2][2][3][16] = {
  498. {
  499. {
  500. {
  501. // Plaintext 160 bytes
  502. {
  503. 0x67, 0x92, 0xb1, 0x7f, 0x44, 0x1f, 0x95, 0xfb,
  504. 0x33, 0x76, 0x66, 0xb7, 0x4f, 0x3e, 0xec, 0x4d,
  505. },
  506. {
  507. 0xb1, 0x99, 0xed, 0x1b, 0x4e, 0x12, 0x87, 0x5e,
  508. 0xf4, 0xe3, 0x81, 0xd8, 0x96, 0x07, 0xda, 0xff,
  509. },
  510. {
  511. 0x73, 0x35, 0x0c, 0xf5, 0x70, 0x1e, 0xc0, 0x99,
  512. 0x34, 0xba, 0x1a, 0x50, 0x23, 0xac, 0x21, 0x33,
  513. },
  514. },
  515. {
  516. // Plaintext 321 bytes
  517. {
  518. 0x2d, 0xf6, 0xd0, 0x7a, 0x75, 0x4d, 0x9d,
  519. 0xb5, 0x9d, 0x43, 0xbf, 0x57, 0x10, 0xa3, 0xff,
  520. 0x3d
  521. },
  522. {
  523. 0x06, 0x91, 0xe4, 0x38, 0x3a, 0xe1, 0x6e,
  524. 0x2d, 0x83, 0x68, 0x2e, 0xb0, 0x26, 0x2f, 0xe4,
  525. 0x78
  526. },
  527. {
  528. 0x1b, 0x58, 0x2f, 0x9b, 0xe9, 0xe0, 0xe0,
  529. 0x43, 0x83, 0x08, 0xec, 0x58, 0x3a, 0x78, 0xe9,
  530. 0x69,
  531. }
  532. }
  533. },
  534. {
  535. {
  536. // Plaintext 160 bytes
  537. {
  538. 0x77, 0xe5, 0x2e, 0x2d, 0x94, 0xb8, 0x03, 0x61,
  539. 0x7a, 0xd5, 0x0c, 0x3c, 0x9c, 0x40, 0x92, 0x9b
  540. },
  541. {
  542. 0xa1, 0xee, 0x72, 0x49, 0x9e, 0xb5, 0x11, 0xc4,
  543. 0xbd, 0x40, 0xeb, 0x53, 0x45, 0x79, 0xa4, 0x29
  544. },
  545. {
  546. 0x63, 0x42, 0x93, 0xa7, 0xa0, 0xb9, 0x56, 0x03,
  547. 0x7d, 0x19, 0x70, 0xdb, 0xf0, 0xd2, 0x5f, 0xe5
  548. },
  549. },
  550. {
  551. // Plaintext 321 bytes
  552. {
  553. 0x50, 0xa3, 0x79, 0xfc, 0x17, 0xb8, 0xf4,
  554. 0xf6, 0x14, 0xaa, 0x4a, 0xe7, 0xd4, 0xa0, 0xea,
  555. 0xee
  556. },
  557. {
  558. 0x7b, 0xc4, 0x4d, 0xbe, 0x58, 0x14, 0x07,
  559. 0x6e, 0x0a, 0x81, 0xdb, 0x00, 0xe2, 0x2c, 0xf1,
  560. 0xab
  561. },
  562. {
  563. 0x66, 0x0d, 0x86, 0x1d, 0x8b, 0x15, 0x89,
  564. 0x00, 0x0a, 0xe1, 0x19, 0xe8, 0xfe, 0x7b, 0xfc,
  565. 0xba
  566. }
  567. }
  568. },
  569. },
  570. {
  571. {
  572. {
  573. // Plaintext 160 bytes
  574. {
  575. 0x04, 0x04, 0x15, 0xb1, 0xd3, 0x98, 0x15, 0x45,
  576. 0xa2, 0x44, 0xba, 0x4a, 0xde, 0xc2, 0x8d, 0xd6,
  577. },
  578. {
  579. 0x94, 0x3e, 0xc3, 0x5d, 0xdc, 0x42, 0xf6, 0x4c,
  580. 0x80, 0x15, 0xe4, 0xb9, 0x0b, 0xc9, 0x87, 0x01,
  581. },
  582. {
  583. 0x93, 0x6e, 0x26, 0x5b, 0x7e, 0x17, 0xc8, 0x73,
  584. 0x9b, 0x71, 0x31, 0x7a, 0x8b, 0x0e, 0x19, 0x89,
  585. }
  586. },
  587. {
  588. // Plaintext 321 bytes
  589. {
  590. 0x99, 0x5e, 0x77, 0x28, 0x8b, 0xa8, 0x9b,
  591. 0xb3, 0x35, 0xc3, 0x99, 0x90, 0xd4, 0x5d, 0x63,
  592. 0xa7,
  593. },
  594. {
  595. 0xbc, 0xc2, 0x9f, 0xe6, 0x38, 0xef, 0xf5,
  596. 0x11, 0x76, 0x09, 0x17, 0x3a, 0xd4, 0x91, 0xee,
  597. 0xfe,
  598. },
  599. {
  600. 0x9f, 0xa6, 0x23, 0x5a, 0x4d, 0x78, 0xae,
  601. 0xce, 0x10, 0x35, 0xc1, 0x0c, 0x6e, 0xc2, 0x4e,
  602. 0xe8,
  603. }
  604. }
  605. },
  606. {
  607. {
  608. // Plaintext 160 bytes
  609. {
  610. 0xfb, 0x74, 0x7e, 0x21, 0xf2, 0xe7, 0xe3, 0xf5,
  611. 0xfa, 0xc8, 0x23, 0xab, 0x54, 0x9a, 0xb9, 0xcf,
  612. },
  613. {
  614. 0x6b, 0x4e, 0xa8, 0xcd, 0xfd, 0x3d, 0x00, 0xfc,
  615. 0xd8, 0x99, 0x7d, 0x58, 0x81, 0x91, 0xb3, 0x18,
  616. },
  617. {
  618. 0x6c, 0x1e, 0x4d, 0xcb, 0x5f, 0x68, 0x3e, 0xc3,
  619. 0xc3, 0xfd, 0xa8, 0x9b, 0x01, 0x56, 0x2d, 0x90,
  620. },
  621. },
  622. {
  623. // Plaintext 321 bytes
  624. {
  625. 0xcd, 0x49, 0x75, 0x4c, 0x2a, 0x62, 0x65,
  626. 0x6f, 0xfe, 0x14, 0xc2, 0x5d, 0x41, 0x07, 0x24,
  627. 0x55
  628. },
  629. {
  630. 0xe8, 0xd5, 0x9d, 0x82, 0x99, 0x25, 0x0b,
  631. 0xcd, 0xbd, 0xde, 0x4c, 0xf7, 0x41, 0xcb, 0xa9,
  632. 0x0c,
  633. },
  634. {
  635. 0xcb, 0xb1, 0x21, 0x3e, 0xec, 0xb2, 0x50,
  636. 0x12, 0xdb, 0xe2, 0x9a, 0xc1, 0xfb, 0x98, 0x09,
  637. 0x1a,
  638. }
  639. }
  640. },
  641. },
  642. };
  643. aes_gcm_test_cfg_t cfg = {
  644. .output_caps = MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL,
  645. .tag_len = 16
  646. };
  647. for (int i_key = 0; i_key < sizeof(key_length) / sizeof(key_length[0]); i_key++) {
  648. printf("Test AES-GCM with key length = %d\n", key_length[i_key]);
  649. cfg.key = key;
  650. cfg.key_bits = 8 * key_length[i_key];
  651. for (int i_iv = 0; i_iv < sizeof(iv_length) / sizeof(iv_length[0]); i_iv++) {
  652. printf("Test AES-GCM with IV length = %d\n", iv_length[i_iv]);
  653. cfg.iv = iv;
  654. cfg.iv_length = iv_length[i_iv];
  655. for (int i_len = 0; i_len < sizeof(length) / sizeof(length[0]); i_len++) {
  656. printf("Test AES-GCM with plaintext length = %d\n", length[i_len]);
  657. uint8_t *input = heap_caps_malloc(length[i_len], MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  658. TEST_ASSERT(input != NULL || length[i_len] == 0);
  659. memset(input, INPUT_BYTES_VALUE, length[i_len]);
  660. cfg.plaintext = input;
  661. cfg.plaintext_length = length[i_len];
  662. aes_gcm_test_expected_res_t res = {0};
  663. for (int i_add = 0; i_add < sizeof(add_len) / sizeof(add_len[0]); i_add++) {
  664. printf("Test AES-GCM with add length = %d\n", add_len[i_add]);
  665. uint8_t *add = heap_caps_malloc(add_len[i_add], MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  666. TEST_ASSERT(add != NULL || add_len[i_add] == 0);
  667. memset(add, ADD_BYTES_VALUE, add_len[i_add]);
  668. cfg.add_buf = add;
  669. cfg.add_length = add_len[i_add];
  670. res.expected_tag = expected_tag[i_key][i_iv][i_len][i_add];
  671. res.ciphertext_last_block = expected_last_block[i_key][i_iv][i_len],
  672. aes_gcm_test(&cfg, &res, AES_GCM_TEST_CRYPT_N_TAG);
  673. free(add);
  674. }
  675. free(input);
  676. }
  677. }
  678. }
  679. }
  680. #endif //CONFIG_MBEDTLS_HARDWARE_GCM