From 5d04dade21133e36042140f83fb8d9d52c746469 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Mon, 14 Oct 2019 00:56:07 +0100 Subject: [PATCH] algorithms.c: Set `hashsz', `tagsz', and `name' properties on MAC keys. The generic MAC objects, and Poly1305. --- algorithms.c | 34 ++++++++++++++++++++++++++++++++-- t/t-algorithms.py | 21 ++++++++++++--------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/algorithms.c b/algorithms.c index 071894a..57a41d7 100644 --- a/algorithms.c +++ b/algorithms.c @@ -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@ */ diff --git a/t/t-algorithms.py b/t/t-algorithms.py index 52decd6..aa8567b 100644 --- a/t/t-algorithms.py +++ b/t/t-algorithms.py @@ -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. -- 2.11.0