From 1054c3abc7cbcba39afe293ea4a8dd921f6a3c18 Mon Sep 17 00:00:00 2001 From: batrick Date: Thu, 19 Jul 2012 02:47:01 +0000 Subject: [PATCH] RC4 cipher for work Ron is doing. --- nse_openssl.cc | 35 +++++++++++++++++++++++++++++++++++ nselib/openssl.luadoc | 9 +++++++++ 2 files changed, 44 insertions(+) diff --git a/nse_openssl.cc b/nse_openssl.cc index d58979bf1..d5b60024e 100644 --- a/nse_openssl.cc +++ b/nse_openssl.cc @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -472,6 +473,38 @@ static int l_DES_string_to_key(lua_State *L) /** DES_string_to_key( string data return 1; } +static int l_rc4_options (lua_State *L) +{ + lua_pushstring(L, RC4_options()); + return 1; +} + +static int l_rc4_encrypt (lua_State *L) +{ + RC4_KEY *key = (RC4_KEY *) lua_touserdata(L, lua_upvalueindex(1)); + size_t len; + const char *indata = luaL_checklstring(L, 1, &len); + unsigned char *outdata = (unsigned char *) lua_newuserdata(L, sizeof(unsigned char)*len); + + RC4(key, len, (const unsigned char *)indata, outdata); + lua_pushlstring(L, (const char *)outdata, len); + + return 1; +} + +static int l_rc4 (lua_State *L) +{ + size_t len; + const char *data = luaL_checklstring(L, 1, &len); + + lua_newuserdata(L, sizeof(RC4_KEY)); + RC4_set_key((RC4_KEY *)lua_touserdata(L, -1), (int)len, (const unsigned char *)data); + + lua_pushcclosure(L, l_rc4_encrypt, 1); + + return 1; +} + static const struct luaL_Reg bignum_methods[] = { { "num_bits", l_bignum_num_bits }, { "num_bytes", l_bignum_num_bytes }, @@ -515,6 +548,8 @@ static const struct luaL_Reg openssllib[] = { { "DES_string_to_key", l_DES_string_to_key }, { "supported_digests", l_supported_digests }, { "supported_ciphers", l_supported_ciphers }, + { "rc4_options", l_rc4_options }, + { "rc4", l_rc4 }, { NULL, NULL } }; diff --git a/nselib/openssl.luadoc b/nselib/openssl.luadoc index 744d5f629..bf2558ac7 100644 --- a/nselib/openssl.luadoc +++ b/nselib/openssl.luadoc @@ -177,3 +177,12 @@ function supported_digests() -- @param data A 7-byte string. -- @return An 8-byte string. function DES_string_to_key(data) + +--- Returns options for RC4. +-- @return Option string. +function rc4_options() + +--- Function which generates an RC4 cipher function with the given key. +-- @param key_data The key for the cipher. +-- @return A function which performs the RC4 stream cipher on an input string and returns the result. +function rc4(key_data)