diff options
Diffstat (limited to 'openssl.c')
-rw-r--r-- | openssl.c | 51 |
1 files changed, 51 insertions, 0 deletions
@@ -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(); |