Merge branch 'maint-0.4.5' into release-0.4.5
[tor.git] / src / test / test_rng.c
blobebaffb74f5634c35d5692dd49a0b2f164801089b
1 /* Copyright (c) 2016-2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /*
5 * Example usage:
7 * ./src/test/test-rng --emit | dieharder -g 200 -a
9 * Remember, dieharder can tell you that your RNG is completely broken, but if
10 * your RNG is not _completely_ broken, dieharder cannot tell you whether your
11 * RNG is actually secure.
14 #include "orconfig.h"
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #include <stdio.h>
20 #include <string.h>
21 #include <errno.h>
23 #include "lib/crypt_ops/crypto_rand.h"
25 int
26 main(int argc, char **argv)
28 uint8_t buf[0x123];
30 if (argc != 2 || strcmp(argv[1], "--emit")) {
31 fprintf(stderr, "If you want me to fill stdout with a bunch of random "
32 "bytes, you need to say --emit.\n");
33 return 1;
36 if (crypto_seed_rng() < 0) {
37 fprintf(stderr, "Can't seed RNG.\n");
38 return 1;
41 #if 0
42 while (1) {
43 crypto_rand(buf, sizeof(buf));
44 if (write(1 /*stdout*/, buf, sizeof(buf)) != sizeof(buf)) {
45 fprintf(stderr, "write() failed: %s\n", strerror(errno));
46 return 1;
49 #endif /* 0 */
51 crypto_fast_rng_t *rng = crypto_fast_rng_new();
52 while (1) {
53 crypto_fast_rng_getbytes(rng, buf, sizeof(buf));
54 if (write(1 /*stdout*/, buf, sizeof(buf)) != sizeof(buf)) {
55 fprintf(stderr, "write() failed: %s\n", strerror(errno));
56 return 1;