Merge remote-tracking branch 'origin/pr/624' into baremetal

This commit is contained in:
Simon Butcher 2019-09-07 12:44:42 +01:00
commit d91adcf7f5
2 changed files with 30 additions and 23 deletions

View File

@ -1271,9 +1271,11 @@
* *
* The default implementation is meant to be a reasonnable compromise between * The default implementation is meant to be a reasonnable compromise between
* performance and size. This version optimizes more aggressively for size at * performance and size. This version optimizes more aggressively for size at
* the expense of performance. Eg on Cortex-M4 it reduces the size of * the expense of performance.
* mbedtls_sha256_process() from ~2KB to ~0.5KB for a performance hit of about *
* 30%. * For example, on a Cortex-M0 core it reduces the size of the module by about
* 2KiB for a performance cost of about 45%; on a Cortex-M4 core the size
* benefit is about 1.5 KiB for a performance cost of of about 30%.
* *
* Uncomment to enable the smaller implementation of SHA256. * Uncomment to enable the smaller implementation of SHA256.
*/ */

View File

@ -49,6 +49,11 @@
#endif /* MBEDTLS_PLATFORM_C */ #endif /* MBEDTLS_PLATFORM_C */
#endif /* MBEDTLS_SELF_TEST */ #endif /* MBEDTLS_SELF_TEST */
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
!defined(inline) && !defined(__cplusplus)
#define inline __inline
#endif
#define SHA256_VALIDATE_RET(cond) \ #define SHA256_VALIDATE_RET(cond) \
MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA256_BAD_INPUT_DATA ) MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA256_BAD_INPUT_DATA )
#define SHA256_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond ) #define SHA256_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
@ -56,7 +61,7 @@
#if !defined(MBEDTLS_SHA256_ALT) #if !defined(MBEDTLS_SHA256_ALT)
/* /*
* 32-bit integer manipulation macros (big endian) * 32-bit integer manipulation (big endian)
*/ */
#ifndef GET_UINT32_BE #ifndef GET_UINT32_BE
#define GET_UINT32_BE(n,b,i) \ #define GET_UINT32_BE(n,b,i) \
@ -68,15 +73,15 @@ do { \
} while( 0 ) } while( 0 )
#endif #endif
#ifndef PUT_UINT32_BE static inline void sha256_put_uint32_be( uint32_t n,
#define PUT_UINT32_BE(n,b,i) \ unsigned char *b,
do { \ uint8_t i )
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ {
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ b[i ] = (unsigned char) ( n >> 24 );
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ b[i + 1] = (unsigned char) ( n >> 16 );
(b)[(i) + 3] = (unsigned char) ( (n) ); \ b[i + 2] = (unsigned char) ( n >> 8 );
} while( 0 ) b[i + 3] = (unsigned char) ( n );
#endif }
void mbedtls_sha256_init( mbedtls_sha256_context *ctx ) void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
{ {
@ -373,8 +378,8 @@ int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
| ( ctx->total[1] << 3 ); | ( ctx->total[1] << 3 );
low = ( ctx->total[0] << 3 ); low = ( ctx->total[0] << 3 );
PUT_UINT32_BE( high, ctx->buffer, 56 ); sha256_put_uint32_be( high, ctx->buffer, 56 );
PUT_UINT32_BE( low, ctx->buffer, 60 ); sha256_put_uint32_be( low, ctx->buffer, 60 );
if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 ) if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 )
return( ret ); return( ret );
@ -382,16 +387,16 @@ int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
/* /*
* Output final state * Output final state
*/ */
PUT_UINT32_BE( ctx->state[0], output, 0 ); sha256_put_uint32_be( ctx->state[0], output, 0 );
PUT_UINT32_BE( ctx->state[1], output, 4 ); sha256_put_uint32_be( ctx->state[1], output, 4 );
PUT_UINT32_BE( ctx->state[2], output, 8 ); sha256_put_uint32_be( ctx->state[2], output, 8 );
PUT_UINT32_BE( ctx->state[3], output, 12 ); sha256_put_uint32_be( ctx->state[3], output, 12 );
PUT_UINT32_BE( ctx->state[4], output, 16 ); sha256_put_uint32_be( ctx->state[4], output, 16 );
PUT_UINT32_BE( ctx->state[5], output, 20 ); sha256_put_uint32_be( ctx->state[5], output, 20 );
PUT_UINT32_BE( ctx->state[6], output, 24 ); sha256_put_uint32_be( ctx->state[6], output, 24 );
if( ctx->is224 == 0 ) if( ctx->is224 == 0 )
PUT_UINT32_BE( ctx->state[7], output, 28 ); sha256_put_uint32_be( ctx->state[7], output, 28 );
return( 0 ); return( 0 );
} }