prng_test.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. */
  9. #include <tomcrypt_test.h>
  10. #ifdef LTC_PRNG_ENABLE_LTC_RNG
  11. static unsigned long my_test_rng_read;
  12. static unsigned long my_test_rng(unsigned char *buf, unsigned long len,
  13. void (*callback)(void))
  14. {
  15. unsigned long n;
  16. LTC_UNUSED_PARAM(callback);
  17. for (n = 0; n < len; ++n) {
  18. buf[n] = 4;
  19. }
  20. my_test_rng_read += n;
  21. return n;
  22. }
  23. #endif
  24. int prng_test(void)
  25. {
  26. int err = CRYPT_NOP;
  27. int x;
  28. unsigned char buf[4096] = { 0 };
  29. unsigned long n, one;
  30. prng_state nprng;
  31. #ifdef LTC_PRNG_ENABLE_LTC_RNG
  32. unsigned long before;
  33. unsigned long (*previous)(unsigned char *, unsigned long , void (*)(void)) = ltc_rng;
  34. ltc_rng = my_test_rng;
  35. before = my_test_rng_read;
  36. if ((err = rng_make_prng(128, find_prng("yarrow"), &nprng, NULL)) != CRYPT_OK) {
  37. fprintf(stderr, "rng_make_prng with 'my_test_rng' failed: %s\n", error_to_string(err));
  38. exit(EXIT_FAILURE);
  39. }
  40. DO(yarrow_done(&nprng));
  41. if (before == my_test_rng_read) {
  42. fprintf(stderr, "somehow there was no read from the ltc_rng! %lu == %lu\n", before, my_test_rng_read);
  43. exit(EXIT_FAILURE);
  44. }
  45. ltc_rng = previous;
  46. #endif
  47. /* test prngs (test, import/export) */
  48. for (x = 0; prng_descriptor[x].name != NULL; x++) {
  49. if(strstr(prng_descriptor[x].name, "no_prng") == prng_descriptor[x].name) continue;
  50. DOX(prng_descriptor[x].test(), prng_descriptor[x].name);
  51. DOX(prng_descriptor[x].start(&nprng), prng_descriptor[x].name);
  52. DOX(prng_descriptor[x].add_entropy((unsigned char *)"helloworld12", 12, &nprng), prng_descriptor[x].name);
  53. DOX(prng_descriptor[x].ready(&nprng), prng_descriptor[x].name);
  54. n = sizeof(buf);
  55. if (strcmp(prng_descriptor[x].name, "sprng")) {
  56. one = 1;
  57. if (prng_descriptor[x].pexport(buf, &one, &nprng) != CRYPT_BUFFER_OVERFLOW) {
  58. fprintf(stderr, "Error testing pexport with a short buffer (%s)\n", prng_descriptor[x].name);
  59. return CRYPT_ERROR;
  60. }
  61. }
  62. DOX(prng_descriptor[x].pexport(buf, &n, &nprng), prng_descriptor[x].name);
  63. prng_descriptor[x].done(&nprng);
  64. DOX(prng_descriptor[x].pimport(buf, n, &nprng), prng_descriptor[x].name);
  65. DOX(prng_descriptor[x].pimport(buf, sizeof(buf), &nprng), prng_descriptor[x].name); /* try to import larger data */
  66. DOX(prng_descriptor[x].ready(&nprng), prng_descriptor[x].name);
  67. if (prng_descriptor[x].read(buf, 100, &nprng) != 100) {
  68. fprintf(stderr, "Error reading from imported PRNG (%s)!\n", prng_descriptor[x].name);
  69. return CRYPT_ERROR;
  70. }
  71. prng_descriptor[x].done(&nprng);
  72. }
  73. if ((err = rng_make_prng(-1, find_prng("yarrow"), &nprng, NULL)) != CRYPT_OK) {
  74. fprintf(stderr, "rng_make_prng(-1,..) with 'yarrow' failed: %s\n", error_to_string(err));
  75. }
  76. DO(yarrow_done(&nprng));
  77. #ifdef LTC_FORTUNA
  78. DO(fortuna_start(&nprng));
  79. DO(fortuna_add_entropy(buf, 32, &nprng));
  80. DO(fortuna_ready(&nprng));
  81. if (fortuna_read(buf + 32, 32, &nprng) != 32) {
  82. fprintf(stderr, "Error reading from Fortuna after fortuna_add_entropy()!\n");
  83. return CRYPT_ERROR;
  84. }
  85. DO(fortuna_done(&nprng));
  86. DO(fortuna_start(&nprng));
  87. DO(fortuna_add_random_event(0, 0, buf, 32, &nprng));
  88. DO(fortuna_ready(&nprng));
  89. if (fortuna_read(buf + 64, 32, &nprng) != 32) {
  90. fprintf(stderr, "Error reading from Fortuna after fortuna_add_random_event()!\n");
  91. return CRYPT_ERROR;
  92. }
  93. DO(fortuna_done(&nprng));
  94. if (compare_testvector(buf + 64, 32, buf + 32, 32, "fortuna_add_entropy() vs. fortuna_add_random_event()", 0) != 0) {
  95. err = CRYPT_FAIL_TESTVECTOR;
  96. }
  97. #endif
  98. return err;
  99. }
  100. /* ref: $Format:%D$ */
  101. /* git commit: $Format:%H$ */
  102. /* commit time: $Format:%ai$ */