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 --- GNUmakefile | 3 ++- openssl.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ openssl.rand.lua | 3 +++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 openssl.rand.lua diff --git a/GNUmakefile b/GNUmakefile index dde5a9e..fcf79a6 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -87,7 +87,8 @@ MODS$(1)_$(d) = \ $$(DESTDIR)$(3)/openssl/ssl.lua \ $$(DESTDIR)$(3)/openssl/digest.lua \ $$(DESTDIR)$(3)/openssl/hmac.lua \ - $$(DESTDIR)$(3)/openssl/cipher.lua + $$(DESTDIR)$(3)/openssl/cipher.lua \ + $$(DESTDIR)$(3)/openssl/rand.lua .SECONDARY: liblua$(1)-openssl-install openssl$(1)-install 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(); diff --git a/openssl.rand.lua b/openssl.rand.lua new file mode 100644 index 0000000..70e9d3f --- /dev/null +++ b/openssl.rand.lua @@ -0,0 +1,3 @@ +local ctx = require"_openssl.rand" + +return ctx -- cgit v1.2.3-59-g8ed1b