summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2016-01-03 12:26:31 +1100
committerdaurnimator <quae@daurnimator.com>2016-01-03 12:35:24 +1100
commitf3c45dfe948872e18c2685bea57b28f401c49809 (patch)
treec64546373ade67ee1daeff60e06a5f9c69b56841
parent0706e6447e3b6e7b8283686b6f89b46c68f8e1fd (diff)
downloadluaossl-f3c45dfe948872e18c2685bea57b28f401c49809.tar.gz
luaossl-f3c45dfe948872e18c2685bea57b28f401c49809.tar.bz2
luaossl-f3c45dfe948872e18c2685bea57b28f401c49809.zip
bignum: Add generatePrime as new constructor
-rw-r--r--src/openssl.c29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/openssl.c b/src/openssl.c
index c311ba6..1ea5d16 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -223,6 +223,14 @@ static const char *xitoa(char *dst, size_t lim, long i) {
223} /* xitoa() */ 223} /* xitoa() */
224 224
225 225
226static _Bool optbool(lua_State *L, int idx, _Bool d) {
227 if (lua_isnoneornil(L, idx))
228 return d;
229 luaL_checktype(L, idx, LUA_TBOOLEAN);
230 return lua_toboolean(L, idx);
231} /* optbool() */
232
233
226static void *prepudata(lua_State *L, size_t size, const char *tname, int (*gc)(lua_State *)) { 234static void *prepudata(lua_State *L, size_t size, const char *tname, int (*gc)(lua_State *)) {
227 void *p = memset(lua_newuserdata(L, size), 0, size); 235 void *p = memset(lua_newuserdata(L, size), 0, size);
228 236
@@ -1976,6 +1984,20 @@ static int bn__gc(lua_State *L) {
1976} /* bn__gc() */ 1984} /* bn__gc() */
1977 1985
1978 1986
1987static int bn_generatePrime(lua_State *L) {
1988 int bits = luaL_checkinteger(L, 1);
1989 _Bool safe = optbool(L, 2, 0);
1990 const BIGNUM *add = lua_isnoneornil(L, 3) ? NULL : checkbig(L, 3);
1991 const BIGNUM *rem = lua_isnoneornil(L, 4) ? NULL : checkbig(L, 4);
1992 BIGNUM *bn = bn_push(L);
1993
1994 if (!BN_generate_prime_ex(bn, bits, safe, add, rem, NULL))
1995 return auxL_error(L, auxL_EOPENSSL, "bignum.generatePrime");
1996
1997 return 1;
1998} /* bn_generatePrime() */
1999
2000
1979static int bn_isPrime(lua_State *L) { 2001static int bn_isPrime(lua_State *L) {
1980 BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS); 2002 BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS);
1981 int nchecks = luaL_optinteger(L, 2, BN_prime_checks); 2003 int nchecks = luaL_optinteger(L, 2, BN_prime_checks);
@@ -2090,9 +2112,10 @@ static const luaL_Reg bn_metatable[] = {
2090 2112
2091 2113
2092static const luaL_Reg bn_globals[] = { 2114static const luaL_Reg bn_globals[] = {
2093 { "new", &bn_new }, 2115 { "new", &bn_new },
2094 { "interpose", &bn_interpose }, 2116 { "interpose", &bn_interpose },
2095 { NULL, NULL }, 2117 { "generatePrime", &bn_generatePrime },
2118 { NULL, NULL },
2096}; 2119};
2097 2120
2098int luaopen__openssl_bignum(lua_State *L) { 2121int luaopen__openssl_bignum(lua_State *L) {