2 * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/opensslconf.h>
14 #include <internal/engine.h>
15 #include <openssl/rand.h>
16 #include <openssl/err.h>
17 #include <openssl/crypto.h>
19 #if (defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
20 defined(__x86_64) || defined(__x86_64__) || \
21 defined(_M_AMD64) || defined (_M_X64)) && defined(OPENSSL_CPUID_OBJ)
23 size_t OPENSSL_ia32_rdrand(void);
25 static int get_random_bytes(unsigned char *buf, int num)
29 while (num >= (int)sizeof(size_t)) {
30 if ((rnd = OPENSSL_ia32_rdrand()) == 0)
33 *((size_t *)buf) = rnd;
34 buf += sizeof(size_t);
35 num -= sizeof(size_t);
38 if ((rnd = OPENSSL_ia32_rdrand()) == 0)
41 memcpy(buf, &rnd, num);
47 static int random_status(void)
52 static RAND_METHOD rdrand_meth = {
61 static int rdrand_init(ENGINE *e)
66 static const char *engine_e_rdrand_id = "rdrand";
67 static const char *engine_e_rdrand_name = "Intel RDRAND engine";
69 static int bind_helper(ENGINE *e)
71 if (!ENGINE_set_id(e, engine_e_rdrand_id) ||
72 !ENGINE_set_name(e, engine_e_rdrand_name) ||
73 !ENGINE_set_flags(e, ENGINE_FLAGS_NO_REGISTER_ALL) ||
74 !ENGINE_set_init_function(e, rdrand_init) ||
75 !ENGINE_set_RAND(e, &rdrand_meth))
81 static ENGINE *ENGINE_rdrand(void)
83 ENGINE *ret = ENGINE_new();
86 if (!bind_helper(ret)) {
93 void engine_load_rdrand_int(void)
95 extern unsigned int OPENSSL_ia32cap_P[];
97 if (OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) {
98 ENGINE *toadd = ENGINE_rdrand();
107 void engine_load_rdrand_int(void)