From 0c26658906ef9d7bc44992ee1f73cabeaeb0c085 Mon Sep 17 00:00:00 2001 From: anjiahao Date: Wed, 9 Nov 2022 11:41:22 +0800 Subject: [PATCH] random_pool:add a new api arc4random Signed-off-by: anjiahao --- crypto/random_pool.c | 24 ++++++++++++++++++++++++ include/stdlib.h | 1 + 2 files changed, 25 insertions(+) diff --git a/crypto/random_pool.c b/crypto/random_pool.c index 5e773a3924..e864d4130b 100644 --- a/crypto/random_pool.c +++ b/crypto/random_pool.c @@ -550,3 +550,27 @@ void arc4random_buf(FAR void *bytes, size_t nbytes) rng_buf_internal(bytes, nbytes); nxmutex_unlock(&g_rng.rd_lock); } + +/**************************************************************************** + * Name: arc4random + * + * Description: + * Returns a single 32-bit value. This is the preferred interface for + * getting random numbers. The traditional /dev/random approach is + * susceptible for things like the attacker exhausting file + * descriptors on purpose. + * + * Note that this function cannot fail, other than by asserting. + * + * Returned Value: + * a random 32-bit value. + * + ****************************************************************************/ + +uint32_t arc4random(void) +{ + uint32_t ret; + + arc4random_buf(&ret, sizeof(ret)); + return ret; +} diff --git a/include/stdlib.h b/include/stdlib.h index 72724061b1..4141b3a71c 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -138,6 +138,7 @@ long random(void); #ifdef CONFIG_CRYPTO_RANDOM_POOL void arc4random_buf(FAR void *bytes, size_t nbytes); +uint32_t arc4random(void); #endif /* Environment variable support */