From d4ee197c6c81798f5df07ca130b5c440994206b7 Mon Sep 17 00:00:00 2001 From: Paul Adelsbach Date: Tue, 24 Nov 2026 14:57:46 -0800 Subject: [PATCH 1/2] Add Mbed TLS 3.6 ctr_drbg, entropy and ssl_conf_rng --- docs/mbedtls-compatibility.md | 23 +- src/mbedtls/error.c | 18 +- src/mbedtls/mbedtls3.c | 380 +++++++++++++++++++++- test/mbedtls_compat/mbedtls3_test.c | 362 +++++++++++++++++++++ tools/psa_want_probe.c | 7 +- wolfpsa.map | 2 + wolfpsa/mbedtls_compat/mbedtls/ctr_drbg.h | 81 +++++ wolfpsa/mbedtls_compat/mbedtls/entropy.h | 90 +++++ wolfpsa/mbedtls_compat/mbedtls/ssl.h | 4 + 9 files changed, 961 insertions(+), 6 deletions(-) create mode 100644 wolfpsa/mbedtls_compat/mbedtls/ctr_drbg.h create mode 100644 wolfpsa/mbedtls_compat/mbedtls/entropy.h diff --git a/docs/mbedtls-compatibility.md b/docs/mbedtls-compatibility.md index ce6fb53b..01194191 100644 --- a/docs/mbedtls-compatibility.md +++ b/docs/mbedtls-compatibility.md @@ -117,6 +117,10 @@ Mbed TLS 3.6, with `WOLFPSA_MBEDTLS_3`: `MBEDTLS_SSL_MAJOR_VERSION_3`, `MBEDTLS_SSL_MINOR_VERSION_3` and `MBEDTLS_SSL_MINOR_VERSION_4`. As in 3.6, `MBEDTLS_DEPRECATED_WARNING` warns on them and `MBEDTLS_DEPRECATED_REMOVED` removes them. +- `mbedtls_ssl_conf_rng` +- `mbedtls_ctr_drbg_*`: init, free, seed, random, reseed, + set_prediction_resistance, on wolfCrypt's RNG +- `mbedtls_entropy_*`: init, free, add_source, func TF-PSA-Crypto, in every build: @@ -182,7 +186,6 @@ Other: Mbed TLS 3.6: -- `mbedtls_ssl_conf_rng`, `mbedtls/ctr_drbg.h` and `mbedtls/entropy.h` - The `f_rng` and `p_rng` arguments of `mbedtls_pk_parse_key` and `mbedtls_pk_parse_keyfile` - `mbedtls_pk_ec`, `mbedtls_pk_rsa`, `mbedtls_ssl_conf_curves` and the @@ -370,6 +373,24 @@ Mbed TLS 3.6: such as `MBEDTLS_ERR_SSL_BAD_INPUT_DATA`, keep those values, so that one library serves both APIs. Code that compares with the names works, but the numbers differ from 3.6 and `mbedtls_strerror` reports them as unknown. +- `mbedtls_ssl_conf_rng` is accepted and ignored: wolfSSL draws its own + random numbers. +- `mbedtls_ctr_drbg_*` use wolfCrypt's RNG, a hash DRBG, not an AES + CTR_DRBG. wolfCrypt seeds it from its own seed source and adds the entropy + function's output and the custom string. So the same entropy, for example + a fixed one in a test, gives other output each time, where Mbed TLS gives + the same. `mbedtls_ctr_drbg_random` and `mbedtls_ctr_drbg_reseed` on a + context that is not seeded return + `MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED`. They need wolfCrypt's hash + DRBG, so they are not built with `WC_NO_HASHDRBG`, or with + `CUSTOM_RAND_GENERATE_BLOCK` or `HAVE_INTEL_RDRAND`, which bypass it. +- `mbedtls_entropy_init` adds one source, wolfCrypt's RNG, in place of Mbed + TLS's platform, hardware and NV seed sources. Sources from + `mbedtls_entropy_add_source` are polled as in Mbed TLS. Each + `mbedtls_entropy_func` output is a hash of what the sources gave for it; + Mbed TLS also keeps what they gave for earlier outputs. The hash is + SHA-512, or SHA-256 without it, through wolfCrypt's hash wrapper, so + `mbedtls_entropy_*` are not built with `NO_HASH_WRAPPER` or without both. ## Tests diff --git a/src/mbedtls/error.c b/src/mbedtls/error.c index 2fd21fb4..b53eb734 100644 --- a/src/mbedtls/error.c +++ b/src/mbedtls/error.c @@ -21,6 +21,11 @@ /* Mbed TLS 4.x error strings (MBEDTLS_COMPAT=1). */ +/* For the 3.6-only codes too, as one mbedtls_strerror serves both APIs */ +#ifndef WOLFPSA_MBEDTLS_3 + #define WOLFPSA_MBEDTLS_3 +#endif /* !WOLFPSA_MBEDTLS_3 */ + #ifdef HAVE_CONFIG_H #include #endif /* HAVE_CONFIG_H */ @@ -32,6 +37,8 @@ #include #include #include +#include +#include #include #include #include @@ -62,7 +69,16 @@ static const struct { "Actual length differs from expected length"), MBED_ERR(ASN1, INVALID_DATA, "Data is invalid"), MBED_ERR(BASE64, INVALID_CHARACTER, "Invalid character in input"), - MBED_ERR(MD, FEATURE_UNAVAILABLE, "The selected feature is not available"), + MBED_ERR(CTR_DRBG, ENTROPY_SOURCE_FAILED, "The entropy source failed"), + MBED_ERR(CTR_DRBG, REQUEST_TOO_BIG, + "The requested random buffer length is too big"), + MBED_ERR(CTR_DRBG, INPUT_TOO_BIG, + "The input (entropy + additional data) is too large"), + MBED_ERR(ENTROPY, SOURCE_FAILED, "Critical entropy source failure"), + MBED_ERR(ENTROPY, MAX_SOURCES, "No more sources can be added"), + MBED_ERR(ENTROPY, NO_SOURCES_DEFINED, + "No sources have been added to poll"), + MBED_ERR(MD,FEATURE_UNAVAILABLE, "The selected feature is not available"), MBED_ERR(NET, SOCKET_FAILED, "Failed to open a socket"), MBED_ERR(NET, CONNECT_FAILED, "The connection to the given server / port failed"), diff --git a/src/mbedtls/mbedtls3.c b/src/mbedtls/mbedtls3.c index 50c8b40b..13f8bcdc 100644 --- a/src/mbedtls/mbedtls3.c +++ b/src/mbedtls/mbedtls3.c @@ -19,9 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ -/* Mbed TLS 3.6 names that 4.x removed (MBEDTLS_COMPAT=1), for applications - * built with WOLFPSA_MBEDTLS_3. Defined here before any include, so the 3.6 - * declarations are checked against these functions. */ +/* Mbed TLS 3.6 names that 4.x removed or made private (MBEDTLS_COMPAT=1), for + * applications built with WOLFPSA_MBEDTLS_3. Defined here before any include, + * so the 3.6 declarations are checked against these functions. */ #ifndef WOLFPSA_MBEDTLS_3 #define WOLFPSA_MBEDTLS_3 @@ -35,9 +35,21 @@ #if defined(WOLFSSL_PSA_ENGINE) && defined(WOLFPSA_MBEDTLS_COMPAT) +#include #include +#include +#include +#include #include +#ifdef MBEDTLS_THREADING_C +#define MBED_LOCK(m) mbedtls_mutex_lock(m) +#define MBED_UNLOCK(m) (void)mbedtls_mutex_unlock(m) +#else +#define MBED_LOCK(m) 0 +#define MBED_UNLOCK(m) WC_DO_NOTHING +#endif /* MBEDTLS_THREADING_C */ + #if defined(MBEDTLS_SSL_TLS_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) /* By the bytes of the TLS version */ void mbedtls_ssl_conf_min_version(mbedtls_ssl_config* conf, int major, @@ -59,4 +71,366 @@ void mbedtls_ssl_conf_max_version(mbedtls_ssl_config* conf, int major, } #endif /* MBEDTLS_SSL_TLS_C && !MBEDTLS_DEPRECATED_REMOVED */ +#ifdef MBEDTLS_SSL_TLS_C +/* wolfSSL draws its own random numbers */ +void mbedtls_ssl_conf_rng(mbedtls_ssl_config* conf, + int (*f_rng)(void*, unsigned char*, size_t), void* p_rng) +{ + WOLFSSL_ENTER("mbedtls_ssl_conf_rng"); + + (void)conf; + (void)f_rng; + (void)p_rng; +} +#endif /* MBEDTLS_SSL_TLS_C */ + +#ifdef MBEDTLS_ENTROPY_C +#ifdef MBEDTLS_ENTROPY_SHA512_ACCUMULATOR +#define MBED_ENTROPY_HASH WC_HASH_TYPE_SHA512 +#else +#define MBED_ENTROPY_HASH WC_HASH_TYPE_SHA256 +#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */ +/* As Mbed TLS: polls before failing, and the platform source's threshold */ +#define MBED_ENTROPY_MAX_LOOP 256 +#define MBED_ENTROPY_MIN_DEFAULT 32 + +/* The default source, in place of Mbed TLS's platform source */ +static int MbedEntropyDefault(void* data, unsigned char* output, size_t len, + size_t* olen) +{ + WC_RNG rng; + int ret; + + (void)data; + ret = wc_InitRng_ex(&rng, NULL, wolfPSA_GetDefaultDevID()); + if (ret == 0) { + ret = wc_RNG_GenerateBlock(&rng, output, (word32)len); + (void)wc_FreeRng(&rng); + } + if (ret != 0) { + return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; + } + *olen = len; + return 0; +} + +void mbedtls_entropy_init(mbedtls_entropy_context* ctx) +{ + WOLFSSL_ENTER("mbedtls_entropy_init"); + + if (ctx != NULL) { + XMEMSET(ctx, 0, sizeof(*ctx)); +#ifdef MBEDTLS_THREADING_C + mbedtls_mutex_init(&ctx->mutex); +#endif /* MBEDTLS_THREADING_C */ + (void)mbedtls_entropy_add_source(ctx, MbedEntropyDefault, NULL, + MBED_ENTROPY_MIN_DEFAULT, MBEDTLS_ENTROPY_SOURCE_STRONG); + } +} + +void mbedtls_entropy_free(mbedtls_entropy_context* ctx) +{ + WOLFSSL_ENTER("mbedtls_entropy_free"); + + if (ctx != NULL) { +#ifdef MBEDTLS_THREADING_C + mbedtls_mutex_free(&ctx->mutex); +#endif /* MBEDTLS_THREADING_C */ + wc_ForceZero(ctx, sizeof(*ctx)); + } +} + +int mbedtls_entropy_add_source(mbedtls_entropy_context* ctx, + mbedtls_entropy_f_source_ptr f_source, void* p_source, size_t threshold, + int strong) +{ + mbedtls_entropy_source_state* src; + int ret; + + WOLFSSL_ENTER("mbedtls_entropy_add_source"); + + if (ctx == NULL || f_source == NULL) { + return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; + } + ret = MBED_LOCK(&ctx->mutex); + if (ret == 0) { + if (ctx->source_count >= MBEDTLS_ENTROPY_MAX_SOURCES) { + ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES; + } + else { + src = &ctx->source[ctx->source_count++]; + src->f_source = f_source; + src->p_source = p_source; + src->size = 0; + src->threshold = threshold; + src->strong = strong; + } + MBED_UNLOCK(&ctx->mutex); + } + return ret; +} + +/* Polls each source once. done: thresholds met, a block from strong ones */ +static int MbedEntropyGather(mbedtls_entropy_context* ctx, wc_HashAlg* hash, + int* done) +{ + mbedtls_entropy_source_state* src; + byte buf[MBEDTLS_ENTROPY_MAX_GATHER]; + byte header[2]; + size_t strong = 0; + size_t olen; + int ret = 0; + int i; + + *done = 1; + for (i = 0; ret == 0 && i < ctx->source_count; i++) { + src = &ctx->source[i]; + olen = 0; + ret = src->f_source(src->p_source, buf, sizeof(buf), &olen); + if (ret == 0 && olen > sizeof(buf)) { + ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; + } + if (ret == 0 && olen > 0) { + /* As Mbed TLS: the source and length before the data */ + header[0] = (byte)i; + header[1] = (byte)olen; + if (wc_HashUpdate(hash, MBED_ENTROPY_HASH, header, + sizeof(header)) != 0 || + wc_HashUpdate(hash, MBED_ENTROPY_HASH, buf, + (word32)olen) != 0) { + ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; + } + src->size += olen; + } + if (src->size < src->threshold) { + *done = 0; + } + if (src->strong == MBEDTLS_ENTROPY_SOURCE_STRONG) { + strong += src->size; + } + } + if (strong < MBEDTLS_ENTROPY_BLOCK_SIZE) { + *done = 0; + } + wc_ForceZero(buf, sizeof(buf)); + return ret; +} + +int mbedtls_entropy_func(void* data, unsigned char* output, size_t len) +{ + mbedtls_entropy_context* ctx = (mbedtls_entropy_context*)data; + wc_HashAlg hash; + byte digest[MBEDTLS_ENTROPY_BLOCK_SIZE]; + int rounds = 0; + int done = 0; + int ret; + int i; + + WOLFSSL_ENTER("mbedtls_entropy_func"); + + if (ctx == NULL || (output == NULL && len > 0) || len > sizeof(digest)) { + return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; + } + ret = MBED_LOCK(&ctx->mutex); + if (ret != 0) { + return ret; + } + if (ctx->source_count == 0) { + ret = MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED; + } + else if (wc_HashInit_ex(&hash, MBED_ENTROPY_HASH, NULL, + wolfPSA_GetDefaultDevID()) != 0) { + ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; + } + else { + while (ret == 0 && !done) { + if (rounds++ == MBED_ENTROPY_MAX_LOOP) { + ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; + } + else { + ret = MbedEntropyGather(ctx, &hash, &done); + } + } + if (ret == 0 && wc_HashFinal(&hash, MBED_ENTROPY_HASH, digest) != 0) { + ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; + } + (void)wc_HashFree(&hash, MBED_ENTROPY_HASH); + } + if (ret == 0 && len > 0) { + XMEMCPY(output, digest, len); + } + for (i = 0; i < ctx->source_count; i++) { + ctx->source[i].size = 0; + } + wc_ForceZero(digest, sizeof(digest)); + MBED_UNLOCK(&ctx->mutex); + return ret; +} +#endif /* MBEDTLS_ENTROPY_C */ + +#ifdef MBEDTLS_CTR_DRBG_C +/* seed: MBEDTLS_CTR_DRBG_ENTROPY_LEN bytes from f_entropy, then add */ +static int MbedDrbgSeedInput(int (*f_entropy)(void*, unsigned char*, size_t), + void* p_entropy, const unsigned char* add, size_t addLen, + byte seed[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT], word32* seedLen) +{ + if (addLen > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - + MBEDTLS_CTR_DRBG_ENTROPY_LEN) { + return MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG; + } + if (f_entropy(p_entropy, seed, MBEDTLS_CTR_DRBG_ENTROPY_LEN) != 0) { + return MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; + } + *seedLen = MBEDTLS_CTR_DRBG_ENTROPY_LEN; + /* NULL add is empty, as in Mbed TLS */ + if (add != NULL && addLen > 0) { + XMEMCPY(seed + *seedLen, add, addLen); + *seedLen += (word32)addLen; + } + return 0; +} + +/* Called with the context locked */ +static int MbedDrbgReseed(mbedtls_ctr_drbg_context* ctx, + const unsigned char* additional, size_t len) +{ + byte seed[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT]; + word32 seedLen = 0; + int ret; + + ret = MbedDrbgSeedInput(ctx->f_entropy, ctx->p_entropy, additional, len, + seed, &seedLen); + if (ret == 0 && wc_RNG_DRBG_Reseed(ctx->rng, seed, seedLen) != 0) { + ret = MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; + } + if (ret == 0) { + ctx->reseed_counter = 0; + } + wc_ForceZero(seed, sizeof(seed)); + return ret; +} + +/* Frees what mbedtls_ctr_drbg_seed() made */ +static void MbedDrbgUnseed(mbedtls_ctr_drbg_context* ctx) +{ +#ifdef MBEDTLS_THREADING_C + mbedtls_mutex_free(&ctx->mutex); +#endif /* MBEDTLS_THREADING_C */ + wc_rng_free(ctx->rng); + ctx->rng = NULL; +} + +void mbedtls_ctr_drbg_init(mbedtls_ctr_drbg_context* ctx) +{ + WOLFSSL_ENTER("mbedtls_ctr_drbg_init"); + + if (ctx != NULL) { + XMEMSET(ctx, 0, sizeof(*ctx)); + } +} + +void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context* ctx) +{ + WOLFSSL_ENTER("mbedtls_ctr_drbg_free"); + + if (ctx != NULL) { + MbedDrbgUnseed(ctx); + XMEMSET(ctx, 0, sizeof(*ctx)); + } +} + +int mbedtls_ctr_drbg_seed(mbedtls_ctr_drbg_context* ctx, + int (*f_entropy)(void*, unsigned char*, size_t), void* p_entropy, + const unsigned char* custom, size_t len) +{ + byte seed[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT]; + word32 seedLen = 0; + int ret; + + WOLFSSL_ENTER("mbedtls_ctr_drbg_seed"); + + if (ctx == NULL || f_entropy == NULL) { + return MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; + } + MbedDrbgUnseed(ctx); + ret = MbedDrbgSeedInput(f_entropy, p_entropy, custom, len, seed, + &seedLen); + /* wolfCrypt seeds its DRBG itself, with the seed input as the nonce */ + if (ret == 0 && wc_rng_new_ex(&ctx->rng, seed, seedLen, NULL, + wolfPSA_GetDefaultDevID()) != 0) { + ret = MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; + } + if (ret == 0) { + ctx->f_entropy = f_entropy; + ctx->p_entropy = p_entropy; + ctx->reseed_counter = 0; +#ifdef MBEDTLS_THREADING_C + mbedtls_mutex_init(&ctx->mutex); +#endif /* MBEDTLS_THREADING_C */ + } + wc_ForceZero(seed, sizeof(seed)); + return ret; +} + +void mbedtls_ctr_drbg_set_prediction_resistance(mbedtls_ctr_drbg_context* ctx, + int resistance) +{ + WOLFSSL_ENTER("mbedtls_ctr_drbg_set_prediction_resistance"); + + if (ctx != NULL) { + ctx->prediction_resistance = resistance; + } +} + +int mbedtls_ctr_drbg_reseed(mbedtls_ctr_drbg_context* ctx, + const unsigned char* additional, size_t len) +{ + int ret; + + WOLFSSL_ENTER("mbedtls_ctr_drbg_reseed"); + + if (ctx == NULL || ctx->rng == NULL) { + return MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; + } + ret = MBED_LOCK(&ctx->mutex); + if (ret == 0) { + ret = MbedDrbgReseed(ctx, additional, len); + MBED_UNLOCK(&ctx->mutex); + } + return ret; +} + +int mbedtls_ctr_drbg_random(void* p_rng, unsigned char* output, + size_t output_len) +{ + mbedtls_ctr_drbg_context* ctx = (mbedtls_ctr_drbg_context*)p_rng; + int ret; + + WOLFSSL_ENTER("mbedtls_ctr_drbg_random"); + + if (output_len > MBEDTLS_CTR_DRBG_MAX_REQUEST) { + return MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG; + } + if (ctx == NULL || ctx->rng == NULL || (output == NULL && output_len > 0)) { + return MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; + } + ret = MBED_LOCK(&ctx->mutex); + if (ret == 0) { + if (ctx->prediction_resistance || + ctx->reseed_counter >= MBEDTLS_CTR_DRBG_RESEED_INTERVAL) { + ret = MbedDrbgReseed(ctx, NULL, 0); + } + if (ret == 0 && output_len > 0 && wc_RNG_GenerateBlock(ctx->rng, + output, (word32)output_len) != 0) { + ret = MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; + } + if (ret == 0) { + ctx->reseed_counter++; + } + MBED_UNLOCK(&ctx->mutex); + } + return ret; +} +#endif /* MBEDTLS_CTR_DRBG_C */ + #endif /* WOLFSSL_PSA_ENGINE && WOLFPSA_MBEDTLS_COMPAT */ diff --git a/test/mbedtls_compat/mbedtls3_test.c b/test/mbedtls_compat/mbedtls3_test.c index 75e2c6a5..b2a14d12 100644 --- a/test/mbedtls_compat/mbedtls3_test.c +++ b/test/mbedtls_compat/mbedtls3_test.c @@ -24,6 +24,9 @@ #include #include +#include +#include +#include #include #include @@ -75,6 +78,348 @@ static int VersionsMajorMinor(void) } #endif /* !MBEDTLS_DEPRECATED_REMOVED */ +/* The 3.6-only codes, with Mbed TLS 3.6's text */ +static int Strerror(void) +{ + static const struct { + int code; + const char* str; + } errs[] = { + { MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, + "CTR_DRBG - The entropy source failed" }, + { MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG, + "CTR_DRBG - The requested random buffer length is too big" }, + { MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG, + "CTR_DRBG - The input (entropy + additional data) is too large" }, + { MBEDTLS_ERR_ENTROPY_SOURCE_FAILED, + "ENTROPY - Critical entropy source failure" }, + { MBEDTLS_ERR_ENTROPY_MAX_SOURCES, + "ENTROPY - No more sources can be added" }, + { MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED, + "ENTROPY - No sources have been added to poll" } + }; + char buf[128]; + size_t i; + int ok = 1; + + for (i = 0; ok && i < sizeof(errs) / sizeof(errs[0]); i++) { + mbedtls_strerror(errs[i].code, buf, sizeof(buf)); + ok = strcmp(buf, errs[i].str) == 0; + if (!ok) { + printf("strerror: \"%s\", expected \"%s\"\n", buf, errs[i].str); + } + } + return ok; +} + +#ifdef MBEDTLS_ENTROPY_C +/* An application's own error code */ +#define TEST_ENTROPY_ERR -1 + +/* Entropy for a DRBG or a source: counts calls, returns ret */ +typedef struct { + int calls; + size_t len; /* asked for by the last call */ + size_t size; + int ret; +} TestEntropy; + +static int TestSource(void* data, unsigned char* output, size_t len, + size_t* olen) +{ + TestEntropy* e = (TestEntropy*)data; + + e->calls++; + e->len = len; + *olen = (e->size < len) ? e->size : len; + memset(output, 0x5A, *olen); + return e->ret; +} + +/* Each output is at most a block, and new */ +static int EntropyOutput(void) +{ + mbedtls_entropy_context entropy; + unsigned char a[MBEDTLS_ENTROPY_BLOCK_SIZE + 1]; + unsigned char b[MBEDTLS_ENTROPY_BLOCK_SIZE]; + int ok; + + mbedtls_entropy_init(&entropy); + ok = mbedtls_entropy_func(&entropy, a, sizeof(b)) == 0 && + mbedtls_entropy_func(&entropy, b, sizeof(b)) == 0 && + memcmp(a, b, sizeof(b)) != 0 && + mbedtls_entropy_func(&entropy, a, sizeof(a)) == + MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; + mbedtls_entropy_free(&entropy); + ok = ok && mbedtls_entropy_func(&entropy, b, sizeof(b)) != 0; + mbedtls_entropy_free(&entropy); + return ok; +} + +/* Polled up to its threshold; its error fails the output */ +static int EntropySource(void) +{ + TestEntropy e = { 0, 0, 8, 0 }; + mbedtls_entropy_context entropy; + unsigned char out[MBEDTLS_ENTROPY_BLOCK_SIZE]; + int ok; + + mbedtls_entropy_init(&entropy); + ok = mbedtls_entropy_add_source(&entropy, TestSource, &e, 32, + MBEDTLS_ENTROPY_SOURCE_STRONG) == 0 && + mbedtls_entropy_func(&entropy, out, sizeof(out)) == 0 && + e.calls == 4 && e.len == MBEDTLS_ENTROPY_MAX_GATHER; + e.ret = TEST_ENTROPY_ERR; + ok = ok && mbedtls_entropy_func(&entropy, out, sizeof(out)) == + TEST_ENTROPY_ERR; + mbedtls_entropy_free(&entropy); + return ok; +} + +/* A source that never reaches its threshold fails the output */ +static int EntropyThresholdNotReached(void) +{ + TestEntropy e = { 0, 0, 0, 0 }; + mbedtls_entropy_context entropy; + unsigned char out[MBEDTLS_ENTROPY_BLOCK_SIZE]; + int ok; + + mbedtls_entropy_init(&entropy); + ok = mbedtls_entropy_add_source(&entropy, TestSource, &e, 1, + MBEDTLS_ENTROPY_SOURCE_WEAK) == 0 && + mbedtls_entropy_func(&entropy, out, sizeof(out)) == + MBEDTLS_ERR_ENTROPY_SOURCE_FAILED && + e.calls > 1; + mbedtls_entropy_free(&entropy); + return ok; +} + +/* The default source is one of MBEDTLS_ENTROPY_MAX_SOURCES */ +static int EntropyMaxSources(void) +{ + TestEntropy e = { 0, 0, 0, 0 }; + mbedtls_entropy_context entropy; + int ok = 1; + int i; + + mbedtls_entropy_init(&entropy); + for (i = 1; ok && i < MBEDTLS_ENTROPY_MAX_SOURCES; i++) { + ok = mbedtls_entropy_add_source(&entropy, TestSource, &e, 0, + MBEDTLS_ENTROPY_SOURCE_WEAK) == 0; + } + ok = ok && mbedtls_entropy_add_source(&entropy, TestSource, &e, 0, + MBEDTLS_ENTROPY_SOURCE_WEAK) == MBEDTLS_ERR_ENTROPY_MAX_SOURCES; + mbedtls_entropy_free(&entropy); + return ok; +} + +#ifdef MBEDTLS_CTR_DRBG_C +static int TestDrbgEntropy(void* data, unsigned char* output, size_t len) +{ + TestEntropy* e = (TestEntropy*)data; + + e->calls++; + e->len = len; + memset(output, 0x5A, len); + return e->ret; +} + +/* Seeded from the default entropy source, as most applications do */ +static int DrbgRandom(void) +{ + static const unsigned char pers[] = "mbedtls3_test"; + mbedtls_entropy_context entropy; + mbedtls_ctr_drbg_context drbg; + unsigned char a[32]; + unsigned char b[32]; + unsigned char big[MBEDTLS_CTR_DRBG_MAX_REQUEST + 1]; + int ok; + + mbedtls_entropy_init(&entropy); + mbedtls_ctr_drbg_init(&drbg); + ok = mbedtls_ctr_drbg_seed(&drbg, mbedtls_entropy_func, &entropy, pers, + sizeof(pers)) == 0 && + mbedtls_ctr_drbg_random(&drbg, a, sizeof(a)) == 0 && + mbedtls_ctr_drbg_random(&drbg, b, sizeof(b)) == 0 && + memcmp(a, b, sizeof(a)) != 0 && + mbedtls_ctr_drbg_random(&drbg, big, sizeof(big) - 1) == 0 && + mbedtls_ctr_drbg_random(&drbg, big, sizeof(big)) == + MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG; + mbedtls_ctr_drbg_free(&drbg); + mbedtls_entropy_free(&entropy); + return ok; +} + +/* Entropy at seed, reseed, and each output with prediction resistance */ +static int DrbgEntropyCalls(void) +{ + static const unsigned char add[16] = { 0 }; + TestEntropy e = { 0, 0, 0, 0 }; + mbedtls_ctr_drbg_context drbg; + unsigned char out[16]; + int ok; + + mbedtls_ctr_drbg_init(&drbg); + ok = mbedtls_ctr_drbg_seed(&drbg, TestDrbgEntropy, &e, NULL, 0) == 0 && + e.calls == 1 && e.len == MBEDTLS_CTR_DRBG_ENTROPY_LEN && + mbedtls_ctr_drbg_random(&drbg, out, sizeof(out)) == 0 && + e.calls == 1 && + mbedtls_ctr_drbg_reseed(&drbg, add, sizeof(add)) == 0 && + e.calls == 2; + mbedtls_ctr_drbg_set_prediction_resistance(&drbg, MBEDTLS_CTR_DRBG_PR_ON); + ok = ok && mbedtls_ctr_drbg_random(&drbg, out, sizeof(out)) == 0 && + mbedtls_ctr_drbg_random(&drbg, out, sizeof(out)) == 0 && + e.calls == 4; + mbedtls_ctr_drbg_free(&drbg); + return ok; +} + +static int DrbgReseedInterval(void) +{ + TestEntropy e = { 0, 0, 0, 0 }; + mbedtls_ctr_drbg_context drbg; + unsigned char out[1]; + int ok; + int i; + + mbedtls_ctr_drbg_init(&drbg); + ok = mbedtls_ctr_drbg_seed(&drbg, TestDrbgEntropy, &e, NULL, 0) == 0; + for (i = 0; ok && i < MBEDTLS_CTR_DRBG_RESEED_INTERVAL; i++) { + ok = mbedtls_ctr_drbg_random(&drbg, out, sizeof(out)) == 0; + } + ok = ok && e.calls == 1 && + mbedtls_ctr_drbg_random(&drbg, out, sizeof(out)) == 0 && + e.calls == 2; + mbedtls_ctr_drbg_free(&drbg); + return ok; +} + +/* Entropy function errors, too much input and a DRBG not seeded */ +static int DrbgErrors(void) +{ + static const unsigned char custom[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - + MBEDTLS_CTR_DRBG_ENTROPY_LEN + 1] = { 0 }; + TestEntropy e = { 0, 0, 0, TEST_ENTROPY_ERR }; + mbedtls_ctr_drbg_context drbg; + unsigned char out[16]; + int ok; + + mbedtls_ctr_drbg_init(&drbg); + ok = mbedtls_ctr_drbg_random(&drbg, out, sizeof(out)) == + MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED && + mbedtls_ctr_drbg_seed(&drbg, TestDrbgEntropy, &e, NULL, 0) == + MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED && + mbedtls_ctr_drbg_random(&drbg, out, sizeof(out)) == + MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; + e.ret = 0; + ok = ok && mbedtls_ctr_drbg_seed(&drbg, TestDrbgEntropy, &e, custom, + sizeof(custom)) == MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG && + mbedtls_ctr_drbg_seed(&drbg, TestDrbgEntropy, &e, custom, + sizeof(custom) - 1) == 0 && + mbedtls_ctr_drbg_reseed(&drbg, custom, sizeof(custom)) == + MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG; + e.ret = TEST_ENTROPY_ERR; + mbedtls_ctr_drbg_set_prediction_resistance(&drbg, MBEDTLS_CTR_DRBG_PR_ON); + ok = ok && mbedtls_ctr_drbg_reseed(&drbg, NULL, 0) == + MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED && + mbedtls_ctr_drbg_random(&drbg, out, sizeof(out)) == + MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; + mbedtls_ctr_drbg_free(&drbg); + mbedtls_ctr_drbg_free(&drbg); + return ok; +} + +/* NULL custom or additional input is empty, its length still checked */ +static int DrbgNullInput(void) +{ + const size_t tooBig = MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - + MBEDTLS_CTR_DRBG_ENTROPY_LEN + 1; + TestEntropy e = { 0, 0, 0, 0 }; + mbedtls_ctr_drbg_context drbg; + unsigned char out[16]; + int ok; + + mbedtls_ctr_drbg_init(&drbg); + ok = mbedtls_ctr_drbg_seed(&drbg, TestDrbgEntropy, &e, NULL, tooBig) == + MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG && + mbedtls_ctr_drbg_seed(&drbg, TestDrbgEntropy, &e, NULL, 1) == 0 && + mbedtls_ctr_drbg_reseed(&drbg, NULL, tooBig) == + MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG && + mbedtls_ctr_drbg_reseed(&drbg, NULL, 1) == 0 && + mbedtls_ctr_drbg_random(&drbg, out, sizeof(out)) == 0; + mbedtls_ctr_drbg_free(&drbg); + return ok; +} + +/* wolfCrypt adds its own seed, so the same entropy gives other output */ +static int DrbgOwnSeed(void) +{ + TestEntropy e = { 0, 0, 0, 0 }; + mbedtls_ctr_drbg_context drbg[2]; + unsigned char out[2][32]; + int ok = 1; + int i; + + for (i = 0; i < 2; i++) { + mbedtls_ctr_drbg_init(&drbg[i]); + ok = ok && mbedtls_ctr_drbg_seed(&drbg[i], TestDrbgEntropy, &e, NULL, + 0) == 0 && + mbedtls_ctr_drbg_random(&drbg[i], out[i], sizeof(out[i])) == 0; + } + ok = ok && memcmp(out[0], out[1], sizeof(out[0])) != 0; + for (i = 0; i < 2; i++) { + mbedtls_ctr_drbg_free(&drbg[i]); + } + return ok; +} + +/* An entropy source error fails the seed */ +static int DrbgSourceError(void) +{ + TestEntropy e = { 0, 0, 8, TEST_ENTROPY_ERR }; + mbedtls_entropy_context entropy; + mbedtls_ctr_drbg_context drbg; + int ok; + + mbedtls_entropy_init(&entropy); + mbedtls_ctr_drbg_init(&drbg); + ok = mbedtls_entropy_add_source(&entropy, TestSource, &e, 32, + MBEDTLS_ENTROPY_SOURCE_STRONG) == 0 && + mbedtls_ctr_drbg_seed(&drbg, mbedtls_entropy_func, &entropy, NULL, + 0) == MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; + mbedtls_ctr_drbg_free(&drbg); + mbedtls_entropy_free(&entropy); + return ok; +} + +/* The setup of every 3.6 application */ +static int ConfRng(void) +{ + mbedtls_entropy_context entropy; + mbedtls_ctr_drbg_context drbg; + mbedtls_ssl_config conf; + mbedtls_ssl_context ssl; + int ok; + + mbedtls_entropy_init(&entropy); + mbedtls_ctr_drbg_init(&drbg); + mbedtls_ssl_config_init(&conf); + mbedtls_ssl_init(&ssl); + ok = mbedtls_ctr_drbg_seed(&drbg, mbedtls_entropy_func, &entropy, NULL, + 0) == 0 && + mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT, + MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT) == 0; + mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &drbg); + ok = ok && mbedtls_ssl_setup(&ssl, &conf) == 0; + mbedtls_ssl_free(&ssl); + mbedtls_ssl_config_free(&conf); + mbedtls_ctr_drbg_free(&drbg); + mbedtls_entropy_free(&entropy); + return ok; +} +#endif /* MBEDTLS_CTR_DRBG_C */ +#endif /* MBEDTLS_ENTROPY_C */ + static const struct { const char* name; int (*test)(void); @@ -83,6 +428,23 @@ static const struct { #ifndef MBEDTLS_DEPRECATED_REMOVED { "version ranges by major and minor", VersionsMajorMinor }, #endif /* !MBEDTLS_DEPRECATED_REMOVED */ + { "strerror of the ctr_drbg and entropy codes", Strerror }, +#ifdef MBEDTLS_ENTROPY_C + { "entropy output", EntropyOutput }, + { "entropy source", EntropySource }, + { "entropy threshold not reached", EntropyThresholdNotReached }, + { "entropy max sources", EntropyMaxSources }, +#ifdef MBEDTLS_CTR_DRBG_C + { "ctr_drbg random", DrbgRandom }, + { "ctr_drbg entropy calls", DrbgEntropyCalls }, + { "ctr_drbg reseed interval", DrbgReseedInterval }, + { "ctr_drbg errors", DrbgErrors }, + { "ctr_drbg NULL input", DrbgNullInput }, + { "ctr_drbg own seed", DrbgOwnSeed }, + { "ctr_drbg entropy source error", DrbgSourceError }, + { "ssl_conf_rng", ConfRng }, +#endif /* MBEDTLS_CTR_DRBG_C */ +#endif /* MBEDTLS_ENTROPY_C */ }; int main(void) diff --git a/tools/psa_want_probe.c b/tools/psa_want_probe.c index efe6855b..d82908fb 100644 --- a/tools/psa_want_probe.c +++ b/tools/psa_want_probe.c @@ -272,8 +272,13 @@ __WPSA_EMIT__PSA_WANT_KEY_TYPE_ML_DSA __WPSA_EMIT__MBEDTLS_FS_IO #endif /* The PSA RNG, wolfCrypt's DRBG. No public API in TF-PSA-Crypto 1.x. */ -#if !defined(WC_NO_RNG) +#if !defined(WC_NO_RNG) && !defined(NO_HASH_WRAPPER) && \ + (!defined(NO_SHA256) || defined(WOLFSSL_SHA512)) __WPSA_EMIT__MBEDTLS_ENTROPY_C +#endif +/* ctr_drbg needs the Hash DRBG: a custom generator or RDRAND bypasses it */ +#if !defined(WC_NO_RNG) && !defined(WC_NO_HASHDRBG) && \ + !defined(CUSTOM_RAND_GENERATE_BLOCK) && !defined(HAVE_INTEL_RDRAND) __WPSA_EMIT__MBEDTLS_CTR_DRBG_C #endif /* wolfPSA's own Base64, off with wolfCrypt's NO_CODING */ diff --git a/wolfpsa.map b/wolfpsa.map index c3abd878..8d1e9723 100644 --- a/wolfpsa.map +++ b/wolfpsa.map @@ -142,7 +142,9 @@ WOLFPSA_1.0 { wolfPSA_SetDefaultDevID; wolfPSA_GetDefaultDevID; /* Mbed TLS compatibility layer, MBEDTLS_COMPAT=1 */ + mbedtls_ctr_drbg_*; mbedtls_debug_*; + mbedtls_entropy_*; mbedtls_ms_time*; mbedtls_net_*; mbedtls_oid_*; diff --git a/wolfpsa/mbedtls_compat/mbedtls/ctr_drbg.h b/wolfpsa/mbedtls_compat/mbedtls/ctr_drbg.h new file mode 100644 index 00000000..a5009d6a --- /dev/null +++ b/wolfpsa/mbedtls_compat/mbedtls/ctr_drbg.h @@ -0,0 +1,81 @@ +/* ctr_drbg.h + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfPSA. + * + * wolfPSA is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfPSA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#ifndef MBEDTLS_CTR_DRBG_H +#define MBEDTLS_CTR_DRBG_H + +#include "mbedtls/build_info.h" +#include "mbedtls/entropy.h" + +#include + +#ifdef __cplusplus + extern "C" { +#endif /* __cplusplus */ + +#define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 +#define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 +#define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 + +#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) + #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48 +#else + #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32 +#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */ +#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000 +#define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024 +#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384 +#define MBEDTLS_CTR_DRBG_PR_OFF 0 +#define MBEDTLS_CTR_DRBG_PR_ON 1 + +/* wolfCrypt's DRBG, opaque to the application */ +struct WC_RNG; + +typedef struct mbedtls_ctr_drbg_context { + struct WC_RNG* rng; /* NULL until seeded */ + int (*f_entropy)(void*, unsigned char*, size_t); + void* p_entropy; + int prediction_resistance; + int reseed_counter; /* requests since the last seed */ +#if defined(MBEDTLS_THREADING_C) + mbedtls_threading_mutex_t mutex; /* initialized when seeded */ +#endif /* MBEDTLS_THREADING_C */ +} mbedtls_ctr_drbg_context; + +#if defined(MBEDTLS_CTR_DRBG_C) +void mbedtls_ctr_drbg_init(mbedtls_ctr_drbg_context* ctx); +void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context* ctx); +int mbedtls_ctr_drbg_seed(mbedtls_ctr_drbg_context* ctx, + int (*f_entropy)(void*, unsigned char*, size_t), void* p_entropy, + const unsigned char* custom, size_t len); +void mbedtls_ctr_drbg_set_prediction_resistance(mbedtls_ctr_drbg_context* ctx, + int resistance); +int mbedtls_ctr_drbg_reseed(mbedtls_ctr_drbg_context* ctx, + const unsigned char* additional, size_t len); +int mbedtls_ctr_drbg_random(void* p_rng, unsigned char* output, + size_t output_len); +#endif /* MBEDTLS_CTR_DRBG_C */ + +#ifdef __cplusplus + } /* extern "C" */ +#endif /* __cplusplus */ + +#endif /* MBEDTLS_CTR_DRBG_H */ diff --git a/wolfpsa/mbedtls_compat/mbedtls/entropy.h b/wolfpsa/mbedtls_compat/mbedtls/entropy.h new file mode 100644 index 00000000..d5caa8bc --- /dev/null +++ b/wolfpsa/mbedtls_compat/mbedtls/entropy.h @@ -0,0 +1,90 @@ +/* entropy.h + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfPSA. + * + * wolfPSA is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfPSA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#ifndef MBEDTLS_ENTROPY_H +#define MBEDTLS_ENTROPY_H + +#include "mbedtls/build_info.h" + +#ifndef WOLFPSA_MBEDTLS_3 + #error "mbedtls/entropy.h is Mbed TLS 3.6 API: define WOLFPSA_MBEDTLS_3" +#endif /* !WOLFPSA_MBEDTLS_3 */ + +#if defined(MBEDTLS_THREADING_C) + #include "mbedtls/threading.h" +#endif /* MBEDTLS_THREADING_C */ + +#include + +#ifdef __cplusplus + extern "C" { +#endif /* __cplusplus */ + +#if defined(PSA_WANT_ALG_SHA_512) + #define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR + #define MBEDTLS_ENTROPY_BLOCK_SIZE 64 +#else + #define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR + #define MBEDTLS_ENTROPY_BLOCK_SIZE 32 +#endif /* PSA_WANT_ALG_SHA_512 */ + +#define MBEDTLS_ERR_ENTROPY_SOURCE_FAILED -0x003C +#define MBEDTLS_ERR_ENTROPY_MAX_SOURCES -0x003E +#define MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 + +#define MBEDTLS_ENTROPY_MAX_SOURCES 20 +#define MBEDTLS_ENTROPY_MAX_GATHER 128 +#define MBEDTLS_ENTROPY_SOURCE_STRONG 1 +#define MBEDTLS_ENTROPY_SOURCE_WEAK 0 + +typedef int (*mbedtls_entropy_f_source_ptr)(void* data, unsigned char* output, + size_t len, size_t* olen); + +typedef struct mbedtls_entropy_source_state { + mbedtls_entropy_f_source_ptr f_source; + void* p_source; + size_t size; /* gathered since the last output */ + size_t threshold; + int strong; +} mbedtls_entropy_source_state; + +typedef struct mbedtls_entropy_context { + int source_count; + mbedtls_entropy_source_state source[MBEDTLS_ENTROPY_MAX_SOURCES]; +#if defined(MBEDTLS_THREADING_C) + mbedtls_threading_mutex_t mutex; +#endif /* MBEDTLS_THREADING_C */ +} mbedtls_entropy_context; + +#if defined(MBEDTLS_ENTROPY_C) +void mbedtls_entropy_init(mbedtls_entropy_context* ctx); +void mbedtls_entropy_free(mbedtls_entropy_context* ctx); +int mbedtls_entropy_add_source(mbedtls_entropy_context* ctx, + mbedtls_entropy_f_source_ptr f_source, void* p_source, size_t threshold, + int strong); +int mbedtls_entropy_func(void* data, unsigned char* output, size_t len); +#endif /* MBEDTLS_ENTROPY_C */ + +#ifdef __cplusplus + } /* extern "C" */ +#endif /* __cplusplus */ + +#endif /* MBEDTLS_ENTROPY_H */ diff --git a/wolfpsa/mbedtls_compat/mbedtls/ssl.h b/wolfpsa/mbedtls_compat/mbedtls/ssl.h index 57a9c9c6..02670056 100644 --- a/wolfpsa/mbedtls_compat/mbedtls/ssl.h +++ b/wolfpsa/mbedtls_compat/mbedtls/ssl.h @@ -339,6 +339,10 @@ void MBEDTLS_DEPRECATED mbedtls_ssl_conf_min_version(mbedtls_ssl_config* conf, void MBEDTLS_DEPRECATED mbedtls_ssl_conf_max_version(mbedtls_ssl_config* conf, int major, int minor); #endif /* WOLFPSA_MBEDTLS_3 && !MBEDTLS_DEPRECATED_REMOVED */ +#ifdef WOLFPSA_MBEDTLS_3 +void mbedtls_ssl_conf_rng(mbedtls_ssl_config* conf, + int (*f_rng)(void*, unsigned char*, size_t), void* p_rng); +#endif /* WOLFPSA_MBEDTLS_3 */ #ifdef MBEDTLS_SSL_MAX_FRAGMENT_LENGTH int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config* conf, unsigned char mfl_code); From 4404c2ad6bfbc550626d867a3f5da6ae2c0a865e Mon Sep 17 00:00:00 2001 From: Paul Adelsbach Date: Wed, 25 Nov 2026 09:07:31 -0800 Subject: [PATCH 2/2] Address review of Mbed TLS 3.6 ctr_drbg and entropy --- docs/mbedtls-compatibility.md | 16 ++-- src/mbedtls/error.c | 2 +- src/mbedtls/mbedtls3.c | 44 ++++++++-- test/mbedtls_compat/mbedtls3_test.c | 98 ++++++++++++++--------- wolfpsa/mbedtls_compat/mbedtls/ctr_drbg.h | 12 ++- 5 files changed, 118 insertions(+), 54 deletions(-) diff --git a/docs/mbedtls-compatibility.md b/docs/mbedtls-compatibility.md index 01194191..b57d2120 100644 --- a/docs/mbedtls-compatibility.md +++ b/docs/mbedtls-compatibility.md @@ -376,16 +376,22 @@ Mbed TLS 3.6: - `mbedtls_ssl_conf_rng` is accepted and ignored: wolfSSL draws its own random numbers. - `mbedtls_ctr_drbg_*` use wolfCrypt's RNG, a hash DRBG, not an AES - CTR_DRBG. wolfCrypt seeds it from its own seed source and adds the entropy - function's output and the custom string. So the same entropy, for example - a fixed one in a test, gives other output each time, where Mbed TLS gives + CTR_DRBG. wolfCrypt seeds and reseeds it from its own seed source and + adds the entropy function's output and the custom or additional data, + without crediting them as entropy. So the same entropy, for example a + fixed one in a test, gives other output each time, where Mbed TLS gives the same. `mbedtls_ctr_drbg_random` and `mbedtls_ctr_drbg_reseed` on a context that is not seeded return `MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED`. They need wolfCrypt's hash DRBG, so they are not built with `WC_NO_HASHDRBG`, or with `CUSTOM_RAND_GENERATE_BLOCK` or `HAVE_INTEL_RDRAND`, which bypass it. -- `mbedtls_entropy_init` adds one source, wolfCrypt's RNG, in place of Mbed - TLS's platform, hardware and NV seed sources. Sources from +- When wolfPSA needs `psa_crypto_init` first, `mbedtls_ctr_drbg_seed` and + `mbedtls_entropy_func` return `PSA_ERROR_BAD_STATE` before it, as the + parsing and TLS setup functions do. +- `mbedtls_entropy_init` adds one source, wolfCrypt's seed source + `wc_GenerateSeed`, in place of Mbed TLS's platform, hardware and NV seed + sources. With `CUSTOM_RAND_GENERATE_BLOCK` or `WC_RNG_SEED_CB` it is + wolfCrypt's RNG instead. Sources from `mbedtls_entropy_add_source` are polled as in Mbed TLS. Each `mbedtls_entropy_func` output is a hash of what the sources gave for it; Mbed TLS also keeps what they gave for earlier outputs. The hash is diff --git a/src/mbedtls/error.c b/src/mbedtls/error.c index b53eb734..61ba3650 100644 --- a/src/mbedtls/error.c +++ b/src/mbedtls/error.c @@ -78,7 +78,7 @@ static const struct { MBED_ERR(ENTROPY, MAX_SOURCES, "No more sources can be added"), MBED_ERR(ENTROPY, NO_SOURCES_DEFINED, "No sources have been added to poll"), - MBED_ERR(MD,FEATURE_UNAVAILABLE, "The selected feature is not available"), + MBED_ERR(MD, FEATURE_UNAVAILABLE, "The selected feature is not available"), MBED_ERR(NET, SOCKET_FAILED, "Failed to open a socket"), MBED_ERR(NET, CONNECT_FAILED, "The connection to the given server / port failed"), diff --git a/src/mbedtls/mbedtls3.c b/src/mbedtls/mbedtls3.c index 13f8bcdc..7ac23a49 100644 --- a/src/mbedtls/mbedtls3.c +++ b/src/mbedtls/mbedtls3.c @@ -98,15 +98,25 @@ void mbedtls_ssl_conf_rng(mbedtls_ssl_config* conf, static int MbedEntropyDefault(void* data, unsigned char* output, size_t len, size_t* olen) { +#if defined(CUSTOM_RAND_GENERATE_BLOCK) || defined(WC_RNG_SEED_CB) + /* No wc_GenerateSeed, or not the seed source the RNG uses */ WC_RNG rng; +#else + OS_Seed os; +#endif /* CUSTOM_RAND_GENERATE_BLOCK || WC_RNG_SEED_CB */ int ret; (void)data; +#if defined(CUSTOM_RAND_GENERATE_BLOCK) || defined(WC_RNG_SEED_CB) ret = wc_InitRng_ex(&rng, NULL, wolfPSA_GetDefaultDevID()); if (ret == 0) { ret = wc_RNG_GenerateBlock(&rng, output, (word32)len); (void)wc_FreeRng(&rng); } +#else + XMEMSET(&os, 0, sizeof(os)); + ret = wc_GenerateSeed(&os, output, (word32)len); +#endif /* CUSTOM_RAND_GENERATE_BLOCK || WC_RNG_SEED_CB */ if (ret != 0) { return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; } @@ -231,6 +241,9 @@ int mbedtls_entropy_func(void* data, unsigned char* output, size_t len) if (ctx == NULL || (output == NULL && len > 0) || len > sizeof(digest)) { return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; } + if (!wolfPSA_CryptoReady()) { + return PSA_ERROR_BAD_STATE; + } ret = MBED_LOCK(&ctx->mutex); if (ret != 0) { return ret; @@ -269,19 +282,26 @@ int mbedtls_entropy_func(void* data, unsigned char* output, size_t len) #endif /* MBEDTLS_ENTROPY_C */ #ifdef MBEDTLS_CTR_DRBG_C -/* seed: MBEDTLS_CTR_DRBG_ENTROPY_LEN bytes from f_entropy, then add */ +/* seed: MBEDTLS_CTR_DRBG_ENTROPY_LEN bytes from f_entropy, nonceLen more in + * a second call, then add */ static int MbedDrbgSeedInput(int (*f_entropy)(void*, unsigned char*, size_t), - void* p_entropy, const unsigned char* add, size_t addLen, + void* p_entropy, size_t nonceLen, const unsigned char* add, size_t addLen, byte seed[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT], word32* seedLen) { if (addLen > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - - MBEDTLS_CTR_DRBG_ENTROPY_LEN) { + MBEDTLS_CTR_DRBG_ENTROPY_LEN - nonceLen) { return MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG; } if (f_entropy(p_entropy, seed, MBEDTLS_CTR_DRBG_ENTROPY_LEN) != 0) { return MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; } *seedLen = MBEDTLS_CTR_DRBG_ENTROPY_LEN; + if (nonceLen > 0) { + if (f_entropy(p_entropy, seed + *seedLen, nonceLen) != 0) { + return MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; + } + *seedLen += (word32)nonceLen; + } /* NULL add is empty, as in Mbed TLS */ if (add != NULL && addLen > 0) { XMEMCPY(seed + *seedLen, add, addLen); @@ -298,11 +318,18 @@ static int MbedDrbgReseed(mbedtls_ctr_drbg_context* ctx, word32 seedLen = 0; int ret; - ret = MbedDrbgSeedInput(ctx->f_entropy, ctx->p_entropy, additional, len, - seed, &seedLen); + ret = MbedDrbgSeedInput(ctx->f_entropy, ctx->p_entropy, 0, additional, + len, seed, &seedLen); +#ifdef WC_RNG_HAVE_RESEED_NOW + /* From wolfCrypt's seed source, the input uncredited */ + if (ret == 0 && wc_RNG_DRBG_Reseed_Now(ctx->rng, seed, seedLen) != 0) { + ret = MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; + } +#else if (ret == 0 && wc_RNG_DRBG_Reseed(ctx->rng, seed, seedLen) != 0) { ret = MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; } +#endif /* WC_RNG_HAVE_RESEED_NOW */ if (ret == 0) { ctx->reseed_counter = 0; } @@ -352,9 +379,12 @@ int mbedtls_ctr_drbg_seed(mbedtls_ctr_drbg_context* ctx, if (ctx == NULL || f_entropy == NULL) { return MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; } + if (!wolfPSA_CryptoReady()) { + return PSA_ERROR_BAD_STATE; + } MbedDrbgUnseed(ctx); - ret = MbedDrbgSeedInput(f_entropy, p_entropy, custom, len, seed, - &seedLen); + ret = MbedDrbgSeedInput(f_entropy, p_entropy, + MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN, custom, len, seed, &seedLen); /* wolfCrypt seeds its DRBG itself, with the seed input as the nonce */ if (ret == 0 && wc_rng_new_ex(&ctx->rng, seed, seedLen, NULL, wolfPSA_GetDefaultDevID()) != 0) { diff --git a/test/mbedtls_compat/mbedtls3_test.c b/test/mbedtls_compat/mbedtls3_test.c index b2a14d12..b1449a37 100644 --- a/test/mbedtls_compat/mbedtls3_test.c +++ b/test/mbedtls_compat/mbedtls3_test.c @@ -112,7 +112,7 @@ static int Strerror(void) return ok; } -#ifdef MBEDTLS_ENTROPY_C +#if defined(MBEDTLS_ENTROPY_C) || defined(MBEDTLS_CTR_DRBG_C) /* An application's own error code */ #define TEST_ENTROPY_ERR -1 @@ -123,7 +123,9 @@ typedef struct { size_t size; int ret; } TestEntropy; +#endif /* MBEDTLS_ENTROPY_C || MBEDTLS_CTR_DRBG_C */ +#ifdef MBEDTLS_ENTROPY_C static int TestSource(void* data, unsigned char* output, size_t len, size_t* olen) { @@ -212,8 +214,18 @@ static int EntropyMaxSources(void) mbedtls_entropy_free(&entropy); return ok; } +#endif /* MBEDTLS_ENTROPY_C */ #ifdef MBEDTLS_CTR_DRBG_C +/* The seed reads a nonce after the entropy, if it has one */ +#if MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN > 0 + #define TEST_SEED_CALLS 2 + #define TEST_SEED_LAST_LEN MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN +#else + #define TEST_SEED_CALLS 1 + #define TEST_SEED_LAST_LEN MBEDTLS_CTR_DRBG_ENTROPY_LEN +#endif /* MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN > 0 */ + static int TestDrbgEntropy(void* data, unsigned char* output, size_t len) { TestEntropy* e = (TestEntropy*)data; @@ -224,32 +236,6 @@ static int TestDrbgEntropy(void* data, unsigned char* output, size_t len) return e->ret; } -/* Seeded from the default entropy source, as most applications do */ -static int DrbgRandom(void) -{ - static const unsigned char pers[] = "mbedtls3_test"; - mbedtls_entropy_context entropy; - mbedtls_ctr_drbg_context drbg; - unsigned char a[32]; - unsigned char b[32]; - unsigned char big[MBEDTLS_CTR_DRBG_MAX_REQUEST + 1]; - int ok; - - mbedtls_entropy_init(&entropy); - mbedtls_ctr_drbg_init(&drbg); - ok = mbedtls_ctr_drbg_seed(&drbg, mbedtls_entropy_func, &entropy, pers, - sizeof(pers)) == 0 && - mbedtls_ctr_drbg_random(&drbg, a, sizeof(a)) == 0 && - mbedtls_ctr_drbg_random(&drbg, b, sizeof(b)) == 0 && - memcmp(a, b, sizeof(a)) != 0 && - mbedtls_ctr_drbg_random(&drbg, big, sizeof(big) - 1) == 0 && - mbedtls_ctr_drbg_random(&drbg, big, sizeof(big)) == - MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG; - mbedtls_ctr_drbg_free(&drbg); - mbedtls_entropy_free(&entropy); - return ok; -} - /* Entropy at seed, reseed, and each output with prediction resistance */ static int DrbgEntropyCalls(void) { @@ -261,15 +247,16 @@ static int DrbgEntropyCalls(void) mbedtls_ctr_drbg_init(&drbg); ok = mbedtls_ctr_drbg_seed(&drbg, TestDrbgEntropy, &e, NULL, 0) == 0 && - e.calls == 1 && e.len == MBEDTLS_CTR_DRBG_ENTROPY_LEN && + e.calls == TEST_SEED_CALLS && e.len == TEST_SEED_LAST_LEN && mbedtls_ctr_drbg_random(&drbg, out, sizeof(out)) == 0 && - e.calls == 1 && + e.calls == TEST_SEED_CALLS && mbedtls_ctr_drbg_reseed(&drbg, add, sizeof(add)) == 0 && - e.calls == 2; + e.calls == TEST_SEED_CALLS + 1 && + e.len == MBEDTLS_CTR_DRBG_ENTROPY_LEN; mbedtls_ctr_drbg_set_prediction_resistance(&drbg, MBEDTLS_CTR_DRBG_PR_ON); ok = ok && mbedtls_ctr_drbg_random(&drbg, out, sizeof(out)) == 0 && mbedtls_ctr_drbg_random(&drbg, out, sizeof(out)) == 0 && - e.calls == 4; + e.calls == TEST_SEED_CALLS + 3; mbedtls_ctr_drbg_free(&drbg); return ok; } @@ -287,9 +274,9 @@ static int DrbgReseedInterval(void) for (i = 0; ok && i < MBEDTLS_CTR_DRBG_RESEED_INTERVAL; i++) { ok = mbedtls_ctr_drbg_random(&drbg, out, sizeof(out)) == 0; } - ok = ok && e.calls == 1 && + ok = ok && e.calls == TEST_SEED_CALLS && mbedtls_ctr_drbg_random(&drbg, out, sizeof(out)) == 0 && - e.calls == 2; + e.calls == TEST_SEED_CALLS + 1; mbedtls_ctr_drbg_free(&drbg); return ok; } @@ -299,6 +286,9 @@ static int DrbgErrors(void) { static const unsigned char custom[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - MBEDTLS_CTR_DRBG_ENTROPY_LEN + 1] = { 0 }; + /* The nonce counts against custom, not against additional */ + const size_t seedMax = sizeof(custom) - 1 - + MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN; TestEntropy e = { 0, 0, 0, TEST_ENTROPY_ERR }; mbedtls_ctr_drbg_context drbg; unsigned char out[16]; @@ -313,11 +303,12 @@ static int DrbgErrors(void) MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; e.ret = 0; ok = ok && mbedtls_ctr_drbg_seed(&drbg, TestDrbgEntropy, &e, custom, - sizeof(custom)) == MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG && + seedMax + 1) == MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG && mbedtls_ctr_drbg_seed(&drbg, TestDrbgEntropy, &e, custom, - sizeof(custom) - 1) == 0 && + seedMax) == 0 && mbedtls_ctr_drbg_reseed(&drbg, custom, sizeof(custom)) == - MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG; + MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG && + mbedtls_ctr_drbg_reseed(&drbg, custom, sizeof(custom) - 1) == 0; e.ret = TEST_ENTROPY_ERR; mbedtls_ctr_drbg_set_prediction_resistance(&drbg, MBEDTLS_CTR_DRBG_PR_ON); ok = ok && mbedtls_ctr_drbg_reseed(&drbg, NULL, 0) == @@ -373,6 +364,33 @@ static int DrbgOwnSeed(void) return ok; } +#ifdef MBEDTLS_ENTROPY_C +/* Seeded from the default entropy source, as most applications do */ +static int DrbgRandom(void) +{ + static const unsigned char pers[] = "mbedtls3_test"; + mbedtls_entropy_context entropy; + mbedtls_ctr_drbg_context drbg; + unsigned char a[32]; + unsigned char b[32]; + unsigned char big[MBEDTLS_CTR_DRBG_MAX_REQUEST + 1]; + int ok; + + mbedtls_entropy_init(&entropy); + mbedtls_ctr_drbg_init(&drbg); + ok = mbedtls_ctr_drbg_seed(&drbg, mbedtls_entropy_func, &entropy, pers, + sizeof(pers)) == 0 && + mbedtls_ctr_drbg_random(&drbg, a, sizeof(a)) == 0 && + mbedtls_ctr_drbg_random(&drbg, b, sizeof(b)) == 0 && + memcmp(a, b, sizeof(a)) != 0 && + mbedtls_ctr_drbg_random(&drbg, big, sizeof(big) - 1) == 0 && + mbedtls_ctr_drbg_random(&drbg, big, sizeof(big)) == + MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG; + mbedtls_ctr_drbg_free(&drbg); + mbedtls_entropy_free(&entropy); + return ok; +} + /* An entropy source error fails the seed */ static int DrbgSourceError(void) { @@ -417,8 +435,8 @@ static int ConfRng(void) mbedtls_entropy_free(&entropy); return ok; } -#endif /* MBEDTLS_CTR_DRBG_C */ #endif /* MBEDTLS_ENTROPY_C */ +#endif /* MBEDTLS_CTR_DRBG_C */ static const struct { const char* name; @@ -434,17 +452,19 @@ static const struct { { "entropy source", EntropySource }, { "entropy threshold not reached", EntropyThresholdNotReached }, { "entropy max sources", EntropyMaxSources }, +#endif /* MBEDTLS_ENTROPY_C */ #ifdef MBEDTLS_CTR_DRBG_C - { "ctr_drbg random", DrbgRandom }, { "ctr_drbg entropy calls", DrbgEntropyCalls }, { "ctr_drbg reseed interval", DrbgReseedInterval }, { "ctr_drbg errors", DrbgErrors }, { "ctr_drbg NULL input", DrbgNullInput }, { "ctr_drbg own seed", DrbgOwnSeed }, +#ifdef MBEDTLS_ENTROPY_C + { "ctr_drbg random", DrbgRandom }, { "ctr_drbg entropy source error", DrbgSourceError }, { "ssl_conf_rng", ConfRng }, -#endif /* MBEDTLS_CTR_DRBG_C */ #endif /* MBEDTLS_ENTROPY_C */ +#endif /* MBEDTLS_CTR_DRBG_C */ }; int main(void) diff --git a/wolfpsa/mbedtls_compat/mbedtls/ctr_drbg.h b/wolfpsa/mbedtls_compat/mbedtls/ctr_drbg.h index a5009d6a..0e14b0da 100644 --- a/wolfpsa/mbedtls_compat/mbedtls/ctr_drbg.h +++ b/wolfpsa/mbedtls_compat/mbedtls/ctr_drbg.h @@ -23,6 +23,11 @@ #define MBEDTLS_CTR_DRBG_H #include "mbedtls/build_info.h" + +#ifndef WOLFPSA_MBEDTLS_3 + #error "mbedtls/ctr_drbg.h is Mbed TLS 3.6 API: define WOLFPSA_MBEDTLS_3" +#endif /* !WOLFPSA_MBEDTLS_3 */ + #include "mbedtls/entropy.h" #include @@ -35,10 +40,13 @@ #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 +/* As Mbed TLS: a second read at seed when the first is short of a nonce */ #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) - #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48 + #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48 + #define MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN 0 #else - #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32 + #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32 + #define MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN 16 #endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */ #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024