xref: /dragonfly/sys/sys/csprng.h (revision 7d84b73d)
1 #ifndef _SYS_CSPRNG_H_
2 #define _SYS_CSPRNG_H_
3 
4 #include <crypto/chacha20/_chacha.h>
5 #include <crypto/sha2/sha2.h>
6 
7 #include <sys/callout.h>
8 #include <sys/spinlock.h>
9 #include <sys/time.h>
10 #include <sys/ibaa.h>
11 
12 /* Flags for various calls */
13 #define CSPRNG_TRYLOCK		0x0001
14 #define CSPRNG_UNLIMITED	0x0002	/* unlimited (/dev/urandom) read */
15 
16 struct csprng_pool {
17 	uint64_t	bytes;
18 	SHA256_CTX	hash_ctx;
19 };
20 
21 CTASSERT(SHA256_DIGEST_LENGTH == 32);
22 
23 struct csprng_state {
24 	uint8_t		key[SHA256_DIGEST_LENGTH];
25 
26 	uint64_t	reseed_cnt;	/* Times we have reseeded */
27 
28 	struct chacha_ctx cipher_ctx;	/* (Stream) cipher context */
29 
30 	/* Pools and the per-source round robin pool index */
31 	struct csprng_pool pool[32];
32 	uint8_t		src_pool_idx[256];
33 
34 	struct spinlock	spin;
35 	struct callout	reseed_callout;
36 	uint32_t	failed_reseeds;
37 	int		callout_based_reseed;
38 	uint8_t		inject_counter[256];
39 	long		nrandevents;
40 	long		nrandseed;
41 	struct timeval  last_reseed;
42 	struct ibaa_state ibaa;
43 	struct l15_state l15;
44 } __cachealign;
45 
46 int csprng_init(struct csprng_state *state);
47 int csprng_get_random(struct csprng_state *state, uint8_t *out, int bytes,
48     int flags);
49 int csprng_add_entropy(struct csprng_state *state, int src_id,
50     const uint8_t *entropy, size_t bytes, int flags);
51 
52 #endif
53