X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/catacomb-python/blobdiff_plain/3aa33042dc760937bb9da54c09f7c668f00eb241..183e9cd31b1ac2f14b86c5de6ac2643b8a4364a2:/algorithms.c diff --git a/algorithms.c b/algorithms.c index a740a71..7eab3ae 100644 --- a/algorithms.c +++ b/algorithms.c @@ -1,13 +1,11 @@ /* -*-c-*- - * - * $Id$ * * Symmetric cryptography * * (c) 2004 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of the Python interface to Catacomb. * @@ -15,12 +13,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * Catacomb/Python is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with Catacomb/Python; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. @@ -37,38 +35,49 @@ PyTypeObject *keysz_pytype; PyTypeObject *keyszany_pytype, *keyszrange_pytype, *keyszset_pytype; PyObject *sha_pyobj, *has160_pyobj; +#ifndef KSZ_OPMASK +# define KSZ_OPMASK 0x1f +#endif + +#ifndef KSZ_16BIT +# define KSZ_16BIT 0x20 +#endif + PyObject *keysz_pywrap(const octet *k) { - switch (k[0]) { + unsigned op = *k++; +#define ARG(i) (op&KSZ_16BIT ? LOAD16(k + 2*(i)) : k[i]) + switch (op&KSZ_OPMASK) { case KSZ_ANY: { keysz_pyobj *o = PyObject_New(keysz_pyobj, keyszany_pytype); - o->dfl = k[1]; + o->dfl = ARG(0); return ((PyObject *)o); } break; case KSZ_RANGE: { keyszrange_pyobj *o = - PyObject_New(keyszrange_pyobj, keyszrange_pytype); - o->dfl = k[1]; - o->min = k[2]; - o->max = k[3]; - o->mod = k[4]; + PyObject_New(keyszrange_pyobj, keyszrange_pytype); + o->dfl = ARG(0); + o->min = ARG(1); + o->max = ARG(2); + o->mod = ARG(3); if (!o->mod) o->mod = 1; return ((PyObject *)o); } break; case KSZ_SET: { keyszset_pyobj *o = - PyObject_New(keyszset_pyobj, keyszset_pytype); + PyObject_New(keyszset_pyobj, keyszset_pytype); int i, n; - o->dfl = k[1]; - for (i = 0; k[i + 1]; i++) ; + o->dfl = ARG(0); + for (i = 0; ARG(i); i++) ; n = i; o->set = PyTuple_New(n); for (i = 0; i < n; i++) - PyTuple_SET_ITEM(o->set, i, PyInt_FromLong(k[i + 1])); + PyTuple_SET_ITEM(o->set, i, PyInt_FromLong(ARG(i))); return ((PyObject *)o); } break; default: abort(); } +#undef ARG } static PyObject *keyszany_pynew(PyTypeObject *ty, @@ -98,11 +107,9 @@ static PyObject *keyszrange_pynew(PyTypeObject *ty, if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|iii:new", kwlist, &dfl, &min, &max, &mod)) goto end; - if (dfl < 0 || min < 0 || max < 0) - VALERR("key size cannot be negative"); - if (min > dfl || (max && dfl > max)) - VALERR("bad key size bounds"); - if (mod <= 0 || dfl % mod || min % mod || max % mod) + if (dfl < 0 || min < 0) VALERR("key size cannot be negative"); + if (min > dfl || (max && dfl > max)) VALERR("bad key size bounds"); + if (mod <= 0 || dfl%mod || min%mod || max%mod) VALERR("bad key size modulus"); o = (keyszrange_pyobj *)ty->tp_alloc(ty, 0); o->dfl = dfl; @@ -123,8 +130,7 @@ static PyObject *keyszset_pynew(PyTypeObject *ty, PyObject *x = 0, *l = 0; keyszset_pyobj *o = 0; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|O:new", kwlist, - &dfl, &set)) + if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|O:new", kwlist, &dfl, &set)) goto end; if (!set) set = PyTuple_New(0); else Py_INCREF(set); @@ -145,7 +151,7 @@ static PyObject *keyszset_pynew(PyTypeObject *ty, if (xx < 0) VALERR("key size cannot be negative"); PyList_Append(l, x); Py_DECREF(x); - x = 0; + x = 0; } Py_DECREF(set); if ((set = PySequence_Tuple(l)) == 0) goto end; @@ -231,8 +237,8 @@ static PyMemberDef keyszset_pymembers[] = { }; static PyTypeObject keysz_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.KeySZ", /* @tp_name@ */ + PyObject_HEAD_INIT(0) 0, /* Header */ + "KeySZ", /* @tp_name@ */ sizeof(keysz_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -255,14 +261,14 @@ static PyTypeObject keysz_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Key size constraints.", +"Key size constraints. Abstract.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ + 0, /* @tp_iternext@ */ 0, /* @tp_methods@ */ keysz_pymembers, /* @tp_members@ */ 0, /* @tp_getset@ */ @@ -279,8 +285,8 @@ static PyTypeObject keysz_pytype_skel = { }; static PyTypeObject keyszany_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.KeySZAny", /* @tp_name@ */ + PyObject_HEAD_INIT(0) 0, /* Header */ + "KeySZAny", /* @tp_name@ */ sizeof(keysz_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -303,14 +309,15 @@ static PyTypeObject keyszany_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Key size constraints. This object imposes no constraints on size.", +"KeySZAny(DEFAULT)\n\ + Key size constraints. This object imposes no constraints on size.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ + 0, /* @tp_iternext@ */ 0, /* @tp_methods@ */ 0, /* @tp_members@ */ keyszany_pygetset, /* @tp_getset@ */ @@ -327,8 +334,8 @@ static PyTypeObject keyszany_pytype_skel = { }; static PyTypeObject keyszrange_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.KeySZRange", /* @tp_name@ */ + PyObject_HEAD_INIT(0) 0, /* Header */ + "KeySZRange", /* @tp_name@ */ sizeof(keyszrange_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -351,15 +358,16 @@ static PyTypeObject keyszrange_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Key size constraints. This object asserts minimum and maximum (if\n\ -sizes, and requires the key length to be a multiple of some value.", +"KeySZRange(DEFAULT, [min = 0], [max = 0], [mod = 1])\n\ + Key size constraints. Key size must be between MIN and MAX inclusive,\n\ + and be a multiple of MOD.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ + 0, /* @tp_iternext@ */ 0, /* @tp_methods@ */ keyszrange_pymembers, /* @tp_members@ */ 0, /* @tp_getset@ */ @@ -376,8 +384,8 @@ sizes, and requires the key length to be a multiple of some value.", }; static PyTypeObject keyszset_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.KeySZSet", /* @tp_name@ */ + PyObject_HEAD_INIT(0) 0, /* Header */ + "KeySZSet", /* @tp_name@ */ sizeof(keyszset_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -400,15 +408,15 @@ static PyTypeObject keyszset_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Key size constraints. This object requires the key to be one of a\n\ -few listed sizes.", +"KeySZSet(DEFAULT, SEQ)\n\ + Key size constraints. Key size must be DEFAULT or one in SEQ.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ + 0, /* @tp_iternext@ */ 0, /* @tp_methods@ */ keyszset_pymembers, /* @tp_members@ */ keyszset_pygetset, /* @tp_getset@ */ @@ -424,6 +432,24 @@ few listed sizes.", 0 /* @tp_is_gc@ */ }; +#define KSZCONVOP(op) \ + static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \ + { \ + double x, y; \ + if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \ + y = keysz_##op(x); \ + return (PyFloat_FromDouble(y)); \ + } +KSZCONVOP(fromdl) +KSZCONVOP(fromschnorr) +KSZCONVOP(fromif) +KSZCONVOP(fromec) +KSZCONVOP(todl) +KSZCONVOP(toschnorr) +KSZCONVOP(toif) +KSZCONVOP(toec) +#undef KSZCONVOP + /*----- Symmetric encryption ----------------------------------------------*/ PyTypeObject *gccipher_pytype, *gcipher_pytype; @@ -439,14 +465,14 @@ PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c, unsigned f) g = PyObject_NEW(gcipher_pyobj, (PyTypeObject *)cobj); g->c = c; g->f = f; - return ((PyObject *)g); + return ((PyObject *)g); } static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) { char *kwlist[] = { "k", 0 }; char *k; - int sz; + Py_ssize_t sz; if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) goto end; @@ -455,24 +481,23 @@ static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) GC_INIT(GCCIPHER_CC(ty), k, sz), f_freeme)); end: - return (0); + return (0); } PyObject *gccipher_pywrap(gccipher *cc) { - gccipher_pyobj *g = newtype(gccipher_pytype, 0); + gccipher_pyobj *g = newtype(gccipher_pytype, 0, cc->name); g->cc = cc; - g->ty.tp_name = (/*unconst*/ char *)cc->name; - g->ty.tp_basicsize = sizeof(gcipher_pyobj); - g->ty.tp_base = gcipher_pytype; + g->ty.ht_type.tp_basicsize = sizeof(gcipher_pyobj); + g->ty.ht_type.tp_base = gcipher_pytype; Py_INCREF(gcipher_pytype); - g->ty.tp_flags = (Py_TPFLAGS_DEFAULT | - Py_TPFLAGS_BASETYPE | - Py_TPFLAGS_HEAPTYPE); - g->ty.tp_alloc = PyType_GenericAlloc; - g->ty.tp_free = 0; - g->ty.tp_new = gcipher_pynew; - PyType_Ready(&g->ty); + g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | + Py_TPFLAGS_BASETYPE | + Py_TPFLAGS_HEAPTYPE); + g->ty.ht_type.tp_alloc = PyType_GenericAlloc; + g->ty.ht_type.tp_free = 0; + g->ty.ht_type.tp_new = gcipher_pynew; + typeready(&g->ty.ht_type); return ((PyObject *)g); } @@ -496,7 +521,7 @@ static PyObject *gccget_blksz(PyObject *me, void *hunoz) static PyObject *gcmeth_encrypt(PyObject *me, PyObject *arg) { char *p; - int sz; + Py_ssize_t sz; PyObject *rc = 0; if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &sz)) return (0); @@ -522,7 +547,7 @@ static PyObject *gcmeth_enczero(PyObject *me, PyObject *arg) static PyObject *gcmeth_decrypt(PyObject *me, PyObject *arg) { char *p; - int sz; + Py_ssize_t sz; PyObject *rc = 0; if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &sz)) return (0); @@ -548,9 +573,10 @@ static PyObject *gcmeth_deczero(PyObject *me, PyObject *arg) static PyObject *gcmeth_setiv(PyObject *me, PyObject *arg) { char *p; - int sz; + Py_ssize_t sz; if (!PyArg_ParseTuple(arg, "s#:setiv", &p, &sz)) goto end; + if (!GCIPHER_C(me)->ops->setiv) VALERR("`setiv' not supported"); if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode"); if (sz != GC_CLASS(GCIPHER_C(me))->blksz) VALERR("bad IV length"); GC_SETIV(GCIPHER_C(me), p); @@ -562,6 +588,7 @@ end: static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg) { if (!PyArg_ParseTuple(arg, ":bdry")) goto end; + if (!GCIPHER_C(me)->ops->bdry) VALERR("`bdry' not supported"); if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode"); GC_BDRY(GCIPHER_C(me)); RETURN_ME; @@ -591,8 +618,8 @@ static PyMethodDef gcipher_pymethods[] = { }; static PyTypeObject gccipher_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.GCCipher", /* @tp_name@ */ + PyObject_HEAD_INIT(0) 0, /* Header */ + "GCCipher", /* @tp_name@ */ sizeof(gccipher_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -622,7 +649,7 @@ static PyTypeObject gccipher_pytype_skel = { 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ + 0, /* @tp_iternext@ */ 0, /* @tp_methods@ */ 0, /* @tp_members@ */ gccipher_pygetset, /* @tp_getset@ */ @@ -639,8 +666,8 @@ static PyTypeObject gccipher_pytype_skel = { }; static PyTypeObject gcipher_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.GCipher", /* @tp_name@ */ + PyObject_HEAD_INIT(0) 0, /* Header */ + "GCipher", /* @tp_name@ */ sizeof(gcipher_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -670,7 +697,7 @@ static PyTypeObject gcipher_pytype_skel = { 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ + 0, /* @tp_iternext@ */ gcipher_pymethods, /* @tp_methods@ */ 0, /* @tp_members@ */ 0, /* @tp_getset@ */ @@ -700,24 +727,23 @@ static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) goto end; return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty)), f_freeme)); end: - return (0); + return (0); } PyObject *gchash_pywrap(gchash *ch) { - gchash_pyobj *g = newtype(gchash_pytype, 0); + gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name); g->ch = ch; - g->ty.tp_name = (/*unconst*/ char *)ch->name; - g->ty.tp_basicsize = sizeof(ghash_pyobj); - g->ty.tp_base = ghash_pytype; + g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj); + g->ty.ht_type.tp_base = ghash_pytype; Py_INCREF(ghash_pytype); - g->ty.tp_flags = (Py_TPFLAGS_DEFAULT | - Py_TPFLAGS_BASETYPE | - Py_TPFLAGS_HEAPTYPE); - g->ty.tp_alloc = PyType_GenericAlloc; - g->ty.tp_free = 0; - g->ty.tp_new = ghash_pynew; - PyType_Ready(&g->ty); + g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | + Py_TPFLAGS_BASETYPE | + Py_TPFLAGS_HEAPTYPE); + g->ty.ht_type.tp_alloc = PyType_GenericAlloc; + g->ty.ht_type.tp_free = 0; + g->ty.ht_type.tp_new = ghash_pynew; + typeready(&g->ty.ht_type); return ((PyObject *)g); } @@ -729,7 +755,7 @@ PyObject *ghash_pywrap(PyObject *cobj, ghash *h, unsigned f) g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj); g->h = h; g->f = f; - return ((PyObject *)g); + return ((PyObject *)g); } static void ghash_pydealloc(PyObject *me) @@ -752,12 +778,46 @@ static PyObject *gchget_bufsz(PyObject *me, void *hunoz) static PyObject *ghmeth_hash(PyObject *me, PyObject *arg) { char *p; - int sz; + Py_ssize_t sz; if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0); GH_HASH(GHASH_H(me), p, sz); RETURN_ME; } +#define GHMETH_HASHU_(n, W, w) \ + static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \ + { \ + uint##n x; \ + if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \ + GH_HASHU##W(GHASH_H(me), x); \ + RETURN_ME; \ + end: \ + return (0); \ + } +DOUINTCONV(GHMETH_HASHU_) + +#define GHMETH_HASHBUF_(n, W, w) \ + static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \ + { \ + char *p; \ + Py_ssize_t sz; \ + if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \ + if (sz > MASK##n) TYERR("string too long"); \ + GH_HASHBUF##W(GHASH_H(me), p, sz); \ + RETURN_ME; \ + end: \ + return (0); \ + } +DOUINTCONV(GHMETH_HASHBUF_) + +static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg) +{ + char *p; + if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); + GH_HASHSTRZ(GHASH_H(me), p); + RETURN_ME; +} + static PyObject *ghmeth_done(PyObject *me, PyObject *arg) { ghash *g; @@ -773,7 +833,7 @@ static PyObject *ghmeth_done(PyObject *me, PyObject *arg) static PyGetSetDef gchash_pygetset[] = { #define GETSETNAME(op, name) gch##op##_##name GET (bufsz, "CH.bufsz -> hash buffer size, or zero") - GET (hashsz, "CH.blksz -> hash output size") + GET (hashsz, "CH.hashsz -> hash output size") GET (name, "CH.name -> name of this kind of hash") #undef GETSETNAME { 0 } @@ -782,14 +842,21 @@ static PyGetSetDef gchash_pygetset[] = { static PyMethodDef ghash_pymethods[] = { #define METHNAME(name) ghmeth_##name METH (hash, "H.hash(M)") +#define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)") + DOUINTCONV(METHU_) +#undef METHU_ +#define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)") + DOUINTCONV(METHBUF_) +#undef METHBUF_ + METH (hashstrz, "H.hashstrz(STRING)") METH (done, "H.done() -> HASH") #undef METHNAME { 0 } }; static PyTypeObject gchash_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.GCHash", /* @tp_name@ */ + PyObject_HEAD_INIT(0) 0, /* Header */ + "GCHash", /* @tp_name@ */ sizeof(gchash_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -819,7 +886,7 @@ static PyTypeObject gchash_pytype_skel = { 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ + 0, /* @tp_iternext@ */ 0, /* @tp_methods@ */ 0, /* @tp_members@ */ gchash_pygetset, /* @tp_getset@ */ @@ -836,8 +903,8 @@ static PyTypeObject gchash_pytype_skel = { }; static PyTypeObject ghash_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.GHash", /* @tp_name@ */ + PyObject_HEAD_INIT(0) 0, /* Header */ + "GHash", /* @tp_name@ */ sizeof(ghash_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -867,7 +934,7 @@ static PyTypeObject ghash_pytype_skel = { 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ + 0, /* @tp_iternext@ */ ghash_pymethods, /* @tp_methods@ */ 0, /* @tp_members@ */ 0, /* @tp_getset@ */ @@ -895,7 +962,7 @@ static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) { char *kwlist[] = { "k", 0 }; char *k; - int sz; + Py_ssize_t sz; if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) goto end; @@ -904,7 +971,7 @@ static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) GM_KEY(GCMAC_CM(ty), k, sz), f_freeme)); end: - return (0); + return (0); } static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) @@ -922,19 +989,18 @@ static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) PyObject *gcmac_pywrap(gcmac *cm) { - gcmac_pyobj *g = newtype(gcmac_pytype, 0); + gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name); g->cm = cm; - g->ty.tp_name = (/*unconst*/ char *)cm->name; - g->ty.tp_basicsize = sizeof(gmac_pyobj); - g->ty.tp_base = gmac_pytype; + g->ty.ht_type.tp_basicsize = sizeof(gmac_pyobj); + g->ty.ht_type.tp_base = gmac_pytype; Py_INCREF(gmac_pytype); - g->ty.tp_flags = (Py_TPFLAGS_DEFAULT | - Py_TPFLAGS_BASETYPE | - Py_TPFLAGS_HEAPTYPE); - g->ty.tp_alloc = PyType_GenericAlloc; - g->ty.tp_free = 0; - g->ty.tp_new = gmac_pynew; - PyType_Ready(&g->ty); + g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | + Py_TPFLAGS_BASETYPE | + Py_TPFLAGS_HEAPTYPE); + g->ty.ht_type.tp_alloc = PyType_GenericAlloc; + g->ty.ht_type.tp_free = 0; + g->ty.ht_type.tp_new = gmac_pynew; + typeready(&g->ty.ht_type); return ((PyObject *)g); } @@ -943,21 +1009,22 @@ PyObject *gmac_pywrap(PyObject *cobj, gmac *m, unsigned f) gmac_pyobj *g; if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m)); else Py_INCREF(cobj); - g = newtype((PyTypeObject *)cobj, 0); - g->nameobj = PyString_FromFormat("%s(keyed)", m->ops->c->name); - g->ty.tp_name = PyString_AS_STRING(g->nameobj); - g->ty.tp_base = gmhash_pytype; + g = newtype((PyTypeObject *)cobj, 0, 0); + g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj); + g->ty.ht_name = PyString_FromFormat("%s(keyed)", m->ops->c->name); + g->ty.ht_type.tp_name = PyString_AS_STRING(g->ty.ht_name); + g->ty.ht_type.tp_base = gmhash_pytype; Py_INCREF(gmac_pytype); - g->ty.tp_flags = (Py_TPFLAGS_DEFAULT | - Py_TPFLAGS_BASETYPE | - Py_TPFLAGS_HEAPTYPE); - g->ty.tp_alloc = PyType_GenericAlloc; - g->ty.tp_free = 0; - g->ty.tp_new = gmhash_pynew; - PyType_Ready(&g->ty); + g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | + Py_TPFLAGS_BASETYPE | + Py_TPFLAGS_HEAPTYPE); + g->ty.ht_type.tp_alloc = PyType_GenericAlloc; + g->ty.ht_type.tp_free = 0; + g->ty.ht_type.tp_new = gmhash_pynew; + typeready(&g->ty.ht_type); g->m = m; g->f = f; - return ((PyObject *)g); + return ((PyObject *)g); } static void gmac_pydealloc(PyObject *me) @@ -965,7 +1032,6 @@ static void gmac_pydealloc(PyObject *me) if (GMAC_F(me) & f_freeme) GM_DESTROY(GMAC_M(me)); Py_DECREF(me->ob_type); - Py_DECREF(GMAC_NAMEOBJ(me)); PyType_Type.tp_dealloc(me); } @@ -988,8 +1054,8 @@ static PyGetSetDef gcmac_pygetset[] = { }; static PyTypeObject gcmac_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.GCMAC", /* @tp_name@ */ + PyObject_HEAD_INIT(0) 0, /* Header */ + "GCMAC", /* @tp_name@ */ sizeof(gchash_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -1019,7 +1085,7 @@ static PyTypeObject gcmac_pytype_skel = { 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ + 0, /* @tp_iternext@ */ 0, /* @tp_methods@ */ 0, /* @tp_members@ */ gcmac_pygetset, /* @tp_getset@ */ @@ -1036,8 +1102,8 @@ static PyTypeObject gcmac_pytype_skel = { }; static PyTypeObject gmac_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.GMAC", /* @tp_name@ */ + PyObject_HEAD_INIT(0) 0, /* Header */ + "GMAC", /* @tp_name@ */ sizeof(gmac_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -1067,7 +1133,7 @@ static PyTypeObject gmac_pytype_skel = { 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ + 0, /* @tp_iternext@ */ 0, /* @tp_methods@ */ 0, /* @tp_members@ */ 0, /* @tp_getset@ */ @@ -1084,8 +1150,8 @@ static PyTypeObject gmac_pytype_skel = { }; static PyTypeObject gmhash_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.GMACHash", /* @tp_name@ */ + PyObject_HEAD_INIT(0) 0, /* Header */ + "GMACHash", /* @tp_name@ */ sizeof(ghash_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -1115,7 +1181,7 @@ static PyTypeObject gmhash_pytype_skel = { 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ + 0, /* @tp_iternext@ */ 0, /* @tp_methods@ */ 0, /* @tp_members@ */ 0, /* @tp_getset@ */ @@ -1131,42 +1197,1208 @@ static PyTypeObject gmhash_pytype_skel = { 0 /* @tp_is_gc@ */ }; -/*----- Main code ---------------------------------------------------------*/ +/*----- Special snowflake for Poly1305 ------------------------------------*/ -void algorithms_pyinit(void) +PyTypeObject *poly1305cls_pytype, *poly1305key_pytype, *poly1305hash_pytype; + +typedef struct poly1305key_pyobj { + PyHeapTypeObject ty; + poly1305_key k; +} poly1305key_pyobj; + +typedef struct poly1305hash_pyobj { + PyObject_HEAD + unsigned f; +#define f_mask 1u + poly1305_ctx ctx; +} poly1305hash_pyobj; + +#define P1305_F(o) (((poly1305hash_pyobj *)(o))->f) +#define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx) +CONVFUNC(poly1305hash, poly1305_ctx *, P1305_CTX) + +static PyObject *poly1305hash_pynew(PyTypeObject *ty, + PyObject *arg, PyObject *kw) { - INITTYPE(keysz, root); - INITTYPE(keyszany, keysz); - INITTYPE(keyszrange, keysz); - INITTYPE(keyszset, keysz); - INITTYPE(gccipher, type); - INITTYPE(gcipher, root); - INITTYPE(gchash, type); - INITTYPE(ghash, root); - INITTYPE(gcmac, type); - INITTYPE(gmac, type); - INITTYPE(gmhash, ghash); + char *kwlist[] = { "mask", 0 }; + poly1305key_pyobj *pk = (poly1305key_pyobj *)ty; + poly1305hash_pyobj *ph; + char *m = 0; + Py_ssize_t sz; + + if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#:new", kwlist, &m, &sz)) + return (0); + if (m && sz != POLY1305_MASKSZ) VALERR("bad mask length"); + ph = PyObject_NEW(poly1305hash_pyobj, ty); + ph->f = 0; + if (m) ph->f |= f_mask; + poly1305_macinit(&ph->ctx, &pk->k, m); + Py_INCREF(ty); + return ((PyObject *)ph); +end: + return (0); +} + +static PyObject *poly1305key_pynew(PyTypeObject *ty, + PyObject *arg, PyObject *kw) +{ + char *kwlist[] = { "k", 0 }; + poly1305key_pyobj *pk; + char *k; + Py_ssize_t sz; + + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) + goto end; + if (keysz(sz, poly1305_keysz) != sz) VALERR("bad key length"); + + pk = newtype(ty, 0, 0); + pk->ty.ht_name = PyString_FromString("poly1305(keyed)"); + pk->ty.ht_type.tp_basicsize = sizeof(poly1305hash_pyobj); + pk->ty.ht_type.tp_name = PyString_AS_STRING(pk->ty.ht_name); + pk->ty.ht_type.tp_base = poly1305hash_pytype; + Py_INCREF(poly1305key_pytype); + pk->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | + Py_TPFLAGS_BASETYPE | + Py_TPFLAGS_HEAPTYPE); + pk->ty.ht_type.tp_alloc = PyType_GenericAlloc; + pk->ty.ht_type.tp_free = 0; + pk->ty.ht_type.tp_new = poly1305hash_pynew; + typeready(&pk->ty.ht_type); + + poly1305_keyinit(&pk->k, k, sz); + return ((PyObject *)pk); + +end: + return (0); +} + +static PyObject *poly1305clsget_name(PyObject *me, void *hunoz) + { return (PyString_FromString("poly1305")); } + +static PyObject *poly1305clsget_keysz(PyObject *me, void *hunoz) + { return (keysz_pywrap(poly1305_keysz)); } + +static PyObject *poly1305clsget_masksz(PyObject *me, void *hunoz) + { return (PyInt_FromLong(POLY1305_MASKSZ)); } + +static PyObject *poly1305clsget_tagsz(PyObject *me, void *hunoz) + { return (PyInt_FromLong(POLY1305_TAGSZ)); } + +static PyObject *polymeth_copy(PyObject *me, PyObject *arg) +{ + poly1305hash_pyobj *ph; + if (!PyArg_ParseTuple(arg, ":copy")) return (0); + ph = PyObject_NEW(poly1305hash_pyobj, me->ob_type); + poly1305_copy(&ph->ctx, P1305_CTX(me)); + Py_INCREF(me->ob_type); + return ((PyObject *)ph); +} + +static PyObject *polymeth_hash(PyObject *me, PyObject *arg) +{ + char *p; + Py_ssize_t sz; + if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0); + poly1305_hash(P1305_CTX(me), p, sz); + RETURN_ME; +} + +#define POLYMETH_HASHU_(n, W, w) \ + static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \ + { \ + uint##n x; \ + octet b[SZ_##W]; \ + if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \ + STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \ + RETURN_ME; \ + end: \ + return (0); \ + } +DOUINTCONV(POLYMETH_HASHU_) + +#define POLYMETH_HASHBUF_(n, W, w) \ + static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \ + { \ + char *p; \ + Py_ssize_t sz; \ + octet b[SZ_##W]; \ + if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \ + if (sz > MASK##n) TYERR("string too long"); \ + STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \ + poly1305_hash(P1305_CTX(me), p, sz); \ + RETURN_ME; \ + end: \ + return (0); \ + } +DOUINTCONV(POLYMETH_HASHBUF_) + +static PyObject *polymeth_hashstrz(PyObject *me, PyObject *arg) +{ + char *p; + if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); + poly1305_hash(P1305_CTX(me), p, strlen(p) + 1); + RETURN_ME; +} + +static PyObject *polymeth_flush(PyObject *me, PyObject *arg) +{ + if (!PyArg_ParseTuple(arg, ":flush")) return (0); + poly1305_flush(P1305_CTX(me)); + RETURN_ME; +} + +static PyObject *polymeth_flushzero(PyObject *me, PyObject *arg) +{ + if (!PyArg_ParseTuple(arg, ":flushzero")) return (0); + poly1305_flushzero(P1305_CTX(me)); + RETURN_ME; +} + +static PyObject *polymeth_concat(PyObject *me, PyObject *arg) +{ + PyObject *pre, *suff; + if (!PyArg_ParseTuple(arg, "OO:concat", &pre, &suff)) return (0); + if (!PyObject_TypeCheck(pre, poly1305hash_pytype) || + !PyObject_TypeCheck(suff, poly1305hash_pytype)) + TYERR("wanted a poly1305hash"); + if (me->ob_type != pre->ob_type || me->ob_type != suff->ob_type) + TYERR("key mismatch"); + if (P1305_CTX(pre)->nbuf) VALERR("prefix is not block-aligned"); + poly1305_concat(P1305_CTX(me), P1305_CTX(pre), P1305_CTX(suff)); + RETURN_ME; +end: + return (0); +} + +static PyObject *polymeth_done(PyObject *me, PyObject *arg) +{ + PyObject *rc; + if (!PyArg_ParseTuple(arg, ":done")) return (0); + if (!(P1305_F(me) & f_mask)) VALERR("no mask"); + rc = bytestring_pywrap(0, POLY1305_TAGSZ); + poly1305_done(P1305_CTX(me), PyString_AS_STRING(rc)); + return (rc); +end: + return (0); } -#define GEN(func, base) \ - static PyObject *func(void) \ +static PyGetSetDef poly1305cls_pygetset[] = { +#define GETSETNAME(op, name) poly1305cls##op##_##name + GET (keysz, "PC.keysz -> acceptable key sizes") + GET (masksz, "PC.masksz -> mask size") + GET (tagsz, "PC.tagsz -> MAC output size") + GET (name, "PC.name -> name of this kind of MAC") +#undef GETSETNAME + { 0 } +}; + +static PyMethodDef poly1305hash_pymethods[] = { +#define METHNAME(name) polymeth_##name + METH (copy, "P.copy() -> PP") + METH (hash, "P.hash(M)") +#define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)") + DOUINTCONV(METHU_) +#undef METHU_ +#define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)") + DOUINTCONV(METHBUF_) +#undef METHBUF_ + METH (hashstrz, "P.hashstrz(STRING)") + METH (flush, "P.flush()") + METH (flushzero, "P.flushzero()") + METH (concat, "P.concat(PREFIX, SUFFIX)") + METH (done, "P.done() -> TAG") +#undef METHNAME + { 0 } +}; + +static PyTypeObject poly1305cls_pytype_skel = { + PyObject_HEAD_INIT(0) 0, /* Header */ + "Poly1305Class", /* @tp_name@ */ + sizeof(PyHeapTypeObject), /* @tp_basicsize@ */ + 0, /* @tp_itemsize@ */ + + 0, /* @tp_dealloc@ */ + 0, /* @tp_print@ */ + 0, /* @tp_getattr@ */ + 0, /* @tp_setattr@ */ + 0, /* @tp_compare@ */ + 0, /* @tp_repr@ */ + 0, /* @tp_as_number@ */ + 0, /* @tp_as_sequence@ */ + 0, /* @tp_as_mapping@ */ + 0, /* @tp_hash@ */ + 0, /* @tp_call@ */ + 0, /* @tp_str@ */ + 0, /* @tp_getattro@ */ + 0, /* @tp_setattro@ */ + 0, /* @tp_as_buffer@ */ + Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ + Py_TPFLAGS_BASETYPE, + + /* @tp_doc@ */ +"Poly1305 metametaclass. Best not to ask.", + + 0, /* @tp_traverse@ */ + 0, /* @tp_clear@ */ + 0, /* @tp_richcompare@ */ + 0, /* @tp_weaklistoffset@ */ + 0, /* @tp_iter@ */ + 0, /* @tp_iternext@ */ + 0, /* @tp_methods@ */ + 0, /* @tp_members@ */ + poly1305cls_pygetset, /* @tp_getset@ */ + 0, /* @tp_base@ */ + 0, /* @tp_dict@ */ + 0, /* @tp_descr_get@ */ + 0, /* @tp_descr_set@ */ + 0, /* @tp_dictoffset@ */ + 0, /* @tp_init@ */ + PyType_GenericAlloc, /* @tp_alloc@ */ + abstract_pynew, /* @tp_new@ */ + 0, /* @tp_free@ */ + 0 /* @tp_is_gc@ */ +}; + +static PyTypeObject poly1305key_pytype_skel = { + PyObject_HEAD_INIT(0) 0, /* Header */ + "poly1305", /* @tp_name@ */ + sizeof(poly1305key_pyobj), /* @tp_basicsize@ */ + 0, /* @tp_itemsize@ */ + + 0, /* @tp_dealloc@ */ + 0, /* @tp_print@ */ + 0, /* @tp_getattr@ */ + 0, /* @tp_setattr@ */ + 0, /* @tp_compare@ */ + 0, /* @tp_repr@ */ + 0, /* @tp_as_number@ */ + 0, /* @tp_as_sequence@ */ + 0, /* @tp_as_mapping@ */ + 0, /* @tp_hash@ */ + 0, /* @tp_call@ */ + 0, /* @tp_str@ */ + 0, /* @tp_getattro@ */ + 0, /* @tp_setattro@ */ + 0, /* @tp_as_buffer@ */ + Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ + Py_TPFLAGS_BASETYPE, + + /* @tp_doc@ */ +"poly1305(K): Poly1305 key.", + + 0, /* @tp_traverse@ */ + 0, /* @tp_clear@ */ + 0, /* @tp_richcompare@ */ + 0, /* @tp_weaklistoffset@ */ + 0, /* @tp_iter@ */ + 0, /* @tp_iternext@ */ + 0, /* @tp_methods@ */ + 0, /* @tp_members@ */ + 0, /* @tp_getset@ */ + 0, /* @tp_base@ */ + 0, /* @tp_dict@ */ + 0, /* @tp_descr_get@ */ + 0, /* @tp_descr_set@ */ + 0, /* @tp_dictoffset@ */ + 0, /* @tp_init@ */ + PyType_GenericAlloc, /* @tp_alloc@ */ + poly1305key_pynew, /* @tp_new@ */ + 0, /* @tp_free@ */ + 0 /* @tp_is_gc@ */ +}; + +static PyTypeObject poly1305hash_pytype_skel = { + PyObject_HEAD_INIT(0) 0, /* Header */ + "Poly1305Hash", /* @tp_name@ */ + sizeof(poly1305hash_pyobj), /* @tp_basicsize@ */ + 0, /* @tp_itemsize@ */ + + 0, /* @tp_dealloc@ */ + 0, /* @tp_print@ */ + 0, /* @tp_getattr@ */ + 0, /* @tp_setattr@ */ + 0, /* @tp_compare@ */ + 0, /* @tp_repr@ */ + 0, /* @tp_as_number@ */ + 0, /* @tp_as_sequence@ */ + 0, /* @tp_as_mapping@ */ + 0, /* @tp_hash@ */ + 0, /* @tp_call@ */ + 0, /* @tp_str@ */ + 0, /* @tp_getattro@ */ + 0, /* @tp_setattro@ */ + 0, /* @tp_as_buffer@ */ + Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ + Py_TPFLAGS_BASETYPE, + + /* @tp_doc@ */ +"Poly1305 MAC context base class.", + + 0, /* @tp_traverse@ */ + 0, /* @tp_clear@ */ + 0, /* @tp_richcompare@ */ + 0, /* @tp_weaklistoffset@ */ + 0, /* @tp_iter@ */ + 0, /* @tp_iternext@ */ + poly1305hash_pymethods, /* @tp_methods@ */ + 0, /* @tp_members@ */ + 0, /* @tp_getset@ */ + 0, /* @tp_base@ */ + 0, /* @tp_dict@ */ + 0, /* @tp_descr_get@ */ + 0, /* @tp_descr_set@ */ + 0, /* @tp_dictoffset@ */ + 0, /* @tp_init@ */ + PyType_GenericAlloc, /* @tp_alloc@ */ + abstract_pynew, /* @tp_new@ */ + 0, /* @tp_free@ */ + 0 /* @tp_is_gc@ */ +}; + +/*----- Special snowflake for HSalsa and HChaCha --------------------------*/ + +#define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \ + static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \ { \ - PyObject *d = PyDict_New(); \ - PyObject *o; \ - int i; \ - \ - for (i = 0; g##base##tab[i]; i++) { \ - o = gc##base##_pywrap((/*unconst*/ gc##base *)g##base##tab[i]); \ - PyDict_SetItemString(d, \ - (/*unconst*/ char *)g##base##tab[i]->name, \ - o); \ - Py_DECREF(o); \ - } \ - return (d); \ + dance##_ctx dance; \ + char *k, *n; \ + Py_ssize_t ksz, nsz; \ + PyObject *rc; \ + if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf", \ + &k, &ksz, &n, &nsz)) \ + goto end; \ + if (ksz != DANCE##_KEYSZ) VALERR("bad key length"); \ + if (nsz != HDANCE##_INSZ) VALERR("bad input length"); \ + rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \ + dance##_init(&dance, k, ksz, 0); \ + hdance##_prf(&dance, n, PyString_AS_STRING(rc)); \ + return (rc); \ + end: \ + return (0); \ } + +DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa20) +DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa2012) +DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa208) + +DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha20) +DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha12) +DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha8) + +/*----- Keccak-p[1600, n] -------------------------------------------------*/ + +static PyTypeObject *kxvik_pytype; + +typedef struct kxvik_pyobj { + PyObject_HEAD + keccak1600_state s; + unsigned n; +} kxvik_pyobj; + +static PyObject *kxvik_pynew(PyTypeObject *ty, + PyObject *arg, PyObject *kw) +{ + unsigned n = 24; + kxvik_pyobj *rc = 0; + char *kwlist[] = { "nround", 0 }; + if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", kwlist, + convuint, &n)) + goto end; + rc = (kxvik_pyobj *)ty->tp_alloc(ty, 0); + rc->n = n; + keccak1600_init(&rc->s); +end: + return ((PyObject *)rc); +} + +static PyObject *kxvikmeth_mix(PyObject *me, PyObject *arg) +{ + kxvik_pyobj *k = (kxvik_pyobj *)me; + kludge64 t[25]; + const octet *q; + octet buf[8]; + unsigned i; + char *p; Py_ssize_t n; + + if (!PyArg_ParseTuple(arg, "s#:mix", &p, &n)) goto end; + if (n > 200) VALERR("out of range"); + q = (const octet *)p; + i = 0; + while (n > 8) { LOAD64_L_(t[i], q); i++; q += 8; n -= 8; } + if (n) { + memcpy(buf, q, n); memset(buf + n, 0, 8 - n); + LOAD64_L_(t[i], buf); i++; + } + keccak1600_mix(&k->s, t, i); + RETURN_ME; +end: + return (0); +} + +static PyObject *kxvikmeth_extract(PyObject *me, PyObject *arg) +{ + kxvik_pyobj *k = (kxvik_pyobj *)me; + PyObject *rc = 0; + kludge64 t[25]; + octet *q, buf[8]; + unsigned i; + unsigned n; + + if (!PyArg_ParseTuple(arg, "O&:mix", convuint, &n)) goto end; + if (n > 200) VALERR("out of range"); + rc = bytestring_pywrap(0, n); + q = (octet *)PyString_AS_STRING(rc); + keccak1600_extract(&k->s, t, (n + 7)/8); + i = 0; + while (n > 8) { STORE64_L_(q, t[i]); i++; q += 8; n -= 8; } + if (n) { STORE64_L_(buf, t[i]); memcpy(q, buf, n); } +end: + return (rc); +} + +static PyObject *kxvikmeth_step(PyObject *me, PyObject *arg) +{ + kxvik_pyobj *k = (kxvik_pyobj *)me; + if (!PyArg_ParseTuple(arg, ":step")) return (0); + keccak1600_p(&k->s, &k->s, k->n); + RETURN_ME; +} + +static PyObject *kxvikget_nround(PyObject *me, void *hunoz) +{ + kxvik_pyobj *k = (kxvik_pyobj *)me; + return (PyInt_FromLong(k->n)); +} + +static int kxvikset_nround(PyObject *me, PyObject *val, void *hunoz) +{ + kxvik_pyobj *k = (kxvik_pyobj *)me; + unsigned n; + + if (!convuint(val, &n)) return (-1); + k->n = n; + return (0); +} + +static PyGetSetDef kxvik_pygetset[] = { +#define GETSETNAME(op, name) kxvik##op##_##name + GETSET(nround, "KECCAK.nround -> number of rounds") +#undef GETSETNAME + { 0 } +}; + +static PyMethodDef kxvik_pymethods[] = { +#define METHNAME(func) kxvikmeth_##func + METH (mix, "KECCAK.mix(DATA)") + METH (extract, "KECCAK.extract(NOCTETS)") + METH (step, "KECCAK.step()") +#undef METHNAME + { 0 } +}; + +static PyTypeObject kxvik_pytype_skel = { + PyObject_HEAD_INIT(0) 0, /* Header */ + "Keccak1600", /* @tp_name@ */ + sizeof(kxvik_pyobj), /* @tp_basicsize@ */ + 0, /* @tp_itemsize@ */ + + 0, /* @tp_dealloc@ */ + 0, /* @tp_print@ */ + 0, /* @tp_getattr@ */ + 0, /* @tp_setattr@ */ + 0, /* @tp_compare@ */ + 0, /* @tp_repr@ */ + 0, /* @tp_as_number@ */ + 0, /* @tp_as_sequence@ */ + 0, /* @tp_as_mapping@ */ + 0, /* @tp_hash@ */ + 0, /* @tp_call@ */ + 0, /* @tp_str@ */ + 0, /* @tp_getattro@ */ + 0, /* @tp_setattro@ */ + 0, /* @tp_as_buffer@ */ + Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ + Py_TPFLAGS_BASETYPE, + + /* @tp_doc@ */ +"Keccak1600([nround = 24]): Keccak-p[1600, n] state.", + + 0, /* @tp_traverse@ */ + 0, /* @tp_clear@ */ + 0, /* @tp_richcompare@ */ + 0, /* @tp_weaklistoffset@ */ + 0, /* @tp_iter@ */ + 0, /* @tp_iternext@ */ + kxvik_pymethods, /* @tp_methods@ */ + 0, /* @tp_members@ */ + kxvik_pygetset, /* @tp_getset@ */ + 0, /* @tp_base@ */ + 0, /* @tp_dict@ */ + 0, /* @tp_descr_get@ */ + 0, /* @tp_descr_set@ */ + 0, /* @tp_dictoffset@ */ + 0, /* @tp_init@ */ + PyType_GenericAlloc, /* @tp_alloc@ */ + kxvik_pynew, /* @tp_new@ */ + 0, /* @tp_free@ */ + 0 /* @tp_is_gc@ */ +}; + +static PyTypeObject *shake_pytype, *shake128_pytype, *shake256_pytype; + +typedef struct shake_pyobj { + PyObject_HEAD + int st; + shake_ctx h; +} shake_pyobj; + +#define SHAKE_H(o) (&((shake_pyobj *)(o))->h) +#define SHAKE_ST(o) (((shake_pyobj *)(o))->st) + +static PyObject *shake_dopynew(void (*initfn)(shake_ctx *, + const void *, size_t, + const void *, size_t), + PyTypeObject *ty, + PyObject *arg, PyObject *kw) +{ + shake_pyobj *rc = 0; + char *p = 0, *f = 0; + Py_ssize_t psz = 0, fsz = 0; + char *kwlist[] = { "perso", "func", 0 }; + + if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#s#:new", kwlist, + &p, &psz, &f, &fsz)) + goto end; + rc = (shake_pyobj *)ty->tp_alloc(ty, 0); + initfn(&rc->h, f, fsz, p, psz); + rc->st = 0; +end: + return ((PyObject *)rc); +} + +static PyObject *shake128_pynew(PyTypeObject *ty, + PyObject *arg, PyObject *kw) + { return (shake_dopynew(cshake128_init, ty, arg, kw)); } + +static PyObject *shake256_pynew(PyTypeObject *ty, + PyObject *arg, PyObject *kw) + { return (shake_dopynew(cshake256_init, ty, arg, kw)); } + +static int shake_check(PyObject *me, int st) +{ + if (SHAKE_ST(me) != st) VALERR("wrong state"); + return (0); +end: + return (-1); +} + +static PyObject *shakemeth_hash(PyObject *me, PyObject *arg) +{ + char *p; + Py_ssize_t sz; + if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0); + if (shake_check(me, 0)) return (0); + shake_hash(SHAKE_H(me), p, sz); + RETURN_ME; +} + +#define SHAKEMETH_HASHU_(n, W, w) \ + static PyObject *shakemeth_hashu##w(PyObject *me, PyObject *arg) \ + { \ + uint##n x; \ + octet b[SZ_##W]; \ + if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \ + if (shake_check(me, 0)) goto end; \ + STORE##W(b, x); shake_hash(SHAKE_H(me), b, sizeof(b)); \ + RETURN_ME; \ + end: \ + return (0); \ + } +DOUINTCONV(SHAKEMETH_HASHU_) + +#define SHAKEMETH_HASHBUF_(n, W, w) \ + static PyObject *shakemeth_hashbuf##w(PyObject *me, PyObject *arg) \ + { \ + char *p; \ + Py_ssize_t sz; \ + octet b[SZ_##W]; \ + if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \ + if (sz > MASK##n) TYERR("string too long"); \ + if (shake_check(me, 0)) goto end; \ + STORE##W(b, sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \ + shake_hash(SHAKE_H(me), p, sz); \ + RETURN_ME; \ + end: \ + return (0); \ + } +DOUINTCONV(SHAKEMETH_HASHBUF_) + +static PyObject *shakemeth_hashstrz(PyObject *me, PyObject *arg) +{ + char *p; + if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); + if (shake_check(me, 0)) return (0); + shake_hash(SHAKE_H(me), p, strlen(p) + 1); + RETURN_ME; +} + +static PyObject *shakemeth_xof(PyObject *me, PyObject *arg) +{ + if (!PyArg_ParseTuple(arg, ":xof")) goto end; + if (shake_check(me, 0)) goto end; + shake_xof(SHAKE_H(me)); + SHAKE_ST(me) = 1; + RETURN_ME; +end: + return (0); +} + +static PyObject *shakemeth_done(PyObject *me, PyObject *arg) +{ + PyObject *rc = 0; + size_t n; + if (!PyArg_ParseTuple(arg, "O&:done", convszt, &n)) goto end; + if (shake_check(me, 0)) goto end; + rc = bytestring_pywrap(0, n); + shake_done(SHAKE_H(me), PyString_AS_STRING(rc), n); + SHAKE_ST(me) = -1; +end: + return (rc); +} + +static PyObject *shakemeth_copy(PyObject *me, PyObject *arg) +{ + shake_pyobj *rc = 0; + + if (!PyArg_ParseTuple(arg, ":copy")) goto end; + rc = PyObject_NEW(shake_pyobj, me->ob_type); + rc->h = *SHAKE_H(me); + rc->st = SHAKE_ST(me); +end: + return ((PyObject *)me); +} + +static PyObject *shakemeth_get(PyObject *me, PyObject *arg) +{ + PyObject *rc = 0; + size_t sz; + + if (!PyArg_ParseTuple(arg, "O&:get", convszt, &sz)) goto end; + if (shake_check(me, 1)) goto end; + rc = bytestring_pywrap(0, sz); + shake_get(SHAKE_H(me), PyString_AS_STRING(rc), sz); +end: + return (rc); +} + +static PyObject *shakemeth_mask(PyObject *me, PyObject *arg) +{ + PyObject *rc = 0; + char *p; Py_ssize_t sz; + + if (!PyArg_ParseTuple(arg, "s#:mask", &p, &sz)) goto end; + if (shake_check(me, 1)) goto end; + rc = bytestring_pywrap(0, sz); + shake_mask(SHAKE_H(me), p, PyString_AS_STRING(rc), sz); +end: + return (rc); +} + +static PyObject *shakeget_rate(PyObject *me, void *hunoz) + { return (PyInt_FromLong(SHAKE_H(me)->h.r)); } + +static PyObject *shakeget_buffered(PyObject *me, void *hunoz) + { return (PyInt_FromLong(SHAKE_H(me)->h.n)); } + +static PyObject *shakeget_state(PyObject *me, void *hunoz) +{ + int st = SHAKE_ST(me); + return (PyString_FromString(st == 0 ? "absorb" : + st == 1 ? "squeeze" : "dead")); +} + +static PyGetSetDef shake_pygetset[] = { +#define GETSETNAME(op, name) shake##op##_##name + GET (rate, "S.rate -> rate, in bytes") + GET (buffered, "S.buffered -> amount currently buffered") + GET (state, "S.state -> `absorb', `squeeze', `dead'") +#undef GETSETNAME + { 0 } +}; + +static PyMethodDef shake_pymethods[] = { +#define METHNAME(func) shakemeth_##func + METH (copy, "S.copy() -> SS") + METH (hash, "S.hash(M)") +#define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)") + DOUINTCONV(METHU_) +#undef METHU_ +#define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)") + DOUINTCONV(METHBUF_) +#undef METHBUF_ + METH (hashstrz, "S.hashstrz(STRING)") + METH (xof, "S.xof()") + METH (done, "S.done(LEN) ->H") + METH (get, "S.get(LEN) -> H") + METH (mask, "S.mask(M) -> C") +#undef METHNAME + { 0 } +}; + +static PyTypeObject shake_pytype_skel = { + PyObject_HEAD_INIT(0) 0, /* Header */ + "Shake", /* @tp_name@ */ + sizeof(shake_pyobj), /* @tp_basicsize@ */ + 0, /* @tp_itemsize@ */ + + 0, /* @tp_dealloc@ */ + 0, /* @tp_print@ */ + 0, /* @tp_getattr@ */ + 0, /* @tp_setattr@ */ + 0, /* @tp_compare@ */ + 0, /* @tp_repr@ */ + 0, /* @tp_as_number@ */ + 0, /* @tp_as_sequence@ */ + 0, /* @tp_as_mapping@ */ + 0, /* @tp_hash@ */ + 0, /* @tp_call@ */ + 0, /* @tp_str@ */ + 0, /* @tp_getattro@ */ + 0, /* @tp_setattro@ */ + 0, /* @tp_as_buffer@ */ + Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ + Py_TPFLAGS_BASETYPE, + + /* @tp_doc@ */ +"SHAKE/cSHAKE base class.", + + 0, /* @tp_traverse@ */ + 0, /* @tp_clear@ */ + 0, /* @tp_richcompare@ */ + 0, /* @tp_weaklistoffset@ */ + 0, /* @tp_iter@ */ + 0, /* @tp_iternext@ */ + shake_pymethods, /* @tp_methods@ */ + 0, /* @tp_members@ */ + shake_pygetset, /* @tp_getset@ */ + 0, /* @tp_base@ */ + 0, /* @tp_dict@ */ + 0, /* @tp_descr_get@ */ + 0, /* @tp_descr_set@ */ + 0, /* @tp_dictoffset@ */ + 0, /* @tp_init@ */ + PyType_GenericAlloc, /* @tp_alloc@ */ + abstract_pynew, /* @tp_new@ */ + 0, /* @tp_free@ */ + 0 /* @tp_is_gc@ */ +}; + +static PyTypeObject shake128_pytype_skel = { + PyObject_HEAD_INIT(0) 0, /* Header */ + "Shake128", /* @tp_name@ */ + 0, /* @tp_basicsize@ */ + 0, /* @tp_itemsize@ */ + + 0, /* @tp_dealloc@ */ + 0, /* @tp_print@ */ + 0, /* @tp_getattr@ */ + 0, /* @tp_setattr@ */ + 0, /* @tp_compare@ */ + 0, /* @tp_repr@ */ + 0, /* @tp_as_number@ */ + 0, /* @tp_as_sequence@ */ + 0, /* @tp_as_mapping@ */ + 0, /* @tp_hash@ */ + 0, /* @tp_call@ */ + 0, /* @tp_str@ */ + 0, /* @tp_getattro@ */ + 0, /* @tp_setattro@ */ + 0, /* @tp_as_buffer@ */ + Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ + Py_TPFLAGS_BASETYPE, + + /* @tp_doc@ */ +"Shake128([perso = STR], [func = STR]): SHAKE128/cSHAKE128 XOF.", + + 0, /* @tp_traverse@ */ + 0, /* @tp_clear@ */ + 0, /* @tp_richcompare@ */ + 0, /* @tp_weaklistoffset@ */ + 0, /* @tp_iter@ */ + 0, /* @tp_iternext@ */ + 0, /* @tp_methods@ */ + 0, /* @tp_members@ */ + 0, /* @tp_getset@ */ + 0, /* @tp_base@ */ + 0, /* @tp_dict@ */ + 0, /* @tp_descr_get@ */ + 0, /* @tp_descr_set@ */ + 0, /* @tp_dictoffset@ */ + 0, /* @tp_init@ */ + PyType_GenericAlloc, /* @tp_alloc@ */ + shake128_pynew, /* @tp_new@ */ + 0, /* @tp_free@ */ + 0 /* @tp_is_gc@ */ +}; + +static PyTypeObject shake256_pytype_skel = { + PyObject_HEAD_INIT(0) 0, /* Header */ + "Shake256", /* @tp_name@ */ + 0, /* @tp_basicsize@ */ + 0, /* @tp_itemsize@ */ + + 0, /* @tp_dealloc@ */ + 0, /* @tp_print@ */ + 0, /* @tp_getattr@ */ + 0, /* @tp_setattr@ */ + 0, /* @tp_compare@ */ + 0, /* @tp_repr@ */ + 0, /* @tp_as_number@ */ + 0, /* @tp_as_sequence@ */ + 0, /* @tp_as_mapping@ */ + 0, /* @tp_hash@ */ + 0, /* @tp_call@ */ + 0, /* @tp_str@ */ + 0, /* @tp_getattro@ */ + 0, /* @tp_setattro@ */ + 0, /* @tp_as_buffer@ */ + Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ + Py_TPFLAGS_BASETYPE, + + /* @tp_doc@ */ +"Shake256([perso = STR], [func = STR]): SHAKE256/cSHAKE256 XOF.", + + 0, /* @tp_traverse@ */ + 0, /* @tp_clear@ */ + 0, /* @tp_richcompare@ */ + 0, /* @tp_weaklistoffset@ */ + 0, /* @tp_iter@ */ + 0, /* @tp_iternext@ */ + 0, /* @tp_methods@ */ + 0, /* @tp_members@ */ + 0, /* @tp_getset@ */ + 0, /* @tp_base@ */ + 0, /* @tp_dict@ */ + 0, /* @tp_descr_get@ */ + 0, /* @tp_descr_set@ */ + 0, /* @tp_dictoffset@ */ + 0, /* @tp_init@ */ + PyType_GenericAlloc, /* @tp_alloc@ */ + shake256_pynew, /* @tp_new@ */ + 0, /* @tp_free@ */ + 0 /* @tp_is_gc@ */ +}; + +/*----- Pseudorandom permutations -----------------------------------------*/ + +static PyTypeObject *gcprp_pytype, *gprp_pytype; + +typedef struct prpinfo { + const char *name; + const octet *keysz; + size_t ctxsz; + size_t blksz; + void (*init)(void *, const void *, size_t); + void (*eblk)(void *, const void *, void *); + void (*dblk)(void *, const void *, void *); +} prpinfo; + +#define PRP_DEF(PRE, pre) \ + static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \ + { pre##_init(ctx, k, ksz); } \ + static void pre##_prpeblk(void *ctx, const void *in, void *out) \ + { \ + uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \ + pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \ + } \ + static void pre##_prpdblk(void *ctx, const void *in, void *out) \ + { \ + uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \ + pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \ + } \ + static const prpinfo pre##_prpinfo = { \ + #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \ + pre##_prpinit, pre##_prpeblk, pre##_prpdblk \ + }; +PRPS(PRP_DEF) + +static const struct prpinfo *const gprptab[] = { +#define PRP_ENTRY(PRE, pre) &pre##_prpinfo, + PRPS(PRP_ENTRY) + 0 +}; + +typedef struct gcprp_pyobj { + PyHeapTypeObject ty; + const prpinfo *prp; +} gcprp_pyobj; +#define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp) + +typedef struct gprp_pyobj { + PyObject_HEAD + const prpinfo *prp; +} gprp_pyobj; +#define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp) +#define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1) + +typedef struct prp { + const prpinfo *prp; + void *ctx; +} prp; + +static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) +{ + char *kwlist[] = { "key", 0 }; + char *k; + Py_ssize_t sz; + const prpinfo *prp = GCPRP_PRP(ty); + PyObject *me; + + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) + goto end; + if (keysz(sz, prp->keysz) != sz) VALERR("bad key length"); + me = (PyObject *)ty->tp_alloc(ty, 0); + GPRP_PRP(me) = prp; + prp->init(GPRP_CTX(me), k, sz); + Py_INCREF(me); + return (me); +end: + return (0); +} + +static void gprp_pydealloc(PyObject *me) + { Py_DECREF(me->ob_type); FREEOBJ(me); } + +static PyObject *gcprp_pywrap(const prpinfo *prp) +{ + gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name); + g->prp = prp; + g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz; + g->ty.ht_type.tp_base = gprp_pytype; + Py_INCREF(gprp_pytype); + g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | + Py_TPFLAGS_BASETYPE | + Py_TPFLAGS_HEAPTYPE); + g->ty.ht_type.tp_alloc = PyType_GenericAlloc; + g->ty.ht_type.tp_free = 0; + g->ty.ht_type.tp_new = gprp_pynew; + typeready(&g->ty.ht_type); + return ((PyObject *)g); +} + +static PyObject *gcpget_name(PyObject *me, void *hunoz) + { return (PyString_FromString(GCPRP_PRP(me)->name)); } +static PyObject *gcpget_keysz(PyObject *me, void *hunoz) + { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); } +static PyObject *gcpget_blksz(PyObject *me, void *hunoz) + { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); } + +static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg) +{ + char *p; + Py_ssize_t n; + PyObject *rc = 0; + + if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &n)) goto end; + if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length"); + rc = bytestring_pywrap(0, n); + GPRP_PRP(me)->eblk(GPRP_CTX(me), p, PyString_AS_STRING(rc)); +end: + return (rc); +} + +static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg) +{ + char *p; + Py_ssize_t n; + PyObject *rc = 0; + + if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &n)) goto end; + if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length"); + rc = bytestring_pywrap(0, n); + GPRP_PRP(me)->dblk(GPRP_CTX(me), p, PyString_AS_STRING(rc)); +end: + return (rc); +} + +static PyGetSetDef gcprp_pygetset[] = { +#define GETSETNAME(op, name) gcp##op##_##name + GET (keysz, "CP.keysz -> acceptable key sizes") + GET (blksz, "CP.blksz -> block size") + GET (name, "CP.name -> name of this kind of PRP") +#undef GETSETNAME + { 0 } +}; + +static PyMethodDef gprp_pymethods[] = { +#define METHNAME(name) gpmeth_##name + METH (encrypt, "P.encrypt(PT) -> CT") + METH (decrypt, "P.decrypt(CT) -> PT") +#undef METHNAME + { 0 } +}; + +static PyTypeObject gcprp_pytype_skel = { + PyObject_HEAD_INIT(0) 0, /* Header */ + "GCPRP", /* @tp_name@ */ + sizeof(gcprp_pyobj), /* @tp_basicsize@ */ + 0, /* @tp_itemsize@ */ + + 0, /* @tp_dealloc@ */ + 0, /* @tp_print@ */ + 0, /* @tp_getattr@ */ + 0, /* @tp_setattr@ */ + 0, /* @tp_compare@ */ + 0, /* @tp_repr@ */ + 0, /* @tp_as_number@ */ + 0, /* @tp_as_sequence@ */ + 0, /* @tp_as_mapping@ */ + 0, /* @tp_hash@ */ + 0, /* @tp_call@ */ + 0, /* @tp_str@ */ + 0, /* @tp_getattro@ */ + 0, /* @tp_setattro@ */ + 0, /* @tp_as_buffer@ */ + Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ + Py_TPFLAGS_BASETYPE, + + /* @tp_doc@ */ +"Pseudorandom permutation metaclass.", + + 0, /* @tp_traverse@ */ + 0, /* @tp_clear@ */ + 0, /* @tp_richcompare@ */ + 0, /* @tp_weaklistoffset@ */ + 0, /* @tp_iter@ */ + 0, /* @tp_iternext@ */ + 0, /* @tp_methods@ */ + 0, /* @tp_members@ */ + gcprp_pygetset, /* @tp_getset@ */ + 0, /* @tp_base@ */ + 0, /* @tp_dict@ */ + 0, /* @tp_descr_get@ */ + 0, /* @tp_descr_set@ */ + 0, /* @tp_dictoffset@ */ + 0, /* @tp_init@ */ + PyType_GenericAlloc, /* @tp_alloc@ */ + abstract_pynew, /* @tp_new@ */ + 0, /* @tp_free@ */ + 0 /* @tp_is_gc@ */ +}; + +static PyTypeObject gprp_pytype_skel = { + PyObject_HEAD_INIT(0) 0, /* Header */ + "GPRP", /* @tp_name@ */ + sizeof(gprp_pyobj), /* @tp_basicsize@ */ + 0, /* @tp_itemsize@ */ + + gprp_pydealloc, /* @tp_dealloc@ */ + 0, /* @tp_print@ */ + 0, /* @tp_getattr@ */ + 0, /* @tp_setattr@ */ + 0, /* @tp_compare@ */ + 0, /* @tp_repr@ */ + 0, /* @tp_as_number@ */ + 0, /* @tp_as_sequence@ */ + 0, /* @tp_as_mapping@ */ + 0, /* @tp_hash@ */ + 0, /* @tp_call@ */ + 0, /* @tp_str@ */ + 0, /* @tp_getattro@ */ + 0, /* @tp_setattro@ */ + 0, /* @tp_as_buffer@ */ + Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ + Py_TPFLAGS_BASETYPE, + + /* @tp_doc@ */ +"Pseudorandom permutation, abstract base class.", + + 0, /* @tp_traverse@ */ + 0, /* @tp_clear@ */ + 0, /* @tp_richcompare@ */ + 0, /* @tp_weaklistoffset@ */ + 0, /* @tp_iter@ */ + 0, /* @tp_iternext@ */ + gprp_pymethods, /* @tp_methods@ */ + 0, /* @tp_members@ */ + 0, /* @tp_getset@ */ + 0, /* @tp_base@ */ + 0, /* @tp_dict@ */ + 0, /* @tp_descr_get@ */ + 0, /* @tp_descr_set@ */ + 0, /* @tp_dictoffset@ */ + 0, /* @tp_init@ */ + PyType_GenericAlloc, /* @tp_alloc@ */ + abstract_pynew, /* @tp_new@ */ + 0, /* @tp_free@ */ + 0 /* @tp_is_gc@ */ +}; + +/*----- Main code ---------------------------------------------------------*/ + +static PyMethodDef methods[] = { +#define METHNAME(func) meth_##func + METH (_KeySZ_fromdl, "\ +fromdl(N) -> M: convert integer discrete log field size to work factor") + METH (_KeySZ_fromschnorr, "\ +fromschnorr(N) -> M: convert Schnorr group order to work factor") + METH (_KeySZ_fromif, "\ +fromif(N) -> M: convert integer factorization problem size to work factor") + METH (_KeySZ_fromec, "\ +fromec(N) -> M: convert elliptic curve group order to work factor") + METH (_KeySZ_todl, "\ +todl(N) -> M: convert work factor to integer discrete log field size") + METH (_KeySZ_toschnorr, "\ +toschnorr(N) -> M: convert work factor to Schnorr group order") + METH (_KeySZ_toif, "\ +toif(N) -> M: convert work factor to integer factorization problem size") + METH (_KeySZ_toec, "\ +toec(N) -> M: convert work factor to elliptic curve group order") + METH (_KeySZ_toec, "\ +toec(N) -> M: convert work factor to elliptic curve group order") +#define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\ +" #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K") + METH_HDANCE(hsalsa20, "HSalsa20") + METH_HDANCE(hsalsa2012, "HSalsa20/12") + METH_HDANCE(hsalsa208, "HSalsa20/8") + METH_HDANCE(hchacha20, "HChaCha20") + METH_HDANCE(hchacha12, "HChaCha12") + METH_HDANCE(hchacha8, "HChaCha8") +#undef METH_DANCE +#undef METHNAME + { 0 } +}; + +void algorithms_pyinit(void) +{ + INITTYPE(keysz, root); + INITTYPE(keyszany, keysz); + INITTYPE(keyszrange, keysz); + INITTYPE(keyszset, keysz); + INITTYPE(gccipher, type); + INITTYPE(gcipher, root); + INITTYPE(gchash, type); + INITTYPE(ghash, root); + INITTYPE(gcmac, type); + INITTYPE(gmac, type); + INITTYPE(gmhash, ghash); + INITTYPE(poly1305cls, type); + INITTYPE_META(poly1305key, type, poly1305cls); + INITTYPE(poly1305hash, root); + INITTYPE(kxvik, root); + INITTYPE(shake, root); + INITTYPE(shake128, shake); + INITTYPE(shake256, shake); + INITTYPE(gcprp, type); + INITTYPE(gprp, root); + addmethods(methods); +} + GEN(gcciphers, cipher) GEN(gchashes, hash) GEN(gcmacs, mac) +#define gcprp prpinfo +GEN(gcprps, prp) void algorithms_pyinsert(PyObject *mod) { @@ -1187,6 +2419,16 @@ void algorithms_pyinsert(PyObject *mod) INSERT("GMAC", gmac_pytype); INSERT("GMACHash", gmhash_pytype); INSERT("gcmacs", gcmacs()); + INSERT("Poly1305Class", poly1305cls_pytype); + INSERT("poly1305", poly1305key_pytype); + INSERT("Poly1305Hash", poly1305hash_pytype); + INSERT("Keccak1600", kxvik_pytype); + INSERT("Shake", shake_pytype); + INSERT("Shake128", shake128_pytype); + INSERT("Shake256", shake256_pytype); + INSERT("GCPRP", gcprp_pytype); + INSERT("GPRP", gprp_pytype); + INSERT("gcprps", gcprps()); } /*----- That's all, folks -------------------------------------------------*/