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.
 
 
 
 
 
 

57 lines
1.4 KiB

  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/rand.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <openssl/cpu.h>
  18. #if defined(OPENSSL_X86_64)
  19. int CRYPTO_have_hwrand(void) {
  20. return (OPENSSL_ia32cap_P[1] & (1u << 30)) != 0;
  21. }
  22. /* CRYPTO_rdrand is defined in asm/rdrand-x86_64.pl */
  23. extern uint64_t CRYPTO_rdrand(void);
  24. void CRYPTO_hwrand(uint8_t *buf, size_t len) {
  25. while (len >= 8) {
  26. uint64_t rand = CRYPTO_rdrand();
  27. memcpy(buf, &rand, sizeof(rand));
  28. len -= sizeof(rand);
  29. buf += sizeof(rand);
  30. }
  31. if (len > 0) {
  32. uint64_t rand = CRYPTO_rdrand();
  33. memcpy(buf, &rand, len);
  34. }
  35. }
  36. #else
  37. int CRYPTO_have_hwrand(void) {
  38. return 0;
  39. }
  40. void CRYPTO_hwrand(uint8_t *buf, size_t len) {
  41. abort();
  42. }
  43. #endif