Comparison

util-src/crand.c @ 8443:980885ba062c

util.crand: Try getrandom() again until buffer is filled
author Kim Alvefur <zash@zash.se>
date Sat, 02 Dec 2017 10:58:37 +0100
parent 8425:91c220f43826
child 8444:adb079840714
comparison
equal deleted inserted replaced
8442:3a390fa561bf 8443:980885ba062c
66 #if defined(WITH_GETRANDOM) 66 #if defined(WITH_GETRANDOM)
67 /* 67 /*
68 * This acts like a read from /dev/urandom with the exception that it 68 * This acts like a read from /dev/urandom with the exception that it
69 * *does* block if the entropy pool is not yet initialized. 69 * *does* block if the entropy pool is not yet initialized.
70 */ 70 */
71 ret = getrandom(buf, len, 0); 71 int left = len;
72 char *b = buf;
72 73
73 if(ret < 0) { 74 do {
74 lua_pushstring(L, strerror(errno)); 75 ret = getrandom(b, left, 0);
75 return lua_error(L); 76
76 } 77 if(ret < 0) {
78 lua_pushstring(L, strerror(errno));
79 return lua_error(L);
80 }
81
82 b += ret;
83 left -= ret;
84 } while(left > 0);
85
86 ret = len;
77 87
78 #elif defined(WITH_ARC4RANDOM) 88 #elif defined(WITH_ARC4RANDOM)
79 arc4random_buf(buf, len); 89 arc4random_buf(buf, len);
80 ret = len; 90 ret = len;
81 #elif defined(WITH_OPENSSL) 91 #elif defined(WITH_OPENSSL)