Path: blob/main/crypto/openssl/providers/implementations/rands/seed_src_jitter.c
48383 views
/*1* Copyright 2024-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#include <string.h>10#include <openssl/rand.h>11#include <openssl/core_dispatch.h>12#include <openssl/e_os2.h>13#include <openssl/params.h>14#include <openssl/core_names.h>15#include <openssl/evp.h>16#include <openssl/err.h>17#include <openssl/randerr.h>18#include <openssl/proverr.h>19#include <openssl/self_test.h>20#include "prov/implementations.h"21#include "prov/provider_ctx.h"22#include "prov/providercommon.h"23#include "crypto/rand.h"24#include "crypto/rand_pool.h"2526#ifndef OPENSSL_NO_JITTER27# include <jitterentropy.h>2829# define JITTER_MAX_NUM_TRIES 33031static OSSL_FUNC_rand_newctx_fn jitter_new;32static OSSL_FUNC_rand_freectx_fn jitter_free;33static OSSL_FUNC_rand_instantiate_fn jitter_instantiate;34static OSSL_FUNC_rand_uninstantiate_fn jitter_uninstantiate;35static OSSL_FUNC_rand_generate_fn jitter_generate;36static OSSL_FUNC_rand_reseed_fn jitter_reseed;37static OSSL_FUNC_rand_gettable_ctx_params_fn jitter_gettable_ctx_params;38static OSSL_FUNC_rand_get_ctx_params_fn jitter_get_ctx_params;39static OSSL_FUNC_rand_verify_zeroization_fn jitter_verify_zeroization;40static OSSL_FUNC_rand_enable_locking_fn jitter_enable_locking;41static OSSL_FUNC_rand_lock_fn jitter_lock;42static OSSL_FUNC_rand_unlock_fn jitter_unlock;43static OSSL_FUNC_rand_get_seed_fn jitter_get_seed;44static OSSL_FUNC_rand_clear_seed_fn jitter_clear_seed;4546typedef struct {47void *provctx;48int state;49} PROV_JITTER;5051static size_t get_jitter_random_value(PROV_JITTER *s, unsigned char *buf, size_t len);5253/*54* Acquire entropy from jitterentropy library55*56* Returns the total entropy count, if it exceeds the requested57* entropy count. Otherwise, returns an entropy count of 0.58*/59static size_t ossl_prov_acquire_entropy_from_jitter(PROV_JITTER *s,60RAND_POOL *pool)61{62size_t bytes_needed;63unsigned char *buffer;6465bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /* entropy_factor */);66if (bytes_needed > 0) {67buffer = ossl_rand_pool_add_begin(pool, bytes_needed);6869if (buffer != NULL) {70if (get_jitter_random_value(s, buffer, bytes_needed) == bytes_needed) {71ossl_rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);72} else {73ossl_rand_pool_add_end(pool, 0, 0);74}75}76}7778return ossl_rand_pool_entropy_available(pool);79}8081/* Obtain random bytes from the jitter library */82static size_t get_jitter_random_value(PROV_JITTER *s,83unsigned char *buf, size_t len)84{85struct rand_data *jitter_ec = NULL;86ssize_t result = 0;87size_t num_tries;8889/* Retry intermittent failures, then give up */90for (num_tries = 0; num_tries < JITTER_MAX_NUM_TRIES; num_tries++) {91/* Allocate a fresh collector */92jitter_ec = jent_entropy_collector_alloc(0, JENT_FORCE_FIPS);93if (jitter_ec == NULL)94continue;9596/* Do not use _safe API as per typical security policies */97result = jent_read_entropy(jitter_ec, (char *) buf, len);98jent_entropy_collector_free(jitter_ec);99100/*101* Permanent Failure102* https://github.com/smuellerDD/jitterentropy-library/blob/master/doc/jitterentropy.3#L234103*/104if (result < -5) {105ossl_set_error_state(OSSL_SELF_TEST_TYPE_CRNG);106break;107}108109/* Success */110if (result >= 0 && (size_t)result == len)111return len;112}113114/* Permanent failure or too many intermittent failures */115s->state = EVP_RAND_STATE_ERROR;116ERR_raise_data(ERR_LIB_RAND, RAND_R_ERROR_RETRIEVING_ENTROPY,117"jent_read_entropy (%d)", result);118return 0;119}120121static void *jitter_new(void *provctx, void *parent,122const OSSL_DISPATCH *parent_dispatch)123{124PROV_JITTER *s;125126if (parent != NULL) {127ERR_raise(ERR_LIB_PROV, PROV_R_SEED_SOURCES_MUST_NOT_HAVE_A_PARENT);128return NULL;129}130131s = OPENSSL_zalloc(sizeof(*s));132if (s == NULL)133return NULL;134135s->provctx = provctx;136s->state = EVP_RAND_STATE_UNINITIALISED;137return s;138}139140static void jitter_free(void *vseed)141{142OPENSSL_free(vseed);143}144145static int jitter_instantiate(void *vseed, unsigned int strength,146int prediction_resistance,147const unsigned char *pstr,148size_t pstr_len,149ossl_unused const OSSL_PARAM params[])150{151PROV_JITTER *s = (PROV_JITTER *)vseed;152int ret;153154if ((ret = jent_entropy_init_ex(0, JENT_FORCE_FIPS)) != 0) {155ERR_raise_data(ERR_LIB_RAND, RAND_R_ERROR_RETRIEVING_ENTROPY,156"jent_entropy_init_ex (%d)", ret);157s->state = EVP_RAND_STATE_ERROR;158return 0;159}160161s->state = EVP_RAND_STATE_READY;162return 1;163}164165static int jitter_uninstantiate(void *vseed)166{167PROV_JITTER *s = (PROV_JITTER *)vseed;168169s->state = EVP_RAND_STATE_UNINITIALISED;170return 1;171}172173static int jitter_generate(void *vseed, unsigned char *out, size_t outlen,174unsigned int strength,175ossl_unused int prediction_resistance,176ossl_unused const unsigned char *adin,177ossl_unused size_t adin_len)178{179PROV_JITTER *s = (PROV_JITTER *)vseed;180size_t entropy_available;181RAND_POOL *pool;182183if (s->state != EVP_RAND_STATE_READY) {184ERR_raise(ERR_LIB_PROV,185s->state == EVP_RAND_STATE_ERROR ? PROV_R_IN_ERROR_STATE186: PROV_R_NOT_INSTANTIATED);187return 0;188}189190pool = ossl_rand_pool_new(strength, 1, outlen, outlen);191if (pool == NULL) {192ERR_raise(ERR_LIB_PROV, ERR_R_RAND_LIB);193return 0;194}195196/* Get entropy from jitter entropy library. */197entropy_available = ossl_prov_acquire_entropy_from_jitter(s, pool);198199if (entropy_available > 0) {200if (!ossl_rand_pool_adin_mix_in(pool, adin, adin_len)) {201ossl_rand_pool_free(pool);202return 0;203}204memcpy(out, ossl_rand_pool_buffer(pool), ossl_rand_pool_length(pool));205}206207ossl_rand_pool_free(pool);208return entropy_available > 0;209}210211static int jitter_reseed(void *vseed,212ossl_unused int prediction_resistance,213ossl_unused const unsigned char *ent,214ossl_unused size_t ent_len,215ossl_unused const unsigned char *adin,216ossl_unused size_t adin_len)217{218PROV_JITTER *s = (PROV_JITTER *)vseed;219220if (s->state != EVP_RAND_STATE_READY) {221ERR_raise(ERR_LIB_PROV,222s->state == EVP_RAND_STATE_ERROR ? PROV_R_IN_ERROR_STATE223: PROV_R_NOT_INSTANTIATED);224return 0;225}226return 1;227}228229static int jitter_get_ctx_params(void *vseed, OSSL_PARAM params[])230{231PROV_JITTER *s = (PROV_JITTER *)vseed;232OSSL_PARAM *p;233234p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);235if (p != NULL && !OSSL_PARAM_set_int(p, s->state))236return 0;237238p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);239if (p != NULL && !OSSL_PARAM_set_int(p, 1024))240return 0;241242p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);243if (p != NULL && !OSSL_PARAM_set_size_t(p, 128))244return 0;245return 1;246}247248static const OSSL_PARAM *jitter_gettable_ctx_params(ossl_unused void *vseed,249ossl_unused void *provctx)250{251static const OSSL_PARAM known_gettable_ctx_params[] = {252OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL),253OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),254OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),255OSSL_PARAM_END256};257return known_gettable_ctx_params;258}259260static int jitter_verify_zeroization(ossl_unused void *vseed)261{262return 1;263}264265static size_t jitter_get_seed(void *vseed, unsigned char **pout,266int entropy, size_t min_len,267size_t max_len,268int prediction_resistance,269const unsigned char *adin,270size_t adin_len)271{272size_t ret = 0;273size_t entropy_available = 0;274RAND_POOL *pool;275PROV_JITTER *s = (PROV_JITTER *)vseed;276277pool = ossl_rand_pool_new(entropy, 1, min_len, max_len);278if (pool == NULL) {279ERR_raise(ERR_LIB_PROV, ERR_R_RAND_LIB);280return 0;281}282283/* Get entropy from jitter entropy library. */284entropy_available = ossl_prov_acquire_entropy_from_jitter(s, pool);285286if (entropy_available > 0287&& ossl_rand_pool_adin_mix_in(pool, adin, adin_len)) {288ret = ossl_rand_pool_length(pool);289*pout = ossl_rand_pool_detach(pool);290} else {291ERR_raise(ERR_LIB_PROV, PROV_R_ENTROPY_SOURCE_STRENGTH_TOO_WEAK);292}293ossl_rand_pool_free(pool);294return ret;295}296297# ifndef OPENSSL_NO_FIPS_JITTER298size_t ossl_rand_jitter_get_seed(unsigned char **pout, int entropy, size_t min_len, size_t max_len)299{300size_t ret = 0;301OSSL_PARAM params[1] = { OSSL_PARAM_END };302PROV_JITTER *s = jitter_new(NULL, NULL, NULL);303304if (s == NULL)305return ret;306if (!jitter_instantiate(s, 0, 0, NULL, 0, params))307goto end;308ret = jitter_get_seed(s, pout, entropy, min_len, max_len, 0, NULL, 0);309end:310jitter_free(s);311return ret;312}313# endif314315static void jitter_clear_seed(ossl_unused void *vdrbg,316unsigned char *out, size_t outlen)317{318OPENSSL_secure_clear_free(out, outlen);319}320321static int jitter_enable_locking(ossl_unused void *vseed)322{323return 1;324}325326int jitter_lock(ossl_unused void *vctx)327{328return 1;329}330331void jitter_unlock(ossl_unused void *vctx)332{333}334335const OSSL_DISPATCH ossl_jitter_functions[] = {336{ OSSL_FUNC_RAND_NEWCTX, (void(*)(void))jitter_new },337{ OSSL_FUNC_RAND_FREECTX, (void(*)(void))jitter_free },338{ OSSL_FUNC_RAND_INSTANTIATE,339(void(*)(void))jitter_instantiate },340{ OSSL_FUNC_RAND_UNINSTANTIATE,341(void(*)(void))jitter_uninstantiate },342{ OSSL_FUNC_RAND_GENERATE, (void(*)(void))jitter_generate },343{ OSSL_FUNC_RAND_RESEED, (void(*)(void))jitter_reseed },344{ OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))jitter_enable_locking },345{ OSSL_FUNC_RAND_LOCK, (void(*)(void))jitter_lock },346{ OSSL_FUNC_RAND_UNLOCK, (void(*)(void))jitter_unlock },347{ OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,348(void(*)(void))jitter_gettable_ctx_params },349{ OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))jitter_get_ctx_params },350{ OSSL_FUNC_RAND_VERIFY_ZEROIZATION,351(void(*)(void))jitter_verify_zeroization },352{ OSSL_FUNC_RAND_GET_SEED, (void(*)(void))jitter_get_seed },353{ OSSL_FUNC_RAND_CLEAR_SEED, (void(*)(void))jitter_clear_seed },354OSSL_DISPATCH_END355};356#else357NON_EMPTY_TRANSLATION_UNIT358#endif359360361