test_rng.c 1.3 KB

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