/src/openssl/providers/implementations/exchange/ecx_exch.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2020-2025 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/crypto.h> |
11 | | #include <openssl/core_dispatch.h> |
12 | | #include <openssl/core_names.h> |
13 | | #include <openssl/params.h> |
14 | | #include <openssl/err.h> |
15 | | #include <openssl/proverr.h> |
16 | | #include "internal/cryptlib.h" |
17 | | #include "crypto/ecx.h" |
18 | | #include "prov/implementations.h" |
19 | | #include "prov/providercommon.h" |
20 | | #include "prov/securitycheck.h" |
21 | | |
22 | | #ifdef FIPS_MODULE |
23 | | # include "providers/implementations/exchange/ecx_exch.inc" |
24 | | #endif |
25 | | |
26 | | static OSSL_FUNC_keyexch_newctx_fn x25519_newctx; |
27 | | static OSSL_FUNC_keyexch_newctx_fn x448_newctx; |
28 | | static OSSL_FUNC_keyexch_init_fn x25519_init; |
29 | | static OSSL_FUNC_keyexch_init_fn x448_init; |
30 | | static OSSL_FUNC_keyexch_set_peer_fn ecx_set_peer; |
31 | | static OSSL_FUNC_keyexch_derive_fn ecx_derive; |
32 | | static OSSL_FUNC_keyexch_freectx_fn ecx_freectx; |
33 | | static OSSL_FUNC_keyexch_dupctx_fn ecx_dupctx; |
34 | | static OSSL_FUNC_keyexch_gettable_ctx_params_fn ecx_gettable_ctx_params; |
35 | | static OSSL_FUNC_keyexch_get_ctx_params_fn ecx_get_ctx_params; |
36 | | |
37 | | /* |
38 | | * What's passed as an actual key is defined by the KEYMGMT interface. |
39 | | * We happen to know that our KEYMGMT simply passes ECX_KEY structures, so |
40 | | * we use that here too. |
41 | | */ |
42 | | |
43 | | typedef struct { |
44 | | size_t keylen; |
45 | | ECX_KEY *key; |
46 | | ECX_KEY *peerkey; |
47 | | } PROV_ECX_CTX; |
48 | | |
49 | | static void *ecx_newctx(void *provctx, size_t keylen) |
50 | 0 | { |
51 | 0 | PROV_ECX_CTX *ctx; |
52 | |
|
53 | 0 | if (!ossl_prov_is_running()) |
54 | 0 | return NULL; |
55 | | |
56 | 0 | ctx = OPENSSL_zalloc(sizeof(PROV_ECX_CTX)); |
57 | 0 | if (ctx == NULL) |
58 | 0 | return NULL; |
59 | | |
60 | 0 | ctx->keylen = keylen; |
61 | |
|
62 | 0 | return ctx; |
63 | 0 | } |
64 | | |
65 | | static void *x25519_newctx(void *provctx) |
66 | 0 | { |
67 | 0 | return ecx_newctx(provctx, X25519_KEYLEN); |
68 | 0 | } |
69 | | |
70 | | static void *x448_newctx(void *provctx) |
71 | 0 | { |
72 | 0 | return ecx_newctx(provctx, X448_KEYLEN); |
73 | 0 | } |
74 | | |
75 | | static int ecx_init(void *vecxctx, void *vkey, const char *algname) |
76 | 0 | { |
77 | 0 | PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx; |
78 | 0 | ECX_KEY *key = vkey; |
79 | |
|
80 | 0 | if (!ossl_prov_is_running()) |
81 | 0 | return 0; |
82 | | |
83 | 0 | if (ecxctx == NULL |
84 | 0 | || key == NULL |
85 | 0 | || key->keylen != ecxctx->keylen |
86 | 0 | || !ossl_ecx_key_up_ref(key)) { |
87 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
88 | 0 | return 0; |
89 | 0 | } |
90 | | |
91 | 0 | ossl_ecx_key_free(ecxctx->key); |
92 | 0 | ecxctx->key = key; |
93 | |
|
94 | | #ifdef FIPS_MODULE |
95 | | if (!ossl_FIPS_IND_callback(key->libctx, algname, "Init")) |
96 | | return 0; |
97 | | #endif |
98 | 0 | return 1; |
99 | 0 | } |
100 | | |
101 | | static int x25519_init(void *vecxctx, void *vkey, |
102 | | ossl_unused const OSSL_PARAM params[]) |
103 | 0 | { |
104 | 0 | return ecx_init(vecxctx, vkey, "X25519"); |
105 | 0 | } |
106 | | |
107 | | static int x448_init(void *vecxctx, void *vkey, |
108 | | ossl_unused const OSSL_PARAM params[]) |
109 | 0 | { |
110 | 0 | return ecx_init(vecxctx, vkey, "X448"); |
111 | 0 | } |
112 | | |
113 | | static int ecx_set_peer(void *vecxctx, void *vkey) |
114 | 0 | { |
115 | 0 | PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx; |
116 | 0 | ECX_KEY *key = vkey; |
117 | |
|
118 | 0 | if (!ossl_prov_is_running()) |
119 | 0 | return 0; |
120 | | |
121 | 0 | if (ecxctx == NULL |
122 | 0 | || key == NULL |
123 | 0 | || key->keylen != ecxctx->keylen |
124 | 0 | || !ossl_ecx_key_up_ref(key)) { |
125 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
126 | 0 | return 0; |
127 | 0 | } |
128 | 0 | ossl_ecx_key_free(ecxctx->peerkey); |
129 | 0 | ecxctx->peerkey = key; |
130 | |
|
131 | 0 | return 1; |
132 | 0 | } |
133 | | |
134 | | static int ecx_derive(void *vecxctx, unsigned char *secret, size_t *secretlen, |
135 | | size_t outlen) |
136 | 0 | { |
137 | 0 | PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx; |
138 | |
|
139 | 0 | if (!ossl_prov_is_running()) |
140 | 0 | return 0; |
141 | 0 | return ossl_ecx_compute_key(ecxctx->peerkey, ecxctx->key, ecxctx->keylen, |
142 | 0 | secret, secretlen, outlen); |
143 | 0 | } |
144 | | |
145 | | static void ecx_freectx(void *vecxctx) |
146 | 0 | { |
147 | 0 | PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx; |
148 | |
|
149 | 0 | ossl_ecx_key_free(ecxctx->key); |
150 | 0 | ossl_ecx_key_free(ecxctx->peerkey); |
151 | |
|
152 | 0 | OPENSSL_free(ecxctx); |
153 | 0 | } |
154 | | |
155 | | static void *ecx_dupctx(void *vecxctx) |
156 | 0 | { |
157 | 0 | PROV_ECX_CTX *srcctx = (PROV_ECX_CTX *)vecxctx; |
158 | 0 | PROV_ECX_CTX *dstctx; |
159 | |
|
160 | 0 | if (!ossl_prov_is_running()) |
161 | 0 | return NULL; |
162 | | |
163 | 0 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
164 | 0 | if (dstctx == NULL) |
165 | 0 | return NULL; |
166 | | |
167 | 0 | *dstctx = *srcctx; |
168 | 0 | if (dstctx->key != NULL && !ossl_ecx_key_up_ref(dstctx->key)) { |
169 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
170 | 0 | OPENSSL_free(dstctx); |
171 | 0 | return NULL; |
172 | 0 | } |
173 | | |
174 | 0 | if (dstctx->peerkey != NULL && !ossl_ecx_key_up_ref(dstctx->peerkey)) { |
175 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
176 | 0 | ossl_ecx_key_free(dstctx->key); |
177 | 0 | OPENSSL_free(dstctx); |
178 | 0 | return NULL; |
179 | 0 | } |
180 | | |
181 | 0 | return dstctx; |
182 | 0 | } |
183 | | |
184 | | #ifdef FIPS_MODULE |
185 | | |
186 | | #endif |
187 | | |
188 | | static const OSSL_PARAM *ecx_gettable_ctx_params(ossl_unused void *vctx, |
189 | | ossl_unused void *provctx) |
190 | 0 | { |
191 | | #ifdef FIPS_MODULE |
192 | | return ecx_get_ctx_params_list; |
193 | | #else |
194 | 0 | static OSSL_PARAM params[] = { OSSL_PARAM_END }; |
195 | |
|
196 | 0 | return params; |
197 | 0 | #endif |
198 | 0 | } |
199 | | |
200 | | static int ecx_get_ctx_params(ossl_unused void *vctx, OSSL_PARAM params[]) |
201 | 0 | { |
202 | | #ifdef FIPS_MODULE |
203 | | int approved = 0; |
204 | | struct ecx_get_ctx_params_st p; |
205 | | |
206 | | if (vctx == NULL || !ecx_get_ctx_params_decoder(params, &p)) |
207 | | return 0; |
208 | | |
209 | | if (p.ind != NULL && !OSSL_PARAM_set_int(p.ind, approved)) |
210 | | return 0; |
211 | | #endif |
212 | 0 | return 1; |
213 | 0 | } |
214 | | |
215 | | const OSSL_DISPATCH ossl_x25519_keyexch_functions[] = { |
216 | | { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x25519_newctx }, |
217 | | { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))x25519_init }, |
218 | | { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive }, |
219 | | { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer }, |
220 | | { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx }, |
221 | | { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx }, |
222 | | { OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS, (void (*)(void))ecx_get_ctx_params }, |
223 | | { OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS, |
224 | | (void (*)(void))ecx_gettable_ctx_params }, |
225 | | OSSL_DISPATCH_END |
226 | | }; |
227 | | |
228 | | const OSSL_DISPATCH ossl_x448_keyexch_functions[] = { |
229 | | { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x448_newctx }, |
230 | | { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))x448_init }, |
231 | | { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive }, |
232 | | { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer }, |
233 | | { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx }, |
234 | | { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx }, |
235 | | { OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS, (void (*)(void))ecx_get_ctx_params }, |
236 | | { OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS, |
237 | | (void (*)(void))ecx_gettable_ctx_params }, |
238 | | OSSL_DISPATCH_END |
239 | | }; |