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