mirror of
https://github.com/espressif/esp-idf.git
synced 2025-09-25 01:37:22 +00:00
fix(mbedtls): Fix the port for the mbedtls_internal_shaX_process API
- Also added the fix to update intermediate SHA state in the mbedtls_shaX_update API
This commit is contained in:

committed by
Mahavir Jain

parent
0c496896ba
commit
79d07f9909
@@ -7,7 +7,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.
|
||||
@@ -206,30 +206,6 @@ static const uint64_t K[80] = {
|
||||
UL64(0x5FCB6FAB3AD6FAEC), UL64(0x6C44198C4A475817)
|
||||
};
|
||||
|
||||
static void mbedtls_sha512_software_process( mbedtls_sha512_context *ctx, const unsigned char data[128] );
|
||||
|
||||
int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] )
|
||||
{
|
||||
bool first_block = false;
|
||||
|
||||
if (ctx->mode == ESP_MBEDTLS_SHA512_UNUSED) {
|
||||
/* try to use hardware for this digest */
|
||||
if (esp_sha_try_lock_engine(sha_type(ctx))) {
|
||||
ctx->mode = ESP_MBEDTLS_SHA512_HARDWARE;
|
||||
first_block = true;
|
||||
} else {
|
||||
ctx->mode = ESP_MBEDTLS_SHA512_SOFTWARE;
|
||||
}
|
||||
}
|
||||
|
||||
if (ctx->mode == ESP_MBEDTLS_SHA512_HARDWARE) {
|
||||
esp_sha_block(sha_type(ctx), data, first_block);
|
||||
} else {
|
||||
mbedtls_sha512_software_process(ctx, data);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void mbedtls_sha512_software_process( mbedtls_sha512_context *ctx, const unsigned char data[128] )
|
||||
{
|
||||
@@ -296,13 +272,47 @@ static void mbedtls_sha512_software_process( mbedtls_sha512_context *ctx, const
|
||||
ctx->state[7] += H;
|
||||
}
|
||||
|
||||
|
||||
static int esp_internal_sha512_parallel_engine_process( mbedtls_sha512_context *ctx, const unsigned char data[128], bool read_digest )
|
||||
{
|
||||
bool first_block = false;
|
||||
|
||||
if (ctx->mode == ESP_MBEDTLS_SHA512_UNUSED) {
|
||||
/* try to use hardware for this digest */
|
||||
if (esp_sha_try_lock_engine(sha_type(ctx))) {
|
||||
ctx->mode = ESP_MBEDTLS_SHA512_HARDWARE;
|
||||
first_block = true;
|
||||
} else {
|
||||
ctx->mode = ESP_MBEDTLS_SHA512_SOFTWARE;
|
||||
}
|
||||
}
|
||||
|
||||
if (ctx->mode == ESP_MBEDTLS_SHA512_HARDWARE) {
|
||||
esp_sha_block(sha_type(ctx), data, first_block);
|
||||
if (read_digest) {
|
||||
esp_sha_read_digest_state(sha_type(ctx), ctx->state);
|
||||
}
|
||||
} else {
|
||||
mbedtls_sha512_software_process(ctx, data);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] )
|
||||
{
|
||||
return esp_internal_sha512_parallel_engine_process(ctx, data, true);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* SHA-512 process buffer
|
||||
*/
|
||||
int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *input,
|
||||
size_t ilen )
|
||||
{
|
||||
int ret;
|
||||
int ret = -1;
|
||||
size_t fill;
|
||||
unsigned int left;
|
||||
|
||||
@@ -321,7 +331,7 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
|
||||
|
||||
if ( left && ilen >= fill ) {
|
||||
memcpy( (void *) (ctx->buffer + left), input, fill );
|
||||
if ( ( ret = mbedtls_internal_sha512_process( ctx, ctx->buffer ) ) != 0 ) {
|
||||
if ( ( ret = esp_internal_sha512_parallel_engine_process( ctx, ctx->buffer, false ) ) != 0 ) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -331,7 +341,7 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
|
||||
}
|
||||
|
||||
while ( ilen >= 128 ) {
|
||||
if ( ( ret = mbedtls_internal_sha512_process( ctx, input ) ) != 0 ) {
|
||||
if ( ( ret = esp_internal_sha512_parallel_engine_process( ctx, input, false ) ) != 0 ) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -339,6 +349,10 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
|
||||
ilen -= 128;
|
||||
}
|
||||
|
||||
if (ctx->mode == ESP_MBEDTLS_SHA512_HARDWARE) {
|
||||
esp_sha_read_digest_state(sha_type(ctx), ctx->state);
|
||||
}
|
||||
|
||||
if ( ilen > 0 ) {
|
||||
memcpy( (void *) (ctx->buffer + left), input, ilen );
|
||||
}
|
||||
@@ -362,7 +376,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];
|
||||
|
Reference in New Issue
Block a user