You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

66 lines
1.8 KiB

  1. /***
  2. This file is part of systemd.
  3. Copyright 2017 Zbigniew Jędrzejewski-Szmek
  4. systemd is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. systemd is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with systemd; If not, see <http://www.gnu.org/licenses/>.
  14. ***/
  15. #include "hexdecoct.h"
  16. #include "random-util.h"
  17. #include "log.h"
  18. static void test_acquire_random_bytes(bool high_quality_required) {
  19. uint8_t buf[16] = {};
  20. unsigned i;
  21. log_info("/* %s */", __func__);
  22. for (i = 1; i < sizeof buf; i++) {
  23. assert_se(acquire_random_bytes(buf, i, high_quality_required) == 0);
  24. if (i + 1 < sizeof buf)
  25. assert_se(buf[i] == 0);
  26. hexdump(stdout, buf, i);
  27. }
  28. }
  29. static void test_pseudorandom_bytes(void) {
  30. uint8_t buf[16] = {};
  31. unsigned i;
  32. log_info("/* %s */", __func__);
  33. for (i = 1; i < sizeof buf; i++) {
  34. pseudorandom_bytes(buf, i);
  35. if (i + 1 < sizeof buf)
  36. assert_se(buf[i] == 0);
  37. hexdump(stdout, buf, i);
  38. }
  39. }
  40. int main(int argc, char **argv) {
  41. log_set_max_level(LOG_DEBUG);
  42. log_parse_environment();
  43. log_open();
  44. test_acquire_random_bytes(false);
  45. test_acquire_random_bytes(true);
  46. test_pseudorandom_bytes();
  47. return 0;
  48. }