|
|
@@ -48,11 +48,15 @@
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
|
|
|
|
|
-#include "esp32/sha.h"
|
|
|
+#include "sha/sha_parallel_engine.h"
|
|
|
|
|
|
/* Implementation that should never be optimized out by the compiler */
|
|
|
-static void mbedtls_zeroize( void *v, size_t n ) {
|
|
|
- volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
|
|
+static void mbedtls_zeroize( void *v, size_t n )
|
|
|
+{
|
|
|
+ volatile unsigned char *p = v;
|
|
|
+ while ( n-- ) {
|
|
|
+ *p++ = 0;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
@@ -85,8 +89,9 @@ void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
|
|
|
|
|
|
void mbedtls_sha256_free( mbedtls_sha256_context *ctx )
|
|
|
{
|
|
|
- if( ctx == NULL )
|
|
|
+ if ( ctx == NULL ) {
|
|
|
return;
|
|
|
+ }
|
|
|
|
|
|
if (ctx->mode == ESP_MBEDTLS_SHA256_HARDWARE) {
|
|
|
esp_sha_unlock_engine(SHA2_256);
|
|
|
@@ -116,8 +121,7 @@ int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 )
|
|
|
ctx->total[0] = 0;
|
|
|
ctx->total[1] = 0;
|
|
|
|
|
|
- if( is224 == 0 )
|
|
|
- {
|
|
|
+ if ( is224 == 0 ) {
|
|
|
/* SHA-256 */
|
|
|
ctx->state[0] = 0x6A09E667;
|
|
|
ctx->state[1] = 0xBB67AE85;
|
|
|
@@ -127,9 +131,7 @@ int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 )
|
|
|
ctx->state[5] = 0x9B05688C;
|
|
|
ctx->state[6] = 0x1F83D9AB;
|
|
|
ctx->state[7] = 0x5BE0CD19;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
+ } else {
|
|
|
/* SHA-224 */
|
|
|
ctx->state[0] = 0xC1059ED8;
|
|
|
ctx->state[1] = 0x367CD507;
|
|
|
@@ -157,8 +159,7 @@ void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
-static const uint32_t K[] =
|
|
|
-{
|
|
|
+static const uint32_t K[] = {
|
|
|
0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
|
|
|
0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
|
|
|
0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
|
|
|
@@ -241,16 +242,17 @@ static void mbedtls_sha256_software_process( mbedtls_sha256_context *ctx, const
|
|
|
uint32_t A[8];
|
|
|
unsigned int i;
|
|
|
|
|
|
- for( i = 0; i < 8; i++ )
|
|
|
+ for ( i = 0; i < 8; i++ ) {
|
|
|
A[i] = ctx->state[i];
|
|
|
+ }
|
|
|
|
|
|
#if defined(MBEDTLS_SHA256_SMALLER)
|
|
|
- for( i = 0; i < 64; i++ )
|
|
|
- {
|
|
|
- if( i < 16 )
|
|
|
+ for ( i = 0; i < 64; i++ ) {
|
|
|
+ if ( i < 16 ) {
|
|
|
GET_UINT32_BE( W[i], data, 4 * i );
|
|
|
- else
|
|
|
+ } else {
|
|
|
R( i );
|
|
|
+ }
|
|
|
|
|
|
P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i], K[i] );
|
|
|
|
|
|
@@ -258,50 +260,51 @@ static void mbedtls_sha256_software_process( mbedtls_sha256_context *ctx, const
|
|
|
A[3] = A[2]; A[2] = A[1]; A[1] = A[0]; A[0] = temp1;
|
|
|
}
|
|
|
#else /* MBEDTLS_SHA256_SMALLER */
|
|
|
- for( i = 0; i < 16; i++ )
|
|
|
+ for ( i = 0; i < 16; i++ ) {
|
|
|
GET_UINT32_BE( W[i], data, 4 * i );
|
|
|
+ }
|
|
|
|
|
|
- for( i = 0; i < 16; i += 8 )
|
|
|
- {
|
|
|
- P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i+0], K[i+0] );
|
|
|
- P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], W[i+1], K[i+1] );
|
|
|
- P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], W[i+2], K[i+2] );
|
|
|
- P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], W[i+3], K[i+3] );
|
|
|
- P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], W[i+4], K[i+4] );
|
|
|
- P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], W[i+5], K[i+5] );
|
|
|
- P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], W[i+6], K[i+6] );
|
|
|
- P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], W[i+7], K[i+7] );
|
|
|
+ for ( i = 0; i < 16; i += 8 ) {
|
|
|
+ P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i + 0], K[i + 0] );
|
|
|
+ P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], W[i + 1], K[i + 1] );
|
|
|
+ P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], W[i + 2], K[i + 2] );
|
|
|
+ P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], W[i + 3], K[i + 3] );
|
|
|
+ P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], W[i + 4], K[i + 4] );
|
|
|
+ P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], W[i + 5], K[i + 5] );
|
|
|
+ P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], W[i + 6], K[i + 6] );
|
|
|
+ P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], W[i + 7], K[i + 7] );
|
|
|
}
|
|
|
|
|
|
- for( i = 16; i < 64; i += 8 )
|
|
|
- {
|
|
|
- P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], R(i+0), K[i+0] );
|
|
|
- P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], R(i+1), K[i+1] );
|
|
|
- P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], R(i+2), K[i+2] );
|
|
|
- P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], R(i+3), K[i+3] );
|
|
|
- P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], R(i+4), K[i+4] );
|
|
|
- P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], R(i+5), K[i+5] );
|
|
|
- P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], R(i+6), K[i+6] );
|
|
|
- P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], R(i+7), K[i+7] );
|
|
|
+ for ( i = 16; i < 64; i += 8 ) {
|
|
|
+ P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], R(i + 0), K[i + 0] );
|
|
|
+ P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], R(i + 1), K[i + 1] );
|
|
|
+ P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], R(i + 2), K[i + 2] );
|
|
|
+ P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], R(i + 3), K[i + 3] );
|
|
|
+ P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], R(i + 4), K[i + 4] );
|
|
|
+ P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], R(i + 5), K[i + 5] );
|
|
|
+ P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], R(i + 6), K[i + 6] );
|
|
|
+ P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], R(i + 7), K[i + 7] );
|
|
|
}
|
|
|
#endif /* MBEDTLS_SHA256_SMALLER */
|
|
|
|
|
|
- for( i = 0; i < 8; i++ )
|
|
|
+ for ( i = 0; i < 8; i++ ) {
|
|
|
ctx->state[i] += A[i];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
* SHA-256 process buffer
|
|
|
*/
|
|
|
int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char *input,
|
|
|
- size_t ilen )
|
|
|
+ size_t ilen )
|
|
|
{
|
|
|
int ret;
|
|
|
size_t fill;
|
|
|
uint32_t left;
|
|
|
|
|
|
- if( ilen == 0 )
|
|
|
+ if ( ilen == 0 ) {
|
|
|
return 0;
|
|
|
+ }
|
|
|
|
|
|
left = ctx->total[0] & 0x3F;
|
|
|
fill = 64 - left;
|
|
|
@@ -309,11 +312,11 @@ int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char
|
|
|
ctx->total[0] += (uint32_t) ilen;
|
|
|
ctx->total[0] &= 0xFFFFFFFF;
|
|
|
|
|
|
- if( ctx->total[0] < (uint32_t) ilen )
|
|
|
+ if ( ctx->total[0] < (uint32_t) ilen ) {
|
|
|
ctx->total[1]++;
|
|
|
+ }
|
|
|
|
|
|
- if( left && ilen >= fill )
|
|
|
- {
|
|
|
+ if ( left && ilen >= fill ) {
|
|
|
memcpy( (void *) (ctx->buffer + left), input, fill );
|
|
|
|
|
|
if ( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 ) {
|
|
|
@@ -325,8 +328,7 @@ int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char
|
|
|
left = 0;
|
|
|
}
|
|
|
|
|
|
- while( ilen >= 64 )
|
|
|
- {
|
|
|
+ while ( ilen >= 64 ) {
|
|
|
if ( ( ret = mbedtls_internal_sha256_process( ctx, input ) ) != 0 ) {
|
|
|
return ret;
|
|
|
}
|
|
|
@@ -335,8 +337,9 @@ int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char
|
|
|
ilen -= 64;
|
|
|
}
|
|
|
|
|
|
- if( ilen > 0 )
|
|
|
+ if ( ilen > 0 ) {
|
|
|
memcpy( (void *) (ctx->buffer + left), input, ilen );
|
|
|
+ }
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
@@ -350,9 +353,8 @@ void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
-static const unsigned char sha256_padding[64] =
|
|
|
-{
|
|
|
- 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
+static const unsigned char sha256_padding[64] = {
|
|
|
+ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
|
@@ -369,7 +371,7 @@ int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, unsigned char output
|
|
|
unsigned char msglen[8];
|
|
|
|
|
|
high = ( ctx->total[0] >> 29 )
|
|
|
- | ( ctx->total[1] << 3 );
|
|
|
+ | ( ctx->total[1] << 3 );
|
|
|
low = ( ctx->total[0] << 3 );
|
|
|
|
|
|
PUT_UINT32_BE( high, msglen, 0 );
|
|
|
@@ -399,8 +401,9 @@ int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, unsigned char output
|
|
|
PUT_UINT32_BE( ctx->state[5], output, 20 );
|
|
|
PUT_UINT32_BE( ctx->state[6], output, 24 );
|
|
|
|
|
|
- if( ctx->is224 == 0 )
|
|
|
+ if ( ctx->is224 == 0 ) {
|
|
|
PUT_UINT32_BE( ctx->state[7], output, 28 );
|
|
|
+ }
|
|
|
|
|
|
out:
|
|
|
if (ctx->mode == ESP_MBEDTLS_SHA256_HARDWARE) {
|