blob: 0d146a89edd9673f97d70c5320cfe74590d91665 [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
Matt Braithwaiteaf3d5bd2015-04-24 17:40:19 -070017#include <limits.h>
Adam Langley310d4dd2015-04-13 11:04:21 -070018#include <string.h>
19
David Benjamin1db476e2015-06-17 00:53:09 -040020#include <openssl/chacha.h>
Adam Langley310d4dd2015-04-13 11:04:21 -070021#include <openssl/mem.h>
22
23#include "internal.h"
24#include "../internal.h"
25
26
27/* It's assumed that the operating system always has an unfailing source of
28 * entropy which is accessed via |CRYPTO_sysrand|. (If the operating system
29 * entropy source fails, it's up to |CRYPTO_sysrand| to abort the process—we
30 * don't try to handle it.)
31 *
32 * In addition, the hardware may provide a low-latency RNG. Intel's rdrand
33 * instruction is the canonical example of this. When a hardware RNG is
34 * available we don't need to worry about an RNG failure arising from fork()ing
35 * the process or moving a VM, so we can keep thread-local RNG state and XOR
36 * the hardware entropy in.
37 *
38 * (We assume that the OS entropy is safe from fork()ing and VM duplication.
39 * This might be a bit of a leap of faith, esp on Windows, but there's nothing
40 * that we can do about it.) */
41
42/* rand_thread_state contains the per-thread state for the RNG. This is only
43 * used if the system has support for a hardware RNG. */
44struct rand_thread_state {
45 uint8_t key[32];
46 uint64_t calls_used;
47 size_t bytes_used;
48 uint8_t partial_block[64];
49 unsigned partial_block_used;
50};
51
52/* kMaxCallsPerRefresh is the maximum number of |RAND_bytes| calls that we'll
53 * serve before reading a new key from the operating system. This only applies
54 * if we have a hardware RNG. */
55static const unsigned kMaxCallsPerRefresh = 1024;
56
57/* kMaxBytesPerRefresh is the maximum number of bytes that we'll return from
58 * |RAND_bytes| before reading a new key from the operating system. This only
59 * applies if we have a hardware RNG. */
60static const uint64_t kMaxBytesPerRefresh = 1024 * 1024;
61
62/* rand_thread_state_free frees a |rand_thread_state|. This is called when a
63 * thread exits. */
64static void rand_thread_state_free(void *state) {
65 if (state == NULL) {
66 return;
67 }
68
69 OPENSSL_cleanse(state, sizeof(struct rand_thread_state));
70 OPENSSL_free(state);
71}
72
Brian Smith37007782015-04-14 16:21:27 -100073int RAND_bytes(uint8_t *buf, size_t len) {
Adam Langley310d4dd2015-04-13 11:04:21 -070074 if (len == 0) {
75 return 1;
76 }
77
Adam Langley2cac3502015-06-19 11:06:28 -070078 if (!CRYPTO_have_hwrand() ||
79 !CRYPTO_hwrand(buf, len)) {
Adam Langley310d4dd2015-04-13 11:04:21 -070080 /* Without a hardware RNG to save us from address-space duplication, the OS
81 * entropy is used directly. */
82 CRYPTO_sysrand(buf, len);
83 return 1;
84 }
85
86 struct rand_thread_state *state =
87 CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND);
88 if (state == NULL) {
89 state = OPENSSL_malloc(sizeof(struct rand_thread_state));
90 if (state == NULL ||
91 !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state,
92 rand_thread_state_free)) {
93 CRYPTO_sysrand(buf, len);
94 return 1;
95 }
96
David Benjamin81091d52015-05-15 15:50:22 -040097 memset(state->partial_block, 0, sizeof(state->partial_block));
Adam Langley310d4dd2015-04-13 11:04:21 -070098 state->calls_used = kMaxCallsPerRefresh;
99 }
100
101 if (state->calls_used >= kMaxCallsPerRefresh ||
102 state->bytes_used >= kMaxBytesPerRefresh) {
103 CRYPTO_sysrand(state->key, sizeof(state->key));
104 state->calls_used = 0;
105 state->bytes_used = 0;
106 state->partial_block_used = sizeof(state->partial_block);
107 }
108
Adam Langley310d4dd2015-04-13 11:04:21 -0700109 if (len >= sizeof(state->partial_block)) {
110 size_t remaining = len;
111 while (remaining > 0) {
112 // kMaxBytesPerCall is only 2GB, while ChaCha can handle 256GB. But this
113 // is sufficient and easier on 32-bit.
114 static const size_t kMaxBytesPerCall = 0x80000000;
115 size_t todo = remaining;
116 if (todo > kMaxBytesPerCall) {
117 todo = kMaxBytesPerCall;
118 }
119 CRYPTO_chacha_20(buf, buf, todo, state->key,
120 (uint8_t *)&state->calls_used, 0);
121 buf += todo;
122 remaining -= todo;
123 state->calls_used++;
124 }
125 } else {
126 if (sizeof(state->partial_block) - state->partial_block_used < len) {
127 CRYPTO_chacha_20(state->partial_block, state->partial_block,
128 sizeof(state->partial_block), state->key,
129 (uint8_t *)&state->calls_used, 0);
130 state->partial_block_used = 0;
131 }
132
133 unsigned i;
134 for (i = 0; i < len; i++) {
135 buf[i] ^= state->partial_block[state->partial_block_used++];
136 }
137 state->calls_used++;
138 }
139 state->bytes_used += len;
140
141 return 1;
142}
Adam Langley95c29f32014-06-20 12:00:00 -0700143
144int RAND_pseudo_bytes(uint8_t *buf, size_t len) {
145 return RAND_bytes(buf, len);
146}
147
148void RAND_seed(const void *buf, int num) {}
149
Matt Braithwaiteaf3d5bd2015-04-24 17:40:19 -0700150int RAND_load_file(const char *path, long num) {
151 if (num < 0) { /* read the "whole file" */
152 return 1;
153 } else if (num <= INT_MAX) {
154 return (int) num;
155 } else {
156 return INT_MAX;
157 }
158}
159
Adam Langley95c29f32014-06-20 12:00:00 -0700160void RAND_add(const void *buf, int num, double entropy) {}
161
Matt Braithwaite3e5e99d2015-06-24 16:41:10 -0700162int RAND_egd(const char *path) {
163 return 255;
164}
165
Adam Langley95c29f32014-06-20 12:00:00 -0700166int RAND_poll(void) {
167 return 1;
168}
Adam Langleyc3ef76f2015-04-13 14:34:17 -0700169
170int RAND_status(void) {
171 return 1;
172}
Matt Braithwaite3e5e99d2015-06-24 16:41:10 -0700173
174static const struct rand_meth_st kSSLeayMethod = {NULL, NULL, NULL,
175 NULL, NULL, NULL};
176
177RAND_METHOD *RAND_SSLeay(void) {
178 return (RAND_METHOD*) &kSSLeayMethod;
179}
180
Adam Langleya59347e2015-06-24 17:02:15 -0700181void RAND_set_rand_method(const RAND_METHOD *method) {}