mirror of
https://github.com/nmap/nmap.git
synced 2025-12-07 21:21:31 +00:00
Factor out some code for returning bignums
This commit is contained in:
@@ -30,17 +30,22 @@ typedef struct bignum_data {
|
|||||||
BIGNUM * bn;
|
BIGNUM * bn;
|
||||||
} bignum_data_t;
|
} bignum_data_t;
|
||||||
|
|
||||||
|
static int nse_pushbn( lua_State *L, BIGNUM *num )
|
||||||
|
{
|
||||||
|
bignum_data_t * data = (bignum_data_t *) lua_newuserdata( L, sizeof(bignum_data_t));
|
||||||
|
luaL_getmetatable( L, "BIGNUM" );
|
||||||
|
lua_setmetatable( L, -2 );
|
||||||
|
data->bn = num;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int l_bignum_bin2bn( lua_State *L ) /** bignum_bin2bn( string s ) */
|
static int l_bignum_bin2bn( lua_State *L ) /** bignum_bin2bn( string s ) */
|
||||||
{
|
{
|
||||||
size_t len;
|
size_t len;
|
||||||
const unsigned char * s = (unsigned char *) luaL_checklstring( L, 1, &len );
|
const unsigned char * s = (unsigned char *) luaL_checklstring( L, 1, &len );
|
||||||
BIGNUM * num = BN_new();
|
BIGNUM * num = BN_new();
|
||||||
BN_bin2bn( s, len, num );
|
BN_bin2bn( s, len, num );
|
||||||
bignum_data_t * data = (bignum_data_t *) lua_newuserdata( L, sizeof(bignum_data_t));
|
return nse_pushbn(L, num);
|
||||||
luaL_getmetatable( L, "BIGNUM" );
|
|
||||||
lua_setmetatable( L, -2 );
|
|
||||||
data->bn = num;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_bignum_dec2bn( lua_State *L ) /** bignum_dec2bn( string s ) */
|
static int l_bignum_dec2bn( lua_State *L ) /** bignum_dec2bn( string s ) */
|
||||||
@@ -48,11 +53,7 @@ static int l_bignum_dec2bn( lua_State *L ) /** bignum_dec2bn( string s ) */
|
|||||||
const char * s = luaL_checkstring( L, 1 );
|
const char * s = luaL_checkstring( L, 1 );
|
||||||
BIGNUM * num = BN_new();
|
BIGNUM * num = BN_new();
|
||||||
BN_dec2bn( &num, s );
|
BN_dec2bn( &num, s );
|
||||||
bignum_data_t * data = (bignum_data_t *) lua_newuserdata( L, sizeof(bignum_data_t));
|
return nse_pushbn(L, num);
|
||||||
luaL_getmetatable( L, "BIGNUM" );
|
|
||||||
lua_setmetatable( L, -2 );
|
|
||||||
data->bn = num;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_bignum_hex2bn( lua_State *L ) /** bignum_hex2bn( string s ) */
|
static int l_bignum_hex2bn( lua_State *L ) /** bignum_hex2bn( string s ) */
|
||||||
@@ -60,11 +61,7 @@ static int l_bignum_hex2bn( lua_State *L ) /** bignum_hex2bn( string s ) */
|
|||||||
const char * s = luaL_checkstring( L, 1 );
|
const char * s = luaL_checkstring( L, 1 );
|
||||||
BIGNUM * num = BN_new();
|
BIGNUM * num = BN_new();
|
||||||
BN_hex2bn( &num, s );
|
BN_hex2bn( &num, s );
|
||||||
bignum_data_t * data = (bignum_data_t *) lua_newuserdata( L, sizeof(bignum_data_t));
|
return nse_pushbn(L, num);
|
||||||
luaL_getmetatable( L, "BIGNUM" );
|
|
||||||
lua_setmetatable( L, -2 );
|
|
||||||
data->bn = num;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_bignum_rand( lua_State *L ) /** bignum_rand( number bits ) */
|
static int l_bignum_rand( lua_State *L ) /** bignum_rand( number bits ) */
|
||||||
@@ -72,11 +69,7 @@ static int l_bignum_rand( lua_State *L ) /** bignum_rand( number bits ) */
|
|||||||
size_t bits = luaL_checkint( L, 1 );
|
size_t bits = luaL_checkint( L, 1 );
|
||||||
BIGNUM * num = BN_new();
|
BIGNUM * num = BN_new();
|
||||||
BN_rand( num, bits, -1, 0 );
|
BN_rand( num, bits, -1, 0 );
|
||||||
bignum_data_t * data = (bignum_data_t *) lua_newuserdata( L, sizeof(bignum_data_t));
|
return nse_pushbn(L, num);
|
||||||
luaL_getmetatable( L, "BIGNUM" );
|
|
||||||
lua_setmetatable( L, -2 );
|
|
||||||
data->bn = num;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_bignum_pseudo_rand( lua_State *L ) /** bignum_pseudo_rand( number bits ) */
|
static int l_bignum_pseudo_rand( lua_State *L ) /** bignum_pseudo_rand( number bits ) */
|
||||||
@@ -84,11 +77,7 @@ static int l_bignum_pseudo_rand( lua_State *L ) /** bignum_pseudo_rand( number b
|
|||||||
size_t bits = luaL_checkint( L, 1 );
|
size_t bits = luaL_checkint( L, 1 );
|
||||||
BIGNUM * num = BN_new();
|
BIGNUM * num = BN_new();
|
||||||
BN_pseudo_rand( num, bits, -1, 0 );
|
BN_pseudo_rand( num, bits, -1, 0 );
|
||||||
bignum_data_t * data = (bignum_data_t *) lua_newuserdata( L, sizeof(bignum_data_t));
|
return nse_pushbn(L, num);
|
||||||
luaL_getmetatable( L, "BIGNUM" );
|
|
||||||
lua_setmetatable( L, -2 );
|
|
||||||
data->bn = num;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_bignum_mod_exp( lua_State *L ) /** bignum_mod_exp( BIGNUM a, BIGNUM p, BIGNUM m ) */
|
static int l_bignum_mod_exp( lua_State *L ) /** bignum_mod_exp( BIGNUM a, BIGNUM p, BIGNUM m ) */
|
||||||
@@ -100,11 +89,7 @@ static int l_bignum_mod_exp( lua_State *L ) /** bignum_mod_exp( BIGNUM a, BIGNUM
|
|||||||
BN_CTX * ctx = BN_CTX_new();
|
BN_CTX * ctx = BN_CTX_new();
|
||||||
BN_mod_exp( result, a->bn, p->bn, m->bn, ctx );
|
BN_mod_exp( result, a->bn, p->bn, m->bn, ctx );
|
||||||
BN_CTX_free( ctx );
|
BN_CTX_free( ctx );
|
||||||
bignum_data_t * data = (bignum_data_t *) lua_newuserdata( L, sizeof(bignum_data_t));
|
return nse_pushbn(L, result);
|
||||||
luaL_getmetatable( L, "BIGNUM" );
|
|
||||||
lua_setmetatable( L, -2 );
|
|
||||||
data->bn = result;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_bignum_add( lua_State *L ) /** bignum_add( BIGNUM a, BIGNUM b ) */
|
static int l_bignum_add( lua_State *L ) /** bignum_add( BIGNUM a, BIGNUM b ) */
|
||||||
@@ -113,11 +98,7 @@ static int l_bignum_add( lua_State *L ) /** bignum_add( BIGNUM a, BIGNUM b ) */
|
|||||||
bignum_data_t * b = (bignum_data_t *) luaL_checkudata(L, 2, "BIGNUM");
|
bignum_data_t * b = (bignum_data_t *) luaL_checkudata(L, 2, "BIGNUM");
|
||||||
BIGNUM * result = BN_new();
|
BIGNUM * result = BN_new();
|
||||||
BN_add( result, a->bn, b->bn );
|
BN_add( result, a->bn, b->bn );
|
||||||
bignum_data_t * data = (bignum_data_t *) lua_newuserdata( L, sizeof(bignum_data_t));
|
return nse_pushbn(L, result);
|
||||||
luaL_getmetatable( L, "BIGNUM" );
|
|
||||||
lua_setmetatable( L, -2 );
|
|
||||||
data->bn = result;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_bignum_num_bits( lua_State *L ) /** bignum_num_bits( BIGNUM bn ) */
|
static int l_bignum_num_bits( lua_State *L ) /** bignum_num_bits( BIGNUM bn ) */
|
||||||
|
|||||||
Reference in New Issue
Block a user