aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--GNUmakefile3
-rw-r--r--openssl.c51
-rw-r--r--openssl.rand.lua3
3 files changed, 56 insertions, 1 deletions
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 <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/hmac.h>
+#include <openssl/rand.h>
#include <lua.h>
#include <lualib.h>
@@ -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