1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 /* ---- PRNG Stuff ---- */
5 #ifdef LTC_YARROW
6 struct yarrow_prng {
7     int                   cipher, hash;
8     unsigned char         pool[MAXBLOCKSIZE];
9     symmetric_CTR         ctr;
10 };
11 #endif
12 
13 #ifdef LTC_RC4
14 struct rc4_prng {
15     rc4_state s;
16 };
17 #endif
18 
19 #ifdef LTC_CHACHA20_PRNG
20 struct chacha20_prng {
21     chacha_state s;        /* chacha state */
22     unsigned char ent[40]; /* entropy buffer */
23     unsigned long idx;     /* entropy counter */
24 };
25 #endif
26 
27 #ifdef LTC_FORTUNA
28 struct fortuna_prng {
29     hash_state pool[LTC_FORTUNA_POOLS];     /* the  pools */
30 
31     symmetric_key skey;
32 
33     unsigned char K[32],      /* the current key */
34                   IV[16];     /* IV for CTR mode */
35 
36     unsigned long pool_idx,   /* current pool we will add to */
37                   pool0_len;  /* length of 0'th pool */
38     ulong64       wd;
39     ulong64       reset_cnt;  /* number of times we have reseeded */
40 };
41 #endif
42 
43 #ifdef LTC_SOBER128
44 struct sober128_prng {
45     sober128_state s;      /* sober128 state */
46     unsigned char ent[40]; /* entropy buffer */
47     unsigned long idx;     /* entropy counter */
48 };
49 #endif
50 
51 typedef struct {
52    union {
53       char dummy[1];
54 #ifdef LTC_YARROW
55       struct yarrow_prng    yarrow;
56 #endif
57 #ifdef LTC_RC4
58       struct rc4_prng       rc4;
59 #endif
60 #ifdef LTC_CHACHA20_PRNG
61       struct chacha20_prng  chacha;
62 #endif
63 #ifdef LTC_FORTUNA
64       struct fortuna_prng   fortuna;
65 #endif
66 #ifdef LTC_SOBER128
67       struct sober128_prng  sober128;
68 #endif
69    } u;
70    short ready;            /* ready flag 0-1 */
71    LTC_MUTEX_TYPE(lock)    /* lock */
72 } prng_state;
73 
74 /** PRNG descriptor */
75 extern const struct ltc_prng_descriptor {
76     /** Name of the PRNG */
77     const char *name;
78     /** size in bytes of exported state */
79     int  export_size;
80     /** Start a PRNG state
81         @param prng   [out] The state to initialize
82         @return CRYPT_OK if successful
83     */
84     int (*start)(prng_state *prng);
85     /** Add entropy to the PRNG
86         @param in         The entropy
87         @param inlen      Length of the entropy (octets)\
88         @param prng       The PRNG state
89         @return CRYPT_OK if successful
90     */
91     int (*add_entropy)(const unsigned char *in, unsigned long inlen, prng_state *prng);
92     /** Ready a PRNG state to read from
93         @param prng       The PRNG state to ready
94         @return CRYPT_OK if successful
95     */
96     int (*ready)(prng_state *prng);
97     /** Read from the PRNG
98         @param out     [out] Where to store the data
99         @param outlen  Length of data desired (octets)
100         @param prng    The PRNG state to read from
101         @return Number of octets read
102     */
103     unsigned long (*read)(unsigned char *out, unsigned long outlen, prng_state *prng);
104     /** Terminate a PRNG state
105         @param prng   The PRNG state to terminate
106         @return CRYPT_OK if successful
107     */
108     int (*done)(prng_state *prng);
109     /** Export a PRNG state
110         @param out     [out] The destination for the state
111         @param outlen  [in/out] The max size and resulting size of the PRNG state
112         @param prng    The PRNG to export
113         @return CRYPT_OK if successful
114     */
115     int (*pexport)(unsigned char *out, unsigned long *outlen, prng_state *prng);
116     /** Import a PRNG state
117         @param in      The data to import
118         @param inlen   The length of the data to import (octets)
119         @param prng    The PRNG to initialize/import
120         @return CRYPT_OK if successful
121     */
122     int (*pimport)(const unsigned char *in, unsigned long inlen, prng_state *prng);
123     /** Self-test the PRNG
124         @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
125     */
126     int (*test)(void);
127 } *prng_descriptor[];
128 
129 #ifdef LTC_YARROW
130 int yarrow_start(prng_state *prng);
131 int yarrow_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
132 int yarrow_ready(prng_state *prng);
133 unsigned long yarrow_read(unsigned char *out, unsigned long outlen, prng_state *prng);
134 int yarrow_done(prng_state *prng);
135 int  yarrow_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
136 int  yarrow_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
137 int  yarrow_test(void);
138 extern const struct ltc_prng_descriptor yarrow_desc;
139 #endif
140 
141 #ifdef LTC_FORTUNA
142 int fortuna_start(prng_state *prng);
143 int fortuna_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
144 int fortuna_add_random_event(unsigned long source, unsigned long pool, const unsigned char *in, unsigned long inlen, prng_state *prng);
145 int fortuna_ready(prng_state *prng);
146 unsigned long fortuna_read(unsigned char *out, unsigned long outlen, prng_state *prng);
147 int fortuna_done(prng_state *prng);
148 int fortuna_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
149 int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
150 int fortuna_update_seed(const unsigned char *in, unsigned long inlen, prng_state *prng);
151 int fortuna_test(void);
152 extern const struct ltc_prng_descriptor fortuna_desc;
153 #endif
154 
155 #ifdef LTC_RC4
156 int rc4_start(prng_state *prng);
157 int rc4_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
158 int rc4_ready(prng_state *prng);
159 unsigned long rc4_read(unsigned char *out, unsigned long outlen, prng_state *prng);
160 int  rc4_done(prng_state *prng);
161 int  rc4_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
162 int  rc4_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
163 int  rc4_test(void);
164 extern const struct ltc_prng_descriptor rc4_desc;
165 #endif
166 
167 #ifdef LTC_CHACHA20_PRNG
168 int chacha20_prng_start(prng_state *prng);
169 int chacha20_prng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
170 int chacha20_prng_ready(prng_state *prng);
171 unsigned long chacha20_prng_read(unsigned char *out, unsigned long outlen, prng_state *prng);
172 int  chacha20_prng_done(prng_state *prng);
173 int  chacha20_prng_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
174 int  chacha20_prng_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
175 int  chacha20_prng_test(void);
176 extern const struct ltc_prng_descriptor chacha20_prng_desc;
177 #endif
178 
179 #ifdef LTC_SPRNG
180 int sprng_start(prng_state *prng);
181 int sprng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
182 int sprng_ready(prng_state *prng);
183 unsigned long sprng_read(unsigned char *out, unsigned long outlen, prng_state *prng);
184 int sprng_done(prng_state *prng);
185 int  sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
186 int  sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
187 int  sprng_test(void);
188 extern const struct ltc_prng_descriptor sprng_desc;
189 #endif
190 
191 #ifdef LTC_SOBER128
192 int sober128_start(prng_state *prng);
193 int sober128_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
194 int sober128_ready(prng_state *prng);
195 unsigned long sober128_read(unsigned char *out, unsigned long outlen, prng_state *prng);
196 int sober128_done(prng_state *prng);
197 int  sober128_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
198 int  sober128_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
199 int  sober128_test(void);
200 extern const struct ltc_prng_descriptor sober128_desc;
201 #endif
202 
203 int find_prng(const char *name);
204 int register_prng(const struct ltc_prng_descriptor *prng);
205 int unregister_prng(const struct ltc_prng_descriptor *prng);
206 int register_all_prngs(void);
207 int prng_is_valid(int idx);
208 LTC_MUTEX_PROTO(ltc_prng_mutex)
209 
210 /* Slow RNG you **might** be able to use to seed a PRNG with.  Be careful as this
211  * might not work on all platforms as planned
212  */
213 unsigned long rng_get_bytes(unsigned char *out,
214                             unsigned long outlen,
215                             void (*callback)(void));
216 
217 int rng_make_prng(int bits, int wprng, prng_state *prng, void (*callback)(void));
218 
219 #ifdef LTC_PRNG_ENABLE_LTC_RNG
220 extern unsigned long (*ltc_rng)(unsigned char *out, unsigned long outlen,
221       void (*callback)(void));
222 #endif
223 
224