From 69550f9be98c8fc0bdbf6118dee9bb0df403a314 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Fri, 4 Feb 2022 13:35:13 +0100 Subject: [PATCH] allow reading arbitrary number of bytes from `xmpp_rand_bytes()` If we expose the API it shouldn't be limited. Signed-off-by: Steffen Jaeckel --- src/rand.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/rand.c b/src/rand.c index c21fbe4..58de330 100644 --- a/src/rand.c +++ b/src/rand.c @@ -284,12 +284,18 @@ void xmpp_rand_free(xmpp_ctx_t *ctx, xmpp_rand_t *rand) void xmpp_rand_bytes(xmpp_rand_t *rand, unsigned char *output, size_t len) { int rc; - - rc = Hash_DRBG_Generate(&rand->ctx, (uint8_t *)output, len); - if (rc == RESEED_NEEDED) { - xmpp_rand_reseed(rand); - rc = Hash_DRBG_Generate(&rand->ctx, (uint8_t *)output, len); - assert(rc == 0); + size_t gen, tot = 0; + while (tot < len) { + gen = len - tot; + if (gen > GENERATE_MAX) + gen = GENERATE_MAX; + rc = Hash_DRBG_Generate(&rand->ctx, (uint8_t *)output + tot, gen); + if (rc == RESEED_NEEDED) { + xmpp_rand_reseed(rand); + rc = Hash_DRBG_Generate(&rand->ctx, (uint8_t *)output + tot, gen); + assert(rc == 0); + } + tot += gen; } }