From ff2d528a0a6943bd4247f3c122c145a5b19f0387 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 4 Jul 2018 18:49:03 -0500 Subject: Add xgetrandom() with probe for new system call (else open/read /dev/{,u}random) --- lib/lib.c | 4 +--- lib/password.c | 6 ++---- lib/portability.c | 11 +++++++++++ lib/portability.h | 5 +++++ 4 files changed, 19 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/lib.c b/lib/lib.c index 88dd13a0..6d75e0f3 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -1144,9 +1144,7 @@ int qstrcmp(const void *a, const void *b) void create_uuid(char *uuid) { // "Set all the ... bits to randomly (or pseudo-randomly) chosen values". - int fd = xopenro("/dev/urandom"); - xreadall(fd, uuid, 16); - close(fd); + xgetrandom(uuid, 16, 0); // "Set the four most significant bits ... of the time_hi_and_version // field to the 4-bit version number [4]". diff --git a/lib/password.c b/lib/password.c index eab2d669..20c2f481 100644 --- a/lib/password.c +++ b/lib/password.c @@ -8,7 +8,7 @@ #include "toys.h" #include -// generate appropriate random salt string for given encryption algorithm. +// generate ID prefix and random salt for given encryption algorithm. int get_salt(char *salt, char *algo) { struct { @@ -24,9 +24,7 @@ int get_salt(char *salt, char *algo) if (al[i].id) s += sprintf(s, "$%c$", '0'+al[i].id); // Read appropriate number of random bytes for salt - i = xopenro("/dev/urandom"); - xreadall(i, libbuf, ((len*6)+7)/8); - close(i); + xgetrandom(libbuf, ((len*6)+7)/8, 0); // Grab 6 bit chunks and convert to characters in ./0-9a-zA-Z for (i=0; i +#endif +void xgetrandom(void *buf, unsigned len, unsigned flags); -- cgit v1.2.3