/src/openssl/crypto/evp/kdf_lib.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2018-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2018-2019, Oracle and/or its affiliates. All rights reserved. |
4 | | * |
5 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
6 | | * this file except in compliance with the License. You can obtain a copy |
7 | | * in the file LICENSE in the source distribution or at |
8 | | * https://www.openssl.org/source/license.html |
9 | | */ |
10 | | |
11 | | #include <stdio.h> |
12 | | #include <stdlib.h> |
13 | | #include "internal/cryptlib.h" |
14 | | #include <openssl/evp.h> |
15 | | #include <openssl/kdf.h> |
16 | | #include <openssl/core.h> |
17 | | #include <openssl/core_names.h> |
18 | | #include "crypto/evp.h" |
19 | | #include "internal/numbers.h" |
20 | | #include "internal/provider.h" |
21 | | #include "evp_local.h" |
22 | | #include "internal/param_build_set.h" |
23 | | |
24 | | EVP_KDF_CTX *EVP_KDF_CTX_new(EVP_KDF *kdf) |
25 | 1.10M | { |
26 | 1.10M | EVP_KDF_CTX *ctx = NULL; |
27 | | |
28 | 1.10M | if (kdf == NULL) |
29 | 0 | return NULL; |
30 | | |
31 | 1.10M | ctx = OPENSSL_zalloc(sizeof(EVP_KDF_CTX)); |
32 | 1.10M | if (ctx == NULL |
33 | 1.10M | || (ctx->algctx = kdf->newctx(ossl_provider_ctx(kdf->prov))) == NULL |
34 | 1.10M | || !EVP_KDF_up_ref(kdf)) { |
35 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
36 | 0 | if (ctx != NULL) |
37 | 0 | kdf->freectx(ctx->algctx); |
38 | 0 | OPENSSL_free(ctx); |
39 | 0 | ctx = NULL; |
40 | 1.10M | } else { |
41 | 1.10M | ctx->meth = kdf; |
42 | 1.10M | } |
43 | 1.10M | return ctx; |
44 | 1.10M | } |
45 | | |
46 | | void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx) |
47 | 1.10M | { |
48 | 1.10M | if (ctx == NULL) |
49 | 0 | return; |
50 | 1.10M | ctx->meth->freectx(ctx->algctx); |
51 | 1.10M | ctx->algctx = NULL; |
52 | 1.10M | EVP_KDF_free(ctx->meth); |
53 | 1.10M | OPENSSL_free(ctx); |
54 | 1.10M | } |
55 | | |
56 | | EVP_KDF_CTX *EVP_KDF_CTX_dup(const EVP_KDF_CTX *src) |
57 | 0 | { |
58 | 0 | EVP_KDF_CTX *dst; |
59 | |
|
60 | 0 | if (src == NULL || src->algctx == NULL || src->meth->dupctx == NULL) |
61 | 0 | return NULL; |
62 | | |
63 | 0 | dst = OPENSSL_malloc(sizeof(*dst)); |
64 | 0 | if (dst == NULL) |
65 | 0 | return NULL; |
66 | | |
67 | 0 | memcpy(dst, src, sizeof(*dst)); |
68 | 0 | if (!EVP_KDF_up_ref(dst->meth)) { |
69 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
70 | 0 | OPENSSL_free(dst); |
71 | 0 | return NULL; |
72 | 0 | } |
73 | | |
74 | 0 | dst->algctx = src->meth->dupctx(src->algctx); |
75 | 0 | if (dst->algctx == NULL) { |
76 | 0 | EVP_KDF_CTX_free(dst); |
77 | 0 | return NULL; |
78 | 0 | } |
79 | 0 | return dst; |
80 | 0 | } |
81 | | |
82 | | int evp_kdf_get_number(const EVP_KDF *kdf) |
83 | 0 | { |
84 | 0 | return kdf->name_id; |
85 | 0 | } |
86 | | |
87 | | const char *EVP_KDF_get0_name(const EVP_KDF *kdf) |
88 | 0 | { |
89 | 0 | return kdf->type_name; |
90 | 0 | } |
91 | | |
92 | | const char *EVP_KDF_get0_description(const EVP_KDF *kdf) |
93 | 0 | { |
94 | 0 | return kdf->description; |
95 | 0 | } |
96 | | |
97 | | int EVP_KDF_is_a(const EVP_KDF *kdf, const char *name) |
98 | 0 | { |
99 | 0 | return kdf != NULL && evp_is_a(kdf->prov, kdf->name_id, NULL, name); |
100 | 0 | } |
101 | | |
102 | | const OSSL_PROVIDER *EVP_KDF_get0_provider(const EVP_KDF *kdf) |
103 | 7.50k | { |
104 | 7.50k | return kdf->prov; |
105 | 7.50k | } |
106 | | |
107 | | const EVP_KDF *EVP_KDF_CTX_get0_kdf(const EVP_KDF_CTX *ctx) |
108 | 0 | { |
109 | 0 | return ctx->meth; |
110 | 0 | } |
111 | | |
112 | | #if !defined(OPENSSL_NO_DEPRECATED_4_1) |
113 | | const EVP_KDF *EVP_KDF_CTX_kdf(const EVP_KDF_CTX *ctx) |
114 | 0 | { |
115 | 0 | return EVP_KDF_CTX_get0_kdf(ctx); |
116 | 0 | } |
117 | | #endif /* !OPENSSL_NO_DEPRECATED_4_1 */ |
118 | | |
119 | | EVP_KDF *EVP_KDF_CTX_get1_kdf(const EVP_KDF_CTX *ctx) |
120 | 0 | { |
121 | 0 | if (!EVP_KDF_up_ref(ctx->meth)) |
122 | 0 | return NULL; |
123 | 0 | return ctx->meth; |
124 | 0 | } |
125 | | |
126 | | void EVP_KDF_CTX_reset(EVP_KDF_CTX *ctx) |
127 | 0 | { |
128 | 0 | if (ctx == NULL) |
129 | 0 | return; |
130 | | |
131 | 0 | if (ctx->meth->reset != NULL) |
132 | 0 | ctx->meth->reset(ctx->algctx); |
133 | 0 | } |
134 | | |
135 | | size_t EVP_KDF_CTX_get_kdf_size(EVP_KDF_CTX *ctx) |
136 | 126 | { |
137 | 126 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
138 | 126 | size_t s = 0; |
139 | | |
140 | 126 | if (ctx == NULL) |
141 | 0 | return 0; |
142 | | |
143 | 126 | *params = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_SIZE, &s); |
144 | 126 | if (ctx->meth->get_ctx_params != NULL |
145 | 126 | && ctx->meth->get_ctx_params(ctx->algctx, params)) |
146 | 126 | return s; |
147 | 0 | if (ctx->meth->get_params != NULL |
148 | 0 | && ctx->meth->get_params(params)) |
149 | 0 | return s; |
150 | 0 | return 0; |
151 | 0 | } |
152 | | |
153 | | int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen, |
154 | | const OSSL_PARAM params[]) |
155 | 1.09M | { |
156 | 1.09M | if (ctx == NULL) |
157 | 0 | return 0; |
158 | | |
159 | 1.09M | return ctx->meth->derive(ctx->algctx, key, keylen, params); |
160 | 1.09M | } |
161 | | |
162 | | #ifndef FIPS_MODULE |
163 | | struct convert_key { |
164 | | const char *name; |
165 | | OSSL_PARAM *param; |
166 | | }; |
167 | | |
168 | | static int convert_key_cb(const OSSL_PARAM params[], void *arg) |
169 | 0 | { |
170 | 0 | struct convert_key *ckey = arg; |
171 | 0 | const OSSL_PARAM *raw_bytes; |
172 | 0 | unsigned char *data; |
173 | 0 | size_t len; |
174 | |
|
175 | 0 | raw_bytes = OSSL_PARAM_locate_const(params, OSSL_SKEY_PARAM_RAW_BYTES); |
176 | 0 | if (raw_bytes == NULL) |
177 | 0 | return 0; |
178 | | |
179 | 0 | if (!OSSL_PARAM_get_octet_string_ptr(raw_bytes, (const void **)&data, &len)) |
180 | 0 | return 0; |
181 | | |
182 | 0 | *ckey->param = OSSL_PARAM_construct_octet_string(ckey->name, data, len); |
183 | 0 | return 1; |
184 | 0 | } |
185 | | |
186 | | int EVP_KDF_CTX_set_SKEY(EVP_KDF_CTX *ctx, EVP_SKEY *key, const char *paramname) |
187 | 0 | { |
188 | 0 | struct convert_key ckey; |
189 | 0 | OSSL_PARAM params[2] = { |
190 | 0 | OSSL_PARAM_END, |
191 | 0 | OSSL_PARAM_END, |
192 | 0 | }; |
193 | |
|
194 | 0 | if (ctx == NULL) |
195 | 0 | return 0; |
196 | | |
197 | 0 | ckey.name = (paramname != NULL) ? paramname : OSSL_KDF_PARAM_KEY; |
198 | |
|
199 | 0 | if (ctx->meth->set_skey != NULL && ctx->meth->prov == key->skeymgmt->prov) |
200 | 0 | return ctx->meth->set_skey(ctx->algctx, key->keydata, ckey.name); |
201 | | |
202 | | /* |
203 | | * We can't use the opaque key directly. |
204 | | * Let's try to export it and set the ctx params in a traditional manner. |
205 | | */ |
206 | 0 | ckey.param = ¶ms[0]; |
207 | |
|
208 | 0 | if (!ctx->meth->set_ctx_params) |
209 | 0 | return 0; |
210 | | |
211 | 0 | if (EVP_SKEY_export(key, OSSL_SKEYMGMT_SELECT_SECRET_KEY, |
212 | 0 | convert_key_cb, &ckey)) |
213 | 0 | return ctx->meth->set_ctx_params(ctx->algctx, params); |
214 | | |
215 | 0 | return 0; |
216 | 0 | } |
217 | | |
218 | | EVP_SKEY *EVP_KDF_derive_SKEY(EVP_KDF_CTX *ctx, EVP_SKEYMGMT *mgmt, |
219 | | const char *key_type, const char *propquery, |
220 | | size_t keylen, const OSSL_PARAM params[]) |
221 | 0 | { |
222 | 0 | EVP_SKEYMGMT *skeymgmt = NULL; |
223 | 0 | EVP_SKEY *ret = NULL; |
224 | |
|
225 | 0 | if (ctx == NULL || key_type == NULL) { |
226 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
227 | 0 | return NULL; |
228 | 0 | } |
229 | | |
230 | 0 | if (mgmt != NULL) { |
231 | 0 | skeymgmt = mgmt; |
232 | 0 | } else { |
233 | 0 | skeymgmt = evp_skeymgmt_fetch_from_prov(ctx->meth->prov, |
234 | 0 | key_type, propquery); |
235 | 0 | if (skeymgmt == NULL) { |
236 | | /* |
237 | | * The provider does not support skeymgmt, let's try to fallback |
238 | | * to a provider that supports it |
239 | | */ |
240 | 0 | skeymgmt = EVP_SKEYMGMT_fetch(ossl_provider_libctx(ctx->meth->prov), |
241 | 0 | key_type, propquery); |
242 | 0 | } |
243 | |
|
244 | 0 | if (skeymgmt == NULL) { |
245 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_FETCH_FAILED); |
246 | 0 | return NULL; |
247 | 0 | } |
248 | 0 | } |
249 | | |
250 | | /* Fallback to raw derive + import if necessary */ |
251 | 0 | if (skeymgmt->prov != ctx->meth->prov || ctx->meth->derive_skey == NULL) { |
252 | 0 | unsigned char *key = NULL; |
253 | 0 | OSSL_PARAM import_params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
254 | |
|
255 | 0 | if (ctx->meth->derive == NULL) { |
256 | 0 | ERR_raise(ERR_R_EVP_LIB, ERR_R_UNSUPPORTED); |
257 | 0 | return NULL; |
258 | 0 | } |
259 | | |
260 | 0 | key = OPENSSL_zalloc(keylen); |
261 | 0 | if (!key) |
262 | 0 | return NULL; |
263 | | |
264 | 0 | if (!ctx->meth->derive(ctx->algctx, key, keylen, params)) { |
265 | 0 | OPENSSL_free(key); |
266 | 0 | return NULL; |
267 | 0 | } |
268 | 0 | import_params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES, |
269 | 0 | key, keylen); |
270 | |
|
271 | 0 | ret = EVP_SKEY_import_SKEYMGMT(ossl_provider_libctx(ctx->meth->prov), skeymgmt, |
272 | 0 | OSSL_SKEYMGMT_SELECT_SECRET_KEY, import_params); |
273 | |
|
274 | 0 | if (mgmt != skeymgmt) |
275 | 0 | EVP_SKEYMGMT_free(skeymgmt); |
276 | |
|
277 | 0 | OPENSSL_clear_free(key, keylen); |
278 | 0 | return ret; |
279 | 0 | } |
280 | | |
281 | 0 | ret = evp_skey_alloc(skeymgmt); |
282 | 0 | if (ret == NULL) { |
283 | 0 | if (mgmt != skeymgmt) |
284 | 0 | EVP_SKEYMGMT_free(skeymgmt); |
285 | 0 | return NULL; |
286 | 0 | } |
287 | | |
288 | 0 | ret->keydata = ctx->meth->derive_skey(ctx->algctx, key_type, ossl_provider_ctx(skeymgmt->prov), |
289 | 0 | skeymgmt->import, keylen, params); |
290 | 0 | if (ret->keydata == NULL) { |
291 | 0 | EVP_SKEY_free(ret); |
292 | 0 | ret = NULL; |
293 | 0 | } |
294 | |
|
295 | 0 | if (mgmt != skeymgmt) |
296 | 0 | EVP_SKEYMGMT_free(skeymgmt); |
297 | 0 | return ret; |
298 | 0 | } |
299 | | #endif /* !FIPS_MODULE */ |
300 | | |
301 | | /* |
302 | | * The {get,set}_params functions return 1 if there is no corresponding |
303 | | * function in the implementation. This is the same as if there was one, |
304 | | * but it didn't recognise any of the given params, i.e. nothing in the |
305 | | * bag of parameters was useful. |
306 | | */ |
307 | | int EVP_KDF_get_params(EVP_KDF *kdf, OSSL_PARAM params[]) |
308 | 0 | { |
309 | 0 | if (kdf->get_params != NULL) |
310 | 0 | return kdf->get_params(params); |
311 | 0 | return 1; |
312 | 0 | } |
313 | | |
314 | | int EVP_KDF_CTX_get_params(EVP_KDF_CTX *ctx, OSSL_PARAM params[]) |
315 | 0 | { |
316 | 0 | if (ctx->meth->get_ctx_params != NULL) |
317 | 0 | return ctx->meth->get_ctx_params(ctx->algctx, params); |
318 | 0 | return 1; |
319 | 0 | } |
320 | | |
321 | | int EVP_KDF_CTX_set_params(EVP_KDF_CTX *ctx, const OSSL_PARAM params[]) |
322 | 7.54k | { |
323 | 7.54k | if (ctx->meth->set_ctx_params != NULL) |
324 | 7.54k | return ctx->meth->set_ctx_params(ctx->algctx, params); |
325 | 0 | return 1; |
326 | 7.54k | } |
327 | | |
328 | | int EVP_KDF_names_do_all(const EVP_KDF *kdf, |
329 | | void (*fn)(const char *name, void *data), |
330 | | void *data) |
331 | 0 | { |
332 | 0 | if (kdf->prov != NULL) |
333 | 0 | return evp_names_do_all(kdf->prov, kdf->name_id, fn, data); |
334 | | |
335 | 0 | return 1; |
336 | 0 | } |