Skip to content

Commit

Permalink
refactor(mbedtls): refactored the return values check in some esp-aes…
Browse files Browse the repository at this point in the history
… APIs
  • Loading branch information
Harshal5 committed Jul 14, 2023
1 parent 895d866 commit 34dccf8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
6 changes: 3 additions & 3 deletions components/mbedtls/port/aes/block/esp_aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ int esp_internal_aes_encrypt(esp_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] )
{
int r;
int r = -1;

if (esp_aes_validate_input(ctx, input, output)) {
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
Expand Down Expand Up @@ -186,7 +186,7 @@ int esp_internal_aes_decrypt(esp_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] )
{
int r;
int r = -1;

if (esp_aes_validate_input(ctx, input, output)) {
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
Expand All @@ -212,7 +212,7 @@ int esp_aes_crypt_ecb(esp_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] )
{
int r;
int r = -1;

if (esp_aes_validate_input(ctx, input, output)) {
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
Expand Down
41 changes: 19 additions & 22 deletions components/mbedtls/port/aes/dma/esp_aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ int esp_internal_aes_encrypt(esp_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] )
{
int r;
int r = -1;

if (esp_aes_validate_input(ctx, input, output)) {
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
Expand Down Expand Up @@ -676,7 +676,7 @@ int esp_internal_aes_decrypt(esp_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] )
{
int r;
int r = -1;

if (esp_aes_validate_input(ctx, input, output)) {
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
Expand Down Expand Up @@ -712,7 +712,7 @@ int esp_aes_crypt_ecb(esp_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] )
{
int r;
int r = -1;

if (esp_aes_validate_input(ctx, input, output)) {
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
Expand Down Expand Up @@ -742,7 +742,7 @@ int esp_aes_crypt_cbc(esp_aes_context *ctx,
const unsigned char *input,
unsigned char *output )
{
int r = 0;
int r = -1;
if (esp_aes_validate_input(ctx, input, output)) {
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
}
Expand Down Expand Up @@ -771,13 +771,13 @@ int esp_aes_crypt_cbc(esp_aes_context *ctx,

r = esp_aes_process_dma(ctx, input, output, length, NULL);
if (r != 0) {
esp_aes_release_hardware();
return r;
goto cleanup;
}

aes_hal_read_iv(iv);
esp_aes_release_hardware();

cleanup:
esp_aes_release_hardware();
return r;
}

Expand All @@ -791,9 +791,9 @@ int esp_aes_crypt_cfb8(esp_aes_context *ctx,
const unsigned char *input,
unsigned char *output )
{
int r = -1;
unsigned char c;
unsigned char ov[17];
int r = 0;
size_t block_bytes = length - (length % AES_BLOCK_BYTES);

if (esp_aes_validate_input(ctx, input, output)) {
Expand Down Expand Up @@ -823,8 +823,7 @@ int esp_aes_crypt_cfb8(esp_aes_context *ctx,
aes_hal_set_iv(iv);
r = esp_aes_process_dma(ctx, input, output, block_bytes, NULL);
if (r != 0) {
esp_aes_release_hardware();
return r;
goto cleanup;
}

aes_hal_read_iv(iv);
Expand All @@ -845,8 +844,7 @@ int esp_aes_crypt_cfb8(esp_aes_context *ctx,

r = esp_aes_process_dma(ctx, iv, iv, AES_BLOCK_BYTES, NULL);
if (r != 0) {
esp_aes_release_hardware();
return r;
goto cleanup;
}

if ( mode == MBEDTLS_AES_DECRYPT ) {
Expand All @@ -862,8 +860,10 @@ int esp_aes_crypt_cfb8(esp_aes_context *ctx,
}

}
esp_aes_release_hardware();
r = 0;

cleanup:
esp_aes_release_hardware();
return r;
}

Expand All @@ -880,7 +880,6 @@ int esp_aes_crypt_cfb128(esp_aes_context *ctx,

{
uint8_t c;
int r = 0;
size_t stream_bytes = 0;
size_t n;

Expand Down Expand Up @@ -928,7 +927,7 @@ int esp_aes_crypt_cfb128(esp_aes_context *ctx,
aes_hal_mode_init(ESP_AES_BLOCK_MODE_CFB128);
aes_hal_set_iv(iv);

r = esp_aes_process_dma(ctx, input, output, length, iv);
int r = esp_aes_process_dma(ctx, input, output, length, iv);
if (r != 0) {
esp_aes_release_hardware();
return r;
Expand All @@ -951,7 +950,7 @@ int esp_aes_crypt_cfb128(esp_aes_context *ctx,
}

*iv_off = n + stream_bytes;
return r;
return 0;
}

/*
Expand All @@ -965,7 +964,6 @@ int esp_aes_crypt_ofb(esp_aes_context *ctx,
const unsigned char *input,
unsigned char *output )
{
int r = 0;
size_t n;
size_t stream_bytes = 0;

Expand Down Expand Up @@ -1001,7 +999,7 @@ int esp_aes_crypt_ofb(esp_aes_context *ctx,
aes_hal_mode_init(ESP_AES_BLOCK_MODE_OFB);
aes_hal_set_iv(iv);

r = esp_aes_process_dma(ctx, input, output, length, iv);
int r = esp_aes_process_dma(ctx, input, output, length, iv);
if (r != 0) {
esp_aes_release_hardware();
return r;
Expand All @@ -1013,7 +1011,7 @@ int esp_aes_crypt_ofb(esp_aes_context *ctx,

*iv_off = n + stream_bytes;

return r;
return 0;
}

/*
Expand All @@ -1027,7 +1025,6 @@ int esp_aes_crypt_ctr(esp_aes_context *ctx,
const unsigned char *input,
unsigned char *output )
{
int r = 0;
size_t n;

if (esp_aes_validate_input(ctx, input, output)) {
Expand Down Expand Up @@ -1072,7 +1069,7 @@ int esp_aes_crypt_ctr(esp_aes_context *ctx,
aes_hal_mode_init(ESP_AES_BLOCK_MODE_CTR);
aes_hal_set_iv(nonce_counter);

r = esp_aes_process_dma(ctx, input, output, length, stream_block);
int r = esp_aes_process_dma(ctx, input, output, length, stream_block);

if (r != 0) {
esp_aes_release_hardware();
Expand All @@ -1086,7 +1083,7 @@ int esp_aes_crypt_ctr(esp_aes_context *ctx,
}
*nc_off = n + (length % AES_BLOCK_BYTES);

return r;
return 0;
}

static bool s_check_dma_capable(const void *p)
Expand Down

0 comments on commit 34dccf8

Please sign in to comment.