2
0

eng_rdrand.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2011-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. /* We need to use some engine deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <openssl/opensslconf.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "crypto/engine.h"
  15. #include "internal/cryptlib.h"
  16. #include <openssl/rand.h>
  17. #include <openssl/err.h>
  18. #include <openssl/crypto.h>
  19. #if defined(__has_feature)
  20. # if __has_feature(memory_sanitizer)
  21. # include <sanitizer/msan_interface.h>
  22. # endif
  23. #endif
  24. #if (defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
  25. defined(__x86_64) || defined(__x86_64__) || \
  26. defined(_M_AMD64) || defined (_M_X64)) && defined(OPENSSL_CPUID_OBJ)
  27. size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf, size_t len);
  28. static int get_random_bytes(unsigned char *buf, int num)
  29. {
  30. if (num < 0) {
  31. return 0;
  32. }
  33. # if defined(__has_feature)
  34. # if __has_feature(memory_sanitizer)
  35. /*
  36. * MemorySanitizer fails to understand asm and produces false positive
  37. * use-of-uninitialized-value warnings.
  38. */
  39. __msan_unpoison(buf, num);
  40. # endif
  41. # endif
  42. return (size_t)num == OPENSSL_ia32_rdrand_bytes(buf, (size_t)num);
  43. }
  44. static int random_status(void)
  45. {
  46. return 1;
  47. }
  48. static RAND_METHOD rdrand_meth = {
  49. NULL, /* seed */
  50. get_random_bytes,
  51. NULL, /* cleanup */
  52. NULL, /* add */
  53. get_random_bytes,
  54. random_status,
  55. };
  56. static int rdrand_init(ENGINE *e)
  57. {
  58. return 1;
  59. }
  60. static const char *engine_e_rdrand_id = "rdrand";
  61. static const char *engine_e_rdrand_name = "Intel RDRAND engine";
  62. static int bind_helper(ENGINE *e)
  63. {
  64. if (!ENGINE_set_id(e, engine_e_rdrand_id) ||
  65. !ENGINE_set_name(e, engine_e_rdrand_name) ||
  66. !ENGINE_set_flags(e, ENGINE_FLAGS_NO_REGISTER_ALL) ||
  67. !ENGINE_set_init_function(e, rdrand_init) ||
  68. !ENGINE_set_RAND(e, &rdrand_meth))
  69. return 0;
  70. return 1;
  71. }
  72. static ENGINE *ENGINE_rdrand(void)
  73. {
  74. ENGINE *ret = ENGINE_new();
  75. if (ret == NULL)
  76. return NULL;
  77. if (!bind_helper(ret)) {
  78. ENGINE_free(ret);
  79. return NULL;
  80. }
  81. return ret;
  82. }
  83. void engine_load_rdrand_int(void)
  84. {
  85. if (OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) {
  86. ENGINE *toadd = ENGINE_rdrand();
  87. if (!toadd)
  88. return;
  89. ERR_set_mark();
  90. ENGINE_add(toadd);
  91. /*
  92. * If the "add" worked, it gets a structural reference. So either way, we
  93. * release our just-created reference.
  94. */
  95. ENGINE_free(toadd);
  96. /*
  97. * If the "add" didn't work, it was probably a conflict because it was
  98. * already added (eg. someone calling ENGINE_load_blah then calling
  99. * ENGINE_load_builtin_engines() perhaps).
  100. */
  101. ERR_pop_to_mark();
  102. }
  103. }
  104. #else
  105. void engine_load_rdrand_int(void)
  106. {
  107. }
  108. #endif