From 18292456c50f80ec230a8dbcb0edf35ad3465681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Fri, 9 Jan 2015 14:34:13 +0100 Subject: [PATCH] Add support for getrandom() --- ChangeLog | 6 ++++++ library/entropy_poll.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/ChangeLog b/ChangeLog index fd83b9e60..34e9d2703 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ PolarSSL ChangeLog (Sorted per branch, date) += PolarSSL 1.3.10 released ??? + +Features + * Add support for getrandom() syscall on recent Linux kernels with Glibc or + a compatible enough libc (eg uClibc). + = PolarSSL 1.3.9 released 2014-10-20 Security * Lowest common hash was selected from signature_algorithms extension in diff --git a/library/entropy_poll.c b/library/entropy_poll.c index 9ca9e9519..33e75bc65 100644 --- a/library/entropy_poll.c +++ b/library/entropy_poll.c @@ -73,6 +73,43 @@ int platform_entropy_poll( void *data, unsigned char *output, size_t len, } #else /* _WIN32 && !EFIX64 && !EFI32 */ +/* + * Test for Linux getrandom() support. + * Since there is no wrapper in the libc yet, use the generic syscall wrapper + * available in GNU libc and compatible libc's (eg uClibc). + */ +#if defined(__linux__) && defined(__GLIBC__) +#include +#include +#include +#if defined(SYS_getrandom) +#define HAVE_GETRANDOM +static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags ) +{ + return( syscall( SYS_getrandom, buf, buflen, flags ) ); +} +#endif /* SYS_getrandom */ +#endif /* __linux__ */ + +#if defined(HAVE_GETRANDOM) + +#include + +int platform_entropy_poll( void *data, + unsigned char *output, size_t len, size_t *olen ) +{ + int ret; + ((void) data); + + if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 ) + return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); + + *olen = ret; + return( 0 ); +} + +#else /* HAVE_GETRANDOM */ + #include int platform_entropy_poll( void *data, @@ -100,6 +137,7 @@ int platform_entropy_poll( void *data, return( 0 ); } +#endif /* HAVE_GETRANDOM */ #endif /* _WIN32 && !EFIX64 && !EFI32 */ #endif /* !POLARSSL_NO_PLATFORM_ENTROPY */