forked from nuttx/nuttx-update
libc: implement getentropy function
specify here: https://man7.org/linux/man-pages/man3/getentropy.3.html Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
parent
0bd93a2a76
commit
de0dd2f569
3 changed files with 86 additions and 1 deletions
|
@ -410,6 +410,8 @@ gid_t getegid(void);
|
|||
int setreuid(uid_t ruid, uid_t euid);
|
||||
int setregid(gid_t rgid, gid_t egid);
|
||||
|
||||
int getentropy(FAR void *buffer, size_t length);
|
||||
|
||||
#undef EXTERN
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
# Add the unistd C files to the build
|
||||
|
||||
CSRCS += lib_access.c lib_daemon.c lib_swab.c lib_pathconf.c lib_sysconf.c
|
||||
CSRCS += lib_getopt_common.c lib_getopt.c lib_getopt_long.c
|
||||
CSRCS += lib_getentropy.c lib_getopt_common.c lib_getopt.c lib_getopt_long.c
|
||||
CSRCS += lib_getopt_longonly.c lib_getoptvars.c lib_getoptargp.c
|
||||
CSRCS += lib_getopterrp.c lib_getoptindp.c lib_getoptoptp.c lib_times.c
|
||||
CSRCS += lib_alarm.c lib_fstatvfs.c lib_statvfs.c lib_sleep.c lib_nice.c
|
||||
|
|
83
libs/libc/unistd/lib_getentropy.c
Normal file
83
libs/libc/unistd/lib_getentropy.c
Normal file
|
@ -0,0 +1,83 @@
|
|||
/****************************************************************************
|
||||
* libs/libc/unistd/lib_getentropy.c
|
||||
*
|
||||
* 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 <sys/random.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: getentropy
|
||||
*
|
||||
* Description:
|
||||
* The getentropy() function writes length bytes of high-quality
|
||||
* random data to the buffer starting at the location pointed to by
|
||||
* buffer. The maximum permitted value for the length argument is
|
||||
* 256.
|
||||
*
|
||||
* A successful call to getentropy() always provides the requested
|
||||
* number of bytes of entropy.
|
||||
*
|
||||
* Input Parameters:
|
||||
* buffer - Buffer for returned random bytes
|
||||
* length - Number of bytes requested.
|
||||
*
|
||||
* Returned Value:
|
||||
* On success, this function returns zero. On error, -1 is
|
||||
* returned, and errno is set to indicate the error.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int getentropy(FAR void *buffer, size_t length)
|
||||
{
|
||||
FAR char *pos = buffer;
|
||||
|
||||
if (length > 256)
|
||||
{
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (length > 0)
|
||||
{
|
||||
int ret = getrandom(pos, length, 0);
|
||||
if (ret < 0)
|
||||
{
|
||||
if (errno == EINTR)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
pos += ret;
|
||||
length -= ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue