diff options
| author | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2013-08-10 08:14:28 +0200 | 
|---|---|---|
| committer | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2013-08-10 08:14:28 +0200 | 
| commit | a702df0e6b2cf5d523a8db764d6523d868eba8df (patch) | |
| tree | c7f618894aabb0879ea5ba748b875c1eee8ac6b1 | |
| parent | 5ddf9b5b18431b7fe85bf555d9cfa7fe04aae5e1 (diff) | |
| parent | 697f9e4655829013e464c9c4485c91b5a4e5f132 (diff) | |
Merge branch 'master' of /pub/repo.git/Balau
| -rw-r--r-- | Makefile | 20 | ||||
| -rw-r--r-- | lcrypt/Makefile | 7 | ||||
| -rw-r--r-- | lcrypt/lcrypt.c | 47 | ||||
| -rw-r--r-- | lcrypt/lcrypt_rsa.c | 115 | ||||
| -rw-r--r-- | src/BLua.cc | 2 | 
5 files changed, 42 insertions, 149 deletions
| @@ -8,9 +8,9 @@ CPPFLAGS += -g3 -gdwarf-2 -DDEBUG -DEV_VERIFY=3  LDFLAGS += -g3 -gdwarf-2  endif -INCLUDES = includes libcoro libev LuaJIT/src +INCLUDES = includes libcoro libev LuaJIT/src lcrypt libtommath libtomcrypt/src/headers  LIBS = z -DEFINES = _LARGEFILE64_SOURCE +DEFINES = _LARGEFILE64_SOURCE LITTLE_ENDIAN LTM_DESC LTC_SOURCE USE_LTM  ifeq ($(SYSTEM),Darwin)      LIBS += pthread iconv @@ -29,7 +29,7 @@ LDFLAGS += $(ARCH_FLAGS)  LDLIBS = $(addprefix -l, $(LIBS))  vpath %.cc src:tests -vpath %.c libcoro:libev:win32/pthreads-win32:win32/iconv:win32/regex +vpath %.c libcoro:libev:win32/pthreads-win32:win32/iconv:win32/regex:lcrypt  BALAU_SOURCES = \  Exceptions.cc \ @@ -69,6 +69,8 @@ LuaHandle.cc \  LuaTask.cc \  \  BRegex.cc \ +\ +lcrypt.c \  ifeq ($(SYSTEM),MINGW32)  WIN32_SOURCES = \ @@ -152,20 +154,15 @@ libtomcrypt: libtomcrypt/libtomcrypt.a  libtomcrypt/libtomcrypt.a:  	$(MAKE) -C libtomcrypt CC="$(CC) $(ARCH_FLAGS) -DLTM_DESC -DUSE_LTM -I../libtommath" -lcrypt: lcrypt/lcrypt.o - -lcrypt/lcrypt.o: -	LUA=../LuaJIT TOMCRYPT=../libtomcrypt $(MAKE) -C lcrypt CC="$(CC) $(ARCH_FLAGS)" -  LuaJIT: LuaJIT/src/libluajit.a -libBalau.a: LuaJIT/src/libluajit.a libtommath/libtommath.a libtomcrypt/libtomcrypt.a lcrypt $(BALAU_OBJECTS) +libBalau.a: LuaJIT/src/libluajit.a libtommath/libtommath.a libtomcrypt/libtomcrypt.a $(BALAU_OBJECTS)  ifeq ($(SYSTEM),Darwin)  ifneq ($(CROSSCOMPILE),true)  	rm -f libBalau.a  endif  endif -	$(AR) libBalau.a $(BALAU_OBJECTS) lcrypt/lcrypt.o +	$(AR) libBalau.a $(BALAU_OBJECTS)  %.$(BINEXT) : %.o $(LIB)  	$(LD) $(LDFLAGS) -o $@ $< ./$(LIB) ./LuaJIT/src/libluajit.a ./libtomcrypt/libtomcrypt.a ./libtommath/libtommath.a $(LDLIBS) @@ -185,6 +182,5 @@ clean:  	$(MAKE) -C LuaJIT clean  	$(MAKE) -C libtommath clean  	$(MAKE) -C libtomcrypt clean -	$(MAKE) -C lcrypt clean -.PHONY: lib tests clean strip LuaJIT libtommath libtomcrypt lcrypt +.PHONY: lib tests clean strip LuaJIT libtommath libtomcrypt diff --git a/lcrypt/Makefile b/lcrypt/Makefile deleted file mode 100644 index 0347ee8..0000000 --- a/lcrypt/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -CFLAGS += -O3 -Wall -DLITTLE_ENDIAN -DLTM_DESC -DLTC_SOURCE -DUSE_LTM -fPIC -I$(TOMCRYPT)/src/headers -I$(LUA)/src -I../src - -lcrypt.o: lcrypt.c lcrypt_ciphers.c lcrypt_hashes.c lcrypt_math.c lcrypt_bits.c lcrypt_rsa.c -	$(CC) $(CFLAGS) -c lcrypt.c -o $@ - -clean: -	rm -f lcrypt.o lcrypt.so diff --git a/lcrypt/lcrypt.c b/lcrypt/lcrypt.c index 5ca67cf..779504e 100644 --- a/lcrypt/lcrypt.c +++ b/lcrypt/lcrypt.c @@ -45,7 +45,6 @@ static void* lcrypt_malloc(lua_State *L, size_t size)  #include "lcrypt_hashes.c"  //#include "lcrypt_math.c"  #include "lcrypt_bits.c" -#include "lcrypt_rsa.c"  static int lcrypt_tohex(lua_State *L)  { @@ -221,20 +220,42 @@ static int lcrypt_time(lua_State *L)  static int lcrypt_random(lua_State *L)  {    int len = luaL_checkint(L, 1); -  FILE *fp;    char *buffer = lcrypt_malloc(L, len); -  if(unlikely((fp = fopen("/dev/urandom", "rb")) == NULL)) -  { -    lua_pushstring(L, "Unable to open /dev/urandom."); -    (void)lua_error(L); -  } -  if(unlikely(fread(buffer, len, 1, fp) != 1)) -  { +  #ifdef _WIN32 +    HMODULE hLib = LoadLibrary("ADVAPI32.DLL"); +    if (unlikely(!hLib)) +    { +      lua_pushstring(L, "Unable to open ADVAPI32.DLL"); +      (void)lua_error(L); +    } +    BOOLEAN (APIENTRY *pfn)(void *, ULONG) = +      (BOOLEAN (APIENTRY *)(void *, ULONG)) GetProcAddress(hLib, "SystemFunction036"); +    if (unlikely(!pfn)) +    { +      lua_pushstring(L, "Unable to open ADVAPI32.DLL"); +      (void)lua_error(L); +    } +    ULONG ulCbBuff = len; +    if (unlikely(!pfn(buffer, ulCbBuff))) +    { +      lua_pushstring(L, "Call to SystemFunction036 failed."); +      (void)lua_error(L); +    } +  #else +    FILE *fp; +    if(unlikely((fp = fopen("/dev/urandom", "rb")) == NULL)) +    { +      lua_pushstring(L, "Unable to open /dev/urandom."); +      (void)lua_error(L); +    } +    if(unlikely(fread(buffer, len, 1, fp) != 1)) +    { +      fclose(fp); +      lua_pushstring(L, "Unable to read /dev/urandom."); +      (void)lua_error(L); +    }      fclose(fp); -    lua_pushstring(L, "Unable to read /dev/urandom."); -    (void)lua_error(L); -  } -  fclose(fp); +  #endif    lua_pushlstring(L, buffer, len);    free(buffer);    return 1; diff --git a/lcrypt/lcrypt_rsa.c b/lcrypt/lcrypt_rsa.c deleted file mode 100644 index 77ea4a2..0000000 --- a/lcrypt/lcrypt_rsa.c +++ /dev/null @@ -1,115 +0,0 @@ -const char * lcrypt_rsa = "" -"rsa = {}\n" -"\n" -"function rsa:pkcs1_pad(data, out_length)\n" -"  local asn1 = string.char(0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14)\n" -"  return string.char(0x00, 0x01) .. string.char(0xff):rep(out_length - #asn1 - #data - 2) .. asn1 .. data\n" -"end\n" -"\n" -"function rsa:encode_int(value, len)\n" -"  local ret = ''\n" -"  for i=1,len do\n" -"    ret = string.char(value % 256) .. ret\n" -"    value = math.floor(value / 256)\n" -"  end\n" -"  return ret\n" -"end\n" -"\n" -"function rsa:oaep_g(data, out_length)\n" -"  local out,counter = '', 0\n" -"  while #out < out_length do\n" -"    out = out .. lcrypt.hashes.sha1:hash(data .. self:encode_int(counter, 4)):done()\n" -"    counter = counter + 1\n" -"  end\n" -"  return out:sub(1, out_length)\n" -"end\n" -"\n" -"function rsa:oaep_pad(data, param, out_length)\n" -"  out_length = out_length - 1\n" -"  local h_length = #data\n" -"  local g_length = out_length - h_length\n" -"  local seed = lcrypt.random(h_length)\n" -"  local c = lcrypt.hashes.sha1:hash(param):done()\n" -"  c = c .. string.rep(string.char(0), g_length - h_length - 2 - #c) .. string.char(0, 1) .. data\n" -"  local x = lcrypt.xor(c, self:oaep_g(seed, g_length))\n" -"  local y = lcrypt.xor(seed, self:oaep_g(x, h_length))\n" -"  return string.char(0) .. x .. y\n" -"end\n" -"\n" -"function rsa:oaep_unpad(data, param, out_length)\n" -"  data = data:sub(2, #data)\n" -"  local g_length = #data - out_length\n" -"  local x = data:sub(1, g_length)\n" -"  local seed = lcrypt.xor(self:oaep_g(x, out_length), data:sub(g_length +1, #data))\n" -"  local c = lcrypt.xor(x, self:oaep_g(seed, g_length))\n" -"  local v = lcrypt.hashes.sha1:hash(param):done()\n" -"  if c:sub(1,#v) == v then return c:sub(g_length - out_length + 1, #c) end\n" -"end\n" -"\n" -"function rsa:prime(bits)\n" -"  bits = math.floor(bits)\n" -"  if bits < 24 then return end\n" -"  local ret, high, bytes = nil, 1, math.floor((bits - 7) / 8)\n" -"  for i=1,bits-bytes*8-1 do high = 1 + high + high end\n" -"  high = string.char(high)\n" -"  low = lcrypt.random(1):byte()\n" -"  if low / 2 == math.floor(low / 2) then low = low + 1 end\n" -"  low = string.char(low)\n" -"  bytes = bytes - 1\n" -"  repeat\n" -"    ret = lcrypt.bigint(high .. lcrypt.random(bytes) .. low)\n" -"  until ret.isprime\n" -"  return ret\n" -"end\n" -"\n" -"function rsa:gen_key(bits, e)\n" -"  local key,one,p1,q1 = { e=lcrypt.bigint(e) }, lcrypt.bigint(1), nil, nil\n" -"  bits = bits / 2\n" -"  repeat\n" -"    key.p = self:prime(bits)\n" -"    p1 = key.p - one\n" -"  until p1:gcd(key.e) == one\n" -"  repeat\n" -"    key.q = self:prime(bits)\n" -"    q1 = key.q - one\n" -"  until q1:gcd(key.e) == one\n" -"  key.d = key.e:invmod(p1:lcm(q1))\n" -"  key.n = key.p * key.q\n" -"  key.dp = key.d % p1\n" -"  key.dq = key.d % q1\n" -"  key.qp = key.q:invmod(key.p)\n" -"  return key\n" -"end\n" -"\n" -"function rsa:private(msg, key)\n" -"  msg = lcrypt.bigint(msg)\n" -"  local a,b = msg:exptmod(key.dp, key.p), msg:exptmod(key.dq, key.q)\n" -"  local ret = tostring(key.qp:mulmod(a - b, key.p) * key.q + b)\n" -"  if ret:byte(1) == 0 then ret = ret:sub(2, #ret) end\n" -"  return ret\n" -"end\n" -"\n" -"function rsa:public(msg, key)\n" -"  return tostring(lcrypt.bigint(msg):exptmod(key.e, key.n))\n" -"end\n" -"\n" -"function rsa:sign_pkcs1(msg, key)\n" -"  return self:private(self:pkcs1_pad(lcrypt.hashes.sha1:hash(msg):done(), key.n.bits / 8), key)\n" -"end\n" -"\n" -"function rsa:verify_pkcs1(signature, msg, key)\n" -"  msg = lcrypt.hashes.sha1:hash(msg):done()\n" -"  local tmp = self:public(signature, key)\n" -"  if tmp:sub(#tmp - #msg + 1, #tmp) == msg then return true end\n" -"end\n" -"\n" -"function rsa:sign_oaep(msg, param, key)\n" -"  return self:private(self:oaep_pad(lcrypt.hashes.sha1:hash(msg):done(), param, key.n.bits / 8), key)\n" -"end\n" -"\n" -"function rsa:verify_oaep(signature, msg, param, key)\n" -"  local tmp = self:public(signature, key)\n" -"  local h = self:oaep_unpad(tmp, param, 20)\n" -"  if h == lcrypt.hashes.sha1:hash(msg):done() then return true end\n" -"end\n" -""; diff --git a/src/BLua.cc b/src/BLua.cc index 13b09cd..eee663b 100644 --- a/src/BLua.cc +++ b/src/BLua.cc @@ -450,13 +450,11 @@ void Balau::Lua::open_bit() {  extern "C" {  int luaopen_lcrypt(lua_State *L); -extern const char * lcrypt_rsa;  }  void Balau::Lua::open_lcrypt() {      int n = gettop();      luaopen_lcrypt(L); -    load(lcrypt_rsa);      while (n < gettop()) pop();  } | 
