~ chicken-core (chicken-5) a72111962e59c1e4d8403b7c93ee3d3379cf4f6e
commit a72111962e59c1e4d8403b7c93ee3d3379cf4f6e Author: felix <felix@call-with-current-continuation.org> AuthorDate: Fri Nov 3 18:04:41 2017 +0100 Commit: felix <felix@call-with-current-continuation.org> CommitDate: Fri Nov 3 18:04:41 2017 +0100 perform reading of random bytes inside runtime.c diff --git a/extras.scm b/extras.scm index f4cccce5..af39c2b2 100644 --- a/extras.scm +++ b/extras.scm @@ -685,16 +685,10 @@ (else (make-string (or size nstate))))) (r (##core#inline "C_random_bytes" dest (or size (##sys#size dest))))) - (cond ((eq? -1 r) - (##sys#error 'random-bytes "error while obtaining random bytes")) - ((not r) ; no syscall or API function, read from /dev/urandom... - (unless in - (if (file-exists? "/dev/urandom") - (set! in (open-input-file "/dev/urandom" #:binary)) - (##sys#error 'random-bytes "no entropy source available"))) - (read-string! nstate dest in) - (unless (eq? buf dest) - (##core#inline "C_string_to_bytevector" dest)))) - dest)))) + (when (eq? -1 r) + (##sys#error 'random-bytes "unable to read random bytes")) + (unless (eq? buf dest) + (##core#inline "C_string_to_bytevector" dest)) + dest)))) ) diff --git a/runtime.c b/runtime.c index 63c0e070..b7c06c2a 100644 --- a/runtime.c +++ b/runtime.c @@ -12530,6 +12530,7 @@ C_i_pending_interrupt(C_word dummy) #ifdef __linux__ # include <sys/syscall.h> +# include <fcntl.h> #elif defined(__OpenBSD__) # include <sys/systm.h> #endif @@ -12558,7 +12559,22 @@ C_word C_random_bytes(C_word buf, C_word size) if(!RtlGenRandom((PVOID)C_data_pointer(buf), (LONG)count)) r = -1; #else - return C_SCHEME_FALSE; + static int fd = 0; + + if(fd == 0) { + fd = open("/dev/urandom", O_RDONLY); + + if(fd == -1) { + fd = 0; + return C_fix(-1); + } + } + + do { + r = read(fd, C_data_pointer(buf), count); + } while(r == -1 && errno == EINTR); + + r = 0; #endif return C_fix(r); }Trap