Comparison

util-src/crand.c @ 7915:e3d3ebd417f4

util.crand: Throw error if OpenSSLs RNG is not seeded
author Kim Alvefur <zash@zash.se>
date Sat, 25 Feb 2017 17:57:22 +0100
parent 7832:d02ef0ae94af
child 7918:12e5a54907b6
comparison
equal deleted inserted replaced
7914:a6eb3b6bf903 7915:e3d3ebd417f4
65 65
66 #elif defined(WITH_ARC4RANDOM) 66 #elif defined(WITH_ARC4RANDOM)
67 arc4random_buf(buf, len); 67 arc4random_buf(buf, len);
68 ret = len; 68 ret = len;
69 #elif defined(WITH_OPENSSL) 69 #elif defined(WITH_OPENSSL)
70 if(!RAND_status()) {
71 lua_pushliteral(L, "OpenSSL PRNG not seeded");
72 lua_error(L);
73 }
74
70 ret = RAND_bytes(buf, len); 75 ret = RAND_bytes(buf, len);
71 76
72 if(ret == 1) { 77 if(ret == 1) {
73 ret = len; 78 ret = len;
74 } else { 79 } else {
85 90
86 int luaopen_util_crand(lua_State *L) { 91 int luaopen_util_crand(lua_State *L) {
87 #if (LUA_VERSION_NUM > 501) 92 #if (LUA_VERSION_NUM > 501)
88 luaL_checkversion(L); 93 luaL_checkversion(L);
89 #endif 94 #endif
95
90 lua_newtable(L); 96 lua_newtable(L);
91 lua_pushcfunction(L, Lrandom); 97 lua_pushcfunction(L, Lrandom);
92 lua_setfield(L, -2, "bytes"); 98 lua_setfield(L, -2, "bytes");
93 99
94 #if defined(WITH_GETRANDOM) 100 #if defined(WITH_GETRANDOM)
98 #elif defined(WITH_OPENSSL) 104 #elif defined(WITH_OPENSSL)
99 lua_pushstring(L, "OpenSSL"); 105 lua_pushstring(L, "OpenSSL");
100 #endif 106 #endif
101 lua_setfield(L, -2, "_source"); 107 lua_setfield(L, -2, "_source");
102 108
103 #if defined(WITH_OPENSSL) && defined(_WIN32)
104 /* TODO Do we need to seed this on Windows? */
105 #endif
106
107 return 1; 109 return 1;
108 } 110 }
109 111