|
@@ -5,7 +5,7 @@
|
|
|
*
|
|
*
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
* 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.
|
|
* The SHA-512 Secure Hash Standard was published by NIST in 2002.
|
|
@@ -122,6 +122,26 @@ int mbedtls_sha512_starts( mbedtls_sha512_context *ctx, int is384 )
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+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)
|
|
@@ -135,9 +155,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;
|
|
@@ -150,7 +183,6 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, const unsigned
|
|
|
int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *input,
|
|
int mbedtls_sha512_update( 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;
|
|
|
|
|
|
|
@@ -182,31 +214,24 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
|
|
|
|
|
|
|
|
esp_sha_acquire_hardware();
|
|
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) {
|
|
|
|
|
- ctx->first_block = false;
|
|
|
|
|
- esp_sha_write_digest_state(ctx->mode, ctx->state);
|
|
|
|
|
|
|
+ if (ret != 0) {
|
|
|
|
|
+ esp_sha_release_hardware();
|
|
|
|
|
+ return ret;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
- esp_sha_read_digest_state(ctx->mode, ctx->state);
|
|
|
|
|
-
|
|
|
|
|
- esp_sha_release_hardware();
|
|
|
|
|
-
|
|
|
|
|
if (ret != 0) {
|
|
if (ret != 0) {
|
|
|
|
|
+ esp_sha_release_hardware();
|
|
|
return ret;
|
|
return ret;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ esp_sha_read_digest_state(ctx->mode, ctx->state);
|
|
|
|
|
+
|
|
|
|
|
+ esp_sha_release_hardware();
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -233,7 +258,7 @@ static const unsigned char sha512_padding[128] = {
|
|
|
*/
|
|
*/
|
|
|
int mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char *output )
|
|
int mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char *output )
|
|
|
{
|
|
{
|
|
|
- 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];
|