3434# include <stdio.h>
3535# include <sys/ioctl.h>
3636# if defined(__linux__ ) && defined(__GLIBC__ ) && ((__GLIBC__ > 2 ) || (__GLIBC_MINOR__ > 24 ))
37+ # define USE_GLIBC
3738# include <sys/random.h>
3839# endif /* defined(__linux__) && defined(__GLIBC__) && ((__GLIBC__ > 2) || (__GLIBC_MINOR__ > 24)) */
3940# include <sys/stat.h>
@@ -92,31 +93,15 @@ static int randombytes_wasi_randombytes(void *buf, size_t n) {
9293}
9394#endif /* defined(__wasi__) */
9495
95- #if defined(__linux__ ) && defined(__GLIBC__ ) && ((__GLIBC__ > 2 ) || (__GLIBC_MINOR__ > 24 ))
96- static int randombytes_linux_randombytes_getrandom_function (void * buf , size_t n )
97- {
98- /* I have thought about using a separate PRF, seeded by getrandom, but
99- * it turns out that the performance of getrandom is good enough
100- * (250 MB/s on my laptop).
101- */
102- size_t offset = 0 , chunk ;
103- int ret ;
104- while (n > 0 ) {
105- /* getrandom does not allow chunks larger than 33554431 */
106- chunk = n <= 33554431 ? n : 33554431 ;
107- do {
108- ret = getrandom ((char * )buf + offset , chunk , 0 );
109- } while (ret == -1 && errno == EINTR );
110- if (ret < 0 ) return ret ;
111- offset += ret ;
112- n -= ret ;
113- }
114- assert (n == 0 );
115- return 0 ;
96+ #if defined(__linux__ ) && (defined(USE_GLIBC ) || defined(SYS_getrandom ))
97+ # if defined(USE_GLIBC )
98+ // getrandom is declared in glibc.
99+ # elif defined(SYS_getrandom )
100+ static ssize_t getrandom (void * buf , size_t buflen , unsigned int flags ) {
101+ return syscall (SYS_getrandom , buf , buflen , flags );
116102}
103+ # endif
117104
118-
119- #elif defined(__linux__ ) && defined(SYS_getrandom )
120105static int randombytes_linux_randombytes_getrandom (void * buf , size_t n )
121106{
122107 /* I have thought about using a separate PRF, seeded by getrandom, but
@@ -129,7 +114,7 @@ static int randombytes_linux_randombytes_getrandom(void *buf, size_t n)
129114 /* getrandom does not allow chunks larger than 33554431 */
130115 chunk = n <= 33554431 ? n : 33554431 ;
131116 do {
132- ret = syscall ( SYS_getrandom , (char * )buf + offset , chunk , 0 );
117+ ret = getrandom ( (char * )buf + offset , chunk , 0 );
133118 } while (ret == -1 && errno == EINTR );
134119 if (ret < 0 ) return ret ;
135120 offset += ret ;
@@ -138,7 +123,7 @@ static int randombytes_linux_randombytes_getrandom(void *buf, size_t n)
138123 assert (n == 0 );
139124 return 0 ;
140125}
141- #endif /* defined(__linux__) && (defined(SYS_getrandom) or glibc version > 2.24) */
126+ #endif // defined(__linux__) && (defined(USE_GLIBC) || defined(SYS_getrandom))
142127
143128
144129#if defined(__linux__ ) && !defined(SYS_getrandom )
@@ -323,10 +308,10 @@ int randombytes(void *buf, size_t n)
323308# pragma message("Using crypto api from NodeJS")
324309 return randombytes_js_randombytes_nodejs (buf , n );
325310#elif defined(__linux__ )
326- # if defined(__GLIBC__ ) && (( __GLIBC__ > 2 ) || ( __GLIBC_MINOR__ > 24 ) )
311+ # if defined(USE_GLIBC )
327312# pragma message("Using getrandom function call")
328313 /* Use getrandom system call */
329- return randombytes_linux_randombytes_getrandom_function (buf , n );
314+ return randombytes_linux_randombytes_getrandom (buf , n );
330315# elif defined(SYS_getrandom )
331316# pragma message("Using getrandom system call")
332317 /* Use getrandom system call */
0 commit comments