feat(newlib): Implement getentropy() function

Closes https://github.com/espressif/esp-idf/issues/11963
pull/11739/merge
Alexey Gerenkov 2023-08-03 16:25:53 +03:00
rodzic 172f086c6e
commit f8e020b1d7
5 zmienionych plików z 58 dodań i 4 usunięć

Wyświetl plik

@ -53,15 +53,18 @@ target_link_libraries(${COMPONENT_LIB} INTERFACE "-u __cxa_guard_dummy")
# Furthermore, force libcxx to appear later than libgcc because some libgcc unwind code is wrapped, if C++
# exceptions are disabled. libcxx (this component) provides the unwind code wrappers.
# This is to prevent linking of libgcc's unwind code which considerably increases the binary size.
# Also force libnewlib to appear later than libstdc++ in link line since libstdc++ depends on
# some functions in libnewlib, e.g. getentropy().
idf_component_get_property(pthread pthread COMPONENT_LIB)
idf_component_get_property(newlib newlib COMPONENT_LIB)
idf_component_get_property(cxx cxx COMPONENT_LIB)
add_library(stdcpp_pthread INTERFACE)
add_library(stdcpp_deps INTERFACE)
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
target_link_libraries(stdcpp_pthread INTERFACE stdc++ c $<TARGET_FILE:${pthread}>)
target_link_libraries(stdcpp_deps INTERFACE stdc++ c $<TARGET_FILE:${pthread}> $<TARGET_FILE:${newlib}>)
else()
target_link_libraries(stdcpp_pthread INTERFACE stdc++ $<TARGET_FILE:${pthread}>)
target_link_libraries(stdcpp_deps INTERFACE stdc++ $<TARGET_FILE:${pthread}> $<TARGET_FILE:${newlib}>)
endif()
target_link_libraries(${COMPONENT_LIB} PUBLIC stdcpp_pthread)
target_link_libraries(${COMPONENT_LIB} PUBLIC stdcpp_deps)
add_library(libgcc_cxx INTERFACE)
target_link_libraries(libgcc_cxx INTERFACE ${CONFIG_COMPILER_RT_LIB_NAME} $<TARGET_FILE:${cxx}>)
target_link_libraries(${COMPONENT_LIB} PUBLIC libgcc_cxx)

Wyświetl plik

@ -12,6 +12,7 @@ set(srcs
"poll.c"
"pthread.c"
"random.c"
"getentropy.c"
"reent_init.c"
"newlib_init.c"
"syscalls.c"

Wyświetl plik

@ -0,0 +1,30 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <sys/random.h>
#include <errno.h>
int getentropy(void *buffer, size_t length)
{
ssize_t ret;
if (buffer == NULL) {
errno = EFAULT;
return -1;
}
if (length > 256) {
errno = EIO;
return -1;
}
ret = getrandom(buffer, length, 0);
if (ret == -1) {
return -1;
}
return 0;
}

Wyświetl plik

@ -16,6 +16,7 @@ extern "C" {
int truncate(const char *, off_t __length);
int gethostname(char *__name, size_t __len);
int getentropy(void *buffer, size_t length);
#ifdef __cplusplus
}

Wyświetl plik

@ -62,5 +62,24 @@ The ``flags`` argument is ignored, this function is always non-blocking but the
Return value is -1 (with ``errno`` set to ``EFAULT``) if the ``buf`` argument is NULL, and equal to ``buflen`` otherwise.
getentropy
----------
A compatible version of the Linux ``getentropy()`` function is also provided for ease of porting:
.. code-block:: c
#include <unistd.h>
int getentropy(void *buffer, size_t length);
This function is implemented by calling :cpp:func:`getrandom` internally.
Strength of any random numbers is dependent on the same conditions described above.
Return value is 0 on success and -1 otherwise with ``errno`` set to:
- ``EFAULT`` if the ``buffer`` argument is NULL.
- ``EIO`` if the ``length`` is more then 256.
.. _Dieharder: https://webhome.phy.duke.edu/~rgb/General/dieharder.php