2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #ifndef OSSL_CRYPTO_PROV_LOCAL_H
11 # define OSSL_CRYPTO_PROV_LOCAL_H
13 # include <openssl/evp.h>
14 # include <openssl/core_numbers.h>
15 # include <openssl/core_names.h>
16 # include <openssl/params.h>
17 # include "internal/tsan_assist.h"
19 # include "internal/numbers.h"
21 /* How many times to read the TSC as a randomness source. */
22 # define TSC_READ_COUNT 4
24 /* Maximum reseed intervals */
25 # define MAX_RESEED_INTERVAL (1 << 24)
26 # define MAX_RESEED_TIME_INTERVAL (1 << 20) /* approx. 12 days */
28 /* Default reseed intervals */
29 # define MASTER_RESEED_INTERVAL (1 << 8)
30 # define SLAVE_RESEED_INTERVAL (1 << 16)
31 # define MASTER_RESEED_TIME_INTERVAL (60*60) /* 1 hour */
32 # define SLAVE_RESEED_TIME_INTERVAL (7*60) /* 7 minutes */
35 * The number of bytes that constitutes an atomic lump of entropy with respect
36 * to the FIPS 140-2 section 4.9.2 Conditional Tests. The size is somewhat
37 * arbitrary, the smaller the value, the less entropy is consumed on first
38 * read but the higher the probability of the test failing by accident.
40 * The value is in bytes.
42 #define CRNGT_BUFSIZ 16
45 * Maximum input size for the DRBG (entropy, nonce, personalization string)
47 * NIST SP800 90Ar1 allows a maximum of (1 << 35) bits i.e., (1 << 32) bytes.
49 * We lower it to 'only' INT32_MAX bytes, which is equivalent to 2 gigabytes.
51 # define DRBG_MAX_LENGTH INT32_MAX
53 /* The default nonce */
55 # define DRBG_DEFAULT_PERS_STRING { 0x4f, 0x70, 0x65, 0x6e, 0x53, 0x53, \
56 0x4c, 0x20, 0x4e, 0x49, 0x53, 0x54, 0x20, 0x53, 0x50, 0x20, 0x38, 0x30, \
57 0x30, 0x2d, 0x39, 0x30, 0x41, 0x20, 0x44, 0x52, 0x42, 0x47, 0x00};
59 # define DRBG_DEFAULT_PERS_STRING "OpenSSL NIST SP 800-90A DRBG"
62 typedef struct prov_drbg_st PROV_DRBG;
64 /* DRBG status values */
65 typedef enum drbg_status_e {
75 typedef struct rand_drbg_hmac_st {
79 unsigned char K[EVP_MAX_MD_SIZE];
80 unsigned char V[EVP_MAX_MD_SIZE];
84 * The state of a DRBG AES-CTR.
86 typedef struct rand_drbg_ctr_st {
87 EVP_CIPHER_CTX *ctx_ecb;
88 EVP_CIPHER_CTX *ctx_ctr;
89 EVP_CIPHER_CTX *ctx_df;
90 EVP_CIPHER *cipher_ecb;
91 EVP_CIPHER *cipher_ctr;
95 /* Temporary block storage used by ctr_df */
96 unsigned char bltmp[16];
103 * The state of all types of DRBGs, even though we only have CTR mode
106 struct prov_drbg_st {
108 /* The library context this DRBG is associated with, if any */
111 const OSSL_DISPATCH *parent_dispatch;
112 int secure; /* 1: allocated on the secure heap, 0: otherwise */
114 * Stores the return value of openssl_get_fork_id() as of when we last
115 * reseeded. The DRBG reseeds automatically whenever drbg->fork_id !=
116 * openssl_get_fork_id(). Used to provide fork-safety and reseed this
117 * DRBG in the child process.
120 unsigned short flags; /* various external flags */
123 * The random_data is used by PROV_add()/drbg_add() to attach random
124 * data to the global drbg, such that the rand_drbg_get_entropy() callback
125 * can pull it during instantiation and reseeding. This is necessary to
126 * reconcile the different philosophies of the PROV and the PROV_DRBG
127 * with respect to how randomness is added to the RNG during reseeding
130 struct rand_pool_st *seed_pool;
133 * Auxiliary pool for additional data.
135 struct rand_pool_st *adin_pool;
138 * The following parameters are setup by the per-type "init" function.
140 * The supported types and their init functions are:
141 * (1) CTR_DRBG: drbg_ctr_init().
142 * (2) HMAC_DRBG: drbg_hmac_init().
143 * (3) HASH_DRBG: drbg_hash_init().
145 * The parameters are closely related to the ones described in
146 * section '10.2.1 CTR_DRBG' of [NIST SP 800-90Ar1], with one
147 * crucial difference: In the NIST standard, all counts are given
148 * in bits, whereas in OpenSSL entropy counts are given in bits
149 * and buffer lengths are given in bytes.
151 * Since this difference has lead to some confusion in the past,
152 * (see [GitHub Issue #2443], formerly [rt.openssl.org #4055])
153 * the 'len' suffix has been added to all buffer sizes for
159 size_t min_entropylen, max_entropylen;
160 size_t min_noncelen, max_noncelen;
161 size_t max_perslen, max_adinlen;
164 * Counts the number of generate requests since the last reseed
165 * (Starts at 1). This value is the reseed_counter as defined in
168 unsigned int reseed_gen_counter;
170 * Maximum number of generate requests until a reseed is required.
171 * This value is ignored if it is zero.
173 unsigned int reseed_interval;
174 /* Stores the time when the last reseeding occurred */
177 * Specifies the maximum time interval (in seconds) between reseeds.
178 * This value is ignored if it is zero.
180 time_t reseed_time_interval;
182 * Counts the number of reseeds since instantiation.
183 * This value is ignored if it is zero.
185 * This counter is used only for seed propagation from the <master> DRBG
186 * to its two children, the <public> and <private> DRBG. This feature is
187 * very special and its sole purpose is to ensure that any randomness which
188 * is added by PROV_add() or PROV_seed() will have an immediate effect on
189 * the output of PROV_bytes() resp. PROV_priv_bytes().
191 TSAN_QUALIFIER unsigned int reseed_prop_counter;
192 unsigned int reseed_next_counter;
200 /* Application data, mainly used in the KATs. */
201 CRYPTO_EX_DATA ex_data;
206 int rand_drbg_restart(PROV_DRBG *drbg,
207 const unsigned char *buffer, size_t len, size_t entropy);
208 size_t rand_drbg_seedlen(PROV_DRBG *drbg);
210 PROV_DRBG *prov_rand_drbg_new(void *provctx, int secure, void *parent,
211 const OSSL_DISPATCH *parent_dispatch,
212 int (*dnew)(PROV_DRBG *ctx, int secure));
213 void prov_rand_free(PROV_DRBG *drbg);
215 int PROV_DRBG_instantiate(PROV_DRBG *drbg, int strength,
216 int prediction_resistance,
217 const unsigned char *pers, size_t perslen,
218 int (*ifnc)(PROV_DRBG *drbg,
219 const unsigned char *ent, size_t ent_len,
220 const unsigned char *nonce,
222 const unsigned char *pstr,
225 int PROV_DRBG_reseed(PROV_DRBG *drbg, int prediction_resistance,
226 const unsigned char *ent, size_t ent_len,
227 const unsigned char *adin, size_t adinlen,
228 int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent,
229 size_t ent_len, const unsigned char *adin,
232 int PROV_DRBG_generate(PROV_DRBG *drbg, unsigned char *out, size_t outlen,
233 int strength, int prediction_resistance,
234 const unsigned char *adin, size_t adinlen,
235 int (*generate)(PROV_DRBG *, unsigned char *out,
236 size_t outlen, const unsigned char *adin,
238 int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent,
239 size_t ent_len, const unsigned char *adin,
243 OSSL_OP_rand_enable_locking_fn drbg_enable_locking;
244 OSSL_OP_rand_lock_fn drbg_lock;
245 OSSL_OP_rand_unlock_fn drbg_unlock;
247 int drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[]);
248 int drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[]);
250 #define OSSL_PARAM_DRBG_SETABLE_CTX_COMMON \
251 OSSL_PARAM_uint(OSSL_RAND_PARAM_RESEED_REQUESTS, NULL), \
252 OSSL_PARAM_uint64(OSSL_RAND_PARAM_RESEED_TIME_INTERVAL, NULL)
254 #define OSSL_PARAM_DRBG_GETABLE_CTX_COMMON \
255 OSSL_PARAM_int(OSSL_RAND_PARAM_STATUS, NULL), \
256 OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL), \
257 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL), \
258 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MIN_ENTROPYLEN, NULL), \
259 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_ENTROPYLEN, NULL), \
260 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MIN_NONCELEN, NULL), \
261 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_NONCELEN, NULL), \
262 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_PERSLEN, NULL), \
263 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_ADINLEN, NULL), \
264 OSSL_PARAM_uint(OSSL_RAND_PARAM_RESEED_CTR, NULL), \
265 OSSL_PARAM_uint(OSSL_RAND_PARAM_RESEED_REQUESTS, NULL), \
266 OSSL_PARAM_uint64(OSSL_RAND_PARAM_RESEED_TIME_INTERVAL, NULL)
268 size_t prov_crngt_get_entropy(PROV_DRBG *drbg,
269 unsigned char **pout,
270 int entropy, size_t min_len, size_t max_len,
271 int prediction_resistance);
272 void prov_crngt_cleanup_entropy(PROV_DRBG *drbg,
273 unsigned char *out, size_t outlen);
276 * Entropy call back for the FIPS 140-2 section 4.9.2 Conditional Tests.
277 * These need to be exposed for the unit tests.
280 int rand_crngt_get_entropy_cb(OPENSSL_CTX *ctx, PROV_POOL *pool,
281 unsigned char *buf, unsigned char *md,
282 unsigned int *md_size);
283 extern int (*crngt_get_entropy)(OPENSSL_CTX *ctx, PROV_POOL *pool,
284 unsigned char *buf, unsigned char *md,
285 unsigned int *md_size);