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