mirror of
https://github.com/godotengine/godot.git
synced 2024-11-22 04:06:14 +00:00
Merge pull request #81884 from Faless/crypto/random_in_chunks
[Crypto] Fix `generate_random_bytes` for large chunks
This commit is contained in:
commit
3514053fee
@ -419,9 +419,19 @@ Ref<X509Certificate> CryptoMbedTLS::generate_self_signed_certificate(Ref<CryptoK
|
||||
}
|
||||
|
||||
PackedByteArray CryptoMbedTLS::generate_random_bytes(int p_bytes) {
|
||||
ERR_FAIL_COND_V(p_bytes < 0, PackedByteArray());
|
||||
PackedByteArray out;
|
||||
out.resize(p_bytes);
|
||||
mbedtls_ctr_drbg_random(&ctr_drbg, out.ptrw(), p_bytes);
|
||||
int left = p_bytes;
|
||||
int pos = 0;
|
||||
// Ensure we generate random in chunks of no more than MBEDTLS_CTR_DRBG_MAX_REQUEST bytes or mbedtls_ctr_drbg_random will fail.
|
||||
while (left > 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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user