diff options
author | William Ahern <william+macosx@25thandclement.com> | 2016-02-16 13:56:10 -0800 |
---|---|---|
committer | William Ahern <william+macosx@25thandclement.com> | 2016-02-16 13:56:10 -0800 |
commit | a5937e7257bf15052ad80fc5d3a7964f582318c7 (patch) | |
tree | 9173599346e857dbec99c61b8ad00310aabd6cb2 | |
parent | 8d0e1581ce8642f692a64a0e35d60d1c6e6e59eb (diff) | |
parent | f53625badddd6c75421e5d5132c4eac6ee6eb01d (diff) | |
download | luaossl-a5937e7257bf15052ad80fc5d3a7964f582318c7.tar.gz luaossl-a5937e7257bf15052ad80fc5d3a7964f582318c7.tar.bz2 luaossl-a5937e7257bf15052ad80fc5d3a7964f582318c7.zip |
Merge branch 'bignum-new' of git://github.com/daurnimator/luaossl into daurnimator-bignum-new
-rw-r--r-- | src/openssl.c | 49 |
1 files changed, 42 insertions, 7 deletions
diff --git a/src/openssl.c b/src/openssl.c index f0c5684..11d02a0 100644 --- a/src/openssl.c +++ b/src/openssl.c | |||
@@ -29,7 +29,7 @@ | |||
29 | #include <strings.h> /* strcasecmp(3) */ | 29 | #include <strings.h> /* strcasecmp(3) */ |
30 | #include <math.h> /* INFINITY fabs(3) floor(3) frexp(3) fmod(3) round(3) isfinite(3) */ | 30 | #include <math.h> /* INFINITY fabs(3) floor(3) frexp(3) fmod(3) round(3) isfinite(3) */ |
31 | #include <time.h> /* struct tm time_t strptime(3) time(2) */ | 31 | #include <time.h> /* struct tm time_t strptime(3) time(2) */ |
32 | #include <ctype.h> /* tolower(3) */ | 32 | #include <ctype.h> /* isdigit(3), isxdigit(3), tolower(3) */ |
33 | #include <errno.h> /* ENOMEM ENOTSUP EOVERFLOW errno */ | 33 | #include <errno.h> /* ENOMEM ENOTSUP EOVERFLOW errno */ |
34 | #include <assert.h> /* assert */ | 34 | #include <assert.h> /* assert */ |
35 | 35 | ||
@@ -1757,6 +1757,17 @@ static int bn_new(lua_State *L) { | |||
1757 | } /* bn_new() */ | 1757 | } /* bn_new() */ |
1758 | 1758 | ||
1759 | 1759 | ||
1760 | static int bn_fromBinary(lua_State *L) { | ||
1761 | size_t len; | ||
1762 | const char *s = luaL_checklstring(L, 1, &len); | ||
1763 | BIGNUM *bn = bn_push(L); | ||
1764 | if (!BN_bin2bn((const unsigned char*)s, len, bn)) { | ||
1765 | auxL_error(L, auxL_EOPENSSL, "bignum"); | ||
1766 | } | ||
1767 | return 1; | ||
1768 | } /* bn_fromBinary() */ | ||
1769 | |||
1770 | |||
1760 | static int bn_interpose(lua_State *L) { | 1771 | static int bn_interpose(lua_State *L) { |
1761 | return interpose(L, BIGNUM_CLASS); | 1772 | return interpose(L, BIGNUM_CLASS); |
1762 | } /* bn_interpose() */ | 1773 | } /* bn_interpose() */ |
@@ -1832,8 +1843,9 @@ static _Bool f2bn(BIGNUM **bn, double f) { | |||
1832 | 1843 | ||
1833 | static BIGNUM *(checkbig)(lua_State *L, int index, _Bool *lvalue) { | 1844 | static BIGNUM *(checkbig)(lua_State *L, int index, _Bool *lvalue) { |
1834 | BIGNUM **bn; | 1845 | BIGNUM **bn; |
1835 | const char *dec; | 1846 | const char *str; |
1836 | size_t len; | 1847 | size_t len, i; |
1848 | _Bool neg, hex; | ||
1837 | 1849 | ||
1838 | index = lua_absindex(L, index); | 1850 | index = lua_absindex(L, index); |
1839 | 1851 | ||
@@ -1841,14 +1853,36 @@ static BIGNUM *(checkbig)(lua_State *L, int index, _Bool *lvalue) { | |||
1841 | case LUA_TSTRING: | 1853 | case LUA_TSTRING: |
1842 | *lvalue = 0; | 1854 | *lvalue = 0; |
1843 | 1855 | ||
1844 | dec = lua_tolstring(L, index, &len); | 1856 | str = lua_tolstring(L, index, &len); |
1845 | 1857 | ||
1846 | luaL_argcheck(L, len > 0 && *dec, index, "invalid big number string"); | 1858 | neg = (str[0] == '-'); |
1859 | hex = (str[neg] == '0' && (str[neg+1] == 'x' || str[neg+1] == 'X')); | ||
1860 | |||
1861 | if (hex) { | ||
1862 | luaL_argcheck(L, len > 2+(size_t)neg, index, "invalid hex string"); | ||
1863 | for (i = 2+neg; i < len; i++) { | ||
1864 | if (!isxdigit(str[i])) | ||
1865 | luaL_argerror(L, 1, "invalid hex string"); | ||
1866 | } | ||
1867 | } else { | ||
1868 | luaL_argcheck(L, len > neg, index, "invalid decimal string"); | ||
1869 | for (i = neg; i < len; i++) { | ||
1870 | if (!isdigit(str[i])) | ||
1871 | luaL_argerror(L, 1, "invalid decimal string"); | ||
1872 | } | ||
1873 | } | ||
1847 | 1874 | ||
1848 | bn = prepsimple(L, BIGNUM_CLASS); | 1875 | bn = prepsimple(L, BIGNUM_CLASS); |
1849 | 1876 | ||
1850 | if (!BN_dec2bn(bn, dec)) | 1877 | if (hex) { |
1851 | auxL_error(L, auxL_EOPENSSL, "bignum"); | 1878 | if (!BN_hex2bn(bn, str+2+neg)) |
1879 | auxL_error(L, auxL_EOPENSSL, "bignum"); | ||
1880 | if (neg) | ||
1881 | BN_set_negative(*bn, 1); | ||
1882 | } else { | ||
1883 | if (!BN_dec2bn(bn, str)) | ||
1884 | auxL_error(L, auxL_EOPENSSL, "bignum"); | ||
1885 | } | ||
1852 | 1886 | ||
1853 | lua_replace(L, index); | 1887 | lua_replace(L, index); |
1854 | 1888 | ||
@@ -2273,6 +2307,7 @@ static const auxL_Reg bn_metatable[] = { | |||
2273 | static const auxL_Reg bn_globals[] = { | 2307 | static const auxL_Reg bn_globals[] = { |
2274 | { "new", &bn_new }, | 2308 | { "new", &bn_new }, |
2275 | { "interpose", &bn_interpose }, | 2309 | { "interpose", &bn_interpose }, |
2310 | { "fromBinary", &bn_fromBinary }, | ||
2276 | { "generatePrime", &bn_generatePrime }, | 2311 | { "generatePrime", &bn_generatePrime }, |
2277 | { NULL, NULL }, | 2312 | { NULL, NULL }, |
2278 | }; | 2313 | }; |