Use MD-light in entropy.c

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
This commit is contained in:
Manuel Pégourié-Gonnard 2023-02-02 13:14:59 +01:00
parent ac6db4d649
commit 5cd4b6403b
6 changed files with 79 additions and 100 deletions

View file

@ -99,6 +99,7 @@
*/ */
#if defined(MBEDTLS_ECJPAKE_C) || \ #if defined(MBEDTLS_ECJPAKE_C) || \
defined(MBEDTLS_PEM_PARSE_C) || \ defined(MBEDTLS_PEM_PARSE_C) || \
defined(MBEDTLS_ENTROPY_C) || \
defined(MBEDTLS_PKCS12_C) || \ defined(MBEDTLS_PKCS12_C) || \
defined(MBEDTLS_RSA_C) defined(MBEDTLS_RSA_C)
#define MBEDTLS_MD_LIGHT #define MBEDTLS_MD_LIGHT

View file

@ -173,21 +173,27 @@
#error "MBEDTLS_PKCS5_C defined, but not all prerequisites" #error "MBEDTLS_PKCS5_C defined, but not all prerequisites"
#endif #endif
#if defined(MBEDTLS_ENTROPY_C) && (!defined(MBEDTLS_SHA512_C) && \ #if defined(MBEDTLS_ENTROPY_C) && \
!defined(MBEDTLS_SHA256_C)) !( defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA256_C) || \
(defined(MBEDTLS_PSA_CRYPTO_C) && \
(defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA_256))))
#error "MBEDTLS_ENTROPY_C defined, but not all prerequisites" #error "MBEDTLS_ENTROPY_C defined, but not all prerequisites"
#endif #endif
#if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_SHA512_C) && \ #if defined(MBEDTLS_ENTROPY_C) && \
defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) && (MBEDTLS_CTR_DRBG_ENTROPY_LEN > 64) defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) && (MBEDTLS_CTR_DRBG_ENTROPY_LEN > 64)
#error "MBEDTLS_CTR_DRBG_ENTROPY_LEN value too high" #error "MBEDTLS_CTR_DRBG_ENTROPY_LEN value too high"
#endif #endif
#if defined(MBEDTLS_ENTROPY_C) && \ #if defined(MBEDTLS_ENTROPY_C) && \
( !defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_ENTROPY_FORCE_SHA256) ) \ ( defined(MBEDTLS_ENTROPY_FORCE_SHA256) || \
!( defined(MBEDTLS_SHA512_C) || \
(defined(MBEDTLS_PSA_CRYPTO_C) && defined(PSA_WANT_ALG_SHA_512)) ) ) \
&& defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) && (MBEDTLS_CTR_DRBG_ENTROPY_LEN > 32) && defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) && (MBEDTLS_CTR_DRBG_ENTROPY_LEN > 32)
#error "MBEDTLS_CTR_DRBG_ENTROPY_LEN value too high" #error "MBEDTLS_CTR_DRBG_ENTROPY_LEN value too high"
#endif #endif
#if defined(MBEDTLS_ENTROPY_C) && \ #if defined(MBEDTLS_ENTROPY_C) && \
defined(MBEDTLS_ENTROPY_FORCE_SHA256) && !defined(MBEDTLS_SHA256_C) defined(MBEDTLS_ENTROPY_FORCE_SHA256) && \
!( defined(MBEDTLS_SHA256_C) || \
(defined(MBEDTLS_PSA_CRYPTO_C) && defined(PSA_WANT_ALG_SHA_256)) )
#error "MBEDTLS_ENTROPY_FORCE_SHA256 defined, but not all prerequisites" #error "MBEDTLS_ENTROPY_FORCE_SHA256 defined, but not all prerequisites"
#endif #endif

View file

@ -27,13 +27,17 @@
#include <stddef.h> #include <stddef.h>
#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) #include "md.h"
#include "mbedtls/sha512.h"
#if defined(MBEDTLS_MD_CAN_SHA512) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
#define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR #define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR
#define MBEDTLS_ENTROPY_MD MBEDTLS_MD_SHA512
#define MBEDTLS_ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
#else #else
#if defined(MBEDTLS_SHA256_C) #if defined(MBEDTLS_MD_CAN_SHA256)
#define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR #define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR
#include "mbedtls/sha256.h" #define MBEDTLS_ENTROPY_MD MBEDTLS_MD_SHA256
#define MBEDTLS_ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */
#endif #endif
#endif #endif
@ -71,12 +75,6 @@
/** \} name SECTION: Module settings */ /** \} name SECTION: Module settings */
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
#define MBEDTLS_ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
#else
#define MBEDTLS_ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */
#endif
#define MBEDTLS_ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */ #define MBEDTLS_ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */
#define MBEDTLS_ENTROPY_SOURCE_MANUAL MBEDTLS_ENTROPY_MAX_SOURCES #define MBEDTLS_ENTROPY_SOURCE_MANUAL MBEDTLS_ENTROPY_MAX_SOURCES
@ -120,11 +118,7 @@ typedef struct mbedtls_entropy_context {
int MBEDTLS_PRIVATE(accumulator_started); /* 0 after init. int MBEDTLS_PRIVATE(accumulator_started); /* 0 after init.
* 1 after the first update. * 1 after the first update.
* -1 after free. */ * -1 after free. */
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) mbedtls_md_context_t MBEDTLS_PRIVATE(accumulator);
mbedtls_sha512_context MBEDTLS_PRIVATE(accumulator);
#elif defined(MBEDTLS_ENTROPY_SHA256_ACCUMULATOR)
mbedtls_sha256_context MBEDTLS_PRIVATE(accumulator);
#endif
int MBEDTLS_PRIVATE(source_count); /* Number of entries used in source. */ int MBEDTLS_PRIVATE(source_count); /* Number of entries used in source. */
mbedtls_entropy_source_state MBEDTLS_PRIVATE(source)[MBEDTLS_ENTROPY_MAX_SOURCES]; mbedtls_entropy_source_state MBEDTLS_PRIVATE(source)[MBEDTLS_ENTROPY_MAX_SOURCES];
#if defined(MBEDTLS_THREADING_C) #if defined(MBEDTLS_THREADING_C)

View file

@ -34,9 +34,6 @@
#include "mbedtls/platform.h" #include "mbedtls/platform.h"
#include "mbedtls/platform.h"
#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */ #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
void mbedtls_entropy_init(mbedtls_entropy_context *ctx) void mbedtls_entropy_init(mbedtls_entropy_context *ctx)
@ -49,11 +46,7 @@ void mbedtls_entropy_init(mbedtls_entropy_context *ctx)
#endif #endif
ctx->accumulator_started = 0; ctx->accumulator_started = 0;
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) mbedtls_md_init(&ctx->accumulator);
mbedtls_sha512_init(&ctx->accumulator);
#else
mbedtls_sha256_init(&ctx->accumulator);
#endif
/* Reminder: Update ENTROPY_HAVE_STRONG in the test files /* Reminder: Update ENTROPY_HAVE_STRONG in the test files
* when adding more strong entropy sources here. */ * when adding more strong entropy sources here. */
@ -89,11 +82,7 @@ void mbedtls_entropy_free(mbedtls_entropy_context *ctx)
#if defined(MBEDTLS_THREADING_C) #if defined(MBEDTLS_THREADING_C)
mbedtls_mutex_free(&ctx->mutex); mbedtls_mutex_free(&ctx->mutex);
#endif #endif
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) mbedtls_md_free(&ctx->accumulator);
mbedtls_sha512_free(&ctx->accumulator);
#else
mbedtls_sha256_free(&ctx->accumulator);
#endif
#if defined(MBEDTLS_ENTROPY_NV_SEED) #if defined(MBEDTLS_ENTROPY_NV_SEED)
ctx->initial_entropy_run = 0; ctx->initial_entropy_run = 0;
#endif #endif
@ -150,15 +139,10 @@ static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id,
int ret = 0; int ret = 0;
if (use_len > MBEDTLS_ENTROPY_BLOCK_SIZE) { if (use_len > MBEDTLS_ENTROPY_BLOCK_SIZE) {
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) if ((ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_ENTROPY_MD),
if ((ret = mbedtls_sha512(data, len, tmp, 0)) != 0) { data, len, tmp)) != 0) {
goto cleanup; goto cleanup;
} }
#else
if ((ret = mbedtls_sha256(data, len, tmp, 0)) != 0) {
goto cleanup;
}
#endif
p = tmp; p = tmp;
use_len = MBEDTLS_ENTROPY_BLOCK_SIZE; use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
} }
@ -171,29 +155,22 @@ static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id,
* it is sufficient to start the accumulator here only because all calls to * it is sufficient to start the accumulator here only because all calls to
* gather entropy eventually execute this code. * gather entropy eventually execute this code.
*/ */
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) if (ctx->accumulator_started == 0) {
if (ctx->accumulator_started == 0 && ret = mbedtls_md_setup(&ctx->accumulator,
(ret = mbedtls_sha512_starts(&ctx->accumulator, 0)) != 0) { mbedtls_md_info_from_type(MBEDTLS_ENTROPY_MD), 0);
goto cleanup; if (ret != 0) {
} else { goto cleanup;
}
ret = mbedtls_md_starts(&ctx->accumulator);
if (ret != 0) {
goto cleanup;
}
ctx->accumulator_started = 1; ctx->accumulator_started = 1;
} }
if ((ret = mbedtls_sha512_update(&ctx->accumulator, header, 2)) != 0) { if ((ret = mbedtls_md_update(&ctx->accumulator, header, 2)) != 0) {
goto cleanup; goto cleanup;
} }
ret = mbedtls_sha512_update(&ctx->accumulator, p, use_len); ret = mbedtls_md_update(&ctx->accumulator, p, use_len);
#else
if (ctx->accumulator_started == 0 &&
(ret = mbedtls_sha256_starts(&ctx->accumulator, 0)) != 0) {
goto cleanup;
} else {
ctx->accumulator_started = 1;
}
if ((ret = mbedtls_sha256_update(&ctx->accumulator, header, 2)) != 0) {
goto cleanup;
}
ret = mbedtls_sha256_update(&ctx->accumulator, p, use_len);
#endif
cleanup: cleanup:
mbedtls_platform_zeroize(tmp, sizeof(tmp)); mbedtls_platform_zeroize(tmp, sizeof(tmp));
@ -354,62 +331,41 @@ int mbedtls_entropy_func(void *data, unsigned char *output, size_t len)
memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
/* /*
* Note that at this stage it is assumed that the accumulator was started * Note that at this stage it is assumed that the accumulator was started
* in a previous call to entropy_update(). If this is not guaranteed, the * in a previous call to entropy_update(). If this is not guaranteed, the
* code below will fail. * code below will fail.
*/ */
if ((ret = mbedtls_sha512_finish(&ctx->accumulator, buf)) != 0) { if ((ret = mbedtls_md_finish(&ctx->accumulator, buf)) != 0) {
goto exit; goto exit;
} }
/* /*
* Reset accumulator and counters and recycle existing entropy * Reset accumulator and counters and recycle existing entropy
*/ */
mbedtls_sha512_free(&ctx->accumulator); mbedtls_md_free(&ctx->accumulator);
mbedtls_sha512_init(&ctx->accumulator); mbedtls_md_init(&ctx->accumulator);
if ((ret = mbedtls_sha512_starts(&ctx->accumulator, 0)) != 0) { ret = mbedtls_md_setup(&ctx->accumulator,
mbedtls_md_info_from_type(MBEDTLS_ENTROPY_MD), 0);
if (ret != 0) {
goto exit; goto exit;
} }
if ((ret = mbedtls_sha512_update(&ctx->accumulator, buf, ret = mbedtls_md_starts(&ctx->accumulator);
MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) { if (ret != 0) {
goto exit;
}
if ((ret = mbedtls_md_update(&ctx->accumulator, buf,
MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
goto exit; goto exit;
} }
/* /*
* Perform second SHA-512 on entropy * Perform second hashing on entropy
*/ */
if ((ret = mbedtls_sha512(buf, MBEDTLS_ENTROPY_BLOCK_SIZE, if ((ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_ENTROPY_MD),
buf, 0)) != 0) { buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf)) != 0) {
goto exit; goto exit;
} }
#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
if ((ret = mbedtls_sha256_finish(&ctx->accumulator, buf)) != 0) {
goto exit;
}
/*
* Reset accumulator and counters and recycle existing entropy
*/
mbedtls_sha256_free(&ctx->accumulator);
mbedtls_sha256_init(&ctx->accumulator);
if ((ret = mbedtls_sha256_starts(&ctx->accumulator, 0)) != 0) {
goto exit;
}
if ((ret = mbedtls_sha256_update(&ctx->accumulator, buf,
MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
goto exit;
}
/*
* Perform second SHA-256 on entropy
*/
if ((ret = mbedtls_sha256(buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
buf, 0)) != 0) {
goto exit;
}
#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
for (i = 0; i < ctx->source_count; i++) { for (i = 0; i < ctx->source_count; i++) {
ctx->source[i].size = 0; ctx->source[i].size = 0;

View file

@ -167,6 +167,8 @@ void entropy_seed_file(char *path, int ret)
{ {
mbedtls_entropy_context ctx; mbedtls_entropy_context ctx;
MD_PSA_INIT();
mbedtls_entropy_init(&ctx); mbedtls_entropy_init(&ctx);
TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, path) == ret); TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, path) == ret);
@ -174,6 +176,7 @@ void entropy_seed_file(char *path, int ret)
exit: exit:
mbedtls_entropy_free(&ctx); mbedtls_entropy_free(&ctx);
MD_PSA_DONE();
} }
/* END_CASE */ /* END_CASE */
@ -182,6 +185,8 @@ void entropy_write_base_seed_file(int ret)
{ {
mbedtls_entropy_context ctx; mbedtls_entropy_context ctx;
MD_PSA_INIT();
mbedtls_entropy_init(&ctx); mbedtls_entropy_init(&ctx);
TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, MBEDTLS_PLATFORM_STD_NV_SEED_FILE) == ret); TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, MBEDTLS_PLATFORM_STD_NV_SEED_FILE) == ret);
@ -189,6 +194,7 @@ void entropy_write_base_seed_file(int ret)
exit: exit:
mbedtls_entropy_free(&ctx); mbedtls_entropy_free(&ctx);
MD_PSA_DONE();
} }
/* END_CASE */ /* END_CASE */
@ -243,6 +249,8 @@ void entropy_func_len(int len, int ret)
unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 }; unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
size_t i, j; size_t i, j;
MD_PSA_INIT();
mbedtls_entropy_init(&ctx); mbedtls_entropy_init(&ctx);
/* /*
@ -267,6 +275,7 @@ void entropy_func_len(int len, int ret)
exit: exit:
mbedtls_entropy_free(&ctx); mbedtls_entropy_free(&ctx);
MD_PSA_DONE();
} }
/* END_CASE */ /* END_CASE */
@ -277,6 +286,8 @@ void entropy_source_fail(char *path)
unsigned char buf[16]; unsigned char buf[16];
entropy_dummy_context dummy = { DUMMY_FAIL, 0, 0 }; entropy_dummy_context dummy = { DUMMY_FAIL, 0, 0 };
MD_PSA_INIT();
mbedtls_entropy_init(&ctx); mbedtls_entropy_init(&ctx);
TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source, TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
@ -299,6 +310,7 @@ void entropy_source_fail(char *path)
exit: exit:
mbedtls_entropy_free(&ctx); mbedtls_entropy_free(&ctx);
MD_PSA_DONE();
} }
/* END_CASE */ /* END_CASE */
@ -312,6 +324,8 @@ void entropy_threshold(int threshold, int chunk_size, int result)
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 }; unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
int ret; int ret;
MD_PSA_INIT();
mbedtls_entropy_init(&ctx); mbedtls_entropy_init(&ctx);
entropy_clear_sources(&ctx); entropy_clear_sources(&ctx);
@ -340,6 +354,7 @@ void entropy_threshold(int threshold, int chunk_size, int result)
exit: exit:
mbedtls_entropy_free(&ctx); mbedtls_entropy_free(&ctx);
MD_PSA_DONE();
} }
/* END_CASE */ /* END_CASE */
@ -359,6 +374,8 @@ void entropy_calls(int strength1, int strength2,
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 }; unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
int ret; int ret;
MD_PSA_INIT();
mbedtls_entropy_init(&ctx); mbedtls_entropy_init(&ctx);
entropy_clear_sources(&ctx); entropy_clear_sources(&ctx);
@ -385,6 +402,7 @@ void entropy_calls(int strength1, int strength2,
exit: exit:
mbedtls_entropy_free(&ctx); mbedtls_entropy_free(&ctx);
MD_PSA_DONE();
} }
/* END_CASE */ /* END_CASE */
@ -455,6 +473,8 @@ void entropy_nv_seed(data_t *read_seed)
unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE]; unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
unsigned char check_entropy[MBEDTLS_ENTROPY_BLOCK_SIZE]; unsigned char check_entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
MD_PSA_INIT();
memset(entropy, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); memset(entropy, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
memset(empty, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); memset(empty, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
@ -523,12 +543,18 @@ exit:
mbedtls_entropy_free(&ctx); mbedtls_entropy_free(&ctx);
mbedtls_nv_seed_read = original_mbedtls_nv_seed_read; mbedtls_nv_seed_read = original_mbedtls_nv_seed_read;
mbedtls_nv_seed_write = original_mbedtls_nv_seed_write; mbedtls_nv_seed_write = original_mbedtls_nv_seed_write;
MD_PSA_DONE();
} }
/* END_CASE */ /* END_CASE */
/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG:MBEDTLS_SELF_TEST */ /* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG:MBEDTLS_SELF_TEST */
void entropy_selftest(int result) void entropy_selftest(int result)
{ {
MD_PSA_INIT();
TEST_ASSERT(mbedtls_entropy_self_test(1) == result); TEST_ASSERT(mbedtls_entropy_self_test(1) == result);
exit:
MD_PSA_DONE();
} }
/* END_CASE */ /* END_CASE */

View file

@ -74,11 +74,7 @@ static void custom_entropy_init(mbedtls_entropy_context *ctx)
#endif #endif
ctx->accumulator_started = 0; ctx->accumulator_started = 0;
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) mbedtls_md_init(&ctx->accumulator);
mbedtls_sha512_init(&ctx->accumulator);
#else
mbedtls_sha256_init(&ctx->accumulator);
#endif
#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY) #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
if (custom_entropy_sources_mask & ENTROPY_SOURCE_PLATFORM) { if (custom_entropy_sources_mask & ENTROPY_SOURCE_PLATFORM) {