blob: 8238da7485e1c1b48a87518802b9e2aa99588b44 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* $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 <stdio.h>
30#include <stdint.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34#include <sys/types.h>
35#include <sys/time.h>
36
37//#define KEYSTREAM_ONLY
38#include "chacha_private.h"
39
40#define minimum(a, b) ((a) < (b) ? (a) : (b))
41#define explicit_bzero(p, s) memset(p, 0, s)
42
43#if defined(__GNUC__) || defined(_MSC_VER)
44#define inline __inline
45#else /* __GNUC__ || _MSC_VER */
46#define inline
47#endif /* !__GNUC__ && !_MSC_VER */
48
49#define KEYSZ 32
50#define IVSZ 8
51#define BLOCKSZ 64
52#define RSBUFSZ (16*BLOCKSZ)
53
54/* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */
55static struct _rs {
56 size_t rs_have; /* valid bytes at end of rs_buf */
57 size_t rs_count; /* bytes till reseed */
58} *rs;
59
60/* Maybe be preserved in fork children, if _rs_allocate() decides. */
61static struct _rsx {
62 chacha_ctx rs_chacha; /* chacha context for random keystream */
63 u_char rs_buf[RSBUFSZ]; /* keystream blocks */
64} *rsx;
65
66int getentropy(void* buffer, size_t buffer_size);
67
68static inline int _rs_allocate(struct _rs **, struct _rsx **);
69static inline void _rs_forkdetect(void);
70
71#include "arc4random.h"
72
73static inline void _rs_rekey(u_char *dat, size_t datlen);
74
75static inline void _rs_init(u_char *buf, size_t n)
76{
77 if (n < KEYSZ + IVSZ)
78 return;
79
80 if (rs == NULL) {
81 if (_rs_allocate(&rs, &rsx) == -1)
82 abort();
83 }
84
85 chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8, 0);
86 chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ);
87}
88
89static void _rs_stir(void)
90{
91 u_char rnd[KEYSZ + IVSZ];
92
93 if (getentropy(rnd, sizeof rnd) == -1)
94 _getentropy_fail();
95
96 if (!rs)
97 _rs_init(rnd, sizeof(rnd));
98 else
99 _rs_rekey(rnd, sizeof(rnd));
100 explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */
101
102 /* invalidate rs_buf */
103 rs->rs_have = 0;
104 memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
105
106 rs->rs_count = 1600000;
107}
108
109static inline void _rs_stir_if_needed(size_t len)
110{
111 _rs_forkdetect();
112 if (!rs || rs->rs_count <= len)
113 _rs_stir();
114 if (rs->rs_count <= len)
115 rs->rs_count = 0;
116 else
117 rs->rs_count -= len;
118}
119
120static inline void _rs_rekey(u_char *dat, size_t datlen)
121{
122#ifndef KEYSTREAM_ONLY
123 memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
124#endif
125 /* fill rs_buf with the keystream */
126 chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf,
127 rsx->rs_buf, sizeof(rsx->rs_buf));
128 /* mix in optional user provided data */
129 if (dat) {
130 size_t i, m;
131
132 m = minimum(datlen, KEYSZ + IVSZ);
133 for (i = 0; i < m; i++)
134 rsx->rs_buf[i] ^= dat[i];
135 }
136 /* immediately reinit for backtracking resistance */
137 _rs_init(rsx->rs_buf, KEYSZ + IVSZ);
138 memset(rsx->rs_buf, 0, KEYSZ + IVSZ);
139 rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ;
140}
141
142static inline void _rs_random_buf(void *_buf, size_t n)
143{
144 u_char *buf = (u_char *)_buf;
145 u_char *keystream;
146 size_t m;
147
148 _rs_stir_if_needed(n);
149 while (n > 0) {
150 if (rs->rs_have > 0) {
151 m = minimum(n, rs->rs_have);
152 keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
153 - rs->rs_have;
154 memcpy(buf, keystream, m);
155 memset(keystream, 0, m);
156 buf += m;
157 n -= m;
158 rs->rs_have -= m;
159 }
160 if (rs->rs_have == 0)
161 _rs_rekey(NULL, 0);
162 }
163}
164
165static inline void _rs_random_u32(uint32_t *val)
166{
167 u_char *keystream;
168
169 _rs_stir_if_needed(sizeof(*val));
170 if (rs->rs_have < sizeof(*val))
171 _rs_rekey(NULL, 0);
172 keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have;
173 memcpy(val, keystream, sizeof(*val));
174 memset(keystream, 0, sizeof(*val));
175 rs->rs_have -= sizeof(*val);
176}
177
178uint32_t arc4random(void)
179{
180 uint32_t val;
181
182 _ARC4_LOCK();
183 _rs_random_u32(&val);
184 _ARC4_UNLOCK();
185 return val;
186}
187
188void arc4random_buf(void *buf, size_t n)
189{
190 _ARC4_LOCK();
191 _rs_random_buf(buf, n);
192 _ARC4_UNLOCK();
193}
194