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