rand.c: Make nonce/IV arguments to cipher-based random generators optional.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 25 Nov 2019 13:04:35 +0000 (13:04 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 25 Nov 2019 17:51:33 +0000 (17:51 +0000)
In all cases, this defaults to zero.

rand.c
t/t-rand.py
t/testutils.py

diff --git a/rand.c b/rand.c
index 22a0b3c..ada2baa 100644 (file)
--- 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");
index d8d7b00..0ad92dc 100644 (file)
@@ -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)
index 0ba49c6..67bcc0e 100644 (file)
@@ -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):
   """