/*----- Header files ------------------------------------------------------*/
+#define PY_SSIZE_T_CLEAN
+
#include <Python.h>
#include <longintrepr.h>
#include <structmember.h>
-#undef ULLONG_MAX
-#undef ULONG_LONG_MAX
-
#include <mLib/darray.h>
#include <mLib/dstr.h>
#include <mLib/macros.h>
#include <mLib/unihash.h>
#include <catacomb/buf.h>
+#include <catacomb/ct.h>
#include <catacomb/grand.h>
#include <catacomb/rand.h>
#include <catacomb/gmac.h>
#include <catacomb/md5.h>
#include <catacomb/md5-hmac.h>
+#include <catacomb/poly1305.h>
#include <catacomb/sha.h>
#include <catacomb/sha-mgf.h>
#include <catacomb/sha-hmac.h>
#include <catacomb/ptab.h>
#include <catacomb/bintab.h>
#include <catacomb/dsa.h>
+#include <catacomb/x25519.h>
+#include <catacomb/x448.h>
+#include <catacomb/ed25519.h>
#include <catacomb/gf.h>
#include <catacomb/gfreduce.h>
#define root_pytype 0
#define type_pytype &PyType_Type
-#define INITTYPE(ty, base) do { \
+#define INITTYPE_META(ty, base, meta) do { \
ty##_pytype_skel.tp_base = base##_pytype; \
- ty##_pytype = inittype(&ty##_pytype_skel); \
+ ty##_pytype = inittype(&ty##_pytype_skel, meta##_pytype); \
} while (0)
+#define INITTYPE(ty, base) INITTYPE_META(ty, base, type)
#define INSERT(name, ob) do { \
PyObject *_o = (PyObject *)(ob); \
DOUINTSZ(DECL_CONVU_)
extern int convmpw(PyObject *, void *);
extern int convuint(PyObject *, void *);
+extern int convk64(PyObject *, void *);
extern int convszt(PyObject *, void *);
extern int convbool(PyObject *, void *);
extern PyObject *abstract_pynew(PyTypeObject *, PyObject *, PyObject *);
extern PyObject *getbool(int);
extern PyObject *getulong(unsigned long);
+extern PyObject *getk64(kludge64);
extern void *newtype(PyTypeObject *, const PyTypeObject *, const char *);
extern PyObject *mkexc(PyObject *, PyObject *, const char *, PyMethodDef *);
extern void typeready(PyTypeObject *);
-extern PyTypeObject *inittype(PyTypeObject *);
+extern PyTypeObject *inittype(PyTypeObject *, PyTypeObject *);
extern void addmethods(const PyMethodDef *);
extern PyMethodDef *donemethods(void);
C(KF_NONSECRET),
C(KF_BURN), C(KF_OPT),
C(EC_XONLY), C(EC_YBIT), C(EC_LSB), C(EC_CMPR), C(EC_EXPLY), C(EC_SORT),
+ C(X25519_KEYSZ), C(X25519_PUBSZ), C(X25519_OUTSZ),
+ C(X448_KEYSZ), C(X448_PUBSZ), C(X448_OUTSZ),
+ C(ED25519_KEYSZ), C(ED25519_PUBSZ), C(ED25519_SIGSZ),
#define ENTRY(tag, val, str) C(KERR_##tag),
KEY_ERRORS(ENTRY)
#undef ENTRY
return (PyLong_FromUnsignedLong(w));
}
+static PyObject *i32 = 0;
+static int init_i32(void)
+ { if (!i32 && (i32 = PyInt_FromLong(32)) == 0) return (-1); return (0); }
+
+PyObject *getk64(kludge64 u)
+{
+ PyObject *i = 0, *j = 0, *t;
+ PyObject *rc = 0;
+
+ if (init_i32()) goto end;
+ if ((i = PyLong_FromUnsignedLong(HI64(u))) == 0) goto end;
+ if ((t = PyNumber_InPlaceLshift(i, i32)) == 0) goto end;
+ Py_DECREF(i); i = t;
+ if ((j = PyLong_FromUnsignedLong(LO64(u))) == 0) goto end;
+ if ((t = PyNumber_InPlaceOr(i, j)) == 0) goto end;
+ Py_DECREF(i); i = t;
+ if ((rc = PyNumber_Int(i)) == 0) goto end;
+end:
+ if (i) Py_DECREF(i);
+ if (j) Py_DECREF(j);
+ return (rc);
+}
+
PyObject *getbool(int b)
{
if (b) RETURN_TRUE;
return (0);
}
+int convk64(PyObject *o, void *pp)
+{
+ PyObject *i = 0, *t;
+ int rc = 0;
+ uint32 lo, hi;
+
+ if (init_i32()) goto end;
+ if ((i = PyNumber_Int(o)) == 0) goto end;
+ lo = PyInt_AsUnsignedLongMask(i);
+ if ((t = PyNumber_InPlaceRshift(i, i32)) == 0) goto end;
+ Py_DECREF(i); i = t;
+ hi = PyInt_AsUnsignedLongMask(i);
+ if ((t = PyNumber_InPlaceRshift(i, i32)) == 0) goto end;
+ Py_DECREF(i); i = t;
+ if (PyObject_IsTrue(i)) VALERR("out of range");
+ SET64(*(kludge64 *)pp, hi, lo);
+ rc = 1;
+end:
+ if (i) Py_DECREF(i);
+ return (rc);
+}
+
int convmpw(PyObject *o, void *pp)
{
unsigned long u;
PyDict_SetItemString(ty->tp_dict, "__module__", modname);
}
-PyTypeObject *inittype(PyTypeObject *tyskel)
+PyTypeObject *inittype(PyTypeObject *tyskel, PyTypeObject *meta)
{
- PyTypeObject *ty = newtype(&PyType_Type, tyskel, 0);
+ PyTypeObject *ty = newtype(meta, tyskel, 0);
ty->tp_flags |= Py_TPFLAGS_HEAPTYPE;
typeready(ty);
return (ty);
/*----- Exceptions --------------------------------------------------------*/
-PyObject * mkexc(PyObject *mod, PyObject *base,
- const char *name, PyMethodDef *mm)
+PyObject *mkexc(PyObject *mod, PyObject *base,
+ const char *name, PyMethodDef *mm)
{
PyObject *nameobj = 0;
PyObject *dict = 0;