/src/openssl/providers/implementations/kdfs/sskdf.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 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 | | /* |
12 | | * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final |
13 | | * Section 4.1. |
14 | | * |
15 | | * The Single Step KDF algorithm is given by: |
16 | | * |
17 | | * Result(0) = empty bit string (i.e., the null string). |
18 | | * For i = 1 to reps, do the following: |
19 | | * Increment counter by 1. |
20 | | * Result(i) = Result(i - 1) || H(counter || Z || FixedInfo). |
21 | | * DKM = LeftmostBits(Result(reps), L)) |
22 | | * |
23 | | * NOTES: |
24 | | * Z is a shared secret required to produce the derived key material. |
25 | | * counter is a 4 byte buffer. |
26 | | * FixedInfo is a bit string containing context specific data. |
27 | | * DKM is the output derived key material. |
28 | | * L is the required size of the DKM. |
29 | | * reps = [L / H_outputBits] |
30 | | * H(x) is the auxiliary function that can be either a hash, HMAC or KMAC. |
31 | | * H_outputBits is the length of the output of the auxiliary function H(x). |
32 | | * |
33 | | * Currently there is not a comprehensive list of test vectors for this |
34 | | * algorithm, especially for H(x) = HMAC and H(x) = KMAC. |
35 | | * Test vectors for H(x) = Hash are indirectly used by CAVS KAS tests. |
36 | | */ |
37 | | #include <stdlib.h> |
38 | | #include <stdarg.h> |
39 | | #include <string.h> |
40 | | #include <openssl/hmac.h> |
41 | | #include <openssl/evp.h> |
42 | | #include <openssl/kdf.h> |
43 | | #include <openssl/core_names.h> |
44 | | #include <openssl/params.h> |
45 | | #include <openssl/proverr.h> |
46 | | #include "internal/cryptlib.h" |
47 | | #include "internal/fips.h" |
48 | | #include "internal/numbers.h" |
49 | | #include "crypto/evp.h" |
50 | | #include "prov/provider_ctx.h" |
51 | | #include "prov/providercommon.h" |
52 | | #include "prov/implementations.h" |
53 | | #include "prov/provider_util.h" |
54 | | #include "prov/securitycheck.h" |
55 | | #include "internal/params.h" |
56 | | |
57 | 0 | #define SSKDF_MAX_INLEN (1 << 30) |
58 | 0 | #define SSKDF_MAX_INFOS 5 |
59 | | |
60 | | typedef struct { |
61 | | void *provctx; |
62 | | EVP_MAC_CTX *macctx; /* H(x) = HMAC_hash OR H(x) = KMAC */ |
63 | | PROV_DIGEST digest; /* H(x) = hash(x) */ |
64 | | unsigned char *secret; |
65 | | size_t secret_len; |
66 | | unsigned char *info; |
67 | | size_t info_len; |
68 | | unsigned char *salt; |
69 | | size_t salt_len; |
70 | | size_t out_len; /* optional KMAC parameter */ |
71 | | int is_kmac; |
72 | | OSSL_FIPS_IND_DECLARE |
73 | | } KDF_SSKDF; |
74 | | |
75 | | struct sskdf_all_set_ctx_params_st { |
76 | | OSSL_PARAM *secret; |
77 | | OSSL_PARAM *propq; |
78 | | OSSL_PARAM *digest; |
79 | | OSSL_PARAM *mac; |
80 | | OSSL_PARAM *salt; |
81 | | OSSL_PARAM *size; |
82 | | #ifdef FIPS_MODULE |
83 | | OSSL_PARAM *ind_k; |
84 | | OSSL_PARAM *ind_d; |
85 | | #endif |
86 | | OSSL_PARAM *info[SSKDF_MAX_INFOS]; |
87 | | int num_info; |
88 | | }; |
89 | | |
90 | | static OSSL_FUNC_kdf_newctx_fn sskdf_common_new; |
91 | | #ifndef OPENSSL_NO_SSKDF |
92 | | static OSSL_FUNC_kdf_newctx_fn sskdf_new; |
93 | | #endif |
94 | | #ifndef OPENSSL_NO_X963KDF |
95 | | static OSSL_FUNC_kdf_newctx_fn x963_new; |
96 | | #endif |
97 | | static OSSL_FUNC_kdf_dupctx_fn sskdf_dup; |
98 | | static OSSL_FUNC_kdf_freectx_fn sskdf_free; |
99 | | static OSSL_FUNC_kdf_reset_fn sskdf_reset; |
100 | | |
101 | | #ifndef OPENSSL_NO_SSKDF |
102 | 0 | #define SSKDF_KMAC128_DEFAULT_SALT_SIZE (168 - 4) |
103 | 0 | #define SSKDF_KMAC256_DEFAULT_SALT_SIZE (136 - 4) |
104 | | /* KMAC uses a Customisation string of 'KDF' */ |
105 | | static const unsigned char kmac_custom_str[] = { 0x4B, 0x44, 0x46 }; |
106 | | |
107 | | static OSSL_FUNC_kdf_derive_fn sskdf_derive; |
108 | | static OSSL_FUNC_kdf_settable_ctx_params_fn sskdf_settable_ctx_params; |
109 | | static OSSL_FUNC_kdf_set_ctx_params_fn sskdf_set_ctx_params; |
110 | | static OSSL_FUNC_kdf_gettable_ctx_params_fn sskdf_gettable_ctx_params; |
111 | | static OSSL_FUNC_kdf_get_ctx_params_fn sskdf_get_ctx_params; |
112 | | #define sskdf_set_ctx_params_st sskdf_all_set_ctx_params_st |
113 | | #include "providers/implementations/kdfs/sskdf.inc" |
114 | | #endif |
115 | | #ifndef OPENSSL_NO_X963KDF |
116 | | static OSSL_FUNC_kdf_derive_fn x963kdf_derive; |
117 | | static OSSL_FUNC_kdf_settable_ctx_params_fn x963kdf_settable_ctx_params; |
118 | | static OSSL_FUNC_kdf_set_ctx_params_fn x963kdf_set_ctx_params; |
119 | | static OSSL_FUNC_kdf_gettable_ctx_params_fn x963kdf_gettable_ctx_params; |
120 | | static OSSL_FUNC_kdf_get_ctx_params_fn x963kdf_get_ctx_params; |
121 | | #define x963kdf_set_ctx_params_st sskdf_all_set_ctx_params_st |
122 | | #include "providers/implementations/kdfs/x963kdf.inc" |
123 | | #endif |
124 | | /* |
125 | | * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final |
126 | | * Section 4. One-Step Key Derivation using H(x) = hash(x) |
127 | | * Note: X9.63 also uses this code with the only difference being that the |
128 | | * counter is appended to the secret 'z'. |
129 | | * i.e. |
130 | | * result[i] = Hash(counter || z || info) for One Step OR |
131 | | * result[i] = Hash(z || counter || info) for X9.63. |
132 | | */ |
133 | | static int SSKDF_hash_kdm(const EVP_MD *kdf_md, |
134 | | const unsigned char *z, size_t z_len, |
135 | | const unsigned char *info, size_t info_len, |
136 | | unsigned int append_ctr, |
137 | | unsigned char *derived_key, size_t derived_key_len) |
138 | 0 | { |
139 | 0 | int ret = 0, hlen; |
140 | 0 | size_t counter, out_len, len = derived_key_len; |
141 | 0 | unsigned char c[4]; |
142 | 0 | unsigned char mac[EVP_MAX_MD_SIZE]; |
143 | 0 | unsigned char *out = derived_key; |
144 | 0 | EVP_MD_CTX *ctx = NULL, *ctx_init = NULL; |
145 | |
|
146 | 0 | if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN |
147 | 0 | || derived_key_len > SSKDF_MAX_INLEN |
148 | 0 | || derived_key_len == 0) |
149 | 0 | return 0; |
150 | | |
151 | 0 | hlen = EVP_MD_get_size(kdf_md); |
152 | 0 | if (hlen <= 0) |
153 | 0 | return 0; |
154 | 0 | out_len = (size_t)hlen; |
155 | |
|
156 | 0 | ctx = EVP_MD_CTX_create(); |
157 | 0 | ctx_init = EVP_MD_CTX_create(); |
158 | 0 | if (ctx == NULL || ctx_init == NULL) |
159 | 0 | goto end; |
160 | | |
161 | 0 | if (!EVP_DigestInit(ctx_init, kdf_md)) |
162 | 0 | goto end; |
163 | | |
164 | 0 | for (counter = 1;; counter++) { |
165 | 0 | c[0] = (unsigned char)((counter >> 24) & 0xff); |
166 | 0 | c[1] = (unsigned char)((counter >> 16) & 0xff); |
167 | 0 | c[2] = (unsigned char)((counter >> 8) & 0xff); |
168 | 0 | c[3] = (unsigned char)(counter & 0xff); |
169 | |
|
170 | 0 | if (!(EVP_MD_CTX_copy_ex(ctx, ctx_init) |
171 | 0 | && (append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c))) |
172 | 0 | && EVP_DigestUpdate(ctx, z, z_len) |
173 | 0 | && (!append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c))) |
174 | 0 | && EVP_DigestUpdate(ctx, info, info_len))) |
175 | 0 | goto end; |
176 | 0 | if (len >= out_len) { |
177 | 0 | if (!EVP_DigestFinal_ex(ctx, out, NULL)) |
178 | 0 | goto end; |
179 | 0 | out += out_len; |
180 | 0 | len -= out_len; |
181 | 0 | if (len == 0) |
182 | 0 | break; |
183 | 0 | } else { |
184 | 0 | if (!EVP_DigestFinal_ex(ctx, mac, NULL)) |
185 | 0 | goto end; |
186 | 0 | memcpy(out, mac, len); |
187 | 0 | break; |
188 | 0 | } |
189 | 0 | } |
190 | 0 | ret = 1; |
191 | 0 | end: |
192 | 0 | EVP_MD_CTX_destroy(ctx); |
193 | 0 | EVP_MD_CTX_destroy(ctx_init); |
194 | 0 | OPENSSL_cleanse(mac, sizeof(mac)); |
195 | 0 | return ret; |
196 | 0 | } |
197 | | |
198 | | #ifndef OPENSSL_NO_SSKDF |
199 | | static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom, |
200 | | size_t custom_len, size_t kmac_out_len, |
201 | | size_t derived_key_len, unsigned char **out) |
202 | 0 | { |
203 | 0 | OSSL_PARAM params[2]; |
204 | | |
205 | | /* Only KMAC has custom data - so return if not KMAC */ |
206 | 0 | if (custom == NULL) |
207 | 0 | return 1; |
208 | | |
209 | 0 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM, |
210 | 0 | (void *)custom, custom_len); |
211 | 0 | params[1] = OSSL_PARAM_construct_end(); |
212 | |
|
213 | 0 | if (!EVP_MAC_CTX_set_params(ctx, params)) |
214 | 0 | return 0; |
215 | | |
216 | | /* By default only do one iteration if kmac_out_len is not specified */ |
217 | 0 | if (kmac_out_len == 0) |
218 | 0 | kmac_out_len = derived_key_len; |
219 | | /* otherwise check the size is valid */ |
220 | 0 | else if (!(kmac_out_len == derived_key_len |
221 | 0 | || kmac_out_len == 20 |
222 | 0 | || kmac_out_len == 28 |
223 | 0 | || kmac_out_len == 32 |
224 | 0 | || kmac_out_len == 48 |
225 | 0 | || kmac_out_len == 64)) |
226 | 0 | return 0; |
227 | | |
228 | 0 | params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, |
229 | 0 | &kmac_out_len); |
230 | |
|
231 | 0 | if (EVP_MAC_CTX_set_params(ctx, params) <= 0) |
232 | 0 | return 0; |
233 | | |
234 | | /* |
235 | | * For kmac the output buffer can be larger than EVP_MAX_MD_SIZE: so |
236 | | * alloc a buffer for this case. |
237 | | */ |
238 | 0 | if (kmac_out_len > EVP_MAX_MD_SIZE) { |
239 | 0 | *out = OPENSSL_zalloc(kmac_out_len); |
240 | 0 | if (*out == NULL) |
241 | 0 | return 0; |
242 | 0 | } |
243 | 0 | return 1; |
244 | 0 | } |
245 | | |
246 | | /* |
247 | | * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final |
248 | | * Section 4. One-Step Key Derivation using MAC: i.e either |
249 | | * H(x) = HMAC-hash(salt, x) OR |
250 | | * H(x) = KMAC#(salt, x, outbits, CustomString='KDF') |
251 | | */ |
252 | | static int SSKDF_mac_kdm(EVP_MAC_CTX *ctx_init, |
253 | | const unsigned char *kmac_custom, |
254 | | size_t kmac_custom_len, size_t kmac_out_len, |
255 | | const unsigned char *salt, size_t salt_len, |
256 | | const unsigned char *z, size_t z_len, |
257 | | const unsigned char *info, size_t info_len, |
258 | | unsigned char *derived_key, size_t derived_key_len) |
259 | 0 | { |
260 | 0 | int ret = 0; |
261 | 0 | size_t counter, out_len, len; |
262 | 0 | unsigned char c[4]; |
263 | 0 | unsigned char mac_buf[EVP_MAX_MD_SIZE]; |
264 | 0 | unsigned char *out = derived_key; |
265 | 0 | EVP_MAC_CTX *ctx = NULL; |
266 | 0 | unsigned char *mac = mac_buf, *kmac_buffer = NULL; |
267 | |
|
268 | 0 | if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN |
269 | 0 | || derived_key_len > SSKDF_MAX_INLEN |
270 | 0 | || derived_key_len == 0) |
271 | 0 | return 0; |
272 | | |
273 | 0 | if (!kmac_init(ctx_init, kmac_custom, kmac_custom_len, kmac_out_len, |
274 | 0 | derived_key_len, &kmac_buffer)) |
275 | 0 | goto end; |
276 | 0 | if (kmac_buffer != NULL) |
277 | 0 | mac = kmac_buffer; |
278 | |
|
279 | 0 | if (!EVP_MAC_init(ctx_init, salt, salt_len, NULL)) |
280 | 0 | goto end; |
281 | | |
282 | 0 | out_len = EVP_MAC_CTX_get_mac_size(ctx_init); /* output size */ |
283 | 0 | if (out_len <= 0 || (mac == mac_buf && out_len > sizeof(mac_buf))) |
284 | 0 | goto end; |
285 | 0 | len = derived_key_len; |
286 | |
|
287 | 0 | for (counter = 1;; counter++) { |
288 | 0 | c[0] = (unsigned char)((counter >> 24) & 0xff); |
289 | 0 | c[1] = (unsigned char)((counter >> 16) & 0xff); |
290 | 0 | c[2] = (unsigned char)((counter >> 8) & 0xff); |
291 | 0 | c[3] = (unsigned char)(counter & 0xff); |
292 | |
|
293 | 0 | ctx = EVP_MAC_CTX_dup(ctx_init); |
294 | 0 | if (!(ctx != NULL |
295 | 0 | && EVP_MAC_update(ctx, c, sizeof(c)) |
296 | 0 | && EVP_MAC_update(ctx, z, z_len) |
297 | 0 | && EVP_MAC_update(ctx, info, info_len))) |
298 | 0 | goto end; |
299 | 0 | if (len >= out_len) { |
300 | 0 | if (!EVP_MAC_final(ctx, out, NULL, len)) |
301 | 0 | goto end; |
302 | 0 | out += out_len; |
303 | 0 | len -= out_len; |
304 | 0 | if (len == 0) |
305 | 0 | break; |
306 | 0 | } else { |
307 | 0 | if (!EVP_MAC_final(ctx, mac, NULL, out_len)) |
308 | 0 | goto end; |
309 | 0 | memcpy(out, mac, len); |
310 | 0 | break; |
311 | 0 | } |
312 | 0 | EVP_MAC_CTX_free(ctx); |
313 | 0 | ctx = NULL; |
314 | 0 | } |
315 | 0 | ret = 1; |
316 | 0 | end: |
317 | 0 | if (kmac_buffer != NULL) |
318 | 0 | OPENSSL_clear_free(kmac_buffer, kmac_out_len); |
319 | 0 | else |
320 | 0 | OPENSSL_cleanse(mac_buf, sizeof(mac_buf)); |
321 | |
|
322 | 0 | EVP_MAC_CTX_free(ctx); |
323 | 0 | return ret; |
324 | 0 | } |
325 | | #endif /* OPENSSL_NO_SSKDF */ |
326 | | |
327 | | static void *sskdf_common_new(void *provctx) |
328 | 0 | { |
329 | 0 | KDF_SSKDF *ctx; |
330 | |
|
331 | 0 | if (!ossl_prov_is_running()) |
332 | 0 | return NULL; |
333 | | |
334 | 0 | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL) { |
335 | 0 | ctx->provctx = provctx; |
336 | 0 | OSSL_FIPS_IND_INIT(ctx) |
337 | 0 | } |
338 | 0 | return ctx; |
339 | 0 | } |
340 | | |
341 | | #ifndef OPENSSL_NO_SSKDF |
342 | | static void *sskdf_new(void *provctx) |
343 | 0 | { |
344 | | #ifdef FIPS_MODULE |
345 | | if (!ossl_deferred_self_test(PROV_LIBCTX_OF(provctx), |
346 | | ST_ID_KDF_SSKDF)) |
347 | | return NULL; |
348 | | #endif |
349 | |
|
350 | 0 | return sskdf_common_new(provctx); |
351 | 0 | } |
352 | | #endif |
353 | | |
354 | | #ifndef OPENSSL_NO_X963KDF |
355 | | static void *x963_new(void *provctx) |
356 | 0 | { |
357 | | #ifdef FIPS_MODULE |
358 | | if (!ossl_deferred_self_test(PROV_LIBCTX_OF(provctx), |
359 | | ST_ID_KDF_X963KDF)) |
360 | | return NULL; |
361 | | #endif |
362 | |
|
363 | 0 | return sskdf_common_new(provctx); |
364 | 0 | } |
365 | | #endif |
366 | | |
367 | | static void sskdf_reset(void *vctx) |
368 | 0 | { |
369 | 0 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
370 | 0 | void *provctx = ctx->provctx; |
371 | |
|
372 | 0 | EVP_MAC_CTX_free(ctx->macctx); |
373 | 0 | ossl_prov_digest_reset(&ctx->digest); |
374 | 0 | OPENSSL_clear_free(ctx->secret, ctx->secret_len); |
375 | 0 | OPENSSL_clear_free(ctx->info, ctx->info_len); |
376 | 0 | OPENSSL_clear_free(ctx->salt, ctx->salt_len); |
377 | 0 | memset(ctx, 0, sizeof(*ctx)); |
378 | 0 | ctx->provctx = provctx; |
379 | 0 | } |
380 | | |
381 | | static void sskdf_free(void *vctx) |
382 | 0 | { |
383 | 0 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
384 | |
|
385 | 0 | if (ctx != NULL) { |
386 | 0 | sskdf_reset(ctx); |
387 | 0 | OPENSSL_free(ctx); |
388 | 0 | } |
389 | 0 | } |
390 | | |
391 | | static void *sskdf_dup(void *vctx) |
392 | 0 | { |
393 | 0 | const KDF_SSKDF *src = (const KDF_SSKDF *)vctx; |
394 | 0 | KDF_SSKDF *dest; |
395 | |
|
396 | 0 | dest = sskdf_common_new(src->provctx); |
397 | 0 | if (dest != NULL) { |
398 | 0 | if (src->macctx != NULL) { |
399 | 0 | dest->macctx = EVP_MAC_CTX_dup(src->macctx); |
400 | 0 | if (dest->macctx == NULL) |
401 | 0 | goto err; |
402 | 0 | } |
403 | 0 | if (!ossl_prov_memdup(src->info, src->info_len, |
404 | 0 | &dest->info, &dest->info_len) |
405 | 0 | || !ossl_prov_memdup(src->salt, src->salt_len, |
406 | 0 | &dest->salt, &dest->salt_len) |
407 | 0 | || !ossl_prov_memdup(src->secret, src->secret_len, |
408 | 0 | &dest->secret, &dest->secret_len) |
409 | 0 | || !ossl_prov_digest_copy(&dest->digest, &src->digest)) |
410 | 0 | goto err; |
411 | 0 | dest->out_len = src->out_len; |
412 | 0 | dest->is_kmac = src->is_kmac; |
413 | 0 | OSSL_FIPS_IND_COPY(dest, src) |
414 | 0 | } |
415 | 0 | return dest; |
416 | | |
417 | 0 | err: |
418 | 0 | sskdf_free(dest); |
419 | 0 | return NULL; |
420 | 0 | } |
421 | | |
422 | | static size_t sskdf_size(KDF_SSKDF *ctx) |
423 | 0 | { |
424 | 0 | int len; |
425 | 0 | const EVP_MD *md = NULL; |
426 | |
|
427 | 0 | if (ctx->is_kmac) |
428 | 0 | return SIZE_MAX; |
429 | | |
430 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
431 | 0 | if (md == NULL) { |
432 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
433 | 0 | return 0; |
434 | 0 | } |
435 | 0 | len = EVP_MD_get_size(md); |
436 | 0 | return (len <= 0) ? 0 : (size_t)len; |
437 | 0 | } |
438 | | |
439 | | #ifndef OPENSSL_NO_SSKDF |
440 | | #ifdef FIPS_MODULE |
441 | | static int fips_sskdf_key_check_passed(KDF_SSKDF *ctx) |
442 | | { |
443 | | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
444 | | int key_approved = ossl_kdf_check_key_size(ctx->secret_len); |
445 | | |
446 | | if (!key_approved) { |
447 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, |
448 | | libctx, "SSKDF", "Key size", |
449 | | ossl_fips_config_sskdf_key_check)) { |
450 | | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
451 | | return 0; |
452 | | } |
453 | | } |
454 | | return 1; |
455 | | } |
456 | | #endif /* FIPS_MODULE */ |
457 | | |
458 | | static int sskdf_derive(void *vctx, unsigned char *key, size_t keylen, |
459 | | const OSSL_PARAM params[]) |
460 | 0 | { |
461 | 0 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
462 | 0 | const EVP_MD *md; |
463 | |
|
464 | 0 | if (!ossl_prov_is_running() || !sskdf_set_ctx_params(ctx, params)) |
465 | 0 | return 0; |
466 | 0 | if (ctx->secret == NULL) { |
467 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET); |
468 | 0 | return 0; |
469 | 0 | } |
470 | | |
471 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
472 | |
|
473 | 0 | if (ctx->macctx != NULL) { |
474 | | /* H(x) = KMAC or H(x) = HMAC */ |
475 | 0 | int ret; |
476 | 0 | const unsigned char *custom = NULL; |
477 | 0 | size_t custom_len = 0; |
478 | 0 | int default_salt_len; |
479 | 0 | EVP_MAC *mac = EVP_MAC_CTX_get0_mac(ctx->macctx); |
480 | |
|
481 | 0 | if (EVP_MAC_is_a(mac, OSSL_MAC_NAME_HMAC)) { |
482 | | /* H(x) = HMAC(x, salt, hash) */ |
483 | 0 | if (md == NULL) { |
484 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
485 | 0 | return 0; |
486 | 0 | } |
487 | 0 | default_salt_len = EVP_MD_get_size(md); |
488 | 0 | if (default_salt_len <= 0) |
489 | 0 | return 0; |
490 | 0 | } else if (ctx->is_kmac) { |
491 | | /* H(x) = KMACzzz(x, salt, custom) */ |
492 | 0 | custom = kmac_custom_str; |
493 | 0 | custom_len = sizeof(kmac_custom_str); |
494 | 0 | if (EVP_MAC_is_a(mac, OSSL_MAC_NAME_KMAC128)) |
495 | 0 | default_salt_len = SSKDF_KMAC128_DEFAULT_SALT_SIZE; |
496 | 0 | else |
497 | 0 | default_salt_len = SSKDF_KMAC256_DEFAULT_SALT_SIZE; |
498 | 0 | } else { |
499 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_MAC_TYPE); |
500 | 0 | return 0; |
501 | 0 | } |
502 | | /* If no salt is set then use a default_salt of zeros */ |
503 | 0 | if (ctx->salt == NULL || ctx->salt_len <= 0) { |
504 | 0 | ctx->salt = OPENSSL_zalloc(default_salt_len); |
505 | 0 | if (ctx->salt == NULL) |
506 | 0 | return 0; |
507 | 0 | ctx->salt_len = default_salt_len; |
508 | 0 | } |
509 | 0 | ret = SSKDF_mac_kdm(ctx->macctx, |
510 | 0 | custom, custom_len, ctx->out_len, |
511 | 0 | ctx->salt, ctx->salt_len, |
512 | 0 | ctx->secret, ctx->secret_len, |
513 | 0 | ctx->info, ctx->info_len, key, keylen); |
514 | 0 | return ret; |
515 | 0 | } else { |
516 | | /* H(x) = hash */ |
517 | 0 | if (md == NULL) { |
518 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
519 | 0 | return 0; |
520 | 0 | } |
521 | 0 | return SSKDF_hash_kdm(md, ctx->secret, ctx->secret_len, |
522 | 0 | ctx->info, ctx->info_len, 0, key, keylen); |
523 | 0 | } |
524 | 0 | } |
525 | | #endif |
526 | | |
527 | | #ifndef OPENSSL_NO_X963KDF |
528 | | #ifdef FIPS_MODULE |
529 | | static int fips_x963kdf_digest_check_passed(KDF_SSKDF *ctx, const EVP_MD *md) |
530 | | { |
531 | | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
532 | | /* |
533 | | * Perform digest check |
534 | | * |
535 | | * X963KDF is a KDF defined in ANSI-X9.63. According to ACVP specification |
536 | | * section 7.3.1, only SHA-2 and SHA-3 can be regarded as valid hash |
537 | | * functions. |
538 | | */ |
539 | | int digest_unapproved = (ctx->is_kmac != 1) && EVP_MD_is_a(md, SN_sha1); |
540 | | |
541 | | if (digest_unapproved) { |
542 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, |
543 | | libctx, "X963KDF", "Digest", |
544 | | ossl_fips_config_x963kdf_digest_check)) { |
545 | | ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED); |
546 | | return 0; |
547 | | } |
548 | | } |
549 | | return 1; |
550 | | } |
551 | | |
552 | | static int fips_x963kdf_key_check_passed(KDF_SSKDF *ctx) |
553 | | { |
554 | | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
555 | | int key_approved = ossl_kdf_check_key_size(ctx->secret_len); |
556 | | |
557 | | if (!key_approved) { |
558 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE1, |
559 | | libctx, "X963KDF", "Key size", |
560 | | ossl_fips_config_x963kdf_key_check)) { |
561 | | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
562 | | return 0; |
563 | | } |
564 | | } |
565 | | return 1; |
566 | | } |
567 | | #endif /* FIPS_MODULE */ |
568 | | |
569 | | static int x963kdf_derive(void *vctx, unsigned char *key, size_t keylen, |
570 | | const OSSL_PARAM params[]) |
571 | 0 | { |
572 | 0 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
573 | 0 | const EVP_MD *md; |
574 | |
|
575 | 0 | if (!ossl_prov_is_running() || !x963kdf_set_ctx_params(ctx, params)) |
576 | 0 | return 0; |
577 | | |
578 | 0 | if (ctx->secret == NULL) { |
579 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET); |
580 | 0 | return 0; |
581 | 0 | } |
582 | | |
583 | 0 | if (ctx->macctx != NULL) { |
584 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED); |
585 | 0 | return 0; |
586 | 0 | } |
587 | | |
588 | | /* H(x) = hash */ |
589 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
590 | 0 | if (md == NULL) { |
591 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
592 | 0 | return 0; |
593 | 0 | } |
594 | | |
595 | 0 | return SSKDF_hash_kdm(md, ctx->secret, ctx->secret_len, |
596 | 0 | ctx->info, ctx->info_len, 1, key, keylen); |
597 | 0 | } |
598 | | #endif /* OPENSSL_NO_X963KDF */ |
599 | | |
600 | | static int sskdf_common_set_ctx_params(KDF_SSKDF *ctx, struct sskdf_all_set_ctx_params_st *p, |
601 | | const OSSL_PARAM *params, OSSL_LIB_CTX *libctx) |
602 | 0 | { |
603 | |
|
604 | 0 | const EVP_MD *md = NULL; |
605 | 0 | size_t sz; |
606 | 0 | int r; |
607 | |
|
608 | 0 | if (p->digest != NULL) { |
609 | 0 | if (!ossl_prov_digest_load(&ctx->digest, p->digest, p->propq, libctx)) |
610 | 0 | return 0; |
611 | | |
612 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
613 | 0 | if (EVP_MD_xof(md)) { |
614 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
615 | 0 | return 0; |
616 | 0 | } |
617 | 0 | } |
618 | | |
619 | 0 | r = ossl_param_get1_octet_string_from_param(p->secret, &ctx->secret, |
620 | 0 | &ctx->secret_len); |
621 | 0 | if (r == 0) |
622 | 0 | return 0; |
623 | | |
624 | 0 | if (ossl_param_get1_concat_octet_string(p->num_info, p->info, &ctx->info, |
625 | 0 | &ctx->info_len) |
626 | 0 | == 0) |
627 | 0 | return 0; |
628 | | |
629 | 0 | if (p->size != NULL) { |
630 | 0 | if (!OSSL_PARAM_get_size_t(p->size, &sz) || sz == 0) |
631 | 0 | return 0; |
632 | 0 | ctx->out_len = sz; |
633 | 0 | } |
634 | 0 | return 1; |
635 | 0 | } |
636 | | |
637 | | #ifndef OPENSSL_NO_SSKDF |
638 | | static int sskdf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
639 | 0 | { |
640 | 0 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
641 | 0 | OSSL_LIB_CTX *libctx; |
642 | 0 | struct sskdf_all_set_ctx_params_st p; |
643 | |
|
644 | 0 | if (ctx == NULL || !sskdf_set_ctx_params_decoder(params, &p)) |
645 | 0 | return 0; |
646 | | |
647 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k)) |
648 | 0 | return 0; |
649 | | |
650 | 0 | libctx = PROV_LIBCTX_OF(ctx->provctx); |
651 | 0 | if (!ossl_prov_macctx_load(&ctx->macctx, |
652 | 0 | p.mac, NULL, p.digest, p.propq, |
653 | 0 | NULL, NULL, NULL, libctx)) |
654 | 0 | return 0; |
655 | 0 | if (ctx->macctx != NULL) { |
656 | 0 | if (EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->macctx), |
657 | 0 | OSSL_MAC_NAME_KMAC128) |
658 | 0 | || EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->macctx), |
659 | 0 | OSSL_MAC_NAME_KMAC256)) { |
660 | 0 | ctx->is_kmac = 1; |
661 | 0 | } |
662 | 0 | } |
663 | 0 | if (ossl_param_get1_octet_string_from_param(p.salt, &ctx->salt, |
664 | 0 | &ctx->salt_len) |
665 | 0 | == 0) |
666 | 0 | return 0; |
667 | 0 | if (!sskdf_common_set_ctx_params(ctx, &p, params, libctx)) |
668 | 0 | return 0; |
669 | | |
670 | | #ifdef FIPS_MODULE |
671 | | if (p.secret != NULL) |
672 | | if (!fips_sskdf_key_check_passed(ctx)) |
673 | | return 0; |
674 | | #endif |
675 | | |
676 | 0 | return 1; |
677 | 0 | } |
678 | | |
679 | | static const OSSL_PARAM *sskdf_settable_ctx_params(ossl_unused void *ctx, |
680 | | ossl_unused void *provctx) |
681 | 0 | { |
682 | 0 | return sskdf_set_ctx_params_list; |
683 | 0 | } |
684 | | |
685 | | static int sskdf_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
686 | 0 | { |
687 | 0 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
688 | 0 | struct sskdf_get_ctx_params_st p; |
689 | |
|
690 | 0 | if (ctx == NULL || !sskdf_get_ctx_params_decoder(params, &p)) |
691 | 0 | return 0; |
692 | | |
693 | 0 | if (p.size != NULL) { |
694 | 0 | if (!OSSL_PARAM_set_size_t(p.size, sskdf_size(ctx))) |
695 | 0 | return 0; |
696 | 0 | } |
697 | | |
698 | 0 | if (!OSSL_FIPS_IND_GET_CTX_PARAM(ctx, p.ind)) |
699 | 0 | return 0; |
700 | | |
701 | 0 | return 1; |
702 | 0 | } |
703 | | |
704 | | static const OSSL_PARAM *sskdf_gettable_ctx_params(ossl_unused void *ctx, ossl_unused void *provctx) |
705 | 0 | { |
706 | 0 | return sskdf_get_ctx_params_list; |
707 | 0 | } |
708 | | |
709 | | #endif /* OPENSSL_NO_SSKDF */ |
710 | | |
711 | | #ifndef OPENSSL_NO_X963KDF |
712 | | static int x963kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
713 | 0 | { |
714 | 0 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
715 | 0 | struct sskdf_all_set_ctx_params_st p; |
716 | |
|
717 | 0 | if (ctx == NULL || !x963kdf_set_ctx_params_decoder(params, &p)) |
718 | 0 | return 0; |
719 | | |
720 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_d)) |
721 | 0 | return 0; |
722 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE1, p.ind_k)) |
723 | 0 | return 0; |
724 | | |
725 | 0 | if (!sskdf_common_set_ctx_params(ctx, &p, params, PROV_LIBCTX_OF(ctx->provctx))) |
726 | 0 | return 0; |
727 | | |
728 | | #ifdef FIPS_MODULE |
729 | | if (p.digest != NULL) { |
730 | | const EVP_MD *md = ossl_prov_digest_md(&ctx->digest); |
731 | | |
732 | | if (!fips_x963kdf_digest_check_passed(ctx, md)) |
733 | | return 0; |
734 | | } |
735 | | |
736 | | if (p.secret != NULL) |
737 | | if (!fips_x963kdf_key_check_passed(ctx)) |
738 | | return 0; |
739 | | #endif |
740 | | |
741 | 0 | return 1; |
742 | 0 | } |
743 | | |
744 | | static const OSSL_PARAM *x963kdf_settable_ctx_params(ossl_unused void *ctx, |
745 | | ossl_unused void *provctx) |
746 | 0 | { |
747 | 0 | return x963kdf_set_ctx_params_list; |
748 | 0 | } |
749 | | |
750 | | static int x963kdf_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
751 | 0 | { |
752 | 0 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
753 | 0 | struct x963kdf_get_ctx_params_st p; |
754 | |
|
755 | 0 | if (ctx == NULL || !x963kdf_get_ctx_params_decoder(params, &p)) |
756 | 0 | return 0; |
757 | | |
758 | 0 | if (p.size != NULL) { |
759 | 0 | if (!OSSL_PARAM_set_size_t(p.size, sskdf_size(ctx))) |
760 | 0 | return 0; |
761 | 0 | } |
762 | | |
763 | 0 | if (!OSSL_FIPS_IND_GET_CTX_PARAM(ctx, p.ind)) |
764 | 0 | return 0; |
765 | | |
766 | 0 | return 1; |
767 | 0 | } |
768 | | |
769 | | static const OSSL_PARAM *x963kdf_gettable_ctx_params(ossl_unused void *ctx, ossl_unused void *provctx) |
770 | 0 | { |
771 | 0 | return x963kdf_get_ctx_params_list; |
772 | 0 | } |
773 | | |
774 | | #endif /* OPENSSL_NO_X963KDF */ |
775 | | |
776 | | #ifndef OPENSSL_NO_SSKDF |
777 | | const OSSL_DISPATCH ossl_kdf_sskdf_functions[] = { |
778 | | { OSSL_FUNC_KDF_NEWCTX, (void (*)(void))sskdf_new }, |
779 | | { OSSL_FUNC_KDF_DUPCTX, (void (*)(void))sskdf_dup }, |
780 | | { OSSL_FUNC_KDF_FREECTX, (void (*)(void))sskdf_free }, |
781 | | { OSSL_FUNC_KDF_RESET, (void (*)(void))sskdf_reset }, |
782 | | { OSSL_FUNC_KDF_DERIVE, (void (*)(void))sskdf_derive }, |
783 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
784 | | (void (*)(void))sskdf_settable_ctx_params }, |
785 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void (*)(void))sskdf_set_ctx_params }, |
786 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
787 | | (void (*)(void))sskdf_gettable_ctx_params }, |
788 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void (*)(void))sskdf_get_ctx_params }, |
789 | | OSSL_DISPATCH_END |
790 | | }; |
791 | | #endif |
792 | | |
793 | | #ifndef OPENSSL_NO_X963KDF |
794 | | const OSSL_DISPATCH ossl_kdf_x963_kdf_functions[] = { |
795 | | { OSSL_FUNC_KDF_NEWCTX, (void (*)(void))x963_new }, |
796 | | { OSSL_FUNC_KDF_DUPCTX, (void (*)(void))sskdf_dup }, |
797 | | { OSSL_FUNC_KDF_FREECTX, (void (*)(void))sskdf_free }, |
798 | | { OSSL_FUNC_KDF_RESET, (void (*)(void))sskdf_reset }, |
799 | | { OSSL_FUNC_KDF_DERIVE, (void (*)(void))x963kdf_derive }, |
800 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
801 | | (void (*)(void))x963kdf_settable_ctx_params }, |
802 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void (*)(void))x963kdf_set_ctx_params }, |
803 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
804 | | (void (*)(void))x963kdf_gettable_ctx_params }, |
805 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void (*)(void))x963kdf_get_ctx_params }, |
806 | | OSSL_DISPATCH_END |
807 | | }; |
808 | | #endif |