From 6105558182628a3ae29a2a39736d025d62b9e9f7 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Fri, 9 Aug 2013 09:01:53 +0200 Subject: Cleaning lcrypt a bit. --- lcrypt/lcrypt.c | 359 ++------------------------------------------------- lcrypt/lcrypt_math.c | 283 ++++++++++++---------------------------- lcrypt/lcrypt_rsa2.c | 115 +++++++++++++++++ 3 files changed, 206 insertions(+), 551 deletions(-) create mode 100644 lcrypt/lcrypt_rsa2.c (limited to 'lcrypt') diff --git a/lcrypt/lcrypt.c b/lcrypt/lcrypt.c index ed514dd..620c88b 100644 --- a/lcrypt/lcrypt.c +++ b/lcrypt/lcrypt.c @@ -11,16 +11,7 @@ #include "lua.h" #include "lauxlib.h" #include "lualib.h" -#include -#include - -#ifdef USE_NCIPHER - #include "ncipher.h" - extern NFastApp_Connection nfast_conn; - extern NFast_AppHandle nfast_app; -#else - #include -#endif +#include "tomcrypt.h" #define likely(x) __builtin_expect((x),1) #define unlikely(x) __builtin_expect((x),0) @@ -217,12 +208,6 @@ static int lcrypt_xor(lua_State *L) return 1; } -static int lcrypt_sleep(lua_State *L) -{ - usleep(1000000.0 * luaL_checknumber(L, 1)); - return(0); -} - static int lcrypt_time(lua_State *L) { double ret; @@ -236,275 +221,25 @@ static int lcrypt_time(lua_State *L) static int lcrypt_random(lua_State *L) { int len = luaL_checkint(L, 1); - #ifdef USE_NCIPHER - M_Command command; - M_Reply reply; - M_Status rc; - memset(&command, 0, sizeof(command)); - memset(&reply, 0, sizeof(reply)); - command.cmd = Cmd_GenerateRandom; - command.args.generaterandom.lenbytes = len; - if(unlikely((rc = NFastApp_Transact(nfast_conn, NULL, &command, &reply, NULL)) != Status_OK)) - { - lua_pushstring(L, NF_Lookup(rc, NF_Status_enumtable)); - (void)lua_error(L); - } - if(unlikely(reply.status != Status_OK)) - { - lua_pushstring(L, NF_Lookup(reply.status, NF_Status_enumtable)); - (void)lua_error(L); - } - if(unlikely(len != reply.reply.generaterandom.data.len)) - { - lua_pushstring(L, "Wrong length returned"); - (void)lua_error(L); - } - lua_pushlstring(L, reply.reply.generaterandom.data.ptr, len); - NFastApp_Free_Reply(nfast_app, NULL, NULL, &reply); - #else - 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)) - { - fclose(fp); - lua_pushstring(L, "Unable to read /dev/urandom."); - (void)lua_error(L); - } - fclose(fp); - lua_pushlstring(L, buffer, len); - free(buffer); - #endif - return 1; -} - -static FILE *lgetfile(lua_State *L, int index) -{ - FILE **fp = lua_touserdata(L, index); - if(unlikely(fp == NULL)) return NULL; - if(lua_getmetatable(L, index)) + FILE *fp; + char *buffer = lcrypt_malloc(L, len); + if(unlikely((fp = fopen("/dev/urandom", "rb")) == NULL)) { - lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); - if(lua_rawequal(L, -1, -2)) - { - lua_pop(L, 2); - return *fp; - } - lua_pop(L, 2); - } - return NULL; -} - -static int lcrypt_tcsetattr(lua_State* L) -{ - struct termios old, new; - FILE *fp = lgetfile(L, 1); - if(unlikely(fp == NULL)) return 0; - if(unlikely(tcgetattr(fileno(fp), &old) != 0)) return 0; - new = old; - new.c_iflag = luaL_optint(L, 2, old.c_iflag); - new.c_oflag = luaL_optint(L, 3, old.c_oflag); - new.c_cflag = luaL_optint(L, 4, old.c_cflag); - new.c_lflag = luaL_optint(L, 5, old.c_lflag); - if(unlikely(tcsetattr(fileno(fp), TCSAFLUSH, &new) != 0)) return 0; - lua_pushinteger(L, new.c_iflag); - lua_pushinteger(L, new.c_oflag); - lua_pushinteger(L, new.c_cflag); - lua_pushinteger(L, new.c_lflag); - return 4; -} - -static int lcrypt_flag_add(lua_State *L) -{ - uint32_t a = luaL_checkint(L, 1); - uint32_t b = luaL_checkint(L, 2); - lua_pushinteger(L, a | b); - return 1; -} - -static int lcrypt_flag_remove(lua_State *L) -{ - uint32_t a = luaL_checkint(L, 1); - uint32_t b = luaL_checkint(L, 2); - lua_pushinteger(L, a & ~b); - return 1; -} - -#ifndef USE_NCIPHER - -typedef struct -{ - int fd; - int pid; - char *command; -} lcrypt_spawn_t; - -static int lcrypt_spawn(lua_State *L) -{ - int fd, pid, argc; - #define MAX_ARGUMENT 128 - const char *command = luaL_checkstring(L, 1); - char *cmd = strdup(command); - char *pos = cmd, *p; - char *argv[MAX_ARGUMENT]; - for(argc = 0; argc < MAX_ARGUMENT-1; argc++) - { - // eat whitespace - while(*pos == ' ' || *pos == '\t' || *pos == '\n' || *pos == '\r') - { - if(*pos == '\\') for(p = pos; *p != '\0'; p++) *p = *(p + 1); - pos++; - } - // start of argument found - argv[argc] = pos; - if(*argv[argc] == '"' || *argv[argc] == '\'') // quoted argument - { - pos++; - while(*pos != *argv[argc] && *pos != '\0') - { - if(*pos == '\\') for(p = pos; *p != '\0'; p++) *p = *(p + 1); - pos++; - } - argv[argc]++; - } - else // non-quoted argument - { - while(*pos != ' ' && *pos != '\t' && *pos != '\n' && *pos != '\r' && *pos != '\0') - { - if(*pos == '\\') for(p = pos; *p != '\0'; p++) *p = *(p + 1); - pos++; - } - } - if(*pos == '\0') break; - *pos++ = '\0'; - } - argv[++argc] = NULL; - - errno = 0; - pid = forkpty(&fd, NULL, NULL, NULL); - if(pid == 0) // child - { - execvp(argv[0], argv); - // if we get here, it's an error! - perror("'unable to spawn process"); - return 0; - } - else if(errno != 0) - { - lua_pushnil(L); - lua_pushstring(L, strerror(errno)); - return 2; - } - else - { - lcrypt_spawn_t *lsp = lua_newuserdata(L, sizeof(lcrypt_spawn_t)); - lsp->fd = fd; - lsp->pid = pid; - lsp->command = cmd; - luaL_getmetatable(L, "LSPAWN"); - (void)lua_setmetatable(L, -2); - return 1; - } -} - -static int lcrypt_spawn_close(lua_State *L) -{ - lcrypt_spawn_t *lsp = (lcrypt_spawn_t*)luaL_checkudata(L, 1, "LSPAWN"); - if(lsp->pid > 0) - { - (void)kill(lsp->pid, SIGQUIT); - lsp->pid = -1; - } - if(lsp->fd >= 0) - { - (void)close(lsp->fd); - lsp->fd = -1; - } - if(lsp->command != NULL) - { - free(lsp->command); - lsp->command = NULL; - } - return 0; -} - -static int lcrypt_spawn_read(lua_State *L) -{ - lcrypt_spawn_t *lsp = (lcrypt_spawn_t*)luaL_checkudata(L, 1, "LSPAWN"); - int count = luaL_optint(L, 2, 4096); - char *buffer; - if(lsp->fd < 0) - { - lua_pushstring(L, "Spawn closed"); - lua_error(L); - return 0; - } - if((buffer = malloc(count)) == NULL) - { - lua_pushnil(L); - lua_pushstring(L, "Unable to allocate memory"); - return 2; + lua_pushstring(L, "Unable to open /dev/urandom."); + (void)lua_error(L); } - count = read(lsp->fd, buffer, count); - if(errno != 0) + if(unlikely(fread(buffer, len, 1, fp) != 1)) { - free(buffer); - lua_pushnil(L); - lua_pushstring(L, strerror(errno)); - return 2; + fclose(fp); + lua_pushstring(L, "Unable to read /dev/urandom."); + (void)lua_error(L); } - lua_pushlstring(L, buffer, count); + fclose(fp); + lua_pushlstring(L, buffer, len); free(buffer); return 1; } -static int lcrypt_spawn_write(lua_State *L) -{ - lcrypt_spawn_t *lsp = (lcrypt_spawn_t*)luaL_checkudata(L, 1, "LSPAWN"); - size_t in_length = 0; - const char* in = luaL_checklstring(L, 2, &in_length); - if(lsp->fd < 0) - { - lua_pushstring(L, "closed"); - lua_error(L); - return 0; - } - write(lsp->fd, in, in_length); - if(errno != 0) - { - lua_pushstring(L, strerror(errno)); - return 1; - } - return 0; -} - -static int lcrypt_spawn_index(lua_State *L) -{ - (void)luaL_checkudata(L, 1, "LSPAWN"); - const char *index = luaL_checkstring(L, 2); - if(strcmp(index, "read") == 0) - lua_pushcfunction(L, lcrypt_spawn_read); - else if(strcmp(index, "write") == 0) - lua_pushcfunction(L, lcrypt_spawn_write); - else if(strcmp(index, "close") == 0) - lua_pushcfunction(L, lcrypt_spawn_close); - else - return 0; - return 1; -} - -static const luaL_Reg lcrypt_spawn_flib[] = -{ - {"__gc", lcrypt_spawn_close}, - {NULL, NULL} -}; - -#endif - static const luaL_Reg lcryptlib[] = { {"tohex", lcrypt_tohex}, @@ -514,31 +249,15 @@ static const luaL_Reg lcryptlib[] = {"base64_encode", lcrypt_base64_encode}, {"base64_decode", lcrypt_base64_decode}, {"xor", lcrypt_xor}, - {"sleep", lcrypt_sleep}, {"time", lcrypt_time}, {"random", lcrypt_random}, - {"tcsetattr", lcrypt_tcsetattr}, - {"flag_add", lcrypt_flag_add}, - {"flag_remove", lcrypt_flag_remove}, - #ifndef USE_NCIPHER - {"spawn", lcrypt_spawn}, - #endif {NULL, NULL} }; -int luaopen_lcrypt(lua_State *L); int luaopen_lcrypt(lua_State *L) { luaL_register(L, "lcrypt", lcryptlib); - #ifndef USE_NCIPHER - (void)luaL_newmetatable(L, "LSPAWN"); - lua_pushliteral(L, "__index"); - lua_pushcfunction(L, lcrypt_spawn_index); - lua_rawset(L, -3); - luaL_register(L, NULL, lcrypt_spawn_flib); - #endif - lua_getglobal(L, "lcrypt"); lcrypt_start_ciphers(L); @@ -546,60 +265,6 @@ int luaopen_lcrypt(lua_State *L) lcrypt_start_math(L); lcrypt_start_bits(L); - lua_pushstring(L, "iflag"); - lua_newtable(L); - ADD_CONSTANT(L, IGNBRK); ADD_CONSTANT(L, BRKINT); ADD_CONSTANT(L, IGNPAR); ADD_CONSTANT(L, PARMRK); - ADD_CONSTANT(L, INPCK); ADD_CONSTANT(L, ISTRIP); ADD_CONSTANT(L, INLCR); ADD_CONSTANT(L, IGNCR); - ADD_CONSTANT(L, ICRNL); ADD_CONSTANT(L, IXON); ADD_CONSTANT(L, IXANY); ADD_CONSTANT(L, IXOFF); - lua_settable(L, -3); - - lua_pushstring(L, "oflag"); - lua_newtable(L); - #ifdef OLCUC - ADD_CONSTANT(L, OLCUC); - #endif - #ifdef OFILL - ADD_CONSTANT(L, OFILL); - #endif - #ifdef OFDEL - ADD_CONSTANT(L, OFDEL); - #endif - #ifdef NLDLY - ADD_CONSTANT(L, NLDLY); - #endif - #ifdef CRDLY - ADD_CONSTANT(L, CRDLY); - #endif - #ifdef TABDLY - ADD_CONSTANT(L, TABDLY); - #endif - #ifdef BSDLY - ADD_CONSTANT(L, BSDLY); - #endif - #ifdef VTDLY - ADD_CONSTANT(L, VTDLY); - #endif - #ifdef FFDLY - ADD_CONSTANT(L, FFDLY); - #endif - ADD_CONSTANT(L, OPOST); ADD_CONSTANT(L, ONLCR); ADD_CONSTANT(L, OCRNL); ADD_CONSTANT(L, ONOCR); - ADD_CONSTANT(L, ONLRET); - lua_settable(L, -3); - - lua_pushstring(L, "cflag"); - lua_newtable(L); - ADD_CONSTANT(L, CS5); ADD_CONSTANT(L, CS6); ADD_CONSTANT(L, CS7); ADD_CONSTANT(L, CS8); - ADD_CONSTANT(L, CSTOPB); ADD_CONSTANT(L, CREAD); ADD_CONSTANT(L, PARENB); ADD_CONSTANT(L, PARODD); - ADD_CONSTANT(L, HUPCL); ADD_CONSTANT(L, CLOCAL); - lua_settable(L, -3); - - lua_pushstring(L, "lflag"); - lua_newtable(L); - ADD_CONSTANT(L, ISIG); ADD_CONSTANT(L, ICANON); ADD_CONSTANT(L, ECHO); ADD_CONSTANT(L, ECHOE); - ADD_CONSTANT(L, ECHOK); ADD_CONSTANT(L, ECHONL); ADD_CONSTANT(L, NOFLSH); ADD_CONSTANT(L, TOSTOP); - ADD_CONSTANT(L, IEXTEN); - lua_settable(L, -3); - lua_pop(L, 1); return 1; } diff --git a/lcrypt/lcrypt_math.c b/lcrypt/lcrypt_math.c index 769a3a4..3189fd4 100644 --- a/lcrypt/lcrypt_math.c +++ b/lcrypt/lcrypt_math.c @@ -1,20 +1,12 @@ -#ifdef USE_NCIPHER - typedef sbigint lcrypt_bigint; -#else - typedef void* lcrypt_bigint; -#endif +typedef void* lcrypt_bigint; static lcrypt_bigint* lcrypt_new_bigint(lua_State *L) { lcrypt_bigint *bi = lua_newuserdata(L, sizeof(lcrypt_bigint)); luaL_getmetatable(L, "LCRYPT_BIGINT"); (void)lua_setmetatable(L, -2); - #ifdef USE_NCIPHER - sbigint_create(bi, NULL, 0); - #else - *bi = NULL; - lcrypt_error(L, ltc_mp.init(bi), NULL); - #endif + *bi = NULL; + lcrypt_error(L, ltc_mp.init(bi), NULL); return bi; } @@ -23,11 +15,7 @@ static int lcrypt_bigint_add(lua_State *L) lcrypt_bigint *bi_a = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); lcrypt_bigint *bi_b = luaL_checkudata(L, 2, "LCRYPT_BIGINT"); lcrypt_bigint *bi = lcrypt_new_bigint(L); - #ifdef USE_NCIPHER - if(unlikely(sbigint_add(bi_a, bi_b, bi) != 0)) return 0; - #else - lcrypt_error(L, ltc_mp.add(*bi_a, *bi_b, *bi), NULL); - #endif + lcrypt_error(L, ltc_mp.add(*bi_a, *bi_b, *bi), NULL); return 1; } @@ -36,11 +24,7 @@ static int lcrypt_bigint_sub(lua_State *L) lcrypt_bigint *bi_a = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); lcrypt_bigint *bi_b = luaL_checkudata(L, 2, "LCRYPT_BIGINT"); lcrypt_bigint *bi = lcrypt_new_bigint(L); - #ifdef USE_NCIPHER - if(unlikely(sbigint_sub(bi_a, bi_b, bi) != 0)) return 0; - #else - lcrypt_error(L, ltc_mp.sub(*bi_a, *bi_b, *bi), NULL); - #endif + lcrypt_error(L, ltc_mp.sub(*bi_a, *bi_b, *bi), NULL); return 1; } @@ -49,11 +33,7 @@ static int lcrypt_bigint_mul(lua_State *L) lcrypt_bigint *bi_a = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); lcrypt_bigint *bi_b = luaL_checkudata(L, 2, "LCRYPT_BIGINT"); lcrypt_bigint *bi = lcrypt_new_bigint(L); - #ifdef USE_NCIPHER - if(unlikely(sbigint_mul(bi_a, bi_b, bi) != 0)) return 0; - #else - lcrypt_error(L, ltc_mp.mul(*bi_a, *bi_b, *bi), NULL); - #endif + lcrypt_error(L, ltc_mp.mul(*bi_a, *bi_b, *bi), NULL); return 1; } @@ -62,11 +42,7 @@ static int lcrypt_bigint_div(lua_State *L) lcrypt_bigint *bi_a = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); lcrypt_bigint *bi_b = luaL_checkudata(L, 2, "LCRYPT_BIGINT"); lcrypt_bigint *bi = lcrypt_new_bigint(L); - #ifdef USE_NCIPHER - if(unlikely(sbigint_divmod(bi_a, bi_b, bi, NULL) != 0)) return 0; - #else - lcrypt_error(L, ltc_mp.mpdiv(*bi_a, *bi_b, *bi, NULL), NULL); - #endif + lcrypt_error(L, ltc_mp.mpdiv(*bi_a, *bi_b, *bi, NULL), NULL); return 1; } @@ -76,11 +52,7 @@ static int lcrypt_bigint_divmod(lua_State *L) lcrypt_bigint *bi_b = luaL_checkudata(L, 2, "LCRYPT_BIGINT"); lcrypt_bigint *bi_q = lcrypt_new_bigint(L); lcrypt_bigint *bi_r = lcrypt_new_bigint(L); - #ifdef USE_NCIPHER - if(unlikely(sbigint_divmod(bi_a, bi_b, bi_q, bi_r) != 0)) return 0; - #else - lcrypt_error(L, ltc_mp.mpdiv(*bi_a, *bi_b, *bi_q, *bi_r), NULL); - #endif + lcrypt_error(L, ltc_mp.mpdiv(*bi_a, *bi_b, *bi_q, *bi_r), NULL); return 2; } @@ -89,11 +61,7 @@ static int lcrypt_bigint_mod(lua_State *L) lcrypt_bigint *bi_a = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); lcrypt_bigint *bi_b = luaL_checkudata(L, 2, "LCRYPT_BIGINT"); lcrypt_bigint *bi = lcrypt_new_bigint(L); - #ifdef USE_NCIPHER - if(unlikely(sbigint_divmod(bi_a, bi_b, NULL, bi) != 0)) return 0; - #else - lcrypt_error(L, ltc_mp.mpdiv(*bi_a, *bi_b, NULL, *bi), NULL); - #endif + lcrypt_error(L, ltc_mp.mpdiv(*bi_a, *bi_b, NULL, *bi), NULL); return 1; } @@ -102,11 +70,7 @@ static int lcrypt_bigint_invmod(lua_State *L) lcrypt_bigint *bi_a = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); lcrypt_bigint *bi_b = luaL_checkudata(L, 2, "LCRYPT_BIGINT"); lcrypt_bigint *bi = lcrypt_new_bigint(L); - #ifdef USE_NCIPHER - if(unlikely(sbigint_invmod(bi_a, bi_b, bi) != 0)) return 0; - #else - lcrypt_error(L, ltc_mp.invmod(*bi_a, *bi_b, *bi), NULL); - #endif + lcrypt_error(L, ltc_mp.invmod(*bi_a, *bi_b, *bi), NULL); return 1; } @@ -116,11 +80,7 @@ static int lcrypt_bigint_mulmod(lua_State *L) lcrypt_bigint *bi_b = luaL_checkudata(L, 2, "LCRYPT_BIGINT"); lcrypt_bigint *bi_c = luaL_checkudata(L, 3, "LCRYPT_BIGINT"); lcrypt_bigint *bi = lcrypt_new_bigint(L); - #ifdef USE_NCIPHER - if(unlikely(sbigint_mulmod(bi_a, bi_b, bi_c, bi) != 0)) return 0; - #else - lcrypt_error(L, ltc_mp.mulmod(*bi_a, *bi_b, *bi_c, *bi), NULL); - #endif + lcrypt_error(L, ltc_mp.mulmod(*bi_a, *bi_b, *bi_c, *bi), NULL); return 1; } @@ -130,11 +90,7 @@ static int lcrypt_bigint_exptmod(lua_State *L) lcrypt_bigint *bi_b = luaL_checkudata(L, 2, "LCRYPT_BIGINT"); lcrypt_bigint *bi_c = luaL_checkudata(L, 3, "LCRYPT_BIGINT"); lcrypt_bigint *bi = lcrypt_new_bigint(L); - #ifdef USE_NCIPHER - if(unlikely(sbigint_exptmod(bi_a, bi_b, bi_c, bi) != 0)) return 0; - #else - lcrypt_error(L, ltc_mp.exptmod(*bi_a, *bi_b, *bi_c, *bi), NULL); - #endif + lcrypt_error(L, ltc_mp.exptmod(*bi_a, *bi_b, *bi_c, *bi), NULL); return 1; } @@ -143,11 +99,7 @@ static int lcrypt_bigint_gcd(lua_State *L) lcrypt_bigint *bi_a = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); lcrypt_bigint *bi_b = luaL_checkudata(L, 2, "LCRYPT_BIGINT"); lcrypt_bigint *bi = lcrypt_new_bigint(L); - #ifdef USE_NCIPHER - if(unlikely(sbigint_gcd(bi_a, bi_b, bi) != 0)) return 0; - #else - lcrypt_error(L, ltc_mp.gcd(*bi_a, *bi_b, *bi), NULL); - #endif + lcrypt_error(L, ltc_mp.gcd(*bi_a, *bi_b, *bi), NULL); return 1; } @@ -156,11 +108,7 @@ static int lcrypt_bigint_lcm(lua_State *L) lcrypt_bigint *bi_a = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); lcrypt_bigint *bi_b = luaL_checkudata(L, 2, "LCRYPT_BIGINT"); lcrypt_bigint *bi = lcrypt_new_bigint(L); - #ifdef USE_NCIPHER - if(unlikely(sbigint_lcm(bi_a, bi_b, bi) != 0)) return 0; - #else - lcrypt_error(L, ltc_mp.lcm(*bi_a, *bi_b, *bi), NULL); - #endif + lcrypt_error(L, ltc_mp.lcm(*bi_a, *bi_b, *bi), NULL); return 1; } @@ -168,12 +116,7 @@ static int lcrypt_bigint_unm(lua_State *L) { lcrypt_bigint *bi_a = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); lcrypt_bigint *bi = lcrypt_new_bigint(L); - #ifdef USE_NCIPHER - sbigint_copy(bi, bi_a); - bi->sign = (bi_a->sign == SBIGINT_POSITIVE) ? SBIGINT_NEGATIVE : SBIGINT_POSITIVE; - #else - lcrypt_error(L, ltc_mp.neg(*bi_a, *bi), NULL); - #endif + lcrypt_error(L, ltc_mp.neg(*bi_a, *bi), NULL); return 1; } @@ -181,11 +124,7 @@ static int lcrypt_bigint_eq(lua_State *L) { lcrypt_bigint *bi_a = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); lcrypt_bigint *bi_b = luaL_checkudata(L, 2, "LCRYPT_BIGINT"); - #ifdef USE_NCIPHER - lua_pushboolean(L, (sbigint_cmp(bi_a, bi_b) == 0) ? 1 : 0); - #else - lua_pushboolean(L, (ltc_mp.compare(*bi_a, *bi_b) == LTC_MP_EQ) ? 1 : 0); - #endif + lua_pushboolean(L, (ltc_mp.compare(*bi_a, *bi_b) == LTC_MP_EQ) ? 1 : 0); return 1; } @@ -193,11 +132,7 @@ static int lcrypt_bigint_lt(lua_State *L) { lcrypt_bigint *bi_a = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); lcrypt_bigint *bi_b = luaL_checkudata(L, 2, "LCRYPT_BIGINT"); - #ifdef USE_NCIPHER - lua_pushboolean(L, (sbigint_cmp(bi_a, bi_b) < 0) ? 1 : 0); - #else - lua_pushboolean(L, (ltc_mp.compare(*bi_a, *bi_b) == LTC_MP_LT) ? 1 : 0); - #endif + lua_pushboolean(L, (ltc_mp.compare(*bi_a, *bi_b) == LTC_MP_LT) ? 1 : 0); return 1; } @@ -205,84 +140,46 @@ static int lcrypt_bigint_le(lua_State *L) { lcrypt_bigint *bi_a = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); lcrypt_bigint *bi_b = luaL_checkudata(L, 2, "LCRYPT_BIGINT"); - #ifdef USE_NCIPHER - lua_pushboolean(L, (sbigint_cmp(bi_a, bi_b) <= 0) ? 1 : 0); - #else - lua_pushboolean(L, (ltc_mp.compare(*bi_a, *bi_b) == LTC_MP_GT) ? 0 : 1); - #endif + lua_pushboolean(L, (ltc_mp.compare(*bi_a, *bi_b) == LTC_MP_GT) ? 0 : 1); return 1; } static int lcrypt_bigint_tostring(lua_State *L) { +#if 0 lcrypt_bigint *bi_a = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); - #ifdef USE_NCIPHER - unsigned char out[4097]; - int length = sizeof(out); - sbigint_tostring(bi_a, out + 1, &length); - if(bi_a->sign == SBIGINT_NEGATIVE || (out[1] & 0x80) == 0x80) - { - out[0] = bi_a->sign; - lua_pushlstring(L, (char*)out, length + 1); - } - else - { - lua_pushlstring(L, (char*)out + 1, length); - } - #else - size_t out_length = (size_t)ltc_mp.unsigned_size(*bi_a) + 1; - unsigned char *out = lcrypt_malloc(L, out_length); - out[0] = (ltc_mp.compare_d(*bi_a, 0) == LTC_MP_LT) ? (unsigned char)0x80 : (unsigned char)0x00; - lcrypt_error(L, ltc_mp.unsigned_write(*bi_a, out+1), out); - if(out[0] == 0 && out[1] < 0x7f) - lua_pushlstring(L, (char*)out+1, out_length-1); - else - lua_pushlstring(L, (char*)out, out_length); - free(out); - #endif + size_t out_length = (size_t)ltc_mp.unsigned_size(*bi_a) + 1; + unsigned char *out = lcrypt_malloc(L, out_length); + out[0] = (ltc_mp.compare_d(*bi_a, 0) == LTC_MP_LT) ? (unsigned char)0x80 : (unsigned char)0x00; + lcrypt_error(L, ltc_mp.unsigned_write(*bi_a, out+1), out); + if(out[0] == 0 && out[1] < 0x7f) + lua_pushlstring(L, (char*)out+1, out_length-1); + else + lua_pushlstring(L, (char*)out, out_length); + free(out); return 1; +#else + lcrypt_bigint *bi_a = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); + char *out = lcrypt_malloc(L, ltc_mp.count_bits(*bi_a) / 3 + 3); + lcrypt_error(L, ltc_mp.write_radix(*bi_a, out, 10), NULL); + lua_pushstring(L, out); + free(out); + return 1; +#endif } static int lcrypt_bigint_index(lua_State *L) { lcrypt_bigint *bi = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); const char *index = luaL_checkstring(L, 2); - #ifdef USE_NCIPHER - if(strcmp(index, "bits") == 0) - { - int len = bi->num.nbytes - 1; - while(len > 0 && bi->num.bytes[len] == 0) len--; - int bits = len * 8; - if(bi->num.bytes[len] & 0x80) bits += 8; - else if(bi->num.bytes[len] & 0x40) bits += 7; - else if(bi->num.bytes[len] & 0x20) bits += 6; - else if(bi->num.bytes[len] & 0x10) bits += 5; - else if(bi->num.bytes[len] & 0x08) bits += 4; - else if(bi->num.bytes[len] & 0x04) bits += 3; - else if(bi->num.bytes[len] & 0x02) bits += 2; - else bits++; - lua_pushinteger(L, bits); - return 1; - } - if(strcmp(index, "isprime") == 0) - { - sbigint c; - int i, prime = 0; - if(unlikely(sbigint_is_prime(bi, &c) != 0)) return 0; - for(i = 0; i < c.num.nbytes; i++) if(c.num.bytes[i] != 0) { prime = 1; break; } - lua_pushboolean(L, prime); - return 1; - } - #else - if(strcmp(index, "bits") == 0) { lua_pushinteger(L, ltc_mp.count_bits(*bi)); return 1; } - if(strcmp(index, "isprime") == 0) - { - int ret = LTC_MP_NO; - lcrypt_error(L, ltc_mp.isprime(*bi, &ret), NULL); - lua_pushboolean(L, (ret == LTC_MP_YES) ? 1 : 0); - return 1; - } - #endif + if(strcmp(index, "bits") == 0) { lua_pushinteger(L, ltc_mp.count_bits(*bi)); return 1; } + if(strcmp(index, "isprime") == 0) + { + int ret = LTC_MP_NO; + lcrypt_error(L, ltc_mp.isprime(*bi, &ret), NULL); + lua_pushboolean(L, (ret == LTC_MP_YES) ? 1 : 0); + return 1; + } if(strcmp(index, "add") == 0) { lua_pushcfunction(L, lcrypt_bigint_add); return 1; } if(strcmp(index, "sub") == 0) { lua_pushcfunction(L, lcrypt_bigint_sub); return 1; } if(strcmp(index, "mul") == 0) { lua_pushcfunction(L, lcrypt_bigint_mul); return 1; } @@ -298,77 +195,57 @@ static int lcrypt_bigint_index(lua_State *L) static int lcrypt_bigint_gc(lua_State *L) { - #ifdef USE_NCIPHER - (void)luaL_checkudata(L, 1, "LCRYPT_BIGINT"); - #else - lcrypt_bigint *bi = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); - if(likely(*bi != NULL)) - { - ltc_mp.deinit(*bi); - *bi = NULL; - } - #endif + lcrypt_bigint *bi = luaL_checkudata(L, 1, "LCRYPT_BIGINT"); + if(likely(*bi != NULL)) + { + ltc_mp.deinit(*bi); + *bi = NULL; + } return 0; } static int lcrypt_bigint_create(lua_State *L) { - #ifdef USE_NCIPHER - if(lua_isnumber(L, 1) == 1) + if(lua_type(L, 1) == LUA_TNUMBER) + { + long n = luaL_checknumber(L, 1); + lcrypt_bigint *bi = lcrypt_new_bigint(L); + if(n < 0) { - long n = luaL_checknumber(L, 1); - lcrypt_bigint *bi = lcrypt_new_bigint(L); - if(n < 0) - { - bi->sign = SBIGINT_NEGATIVE; - n = -n; - } - bi->num.nbytes = 0; - while(n != 0 || bi->num.nbytes % 4 != 0) + void *temp; + int err = CRYPT_OK; + lcrypt_error(L, ltc_mp.init(&temp), NULL); + if((err = ltc_mp.set_int(temp, -n)) == CRYPT_OK) { - bi->num.bytes[bi->num.nbytes++] = n & 0xff; - n >>= 8; + err = ltc_mp.neg(temp, *bi); } + ltc_mp.deinit(temp); + lcrypt_error(L, err, NULL); } else { - size_t n_length = 0; - unsigned char *n = (unsigned char*)luaL_optlstring(L, 1, "", &n_length); - lcrypt_bigint *bi = lua_newuserdata(L, sizeof(lcrypt_bigint)); - luaL_getmetatable(L, "LCRYPT_BIGINT"); - (void)lua_setmetatable(L, -2); - sbigint_create(bi, n, n_length); + lcrypt_error(L, ltc_mp.set_int(*bi, n), NULL); } - #else - if(lua_isnumber(L, 1) == 1) + } + else + { + size_t n_length = 0; + unsigned char *n = (unsigned char*)luaL_optlstring(L, 1, "0", &n_length); + lcrypt_bigint *bi = lcrypt_new_bigint(L); + int radix = 10; + if(lua_isnumber(L, 2) == 1) { - long n = luaL_checknumber(L, 1); - lcrypt_bigint *bi = lcrypt_new_bigint(L); - if(n < 0) - { - void *temp; - int err = CRYPT_OK; - lcrypt_error(L, ltc_mp.init(&temp), NULL); - if((err = ltc_mp.set_int(temp, -n)) == CRYPT_OK) - { - err = ltc_mp.neg(temp, *bi); - } - ltc_mp.deinit(temp); - lcrypt_error(L, err, NULL); - } - else - { - lcrypt_error(L, ltc_mp.set_int(*bi, n), NULL); - } + radix = luaL_checknumber(L, 2); } - else + if(radix <= 0) { - size_t n_length = 0; - unsigned char *n = (unsigned char*)luaL_optlstring(L, 1, "", &n_length); - lcrypt_bigint *bi = lcrypt_new_bigint(L); lcrypt_error(L, ltc_mp.unsigned_read(*bi, n, n_length), NULL); } - #endif + else + { + lcrypt_error(L, ltc_mp.read_radix(*bi, n, radix), NULL); + } + } return 1; } @@ -393,9 +270,7 @@ static void lcrypt_start_math(lua_State *L) { (void)luaL_newmetatable(L, "LCRYPT_BIGINT"); (void)luaL_register(L, NULL, lcrypt_bigint_flib); lua_pop(L, 1); - #ifndef USE_NCIPHER - ltc_mp = ltm_desc; - #endif + ltc_mp = ltm_desc; lua_pushstring(L, "bigint"); lua_pushcfunction(L, lcrypt_bigint_create); lua_settable(L, -3); } diff --git a/lcrypt/lcrypt_rsa2.c b/lcrypt/lcrypt_rsa2.c new file mode 100644 index 0000000..69cdcc1 --- /dev/null +++ b/lcrypt/lcrypt_rsa2.c @@ -0,0 +1,115 @@ +const char * lcrypt_rsa = "\n" +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" +";\n" -- cgit v1.2.3