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