Apply feedback from @RonEld

* Use _C flags at compile time in SHA to avoid compiling in unconfigured features
* Don't define ECP_SHORTWEIERSTRASS since it is part of the application's configuration
pull/4825/head
Steven Cooreman 2017-08-02 16:03:29 +02:00
parent 1e51dfa302
commit e07ab46f32
2 changed files with 14 additions and 3 deletions

View File

@ -40,7 +40,7 @@
#include "mbedtls/sha1.h" #include "mbedtls/sha1.h"
#include "mbedtls/sha256.h" #include "mbedtls/sha256.h"
#if defined(MBEDTLS_SHA256_ALT) || defined(MBEDTLS_SHA1_ALT) #if ( defined(MBEDTLS_SHA256_ALT) && defined(MBEDTLS_SHA256_C) ) || ( defined(MBEDTLS_SHA1_ALT) && defined (MBEDTLS_SHA1_C) )
#include "em_device.h" #include "em_device.h"
@ -51,6 +51,7 @@
#include "em_assert.h" #include "em_assert.h"
#include <string.h> #include <string.h>
#if defined(MBEDTLS_SHA1_C)
static const uint32_t init_state_sha1[8] = static const uint32_t init_state_sha1[8] =
{ {
0x67452301UL, 0x67452301UL,
@ -62,7 +63,9 @@ static const uint32_t init_state_sha1[8] =
0x0UL, 0x0UL,
0x0UL 0x0UL
}; };
#endif /* defined(MBEDTLS_SHA1_C) */
#if defined(MBEDTLS_SHA256_C)
static const uint32_t init_state_sha256[8] = static const uint32_t init_state_sha256[8] =
{ {
0x6A09E667UL, 0x6A09E667UL,
@ -86,6 +89,7 @@ static const uint32_t init_state_sha224[8] =
0x64F98FA7UL, 0x64F98FA7UL,
0xBEFA4FA4UL 0xBEFA4FA4UL
}; };
#endif /* defined(MBEDTLS_SHA256_C) */
static const unsigned char sha_padding[64] = static const unsigned char sha_padding[64] =
{ {
@ -96,8 +100,12 @@ static const unsigned char sha_padding[64] =
}; };
typedef enum { typedef enum {
#if defined(MBEDTLS_SHA1_C)
CRYPTO_SHA1, CRYPTO_SHA1,
#endif /* defined(MBEDTLS_SHA1_C) */
#if defined(MBEDTLS_SHA256_C)
CRYPTO_SHA2 CRYPTO_SHA2
#endif /* defined(MBEDTLS_SHA256_C) */
} crypto_sha_mode_t; } crypto_sha_mode_t;
/* /*
@ -131,12 +139,16 @@ static void crypto_sha_update_state( uint32_t state[8],
CRYPTO_TypeDef *crypto = crypto_management_acquire(); CRYPTO_TypeDef *crypto = crypto_management_acquire();
switch ( mode ) { switch ( mode ) {
#if defined(MBEDTLS_SHA1_C)
case CRYPTO_SHA1: case CRYPTO_SHA1:
crypto->CTRL = CRYPTO_CTRL_SHA_SHA1; crypto->CTRL = CRYPTO_CTRL_SHA_SHA1;
break; break;
#endif /* defined(MBEDTLS_SHA1_C) */
#if defined(MBEDTLS_SHA256_C)
case CRYPTO_SHA2: case CRYPTO_SHA2:
crypto->CTRL = CRYPTO_CTRL_SHA_SHA2; crypto->CTRL = CRYPTO_CTRL_SHA_SHA2;
break; break;
#endif /* defined(MBEDTLS_SHA256_C) */
} }
crypto->WAC = 0; crypto->WAC = 0;
@ -446,4 +458,4 @@ void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
#endif /* #if defined(CRYPTO_PRESENT) */ #endif /* #if defined(CRYPTO_PRESENT) */
#endif /* #if defined(MBEDTLS_SHA256_ALT) || defined(MBEDTLS_SHA1_ALT) */ #endif /* #if ( defined(MBEDTLS_SHA256_ALT) && defined(MBEDTLS_SHA256_C) ) || ( defined(MBEDTLS_SHA1_ALT) && defined (MBEDTLS_SHA1_C) ) */

View File

@ -29,7 +29,6 @@
#define MBEDTLS_AES_ALT #define MBEDTLS_AES_ALT
#define MBEDTLS_ECP_INTERNAL_ALT #define MBEDTLS_ECP_INTERNAL_ALT
#define ECP_SHORTWEIERSTRASS
#define MBEDTLS_ECP_ADD_MIXED_ALT #define MBEDTLS_ECP_ADD_MIXED_ALT
#define MBEDTLS_ECP_DOUBLE_JAC_ALT #define MBEDTLS_ECP_DOUBLE_JAC_ALT
#define MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT #define MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT