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