/src/openssl/crypto/engine/eng_rdrand.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
10 | | #include <openssl/opensslconf.h> |
11 | | |
12 | | #include <stdio.h> |
13 | | #include <string.h> |
14 | | #include "internal/engine.h" |
15 | | #include <openssl/rand.h> |
16 | | #include <openssl/err.h> |
17 | | #include <openssl/crypto.h> |
18 | | |
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) |
22 | | |
23 | | size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf, size_t len); |
24 | | |
25 | | static int get_random_bytes(unsigned char *buf, int num) |
26 | 0 | { |
27 | 0 | if (num < 0) { |
28 | 0 | return 0; |
29 | 0 | } |
30 | 0 | |
31 | 0 | return (size_t)num == OPENSSL_ia32_rdrand_bytes(buf, (size_t)num); |
32 | 0 | } |
33 | | |
34 | | static int random_status(void) |
35 | 0 | { |
36 | 0 | return 1; |
37 | 0 | } |
38 | | |
39 | | static RAND_METHOD rdrand_meth = { |
40 | | NULL, /* seed */ |
41 | | get_random_bytes, |
42 | | NULL, /* cleanup */ |
43 | | NULL, /* add */ |
44 | | get_random_bytes, |
45 | | random_status, |
46 | | }; |
47 | | |
48 | | static int rdrand_init(ENGINE *e) |
49 | 0 | { |
50 | 0 | return 1; |
51 | 0 | } |
52 | | |
53 | | static const char *engine_e_rdrand_id = "rdrand"; |
54 | | static const char *engine_e_rdrand_name = "Intel RDRAND engine"; |
55 | | |
56 | | static int bind_helper(ENGINE *e) |
57 | 0 | { |
58 | 0 | if (!ENGINE_set_id(e, engine_e_rdrand_id) || |
59 | 0 | !ENGINE_set_name(e, engine_e_rdrand_name) || |
60 | 0 | !ENGINE_set_flags(e, ENGINE_FLAGS_NO_REGISTER_ALL) || |
61 | 0 | !ENGINE_set_init_function(e, rdrand_init) || |
62 | 0 | !ENGINE_set_RAND(e, &rdrand_meth)) |
63 | 0 | return 0; |
64 | 0 | |
65 | 0 | return 1; |
66 | 0 | } |
67 | | |
68 | | static ENGINE *ENGINE_rdrand(void) |
69 | 0 | { |
70 | 0 | ENGINE *ret = ENGINE_new(); |
71 | 0 | if (ret == NULL) |
72 | 0 | return NULL; |
73 | 0 | if (!bind_helper(ret)) { |
74 | 0 | ENGINE_free(ret); |
75 | 0 | return NULL; |
76 | 0 | } |
77 | 0 | return ret; |
78 | 0 | } |
79 | | |
80 | | void engine_load_rdrand_int(void) |
81 | 0 | { |
82 | 0 | extern unsigned int OPENSSL_ia32cap_P[]; |
83 | 0 |
|
84 | 0 | if (OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) { |
85 | 0 | ENGINE *toadd = ENGINE_rdrand(); |
86 | 0 | if (!toadd) |
87 | 0 | return; |
88 | 0 | ENGINE_add(toadd); |
89 | 0 | ENGINE_free(toadd); |
90 | 0 | ERR_clear_error(); |
91 | 0 | } |
92 | 0 | } |
93 | | #else |
94 | | void engine_load_rdrand_int(void) |
95 | | { |
96 | | } |
97 | | #endif |