chiark / gitweb /
algorithms.c: Set `hashsz', `tagsz', and `name' properties on MAC keys.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 13 Oct 2019 23:56:07 +0000 (00:56 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 11 Apr 2020 11:49:31 +0000 (12:49 +0100)
The generic MAC objects, and Poly1305.

algorithms.c
t/t-algorithms.py

index 071894a676aecb23230009ba4f18874010cdd7f8..57a41d78e857d61f93c3f5bcbfae0a40f64b913a 100644 (file)
@@ -2309,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@ */
@@ -2392,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@ */
@@ -2632,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")
@@ -2734,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@ */
index 52decd6d6d95ad8459e296ce59f432ebd94061cd..aa8567b1f7a76f963bdc4b556ea9c01d4fa554a9 100644 (file)
@@ -602,29 +602,28 @@ class BaseTestHash (HashBufferTestMixin):
     """
     Check hash class HCLS.
 
-    If NEED_BUFSZ is false, then don't insist that HCLS have working `bufsz',
-    `name', or `hashsz' attributes.  This test is mostly reused for MACs,
-    which don't have these attributes.
+    If NEED_BUFSZ is false, then don't insist that HCLS has a working `bufsz'
+    attribute.  This test is mostly reused for MACs, which don't have this
+    attribute.
     """
     ## Check the class properties.
-    if need_bufsz:
-      me.assertEqual(type(hcls.name), str)
-      me.assertEqual(type(hcls.bufsz), int)
-      me.assertEqual(type(hcls.hashsz), int)
+    me.assertEqual(type(hcls.name), str)
+    if need_bufsz: me.assertEqual(type(hcls.bufsz), int)
+    me.assertEqual(type(hcls.hashsz), int)
 
     ## Set some initial values.
     m = T.span(131)
     h = hcls().hash(m).done()
 
     ## Check that hash length comes out right.
-    if need_bufsz: me.assertEqual(len(h), hcls.hashsz)
+    me.assertEqual(len(h), hcls.hashsz)
 
     ## Check that we get the same answer if we split the message up.
     me.assertEqual(h, hcls().hash(m[0:73]).hash(m[73:131]).done())
 
     ## Check the `check' method.
     me.assertTrue(hcls().hash(m).check(h))
-    me.assertFalse(hcls().hash(m).check(h ^ len(h)*C.bytes("aa")))
+    me.assertFalse(hcls().hash(m).check(h ^ hcls.hashsz*C.bytes("aa")))
 
     ## Check the menagerie of random hashing methods.
     def mkhash(_):
@@ -654,6 +653,7 @@ class TestMessageAuthentication (BaseTestHash, T.GenericTestMixin):
     ## Test hashing.
     k = T.span(mcls.keysz.default)
     key = mcls(k)
+    me.assertEqual(key.hashsz, key.tagsz)
     me.check_hash(key, need_bufsz = False)
 
     ## Check that bad key lengths are rejected.
@@ -685,6 +685,9 @@ class TestPoly1305 (HashBufferTestMixin):
     t = key(u).hash(m).done()
 
     ## Check the key properties.
+    me.assertEqual(key.name, "poly1305")
+    me.assertEqual(key.tagsz, 16)
+    me.assertEqual(key.tagsz, 16)
     me.assertEqual(len(t), 16)
 
     ## Check that we get the same answer if we split the message up.