catacomb.c (mexp_common): Accept an arbitrary iterable.
[catacomb-python] / algorithms.c
index a195de9..48bd480 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:
@@ -152,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);
@@ -206,25 +196,31 @@ static PyObject *krget_max(PyObject *me, void *hunoz)
 
 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));
 }
 
@@ -488,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@ */
@@ -1198,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;                                                         \
@@ -2048,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:                                                                 \
@@ -2313,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@ */
@@ -2396,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@ */
@@ -2576,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;                                                         \
@@ -2636,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")
@@ -2657,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@ */
 
@@ -2738,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@ */
@@ -3055,7 +3081,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);                              \
@@ -3321,6 +3347,215 @@ 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@ */
+};
+
 /*----- Pseudorandom permutations -----------------------------------------*/
 
 static PyTypeObject *gcprp_pytype, *gprp_pytype;
@@ -3391,8 +3626,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);
 }
@@ -3614,6 +3848,9 @@ void algorithms_pyinit(void)
   INITTYPE(shake, root);
   INITTYPE(shake128, shake);
   INITTYPE(shake256, shake);
+  INITTYPE(kmac, shake);
+  INITTYPE(kmac128, kmac);
+  INITTYPE(kmac256, kmac);
   INITTYPE(gcprp, type);
   INITTYPE(gprp, root);
   addmethods(methods);
@@ -3660,13 +3897,16 @@ 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("GCPRP", gcprp_pytype);
   INSERT("GPRP", gprp_pytype);
   INSERT("gcprps", make_algtab(gprptab, sizeof(gcprp *),