Skip to content

Commit

Permalink
Merge branch 'feature/newlib_getentropy' into 'master'
Browse files Browse the repository at this point in the history
feat(newlib): Implement getentropy() function

Closes IDFGH-10746 and IDF-7383

See merge request espressif/esp-idf!25192
  • Loading branch information
gerekon committed Aug 18, 2023
2 parents 9fe2757 + f8e020b commit 3247253
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
11 changes: 7 additions & 4 deletions components/cxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions components/newlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(srcs
"poll.c"
"pthread.c"
"random.c"
"getentropy.c"
"reent_init.c"
"newlib_init.c"
"syscalls.c"
Expand Down
30 changes: 30 additions & 0 deletions components/newlib/getentropy.c
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions components/newlib/platform_include/sys/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
19 changes: 19 additions & 0 deletions docs/en/api-reference/system/random.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3247253

Please sign in to comment.