From c15b36b9c49fd3151080667bbf144411cc2f3d2f Mon Sep 17 00:00:00 2001 From: "harshal.patil" Date: Wed, 7 Dec 2022 11:34:43 +0530 Subject: [PATCH] mbedtls: populate mbedtls_gcm_update() output_length paramater --- components/mbedtls/port/aes/esp_aes_gcm.c | 11 +++++++++-- components/mbedtls/test_apps/main/test_aes_gcm.c | 12 ++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/components/mbedtls/port/aes/esp_aes_gcm.c b/components/mbedtls/port/aes/esp_aes_gcm.c index 652a72ffca..31e5835fe3 100644 --- a/components/mbedtls/port/aes/esp_aes_gcm.c +++ b/components/mbedtls/port/aes/esp_aes_gcm.c @@ -425,6 +425,12 @@ int esp_aes_gcm_update( esp_gcm_context *ctx, uint8_t nonce_counter[AES_BLOCK_BYTES] = {0}; uint8_t stream[AES_BLOCK_BYTES] = {0}; + if (!output_length) { + ESP_LOGE(TAG, "No output length supplied"); + return -1; + } + *output_length = input_length; + if (!ctx) { ESP_LOGE(TAG, "No GCM context supplied"); return -1; @@ -543,6 +549,7 @@ static int esp_aes_gcm_crypt_and_tag_partial_hw( esp_gcm_context *ctx, unsigned char *tag ) { int ret = 0; + size_t olen; if ( ( ret = esp_aes_gcm_starts( ctx, mode, iv, iv_len ) ) != 0 ) { return ( ret ); @@ -552,11 +559,11 @@ static int esp_aes_gcm_crypt_and_tag_partial_hw( esp_gcm_context *ctx, return ( ret ); } - if ( ( ret = esp_aes_gcm_update( ctx, input, length, output, 0, NULL ) ) != 0 ) { + if ( ( ret = esp_aes_gcm_update( ctx, input, length, output, 0, &olen ) ) != 0 ) { return ( ret ); } - if ( ( ret = esp_aes_gcm_finish( ctx, output, 0, NULL, tag, tag_len ) ) != 0 ) { + if ( ( ret = esp_aes_gcm_finish( ctx, output, 0, &olen, tag, tag_len ) ) != 0 ) { return ( ret ); } diff --git a/components/mbedtls/test_apps/main/test_aes_gcm.c b/components/mbedtls/test_apps/main/test_aes_gcm.c index 115a67cf41..3b0e5cb97b 100644 --- a/components/mbedtls/test_apps/main/test_aes_gcm.c +++ b/components/mbedtls/test_apps/main/test_aes_gcm.c @@ -105,13 +105,13 @@ TEST_CASE("mbedtls GCM stream test", "[aes-gcm]") mbedtls_gcm_starts( &ctx, MBEDTLS_AES_ENCRYPT, nonce, sizeof(nonce) ); mbedtls_gcm_update_ad( &ctx, NULL, 0 ); + size_t olen; // Encrypt for (int idx = 0; idx < SZ; idx = idx + bytes_to_process) { // Limit length of last call to avoid exceeding buffer size size_t length = (idx + bytes_to_process > SZ) ? (SZ - idx) : bytes_to_process; - mbedtls_gcm_update(&ctx, plaintext + idx, length, ciphertext + idx, 0, NULL); + mbedtls_gcm_update(&ctx, plaintext + idx, length, ciphertext + idx, 0, &olen); } - size_t olen; mbedtls_gcm_finish( &ctx, NULL, 0, &olen, tag, sizeof(tag) ); TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher, ciphertext, SZ); TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_tag, tag, sizeof(tag)); @@ -129,7 +129,7 @@ TEST_CASE("mbedtls GCM stream test", "[aes-gcm]") // Limit length of last call to avoid exceeding buffer size size_t length = (idx + bytes_to_process > SZ) ? (SZ - idx) : bytes_to_process; - mbedtls_gcm_update(&ctx, ciphertext + idx, length, decryptedtext + idx, 0, NULL); + mbedtls_gcm_update(&ctx, ciphertext + idx, length, decryptedtext + idx, 0, &olen); } mbedtls_gcm_finish( &ctx, NULL, 0, &olen, tag, sizeof(tag) ); TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, SZ); @@ -199,7 +199,7 @@ static void aes_gcm_test(aes_gcm_test_cfg_t *cfg, aes_gcm_test_expected_res_t *r } 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) == 0 ); TEST_ASSERT(mbedtls_gcm_update_ad( &ctx, cfg->add_buf, cfg->add_length) == 0 ); - TEST_ASSERT(mbedtls_gcm_update( &ctx, cfg->plaintext, cfg->plaintext_length, ciphertext, 0, NULL) == 0 ); + TEST_ASSERT(mbedtls_gcm_update( &ctx, cfg->plaintext, cfg->plaintext_length, ciphertext, 0, &olen) == 0 ); TEST_ASSERT(mbedtls_gcm_finish( &ctx, NULL, 0, &olen, tag_buf_encrypt, cfg->tag_len) == 0 ); } size_t offset = cfg->plaintext_length > 16 ? cfg->plaintext_length - 16 : 0; @@ -214,7 +214,7 @@ static void aes_gcm_test(aes_gcm_test_cfg_t *cfg, aes_gcm_test_expected_res_t *r } 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) == 0 ); TEST_ASSERT(mbedtls_gcm_update_ad( &ctx, cfg->add_buf, cfg->add_length) == 0 ); - TEST_ASSERT(mbedtls_gcm_update( &ctx, ciphertext, cfg->plaintext_length, output, 0, NULL) == 0 ); + TEST_ASSERT(mbedtls_gcm_update( &ctx, ciphertext, cfg->plaintext_length, output, 0, &olen) == 0 ); TEST_ASSERT(mbedtls_gcm_finish( &ctx, NULL, 0, &olen, tag_buf_decrypt, cfg->tag_len) == 0 ); /* mbedtls_gcm_auth_decrypt already checks tag so only needed for AES_GCM_TEST_START_UPDATE_FINISH */ @@ -439,7 +439,7 @@ TEST_CASE("mbedtls AES GCM performance, start, update, ret", "[aes-gcm]") TEST_ASSERT(mbedtls_gcm_starts( &ctx, MBEDTLS_AES_ENCRYPT, iv, sizeof(iv) ) == 0 ); TEST_ASSERT(mbedtls_gcm_update_ad( &ctx, aad, sizeof(aad)) == 0 ); - TEST_ASSERT(mbedtls_gcm_update( &ctx, buf, CALL_SZ, buf, 0, NULL) == 0 ); + TEST_ASSERT(mbedtls_gcm_update( &ctx, buf, CALL_SZ, buf, 0, &olen) == 0 ); TEST_ASSERT(mbedtls_gcm_finish( &ctx, NULL, 0, &olen, tag_buf, 16 ) == 0 ); elapsed_usec = ccomp_timer_stop();