From 52e2bde16f5c1e28056ef2de49e046d76b3fb059 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Mon, 18 Sep 2023 21:06:15 +0200 Subject: [PATCH] [Crypto] Fix generate_random_bytes for large chunks Properly handle mbedtls errors, ensure we don't ask more bytes than allowed for each iteration. (cherry picked from commit e0140601a521e58bb36fd2a7f4b78f1bcb393389) --- modules/mbedtls/crypto_mbedtls.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/modules/mbedtls/crypto_mbedtls.cpp b/modules/mbedtls/crypto_mbedtls.cpp index 47c0dc9bb6a..de6dfbe0d26 100644 --- a/modules/mbedtls/crypto_mbedtls.cpp +++ b/modules/mbedtls/crypto_mbedtls.cpp @@ -419,9 +419,19 @@ Ref CryptoMbedTLS::generate_self_signed_certificate(Ref 0) { + int to_read = MIN(left, MBEDTLS_CTR_DRBG_MAX_REQUEST); + int ret = mbedtls_ctr_drbg_random(&ctr_drbg, out.ptrw() + pos, to_read); + ERR_FAIL_COND_V_MSG(ret != 0, PackedByteArray(), vformat("Failed to generate %d random bytes(s). Error: %d.", p_bytes, ret)); + left -= to_read; + pos += to_read; + } return out; }