blob: e02b40be02da5bfc63722051ca0269de6ee26ef6 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <openssl/rand.h>
16
Adam Langleyd7554562015-09-24 15:03:14 -070017#include <assert.h>
Matt Braithwaiteaf3d5bd2015-04-24 17:40:19 -070018#include <limits.h>
Adam Langley310d4dd2015-04-13 11:04:21 -070019#include <string.h>
20
David Benjamin1db476e2015-06-17 00:53:09 -040021#include <openssl/chacha.h>
Adam Langleyd7554562015-09-24 15:03:14 -070022#include <openssl/cpu.h>
Adam Langley310d4dd2015-04-13 11:04:21 -070023#include <openssl/mem.h>
24
25#include "internal.h"
26#include "../internal.h"
27
28
29/* It's assumed that the operating system always has an unfailing source of
30 * entropy which is accessed via |CRYPTO_sysrand|. (If the operating system
31 * entropy source fails, it's up to |CRYPTO_sysrand| to abort the process—we
32 * don't try to handle it.)
33 *
34 * In addition, the hardware may provide a low-latency RNG. Intel's rdrand
35 * instruction is the canonical example of this. When a hardware RNG is
36 * available we don't need to worry about an RNG failure arising from fork()ing
Adam Langley88bb8482017-04-10 16:53:20 -070037 * the process or moving a VM, so we can keep thread-local RNG state and use it
38 * as an additional-data input to CTR-DRBG.
Adam Langley310d4dd2015-04-13 11:04:21 -070039 *
40 * (We assume that the OS entropy is safe from fork()ing and VM duplication.
41 * This might be a bit of a leap of faith, esp on Windows, but there's nothing
42 * that we can do about it.) */
43
Adam Langley88bb8482017-04-10 16:53:20 -070044/* kReseedInterval is the number of generate calls made to CTR-DRBG before
45 * reseeding. */
46static const unsigned kReseedInterval = 4096;
47
48/* rand_thread_state contains the per-thread state for the RNG. */
Adam Langley310d4dd2015-04-13 11:04:21 -070049struct rand_thread_state {
Adam Langley88bb8482017-04-10 16:53:20 -070050 CTR_DRBG_STATE drbg;
51 /* calls is the number of generate calls made on |drbg| since it was last
52 * (re)seeded. This is bound by |kReseedInterval|. */
53 unsigned calls;
Adam Langley310d4dd2015-04-13 11:04:21 -070054};
55
Adam Langley310d4dd2015-04-13 11:04:21 -070056/* rand_thread_state_free frees a |rand_thread_state|. This is called when a
57 * thread exits. */
Adam Langley88bb8482017-04-10 16:53:20 -070058static void rand_thread_state_free(void *state_in) {
59 if (state_in == NULL) {
Adam Langley310d4dd2015-04-13 11:04:21 -070060 return;
61 }
62
Adam Langley88bb8482017-04-10 16:53:20 -070063 struct rand_thread_state *state = state_in;
64 CTR_DRBG_clear(&state->drbg);
Adam Langley310d4dd2015-04-13 11:04:21 -070065 OPENSSL_free(state);
66}
67
David Benjaminbc5b2a22016-03-01 22:57:32 -050068#if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \
David Benjaminec978dd2016-11-04 18:59:33 -040069 !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
Adam Langleyd7554562015-09-24 15:03:14 -070070
71/* These functions are defined in asm/rdrand-x86_64.pl */
72extern int CRYPTO_rdrand(uint8_t out[8]);
73extern int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len);
74
75static int have_rdrand(void) {
76 return (OPENSSL_ia32cap_P[1] & (1u << 30)) != 0;
77}
78
79static int hwrand(uint8_t *buf, size_t len) {
80 if (!have_rdrand()) {
81 return 0;
82 }
83
84 const size_t len_multiple8 = len & ~7;
85 if (!CRYPTO_rdrand_multiple8_buf(buf, len_multiple8)) {
86 return 0;
87 }
88 len -= len_multiple8;
89
90 if (len != 0) {
91 assert(len < 8);
92
93 uint8_t rand_buf[8];
94 if (!CRYPTO_rdrand(rand_buf)) {
95 return 0;
96 }
David Benjamin17cf2cb2016-12-13 01:07:13 -050097 OPENSSL_memcpy(buf + len_multiple8, rand_buf, len);
Adam Langleyd7554562015-09-24 15:03:14 -070098 }
99
100 return 1;
101}
102
103#else
104
105static int hwrand(uint8_t *buf, size_t len) {
106 return 0;
107}
108
109#endif
110
Adam Langley88bb8482017-04-10 16:53:20 -0700111#if defined(BORINGSSL_FIPS)
112
113static void rand_get_seed(uint8_t seed[CTR_DRBG_ENTROPY_LEN]) {
114 /* We overread from /dev/urandom by a factor of 10 and XOR to whiten. */
115#define FIPS_OVERREAD 10
116 uint8_t entropy[CTR_DRBG_ENTROPY_LEN * FIPS_OVERREAD];
117 CRYPTO_sysrand(entropy, sizeof(entropy));
118
119 OPENSSL_memcpy(seed, entropy, CTR_DRBG_ENTROPY_LEN);
120
121 for (size_t i = 1; i < FIPS_OVERREAD; i++) {
122 for (size_t j = 0; j < CTR_DRBG_ENTROPY_LEN; j++) {
123 seed[j] ^= entropy[CTR_DRBG_ENTROPY_LEN * i + j];
124 }
125 }
126}
127
128#else
129
130static void rand_get_seed(uint8_t seed[CTR_DRBG_ENTROPY_LEN]) {
131 /* If not in FIPS mode, we don't overread from the system entropy source. */
132 CRYPTO_sysrand(seed, CTR_DRBG_ENTROPY_LEN);
133}
134
135#endif
136
137void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len,
138 const uint8_t user_additional_data[32]) {
139 if (out_len == 0) {
140 return;
Adam Langley310d4dd2015-04-13 11:04:21 -0700141 }
142
Adam Langley88bb8482017-04-10 16:53:20 -0700143 struct rand_thread_state stack_state;
Adam Langley310d4dd2015-04-13 11:04:21 -0700144 struct rand_thread_state *state =
145 CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND);
Adam Langley88bb8482017-04-10 16:53:20 -0700146
Adam Langley310d4dd2015-04-13 11:04:21 -0700147 if (state == NULL) {
148 state = OPENSSL_malloc(sizeof(struct rand_thread_state));
149 if (state == NULL ||
150 !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state,
151 rand_thread_state_free)) {
Adam Langley88bb8482017-04-10 16:53:20 -0700152 /* If the system is out of memory, use an ephemeral state on the
153 * stack. */
154 state = &stack_state;
Adam Langley310d4dd2015-04-13 11:04:21 -0700155 }
156
Adam Langley88bb8482017-04-10 16:53:20 -0700157 uint8_t seed[CTR_DRBG_ENTROPY_LEN];
158 rand_get_seed(seed);
159 if (!CTR_DRBG_init(&state->drbg, seed, NULL, 0)) {
160 abort();
161 }
162 state->calls = 0;
Adam Langley310d4dd2015-04-13 11:04:21 -0700163 }
164
Adam Langley88bb8482017-04-10 16:53:20 -0700165 if (state->calls >= kReseedInterval) {
166 uint8_t seed[CTR_DRBG_ENTROPY_LEN];
167 rand_get_seed(seed);
168 if (!CTR_DRBG_reseed(&state->drbg, seed, NULL, 0)) {
169 abort();
170 }
171 state->calls = 0;
Adam Langley310d4dd2015-04-13 11:04:21 -0700172 }
173
Adam Langley88bb8482017-04-10 16:53:20 -0700174 /* Additional data is mixed into every CTR-DRBG call to protect, as best we
175 * can, against forks & VM clones. We do not over-read this information and
176 * don't reseed with it so, from the point of view of FIPS, this doesn't
177 * provide “prediction resistance”. But, in practice, it does. */
178 uint8_t additional_data[32];
179 if (!hwrand(additional_data, sizeof(additional_data))) {
180 /* Without a hardware RNG to save us from address-space duplication, the OS
181 * entropy is used. */
182 CRYPTO_sysrand(additional_data, sizeof(additional_data));
Adam Langley310d4dd2015-04-13 11:04:21 -0700183 }
Adam Langley310d4dd2015-04-13 11:04:21 -0700184
Adam Langley88bb8482017-04-10 16:53:20 -0700185 for (size_t i = 0; i < sizeof(additional_data); i++) {
186 additional_data[i] ^= user_additional_data[i];
187 }
188
189 int first_call = 1;
190 while (out_len > 0) {
191 size_t todo = out_len;
192 if (todo > CTR_DRBG_MAX_GENERATE_LENGTH) {
193 todo = CTR_DRBG_MAX_GENERATE_LENGTH;
194 }
195
196 if (!CTR_DRBG_generate(&state->drbg, out, todo, additional_data,
197 first_call ? sizeof(additional_data) : 0)) {
198 abort();
199 }
200
201 out += todo;
202 out_len -= todo;
203 state->calls++;
204 first_call = 0;
205 }
206
207 if (state == &stack_state) {
208 CTR_DRBG_clear(&state->drbg);
209 }
210
211 return;
212}
213
214int RAND_bytes(uint8_t *out, size_t out_len) {
215 static const uint8_t kZeroAdditionalData[32] = {0};
216 RAND_bytes_with_additional_data(out, out_len, kZeroAdditionalData);
Adam Langley310d4dd2015-04-13 11:04:21 -0700217 return 1;
218}
Adam Langley95c29f32014-06-20 12:00:00 -0700219
220int RAND_pseudo_bytes(uint8_t *buf, size_t len) {
221 return RAND_bytes(buf, len);
222}
223
Adam Langleyc5c85de2015-11-16 10:10:59 -0800224void RAND_seed(const void *buf, int num) {
225 /* OpenSSH calls |RAND_seed| before jailing on the assumption that any needed
226 * file descriptors etc will be opened. */
227 uint8_t unused;
228 RAND_bytes(&unused, sizeof(unused));
229}
Adam Langley95c29f32014-06-20 12:00:00 -0700230
Matt Braithwaiteaf3d5bd2015-04-24 17:40:19 -0700231int RAND_load_file(const char *path, long num) {
232 if (num < 0) { /* read the "whole file" */
233 return 1;
234 } else if (num <= INT_MAX) {
235 return (int) num;
236 } else {
237 return INT_MAX;
238 }
239}
240
David Benjamine5aa7912016-01-26 01:09:19 -0500241const char *RAND_file_name(char *buf, size_t num) { return NULL; }
242
Adam Langley95c29f32014-06-20 12:00:00 -0700243void RAND_add(const void *buf, int num, double entropy) {}
244
Matt Braithwaite3e5e99d2015-06-24 16:41:10 -0700245int RAND_egd(const char *path) {
246 return 255;
247}
248
Adam Langley95c29f32014-06-20 12:00:00 -0700249int RAND_poll(void) {
250 return 1;
251}
Adam Langleyc3ef76f2015-04-13 14:34:17 -0700252
253int RAND_status(void) {
254 return 1;
255}
Matt Braithwaite3e5e99d2015-06-24 16:41:10 -0700256
Matt Braithwaitebc97c692015-07-22 18:16:18 -0700257static const struct rand_meth_st kSSLeayMethod = {
258 RAND_seed,
259 RAND_bytes,
260 RAND_cleanup,
261 RAND_add,
262 RAND_pseudo_bytes,
263 RAND_status,
264};
Matt Braithwaite3e5e99d2015-06-24 16:41:10 -0700265
266RAND_METHOD *RAND_SSLeay(void) {
267 return (RAND_METHOD*) &kSSLeayMethod;
268}
269
Adam Langleya59347e2015-06-24 17:02:15 -0700270void RAND_set_rand_method(const RAND_METHOD *method) {}
Alessandro Ghedini0d099f02016-07-10 01:57:44 +0100271
272void RAND_cleanup(void) {}