crypto_entropy_rdrand.c 702 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "cpusupport.h"
  2. #ifdef CPUSUPPORT_X86_RDRAND
  3. /**
  4. * CPUSUPPORT CFLAGS: X86_RDRAND
  5. */
  6. #include <immintrin.h>
  7. #include <stddef.h>
  8. #include "crypto_entropy_rdrand.h"
  9. /**
  10. * generate_seed_rdrand(buf, len):
  11. * Fill the ${buf} buffer with values from RDRAND. This implementation uses
  12. * the RDRAND instruction, and should only be used if CPUSUPPORT_X86_RDRAND is
  13. * defined and cpusupport_x86_rdrand() returns nonzero.
  14. */
  15. int
  16. generate_seed_rdrand(unsigned int * buf, size_t len)
  17. {
  18. size_t i;
  19. /* Fill buffer. */
  20. for (i = 0; i < len; i++) {
  21. if (!_rdrand32_step(&buf[i]))
  22. goto err0;
  23. }
  24. /* Success! */
  25. return (0);
  26. err0:
  27. /* Failure! */
  28. return (1);
  29. }
  30. #endif /* CPUSUPPORT_X86_RDRAND */