rng_test.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. *******************************************************************************
  3. \file rng_test.c
  4. \brief Tests for random number generators
  5. \project bee2/test
  6. \created 2014.10.10
  7. \version 2023.12.16
  8. \copyright The Bee2 authors
  9. \license Licensed under the Apache License, Version 2.0 (see LICENSE.txt).
  10. *******************************************************************************
  11. */
  12. #include <stdio.h>
  13. #include <bee2/core/mem.h>
  14. #include <bee2/core/hex.h>
  15. #include <bee2/core/prng.h>
  16. #include <bee2/core/rng.h>
  17. #include <bee2/core/util.h>
  18. /*
  19. *******************************************************************************
  20. Тестирование
  21. *******************************************************************************
  22. */
  23. bool_t rngTest()
  24. {
  25. const char* sources[] = { "trng", "trng2", "sys", "sys2", "timer" };
  26. octet buf[2500];
  27. char hex[33];
  28. size_t read;
  29. size_t pos;
  30. // пробежать источники
  31. for (pos = 0; pos < COUNT_OF(sources); ++pos)
  32. {
  33. if (rngESRead(&read, buf, 2500, sources[pos]) == ERR_OK)
  34. {
  35. if (read == 2500)
  36. hexFrom(hex, buf, 16),
  37. printf("rngSource[%5s]: %s... [FIPS: 1%c 2%c 3%c 4%c]\n",
  38. sources[pos],
  39. hex,
  40. rngTestFIPS1(buf) ? '+' : '-',
  41. rngTestFIPS2(buf) ? '+' : '-',
  42. rngTestFIPS3(buf) ? '+' : '-',
  43. rngTestFIPS4(buf) ? '+' : '-');
  44. else if (read > 16)
  45. hexFrom(hex, buf, 16),
  46. printf("rngSource[%5s]: %s... (%u bytes)\n",
  47. sources[pos], hex, (unsigned)read);
  48. else
  49. hexFrom(hex, buf, read),
  50. printf("rngSource[%5s]: %s\n", sources[pos], hex);
  51. }
  52. }
  53. // работа с ГСЧ
  54. if (rngCreate(0, 0) != ERR_OK)
  55. return FALSE;
  56. if (!rngIsValid())
  57. return FALSE;
  58. rngStepR(buf, 2500, 0);
  59. hexFrom(hex, buf, 16);
  60. printf("rngStepR: %s... [FIPS: 1%c 2%c 3%c 4%c]\n",
  61. hex,
  62. rngTestFIPS1(buf) ? '+' : '-',
  63. rngTestFIPS2(buf) ? '+' : '-',
  64. rngTestFIPS3(buf) ? '+' : '-',
  65. rngTestFIPS4(buf) ? '+' : '-');
  66. rngRekey();
  67. rngStepR2(buf, 2500, 0);
  68. hexFrom(hex, buf, 16);
  69. printf("rngStepR2: %s... [FIPS: 1%c 2%c 3%c 4%c]\n",
  70. hex,
  71. rngTestFIPS1(buf) ? '+' : '-',
  72. rngTestFIPS2(buf) ? '+' : '-',
  73. rngTestFIPS3(buf) ? '+' : '-',
  74. rngTestFIPS4(buf) ? '+' : '-');
  75. if (rngCreate(0, 0) != ERR_OK)
  76. return FALSE;
  77. rngClose();
  78. if (!rngIsValid())
  79. return FALSE;
  80. rngClose();
  81. if (rngIsValid())
  82. return FALSE;
  83. // все нормально
  84. return TRUE;
  85. }