algorithms.c: Add bindings for STROBE.
[catacomb-python] / algorithms.c
index 42dbe6f..de8fcd6 100644 (file)
@@ -86,12 +86,13 @@ PyObject *keysz_pywrap(const octet *k)
     case KSZ_SET: {
       keyszset_pyobj *o =
        PyObject_New(keyszset_pyobj, keyszset_pytype);
+      PyObject *l;
       int i, n;
       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(ARG(i)));
+      n = i; l = PyList_New(n);
+      for (i = 0; i < n; i++) PyList_SET_ITEM(l, i, PyInt_FromLong(ARG(i)));
+      o->set = PyFrozenSet_New(l); Py_DECREF(l);
       return ((PyObject *)o);
     } break;
     default:
@@ -121,13 +122,20 @@ static PyObject *keyszrange_pynew(PyTypeObject *ty,
                                  PyObject *arg, PyObject *kw)
 {
   static const char *const kwlist[] = { "default", "min", "max", "mod", 0 };
-  int dfl, min = 0, max = 0, mod = 1;
+  int dfl, min = 0, max, mod = 1;
+  PyObject *maxobj = Py_None;
   keyszrange_pyobj *o;
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|iii:new", KWLIST,
-                                  &dfl, &min, &max, &mod))
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|iOi:new", KWLIST,
+                                  &dfl, &min, &maxobj, &mod))
     goto end;
-  if (dfl < 0 || min < 0) VALERR("key size cannot be negative");
+  if (maxobj == Py_None)
+    max = 0;
+  else {
+    max = PyInt_AsLong(maxobj);
+    if (max == -1 && PyErr_Occurred()) 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)
     VALERR("bad key size modulus");
@@ -145,41 +153,30 @@ static PyObject *keyszset_pynew(PyTypeObject *ty,
                                PyObject *arg, PyObject *kw)
 {
   static const char *const kwlist[] = { "default", "set", 0 };
-  int dfl, i, n, xx;
+  int dfl, xx;
   PyObject *set = 0;
-  PyObject *x = 0, *l = 0;
+  PyObject *x = 0, *l = 0, *i = 0;
   keyszset_pyobj *o = 0;
 
   if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|O:new", KWLIST, &dfl, &set))
     goto end;
-  if (!set) set = PyTuple_New(0);
-  else Py_INCREF(set);
-  if (!PySequence_Check(set)) TYERR("want a sequence");
-  n = PySequence_Size(set); if (n < 0) goto end;
+  if (set) i = PyObject_GetIter(set);
+  else { set = PyTuple_New(0); i = PyObject_GetIter(set); Py_DECREF(set); }
+  if (!i) goto end;
   l = PyList_New(0); if (!l) goto end;
   if (dfl < 0) VALERR("key size cannot be negative");
-  x = PyInt_FromLong(dfl);
-  PyList_Append(l, x);
-  Py_DECREF(x);
-  x = 0;
-  for (i = 0; i < n; i++) {
-    if ((x = PySequence_GetItem(set, i)) == 0) goto end;
-    xx = PyInt_AsLong(x);
-    if (PyErr_Occurred()) goto end;
-    if (xx == dfl) continue;
+  x = PyInt_FromLong(dfl); PyList_Append(l, x); Py_DECREF(x); x = 0;
+  for (;;) {
+    x = PyIter_Next(i); if (!x) break;
+    xx = PyInt_AsLong(x); if (xx == -1 && PyErr_Occurred()) goto end;
     if (xx < 0) VALERR("key size cannot be negative");
-    PyList_Append(l, x);
-    Py_DECREF(x);
-    x = 0;
+    PyList_Append(l, x); Py_DECREF(x); x = 0;
   }
-  Py_DECREF(set);
-  if ((set = PySequence_Tuple(l)) == 0) goto end;
+  if ((set = PyFrozenSet_New(l)) == 0) goto end;
   o = (keyszset_pyobj *)ty->tp_alloc(ty, 0);
   o->dfl = dfl;
   o->set = set;
-  Py_INCREF(set);
 end:
-  Py_XDECREF(set);
   Py_XDECREF(l);
   Py_XDECREF(x);
   return ((PyObject *)o);
@@ -187,29 +184,43 @@ end:
 
 static PyObject *kaget_min(PyObject *me, void *hunoz)
   { return (PyInt_FromLong(0)); }
-#define kaget_max kaget_min
+static PyObject *kaget_max(PyObject *me, void *hunoz)
+  { RETURN_NONE; }
+
+static PyObject *krget_max(PyObject *me, void *hunoz)
+{
+  int max = ((keyszrange_pyobj *)me)->max;
+  if (max) return (PyInt_FromLong(max));
+  else RETURN_NONE;
+}
 
 static PyObject *ksget_min(PyObject *me, void *hunoz)
 {
-  PyObject *set = ((keyszset_pyobj *)me)->set;
-  int i, n, y, x = -1;
-  n = PyTuple_GET_SIZE(set);
-  for (i = 0; i < n; i++) {
-    y = PyInt_AS_LONG(PyTuple_GET_ITEM(set, i));
+  PyObject *i = PyObject_GetIter(((keyszset_pyobj *)me)->set);
+  PyObject *v = 0;
+  int y, x = -1;
+  for (;;) {
+    v = PyIter_Next(i); if (!v) break;
+    y = PyInt_AsLong(v); assert(y >= 0);
     if (x == -1 || y < x) x = y;
   }
+  Py_DECREF(i); Py_XDECREF(v);
+  if (PyErr_Occurred()) return (0);
   return (PyInt_FromLong(x));
 }
 
 static PyObject *ksget_max(PyObject *me, void *hunoz)
 {
-  PyObject *set = ((keyszset_pyobj *)me)->set;
-  int i, n, y, x = -1;
-  n = PyTuple_GET_SIZE(set);
-  for (i = 0; i < n; i++) {
-    y = PyInt_AS_LONG(PyTuple_GET_ITEM(set, i));
+  PyObject *i = PyObject_GetIter(((keyszset_pyobj *)me)->set);
+  PyObject *v = 0;
+  int y, x = -1;
+  for (;;) {
+    v = PyIter_Next(i); if (!v) break;
+    y = PyInt_AsLong(v); assert(y >= 0);
     if (y > x) x = y;
   }
+  Py_DECREF(i); Py_XDECREF(v);
+  if (PyErr_Occurred()) return (0);
   return (PyInt_FromLong(x));
 }
 
@@ -273,13 +284,19 @@ static const PyGetSetDef keyszany_pygetset[] = {
 static const PyMemberDef keyszrange_pymembers[] = {
 #define MEMBERSTRUCT keyszrange_pyobj
   MEMBER(min,  T_INT,    READONLY, "KSZ.min -> smallest allowed key size")
-  MEMBER(max,  T_INT,    READONLY, "KSZ.max -> largest allowed key size")
   MEMBER(mod,  T_INT,    READONLY,
                            "KSZ.mod -> key size must be a multiple of this")
 #undef MEMBERSTRUCT
   { 0 }
 };
 
+static const PyGetSetDef keyszrange_pygetset[] = {
+#define GETSETNAME(op, name) kr##op##_##name
+  GET  (max,           "KSZ.max -> largest allowed key size")
+#undef GETSETNAME
+  { 0 }
+};
+
 static const PyGetSetDef keyszset_pygetset[] = {
 #define GETSETNAME(op, name) ks##op##_##name
   GET  (min,           "KSZ.min -> smallest allowed key size")
@@ -429,7 +446,7 @@ static const PyTypeObject keyszrange_pytype_skel = {
   0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   PYMEMBERS(keyszrange),               /* @tp_members@ */
-  0,                                   /* @tp_getset@ */
+  PYGETSET(keyszrange),                        /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -467,8 +484,8 @@ static const PyTypeObject keyszset_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-  "KeySZSet(DEFAULT, SEQ)\n"
-  "  Key size constraints: size must be DEFAULT or an element of SEQ.",
+  "KeySZSet(DEFAULT, ITER)\n"
+  "  Key size constraints: size must be DEFAULT or an element of ITER.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -1177,7 +1194,7 @@ DOUINTCONV(GAEAMETH_HASHU_)
   {                                                                    \
     struct bin in; octet b[SZ_##W];                                    \
     if (!PyArg_ParseTuple(arg, "O&:hashbuf" #w, convbin, &in)) goto end; \
-    if (in.sz > MASK##n) TYERR("string too long");                     \
+    if (in.sz > MASK##n) VALERR("too large");                          \
     STORE##W(b, in.sz); if (gaeadaad_hash(me, b, sizeof(b))) goto end; \
     if (gaeadaad_hash(me, in.p, in.sz)) goto end;                      \
     RETURN_ME;                                                         \
@@ -2027,7 +2044,7 @@ DOUINTCONV(GHMETH_HASHU_)
   {                                                                    \
     struct bin in;                                                     \
     if (!PyArg_ParseTuple(arg, "O&:hashbuf" #w, convbin, &in)) goto end; \
-    if (in.sz > MASK##n) TYERR("string too long");                     \
+    if (in.sz > MASK##n) VALERR("too large");                          \
     GH_HASHBUF##W(GHASH_H(me), in.p, in.sz);                           \
     RETURN_ME;                                                         \
   end:                                                                 \
@@ -2292,6 +2309,22 @@ static const PyGetSetDef gcmac_pygetset[] = {
   { 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@ */
@@ -2375,7 +2408,7 @@ static const PyTypeObject gmac_pytype_skel = {
   0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  0,                                   /* @tp_getset@ */
+  PYGETSET(gmac),                      /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -2555,7 +2588,7 @@ DOUINTCONV(POLYMETH_HASHU_)
     struct bin in;                                                     \
     octet b[SZ_##W];                                                   \
     if (!PyArg_ParseTuple(arg, "O&:hashbuf" #w, convbin, &in)) goto end; \
-    if (in.sz > MASK##n) TYERR("string too long");                     \
+    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;                                                         \
@@ -2615,6 +2648,20 @@ static const PyGetSetDef poly1305cls_pygetset[] = {
   { 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")
@@ -2636,7 +2683,7 @@ static const PyMethodDef poly1305hash_pymethods[] = {
 
 static const PyTypeObject poly1305cls_pytype_skel = {
   PyVarObject_HEAD_INIT(0, 0)          /* Header */
-  "Poly1305Class",                     /* @tp_name@ */
+  "_Poly1305Class",                    /* @tp_name@ */
   sizeof(PyHeapTypeObject),            /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -2717,7 +2764,7 @@ static const PyTypeObject poly1305key_pytype_skel = {
   0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  0,                                   /* @tp_getset@ */
+  PYGETSET(poly1305),                  /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -2865,6 +2912,27 @@ end:
   return (0);
 }
 
+static PyObject *kxvikmeth_set(PyObject *me, PyObject *arg)
+{
+  kxvik_pyobj *k = (kxvik_pyobj *)me;
+  kludge64 t[25];
+  const octet *q;
+  unsigned i;
+  struct bin in;
+  size_t n;
+
+  if (!PyArg_ParseTuple(arg, "O&:set", 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) VALERR("not 64-bit aligned");
+  keccak1600_set(&k->s, t, i);
+  RETURN_ME;
+end:
+  return (0);
+}
+
 static PyObject *kxvikmeth_extract(PyObject *me, PyObject *arg)
 {
   kxvik_pyobj *k = (kxvik_pyobj *)me;
@@ -2904,6 +2972,7 @@ static const PyMethodDef kxvik_pymethods[] = {
 #define METHNAME(func) kxvikmeth_##func
   NAMETH(copy,         "KECCAK.copy() -> KECCAK'")
   METH (mix,           "KECCAK.mix(DATA)")
+  METH (set,           "KECCAK.set(DATA)")
   METH (extract,       "KECCAK.extract(NOCTETS)")
   NAMETH(step,         "KECCAK.step()")
 #undef METHNAME
@@ -3034,7 +3103,7 @@ DOUINTCONV(SHAKEMETH_HASHU_)
     struct bin in;                                                     \
     octet b[SZ_##W];                                                   \
     if (!PyArg_ParseTuple(arg, "O&:hashbuf" #w, convbin, &in)) goto end; \
-    if (in.sz > MASK##n) TYERR("string too long");                     \
+    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);                              \
@@ -3300,6 +3369,574 @@ static const PyTypeObject shake256_pytype_skel = {
   0                                    /* @tp_is_gc@ */
 };
 
+static PyTypeObject *kmac_pytype, *kmac128_pytype, *kmac256_pytype;
+
+static PyObject *kmac_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;
+  struct bin k = { 0, 0 }, p = { 0, 0 };
+  static const char *const kwlist[] = { "key", "perso", 0 };
+
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O:new", KWLIST,
+                                  convbin, &k, &pobj))
+      goto end;
+  if (pobj != Py_None && !convbin(pobj, &p)) goto end;
+  rc = (shake_pyobj *)ty->tp_alloc(ty, 0);
+  initfn(&rc->h, p.p, p.sz, k.p, k.sz);
+  rc->st = 0;
+end:
+  return ((PyObject *)rc);
+}
+
+static PyObject *kmac128_pynew(PyTypeObject *ty,
+                              PyObject *arg, PyObject *kw)
+  { return (kmac_dopynew(kmac128_init, ty, arg, kw)); }
+
+static PyObject *kmac256_pynew(PyTypeObject *ty,
+                              PyObject *arg, PyObject *kw)
+  { return (kmac_dopynew(kmac256_init, ty, arg, kw)); }
+
+static PyObject *kmacmeth_xof(PyObject *me)
+{
+  if (shake_check(me, 0)) goto end;
+  kmac_xof(SHAKE_H(me));
+  SHAKE_ST(me) = 1;
+  RETURN_ME;
+end:
+  return (0);
+}
+
+static PyObject *kmacmeth_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);
+  kmac_done(SHAKE_H(me), BIN_PTR(rc), n);
+  SHAKE_ST(me) = -1;
+end:
+  return (rc);
+}
+
+static const PyMethodDef kmac_pymethods[] = {
+#define METHNAME(func) kmacmeth_##func
+  NAMETH(xof,          "K.xof()")
+  KWMETH(done,         "K.done([hsz = CAP/2]) -> T")
+#undef METHNAME
+  { 0 }
+};
+
+static const PyTypeObject kmac_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
+  "KMAC",                              /* @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@ */
+  "KMAC base class.",
+
+  0,                                   /* @tp_traverse@ */
+  0,                                   /* @tp_clear@ */
+  0,                                   /* @tp_richcompare@ */
+  0,                                   /* @tp_weaklistoffset@ */
+  0,                                   /* @tp_iter@ */
+  0,                                   /* @tp_iternext@ */
+  PYMETHODS(kmac),                     /* @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 kmac128_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
+  "KMAC128",                           /* @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@ */
+  "KMAC128(KEY, [perso = STR]): KMAC XOMAC.",
+
+  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@ */
+  kmac128_pynew,                       /* @tp_new@ */
+  0,                                   /* @tp_free@ */
+  0                                    /* @tp_is_gc@ */
+};
+
+static const PyTypeObject kmac256_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
+  "KMAC256",                           /* @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@ */
+  "KMAC256(KEY, [perso = STR]): KMAC XOMAC.",
+
+  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@ */
+  kmac256_pynew,                       /* @tp_new@ */
+  0,                                   /* @tp_free@ */
+  0                                    /* @tp_is_gc@ */
+};
+
+static PyTypeObject *strobe_pytype;
+
+typedef struct strobe_pyobj {
+  PyObject_HEAD
+  strobe_ctx ctx;
+} strobe_pyobj;
+#define STROBE_CTX(o) (&((strobe_pyobj *)(o))->ctx)
+
+static int convstrbf(PyObject *x, void *p)
+{
+  unsigned *ff = p, f;
+  const char *q;
+  size_t sz;
+  int rc = 0;
+
+  if (TEXT_CHECK(x)) {
+    TEXT_PTRLEN(x, q, sz); f = 0;
+    while (sz--) switch (*q++) {
+      case 'I': f |= STRBF_I; break;
+      case 'A': f |= STRBF_A; break;
+      case 'C': f |= STRBF_C; break;
+      case 'T': f |= STRBF_T; break;
+      case 'M': f |= STRBF_M; break;
+      default: if (!ISSPACE(q[-1])) VALERR("unknown flag character");
+    }
+    *ff = f; rc = 1;
+  } else if (convuint(x, ff))
+    rc = 1;
+  else {
+    PyErr_Clear();
+    TYERR("expected string or int");
+  }
+end:
+  return (rc);
+}
+
+
+static PyObject *strobe_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
+{
+  strobe_pyobj *rc = 0;
+  unsigned lambda = 128;
+  static const char *const kwlist[] = { "l", 0 };
+
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST,
+                                  convuint, &lambda))
+    goto end;
+  if (lambda%32 || lambda > 704) VALERR("invalid security parameter");
+  rc = (strobe_pyobj *)ty->tp_alloc(ty, 0);
+  strobe_init(&rc->ctx, lambda);
+end:
+  return ((PyObject *)rc);
+}
+
+static PyObject *strobemeth_copy(PyObject *me)
+{
+  strobe_pyobj *strobe = (strobe_pyobj *)me, *rc;
+
+  rc = PyObject_NEW(strobe_pyobj, me->ob_type);
+  rc->ctx = strobe->ctx;
+  return ((PyObject *)rc);
+}
+
+static int strobe_checkinactive(PyObject *me)
+{
+  int rc = -1;
+
+  if (STROBE_CTX(me)->f&STRBF_ACTIVE) VALERR("operation already active");
+  rc = 0;
+end:
+  return (rc);
+}
+
+static int strobe_checkactive(PyObject *me)
+{
+  int rc = -1;
+
+  if (!(STROBE_CTX(me)->f&STRBF_ACTIVE)) VALERR("operation not active");
+  rc = 0;
+end:
+  return (rc);
+}
+
+static PyObject *strobemeth_begin(PyObject *me, PyObject *arg)
+{
+  unsigned f;
+
+  if (!PyArg_ParseTuple(arg, "O&:begin", convstrbf, &f)) goto end;
+  if (strobe_checkinactive(me)) goto end;
+  if (f&~STRBF_VALIDMASK) VALERR("bad flags");
+  strobe_begin(STROBE_CTX(me), f);
+  RETURN_ME;
+end:
+  return (0);
+}
+
+static PyObject *strobe_pyprocess(PyObject *me,
+                                 const void *p0, size_t sz0,
+                                 const void *p1, size_t sz1)
+{
+  char *q;
+  PyObject *rc = 0;
+
+  if (strobe_checkactive(me)) goto end;
+  if (!(STROBE_CTX(me)->f&STRBF_WANTIN) && p0)
+    VALERR("no input expected for current operation");
+  if (!(STROBE_CTX(me)->f&STRBF_WANTOUT)) q = 0;
+  else { rc = bytestring_pywrap(0, sz0 + sz1); q = BIN_PTR(rc); }
+  strobe_process(STROBE_CTX(me), p0, q, sz0);
+  if (sz1) strobe_process(STROBE_CTX(me), p1, q ? q + sz0 : 0, sz1);
+  if (!q) RETURN_ME;
+end:
+  return (rc);
+}
+
+static PyObject *strobemeth_process(PyObject *me, PyObject *arg)
+{
+  PyObject *inobj;
+  struct bin in;
+  PyObject *rc = 0;
+
+  in.p = 0;
+  if (!PyArg_ParseTuple(arg, "O:process", &inobj)) goto end;
+  if (!convszt(inobj, &in.sz) && (PyErr_Clear(), !convbin(inobj, &in))) {
+    PyErr_Clear();
+    TYERR("expected integer, string, unicode, or single-segment buffer");
+  }
+  rc = strobe_pyprocess(me, in.p, in.sz, 0, 0);
+end:
+  return (rc);
+}
+
+static PyObject *strobemeth_done(PyObject *me)
+{
+  unsigned f;
+  int rc;
+
+  if (strobe_checkactive(me)) return (0);
+  f = STROBE_CTX(me)->f; rc = strobe_done(STROBE_CTX(me));
+  if (f&STRBF_VRFOUT) return (getbool(!rc));
+  else RETURN_ME;
+}
+
+#define STROBEMETH_PROCESSU_(n, W, w)                                  \
+  static PyObject *strobemeth_processu##w(PyObject *me, PyObject *arg) \
+  {                                                                    \
+    uint##n x;                                                         \
+    octet b[SZ_##W];                                                   \
+    if (!PyArg_ParseTuple(arg, "O&:processu" #w, convu##n, &x)) return (0); \
+    STORE##W(b, x); return (strobe_pyprocess(me, b, sizeof(b), 0, 0)); \
+  }
+DOUINTCONV(STROBEMETH_PROCESSU_)
+
+#define STROBEMETH_PROCESSBUF_(n, W, w)                                        \
+  static PyObject *strobemeth_processbuf##w(PyObject *me, PyObject *arg) \
+  {                                                                    \
+    struct bin in;                                                     \
+    octet b[SZ_##W];                                                   \
+    if (!PyArg_ParseTuple(arg, "O&:processbuf" #w, convbin, &in)) goto end; \
+    if (in.sz > MASK##n) VALERR("too large");                          \
+    STORE##W(b, in.sz);                                                        \
+    return (strobe_pyprocess(me, b, sizeof(b), in.p, in.sz));          \
+  end:                                                                 \
+    return (0);                                                                \
+  }
+DOUINTCONV(STROBEMETH_PROCESSBUF_)
+
+static PyObject *strobemeth_processstrz(PyObject *me, PyObject *arg)
+{
+  char *p;
+  if (!PyArg_ParseTuple(arg, "s:processstrz", &p)) return (0);
+  return (strobe_pyprocess(me, p, strlen(p) + 1, 0, 0));
+}
+
+#define STROBEMETH_INONLY_(meth, arg)                                  \
+  static PyObject *strobemeth_##meth(PyObject *me,                     \
+                                    PyObject *arg, PyObject *kw)       \
+  {                                                                    \
+    struct bin in; unsigned f = 0;                                     \
+    static const char *const kwlist[] = { #arg, "f", 0 };              \
+    if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:" #meth, KWLIST,  \
+                                    convbin, &in, convstrbf, &f))      \
+      goto end;                                                                \
+    if (f&~STRBF_M) VALERR("bad flags");                               \
+    if (strobe_checkinactive(me)) goto end;                            \
+    strobe_##meth(STROBE_CTX(me), f, in.p, in.sz);                     \
+    RETURN_ME;                                                         \
+  end:                                                                 \
+    return (0);                                                                \
+  }
+
+#define STROBEMETH_OUTONLY_(meth)                                      \
+  static PyObject *strobemeth_##meth(PyObject *me,                     \
+                                    PyObject *arg, PyObject *kw)       \
+  {                                                                    \
+    size_t sz; unsigned f = 0; PyObject *rc;                           \
+    static const char *const kwlist[] = { "sz", "f", 0 };              \
+    if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:" #meth, KWLIST,  \
+                         convszt, &sz, convstrbf, &f))                 \
+      goto end;                                                                \
+    if (f&~STRBF_M) VALERR("bad flags");                               \
+    if (strobe_checkinactive(me)) goto end;                            \
+    rc = bytestring_pywrap(0, sz);                                     \
+    strobe_##meth(STROBE_CTX(me), f, BIN_PTR(rc), sz);                 \
+    return (rc);                                                       \
+  end:                                                                 \
+    return (0);                                                                \
+  }
+
+#define STROBEMETH_INOUT_(meth, arg)                                   \
+  static PyObject *strobemeth_##meth(PyObject *me,                     \
+                                    PyObject *arg, PyObject *kw)       \
+  {                                                                    \
+    struct bin in; unsigned f = 0; PyObject *rc;                       \
+    static const char *const kwlist[] = { #arg, "f", 0 };              \
+    if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:" #meth, KWLIST,  \
+                                    convbin, &in, convstrbf, &f))      \
+      goto end;                                                                \
+    if (f&~STRBF_M) VALERR("bad flags");                               \
+    if (strobe_checkinactive(me)) goto end;                            \
+    rc = bytestring_pywrap(0, in.sz);                                  \
+    strobe_##meth(STROBE_CTX(me), f, in.p, BIN_PTR(rc), in.sz);                \
+    return (rc);                                                       \
+  end:                                                                 \
+    return (0);                                                                \
+  }
+
+STROBEMETH_INONLY_(key, key)
+STROBEMETH_INONLY_(ad, msg)
+STROBEMETH_OUTONLY_(prf)
+STROBEMETH_INONLY_(clrout, msg)
+STROBEMETH_INONLY_(clrin, msg)
+STROBEMETH_INOUT_(encout, msg)
+STROBEMETH_INOUT_(encin, ct)
+STROBEMETH_OUTONLY_(macout)
+
+static PyObject *strobemeth_macin(PyObject *me, PyObject *arg, PyObject *kw)
+{
+  struct bin in; unsigned f = 0;
+  static const char *const kwlist[] = { "tag", "f", 0 };
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:macin", KWLIST,
+                                  convbin, &in, convstrbf, &f))
+    goto end;
+  if (f&~STRBF_M) VALERR("bad flags");
+  if (strobe_checkinactive(me)) goto end;
+  return (getbool(!strobe_macin(STROBE_CTX(me), f, in.p, in.sz)));
+end:
+  return (0);
+}
+
+static PyObject *strobemeth_ratchet(PyObject *me,
+                                   PyObject *arg, PyObject *kw)
+{
+  size_t sz; unsigned f = 0;
+  static const char *const kwlist[] = { "sz", "f", 0 };
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:ratchet", KWLIST,
+                                  convszt, &sz, convstrbf, &f))
+    goto end;
+  if (f&~STRBF_M) VALERR("bad flags");
+  if (strobe_checkinactive(me)) goto end;
+  strobe_ratchet(STROBE_CTX(me), f, sz); RETURN_ME;
+end:
+  return (0);
+}
+
+static PyObject *strobeget_l(PyObject *me, void *hunoz)
+  { return (PyInt_FromLong(4*(200 - STROBE_CTX(me)->r))); }
+
+static PyObject *strobeget_role(PyObject *me, void *hunoz)
+  { return (PyInt_FromLong(STROBE_CTX(me)->f&STROBE_ROLEMASK)); }
+
+static PyObject *strobeget_activep(PyObject *me, void *hunoz)
+  { return (getbool(STROBE_CTX(me)->f&STRBF_ACTIVE)); }
+
+static const PyGetSetDef strobe_pygetset[] = {
+#define GETSETNAME(op, name) strobe##op##_##name
+  GET   (l,            "STROBE.l -> security parameter")
+  GET  (role,          "STROBE.role -> STRBRL_...")
+  GET   (activep,      "STROBE.activep -> operation active?")
+#undef GETSETNAME
+  { 0 }
+};
+
+static const PyMethodDef strobe_pymethods[] = {
+#define METHNAME(func) strobemeth_##func
+  NAMETH(copy,         "S.copy() -> SS")
+  METH (begin,         "S.begin(MSGTY)")
+  METH (process,       "S.process(MSG | SZ) [-> OUT]")
+#define METHU_(n, W, w)                                                        \
+    METH(processu##w,  "S.processu" #w "(WORD) [-> OUT]")
+  DOUINTCONV(METHU_)
+#undef METHU_
+#define METHBUF_(n, W, w)                                              \
+    METH(processbuf##w,        "S.processbuf" #w "(BYTES) [-> OUT]")
+  DOUINTCONV(METHBUF_)
+#undef METHBUF_
+  METH (processstrz,   "S.processstrz(STRING) [-> OUT]")
+  NAMETH(done,         "S.done() [-> OK?]")
+  KWMETH(key,          "S.key(KEY, [f = 0])")
+  KWMETH(ad,           "S.ad(MSG, [f = 0])")
+  KWMETH(prf,          "S.prf(SZ, [f = 0]) -> BYTES")
+  KWMETH(clrout,       "S.clrout(MSG, [f = 0])")
+  KWMETH(clrin,                "S.clrin(MSG, [f = 0])")
+  KWMETH(encout,       "S.encout(MSG, [f = 0]) -> CT")
+  KWMETH(encin,                "S.encin(CT, [f = 0]) -> MSG")
+  KWMETH(macout,       "S.macout(SZ, [f = 0]) -> TAG")
+  KWMETH(macin,                "S.macin(TAG, [f = 0]) -> OK?")
+  KWMETH(ratchet,      "S.ratchet(SZ, [f = 0])")
+#undef METHNAME
+  { 0 }
+};
+
+static const PyTypeObject strobe_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
+  "Strobe",                            /* @tp_name@ */
+  sizeof(strobe_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@ */
+"STROBE symmetric-crypto framework.",
+
+  0,                                   /* @tp_traverse@ */
+  0,                                   /* @tp_clear@ */
+  0,                                   /* @tp_richcompare@ */
+  0,                                   /* @tp_weaklistoffset@ */
+  0,                                   /* @tp_iter@ */
+  0,                                   /* @tp_iternext@ */
+  PYMETHODS(strobe),                   /* @tp_methods@ */
+  0,                                   /* @tp_members@ */
+  PYGETSET(strobe),                    /* @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@ */
+  strobe_pynew,                                /* @tp_new@ */
+  0,                                   /* @tp_free@ */
+  0                                    /* @tp_is_gc@ */
+};
+
 /*----- Pseudorandom permutations -----------------------------------------*/
 
 static PyTypeObject *gcprp_pytype, *gprp_pytype;
@@ -3370,8 +4007,7 @@ static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
   me = (PyObject *)ty->tp_alloc(ty, 0);
   GPRP_PRP(me) = prp;
   prp->init(GPRP_CTX(me), k.p, k.sz);
-  Py_INCREF(me);
-  return (me);
+  RETURN_ME;
 end:
   return (0);
 }
@@ -3547,6 +4183,11 @@ static const PyTypeObject gprp_pytype_skel = {
 static const struct nameval consts[] = {
   CONST(AEADF_PCHSZ), CONST(AEADF_PCMSZ), CONST(AEADF_PCTSZ),
   CONST(AEADF_AADNDEP), CONST(AEADF_AADFIRST), CONST(AEADF_NOAAD),
+  CONST(STRBF_I), CONST(STRBF_A), CONST(STRBF_C), CONST(STRBF_T),
+  CONST(STRBF_M), CONST(STROBE_KEY), CONST(STROBE_AD), CONST(STROBE_PRF),
+  CONST(STROBE_CLROUT), CONST(STROBE_CLRIN), CONST(STROBE_ENCOUT),
+  CONST(STROBE_ENCIN), CONST(STRBRL_UNDCD), CONST(STRBRL_INIT),
+  CONST(STRBRL_RESP),
   { 0 }
 };
 
@@ -3593,6 +4234,10 @@ void algorithms_pyinit(void)
   INITTYPE(shake, root);
   INITTYPE(shake128, shake);
   INITTYPE(shake256, shake);
+  INITTYPE(kmac, shake);
+  INITTYPE(kmac128, kmac);
+  INITTYPE(kmac256, kmac);
+  INITTYPE(strobe, root);
   INITTYPE(gcprp, type);
   INITTYPE(gprp, root);
   addmethods(methods);
@@ -3639,13 +4284,17 @@ void algorithms_pyinsert(PyObject *mod)
   INSERT("GMACHash", gmhash_pytype);
   INSERT("gcmacs", make_algtab(gmactab, sizeof(gcmac *),
                               mac_namefn, mac_valfn));
-  INSERT("Poly1305Class", poly1305cls_pytype);
+  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("KMAC", kmac_pytype);
+  INSERT("KMAC128", kmac128_pytype);
+  INSERT("KMAC256", kmac256_pytype);
+  INSERT("Strobe", strobe_pytype);
   INSERT("GCPRP", gcprp_pytype);
   INSERT("GPRP", gprp_pytype);
   INSERT("gcprps", make_algtab(gprptab, sizeof(gcprp *),