1 /* 2 * Copyright 1995-2024 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 * 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. 39 * 40 * The value is in bytes. 41 */ 42 #define CRNGT_BUFSIZ 16 43 44 /* 45 * Maximum input size for the DRBG (entropy, nonce, personalization string) 46 * 47 * NIST SP800 90Ar1 allows a maximum of (1 << 35) bits i.e., (1 << 32) bytes. 48 * 49 * We lower it to 'only' INT32_MAX bytes, which is equivalent to 2 gigabytes. 50 */ 51 # define DRBG_MAX_LENGTH INT32_MAX 52 53 /* The default nonce */ 54 /* ASCII: "OpenSSL NIST SP 800-90A DRBG", in hex for EBCDIC compatibility */ 55 #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" 56 57 typedef struct prov_drbg_st PROV_DRBG; 58 59 /* DRBG status values */ 60 typedef enum drbg_status_e { 61 DRBG_UNINITIALISED, 62 DRBG_READY, 63 DRBG_ERROR 64 } DRBG_STATUS; 65 66 /* 67 * The state of all types of DRBGs. 68 */ 69 struct prov_drbg_st { 70 CRYPTO_RWLOCK *lock; 71 PROV_CTX *provctx; 72 73 /* Virtual functions are cached here */ 74 int (*instantiate)(PROV_DRBG *drbg, 75 const unsigned char *entropy, size_t entropylen, 76 const unsigned char *nonce, size_t noncelen, 77 const unsigned char *pers, size_t perslen); 78 int (*uninstantiate)(PROV_DRBG *ctx); 79 int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len, 80 const unsigned char *adin, size_t adin_len); 81 int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen, 82 const unsigned char *adin, size_t adin_len); 83 84 /* Parent PROV_RAND and its dispatch table functions */ 85 void *parent; 86 OSSL_FUNC_rand_enable_locking_fn *parent_enable_locking; 87 OSSL_FUNC_rand_lock_fn *parent_lock; 88 OSSL_FUNC_rand_unlock_fn *parent_unlock; 89 OSSL_FUNC_rand_get_ctx_params_fn *parent_get_ctx_params; 90 OSSL_FUNC_rand_nonce_fn *parent_nonce; 91 OSSL_FUNC_rand_get_seed_fn *parent_get_seed; 92 OSSL_FUNC_rand_clear_seed_fn *parent_clear_seed; 93 94 /* 95 * Stores the return value of openssl_get_fork_id() as of when we last 96 * reseeded. The DRBG reseeds automatically whenever drbg->fork_id != 97 * openssl_get_fork_id(). Used to provide fork-safety and reseed this 98 * DRBG in the child process. 99 */ 100 int fork_id; 101 unsigned short flags; /* various external flags */ 102 103 /* 104 * The following parameters are setup by the per-type "init" function. 105 * 106 * The supported types and their init functions are: 107 * (1) CTR_DRBG: drbg_ctr_init(). 108 * (2) HMAC_DRBG: drbg_hmac_init(). 109 * (3) HASH_DRBG: drbg_hash_init(). 110 * 111 * The parameters are closely related to the ones described in 112 * section '10.2.1 CTR_DRBG' of [NIST SP 800-90Ar1], with one 113 * crucial difference: In the NIST standard, all counts are given 114 * in bits, whereas in OpenSSL entropy counts are given in bits 115 * and buffer lengths are given in bytes. 116 * 117 * Since this difference has lead to some confusion in the past, 118 * (see [GitHub Issue #2443], formerly [rt.openssl.org #4055]) 119 * the 'len' suffix has been added to all buffer sizes for 120 * clarification. 121 */ 122 123 unsigned int strength; 124 size_t max_request; 125 size_t min_entropylen, max_entropylen; 126 size_t min_noncelen, max_noncelen; 127 size_t max_perslen, max_adinlen; 128 129 /* 130 * Counts the number of generate requests since the last reseed 131 * (Starts at 1). This value is the reseed_counter as defined in 132 * NIST SP 800-90Ar1 133 */ 134 unsigned int generate_counter; 135 /* 136 * Maximum number of generate requests until a reseed is required. 137 * This value is ignored if it is zero. 138 */ 139 unsigned int reseed_interval; 140 /* Stores the time when the last reseeding occurred */ 141 time_t reseed_time; 142 /* 143 * Specifies the maximum time interval (in seconds) between reseeds. 144 * This value is ignored if it is zero. 145 */ 146 time_t reseed_time_interval; 147 /* 148 * Counts the number of reseeds since instantiation. 149 * This value is ignored if it is zero. 150 * 151 * This counter is used only for seed propagation from the <master> DRBG 152 * to its two children, the <public> and <private> DRBG. This feature is 153 * very special and its sole purpose is to ensure that any randomness which 154 * is added by PROV_add() or PROV_seed() will have an immediate effect on 155 * the output of PROV_bytes() resp. PROV_priv_bytes(). 156 */ 157 TSAN_QUALIFIER unsigned int reseed_counter; 158 unsigned int reseed_next_counter; 159 unsigned int parent_reseed_counter; 160 161 size_t seedlen; 162 DRBG_STATUS state; 163 164 /* DRBG specific data */ 165 void *data; 166 167 /* Entropy and nonce gathering callbacks */ 168 void *callback_arg; 169 OSSL_INOUT_CALLBACK *get_entropy_fn; 170 OSSL_CALLBACK *cleanup_entropy_fn; 171 OSSL_INOUT_CALLBACK *get_nonce_fn; 172 OSSL_CALLBACK *cleanup_nonce_fn; 173 174 OSSL_FIPS_IND_DECLARE 175 }; 176 177 PROV_DRBG *ossl_rand_drbg_new 178 (void *provctx, void *parent, const OSSL_DISPATCH *parent_dispatch, 179 int (*dnew)(PROV_DRBG *ctx), 180 void (*dfree)(void *vctx), 181 int (*instantiate)(PROV_DRBG *drbg, 182 const unsigned char *entropy, size_t entropylen, 183 const unsigned char *nonce, size_t noncelen, 184 const unsigned char *pers, size_t perslen), 185 int (*uninstantiate)(PROV_DRBG *ctx), 186 int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len, 187 const unsigned char *adin, size_t adin_len), 188 int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen, 189 const unsigned char *adin, size_t adin_len)); 190 void ossl_rand_drbg_free(PROV_DRBG *drbg); 191 192 int ossl_prov_drbg_instantiate(PROV_DRBG *drbg, unsigned int strength, 193 int prediction_resistance, 194 const unsigned char *pers, size_t perslen); 195 196 int ossl_prov_drbg_uninstantiate(PROV_DRBG *drbg); 197 198 int ossl_prov_drbg_reseed(PROV_DRBG *drbg, int prediction_resistance, 199 const unsigned char *ent, size_t ent_len, 200 const unsigned char *adin, size_t adinlen); 201 202 int ossl_prov_drbg_generate(PROV_DRBG *drbg, unsigned char *out, size_t outlen, 203 unsigned int strength, int prediction_resistance, 204 const unsigned char *adin, size_t adinlen); 205 206 /* Seeding api */ 207 OSSL_FUNC_rand_get_seed_fn ossl_drbg_get_seed; 208 OSSL_FUNC_rand_clear_seed_fn ossl_drbg_clear_seed; 209 210 /* Verify that an array of numeric values is all zero */ 211 #define PROV_DRBG_VERIFY_ZEROIZATION(v) \ 212 { \ 213 size_t i; \ 214 \ 215 for (i = 0; i < OSSL_NELEM(v); i++) \ 216 if ((v)[i] != 0) \ 217 goto err; \ 218 } 219 220 /* locking api */ 221 OSSL_FUNC_rand_enable_locking_fn ossl_drbg_enable_locking; 222 OSSL_FUNC_rand_lock_fn ossl_drbg_lock; 223 OSSL_FUNC_rand_unlock_fn ossl_drbg_unlock; 224 225 /* Common parameters for all of our DRBGs */ 226 int ossl_drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[]); 227 int ossl_drbg_get_ctx_params_no_lock(PROV_DRBG *drbg, OSSL_PARAM params[], 228 int *complete); 229 int ossl_drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[]); 230 231 #define OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON \ 232 OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, NULL), \ 233 OSSL_PARAM_uint64(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, NULL) 234 235 #define OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON \ 236 OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL), \ 237 OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL), \ 238 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL), \ 239 OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_ENTROPYLEN, NULL), \ 240 OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ENTROPYLEN, NULL), \ 241 OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_NONCELEN, NULL), \ 242 OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_NONCELEN, NULL), \ 243 OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_PERSLEN, NULL), \ 244 OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ADINLEN, NULL), \ 245 OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_COUNTER, NULL), \ 246 OSSL_PARAM_time_t(OSSL_DRBG_PARAM_RESEED_TIME, NULL), \ 247 OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, NULL), \ 248 OSSL_PARAM_uint64(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, NULL) 249 250 /* Continuous test "entropy" calls */ 251 size_t ossl_crngt_get_entropy(PROV_DRBG *drbg, 252 unsigned char **pout, 253 int entropy, size_t min_len, size_t max_len, 254 int prediction_resistance); 255 void ossl_crngt_cleanup_entropy(PROV_DRBG *drbg, 256 unsigned char *out, size_t outlen); 257 258 /* Confirm digest is allowed to be used with a DRBG */ 259 int ossl_drbg_verify_digest(PROV_DRBG *drbg, OSSL_LIB_CTX *libctx, const EVP_MD *md); 260 261 #endif 262