From 2feb9645418fe83307203913363b8990c7c137ae Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Sat, 25 Jul 2026 09:23:21 +0200 Subject: [PATCH] mbedTLS: Always use Godot's OS as entropy source mbedTLS < 4 provides internal platform functions to seed the random number generator with entropy from the OS. Historically we have been relying on this when the mbedTLS module is enabled, while we have been using the OS::get_entropy when building without the module. Given we had issues on some platform with the built-in implementation, this commit forces the use of OS::get_entropy in all cases (module or not). This is already the case when using mbedTLS 4 (Godot 4.8+). --- core/crypto/crypto_core.cpp | 19 ++++++++++--------- core/crypto/crypto_core.h | 2 -- .../include/godot_core_mbedtls_config.h | 5 ++++- .../include/godot_module_mbedtls_config.h | 4 ++++ 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/core/crypto/crypto_core.cpp b/core/crypto/crypto_core.cpp index 1c7d5d0790..489228f611 100644 --- a/core/crypto/crypto_core.cpp +++ b/core/crypto/crypto_core.cpp @@ -44,11 +44,20 @@ #include #endif +extern "C" { +int mbedtls_hardware_poll(void *p_data, unsigned char *r_buffer, size_t p_len, size_t *r_len) { + *r_len = 0; + Error err = OS::get_singleton()->get_entropy(r_buffer, p_len); + ERR_FAIL_COND_V(err, MBEDTLS_ERR_ENTROPY_SOURCE_FAILED); + *r_len = p_len; + return 0; +} +} + // RandomGenerator CryptoCore::RandomGenerator::RandomGenerator() { entropy = memalloc(sizeof(mbedtls_entropy_context)); mbedtls_entropy_init((mbedtls_entropy_context *)entropy); - mbedtls_entropy_add_source((mbedtls_entropy_context *)entropy, &CryptoCore::RandomGenerator::_entropy_poll, nullptr, 256, MBEDTLS_ENTROPY_SOURCE_STRONG); ctx = memalloc(sizeof(mbedtls_ctr_drbg_context)); mbedtls_ctr_drbg_init((mbedtls_ctr_drbg_context *)ctx); } @@ -60,14 +69,6 @@ CryptoCore::RandomGenerator::~RandomGenerator() { memfree(entropy); } -int CryptoCore::RandomGenerator::_entropy_poll(void *p_data, unsigned char *r_buffer, size_t p_len, size_t *r_len) { - *r_len = 0; - Error err = OS::get_singleton()->get_entropy(r_buffer, p_len); - ERR_FAIL_COND_V(err, MBEDTLS_ERR_ENTROPY_SOURCE_FAILED); - *r_len = p_len; - return 0; -} - Error CryptoCore::RandomGenerator::init() { int ret = mbedtls_ctr_drbg_seed((mbedtls_ctr_drbg_context *)ctx, mbedtls_entropy_func, (mbedtls_entropy_context *)entropy, nullptr, 0); if (ret) { diff --git a/core/crypto/crypto_core.h b/core/crypto/crypto_core.h index a061a74387..c9d628605f 100644 --- a/core/crypto/crypto_core.h +++ b/core/crypto/crypto_core.h @@ -44,8 +44,6 @@ public: void *entropy = nullptr; void *ctx = nullptr; - static int _entropy_poll(void *p_data, unsigned char *r_buffer, size_t p_len, size_t *r_len); - public: RandomGenerator(); ~RandomGenerator(); diff --git a/thirdparty/mbedtls/include/godot_core_mbedtls_config.h b/thirdparty/mbedtls/include/godot_core_mbedtls_config.h index f22ef3aa08..52a3077537 100644 --- a/thirdparty/mbedtls/include/godot_core_mbedtls_config.h +++ b/thirdparty/mbedtls/include/godot_core_mbedtls_config.h @@ -48,7 +48,10 @@ #define MBEDTLS_SHA1_C #define MBEDTLS_SHA256_C #define MBEDTLS_PLATFORM_ZEROIZE_ALT -#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES + +// Custom entropy source. +#define MBEDTLS_NO_PLATFORM_ENTROPY +#define MBEDTLS_ENTROPY_HARDWARE_ALT // This is only to pass a check in the mbedtls check_config.h header, none of // the files we include as part of the core build uses it anyway, we already diff --git a/thirdparty/mbedtls/include/godot_module_mbedtls_config.h b/thirdparty/mbedtls/include/godot_module_mbedtls_config.h index c25804979f..e28075de3d 100644 --- a/thirdparty/mbedtls/include/godot_module_mbedtls_config.h +++ b/thirdparty/mbedtls/include/godot_module_mbedtls_config.h @@ -56,6 +56,10 @@ #define GODOT_MBEDTLS_THREADING_ALT #endif +// Custom entropy source. +#define MBEDTLS_NO_PLATFORM_ENTROPY +#define MBEDTLS_ENTROPY_HARDWARE_ALT + #if !(defined(__linux__) && defined(__aarch64__)) // ARMv8 hardware AES operations. Detection only possible on linux. // May technically be supported on some ARM32 arches but doesn't seem