Skip to content

Commit

Permalink
Merge branch 'fix/mbedtls_port_sanity_checks_and_return_values_v4.4' …
Browse files Browse the repository at this point in the history
…into 'release/v4.4'

mbedtls/port: refactor sanity checks and their return values (v4.4)

See merge request espressif/esp-idf!22128
  • Loading branch information
mahavirj committed Feb 2, 2023
2 parents 11277bf + ecdd202 commit 0e4c086
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions components/mbedtls/port/aes/dma/esp_aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,11 @@ int esp_aes_crypt_ctr(esp_aes_context *ctx,
return -1;
}

if (!stream_block) {
ESP_LOGE(TAG, "No stream supplied");
return -1;
}

if (!nonce_counter) {
ESP_LOGE(TAG, "No nonce supplied");
return -1;
Expand Down
9 changes: 8 additions & 1 deletion components/mbedtls/port/aes/esp_aes_gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "esp_heap_caps.h"
#include "soc/soc_memory_layout.h"

#include "mbedtls/error.h"
#include <string.h>

#define ESP_PUT_BE64(a, val) \
Expand Down Expand Up @@ -257,6 +258,11 @@ int esp_aes_gcm_setkey( esp_gcm_context *ctx,
const unsigned char *key,
unsigned int keybits )
{
#if !SOC_AES_SUPPORT_AES_192
if (keybits == 192) {
return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
}
#endif
if (keybits != 128 && keybits != 192 && keybits != 256) {
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
}
Expand Down Expand Up @@ -471,6 +477,7 @@ int esp_aes_gcm_finish( esp_gcm_context *ctx,
{
size_t nc_off = 0;
uint8_t len_block[AES_BLOCK_BYTES] = {0};
uint8_t stream[AES_BLOCK_BYTES] = {0};

if ( tag_len > 16 || tag_len < 4 ) {
return ( MBEDTLS_ERR_GCM_BAD_INPUT );
Expand All @@ -482,7 +489,7 @@ int esp_aes_gcm_finish( esp_gcm_context *ctx,
esp_gcm_ghash(ctx, len_block, AES_BLOCK_BYTES, ctx->ghash);

/* Tag T = GCTR(J0, ) where T is truncated to tag_len */
esp_aes_crypt_ctr(&ctx->aes_ctx, tag_len, &nc_off, ctx->ori_j0, 0, ctx->ghash, tag);
esp_aes_crypt_ctr(&ctx->aes_ctx, tag_len, &nc_off, ctx->ori_j0, stream, ctx->ghash, tag);

return 0;
}
Expand Down

0 comments on commit 0e4c086

Please sign in to comment.