test_aes_gcm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* mbedTLS GCM test
  2. */
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6. #include <esp_system.h>
  7. #include "mbedtls/aes.h"
  8. #include "mbedtls/gcm.h"
  9. #include "unity.h"
  10. #include "sdkconfig.h"
  11. #include "esp_heap_caps.h"
  12. #include "test_utils.h"
  13. #include "ccomp_timer.h"
  14. #include "sys/param.h"
  15. #if CONFIG_MBEDTLS_HARDWARE_GCM
  16. /*
  17. Python example code for generating test vectors
  18. import os, binascii
  19. from cryptography.hazmat.primitives.ciphers.aead import AESGCM
  20. def as_c_array(byte_arr):
  21. hex_str = ''
  22. for idx, byte in enumerate(byte_arr):
  23. hex_str += "0x{:02x}, ".format(byte)
  24. bytes_per_line = 8
  25. if idx % bytes_per_line == bytes_per_line - 1:
  26. hex_str += '\n'
  27. return hex_str
  28. key = b'\x44' * 16
  29. iv = b'\xEE' * 16
  30. data = b'\xAA' * 3200
  31. aad = b'\x76' * 16
  32. aesgcm = AESGCM(key)
  33. ct = aesgcm.encrypt(iv, data, aad)
  34. print(as_c_array(ct))
  35. */
  36. TEST_CASE("mbedtls GCM stream test", "[aes-gcm]")
  37. {
  38. const unsigned SZ = 100;
  39. mbedtls_gcm_context ctx;
  40. uint8_t nonce[16];
  41. uint8_t key[16];
  42. uint8_t tag[16];
  43. mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
  44. const uint8_t expected_cipher[] = {
  45. 0x03, 0x92, 0x13, 0x49, 0x1f, 0x1f, 0x24, 0x41,
  46. 0xe8, 0xeb, 0x89, 0x47, 0x50, 0x0a, 0xce, 0xa3,
  47. 0xc7, 0x1c, 0x10, 0x70, 0xb0, 0x89, 0x82, 0x5e,
  48. 0x0f, 0x4a, 0x23, 0xee, 0xd2, 0xfc, 0xff, 0x45,
  49. 0x61, 0x4c, 0xd1, 0xfb, 0x6d, 0xe2, 0xbe, 0x67,
  50. 0x6f, 0x94, 0x72, 0xa3, 0xe7, 0x04, 0x99, 0xb3,
  51. 0x4a, 0x46, 0xf9, 0x2b, 0xaf, 0xac, 0xa9, 0x0e,
  52. 0x43, 0x7e, 0x8b, 0xc4, 0xbf, 0x49, 0xa4, 0x83,
  53. 0x9c, 0x31, 0x11, 0x1c, 0x09, 0xac, 0x90, 0xdf,
  54. 0x00, 0x34, 0x08, 0xe5, 0x70, 0xa3, 0x7e, 0x4b,
  55. 0x36, 0x48, 0x5a, 0x3f, 0x28, 0xc7, 0x1c, 0xd9,
  56. 0x1b, 0x1b, 0x49, 0x96, 0xe9, 0x7c, 0xea, 0x54,
  57. 0x7c, 0x71, 0x29, 0x0d
  58. };
  59. const uint8_t expected_tag[] = {
  60. 0x35, 0x1c, 0x21, 0xc6, 0xbc, 0x6b, 0x18, 0x52,
  61. 0x90, 0xe1, 0xf2, 0x5b, 0xe1, 0xf6, 0x15, 0xee,
  62. };
  63. memset(nonce, 0x89, 16);
  64. memset(key, 0x56, 16);
  65. // allocate internal memory
  66. uint8_t *chipertext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  67. uint8_t *plaintext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  68. uint8_t *decryptedtext = heap_caps_malloc(SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  69. TEST_ASSERT_NOT_NULL(chipertext);
  70. TEST_ASSERT_NOT_NULL(plaintext);
  71. TEST_ASSERT_NOT_NULL(decryptedtext);
  72. memset(plaintext, 0xAB, SZ);
  73. /* Test that all the end results are the same
  74. no matter how many bytes we encrypt each call
  75. */
  76. for (int bytes_to_process = 16; bytes_to_process < SZ; bytes_to_process = bytes_to_process + 16) {
  77. memset(nonce, 0x89, 16);
  78. memset(chipertext, 0x0, SZ);
  79. memset(decryptedtext, 0x0, SZ);
  80. memset(tag, 0x0, 16);
  81. mbedtls_gcm_init(&ctx);
  82. mbedtls_gcm_setkey(&ctx, cipher, key, 128);
  83. mbedtls_gcm_starts( &ctx, MBEDTLS_AES_ENCRYPT, nonce, sizeof(nonce), NULL, 0 );
  84. // Encrypt
  85. for (int idx = 0; idx < SZ; idx = idx + bytes_to_process) {
  86. // Limit length of last call to avoid exceeding buffer size
  87. size_t length = (idx + bytes_to_process > SZ) ? (SZ - idx) : bytes_to_process;
  88. mbedtls_gcm_update(&ctx, length, plaintext + idx, chipertext + idx );
  89. }
  90. mbedtls_gcm_finish( &ctx, tag, sizeof(tag) );
  91. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher, chipertext, SZ);
  92. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_tag, tag, sizeof(tag));
  93. // Decrypt
  94. memset(nonce, 0x89, 16);
  95. mbedtls_gcm_free( &ctx );
  96. mbedtls_gcm_init(&ctx);
  97. mbedtls_gcm_setkey(&ctx, cipher, key, 128);
  98. mbedtls_gcm_starts( &ctx, MBEDTLS_AES_DECRYPT, nonce, sizeof(nonce), NULL, 0 );
  99. for (int idx = 0; idx < SZ; idx = idx + bytes_to_process) {
  100. // Limit length of last call to avoid exceeding buffer size
  101. size_t length = (idx + bytes_to_process > SZ) ? (SZ - idx) : bytes_to_process;
  102. mbedtls_gcm_update(&ctx, length, chipertext + idx, decryptedtext + idx );
  103. }
  104. mbedtls_gcm_finish( &ctx, tag, sizeof(tag) );
  105. TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, SZ);
  106. mbedtls_gcm_free( &ctx );
  107. }
  108. free(plaintext);
  109. free(chipertext);
  110. free(decryptedtext);
  111. }
  112. TEST_CASE("mbedtls AES GCM self-tests", "[aes-gcm]")
  113. {
  114. TEST_ASSERT_FALSE_MESSAGE(mbedtls_gcm_self_test(1), "AES GCM self-test should pass.");
  115. }
  116. typedef struct {
  117. uint8_t *plaintext;
  118. size_t plaintext_length;
  119. uint32_t output_caps;
  120. uint8_t *add_buf;
  121. size_t add_length;
  122. uint8_t *iv;
  123. size_t iv_length;
  124. uint8_t *key;
  125. size_t key_bits;
  126. size_t tag_len;
  127. } aes_gcm_test_cfg_t;
  128. typedef struct {
  129. const uint8_t *expected_tag;
  130. const uint8_t *ciphertext_last_block; // Last block of the chipertext
  131. } aes_gcm_test_expected_res_t;
  132. typedef enum {
  133. AES_GCM_TEST_CRYPT_N_TAG,
  134. AES_GCM_TEST_START_UPDATE_FINISH,
  135. } aes_gcm_test_type_t;
  136. 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)
  137. {
  138. mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
  139. mbedtls_gcm_context ctx;
  140. uint8_t tag_buf_encrypt[16] = {};
  141. uint8_t tag_buf_decrypt[16] = {};
  142. uint8_t iv_buf[16] = {};
  143. uint8_t *ciphertext = heap_caps_malloc(cfg->plaintext_length, cfg->output_caps);
  144. uint8_t *output = heap_caps_malloc(cfg->plaintext_length, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  145. if (cfg->plaintext_length != 0) {
  146. TEST_ASSERT_NOT_NULL(ciphertext);
  147. TEST_ASSERT_NOT_NULL(output);
  148. }
  149. memset(ciphertext, 0, cfg->plaintext_length);
  150. memset(output, 0, cfg->plaintext_length);
  151. memcpy(iv_buf, cfg->iv, cfg->iv_length);
  152. mbedtls_gcm_init(&ctx);
  153. mbedtls_gcm_setkey(&ctx, cipher, cfg->key, cfg->key_bits);
  154. /* Encrypt and tag */
  155. if (aes_gcm_type == AES_GCM_TEST_CRYPT_N_TAG) {
  156. 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);
  157. } else if (aes_gcm_type == AES_GCM_TEST_START_UPDATE_FINISH){
  158. TEST_ASSERT(mbedtls_gcm_starts( &ctx, MBEDTLS_AES_ENCRYPT, iv_buf, cfg->iv_length, cfg->add_buf, cfg->add_length) == 0 );
  159. TEST_ASSERT(mbedtls_gcm_update( &ctx, cfg->plaintext_length, cfg->plaintext, ciphertext) == 0 );
  160. TEST_ASSERT(mbedtls_gcm_finish( &ctx, tag_buf_encrypt, cfg->tag_len) == 0 );
  161. }
  162. size_t offset = cfg->plaintext_length > 16 ? cfg->plaintext_length - 16 : 0;
  163. /* Sanity check: make sure the last ciphertext block matches what we expect to see. */
  164. TEST_ASSERT_EQUAL_HEX8_ARRAY(res->ciphertext_last_block, ciphertext + offset, MIN(16, cfg->plaintext_length));
  165. TEST_ASSERT_EQUAL_HEX8_ARRAY(res->expected_tag, tag_buf_encrypt, cfg->tag_len);
  166. /* Decrypt and authenticate */
  167. if (aes_gcm_type == AES_GCM_TEST_CRYPT_N_TAG) {
  168. 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);
  169. } else if (aes_gcm_type == AES_GCM_TEST_START_UPDATE_FINISH){
  170. TEST_ASSERT(mbedtls_gcm_starts( &ctx, MBEDTLS_AES_DECRYPT, iv_buf, cfg->iv_length, cfg->add_buf, cfg->add_length) == 0 );
  171. TEST_ASSERT(mbedtls_gcm_update( &ctx, cfg->plaintext_length, ciphertext, output) == 0 );
  172. TEST_ASSERT(mbedtls_gcm_finish( &ctx, tag_buf_decrypt, cfg->tag_len) == 0 );
  173. /* mbedtls_gcm_auth_decrypt already checks tag so only needed for AES_GCM_TEST_START_UPDATE_FINISH */
  174. TEST_ASSERT_EQUAL_HEX8_ARRAY(res->expected_tag, tag_buf_decrypt, cfg->tag_len);
  175. }
  176. TEST_ASSERT_EQUAL_HEX8_ARRAY(cfg->plaintext, output, cfg->plaintext_length);
  177. free(ciphertext);
  178. free(output);
  179. }
  180. TEST_CASE("mbedtls AES GCM", "[aes-gcm]")
  181. {
  182. uint8_t iv[16];
  183. uint8_t key[16];
  184. uint8_t add[30];
  185. memset(iv, 0xB1, sizeof(iv));
  186. memset(key, 0x27, sizeof(key));
  187. memset(add, 0x90, sizeof(add));
  188. size_t length[] = {10, 16, 500, 5000, 12345};
  189. const uint8_t expected_last_block[][16] = {
  190. {0x37, 0x99, 0x4b, 0x16, 0x5f, 0x8d, 0x27, 0xb1,
  191. 0x60, 0x72},
  192. {0x37, 0x99, 0x4b, 0x16, 0x5f, 0x8d, 0x27, 0xb1,
  193. 0x60, 0x72, 0x9a, 0x81, 0x8d, 0x3c, 0x69, 0x66},
  194. {0x9d, 0x7a, 0xac, 0x84,0xe3, 0x70, 0x43, 0x0f,
  195. 0xa7, 0x83, 0x43, 0xc9, 0x04, 0xf8, 0x7d, 0x48},
  196. {0xee, 0xfd, 0xab, 0x2a, 0x09, 0x44, 0x41, 0x6a,
  197. 0x91, 0xb0, 0x74, 0x24, 0xee, 0x35, 0xb1, 0x39},
  198. {0x51, 0xf7, 0x1f, 0x67, 0x1a, 0x4a, 0x12, 0x37,
  199. 0x60, 0x3b, 0x68, 0x01, 0x20, 0x4f, 0xf3, 0xd9},
  200. };
  201. const uint8_t expected_tag[][16] = {
  202. {0x06, 0x4f, 0xb5, 0x91, 0x12, 0x24, 0xb4, 0x24,
  203. 0x0b, 0xc2, 0x85, 0x59, 0x6a, 0x7c, 0x1f, 0xc9},
  204. {0x45, 0xc2, 0xa8, 0xfe, 0xff, 0x49, 0x1f, 0x45,
  205. 0x8e, 0x29, 0x74, 0x41, 0xed, 0x9b, 0x54, 0x28},
  206. {0xe1, 0xf9, 0x40, 0xfa, 0x29, 0x6f, 0x30, 0xae,
  207. 0xb6, 0x9b, 0x33, 0xdb, 0x8a, 0xf9, 0x70, 0xc4},
  208. {0x22, 0xe1, 0x22, 0x34, 0x0c, 0x91, 0x0b, 0xcf,
  209. 0xa3, 0x42, 0xe0, 0x48, 0xe6, 0xfe, 0x2e, 0x28},
  210. {0xfb, 0xfe, 0x5a, 0xed, 0x26, 0x5c, 0x5e, 0x66,
  211. 0x4e, 0xb2, 0x48, 0xce, 0xe9, 0x88, 0x1c, 0xe0},
  212. };
  213. aes_gcm_test_cfg_t cfg = {
  214. .output_caps = MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL,
  215. .iv = iv,
  216. .iv_length = sizeof(iv),
  217. .key = key,
  218. .key_bits = 8*sizeof(key),
  219. .add_buf = add,
  220. .add_length = sizeof(add),
  221. .tag_len = 16
  222. };
  223. aes_gcm_test_expected_res_t res = {
  224. };
  225. for (int i = 0; i < sizeof(length)/sizeof(length[0]); i++) {
  226. printf("Test AES-GCM with plaintext length = %d\n", length[i]);
  227. uint8_t *input = heap_caps_malloc(length[i], MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  228. TEST_ASSERT(input != NULL || length[i] == 0);
  229. memset(input, 0x36, length[i]);
  230. cfg.plaintext = input;
  231. cfg.plaintext_length = length[i];
  232. res.expected_tag = expected_tag[i];
  233. res.ciphertext_last_block = expected_last_block[i],
  234. aes_gcm_test(&cfg, &res, AES_GCM_TEST_CRYPT_N_TAG);
  235. aes_gcm_test(&cfg, &res, AES_GCM_TEST_START_UPDATE_FINISH);
  236. free(input);
  237. }
  238. }
  239. TEST_CASE("mbedtls AES GCM - Different add messages", "[aes-gcm]")
  240. {
  241. const unsigned CALL_SZ = 160;
  242. uint8_t iv[16];
  243. uint8_t key[16];
  244. uint8_t *input = heap_caps_malloc(CALL_SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  245. TEST_ASSERT_NOT_NULL(input);
  246. memset(input, 0x67, CALL_SZ);
  247. memset(iv, 0xA2, sizeof(iv));
  248. memset(key, 0x48, sizeof(key));
  249. const uint8_t expected_last_block[] = {
  250. 0xcd, 0xb9, 0xad, 0x6f, 0xc9, 0x35, 0x21, 0x0d,
  251. 0xc9, 0x5d, 0xea, 0xd9, 0xf7, 0x1d, 0x43, 0xed
  252. };
  253. size_t add_len[] = {0, 10, 16, 500, 5000};
  254. const uint8_t expected_tag[][16] = {
  255. {0xe3, 0x91, 0xad, 0x40, 0x96, 0xb7, 0x8c, 0x53,
  256. 0x4d, 0x15, 0x7d, 0x55, 0x15, 0xdf, 0x10, 0x69},
  257. {0xc2, 0x38, 0x36, 0xe9, 0x12, 0x72, 0x5b, 0x31,
  258. 0x0c, 0xde, 0xb5, 0xc9, 0x8c, 0xa3, 0xcb, 0xe7},
  259. {0x57, 0x10, 0x22, 0x91, 0x65, 0xfa, 0x89, 0xba,
  260. 0x0a, 0x3e, 0xc1, 0x7c, 0x93, 0x6e, 0x35, 0xac},
  261. {0x3c, 0x28, 0x03, 0xc2, 0x14, 0x40, 0xec, 0xb6,
  262. 0x25, 0xfb, 0xdd, 0x55, 0xa0, 0xb2, 0x47, 0x7b},
  263. {0xfa, 0x66, 0x4a, 0x97, 0x2d, 0x02, 0x32, 0x5b,
  264. 0x92, 0x94, 0xf1, 0x00, 0x1c, 0xfa, 0xe3, 0x07}
  265. };
  266. aes_gcm_test_cfg_t cfg = {
  267. .plaintext = input,
  268. .plaintext_length = CALL_SZ,
  269. .output_caps = MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL,
  270. .iv = iv,
  271. .iv_length = sizeof(iv),
  272. .key = key,
  273. .key_bits = 8*sizeof(key),
  274. .tag_len = 16
  275. };
  276. aes_gcm_test_expected_res_t res = {
  277. .ciphertext_last_block = expected_last_block,
  278. };
  279. for (int i = 0; i < sizeof(add_len)/sizeof(add_len[0]); i++) {
  280. printf("Test AES-GCM with add length = %d\n", add_len[i]);
  281. uint8_t *add = heap_caps_malloc(add_len[i], MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  282. TEST_ASSERT(add != NULL || add_len[i] == 0);
  283. memset(add, 0x12, add_len[i]);
  284. cfg.add_buf = add;
  285. cfg.add_length = add_len[i];
  286. res.expected_tag = expected_tag[i];
  287. aes_gcm_test(&cfg, &res, AES_GCM_TEST_CRYPT_N_TAG);
  288. aes_gcm_test(&cfg, &res, AES_GCM_TEST_START_UPDATE_FINISH);
  289. free(add);
  290. }
  291. free(input);
  292. }
  293. TEST_CASE("mbedtls AES GCM performance, start, update, ret", "[aes-gcm]")
  294. {
  295. const unsigned CALL_SZ = 16*3200;
  296. mbedtls_gcm_context ctx;
  297. float elapsed_usec;
  298. unsigned char tag_buf[16];
  299. mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
  300. uint8_t iv[16];
  301. uint8_t key[16];
  302. uint8_t aad[16];
  303. memset(iv, 0xEE, 16);
  304. memset(key, 0x44, 16);
  305. memset(aad, 0x76, 16);
  306. // allocate internal memory
  307. uint8_t *buf = heap_caps_malloc(CALL_SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  308. TEST_ASSERT_NOT_NULL(buf);
  309. mbedtls_gcm_init(&ctx);
  310. mbedtls_gcm_setkey( &ctx, cipher, key, 128);
  311. ccomp_timer_start();
  312. memset(buf, 0xAA, CALL_SZ);
  313. TEST_ASSERT(mbedtls_gcm_starts( &ctx, MBEDTLS_AES_ENCRYPT, iv, sizeof(iv), aad, sizeof(aad) ) == 0 );
  314. TEST_ASSERT(mbedtls_gcm_update( &ctx, CALL_SZ, buf, buf ) == 0 );
  315. TEST_ASSERT(mbedtls_gcm_finish( &ctx, tag_buf, 16 ) == 0 );
  316. elapsed_usec = ccomp_timer_stop();
  317. /* Sanity check: make sure the last ciphertext block matches
  318. what we expect to see.
  319. */
  320. const uint8_t expected_last_block[] = {
  321. 0xd4, 0x25, 0x88, 0xd4, 0x32, 0x52, 0x3d, 0x6f,
  322. 0xae, 0x49, 0x19, 0xb5, 0x95, 0x01, 0xde, 0x7d,
  323. };
  324. const uint8_t expected_tag[] = {
  325. 0xf5, 0x10, 0x1f, 0x21, 0x5b, 0x07, 0x0d, 0x3f,
  326. 0xac, 0xc9, 0xd0, 0x42, 0x45, 0xef, 0xc7, 0xfa,
  327. };
  328. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_last_block, buf + CALL_SZ - 16 , 16);
  329. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_tag, tag_buf, 16);
  330. free(buf);
  331. // bytes/usec = MB/sec
  332. float mb_sec = CALL_SZ / elapsed_usec;
  333. printf("GCM encryption rate %.3fMB/sec\n", mb_sec);
  334. #ifdef CONFIG_MBEDTLS_HARDWARE_GCM
  335. // Don't put a hard limit on software AES performance
  336. TEST_PERFORMANCE_GREATER_THAN(AES_GCM_UPDATE_THROUGHPUT_MBSEC, "%.3fMB/sec", mb_sec);
  337. #endif
  338. }
  339. TEST_CASE("mbedtls AES GCM performance, crypt-and-tag", "[aes-gcm]")
  340. {
  341. const unsigned CALL_SZ = 16*3200;
  342. mbedtls_gcm_context ctx;
  343. float elapsed_usec;
  344. unsigned char tag_buf[16] = {};
  345. mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
  346. uint8_t iv[16];
  347. uint8_t key[16];
  348. uint8_t aad[16];
  349. memset(iv, 0xEE, 16);
  350. memset(key, 0x44, 16);
  351. memset(aad, 0x76, 16);
  352. // allocate internal memory
  353. uint8_t *buf = heap_caps_malloc(CALL_SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  354. TEST_ASSERT_NOT_NULL(buf);
  355. mbedtls_gcm_init(&ctx);
  356. mbedtls_gcm_setkey( &ctx, cipher, key, 128);
  357. memset(buf, 0xAA, CALL_SZ);
  358. ccomp_timer_start();
  359. mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_AES_ENCRYPT, CALL_SZ, iv, sizeof(iv), aad, sizeof(aad), buf, buf, 16, tag_buf);
  360. elapsed_usec = ccomp_timer_stop();
  361. /* Sanity check: make sure the last ciphertext block matches
  362. what we expect to see.
  363. */
  364. const uint8_t expected_last_block[] = {
  365. 0xd4, 0x25, 0x88, 0xd4, 0x32, 0x52, 0x3d, 0x6f,
  366. 0xae, 0x49, 0x19, 0xb5, 0x95, 0x01, 0xde, 0x7d,
  367. };
  368. const uint8_t expected_tag[] = {
  369. 0xf5, 0x10, 0x1f, 0x21, 0x5b, 0x07, 0x0d, 0x3f,
  370. 0xac, 0xc9, 0xd0, 0x42, 0x45, 0xef, 0xc7, 0xfa,
  371. };
  372. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_last_block, buf + CALL_SZ - 16 , 16);
  373. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_tag, tag_buf, 16);
  374. free(buf);
  375. // bytes/usec = MB/sec
  376. float mb_sec = CALL_SZ / elapsed_usec;
  377. printf("GCM encryption rate %.3fMB/sec\n", mb_sec);
  378. #ifdef CONFIG_MBEDTLS_HARDWARE_GCM
  379. // Don't put a hard limit on software AES performance
  380. TEST_PERFORMANCE_GREATER_THAN(AES_GCM_CRYPT_TAG_THROUGHPUT_MBSEC, "%.3fMB/sec", mb_sec);
  381. #endif
  382. }
  383. #endif //CONFIG_MBEDTLS_HARDWARE_GCM