forked from espressif/esp-idf
Merge branch 'fix/sha_port_formatting_and_local_val_types' into 'master'
fix(mbedtls/sha): Fix formatting and change local variable's types Closes IDF-12217 and IDF-12218 See merge request espressif/esp-idf!36792
This commit is contained in:
@@ -36,10 +36,10 @@
|
|||||||
#include "sha/sha_core.h"
|
#include "sha/sha_core.h"
|
||||||
|
|
||||||
/* Implementation that should never be optimized out by the compiler */
|
/* Implementation that should never be optimized out by the compiler */
|
||||||
static void mbedtls_zeroize( void *v, size_t n )
|
static void mbedtls_zeroize(void *v, size_t n)
|
||||||
{
|
{
|
||||||
volatile unsigned char *p = (unsigned char *)v;
|
volatile unsigned char *p = (unsigned char *)v;
|
||||||
while ( n-- ) {
|
while (n--) {
|
||||||
*p++ = 0;
|
*p++ = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,28 +51,28 @@ static void mbedtls_zeroize( void *v, size_t n )
|
|||||||
#ifndef PUT_UINT32_BE
|
#ifndef PUT_UINT32_BE
|
||||||
#define PUT_UINT32_BE(n,b,i) \
|
#define PUT_UINT32_BE(n,b,i) \
|
||||||
{ \
|
{ \
|
||||||
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
|
(b)[(i) ] = (unsigned char) ((n) >> 24); \
|
||||||
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
|
(b)[(i) + 1] = (unsigned char) ((n) >> 16); \
|
||||||
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
|
(b)[(i) + 2] = (unsigned char) ((n) >> 8); \
|
||||||
(b)[(i) + 3] = (unsigned char) ( (n) ); \
|
(b)[(i) + 3] = (unsigned char) ((n) ); \
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
|
void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
|
||||||
{
|
{
|
||||||
memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
|
memset(ctx, 0, sizeof(mbedtls_sha1_context));
|
||||||
}
|
}
|
||||||
|
|
||||||
void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
|
void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
|
||||||
{
|
{
|
||||||
if ( ctx == NULL ) {
|
if (ctx == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
|
mbedtls_zeroize(ctx, sizeof(mbedtls_sha1_context));
|
||||||
}
|
}
|
||||||
|
|
||||||
void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
|
void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
|
||||||
const mbedtls_sha1_context *src )
|
const mbedtls_sha1_context *src)
|
||||||
{
|
{
|
||||||
memcpy(dst, src, sizeof(mbedtls_sha1_context));
|
memcpy(dst, src, sizeof(mbedtls_sha1_context));
|
||||||
}
|
}
|
||||||
@@ -80,11 +80,11 @@ void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
|
|||||||
/*
|
/*
|
||||||
* SHA-1 context setup
|
* SHA-1 context setup
|
||||||
*/
|
*/
|
||||||
int mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
|
int mbedtls_sha1_starts(mbedtls_sha1_context *ctx)
|
||||||
{
|
{
|
||||||
ctx->total[0] = 0;
|
ctx->total[0] = 0;
|
||||||
ctx->total[1] = 0;
|
ctx->total[1] = 0;
|
||||||
memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
|
memset(ctx, 0, sizeof(mbedtls_sha1_context));
|
||||||
ctx->mode = SHA1;
|
ctx->mode = SHA1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -110,7 +110,7 @@ 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_internal_sha_update_state(ctx);
|
||||||
@@ -133,12 +133,12 @@ int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, const unsigned cha
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
size_t fill;
|
size_t fill, left, len;
|
||||||
uint32_t left, len, local_len = 0;
|
uint32_t local_len = 0;
|
||||||
|
|
||||||
if ( !ilen || (input == NULL)) {
|
if (!ilen || (input == NULL)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,21 +148,21 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
|
|||||||
ctx->total[0] += (uint32_t) ilen;
|
ctx->total[0] += (uint32_t) ilen;
|
||||||
ctx->total[0] &= 0xFFFFFFFF;
|
ctx->total[0] &= 0xFFFFFFFF;
|
||||||
|
|
||||||
if ( ctx->total[0] < (uint32_t) ilen ) {
|
if (ctx->total[0] < (uint32_t) ilen) {
|
||||||
ctx->total[1]++;
|
ctx->total[1]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( left && ilen >= fill ) {
|
if (left && ilen >= fill) {
|
||||||
memcpy( (void *) (ctx->buffer + left), input, fill );
|
memcpy((void *) (ctx->buffer + left), input, fill);
|
||||||
input += fill;
|
input += fill;
|
||||||
ilen -= fill;
|
ilen -= fill;
|
||||||
left = 0;
|
left = 0;
|
||||||
local_len = 64;
|
local_len = 64;
|
||||||
}
|
}
|
||||||
|
|
||||||
len = (ilen / 64) * 64;
|
len = SHA_ALIGN_DOWN(ilen , 64);
|
||||||
|
|
||||||
if ( len || local_len) {
|
if (len || local_len) {
|
||||||
|
|
||||||
esp_sha_acquire_hardware();
|
esp_sha_acquire_hardware();
|
||||||
|
|
||||||
@@ -179,12 +179,12 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
|
|||||||
#endif /* SOC_SHA_SUPPORT_DMA */
|
#endif /* SOC_SHA_SUPPORT_DMA */
|
||||||
{
|
{
|
||||||
/* First process buffered block, if any */
|
/* First process buffered block, if any */
|
||||||
if ( local_len ) {
|
if (local_len) {
|
||||||
esp_internal_sha1_block_process(ctx, ctx->buffer);
|
esp_internal_sha1_block_process(ctx, ctx->buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t length_processed = 0;
|
uint32_t length_processed = 0;
|
||||||
while ( len - length_processed > 0 ) {
|
while (len - length_processed != 0) {
|
||||||
esp_internal_sha1_block_process(ctx, input + length_processed);
|
esp_internal_sha1_block_process(ctx, input + length_processed);
|
||||||
length_processed += 64;
|
length_processed += 64;
|
||||||
}
|
}
|
||||||
@@ -196,8 +196,8 @@ int mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ilen > 0 ) {
|
if (ilen > 0) {
|
||||||
memcpy( (void *) (ctx->buffer + left), input + len, ilen - len );
|
memcpy((void *) (ctx->buffer + left), input + len, ilen - len);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -212,28 +212,27 @@ static const unsigned char sha1_padding[64] = {
|
|||||||
/*
|
/*
|
||||||
* SHA-1 final digest
|
* SHA-1 final digest
|
||||||
*/
|
*/
|
||||||
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 = -1;
|
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];
|
||||||
|
|
||||||
high = ( ctx->total[0] >> 29 )
|
high = (ctx->total[0] >> 29)
|
||||||
| ( ctx->total[1] << 3 );
|
| (ctx->total[1] << 3);
|
||||||
low = ( ctx->total[0] << 3 );
|
low = (ctx->total[0] << 3);
|
||||||
|
|
||||||
PUT_UINT32_BE( high, msglen, 0 );
|
PUT_UINT32_BE(high, msglen, 0);
|
||||||
PUT_UINT32_BE( low, msglen, 4 );
|
PUT_UINT32_BE(low, msglen, 4);
|
||||||
|
|
||||||
last = ctx->total[0] & 0x3F;
|
last = ctx->total[0] & 0x3F;
|
||||||
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
|
padn = (last < 56) ? (56 - last) : (120 - last);
|
||||||
|
|
||||||
|
if ((ret = mbedtls_sha1_update(ctx, sha1_padding, padn)) != 0) {
|
||||||
if ( ( ret = mbedtls_sha1_update( ctx, sha1_padding, padn ) ) != 0 ) {
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
if ( ( ret = mbedtls_sha1_update( ctx, msglen, 8 ) ) != 0 ) {
|
if ((ret = mbedtls_sha1_update(ctx, msglen, 8)) != 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -36,10 +36,10 @@
|
|||||||
#include "sha/sha_core.h"
|
#include "sha/sha_core.h"
|
||||||
|
|
||||||
/* Implementation that should never be optimized out by the compiler */
|
/* Implementation that should never be optimized out by the compiler */
|
||||||
static void mbedtls_zeroize( void *v, size_t n )
|
static void mbedtls_zeroize(void *v, size_t n)
|
||||||
{
|
{
|
||||||
volatile unsigned char *p = v;
|
volatile unsigned char *p = v;
|
||||||
while ( n-- ) {
|
while (n--) {
|
||||||
*p++ = 0;
|
*p++ = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -50,39 +50,39 @@ static void mbedtls_zeroize( void *v, size_t n )
|
|||||||
#ifndef GET_UINT32_BE
|
#ifndef GET_UINT32_BE
|
||||||
#define GET_UINT32_BE(n,b,i) \
|
#define GET_UINT32_BE(n,b,i) \
|
||||||
do { \
|
do { \
|
||||||
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
|
(n) = ((uint32_t) (b)[(i) ] << 24) \
|
||||||
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
|
| ((uint32_t) (b)[(i) + 1] << 16) \
|
||||||
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
|
| ((uint32_t) (b)[(i) + 2] << 8) \
|
||||||
| ( (uint32_t) (b)[(i) + 3] ); \
|
| ((uint32_t) (b)[(i) + 3] ); \
|
||||||
} while( 0 )
|
} while(0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef PUT_UINT32_BE
|
#ifndef PUT_UINT32_BE
|
||||||
#define PUT_UINT32_BE(n,b,i) \
|
#define PUT_UINT32_BE(n,b,i) \
|
||||||
do { \
|
do { \
|
||||||
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
|
(b)[(i) ] = (unsigned char) ((n) >> 24); \
|
||||||
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
|
(b)[(i) + 1] = (unsigned char) ((n) >> 16); \
|
||||||
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
|
(b)[(i) + 2] = (unsigned char) ((n) >> 8); \
|
||||||
(b)[(i) + 3] = (unsigned char) ( (n) ); \
|
(b)[(i) + 3] = (unsigned char) ((n) ); \
|
||||||
} while( 0 )
|
} while(0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
|
void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
|
||||||
{
|
{
|
||||||
memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
|
memset(ctx, 0, sizeof(mbedtls_sha256_context));
|
||||||
}
|
}
|
||||||
|
|
||||||
void mbedtls_sha256_free( mbedtls_sha256_context *ctx )
|
void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
|
||||||
{
|
{
|
||||||
if ( ctx == NULL ) {
|
if (ctx == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
mbedtls_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
|
mbedtls_zeroize(ctx, sizeof(mbedtls_sha256_context));
|
||||||
}
|
}
|
||||||
|
|
||||||
void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
|
void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
|
||||||
const mbedtls_sha256_context *src )
|
const mbedtls_sha256_context *src)
|
||||||
{
|
{
|
||||||
*dst = *src;
|
*dst = *src;
|
||||||
}
|
}
|
||||||
@@ -90,11 +90,11 @@ void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
|
|||||||
/*
|
/*
|
||||||
* SHA-256 context setup
|
* SHA-256 context setup
|
||||||
*/
|
*/
|
||||||
int mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
|
int mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224)
|
||||||
{
|
{
|
||||||
memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
|
memset(ctx, 0, sizeof(mbedtls_sha256_context));
|
||||||
|
|
||||||
if ( is224 ) {
|
if (is224) {
|
||||||
ctx->mode = SHA2_224;
|
ctx->mode = SHA2_224;
|
||||||
} else {
|
} else {
|
||||||
ctx->mode = SHA2_256;
|
ctx->mode = SHA2_256;
|
||||||
@@ -123,7 +123,7 @@ 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_internal_sha_update_state(ctx);
|
||||||
@@ -149,13 +149,13 @@ int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned
|
|||||||
/*
|
/*
|
||||||
* 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)
|
||||||
{
|
{
|
||||||
size_t fill;
|
size_t fill, left, len;
|
||||||
uint32_t left, len, local_len = 0;
|
uint32_t local_len = 0;
|
||||||
|
|
||||||
if ( ilen == 0 ) {
|
if (ilen == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,13 +165,13 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
|
|||||||
ctx->total[0] += (uint32_t) ilen;
|
ctx->total[0] += (uint32_t) ilen;
|
||||||
ctx->total[0] &= 0xFFFFFFFF;
|
ctx->total[0] &= 0xFFFFFFFF;
|
||||||
|
|
||||||
if ( ctx->total[0] < (uint32_t) ilen ) {
|
if (ctx->total[0] < (uint32_t) ilen) {
|
||||||
ctx->total[1]++;
|
ctx->total[1]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if any data pending from previous call to this API */
|
/* Check if any data pending from previous call to this API */
|
||||||
if ( left && ilen >= fill ) {
|
if (left && ilen >= fill) {
|
||||||
memcpy( (void *) (ctx->buffer + left), input, fill );
|
memcpy((void *) (ctx->buffer + left), input, fill);
|
||||||
|
|
||||||
input += fill;
|
input += fill;
|
||||||
ilen -= fill;
|
ilen -= fill;
|
||||||
@@ -179,8 +179,9 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
|
|||||||
local_len = 64;
|
local_len = 64;
|
||||||
}
|
}
|
||||||
|
|
||||||
len = (ilen / 64) * 64;
|
len = SHA_ALIGN_DOWN(ilen , 64);
|
||||||
if ( len || local_len) {
|
|
||||||
|
if (len || local_len) {
|
||||||
|
|
||||||
esp_sha_acquire_hardware();
|
esp_sha_acquire_hardware();
|
||||||
|
|
||||||
@@ -197,12 +198,12 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
|
|||||||
#endif /* SOC_SHA_SUPPORT_DMA */
|
#endif /* SOC_SHA_SUPPORT_DMA */
|
||||||
{
|
{
|
||||||
/* First process buffered block, if any */
|
/* First process buffered block, if any */
|
||||||
if ( local_len ) {
|
if (local_len) {
|
||||||
esp_internal_sha256_block_process(ctx, ctx->buffer);
|
esp_internal_sha256_block_process(ctx, ctx->buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t length_processed = 0;
|
uint32_t length_processed = 0;
|
||||||
while ( len - length_processed > 0 ) {
|
while (len - length_processed != 0) {
|
||||||
esp_internal_sha256_block_process(ctx, input + length_processed);
|
esp_internal_sha256_block_process(ctx, input + length_processed);
|
||||||
length_processed += 64;
|
length_processed += 64;
|
||||||
}
|
}
|
||||||
@@ -213,8 +214,8 @@ int mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *inp
|
|||||||
esp_sha_release_hardware();
|
esp_sha_release_hardware();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ilen > 0 ) {
|
if (ilen > 0) {
|
||||||
memcpy( (void *) (ctx->buffer + left), input + len, ilen - len );
|
memcpy((void *) (ctx->buffer + left), input + len, ilen - len);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -230,28 +231,28 @@ static const unsigned char sha256_padding[64] = {
|
|||||||
/*
|
/*
|
||||||
* SHA-256 final digest
|
* SHA-256 final digest
|
||||||
*/
|
*/
|
||||||
int mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char *output )
|
int mbedtls_sha256_finish(mbedtls_sha256_context *ctx, unsigned char *output)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
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];
|
||||||
|
|
||||||
high = ( ctx->total[0] >> 29 )
|
high = (ctx->total[0] >> 29)
|
||||||
| ( ctx->total[1] << 3 );
|
| (ctx->total[1] << 3);
|
||||||
low = ( ctx->total[0] << 3 );
|
low = (ctx->total[0] << 3);
|
||||||
|
|
||||||
PUT_UINT32_BE( high, msglen, 0 );
|
PUT_UINT32_BE(high, msglen, 0);
|
||||||
PUT_UINT32_BE( low, msglen, 4 );
|
PUT_UINT32_BE(low, msglen, 4);
|
||||||
|
|
||||||
last = ctx->total[0] & 0x3F;
|
last = ctx->total[0] & 0x3F;
|
||||||
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
|
padn = (last < 56) ? (56 - last) : (120 - last);
|
||||||
|
|
||||||
if ( ( ret = mbedtls_sha256_update( ctx, sha256_padding, padn ) ) != 0 ) {
|
if ((ret = mbedtls_sha256_update(ctx, sha256_padding, padn)) != 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ( ret = mbedtls_sha256_update( ctx, msglen, 8 ) ) != 0 ) {
|
if ((ret = mbedtls_sha256_update(ctx, msglen, 8)) != 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -42,10 +42,10 @@
|
|||||||
#include "sha/sha_core.h"
|
#include "sha/sha_core.h"
|
||||||
|
|
||||||
/* Implementation that should never be optimized out by the compiler */
|
/* Implementation that should never be optimized out by the compiler */
|
||||||
static void mbedtls_zeroize( void *v, size_t n )
|
static void mbedtls_zeroize(void *v, size_t n)
|
||||||
{
|
{
|
||||||
volatile unsigned char *p = v;
|
volatile unsigned char *p = v;
|
||||||
while ( n-- ) {
|
while (n--) {
|
||||||
*p++ = 0;
|
*p++ = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -56,14 +56,14 @@ static void mbedtls_zeroize( void *v, size_t n )
|
|||||||
#ifndef PUT_UINT64_BE
|
#ifndef PUT_UINT64_BE
|
||||||
#define PUT_UINT64_BE(n,b,i) \
|
#define PUT_UINT64_BE(n,b,i) \
|
||||||
{ \
|
{ \
|
||||||
(b)[(i) ] = (unsigned char) ( (n) >> 56 ); \
|
(b)[(i) ] = (unsigned char) ((n) >> 56); \
|
||||||
(b)[(i) + 1] = (unsigned char) ( (n) >> 48 ); \
|
(b)[(i) + 1] = (unsigned char) ((n) >> 48); \
|
||||||
(b)[(i) + 2] = (unsigned char) ( (n) >> 40 ); \
|
(b)[(i) + 2] = (unsigned char) ((n) >> 40); \
|
||||||
(b)[(i) + 3] = (unsigned char) ( (n) >> 32 ); \
|
(b)[(i) + 3] = (unsigned char) ((n) >> 32); \
|
||||||
(b)[(i) + 4] = (unsigned char) ( (n) >> 24 ); \
|
(b)[(i) + 4] = (unsigned char) ((n) >> 24); \
|
||||||
(b)[(i) + 5] = (unsigned char) ( (n) >> 16 ); \
|
(b)[(i) + 5] = (unsigned char) ((n) >> 16); \
|
||||||
(b)[(i) + 6] = (unsigned char) ( (n) >> 8 ); \
|
(b)[(i) + 6] = (unsigned char) ((n) >> 8); \
|
||||||
(b)[(i) + 7] = (unsigned char) ( (n) ); \
|
(b)[(i) + 7] = (unsigned char) ((n) ); \
|
||||||
}
|
}
|
||||||
#endif /* PUT_UINT64_BE */
|
#endif /* PUT_UINT64_BE */
|
||||||
|
|
||||||
@@ -83,27 +83,27 @@ void esp_sha512_set_mode(mbedtls_sha512_context *ctx, esp_sha_type type)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* For SHA512/t mode the initial hash value will depend on t */
|
/* For SHA512/t mode the initial hash value will depend on t */
|
||||||
void esp_sha512_set_t( mbedtls_sha512_context *ctx, uint16_t t_val)
|
void esp_sha512_set_t(mbedtls_sha512_context *ctx, uint16_t t_val)
|
||||||
{
|
{
|
||||||
ctx->t_val = t_val;
|
ctx->t_val = t_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mbedtls_sha512_init( mbedtls_sha512_context *ctx )
|
void mbedtls_sha512_init(mbedtls_sha512_context *ctx)
|
||||||
{
|
{
|
||||||
memset( ctx, 0, sizeof( mbedtls_sha512_context ) );
|
memset(ctx, 0, sizeof(mbedtls_sha512_context));
|
||||||
}
|
}
|
||||||
|
|
||||||
void mbedtls_sha512_free( mbedtls_sha512_context *ctx )
|
void mbedtls_sha512_free(mbedtls_sha512_context *ctx)
|
||||||
{
|
{
|
||||||
if ( ctx == NULL ) {
|
if (ctx == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
mbedtls_zeroize( ctx, sizeof( mbedtls_sha512_context ) );
|
mbedtls_zeroize(ctx, sizeof(mbedtls_sha512_context));
|
||||||
}
|
}
|
||||||
|
|
||||||
void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
|
void mbedtls_sha512_clone(mbedtls_sha512_context *dst,
|
||||||
const mbedtls_sha512_context *src )
|
const mbedtls_sha512_context *src)
|
||||||
{
|
{
|
||||||
memcpy(dst, src, sizeof(mbedtls_sha512_context));
|
memcpy(dst, src, sizeof(mbedtls_sha512_context));
|
||||||
}
|
}
|
||||||
@@ -111,11 +111,11 @@ void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
|
|||||||
/*
|
/*
|
||||||
* SHA-512 context setup
|
* SHA-512 context setup
|
||||||
*/
|
*/
|
||||||
int mbedtls_sha512_starts( mbedtls_sha512_context *ctx, int is384 )
|
int mbedtls_sha512_starts(mbedtls_sha512_context *ctx, int is384)
|
||||||
{
|
{
|
||||||
mbedtls_zeroize( ctx, sizeof( mbedtls_sha512_context ) );
|
mbedtls_zeroize(ctx, sizeof(mbedtls_sha512_context));
|
||||||
|
|
||||||
if ( is384 ) {
|
if (is384) {
|
||||||
ctx->mode = SHA2_384;
|
ctx->mode = SHA2_384;
|
||||||
} else {
|
} else {
|
||||||
ctx->mode = SHA2_512;
|
ctx->mode = SHA2_512;
|
||||||
@@ -154,7 +154,7 @@ 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;
|
int ret = -1;
|
||||||
|
|
||||||
@@ -187,27 +187,26 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, const unsigned
|
|||||||
/*
|
/*
|
||||||
* 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 )
|
|
||||||
{
|
{
|
||||||
size_t fill;
|
size_t fill, left, len;
|
||||||
unsigned int left, len, local_len = 0;
|
uint32_t local_len = 0;
|
||||||
|
|
||||||
if ( ilen == 0 ) {
|
if (ilen == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
left = (unsigned int) (ctx->total[0] & 0x7F);
|
left = (size_t) (ctx->total[0] & 0x7F);
|
||||||
fill = 128 - left;
|
fill = 128 - left;
|
||||||
|
|
||||||
ctx->total[0] += (uint64_t) ilen;
|
ctx->total[0] += (uint64_t) ilen;
|
||||||
|
|
||||||
if ( ctx->total[0] < (uint64_t) ilen ) {
|
if (ctx->total[0] < (uint64_t) ilen) {
|
||||||
ctx->total[1]++;
|
ctx->total[1]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( left && ilen >= fill ) {
|
if (left && ilen >= fill) {
|
||||||
memcpy( (void *) (ctx->buffer + left), input, fill );
|
memcpy((void *) (ctx->buffer + left), input, fill);
|
||||||
|
|
||||||
input += fill;
|
input += fill;
|
||||||
ilen -= fill;
|
ilen -= fill;
|
||||||
@@ -215,8 +214,9 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
|
|||||||
local_len = 128;
|
local_len = 128;
|
||||||
}
|
}
|
||||||
|
|
||||||
len = (ilen / 128) * 128;
|
len = SHA_ALIGN_DOWN(ilen , 128);
|
||||||
if ( len || local_len) {
|
|
||||||
|
if (len || local_len) {
|
||||||
|
|
||||||
esp_sha_acquire_hardware();
|
esp_sha_acquire_hardware();
|
||||||
|
|
||||||
@@ -238,12 +238,12 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
|
|||||||
#endif /* SOC_SHA_SUPPORT_DMA */
|
#endif /* SOC_SHA_SUPPORT_DMA */
|
||||||
{
|
{
|
||||||
/* First process buffered block, if any */
|
/* First process buffered block, if any */
|
||||||
if ( local_len ) {
|
if (local_len) {
|
||||||
esp_internal_sha512_block_process(ctx, ctx->buffer);
|
esp_internal_sha512_block_process(ctx, ctx->buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t length_processed = 0;
|
uint32_t length_processed = 0;
|
||||||
while ( len - length_processed > 0 ) {
|
while (len - length_processed != 0) {
|
||||||
esp_internal_sha512_block_process(ctx, input + length_processed);
|
esp_internal_sha512_block_process(ctx, input + length_processed);
|
||||||
length_processed += 128;
|
length_processed += 128;
|
||||||
}
|
}
|
||||||
@@ -254,8 +254,8 @@ int mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *inp
|
|||||||
esp_sha_release_hardware();
|
esp_sha_release_hardware();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ilen > 0 ) {
|
if (ilen > 0) {
|
||||||
memcpy( (void *) (ctx->buffer + left), input + len, ilen - len );
|
memcpy((void *) (ctx->buffer + left), input + len, ilen - len);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -275,28 +275,28 @@ static const unsigned char sha512_padding[128] = {
|
|||||||
/*
|
/*
|
||||||
* SHA-512 final digest
|
* SHA-512 final digest
|
||||||
*/
|
*/
|
||||||
int mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char *output )
|
int mbedtls_sha512_finish(mbedtls_sha512_context *ctx, unsigned char *output)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
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];
|
||||||
|
|
||||||
high = ( ctx->total[0] >> 61 )
|
high = (ctx->total[0] >> 61)
|
||||||
| ( ctx->total[1] << 3 );
|
| (ctx->total[1] << 3);
|
||||||
low = ( ctx->total[0] << 3 );
|
low = (ctx->total[0] << 3);
|
||||||
|
|
||||||
PUT_UINT64_BE( high, msglen, 0 );
|
PUT_UINT64_BE(high, msglen, 0);
|
||||||
PUT_UINT64_BE( low, msglen, 8 );
|
PUT_UINT64_BE(low, msglen, 8);
|
||||||
|
|
||||||
last = (size_t)( ctx->total[0] & 0x7F );
|
last = (size_t)(ctx->total[0] & 0x7F);
|
||||||
padn = ( last < 112 ) ? ( 112 - last ) : ( 240 - last );
|
padn = (last < 112) ? (112 - last) : (240 - last);
|
||||||
|
|
||||||
if ( ( ret = mbedtls_sha512_update( ctx, sha512_padding, padn ) ) != 0 ) {
|
if ((ret = mbedtls_sha512_update(ctx, sha512_padding, padn)) != 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ( ret = mbedtls_sha512_update( ctx, msglen, 16 ) ) != 0 ) {
|
if ((ret = mbedtls_sha512_update(ctx, msglen, 16)) != 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -37,6 +37,8 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
#endif /* SOC_SHA_SUPPORT_DMA */
|
#endif /* SOC_SHA_SUPPORT_DMA */
|
||||||
|
|
||||||
|
#define SHA_ALIGN_DOWN(num, align) ((num) & ~((align) - 1))
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
SHA_BLOCK_MODE,
|
SHA_BLOCK_MODE,
|
||||||
#if SOC_SHA_SUPPORT_DMA
|
#if SOC_SHA_SUPPORT_DMA
|
||||||
|
Reference in New Issue
Block a user