xref: /freebsd-src/crypto/openssl/providers/common/provider_seeding.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery  * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery  */
9*b077aed3SPierre Pronchery 
10*b077aed3SPierre Pronchery #include <openssl/core_dispatch.h>
11*b077aed3SPierre Pronchery #include "prov/seeding.h"
12*b077aed3SPierre Pronchery 
13*b077aed3SPierre Pronchery static OSSL_FUNC_get_entropy_fn *c_get_entropy = NULL;
14*b077aed3SPierre Pronchery static OSSL_FUNC_cleanup_entropy_fn *c_cleanup_entropy = NULL;
15*b077aed3SPierre Pronchery static OSSL_FUNC_get_nonce_fn *c_get_nonce = NULL;
16*b077aed3SPierre Pronchery static OSSL_FUNC_cleanup_nonce_fn *c_cleanup_nonce = NULL;
17*b077aed3SPierre Pronchery 
ossl_prov_seeding_from_dispatch(const OSSL_DISPATCH * fns)18*b077aed3SPierre Pronchery int ossl_prov_seeding_from_dispatch(const OSSL_DISPATCH *fns)
19*b077aed3SPierre Pronchery {
20*b077aed3SPierre Pronchery     for (; fns->function_id != 0; fns++) {
21*b077aed3SPierre Pronchery         /*
22*b077aed3SPierre Pronchery          * We do not support the scenario of an application linked against
23*b077aed3SPierre Pronchery          * multiple versions of libcrypto (e.g. one static and one dynamic), but
24*b077aed3SPierre Pronchery          * sharing a single fips.so. We do a simple sanity check here.
25*b077aed3SPierre Pronchery          */
26*b077aed3SPierre Pronchery #define set_func(c, f) \
27*b077aed3SPierre Pronchery     do { if (c == NULL) c = f; else if (c != f) return 0; } while (0)
28*b077aed3SPierre Pronchery         switch (fns->function_id) {
29*b077aed3SPierre Pronchery         case OSSL_FUNC_GET_ENTROPY:
30*b077aed3SPierre Pronchery             set_func(c_get_entropy, OSSL_FUNC_get_entropy(fns));
31*b077aed3SPierre Pronchery             break;
32*b077aed3SPierre Pronchery         case OSSL_FUNC_CLEANUP_ENTROPY:
33*b077aed3SPierre Pronchery             set_func(c_cleanup_entropy, OSSL_FUNC_cleanup_entropy(fns));
34*b077aed3SPierre Pronchery             break;
35*b077aed3SPierre Pronchery         case OSSL_FUNC_GET_NONCE:
36*b077aed3SPierre Pronchery             set_func(c_get_nonce, OSSL_FUNC_get_nonce(fns));
37*b077aed3SPierre Pronchery             break;
38*b077aed3SPierre Pronchery         case OSSL_FUNC_CLEANUP_NONCE:
39*b077aed3SPierre Pronchery             set_func(c_cleanup_nonce, OSSL_FUNC_cleanup_nonce(fns));
40*b077aed3SPierre Pronchery             break;
41*b077aed3SPierre Pronchery         }
42*b077aed3SPierre Pronchery #undef set_func
43*b077aed3SPierre Pronchery     }
44*b077aed3SPierre Pronchery     return 1;
45*b077aed3SPierre Pronchery }
46*b077aed3SPierre Pronchery 
ossl_prov_get_entropy(PROV_CTX * prov_ctx,unsigned char ** pout,int entropy,size_t min_len,size_t max_len)47*b077aed3SPierre Pronchery size_t ossl_prov_get_entropy(PROV_CTX *prov_ctx, unsigned char **pout,
48*b077aed3SPierre Pronchery                              int entropy, size_t min_len, size_t max_len)
49*b077aed3SPierre Pronchery {
50*b077aed3SPierre Pronchery     if (c_get_entropy == NULL)
51*b077aed3SPierre Pronchery         return 0;
52*b077aed3SPierre Pronchery     return c_get_entropy(ossl_prov_ctx_get0_handle(prov_ctx),
53*b077aed3SPierre Pronchery                          pout, entropy, min_len, max_len);
54*b077aed3SPierre Pronchery }
55*b077aed3SPierre Pronchery 
ossl_prov_cleanup_entropy(PROV_CTX * prov_ctx,unsigned char * buf,size_t len)56*b077aed3SPierre Pronchery void ossl_prov_cleanup_entropy(PROV_CTX *prov_ctx, unsigned char *buf,
57*b077aed3SPierre Pronchery                                size_t len)
58*b077aed3SPierre Pronchery {
59*b077aed3SPierre Pronchery     if (c_cleanup_entropy != NULL)
60*b077aed3SPierre Pronchery         c_cleanup_entropy(ossl_prov_ctx_get0_handle(prov_ctx), buf, len);
61*b077aed3SPierre Pronchery }
62*b077aed3SPierre Pronchery 
ossl_prov_get_nonce(PROV_CTX * prov_ctx,unsigned char ** pout,size_t min_len,size_t max_len,const void * salt,size_t salt_len)63*b077aed3SPierre Pronchery size_t ossl_prov_get_nonce(PROV_CTX *prov_ctx, unsigned char **pout,
64*b077aed3SPierre Pronchery                            size_t min_len, size_t max_len,
65*b077aed3SPierre Pronchery                            const void *salt,size_t salt_len)
66*b077aed3SPierre Pronchery {
67*b077aed3SPierre Pronchery     if (c_get_nonce == NULL)
68*b077aed3SPierre Pronchery         return 0;
69*b077aed3SPierre Pronchery     return c_get_nonce(ossl_prov_ctx_get0_handle(prov_ctx), pout,
70*b077aed3SPierre Pronchery                        min_len, max_len, salt, salt_len);
71*b077aed3SPierre Pronchery }
72*b077aed3SPierre Pronchery 
ossl_prov_cleanup_nonce(PROV_CTX * prov_ctx,unsigned char * buf,size_t len)73*b077aed3SPierre Pronchery void ossl_prov_cleanup_nonce(PROV_CTX *prov_ctx, unsigned char *buf, size_t len)
74*b077aed3SPierre Pronchery {
75*b077aed3SPierre Pronchery     if (c_cleanup_nonce != NULL)
76*b077aed3SPierre Pronchery         c_cleanup_nonce(ossl_prov_ctx_get0_handle(prov_ctx), buf, len);
77*b077aed3SPierre Pronchery }
78