1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3*0957b409SSimon J. Gerraty *
4*0957b409SSimon J. Gerraty * Permission is hereby granted, free of charge, to any person obtaining
5*0957b409SSimon J. Gerraty * a copy of this software and associated documentation files (the
6*0957b409SSimon J. Gerraty * "Software"), to deal in the Software without restriction, including
7*0957b409SSimon J. Gerraty * without limitation the rights to use, copy, modify, merge, publish,
8*0957b409SSimon J. Gerraty * distribute, sublicense, and/or sell copies of the Software, and to
9*0957b409SSimon J. Gerraty * permit persons to whom the Software is furnished to do so, subject to
10*0957b409SSimon J. Gerraty * the following conditions:
11*0957b409SSimon J. Gerraty *
12*0957b409SSimon J. Gerraty * The above copyright notice and this permission notice shall be
13*0957b409SSimon J. Gerraty * included in all copies or substantial portions of the Software.
14*0957b409SSimon J. Gerraty *
15*0957b409SSimon J. Gerraty * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*0957b409SSimon J. Gerraty * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*0957b409SSimon J. Gerraty * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*0957b409SSimon J. Gerraty * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*0957b409SSimon J. Gerraty * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*0957b409SSimon J. Gerraty * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*0957b409SSimon J. Gerraty * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*0957b409SSimon J. Gerraty * SOFTWARE.
23*0957b409SSimon J. Gerraty */
24*0957b409SSimon J. Gerraty
25*0957b409SSimon J. Gerraty #ifndef BR_BEARSSL_RAND_H__
26*0957b409SSimon J. Gerraty #define BR_BEARSSL_RAND_H__
27*0957b409SSimon J. Gerraty
28*0957b409SSimon J. Gerraty #include <stddef.h>
29*0957b409SSimon J. Gerraty #include <stdint.h>
30*0957b409SSimon J. Gerraty
31*0957b409SSimon J. Gerraty #include "bearssl_block.h"
32*0957b409SSimon J. Gerraty #include "bearssl_hash.h"
33*0957b409SSimon J. Gerraty
34*0957b409SSimon J. Gerraty #ifdef __cplusplus
35*0957b409SSimon J. Gerraty extern "C" {
36*0957b409SSimon J. Gerraty #endif
37*0957b409SSimon J. Gerraty
38*0957b409SSimon J. Gerraty /** \file bearssl_rand.h
39*0957b409SSimon J. Gerraty *
40*0957b409SSimon J. Gerraty * # Pseudo-Random Generators
41*0957b409SSimon J. Gerraty *
42*0957b409SSimon J. Gerraty * A PRNG is a state-based engine that outputs pseudo-random bytes on
43*0957b409SSimon J. Gerraty * demand. It is initialized with an initial seed, and additional seed
44*0957b409SSimon J. Gerraty * bytes can be added afterwards. Bytes produced depend on the seeds and
45*0957b409SSimon J. Gerraty * also on the exact sequence of calls (including sizes requested for
46*0957b409SSimon J. Gerraty * each call).
47*0957b409SSimon J. Gerraty *
48*0957b409SSimon J. Gerraty *
49*0957b409SSimon J. Gerraty * ## Procedural and OOP API
50*0957b409SSimon J. Gerraty *
51*0957b409SSimon J. Gerraty * For the PRNG of name "`xxx`", two API are provided. The _procedural_
52*0957b409SSimon J. Gerraty * API defined a context structure `br_xxx_context` and three functions:
53*0957b409SSimon J. Gerraty *
54*0957b409SSimon J. Gerraty * - `br_xxx_init()`
55*0957b409SSimon J. Gerraty *
56*0957b409SSimon J. Gerraty * Initialise the context with an initial seed.
57*0957b409SSimon J. Gerraty *
58*0957b409SSimon J. Gerraty * - `br_xxx_generate()`
59*0957b409SSimon J. Gerraty *
60*0957b409SSimon J. Gerraty * Produce some pseudo-random bytes.
61*0957b409SSimon J. Gerraty *
62*0957b409SSimon J. Gerraty * - `br_xxx_update()`
63*0957b409SSimon J. Gerraty *
64*0957b409SSimon J. Gerraty * Inject some additional seed.
65*0957b409SSimon J. Gerraty *
66*0957b409SSimon J. Gerraty * The initialisation function sets the first context field (`vtable`)
67*0957b409SSimon J. Gerraty * to a pointer to the vtable that supports the OOP API. The OOP API
68*0957b409SSimon J. Gerraty * provides access to the same functions through function pointers,
69*0957b409SSimon J. Gerraty * named `init()`, `generate()` and `update()`.
70*0957b409SSimon J. Gerraty *
71*0957b409SSimon J. Gerraty * Note that the context initialisation method may accept additional
72*0957b409SSimon J. Gerraty * parameters, provided as a 'const void *' pointer at API level. These
73*0957b409SSimon J. Gerraty * additional parameters depend on the implemented PRNG.
74*0957b409SSimon J. Gerraty *
75*0957b409SSimon J. Gerraty *
76*0957b409SSimon J. Gerraty * ## HMAC_DRBG
77*0957b409SSimon J. Gerraty *
78*0957b409SSimon J. Gerraty * HMAC_DRBG is defined in [NIST SP 800-90A Revision
79*0957b409SSimon J. Gerraty * 1](http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-90Ar1.pdf).
80*0957b409SSimon J. Gerraty * It uses HMAC repeatedly, over some configurable underlying hash
81*0957b409SSimon J. Gerraty * function. In BearSSL, it is implemented under the "`hmac_drbg`" name.
82*0957b409SSimon J. Gerraty * The "extra parameters" pointer for context initialisation should be
83*0957b409SSimon J. Gerraty * set to a pointer to the vtable for the underlying hash function (e.g.
84*0957b409SSimon J. Gerraty * pointer to `br_sha256_vtable` to use HMAC_DRBG with SHA-256).
85*0957b409SSimon J. Gerraty *
86*0957b409SSimon J. Gerraty * According to the NIST standard, each request shall produce up to
87*0957b409SSimon J. Gerraty * 2<sup>19</sup> bits (i.e. 64 kB of data); moreover, the context shall
88*0957b409SSimon J. Gerraty * be reseeded at least once every 2<sup>48</sup> requests. This
89*0957b409SSimon J. Gerraty * implementation does not maintain the reseed counter (the threshold is
90*0957b409SSimon J. Gerraty * too high to be reached in practice) and does not object to producing
91*0957b409SSimon J. Gerraty * more than 64 kB in a single request; thus, the code cannot fail,
92*0957b409SSimon J. Gerraty * which corresponds to the fact that the API has no room for error
93*0957b409SSimon J. Gerraty * codes. However, this implies that requesting more than 64 kB in one
94*0957b409SSimon J. Gerraty * `generate()` request, or making more than 2<sup>48</sup> requests
95*0957b409SSimon J. Gerraty * without reseeding, is formally out of NIST specification. There is
96*0957b409SSimon J. Gerraty * no currently known security penalty for exceeding the NIST limits,
97*0957b409SSimon J. Gerraty * and, in any case, HMAC_DRBG usage in implementing SSL/TLS always
98*0957b409SSimon J. Gerraty * stays much below these thresholds.
99*0957b409SSimon J. Gerraty *
100*0957b409SSimon J. Gerraty *
101*0957b409SSimon J. Gerraty * ## AESCTR_DRBG
102*0957b409SSimon J. Gerraty *
103*0957b409SSimon J. Gerraty * AESCTR_DRBG is a custom PRNG based on AES-128 in CTR mode. This is
104*0957b409SSimon J. Gerraty * meant to be used only in situations where you are desperate for
105*0957b409SSimon J. Gerraty * speed, and have an hardware-optimized AES/CTR implementation. Whether
106*0957b409SSimon J. Gerraty * this will yield perceptible improvements depends on what you use the
107*0957b409SSimon J. Gerraty * pseudorandom bytes for, and how many you want; for instance, RSA key
108*0957b409SSimon J. Gerraty * pair generation uses a substantial amount of randomness, and using
109*0957b409SSimon J. Gerraty * AESCTR_DRBG instead of HMAC_DRBG yields a 15 to 20% increase in key
110*0957b409SSimon J. Gerraty * generation speed on a recent x86 CPU (Intel Core i7-6567U at 3.30 GHz).
111*0957b409SSimon J. Gerraty *
112*0957b409SSimon J. Gerraty * Internally, it uses CTR mode with successive counter values, starting
113*0957b409SSimon J. Gerraty * at zero (counter value expressed over 128 bits, big-endian convention).
114*0957b409SSimon J. Gerraty * The counter is not allowed to reach 32768; thus, every 32768*16 bytes
115*0957b409SSimon J. Gerraty * at most, the `update()` function is run (on an empty seed, if none is
116*0957b409SSimon J. Gerraty * provided). The `update()` function computes the new AES-128 key by
117*0957b409SSimon J. Gerraty * applying a custom hash function to the concatenation of a state-dependent
118*0957b409SSimon J. Gerraty * word (encryption of an all-one block with the current key) and the new
119*0957b409SSimon J. Gerraty * seed. The custom hash function uses Hirose's construction over AES-256;
120*0957b409SSimon J. Gerraty * see the comments in `aesctr_drbg.c` for details.
121*0957b409SSimon J. Gerraty *
122*0957b409SSimon J. Gerraty * This DRBG does not follow an existing standard, and thus should be
123*0957b409SSimon J. Gerraty * considered as inadequate for production use until it has been properly
124*0957b409SSimon J. Gerraty * analysed.
125*0957b409SSimon J. Gerraty */
126*0957b409SSimon J. Gerraty
127*0957b409SSimon J. Gerraty /**
128*0957b409SSimon J. Gerraty * \brief Class type for PRNG implementations.
129*0957b409SSimon J. Gerraty *
130*0957b409SSimon J. Gerraty * A `br_prng_class` instance references the methods implementing a PRNG.
131*0957b409SSimon J. Gerraty * Constant instances of this structure are defined for each implemented
132*0957b409SSimon J. Gerraty * PRNG. Such instances are also called "vtables".
133*0957b409SSimon J. Gerraty */
134*0957b409SSimon J. Gerraty typedef struct br_prng_class_ br_prng_class;
135*0957b409SSimon J. Gerraty struct br_prng_class_ {
136*0957b409SSimon J. Gerraty /**
137*0957b409SSimon J. Gerraty * \brief Size (in bytes) of the context structure appropriate for
138*0957b409SSimon J. Gerraty * running this PRNG.
139*0957b409SSimon J. Gerraty */
140*0957b409SSimon J. Gerraty size_t context_size;
141*0957b409SSimon J. Gerraty
142*0957b409SSimon J. Gerraty /**
143*0957b409SSimon J. Gerraty * \brief Initialisation method.
144*0957b409SSimon J. Gerraty *
145*0957b409SSimon J. Gerraty * The context to initialise is provided as a pointer to its
146*0957b409SSimon J. Gerraty * first field (the vtable pointer); this function sets that
147*0957b409SSimon J. Gerraty * first field to a pointer to the vtable.
148*0957b409SSimon J. Gerraty *
149*0957b409SSimon J. Gerraty * The extra parameters depend on the implementation; each
150*0957b409SSimon J. Gerraty * implementation defines what kind of extra parameters it
151*0957b409SSimon J. Gerraty * expects (if any).
152*0957b409SSimon J. Gerraty *
153*0957b409SSimon J. Gerraty * Requirements on the initial seed depend on the implemented
154*0957b409SSimon J. Gerraty * PRNG.
155*0957b409SSimon J. Gerraty *
156*0957b409SSimon J. Gerraty * \param ctx PRNG context to initialise.
157*0957b409SSimon J. Gerraty * \param params extra parameters for the PRNG.
158*0957b409SSimon J. Gerraty * \param seed initial seed.
159*0957b409SSimon J. Gerraty * \param seed_len initial seed length (in bytes).
160*0957b409SSimon J. Gerraty */
161*0957b409SSimon J. Gerraty void (*init)(const br_prng_class **ctx, const void *params,
162*0957b409SSimon J. Gerraty const void *seed, size_t seed_len);
163*0957b409SSimon J. Gerraty
164*0957b409SSimon J. Gerraty /**
165*0957b409SSimon J. Gerraty * \brief Random bytes generation.
166*0957b409SSimon J. Gerraty *
167*0957b409SSimon J. Gerraty * This method produces `len` pseudorandom bytes, in the `out`
168*0957b409SSimon J. Gerraty * buffer. The context is updated accordingly.
169*0957b409SSimon J. Gerraty *
170*0957b409SSimon J. Gerraty * \param ctx PRNG context.
171*0957b409SSimon J. Gerraty * \param out output buffer.
172*0957b409SSimon J. Gerraty * \param len number of pseudorandom bytes to produce.
173*0957b409SSimon J. Gerraty */
174*0957b409SSimon J. Gerraty void (*generate)(const br_prng_class **ctx, void *out, size_t len);
175*0957b409SSimon J. Gerraty
176*0957b409SSimon J. Gerraty /**
177*0957b409SSimon J. Gerraty * \brief Inject additional seed bytes.
178*0957b409SSimon J. Gerraty *
179*0957b409SSimon J. Gerraty * The provided seed bytes are added into the PRNG internal
180*0957b409SSimon J. Gerraty * entropy pool.
181*0957b409SSimon J. Gerraty *
182*0957b409SSimon J. Gerraty * \param ctx PRNG context.
183*0957b409SSimon J. Gerraty * \param seed additional seed.
184*0957b409SSimon J. Gerraty * \param seed_len additional seed length (in bytes).
185*0957b409SSimon J. Gerraty */
186*0957b409SSimon J. Gerraty void (*update)(const br_prng_class **ctx,
187*0957b409SSimon J. Gerraty const void *seed, size_t seed_len);
188*0957b409SSimon J. Gerraty };
189*0957b409SSimon J. Gerraty
190*0957b409SSimon J. Gerraty /**
191*0957b409SSimon J. Gerraty * \brief Context for HMAC_DRBG.
192*0957b409SSimon J. Gerraty *
193*0957b409SSimon J. Gerraty * The context contents are opaque, except the first field, which
194*0957b409SSimon J. Gerraty * supports OOP.
195*0957b409SSimon J. Gerraty */
196*0957b409SSimon J. Gerraty typedef struct {
197*0957b409SSimon J. Gerraty /**
198*0957b409SSimon J. Gerraty * \brief Pointer to the vtable.
199*0957b409SSimon J. Gerraty *
200*0957b409SSimon J. Gerraty * This field is set with the initialisation method/function.
201*0957b409SSimon J. Gerraty */
202*0957b409SSimon J. Gerraty const br_prng_class *vtable;
203*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE
204*0957b409SSimon J. Gerraty unsigned char K[64];
205*0957b409SSimon J. Gerraty unsigned char V[64];
206*0957b409SSimon J. Gerraty const br_hash_class *digest_class;
207*0957b409SSimon J. Gerraty #endif
208*0957b409SSimon J. Gerraty } br_hmac_drbg_context;
209*0957b409SSimon J. Gerraty
210*0957b409SSimon J. Gerraty /**
211*0957b409SSimon J. Gerraty * \brief Statically allocated, constant vtable for HMAC_DRBG.
212*0957b409SSimon J. Gerraty */
213*0957b409SSimon J. Gerraty extern const br_prng_class br_hmac_drbg_vtable;
214*0957b409SSimon J. Gerraty
215*0957b409SSimon J. Gerraty /**
216*0957b409SSimon J. Gerraty * \brief HMAC_DRBG initialisation.
217*0957b409SSimon J. Gerraty *
218*0957b409SSimon J. Gerraty * The context to initialise is provided as a pointer to its first field
219*0957b409SSimon J. Gerraty * (the vtable pointer); this function sets that first field to a
220*0957b409SSimon J. Gerraty * pointer to the vtable.
221*0957b409SSimon J. Gerraty *
222*0957b409SSimon J. Gerraty * The `seed` value is what is called, in NIST terminology, the
223*0957b409SSimon J. Gerraty * concatenation of the "seed", "nonce" and "personalization string", in
224*0957b409SSimon J. Gerraty * that order.
225*0957b409SSimon J. Gerraty *
226*0957b409SSimon J. Gerraty * The `digest_class` parameter defines the underlying hash function.
227*0957b409SSimon J. Gerraty * Formally, the NIST standard specifies that the hash function shall
228*0957b409SSimon J. Gerraty * be only SHA-1 or one of the SHA-2 functions. This implementation also
229*0957b409SSimon J. Gerraty * works with any other implemented hash function (such as MD5), but
230*0957b409SSimon J. Gerraty * this is non-standard and therefore not recommended.
231*0957b409SSimon J. Gerraty *
232*0957b409SSimon J. Gerraty * \param ctx HMAC_DRBG context to initialise.
233*0957b409SSimon J. Gerraty * \param digest_class vtable for the underlying hash function.
234*0957b409SSimon J. Gerraty * \param seed initial seed.
235*0957b409SSimon J. Gerraty * \param seed_len initial seed length (in bytes).
236*0957b409SSimon J. Gerraty */
237*0957b409SSimon J. Gerraty void br_hmac_drbg_init(br_hmac_drbg_context *ctx,
238*0957b409SSimon J. Gerraty const br_hash_class *digest_class, const void *seed, size_t seed_len);
239*0957b409SSimon J. Gerraty
240*0957b409SSimon J. Gerraty /**
241*0957b409SSimon J. Gerraty * \brief Random bytes generation with HMAC_DRBG.
242*0957b409SSimon J. Gerraty *
243*0957b409SSimon J. Gerraty * This method produces `len` pseudorandom bytes, in the `out`
244*0957b409SSimon J. Gerraty * buffer. The context is updated accordingly. Formally, requesting
245*0957b409SSimon J. Gerraty * more than 65536 bytes in one request falls out of specification
246*0957b409SSimon J. Gerraty * limits (but it won't fail).
247*0957b409SSimon J. Gerraty *
248*0957b409SSimon J. Gerraty * \param ctx HMAC_DRBG context.
249*0957b409SSimon J. Gerraty * \param out output buffer.
250*0957b409SSimon J. Gerraty * \param len number of pseudorandom bytes to produce.
251*0957b409SSimon J. Gerraty */
252*0957b409SSimon J. Gerraty void br_hmac_drbg_generate(br_hmac_drbg_context *ctx, void *out, size_t len);
253*0957b409SSimon J. Gerraty
254*0957b409SSimon J. Gerraty /**
255*0957b409SSimon J. Gerraty * \brief Inject additional seed bytes in HMAC_DRBG.
256*0957b409SSimon J. Gerraty *
257*0957b409SSimon J. Gerraty * The provided seed bytes are added into the HMAC_DRBG internal
258*0957b409SSimon J. Gerraty * entropy pool. The process does not _replace_ existing entropy,
259*0957b409SSimon J. Gerraty * thus pushing non-random bytes (i.e. bytes which are known to the
260*0957b409SSimon J. Gerraty * attackers) does not degrade the overall quality of generated bytes.
261*0957b409SSimon J. Gerraty *
262*0957b409SSimon J. Gerraty * \param ctx HMAC_DRBG context.
263*0957b409SSimon J. Gerraty * \param seed additional seed.
264*0957b409SSimon J. Gerraty * \param seed_len additional seed length (in bytes).
265*0957b409SSimon J. Gerraty */
266*0957b409SSimon J. Gerraty void br_hmac_drbg_update(br_hmac_drbg_context *ctx,
267*0957b409SSimon J. Gerraty const void *seed, size_t seed_len);
268*0957b409SSimon J. Gerraty
269*0957b409SSimon J. Gerraty /**
270*0957b409SSimon J. Gerraty * \brief Get the hash function implementation used by a given instance of
271*0957b409SSimon J. Gerraty * HMAC_DRBG.
272*0957b409SSimon J. Gerraty *
273*0957b409SSimon J. Gerraty * This calls MUST NOT be performed on a context which was not
274*0957b409SSimon J. Gerraty * previously initialised.
275*0957b409SSimon J. Gerraty *
276*0957b409SSimon J. Gerraty * \param ctx HMAC_DRBG context.
277*0957b409SSimon J. Gerraty * \return the hash function vtable.
278*0957b409SSimon J. Gerraty */
279*0957b409SSimon J. Gerraty static inline const br_hash_class *
br_hmac_drbg_get_hash(const br_hmac_drbg_context * ctx)280*0957b409SSimon J. Gerraty br_hmac_drbg_get_hash(const br_hmac_drbg_context *ctx)
281*0957b409SSimon J. Gerraty {
282*0957b409SSimon J. Gerraty return ctx->digest_class;
283*0957b409SSimon J. Gerraty }
284*0957b409SSimon J. Gerraty
285*0957b409SSimon J. Gerraty /**
286*0957b409SSimon J. Gerraty * \brief Type for a provider of entropy seeds.
287*0957b409SSimon J. Gerraty *
288*0957b409SSimon J. Gerraty * A "seeder" is a function that is able to obtain random values from
289*0957b409SSimon J. Gerraty * some source and inject them as entropy seed in a PRNG. A seeder
290*0957b409SSimon J. Gerraty * shall guarantee that the total entropy of the injected seed is large
291*0957b409SSimon J. Gerraty * enough to seed a PRNG for purposes of cryptographic key generation
292*0957b409SSimon J. Gerraty * (i.e. at least 128 bits).
293*0957b409SSimon J. Gerraty *
294*0957b409SSimon J. Gerraty * A seeder may report a failure to obtain adequate entropy. Seeders
295*0957b409SSimon J. Gerraty * shall endeavour to fix themselves transient errors by trying again;
296*0957b409SSimon J. Gerraty * thus, callers may consider reported errors as permanent.
297*0957b409SSimon J. Gerraty *
298*0957b409SSimon J. Gerraty * \param ctx PRNG context to seed.
299*0957b409SSimon J. Gerraty * \return 1 on success, 0 on error.
300*0957b409SSimon J. Gerraty */
301*0957b409SSimon J. Gerraty typedef int (*br_prng_seeder)(const br_prng_class **ctx);
302*0957b409SSimon J. Gerraty
303*0957b409SSimon J. Gerraty /**
304*0957b409SSimon J. Gerraty * \brief Get a seeder backed by the operating system or hardware.
305*0957b409SSimon J. Gerraty *
306*0957b409SSimon J. Gerraty * Get a seeder that feeds on RNG facilities provided by the current
307*0957b409SSimon J. Gerraty * operating system or hardware. If no such facility is known, then 0
308*0957b409SSimon J. Gerraty * is returned.
309*0957b409SSimon J. Gerraty *
310*0957b409SSimon J. Gerraty * If `name` is not `NULL`, then `*name` is set to a symbolic string
311*0957b409SSimon J. Gerraty * that identifies the seeder implementation. If no seeder is returned
312*0957b409SSimon J. Gerraty * and `name` is not `NULL`, then `*name` is set to a pointer to the
313*0957b409SSimon J. Gerraty * constant string `"none"`.
314*0957b409SSimon J. Gerraty *
315*0957b409SSimon J. Gerraty * \param name receiver for seeder name, or `NULL`.
316*0957b409SSimon J. Gerraty * \return the system seeder, if available, or 0.
317*0957b409SSimon J. Gerraty */
318*0957b409SSimon J. Gerraty br_prng_seeder br_prng_seeder_system(const char **name);
319*0957b409SSimon J. Gerraty
320*0957b409SSimon J. Gerraty /**
321*0957b409SSimon J. Gerraty * \brief Context for AESCTR_DRBG.
322*0957b409SSimon J. Gerraty *
323*0957b409SSimon J. Gerraty * The context contents are opaque, except the first field, which
324*0957b409SSimon J. Gerraty * supports OOP.
325*0957b409SSimon J. Gerraty */
326*0957b409SSimon J. Gerraty typedef struct {
327*0957b409SSimon J. Gerraty /**
328*0957b409SSimon J. Gerraty * \brief Pointer to the vtable.
329*0957b409SSimon J. Gerraty *
330*0957b409SSimon J. Gerraty * This field is set with the initialisation method/function.
331*0957b409SSimon J. Gerraty */
332*0957b409SSimon J. Gerraty const br_prng_class *vtable;
333*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE
334*0957b409SSimon J. Gerraty br_aes_gen_ctr_keys sk;
335*0957b409SSimon J. Gerraty uint32_t cc;
336*0957b409SSimon J. Gerraty #endif
337*0957b409SSimon J. Gerraty } br_aesctr_drbg_context;
338*0957b409SSimon J. Gerraty
339*0957b409SSimon J. Gerraty /**
340*0957b409SSimon J. Gerraty * \brief Statically allocated, constant vtable for AESCTR_DRBG.
341*0957b409SSimon J. Gerraty */
342*0957b409SSimon J. Gerraty extern const br_prng_class br_aesctr_drbg_vtable;
343*0957b409SSimon J. Gerraty
344*0957b409SSimon J. Gerraty /**
345*0957b409SSimon J. Gerraty * \brief AESCTR_DRBG initialisation.
346*0957b409SSimon J. Gerraty *
347*0957b409SSimon J. Gerraty * The context to initialise is provided as a pointer to its first field
348*0957b409SSimon J. Gerraty * (the vtable pointer); this function sets that first field to a
349*0957b409SSimon J. Gerraty * pointer to the vtable.
350*0957b409SSimon J. Gerraty *
351*0957b409SSimon J. Gerraty * The internal AES key is first set to the all-zero key; then, the
352*0957b409SSimon J. Gerraty * `br_aesctr_drbg_update()` function is called with the provided `seed`.
353*0957b409SSimon J. Gerraty * The call is performed even if the seed length (`seed_len`) is zero.
354*0957b409SSimon J. Gerraty *
355*0957b409SSimon J. Gerraty * The `aesctr` parameter defines the underlying AES/CTR implementation.
356*0957b409SSimon J. Gerraty *
357*0957b409SSimon J. Gerraty * \param ctx AESCTR_DRBG context to initialise.
358*0957b409SSimon J. Gerraty * \param aesctr vtable for the AES/CTR implementation.
359*0957b409SSimon J. Gerraty * \param seed initial seed (can be `NULL` if `seed_len` is zero).
360*0957b409SSimon J. Gerraty * \param seed_len initial seed length (in bytes).
361*0957b409SSimon J. Gerraty */
362*0957b409SSimon J. Gerraty void br_aesctr_drbg_init(br_aesctr_drbg_context *ctx,
363*0957b409SSimon J. Gerraty const br_block_ctr_class *aesctr, const void *seed, size_t seed_len);
364*0957b409SSimon J. Gerraty
365*0957b409SSimon J. Gerraty /**
366*0957b409SSimon J. Gerraty * \brief Random bytes generation with AESCTR_DRBG.
367*0957b409SSimon J. Gerraty *
368*0957b409SSimon J. Gerraty * This method produces `len` pseudorandom bytes, in the `out`
369*0957b409SSimon J. Gerraty * buffer. The context is updated accordingly.
370*0957b409SSimon J. Gerraty *
371*0957b409SSimon J. Gerraty * \param ctx AESCTR_DRBG context.
372*0957b409SSimon J. Gerraty * \param out output buffer.
373*0957b409SSimon J. Gerraty * \param len number of pseudorandom bytes to produce.
374*0957b409SSimon J. Gerraty */
375*0957b409SSimon J. Gerraty void br_aesctr_drbg_generate(br_aesctr_drbg_context *ctx,
376*0957b409SSimon J. Gerraty void *out, size_t len);
377*0957b409SSimon J. Gerraty
378*0957b409SSimon J. Gerraty /**
379*0957b409SSimon J. Gerraty * \brief Inject additional seed bytes in AESCTR_DRBG.
380*0957b409SSimon J. Gerraty *
381*0957b409SSimon J. Gerraty * The provided seed bytes are added into the AESCTR_DRBG internal
382*0957b409SSimon J. Gerraty * entropy pool. The process does not _replace_ existing entropy,
383*0957b409SSimon J. Gerraty * thus pushing non-random bytes (i.e. bytes which are known to the
384*0957b409SSimon J. Gerraty * attackers) does not degrade the overall quality of generated bytes.
385*0957b409SSimon J. Gerraty *
386*0957b409SSimon J. Gerraty * \param ctx AESCTR_DRBG context.
387*0957b409SSimon J. Gerraty * \param seed additional seed.
388*0957b409SSimon J. Gerraty * \param seed_len additional seed length (in bytes).
389*0957b409SSimon J. Gerraty */
390*0957b409SSimon J. Gerraty void br_aesctr_drbg_update(br_aesctr_drbg_context *ctx,
391*0957b409SSimon J. Gerraty const void *seed, size_t seed_len);
392*0957b409SSimon J. Gerraty
393*0957b409SSimon J. Gerraty #ifdef __cplusplus
394*0957b409SSimon J. Gerraty }
395*0957b409SSimon J. Gerraty #endif
396*0957b409SSimon J. Gerraty
397*0957b409SSimon J. Gerraty #endif
398