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
+2
View File
@@ -14,6 +14,8 @@
"aio_suspend","aio.h","defined(CONFIG_FS_AIO)","int","FAR const struct aiocb * const []|FAR const struct aiocb * const *","int","FAR const struct timespec *"
"alarm","unistd.h","!defined(CONFIG_DISABLE_POSIX_TIMERS)","unsigned int","unsigned int"
"alphasort","dirent.h","","int","FAR const struct dirent **","FAR const struct dirent **"
"arc4random","stdlib.h","","uint32_t"
"arc4random_buf","stdlib.h","","void","FAR void *","size_t"
"asprintf","stdio.h","","int","FAR char **","FAR const IPTR char *","..."
"atof","stdlib.h","defined(CONFIG_HAVE_DOUBLE)","double","FAR const char *"
"atoi","stdlib.h","","int","FAR const char *"
1 __assert assert.h void FAR const char * int FAR const char *
14 aio_suspend aio.h defined(CONFIG_FS_AIO) int FAR const struct aiocb * const []|FAR const struct aiocb * const * int FAR const struct timespec *
15 alarm unistd.h !defined(CONFIG_DISABLE_POSIX_TIMERS) unsigned int unsigned int
16 alphasort dirent.h int FAR const struct dirent ** FAR const struct dirent **
17 arc4random stdlib.h uint32_t
18 arc4random_buf stdlib.h void FAR void * size_t
19 asprintf stdio.h int FAR char ** FAR const IPTR char * ...
20 atof stdlib.h defined(CONFIG_HAVE_DOUBLE) double FAR const char *
21 atoi stdlib.h int FAR const char *
+1
View File
@@ -61,6 +61,7 @@ set(SRCS
lib_wctomb.c
lib_mbstowcs.c
lib_wcstombs.c
lib_arc4random.c
lib_atexit.c)
if(CONFIG_PSEUDOTERM)
+1 -1
View File
@@ -30,7 +30,7 @@ CSRCS += lib_strtoll.c lib_strtoul.c lib_strtoull.c lib_strtold.c
CSRCS += lib_checkbase.c lib_mktemp.c lib_mkstemp.c lib_mkdtemp.c
CSRCS += lib_aligned_alloc.c lib_posix_memalign.c lib_valloc.c lib_mblen.c
CSRCS += lib_mbtowc.c lib_wctomb.c lib_mbstowcs.c lib_wcstombs.c lib_atexit.c
CSRCS += lib_reallocarray.c
CSRCS += lib_reallocarray.c lib_arc4random.c
ifeq ($(CONFIG_PSEUDOTERM),y)
CSRCS += lib_ptsname.c lib_ptsnamer.c lib_unlockpt.c lib_openpty.c
+143
View File
@@ -0,0 +1,143 @@
/****************************************************************************
* libs/libc/stdlib/lib_arc4random.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/random.h>
#include <nuttx/clock.h>
#include <nuttx/hashtable.h>
/****************************************************************************
* Private Functions
****************************************************************************/
#if defined(CONFIG_DEV_URANDOM) || defined(CONFIG_DEV_RANDOM)
static int getrandom_all(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;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: arc4random_buf
*
* Description:
* Fill a buffer of arbitrary length with randomness. 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.
*
* Input Parameters:
* bytes - Buffer for returned random bytes
* nbytes - Number of bytes requested.
*
* Returned Value:
* None
*
****************************************************************************/
void arc4random_buf(FAR void *bytes, size_t nbytes)
{
#if defined(CONFIG_DEV_URANDOM) || defined(CONFIG_DEV_RANDOM)
if (getrandom_all(bytes, nbytes, GRND_RANDOM) >= 0)
{
return;
}
if (getrandom_all(bytes, nbytes, 0) >= 0)
{
return;
}
#endif
/* Fallback to hash of clock_systime_ticks(), minus nbytes to avoid getting
* same tick count when looping more than once.
*/
while (nbytes > 0)
{
uint32_t hash = HASH(clock_systime_ticks() - nbytes, 32);
size_t ncopy = MIN(nbytes, sizeof(hash));
memcpy(bytes, &hash, ncopy);
nbytes -= ncopy;
bytes = (FAR uint8_t *)bytes + ncopy;
}
}
/****************************************************************************
* 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;
}
+2 -20
View File
@@ -24,8 +24,8 @@
* Included Files
****************************************************************************/
#include <sys/random.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
/****************************************************************************
@@ -56,30 +56,12 @@
int getentropy(FAR void *buffer, size_t length)
{
FAR char *pos = buffer;
if (length > 256)
{
set_errno(EIO);
return -1;
}
while (length > 0)
{
int ret = getrandom(pos, length, 0);
if (ret < 0)
{
if (get_errno() == EINTR)
{
continue;
}
return ret;
}
pos += ret;
length -= ret;
}
arc4random_buf(buffer, length);
return 0;
}
+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);