Path: blob/main/crypto/openssl/providers/common/provider_seeding.c
106842 views
/*1* Copyright 2020-2023 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 <openssl/core_dispatch.h>10#include "prov/seeding.h"11#include "prov/providercommon.h"1213static OSSL_FUNC_get_entropy_fn *c_get_entropy = NULL;14static OSSL_FUNC_get_user_entropy_fn *c_get_user_entropy = NULL;15static OSSL_FUNC_cleanup_entropy_fn *c_cleanup_entropy = NULL;16static OSSL_FUNC_cleanup_user_entropy_fn *c_cleanup_user_entropy = NULL;17static OSSL_FUNC_get_nonce_fn *c_get_nonce = NULL;18static OSSL_FUNC_get_user_nonce_fn *c_get_user_nonce = NULL;19static OSSL_FUNC_cleanup_nonce_fn *c_cleanup_nonce = NULL;20static OSSL_FUNC_cleanup_user_nonce_fn *c_cleanup_user_nonce = NULL;2122#ifdef FIPS_MODULE23/*24* The FIPS provider uses an internal library context which is what the25* passed provider context references. Since the seed source is external26* to the FIPS provider, this is the wrong one. We need to convert this27* to the correct core handle before up-calling libcrypto.28*/29#define CORE_HANDLE(provctx) \30FIPS_get_core_handle(ossl_prov_ctx_get0_libctx(provctx))31#else32/*33* The non-FIPS path *should* be unused because the full DRBG chain including34* seed source is instantiated. However, that might not apply for third35* party providers, so this is retained for compatibility.36*/37#define CORE_HANDLE(provctx) ossl_prov_ctx_get0_handle(provctx)38#endif3940int ossl_prov_seeding_from_dispatch(const OSSL_DISPATCH *fns)41{42for (; fns->function_id != 0; fns++) {43/*44* We do not support the scenario of an application linked against45* multiple versions of libcrypto (e.g. one static and one dynamic), but46* sharing a single fips.so. We do a simple sanity check here.47*/48#define set_func(c, f) \49do { \50if (c == NULL) \51c = f; \52else if (c != f) \53return 0; \54} while (0)55switch (fns->function_id) {56case OSSL_FUNC_GET_ENTROPY:57set_func(c_get_entropy, OSSL_FUNC_get_entropy(fns));58break;59case OSSL_FUNC_GET_USER_ENTROPY:60set_func(c_get_user_entropy, OSSL_FUNC_get_user_entropy(fns));61break;62case OSSL_FUNC_CLEANUP_ENTROPY:63set_func(c_cleanup_entropy, OSSL_FUNC_cleanup_entropy(fns));64break;65case OSSL_FUNC_CLEANUP_USER_ENTROPY:66set_func(c_cleanup_user_entropy, OSSL_FUNC_cleanup_user_entropy(fns));67break;68case OSSL_FUNC_GET_NONCE:69set_func(c_get_nonce, OSSL_FUNC_get_nonce(fns));70break;71case OSSL_FUNC_GET_USER_NONCE:72set_func(c_get_user_nonce, OSSL_FUNC_get_user_nonce(fns));73break;74case OSSL_FUNC_CLEANUP_NONCE:75set_func(c_cleanup_nonce, OSSL_FUNC_cleanup_nonce(fns));76break;77case OSSL_FUNC_CLEANUP_USER_NONCE:78set_func(c_cleanup_user_nonce, OSSL_FUNC_cleanup_user_nonce(fns));79break;80}81#undef set_func82}83return 1;84}8586size_t ossl_prov_get_entropy(PROV_CTX *prov_ctx, unsigned char **pout,87int entropy, size_t min_len, size_t max_len)88{89const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx);9091if (c_get_user_entropy != NULL)92return c_get_user_entropy(handle, pout, entropy, min_len, max_len);93if (c_get_entropy != NULL)94return c_get_entropy(handle, pout, entropy, min_len, max_len);95return 0;96}9798void ossl_prov_cleanup_entropy(PROV_CTX *prov_ctx, unsigned char *buf,99size_t len)100{101const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx);102103if (c_cleanup_user_entropy != NULL)104c_cleanup_user_entropy(handle, buf, len);105else if (c_cleanup_entropy != NULL)106c_cleanup_entropy(handle, buf, len);107}108109size_t ossl_prov_get_nonce(PROV_CTX *prov_ctx, unsigned char **pout,110size_t min_len, size_t max_len,111const void *salt, size_t salt_len)112{113const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx);114115if (c_get_user_nonce != NULL)116return c_get_user_nonce(handle, pout, min_len, max_len, salt, salt_len);117if (c_get_nonce != NULL)118return c_get_nonce(handle, pout, min_len, max_len, salt, salt_len);119return 0;120}121122void ossl_prov_cleanup_nonce(PROV_CTX *prov_ctx, unsigned char *buf, size_t len)123{124const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx);125126if (c_cleanup_user_nonce != NULL)127c_cleanup_user_nonce(handle, buf, len);128else if (c_cleanup_nonce != NULL)129c_cleanup_nonce(handle, buf, len);130}131132133