You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by pk...@apache.org on 2022/11/09 05:41:25 UTC

[incubator-nuttx] branch master updated: random_pool:add a new api arc4random

This is an automated email from the ASF dual-hosted git repository.

pkarashchenko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 0c26658906 random_pool:add a new api arc4random
0c26658906 is described below

commit 0c26658906ef9d7bc44992ee1f73cabeaeb0c085
Author: anjiahao <an...@xiaomi.com>
AuthorDate: Wed Nov 9 11:41:22 2022 +0800

    random_pool:add a new api arc4random
    
    Signed-off-by: anjiahao <an...@xiaomi.com>
---
 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 */