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:
harshal.patil
2023-07-04 14:33:40 +05:30
committed by Shreyas Sheth
parent ca78fb58bc
commit edef8d5fae
6 changed files with 224 additions and 142 deletions

View File

@ -110,6 +110,17 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
} }
#endif #endif
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, static int esp_internal_sha1_dma_process(mbedtls_sha1_context *ctx,
const uint8_t *data, size_t len, const uint8_t *data, size_t len,
uint8_t *buf, size_t buf_len) uint8_t *buf, size_t buf_len)
@ -119,9 +130,17 @@ 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 mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
{ {
int ret; int ret = -1;
esp_sha_acquire_hardware(); esp_sha_acquire_hardware();
esp_internal_sha_update_state(ctx);
ret = esp_sha_dma(ctx->mode, data, 64, 0, 0, ctx->first_block); 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(); esp_sha_release_hardware();
return ret; return ret;
} }
@ -136,7 +155,6 @@ void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ) int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
{ {
int ret;
size_t fill; size_t fill;
uint32_t left, len, local_len = 0; uint32_t left, len, local_len = 0;
@ -167,25 +185,19 @@ int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, const unsigned char *inp
if ( len || local_len) { if ( len || local_len) {
esp_sha_acquire_hardware(); esp_sha_acquire_hardware();
if (ctx->sha_state == ESP_SHA1_STATE_INIT) {
ctx->first_block = true;
ctx->sha_state = ESP_SHA1_STATE_IN_PROCESS; esp_internal_sha_update_state(ctx);
} else if (ctx->sha_state == ESP_SHA1_STATE_IN_PROCESS) {
ctx->first_block = false; int ret = esp_internal_sha1_dma_process(ctx, input, len, ctx->buffer, local_len);
esp_sha_write_digest_state(SHA1, ctx->state); if (ret != 0) {
esp_sha_release_hardware();
return ret;
} }
ret = esp_internal_sha1_dma_process(ctx, input, len, ctx->buffer, local_len);
esp_sha_read_digest_state(SHA1, ctx->state); esp_sha_read_digest_state(SHA1, ctx->state);
esp_sha_release_hardware(); esp_sha_release_hardware();
if (ret != 0) {
return ret;
}
} }
if ( ilen > 0 ) { if ( ilen > 0 ) {
@ -216,7 +228,7 @@ static const unsigned char sha1_padding[64] = {
*/ */
int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx, unsigned char output[20] ) int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx, unsigned char output[20] )
{ {
int ret; int ret = -1;
uint32_t last, padn; uint32_t last, padn;
uint32_t high, low; uint32_t high, low;
unsigned char msglen[8]; unsigned char msglen[8];

View File

@ -125,11 +125,30 @@ void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
#endif #endif
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);
}
}
int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] ) int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
{ {
int ret; int ret = -1;
esp_sha_acquire_hardware(); esp_sha_acquire_hardware();
esp_internal_sha_update_state(ctx);
ret = esp_sha_dma(ctx->mode, data, 64, 0, 0, ctx->first_block); 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(); esp_sha_release_hardware();
return ret; return ret;
@ -149,7 +168,6 @@ void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char *input, int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char *input,
size_t ilen ) size_t ilen )
{ {
int ret = 0;
size_t fill; size_t fill;
uint32_t left, len, local_len = 0; uint32_t left, len, local_len = 0;
@ -181,24 +199,18 @@ int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char
if ( len || local_len) { if ( len || local_len) {
esp_sha_acquire_hardware(); esp_sha_acquire_hardware();
esp_internal_sha_update_state(ctx);
if (ctx->sha_state == ESP_SHA256_STATE_INIT) { int ret = esp_sha_dma(ctx->mode, input, len, ctx->buffer, local_len, ctx->first_block);
ctx->first_block = true;
ctx->sha_state = ESP_SHA256_STATE_IN_PROCESS; if (ret != 0) {
} else if (ctx->sha_state == ESP_SHA256_STATE_IN_PROCESS) { esp_sha_release_hardware();
ctx->first_block = false; return ret;
esp_sha_write_digest_state(ctx->mode, ctx->state);
} }
ret = esp_sha_dma(ctx->mode, input, len, ctx->buffer, local_len, ctx->first_block);
esp_sha_read_digest_state(ctx->mode, ctx->state); esp_sha_read_digest_state(ctx->mode, ctx->state);
esp_sha_release_hardware(); esp_sha_release_hardware();
if (ret != 0) {
return ret;
}
} }
if ( ilen > 0 ) { if ( ilen > 0 ) {
@ -229,7 +241,7 @@ static const unsigned char sha256_padding[64] = {
*/ */
int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, unsigned char output[32] ) int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, unsigned char output[32] )
{ {
int ret; int ret = -1;
uint32_t last, padn; uint32_t last, padn;
uint32_t high, low; uint32_t high, low;
unsigned char msglen[8]; unsigned char msglen[8];

View File

@ -146,6 +146,26 @@ void mbedtls_sha512_starts( mbedtls_sha512_context *ctx,
} }
#endif #endif
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 int esp_internal_sha512_dma_process(mbedtls_sha512_context *ctx, static int esp_internal_sha512_dma_process(mbedtls_sha512_context *ctx,
const uint8_t *data, size_t len, const uint8_t *data, size_t len,
uint8_t *buf, size_t buf_len) uint8_t *buf, size_t buf_len)
@ -159,9 +179,22 @@ static int esp_internal_sha512_dma_process(mbedtls_sha512_context *ctx,
int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] ) int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] )
{ {
int ret; int ret = -1;
esp_sha_acquire_hardware(); esp_sha_acquire_hardware();
ret = esp_internal_sha_update_state(ctx);
if (ret != 0) {
esp_sha_release_hardware();
return ret;
}
ret = esp_internal_sha512_dma_process(ctx, data, 128, 0, 0); ret = esp_internal_sha512_dma_process(ctx, data, 128, 0, 0);
if (ret != 0) {
esp_sha_release_hardware();
return ret;
}
esp_sha_read_digest_state(ctx->mode, ctx->state);
esp_sha_release_hardware(); esp_sha_release_hardware();
return ret; return ret;
@ -182,7 +215,6 @@ void mbedtls_sha512_process( mbedtls_sha512_context *ctx,
int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx, const unsigned char *input, int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx, const unsigned char *input,
size_t ilen ) size_t ilen )
{ {
int ret;
size_t fill; size_t fill;
unsigned int left, len, local_len = 0; unsigned int left, len, local_len = 0;
@ -214,31 +246,24 @@ int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx, const unsigned char
esp_sha_acquire_hardware(); esp_sha_acquire_hardware();
if (ctx->sha_state == ESP_SHA512_STATE_INIT) { int ret = esp_internal_sha_update_state(ctx);
if (ctx->mode == SHA2_512T) { if (ret != 0) {
esp_sha_512_t_init_hash(ctx->t_val); esp_sha_release_hardware();
ctx->first_block = false; return ret;
} 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);
} }
ret = esp_internal_sha512_dma_process(ctx, input, len, ctx->buffer, local_len); ret = esp_internal_sha512_dma_process(ctx, input, len, ctx->buffer, local_len);
if (ret != 0) {
esp_sha_release_hardware();
return ret;
}
esp_sha_read_digest_state(ctx->mode, ctx->state); esp_sha_read_digest_state(ctx->mode, ctx->state);
esp_sha_release_hardware(); esp_sha_release_hardware();
if (ret != 0) {
return ret;
}
} }
@ -275,7 +300,7 @@ static const unsigned char sha512_padding[128] = {
*/ */
int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx, unsigned char output[64] ) int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx, unsigned char output[64] )
{ {
int ret; int ret = -1;
size_t last, padn; size_t last, padn;
uint64_t high, low; uint64_t high, low;
unsigned char msglen[16]; unsigned char msglen[16];

View File

@ -142,39 +142,6 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
} }
#endif #endif
static void mbedtls_sha1_software_process( mbedtls_sha1_context *ctx, const unsigned char data[64] );
int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
{
bool first_block = false;
if (ctx->mode == ESP_MBEDTLS_SHA1_UNUSED) {
/* try to use hardware for this digest */
if (esp_sha_try_lock_engine(SHA1)) {
ctx->mode = ESP_MBEDTLS_SHA1_HARDWARE;
first_block = true;
} else {
ctx->mode = ESP_MBEDTLS_SHA1_SOFTWARE;
}
}
if (ctx->mode == ESP_MBEDTLS_SHA1_HARDWARE) {
esp_sha_block(SHA1, data, first_block);
} else {
mbedtls_sha1_software_process(ctx, data);
}
return 0;
}
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
const unsigned char data[64] )
{
mbedtls_internal_sha1_process( ctx, data );
}
#endif
static void mbedtls_sha1_software_process( mbedtls_sha1_context *ctx, const unsigned char data[64] ) static void mbedtls_sha1_software_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
{ {
uint32_t temp, W[16], A, B, C, D, E; uint32_t temp, W[16], A, B, C, D, E;
@ -331,12 +298,46 @@ static void mbedtls_sha1_software_process( mbedtls_sha1_context *ctx, const unsi
ctx->state[4] += E; ctx->state[4] += E;
} }
static int esp_internal_sha1_parallel_engine_process( mbedtls_sha1_context *ctx, const unsigned char data[64], bool read_digest )
{
bool first_block = false;
if (ctx->mode == ESP_MBEDTLS_SHA1_UNUSED) {
/* try to use hardware for this digest */
if (esp_sha_try_lock_engine(SHA1)) {
ctx->mode = ESP_MBEDTLS_SHA1_HARDWARE;
first_block = true;
} else {
ctx->mode = ESP_MBEDTLS_SHA1_SOFTWARE;
}
}
if (ctx->mode == ESP_MBEDTLS_SHA1_HARDWARE) {
esp_sha_block(SHA1, data, first_block);
if (read_digest) {
esp_sha_read_digest_state(SHA1, ctx->state);
}
} else {
mbedtls_sha1_software_process(ctx, data);
}
return 0;
}
int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
{
return esp_internal_sha1_parallel_engine_process(ctx, data, true);
}
/* /*
* SHA-1 process buffer * SHA-1 process buffer
*/ */
int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ) int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
{ {
int ret; int ret = -1;
size_t fill; size_t fill;
uint32_t left; uint32_t left;
@ -357,7 +358,7 @@ int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, const unsigned char *inp
if ( left && ilen >= fill ) { if ( left && ilen >= fill ) {
memcpy( (void *) (ctx->buffer + left), input, fill ); memcpy( (void *) (ctx->buffer + left), input, fill );
if ( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 ) { if ( ( ret = esp_internal_sha1_parallel_engine_process( ctx, ctx->buffer, false ) ) != 0 ) {
return ret; return ret;
} }
@ -367,7 +368,7 @@ int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, const unsigned char *inp
} }
while ( ilen >= 64 ) { while ( ilen >= 64 ) {
if ( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 ) { if ( ( ret = esp_internal_sha1_parallel_engine_process( ctx, input, false ) ) != 0 ) {
return ret; return ret;
} }
@ -375,6 +376,10 @@ int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, const unsigned char *inp
ilen -= 64; ilen -= 64;
} }
if (ctx->mode == ESP_MBEDTLS_SHA1_HARDWARE) {
esp_sha_read_digest_state(SHA1, ctx->state);
}
if ( ilen > 0 ) { if ( ilen > 0 ) {
memcpy( (void *) (ctx->buffer + left), input, ilen ); memcpy( (void *) (ctx->buffer + left), input, ilen );
} }
@ -403,7 +408,7 @@ static const unsigned char sha1_padding[64] = {
*/ */
int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx, unsigned char output[20] ) int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx, unsigned char output[20] )
{ {
int ret; int ret = -1;
uint32_t last, padn; uint32_t last, padn;
uint32_t high, low; uint32_t high, low;
unsigned char msglen[8]; unsigned char msglen[8];

View File

@ -203,30 +203,6 @@ static const uint32_t K[] = {
d += temp1; h = temp1 + temp2; \ d += temp1; h = temp1 + temp2; \
} }
static void mbedtls_sha256_software_process( mbedtls_sha256_context *ctx, const unsigned char data[64] );
int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
{
bool first_block = false;
if (ctx->mode == ESP_MBEDTLS_SHA256_UNUSED) {
/* try to use hardware for this digest */
if (!ctx->is224 && esp_sha_try_lock_engine(SHA2_256)) {
ctx->mode = ESP_MBEDTLS_SHA256_HARDWARE;
first_block = true;
} else {
ctx->mode = ESP_MBEDTLS_SHA256_SOFTWARE;
}
}
if (ctx->mode == ESP_MBEDTLS_SHA256_HARDWARE) {
esp_sha_block(SHA2_256, data, first_block);
} else {
mbedtls_sha256_software_process(ctx, data);
}
return 0;
}
#if !defined(MBEDTLS_DEPRECATED_REMOVED) #if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_sha256_process( mbedtls_sha256_context *ctx, void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
@ -292,13 +268,47 @@ static void mbedtls_sha256_software_process( mbedtls_sha256_context *ctx, const
} }
} }
static int esp_internal_sha256_parallel_engine_process( mbedtls_sha256_context *ctx, const unsigned char data[64], bool read_digest )
{
bool first_block = false;
if (ctx->mode == ESP_MBEDTLS_SHA256_UNUSED) {
/* try to use hardware for this digest */
if (!ctx->is224 && esp_sha_try_lock_engine(SHA2_256)) {
ctx->mode = ESP_MBEDTLS_SHA256_HARDWARE;
first_block = true;
} else {
ctx->mode = ESP_MBEDTLS_SHA256_SOFTWARE;
}
}
if (ctx->mode == ESP_MBEDTLS_SHA256_HARDWARE) {
esp_sha_block(SHA2_256, data, first_block);
if (read_digest) {
esp_sha_read_digest_state(SHA2_256, ctx->state);
}
} else {
mbedtls_sha256_software_process(ctx, data);
}
return 0;
}
int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
{
return esp_internal_sha256_parallel_engine_process(ctx, data, true);
}
/* /*
* SHA-256 process buffer * SHA-256 process buffer
*/ */
int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char *input, int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char *input,
size_t ilen ) size_t ilen )
{ {
int ret; int ret = -1;
size_t fill; size_t fill;
uint32_t left; uint32_t left;
@ -319,7 +329,7 @@ int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char
if ( left && ilen >= fill ) { if ( left && ilen >= fill ) {
memcpy( (void *) (ctx->buffer + left), input, fill ); memcpy( (void *) (ctx->buffer + left), input, fill );
if ( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 ) { if ( ( ret = esp_internal_sha256_parallel_engine_process( ctx, ctx->buffer, false ) ) != 0 ) {
return ret; return ret;
} }
@ -329,7 +339,7 @@ int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char
} }
while ( ilen >= 64 ) { while ( ilen >= 64 ) {
if ( ( ret = mbedtls_internal_sha256_process( ctx, input ) ) != 0 ) { if ( ( ret = esp_internal_sha256_parallel_engine_process( ctx, input, false ) ) != 0 ) {
return ret; return ret;
} }
@ -337,6 +347,10 @@ int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char
ilen -= 64; ilen -= 64;
} }
if (ctx->mode == ESP_MBEDTLS_SHA256_HARDWARE) {
esp_sha_read_digest_state(SHA2_256, ctx->state);
}
if ( ilen > 0 ) { if ( ilen > 0 ) {
memcpy( (void *) (ctx->buffer + left), input, ilen ); memcpy( (void *) (ctx->buffer + left), input, ilen );
} }
@ -365,7 +379,7 @@ static const unsigned char sha256_padding[64] = {
*/ */
int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, unsigned char output[32] ) int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, unsigned char output[32] )
{ {
int ret; int ret = -1;
uint32_t last, padn; uint32_t last, padn;
uint32_t high, low; uint32_t high, low;
unsigned char msglen[8]; unsigned char msglen[8];

View File

@ -230,30 +230,6 @@ static const uint64_t K[80] = {
UL64(0x5FCB6FAB3AD6FAEC), UL64(0x6C44198C4A475817) 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;
}
#if !defined(MBEDTLS_DEPRECATED_REMOVED) #if !defined(MBEDTLS_DEPRECATED_REMOVED)
void mbedtls_sha512_process( mbedtls_sha512_context *ctx, void mbedtls_sha512_process( mbedtls_sha512_context *ctx,
@ -329,13 +305,47 @@ static void mbedtls_sha512_software_process( mbedtls_sha512_context *ctx, const
ctx->state[7] += H; 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 * SHA-512 process buffer
*/ */
int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx, const unsigned char *input, int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx, const unsigned char *input,
size_t ilen ) size_t ilen )
{ {
int ret; int ret = -1;
size_t fill; size_t fill;
unsigned int left; unsigned int left;
@ -354,7 +364,7 @@ int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx, const unsigned char
if ( left && ilen >= fill ) { if ( left && ilen >= fill ) {
memcpy( (void *) (ctx->buffer + left), input, 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; return ret;
} }
@ -364,7 +374,7 @@ int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx, const unsigned char
} }
while ( ilen >= 128 ) { 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; return ret;
} }
@ -372,6 +382,10 @@ int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx, const unsigned char
ilen -= 128; ilen -= 128;
} }
if (ctx->mode == ESP_MBEDTLS_SHA512_HARDWARE) {
esp_sha_read_digest_state(sha_type(ctx), ctx->state);
}
if ( ilen > 0 ) { if ( ilen > 0 ) {
memcpy( (void *) (ctx->buffer + left), input, ilen ); memcpy( (void *) (ctx->buffer + left), input, ilen );
} }
@ -405,7 +419,7 @@ static const unsigned char sha512_padding[128] = {
*/ */
int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx, unsigned char output[64] ) int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx, unsigned char output[64] )
{ {
int ret; int ret = -1;
size_t last, padn; size_t last, padn;
uint64_t high, low; uint64_t high, low;
unsigned char msglen[16]; unsigned char msglen[16];