chiark / gitweb /
Overhaul build system using new `cfd' machinery.
[catacomb-python] / algorithms.c
index 6eda8029c92a3e7d5201924dc27824b8c4301a98..16d426d0f8a5b0f3e7a6bf33edc35def295e2dde 100644 (file)
@@ -1,13 +1,11 @@
 /* -*-c-*-
- *
- * $Id$
  *
  * Symmetric cryptography
  *
  * (c) 2004 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of the Python interface to Catacomb.
  *
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * Catacomb/Python is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with Catacomb/Python; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
@@ -47,7 +45,7 @@ PyObject *keysz_pywrap(const octet *k)
     } break;
     case KSZ_RANGE: {
       keyszrange_pyobj *o =
-        PyObject_New(keyszrange_pyobj, keyszrange_pytype);
+       PyObject_New(keyszrange_pyobj, keyszrange_pytype);
       o->dfl = k[1];
       o->min = k[2];
       o->max = k[3];
@@ -57,7 +55,7 @@ PyObject *keysz_pywrap(const octet *k)
     } break;
     case KSZ_SET: {
       keyszset_pyobj *o =
-        PyObject_New(keyszset_pyobj, keyszset_pytype);
+       PyObject_New(keyszset_pyobj, keyszset_pytype);
       int i, n;
       o->dfl = k[1];
       for (i = 0; k[i + 1]; i++) ;
@@ -145,7 +143,7 @@ static PyObject *keyszset_pynew(PyTypeObject *ty,
     if (xx < 0) VALERR("key size cannot be negative");
     PyList_Append(l, x);
     Py_DECREF(x);
-    x = 0;    
+    x = 0;
   }
   Py_DECREF(set);
   if ((set = PySequence_Tuple(l)) == 0) goto end;
@@ -231,12 +229,12 @@ static PyMemberDef keyszset_pymembers[] = {
 };
 
 static PyTypeObject keysz_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+  PyObject_HEAD_INIT(0) 0,             /* Header */
   "catacomb.KeySZ",                    /* @tp_name@ */
   sizeof(keysz_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
-  _PyObject_Del,                       /* @tp_dealloc@ */
+  0,                                   /* @tp_dealloc@ */
   0,                                   /* @tp_print@ */
   0,                                   /* @tp_getattr@ */
   0,                                   /* @tp_setattr@ */
@@ -262,7 +260,7 @@ static PyTypeObject keysz_pytype_skel = {
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
+  0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   keysz_pymembers,                     /* @tp_members@ */
   0,                                   /* @tp_getset@ */
@@ -274,17 +272,17 @@ static PyTypeObject keysz_pytype_skel = {
   0,                                   /* @tp_init@ */
   PyType_GenericAlloc,                 /* @tp_alloc@ */
   abstract_pynew,                      /* @tp_new@ */
-  _PyObject_Del,                       /* @tp_free@ */
+  0,                                   /* @tp_free@ */
   0                                    /* @tp_is_gc@ */
 };
 
 static PyTypeObject keyszany_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+  PyObject_HEAD_INIT(0) 0,             /* Header */
   "catacomb.KeySZAny",                 /* @tp_name@ */
   sizeof(keysz_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
-  _PyObject_Del,                       /* @tp_dealloc@ */
+  0,                                   /* @tp_dealloc@ */
   0,                                   /* @tp_print@ */
   0,                                   /* @tp_getattr@ */
   0,                                   /* @tp_setattr@ */
@@ -310,7 +308,7 @@ static PyTypeObject keyszany_pytype_skel = {
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
+  0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
   keyszany_pygetset,                   /* @tp_getset@ */
@@ -322,17 +320,17 @@ static PyTypeObject keyszany_pytype_skel = {
   0,                                   /* @tp_init@ */
   PyType_GenericAlloc,                 /* @tp_alloc@ */
   keyszany_pynew,                      /* @tp_new@ */
-  _PyObject_Del,                       /* @tp_free@ */
+  0,                                   /* @tp_free@ */
   0                                    /* @tp_is_gc@ */
 };
 
 static PyTypeObject keyszrange_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+  PyObject_HEAD_INIT(0) 0,             /* Header */
   "catacomb.KeySZRange",               /* @tp_name@ */
   sizeof(keyszrange_pyobj),            /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
-  _PyObject_Del,                       /* @tp_dealloc@ */
+  0,                                   /* @tp_dealloc@ */
   0,                                   /* @tp_print@ */
   0,                                   /* @tp_getattr@ */
   0,                                   /* @tp_setattr@ */
@@ -359,7 +357,7 @@ sizes, and requires the key length to be a multiple of some value.",
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
+  0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   keyszrange_pymembers,                        /* @tp_members@ */
   0,                                   /* @tp_getset@ */
@@ -371,17 +369,17 @@ sizes, and requires the key length to be a multiple of some value.",
   0,                                   /* @tp_init@ */
   PyType_GenericAlloc,                 /* @tp_alloc@ */
   keyszrange_pynew,                    /* @tp_new@ */
-  _PyObject_Del,                       /* @tp_free@ */
+  0,                                   /* @tp_free@ */
   0                                    /* @tp_is_gc@ */
 };
 
 static PyTypeObject keyszset_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+  PyObject_HEAD_INIT(0) 0,             /* Header */
   "catacomb.KeySZSet",                 /* @tp_name@ */
   sizeof(keyszset_pyobj),              /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
-  _PyObject_Del,                       /* @tp_dealloc@ */
+  0,                                   /* @tp_dealloc@ */
   0,                                   /* @tp_print@ */
   0,                                   /* @tp_getattr@ */
   0,                                   /* @tp_setattr@ */
@@ -408,7 +406,7 @@ few listed sizes.",
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
+  0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   keyszset_pymembers,                  /* @tp_members@ */
   keyszset_pygetset,                   /* @tp_getset@ */
@@ -420,7 +418,7 @@ few listed sizes.",
   0,                                   /* @tp_init@ */
   PyType_GenericAlloc,                 /* @tp_alloc@ */
   keyszset_pynew,                      /* @tp_new@ */
-  _PyObject_Del,                       /* @tp_free@ */
+  0,                                   /* @tp_free@ */
   0                                    /* @tp_is_gc@ */
 };
 
@@ -439,7 +437,7 @@ PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c, unsigned f)
   g = PyObject_NEW(gcipher_pyobj, (PyTypeObject *)cobj);
   g->c = c;
   g->f = f;
-  return ((PyObject *)g);  
+  return ((PyObject *)g);
 }
 
 static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
@@ -455,24 +453,23 @@ static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
                         GC_INIT(GCCIPHER_CC(ty), k, sz),
                         f_freeme));
 end:
-  return (0);  
+  return (0);
 }
 
 PyObject *gccipher_pywrap(gccipher *cc)
 {
-  gccipher_pyobj *g = newtype(gccipher_pytype, 0);
+  gccipher_pyobj *g = newtype(gccipher_pytype, 0, cc->name);
   g->cc = cc;
-  g->ty.tp_name = (/*unconst*/ char *)cc->name;
-  g->ty.tp_basicsize = sizeof(gcipher_pyobj);
-  g->ty.tp_base = gcipher_pytype;
+  g->ty.ht_type.tp_basicsize = sizeof(gcipher_pyobj);
+  g->ty.ht_type.tp_base = gcipher_pytype;
   Py_INCREF(gcipher_pytype);
-  g->ty.tp_flags = (Py_TPFLAGS_DEFAULT |
-                   Py_TPFLAGS_BASETYPE |
-                   Py_TPFLAGS_HEAPTYPE);
-  g->ty.tp_alloc = PyType_GenericAlloc;
-  g->ty.tp_free = _PyObject_Del;
-  g->ty.tp_new = gcipher_pynew;
-  PyType_Ready(&g->ty);
+  g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
+                           Py_TPFLAGS_BASETYPE |
+                           Py_TPFLAGS_HEAPTYPE);
+  g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
+  g->ty.ht_type.tp_free = 0;
+  g->ty.ht_type.tp_new = gcipher_pynew;
+  PyType_Ready(&g->ty.ht_type);
   return ((PyObject *)g);
 }
 
@@ -481,7 +478,7 @@ static void gcipher_pydealloc(PyObject *me)
   if (GCIPHER_F(me) & f_freeme)
     GC_DESTROY(GCIPHER_C(me));
   Py_DECREF(me->ob_type);
-  PyObject_DEL(me);
+  FREEOBJ(me);
 }
 
 static PyObject *gccget_name(PyObject *me, void *hunoz)
@@ -591,7 +588,7 @@ static PyMethodDef gcipher_pymethods[] = {
 };
 
 static PyTypeObject gccipher_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+  PyObject_HEAD_INIT(0) 0,             /* Header */
   "catacomb.GCCipher",                 /* @tp_name@ */
   sizeof(gccipher_pyobj),              /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
@@ -622,7 +619,7 @@ static PyTypeObject gccipher_pytype_skel = {
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
+  0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
   gccipher_pygetset,                   /* @tp_getset@ */
@@ -634,12 +631,12 @@ static PyTypeObject gccipher_pytype_skel = {
   0,                                   /* @tp_init@ */
   PyType_GenericAlloc,                 /* @tp_alloc@ */
   abstract_pynew,                      /* @tp_new@ */
-  _PyObject_Del,                       /* @tp_free@ */
+  0,                                   /* @tp_free@ */
   0                                    /* @tp_is_gc@ */
 };
 
 static PyTypeObject gcipher_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+  PyObject_HEAD_INIT(0) 0,             /* Header */
   "catacomb.GCipher",                  /* @tp_name@ */
   sizeof(gcipher_pyobj),               /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
@@ -670,7 +667,7 @@ static PyTypeObject gcipher_pytype_skel = {
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
+  0,                                   /* @tp_iternext@ */
   gcipher_pymethods,                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
   0,                                   /* @tp_getset@ */
@@ -682,7 +679,7 @@ static PyTypeObject gcipher_pytype_skel = {
   0,                                   /* @tp_init@ */
   PyType_GenericAlloc,                 /* @tp_alloc@ */
   abstract_pynew,                      /* @tp_new@ */
-  _PyObject_Del,                       /* @tp_free@ */
+  0,                                   /* @tp_free@ */
   0                                    /* @tp_is_gc@ */
 };
 
@@ -700,24 +697,23 @@ static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
     goto end;
   return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty)), f_freeme));
 end:
-  return (0);  
+  return (0);
 }
 
 PyObject *gchash_pywrap(gchash *ch)
 {
-  gchash_pyobj *g = newtype(gchash_pytype, 0);
+  gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name);
   g->ch = ch;
-  g->ty.tp_name = (/*unconst*/ char *)ch->name;
-  g->ty.tp_basicsize = sizeof(ghash_pyobj);
-  g->ty.tp_base = ghash_pytype;
+  g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
+  g->ty.ht_type.tp_base = ghash_pytype;
   Py_INCREF(ghash_pytype);
-  g->ty.tp_flags = (Py_TPFLAGS_DEFAULT |
-                   Py_TPFLAGS_BASETYPE |
-                   Py_TPFLAGS_HEAPTYPE);
-  g->ty.tp_alloc = PyType_GenericAlloc;
-  g->ty.tp_free = _PyObject_Del;
-  g->ty.tp_new = ghash_pynew;
-  PyType_Ready(&g->ty);
+  g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
+                           Py_TPFLAGS_BASETYPE |
+                           Py_TPFLAGS_HEAPTYPE);
+  g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
+  g->ty.ht_type.tp_free = 0;
+  g->ty.ht_type.tp_new = ghash_pynew;
+  PyType_Ready(&g->ty.ht_type);
   return ((PyObject *)g);
 }
 
@@ -729,7 +725,7 @@ PyObject *ghash_pywrap(PyObject *cobj, ghash *h, unsigned f)
   g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj);
   g->h = h;
   g->f = f;
-  return ((PyObject *)g);  
+  return ((PyObject *)g);
 }
 
 static void ghash_pydealloc(PyObject *me)
@@ -737,7 +733,7 @@ static void ghash_pydealloc(PyObject *me)
   if (GHASH_F(me) & f_freeme)
     GH_DESTROY(GHASH_H(me));
   Py_DECREF(me->ob_type);
-  PyObject_DEL(me);
+  FREEOBJ(me);
 }
 
 static PyObject *gchget_name(PyObject *me, void *hunoz)
@@ -779,16 +775,55 @@ static PyGetSetDef gchash_pygetset[] = {
   { 0 }
 };
 
+#define GHMETH_HASHU_(n, W, w)                                         \
+  static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg)                \
+  {                                                                    \
+    uint##n x;                                                         \
+    if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
+    GH_HASHU##W(GHASH_H(me), x);                                       \
+    RETURN_ME;                                                         \
+  end:                                                                 \
+    return (0);                                                                \
+  }
+DOUINTCONV(GHMETH_HASHU_)
+
+#define GHMETH_HASHBUF_(n, W, w)                                       \
+  static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg)      \
+  {                                                                    \
+    char *p;                                                           \
+    int sz;                                                            \
+    if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end;    \
+    if (sz > MASK##n) TYERR("string too long");                                \
+    GH_HASHBUF##W(GHASH_H(me), p, sz);                                 \
+    RETURN_ME;                                                         \
+  end:                                                                 \
+    return (0);                                                                \
+  }
+DOUINTCONV(GHMETH_HASHBUF_)
+
+static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg)
+{
+  char *p;
+  if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
+  GH_HASHSTRZ(GHASH_H(me), p);
+  RETURN_ME;
+}
+
 static PyMethodDef ghash_pymethods[] = {
 #define METHNAME(name) ghmeth_##name
   METH (hash,                  "H.hash(M)")
+#define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
+  DOUINTCONV(METHU_)
+#define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
+  DOUINTCONV(METHBUF_)
+  METH (hashstrz,              "H.hashstrz(STRING)")
   METH (done,                  "H.done() -> HASH")
 #undef METHNAME
   { 0 }
 };
 
 static PyTypeObject gchash_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+  PyObject_HEAD_INIT(0) 0,             /* Header */
   "catacomb.GCHash",                   /* @tp_name@ */
   sizeof(gchash_pyobj),                        /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
@@ -819,7 +854,7 @@ static PyTypeObject gchash_pytype_skel = {
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
+  0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
   gchash_pygetset,                     /* @tp_getset@ */
@@ -831,12 +866,12 @@ static PyTypeObject gchash_pytype_skel = {
   0,                                   /* @tp_init@ */
   PyType_GenericAlloc,                 /* @tp_alloc@ */
   abstract_pynew,                      /* @tp_new@ */
-  _PyObject_Del,                       /* @tp_free@ */
+  0,                                   /* @tp_free@ */
   0                                    /* @tp_is_gc@ */
 };
 
 static PyTypeObject ghash_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+  PyObject_HEAD_INIT(0) 0,             /* Header */
   "catacomb.GHash",                    /* @tp_name@ */
   sizeof(ghash_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
@@ -867,7 +902,7 @@ static PyTypeObject ghash_pytype_skel = {
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
+  0,                                   /* @tp_iternext@ */
   ghash_pymethods,                     /* @tp_methods@ */
   0,                                   /* @tp_members@ */
   0,                                   /* @tp_getset@ */
@@ -879,7 +914,7 @@ static PyTypeObject ghash_pytype_skel = {
   0,                                   /* @tp_init@ */
   PyType_GenericAlloc,                 /* @tp_alloc@ */
   abstract_pynew,                      /* @tp_new@ */
-  _PyObject_Del,                       /* @tp_free@ */
+  0,                                   /* @tp_free@ */
   0                                    /* @tp_is_gc@ */
 };
 
@@ -904,7 +939,7 @@ static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
                      GM_KEY(GCMAC_CM(ty), k, sz),
                      f_freeme));
 end:
-  return (0);  
+  return (0);
 }
 
 static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
@@ -922,19 +957,18 @@ static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
 
 PyObject *gcmac_pywrap(gcmac *cm)
 {
-  gcmac_pyobj *g = newtype(gcmac_pytype, 0);
+  gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name);
   g->cm = cm;
-  g->ty.tp_name = (/*unconst*/ char *)cm->name;
-  g->ty.tp_basicsize = sizeof(gmac_pyobj);
-  g->ty.tp_base = gmac_pytype;
+  g->ty.ht_type.tp_basicsize = sizeof(gmac_pyobj);
+  g->ty.ht_type.tp_base = gmac_pytype;
   Py_INCREF(gmac_pytype);
-  g->ty.tp_flags = (Py_TPFLAGS_DEFAULT |
-                   Py_TPFLAGS_BASETYPE |
-                   Py_TPFLAGS_HEAPTYPE);
-  g->ty.tp_alloc = PyType_GenericAlloc;
-  g->ty.tp_free = _PyObject_Del;
-  g->ty.tp_new = gmac_pynew;
-  PyType_Ready(&g->ty);
+  g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
+                           Py_TPFLAGS_BASETYPE |
+                           Py_TPFLAGS_HEAPTYPE);
+  g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
+  g->ty.ht_type.tp_free = 0;
+  g->ty.ht_type.tp_new = gmac_pynew;
+  PyType_Ready(&g->ty.ht_type);
   return ((PyObject *)g);
 }
 
@@ -943,21 +977,21 @@ PyObject *gmac_pywrap(PyObject *cobj, gmac *m, unsigned f)
   gmac_pyobj *g;
   if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m));
   else Py_INCREF(cobj);
-  g = newtype((PyTypeObject *)cobj, 0);
-  g->nameobj = PyString_FromFormat("%s(keyed)", m->ops->c->name);
-  g->ty.tp_name = PyString_AS_STRING(g->nameobj);
-  g->ty.tp_base = gmhash_pytype;
+  g = newtype((PyTypeObject *)cobj, 0, 0);
+  g->ty.ht_name = PyString_FromFormat("%s(keyed)", m->ops->c->name);
+  g->ty.ht_type.tp_name = PyString_AS_STRING(g->ty.ht_name);
+  g->ty.ht_type.tp_base = gmhash_pytype;
   Py_INCREF(gmac_pytype);
-  g->ty.tp_flags = (Py_TPFLAGS_DEFAULT |
-                   Py_TPFLAGS_BASETYPE |
-                   Py_TPFLAGS_HEAPTYPE);
-  g->ty.tp_alloc = PyType_GenericAlloc;
-  g->ty.tp_free = _PyObject_Del;
-  g->ty.tp_new = gmhash_pynew;
-  PyType_Ready(&g->ty);
+  g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
+                           Py_TPFLAGS_BASETYPE |
+                           Py_TPFLAGS_HEAPTYPE);
+  g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
+  g->ty.ht_type.tp_free = 0;
+  g->ty.ht_type.tp_new = gmhash_pynew;
+  PyType_Ready(&g->ty.ht_type);
   g->m = m;
   g->f = f;
-  return ((PyObject *)g);  
+  return ((PyObject *)g);
 }
 
 static void gmac_pydealloc(PyObject *me)
@@ -965,7 +999,6 @@ static void gmac_pydealloc(PyObject *me)
   if (GMAC_F(me) & f_freeme)
     GM_DESTROY(GMAC_M(me));
   Py_DECREF(me->ob_type);
-  Py_DECREF(GMAC_NAMEOBJ(me));
   PyType_Type.tp_dealloc(me);
 }
 
@@ -988,7 +1021,7 @@ static PyGetSetDef gcmac_pygetset[] = {
 };
 
 static PyTypeObject gcmac_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+  PyObject_HEAD_INIT(0) 0,             /* Header */
   "catacomb.GCMAC",                    /* @tp_name@ */
   sizeof(gchash_pyobj),                        /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
@@ -1019,7 +1052,7 @@ static PyTypeObject gcmac_pytype_skel = {
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
+  0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
   gcmac_pygetset,                      /* @tp_getset@ */
@@ -1031,12 +1064,12 @@ static PyTypeObject gcmac_pytype_skel = {
   0,                                   /* @tp_init@ */
   PyType_GenericAlloc,                 /* @tp_alloc@ */
   abstract_pynew,                      /* @tp_new@ */
-  _PyObject_Del,                       /* @tp_free@ */
+  0,                                   /* @tp_free@ */
   0                                    /* @tp_is_gc@ */
 };
 
 static PyTypeObject gmac_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+  PyObject_HEAD_INIT(0) 0,             /* Header */
   "catacomb.GMAC",                     /* @tp_name@ */
   sizeof(gmac_pyobj),                  /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
@@ -1067,7 +1100,7 @@ static PyTypeObject gmac_pytype_skel = {
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
+  0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
   0,                                   /* @tp_getset@ */
@@ -1079,12 +1112,12 @@ static PyTypeObject gmac_pytype_skel = {
   0,                                   /* @tp_init@ */
   PyType_GenericAlloc,                 /* @tp_alloc@ */
   abstract_pynew,                      /* @tp_new@ */
-  _PyObject_Del,                       /* @tp_free@ */
+  0,                                   /* @tp_free@ */
   0                                    /* @tp_is_gc@ */
 };
 
 static PyTypeObject gmhash_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+  PyObject_HEAD_INIT(0) 0,             /* Header */
   "catacomb.GMACHash",                 /* @tp_name@ */
   sizeof(ghash_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
@@ -1115,9 +1148,254 @@ static PyTypeObject gmhash_pytype_skel = {
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
+  0,                                   /* @tp_iternext@ */
+  0,                                   /* @tp_methods@ */
+  0,                                   /* @tp_members@ */
+  0,                                   /* @tp_getset@ */
+  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@ */
+};
+
+/*----- Pseudorandom permutations -----------------------------------------*/
+
+static PyTypeObject *gcprp_pytype, *gprp_pytype;
+
+typedef struct prpinfo {
+  const char *name;
+  const octet *keysz;
+  size_t ctxsz;
+  size_t blksz;
+  void (*init)(void *, const void *, size_t);
+  void (*eblk)(void *, const void *, void *);
+  void (*dblk)(void *, const void *, void *);
+} prpinfo;
+
+#define PRP_DEF(PRE, pre)                                              \
+  static void pre##_prpinit(void *ctx, const void *k, size_t ksz)      \
+    { pre##_init(ctx, k, ksz); }                                       \
+  static void pre##_prpeblk(void *ctx, const void *in, void *out)      \
+  {                                                                    \
+    uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in);                    \
+    pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w);                    \
+  }                                                                    \
+  static void pre##_prpdblk(void *ctx, const void *in, void *out)      \
+  {                                                                    \
+    uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in);                    \
+    pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w);                    \
+  }                                                                    \
+  static const prpinfo pre##_prpinfo = {                               \
+    #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ,                 \
+    pre##_prpinit, pre##_prpeblk, pre##_prpdblk                                \
+  };
+PRPS(PRP_DEF)
+
+static const struct prpinfo *const gprptab[] = {
+#define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
+  PRPS(PRP_ENTRY)
+  0
+};
+
+typedef struct gcprp_pyobj {
+  PyHeapTypeObject ty;
+  const prpinfo *prp;
+} gcprp_pyobj;
+#define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
+
+typedef struct gprp_pyobj {
+  PyObject_HEAD
+  const prpinfo *prp;
+} gprp_pyobj;
+#define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
+#define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
+
+typedef struct prp {
+  const prpinfo *prp;
+  void *ctx;
+} prp;
+
+static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
+{
+  char *kwlist[] = { "key", 0 };
+  char *k;
+  int sz;
+  const prpinfo *prp = GCPRP_PRP(ty);
+  PyObject *me;
+
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
+    goto end;
+  if (keysz(sz, prp->keysz) != sz) VALERR("bad key length");
+  me = (PyObject *)ty->tp_alloc(ty, 0);
+  GPRP_PRP(me) = prp;
+  prp->init(GPRP_CTX(me), k, sz);
+  Py_INCREF(me);
+  return (me);
+end:
+  return (0);
+}
+
+static void gprp_pydealloc(PyObject *me)
+  { Py_DECREF(me->ob_type); FREEOBJ(me); }
+
+static PyObject *gcprp_pywrap(const prpinfo *prp)
+{
+  gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name);
+  g->prp = prp;
+  g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz;
+  g->ty.ht_type.tp_base = gprp_pytype;
+  Py_INCREF(gprp_pytype);
+  g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
+                           Py_TPFLAGS_BASETYPE |
+                           Py_TPFLAGS_HEAPTYPE);
+  g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
+  g->ty.ht_type.tp_free = 0;
+  g->ty.ht_type.tp_new = gprp_pynew;
+  PyType_Ready(&g->ty.ht_type);
+  return ((PyObject *)g);
+}
+
+static PyObject *gcpget_name(PyObject *me, void *hunoz)
+  { return (PyString_FromString(GCPRP_PRP(me)->name)); }
+static PyObject *gcpget_keysz(PyObject *me, void *hunoz)
+  { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); }
+static PyObject *gcpget_blksz(PyObject *me, void *hunoz)
+  { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); }
+
+static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg)
+{
+  char *p;
+  int n;
+  PyObject *rc = 0;
+
+  if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &n)) goto end;
+  if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
+  rc = bytestring_pywrap(0, n);
+  GPRP_PRP(me)->eblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
+end:
+  return (rc);
+}
+
+static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg)
+{
+  char *p;
+  int n;
+  PyObject *rc = 0;
+
+  if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &n)) goto end;
+  if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
+  rc = bytestring_pywrap(0, n);
+  GPRP_PRP(me)->dblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
+end:
+  return (rc);
+}
+
+static PyGetSetDef gcprp_pygetset[] = {
+#define GETSETNAME(op, name) gcp##op##_##name
+  GET  (keysz,                 "CP.keysz -> acceptable key sizes")
+  GET  (blksz,                 "CP.blksz -> block size")
+  GET  (name,                  "CP.name -> name of this kind of PRP")
+#undef GETSETNAME
+  { 0 }
+};
+
+static PyMethodDef gprp_pymethods[] = {
+#define METHNAME(name) gpmeth_##name
+  METH (encrypt,               "P.encrypt(PT) -> CT")
+  METH (decrypt,               "P.decrypt(CT) -> PT")
+#undef METHNAME
+  { 0 }
+};
+
+static PyTypeObject gcprp_pytype_skel = {
+  PyObject_HEAD_INIT(0) 0,             /* Header */
+  "catacomb.GCPRP",                    /* @tp_name@ */
+  sizeof(gcprp_pyobj),                 /* @tp_basicsize@ */
+  0,                                   /* @tp_itemsize@ */
+
+  0,                                   /* @tp_dealloc@ */
+  0,                                   /* @tp_print@ */
+  0,                                   /* @tp_getattr@ */
+  0,                                   /* @tp_setattr@ */
+  0,                                   /* @tp_compare@ */
+  0,                                   /* @tp_repr@ */
+  0,                                   /* @tp_as_number@ */
+  0,                                   /* @tp_as_sequence@ */
+  0,                                   /* @tp_as_mapping@ */
+  0,                                   /* @tp_hash@ */
+  0,                                   /* @tp_call@ */
+  0,                                   /* @tp_str@ */
+  0,                                   /* @tp_getattro@ */
+  0,                                   /* @tp_setattro@ */
+  0,                                   /* @tp_as_buffer@ */
+  Py_TPFLAGS_DEFAULT |                 /* @tp_flags@ */
+    Py_TPFLAGS_BASETYPE,
+
+  /* @tp_doc@ */
+"Pseudorandom permutation metaclass.",
+
+  0,                                   /* @tp_traverse@ */
+  0,                                   /* @tp_clear@ */
+  0,                                   /* @tp_richcompare@ */
+  0,                                   /* @tp_weaklistoffset@ */
+  0,                                   /* @tp_iter@ */
+  0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
+  gcprp_pygetset,                      /* @tp_getset@ */
+  0,                                   /* @tp_base@ */
+  0,                                   /* @tp_dict@ */
+  0,                                   /* @tp_descr_get@ */
+  0,                                   /* @tp_descr_set@ */
+  0,                                   /* @tp_dictoffset@ */
+  0,                                   /* @tp_init@ */
+  PyType_GenericAlloc,                 /* @tp_alloc@ */
+  abstract_pynew,                      /* @tp_new@ */
+  0,                                   /* @tp_free@ */
+  0                                    /* @tp_is_gc@ */
+};
+
+static PyTypeObject gprp_pytype_skel = {
+  PyObject_HEAD_INIT(0) 0,             /* Header */
+  "catacomb.GPRP",                     /* @tp_name@ */
+  sizeof(gprp_pyobj),                  /* @tp_basicsize@ */
+  0,                                   /* @tp_itemsize@ */
+
+  gprp_pydealloc,                      /* @tp_dealloc@ */
+  0,                                   /* @tp_print@ */
+  0,                                   /* @tp_getattr@ */
+  0,                                   /* @tp_setattr@ */
+  0,                                   /* @tp_compare@ */
+  0,                                   /* @tp_repr@ */
+  0,                                   /* @tp_as_number@ */
+  0,                                   /* @tp_as_sequence@ */
+  0,                                   /* @tp_as_mapping@ */
+  0,                                   /* @tp_hash@ */
+  0,                                   /* @tp_call@ */
+  0,                                   /* @tp_str@ */
+  0,                                   /* @tp_getattro@ */
+  0,                                   /* @tp_setattro@ */
+  0,                                   /* @tp_as_buffer@ */
+  Py_TPFLAGS_DEFAULT |                 /* @tp_flags@ */
+    Py_TPFLAGS_BASETYPE,
+
+  /* @tp_doc@ */
+"Pseudorandom permutation, abstract base class.",
+
+  0,                                   /* @tp_traverse@ */
+  0,                                   /* @tp_clear@ */
+  0,                                   /* @tp_richcompare@ */
+  0,                                   /* @tp_weaklistoffset@ */
+  0,                                   /* @tp_iter@ */
+  0,                                   /* @tp_iternext@ */
+  gprp_pymethods,                      /* @tp_methods@ */
+  0,                                   /* @tp_members@ */
   0,                                   /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
@@ -1127,7 +1405,7 @@ static PyTypeObject gmhash_pytype_skel = {
   0,                                   /* @tp_init@ */
   PyType_GenericAlloc,                 /* @tp_alloc@ */
   abstract_pynew,                      /* @tp_new@ */
-  _PyObject_Del,                       /* @tp_free@ */
+  0,                                   /* @tp_free@ */
   0                                    /* @tp_is_gc@ */
 };
 
@@ -1146,27 +1424,15 @@ void algorithms_pyinit(void)
   INITTYPE(gcmac, type);
   INITTYPE(gmac, type);
   INITTYPE(gmhash, ghash);
+  INITTYPE(gcprp, type);
+  INITTYPE(gprp, root);
 }
 
-#define GEN(func, base)                                                        \
-  static PyObject *func(void)                                          \
-  {                                                                    \
-    PyObject *d = PyDict_New();                                                \
-    PyObject *o;                                                       \
-    int i;                                                             \
-                                                                       \
-    for (i = 0; g##base##tab[i]; i++) {                                        \
-      o = gc##base##_pywrap((/*unconst*/ gc##base *)g##base##tab[i]);  \
-      PyDict_SetItemString(d,                                          \
-                          (/*unconst*/ char *)g##base##tab[i]->name,   \
-                          o);                                          \
-      Py_DECREF(o);                                                    \
-    }                                                                  \
-    return (d);                                                                \
-  }
 GEN(gcciphers, cipher)
 GEN(gchashes, hash)
 GEN(gcmacs, mac)
+#define gcprp prpinfo
+GEN(gcprps, prp)
 
 void algorithms_pyinsert(PyObject *mod)
 {
@@ -1187,6 +1453,9 @@ void algorithms_pyinsert(PyObject *mod)
   INSERT("GMAC", gmac_pytype);
   INSERT("GMACHash", gmhash_pytype);
   INSERT("gcmacs", gcmacs());
+  INSERT("GCPRP", gcprp_pytype);
+  INSERT("GPRP", gprp_pytype);
+  INSERT("gcprps", gcprps());
 }
 
 /*----- That's all, folks -------------------------------------------------*/