mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
Merge branch 'fix/mbedtls_internal_shaX_process_api_port' into 'master'
fix(mbedtls): Fix the port for mbedtls_internal_shaX_process api See merge request espressif/esp-idf!24545
This commit is contained in:
commit
48ead0158e
@ -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-1 standard was published by NIST in 1993.
|
* The SHA-1 standard was published by NIST in 1993.
|
||||||
@ -90,6 +90,16 @@ int mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
|
|||||||
return 0;
|
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)
|
static void esp_internal_sha1_block_process(mbedtls_sha1_context *ctx, const uint8_t *data)
|
||||||
{
|
{
|
||||||
@ -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] )
|
int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
|
||||||
{
|
{
|
||||||
esp_sha_acquire_hardware();
|
esp_sha_acquire_hardware();
|
||||||
|
esp_internal_sha_update_state(ctx);
|
||||||
esp_sha_block(ctx->mode, data, ctx->first_block);
|
esp_sha_block(ctx->mode, data, ctx->first_block);
|
||||||
|
esp_sha_read_digest_state(ctx->mode, ctx->state);
|
||||||
esp_sha_release_hardware();
|
esp_sha_release_hardware();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -138,12 +150,8 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
|
|||||||
if ( (ilen >= 64) || local_len) {
|
if ( (ilen >= 64) || local_len) {
|
||||||
|
|
||||||
esp_sha_acquire_hardware();
|
esp_sha_acquire_hardware();
|
||||||
if (ctx->sha_state == ESP_SHA1_STATE_INIT) {
|
|
||||||
ctx->first_block = true;
|
esp_internal_sha_update_state(ctx);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* First process buffered block, if any */
|
/* First process buffered block, if any */
|
||||||
if ( local_len ) {
|
if ( local_len ) {
|
||||||
@ -181,7 +189,7 @@ static const unsigned char sha1_padding[64] = {
|
|||||||
*/
|
*/
|
||||||
int mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
|
int mbedtls_sha1_finish( 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];
|
||||||
|
@ -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-256 Secure Hash Standard was published by NIST in 2002.
|
* The SHA-256 Secure Hash Standard was published by NIST in 2002.
|
||||||
@ -103,6 +103,17 @@ int mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
|
|||||||
return 0;
|
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)
|
static void esp_internal_sha256_block_process(mbedtls_sha256_context *ctx, const uint8_t *data)
|
||||||
{
|
{
|
||||||
esp_sha_block(ctx->mode, data, ctx->first_block);
|
esp_sha_block(ctx->mode, data, ctx->first_block);
|
||||||
@ -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] )
|
int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
|
||||||
{
|
{
|
||||||
esp_sha_acquire_hardware();
|
esp_sha_acquire_hardware();
|
||||||
|
esp_internal_sha_update_state(ctx);
|
||||||
esp_sha_block(ctx->mode, data, ctx->first_block);
|
esp_sha_block(ctx->mode, data, ctx->first_block);
|
||||||
|
esp_sha_read_digest_state(ctx->mode, ctx->state);
|
||||||
esp_sha_release_hardware();
|
esp_sha_release_hardware();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -156,13 +169,8 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
|
|||||||
if ( (ilen >= 64) || local_len) {
|
if ( (ilen >= 64) || local_len) {
|
||||||
|
|
||||||
esp_sha_acquire_hardware();
|
esp_sha_acquire_hardware();
|
||||||
if (ctx->sha_state == ESP_SHA256_STATE_INIT) {
|
|
||||||
ctx->first_block = true;
|
|
||||||
|
|
||||||
ctx->sha_state = ESP_SHA256_STATE_IN_PROCESS;
|
esp_internal_sha_update_state(ctx);
|
||||||
} else if (ctx->sha_state == ESP_SHA256_STATE_IN_PROCESS) {
|
|
||||||
esp_sha_write_digest_state(ctx->mode, ctx->state);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* First process buffered block, if any */
|
/* First process buffered block, if any */
|
||||||
if ( local_len ) {
|
if ( local_len ) {
|
||||||
@ -200,7 +208,7 @@ static const unsigned char sha256_padding[64] = {
|
|||||||
*/
|
*/
|
||||||
int mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char *output )
|
int mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char *output )
|
||||||
{
|
{
|
||||||
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];
|
||||||
|
@ -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.
|
||||||
@ -125,6 +125,27 @@ 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 void esp_internal_sha512_block_process(mbedtls_sha512_context *ctx, const uint8_t *data)
|
static void esp_internal_sha512_block_process(mbedtls_sha512_context *ctx, const uint8_t *data)
|
||||||
{
|
{
|
||||||
esp_sha_block(ctx->mode, data, ctx->first_block);
|
esp_sha_block(ctx->mode, data, ctx->first_block);
|
||||||
@ -136,10 +157,19 @@ static void esp_internal_sha512_block_process(mbedtls_sha512_context *ctx, const
|
|||||||
|
|
||||||
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 = -1;
|
||||||
esp_sha_acquire_hardware();
|
esp_sha_acquire_hardware();
|
||||||
esp_sha_block(ctx->mode, data, ctx->first_block);
|
|
||||||
|
ret = esp_internal_sha_update_state(ctx);
|
||||||
|
if (ret != 0) {
|
||||||
esp_sha_release_hardware();
|
esp_sha_release_hardware();
|
||||||
return 0;
|
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 ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -178,18 +208,11 @@ 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) {
|
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) {
|
|
||||||
esp_sha_write_digest_state(ctx->mode, ctx->state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* First process buffered block, if any */
|
/* First process buffered block, if any */
|
||||||
@ -206,7 +229,6 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
|
|||||||
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 ( ilen > 0 ) {
|
if ( ilen > 0 ) {
|
||||||
@ -232,7 +254,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];
|
||||||
|
@ -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-1 standard was published by NIST in 1993.
|
* The SHA-1 standard was published by NIST in 1993.
|
||||||
@ -88,6 +88,16 @@ int mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
|
|||||||
return 0;
|
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,
|
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,
|
||||||
@ -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 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
|
int mbedtls_sha1_update( 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;
|
||||||
|
|
||||||
@ -138,25 +155,19 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
|
|||||||
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 ) {
|
||||||
@ -178,7 +189,7 @@ static const unsigned char sha1_padding[64] = {
|
|||||||
*/
|
*/
|
||||||
int mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
|
int mbedtls_sha1_finish( 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];
|
||||||
|
@ -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-256 Secure Hash Standard was published by NIST in 2002.
|
* The SHA-256 Secure Hash Standard was published by NIST in 2002.
|
||||||
@ -100,11 +100,30 @@ int mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
|
|||||||
return 0;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
@ -116,7 +135,6 @@ int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned
|
|||||||
int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input,
|
int mbedtls_sha256_update( 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;
|
||||||
|
|
||||||
@ -148,24 +166,18 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
|
|||||||
|
|
||||||
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 ) {
|
||||||
@ -187,7 +199,7 @@ static const unsigned char sha256_padding[64] = {
|
|||||||
*/
|
*/
|
||||||
int mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char *output )
|
int mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char *output )
|
||||||
{
|
{
|
||||||
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];
|
||||||
|
@ -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) {
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -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];
|
||||||
|
@ -7,7 +7,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-1 standard was published by NIST in 1993.
|
* The SHA-1 standard was published by NIST in 1993.
|
||||||
@ -121,30 +121,6 @@ int mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
||||||
@ -301,12 +277,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( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
|
int mbedtls_sha1_update( 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;
|
||||||
|
|
||||||
@ -327,7 +337,7 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -337,7 +347,7 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
|
|||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -345,6 +355,10 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
|
|||||||
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 );
|
||||||
}
|
}
|
||||||
@ -364,7 +378,7 @@ static const unsigned char sha1_padding[64] = {
|
|||||||
*/
|
*/
|
||||||
int mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
|
int mbedtls_sha1_finish( 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];
|
||||||
|
@ -7,7 +7,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-256 Secure Hash Standard was published by NIST in 2002.
|
* The SHA-256 Secure Hash Standard was published by NIST in 2002.
|
||||||
@ -179,30 +179,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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mbedtls_sha256_software_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
|
static void mbedtls_sha256_software_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
|
||||||
{
|
{
|
||||||
@ -260,13 +236,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( mbedtls_sha256_context *ctx, const unsigned char *input,
|
int mbedtls_sha256_update( 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;
|
||||||
|
|
||||||
@ -287,7 +297,7 @@ int mbedtls_sha256_update( mbedtls_sha256_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_sha256_process( ctx, ctx->buffer ) ) != 0 ) {
|
if ( ( ret = esp_internal_sha256_parallel_engine_process( ctx, ctx->buffer, false ) ) != 0 ) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -297,7 +307,7 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
|
|||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -305,6 +315,10 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
|
|||||||
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 );
|
||||||
}
|
}
|
||||||
@ -324,7 +338,7 @@ static const unsigned char sha256_padding[64] = {
|
|||||||
*/
|
*/
|
||||||
int mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char *output )
|
int mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char *output )
|
||||||
{
|
{
|
||||||
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];
|
||||||
|
@ -7,7 +7,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.
|
||||||
@ -206,30 +206,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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mbedtls_sha512_software_process( mbedtls_sha512_context *ctx, const unsigned char data[128] )
|
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;
|
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( 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;
|
int ret = -1;
|
||||||
size_t fill;
|
size_t fill;
|
||||||
unsigned int left;
|
unsigned int left;
|
||||||
|
|
||||||
@ -321,7 +331,7 @@ int mbedtls_sha512_update( mbedtls_sha512_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_sha512_process( ctx, ctx->buffer ) ) != 0 ) {
|
if ( ( ret = esp_internal_sha512_parallel_engine_process( ctx, ctx->buffer, false ) ) != 0 ) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -331,7 +341,7 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
|
|||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -339,6 +349,10 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
|
|||||||
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 );
|
||||||
}
|
}
|
||||||
@ -362,7 +376,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];
|
||||||
|
@ -142,4 +142,92 @@ TEST_CASE("Test esp_sha() function with long input", "[hw_crypto]")
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#if CONFIG_MBEDTLS_HARDWARE_SHA
|
||||||
|
|
||||||
|
TEST_CASE("Test mbedtls_internal_sha_process()", "[hw_crypto]")
|
||||||
|
{
|
||||||
|
const size_t BUFFER_SZ = 128;
|
||||||
|
int ret;
|
||||||
|
unsigned char output[64] = { 0 };
|
||||||
|
void *buffer = heap_caps_malloc(BUFFER_SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
|
||||||
|
TEST_ASSERT_NOT_NULL(buffer);
|
||||||
|
memset(buffer, 0xEE, BUFFER_SZ);
|
||||||
|
|
||||||
|
mbedtls_sha1_context sha1_ctx;
|
||||||
|
|
||||||
|
const uint8_t sha1_expected[20] = { 0x41, 0x63, 0x12, 0x5b, 0x9c, 0x68, 0x85, 0xc8,
|
||||||
|
0x01, 0x40, 0xf4, 0x03, 0x5d, 0x0d, 0x84, 0x0e,
|
||||||
|
0xa4, 0xae, 0x4d, 0xe9 };
|
||||||
|
|
||||||
|
mbedtls_sha1_init(&sha1_ctx);
|
||||||
|
mbedtls_sha1_starts(&sha1_ctx);
|
||||||
|
|
||||||
|
ret = mbedtls_internal_sha1_process(&sha1_ctx, buffer);
|
||||||
|
TEST_ASSERT_EQUAL(0, ret);
|
||||||
|
|
||||||
|
ret = mbedtls_internal_sha1_process(&sha1_ctx, buffer);
|
||||||
|
TEST_ASSERT_EQUAL(0, ret);
|
||||||
|
|
||||||
|
#if SOC_SHA_ENDIANNESS_BE
|
||||||
|
for (int i = 0; i < sizeof(sha1_ctx.state)/sizeof(sha1_ctx.state[0]); i++)
|
||||||
|
{
|
||||||
|
*(uint32_t *)(output + i*4) = __builtin_bswap32(sha1_ctx.state[i]);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
memcpy(output, sha1_ctx.state, 20);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Check if the intermediate states are correct
|
||||||
|
TEST_ASSERT_EQUAL_HEX8_ARRAY(sha1_expected, output, sizeof(sha1_expected));
|
||||||
|
|
||||||
|
ret = mbedtls_sha1_finish(&sha1_ctx, output);
|
||||||
|
TEST_ASSERT_EQUAL(0, ret);
|
||||||
|
|
||||||
|
mbedtls_sha1_free(&sha1_ctx);
|
||||||
|
|
||||||
|
#if SOC_SHA_SUPPORT_SHA512
|
||||||
|
mbedtls_sha512_context sha512_ctx;
|
||||||
|
|
||||||
|
const uint8_t sha512_expected[64] = { 0x3c, 0x77, 0x5f, 0xb0, 0x3b, 0x25, 0x8d, 0x3b,
|
||||||
|
0xa9, 0x28, 0xa2, 0x29, 0xf2, 0x14, 0x7d, 0xb3,
|
||||||
|
0x64, 0x1e, 0x76, 0xd5, 0x0b, 0xbc, 0xdf, 0xb4,
|
||||||
|
0x75, 0x1d, 0xe7, 0x7f, 0x62, 0x83, 0xdd, 0x78,
|
||||||
|
0x6b, 0x0e, 0xa4, 0xd2, 0xbe, 0x51, 0x56, 0xd4,
|
||||||
|
0xfe, 0x3b, 0xa3, 0x3a, 0xd7, 0xf6, 0xd3, 0xb3,
|
||||||
|
0xe7, 0x9d, 0xb5, 0xe6, 0x76, 0x35, 0x2a, 0xae,
|
||||||
|
0x07, 0x0a, 0x3a, 0x03, 0x44, 0xf0, 0xb8, 0xfe };
|
||||||
|
|
||||||
|
mbedtls_sha512_init(&sha512_ctx);
|
||||||
|
mbedtls_sha512_starts(&sha512_ctx, 0);
|
||||||
|
|
||||||
|
ret = mbedtls_internal_sha512_process(&sha512_ctx, buffer);
|
||||||
|
TEST_ASSERT_EQUAL(0, ret);
|
||||||
|
|
||||||
|
ret = mbedtls_internal_sha512_process(&sha512_ctx, buffer);
|
||||||
|
TEST_ASSERT_EQUAL(0, ret);
|
||||||
|
|
||||||
|
#if SOC_SHA_ENDIANNESS_BE
|
||||||
|
for (int i = 0; i < sizeof(sha512_ctx.state)/sizeof(sha512_ctx.state[0]); i++)
|
||||||
|
{
|
||||||
|
*(uint64_t *)(output + i*8) = __builtin_bswap64(sha512_ctx.state[i]);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
memcpy(output, sha512_ctx.state, 64);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Check if the intermediate states are correct
|
||||||
|
TEST_ASSERT_EQUAL_HEX8_ARRAY(sha512_expected, output, sizeof(sha512_expected));
|
||||||
|
|
||||||
|
ret = mbedtls_sha512_finish(&sha512_ctx, output);
|
||||||
|
TEST_ASSERT_EQUAL(0, ret);
|
||||||
|
|
||||||
|
mbedtls_sha512_free(&sha512_ctx);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
free(buffer);
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif // SOC_SHA_SUPPORTED
|
#endif // SOC_SHA_SUPPORTED
|
||||||
|
@ -691,6 +691,10 @@ config SOC_SHA_SUPPORT_PARALLEL_ENG
|
|||||||
bool
|
bool
|
||||||
default y
|
default y
|
||||||
|
|
||||||
|
config SOC_SHA_ENDIANNESS_BE
|
||||||
|
bool
|
||||||
|
default y
|
||||||
|
|
||||||
config SOC_SHA_SUPPORT_SHA1
|
config SOC_SHA_SUPPORT_SHA1
|
||||||
bool
|
bool
|
||||||
default y
|
default y
|
||||||
|
@ -338,6 +338,9 @@
|
|||||||
/* ESP32 style SHA engine, where multiple states can be stored in parallel */
|
/* ESP32 style SHA engine, where multiple states can be stored in parallel */
|
||||||
#define SOC_SHA_SUPPORT_PARALLEL_ENG (1)
|
#define SOC_SHA_SUPPORT_PARALLEL_ENG (1)
|
||||||
|
|
||||||
|
/* ESP32's SHA peripheral processes and stores data in big-endian format */
|
||||||
|
#define SOC_SHA_ENDIANNESS_BE (1)
|
||||||
|
|
||||||
/* Supported HW algorithms */
|
/* Supported HW algorithms */
|
||||||
#define SOC_SHA_SUPPORT_SHA1 (1)
|
#define SOC_SHA_SUPPORT_SHA1 (1)
|
||||||
#define SOC_SHA_SUPPORT_SHA256 (1)
|
#define SOC_SHA_SUPPORT_SHA256 (1)
|
||||||
|
@ -185,6 +185,9 @@
|
|||||||
|
|
||||||
/*--------------------------- SHA CAPS ---------------------------------------*/
|
/*--------------------------- SHA CAPS ---------------------------------------*/
|
||||||
|
|
||||||
|
/* Due to very limited availability of the DMA channels, DMA support for the SHA peripheral is disabled */
|
||||||
|
// #define SOC_SHA_SUPPORT_DMA (1)
|
||||||
|
|
||||||
/* The SHA engine is able to resume hashing from a user */
|
/* The SHA engine is able to resume hashing from a user */
|
||||||
#define SOC_SHA_SUPPORT_RESUME (1)
|
#define SOC_SHA_SUPPORT_RESUME (1)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user