Skip to content

Commit

Permalink
Merge branch 'fix/mbedtls_internal_shaX_process_api_port_v5.1' into '…
Browse files Browse the repository at this point in the history
…release/v5.1'

fix(mbedtls): Fix the port for mbedtls_internal_shaX_process api (v5.1)

See merge request espressif/esp-idf!24809
  • Loading branch information
mahavirj committed Jul 14, 2023
2 parents f447ee3 + 8692da5 commit 77f6f72
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 173 deletions.
24 changes: 16 additions & 8 deletions components/mbedtls/port/sha/block/esp_sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*
* SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
*/
/*
* The SHA-1 standard was published by NIST in 1993.
Expand Down Expand Up @@ -90,6 +90,16 @@ int mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
return 0;
}

static void esp_internal_sha_update_state(mbedtls_sha1_context *ctx)
{
if (ctx->sha_state == ESP_SHA1_STATE_INIT) {
ctx->first_block = true;
ctx->sha_state = ESP_SHA1_STATE_IN_PROCESS;
} else if (ctx->sha_state == ESP_SHA1_STATE_IN_PROCESS) {
ctx->first_block = false;
esp_sha_write_digest_state(ctx->mode, ctx->state);
}
}

static void esp_internal_sha1_block_process(mbedtls_sha1_context *ctx, const uint8_t *data)
{
Expand All @@ -103,7 +113,9 @@ static void esp_internal_sha1_block_process(mbedtls_sha1_context *ctx, const uin
int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
{
esp_sha_acquire_hardware();
esp_internal_sha_update_state(ctx);
esp_sha_block(ctx->mode, data, ctx->first_block);
esp_sha_read_digest_state(ctx->mode, ctx->state);
esp_sha_release_hardware();
return 0;
}
Expand Down Expand Up @@ -138,12 +150,8 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
if ( (ilen >= 64) || local_len) {

esp_sha_acquire_hardware();
if (ctx->sha_state == ESP_SHA1_STATE_INIT) {
ctx->first_block = true;
ctx->sha_state = ESP_SHA1_STATE_IN_PROCESS;
} else if (ctx->sha_state == ESP_SHA1_STATE_IN_PROCESS) {
esp_sha_write_digest_state(SHA1, ctx->state);
}

esp_internal_sha_update_state(ctx);

/* First process buffered block, if any */
if ( local_len ) {
Expand Down Expand Up @@ -181,7 +189,7 @@ static const unsigned char sha1_padding[64] = {
*/
int mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
{
int ret;
int ret = -1;
uint32_t last, padn;
uint32_t high, low;
unsigned char msglen[8];
Expand Down
24 changes: 16 additions & 8 deletions components/mbedtls/port/sha/block/esp_sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*
* SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
*/
/*
* The SHA-256 Secure Hash Standard was published by NIST in 2002.
Expand Down Expand Up @@ -103,6 +103,17 @@ int mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
return 0;
}

static void esp_internal_sha_update_state(mbedtls_sha256_context *ctx)
{
if (ctx->sha_state == ESP_SHA256_STATE_INIT) {
ctx->first_block = true;
ctx->sha_state = ESP_SHA256_STATE_IN_PROCESS;
} else if (ctx->sha_state == ESP_SHA256_STATE_IN_PROCESS) {
ctx->first_block = false;
esp_sha_write_digest_state(ctx->mode, ctx->state);
}
}

static void esp_internal_sha256_block_process(mbedtls_sha256_context *ctx, const uint8_t *data)
{
esp_sha_block(ctx->mode, data, ctx->first_block);
Expand All @@ -115,7 +126,9 @@ static void esp_internal_sha256_block_process(mbedtls_sha256_context *ctx, const
int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
{
esp_sha_acquire_hardware();
esp_internal_sha_update_state(ctx);
esp_sha_block(ctx->mode, data, ctx->first_block);
esp_sha_read_digest_state(ctx->mode, ctx->state);
esp_sha_release_hardware();
return 0;
}
Expand Down Expand Up @@ -156,13 +169,8 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
if ( (ilen >= 64) || local_len) {

esp_sha_acquire_hardware();
if (ctx->sha_state == ESP_SHA256_STATE_INIT) {
ctx->first_block = true;

ctx->sha_state = ESP_SHA256_STATE_IN_PROCESS;
} else if (ctx->sha_state == ESP_SHA256_STATE_IN_PROCESS) {
esp_sha_write_digest_state(ctx->mode, ctx->state);
}
esp_internal_sha_update_state(ctx);

/* First process buffered block, if any */
if ( local_len ) {
Expand Down Expand Up @@ -200,7 +208,7 @@ static const unsigned char sha256_padding[64] = {
*/
int mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char *output )
{
int ret;
int ret = -1;
uint32_t last, padn;
uint32_t high, low;
unsigned char msglen[8];
Expand Down
56 changes: 38 additions & 18 deletions components/mbedtls/port/sha/block/esp_sha512.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*
* SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
*/
/*
* The SHA-512 Secure Hash Standard was published by NIST in 2002.
Expand Down Expand Up @@ -125,9 +125,28 @@ int mbedtls_sha512_starts( mbedtls_sha512_context *ctx, int is384 )
return 0;
}

static int esp_internal_sha512_block_process(mbedtls_sha512_context *ctx,
const uint8_t *data, size_t len,
uint8_t *buf, size_t buf_len)
static int esp_internal_sha_update_state(mbedtls_sha512_context *ctx)
{
if (ctx->sha_state == ESP_SHA512_STATE_INIT) {
if (ctx->mode == SHA2_512T) {
int ret = -1;
if ((ret = esp_sha_512_t_init_hash(ctx->t_val)) != 0) {
return ret;
}
ctx->first_block = false;
} else {
ctx->first_block = true;
}
ctx->sha_state = ESP_SHA512_STATE_IN_PROCESS;

} else if (ctx->sha_state == ESP_SHA512_STATE_IN_PROCESS) {
ctx->first_block = false;
esp_sha_write_digest_state(ctx->mode, ctx->state);
}
return 0;
}

static void esp_internal_sha512_block_process(mbedtls_sha512_context *ctx, const uint8_t *data)
{
esp_sha_block(ctx->mode, data, ctx->first_block);

Expand All @@ -138,10 +157,19 @@ static int esp_internal_sha512_block_process(mbedtls_sha512_context *ctx,

int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] )
{
int ret = -1;
esp_sha_acquire_hardware();

ret = esp_internal_sha_update_state(ctx);
if (ret != 0) {
esp_sha_release_hardware();
return ret;
}

esp_sha_block(ctx->mode, data, ctx->first_block);
esp_sha_read_digest_state(ctx->mode, ctx->state);
esp_sha_release_hardware();
return 0;
return ret;
}

/*
Expand Down Expand Up @@ -181,18 +209,11 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp

esp_sha_acquire_hardware();

if (ctx->sha_state == ESP_SHA512_STATE_INIT) {

if (ctx->mode == SHA2_512T) {
esp_sha_512_t_init_hash(ctx->t_val);
ctx->first_block = false;
} else {
ctx->first_block = true;
}
ctx->sha_state = ESP_SHA512_STATE_IN_PROCESS;
int ret = esp_internal_sha_update_state(ctx);

} else if (ctx->sha_state == ESP_SHA512_STATE_IN_PROCESS) {
esp_sha_write_digest_state(ctx->mode, ctx->state);
if (ret != 0) {
esp_sha_release_hardware();
return ret;
}

/* First process buffered block, if any */
Expand All @@ -209,7 +230,6 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
esp_sha_read_digest_state(ctx->mode, ctx->state);

esp_sha_release_hardware();

}

if ( ilen > 0 ) {
Expand All @@ -235,7 +255,7 @@ static const unsigned char sha512_padding[128] = {
*/
int mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char *output )
{
int ret;
int ret = -1;
size_t last, padn;
uint64_t high, low;
unsigned char msglen[16];
Expand Down
43 changes: 27 additions & 16 deletions components/mbedtls/port/sha/dma/esp_sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*
* SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
*/
/*
* The SHA-1 standard was published by NIST in 1993.
Expand Down Expand Up @@ -88,6 +88,16 @@ int mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
return 0;
}

static void esp_internal_sha_update_state(mbedtls_sha1_context *ctx)
{
if (ctx->sha_state == ESP_SHA1_STATE_INIT) {
ctx->first_block = true;
ctx->sha_state = ESP_SHA1_STATE_IN_PROCESS;
} else if (ctx->sha_state == ESP_SHA1_STATE_IN_PROCESS) {
ctx->first_block = false;
esp_sha_write_digest_state(ctx->mode, ctx->state);
}
}

static int esp_internal_sha1_dma_process(mbedtls_sha1_context *ctx,
const uint8_t *data, size_t len,
Expand All @@ -98,16 +108,23 @@ static int esp_internal_sha1_dma_process(mbedtls_sha1_context *ctx,

int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
{
int ret;
int ret = -1;
esp_sha_acquire_hardware();
esp_internal_sha_update_state(ctx);

ret = esp_sha_dma(ctx->mode, data, 64, 0, 0, ctx->first_block);
if (ret != 0) {
esp_sha_release_hardware();
return ret;
}

esp_sha_read_digest_state(ctx->mode, ctx->state);
esp_sha_release_hardware();
return ret;
}

int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
{
int ret;
size_t fill;
uint32_t left, len, local_len = 0;

Expand Down Expand Up @@ -138,25 +155,19 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
if ( len || local_len) {

esp_sha_acquire_hardware();
if (ctx->sha_state == ESP_SHA1_STATE_INIT) {
ctx->first_block = true;

ctx->sha_state = ESP_SHA1_STATE_IN_PROCESS;
} else if (ctx->sha_state == ESP_SHA1_STATE_IN_PROCESS) {
ctx->first_block = false;
esp_sha_write_digest_state(SHA1, ctx->state);
}
esp_internal_sha_update_state(ctx);

ret = esp_internal_sha1_dma_process(ctx, input, len, ctx->buffer, local_len);
int ret = esp_internal_sha1_dma_process(ctx, input, len, ctx->buffer, local_len);
if (ret != 0) {
esp_sha_release_hardware();
return ret;
}

esp_sha_read_digest_state(SHA1, ctx->state);

esp_sha_release_hardware();

if (ret != 0) {
return ret;
}

}

if ( ilen > 0 ) {
Expand All @@ -178,7 +189,7 @@ static const unsigned char sha1_padding[64] = {
*/
int mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
{
int ret;
int ret = -1;
uint32_t last, padn;
uint32_t high, low;
unsigned char msglen[8];
Expand Down
Loading

0 comments on commit 77f6f72

Please sign in to comment.