Path: blob/main/crypto/openssl/providers/common/provider_seeding.c
48262 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 { if (c == NULL) c = f; else if (c != f) return 0; } while (0)50switch (fns->function_id) {51case OSSL_FUNC_GET_ENTROPY:52set_func(c_get_entropy, OSSL_FUNC_get_entropy(fns));53break;54case OSSL_FUNC_GET_USER_ENTROPY:55set_func(c_get_user_entropy, OSSL_FUNC_get_user_entropy(fns));56break;57case OSSL_FUNC_CLEANUP_ENTROPY:58set_func(c_cleanup_entropy, OSSL_FUNC_cleanup_entropy(fns));59break;60case OSSL_FUNC_CLEANUP_USER_ENTROPY:61set_func(c_cleanup_user_entropy, OSSL_FUNC_cleanup_user_entropy(fns));62break;63case OSSL_FUNC_GET_NONCE:64set_func(c_get_nonce, OSSL_FUNC_get_nonce(fns));65break;66case OSSL_FUNC_GET_USER_NONCE:67set_func(c_get_user_nonce, OSSL_FUNC_get_user_nonce(fns));68break;69case OSSL_FUNC_CLEANUP_NONCE:70set_func(c_cleanup_nonce, OSSL_FUNC_cleanup_nonce(fns));71break;72case OSSL_FUNC_CLEANUP_USER_NONCE:73set_func(c_cleanup_user_nonce, OSSL_FUNC_cleanup_user_nonce(fns));74break;75}76#undef set_func77}78return 1;79}8081size_t ossl_prov_get_entropy(PROV_CTX *prov_ctx, unsigned char **pout,82int entropy, size_t min_len, size_t max_len)83{84const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx);8586if (c_get_user_entropy != NULL)87return c_get_user_entropy(handle, pout, entropy, min_len, max_len);88if (c_get_entropy != NULL)89return c_get_entropy(handle, pout, entropy, min_len, max_len);90return 0;91}9293void ossl_prov_cleanup_entropy(PROV_CTX *prov_ctx, unsigned char *buf,94size_t len)95{96const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx);9798if (c_cleanup_user_entropy != NULL)99c_cleanup_user_entropy(handle, buf, len);100else if (c_cleanup_entropy != NULL)101c_cleanup_entropy(handle, buf, len);102}103104size_t ossl_prov_get_nonce(PROV_CTX *prov_ctx, unsigned char **pout,105size_t min_len, size_t max_len,106const void *salt, size_t salt_len)107{108const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx);109110if (c_get_user_nonce != NULL)111return c_get_user_nonce(handle, pout, min_len, max_len, salt, salt_len);112if (c_get_nonce != NULL)113return c_get_nonce(handle, pout, min_len, max_len, salt, salt_len);114return 0;115}116117void ossl_prov_cleanup_nonce(PROV_CTX *prov_ctx, unsigned char *buf, size_t len)118{119const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx);120121if (c_cleanup_user_nonce != NULL)122c_cleanup_user_nonce(handle, buf, len);123else if (c_cleanup_nonce != NULL)124c_cleanup_nonce(handle, buf, len);125}126127128