xref: /freebsd/sys/dev/random/fenestrasX/fx_brng.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1a3c41f8bSConrad Meyer /*-
2*4d846d26SWarner 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 <dev/random/fenestrasX/fx_rng.h>
30a3c41f8bSConrad Meyer 
31a3c41f8bSConrad Meyer #define	FXRNG_BUFRNG_SZ	128
32a3c41f8bSConrad Meyer 
33a3c41f8bSConrad Meyer /*
34a3c41f8bSConrad Meyer  * An object representing a buffered random number generator with forward
35a3c41f8bSConrad Meyer  * secrecy (aka "fast-key-erasure").
36a3c41f8bSConrad Meyer  *
37a3c41f8bSConrad Meyer  * There is a single static root instance of this object associated with the
38a3c41f8bSConrad Meyer  * entropy harvester, as well as additional instances per CPU, lazily allocated
39a3c41f8bSConrad Meyer  * in NUMA-local memory, seeded from output of the root generator.
40a3c41f8bSConrad Meyer  */
41a3c41f8bSConrad Meyer struct fxrng_buffered_rng {
42a3c41f8bSConrad Meyer 	struct fxrng_basic_rng	brng_rng;
43a3c41f8bSConrad Meyer #define	FXRNG_BRNG_LOCK(brng)	mtx_lock(&(brng)->brng_rng.rng_lk)
44a3c41f8bSConrad Meyer #define	FXRNG_BRNG_UNLOCK(brng)	mtx_unlock(&(brng)->brng_rng.rng_lk)
45a3c41f8bSConrad Meyer #define	FXRNG_BRNG_ASSERT(brng)	mtx_assert(&(brng)->brng_rng.rng_lk, MA_OWNED)
46a3c41f8bSConrad Meyer #define	FXRNG_BRNG_ASSERT_NOT(brng) \
47a3c41f8bSConrad Meyer 	mtx_assert(&(brng)->brng_rng.rng_lk, MA_NOTOWNED)
48a3c41f8bSConrad Meyer 
49a3c41f8bSConrad Meyer 	/* Entropy reseed generation ("seed version"). */
50a3c41f8bSConrad Meyer 	uint64_t	brng_generation;
51a3c41f8bSConrad Meyer 
52a3c41f8bSConrad Meyer 	/* Buffered output for quick access by small requests. */
53a3c41f8bSConrad Meyer 	uint8_t		brng_buffer[FXRNG_BUFRNG_SZ];
54a3c41f8bSConrad Meyer 	uint8_t		brng_avail_idx;
55a3c41f8bSConrad Meyer };
56a3c41f8bSConrad Meyer 
57a3c41f8bSConrad Meyer void fxrng_brng_init(struct fxrng_buffered_rng *);
58a3c41f8bSConrad Meyer void fxrng_brng_produce_seed_data_internal(struct fxrng_buffered_rng *, void *,
59a3c41f8bSConrad Meyer     size_t, uint64_t *seed_generation);
60a3c41f8bSConrad Meyer void fxrng_brng_read(struct fxrng_buffered_rng *, void *, size_t);
61a3c41f8bSConrad Meyer 
62a3c41f8bSConrad Meyer void fxrng_brng_reseed(const void *, size_t);
63a3c41f8bSConrad Meyer struct harvest_event;
64a3c41f8bSConrad Meyer void fxrng_brng_src_reseed(const struct harvest_event *);
65