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 # define CORE_HANDLE(provctx) ossl_prov_ctx_get0_handle(provctx)
39 #endif
40
ossl_prov_seeding_from_dispatch(const OSSL_DISPATCH * fns)41 int ossl_prov_seeding_from_dispatch(const OSSL_DISPATCH *fns)
42 {
43 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 #define set_func(c, f) \
50 do { if (c == NULL) c = f; else if (c != f) return 0; } while (0)
51 switch (fns->function_id) {
52 case OSSL_FUNC_GET_ENTROPY:
53 set_func(c_get_entropy, OSSL_FUNC_get_entropy(fns));
54 break;
55 case OSSL_FUNC_GET_USER_ENTROPY:
56 set_func(c_get_user_entropy, OSSL_FUNC_get_user_entropy(fns));
57 break;
58 case OSSL_FUNC_CLEANUP_ENTROPY:
59 set_func(c_cleanup_entropy, OSSL_FUNC_cleanup_entropy(fns));
60 break;
61 case OSSL_FUNC_CLEANUP_USER_ENTROPY:
62 set_func(c_cleanup_user_entropy, OSSL_FUNC_cleanup_user_entropy(fns));
63 break;
64 case OSSL_FUNC_GET_NONCE:
65 set_func(c_get_nonce, OSSL_FUNC_get_nonce(fns));
66 break;
67 case OSSL_FUNC_GET_USER_NONCE:
68 set_func(c_get_user_nonce, OSSL_FUNC_get_user_nonce(fns));
69 break;
70 case OSSL_FUNC_CLEANUP_NONCE:
71 set_func(c_cleanup_nonce, OSSL_FUNC_cleanup_nonce(fns));
72 break;
73 case OSSL_FUNC_CLEANUP_USER_NONCE:
74 set_func(c_cleanup_user_nonce, OSSL_FUNC_cleanup_user_nonce(fns));
75 break;
76 }
77 #undef set_func
78 }
79 return 1;
80 }
81
ossl_prov_get_entropy(PROV_CTX * prov_ctx,unsigned char ** pout,int entropy,size_t min_len,size_t max_len)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 {
85 const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx);
86
87 if (c_get_user_entropy != NULL)
88 return c_get_user_entropy(handle, pout, entropy, min_len, max_len);
89 if (c_get_entropy != NULL)
90 return c_get_entropy(handle, pout, entropy, min_len, max_len);
91 return 0;
92 }
93
ossl_prov_cleanup_entropy(PROV_CTX * prov_ctx,unsigned char * buf,size_t len)94 void ossl_prov_cleanup_entropy(PROV_CTX *prov_ctx, unsigned char *buf,
95 size_t len)
96 {
97 const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx);
98
99 if (c_cleanup_user_entropy != NULL)
100 c_cleanup_user_entropy(handle, buf, len);
101 else if (c_cleanup_entropy != NULL)
102 c_cleanup_entropy(handle, buf, len);
103 }
104
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)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 {
109 const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx);
110
111 if (c_get_user_nonce != NULL)
112 return c_get_user_nonce(handle, pout, min_len, max_len, salt, salt_len);
113 if (c_get_nonce != NULL)
114 return c_get_nonce(handle, pout, min_len, max_len, salt, salt_len);
115 return 0;
116 }
117
ossl_prov_cleanup_nonce(PROV_CTX * prov_ctx,unsigned char * buf,size_t len)118 void ossl_prov_cleanup_nonce(PROV_CTX *prov_ctx, unsigned char *buf, size_t len)
119 {
120 const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx);
121
122 if (c_cleanup_user_nonce != NULL)
123 c_cleanup_user_nonce(handle, buf, len);
124 else if (c_cleanup_nonce != NULL)
125 c_cleanup_nonce(handle, buf, len);
126 }
127