diff options
Diffstat (limited to 'lcrypt')
-rw-r--r-- | lcrypt/lcrypt.c | 44 | ||||
-rw-r--r-- | lcrypt/lcrypt_bits.c | 4 | ||||
-rw-r--r-- | lcrypt/lcrypt_ciphers.c | 38 | ||||
-rw-r--r-- | lcrypt/lcrypt_hashes.c | 8 |
4 files changed, 66 insertions, 28 deletions
diff --git a/lcrypt/lcrypt.c b/lcrypt/lcrypt.c index 779504e..987638f 100644 --- a/lcrypt/lcrypt.c +++ b/lcrypt/lcrypt.c @@ -1,20 +1,30 @@ // gcc -Wall -O3 -shared -fPIC -DLITTLE_ENDIAN -DLTM_DESC -DLTC_SOURCE -DUSE_LTM -I/usr/include/tomcrypt -I/usr/include/tommath -lz -lutil -ltomcrypt -ltommath lcrypt.c -o /usr/lib64/lua/5.1/lcrypt.so +#ifdef _WIN32 +#include <windows.h> +#else #include <unistd.h> +#include <sys/time.h> +#endif #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> +#include <malloc.h> #include <errno.h> #include <sys/types.h> -#include <sys/time.h> #include <zlib.h> #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "tomcrypt.h" +#ifdef _WIN32 +#define likely(x) (x) +#define unlikely(x) (x) +#else #define likely(x) __builtin_expect((x),1) #define unlikely(x) __builtin_expect((x),0) +#endif #define ADD_FUNCTION(L,name) { lua_pushstring(L, #name); lua_pushcfunction(L, lcrypt_ ## name); lua_settable(L, -3); } #define ADD_CONSTANT(L,name) { lua_pushstring(L, #name); lua_pushinteger(L, name); lua_settable(L, -3); } @@ -77,7 +87,7 @@ static int lcrypt_fromhex(lua_State *L) { size_t in_length; const unsigned char *in = (const unsigned char*)luaL_checklstring(L, 1, &in_length); - unsigned char result[in_length]; + unsigned char * result = (unsigned char *)alloca(in_length); int i, d = -1, e = -1, pos = 0; for(i = 0; i < (int)in_length; i++) { @@ -207,6 +217,28 @@ static int lcrypt_xor(lua_State *L) return 1; } +#ifdef _WIN32 +
+static const unsigned __int64 epoch = 116444736000000000ULL;
+
+static int gettimeofday(struct timeval * tp, struct timezone * tzp)
+{
+ FILETIME file_time;
+ SYSTEMTIME system_time;
+ ULARGE_INTEGER ularge;
+
+ GetSystemTime(&system_time);
+ SystemTimeToFileTime(&system_time, &file_time);
+ ularge.LowPart = file_time.dwLowDateTime;
+ ularge.HighPart = file_time.dwHighDateTime;
+
+ tp->tv_sec = (long)((ularge.QuadPart - epoch) / 10000000L);
+ tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
+
+ return 0;
+} +#endif + static int lcrypt_time(lua_State *L) { double ret; @@ -217,12 +249,18 @@ static int lcrypt_time(lua_State *L) return(1); } +#ifdef UNICODE
+#define _T(x) L##x
+#else
+#define _T(x) x
+#endif + static int lcrypt_random(lua_State *L) { int len = luaL_checkint(L, 1); char *buffer = lcrypt_malloc(L, len); #ifdef _WIN32 - HMODULE hLib = LoadLibrary("ADVAPI32.DLL"); + HMODULE hLib = LoadLibrary((LPCTSTR)_T("ADVAPI32.DLL")); if (unlikely(!hLib)) { lua_pushstring(L, "Unable to open ADVAPI32.DLL"); diff --git a/lcrypt/lcrypt_bits.c b/lcrypt/lcrypt_bits.c index 0ea7851..9712933 100644 --- a/lcrypt/lcrypt_bits.c +++ b/lcrypt/lcrypt_bits.c @@ -126,7 +126,7 @@ static int lcrypt_bget(lua_State *L) else if(type == B_STR) { int len = (bits + 7) / 8; - uint8_t data[len]; + uint8_t * data = (uint8_t *)alloca(len); memset(data, 0, len); copy_bits(data, 0, in, offset, bits); lua_pushlstring(L, (char*)data, len); @@ -176,7 +176,7 @@ static int lcrypt_bput(lua_State *L) len += bits; } len = (len + 7) / 8; - uint8_t ret[len]; + uint8_t * ret = (uint8_t *)alloca(len); memset(ret, 0, len); for(i = 1; i <= argc; i += 3) diff --git a/lcrypt/lcrypt_ciphers.c b/lcrypt/lcrypt_ciphers.c index dc8c47b..4dc17ae 100644 --- a/lcrypt/lcrypt_ciphers.c +++ b/lcrypt/lcrypt_ciphers.c @@ -33,7 +33,7 @@ static int lcrypt_cipher_ ## name (lua_State *L) static int lcrypt_cipher_ ## _name ## _index(lua_State *L) \ { \ symmetric_ ## NAME *skey = luaL_checkudata(L, 1, "LCRYPT_CIPHER_" #NAME); \ - if(unlikely(skey->cipher < 0)) return 0; \ + if(unlikely(skey->cipher < 0)) return 0;; \ const char *index = luaL_checkstring(L, 2); \ if(strcmp(index, "type") == 0) { lua_pushstring(L, "LCRYPT_CIPHER_" #NAME); return 1; } \ if(strcmp(index, "iv") == 0) \ @@ -238,15 +238,15 @@ static int lcrypt_cipher_ctr(lua_State *L) return 1; } -LCRYPT_CIPHER_XXX_ENCRYPT(CTR, ctr) -LCRYPT_CIPHER_XXX_DECRYPT(CTR, ctr) -LCRYPT_CIPHER_XXX_INDEX(CTR, ctr) -LCRYPT_CIPHER_XXX_NEWINDEX(CTR, ctr) -LCRYPT_CIPHER_XXX_GC(CTR, ctr) +LCRYPT_CIPHER_XXX_ENCRYPT(CTR, ctr); +LCRYPT_CIPHER_XXX_DECRYPT(CTR, ctr); +LCRYPT_CIPHER_XXX_INDEX(CTR, ctr); +LCRYPT_CIPHER_XXX_NEWINDEX(CTR, ctr); +LCRYPT_CIPHER_XXX_GC(CTR, ctr); -LCRYPT_CIPHER_XXX(CBC, cbc) -LCRYPT_CIPHER_XXX(CFB, cfb) -LCRYPT_CIPHER_XXX(OFB, ofb) +LCRYPT_CIPHER_XXX(CBC, cbc); +LCRYPT_CIPHER_XXX(CFB, cfb); +LCRYPT_CIPHER_XXX(OFB, ofb); static int lcrypt_cipher_lrw(lua_State *L) { @@ -274,11 +274,11 @@ static int lcrypt_cipher_lrw(lua_State *L) return 1; } -LCRYPT_CIPHER_XXX_ENCRYPT(LRW, lrw) -LCRYPT_CIPHER_XXX_DECRYPT(LRW, lrw) -LCRYPT_CIPHER_XXX_INDEX(LRW, lrw) -LCRYPT_CIPHER_XXX_NEWINDEX(LRW, lrw) -LCRYPT_CIPHER_XXX_GC(LRW, lrw) +LCRYPT_CIPHER_XXX_ENCRYPT(LRW, lrw); +LCRYPT_CIPHER_XXX_DECRYPT(LRW, lrw); +LCRYPT_CIPHER_XXX_INDEX(LRW, lrw); +LCRYPT_CIPHER_XXX_NEWINDEX(LRW, lrw); +LCRYPT_CIPHER_XXX_GC(LRW, lrw); static int lcrypt_cipher_f8(lua_State *L) { @@ -301,11 +301,11 @@ static int lcrypt_cipher_f8(lua_State *L) return 1; } -LCRYPT_CIPHER_XXX_ENCRYPT(F8, f8) -LCRYPT_CIPHER_XXX_DECRYPT(F8, f8) -LCRYPT_CIPHER_XXX_INDEX(F8, f8) -LCRYPT_CIPHER_XXX_NEWINDEX(F8, f8) -LCRYPT_CIPHER_XXX_GC(F8, f8) +LCRYPT_CIPHER_XXX_ENCRYPT(F8, f8); +LCRYPT_CIPHER_XXX_DECRYPT(F8, f8); +LCRYPT_CIPHER_XXX_INDEX(F8, f8); +LCRYPT_CIPHER_XXX_NEWINDEX(F8, f8); +LCRYPT_CIPHER_XXX_GC(F8, f8); #undef LCRYPT_CIPHER_XXX #undef LCRYPT_CIPHER_XXX_START diff --git a/lcrypt/lcrypt_hashes.c b/lcrypt/lcrypt_hashes.c index 4eccbfc..46edf28 100644 --- a/lcrypt/lcrypt_hashes.c +++ b/lcrypt/lcrypt_hashes.c @@ -21,7 +21,7 @@ static int lcrypt_hash_done(lua_State *L) lcrypt_hash *h = luaL_checkudata(L, 1, "LCRYPT_HASH_STATE"); if(likely(h->hash >= 0)) { - unsigned char out[hash_descriptor[h->hash].hashsize]; + unsigned char * out = (unsigned char *)alloca(hash_descriptor[h->hash].hashsize); lcrypt_error(L, hash_descriptor[h->hash].done(&h->state, out), NULL); lua_pushlstring(L, (char*)out, hash_descriptor[h->hash].hashsize); } @@ -35,7 +35,7 @@ static int lcrypt_hash_state_gc(lua_State *L) lcrypt_hash *h = luaL_checkudata(L, 1, "LCRYPT_HASH_STATE"); if(likely(h->hash >= 0)) { - unsigned char out[hash_descriptor[h->hash].hashsize]; + unsigned char * out = (unsigned char *)alloca(hash_descriptor[h->hash].hashsize); lcrypt_error(L, hash_descriptor[h->hash].done(&h->state, out), NULL); memset(h, 0, sizeof(lcrypt_hash)); h->hash = -1; @@ -96,7 +96,7 @@ static int lcrypt_hmac_done(lua_State *L) if(likely(h->hash >= 0)) { unsigned long out_length = hash_descriptor[h->hash].hashsize; - unsigned char out[out_length]; + unsigned char * out = (unsigned char *)alloca(out_length); lcrypt_error(L, hmac_done(h, out, &out_length), NULL); lua_pushlstring(L, (char*)out, out_length); memset(h, 0, sizeof(hmac_state)); @@ -112,7 +112,7 @@ static int lcrypt_hmac_state_gc(lua_State *L) if(likely(h->hash >= 0)) { unsigned long out_length = hash_descriptor[h->hash].hashsize; - unsigned char out[out_length]; + unsigned char * out = (unsigned char *)alloca(out_length); lcrypt_error(L, hmac_done(h, out, &out_length), NULL); memset(h, 0, sizeof(hmac_state)); h->hash = -1; |