From 3adfb1aa5bb924aad5b753057f9cf7214f81e0e4 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Fri, 15 Apr 2022 15:31:30 +0100 Subject: [PATCH] testsc: add random_advance_counter(). In test_primegen, we loop round retrieving random data until we find some that will permit a successful prime generation, so that we can log only the successful attempts, and not the failures (which don't have to be time-safe). But this itself introduces a potential mismatch between logs, because the simplistic RNG used in testsc will have different control flow depending on how far through a buffer of hash data it is at the start of a given run. random_advance_counter() gives it a fresh buffer, so calling that at the start of a run should normalise this out. The code to do that was already in the middle of random_read(); I've just pulled it out into a separately callable function. This hasn't _actually_ caused failures in test_primegen, but I'm not sure why not. (Perhaps just luck.) But it did cause a failure in another test of a similar nature, so before I commit _that_ test (and the thing it's testing), I'd better fix this. --- test/testsc.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/test/testsc.c b/test/testsc.c index c9029662..4d8b55a4 100644 --- a/test/testsc.c +++ b/test/testsc.c @@ -114,19 +114,23 @@ static void random_seed(const char *seedstr) random_buf_limit = 0; } +static void random_advance_counter(void) +{ + ssh_hash_reset(random_hash); + put_asciz(random_hash, random_seedstr); + put_uint64(random_hash, random_counter); + random_counter++; + random_buf_limit = ssh_hash_alg(random_hash)->hlen; + ssh_hash_digest(random_hash, random_buf); +} + void random_read(void *vbuf, size_t size) { assert(random_seedstr); uint8_t *buf = (uint8_t *)vbuf; while (size-- > 0) { - if (random_buf_limit == 0) { - ssh_hash_reset(random_hash); - put_asciz(random_hash, random_seedstr); - put_uint64(random_hash, random_counter); - random_counter++; - random_buf_limit = ssh_hash_alg(random_hash)->hlen; - ssh_hash_digest(random_hash, random_buf); - } + if (random_buf_limit == 0) + random_advance_counter(); *buf++ = random_buf[random_buf_limit--]; } } @@ -1514,6 +1518,7 @@ static void test_primegen(const PrimeGenerationPolicy *policy) for (size_t i = 0; i < looplimit(2); i++) { while (true) { + random_advance_counter(); struct random_state st = random_get_state(); PrimeCandidateSource *pcs = pcs_new(128);