diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2016-04-06 19:19:32 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2016-04-11 19:55:33 +0200 |
commit | f51df5da26a67387f9dac0f35aa7eea4f77cd6d7 (patch) | |
tree | 0b81e3fdf0ad214e00dc5a2cfb3e5ffc75a2dd07 /SWIG/_ec.i | |
parent | 3aa2c8a70855685bbecd7575efa5f7631013808a (diff) | |
download | m2crypto-f51df5da26a67387f9dac0f35aa7eea4f77cd6d7.tar.gz |
Add implementation of the function ec_get_builtin_curves.
* including Python interface (EC.get_builtin_curves).
* including test for EC.get_builtin_curves
Diffstat (limited to 'SWIG/_ec.i')
-rw-r--r-- | SWIG/_ec.i | 51 |
1 files changed, 51 insertions, 0 deletions
@@ -115,6 +115,57 @@ void ec_init(PyObject *ec_err) { _ec_err = ec_err; } +PyObject *ec_get_builtin_curves(void) { + /* size_t EC_get_builtin_curves(EC_builtin_curve *r, size_t + * nitems); */ + EC_builtin_curve *curves; + Py_ssize_t ret_curves = 0; + size_t num_curves = EC_get_builtin_curves(NULL, 0); + PyObject *ret_tuple = NULL; + PyObject *ret_dict = NULL; + Py_ssize_t i; + const char *comment; + const char *sname; + + if (!(curves = PyMem_Malloc(num_curves * sizeof(EC_builtin_curve)))) { + PyErr_SetString(PyExc_MemoryError, "ec_get_builtin_curves"); + return NULL; + } + + ret_curves = (Py_ssize_t)EC_get_builtin_curves(curves, num_curves); + + if (!(ret_tuple = PyTuple_New(ret_curves))) { + PyErr_SetString(PyExc_MemoryError, "ec_get_builtin_curves"); + return NULL; + } + + for (i = 0; i < ret_curves; i++) { + if (!(ret_dict = PyDict_New())) { + PyErr_SetString(PyExc_MemoryError, "ec_get_builtin_curves"); + return NULL; + } + + comment = curves[i].comment; + sname = OBJ_nid2sn(curves[i].nid); + if (sname == NULL) + sname = ""; + + PyDict_SetItemString(ret_dict, "NID", + PyLong_FromLong((long)curves[i].nid)); + PyDict_SetItemString(ret_dict, "sname", + PyString_FromString(sname)); + PyDict_SetItemString(ret_dict, "comment", + PyString_FromString(comment)); + + PyTuple_SET_ITEM(ret_tuple, i, ret_dict); + + } + + PyMem_Free(curves); + + return ret_tuple; +} + EC_KEY* ec_key_new_by_curve_name(int nid) { EC_KEY *key; |