Skip to content

Commit f66e7ea

Browse files
authored
Merge pull request #178 from mpg/sha512-smaller
New config.h option to make SHA-512 smaller
2 parents 4013b98 + 49d65ba commit f66e7ea

File tree

4 files changed

+74
-36
lines changed

4 files changed

+74
-36
lines changed

include/mbedtls/config.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,16 @@
10061006
*/
10071007
//#define MBEDTLS_SHA256_SMALLER
10081008

1009+
/**
1010+
* \def MBEDTLS_SHA512_SMALLER
1011+
*
1012+
* Enable an implementation of SHA-512 that has lower ROM footprint but also
1013+
* lower performance.
1014+
*
1015+
* Uncomment to enable the smaller implementation of SHA512.
1016+
*/
1017+
//#define MBEDTLS_SHA512_SMALLER
1018+
10091019
/**
10101020
* \def MBEDTLS_THREADING_ALT
10111021
*

library/sha512.c

Lines changed: 53 additions & 36 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 );
@@ -219,7 +228,7 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
219228
{
220229
int i;
221230
uint64_t temp1, temp2, W[80];
222-
uint64_t A, B, C, D, E, F, G, H;
231+
uint64_t A[8];
223232

224233
SHA512_VALIDATE_RET( ctx != NULL );
225234
SHA512_VALIDATE_RET( (const unsigned char *)data != NULL );
@@ -244,6 +253,28 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
244253
(d) += temp1; (h) = temp1 + temp2; \
245254
} while( 0 )
246255

256+
for( i = 0; i < 8; i++ )
257+
A[i] = ctx->state[i];
258+
259+
#if defined(MBEDTLS_SHA512_SMALLER)
260+
for( i = 0; i < 80; i++ )
261+
{
262+
if( i < 16 )
263+
{
264+
GET_UINT64_BE( W[i], data, i << 3 );
265+
}
266+
else
267+
{
268+
W[i] = S1(W[i - 2]) + W[i - 7] +
269+
S0(W[i - 15]) + W[i - 16];
270+
}
271+
272+
P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i], K[i] );
273+
274+
temp1 = A[7]; A[7] = A[6]; A[6] = A[5]; A[5] = A[4]; A[4] = A[3];
275+
A[3] = A[2]; A[2] = A[1]; A[1] = A[0]; A[0] = temp1;
276+
}
277+
#else /* MBEDTLS_SHA512_SMALLER */
247278
for( i = 0; i < 16; i++ )
248279
{
249280
GET_UINT64_BE( W[i], data, i << 3 );
@@ -255,37 +286,23 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
255286
S0(W[i - 15]) + W[i - 16];
256287
}
257288

258-
A = ctx->state[0];
259-
B = ctx->state[1];
260-
C = ctx->state[2];
261-
D = ctx->state[3];
262-
E = ctx->state[4];
263-
F = ctx->state[5];
264-
G = ctx->state[6];
265-
H = ctx->state[7];
266289
i = 0;
267-
268290
do
269291
{
270-
P( A, B, C, D, E, F, G, H, W[i], K[i] ); i++;
271-
P( H, A, B, C, D, E, F, G, W[i], K[i] ); i++;
272-
P( G, H, A, B, C, D, E, F, W[i], K[i] ); i++;
273-
P( F, G, H, A, B, C, D, E, W[i], K[i] ); i++;
274-
P( E, F, G, H, A, B, C, D, W[i], K[i] ); i++;
275-
P( D, E, F, G, H, A, B, C, W[i], K[i] ); i++;
276-
P( C, D, E, F, G, H, A, B, W[i], K[i] ); i++;
277-
P( B, C, D, E, F, G, H, A, W[i], K[i] ); i++;
292+
P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i], K[i] ); i++;
293+
P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], W[i], K[i] ); i++;
294+
P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], W[i], K[i] ); i++;
295+
P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], W[i], K[i] ); i++;
296+
P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], W[i], K[i] ); i++;
297+
P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], W[i], K[i] ); i++;
298+
P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], W[i], K[i] ); i++;
299+
P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], W[i], K[i] ); i++;
278300
}
279301
while( i < 80 );
302+
#endif /* MBEDTLS_SHA512_SMALLER */
280303

281-
ctx->state[0] += A;
282-
ctx->state[1] += B;
283-
ctx->state[2] += C;
284-
ctx->state[3] += D;
285-
ctx->state[4] += E;
286-
ctx->state[5] += F;
287-
ctx->state[6] += G;
288-
ctx->state[7] += H;
304+
for( i = 0; i < 8; i++ )
305+
ctx->state[i] += A[i];
289306

290307
return( 0 );
291308
}
@@ -403,26 +420,26 @@ int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
403420
| ( ctx->total[1] << 3 );
404421
low = ( ctx->total[0] << 3 );
405422

406-
PUT_UINT64_BE( high, ctx->buffer, 112 );
407-
PUT_UINT64_BE( low, ctx->buffer, 120 );
423+
sha512_put_uint64_be( high, ctx->buffer, 112 );
424+
sha512_put_uint64_be( low, ctx->buffer, 120 );
408425

409426
if( ( ret = mbedtls_internal_sha512_process( ctx, ctx->buffer ) ) != 0 )
410427
return( ret );
411428

412429
/*
413430
* Output final state
414431
*/
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 );
432+
sha512_put_uint64_be( ctx->state[0], output, 0 );
433+
sha512_put_uint64_be( ctx->state[1], output, 8 );
434+
sha512_put_uint64_be( ctx->state[2], output, 16 );
435+
sha512_put_uint64_be( ctx->state[3], output, 24 );
436+
sha512_put_uint64_be( ctx->state[4], output, 32 );
437+
sha512_put_uint64_be( ctx->state[5], output, 40 );
421438

422439
if( ctx->is384 == 0 )
423440
{
424-
PUT_UINT64_BE( ctx->state[6], output, 48 );
425-
PUT_UINT64_BE( ctx->state[7], output, 56 );
441+
sha512_put_uint64_be( ctx->state[6], output, 48 );
442+
sha512_put_uint64_be( ctx->state[7], output, 56 );
426443
}
427444

428445
return( 0 );

library/version_features.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ static const char * const features[] = {
408408
#if defined(MBEDTLS_SHA256_SMALLER)
409409
"MBEDTLS_SHA256_SMALLER",
410410
#endif /* MBEDTLS_SHA256_SMALLER */
411+
#if defined(MBEDTLS_SHA512_SMALLER)
412+
"MBEDTLS_SHA512_SMALLER",
413+
#endif /* MBEDTLS_SHA512_SMALLER */
411414
#if defined(MBEDTLS_THREADING_ALT)
412415
"MBEDTLS_THREADING_ALT",
413416
#endif /* MBEDTLS_THREADING_ALT */

programs/test/query_config.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,14 @@ int query_config( const char *config )
11161116
}
11171117
#endif /* MBEDTLS_SHA256_SMALLER */
11181118

1119+
#if defined(MBEDTLS_SHA512_SMALLER)
1120+
if( strcmp( "MBEDTLS_SHA512_SMALLER", config ) == 0 )
1121+
{
1122+
MACRO_EXPANSION_TO_STR( MBEDTLS_SHA512_SMALLER );
1123+
return( 0 );
1124+
}
1125+
#endif /* MBEDTLS_SHA512_SMALLER */
1126+
11191127
#if defined(MBEDTLS_THREADING_ALT)
11201128
if( strcmp( "MBEDTLS_THREADING_ALT", config ) == 0 )
11211129
{

0 commit comments

Comments
 (0)