Skip to content

Commit 7f07195

Browse files
committed
Make SHA512_SMALLER turn a macro into a function
Saves 356 bytes on sha512.o compiling for Cortex-M0+ with ARM-GCC Size measured with: arm-none-eabi-gcc -Wall -Wextra -Iinclude -Os -mcpu=cortex-m0plus -mthumb -c library/sha512.c arm-none-eabi-size sha512.o GCC version: arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 7-2018-q2-update) 7.3.1 20180622 (release) [ARM/embedded-7-branch revision 261907]
1 parent 2306d15 commit 7f07195

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

library/sha512.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@
9292
}
9393
#endif /* PUT_UINT64_BE */
9494

95+
#if defined(MBEDTLS_SHA512_SMALLER)
96+
static void sha512_put_uint64_be( uint64_t n, unsigned char *b, uint8_t i )
97+
{
98+
PUT_UINT64_BE(n, b, i);
99+
}
100+
#else
101+
#define sha512_put_uint64_be PUT_UINT64_BE
102+
#endif /* MBEDTLS_SHA512_SMALLER */
103+
95104
void mbedtls_sha512_init( mbedtls_sha512_context *ctx )
96105
{
97106
SHA512_VALIDATE( ctx != NULL );
@@ -403,26 +412,26 @@ int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
403412
| ( ctx->total[1] << 3 );
404413
low = ( ctx->total[0] << 3 );
405414

406-
PUT_UINT64_BE( high, ctx->buffer, 112 );
407-
PUT_UINT64_BE( low, ctx->buffer, 120 );
415+
sha512_put_uint64_be( high, ctx->buffer, 112 );
416+
sha512_put_uint64_be( low, ctx->buffer, 120 );
408417

409418
if( ( ret = mbedtls_internal_sha512_process( ctx, ctx->buffer ) ) != 0 )
410419
return( ret );
411420

412421
/*
413422
* Output final state
414423
*/
415-
PUT_UINT64_BE( ctx->state[0], output, 0 );
416-
PUT_UINT64_BE( ctx->state[1], output, 8 );
417-
PUT_UINT64_BE( ctx->state[2], output, 16 );
418-
PUT_UINT64_BE( ctx->state[3], output, 24 );
419-
PUT_UINT64_BE( ctx->state[4], output, 32 );
420-
PUT_UINT64_BE( ctx->state[5], output, 40 );
424+
sha512_put_uint64_be( ctx->state[0], output, 0 );
425+
sha512_put_uint64_be( ctx->state[1], output, 8 );
426+
sha512_put_uint64_be( ctx->state[2], output, 16 );
427+
sha512_put_uint64_be( ctx->state[3], output, 24 );
428+
sha512_put_uint64_be( ctx->state[4], output, 32 );
429+
sha512_put_uint64_be( ctx->state[5], output, 40 );
421430

422431
if( ctx->is384 == 0 )
423432
{
424-
PUT_UINT64_BE( ctx->state[6], output, 48 );
425-
PUT_UINT64_BE( ctx->state[7], output, 56 );
433+
sha512_put_uint64_be( ctx->state[6], output, 48 );
434+
sha512_put_uint64_be( ctx->state[7], output, 56 );
426435
}
427436

428437
return( 0 );

0 commit comments

Comments
 (0)