]>
Commit | Line | Data |
---|---|---|
714a1bb3 P |
1 | /* |
2 | * Copyright 1995-2020 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> | |
363b1e5d | 14 | # include <openssl/core_dispatch.h> |
714a1bb3 P |
15 | # include <openssl/core_names.h> |
16 | # include <openssl/params.h> | |
17 | # include "internal/tsan_assist.h" | |
f000e828 | 18 | # include "internal/nelem.h" |
714a1bb3 P |
19 | # include "internal/numbers.h" |
20 | ||
21 | /* How many times to read the TSC as a randomness source. */ | |
22 | # define TSC_READ_COUNT 4 | |
23 | ||
24 | /* Maximum reseed intervals */ | |
25 | # define MAX_RESEED_INTERVAL (1 << 24) | |
26 | # define MAX_RESEED_TIME_INTERVAL (1 << 20) /* approx. 12 days */ | |
27 | ||
28 | /* Default reseed intervals */ | |
f000e828 P |
29 | # define RESEED_INTERVAL (1 << 8) |
30 | # define TIME_INTERVAL (60*60) /* 1 hour */ | |
714a1bb3 P |
31 | |
32 | /* | |
33 | * The number of bytes that constitutes an atomic lump of entropy with respect | |
34 | * to the FIPS 140-2 section 4.9.2 Conditional Tests. The size is somewhat | |
35 | * arbitrary, the smaller the value, the less entropy is consumed on first | |
36 | * read but the higher the probability of the test failing by accident. | |
37 | * | |
38 | * The value is in bytes. | |
39 | */ | |
40 | #define CRNGT_BUFSIZ 16 | |
41 | ||
42 | /* | |
43 | * Maximum input size for the DRBG (entropy, nonce, personalization string) | |
44 | * | |
45 | * NIST SP800 90Ar1 allows a maximum of (1 << 35) bits i.e., (1 << 32) bytes. | |
46 | * | |
47 | * We lower it to 'only' INT32_MAX bytes, which is equivalent to 2 gigabytes. | |
48 | */ | |
49 | # define DRBG_MAX_LENGTH INT32_MAX | |
50 | ||
51 | /* The default nonce */ | |
52 | #ifdef CHARSET_EBCDIC | |
53 | # define DRBG_DEFAULT_PERS_STRING { 0x4f, 0x70, 0x65, 0x6e, 0x53, 0x53, \ | |
54 | 0x4c, 0x20, 0x4e, 0x49, 0x53, 0x54, 0x20, 0x53, 0x50, 0x20, 0x38, 0x30, \ | |
55 | 0x30, 0x2d, 0x39, 0x30, 0x41, 0x20, 0x44, 0x52, 0x42, 0x47, 0x00}; | |
56 | #else | |
57 | # define DRBG_DEFAULT_PERS_STRING "OpenSSL NIST SP 800-90A DRBG" | |
58 | #endif | |
59 | ||
60 | typedef struct prov_drbg_st PROV_DRBG; | |
61 | ||
62 | /* DRBG status values */ | |
63 | typedef enum drbg_status_e { | |
64 | DRBG_UNINITIALISED, | |
65 | DRBG_READY, | |
66 | DRBG_ERROR | |
67 | } DRBG_STATUS; | |
68 | ||
69 | /* | |
f000e828 | 70 | * The state of all types of DRBGs. |
714a1bb3 P |
71 | */ |
72 | struct prov_drbg_st { | |
73 | CRYPTO_RWLOCK *lock; | |
f000e828 P |
74 | void *provctx; |
75 | ||
76 | /* Virtual functions are cache here */ | |
77 | int (*instantiate)(PROV_DRBG *drbg, | |
78 | const unsigned char *entropy, size_t entropylen, | |
79 | const unsigned char *nonce, size_t noncelen, | |
80 | const unsigned char *pers, size_t perslen); | |
81 | int (*uninstantiate)(PROV_DRBG *ctx); | |
82 | int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len, | |
83 | const unsigned char *adin, size_t adin_len); | |
84 | int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen, | |
85 | const unsigned char *adin, size_t adin_len); | |
86 | ||
87 | /* Parent PROV_RAND and its dispatch table functions */ | |
714a1bb3 | 88 | void *parent; |
363b1e5d DMSP |
89 | OSSL_FUNC_rand_enable_locking_fn *parent_enable_locking; |
90 | OSSL_FUNC_rand_lock_fn *parent_lock; | |
91 | OSSL_FUNC_rand_unlock_fn *parent_unlock; | |
92 | OSSL_FUNC_rand_get_ctx_params_fn *parent_get_ctx_params; | |
93 | OSSL_FUNC_rand_generate_fn *parent_generate; | |
94 | OSSL_FUNC_rand_nonce_fn *parent_nonce; | |
f000e828 | 95 | |
714a1bb3 | 96 | const OSSL_DISPATCH *parent_dispatch; |
f000e828 | 97 | |
714a1bb3 P |
98 | /* |
99 | * Stores the return value of openssl_get_fork_id() as of when we last | |
100 | * reseeded. The DRBG reseeds automatically whenever drbg->fork_id != | |
101 | * openssl_get_fork_id(). Used to provide fork-safety and reseed this | |
102 | * DRBG in the child process. | |
103 | */ | |
104 | int fork_id; | |
105 | unsigned short flags; /* various external flags */ | |
106 | ||
107 | /* | |
108 | * The random_data is used by PROV_add()/drbg_add() to attach random | |
109 | * data to the global drbg, such that the rand_drbg_get_entropy() callback | |
110 | * can pull it during instantiation and reseeding. This is necessary to | |
111 | * reconcile the different philosophies of the PROV and the PROV_DRBG | |
112 | * with respect to how randomness is added to the RNG during reseeding | |
113 | * (see PR #4328). | |
114 | */ | |
115 | struct rand_pool_st *seed_pool; | |
116 | ||
117 | /* | |
118 | * Auxiliary pool for additional data. | |
119 | */ | |
120 | struct rand_pool_st *adin_pool; | |
121 | ||
122 | /* | |
123 | * The following parameters are setup by the per-type "init" function. | |
124 | * | |
125 | * The supported types and their init functions are: | |
126 | * (1) CTR_DRBG: drbg_ctr_init(). | |
127 | * (2) HMAC_DRBG: drbg_hmac_init(). | |
128 | * (3) HASH_DRBG: drbg_hash_init(). | |
129 | * | |
130 | * The parameters are closely related to the ones described in | |
131 | * section '10.2.1 CTR_DRBG' of [NIST SP 800-90Ar1], with one | |
132 | * crucial difference: In the NIST standard, all counts are given | |
133 | * in bits, whereas in OpenSSL entropy counts are given in bits | |
134 | * and buffer lengths are given in bytes. | |
135 | * | |
136 | * Since this difference has lead to some confusion in the past, | |
137 | * (see [GitHub Issue #2443], formerly [rt.openssl.org #4055]) | |
138 | * the 'len' suffix has been added to all buffer sizes for | |
139 | * clarification. | |
140 | */ | |
141 | ||
f000e828 | 142 | unsigned int strength; |
714a1bb3 P |
143 | size_t max_request; |
144 | size_t min_entropylen, max_entropylen; | |
145 | size_t min_noncelen, max_noncelen; | |
146 | size_t max_perslen, max_adinlen; | |
147 | ||
148 | /* | |
149 | * Counts the number of generate requests since the last reseed | |
150 | * (Starts at 1). This value is the reseed_counter as defined in | |
151 | * NIST SP 800-90Ar1 | |
152 | */ | |
153 | unsigned int reseed_gen_counter; | |
154 | /* | |
155 | * Maximum number of generate requests until a reseed is required. | |
156 | * This value is ignored if it is zero. | |
157 | */ | |
158 | unsigned int reseed_interval; | |
159 | /* Stores the time when the last reseeding occurred */ | |
160 | time_t reseed_time; | |
161 | /* | |
162 | * Specifies the maximum time interval (in seconds) between reseeds. | |
163 | * This value is ignored if it is zero. | |
164 | */ | |
165 | time_t reseed_time_interval; | |
166 | /* | |
167 | * Counts the number of reseeds since instantiation. | |
168 | * This value is ignored if it is zero. | |
169 | * | |
170 | * This counter is used only for seed propagation from the <master> DRBG | |
171 | * to its two children, the <public> and <private> DRBG. This feature is | |
172 | * very special and its sole purpose is to ensure that any randomness which | |
173 | * is added by PROV_add() or PROV_seed() will have an immediate effect on | |
174 | * the output of PROV_bytes() resp. PROV_priv_bytes(). | |
175 | */ | |
f000e828 | 176 | TSAN_QUALIFIER unsigned int reseed_counter; |
714a1bb3 | 177 | unsigned int reseed_next_counter; |
f000e828 | 178 | unsigned int parent_reseed_counter; |
714a1bb3 P |
179 | |
180 | size_t seedlen; | |
181 | DRBG_STATUS state; | |
182 | ||
f000e828 | 183 | /* DRBG specific data */ |
714a1bb3 P |
184 | void *data; |
185 | ||
f000e828 P |
186 | /* Entropy and nonce gathering callbacks */ |
187 | void *callback_arg; | |
188 | OSSL_INOUT_CALLBACK *get_entropy_fn; | |
189 | OSSL_CALLBACK *cleanup_entropy_fn; | |
190 | OSSL_INOUT_CALLBACK *get_nonce_fn; | |
191 | OSSL_CALLBACK *cleanup_nonce_fn; | |
714a1bb3 P |
192 | }; |
193 | ||
f000e828 P |
194 | PROV_DRBG *prov_rand_drbg_new |
195 | (void *provctx, void *parent, const OSSL_DISPATCH *parent_dispatch, | |
196 | int (*dnew)(PROV_DRBG *ctx), | |
197 | int (*instantiate)(PROV_DRBG *drbg, | |
198 | const unsigned char *entropy, size_t entropylen, | |
199 | const unsigned char *nonce, size_t noncelen, | |
200 | const unsigned char *pers, size_t perslen), | |
201 | int (*uninstantiate)(PROV_DRBG *ctx), | |
202 | int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len, | |
203 | const unsigned char *adin, size_t adin_len), | |
204 | int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen, | |
205 | const unsigned char *adin, size_t adin_len)); | |
206 | void prov_rand_drbg_free(PROV_DRBG *drbg); | |
207 | ||
208 | int PROV_DRBG_instantiate(PROV_DRBG *drbg, unsigned int strength, | |
714a1bb3 | 209 | int prediction_resistance, |
f000e828 P |
210 | const unsigned char *pers, size_t perslen); |
211 | ||
212 | int PROV_DRBG_uninstantiate(PROV_DRBG *drbg); | |
714a1bb3 P |
213 | |
214 | int PROV_DRBG_reseed(PROV_DRBG *drbg, int prediction_resistance, | |
215 | const unsigned char *ent, size_t ent_len, | |
f000e828 | 216 | const unsigned char *adin, size_t adinlen); |
714a1bb3 P |
217 | |
218 | int PROV_DRBG_generate(PROV_DRBG *drbg, unsigned char *out, size_t outlen, | |
f000e828 P |
219 | unsigned int strength, int prediction_resistance, |
220 | const unsigned char *adin, size_t adinlen); | |
221 | ||
f000e828 P |
222 | /* Verify that an array of numeric values is all zero */ |
223 | #define PROV_DRBG_VERYIFY_ZEROIZATION(v) \ | |
224 | { \ | |
225 | size_t i; \ | |
226 | \ | |
227 | for (i = 0; i < OSSL_NELEM(v); i++) \ | |
228 | if ((v)[i] != 0) \ | |
229 | return 0; \ | |
230 | } | |
714a1bb3 P |
231 | |
232 | /* locking api */ | |
363b1e5d DMSP |
233 | OSSL_FUNC_rand_enable_locking_fn drbg_enable_locking; |
234 | OSSL_FUNC_rand_lock_fn drbg_lock; | |
235 | OSSL_FUNC_rand_unlock_fn drbg_unlock; | |
714a1bb3 | 236 | |
f000e828 | 237 | /* Common parameters for all of our DRBGs */ |
714a1bb3 P |
238 | int drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[]); |
239 | int drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[]); | |
240 | ||
241 | #define OSSL_PARAM_DRBG_SETABLE_CTX_COMMON \ | |
f000e828 P |
242 | OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, NULL), \ |
243 | OSSL_PARAM_uint64(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, NULL) | |
714a1bb3 P |
244 | |
245 | #define OSSL_PARAM_DRBG_GETABLE_CTX_COMMON \ | |
f000e828 | 246 | OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL), \ |
714a1bb3 | 247 | OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL), \ |
f000e828 P |
248 | OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_REQUEST, NULL), \ |
249 | OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_ENTROPYLEN, NULL), \ | |
250 | OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ENTROPYLEN, NULL), \ | |
251 | OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_NONCELEN, NULL), \ | |
252 | OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_NONCELEN, NULL), \ | |
253 | OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_PERSLEN, NULL), \ | |
254 | OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ADINLEN, NULL), \ | |
255 | OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_CTR, NULL), \ | |
256 | OSSL_PARAM_time_t(OSSL_DRBG_PARAM_RESEED_TIME, NULL), \ | |
257 | OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, NULL), \ | |
258 | OSSL_PARAM_uint64(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, NULL) | |
259 | ||
260 | /* Continuous test "entropy" calls */ | |
714a1bb3 P |
261 | size_t prov_crngt_get_entropy(PROV_DRBG *drbg, |
262 | unsigned char **pout, | |
263 | int entropy, size_t min_len, size_t max_len, | |
264 | int prediction_resistance); | |
265 | void prov_crngt_cleanup_entropy(PROV_DRBG *drbg, | |
266 | unsigned char *out, size_t outlen); | |
267 | ||
714a1bb3 | 268 | #endif |