Merge branch 'vendor/OPENBSD_LIBM'
[dragonfly.git] / sys / sys / csprng.h
1 #ifndef _SYS_CSPRNG_H_
2 #define _SYS_CSPRNG_H_
3
4 #include <crypto/sha2/sha2.h>
5 #include <crypto/chacha/chacha.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
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         uint64_t        nonce;          /* Effectively high 64-bits of ctr */
26         uint64_t        ctr;
27
28         uint64_t        reseed_cnt;     /* Times we have reseeded */
29
30         chacha_ctx      cipher_ctx;     /* (Stream) cipher context */
31
32         /* Pools and the per-source round robin pool index */
33         struct csprng_pool pool[32];
34         uint8_t         src_pool_idx[256];
35
36         struct spinlock spin;
37         struct callout  reseed_callout;
38         uint32_t        failed_reseeds;
39         int             callout_based_reseed;
40         uint8_t         inject_counter[256];
41         long            nrandevents;
42         long            nrandseed;
43         struct timeval  last_reseed;
44         struct ibaa_state ibaa;
45         struct l15_state l15;
46 } __cachealign;
47
48 int csprng_init(struct csprng_state *state);
49 int csprng_get_random(struct csprng_state *state, uint8_t *out, int bytes,
50     int flags, int unlimited);
51 int csprng_add_entropy(struct csprng_state *state, int src_id,
52     const uint8_t *entropy, size_t bytes, int flags);
53
54 #endif