blob: 4f5e5791a4cbf43deccbf4b24dc90b5b74a4659b [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"
Adam Langley77841042017-04-14 11:16:20 -070026#include "../../internal.h"
Adam Langley310d4dd2015-04-13 11:04:21 -070027
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
Adam Langley90ada2f2017-04-11 16:19:27 -070048/* CRNGT_BLOCK_SIZE is the number of bytes in a “block” for the purposes of the
49 * continuous random number generator test in FIPS 140-2, section 4.9.2. */
50#define CRNGT_BLOCK_SIZE 16
51
Adam Langley88bb8482017-04-10 16:53:20 -070052/* rand_thread_state contains the per-thread state for the RNG. */
Adam Langley310d4dd2015-04-13 11:04:21 -070053struct rand_thread_state {
Adam Langley88bb8482017-04-10 16:53:20 -070054 CTR_DRBG_STATE drbg;
55 /* calls is the number of generate calls made on |drbg| since it was last
56 * (re)seeded. This is bound by |kReseedInterval|. */
57 unsigned calls;
Adam Langley90ada2f2017-04-11 16:19:27 -070058 /* last_block contains the previous block from |CRYPTO_sysrand|. */
59 uint8_t last_block[CRNGT_BLOCK_SIZE];
60 /* last_block_valid is non-zero iff |last_block| contains data from
61 * |CRYPTO_sysrand|. */
62 int last_block_valid;
Adam Langley310d4dd2015-04-13 11:04:21 -070063};
64
Adam Langley310d4dd2015-04-13 11:04:21 -070065/* rand_thread_state_free frees a |rand_thread_state|. This is called when a
66 * thread exits. */
Adam Langley88bb8482017-04-10 16:53:20 -070067static void rand_thread_state_free(void *state_in) {
68 if (state_in == NULL) {
Adam Langley310d4dd2015-04-13 11:04:21 -070069 return;
70 }
71
Adam Langley88bb8482017-04-10 16:53:20 -070072 struct rand_thread_state *state = state_in;
73 CTR_DRBG_clear(&state->drbg);
Adam Langley310d4dd2015-04-13 11:04:21 -070074 OPENSSL_free(state);
75}
76
David Benjaminbc5b2a22016-03-01 22:57:32 -050077#if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \
David Benjaminec978dd2016-11-04 18:59:33 -040078 !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
Adam Langleyd7554562015-09-24 15:03:14 -070079
80/* These functions are defined in asm/rdrand-x86_64.pl */
81extern int CRYPTO_rdrand(uint8_t out[8]);
82extern int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len);
83
84static int have_rdrand(void) {
David Benjamin91871012017-04-25 15:37:53 -040085 return (OPENSSL_ia32cap_get()[1] & (1u << 30)) != 0;
Adam Langleyd7554562015-09-24 15:03:14 -070086}
87
88static int hwrand(uint8_t *buf, size_t len) {
89 if (!have_rdrand()) {
90 return 0;
91 }
92
93 const size_t len_multiple8 = len & ~7;
94 if (!CRYPTO_rdrand_multiple8_buf(buf, len_multiple8)) {
95 return 0;
96 }
97 len -= len_multiple8;
98
99 if (len != 0) {
100 assert(len < 8);
101
102 uint8_t rand_buf[8];
103 if (!CRYPTO_rdrand(rand_buf)) {
104 return 0;
105 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500106 OPENSSL_memcpy(buf + len_multiple8, rand_buf, len);
Adam Langleyd7554562015-09-24 15:03:14 -0700107 }
108
109 return 1;
110}
111
112#else
113
114static int hwrand(uint8_t *buf, size_t len) {
115 return 0;
116}
117
118#endif
119
Adam Langley88bb8482017-04-10 16:53:20 -0700120#if defined(BORINGSSL_FIPS)
121
Adam Langley90ada2f2017-04-11 16:19:27 -0700122static void rand_get_seed(struct rand_thread_state *state,
123 uint8_t seed[CTR_DRBG_ENTROPY_LEN]) {
124 if (!state->last_block_valid) {
125 CRYPTO_sysrand(state->last_block, sizeof(state->last_block));
126 state->last_block_valid = 1;
127 }
128
Adam Langley88bb8482017-04-10 16:53:20 -0700129 /* We overread from /dev/urandom by a factor of 10 and XOR to whiten. */
130#define FIPS_OVERREAD 10
131 uint8_t entropy[CTR_DRBG_ENTROPY_LEN * FIPS_OVERREAD];
132 CRYPTO_sysrand(entropy, sizeof(entropy));
133
Adam Langley90ada2f2017-04-11 16:19:27 -0700134 /* See FIPS 140-2, section 4.9.2. This is the “continuous random number
135 * generator test” which causes the program to randomly abort. Hopefully the
136 * rate of failure is small enough not to be a problem in practice. */
137 if (CRYPTO_memcmp(state->last_block, entropy, CRNGT_BLOCK_SIZE) == 0) {
138 abort();
139 }
140
141 for (size_t i = CRNGT_BLOCK_SIZE; i < sizeof(entropy);
142 i += CRNGT_BLOCK_SIZE) {
143 if (CRYPTO_memcmp(entropy + i - CRNGT_BLOCK_SIZE, entropy + i,
144 CRNGT_BLOCK_SIZE) == 0) {
145 abort();
146 }
147 }
148 OPENSSL_memcpy(state->last_block,
149 entropy + sizeof(entropy) - CRNGT_BLOCK_SIZE,
150 CRNGT_BLOCK_SIZE);
151
Adam Langley88bb8482017-04-10 16:53:20 -0700152 OPENSSL_memcpy(seed, entropy, CTR_DRBG_ENTROPY_LEN);
153
154 for (size_t i = 1; i < FIPS_OVERREAD; i++) {
155 for (size_t j = 0; j < CTR_DRBG_ENTROPY_LEN; j++) {
156 seed[j] ^= entropy[CTR_DRBG_ENTROPY_LEN * i + j];
157 }
158 }
159}
160
161#else
162
Adam Langley90ada2f2017-04-11 16:19:27 -0700163static void rand_get_seed(struct rand_thread_state *state,
164 uint8_t seed[CTR_DRBG_ENTROPY_LEN]) {
Adam Langley88bb8482017-04-10 16:53:20 -0700165 /* If not in FIPS mode, we don't overread from the system entropy source. */
166 CRYPTO_sysrand(seed, CTR_DRBG_ENTROPY_LEN);
167}
168
169#endif
170
171void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len,
172 const uint8_t user_additional_data[32]) {
173 if (out_len == 0) {
174 return;
Adam Langley310d4dd2015-04-13 11:04:21 -0700175 }
176
Adam Langley88bb8482017-04-10 16:53:20 -0700177 struct rand_thread_state stack_state;
Adam Langley310d4dd2015-04-13 11:04:21 -0700178 struct rand_thread_state *state =
179 CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND);
Adam Langley88bb8482017-04-10 16:53:20 -0700180
Adam Langley310d4dd2015-04-13 11:04:21 -0700181 if (state == NULL) {
182 state = OPENSSL_malloc(sizeof(struct rand_thread_state));
183 if (state == NULL ||
184 !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state,
185 rand_thread_state_free)) {
Adam Langley88bb8482017-04-10 16:53:20 -0700186 /* If the system is out of memory, use an ephemeral state on the
187 * stack. */
188 state = &stack_state;
Adam Langley310d4dd2015-04-13 11:04:21 -0700189 }
190
Adam Langley90ada2f2017-04-11 16:19:27 -0700191 state->last_block_valid = 0;
Adam Langley88bb8482017-04-10 16:53:20 -0700192 uint8_t seed[CTR_DRBG_ENTROPY_LEN];
Adam Langley90ada2f2017-04-11 16:19:27 -0700193 rand_get_seed(state, seed);
Adam Langley88bb8482017-04-10 16:53:20 -0700194 if (!CTR_DRBG_init(&state->drbg, seed, NULL, 0)) {
195 abort();
196 }
197 state->calls = 0;
Adam Langley310d4dd2015-04-13 11:04:21 -0700198 }
199
Adam Langley88bb8482017-04-10 16:53:20 -0700200 if (state->calls >= kReseedInterval) {
201 uint8_t seed[CTR_DRBG_ENTROPY_LEN];
Adam Langley90ada2f2017-04-11 16:19:27 -0700202 rand_get_seed(state, seed);
Adam Langley88bb8482017-04-10 16:53:20 -0700203 if (!CTR_DRBG_reseed(&state->drbg, seed, NULL, 0)) {
204 abort();
205 }
206 state->calls = 0;
Adam Langley310d4dd2015-04-13 11:04:21 -0700207 }
208
Adam Langley88bb8482017-04-10 16:53:20 -0700209 /* Additional data is mixed into every CTR-DRBG call to protect, as best we
210 * can, against forks & VM clones. We do not over-read this information and
211 * don't reseed with it so, from the point of view of FIPS, this doesn't
212 * provide “prediction resistance”. But, in practice, it does. */
213 uint8_t additional_data[32];
214 if (!hwrand(additional_data, sizeof(additional_data))) {
215 /* Without a hardware RNG to save us from address-space duplication, the OS
Adam Langley92f888e2017-04-11 15:06:47 -0700216 * entropy is used. This can be expensive (one read per |RAND_bytes| call)
217 * and so can be disabled by applications that we have ensured don't fork
218 * and aren't at risk of VM cloning. */
219 if (!rand_fork_unsafe_buffering_enabled()) {
220 CRYPTO_sysrand(additional_data, sizeof(additional_data));
221 } else {
222 OPENSSL_memset(additional_data, 0, sizeof(additional_data));
223 }
Adam Langley310d4dd2015-04-13 11:04:21 -0700224 }
Adam Langley310d4dd2015-04-13 11:04:21 -0700225
Adam Langley88bb8482017-04-10 16:53:20 -0700226 for (size_t i = 0; i < sizeof(additional_data); i++) {
227 additional_data[i] ^= user_additional_data[i];
228 }
229
230 int first_call = 1;
231 while (out_len > 0) {
232 size_t todo = out_len;
233 if (todo > CTR_DRBG_MAX_GENERATE_LENGTH) {
234 todo = CTR_DRBG_MAX_GENERATE_LENGTH;
235 }
236
237 if (!CTR_DRBG_generate(&state->drbg, out, todo, additional_data,
238 first_call ? sizeof(additional_data) : 0)) {
239 abort();
240 }
241
242 out += todo;
243 out_len -= todo;
244 state->calls++;
245 first_call = 0;
246 }
247
248 if (state == &stack_state) {
249 CTR_DRBG_clear(&state->drbg);
250 }
251
252 return;
253}
254
255int RAND_bytes(uint8_t *out, size_t out_len) {
256 static const uint8_t kZeroAdditionalData[32] = {0};
257 RAND_bytes_with_additional_data(out, out_len, kZeroAdditionalData);
Adam Langley310d4dd2015-04-13 11:04:21 -0700258 return 1;
259}
Adam Langley95c29f32014-06-20 12:00:00 -0700260
261int RAND_pseudo_bytes(uint8_t *buf, size_t len) {
262 return RAND_bytes(buf, len);
263}