From: Nick Mathewson Date: Thu, 18 Feb 2010 06:43:37 +0000 (-0500) Subject: Add a unit test for secure rng. X-Git-Tag: release-2.0.4-alpha~25 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=48a29b681619bfd431e987c7cbd623121ef91b13;p=libevent Add a unit test for secure rng. Mostly, this is just to make sure our arc4random_buf() implementation isn't dumb. --- diff --git a/test/regress_util.c b/test/regress_util.c index 27f04e4a..9f91ac75 100644 --- a/test/regress_util.c +++ b/test/regress_util.c @@ -46,6 +46,7 @@ #include #include #include +#include #include "event2/event.h" #include "event2/util.h" @@ -711,6 +712,57 @@ end: return -1; } +static void +test_evutil_rand(void *arg) +{ + char buf1[32]; + char buf2[32]; + int counts[256]; + int i, j, k, n=0; + + memset(buf2, 0, sizeof(buf2)); + memset(counts, 0, sizeof(counts)); + + for (k=0;k<32;++k) { + /* Try a few different start and end points; try to catch + * the various misaligned cases of arc4random_buf */ + int startpoint = _evutil_weakrand() % 4; + int endpoint = 32 - (_evutil_weakrand() % 4); + + memset(buf2, 0, sizeof(buf2)); + + /* Do 6 runs over buf1, or-ing the result into buf2 each + * time, to make sure we're setting each byte that we mean + * to set. */ + for (i=0;i<8;++i) { + memset(buf1, 0, sizeof(buf1)); + evutil_secure_rng_get_bytes(buf1 + startpoint, + endpoint-startpoint); + n += endpoint - startpoint; + for (j=0; j<32; ++j) { + if (j >= startpoint && j < endpoint) { + buf2[j] |= buf1[j]; + ++counts[(unsigned char)buf1[j]]; + } else { + assert(buf1[j] == 0); + tt_int_op(buf1[j], ==, 0); + + } + } + } + + /* This will give a false positive with P=(256**8)==(2**64) + * for each character. */ + for (j=startpoint;j