diff options
| author | William Ahern <william@server.local> | 2013-12-04 14:56:32 -0800 |
|---|---|---|
| committer | William Ahern <william@server.local> | 2013-12-04 14:56:32 -0800 |
| commit | 8226aea35589110a6633077f40fd2c6433dc977d (patch) | |
| tree | 6ebc5e2fd94084cf01cb871399716e1b1f353dc7 | |
| parent | 22184c902bf2670fea9fd1cc6e69e16f9ae91f5b (diff) | |
| download | luaossl-8226aea35589110a6633077f40fd2c6433dc977d.tar.gz luaossl-8226aea35589110a6633077f40fd2c6433dc977d.tar.bz2 luaossl-8226aea35589110a6633077f40fd2c6433dc977d.zip | |
-n
add openssl.rand modue
| -rw-r--r-- | GNUmakefile | 3 | ||||
| -rw-r--r-- | openssl.c | 51 | ||||
| -rw-r--r-- | openssl.rand.lua | 3 |
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) = \ | |||
| 87 | $$(DESTDIR)$(3)/openssl/ssl.lua \ | 87 | $$(DESTDIR)$(3)/openssl/ssl.lua \ |
| 88 | $$(DESTDIR)$(3)/openssl/digest.lua \ | 88 | $$(DESTDIR)$(3)/openssl/digest.lua \ |
| 89 | $$(DESTDIR)$(3)/openssl/hmac.lua \ | 89 | $$(DESTDIR)$(3)/openssl/hmac.lua \ |
| 90 | $$(DESTDIR)$(3)/openssl/cipher.lua | 90 | $$(DESTDIR)$(3)/openssl/cipher.lua \ |
| 91 | $$(DESTDIR)$(3)/openssl/rand.lua | ||
| 91 | 92 | ||
| 92 | .SECONDARY: liblua$(1)-openssl-install openssl$(1)-install | 93 | .SECONDARY: liblua$(1)-openssl-install openssl$(1)-install |
| 93 | 94 | ||
| @@ -48,6 +48,7 @@ | |||
| 48 | #include <openssl/pem.h> | 48 | #include <openssl/pem.h> |
| 49 | #include <openssl/ssl.h> | 49 | #include <openssl/ssl.h> |
| 50 | #include <openssl/hmac.h> | 50 | #include <openssl/hmac.h> |
| 51 | #include <openssl/rand.h> | ||
| 51 | 52 | ||
| 52 | #include <lua.h> | 53 | #include <lua.h> |
| 53 | #include <lualib.h> | 54 | #include <lualib.h> |
| @@ -3754,6 +3755,56 @@ int luaopen__openssl_cipher(lua_State *L) { | |||
| 3754 | } /* luaopen__openssl_cipher() */ | 3755 | } /* luaopen__openssl_cipher() */ |
| 3755 | 3756 | ||
| 3756 | 3757 | ||
| 3758 | /* | ||
| 3759 | * Rand - openssl.rand | ||
| 3760 | * | ||
| 3761 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
| 3762 | |||
| 3763 | static int rand_bytes(lua_State *L) { | ||
| 3764 | int size = luaL_checkint(L, 1); | ||
| 3765 | luaL_Buffer B; | ||
| 3766 | int count = 0, n; | ||
| 3767 | |||
| 3768 | luaL_buffinit(L, &B); | ||
| 3769 | |||
| 3770 | while (count < size) { | ||
| 3771 | n = MIN((size - count), LUAL_BUFFERSIZE); | ||
| 3772 | |||
| 3773 | if (!RAND_bytes((void *)luaL_prepbuffer(&B), n)) | ||
| 3774 | return throwssl(L, "rand.bytes"); | ||
| 3775 | |||
| 3776 | luaL_addsize(&B, n); | ||
| 3777 | count += n; | ||
| 3778 | } | ||
| 3779 | |||
| 3780 | luaL_pushresult(&B); | ||
| 3781 | |||
| 3782 | return 1; | ||
| 3783 | } /* rand_bytes() */ | ||
| 3784 | |||
| 3785 | |||
| 3786 | static int rand_ready(lua_State *L) { | ||
| 3787 | lua_pushboolean(L, RAND_status() == 1); | ||
| 3788 | |||
| 3789 | return 1; | ||
| 3790 | } /* rand_ready() */ | ||
| 3791 | |||
| 3792 | |||
| 3793 | static const luaL_Reg rand_globals[] = { | ||
| 3794 | { "bytes", &rand_bytes }, | ||
| 3795 | { "ready", &rand_ready }, | ||
| 3796 | { NULL, NULL }, | ||
| 3797 | }; | ||
| 3798 | |||
| 3799 | int luaopen__openssl_rand(lua_State *L) { | ||
| 3800 | initall(L); | ||
| 3801 | |||
| 3802 | luaL_newlib(L, rand_globals); | ||
| 3803 | |||
| 3804 | return 1; | ||
| 3805 | } /* luaopen__openssl_rand() */ | ||
| 3806 | |||
| 3807 | |||
| 3757 | static void initall(lua_State *L) { | 3808 | static void initall(lua_State *L) { |
| 3758 | ERR_load_crypto_strings(); | 3809 | ERR_load_crypto_strings(); |
| 3759 | OpenSSL_add_all_algorithms(); | 3810 | 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 @@ | |||
| 1 | local ctx = require"_openssl.rand" | ||
| 2 | |||
| 3 | return ctx | ||
