xref: /freebsd/sys/dev/random/fenestrasX/fx_rng.c (revision fdafd315)
1a3c41f8bSConrad Meyer /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3a3c41f8bSConrad Meyer  *
4a3c41f8bSConrad Meyer  * Copyright (c) 2019 Conrad Meyer <cem@FreeBSD.org>
5a3c41f8bSConrad Meyer  *
6a3c41f8bSConrad Meyer  * Redistribution and use in source and binary forms, with or without
7a3c41f8bSConrad Meyer  * modification, are permitted provided that the following conditions
8a3c41f8bSConrad Meyer  * are met:
9a3c41f8bSConrad Meyer  * 1. Redistributions of source code must retain the above copyright
10a3c41f8bSConrad Meyer  *    notice, this list of conditions and the following disclaimer.
11a3c41f8bSConrad Meyer  * 2. Redistributions in binary form must reproduce the above copyright
12a3c41f8bSConrad Meyer  *    notice, this list of conditions and the following disclaimer in the
13a3c41f8bSConrad Meyer  *    documentation and/or other materials provided with the distribution.
14a3c41f8bSConrad Meyer  *
15a3c41f8bSConrad Meyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16a3c41f8bSConrad Meyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17a3c41f8bSConrad Meyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18a3c41f8bSConrad Meyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19a3c41f8bSConrad Meyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20a3c41f8bSConrad Meyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21a3c41f8bSConrad Meyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22a3c41f8bSConrad Meyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23a3c41f8bSConrad Meyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24a3c41f8bSConrad Meyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25a3c41f8bSConrad Meyer  * SUCH DAMAGE.
26a3c41f8bSConrad Meyer  */
27a3c41f8bSConrad Meyer 
28a3c41f8bSConrad Meyer #include <sys/param.h>
29a3c41f8bSConrad Meyer #include <sys/fail.h>
30a3c41f8bSConrad Meyer #include <sys/limits.h>
31a3c41f8bSConrad Meyer #include <sys/lock.h>
32a3c41f8bSConrad Meyer #include <sys/kernel.h>
33a3c41f8bSConrad Meyer #include <sys/malloc.h>
34a3c41f8bSConrad Meyer #include <sys/mutex.h>
35a3c41f8bSConrad Meyer #include <sys/random.h>
36a3c41f8bSConrad Meyer #include <sys/sdt.h>
37a3c41f8bSConrad Meyer #include <sys/sysctl.h>
38a3c41f8bSConrad Meyer #include <sys/systm.h>
39a3c41f8bSConrad Meyer 
40a3c41f8bSConrad Meyer #include <machine/cpu.h>
41a3c41f8bSConrad Meyer #include <machine/stdarg.h>
42a3c41f8bSConrad Meyer 
43a3c41f8bSConrad Meyer #define CHACHA_EMBED
44a3c41f8bSConrad Meyer #define KEYSTREAM_ONLY
45a3c41f8bSConrad Meyer #define CHACHA_NONCE0_CTR128
46a3c41f8bSConrad Meyer #include <crypto/chacha20/chacha.h>
47a3c41f8bSConrad Meyer #include <crypto/rijndael/rijndael-api-fst.h>
48a3c41f8bSConrad Meyer #include <crypto/sha2/sha256.h>
49a3c41f8bSConrad Meyer 
50a3c41f8bSConrad Meyer #include <dev/random/hash.h>
51a3c41f8bSConrad Meyer #include <dev/random/randomdev.h>
52a3c41f8bSConrad Meyer #include <dev/random/random_harvestq.h>
53a3c41f8bSConrad Meyer #include <dev/random/uint128.h>
54a3c41f8bSConrad Meyer 
55a3c41f8bSConrad Meyer #include <dev/random/fenestrasX/fx_hash.h>
56a3c41f8bSConrad Meyer #include <dev/random/fenestrasX/fx_priv.h>
57a3c41f8bSConrad Meyer #include <dev/random/fenestrasX/fx_rng.h>
58a3c41f8bSConrad Meyer 
59a3c41f8bSConrad Meyer _Static_assert(FX_CHACHA20_KEYSIZE == RANDOM_KEYSIZE, "");
60a3c41f8bSConrad Meyer 
61a3c41f8bSConrad Meyer #include <crypto/chacha20/chacha.c>
62a3c41f8bSConrad Meyer 
63a3c41f8bSConrad Meyer static void
fxrng_rng_keystream_internal(struct chacha_ctx * prf,void * buf,size_t nbytes)64a3c41f8bSConrad Meyer fxrng_rng_keystream_internal(struct chacha_ctx *prf, void *buf, size_t nbytes)
65a3c41f8bSConrad Meyer {
66a3c41f8bSConrad Meyer 	size_t chunklen;
67a3c41f8bSConrad Meyer 
68a3c41f8bSConrad Meyer 	while (nbytes > 0) {
69a3c41f8bSConrad Meyer 		chunklen = MIN(nbytes,
70a3c41f8bSConrad Meyer 		    rounddown((size_t)UINT32_MAX, CHACHA_BLOCKLEN));
71a3c41f8bSConrad Meyer 
72a3c41f8bSConrad Meyer 		chacha_encrypt_bytes(prf, NULL, buf, chunklen);
73a3c41f8bSConrad Meyer 		buf = (uint8_t *)buf + chunklen;
74a3c41f8bSConrad Meyer 		nbytes -= chunklen;
75a3c41f8bSConrad Meyer 	}
76a3c41f8bSConrad Meyer }
77a3c41f8bSConrad Meyer 
78a3c41f8bSConrad Meyer /*
79a3c41f8bSConrad Meyer  * This subroutine pulls the counter out of Chacha, which for whatever reason
80a3c41f8bSConrad Meyer  * always encodes and decodes counters in a little endian format, and adds
81a3c41f8bSConrad Meyer  * 'addend' to it, saving the result in Chacha.
82a3c41f8bSConrad Meyer  */
83a3c41f8bSConrad Meyer static void
fxrng_chacha_nonce_add64(struct chacha_ctx * ctx,uint64_t addend)84a3c41f8bSConrad Meyer fxrng_chacha_nonce_add64(struct chacha_ctx *ctx, uint64_t addend)
85a3c41f8bSConrad Meyer {
86a3c41f8bSConrad Meyer 	uint128_t ctr;	/* Native-endian. */
87a3c41f8bSConrad Meyer #if BYTE_ORDER == BIG_ENDIAN
88a3c41f8bSConrad Meyer 	uint128_t lectr;
89a3c41f8bSConrad Meyer 
90a3c41f8bSConrad Meyer 	chacha_ctrsave(ctx, (void *)&lectr);
91a3c41f8bSConrad Meyer 	ctr = le128dec(&lectr);
92a3c41f8bSConrad Meyer #else
93a3c41f8bSConrad Meyer 	chacha_ctrsave(ctx, (void *)&ctr);
94a3c41f8bSConrad Meyer #endif
95a3c41f8bSConrad Meyer 
96a3c41f8bSConrad Meyer 	uint128_add64(&ctr, addend);
97a3c41f8bSConrad Meyer 
98a3c41f8bSConrad Meyer 	/* chacha_ivsetup() does not modify the key, and we rely on that. */
99a3c41f8bSConrad Meyer #if BYTE_ORDER == BIG_ENDIAN
100a3c41f8bSConrad Meyer 	le128enc(&lectr, ctr);
101a3c41f8bSConrad Meyer 	chacha_ivsetup(ctx, NULL, (const void *)&lectr);
102a3c41f8bSConrad Meyer 	explicit_bzero(&lectr, sizeof(lectr));
103a3c41f8bSConrad Meyer #else
104a3c41f8bSConrad Meyer 	chacha_ivsetup(ctx, NULL, (const void *)&ctr);
105a3c41f8bSConrad Meyer #endif
106a3c41f8bSConrad Meyer 	explicit_bzero(&ctr, sizeof(ctr));
107a3c41f8bSConrad Meyer }
108a3c41f8bSConrad Meyer 
109a3c41f8bSConrad Meyer /*
110a3c41f8bSConrad Meyer  * Generate from the unbuffered source PRNG.
111a3c41f8bSConrad Meyer  *
112a3c41f8bSConrad Meyer  * Handles fast key erasure (rekeys the PRF with a generated key under lock).
113a3c41f8bSConrad Meyer  *
114a3c41f8bSConrad Meyer  * RNG lock is required on entry.  If return_unlocked is true, RNG lock will
115a3c41f8bSConrad Meyer  * be dropped on return.
116a3c41f8bSConrad Meyer  */
117a3c41f8bSConrad Meyer void
fxrng_rng_genrandom_internal(struct fxrng_basic_rng * rng,void * buf,size_t nbytes,bool return_unlocked)118a3c41f8bSConrad Meyer fxrng_rng_genrandom_internal(struct fxrng_basic_rng *rng, void *buf,
119a3c41f8bSConrad Meyer     size_t nbytes, bool return_unlocked)
120a3c41f8bSConrad Meyer {
121a3c41f8bSConrad Meyer 	struct chacha_ctx ctx_copy, *p_ctx;
122a3c41f8bSConrad Meyer 	uint8_t newkey[FX_CHACHA20_KEYSIZE];
123a3c41f8bSConrad Meyer 	size_t blockcount;
124a3c41f8bSConrad Meyer 
125a3c41f8bSConrad Meyer 	FXRNG_RNG_ASSERT(rng);
126a3c41f8bSConrad Meyer 
127a3c41f8bSConrad Meyer 	/* Save off the initial output of the generator for rekeying. */
128a3c41f8bSConrad Meyer 	fxrng_rng_keystream_internal(&rng->rng_prf, newkey, sizeof(newkey));
129a3c41f8bSConrad Meyer 
130a3c41f8bSConrad Meyer 	if (return_unlocked) {
131a3c41f8bSConrad Meyer 		memcpy(&ctx_copy, &rng->rng_prf, sizeof(ctx_copy));
132a3c41f8bSConrad Meyer 		p_ctx = &ctx_copy;
133a3c41f8bSConrad Meyer 
134a3c41f8bSConrad Meyer 		/*
135a3c41f8bSConrad Meyer 		 * Forward the Chacha counter state over the blocks we promise
136a3c41f8bSConrad Meyer 		 * to generate for the caller without the lock.
137a3c41f8bSConrad Meyer 		 */
138a3c41f8bSConrad Meyer 		blockcount = howmany(nbytes, CHACHA_BLOCKLEN);
139a3c41f8bSConrad Meyer 		fxrng_chacha_nonce_add64(&rng->rng_prf, blockcount);
140a3c41f8bSConrad Meyer 
141a3c41f8bSConrad Meyer 		/* Re-key before dropping the lock. */
142a3c41f8bSConrad Meyer 		chacha_keysetup(&rng->rng_prf, newkey, sizeof(newkey) * 8);
143a3c41f8bSConrad Meyer 		explicit_bzero(newkey, sizeof(newkey));
144a3c41f8bSConrad Meyer 
145a3c41f8bSConrad Meyer 		FXRNG_RNG_UNLOCK(rng);
146a3c41f8bSConrad Meyer 	} else {
147a3c41f8bSConrad Meyer 		p_ctx = &rng->rng_prf;
148a3c41f8bSConrad Meyer 	}
149a3c41f8bSConrad Meyer 
150a3c41f8bSConrad Meyer 	fxrng_rng_keystream_internal(p_ctx, buf, nbytes);
151a3c41f8bSConrad Meyer 
152a3c41f8bSConrad Meyer 	if (return_unlocked) {
153a3c41f8bSConrad Meyer 		explicit_bzero(&ctx_copy, sizeof(ctx_copy));
154a3c41f8bSConrad Meyer 		FXRNG_RNG_ASSERT_NOT(rng);
155a3c41f8bSConrad Meyer 	} else {
156a3c41f8bSConrad Meyer 		/* Re-key before exit. */
157a3c41f8bSConrad Meyer 		chacha_keysetup(&rng->rng_prf, newkey, sizeof(newkey) * 8);
158a3c41f8bSConrad Meyer 		explicit_bzero(newkey, sizeof(newkey));
159a3c41f8bSConrad Meyer 		FXRNG_RNG_ASSERT(rng);
160a3c41f8bSConrad Meyer 	}
161a3c41f8bSConrad Meyer }
162a3c41f8bSConrad Meyer 
163a3c41f8bSConrad Meyer /*
164a3c41f8bSConrad Meyer  * Helper to reseed the root RNG, incorporating the existing RNG state.
165a3c41f8bSConrad Meyer  *
166a3c41f8bSConrad Meyer  * The root RNG is locked on entry and locked on return.
167a3c41f8bSConrad Meyer  */
168a3c41f8bSConrad Meyer static void
fxrng_rng_reseed_internal(struct fxrng_basic_rng * rng,bool seeded,const void * src,size_t sz,...)169a3c41f8bSConrad Meyer fxrng_rng_reseed_internal(struct fxrng_basic_rng *rng, bool seeded,
170a3c41f8bSConrad Meyer     const void *src, size_t sz, ...)
171a3c41f8bSConrad Meyer {
172a3c41f8bSConrad Meyer 	union {
173a3c41f8bSConrad Meyer 		uint8_t root_state[FX_CHACHA20_KEYSIZE];
174a3c41f8bSConrad Meyer 		uint8_t hash_out[FXRNG_HASH_SZ];
175a3c41f8bSConrad Meyer 	} u;
176a3c41f8bSConrad Meyer 	struct fxrng_hash mix;
177a3c41f8bSConrad Meyer 	va_list ap;
178a3c41f8bSConrad Meyer 
179a3c41f8bSConrad Meyer 	_Static_assert(FX_CHACHA20_KEYSIZE <= FXRNG_HASH_SZ, "");
180a3c41f8bSConrad Meyer 
181a3c41f8bSConrad Meyer 	FXRNG_RNG_ASSERT(rng);
182a3c41f8bSConrad Meyer 
183a3c41f8bSConrad Meyer 	fxrng_hash_init(&mix);
184a3c41f8bSConrad Meyer 	if (seeded) {
185a3c41f8bSConrad Meyer 		fxrng_rng_keystream_internal(&rng->rng_prf, u.root_state,
186a3c41f8bSConrad Meyer 		    sizeof(u.root_state));
187a3c41f8bSConrad Meyer 		fxrng_hash_update(&mix, u.root_state, sizeof(u.root_state));
188a3c41f8bSConrad Meyer 	}
189a3c41f8bSConrad Meyer 	fxrng_hash_update(&mix, src, sz);
190a3c41f8bSConrad Meyer 
191a3c41f8bSConrad Meyer 	va_start(ap, sz);
192a3c41f8bSConrad Meyer 	while (true) {
193a3c41f8bSConrad Meyer 		src = va_arg(ap, const void *);
194a3c41f8bSConrad Meyer 		if (src == NULL)
195a3c41f8bSConrad Meyer 			break;
196a3c41f8bSConrad Meyer 		sz = va_arg(ap, size_t);
197a3c41f8bSConrad Meyer 		fxrng_hash_update(&mix, src, sz);
198a3c41f8bSConrad Meyer 	}
199a3c41f8bSConrad Meyer 	va_end(ap);
200a3c41f8bSConrad Meyer 
201a3c41f8bSConrad Meyer 	fxrng_hash_finish(&mix, u.hash_out, sizeof(u.hash_out));
202a3c41f8bSConrad Meyer 
203a3c41f8bSConrad Meyer 	/*
204a3c41f8bSConrad Meyer 	 * Take the first keysize (32) bytes of our digest (64 bytes).  It is
205a3c41f8bSConrad Meyer 	 * also possible to just have Blake2 emit fewer bytes, but our wrapper
206a3c41f8bSConrad Meyer 	 * API doesn't provide that functionality and there isn't anything
207a3c41f8bSConrad Meyer 	 * obviously wrong with emitting more hash bytes.
208a3c41f8bSConrad Meyer 	 *
209a3c41f8bSConrad Meyer 	 * keysetup does not reset the embedded counter, and we rely on that
210a3c41f8bSConrad Meyer 	 * property.
211a3c41f8bSConrad Meyer 	 */
212a3c41f8bSConrad Meyer 	chacha_keysetup(&rng->rng_prf, u.hash_out, FX_CHACHA20_KEYSIZE * 8);
213a3c41f8bSConrad Meyer 
214a3c41f8bSConrad Meyer 	/* 'mix' zeroed by fxrng_hash_finish(). */
215a3c41f8bSConrad Meyer 	explicit_bzero(u.hash_out, sizeof(u.hash_out));
216a3c41f8bSConrad Meyer 
217a3c41f8bSConrad Meyer 	FXRNG_RNG_ASSERT(rng);
218a3c41f8bSConrad Meyer }
219a3c41f8bSConrad Meyer 
220a3c41f8bSConrad Meyer /*
221a3c41f8bSConrad Meyer  * Directly reseed the root RNG from a first-time entropy source,
222a3c41f8bSConrad Meyer  * incorporating the existing RNG state, called by fxrng_brng_src_reseed.
223a3c41f8bSConrad Meyer  *
224a3c41f8bSConrad Meyer  * The root RNG is locked on entry and locked on return.
225a3c41f8bSConrad Meyer  */
226a3c41f8bSConrad Meyer void
fxrng_rng_src_reseed(struct fxrng_basic_rng * rng,const struct harvest_event * event)227a3c41f8bSConrad Meyer fxrng_rng_src_reseed(struct fxrng_basic_rng *rng,
228a3c41f8bSConrad Meyer     const struct harvest_event *event)
229a3c41f8bSConrad Meyer {
230a3c41f8bSConrad Meyer 	fxrng_rng_reseed_internal(rng, true, &event->he_somecounter,
231a3c41f8bSConrad Meyer 	    sizeof(event->he_somecounter), (const void *)event->he_entropy,
232a3c41f8bSConrad Meyer 	    (size_t)event->he_size, NULL);
233a3c41f8bSConrad Meyer }
234a3c41f8bSConrad Meyer 
235a3c41f8bSConrad Meyer /*
236a3c41f8bSConrad Meyer  * Reseed the root RNG from pooled entropy, incorporating the existing RNG
237a3c41f8bSConrad Meyer  * state, called by fxrng_brng_reseed.
238a3c41f8bSConrad Meyer  *
239a3c41f8bSConrad Meyer  * The root RNG is locked on entry and locked on return.
240a3c41f8bSConrad Meyer  */
241a3c41f8bSConrad Meyer void
fxrng_rng_reseed(struct fxrng_basic_rng * rng,bool seeded,const void * entr,size_t sz)242a3c41f8bSConrad Meyer fxrng_rng_reseed(struct fxrng_basic_rng *rng, bool seeded, const void *entr,
243a3c41f8bSConrad Meyer     size_t sz)
244a3c41f8bSConrad Meyer {
245a3c41f8bSConrad Meyer 	fxrng_rng_reseed_internal(rng, seeded, entr, sz, NULL);
246a3c41f8bSConrad Meyer }
247