+ gaea_sever(&ge->aad); GAEAD_DESTROY(ge->e);
+ Py_DECREF(Py_TYPE(me)); FREEOBJ(me);
+}
+
+static PyObject *gaeeget_hsz(PyObject *me, void *hunoz)
+{
+ if (GAEADENC_F(me)&AEADF_PCHSZ) return getulong(GAEADENC_HSZ(me));
+ else RETURN_NONE;
+}
+
+static PyObject *gaeeget_msz(PyObject *me, void *hunoz)
+{
+ if (GAEADENC_F(me)&AEADF_PCMSZ) return getulong(GAEADENC_MSZ(me));
+ else RETURN_NONE;
+}
+
+static PyObject *gaeeget_tsz(PyObject *me, void *hunoz)
+{
+ if (GAEADENC_F(me)&AEADF_PCTSZ) return getulong(GAEADENC_TSZ(me));
+ else RETURN_NONE;
+}
+
+static PyObject *gaeeget_mlen(PyObject *me, void *hunoz)
+ { return getulong(GAEADENC_MLEN(me)); }
+
+static const PyGetSetDef gaeadenc_pygetset[] = {
+#define GETSETNAME(op, name) gaee##op##_##name
+ GET (hsz, "ENC.hsz -> precommitted header length or `None'")
+ GET (msz, "ENC.msz -> precommitted message length or `None'")
+ GET (tsz, "ENC.tsz -> precommitted tag length or `None'")
+ GET (mlen, "ENC.mlen -> message length so far")
+#undef GETSETNAME
+ { 0 }
+};
+
+static PyObject *gaeemeth_aad(PyObject *me)
+{
+ gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
+ PyObject *rc = 0;
+
+ if (!(ge->f&AEADF_AADNDEP))
+ rc = gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(Py_TYPE(ge))->aad,
+ GAEAD_AAD(ge->e), 0, 0);
+ else {
+ if ((ge->f&AEADF_AADFIRST) && ge->mlen)
+ VALERR("too late for aad");
+ if (!ge->aad)
+ ge->aad = (gaeadaad_pyobj *)
+ gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(Py_TYPE(ge))->aad,
+ GAEAD_AAD(ge->e), ge->f&(AEADF_PCHSZ | AEADF_NOAAD),
+ ge->hsz);
+ Py_INCREF(ge->aad);
+ rc = (PyObject *)ge->aad;
+ }
+end:
+ return (rc);
+}
+
+static PyObject *gaeemeth_reinit(PyObject *me, PyObject *arg, PyObject *kw)
+{
+ static const char *const kwlist[] = { "nonce", "hsz", "msz", "tsz", 0 };
+ gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
+ struct bin n;
+ PyObject *hszobj = Py_None, *mszobj = Py_None, *tszobj = Py_None;
+ size_t hsz = 0, msz = 0, tsz = 0;
+ unsigned f;
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|OOO:enc", KWLIST,
+ convbin, &n, &hszobj, &mszobj, &tszobj))
+ goto end;
+ if (check_aead_encdec(ge->e->ops->c, &f, n.sz,
+ hszobj, &hsz, mszobj, &msz, tszobj, &tsz))
+ goto end;
+ if (GAEAD_REINIT(ge->e, n.p, n.sz, hsz, msz, tsz))
+ VALERR("bad aead parameter combination");
+ gaea_sever(&ge->aad);
+ ge->f = f; ge->hsz = hsz; ge->msz = msz; ge->tsz = tsz;
+end:
+ return (0);
+}
+
+static PyObject *gaeemeth_encrypt(PyObject *me, PyObject *arg)
+{
+ gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
+ struct bin m;
+ char *c = 0; size_t csz; buf b;
+ int err;
+ PyObject *rc = 0;
+
+ if (!PyArg_ParseTuple(arg, "O&:encrypt", convbin, &m)) goto end;
+ if (ge->f&AEADF_AADFIRST) {
+ if ((ge->f&AEADF_PCHSZ) && (ge->aad ? ge->aad->hlen : 0) != ge->hsz)
+ VALERR("header doesn't match precommitted length");
+ gaea_invalidate(ge->aad);
+ }
+ if ((ge->f&AEADF_PCMSZ) && m.sz > ge->msz - ge->mlen)
+ VALERR("too large for precommitted message length");
+ csz = m.sz + ge->e->ops->c->bufsz; c = xmalloc(csz); buf_init(&b, c, csz);
+ err = GAEAD_ENCRYPT(ge->e, m.p, m.sz, &b); assert(!err); (void)err;
+ buf_flip(&b); rc = bytestring_pywrapbuf(&b); ge->mlen += m.sz;
+end:
+ xfree(c);
+ return (rc);
+}
+
+static PyObject *gaeemeth_done(PyObject *me, PyObject *arg, PyObject *kw)
+{
+ static const char *const kwlist[] = { "tsz", "aad", 0 };
+ gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
+ PyObject *aad = Py_None;
+ char *c = 0; size_t csz; buf b;
+ PyObject *tszobj = Py_None; PyObject *tag; size_t tsz;
+ int err;
+ PyObject *rc = 0;
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OO:done", KWLIST,
+ &tszobj, &aad))
+ goto end;
+ if (tszobj != Py_None && !convszt(tszobj, &tsz)) goto end;
+ if (aad != Py_None &&
+ !PyObject_TypeCheck(aad,
+ (PyTypeObject *)GCAEADENC_KEY(Py_TYPE(me))->aad))
+ TYERR("wanted aad");
+ if ((ge->f&AEADF_AADNDEP) && aad != Py_None && aad != (PyObject *)ge->aad)
+ VALERR("mismatched aad");
+ if ((ge->f&AEADF_PCHSZ) &&
+ (aad == Py_None ? 0 : GAEADAAD_HLEN(aad)) != ge->hsz)
+ VALERR("header doesn't match precommitted length");
+ if ((ge->f&AEADF_PCMSZ) && ge->mlen != ge->msz)
+ VALERR("message doesn't match precommitted length");
+ if (tszobj == Py_None) {
+ if (ge->f&AEADF_PCTSZ) tsz = ge->tsz;
+ else tsz = keysz(0, ge->e->ops->c->tagsz);
+ } else {
+ if ((ge->f&AEADF_PCTSZ) && tsz != ge->tsz)
+ VALERR("tag length doesn't match precommitted value");
+ if (keysz(tsz, ge->e->ops->c->tagsz) != tsz) VALERR("bad tag length");
+ }
+ csz = ge->e->ops->c->bufsz; c = xmalloc(csz); buf_init(&b, c, csz);
+ tag = bytestring_pywrap(0, tsz);
+ err = GAEAD_DONE(ge->e, aad == Py_None ? 0 : GAEADAAD_A(aad), &b,
+ BIN_PTR(tag), tsz);
+ assert(!err); (void)err;
+ buf_flip(&b); rc = Py_BuildValue("NN", bytestring_pywrapbuf(&b), tag);
+end:
+ xfree(c);
+ return (rc);
+}
+
+static const PyMethodDef gaeadenc_pymethods[] = {
+#define METHNAME(name) gaeemeth_##name
+ NAMETH(aad, "ENC.aad() -> AAD")
+ KWMETH(reinit, "ENC.reinit(NONCE, [hsz], [msz], [tsz])")
+ METH (encrypt, "ENC.encrypt(MSG) -> CT")
+ KWMETH(done, "ENC.done([tsz], [aad]) -> CT, TAG")
+#undef METHNAME
+ { 0 }
+};
+
+static PyObject *gaeaddec_pywrap(PyObject *cobj, gaead_dec *d, unsigned f,
+ size_t hsz, size_t csz, size_t tsz)
+{
+ gaeaddec_pyobj *gd;
+ assert(cobj); Py_INCREF(cobj);
+ gd = PyObject_NEW(gaeaddec_pyobj, (PyTypeObject *)cobj);
+ gd->d = d; gd->f = f; gd->hsz = hsz; gd->csz = csz; gd->tsz = tsz;
+ gd->aad = 0; gd->clen = 0;
+ return ((PyObject *)gd);
+}
+
+static void gaeaddec_pydealloc(PyObject *me)
+{
+ gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
+
+ gaea_sever(&gd->aad); GAEAD_DESTROY(GAEADDEC_D(me));
+ Py_DECREF(Py_TYPE(me)); FREEOBJ(me);
+}
+
+static PyObject *gaedget_hsz(PyObject *me, void *hunoz)
+{
+ if (GAEADDEC_F(me)&AEADF_PCHSZ) return getulong(GAEADDEC_HSZ(me));
+ else RETURN_NONE;
+}
+
+static PyObject *gaedget_csz(PyObject *me, void *hunoz)
+{
+ if (GAEADDEC_F(me)&AEADF_PCMSZ) return getulong(GAEADDEC_CSZ(me));
+ else RETURN_NONE;
+}
+
+static PyObject *gaedget_tsz(PyObject *me, void *hunoz)
+{
+ if (GAEADDEC_F(me)&AEADF_PCTSZ) return getulong(GAEADDEC_TSZ(me));
+ else RETURN_NONE;
+}
+
+static PyObject *gaedget_clen(PyObject *me, void *hunoz)
+ { return getulong(GAEADDEC_CLEN(me)); }
+
+static const PyGetSetDef gaeaddec_pygetset[] = {
+#define GETSETNAME(op, name) gaed##op##_##name
+ GET (hsz, "DEC.hsz -> precommitted header length or `None'")
+ GET (csz, "DEC.csz -> precommitted ciphertext length or `None'")
+ GET (tsz, "DEC.tsz -> precommitted tag length or `None'")
+ GET (clen, "DEC.clen -> ciphertext length so far")
+#undef GETSETNAME
+ { 0 }
+};
+
+static PyObject *gaedmeth_aad(PyObject *me)
+{
+ gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
+
+ if (!(gd->f&AEADF_AADNDEP))
+ return (gaeadaad_pywrap((PyObject *)GCAEADDEC_KEY(Py_TYPE(gd))->aad,
+ GAEAD_AAD(gd->d), 0, 0));
+ else {
+ if (!gd->aad)
+ gd->aad = (gaeadaad_pyobj *)
+ gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(Py_TYPE(gd))->aad,
+ GAEAD_AAD(gd->d), gd->f&(AEADF_PCHSZ | AEADF_NOAAD),
+ gd->hsz);
+ Py_INCREF(gd->aad);
+ return ((PyObject *)gd->aad);
+ }
+}
+
+static PyObject *gaedmeth_reinit(PyObject *me, PyObject *arg, PyObject *kw)
+{
+ static const char *const kwlist[] = { "nonce", "hsz", "csz", "tsz", 0 };
+ gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
+ struct bin n;
+ PyObject *hszobj = Py_None, *cszobj = Py_None, *tszobj = Py_None;
+ size_t hsz = 0, csz = 0, tsz = 0;
+ unsigned f;
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|OOO:enc", KWLIST,
+ convbin, &n, &hszobj, &cszobj, &tszobj))
+ goto end;
+ if (check_aead_encdec(gd->d->ops->c, &f, n.sz,
+ hszobj, &hsz, cszobj, &csz, tszobj, &tsz))
+ goto end;
+ if (GAEAD_REINIT(gd->d, n.p, n.sz, hsz, csz, tsz))
+ VALERR("bad aead parameter combination");
+ gaea_sever(&gd->aad);
+ gd->f = f; gd->hsz = hsz; gd->csz = csz; gd->tsz = tsz;
+end:
+ return (0);
+}
+
+static PyObject *gaedmeth_decrypt(PyObject *me, PyObject *arg)
+{
+ gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
+ struct bin c;
+ char *m = 0; size_t msz; buf b;
+ int err;
+ PyObject *rc = 0;
+
+ if (!PyArg_ParseTuple(arg, "O&:decrypt", convbin, &c)) goto end;
+ if (gd->f&AEADF_AADFIRST) {
+ if ((gd->f&AEADF_PCHSZ) && (gd->aad ? gd->aad->hlen : 0) != gd->hsz)
+ VALERR("header doesn't match precommitted length");
+ gaea_invalidate(gd->aad);
+ }
+ if ((gd->f&AEADF_PCMSZ) && c.sz > gd->csz - gd->clen)
+ VALERR("too large for precommitted message length");
+ msz = c.sz + gd->d->ops->c->bufsz; m = xmalloc(msz); buf_init(&b, m, msz);
+ err = GAEAD_DECRYPT(gd->d, c.p, c.sz, &b); assert(!err); (void)err;
+ buf_flip(&b); rc = bytestring_pywrapbuf(&b); gd->clen += c.sz;
+end:
+ xfree(m);
+ return (rc);
+}
+
+static PyObject *gaedmeth_done(PyObject *me, PyObject *arg, PyObject *kw)
+{
+ static const char *const kwlist[] = { "tag", "aad", 0 };
+ gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
+ PyObject *aad = Py_None;
+ struct bin t;
+ char *m = 0; size_t msz; buf b;
+ int err;
+ PyObject *rc = 0;
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O:done", KWLIST,
+ convbin, &t, &aad))
+ goto end;
+ if (aad != Py_None &&
+ !PyObject_TypeCheck(aad,
+ (PyTypeObject *)GCAEADENC_KEY(Py_TYPE(me))->aad))
+ TYERR("wanted aad");
+ if ((gd->f&AEADF_AADNDEP) && aad != Py_None && aad != (PyObject *)gd->aad)
+ VALERR("mismatched aad");
+ if ((gd->f&AEADF_PCHSZ) &&
+ (aad == Py_None ? 0 : GAEADAAD_HLEN(aad)) != gd->hsz)
+ VALERR("header doesn't match precommitted length");
+ if ((gd->f&AEADF_PCMSZ) && gd->clen != gd->csz)
+ VALERR("message doesn't match precommitted length");
+ if ((gd->f&AEADF_PCTSZ) && t.sz != gd->tsz)
+ VALERR("tag length doesn't match precommitted value");
+ if (keysz(t.sz, gd->d->ops->c->tagsz) != t.sz) VALERR("bad tag length");
+ msz = gd->d->ops->c->bufsz; m = xmalloc(msz); buf_init(&b, m, msz);
+ err = GAEAD_DONE(gd->d, aad == Py_None ? 0 : GAEADAAD_A(aad),
+ &b, t.p, t.sz);
+ assert(err >= 0);
+ if (!err) VALERR("decryption failed");
+ buf_flip(&b); rc = bytestring_pywrapbuf(&b);
+end:
+ xfree(m);
+ return (rc);
+}
+
+static const PyMethodDef gaeaddec_pymethods[] = {
+#define METHNAME(name) gaedmeth_##name
+ NAMETH(aad, "DEC.aad() -> AAD")
+ KWMETH(reinit, "DEC.reinit(NONCE, [hsz], [csz], [tsz])")
+ METH (decrypt, "DEC.decrypt(CT) -> MSG")
+ KWMETH(done, "DEC.done(TAG, [aad]) -> MSG | None")
+#undef METHNAME
+ { 0 }
+};
+
+static const PyTypeObject gcaead_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "GCAEAD", /* @tp_name@ */
+ sizeof(gcaead_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@ */
+ "Authenticated encryption (key) 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@ */
+ PYGETSET(gcaead), /* @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 const PyTypeObject gaeadkey_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "GAEKey", /* @tp_name@ */
+ sizeof(gaeadkey_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ gaeadkey_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@ */
+ "Authenticated encryption key.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ PYMETHODS(gaeadkey), /* @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@ */
+};
+
+static const PyTypeObject gcaeadaad_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "GAEAADClass", /* @tp_name@ */
+ sizeof(gcaeadaad_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@ */
+ "Authenticated encryption additional-data hash 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@ */
+ 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@ */
+};
+
+static const PyTypeObject gaeadaad_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "GAEAAD", /* @tp_name@ */
+ sizeof(gaeadaad_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ gaeadaad_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@ */
+ "Authenticated encryption AAD hash.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ PYMETHODS(gaeadaad), /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ PYGETSET(gaeadaad), /* @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 const PyTypeObject gcaeadenc_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "GAEEncClass", /* @tp_name@ */
+ sizeof(gcaeadenc_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@ */
+ "Authenticated encryption operation 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@ */
+ 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@ */
+};
+
+static const PyTypeObject gaeadenc_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "GAEEnc", /* @tp_name@ */
+ sizeof(gaeadenc_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ gaeadenc_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@ */
+ "Authenticated encryption operation.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ PYMETHODS(gaeadenc), /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ PYGETSET(gaeadenc), /* @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 const PyTypeObject gcaeaddec_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "GAEDecClass", /* @tp_name@ */
+ sizeof(gcaeaddec_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@ */
+ "Authenticated decryption operation 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@ */
+ 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@ */
+};
+
+static const PyTypeObject gaeaddec_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "GAEDec", /* @tp_name@ */
+ sizeof(gaeaddec_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ gaeaddec_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@ */
+ "Authenticated decryption operation.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ PYMETHODS(gaeaddec), /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ PYGETSET(gaeaddec), /* @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@ */
+};
+
+/*----- Hash functions ----------------------------------------------------*/
+
+PyTypeObject *gchash_pytype;
+static PyTypeObject *ghash_pytype;
+PyObject *sha_pyobj, *has160_pyobj;
+
+typedef struct ghash_pyobj {
+ PyObject_HEAD
+ ghash *h;
+} ghash_pyobj;
+
+#define GHASH_PYCHECK(o) PyObject_TypeCheck((o), ghash_pytype)
+#define GHASH_H(o) (((ghash_pyobj *)(o))->h)
+
+CONVFUNC(gchash, gchash *, GCHASH_CH)
+CONVFUNC(ghash, ghash *, GHASH_H)
+
+static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
+{
+ static const char *const kwlist[] = { 0 };
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST))
+ goto end;
+ return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty))));
+end:
+ return (0);
+}
+
+static PyObject *gchash_pywrap(gchash *ch)
+{
+ gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name);
+ g->ch = ch;
+ g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
+ g->ty.ht_type.tp_base = ghash_pytype;
+ Py_INCREF(ghash_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 = ghash_pynew;
+ typeready(&g->ty.ht_type);
+ return ((PyObject *)g);
+}
+
+PyObject *ghash_pywrap(PyObject *cobj, ghash *h)
+{
+ ghash_pyobj *g;
+ if (!cobj) cobj = gchash_pywrap((/*unconst*/ gchash *)GH_CLASS(h));
+ else Py_INCREF(cobj);
+ g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj);
+ g->h = h;
+ return ((PyObject *)g);
+}
+
+static void ghash_pydealloc(PyObject *me)
+{
+ GH_DESTROY(GHASH_H(me));
+ Py_DECREF(Py_TYPE(me));
+ FREEOBJ(me);
+}
+
+static PyObject *gchget_name(PyObject *me, void *hunoz)
+ { return (TEXT_FROMSTR(GCHASH_CH(me)->name)); }
+
+static PyObject *gchget_hashsz(PyObject *me, void *hunoz)
+ { return (PyInt_FromLong(GCHASH_CH(me)->hashsz)); }
+
+static PyObject *gchget_bufsz(PyObject *me, void *hunoz)
+ { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); }
+
+static PyObject *ghmeth_copy(PyObject *me)
+ { return (ghash_pywrap((PyObject *)Py_TYPE(me), GH_COPY(GHASH_H(me)))); }
+
+static PyObject *ghmeth_hash(PyObject *me, PyObject *arg)
+{
+ struct bin m;
+ if (!PyArg_ParseTuple(arg, "O&:hash", convbin, &m)) return (0);
+ GH_HASH(GHASH_H(me), m.p, m.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)) return (0); \
+ GH_HASHU##W(GHASH_H(me), x); \
+ RETURN_ME; \
+ }
+DOUINTCONV(GHMETH_HASHU_)
+
+#define GHMETH_HASHBUF_(n, W, w) \
+ static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \
+ { \
+ struct bin in; \
+ if (!PyArg_ParseTuple(arg, "O&:hashbuf" #w, convbin, &in)) goto end; \
+ if (in.sz > MASK##n) VALERR("too large"); \
+ GH_HASHBUF##W(GHASH_H(me), in.p, in.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)
+{
+ ghash *g;
+ PyObject *rc;
+ g = GH_COPY(GHASH_H(me));
+ rc = bytestring_pywrap(0, g->ops->c->hashsz);
+ GH_DONE(g, BIN_PTR(rc));
+ GH_DESTROY(g);
+ return (rc);
+}
+
+static const PyGetSetDef gchash_pygetset[] = {
+#define GETSETNAME(op, name) gch##op##_##name
+ GET (bufsz, "CH.bufsz -> hash buffer size, or zero")
+ GET (hashsz, "CH.hashsz -> hash output size")
+ GET (name, "CH.name -> name of this kind of hash")
+#undef GETSETNAME
+ { 0 }
+};
+
+static const PyMethodDef ghash_pymethods[] = {
+#define METHNAME(name) ghmeth_##name
+ NAMETH(copy, "H.copy() -> HH")
+ 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)")
+ NAMETH(done, "H.done() -> HASH")
+#undef METHNAME
+ { 0 }
+};
+
+static const PyTypeObject gchash_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "GCHash", /* @tp_name@ */
+ sizeof(gchash_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@ */
+ "Hash function 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@ */
+ PYGETSET(gchash), /* @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 const PyTypeObject ghash_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "GHash", /* @tp_name@ */
+ sizeof(ghash_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ ghash_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@ */
+ "Hash function, abstract base class.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ PYMETHODS(ghash), /* @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@ */
+};
+
+/*----- Message authentication --------------------------------------------*/
+
+static PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype;
+
+typedef struct gcmac_pyobj {
+ PyHeapTypeObject ty;
+ gcmac *cm;
+} gcmac_pyobj;
+
+#define GCMAC_PYCHECK(o) PyObject_TypeCheck((o), gcmac_pytype)
+#define GCMAC_CM(o) (((gcmac_pyobj *)(o))->cm)
+#define GCMAC_F(o) (((gcmac_pyobj *)(o))->f)
+CONVFUNC(gcmac, gcmac *, GCMAC_CM)
+static PyObject *gmac_pywrap(PyObject *, gmac *);
+
+typedef struct gmac_pyobj {
+ PyHeapTypeObject ty;
+ gmac *m;
+} gmac_pyobj;
+
+extern PyTypeObject *gmac_pytype;
+#define GMAC_PYCHECK(o) PyObject_TypeCheck((o), gmac_pytype)
+#define GMAC_M(o) (((gmac_pyobj *)(o))->m)
+#define GMAC_F(o) (((gmac_pyobj *)(o))->f)
+extern PyObject *gmac_pywrap(PyObject *, gmac *);
+extern int convgmac(PyObject *, void *);
+
+static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
+{
+ static const char *const kwlist[] = { "k", 0 };
+ struct bin k;
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convbin, &k))
+ goto end;
+ if (keysz(k.sz, GCMAC_CM(ty)->keysz) != k.sz) VALERR("bad key length");
+ return (gmac_pywrap((PyObject *)ty,
+ GM_KEY(GCMAC_CM(ty), k.p, k.sz)));
+end:
+ return (0);
+}
+
+static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
+{
+ static const char *const kwlist[] = { 0 };
+ ghash_pyobj *g;
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST)) return (0);
+ g = PyObject_NEW(ghash_pyobj, ty);
+ g->h = GM_INIT(GMAC_M(ty));
+ Py_INCREF(ty);
+ return ((PyObject *)g);
+}
+
+static PyObject *gcmac_pywrap(gcmac *cm)
+{
+ gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name);
+ g->cm = cm;
+ g->ty.ht_type.tp_basicsize = sizeof(gmac_pyobj);
+ g->ty.ht_type.tp_base = gmac_pytype;
+ Py_INCREF(gmac_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 = gmac_pynew;
+ typeready(&g->ty.ht_type);
+ return ((PyObject *)g);
+}
+
+static PyObject *gmac_pywrap(PyObject *cobj, gmac *m)
+{
+ gmac_pyobj *g;
+ if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m));
+ else Py_INCREF(cobj);
+ g = newtype((PyTypeObject *)cobj, 0, 0);
+ g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
+ g->ty.ht_name = TEXT_FORMAT("%s(keyed)", m->ops->c->name);
+ g->ty.ht_type.tp_name = TEXT_PTR(g->ty.ht_name);
+ g->ty.ht_type.tp_base = gmhash_pytype;
+ Py_INCREF(gmac_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 = gmhash_pynew;
+ typeready(&g->ty.ht_type);
+ g->m = m;
+ return ((PyObject *)g);
+}
+
+static void gmac_pydealloc(PyObject *me)
+{
+ GM_DESTROY(GMAC_M(me));
+ Py_DECREF(Py_TYPE(me));
+ PyType_Type.tp_dealloc(me);
+}
+
+static PyObject *gcmget_name(PyObject *me, void *hunoz)
+ { return (TEXT_FROMSTR(GCMAC_CM(me)->name)); }
+
+static PyObject *gcmget_keysz(PyObject *me, void *hunoz)
+ { return (keysz_pywrap(GCMAC_CM(me)->keysz)); }
+
+static PyObject *gcmget_tagsz(PyObject *me, void *hunoz)
+ { return (PyInt_FromLong(GCMAC_CM(me)->hashsz)); }
+
+static const PyGetSetDef gcmac_pygetset[] = {
+#define GETSETNAME(op, name) gcm##op##_##name
+ GET (keysz, "CM.keysz -> acceptable key sizes")
+ GET (tagsz, "CM.tagsz -> MAC output size")
+ GET (name, "CM.name -> name of this kind of MAC")
+#undef GETSETNAME
+ { 0 }
+};
+
+static PyObject *gmget_name(PyObject *me, void *hunoz)
+ { return (TEXT_FROMSTR(GMAC_M(me)->ops->c->name)); }
+
+static PyObject *gmget_hashsz(PyObject *me, void *hunoz)
+ { return (PyInt_FromLong(GMAC_M(me)->ops->c->hashsz)); }
+#define gmget_tagsz gmget_hashsz
+
+static const PyGetSetDef gmac_pygetset[] = {
+#define GETSETNAME(op, name) gm##op##_##name
+ GET (hashsz, "M.hashsz -> MAC output size")
+ GET (tagsz, "M.tagsz -> MAC output size")
+ GET (name, "M.name -> name of this kind of MAC")
+#undef GETSETNAME
+ { 0 }
+};
+
+static const PyTypeObject gcmac_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "GCMAC", /* @tp_name@ */
+ sizeof(gchash_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@ */
+ "Message authentication code metametaclass.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ 0, /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ PYGETSET(gcmac), /* @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 const PyTypeObject gmac_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "GMAC", /* @tp_name@ */
+ sizeof(gmac_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ gmac_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@ */
+ "Message authentication code metaclass, abstract base class.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ 0, /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ PYGETSET(gmac), /* @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 const PyTypeObject gmhash_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "GMACHash", /* @tp_name@ */
+ sizeof(ghash_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ ghash_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@ */
+ "Message authentication code, abstract base class.",
+
+ 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@ */
+ abstract_pynew, /* @tp_new@ */
+ 0, /* @tp_free@ */
+ 0 /* @tp_is_gc@ */
+};
+
+/*----- Special snowflake for Poly1305 ------------------------------------*/
+
+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)
+{
+ static const char *const kwlist[] = { "mask", 0 };
+ poly1305key_pyobj *pk = (poly1305key_pyobj *)ty;
+ poly1305hash_pyobj *ph;
+ struct bin m = { 0, 0 };
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST, convbin, &m))
+ return (0);
+ if (m.p && m.sz != POLY1305_MASKSZ) VALERR("bad mask length");
+ ph = PyObject_NEW(poly1305hash_pyobj, ty);
+ ph->f = 0;
+ if (m.p) ph->f |= f_mask;
+ poly1305_macinit(&ph->ctx, &pk->k, m.p);
+ Py_INCREF(ty);
+ return ((PyObject *)ph);
+end:
+ return (0);
+}
+
+static PyObject *poly1305key_pynew(PyTypeObject *ty,
+ PyObject *arg, PyObject *kw)
+{
+ static const char *const kwlist[] = { "k", 0 };
+ poly1305key_pyobj *pk;
+ struct bin k;
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convbin, &k))
+ goto end;
+ if (keysz(k.sz, poly1305_keysz) != k.sz) VALERR("bad key length");
+
+ pk = newtype(ty, 0, 0);
+ pk->ty.ht_name = TEXT_FROMSTR("poly1305(keyed)");
+ pk->ty.ht_type.tp_basicsize = sizeof(poly1305hash_pyobj);
+ pk->ty.ht_type.tp_name = TEXT_PTR(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.p, k.sz);
+ return ((PyObject *)pk);
+
+end:
+ return (0);
+}
+
+static PyObject *poly1305clsget_name(PyObject *me, void *hunoz)
+ { return (TEXT_FROMSTR("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)
+{
+ poly1305hash_pyobj *ph;
+ ph = PyObject_NEW(poly1305hash_pyobj, Py_TYPE(me));
+ poly1305_copy(&ph->ctx, P1305_CTX(me));
+ Py_INCREF(Py_TYPE(me));
+ return ((PyObject *)ph);
+}
+
+static PyObject *polymeth_hash(PyObject *me, PyObject *arg)
+{
+ struct bin m;
+ if (!PyArg_ParseTuple(arg, "O&:hash", convbin, &m)) return (0);
+ poly1305_hash(P1305_CTX(me), m.p, m.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)) return (0); \
+ STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
+ RETURN_ME; \
+ }
+DOUINTCONV(POLYMETH_HASHU_)
+
+#define POLYMETH_HASHBUF_(n, W, w) \
+ static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \
+ { \
+ struct bin in; \
+ octet b[SZ_##W]; \
+ if (!PyArg_ParseTuple(arg, "O&:hashbuf" #w, convbin, &in)) goto end; \
+ if (in.sz > MASK##n) VALERR("too large"); \
+ STORE##W(b, in.sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
+ poly1305_hash(P1305_CTX(me), in.p, in.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)
+ { poly1305_flush(P1305_CTX(me)); RETURN_ME; }
+
+static PyObject *polymeth_flushzero(PyObject *me)
+ { 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 (Py_TYPE(me) != Py_TYPE(pre) || Py_TYPE(me) != Py_TYPE(suff))
+ 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 *rc;
+ if (!(P1305_F(me) & f_mask)) VALERR("no mask");
+ rc = bytestring_pywrap(0, POLY1305_TAGSZ);
+ poly1305_done(P1305_CTX(me), BIN_PTR(rc));
+ return (rc);
+end:
+ return (0);
+}
+
+static const 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 PyObject *poly1305get_name(PyObject *me, void *hunoz)
+ { RETURN_OBJ(((PyHeapTypeObject *)poly1305key_pytype)->ht_name); }
+
+static PyObject *poly1305get_tagsz(PyObject *me, void *hunoz)
+ { return (PyInt_FromLong(16)); }
+
+static const PyGetSetDef poly1305_pygetset[] = {
+#define GETSETNAME(op, name) poly1305##op##_##name
+ GET (tagsz, "PK.tagsz -> MAC output size")
+ GET (name, "PK.name -> name of this kind of MAC")
+#undef GETSETNAME
+ { 0 }
+};
+
+static const PyMethodDef poly1305hash_pymethods[] = {
+#define METHNAME(name) polymeth_##name
+ NAMETH(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)")
+ NAMETH(flush, "P.flush()")
+ NAMETH(flushzero, "P.flushzero()")
+ METH (concat, "P.concat(PREFIX, SUFFIX)")
+ NAMETH(done, "P.done() -> TAG")
+#undef METHNAME
+ { 0 }
+};
+
+static const PyTypeObject poly1305cls_pytype_skel = {
+ PyVarObject_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@ */
+ PYGETSET(poly1305cls), /* @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 const PyTypeObject poly1305key_pytype_skel = {
+ PyVarObject_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@ */
+ PYGETSET(poly1305), /* @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 const PyTypeObject poly1305hash_pytype_skel = {
+ PyVarObject_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@ */
+ PYMETHODS(poly1305hash), /* @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) \
+ { \
+ dance##_ctx dance; \
+ struct bin k, n; \
+ PyObject *rc; \
+ if (!PyArg_ParseTuple(arg, "O&O&:" #hdance "_prf", \
+ convbin, &k, convbin, &n)) \
+ goto end; \
+ if (k.sz != keysz(k.sz, dance##_keysz)) VALERR("bad key length"); \
+ if (n.sz != HDANCE##_INSZ) VALERR("bad input length"); \
+ rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \
+ dance##_init(&dance, k.p, k.sz, 0); \
+ hdance##_prf(&dance, n.p, BIN_PTR(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;
+ static const char *const 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_copy(PyObject *me)
+{
+ kxvik_pyobj *k = (kxvik_pyobj *)me, *rc = 0;
+ rc = (kxvik_pyobj *)Py_TYPE(k)->tp_alloc(Py_TYPE(k), 0);
+ rc->s = k->s; rc->n = k->n;
+ 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;
+ struct bin in;
+ size_t n;
+
+ if (!PyArg_ParseTuple(arg, "O&:mix", convbin, &in)) goto end;
+ if (in.sz > 200) VALERR("out of range");
+ q = in.p; n = in.sz;
+ 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&:extract", convuint, &n)) goto end;
+ if (n > 200) VALERR("out of range");
+ rc = bytestring_pywrap(0, n);
+ q = (octet *)BIN_PTR(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)
+{
+ kxvik_pyobj *k = (kxvik_pyobj *)me;
+ keccak1600_p(&k->s, &k->s, k->n);
+ RETURN_ME;
+}
+
+static const PyMemberDef kxvik_pymembers[] = {
+#define MEMBERSTRUCT kxvik_pyobj
+ MEMRNM(nround, T_UINT, n, 0, "KECCAC.nround -> number of rounds")
+#undef MEMBERSTRUCT
+ { 0 }
+};
+
+static const PyMethodDef kxvik_pymethods[] = {
+#define METHNAME(func) kxvikmeth_##func
+ NAMETH(copy, "KECCAK.copy() -> KECCAK'")
+ METH (mix, "KECCAK.mix(DATA)")
+ METH (extract, "KECCAK.extract(NOCTETS)")
+ NAMETH(step, "KECCAK.step()")
+#undef METHNAME
+ { 0 }
+};
+
+static const PyTypeObject kxvik_pytype_skel = {
+ PyVarObject_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@ */
+ PYMETHODS(kxvik), /* @tp_methods@ */
+ PYMEMBERS(kxvik), /* @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@ */
+ 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;
+ PyObject *pobj = Py_None, *fobj = Py_None;
+ struct bin p = { 0, 0 }, f = { 0, 0 };
+ static const char *const kwlist[] = { "perso", "func", 0 };
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OO:new", KWLIST, &pobj, &fobj))
+ goto end;
+ if (pobj != Py_None && !convbin(pobj, &p)) goto end;
+ if (fobj != Py_None && !convbin(fobj, &f)) goto end;
+ rc = (shake_pyobj *)ty->tp_alloc(ty, 0);
+ initfn(&rc->h, f.p, f.sz, p.p, p.sz);
+ 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)
+{
+ struct bin m;
+ if (!PyArg_ParseTuple(arg, "O&:hash", convbin, &m)) return (0);
+ if (shake_check(me, 0)) return (0);
+ shake_hash(SHAKE_H(me), m.p, m.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)) return (0); \
+ if (shake_check(me, 0)) return (0); \
+ STORE##W(b, x); shake_hash(SHAKE_H(me), b, sizeof(b)); \
+ RETURN_ME; \
+ }
+DOUINTCONV(SHAKEMETH_HASHU_)
+
+#define SHAKEMETH_HASHBUF_(n, W, w) \
+ static PyObject *shakemeth_hashbuf##w(PyObject *me, PyObject *arg) \
+ { \
+ struct bin in; \
+ octet b[SZ_##W]; \
+ if (!PyArg_ParseTuple(arg, "O&:hashbuf" #w, convbin, &in)) goto end; \
+ if (in.sz > MASK##n) VALERR("too large"); \
+ if (shake_check(me, 0)) goto end; \
+ STORE##W(b, in.sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \
+ shake_hash(SHAKE_H(me), in.p, in.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)
+{
+ 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 *kw)
+{
+ PyObject *rc = 0;
+ size_t n = 100 - SHAKE_H(me)->h.r/2;
+ static const char *const kwlist[] = { "hsz", 0 };
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:done", KWLIST, convszt, &n))
+ goto end;
+ if (shake_check(me, 0)) goto end;
+ rc = bytestring_pywrap(0, n);
+ shake_done(SHAKE_H(me), BIN_PTR(rc), n);
+ SHAKE_ST(me) = -1;
+end:
+ return (rc);
+}
+
+static PyObject *shakemeth_copy(PyObject *me)
+{
+ shake_pyobj *rc = 0;
+
+ rc = PyObject_NEW(shake_pyobj, Py_TYPE(me));
+ rc->h = *SHAKE_H(me);
+ rc->st = SHAKE_ST(me);
+ return ((PyObject *)rc);
+}
+
+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), BIN_PTR(rc), sz);
+end:
+ return (rc);
+}
+
+static PyObject *shakemeth_mask(PyObject *me, PyObject *arg)
+{
+ PyObject *rc = 0;
+ struct bin in;
+
+ if (!PyArg_ParseTuple(arg, "O&:mask", convbin, &in)) goto end;
+ if (shake_check(me, 1)) goto end;
+ rc = bytestring_pywrap(0, in.sz);
+ shake_mask(SHAKE_H(me), in.p, BIN_PTR(rc), in.sz);
+end:
+ return (rc);
+}
+
+static PyObject *shakeget_state(PyObject *me, void *hunoz)
+{
+ int st = SHAKE_ST(me);
+ return (TEXT_FROMSTR(st == 0 ? "absorb" :
+ st == 1 ? "squeeze" : "dead"));
+}
+
+static const PyMemberDef shake_pymembers[] = {
+#define MEMBERSTRUCT shake_pyobj
+ MEMRNM(rate, T_UINT, h.h.r, READONLY, "S.rate -> rate, in bytes")
+ MEMRNM(buffered, T_UINT, h.h.n, READONLY,
+ "S.buffered -> amount currently buffered")
+#undef MEMBERSTRUCT
+ { 0 }
+};
+
+static const PyGetSetDef shake_pygetset[] = {
+#define GETSETNAME(op, name) shake##op##_##name
+ GET (state, "S.state -> `absorb', `squeeze', `dead'")
+#undef GETSETNAME
+ { 0 }
+};
+
+static const PyMethodDef shake_pymethods[] = {
+#define METHNAME(func) shakemeth_##func
+ NAMETH(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)")
+ NAMETH(xof, "S.xof()")
+ KWMETH(done, "S.done([hsz = CAP]) -> H")
+ METH (get, "S.get(LEN) -> H")
+ METH (mask, "S.mask(M) -> C")
+#undef METHNAME
+ { 0 }
+};
+
+static const PyTypeObject shake_pytype_skel = {
+ PyVarObject_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/KMAC base class.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ PYMETHODS(shake), /* @tp_methods@ */
+ PYMEMBERS(shake), /* @tp_members@ */
+ PYGETSET(shake), /* @tp_getset@ */
+ 0, /* @tp_base@ */
+ 0, /* @tp_dict@ */
+ 0, /* @tp_descr_get@ */