Skip to content

Commit f712e16

Browse files
authored
Merge pull request #179 from mpg/sha512-no-sha384
Add option to build SHA-512 without SHA-384
2 parents 8b38978 + 74ca84a commit f712e16

23 files changed

+451
-333
lines changed

include/mbedtls/check_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,10 @@
510510
#error "MBEDTLS_RSA_C defined, but none of the PKCS1 versions enabled"
511511
#endif
512512

513+
#if defined(MBEDTLS_SHA512_NO_SHA384) && !defined(MBEDTLS_SHA512_C)
514+
#error "MBEDTLS_SHA512_NO_SHA384 defined without MBEDTLS_SHA512_C"
515+
#endif
516+
513517
#if defined(MBEDTLS_THREADING_PTHREAD)
514518
#if !defined(MBEDTLS_THREADING_C) || defined(MBEDTLS_THREADING_IMPL)
515519
#error "MBEDTLS_THREADING_PTHREAD defined, but not all prerequisites"

include/mbedtls/config.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,18 @@
10331033
*/
10341034
//#define MBEDTLS_SHA512_SMALLER
10351035

1036+
/**
1037+
* \def MBEDTLS_SHA512_NO_SHA384
1038+
*
1039+
* Disable the SHA-384 option of the SHA-512 module. Use this to save some
1040+
* code size on devices that don't use SHA-384.
1041+
*
1042+
* Requires: MBEDTLS_SHA512_C
1043+
*
1044+
* Uncomment to disable SHA-384
1045+
*/
1046+
//#define MBEDTLS_SHA512_NO_SHA384
1047+
10361048
/**
10371049
* \def MBEDTLS_THREADING_ALT
10381050
*

include/mbedtls/md_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ extern const mbedtls_md_info_t mbedtls_sha224_info;
7979
extern const mbedtls_md_info_t mbedtls_sha256_info;
8080
#endif
8181
#if defined(MBEDTLS_SHA512_C)
82+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
8283
extern const mbedtls_md_info_t mbedtls_sha384_info;
84+
#endif
8385
extern const mbedtls_md_info_t mbedtls_sha512_info;
8486
#endif
8587

include/mbedtls/sha512.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ typedef struct mbedtls_sha512_context
5959
uint64_t total[2]; /*!< The number of Bytes processed. */
6060
uint64_t state[8]; /*!< The intermediate digest state. */
6161
unsigned char buffer[128]; /*!< The data block being processed. */
62+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
6263
int is384; /*!< Determines which function to use:
6364
0: Use SHA-512, or 1: Use SHA-384. */
65+
#endif
6466
}
6567
mbedtls_sha512_context;
6668

@@ -101,7 +103,11 @@ void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
101103
*
102104
* \param ctx The SHA-512 context to use. This must be initialized.
103105
* \param is384 Determines which function to use. This must be
104-
* either \c for SHA-512, or \c 1 for SHA-384.
106+
* either \c 0 for SHA-512, or \c 1 for SHA-384.
107+
*
108+
* \note When \c MBEDTLS_SHA512_NO_SHA384 is defined, \p is384 must
109+
* be \c 0, or the function will return
110+
* #MBEDTLS_ERR_SHA512_BAD_INPUT_DATA.
105111
*
106112
* \return \c 0 on success.
107113
* \return A negative error code on failure.
@@ -169,6 +175,9 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
169175
* \param ctx The SHA-512 context to use. This must be initialized.
170176
* \param is384 Determines which function to use. This must be either
171177
* \c 0 for SHA-512 or \c 1 for SHA-384.
178+
*
179+
* \note When \c MBEDTLS_SHA512_NO_SHA384 is defined, \p is384 must
180+
* be \c 0, or the function will fail to work.
172181
*/
173182
MBEDTLS_DEPRECATED void mbedtls_sha512_starts( mbedtls_sha512_context *ctx,
174183
int is384 );
@@ -239,6 +248,10 @@ MBEDTLS_DEPRECATED void mbedtls_sha512_process(
239248
* \param is384 Determines which function to use. This must be either
240249
* \c 0 for SHA-512, or \c 1 for SHA-384.
241250
*
251+
* \note When \c MBEDTLS_SHA512_NO_SHA384 is defined, \p is384 must
252+
* be \c 0, or the function will return
253+
* #MBEDTLS_ERR_SHA512_BAD_INPUT_DATA.
254+
*
242255
* \return \c 0 on success.
243256
* \return A negative error code on failure.
244257
*/
@@ -273,6 +286,9 @@ int mbedtls_sha512_ret( const unsigned char *input,
273286
* be a writable buffer of length \c 64 Bytes.
274287
* \param is384 Determines which function to use. This must be either
275288
* \c 0 for SHA-512, or \c 1 for SHA-384.
289+
*
290+
* \note When \c MBEDTLS_SHA512_NO_SHA384 is defined, \p is384 must
291+
* be \c 0, or the function will fail to work.
276292
*/
277293
MBEDTLS_DEPRECATED void mbedtls_sha512( const unsigned char *input,
278294
size_t ilen,

library/md.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,14 @@ const mbedtls_md_info_t mbedtls_sha256_info = {
120120
#endif
121121

122122
#if defined(MBEDTLS_SHA512_C)
123+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
123124
const mbedtls_md_info_t mbedtls_sha384_info = {
124125
"SHA384",
125126
MBEDTLS_MD_SHA384,
126127
48,
127128
128,
128129
};
130+
#endif
129131

130132
const mbedtls_md_info_t mbedtls_sha512_info = {
131133
"SHA512",
@@ -142,8 +144,10 @@ static const int supported_digests[] = {
142144

143145
#if defined(MBEDTLS_SHA512_C)
144146
MBEDTLS_MD_SHA512,
147+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
145148
MBEDTLS_MD_SHA384,
146149
#endif
150+
#endif
147151

148152
#if defined(MBEDTLS_SHA256_C)
149153
MBEDTLS_MD_SHA256,
@@ -211,8 +215,10 @@ const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
211215
return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
212216
#endif
213217
#if defined(MBEDTLS_SHA512_C)
218+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
214219
if( !strcmp( "SHA384", md_name ) )
215220
return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
221+
#endif
216222
if( !strcmp( "SHA512", md_name ) )
217223
return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
218224
#endif
@@ -250,8 +256,10 @@ const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
250256
return( &mbedtls_sha256_info );
251257
#endif
252258
#if defined(MBEDTLS_SHA512_C)
259+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
253260
case MBEDTLS_MD_SHA384:
254261
return( &mbedtls_sha384_info );
262+
#endif
255263
case MBEDTLS_MD_SHA512:
256264
return( &mbedtls_sha512_info );
257265
#endif
@@ -306,7 +314,9 @@ void mbedtls_md_free( mbedtls_md_context_t *ctx )
306314
break;
307315
#endif
308316
#if defined(MBEDTLS_SHA512_C)
317+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
309318
case MBEDTLS_MD_SHA384:
319+
#endif
310320
case MBEDTLS_MD_SHA512:
311321
mbedtls_sha512_free( ctx->md_ctx );
312322
break;
@@ -372,7 +382,9 @@ int mbedtls_md_clone( mbedtls_md_context_t *dst,
372382
break;
373383
#endif
374384
#if defined(MBEDTLS_SHA512_C)
385+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
375386
case MBEDTLS_MD_SHA384:
387+
#endif
376388
case MBEDTLS_MD_SHA512:
377389
mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
378390
break;
@@ -439,7 +451,9 @@ int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_inf
439451
break;
440452
#endif
441453
#if defined(MBEDTLS_SHA512_C)
454+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
442455
case MBEDTLS_MD_SHA384:
456+
#endif
443457
case MBEDTLS_MD_SHA512:
444458
ALLOC( sha512 );
445459
break;
@@ -498,8 +512,10 @@ int mbedtls_md_starts( mbedtls_md_context_t *ctx )
498512
return( mbedtls_sha256_starts_ret( ctx->md_ctx, 0 ) );
499513
#endif
500514
#if defined(MBEDTLS_SHA512_C)
515+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
501516
case MBEDTLS_MD_SHA384:
502517
return( mbedtls_sha512_starts_ret( ctx->md_ctx, 1 ) );
518+
#endif
503519
case MBEDTLS_MD_SHA512:
504520
return( mbedtls_sha512_starts_ret( ctx->md_ctx, 0 ) );
505521
#endif
@@ -542,8 +558,10 @@ int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, si
542558
return( mbedtls_sha256_update_ret( ctx->md_ctx, input, ilen ) );
543559
#endif
544560
#if defined(MBEDTLS_SHA512_C)
561+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
545562
case MBEDTLS_MD_SHA384:
546563
return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
564+
#endif
547565
case MBEDTLS_MD_SHA512:
548566
return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
549567
#endif
@@ -586,8 +604,10 @@ int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
586604
return( mbedtls_sha256_finish_ret( ctx->md_ctx, output ) );
587605
#endif
588606
#if defined(MBEDTLS_SHA512_C)
607+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
589608
case MBEDTLS_MD_SHA384:
590609
return( mbedtls_sha512_finish_ret( ctx->md_ctx, output ) );
610+
#endif
591611
case MBEDTLS_MD_SHA512:
592612
return( mbedtls_sha512_finish_ret( ctx->md_ctx, output ) );
593613
#endif
@@ -631,8 +651,10 @@ int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, si
631651
return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
632652
#endif
633653
#if defined(MBEDTLS_SHA512_C)
654+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
634655
case MBEDTLS_MD_SHA384:
635656
return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
657+
#endif
636658
case MBEDTLS_MD_SHA512:
637659
return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
638660
#endif
@@ -839,8 +861,10 @@ int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
839861
return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
840862
#endif
841863
#if defined(MBEDTLS_SHA512_C)
864+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
842865
case MBEDTLS_MD_SHA384:
843866
return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
867+
#endif
844868
case MBEDTLS_MD_SHA512:
845869
return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
846870
#endif

library/psa_crypto.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,8 +2037,10 @@ static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
20372037
return( &mbedtls_sha256_info );
20382038
#endif
20392039
#if defined(MBEDTLS_SHA512_C)
2040+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
20402041
case PSA_ALG_SHA_384:
20412042
return( &mbedtls_sha384_info );
2043+
#endif
20422044
case PSA_ALG_SHA_512:
20432045
return( &mbedtls_sha512_info );
20442046
#endif
@@ -2089,7 +2091,9 @@ psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
20892091
break;
20902092
#endif
20912093
#if defined(MBEDTLS_SHA512_C)
2094+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
20922095
case PSA_ALG_SHA_384:
2096+
#endif
20932097
case PSA_ALG_SHA_512:
20942098
mbedtls_sha512_free( &operation->ctx.sha512 );
20952099
break;
@@ -2155,10 +2159,12 @@ psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
21552159
break;
21562160
#endif
21572161
#if defined(MBEDTLS_SHA512_C)
2162+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
21582163
case PSA_ALG_SHA_384:
21592164
mbedtls_sha512_init( &operation->ctx.sha512 );
21602165
ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
21612166
break;
2167+
#endif
21622168
case PSA_ALG_SHA_512:
21632169
mbedtls_sha512_init( &operation->ctx.sha512 );
21642170
ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
@@ -2227,7 +2233,9 @@ psa_status_t psa_hash_update( psa_hash_operation_t *operation,
22272233
break;
22282234
#endif
22292235
#if defined(MBEDTLS_SHA512_C)
2236+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
22302237
case PSA_ALG_SHA_384:
2238+
#endif
22312239
case PSA_ALG_SHA_512:
22322240
ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
22332241
input, input_length );
@@ -2300,7 +2308,9 @@ psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
23002308
break;
23012309
#endif
23022310
#if defined(MBEDTLS_SHA512_C)
2311+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
23032312
case PSA_ALG_SHA_384:
2313+
#endif
23042314
case PSA_ALG_SHA_512:
23052315
ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
23062316
break;
@@ -2389,7 +2399,9 @@ psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
23892399
break;
23902400
#endif
23912401
#if defined(MBEDTLS_SHA512_C)
2402+
#if !defined(MBEDTLS_SHA512_NO_SHA384)
23922403
case PSA_ALG_SHA_384:
2404+
#endif
23932405
case PSA_ALG_SHA_512:
23942406
mbedtls_sha512_clone( &target_operation->ctx.sha512,
23952407
&source_operation->ctx.sha512 );

0 commit comments

Comments
 (0)