summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Ahern <william@25thandClement.com>2016-01-04 15:31:32 +0800
committerWilliam Ahern <william@25thandClement.com>2016-01-04 15:31:32 +0800
commitcf985a41579b24a9aa821a7e9d95ee21ffca7a9e (patch)
treec64546373ade67ee1daeff60e06a5f9c69b56841
parent86a593a0548563d156d7adc55cff179340a811c8 (diff)
parentf3c45dfe948872e18c2685bea57b28f401c49809 (diff)
downloadluaossl-cf985a41579b24a9aa821a7e9d95ee21ffca7a9e.tar.gz
luaossl-cf985a41579b24a9aa821a7e9d95ee21ffca7a9e.tar.bz2
luaossl-cf985a41579b24a9aa821a7e9d95ee21ffca7a9e.zip
Merge branch 'daurnimator-bignum-methods'
-rw-r--r--src/openssl.c110
1 files changed, 94 insertions, 16 deletions
diff --git a/src/openssl.c b/src/openssl.c
index 499bcce..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
@@ -1814,6 +1822,18 @@ static int bn__mul(lua_State *L) {
1814} /* bn__mul() */ 1822} /* bn__mul() */
1815 1823
1816 1824
1825static int bn_sqr(lua_State *L) {
1826 BIGNUM *r, *a;
1827
1828 bn_prepops(L, &r, &a, NULL, 1);
1829
1830 if (!BN_sqr(r, a, getctx(L)))
1831 return auxL_error(L, auxL_EOPENSSL, "bignum:sqr");
1832
1833 return 1;
1834} /* bn_sqr() */
1835
1836
1817static int bn__idiv(lua_State *L) { 1837static int bn__idiv(lua_State *L) {
1818 BIGNUM *dv, *a, *b; 1838 BIGNUM *dv, *a, *b;
1819 1839
@@ -1844,6 +1864,18 @@ static int bn__mod(lua_State *L) {
1844} /* bn__mod() */ 1864} /* bn__mod() */
1845 1865
1846 1866
1867static int bn_nnmod(lua_State *L) {
1868 BIGNUM *r, *a, *b;
1869
1870 bn_prepops(L, &r, &a, &b, 0);
1871
1872 if (!BN_nnmod(r, a, b, getctx(L)))
1873 return auxL_error(L, auxL_EOPENSSL, "bignum:nnmod");
1874
1875 return 1;
1876} /* bn_nnmod() */
1877
1878
1847static int bn__pow(lua_State *L) { 1879static int bn__pow(lua_State *L) {
1848 BIGNUM *r, *a, *b; 1880 BIGNUM *r, *a, *b;
1849 1881
@@ -1856,6 +1888,18 @@ static int bn__pow(lua_State *L) {
1856} /* bn__pow() */ 1888} /* bn__pow() */
1857 1889
1858 1890
1891static int bn_gcd(lua_State *L) {
1892 BIGNUM *r, *a, *b;
1893
1894 bn_prepops(L, &r, &a, &b, 1);
1895
1896 if (!BN_gcd(r, a, b, getctx(L)))
1897 return auxL_error(L, auxL_EOPENSSL, "bignum:gcd");
1898
1899 return 1;
1900} /* bn_gcd() */
1901
1902
1859static int bn__shl(lua_State *L) { 1903static int bn__shl(lua_State *L) {
1860 BIGNUM *r, *a; 1904 BIGNUM *r, *a;
1861 int n; 1905 int n;
@@ -1888,8 +1932,9 @@ static int bn__shr(lua_State *L) {
1888 1932
1889static int bn__unm(lua_State *L) { 1933static int bn__unm(lua_State *L) {
1890 BIGNUM *a = checksimple(L, 1, BIGNUM_CLASS); 1934 BIGNUM *a = checksimple(L, 1, BIGNUM_CLASS);
1935 BIGNUM *r = bn_dup(L, a);
1891 1936
1892 BN_set_negative(a, !BN_is_negative(a)); 1937 BN_set_negative(r, !BN_is_negative(a));
1893 1938
1894 return 1; 1939 return 1;
1895} /* bn__unm() */ 1940} /* bn__unm() */
@@ -1939,6 +1984,34 @@ static int bn__gc(lua_State *L) {
1939} /* bn__gc() */ 1984} /* bn__gc() */
1940 1985
1941 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
2001static int bn_isPrime(lua_State *L) {
2002 BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS);
2003 int nchecks = luaL_optinteger(L, 2, BN_prime_checks);
2004 int res = BN_is_prime_ex(bn, nchecks, getctx(L), NULL);
2005
2006 if (res == -1)
2007 return auxL_error(L, auxL_EOPENSSL, "bignum:isPrime");
2008
2009 lua_pushboolean(L, res);
2010
2011 return 1;
2012} /* bn_isPrime() */
2013
2014
1942static BIO *getbio(lua_State *); 2015static BIO *getbio(lua_State *);
1943 2016
1944static int bn_todec(lua_State *L) { 2017static int bn_todec(lua_State *L) {
@@ -2000,18 +2073,22 @@ sslerr:
2000 2073
2001 2074
2002static const luaL_Reg bn_methods[] = { 2075static const luaL_Reg bn_methods[] = {
2003 { "add", &bn__add }, 2076 { "add", &bn__add },
2004 { "sub", &bn__sub }, 2077 { "sub", &bn__sub },
2005 { "mul", &bn__mul }, 2078 { "mul", &bn__mul },
2006 { "idiv", &bn__idiv }, 2079 { "sqr", &bn_sqr },
2007 { "mod", &bn__mod }, 2080 { "idiv", &bn__idiv },
2008 { "pow", &bn__pow }, 2081 { "mod", &bn__mod },
2009 { "shl", &bn__shl }, 2082 { "nnmod", &bn_nnmod },
2010 { "shr", &bn__shr }, 2083 { "exp", &bn__pow },
2011 { "tobin", &bn_tobin }, 2084 { "gcd", &bn_gcd },
2012 { "todec", &bn_todec }, 2085 { "lshift", &bn__shl },
2013 { "tohex", &bn_tohex }, 2086 { "rshift", &bn__shr },
2014 { NULL, NULL }, 2087 { "isPrime", &bn_isPrime },
2088 { "tobin", &bn_tobin },
2089 { "todec", &bn_todec },
2090 { "tohex", &bn_tohex },
2091 { NULL, NULL },
2015}; 2092};
2016 2093
2017static const luaL_Reg bn_metatable[] = { 2094static const luaL_Reg bn_metatable[] = {
@@ -2035,9 +2112,10 @@ static const luaL_Reg bn_metatable[] = {
2035 2112
2036 2113
2037static const luaL_Reg bn_globals[] = { 2114static const luaL_Reg bn_globals[] = {
2038 { "new", &bn_new }, 2115 { "new", &bn_new },
2039 { "interpose", &bn_interpose }, 2116 { "interpose", &bn_interpose },
2040 { NULL, NULL }, 2117 { "generatePrime", &bn_generatePrime },
2118 { NULL, NULL },
2041}; 2119};
2042 2120
2043int luaopen__openssl_bignum(lua_State *L) { 2121int luaopen__openssl_bignum(lua_State *L) {