test_rng.c (1376B)
1 /* Copyright (c) 2016-2021, The Tor Project, Inc. */ 2 /* See LICENSE for licensing information */ 3 4 /* 5 * Example usage: 6 * 7 * ./src/test/test-rng --emit | dieharder -g 200 -a 8 * 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. 12 */ 13 14 #include "orconfig.h" 15 16 #ifdef HAVE_UNISTD_H 17 #include <unistd.h> 18 #endif 19 #include <stdio.h> 20 #include <string.h> 21 #include <errno.h> 22 23 #include "lib/crypt_ops/crypto_rand.h" 24 25 int 26 main(int argc, char **argv) 27 { 28 uint8_t buf[0x123]; 29 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; 34 } 35 36 if (crypto_seed_rng() < 0) { 37 fprintf(stderr, "Can't seed RNG.\n"); 38 return 1; 39 } 40 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; 47 } 48 } 49 #endif /* 0 */ 50 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; 57 } 58 } 59 }