blob: 51da6ba27b422ce40c8a106cc43489570c88b64a [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
37 * the process or moving a VM, so we can keep thread-local RNG state and XOR
38 * the hardware entropy in.
39 *
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
44/* rand_thread_state contains the per-thread state for the RNG. This is only
45 * used if the system has support for a hardware RNG. */
46struct rand_thread_state {
47 uint8_t key[32];
48 uint64_t calls_used;
49 size_t bytes_used;
50 uint8_t partial_block[64];
51 unsigned partial_block_used;
52};
53
54/* kMaxCallsPerRefresh is the maximum number of |RAND_bytes| calls that we'll
55 * serve before reading a new key from the operating system. This only applies
56 * if we have a hardware RNG. */
57static const unsigned kMaxCallsPerRefresh = 1024;
58
59/* kMaxBytesPerRefresh is the maximum number of bytes that we'll return from
60 * |RAND_bytes| before reading a new key from the operating system. This only
61 * applies if we have a hardware RNG. */
62static const uint64_t kMaxBytesPerRefresh = 1024 * 1024;
63
64/* rand_thread_state_free frees a |rand_thread_state|. This is called when a
65 * thread exits. */
66static void rand_thread_state_free(void *state) {
67 if (state == NULL) {
68 return;
69 }
70
71 OPENSSL_cleanse(state, sizeof(struct rand_thread_state));
72 OPENSSL_free(state);
73}
74
David Benjaminbc5b2a22016-03-01 22:57:32 -050075#if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \
David Benjaminec978dd2016-11-04 18:59:33 -040076 !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
Adam Langleyd7554562015-09-24 15:03:14 -070077
78/* These functions are defined in asm/rdrand-x86_64.pl */
79extern int CRYPTO_rdrand(uint8_t out[8]);
80extern int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len);
81
82static int have_rdrand(void) {
83 return (OPENSSL_ia32cap_P[1] & (1u << 30)) != 0;
84}
85
86static int hwrand(uint8_t *buf, size_t len) {
87 if (!have_rdrand()) {
88 return 0;
89 }
90
91 const size_t len_multiple8 = len & ~7;
92 if (!CRYPTO_rdrand_multiple8_buf(buf, len_multiple8)) {
93 return 0;
94 }
95 len -= len_multiple8;
96
97 if (len != 0) {
98 assert(len < 8);
99
100 uint8_t rand_buf[8];
101 if (!CRYPTO_rdrand(rand_buf)) {
102 return 0;
103 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500104 OPENSSL_memcpy(buf + len_multiple8, rand_buf, len);
Adam Langleyd7554562015-09-24 15:03:14 -0700105 }
106
107 return 1;
108}
109
110#else
111
112static int hwrand(uint8_t *buf, size_t len) {
113 return 0;
114}
115
116#endif
117
Brian Smith37007782015-04-14 16:21:27 -1000118int RAND_bytes(uint8_t *buf, size_t len) {
Adam Langley310d4dd2015-04-13 11:04:21 -0700119 if (len == 0) {
120 return 1;
121 }
122
Adam Langleyd7554562015-09-24 15:03:14 -0700123 if (!hwrand(buf, len)) {
Adam Langley310d4dd2015-04-13 11:04:21 -0700124 /* Without a hardware RNG to save us from address-space duplication, the OS
125 * entropy is used directly. */
126 CRYPTO_sysrand(buf, len);
127 return 1;
128 }
129
130 struct rand_thread_state *state =
131 CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND);
132 if (state == NULL) {
133 state = OPENSSL_malloc(sizeof(struct rand_thread_state));
134 if (state == NULL ||
135 !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state,
136 rand_thread_state_free)) {
137 CRYPTO_sysrand(buf, len);
138 return 1;
139 }
140
David Benjamin17cf2cb2016-12-13 01:07:13 -0500141 OPENSSL_memset(state->partial_block, 0, sizeof(state->partial_block));
Adam Langley310d4dd2015-04-13 11:04:21 -0700142 state->calls_used = kMaxCallsPerRefresh;
143 }
144
145 if (state->calls_used >= kMaxCallsPerRefresh ||
146 state->bytes_used >= kMaxBytesPerRefresh) {
147 CRYPTO_sysrand(state->key, sizeof(state->key));
148 state->calls_used = 0;
149 state->bytes_used = 0;
150 state->partial_block_used = sizeof(state->partial_block);
151 }
152
Adam Langley310d4dd2015-04-13 11:04:21 -0700153 if (len >= sizeof(state->partial_block)) {
154 size_t remaining = len;
155 while (remaining > 0) {
David Benjamin1b92f642015-10-17 22:26:54 -0400156 /* kMaxBytesPerCall is only 2GB, while ChaCha can handle 256GB. But this
157 * is sufficient and easier on 32-bit. */
Adam Langley310d4dd2015-04-13 11:04:21 -0700158 static const size_t kMaxBytesPerCall = 0x80000000;
159 size_t todo = remaining;
160 if (todo > kMaxBytesPerCall) {
161 todo = kMaxBytesPerCall;
162 }
Brian Smithe80a2ec2015-10-03 08:13:36 -1000163 uint8_t nonce[12];
David Benjamin17cf2cb2016-12-13 01:07:13 -0500164 OPENSSL_memset(nonce, 0, 4);
165 OPENSSL_memcpy(nonce + 4, &state->calls_used, sizeof(state->calls_used));
Brian Smithe80a2ec2015-10-03 08:13:36 -1000166 CRYPTO_chacha_20(buf, buf, todo, state->key, nonce, 0);
Adam Langley310d4dd2015-04-13 11:04:21 -0700167 buf += todo;
168 remaining -= todo;
169 state->calls_used++;
170 }
171 } else {
172 if (sizeof(state->partial_block) - state->partial_block_used < len) {
Brian Smithe80a2ec2015-10-03 08:13:36 -1000173 uint8_t nonce[12];
David Benjamin17cf2cb2016-12-13 01:07:13 -0500174 OPENSSL_memset(nonce, 0, 4);
175 OPENSSL_memcpy(nonce + 4, &state->calls_used, sizeof(state->calls_used));
Adam Langley310d4dd2015-04-13 11:04:21 -0700176 CRYPTO_chacha_20(state->partial_block, state->partial_block,
Brian Smithe80a2ec2015-10-03 08:13:36 -1000177 sizeof(state->partial_block), state->key, nonce, 0);
Adam Langley310d4dd2015-04-13 11:04:21 -0700178 state->partial_block_used = 0;
179 }
180
181 unsigned i;
182 for (i = 0; i < len; i++) {
183 buf[i] ^= state->partial_block[state->partial_block_used++];
184 }
185 state->calls_used++;
186 }
187 state->bytes_used += len;
188
189 return 1;
190}
Adam Langley95c29f32014-06-20 12:00:00 -0700191
192int RAND_pseudo_bytes(uint8_t *buf, size_t len) {
193 return RAND_bytes(buf, len);
194}
195
Adam Langleyc5c85de2015-11-16 10:10:59 -0800196void RAND_seed(const void *buf, int num) {
197 /* OpenSSH calls |RAND_seed| before jailing on the assumption that any needed
198 * file descriptors etc will be opened. */
199 uint8_t unused;
200 RAND_bytes(&unused, sizeof(unused));
201}
Adam Langley95c29f32014-06-20 12:00:00 -0700202
Matt Braithwaiteaf3d5bd2015-04-24 17:40:19 -0700203int RAND_load_file(const char *path, long num) {
204 if (num < 0) { /* read the "whole file" */
205 return 1;
206 } else if (num <= INT_MAX) {
207 return (int) num;
208 } else {
209 return INT_MAX;
210 }
211}
212
David Benjamine5aa7912016-01-26 01:09:19 -0500213const char *RAND_file_name(char *buf, size_t num) { return NULL; }
214
Adam Langley95c29f32014-06-20 12:00:00 -0700215void RAND_add(const void *buf, int num, double entropy) {}
216
Matt Braithwaite3e5e99d2015-06-24 16:41:10 -0700217int RAND_egd(const char *path) {
218 return 255;
219}
220
Adam Langley95c29f32014-06-20 12:00:00 -0700221int RAND_poll(void) {
222 return 1;
223}
Adam Langleyc3ef76f2015-04-13 14:34:17 -0700224
225int RAND_status(void) {
226 return 1;
227}
Matt Braithwaite3e5e99d2015-06-24 16:41:10 -0700228
Matt Braithwaitebc97c692015-07-22 18:16:18 -0700229static const struct rand_meth_st kSSLeayMethod = {
230 RAND_seed,
231 RAND_bytes,
232 RAND_cleanup,
233 RAND_add,
234 RAND_pseudo_bytes,
235 RAND_status,
236};
Matt Braithwaite3e5e99d2015-06-24 16:41:10 -0700237
238RAND_METHOD *RAND_SSLeay(void) {
239 return (RAND_METHOD*) &kSSLeayMethod;
240}
241
Adam Langleya59347e2015-06-24 17:02:15 -0700242void RAND_set_rand_method(const RAND_METHOD *method) {}
Alessandro Ghedini0d099f02016-07-10 01:57:44 +0100243
244void RAND_cleanup(void) {}