From 8226aea35589110a6633077f40fd2c6433dc977d Mon Sep 17 00:00:00 2001 From: William Ahern Date: Wed, 4 Dec 2013 14:56:32 -0800 Subject: -n add openssl.rand modue --- openssl.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'openssl.c') diff --git a/openssl.c b/openssl.c index f8e5ef0..5c46d9c 100644 --- a/openssl.c +++ b/openssl.c @@ -48,6 +48,7 @@ #include #include #include +#include #include #include @@ -3754,6 +3755,56 @@ int luaopen__openssl_cipher(lua_State *L) { } /* luaopen__openssl_cipher() */ +/* + * Rand - openssl.rand + * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +static int rand_bytes(lua_State *L) { + int size = luaL_checkint(L, 1); + luaL_Buffer B; + int count = 0, n; + + luaL_buffinit(L, &B); + + while (count < size) { + n = MIN((size - count), LUAL_BUFFERSIZE); + + if (!RAND_bytes((void *)luaL_prepbuffer(&B), n)) + return throwssl(L, "rand.bytes"); + + luaL_addsize(&B, n); + count += n; + } + + luaL_pushresult(&B); + + return 1; +} /* rand_bytes() */ + + +static int rand_ready(lua_State *L) { + lua_pushboolean(L, RAND_status() == 1); + + return 1; +} /* rand_ready() */ + + +static const luaL_Reg rand_globals[] = { + { "bytes", &rand_bytes }, + { "ready", &rand_ready }, + { NULL, NULL }, +}; + +int luaopen__openssl_rand(lua_State *L) { + initall(L); + + luaL_newlib(L, rand_globals); + + return 1; +} /* luaopen__openssl_rand() */ + + static void initall(lua_State *L) { ERR_load_crypto_strings(); OpenSSL_add_all_algorithms(); -- cgit v1.2.3-55-g6feb