libc: Refine the arc4random_buf implementation

fill the buffer with getrandom instead random pool
and move the implementation to from crypto to libc

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao
2024-10-25 18:09:30 +08:00
committed by Alan C. Assis
parent b5e5cdd851
commit 32784b0898
17 changed files with 103 additions and 154 deletions
+1 -47
View File
@@ -24,39 +24,10 @@
* Included Files
****************************************************************************/
#include <sys/random.h>
#include <errno.h>
#include <stdlib.h>
#include <uuid.h>
/****************************************************************************
* Private Functions
****************************************************************************/
static int uuid_getrandom(FAR void *buf, size_t size, int flags)
{
FAR char *tmp = buf;
while (size > 0)
{
ssize_t ret = getrandom(tmp, size, flags);
if (ret < 0)
{
if (get_errno() == EINTR)
{
continue;
}
return ret;
}
tmp += ret;
size -= ret;
}
return 0;
}
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -74,24 +45,7 @@ static int uuid_getrandom(FAR void *buf, size_t size, int flags)
void uuid_create(FAR uuid_t *u, FAR uint32_t *status)
{
int ret;
ret = uuid_getrandom(u, sizeof(uuid_t), GRND_RANDOM);
if (ret < 0)
{
ret = uuid_getrandom(u, sizeof(uuid_t), 0);
}
if (ret < 0)
{
FAR unsigned long *beg = (FAR unsigned long *)u;
FAR unsigned long *end = (FAR unsigned long *)(u + 1);
while (beg < end)
{
*beg++ = rand();
}
}
arc4random_buf(u, sizeof(uuid_t));
u->clock_seq_hi_and_reserved &= ~(1 << 6);
u->clock_seq_hi_and_reserved |= (1 << 7);