aboutsummaryrefslogtreecommitdiffstats
path: root/openssl.c
diff options
context:
space:
mode:
authorLibravatarLibravatar William Ahern <william@server.local> 2013-12-04 14:56:32 -0800
committerLibravatarLibravatar William Ahern <william@server.local> 2013-12-04 14:56:32 -0800
commit8226aea35589110a6633077f40fd2c6433dc977d (patch)
tree6ebc5e2fd94084cf01cb871399716e1b1f353dc7 /openssl.c
parent22184c902bf2670fea9fd1cc6e69e16f9ae91f5b (diff)
downloadluaossl-8226aea35589110a6633077f40fd2c6433dc977d.tar.gz
luaossl-8226aea35589110a6633077f40fd2c6433dc977d.tar.bz2
luaossl-8226aea35589110a6633077f40fd2c6433dc977d.zip
-n
add openssl.rand modue
Diffstat (limited to 'openssl.c')
-rw-r--r--openssl.c51
1 files changed, 51 insertions, 0 deletions
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();