Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

65 řádky
1.7 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 <assert.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <openssl/cpu.h>
  19. #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM)
  20. int CRYPTO_have_hwrand(void) {
  21. return (OPENSSL_ia32cap_P[1] & (1u << 30)) != 0;
  22. }
  23. /* These functions are defined in asm/rdrand-x86_64.pl */
  24. extern int CRYPTO_rdrand(uint8_t out[8]);
  25. extern int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len);
  26. int CRYPTO_hwrand(uint8_t *buf, size_t len) {
  27. const size_t len_multiple8 = len & ~7;
  28. if (!CRYPTO_rdrand_multiple8_buf(buf, len_multiple8)) {
  29. return 0;
  30. }
  31. len -= len_multiple8;
  32. if (len != 0) {
  33. assert(len < 8);
  34. uint8_t rand_buf[8];
  35. if (!CRYPTO_rdrand(rand_buf)) {
  36. return 0;
  37. }
  38. memcpy(buf + len_multiple8, rand_buf, len);
  39. }
  40. return 1;
  41. }
  42. #else
  43. int CRYPTO_have_hwrand(void) {
  44. return 0;
  45. }
  46. void CRYPTO_hwrand(uint8_t *buf, size_t len) {
  47. abort();
  48. }
  49. #endif