summaryrefslogtreecommitdiff
path: root/SWIG/_bn.i
diff options
context:
space:
mode:
Diffstat (limited to 'SWIG/_bn.i')
-rw-r--r--[-rwxr-xr-x]SWIG/_bn.i84
1 files changed, 49 insertions, 35 deletions
diff --git a/SWIG/_bn.i b/SWIG/_bn.i
index a3b73c4..18dc154 100755..100644
--- a/SWIG/_bn.i
+++ b/SWIG/_bn.i
@@ -17,27 +17,32 @@
%inline %{
PyObject *bn_rand(int bits, int top, int bottom)
{
- BIGNUM rnd;
+ BIGNUM* rnd;
PyObject *ret;
char *randhex;
-
- BN_init(&rnd);
- if (!BN_rand(&rnd, bits, top, bottom)) {
+
+ rnd = BN_new();
+ if (rnd == NULL) {
+ m2_PyErr_Msg(PyExc_Exception);
+ return NULL;
+ }
+
+ if (!BN_rand(rnd, bits, top, bottom)) {
/*Custom errors?*/
- PyErr_SetString(PyExc_Exception, ERR_reason_error_string(ERR_get_error()));
- BN_free(&rnd);
+ m2_PyErr_Msg(PyExc_Exception);
+ BN_free(rnd);
return NULL;
}
-
- randhex = BN_bn2hex(&rnd);
+
+ randhex = BN_bn2hex(rnd);
if (!randhex) {
/*Custom errors?*/
- PyErr_SetString(PyExc_Exception, ERR_reason_error_string(ERR_get_error()));
- BN_free(&rnd);
+ m2_PyErr_Msg(PyExc_Exception);
+ BN_free(rnd);
return NULL;
}
- BN_free(&rnd);
-
+ BN_free(rnd);
+
ret = PyLong_FromString(randhex, NULL, 16);
OPENSSL_free(randhex);
return ret;
@@ -46,15 +51,18 @@ PyObject *bn_rand(int bits, int top, int bottom)
PyObject *bn_rand_range(PyObject *range)
{
- BIGNUM rnd;
+ BIGNUM* rnd;
BIGNUM *rng = NULL;
PyObject *ret, *tuple;
PyObject *format, *rangePyString;
- char *randhex, *rangehex;
-
+ char *randhex; /* PyLong_FromString is unhappy with const */
+ const char *rangehex;
+
/* Wow, it's a lot of work to convert into a hex string in C! */
- format = PyString_FromString("%x");
+ format = PyUnicode_FromString("%x");
+
if (!format) {
+ PyErr_SetString(PyExc_RuntimeError, "Cannot create Python string '%x'");
return NULL;
}
tuple = PyTuple_New(1);
@@ -65,47 +73,53 @@ PyObject *bn_rand_range(PyObject *range)
}
Py_INCREF(range);
PyTuple_SET_ITEM(tuple, 0, range);
- rangePyString = PyString_Format(format, tuple);
+
+ rangePyString = PyUnicode_Format(format, tuple);
+
if (!rangePyString) {
- PyErr_SetString(PyExc_Exception, "PyString_Format failed");
+ PyErr_SetString(PyExc_Exception, "String Format failed");
Py_DECREF(format);
Py_DECREF(tuple);
- return NULL;
+ return NULL;
}
Py_DECREF(format);
Py_DECREF(tuple);
- rangehex = PyString_AsString(rangePyString);
-
+
+ rangehex = (const char*)PyUnicode_AsUTF8(rangePyString);
+
if (!BN_hex2bn(&rng, rangehex)) {
/*Custom errors?*/
- PyErr_SetString(PyExc_Exception, ERR_reason_error_string(ERR_get_error()));
+ m2_PyErr_Msg(PyExc_Exception);
Py_DECREF(rangePyString);
- return NULL;
+ return NULL;
}
Py_DECREF(rangePyString);
-
- BN_init(&rnd);
- if (!BN_rand_range(&rnd, rng)) {
+ if (!(rnd = BN_new())) {
+ PyErr_SetString(PyExc_MemoryError, "bn_rand_range");
+ return NULL;
+ }
+
+ if (!BN_rand_range(rnd, rng)) {
/*Custom errors?*/
- PyErr_SetString(PyExc_Exception, ERR_reason_error_string(ERR_get_error()));
- BN_free(&rnd);
+ m2_PyErr_Msg(PyExc_Exception);
+ BN_free(rnd);
BN_free(rng);
- return NULL;
- }
+ return NULL;
+ }
BN_free(rng);
- randhex = BN_bn2hex(&rnd);
+ randhex = BN_bn2hex(rnd);
if (!randhex) {
/*Custom errors?*/
- PyErr_SetString(PyExc_Exception, ERR_reason_error_string(ERR_get_error()));
- BN_free(&rnd);
+ m2_PyErr_Msg(PyExc_Exception);
+ BN_free(rnd);
return NULL;
}
- BN_free(&rnd);
-
+ BN_free(rnd);
+
ret = PyLong_FromString(randhex, NULL, 16);
OPENSSL_free(randhex);
return ret;