From 71225b77a843d3eeb6bf2fe5123c1a192f6fdabb Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 18 Dec 2017 15:31:39 +1100 Subject: src/openssl.c: Add bn:mod_exp() function --- src/openssl.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src') diff --git a/src/openssl.c b/src/openssl.c index 5ff80fe..ba6e9b6 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -2798,6 +2798,20 @@ static void bn_prepbop(lua_State *L, BIGNUM **r, BIGNUM **a, BIGNUM **b, _Bool c } /* bn_prepbop() */ +/* prepare numbers at top of stack for ternary operation, and push result object onto stack */ +static void bn_preptop(lua_State *L, BIGNUM **r, BIGNUM **a, BIGNUM **b, BIGNUM **c) { + _Bool a_lvalue, b_lvalue, c_lvalue; + + *a = checkbig(L, 1, &a_lvalue); + *b = checkbig(L, 2, &b_lvalue); + *c = checkbig(L, 3, &c_lvalue); + + bn_push(L); + + *r = *(BIGNUM **)lua_touserdata(L, -1); +} /* bn_preptop() */ + + static int ctx__gc(lua_State *L) { BN_CTX **ctx = lua_touserdata(L, 1); @@ -2959,6 +2973,19 @@ static int bn__pow(lua_State *L) { } /* bn__pow() */ +static int bn_mod_exp(lua_State *L) { + BIGNUM *r, *a, *b, *c; + + lua_settop(L, 3); + bn_preptop(L, &r, &a, &b, &c); + + if (!BN_mod_exp(r, a, b, c, getctx(L))) + return auxL_error(L, auxL_EOPENSSL, "bignum:mod_exp"); + + return 1; +} /* bn_mod_exp() */ + + static int bn_gcd(lua_State *L) { BIGNUM *r, *a, *b; @@ -3153,6 +3180,7 @@ static const auxL_Reg bn_methods[] = { { "mod", &bn__mod }, { "nnmod", &bn_nnmod }, { "exp", &bn__pow }, + { "mod_exp", &bn_mod_exp }, { "gcd", &bn_gcd }, { "lshift", &bn__shl }, { "rshift", &bn__shr }, -- cgit v1.2.3-59-g8ed1b From a44ab92ae547eb103af7f47cbdf4045bbb61e101 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 18 Dec 2017 16:00:39 +1100 Subject: src/openssl.c: Add bn:mod_{add,sub,mul} functions --- src/openssl.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'src') diff --git a/src/openssl.c b/src/openssl.c index ba6e9b6..38d5e8d 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -2960,6 +2960,45 @@ static int bn_nnmod(lua_State *L) { } /* bn_nnmod() */ +static int bn_mod_add(lua_State *L) { + BIGNUM *r, *a, *b, *c; + + lua_settop(L, 3); + bn_preptop(L, &r, &a, &b, &c); + + if (!BN_mod_add(r, a, b, c, getctx(L))) + return auxL_error(L, auxL_EOPENSSL, "bignum:mod_add"); + + return 1; +} /* bn_mod_add() */ + + +static int bn_mod_sub(lua_State *L) { + BIGNUM *r, *a, *b, *c; + + lua_settop(L, 3); + bn_preptop(L, &r, &a, &b, &c); + + if (!BN_mod_sub(r, a, b, c, getctx(L))) + return auxL_error(L, auxL_EOPENSSL, "bignum:mod_sub"); + + return 1; +} /* bn_mod_sub() */ + + +static int bn_mod_mul(lua_State *L) { + BIGNUM *r, *a, *b, *c; + + lua_settop(L, 3); + bn_preptop(L, &r, &a, &b, &c); + + if (!BN_mod_mul(r, a, b, c, getctx(L))) + return auxL_error(L, auxL_EOPENSSL, "bignum:mod_mul"); + + return 1; +} /* bn_mod_mul() */ + + static int bn__pow(lua_State *L) { BIGNUM *r, *a, *b; @@ -3179,6 +3218,9 @@ static const auxL_Reg bn_methods[] = { { "idiv", &bn__idiv }, { "mod", &bn__mod }, { "nnmod", &bn_nnmod }, + { "mod_add", &bn_mod_add }, + { "mod_sub", &bn_mod_sub }, + { "mod_mul", &bn_mod_mul }, { "exp", &bn__pow }, { "mod_exp", &bn_mod_exp }, { "gcd", &bn_gcd }, -- cgit v1.2.3-59-g8ed1b From 09402eb482df069d21b07a0c8468824a5efff30a Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 18 Dec 2017 16:00:50 +1100 Subject: src/openssl.c: Add bn:mod_sqr function --- src/openssl.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src') diff --git a/src/openssl.c b/src/openssl.c index 38d5e8d..5bec55a 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -2999,6 +2999,19 @@ static int bn_mod_mul(lua_State *L) { } /* bn_mod_mul() */ +static int bn_mod_sqr(lua_State *L) { + BIGNUM *r, *a, *b; + + lua_settop(L, 2); + bn_prepbop(L, &r, &a, &b, 0); + + if (!BN_mod_sqr(r, a, b, getctx(L))) + return auxL_error(L, auxL_EOPENSSL, "bignum:mod_sqr"); + + return 1; +} /* bn_mod_sqr() */ + + static int bn__pow(lua_State *L) { BIGNUM *r, *a, *b; @@ -3221,6 +3234,7 @@ static const auxL_Reg bn_methods[] = { { "mod_add", &bn_mod_add }, { "mod_sub", &bn_mod_sub }, { "mod_mul", &bn_mod_mul }, + { "mod_sqr", &bn_mod_sqr }, { "exp", &bn__pow }, { "mod_exp", &bn_mod_exp }, { "gcd", &bn_gcd }, -- cgit v1.2.3-59-g8ed1b