From 100c057d0d1b06602b41bf1444e43f4dec4fc8bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 17 Jul 2019 12:15:05 +0200 Subject: [PATCH 1/4] Make SHA256_SMALLER option yield even smaller code --- library/sha256.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/library/sha256.c b/library/sha256.c index 2dc0e1a2c..fe381d2a2 100644 --- a/library/sha256.c +++ b/library/sha256.c @@ -78,6 +78,15 @@ do { \ } while( 0 ) #endif +#if defined(MBEDTLS_SHA256_SMALLER) +static void sha256_put_uint32_be( uint32_t n, unsigned char *b, uint8_t i ) +{ + PUT_UINT32_BE(n, b, i); +} +#else +#define sha256_put_uint32_be PUT_UINT32_BE +#endif + void mbedtls_sha256_init( mbedtls_sha256_context *ctx ) { SHA256_VALIDATE( ctx != NULL ); @@ -373,8 +382,8 @@ int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, | ( ctx->total[1] << 3 ); low = ( ctx->total[0] << 3 ); - PUT_UINT32_BE( high, ctx->buffer, 56 ); - PUT_UINT32_BE( low, ctx->buffer, 60 ); + sha256_put_uint32_be( high, ctx->buffer, 56 ); + sha256_put_uint32_be( low, ctx->buffer, 60 ); if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 ) return( ret ); @@ -382,16 +391,16 @@ int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, /* * Output final state */ - PUT_UINT32_BE( ctx->state[0], output, 0 ); - PUT_UINT32_BE( ctx->state[1], output, 4 ); - PUT_UINT32_BE( ctx->state[2], output, 8 ); - PUT_UINT32_BE( ctx->state[3], output, 12 ); - PUT_UINT32_BE( ctx->state[4], output, 16 ); - PUT_UINT32_BE( ctx->state[5], output, 20 ); - PUT_UINT32_BE( ctx->state[6], output, 24 ); + sha256_put_uint32_be( ctx->state[0], output, 0 ); + sha256_put_uint32_be( ctx->state[1], output, 4 ); + sha256_put_uint32_be( ctx->state[2], output, 8 ); + sha256_put_uint32_be( ctx->state[3], output, 12 ); + sha256_put_uint32_be( ctx->state[4], output, 16 ); + sha256_put_uint32_be( ctx->state[5], output, 20 ); + sha256_put_uint32_be( ctx->state[6], output, 24 ); if( ctx->is224 == 0 ) - PUT_UINT32_BE( ctx->state[7], output, 28 ); + sha256_put_uint32_be( ctx->state[7], output, 28 ); return( 0 ); } From c7abba379647986bca193f0bbe7c77d181a4687d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 5 Sep 2019 11:33:32 +0200 Subject: [PATCH 2/4] Use static inline function unconditionally No need to play tricks with macros and functions depending on whether SHA256_SMALLER is enabled or not, with a static inline function all common compilers (tested with arm-gcc, armcc5, arm-clang) will Do The Right Thing depending on whether we told them to optimize for size or speed. --- library/sha256.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/library/sha256.c b/library/sha256.c index fe381d2a2..5551140c3 100644 --- a/library/sha256.c +++ b/library/sha256.c @@ -56,7 +56,7 @@ #if !defined(MBEDTLS_SHA256_ALT) /* - * 32-bit integer manipulation macros (big endian) + * 32-bit integer manipulation (big endian) */ #ifndef GET_UINT32_BE #define GET_UINT32_BE(n,b,i) \ @@ -68,24 +68,15 @@ do { \ } while( 0 ) #endif -#ifndef PUT_UINT32_BE -#define PUT_UINT32_BE(n,b,i) \ -do { \ - (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) ); \ -} while( 0 ) -#endif - -#if defined(MBEDTLS_SHA256_SMALLER) -static void sha256_put_uint32_be( uint32_t n, unsigned char *b, uint8_t i ) +static inline void sha256_put_uint32_be( uint32_t n, + unsigned char *b, + uint8_t i ) { - PUT_UINT32_BE(n, b, i); + b[i ] = (unsigned char) ( n >> 24 ); + b[i + 1] = (unsigned char) ( n >> 16 ); + b[i + 2] = (unsigned char) ( n >> 8 ); + b[i + 3] = (unsigned char) ( n ); } -#else -#define sha256_put_uint32_be PUT_UINT32_BE -#endif void mbedtls_sha256_init( mbedtls_sha256_context *ctx ) { From 8bf8f2ebde9d0b0c99d8a6c40492bf4403eb40f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 5 Sep 2019 13:08:21 +0200 Subject: [PATCH 3/4] Improve documentation in config.h --- include/mbedtls/config.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h index 5b172387e..8e6bc4bcb 100644 --- a/include/mbedtls/config.h +++ b/include/mbedtls/config.h @@ -1271,9 +1271,11 @@ * * The default implementation is meant to be a reasonnable compromise between * performance and size. This version optimizes more aggressively for size at - * the expense of performance. Eg on Cortex-M4 it reduces the size of - * mbedtls_sha256_process() from ~2KB to ~0.5KB for a performance hit of about - * 30%. + * the expense of performance. + * + * 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. */ From 0cfb6efad4d4524ad4b6851460b9f0a647c087c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 5 Sep 2019 14:07:01 +0200 Subject: [PATCH 4/4] Add work-around for non-C99 compilers --- library/sha256.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/library/sha256.c b/library/sha256.c index 5551140c3..91d461c95 100644 --- a/library/sha256.c +++ b/library/sha256.c @@ -49,6 +49,11 @@ #endif /* MBEDTLS_PLATFORM_C */ #endif /* MBEDTLS_SELF_TEST */ +#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ + !defined(inline) && !defined(__cplusplus) +#define inline __inline +#endif + #define SHA256_VALIDATE_RET(cond) \ MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA256_BAD_INPUT_DATA ) #define SHA256_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )