From 348254521f404f683d0230a84ae4943c9d20e51c Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Thu, 11 May 2017 10:42:15 +0100 Subject: [PATCH] rand.c, algorithms.py: Change how kinds of RNGs are distinguished. Rather than a set of flags, assign them numbers and use `switch'. It doesn't seem like the flags are helpfully marking out sets of RNGs, and this approach scales better to handling more kinds. --- algorithms.py | 8 ++++---- rand.c | 26 ++++++++++++++++++-------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/algorithms.py b/algorithms.py index a2a468f..a7a2caa 100644 --- a/algorithms.py +++ b/algorithms.py @@ -73,14 +73,14 @@ print print '#define RNGS(_) \\' for i in (cross(prps, ['ofb', 'counter'])): print ('\t_("%(prim)s-%(mode)s", %(prim)s_keysz, ' + - '%(prim)s_%(mode)srand, 0, 0) \\') % \ + '%(prim)s_%(mode)srand, RNG_PLAIN, 0) \\') % \ {'prim': i[0], 'mode': i[1]} for i in (cross(hashes, 'mgf')): print ('\t_("%(prim)s-%(mode)s", %(prim)s_%(mode)skeysz, ' + - '%(prim)s_%(mode)srand, 0, 0) \\') % \ + '%(prim)s_%(mode)srand, RNG_PLAIN, 0) \\') % \ {'prim': i[0], 'mode': i[1]} print '\t_("rc4", rc4_keysz, rc4_rand, 0, 0) \\' -print '\t_("seal", seal_keysz, seal_rand, RNGF_INT, 0) \\' +print '\t_("seal", seal_keysz, seal_rand, RNG_SEAL, 0) \\' for i in latindances: for r in ['salsa20', 'xsalsa20', 'chacha', 'xchacha']: if i.startswith(r): @@ -90,7 +90,7 @@ for i in latindances: raise ValueError, 'failed to find root name for %s' % i if i.endswith('-ietf'): root += '_ietf' print ('\t_("%(name)s", %(root)s_keysz, %(id)s_rand, ' + - 'RNGF_NONCE | RNGF_LATIN, %(ROOT)s_NONCESZ) \\') % \ + 'RNG_LATIN, %(ROOT)s_NONCESZ) \\') % \ {'name': i, 'id': i.translate(None, '/').replace('-', '_'), 'root': root, 'ROOT': root.upper()} print '\t/* end */' diff --git a/rand.c b/rand.c index f163efa..5402737 100644 --- a/rand.c +++ b/rand.c @@ -591,9 +591,15 @@ typedef struct gccrand_info { gcrand_func *func; } gccrand_info; -#define RNGF_INT 1u -#define RNGF_NONCE 2u -#define RNGF_LATIN 4u +#define RNGF_MASK 255u + +enum { + RNG_PLAIN = 0, + RNG_SEAL, + RNG_LATIN, + RNG_SHAKE, + RNG_KMAC +}; typedef struct gccrand_pyobj { PyHeapTypeObject ty; @@ -670,17 +676,21 @@ static PyObject *gccrand_pywrap(const gccrand_info *info) gccrand_pyobj *g = newtype(gccrand_pytype, 0, info->name); g->info = info; g->ty.ht_type.tp_basicsize = sizeof(grand_pyobj); - g->ty.ht_type.tp_base = - (info->f & RNGF_LATIN) ? gclatinrand_pytype : gcrand_pytype; + switch (info->f&RNGF_MASK) { + case RNG_LATIN: g->ty.ht_type.tp_base = gclatinrand_pytype; break; + default: g->ty.ht_type.tp_base = gcrand_pytype; break; + } Py_INCREF(g->ty.ht_type.tp_base); 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; - if (info->f & RNGF_INT) g->ty.ht_type.tp_new = gcirand_pynew; - else if (info->f & RNGF_NONCE) g->ty.ht_type.tp_new = gcnrand_pynew; - else g->ty.ht_type.tp_new = gcrand_pynew; + switch (info->f&RNGF_MASK) { + case RNG_LATIN: g->ty.ht_type.tp_new = gcnrand_pynew; break; + case RNG_SEAL: g->ty.ht_type.tp_new = gcirand_pynew; break; + default: g->ty.ht_type.tp_new = gcrand_pynew; break; + } typeready(&g->ty.ht_type); return ((PyObject *)g); } -- 2.11.0