1/*
2 * Copyright 2011-2020 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(__i386)   || defined(__i386__)   || defined(_M_IX86) || \
24     defined(__x86_64) || defined(__x86_64__) || \
25     defined(_M_AMD64) || defined (_M_X64)) && defined(OPENSSL_CPUID_OBJ)
26
27size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf, size_t len);
28
29static int get_random_bytes(unsigned char *buf, int num)
30{
31    if (num < 0) {
32        return 0;
33    }
34
35    return (size_t)num == OPENSSL_ia32_rdrand_bytes(buf, (size_t)num);
36}
37
38static int random_status(void)
39{
40    return 1;
41}
42
43static RAND_METHOD rdrand_meth = {
44    NULL,                       /* seed */
45    get_random_bytes,
46    NULL,                       /* cleanup */
47    NULL,                       /* add */
48    get_random_bytes,
49    random_status,
50};
51
52static int rdrand_init(ENGINE *e)
53{
54    return 1;
55}
56
57static const char *engine_e_rdrand_id = "rdrand";
58static const char *engine_e_rdrand_name = "Intel RDRAND engine";
59
60static int bind_helper(ENGINE *e)
61{
62    if (!ENGINE_set_id(e, engine_e_rdrand_id) ||
63        !ENGINE_set_name(e, engine_e_rdrand_name) ||
64        !ENGINE_set_flags(e, ENGINE_FLAGS_NO_REGISTER_ALL) ||
65        !ENGINE_set_init_function(e, rdrand_init) ||
66        !ENGINE_set_RAND(e, &rdrand_meth))
67        return 0;
68
69    return 1;
70}
71
72static 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
84void engine_load_rdrand_int(void)
85{
86    if (OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) {
87        ENGINE *toadd = ENGINE_rdrand();
88        if (!toadd)
89            return;
90        ERR_set_mark();
91        ENGINE_add(toadd);
92        /*
93        * If the "add" worked, it gets a structural reference. So either way, we
94        * release our just-created reference.
95        */
96        ENGINE_free(toadd);
97        /*
98        * If the "add" didn't work, it was probably a conflict because it was
99        * already added (eg. someone calling ENGINE_load_blah then calling
100        * ENGINE_load_builtin_engines() perhaps).
101        */
102        ERR_pop_to_mark();
103    }
104}
105#else
106void engine_load_rdrand_int(void)
107{
108}
109#endif
110