Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/openssl/providers/implementations/rands/drbg_local.h
48383 views
1
/*
2
* Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3
*
4
* Licensed under the Apache License 2.0 (the "License"). You may not use
5
* this file except in compliance with the License. You can obtain a copy
6
* in the file LICENSE in the source distribution or at
7
* https://www.openssl.org/source/license.html
8
*/
9
10
#ifndef OSSL_CRYPTO_PROV_LOCAL_H
11
# define OSSL_CRYPTO_PROV_LOCAL_H
12
13
# include <openssl/evp.h>
14
# include <openssl/core_dispatch.h>
15
# include <openssl/core_names.h>
16
# include <openssl/params.h>
17
# include "internal/tsan_assist.h"
18
# include "internal/nelem.h"
19
# include "internal/numbers.h"
20
# include "prov/provider_ctx.h"
21
# include "prov/securitycheck.h"
22
23
/* How many times to read the TSC as a randomness source. */
24
# define TSC_READ_COUNT 4
25
26
/* Maximum reseed intervals */
27
# define MAX_RESEED_INTERVAL (1 << 24)
28
# define MAX_RESEED_TIME_INTERVAL (1 << 20) /* approx. 12 days */
29
30
/* Default reseed intervals */
31
# define RESEED_INTERVAL (1 << 8)
32
# define TIME_INTERVAL (60*60) /* 1 hour */
33
34
/*
35
* Maximum input size for the DRBG (entropy, nonce, personalization string)
36
*
37
* NIST SP800 90Ar1 allows a maximum of (1 << 35) bits i.e., (1 << 32) bytes.
38
*
39
* We lower it to 'only' INT32_MAX bytes, which is equivalent to 2 gigabytes.
40
*/
41
# define DRBG_MAX_LENGTH INT32_MAX
42
43
/* The default nonce */
44
/* ASCII: "OpenSSL NIST SP 800-90A DRBG", in hex for EBCDIC compatibility */
45
#define DRBG_DEFAULT_PERS_STRING "\x4f\x70\x65\x6e\x53\x53\x4c\x20\x4e\x49\x53\x54\x20\x53\x50\x20\x38\x30\x30\x2d\x39\x30\x41\x20\x44\x52\x42\x47"
46
47
typedef struct prov_drbg_st PROV_DRBG;
48
49
/* DRBG status values */
50
typedef enum drbg_status_e {
51
DRBG_UNINITIALISED,
52
DRBG_READY,
53
DRBG_ERROR
54
} DRBG_STATUS;
55
56
/*
57
* The state of all types of DRBGs.
58
*/
59
struct prov_drbg_st {
60
CRYPTO_RWLOCK *lock;
61
PROV_CTX *provctx;
62
63
/* Virtual functions are cached here */
64
int (*instantiate)(PROV_DRBG *drbg,
65
const unsigned char *entropy, size_t entropylen,
66
const unsigned char *nonce, size_t noncelen,
67
const unsigned char *pers, size_t perslen);
68
int (*uninstantiate)(PROV_DRBG *ctx);
69
int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len,
70
const unsigned char *adin, size_t adin_len);
71
int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen,
72
const unsigned char *adin, size_t adin_len);
73
74
/* Parent PROV_RAND and its dispatch table functions */
75
void *parent;
76
OSSL_FUNC_rand_enable_locking_fn *parent_enable_locking;
77
OSSL_FUNC_rand_lock_fn *parent_lock;
78
OSSL_FUNC_rand_unlock_fn *parent_unlock;
79
OSSL_FUNC_rand_get_ctx_params_fn *parent_get_ctx_params;
80
OSSL_FUNC_rand_nonce_fn *parent_nonce;
81
OSSL_FUNC_rand_get_seed_fn *parent_get_seed;
82
OSSL_FUNC_rand_clear_seed_fn *parent_clear_seed;
83
84
/*
85
* Stores the return value of openssl_get_fork_id() as of when we last
86
* reseeded. The DRBG reseeds automatically whenever drbg->fork_id !=
87
* openssl_get_fork_id(). Used to provide fork-safety and reseed this
88
* DRBG in the child process.
89
*/
90
int fork_id;
91
unsigned short flags; /* various external flags */
92
93
/*
94
* The following parameters are setup by the per-type "init" function.
95
*
96
* The supported types and their init functions are:
97
* (1) CTR_DRBG: drbg_ctr_init().
98
* (2) HMAC_DRBG: drbg_hmac_init().
99
* (3) HASH_DRBG: drbg_hash_init().
100
*
101
* The parameters are closely related to the ones described in
102
* section '10.2.1 CTR_DRBG' of [NIST SP 800-90Ar1], with one
103
* crucial difference: In the NIST standard, all counts are given
104
* in bits, whereas in OpenSSL entropy counts are given in bits
105
* and buffer lengths are given in bytes.
106
*
107
* Since this difference has lead to some confusion in the past,
108
* (see [GitHub Issue #2443], formerly [rt.openssl.org #4055])
109
* the 'len' suffix has been added to all buffer sizes for
110
* clarification.
111
*/
112
113
unsigned int strength;
114
size_t max_request;
115
size_t min_entropylen, max_entropylen;
116
size_t min_noncelen, max_noncelen;
117
size_t max_perslen, max_adinlen;
118
119
/*
120
* Counts the number of generate requests since the last reseed
121
* (Starts at 1). This value is the reseed_counter as defined in
122
* NIST SP 800-90Ar1
123
*/
124
unsigned int generate_counter;
125
/*
126
* Maximum number of generate requests until a reseed is required.
127
* This value is ignored if it is zero.
128
*/
129
unsigned int reseed_interval;
130
/* Stores the time when the last reseeding occurred */
131
time_t reseed_time;
132
/*
133
* Specifies the maximum time interval (in seconds) between reseeds.
134
* This value is ignored if it is zero.
135
*/
136
time_t reseed_time_interval;
137
/*
138
* Counts the number of reseeds since instantiation.
139
* This value is ignored if it is zero.
140
*
141
* This counter is used only for seed propagation from the <master> DRBG
142
* to its two children, the <public> and <private> DRBG. This feature is
143
* very special and its sole purpose is to ensure that any randomness which
144
* is added by PROV_add() or PROV_seed() will have an immediate effect on
145
* the output of PROV_bytes() resp. PROV_priv_bytes().
146
*/
147
TSAN_QUALIFIER unsigned int reseed_counter;
148
unsigned int reseed_next_counter;
149
unsigned int parent_reseed_counter;
150
151
size_t seedlen;
152
DRBG_STATUS state;
153
154
/* DRBG specific data */
155
void *data;
156
157
/* Entropy and nonce gathering callbacks */
158
void *callback_arg;
159
OSSL_INOUT_CALLBACK *get_entropy_fn;
160
OSSL_CALLBACK *cleanup_entropy_fn;
161
OSSL_INOUT_CALLBACK *get_nonce_fn;
162
OSSL_CALLBACK *cleanup_nonce_fn;
163
164
OSSL_FIPS_IND_DECLARE
165
};
166
167
PROV_DRBG *ossl_rand_drbg_new
168
(void *provctx, void *parent, const OSSL_DISPATCH *parent_dispatch,
169
int (*dnew)(PROV_DRBG *ctx),
170
void (*dfree)(void *vctx),
171
int (*instantiate)(PROV_DRBG *drbg,
172
const unsigned char *entropy, size_t entropylen,
173
const unsigned char *nonce, size_t noncelen,
174
const unsigned char *pers, size_t perslen),
175
int (*uninstantiate)(PROV_DRBG *ctx),
176
int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len,
177
const unsigned char *adin, size_t adin_len),
178
int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen,
179
const unsigned char *adin, size_t adin_len));
180
void ossl_rand_drbg_free(PROV_DRBG *drbg);
181
182
int ossl_prov_drbg_instantiate(PROV_DRBG *drbg, unsigned int strength,
183
int prediction_resistance,
184
const unsigned char *pers, size_t perslen);
185
186
int ossl_prov_drbg_uninstantiate(PROV_DRBG *drbg);
187
188
int ossl_prov_drbg_reseed(PROV_DRBG *drbg, int prediction_resistance,
189
const unsigned char *ent, size_t ent_len,
190
const unsigned char *adin, size_t adinlen);
191
192
int ossl_prov_drbg_generate(PROV_DRBG *drbg, unsigned char *out, size_t outlen,
193
unsigned int strength, int prediction_resistance,
194
const unsigned char *adin, size_t adinlen);
195
196
/* Seeding api */
197
OSSL_FUNC_rand_get_seed_fn ossl_drbg_get_seed;
198
OSSL_FUNC_rand_clear_seed_fn ossl_drbg_clear_seed;
199
200
/* Verify that an array of numeric values is all zero */
201
#define PROV_DRBG_VERIFY_ZEROIZATION(v) \
202
{ \
203
size_t i; \
204
\
205
for (i = 0; i < OSSL_NELEM(v); i++) \
206
if ((v)[i] != 0) \
207
goto err; \
208
}
209
210
/* locking api */
211
OSSL_FUNC_rand_enable_locking_fn ossl_drbg_enable_locking;
212
OSSL_FUNC_rand_lock_fn ossl_drbg_lock;
213
OSSL_FUNC_rand_unlock_fn ossl_drbg_unlock;
214
215
/* Common parameters for all of our DRBGs */
216
int ossl_drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[]);
217
int ossl_drbg_get_ctx_params_no_lock(PROV_DRBG *drbg, OSSL_PARAM params[],
218
int *complete);
219
int ossl_drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[]);
220
221
#define OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON \
222
OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, NULL), \
223
OSSL_PARAM_uint64(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, NULL)
224
225
#define OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON \
226
OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL), \
227
OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL), \
228
OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL), \
229
OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_ENTROPYLEN, NULL), \
230
OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ENTROPYLEN, NULL), \
231
OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_NONCELEN, NULL), \
232
OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_NONCELEN, NULL), \
233
OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_PERSLEN, NULL), \
234
OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ADINLEN, NULL), \
235
OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_COUNTER, NULL), \
236
OSSL_PARAM_time_t(OSSL_DRBG_PARAM_RESEED_TIME, NULL), \
237
OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, NULL), \
238
OSSL_PARAM_uint64(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, NULL)
239
240
/* Confirm digest is allowed to be used with a DRBG */
241
int ossl_drbg_verify_digest(PROV_DRBG *drbg, OSSL_LIB_CTX *libctx, const EVP_MD *md);
242
243
#endif
244
245