xref: /freebsd/sys/libkern/arc4random.c (revision edf8578117e8844e02c0121147f45e4609b30680)
1 /*-
2  * Copyright (c) 2017 The FreeBSD Foundation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer
9  *    in this position and unchanged.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 #include <sys/cdefs.h>
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/libkern.h>
32 #include <sys/linker.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/msan.h>
36 #include <sys/mutex.h>
37 #include <sys/random.h>
38 #include <sys/smp.h>
39 #include <sys/time.h>
40 
41 #include <machine/cpu.h>
42 
43 #include <crypto/chacha20/chacha.h>
44 #include <crypto/sha2/sha256.h>
45 #include <dev/random/randomdev.h>
46 #ifdef RANDOM_FENESTRASX
47 #include <dev/random/fenestrasX/fx_pub.h>
48 #endif
49 
50 #define	CHACHA20_RESEED_BYTES	65536
51 #define	CHACHA20_RESEED_SECONDS	300
52 #define	CHACHA20_KEYBYTES	32
53 #define	CHACHA20_BUFFER_SIZE	64
54 
55 CTASSERT(CHACHA20_KEYBYTES*8 >= CHACHA_MINKEYLEN);
56 
57 #ifndef RANDOM_FENESTRASX
58 int arc4rand_iniseed_state = ARC4_ENTR_NONE;
59 #endif
60 
61 MALLOC_DEFINE(M_CHACHA20RANDOM, "chacha20random", "chacha20random structures");
62 
63 struct chacha20_s {
64 	struct mtx mtx;
65 	int numbytes;
66 	time_t t_reseed;
67 	uint8_t m_buffer[CHACHA20_BUFFER_SIZE];
68 	struct chacha_ctx ctx;
69 #ifdef RANDOM_FENESTRASX
70 	uint64_t seed_version;
71 #endif
72 } __aligned(CACHE_LINE_SIZE);
73 
74 static struct chacha20_s *chacha20inst = NULL;
75 
76 #define CHACHA20_FOREACH(_chacha20) \
77 	for (_chacha20 = &chacha20inst[0]; \
78 	     _chacha20 <= &chacha20inst[mp_maxid]; \
79 	     _chacha20++)
80 
81 /*
82  * Mix up the current context.
83  */
84 static void
85 chacha20_randomstir(struct chacha20_s *chacha20)
86 {
87 	struct timeval tv_now;
88 	uint8_t key[CHACHA20_KEYBYTES];
89 #ifdef RANDOM_FENESTRASX
90 	uint64_t seed_version;
91 
92 #else
93 	if (__predict_false(random_bypass_before_seeding && !is_random_seeded())) {
94 		SHA256_CTX ctx;
95 		uint64_t cc;
96 		uint32_t fver;
97 
98 		if (!arc4random_bypassed_before_seeding) {
99 			arc4random_bypassed_before_seeding = true;
100 			if (!random_bypass_disable_warnings)
101 				printf("arc4random: WARNING: initial seeding "
102 				    "bypassed the cryptographic random device "
103 				    "because it was not yet seeded and the "
104 				    "knob 'bypass_before_seeding' was "
105 				    "enabled.\n");
106 		}
107 
108 		/*
109 		 * "key" is intentionally left uninitialized here, so with KMSAN
110 		 * enabled the arc4random() return value may be marked
111 		 * uninitialized, leading to spurious reports.  Lie to KMSAN to
112 		 * avoid this situation.
113 		 */
114 		kmsan_mark(key, sizeof(key), KMSAN_STATE_INITED);
115 
116 		/* Last ditch effort to inject something in a bad condition. */
117 		cc = get_cyclecount();
118 		SHA256_Init(&ctx);
119 		SHA256_Update(&ctx, key, sizeof(key));
120 		SHA256_Update(&ctx, &cc, sizeof(cc));
121 		fver = __FreeBSD_version;
122 		SHA256_Update(&ctx, &fver, sizeof(fver));
123 		_Static_assert(sizeof(key) == SHA256_DIGEST_LENGTH,
124 		    "make sure 256 bits is still 256 bits");
125 		SHA256_Final(key, &ctx);
126 	} else {
127 #endif
128 #ifdef RANDOM_FENESTRASX
129 		read_random_key(key, CHACHA20_KEYBYTES, &seed_version);
130 #else
131 		/*
132 		* If the loader(8) did not have an entropy stash from the
133 		* previous shutdown to load, then we will block.  The answer is
134 		* to make sure there is an entropy stash at shutdown time.
135 		*
136 		* On the other hand, if the random_bypass_before_seeding knob
137 		* was set and we landed in this branch, we know this won't
138 		* block because we know the random device is seeded.
139 		*/
140 		read_random(key, CHACHA20_KEYBYTES);
141 	}
142 #endif
143 	getmicrouptime(&tv_now);
144 	mtx_lock(&chacha20->mtx);
145 	chacha_keysetup(&chacha20->ctx, key, CHACHA20_KEYBYTES*8);
146 	chacha_ivsetup(&chacha20->ctx, (u_char *)&tv_now.tv_sec, (u_char *)&tv_now.tv_usec);
147 	/* Reset for next reseed cycle. */
148 	chacha20->t_reseed = tv_now.tv_sec + CHACHA20_RESEED_SECONDS;
149 	chacha20->numbytes = 0;
150 #ifdef RANDOM_FENESTRASX
151 	chacha20->seed_version = seed_version;
152 #endif
153 	mtx_unlock(&chacha20->mtx);
154 }
155 
156 /*
157  * Initialize the contexts.
158  */
159 static void
160 chacha20_init(void)
161 {
162 	struct chacha20_s *chacha20;
163 
164 	chacha20inst = malloc((mp_maxid + 1) * sizeof(struct chacha20_s),
165 			M_CHACHA20RANDOM, M_NOWAIT | M_ZERO);
166 	KASSERT(chacha20inst != NULL, ("chacha20_init: memory allocation error"));
167 
168 	CHACHA20_FOREACH(chacha20) {
169 		mtx_init(&chacha20->mtx, "chacha20_mtx", NULL, MTX_DEF);
170 		chacha20->t_reseed = -1;
171 		chacha20->numbytes = 0;
172 		explicit_bzero(chacha20->m_buffer, CHACHA20_BUFFER_SIZE);
173 		explicit_bzero(&chacha20->ctx, sizeof(chacha20->ctx));
174 	}
175 }
176 SYSINIT(chacha20, SI_SUB_LOCK, SI_ORDER_ANY, chacha20_init, NULL);
177 
178 
179 static void
180 chacha20_uninit(void)
181 {
182 	struct chacha20_s *chacha20;
183 
184 	CHACHA20_FOREACH(chacha20)
185 		mtx_destroy(&chacha20->mtx);
186 	free(chacha20inst, M_CHACHA20RANDOM);
187 }
188 SYSUNINIT(chacha20, SI_SUB_LOCK, SI_ORDER_ANY, chacha20_uninit, NULL);
189 
190 
191 /*
192  * MPSAFE
193  */
194 void
195 arc4rand(void *ptr, u_int len, int reseed)
196 {
197 	struct chacha20_s *chacha20;
198 	struct timeval tv;
199 	u_int length;
200 	uint8_t *p;
201 
202 #ifdef RANDOM_FENESTRASX
203 	if (__predict_false(reseed))
204 #else
205 	if (__predict_false(reseed ||
206 	    (arc4rand_iniseed_state == ARC4_ENTR_HAVE &&
207 	    atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_HAVE, ARC4_ENTR_SEED))))
208 #endif
209 		CHACHA20_FOREACH(chacha20)
210 			chacha20_randomstir(chacha20);
211 
212 	getmicrouptime(&tv);
213 	chacha20 = &chacha20inst[curcpu];
214 	/* We may get unlucky and be migrated off this CPU, but that is expected to be infrequent */
215 	if ((chacha20->numbytes > CHACHA20_RESEED_BYTES) || (tv.tv_sec > chacha20->t_reseed))
216 		chacha20_randomstir(chacha20);
217 
218 	mtx_lock(&chacha20->mtx);
219 #ifdef RANDOM_FENESTRASX
220 	if (__predict_false(
221 	    atomic_load_acq_64(&fxrng_root_generation) != chacha20->seed_version
222 	    )) {
223 		mtx_unlock(&chacha20->mtx);
224 		chacha20_randomstir(chacha20);
225 		mtx_lock(&chacha20->mtx);
226 	}
227 #endif
228 
229 	p = ptr;
230 	while (len) {
231 		length = MIN(CHACHA20_BUFFER_SIZE, len);
232 		chacha_encrypt_bytes(&chacha20->ctx, chacha20->m_buffer, p, length);
233 		p += length;
234 		len -= length;
235 		chacha20->numbytes += length;
236 		if (chacha20->numbytes > CHACHA20_RESEED_BYTES) {
237 			mtx_unlock(&chacha20->mtx);
238 			chacha20_randomstir(chacha20);
239 			mtx_lock(&chacha20->mtx);
240 		}
241 	}
242 	mtx_unlock(&chacha20->mtx);
243 }
244 
245 uint32_t
246 arc4random(void)
247 {
248 	uint32_t ret;
249 
250 	arc4rand(&ret, sizeof(ret), 0);
251 	return ret;
252 }
253 
254 void
255 arc4random_buf(void *ptr, size_t len)
256 {
257 
258 	arc4rand(ptr, len, 0);
259 }
260