/src/openssl32/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 | 616 | # 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 | 38 | { |
43 | 1.99k | 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 | 1.95k | #define set_func(c, f) \ |
50 | 1.95k | do { if (c == NULL) c = f; else if (c != f) return 0; } while (0) |
51 | 1.95k | switch (fns->function_id) { |
52 | 38 | case OSSL_FUNC_GET_ENTROPY: |
53 | 38 | set_func(c_get_entropy, OSSL_FUNC_get_entropy(fns)); |
54 | 38 | break; |
55 | 38 | case OSSL_FUNC_GET_USER_ENTROPY: |
56 | 38 | set_func(c_get_user_entropy, OSSL_FUNC_get_user_entropy(fns)); |
57 | 38 | break; |
58 | 38 | case OSSL_FUNC_CLEANUP_ENTROPY: |
59 | 38 | set_func(c_cleanup_entropy, OSSL_FUNC_cleanup_entropy(fns)); |
60 | 38 | break; |
61 | 38 | case OSSL_FUNC_CLEANUP_USER_ENTROPY: |
62 | 38 | set_func(c_cleanup_user_entropy, OSSL_FUNC_cleanup_user_entropy(fns)); |
63 | 38 | break; |
64 | 38 | case OSSL_FUNC_GET_NONCE: |
65 | 38 | set_func(c_get_nonce, OSSL_FUNC_get_nonce(fns)); |
66 | 38 | break; |
67 | 38 | case OSSL_FUNC_GET_USER_NONCE: |
68 | 38 | set_func(c_get_user_nonce, OSSL_FUNC_get_user_nonce(fns)); |
69 | 38 | break; |
70 | 38 | case OSSL_FUNC_CLEANUP_NONCE: |
71 | 38 | set_func(c_cleanup_nonce, OSSL_FUNC_cleanup_nonce(fns)); |
72 | 38 | break; |
73 | 38 | case OSSL_FUNC_CLEANUP_USER_NONCE: |
74 | 38 | set_func(c_cleanup_user_nonce, OSSL_FUNC_cleanup_user_nonce(fns)); |
75 | 38 | break; |
76 | 1.95k | } |
77 | 1.95k | #undef set_func |
78 | 1.95k | } |
79 | 38 | return 1; |
80 | 38 | } |
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 | 208 | { |
85 | 208 | const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx); |
86 | | |
87 | 208 | if (c_get_user_entropy != NULL) |
88 | 208 | 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 | 208 | { |
97 | 208 | const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx); |
98 | | |
99 | 208 | if (c_cleanup_user_entropy != NULL) |
100 | 208 | c_cleanup_user_entropy(handle, buf, len); |
101 | 0 | else if (c_cleanup_entropy != NULL) |
102 | 0 | c_cleanup_entropy(handle, buf, len); |
103 | 208 | } |
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 | 100 | { |
109 | 100 | const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx); |
110 | | |
111 | 100 | if (c_get_user_nonce != NULL) |
112 | 100 | 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 | 100 | { |
120 | 100 | const OSSL_CORE_HANDLE *handle = CORE_HANDLE(prov_ctx); |
121 | | |
122 | 100 | if (c_cleanup_user_nonce != NULL) |
123 | 100 | c_cleanup_user_nonce(handle, buf, len); |
124 | 0 | else if (c_cleanup_nonce != NULL) |
125 | 0 | c_cleanup_nonce(handle, buf, len); |
126 | 100 | } |