xref: /freebsd/sys/dev/random/fenestrasX/fx_rng.h (revision 06c3fb27)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2019 Conrad Meyer <cem@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 #pragma once
28 
29 #include <crypto/chacha20/chacha.h>
30 #include <dev/random/fenestrasX/fx_priv.h>
31 
32 #define	FX_CHACHA20_KEYSIZE	32
33 
34 struct fxrng_basic_rng {
35 	struct mtx		rng_lk;
36 #define	FXRNG_RNG_LOCK(rng)	mtx_lock(&(rng)->rng_lk)
37 #define	FXRNG_RNG_UNLOCK(rng)	mtx_unlock(&(rng)->rng_lk)
38 #define	FXRNG_RNG_ASSERT(rng)	mtx_assert(&(rng)->rng_lk, MA_OWNED)
39 #define	FXRNG_RNG_ASSERT_NOT(rng)	mtx_assert(&(rng)->rng_lk, MA_NOTOWNED)
40 	/* CTR-mode cipher state, including 128-bit embedded counter. */
41 	struct chacha_ctx	rng_prf;
42 };
43 
44 /*
45  * Initialize a basic rng instance (half of a buffered FX BRNG).
46  * Memory should be zero initialized before this routine.
47  */
48 static inline void
49 fxrng_rng_init(struct fxrng_basic_rng *rng, bool is_root_rng)
50 {
51 	if (is_root_rng)
52 		mtx_init(&rng->rng_lk, "fx root brng lk", NULL, MTX_DEF);
53 	else
54 		mtx_init(&rng->rng_lk, "fx pcpu brng lk", NULL, MTX_DEF);
55 }
56 
57 static inline void
58 fxrng_rng_setkey(struct fxrng_basic_rng *rng, const void *newkey, size_t len)
59 {
60 	ASSERT_DEBUG(len == FX_CHACHA20_KEYSIZE, "chacha20 uses 256 bit keys");
61 	chacha_keysetup(&rng->rng_prf, newkey, len * 8);
62 }
63 
64 void fxrng_rng_genrandom_internal(struct fxrng_basic_rng *, void *, size_t,
65     bool return_unlocked);
66 
67 void fxrng_rng_reseed(struct fxrng_basic_rng *, bool seeded, const void *,
68     size_t);
69 void fxrng_rng_src_reseed(struct fxrng_basic_rng *,
70     const struct harvest_event *);
71