blob: 10e5fe9857ad8c0f609b55f64220c11f7af60107 [file] [log] [blame]
xf.lif1aed282024-02-06 00:31:51 -08001/* $OpenBSD: arc4random.c,v 1.54 2015/09/13 08:31:47 guenther Exp $ */
2
3/*
4 * Copyright (c) 1996, David Mazieres <dm@uun.org>
5 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
6 * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
7 * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22/*
23 * ChaCha based random number generator for OpenBSD.
24 */
25
26#include <fcntl.h>
27#include <limits.h>
28#include <signal.h>
29#include <stdint.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33#include <sys/types.h>
34#include <sys/time.h>
35
36//#define KEYSTREAM_ONLY
37#include "chacha_private.h"
38
39#define minimum(a, b) ((a) < (b) ? (a) : (b))
40#define explicit_bzero(p, s) memset(p, 0, s)
41
42#if defined(__GNUC__) || defined(_MSC_VER)
43#define inline __inline
44#else /* __GNUC__ || _MSC_VER */
45#define inline
46#endif /* !__GNUC__ && !_MSC_VER */
47
48#define KEYSZ 32
49#define IVSZ 8
50#define BLOCKSZ 64
51#define RSBUFSZ (16*BLOCKSZ)
52
53/* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */
54static struct _rs {
55 size_t rs_have; /* valid bytes at end of rs_buf */
56 size_t rs_count; /* bytes till reseed */
57} *rs;
58
59/* Maybe be preserved in fork children, if _rs_allocate() decides. */
60static struct _rsx {
61 chacha_ctx rs_chacha; /* chacha context for random keystream */
62 u_char rs_buf[RSBUFSZ]; /* keystream blocks */
63} *rsx;
64
65int getentropy(void* buffer, size_t buffer_size);
66
67static inline int _rs_allocate(struct _rs **, struct _rsx **);
68static inline void _rs_forkdetect(void);
69
70#include "arc4random.h"
71
72static inline void _rs_rekey(u_char *dat, size_t datlen);
73
74static inline void _rs_init(u_char *buf, size_t n)
75{
76 if (n < KEYSZ + IVSZ)
77 return;
78
79 if (rs == NULL) {
80 if (_rs_allocate(&rs, &rsx) == -1)
81 abort();
82 }
83
84 chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8, 0);
85 chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ);
86}
87
88static void _rs_stir(void)
89{
90 u_char rnd[KEYSZ + IVSZ];
91
92 if (getentropy(rnd, sizeof rnd) == -1)
93 _getentropy_fail();
94
95 if (!rs)
96 _rs_init(rnd, sizeof(rnd));
97 else
98 _rs_rekey(rnd, sizeof(rnd));
99 explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */
100
101 /* invalidate rs_buf */
102 rs->rs_have = 0;
103 memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
104
105 rs->rs_count = 1600000;
106}
107
108static inline void _rs_stir_if_needed(size_t len)
109{
110 _rs_forkdetect();
111 if (!rs || rs->rs_count <= len)
112 _rs_stir();
113 if (rs->rs_count <= len)
114 rs->rs_count = 0;
115 else
116 rs->rs_count -= len;
117}
118
119static inline void _rs_rekey(u_char *dat, size_t datlen)
120{
121#ifndef KEYSTREAM_ONLY
122 memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
123#endif
124 /* fill rs_buf with the keystream */
125 chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf,
126 rsx->rs_buf, sizeof(rsx->rs_buf));
127 /* mix in optional user provided data */
128 if (dat) {
129 size_t i, m;
130
131 m = minimum(datlen, KEYSZ + IVSZ);
132 for (i = 0; i < m; i++)
133 rsx->rs_buf[i] ^= dat[i];
134 }
135 /* immediately reinit for backtracking resistance */
136 _rs_init(rsx->rs_buf, KEYSZ + IVSZ);
137 memset(rsx->rs_buf, 0, KEYSZ + IVSZ);
138 rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ;
139}
140
141static inline void _rs_random_buf(void *_buf, size_t n)
142{
143 u_char *buf = (u_char *)_buf;
144 u_char *keystream;
145 size_t m;
146
147 _rs_stir_if_needed(n);
148 while (n > 0) {
149 if (rs->rs_have > 0) {
150 m = minimum(n, rs->rs_have);
151 keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
152 - rs->rs_have;
153 memcpy(buf, keystream, m);
154 memset(keystream, 0, m);
155 buf += m;
156 n -= m;
157 rs->rs_have -= m;
158 }
159 if (rs->rs_have == 0)
160 _rs_rekey(NULL, 0);
161 }
162}
163
164static inline void _rs_random_u32(uint32_t *val)
165{
166 u_char *keystream;
167
168 _rs_stir_if_needed(sizeof(*val));
169 if (rs->rs_have < sizeof(*val))
170 _rs_rekey(NULL, 0);
171 keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have;
172 memcpy(val, keystream, sizeof(*val));
173 memset(keystream, 0, sizeof(*val));
174 rs->rs_have -= sizeof(*val);
175}
176
177uint32_t arc4random(void)
178{
179 uint32_t val;
180
181 _ARC4_LOCK();
182 _rs_random_u32(&val);
183 _ARC4_UNLOCK();
184 return val;
185}
186
187void arc4random_buf(void *buf, size_t n)
188{
189 _ARC4_LOCK();
190 _rs_random_buf(buf, n);
191 _ARC4_UNLOCK();
192}
193