From ef3724411e24357eb4d8007492abc44d541a1cb8 Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Fri, 14 Jan 2022 10:55:06 +0800 Subject: [PATCH] AES: improve AES-GCM test coverage. --- components/mbedtls/test/test_aes_gcm.c | 411 ++++++++++++++++++++++--- 1 file changed, 370 insertions(+), 41 deletions(-) diff --git a/components/mbedtls/test/test_aes_gcm.c b/components/mbedtls/test/test_aes_gcm.c index 352b9fa7ae..9576090468 100644 --- a/components/mbedtls/test/test_aes_gcm.c +++ b/components/mbedtls/test/test_aes_gcm.c @@ -189,7 +189,7 @@ static void aes_gcm_test(aes_gcm_test_cfg_t *cfg, aes_gcm_test_expected_res_t *r /* Encrypt and tag */ if (aes_gcm_type == AES_GCM_TEST_CRYPT_N_TAG) { 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); - } else if (aes_gcm_type == AES_GCM_TEST_START_UPDATE_FINISH){ + } else if (aes_gcm_type == AES_GCM_TEST_START_UPDATE_FINISH) { TEST_ASSERT(mbedtls_gcm_starts( &ctx, MBEDTLS_AES_ENCRYPT, iv_buf, cfg->iv_length, cfg->add_buf, cfg->add_length) == 0 ); TEST_ASSERT(mbedtls_gcm_update( &ctx, cfg->plaintext_length, cfg->plaintext, ciphertext) == 0 ); TEST_ASSERT(mbedtls_gcm_finish( &ctx, tag_buf_encrypt, cfg->tag_len) == 0 ); @@ -203,7 +203,7 @@ static void aes_gcm_test(aes_gcm_test_cfg_t *cfg, aes_gcm_test_expected_res_t *r /* Decrypt and authenticate */ if (aes_gcm_type == AES_GCM_TEST_CRYPT_N_TAG) { 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); - } else if (aes_gcm_type == AES_GCM_TEST_START_UPDATE_FINISH){ + } else if (aes_gcm_type == AES_GCM_TEST_START_UPDATE_FINISH) { TEST_ASSERT(mbedtls_gcm_starts( &ctx, MBEDTLS_AES_DECRYPT, iv_buf, cfg->iv_length, cfg->add_buf, cfg->add_length) == 0 ); TEST_ASSERT(mbedtls_gcm_update( &ctx, cfg->plaintext_length, ciphertext, output) == 0 ); TEST_ASSERT(mbedtls_gcm_finish( &ctx, tag_buf_decrypt, cfg->tag_len) == 0 ); @@ -234,38 +234,58 @@ TEST_CASE("mbedtls AES GCM", "[aes-gcm]") const uint8_t expected_last_block[][16] = { - {0x37, 0x99, 0x4b, 0x16, 0x5f, 0x8d, 0x27, 0xb1, - 0x60, 0x72}, + { + 0x37, 0x99, 0x4b, 0x16, 0x5f, 0x8d, 0x27, 0xb1, + 0x60, 0x72 + }, - {0x37, 0x99, 0x4b, 0x16, 0x5f, 0x8d, 0x27, 0xb1, - 0x60, 0x72, 0x9a, 0x81, 0x8d, 0x3c, 0x69, 0x66}, + { + 0x37, 0x99, 0x4b, 0x16, 0x5f, 0x8d, 0x27, 0xb1, + 0x60, 0x72, 0x9a, 0x81, 0x8d, 0x3c, 0x69, 0x66 + }, - {0x9d, 0x7a, 0xac, 0x84,0xe3, 0x70, 0x43, 0x0f, - 0xa7, 0x83, 0x43, 0xc9, 0x04, 0xf8, 0x7d, 0x48}, + { + 0x9d, 0x7a, 0xac, 0x84, 0xe3, 0x70, 0x43, 0x0f, + 0xa7, 0x83, 0x43, 0xc9, 0x04, 0xf8, 0x7d, 0x48 + }, - {0xee, 0xfd, 0xab, 0x2a, 0x09, 0x44, 0x41, 0x6a, - 0x91, 0xb0, 0x74, 0x24, 0xee, 0x35, 0xb1, 0x39}, + { + 0xee, 0xfd, 0xab, 0x2a, 0x09, 0x44, 0x41, 0x6a, + 0x91, 0xb0, 0x74, 0x24, 0xee, 0x35, 0xb1, 0x39 + }, - {0x51, 0xf7, 0x1f, 0x67, 0x1a, 0x4a, 0x12, 0x37, - 0x60, 0x3b, 0x68, 0x01, 0x20, 0x4f, 0xf3, 0xd9}, + { + 0x51, 0xf7, 0x1f, 0x67, 0x1a, 0x4a, 0x12, 0x37, + 0x60, 0x3b, 0x68, 0x01, 0x20, 0x4f, 0xf3, 0xd9 + }, }; const uint8_t expected_tag[][16] = { - {0x06, 0x4f, 0xb5, 0x91, 0x12, 0x24, 0xb4, 0x24, - 0x0b, 0xc2, 0x85, 0x59, 0x6a, 0x7c, 0x1f, 0xc9}, + { + 0x06, 0x4f, 0xb5, 0x91, 0x12, 0x24, 0xb4, 0x24, + 0x0b, 0xc2, 0x85, 0x59, 0x6a, 0x7c, 0x1f, 0xc9 + }, - {0x45, 0xc2, 0xa8, 0xfe, 0xff, 0x49, 0x1f, 0x45, - 0x8e, 0x29, 0x74, 0x41, 0xed, 0x9b, 0x54, 0x28}, + { + 0x45, 0xc2, 0xa8, 0xfe, 0xff, 0x49, 0x1f, 0x45, + 0x8e, 0x29, 0x74, 0x41, 0xed, 0x9b, 0x54, 0x28 + }, - {0xe1, 0xf9, 0x40, 0xfa, 0x29, 0x6f, 0x30, 0xae, - 0xb6, 0x9b, 0x33, 0xdb, 0x8a, 0xf9, 0x70, 0xc4}, + { + 0xe1, 0xf9, 0x40, 0xfa, 0x29, 0x6f, 0x30, 0xae, + 0xb6, 0x9b, 0x33, 0xdb, 0x8a, 0xf9, 0x70, 0xc4 + }, - {0x22, 0xe1, 0x22, 0x34, 0x0c, 0x91, 0x0b, 0xcf, - 0xa3, 0x42, 0xe0, 0x48, 0xe6, 0xfe, 0x2e, 0x28}, + { + 0x22, 0xe1, 0x22, 0x34, 0x0c, 0x91, 0x0b, 0xcf, + 0xa3, 0x42, 0xe0, 0x48, 0xe6, 0xfe, 0x2e, 0x28 + }, - {0xfb, 0xfe, 0x5a, 0xed, 0x26, 0x5c, 0x5e, 0x66, - 0x4e, 0xb2, 0x48, 0xce, 0xe9, 0x88, 0x1c, 0xe0}, + { + 0xfb, 0xfe, 0x5a, 0xed, 0x26, 0x5c, 0x5e, 0x66, + 0x4e, 0xb2, 0x48, 0xce, 0xe9, 0x88, 0x1c, 0xe0 + }, }; aes_gcm_test_cfg_t cfg = { @@ -273,7 +293,7 @@ TEST_CASE("mbedtls AES GCM", "[aes-gcm]") .iv = iv, .iv_length = sizeof(iv), .key = key, - .key_bits = 8*sizeof(key), + .key_bits = 8 * sizeof(key), .add_buf = add, .add_length = sizeof(add), .tag_len = 16 @@ -282,7 +302,7 @@ TEST_CASE("mbedtls AES GCM", "[aes-gcm]") aes_gcm_test_expected_res_t res = { }; - for (int i = 0; i < sizeof(length)/sizeof(length[0]); i++) { + for (int i = 0; i < sizeof(length) / sizeof(length[0]); i++) { printf("Test AES-GCM with plaintext length = %d\n", length[i]); uint8_t *input = heap_caps_malloc(length[i], MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL); TEST_ASSERT(input != NULL || length[i] == 0); @@ -321,20 +341,30 @@ TEST_CASE("mbedtls AES GCM - Different add messages", "[aes-gcm]") size_t add_len[] = {0, 10, 16, 500, 5000}; const uint8_t expected_tag[][16] = { - {0xe3, 0x91, 0xad, 0x40, 0x96, 0xb7, 0x8c, 0x53, - 0x4d, 0x15, 0x7d, 0x55, 0x15, 0xdf, 0x10, 0x69}, + { + 0xe3, 0x91, 0xad, 0x40, 0x96, 0xb7, 0x8c, 0x53, + 0x4d, 0x15, 0x7d, 0x55, 0x15, 0xdf, 0x10, 0x69 + }, - {0xc2, 0x38, 0x36, 0xe9, 0x12, 0x72, 0x5b, 0x31, - 0x0c, 0xde, 0xb5, 0xc9, 0x8c, 0xa3, 0xcb, 0xe7}, + { + 0xc2, 0x38, 0x36, 0xe9, 0x12, 0x72, 0x5b, 0x31, + 0x0c, 0xde, 0xb5, 0xc9, 0x8c, 0xa3, 0xcb, 0xe7 + }, - {0x57, 0x10, 0x22, 0x91, 0x65, 0xfa, 0x89, 0xba, - 0x0a, 0x3e, 0xc1, 0x7c, 0x93, 0x6e, 0x35, 0xac}, + { + 0x57, 0x10, 0x22, 0x91, 0x65, 0xfa, 0x89, 0xba, + 0x0a, 0x3e, 0xc1, 0x7c, 0x93, 0x6e, 0x35, 0xac + }, - {0x3c, 0x28, 0x03, 0xc2, 0x14, 0x40, 0xec, 0xb6, - 0x25, 0xfb, 0xdd, 0x55, 0xa0, 0xb2, 0x47, 0x7b}, + { + 0x3c, 0x28, 0x03, 0xc2, 0x14, 0x40, 0xec, 0xb6, + 0x25, 0xfb, 0xdd, 0x55, 0xa0, 0xb2, 0x47, 0x7b + }, - {0xfa, 0x66, 0x4a, 0x97, 0x2d, 0x02, 0x32, 0x5b, - 0x92, 0x94, 0xf1, 0x00, 0x1c, 0xfa, 0xe3, 0x07} + { + 0xfa, 0x66, 0x4a, 0x97, 0x2d, 0x02, 0x32, 0x5b, + 0x92, 0x94, 0xf1, 0x00, 0x1c, 0xfa, 0xe3, 0x07 + } }; aes_gcm_test_cfg_t cfg = { @@ -344,7 +374,7 @@ TEST_CASE("mbedtls AES GCM - Different add messages", "[aes-gcm]") .iv = iv, .iv_length = sizeof(iv), .key = key, - .key_bits = 8*sizeof(key), + .key_bits = 8 * sizeof(key), .tag_len = 16 }; @@ -352,7 +382,7 @@ TEST_CASE("mbedtls AES GCM - Different add messages", "[aes-gcm]") .ciphertext_last_block = expected_last_block, }; - for (int i = 0; i < sizeof(add_len)/sizeof(add_len[0]); i++) { + for (int i = 0; i < sizeof(add_len) / sizeof(add_len[0]); i++) { printf("Test AES-GCM with add length = %d\n", add_len[i]); uint8_t *add = heap_caps_malloc(add_len[i], MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL); TEST_ASSERT(add != NULL || add_len[i] == 0); @@ -374,7 +404,7 @@ TEST_CASE("mbedtls AES GCM - Different add messages", "[aes-gcm]") TEST_CASE("mbedtls AES GCM performance, start, update, ret", "[aes-gcm]") { - const unsigned CALL_SZ = 16*3200; + const unsigned CALL_SZ = 16 * 3200; mbedtls_gcm_context ctx; float elapsed_usec; unsigned char tag_buf[16]; @@ -406,7 +436,7 @@ TEST_CASE("mbedtls AES GCM performance, start, update, ret", "[aes-gcm]") /* Sanity check: make sure the last ciphertext block matches what we expect to see. -*/ + */ const uint8_t expected_last_block[] = { 0xd4, 0x25, 0x88, 0xd4, 0x32, 0x52, 0x3d, 0x6f, 0xae, 0x49, 0x19, 0xb5, 0x95, 0x01, 0xde, 0x7d, @@ -417,7 +447,7 @@ TEST_CASE("mbedtls AES GCM performance, start, update, ret", "[aes-gcm]") 0xac, 0xc9, 0xd0, 0x42, 0x45, 0xef, 0xc7, 0xfa, }; - TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_last_block, buf + CALL_SZ - 16 , 16); + TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_last_block, buf + CALL_SZ - 16, 16); TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_tag, tag_buf, 16); free(buf); @@ -435,7 +465,7 @@ TEST_CASE("mbedtls AES GCM performance, start, update, ret", "[aes-gcm]") TEST_CASE("mbedtls AES GCM performance, crypt-and-tag", "[aes-gcm]") { - const unsigned CALL_SZ = 16*3200; + const unsigned CALL_SZ = 16 * 3200; mbedtls_gcm_context ctx; float elapsed_usec; unsigned char tag_buf[16] = {}; @@ -476,7 +506,7 @@ TEST_CASE("mbedtls AES GCM performance, crypt-and-tag", "[aes-gcm]") 0xac, 0xc9, 0xd0, 0x42, 0x45, 0xef, 0xc7, 0xfa, }; - TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_last_block, buf + CALL_SZ - 16 , 16); + TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_last_block, buf + CALL_SZ - 16, 16); TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_tag, tag_buf, 16); free(buf); @@ -491,4 +521,303 @@ TEST_CASE("mbedtls AES GCM performance, crypt-and-tag", "[aes-gcm]") #endif } +TEST_CASE("mbedtls AES GCM - Combine different IV/Key/Plaintext/AAD lengths", "[aes-gcm]") +{ + #define IV_BYTES_VALUE 0xA2 + #define KEY_BYTES_VALUE 0x48 + #define INPUT_BYTES_VALUE 0x36 + #define ADD_BYTES_VALUE 0x12 + + uint8_t iv[16]; + uint8_t key[32]; + + memset(iv, IV_BYTES_VALUE, sizeof(iv)); + memset(key, KEY_BYTES_VALUE, sizeof(key)); + + /* Key length is: 16 bytes, 32 bytes */ + size_t key_length[] = {16, 32}; + + /* IV length is: 12 bytes (standard), 16 bytes */ + size_t iv_length[] = {12, 16}; + + /* Plaintext length is: a multiple of 16 bytes, a non-multiple of 16 bytes */ + size_t length[] = {160, 321}; + + /* Add len is: 0, a multiple of 16 bytes, a non-multiple of 16 bytes */ + size_t add_len[] = {0, 160, 321}; + + /*indexes: Key - IV - Plaintext */ + const uint8_t expected_last_block[2][2][2][16] = { + { + /* 16 byte key */ + + { + { + 0xa2, 0x1e, 0x23, 0x3c, 0xfc, 0x7c, 0xec, 0x9a, + 0x91, 0xe5, 0xdb, 0x3a, 0xe5, 0x0c, 0x3f, 0xc2, + }, + + { + 0xa8, 0xeb, 0x40, 0x9b, 0x7b, 0x87, 0x07, + 0x68, 0x17, 0x5c, 0xc0, 0xb7, 0xb4, 0xb3, 0x81, + 0xbe, + } + }, + { + { + 0x9c, 0xe8, 0xfc, 0x3e, 0x98, 0x64, 0x70, 0x5c, + 0x98, 0x0c, 0xbb, 0x88, 0xa6, 0x4c, 0x12, 0xbc + }, + + { + 0x8b, 0x66, 0xf5, 0xbc, 0x56, 0x59, 0xae, + 0xf0, 0x9e, 0x5c, 0xdb, 0x6d, 0xfc, 0x1f, 0x2e, + 0x00 + } + }, + }, + { + /* 32 byte key */ + { + { + 0xde, 0xc2, 0xd3, 0xeb, 0x5e, 0x03, 0x53, 0x4b, + 0x04, 0x0d, 0x63, 0xf1, 0xd8, 0x5b, 0x1f, 0x85, + }, + + { + 0xb5, 0x53, 0x8e, 0xd3, 0xab, 0x10, 0xf1, + 0x77, 0x41, 0x92, 0xea, 0xdd, 0xdd, 0x9e, 0x5d, + 0x40, + } + }, + { + { + 0x3b, 0xc7, 0xf0, 0x3f, 0xba, 0x97, 0xbd, 0xa0, + 0xa5, 0x48, 0xf3, 0x7a, 0xde, 0x23, 0x19, 0x7a, + }, + + { + 0x57, 0xc7, 0x4d, 0xe3, 0x79, 0x5e, 0xbd, + 0x0d, 0xd7, 0x6a, 0xef, 0x1f, 0x54, 0x29, 0xa6, + 0xd7, + } + }, + }, + }; + + /*indexes: Key - IV - Plaintext - Add len*/ + const uint8_t expected_tag[2][2][2][3][16] = { + { + { + { + // Plaintext 160 bytes + { + 0x67, 0x92, 0xb1, 0x7f, 0x44, 0x1f, 0x95, 0xfb, + 0x33, 0x76, 0x66, 0xb7, 0x4f, 0x3e, 0xec, 0x4d, + }, + + { + 0xb1, 0x99, 0xed, 0x1b, 0x4e, 0x12, 0x87, 0x5e, + 0xf4, 0xe3, 0x81, 0xd8, 0x96, 0x07, 0xda, 0xff, + }, + + { + 0x73, 0x35, 0x0c, 0xf5, 0x70, 0x1e, 0xc0, 0x99, + 0x34, 0xba, 0x1a, 0x50, 0x23, 0xac, 0x21, 0x33, + }, + }, + { + // Plaintext 321 bytes + { + 0x2d, 0xf6, 0xd0, 0x7a, 0x75, 0x4d, 0x9d, + 0xb5, 0x9d, 0x43, 0xbf, 0x57, 0x10, 0xa3, 0xff, + 0x3d + }, + + { + 0x06, 0x91, 0xe4, 0x38, 0x3a, 0xe1, 0x6e, + 0x2d, 0x83, 0x68, 0x2e, 0xb0, 0x26, 0x2f, 0xe4, + 0x78 + }, + + { + 0x1b, 0x58, 0x2f, 0x9b, 0xe9, 0xe0, 0xe0, + 0x43, 0x83, 0x08, 0xec, 0x58, 0x3a, 0x78, 0xe9, + 0x69, + } + } + }, + { + { + // Plaintext 160 bytes + { + 0x77, 0xe5, 0x2e, 0x2d, 0x94, 0xb8, 0x03, 0x61, + 0x7a, 0xd5, 0x0c, 0x3c, 0x9c, 0x40, 0x92, 0x9b + }, + + { + 0xa1, 0xee, 0x72, 0x49, 0x9e, 0xb5, 0x11, 0xc4, + 0xbd, 0x40, 0xeb, 0x53, 0x45, 0x79, 0xa4, 0x29 + }, + + { + 0x63, 0x42, 0x93, 0xa7, 0xa0, 0xb9, 0x56, 0x03, + 0x7d, 0x19, 0x70, 0xdb, 0xf0, 0xd2, 0x5f, 0xe5 + }, + }, + { + // Plaintext 321 bytes + { + 0x50, 0xa3, 0x79, 0xfc, 0x17, 0xb8, 0xf4, + 0xf6, 0x14, 0xaa, 0x4a, 0xe7, 0xd4, 0xa0, 0xea, + 0xee + }, + + { + 0x7b, 0xc4, 0x4d, 0xbe, 0x58, 0x14, 0x07, + 0x6e, 0x0a, 0x81, 0xdb, 0x00, 0xe2, 0x2c, 0xf1, + 0xab + }, + + { + 0x66, 0x0d, 0x86, 0x1d, 0x8b, 0x15, 0x89, + 0x00, 0x0a, 0xe1, 0x19, 0xe8, 0xfe, 0x7b, 0xfc, + 0xba + } + } + }, + }, + { + { + { + // Plaintext 160 bytes + { + 0x04, 0x04, 0x15, 0xb1, 0xd3, 0x98, 0x15, 0x45, + 0xa2, 0x44, 0xba, 0x4a, 0xde, 0xc2, 0x8d, 0xd6, + }, + + { + 0x94, 0x3e, 0xc3, 0x5d, 0xdc, 0x42, 0xf6, 0x4c, + 0x80, 0x15, 0xe4, 0xb9, 0x0b, 0xc9, 0x87, 0x01, + }, + + { + 0x93, 0x6e, 0x26, 0x5b, 0x7e, 0x17, 0xc8, 0x73, + 0x9b, 0x71, 0x31, 0x7a, 0x8b, 0x0e, 0x19, 0x89, + } + }, + { + // Plaintext 321 bytes + { + 0x99, 0x5e, 0x77, 0x28, 0x8b, 0xa8, 0x9b, + 0xb3, 0x35, 0xc3, 0x99, 0x90, 0xd4, 0x5d, 0x63, + 0xa7, + }, + + { + 0xbc, 0xc2, 0x9f, 0xe6, 0x38, 0xef, 0xf5, + 0x11, 0x76, 0x09, 0x17, 0x3a, 0xd4, 0x91, 0xee, + 0xfe, + }, + + { + 0x9f, 0xa6, 0x23, 0x5a, 0x4d, 0x78, 0xae, + 0xce, 0x10, 0x35, 0xc1, 0x0c, 0x6e, 0xc2, 0x4e, + 0xe8, + } + } + }, + { + { + // Plaintext 160 bytes + { + 0xfb, 0x74, 0x7e, 0x21, 0xf2, 0xe7, 0xe3, 0xf5, + 0xfa, 0xc8, 0x23, 0xab, 0x54, 0x9a, 0xb9, 0xcf, + }, + + { + 0x6b, 0x4e, 0xa8, 0xcd, 0xfd, 0x3d, 0x00, 0xfc, + 0xd8, 0x99, 0x7d, 0x58, 0x81, 0x91, 0xb3, 0x18, + }, + + { + 0x6c, 0x1e, 0x4d, 0xcb, 0x5f, 0x68, 0x3e, 0xc3, + 0xc3, 0xfd, 0xa8, 0x9b, 0x01, 0x56, 0x2d, 0x90, + }, + }, + { + // Plaintext 321 bytes + { + 0xcd, 0x49, 0x75, 0x4c, 0x2a, 0x62, 0x65, + 0x6f, 0xfe, 0x14, 0xc2, 0x5d, 0x41, 0x07, 0x24, + 0x55 + }, + + { + 0xe8, 0xd5, 0x9d, 0x82, 0x99, 0x25, 0x0b, + 0xcd, 0xbd, 0xde, 0x4c, 0xf7, 0x41, 0xcb, 0xa9, + 0x0c, + }, + + { + 0xcb, 0xb1, 0x21, 0x3e, 0xec, 0xb2, 0x50, + 0x12, 0xdb, 0xe2, 0x9a, 0xc1, 0xfb, 0x98, 0x09, + 0x1a, + } + } + }, + }, + }; + + aes_gcm_test_cfg_t cfg = { + .output_caps = MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL, + .tag_len = 16 + }; + + + for (int i_key = 0; i_key < sizeof(key_length) / sizeof(key_length[0]); i_key++) { + printf("Test AES-GCM with key length = %d\n", key_length[i_key]); + + cfg.key = key; + cfg.key_bits = 8 * key_length[i_key]; + + for (int i_iv = 0; i_iv < sizeof(iv_length) / sizeof(iv_length[0]); i_iv++) { + printf("Test AES-GCM with IV length = %d\n", iv_length[i_iv]); + + cfg.iv = iv; + cfg.iv_length = iv_length[i_iv]; + + for (int i_len = 0; i_len < sizeof(length) / sizeof(length[0]); i_len++) { + printf("Test AES-GCM with plaintext length = %d\n", length[i_len]); + uint8_t *input = heap_caps_malloc(length[i_len], MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL); + TEST_ASSERT(input != NULL || length[i_len] == 0); + memset(input, INPUT_BYTES_VALUE, length[i_len]); + cfg.plaintext = input; + cfg.plaintext_length = length[i_len]; + + aes_gcm_test_expected_res_t res = {0}; + + for (int i_add = 0; i_add < sizeof(add_len) / sizeof(add_len[0]); i_add++) { + + printf("Test AES-GCM with add length = %d\n", add_len[i_add]); + uint8_t *add = heap_caps_malloc(add_len[i_add], MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL); + TEST_ASSERT(add != NULL || add_len[i_add] == 0); + memset(add, ADD_BYTES_VALUE, add_len[i_add]); + + cfg.add_buf = add; + cfg.add_length = add_len[i_add]; + + res.expected_tag = expected_tag[i_key][i_iv][i_len][i_add]; + res.ciphertext_last_block = expected_last_block[i_key][i_iv][i_len], + + aes_gcm_test(&cfg, &res, AES_GCM_TEST_CRYPT_N_TAG); + + free(add); + } + free(input); + } + } + } +} + #endif //CONFIG_MBEDTLS_HARDWARE_GCM