aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_rng.c
blob: ebaffb74f5634c35d5692dd49a0b2f164801089b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* Copyright (c) 2016-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/*
 * Example usage:
 *
 * ./src/test/test-rng --emit | dieharder -g 200 -a
 *
 * Remember, dieharder can tell you that your RNG is completely broken, but if
 * your RNG is not _completely_ broken, dieharder cannot tell you whether your
 * RNG is actually secure.
 */

#include "orconfig.h"

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "lib/crypt_ops/crypto_rand.h"

int
main(int argc, char **argv)
{
  uint8_t buf[0x123];

  if (argc != 2 || strcmp(argv[1], "--emit")) {
    fprintf(stderr, "If you want me to fill stdout with a bunch of random "
            "bytes, you need to say --emit.\n");
    return 1;
  }

  if (crypto_seed_rng() < 0) {
    fprintf(stderr, "Can't seed RNG.\n");
    return 1;
  }

#if 0
  while (1) {
    crypto_rand(buf, sizeof(buf));
    if (write(1 /*stdout*/, buf, sizeof(buf)) != sizeof(buf)) {
      fprintf(stderr, "write() failed: %s\n", strerror(errno));
      return 1;
    }
  }
#endif /* 0 */

  crypto_fast_rng_t *rng = crypto_fast_rng_new();
  while (1) {
    crypto_fast_rng_getbytes(rng, buf, sizeof(buf));
    if (write(1 /*stdout*/, buf, sizeof(buf)) != sizeof(buf)) {
      fprintf(stderr, "write() failed: %s\n", strerror(errno));
      return 1;
    }
  }
}