xref: /freebsd/sys/dev/random/fenestrasX/fx_rng.h (revision 95ee2897)
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 #pragma once
28a3c41f8bSConrad Meyer 
29a3c41f8bSConrad Meyer #include <crypto/chacha20/chacha.h>
30a3c41f8bSConrad Meyer #include <dev/random/fenestrasX/fx_priv.h>
31a3c41f8bSConrad Meyer 
32a3c41f8bSConrad Meyer #define	FX_CHACHA20_KEYSIZE	32
33a3c41f8bSConrad Meyer 
34a3c41f8bSConrad Meyer struct fxrng_basic_rng {
35a3c41f8bSConrad Meyer 	struct mtx		rng_lk;
36a3c41f8bSConrad Meyer #define	FXRNG_RNG_LOCK(rng)	mtx_lock(&(rng)->rng_lk)
37a3c41f8bSConrad Meyer #define	FXRNG_RNG_UNLOCK(rng)	mtx_unlock(&(rng)->rng_lk)
38a3c41f8bSConrad Meyer #define	FXRNG_RNG_ASSERT(rng)	mtx_assert(&(rng)->rng_lk, MA_OWNED)
39a3c41f8bSConrad Meyer #define	FXRNG_RNG_ASSERT_NOT(rng)	mtx_assert(&(rng)->rng_lk, MA_NOTOWNED)
40a3c41f8bSConrad Meyer 	/* CTR-mode cipher state, including 128-bit embedded counter. */
41a3c41f8bSConrad Meyer 	struct chacha_ctx	rng_prf;
42a3c41f8bSConrad Meyer };
43a3c41f8bSConrad Meyer 
44a3c41f8bSConrad Meyer /*
45a3c41f8bSConrad Meyer  * Initialize a basic rng instance (half of a buffered FX BRNG).
46a3c41f8bSConrad Meyer  * Memory should be zero initialized before this routine.
47a3c41f8bSConrad Meyer  */
48a3c41f8bSConrad Meyer static inline void
fxrng_rng_init(struct fxrng_basic_rng * rng,bool is_root_rng)49a3c41f8bSConrad Meyer fxrng_rng_init(struct fxrng_basic_rng *rng, bool is_root_rng)
50a3c41f8bSConrad Meyer {
51a3c41f8bSConrad Meyer 	if (is_root_rng)
52a3c41f8bSConrad Meyer 		mtx_init(&rng->rng_lk, "fx root brng lk", NULL, MTX_DEF);
53a3c41f8bSConrad Meyer 	else
54a3c41f8bSConrad Meyer 		mtx_init(&rng->rng_lk, "fx pcpu brng lk", NULL, MTX_DEF);
55a3c41f8bSConrad Meyer }
56a3c41f8bSConrad Meyer 
57a3c41f8bSConrad Meyer static inline void
fxrng_rng_setkey(struct fxrng_basic_rng * rng,const void * newkey,size_t len)58a3c41f8bSConrad Meyer fxrng_rng_setkey(struct fxrng_basic_rng *rng, const void *newkey, size_t len)
59a3c41f8bSConrad Meyer {
60a3c41f8bSConrad Meyer 	ASSERT_DEBUG(len == FX_CHACHA20_KEYSIZE, "chacha20 uses 256 bit keys");
61a3c41f8bSConrad Meyer 	chacha_keysetup(&rng->rng_prf, newkey, len * 8);
62a3c41f8bSConrad Meyer }
63a3c41f8bSConrad Meyer 
64a3c41f8bSConrad Meyer void fxrng_rng_genrandom_internal(struct fxrng_basic_rng *, void *, size_t,
65a3c41f8bSConrad Meyer     bool return_unlocked);
66a3c41f8bSConrad Meyer 
67a3c41f8bSConrad Meyer void fxrng_rng_reseed(struct fxrng_basic_rng *, bool seeded, const void *,
68a3c41f8bSConrad Meyer     size_t);
69a3c41f8bSConrad Meyer void fxrng_rng_src_reseed(struct fxrng_basic_rng *,
70a3c41f8bSConrad Meyer     const struct harvest_event *);
71