/src/openssl/providers/common/provider_seeding.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
4  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use  | 
5  |  |  * this file except in compliance with the License.  You can obtain a copy  | 
6  |  |  * in the file LICENSE in the source distribution or at  | 
7  |  |  * https://www.openssl.org/source/license.html  | 
8  |  |  */  | 
9  |  |  | 
10  |  | #include <openssl/core_dispatch.h>  | 
11  |  | #include "prov/seeding.h"  | 
12  |  | #include "prov/providercommon.h"  | 
13  |  |  | 
14  |  | static OSSL_FUNC_get_entropy_fn *c_get_entropy = NULL;  | 
15  |  | static OSSL_FUNC_get_user_entropy_fn *c_get_user_entropy = NULL;  | 
16  |  | static OSSL_FUNC_cleanup_entropy_fn *c_cleanup_entropy = NULL;  | 
17  |  | static OSSL_FUNC_cleanup_user_entropy_fn *c_cleanup_user_entropy = NULL;  | 
18  |  | static OSSL_FUNC_get_nonce_fn *c_get_nonce = NULL;  | 
19  |  | static OSSL_FUNC_get_user_nonce_fn *c_get_user_nonce = NULL;  | 
20  |  | static OSSL_FUNC_cleanup_nonce_fn *c_cleanup_nonce = NULL;  | 
21  |  | static OSSL_FUNC_cleanup_user_nonce_fn *c_cleanup_user_nonce = NULL;  | 
22  |  |  | 
23  |  | #ifdef FIPS_MODULE  | 
24  |  | /*  | 
25  |  |  * The FIPS provider uses an internal library context which is what the  | 
26  |  |  * passed provider context references.  Since the seed source is external  | 
27  |  |  * to the FIPS provider, this is the wrong one.  We need to convert this  | 
28  |  |  * to the correct core handle before up-calling libcrypto.  | 
29  |  |  */  | 
30  |  | # define CORE_HANDLE(provctx) \  | 
31  |  |     FIPS_get_core_handle(ossl_prov_ctx_get0_libctx(provctx))  | 
32  |  | #else  | 
33  |  | /*  | 
34  |  |  * The non-FIPS path *should* be unused because the full DRBG chain including  | 
35  |  |  * seed source is instantiated.  However, that might not apply for third  | 
36  |  |  * party providers, so this is retained for compatibility.  | 
37  |  |  */  | 
38  | 0  | # define CORE_HANDLE(provctx) ossl_prov_ctx_get0_handle(provctx)  | 
39  |  | #endif  | 
40  |  |  | 
41  |  | int ossl_prov_seeding_from_dispatch(const OSSL_DISPATCH *fns)  | 
42  | 0  | { | 
43  | 0  |     for (; fns->function_id != 0; fns++) { | 
44  |  |         /*  | 
45  |  |          * We do not support the scenario of an application linked against  | 
46  |  |          * multiple versions of libcrypto (e.g. one static and one dynamic), but  | 
47  |  |          * sharing a single fips.so. We do a simple sanity check here.  | 
48  |  |          */  | 
49  | 0  | #define set_func(c, f) \  | 
50  | 0  |     do { if (c == NULL) c = f; else if (c != f) return 0; } while (0) | 
51  | 0  |         switch (fns->function_id) { | 
52  | 0  |         case OSSL_FUNC_GET_ENTROPY:  | 
53  | 0  |             set_func(c_get_entropy, OSSL_FUNC_get_entropy(fns));  | 
54  | 0  |             break;  | 
55  | 0  |         case OSSL_FUNC_GET_USER_ENTROPY:  | 
56  | 0  |             set_func(c_get_user_entropy, OSSL_FUNC_get_user_entropy(fns));  | 
57  | 0  |             break;  | 
58  | 0  |         case OSSL_FUNC_CLEANUP_ENTROPY:  | 
59  | 0  |             set_func(c_cleanup_entropy, OSSL_FUNC_cleanup_entropy(fns));  | 
60  | 0  |             break;  | 
61  | 0  |         case OSSL_FUNC_CLEANUP_USER_ENTROPY:  | 
62  | 0  |             set_func(c_cleanup_user_entropy, OSSL_FUNC_cleanup_user_entropy(fns));  | 
63  | 0  |             break;  | 
64  | 0  |         case OSSL_FUNC_GET_NONCE:  | 
65  | 0  |             set_func(c_get_nonce, OSSL_FUNC_get_nonce(fns));  | 
66  | 0  |             break;  | 
67  | 0  |         case OSSL_FUNC_GET_USER_NONCE:  | 
68  | 0  |             set_func(c_get_user_nonce, OSSL_FUNC_get_user_nonce(fns));  | 
69  | 0  |             break;  | 
70  | 0  |         case OSSL_FUNC_CLEANUP_NONCE:  | 
71  | 0  |             set_func(c_cleanup_nonce, OSSL_FUNC_cleanup_nonce(fns));  | 
72  | 0  |             break;  | 
73  | 0  |         case OSSL_FUNC_CLEANUP_USER_NONCE:  | 
74  | 0  |             set_func(c_cleanup_user_nonce, OSSL_FUNC_cleanup_user_nonce(fns));  | 
75  | 0  |             break;  | 
76  | 0  |         }  | 
77  | 0  | #undef set_func  | 
78  | 0  |     }  | 
79  | 0  |     return 1;  | 
80  | 0  | }  | 
81  |  |  | 
82  |  | size_t ossl_prov_get_entropy(PROV_CTX *prov_ctx, unsigned char **pout,  | 
83  |  |                              int entropy, size_t min_len, size_t max_len)  | 
84  | 0  | { | 
85  | 0  |     const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx);  | 
86  |  | 
  | 
87  | 0  |     if (c_get_user_entropy != NULL)  | 
88  | 0  |         return c_get_user_entropy(handle, pout, entropy, min_len, max_len);  | 
89  | 0  |     if (c_get_entropy != NULL)  | 
90  | 0  |         return c_get_entropy(handle, pout, entropy, min_len, max_len);  | 
91  | 0  |     return 0;  | 
92  | 0  | }  | 
93  |  |  | 
94  |  | void ossl_prov_cleanup_entropy(PROV_CTX *prov_ctx, unsigned char *buf,  | 
95  |  |                                size_t len)  | 
96  | 0  | { | 
97  | 0  |     const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx);  | 
98  |  | 
  | 
99  | 0  |     if (c_cleanup_user_entropy != NULL)  | 
100  | 0  |         c_cleanup_user_entropy(handle, buf, len);  | 
101  | 0  |     else if (c_cleanup_entropy != NULL)  | 
102  | 0  |         c_cleanup_entropy(handle, buf, len);  | 
103  | 0  | }  | 
104  |  |  | 
105  |  | size_t ossl_prov_get_nonce(PROV_CTX *prov_ctx, unsigned char **pout,  | 
106  |  |                            size_t min_len, size_t max_len,  | 
107  |  |                            const void *salt, size_t salt_len)  | 
108  | 0  | { | 
109  | 0  |     const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx);  | 
110  |  | 
  | 
111  | 0  |     if (c_get_user_nonce != NULL)  | 
112  | 0  |         return c_get_user_nonce(handle, pout, min_len, max_len, salt, salt_len);  | 
113  | 0  |     if (c_get_nonce != NULL)  | 
114  | 0  |         return c_get_nonce(handle, pout, min_len, max_len, salt, salt_len);  | 
115  | 0  |     return 0;  | 
116  | 0  | }  | 
117  |  |  | 
118  |  | void ossl_prov_cleanup_nonce(PROV_CTX *prov_ctx, unsigned char *buf, size_t len)  | 
119  | 0  | { | 
120  | 0  |     const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx);  | 
121  |  | 
  | 
122  | 0  |     if (c_cleanup_user_nonce != NULL)  | 
123  | 0  |         c_cleanup_user_nonce(handle, buf, len);  | 
124  | 0  |     else if (c_cleanup_nonce != NULL)  | 
125  | 0  |         c_cleanup_nonce(handle, buf, len);  | 
126  | 0  | }  |