From a6bd51d5a77eac779f8c917bbba08fa841837bc7 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Mon, 25 Nov 2019 13:04:35 +0000 Subject: [PATCH] rand.c: Make nonce/IV arguments to cipher-based random generators optional. In all cases, this defaults to zero. --- rand.c | 6 ++++-- t/t-rand.py | 3 +++ t/testutils.py | 3 +-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/rand.c b/rand.c index 22a0b3c..ada2baa 100644 --- a/rand.c +++ b/rand.c @@ -642,7 +642,7 @@ static PyObject *gcirand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) static const char *const kwlist[] = { "key", "i", 0 }; struct bin k; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:new", KWLIST, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", KWLIST, convbin, &k, convu32, &i)) goto end; if (keysz(k.sz, info->keysz) != k.sz) VALERR("bad key length"); @@ -657,9 +657,11 @@ static PyObject *gcnrand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) { const gccrand_info *info = GCCRAND_INFO(ty); static const char *const kwlist[] = { "key", "nonce", 0 }; + static const octet zn[24] = { 0 }; struct bin k, n; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:new", KWLIST, + n.p = zn; n.sz = info->noncesz; assert(info->noncesz <= sizeof(zn)); + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", KWLIST, convbin, &k, convbin, &n)) goto end; if (keysz(k.sz, info->keysz) != k.sz) VALERR("bad key length"); diff --git a/t/t-rand.py b/t/t-rand.py index d8d7b00..0ad92dc 100644 --- a/t/t-rand.py +++ b/t/t-rand.py @@ -105,6 +105,9 @@ class TestRandomGenerator (U.TestCase): rcls = C.gccrands[r] rng = rcls(T.span(rcls.keysz.default), **kw) me.assertTrue(rng.cryptop) + if kw: + rng = rcls(T.span(rcls.keysz.default)) + me.check_rand(rng) def test_sslrand(me): rng = C.SSLRand(T.span(16), T.span(32), C.md5, C.sha) diff --git a/t/testutils.py b/t/testutils.py index 0ba49c6..67bcc0e 100644 --- a/t/testutils.py +++ b/t/testutils.py @@ -99,10 +99,9 @@ def prep_lenseq(w, n, bigendp, goodp): span(n) + \ byteseq([0xff]) -Z64 = C.ByteString.zero(8) def detrand(seed): """Return a fast deterministic random generator with the given SEED.""" - return C.chacha8rand(C.sha256().hash(bin(seed)).done(), Z64) + return C.chacha8rand(C.sha256().hash(bin(seed)).done()) class GenericTestMixin (U.TestCase): """ -- 2.11.0