Path: blob/main/crypto/openssl/providers/implementations/rands/drbg_local.h
48383 views
/*1* Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#ifndef OSSL_CRYPTO_PROV_LOCAL_H10# define OSSL_CRYPTO_PROV_LOCAL_H1112# include <openssl/evp.h>13# include <openssl/core_dispatch.h>14# include <openssl/core_names.h>15# include <openssl/params.h>16# include "internal/tsan_assist.h"17# include "internal/nelem.h"18# include "internal/numbers.h"19# include "prov/provider_ctx.h"20# include "prov/securitycheck.h"2122/* How many times to read the TSC as a randomness source. */23# define TSC_READ_COUNT 42425/* Maximum reseed intervals */26# define MAX_RESEED_INTERVAL (1 << 24)27# define MAX_RESEED_TIME_INTERVAL (1 << 20) /* approx. 12 days */2829/* Default reseed intervals */30# define RESEED_INTERVAL (1 << 8)31# define TIME_INTERVAL (60*60) /* 1 hour */3233/*34* Maximum input size for the DRBG (entropy, nonce, personalization string)35*36* NIST SP800 90Ar1 allows a maximum of (1 << 35) bits i.e., (1 << 32) bytes.37*38* We lower it to 'only' INT32_MAX bytes, which is equivalent to 2 gigabytes.39*/40# define DRBG_MAX_LENGTH INT32_MAX4142/* The default nonce */43/* ASCII: "OpenSSL NIST SP 800-90A DRBG", in hex for EBCDIC compatibility */44#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"4546typedef struct prov_drbg_st PROV_DRBG;4748/* DRBG status values */49typedef enum drbg_status_e {50DRBG_UNINITIALISED,51DRBG_READY,52DRBG_ERROR53} DRBG_STATUS;5455/*56* The state of all types of DRBGs.57*/58struct prov_drbg_st {59CRYPTO_RWLOCK *lock;60PROV_CTX *provctx;6162/* Virtual functions are cached here */63int (*instantiate)(PROV_DRBG *drbg,64const unsigned char *entropy, size_t entropylen,65const unsigned char *nonce, size_t noncelen,66const unsigned char *pers, size_t perslen);67int (*uninstantiate)(PROV_DRBG *ctx);68int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len,69const unsigned char *adin, size_t adin_len);70int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen,71const unsigned char *adin, size_t adin_len);7273/* Parent PROV_RAND and its dispatch table functions */74void *parent;75OSSL_FUNC_rand_enable_locking_fn *parent_enable_locking;76OSSL_FUNC_rand_lock_fn *parent_lock;77OSSL_FUNC_rand_unlock_fn *parent_unlock;78OSSL_FUNC_rand_get_ctx_params_fn *parent_get_ctx_params;79OSSL_FUNC_rand_nonce_fn *parent_nonce;80OSSL_FUNC_rand_get_seed_fn *parent_get_seed;81OSSL_FUNC_rand_clear_seed_fn *parent_clear_seed;8283/*84* Stores the return value of openssl_get_fork_id() as of when we last85* reseeded. The DRBG reseeds automatically whenever drbg->fork_id !=86* openssl_get_fork_id(). Used to provide fork-safety and reseed this87* DRBG in the child process.88*/89int fork_id;90unsigned short flags; /* various external flags */9192/*93* The following parameters are setup by the per-type "init" function.94*95* The supported types and their init functions are:96* (1) CTR_DRBG: drbg_ctr_init().97* (2) HMAC_DRBG: drbg_hmac_init().98* (3) HASH_DRBG: drbg_hash_init().99*100* The parameters are closely related to the ones described in101* section '10.2.1 CTR_DRBG' of [NIST SP 800-90Ar1], with one102* crucial difference: In the NIST standard, all counts are given103* in bits, whereas in OpenSSL entropy counts are given in bits104* and buffer lengths are given in bytes.105*106* Since this difference has lead to some confusion in the past,107* (see [GitHub Issue #2443], formerly [rt.openssl.org #4055])108* the 'len' suffix has been added to all buffer sizes for109* clarification.110*/111112unsigned int strength;113size_t max_request;114size_t min_entropylen, max_entropylen;115size_t min_noncelen, max_noncelen;116size_t max_perslen, max_adinlen;117118/*119* Counts the number of generate requests since the last reseed120* (Starts at 1). This value is the reseed_counter as defined in121* NIST SP 800-90Ar1122*/123unsigned int generate_counter;124/*125* Maximum number of generate requests until a reseed is required.126* This value is ignored if it is zero.127*/128unsigned int reseed_interval;129/* Stores the time when the last reseeding occurred */130time_t reseed_time;131/*132* Specifies the maximum time interval (in seconds) between reseeds.133* This value is ignored if it is zero.134*/135time_t reseed_time_interval;136/*137* Counts the number of reseeds since instantiation.138* This value is ignored if it is zero.139*140* This counter is used only for seed propagation from the <master> DRBG141* to its two children, the <public> and <private> DRBG. This feature is142* very special and its sole purpose is to ensure that any randomness which143* is added by PROV_add() or PROV_seed() will have an immediate effect on144* the output of PROV_bytes() resp. PROV_priv_bytes().145*/146TSAN_QUALIFIER unsigned int reseed_counter;147unsigned int reseed_next_counter;148unsigned int parent_reseed_counter;149150size_t seedlen;151DRBG_STATUS state;152153/* DRBG specific data */154void *data;155156/* Entropy and nonce gathering callbacks */157void *callback_arg;158OSSL_INOUT_CALLBACK *get_entropy_fn;159OSSL_CALLBACK *cleanup_entropy_fn;160OSSL_INOUT_CALLBACK *get_nonce_fn;161OSSL_CALLBACK *cleanup_nonce_fn;162163OSSL_FIPS_IND_DECLARE164};165166PROV_DRBG *ossl_rand_drbg_new167(void *provctx, void *parent, const OSSL_DISPATCH *parent_dispatch,168int (*dnew)(PROV_DRBG *ctx),169void (*dfree)(void *vctx),170int (*instantiate)(PROV_DRBG *drbg,171const unsigned char *entropy, size_t entropylen,172const unsigned char *nonce, size_t noncelen,173const unsigned char *pers, size_t perslen),174int (*uninstantiate)(PROV_DRBG *ctx),175int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len,176const unsigned char *adin, size_t adin_len),177int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen,178const unsigned char *adin, size_t adin_len));179void ossl_rand_drbg_free(PROV_DRBG *drbg);180181int ossl_prov_drbg_instantiate(PROV_DRBG *drbg, unsigned int strength,182int prediction_resistance,183const unsigned char *pers, size_t perslen);184185int ossl_prov_drbg_uninstantiate(PROV_DRBG *drbg);186187int ossl_prov_drbg_reseed(PROV_DRBG *drbg, int prediction_resistance,188const unsigned char *ent, size_t ent_len,189const unsigned char *adin, size_t adinlen);190191int ossl_prov_drbg_generate(PROV_DRBG *drbg, unsigned char *out, size_t outlen,192unsigned int strength, int prediction_resistance,193const unsigned char *adin, size_t adinlen);194195/* Seeding api */196OSSL_FUNC_rand_get_seed_fn ossl_drbg_get_seed;197OSSL_FUNC_rand_clear_seed_fn ossl_drbg_clear_seed;198199/* Verify that an array of numeric values is all zero */200#define PROV_DRBG_VERIFY_ZEROIZATION(v) \201{ \202size_t i; \203\204for (i = 0; i < OSSL_NELEM(v); i++) \205if ((v)[i] != 0) \206goto err; \207}208209/* locking api */210OSSL_FUNC_rand_enable_locking_fn ossl_drbg_enable_locking;211OSSL_FUNC_rand_lock_fn ossl_drbg_lock;212OSSL_FUNC_rand_unlock_fn ossl_drbg_unlock;213214/* Common parameters for all of our DRBGs */215int ossl_drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[]);216int ossl_drbg_get_ctx_params_no_lock(PROV_DRBG *drbg, OSSL_PARAM params[],217int *complete);218int ossl_drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[]);219220#define OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON \221OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, NULL), \222OSSL_PARAM_uint64(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, NULL)223224#define OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON \225OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL), \226OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL), \227OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL), \228OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_ENTROPYLEN, NULL), \229OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ENTROPYLEN, NULL), \230OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_NONCELEN, NULL), \231OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_NONCELEN, NULL), \232OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_PERSLEN, NULL), \233OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ADINLEN, NULL), \234OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_COUNTER, NULL), \235OSSL_PARAM_time_t(OSSL_DRBG_PARAM_RESEED_TIME, NULL), \236OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, NULL), \237OSSL_PARAM_uint64(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, NULL)238239/* Confirm digest is allowed to be used with a DRBG */240int ossl_drbg_verify_digest(PROV_DRBG *drbg, OSSL_LIB_CTX *libctx, const EVP_MD *md);241242#endif243244245