eng_rdrand.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/opensslconf.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "crypto/engine.h"
  13. #include <openssl/rand.h>
  14. #include <openssl/err.h>
  15. #include <openssl/crypto.h>
  16. #if defined(__has_feature)
  17. # if __has_feature(memory_sanitizer)
  18. # include <sanitizer/msan_interface.h>
  19. # endif
  20. #endif
  21. #if (defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
  22. defined(__x86_64) || defined(__x86_64__) || \
  23. defined(_M_AMD64) || defined (_M_X64)) && defined(OPENSSL_CPUID_OBJ)
  24. size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf, size_t len);
  25. static int get_random_bytes(unsigned char *buf, int num)
  26. {
  27. if (num < 0) {
  28. return 0;
  29. }
  30. # if defined(__has_feature)
  31. # if __has_feature(memory_sanitizer)
  32. /*
  33. * MemorySanitizer fails to understand asm and produces false positive
  34. * use-of-uninitialized-value warnings.
  35. */
  36. __msan_unpoison(buf, num);
  37. # endif
  38. # endif
  39. return (size_t)num == OPENSSL_ia32_rdrand_bytes(buf, (size_t)num);
  40. }
  41. static int random_status(void)
  42. {
  43. return 1;
  44. }
  45. static RAND_METHOD rdrand_meth = {
  46. NULL, /* seed */
  47. get_random_bytes,
  48. NULL, /* cleanup */
  49. NULL, /* add */
  50. get_random_bytes,
  51. random_status,
  52. };
  53. static int rdrand_init(ENGINE *e)
  54. {
  55. return 1;
  56. }
  57. static const char *engine_e_rdrand_id = "rdrand";
  58. static const char *engine_e_rdrand_name = "Intel RDRAND engine";
  59. static int bind_helper(ENGINE *e)
  60. {
  61. if (!ENGINE_set_id(e, engine_e_rdrand_id) ||
  62. !ENGINE_set_name(e, engine_e_rdrand_name) ||
  63. !ENGINE_set_flags(e, ENGINE_FLAGS_NO_REGISTER_ALL) ||
  64. !ENGINE_set_init_function(e, rdrand_init) ||
  65. !ENGINE_set_RAND(e, &rdrand_meth))
  66. return 0;
  67. return 1;
  68. }
  69. static ENGINE *ENGINE_rdrand(void)
  70. {
  71. ENGINE *ret = ENGINE_new();
  72. if (ret == NULL)
  73. return NULL;
  74. if (!bind_helper(ret)) {
  75. ENGINE_free(ret);
  76. return NULL;
  77. }
  78. return ret;
  79. }
  80. void engine_load_rdrand_int(void)
  81. {
  82. extern unsigned int OPENSSL_ia32cap_P[];
  83. if (OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) {
  84. ENGINE *toadd = ENGINE_rdrand();
  85. if (!toadd)
  86. return;
  87. ENGINE_add(toadd);
  88. ENGINE_free(toadd);
  89. ERR_clear_error();
  90. }
  91. }
  92. #else
  93. void engine_load_rdrand_int(void)
  94. {
  95. }
  96. #endif