/src/openssl/providers/implementations/kdfs/kbkdf.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  * Copyright 2019 Red Hat, Inc.  | 
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  |  |  * This implements https://csrc.nist.gov/publications/detail/sp/800-108/final  | 
13  |  |  * section 5.1 ("counter mode") and section 5.2 ("feedback mode") in both HMAC | 
14  |  |  * and CMAC.  That document does not name the KDFs it defines; the name is  | 
15  |  |  * derived from  | 
16  |  |  * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Key-Derivation  | 
17  |  |  *  | 
18  |  |  * Note that section 5.3 ("double-pipeline mode") is not implemented, though | 
19  |  |  * it would be possible to do so in the future.  | 
20  |  |  *  | 
21  |  |  * These versions all assume the counter is used.  It would be relatively  | 
22  |  |  * straightforward to expose a configuration handle should the need arise.  | 
23  |  |  *  | 
24  |  |  * Variable names attempt to match those of SP800-108.  | 
25  |  |  */  | 
26  |  |  | 
27  |  | #include <stdarg.h>  | 
28  |  | #include <stdlib.h>  | 
29  |  | #include <string.h>  | 
30  |  |  | 
31  |  | #include <openssl/core_names.h>  | 
32  |  | #include <openssl/evp.h>  | 
33  |  | #include <openssl/hmac.h>  | 
34  |  | #include <openssl/kdf.h>  | 
35  |  | #include <openssl/params.h>  | 
36  |  | #include <openssl/proverr.h>  | 
37  |  |  | 
38  |  | #include "internal/cryptlib.h"  | 
39  |  | #include "crypto/evp.h"  | 
40  |  | #include "internal/numbers.h"  | 
41  |  | #include "internal/endian.h"  | 
42  |  | #include "prov/implementations.h"  | 
43  |  | #include "prov/provider_ctx.h"  | 
44  |  | #include "prov/provider_util.h"  | 
45  |  | #include "prov/providercommon.h"  | 
46  |  | #include "prov/securitycheck.h"  | 
47  |  | #include "internal/e_os.h"  | 
48  |  | #include "internal/params.h"  | 
49  |  |  | 
50  | 0  | #define ossl_min(a, b) ((a) < (b)) ? (a) : (b)  | 
51  |  |  | 
52  | 0  | #define KBKDF_MAX_INFOS 5  | 
53  |  |  | 
54  |  | #include "providers/implementations/kdfs/kbkdf.inc"  | 
55  |  |  | 
56  |  | typedef enum { | 
57  |  |     COUNTER = 0,  | 
58  |  |     FEEDBACK  | 
59  |  | } kbkdf_mode;  | 
60  |  |  | 
61  |  | /* Our context structure. */  | 
62  |  | typedef struct { | 
63  |  |     void *provctx;  | 
64  |  |     kbkdf_mode mode;  | 
65  |  |     EVP_MAC_CTX *ctx_init;  | 
66  |  |  | 
67  |  |     /* Names are lowercased versions of those found in SP800-108. */  | 
68  |  |     int r;  | 
69  |  |     unsigned char *ki;  | 
70  |  |     size_t ki_len;  | 
71  |  |     unsigned char *label;  | 
72  |  |     size_t label_len;  | 
73  |  |     unsigned char *context;  | 
74  |  |     size_t context_len;  | 
75  |  |     unsigned char *iv;  | 
76  |  |     size_t iv_len;  | 
77  |  |     int use_l;  | 
78  |  |     int is_kmac;  | 
79  |  |     int use_separator;  | 
80  |  |     OSSL_FIPS_IND_DECLARE  | 
81  |  | } KBKDF;  | 
82  |  |  | 
83  |  | /* Definitions needed for typechecking. */  | 
84  |  | static OSSL_FUNC_kdf_newctx_fn kbkdf_new;  | 
85  |  | static OSSL_FUNC_kdf_dupctx_fn kbkdf_dup;  | 
86  |  | static OSSL_FUNC_kdf_freectx_fn kbkdf_free;  | 
87  |  | static OSSL_FUNC_kdf_reset_fn kbkdf_reset;  | 
88  |  | static OSSL_FUNC_kdf_derive_fn kbkdf_derive;  | 
89  |  | static OSSL_FUNC_kdf_settable_ctx_params_fn kbkdf_settable_ctx_params;  | 
90  |  | static OSSL_FUNC_kdf_set_ctx_params_fn kbkdf_set_ctx_params;  | 
91  |  | static OSSL_FUNC_kdf_gettable_ctx_params_fn kbkdf_gettable_ctx_params;  | 
92  |  | static OSSL_FUNC_kdf_get_ctx_params_fn kbkdf_get_ctx_params;  | 
93  |  |  | 
94  |  | /* Not all platforms have htobe32(). */  | 
95  |  | static uint32_t be32(uint32_t host)  | 
96  | 0  | { | 
97  | 0  |     uint32_t big = 0;  | 
98  | 0  |     DECLARE_IS_ENDIAN;  | 
99  |  | 
  | 
100  | 0  |     if (!IS_LITTLE_ENDIAN)  | 
101  | 0  |         return host;  | 
102  |  |  | 
103  | 0  |     big |= (host & 0xff000000) >> 24;  | 
104  | 0  |     big |= (host & 0x00ff0000) >> 8;  | 
105  | 0  |     big |= (host & 0x0000ff00) << 8;  | 
106  | 0  |     big |= (host & 0x000000ff) << 24;  | 
107  | 0  |     return big;  | 
108  | 0  | }  | 
109  |  |  | 
110  |  | static void init(KBKDF *ctx)  | 
111  | 0  | { | 
112  | 0  |     ctx->r = 32;  | 
113  | 0  |     ctx->use_l = 1;  | 
114  | 0  |     ctx->use_separator = 1;  | 
115  | 0  |     ctx->is_kmac = 0;  | 
116  | 0  | }  | 
117  |  |  | 
118  |  | static void *kbkdf_new(void *provctx)  | 
119  | 0  | { | 
120  | 0  |     KBKDF *ctx;  | 
121  |  | 
  | 
122  | 0  |     if (!ossl_prov_is_running())  | 
123  | 0  |         return NULL;  | 
124  |  |  | 
125  | 0  |     ctx = OPENSSL_zalloc(sizeof(*ctx));  | 
126  | 0  |     if (ctx == NULL)  | 
127  | 0  |         return NULL;  | 
128  |  |  | 
129  | 0  |     ctx->provctx = provctx;  | 
130  | 0  |     OSSL_FIPS_IND_INIT(ctx)  | 
131  | 0  |     init(ctx);  | 
132  | 0  |     return ctx;  | 
133  | 0  | }  | 
134  |  |  | 
135  |  | static void kbkdf_free(void *vctx)  | 
136  | 0  | { | 
137  | 0  |     KBKDF *ctx = (KBKDF *)vctx;  | 
138  |  | 
  | 
139  | 0  |     if (ctx != NULL) { | 
140  | 0  |         kbkdf_reset(ctx);  | 
141  | 0  |         OPENSSL_free(ctx);  | 
142  | 0  |     }  | 
143  | 0  | }  | 
144  |  |  | 
145  |  | static void kbkdf_reset(void *vctx)  | 
146  | 0  | { | 
147  | 0  |     KBKDF *ctx = (KBKDF *)vctx;  | 
148  | 0  |     void *provctx = ctx->provctx;  | 
149  |  | 
  | 
150  | 0  |     EVP_MAC_CTX_free(ctx->ctx_init);  | 
151  | 0  |     OPENSSL_clear_free(ctx->context, ctx->context_len);  | 
152  | 0  |     OPENSSL_clear_free(ctx->label, ctx->label_len);  | 
153  | 0  |     OPENSSL_clear_free(ctx->ki, ctx->ki_len);  | 
154  | 0  |     OPENSSL_clear_free(ctx->iv, ctx->iv_len);  | 
155  | 0  |     memset(ctx, 0, sizeof(*ctx));  | 
156  | 0  |     ctx->provctx = provctx;  | 
157  | 0  |     init(ctx);  | 
158  | 0  | }  | 
159  |  |  | 
160  |  | static void *kbkdf_dup(void *vctx)  | 
161  | 0  | { | 
162  | 0  |     const KBKDF *src = (const KBKDF *)vctx;  | 
163  | 0  |     KBKDF *dest;  | 
164  |  | 
  | 
165  | 0  |     dest = kbkdf_new(src->provctx);  | 
166  | 0  |     if (dest != NULL) { | 
167  | 0  |         dest->ctx_init = EVP_MAC_CTX_dup(src->ctx_init);  | 
168  | 0  |         if (dest->ctx_init == NULL  | 
169  | 0  |                 || !ossl_prov_memdup(src->ki, src->ki_len,  | 
170  | 0  |                                      &dest->ki, &dest->ki_len)  | 
171  | 0  |                 || !ossl_prov_memdup(src->label, src->label_len,  | 
172  | 0  |                                      &dest->label, &dest->label_len)  | 
173  | 0  |                 || !ossl_prov_memdup(src->context, src->context_len,  | 
174  | 0  |                                      &dest->context, &dest->context_len)  | 
175  | 0  |                 || !ossl_prov_memdup(src->iv, src->iv_len,  | 
176  | 0  |                                      &dest->iv, &dest->iv_len))  | 
177  | 0  |             goto err;  | 
178  | 0  |         dest->mode = src->mode;  | 
179  | 0  |         dest->r = src->r;  | 
180  | 0  |         dest->use_l = src->use_l;  | 
181  | 0  |         dest->use_separator = src->use_separator;  | 
182  | 0  |         dest->is_kmac = src->is_kmac;  | 
183  | 0  |         OSSL_FIPS_IND_COPY(dest, src)  | 
184  | 0  |     }  | 
185  | 0  |     return dest;  | 
186  |  |  | 
187  | 0  |  err:  | 
188  | 0  |     kbkdf_free(dest);  | 
189  | 0  |     return NULL;  | 
190  | 0  | }  | 
191  |  |  | 
192  |  | #ifdef FIPS_MODULE  | 
193  |  | static int fips_kbkdf_key_check_passed(KBKDF *ctx)  | 
194  |  | { | 
195  |  |     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);  | 
196  |  |     int key_approved = ossl_kdf_check_key_size(ctx->ki_len);  | 
197  |  |  | 
198  |  |     if (!key_approved) { | 
199  |  |         if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,  | 
200  |  |                                          libctx, "KBKDF", "Key size",  | 
201  |  |                                          ossl_fips_config_kbkdf_key_check)) { | 
202  |  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);  | 
203  |  |             return 0;  | 
204  |  |         }  | 
205  |  |     }  | 
206  |  |     return 1;  | 
207  |  | }  | 
208  |  | #endif  | 
209  |  |  | 
210  |  | /* SP800-108 section 5.1 or section 5.2 depending on mode. */  | 
211  |  | static int derive(EVP_MAC_CTX *ctx_init, kbkdf_mode mode, unsigned char *iv,  | 
212  |  |                   size_t iv_len, unsigned char *label, size_t label_len,  | 
213  |  |                   unsigned char *context, size_t context_len,  | 
214  |  |                   unsigned char *k_i, size_t h, uint32_t l, int has_separator,  | 
215  |  |                   unsigned char *ko, size_t ko_len, int r)  | 
216  | 0  | { | 
217  | 0  |     int ret = 0;  | 
218  | 0  |     EVP_MAC_CTX *ctx = NULL;  | 
219  | 0  |     size_t written = 0, to_write, k_i_len = iv_len;  | 
220  | 0  |     const unsigned char zero = 0;  | 
221  | 0  |     uint32_t counter, i;  | 
222  |  |     /*  | 
223  |  |      * From SP800-108:  | 
224  |  |      * The fixed input data is a concatenation of a Label,  | 
225  |  |      * a separation indicator 0x00, the Context, and L.  | 
226  |  |      * One or more of these fixed input data fields may be omitted.  | 
227  |  |      *  | 
228  |  |      * has_separator == 0 means that the separator is omitted.  | 
229  |  |      * Passing a value of l == 0 means that L is omitted.  | 
230  |  |      * The Context and L are omitted automatically if a NULL buffer is passed.  | 
231  |  |      */  | 
232  | 0  |     int has_l = (l != 0);  | 
233  |  |  | 
234  |  |     /* Setup K(0) for feedback mode. */  | 
235  | 0  |     if (iv_len > 0)  | 
236  | 0  |         memcpy(k_i, iv, iv_len);  | 
237  |  | 
  | 
238  | 0  |     for (counter = 1; written < ko_len; counter++) { | 
239  | 0  |         i = be32(counter);  | 
240  |  | 
  | 
241  | 0  |         ctx = EVP_MAC_CTX_dup(ctx_init);  | 
242  | 0  |         if (ctx == NULL)  | 
243  | 0  |             goto done;  | 
244  |  |  | 
245  |  |         /* Perform feedback, if appropriate. */  | 
246  | 0  |         if (mode == FEEDBACK && !EVP_MAC_update(ctx, k_i, k_i_len))  | 
247  | 0  |             goto done;  | 
248  |  |  | 
249  | 0  |         if (!EVP_MAC_update(ctx, 4 - (r / 8) + (unsigned char *)&i, r / 8)  | 
250  | 0  |             || !EVP_MAC_update(ctx, label, label_len)  | 
251  | 0  |             || (has_separator && !EVP_MAC_update(ctx, &zero, 1))  | 
252  | 0  |             || !EVP_MAC_update(ctx, context, context_len)  | 
253  | 0  |             || (has_l && !EVP_MAC_update(ctx, (unsigned char *)&l, 4))  | 
254  | 0  |             || !EVP_MAC_final(ctx, k_i, NULL, h))  | 
255  | 0  |             goto done;  | 
256  |  |  | 
257  | 0  |         to_write = ko_len - written;  | 
258  | 0  |         memcpy(ko + written, k_i, ossl_min(to_write, h));  | 
259  | 0  |         written += h;  | 
260  |  | 
  | 
261  | 0  |         k_i_len = h;  | 
262  | 0  |         EVP_MAC_CTX_free(ctx);  | 
263  | 0  |         ctx = NULL;  | 
264  | 0  |     }  | 
265  |  |  | 
266  | 0  |     ret = 1;  | 
267  | 0  | done:  | 
268  | 0  |     EVP_MAC_CTX_free(ctx);  | 
269  | 0  |     return ret;  | 
270  | 0  | }  | 
271  |  |  | 
272  |  | /* This must be run before the key is set */  | 
273  |  | static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom, size_t customlen)  | 
274  | 0  | { | 
275  | 0  |     OSSL_PARAM params[2];  | 
276  |  | 
  | 
277  | 0  |     if (custom == NULL || customlen == 0)  | 
278  | 0  |         return 1;  | 
279  | 0  |     params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,  | 
280  | 0  |                                                   (void *)custom, customlen);  | 
281  | 0  |     params[1] = OSSL_PARAM_construct_end();  | 
282  | 0  |     return EVP_MAC_CTX_set_params(ctx, params) > 0;  | 
283  | 0  | }  | 
284  |  |  | 
285  |  | static int kmac_derive(EVP_MAC_CTX *ctx, unsigned char *out, size_t outlen,  | 
286  |  |                        const unsigned char *context, size_t contextlen)  | 
287  | 0  | { | 
288  | 0  |     OSSL_PARAM params[2];  | 
289  |  | 
  | 
290  | 0  |     params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &outlen);  | 
291  | 0  |     params[1] = OSSL_PARAM_construct_end();  | 
292  | 0  |     return EVP_MAC_CTX_set_params(ctx, params) > 0  | 
293  | 0  |            && EVP_MAC_update(ctx, context, contextlen)  | 
294  | 0  |            && EVP_MAC_final(ctx, out, NULL, outlen);  | 
295  | 0  | }  | 
296  |  |  | 
297  |  | static int kbkdf_derive(void *vctx, unsigned char *key, size_t keylen,  | 
298  |  |                         const OSSL_PARAM params[])  | 
299  | 0  | { | 
300  | 0  |     KBKDF *ctx = (KBKDF *)vctx;  | 
301  | 0  |     int ret = 0;  | 
302  | 0  |     unsigned char *k_i = NULL;  | 
303  | 0  |     uint32_t l = 0;  | 
304  | 0  |     size_t h = 0;  | 
305  | 0  |     uint64_t counter_max;  | 
306  |  | 
  | 
307  | 0  |     if (!ossl_prov_is_running() || !kbkdf_set_ctx_params(ctx, params))  | 
308  | 0  |         return 0;  | 
309  |  |  | 
310  |  |     /* label, context, and iv are permitted to be empty.  Check everything  | 
311  |  |      * else. */  | 
312  | 0  |     if (ctx->ctx_init == NULL) { | 
313  | 0  |         if (ctx->ki_len == 0 || ctx->ki == NULL) { | 
314  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);  | 
315  | 0  |             return 0;  | 
316  | 0  |         }  | 
317  |  |         /* Could either be missing MAC or missing message digest or missing  | 
318  |  |          * cipher - arbitrarily, I pick this one. */  | 
319  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);  | 
320  | 0  |         return 0;  | 
321  | 0  |     }  | 
322  |  |  | 
323  |  |     /* Fail if the output length is zero */  | 
324  | 0  |     if (keylen == 0) { | 
325  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);  | 
326  | 0  |         return 0;  | 
327  | 0  |     }  | 
328  |  |  | 
329  | 0  |     if (ctx->is_kmac) { | 
330  | 0  |         ret = kmac_derive(ctx->ctx_init, key, keylen,  | 
331  | 0  |                           ctx->context, ctx->context_len);  | 
332  | 0  |         goto done;  | 
333  | 0  |     }  | 
334  |  |  | 
335  | 0  |     h = EVP_MAC_CTX_get_mac_size(ctx->ctx_init);  | 
336  | 0  |     if (h == 0)  | 
337  | 0  |         goto done;  | 
338  |  |  | 
339  | 0  |     if (ctx->iv_len != 0 && ctx->iv_len != h) { | 
340  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);  | 
341  | 0  |         goto done;  | 
342  | 0  |     }  | 
343  |  |  | 
344  | 0  |     if (ctx->mode == COUNTER) { | 
345  |  |         /* Fail if keylen is too large for r */  | 
346  | 0  |         counter_max = (uint64_t)1 << (uint64_t)ctx->r;  | 
347  | 0  |         if ((uint64_t)(keylen / h) >= counter_max) { | 
348  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);  | 
349  | 0  |             goto done;  | 
350  | 0  |         }  | 
351  | 0  |     }  | 
352  |  |  | 
353  | 0  |     if (ctx->use_l != 0)  | 
354  | 0  |         l = be32((uint32_t)(keylen * 8));  | 
355  |  | 
  | 
356  | 0  |     k_i = OPENSSL_zalloc(h);  | 
357  | 0  |     if (k_i == NULL)  | 
358  | 0  |         goto done;  | 
359  |  |  | 
360  | 0  |     ret = derive(ctx->ctx_init, ctx->mode, ctx->iv, ctx->iv_len, ctx->label,  | 
361  | 0  |                  ctx->label_len, ctx->context, ctx->context_len, k_i, h, l,  | 
362  | 0  |                  ctx->use_separator, key, keylen, ctx->r);  | 
363  | 0  | done:  | 
364  | 0  |     if (ret != 1)  | 
365  | 0  |         OPENSSL_cleanse(key, keylen);  | 
366  | 0  |     OPENSSL_clear_free(k_i, h);  | 
367  | 0  |     return ret;  | 
368  | 0  | }  | 
369  |  |  | 
370  |  | static int kbkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])  | 
371  | 0  | { | 
372  | 0  |     KBKDF *ctx = (KBKDF *)vctx;  | 
373  | 0  |     OSSL_LIB_CTX *libctx;  | 
374  | 0  |     struct kbkdf_set_ctx_params_st p;  | 
375  | 0  |     const char *s;  | 
376  |  | 
  | 
377  | 0  |     if (ctx == NULL || !kbkdf_set_ctx_params_decoder(params, &p))  | 
378  | 0  |         return 0;  | 
379  |  |  | 
380  | 0  |     libctx = PROV_LIBCTX_OF(ctx->provctx);  | 
381  |  | 
  | 
382  | 0  |     if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k))  | 
383  | 0  |         return 0;  | 
384  |  |  | 
385  | 0  |     if (!ossl_prov_macctx_load(&ctx->ctx_init, p.mac, p.cipher,  | 
386  | 0  |                                p.digest, p.propq, p.engine,  | 
387  | 0  |                                NULL, NULL, NULL, libctx))  | 
388  | 0  |         return 0;  | 
389  |  |  | 
390  | 0  |     if (ctx->ctx_init != NULL) { | 
391  | 0  |         ctx->is_kmac = 0;  | 
392  | 0  |         if (EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),  | 
393  | 0  |                          OSSL_MAC_NAME_KMAC128)  | 
394  | 0  |             || EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),  | 
395  | 0  |                             OSSL_MAC_NAME_KMAC256)) { | 
396  | 0  |             ctx->is_kmac = 1;  | 
397  | 0  |         } else if (!EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),  | 
398  | 0  |                                  OSSL_MAC_NAME_HMAC)  | 
399  | 0  |                    && !EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),  | 
400  | 0  |                                     OSSL_MAC_NAME_CMAC)) { | 
401  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MAC);  | 
402  | 0  |             return 0;  | 
403  | 0  |         }  | 
404  | 0  |     }  | 
405  |  |  | 
406  | 0  |     if (p.mode != NULL) { | 
407  | 0  |         if (!OSSL_PARAM_get_utf8_string_ptr(p.mode, &s))  | 
408  | 0  |             return 0;  | 
409  | 0  |         if (OPENSSL_strncasecmp("counter", s, p.mode->data_size) == 0) { | 
410  | 0  |             ctx->mode = COUNTER;  | 
411  | 0  |         } else if (OPENSSL_strncasecmp("feedback", s, p.mode->data_size) == 0) { | 
412  | 0  |             ctx->mode = FEEDBACK;  | 
413  | 0  |         } else { | 
414  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);  | 
415  | 0  |             return 0;  | 
416  | 0  |         }  | 
417  | 0  |     }  | 
418  |  |  | 
419  | 0  |     if (ossl_param_get1_octet_string_from_param(p.key, &ctx->ki,  | 
420  | 0  |                                                 &ctx->ki_len) == 0)  | 
421  | 0  |         return 0;  | 
422  |  | #ifdef FIPS_MODULE  | 
423  |  |     if (p.key != NULL && !fips_kbkdf_key_check_passed(ctx))  | 
424  |  |         return 0;  | 
425  |  | #endif  | 
426  |  |  | 
427  | 0  |     if (ossl_param_get1_octet_string_from_param(p.salt, &ctx->label,  | 
428  | 0  |                                                 &ctx->label_len) == 0)  | 
429  | 0  |         return 0;  | 
430  |  |  | 
431  | 0  |     if (ossl_param_get1_concat_octet_string(p.num_info, p.info, &ctx->context,  | 
432  | 0  |                                             &ctx->context_len) == 0)  | 
433  | 0  |         return 0;  | 
434  |  |  | 
435  | 0  |     if (ossl_param_get1_octet_string_from_param(p.seed, &ctx->iv,  | 
436  | 0  |                                                 &ctx->iv_len) == 0)  | 
437  | 0  |         return 0;  | 
438  |  |  | 
439  | 0  |     if (p.use_l != NULL && !OSSL_PARAM_get_int(p.use_l, &ctx->use_l))  | 
440  | 0  |         return 0;  | 
441  |  |  | 
442  | 0  |     if (p.r != NULL) { | 
443  | 0  |         int new_r = 0;  | 
444  |  | 
  | 
445  | 0  |         if (!OSSL_PARAM_get_int(p.r, &new_r))  | 
446  | 0  |             return 0;  | 
447  | 0  |         if (new_r != 8 && new_r != 16 && new_r != 24 && new_r != 32)  | 
448  | 0  |             return 0;  | 
449  | 0  |         ctx->r = new_r;  | 
450  | 0  |     }  | 
451  |  |  | 
452  | 0  |     if (p.sep != NULL && !OSSL_PARAM_get_int(p.sep, &ctx->use_separator))  | 
453  | 0  |         return 0;  | 
454  |  |  | 
455  |  |     /* Set up digest context, if we can. */  | 
456  | 0  |     if (ctx->ctx_init != NULL && ctx->ki_len != 0) { | 
457  | 0  |         if ((ctx->is_kmac && !kmac_init(ctx->ctx_init, ctx->label, ctx->label_len))  | 
458  | 0  |             || !EVP_MAC_init(ctx->ctx_init, ctx->ki, ctx->ki_len, NULL))  | 
459  | 0  |             return 0;  | 
460  | 0  |     }  | 
461  | 0  |     return 1;  | 
462  | 0  | }  | 
463  |  |  | 
464  |  | static const OSSL_PARAM *kbkdf_settable_ctx_params(ossl_unused void *ctx,  | 
465  |  |                                                    ossl_unused void *provctx)  | 
466  | 0  | { | 
467  | 0  |     return kbkdf_set_ctx_params_list;  | 
468  | 0  | }  | 
469  |  |  | 
470  |  | static int kbkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])  | 
471  | 0  | { | 
472  | 0  |     struct kbkdf_get_ctx_params_st p;  | 
473  | 0  |     KBKDF *ctx = (KBKDF *)vctx;  | 
474  |  | 
  | 
475  | 0  |     if (ctx == NULL || !kbkdf_get_ctx_params_decoder(params, &p))  | 
476  | 0  |         return 0;  | 
477  |  |  | 
478  |  |     /* KBKDF can produce results as large as you like. */  | 
479  | 0  |     if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX))  | 
480  | 0  |         return 0;  | 
481  |  |  | 
482  | 0  |     if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind))  | 
483  | 0  |         return 0;  | 
484  | 0  |     return 1;  | 
485  | 0  | }  | 
486  |  |  | 
487  |  | static const OSSL_PARAM *kbkdf_gettable_ctx_params(ossl_unused void *ctx,  | 
488  |  |                                                    ossl_unused void *provctx)  | 
489  | 0  | { | 
490  | 0  |     return kbkdf_get_ctx_params_list;  | 
491  | 0  | }  | 
492  |  |  | 
493  |  | const OSSL_DISPATCH ossl_kdf_kbkdf_functions[] = { | 
494  |  |     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kbkdf_new }, | 
495  |  |     { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kbkdf_dup }, | 
496  |  |     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kbkdf_free }, | 
497  |  |     { OSSL_FUNC_KDF_RESET, (void(*)(void))kbkdf_reset }, | 
498  |  |     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kbkdf_derive }, | 
499  |  |     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, | 
500  |  |       (void(*)(void))kbkdf_settable_ctx_params },  | 
501  |  |     { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kbkdf_set_ctx_params }, | 
502  |  |     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, | 
503  |  |       (void(*)(void))kbkdf_gettable_ctx_params },  | 
504  |  |     { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kbkdf_get_ctx_params }, | 
505  |  |     OSSL_DISPATCH_END,  | 
506  |  | };  |