diff --git a/include/unistd.h b/include/unistd.h index b383f20553..d2fd98827b 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -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) } diff --git a/libs/libc/unistd/Make.defs b/libs/libc/unistd/Make.defs index 6a561bec51..35e9e6d5ec 100644 --- a/libs/libc/unistd/Make.defs +++ b/libs/libc/unistd/Make.defs @@ -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 diff --git a/libs/libc/unistd/lib_getentropy.c b/libs/libc/unistd/lib_getentropy.c new file mode 100644 index 0000000000..b47a646a50 --- /dev/null +++ b/libs/libc/unistd/lib_getentropy.c @@ -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 +#include +#include + +/**************************************************************************** + * 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; +}