I2C toy code
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

91 lines
3.0 KiB

  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"), &yarrow_prng, 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. if (before == my_test_rng_read) {
  41. fprintf(stderr, "somehow there was no read from the ltc_rng! %lu == %lu\n", before, my_test_rng_read);
  42. exit(EXIT_FAILURE);
  43. }
  44. ltc_rng = previous;
  45. #endif
  46. /* test prngs (test, import/export) */
  47. for (x = 0; prng_descriptor[x].name != NULL; x++) {
  48. if(strstr(prng_descriptor[x].name, "no_prng") == prng_descriptor[x].name) continue;
  49. err = CRYPT_OK;
  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, 4096, &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. return err;
  74. }
  75. /* ref: $Format:%D$ */
  76. /* git commit: $Format:%H$ */
  77. /* commit time: $Format:%ai$ */