/src/openssl/providers/implementations/kdfs/kbkdf.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2019-2024 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  |  | typedef enum { | 
53  |  |     COUNTER = 0,  | 
54  |  |     FEEDBACK  | 
55  |  | } kbkdf_mode;  | 
56  |  |  | 
57  |  | /* Our context structure. */  | 
58  |  | typedef struct { | 
59  |  |     void *provctx;  | 
60  |  |     kbkdf_mode mode;  | 
61  |  |     EVP_MAC_CTX *ctx_init;  | 
62  |  |  | 
63  |  |     /* Names are lowercased versions of those found in SP800-108. */  | 
64  |  |     int r;  | 
65  |  |     unsigned char *ki;  | 
66  |  |     size_t ki_len;  | 
67  |  |     unsigned char *label;  | 
68  |  |     size_t label_len;  | 
69  |  |     unsigned char *context;  | 
70  |  |     size_t context_len;  | 
71  |  |     unsigned char *iv;  | 
72  |  |     size_t iv_len;  | 
73  |  |     int use_l;  | 
74  |  |     int is_kmac;  | 
75  |  |     int use_separator;  | 
76  |  |     OSSL_FIPS_IND_DECLARE  | 
77  |  | } KBKDF;  | 
78  |  |  | 
79  |  | /* Definitions needed for typechecking. */  | 
80  |  | static OSSL_FUNC_kdf_newctx_fn kbkdf_new;  | 
81  |  | static OSSL_FUNC_kdf_dupctx_fn kbkdf_dup;  | 
82  |  | static OSSL_FUNC_kdf_freectx_fn kbkdf_free;  | 
83  |  | static OSSL_FUNC_kdf_reset_fn kbkdf_reset;  | 
84  |  | static OSSL_FUNC_kdf_derive_fn kbkdf_derive;  | 
85  |  | static OSSL_FUNC_kdf_settable_ctx_params_fn kbkdf_settable_ctx_params;  | 
86  |  | static OSSL_FUNC_kdf_set_ctx_params_fn kbkdf_set_ctx_params;  | 
87  |  | static OSSL_FUNC_kdf_gettable_ctx_params_fn kbkdf_gettable_ctx_params;  | 
88  |  | static OSSL_FUNC_kdf_get_ctx_params_fn kbkdf_get_ctx_params;  | 
89  |  |  | 
90  |  | /* Not all platforms have htobe32(). */  | 
91  |  | static uint32_t be32(uint32_t host)  | 
92  | 0  | { | 
93  | 0  |     uint32_t big = 0;  | 
94  | 0  |     DECLARE_IS_ENDIAN;  | 
95  |  | 
  | 
96  | 0  |     if (!IS_LITTLE_ENDIAN)  | 
97  | 0  |         return host;  | 
98  |  |  | 
99  | 0  |     big |= (host & 0xff000000) >> 24;  | 
100  | 0  |     big |= (host & 0x00ff0000) >> 8;  | 
101  | 0  |     big |= (host & 0x0000ff00) << 8;  | 
102  | 0  |     big |= (host & 0x000000ff) << 24;  | 
103  | 0  |     return big;  | 
104  | 0  | }  | 
105  |  |  | 
106  |  | static void init(KBKDF *ctx)  | 
107  | 0  | { | 
108  | 0  |     ctx->r = 32;  | 
109  | 0  |     ctx->use_l = 1;  | 
110  | 0  |     ctx->use_separator = 1;  | 
111  | 0  |     ctx->is_kmac = 0;  | 
112  | 0  | }  | 
113  |  |  | 
114  |  | static void *kbkdf_new(void *provctx)  | 
115  | 0  | { | 
116  | 0  |     KBKDF *ctx;  | 
117  |  | 
  | 
118  | 0  |     if (!ossl_prov_is_running())  | 
119  | 0  |         return NULL;  | 
120  |  |  | 
121  | 0  |     ctx = OPENSSL_zalloc(sizeof(*ctx));  | 
122  | 0  |     if (ctx == NULL)  | 
123  | 0  |         return NULL;  | 
124  |  |  | 
125  | 0  |     ctx->provctx = provctx;  | 
126  | 0  |     OSSL_FIPS_IND_INIT(ctx)  | 
127  | 0  |     init(ctx);  | 
128  | 0  |     return ctx;  | 
129  | 0  | }  | 
130  |  |  | 
131  |  | static void kbkdf_free(void *vctx)  | 
132  | 0  | { | 
133  | 0  |     KBKDF *ctx = (KBKDF *)vctx;  | 
134  |  | 
  | 
135  | 0  |     if (ctx != NULL) { | 
136  | 0  |         kbkdf_reset(ctx);  | 
137  | 0  |         OPENSSL_free(ctx);  | 
138  | 0  |     }  | 
139  | 0  | }  | 
140  |  |  | 
141  |  | static void kbkdf_reset(void *vctx)  | 
142  | 0  | { | 
143  | 0  |     KBKDF *ctx = (KBKDF *)vctx;  | 
144  | 0  |     void *provctx = ctx->provctx;  | 
145  |  | 
  | 
146  | 0  |     EVP_MAC_CTX_free(ctx->ctx_init);  | 
147  | 0  |     OPENSSL_clear_free(ctx->context, ctx->context_len);  | 
148  | 0  |     OPENSSL_clear_free(ctx->label, ctx->label_len);  | 
149  | 0  |     OPENSSL_clear_free(ctx->ki, ctx->ki_len);  | 
150  | 0  |     OPENSSL_clear_free(ctx->iv, ctx->iv_len);  | 
151  | 0  |     memset(ctx, 0, sizeof(*ctx));  | 
152  | 0  |     ctx->provctx = provctx;  | 
153  | 0  |     init(ctx);  | 
154  | 0  | }  | 
155  |  |  | 
156  |  | static void *kbkdf_dup(void *vctx)  | 
157  | 0  | { | 
158  | 0  |     const KBKDF *src = (const KBKDF *)vctx;  | 
159  | 0  |     KBKDF *dest;  | 
160  |  | 
  | 
161  | 0  |     dest = kbkdf_new(src->provctx);  | 
162  | 0  |     if (dest != NULL) { | 
163  | 0  |         dest->ctx_init = EVP_MAC_CTX_dup(src->ctx_init);  | 
164  | 0  |         if (dest->ctx_init == NULL  | 
165  | 0  |                 || !ossl_prov_memdup(src->ki, src->ki_len,  | 
166  | 0  |                                      &dest->ki, &dest->ki_len)  | 
167  | 0  |                 || !ossl_prov_memdup(src->label, src->label_len,  | 
168  | 0  |                                      &dest->label, &dest->label_len)  | 
169  | 0  |                 || !ossl_prov_memdup(src->context, src->context_len,  | 
170  | 0  |                                      &dest->context, &dest->context_len)  | 
171  | 0  |                 || !ossl_prov_memdup(src->iv, src->iv_len,  | 
172  | 0  |                                      &dest->iv, &dest->iv_len))  | 
173  | 0  |             goto err;  | 
174  | 0  |         dest->mode = src->mode;  | 
175  | 0  |         dest->r = src->r;  | 
176  | 0  |         dest->use_l = src->use_l;  | 
177  | 0  |         dest->use_separator = src->use_separator;  | 
178  | 0  |         dest->is_kmac = src->is_kmac;  | 
179  | 0  |         OSSL_FIPS_IND_COPY(dest, src)  | 
180  | 0  |     }  | 
181  | 0  |     return dest;  | 
182  |  |  | 
183  | 0  |  err:  | 
184  | 0  |     kbkdf_free(dest);  | 
185  | 0  |     return NULL;  | 
186  | 0  | }  | 
187  |  |  | 
188  |  | #ifdef FIPS_MODULE  | 
189  |  | static int fips_kbkdf_key_check_passed(KBKDF *ctx)  | 
190  |  | { | 
191  |  |     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);  | 
192  |  |     int key_approved = ossl_kdf_check_key_size(ctx->ki_len);  | 
193  |  |  | 
194  |  |     if (!key_approved) { | 
195  |  |         if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,  | 
196  |  |                                          libctx, "KBKDF", "Key size",  | 
197  |  |                                          ossl_fips_config_kbkdf_key_check)) { | 
198  |  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);  | 
199  |  |             return 0;  | 
200  |  |         }  | 
201  |  |     }  | 
202  |  |     return 1;  | 
203  |  | }  | 
204  |  | #endif  | 
205  |  |  | 
206  |  | /* SP800-108 section 5.1 or section 5.2 depending on mode. */  | 
207  |  | static int derive(EVP_MAC_CTX *ctx_init, kbkdf_mode mode, unsigned char *iv,  | 
208  |  |                   size_t iv_len, unsigned char *label, size_t label_len,  | 
209  |  |                   unsigned char *context, size_t context_len,  | 
210  |  |                   unsigned char *k_i, size_t h, uint32_t l, int has_separator,  | 
211  |  |                   unsigned char *ko, size_t ko_len, int r)  | 
212  | 0  | { | 
213  | 0  |     int ret = 0;  | 
214  | 0  |     EVP_MAC_CTX *ctx = NULL;  | 
215  | 0  |     size_t written = 0, to_write, k_i_len = iv_len;  | 
216  | 0  |     const unsigned char zero = 0;  | 
217  | 0  |     uint32_t counter, i;  | 
218  |  |     /*  | 
219  |  |      * From SP800-108:  | 
220  |  |      * The fixed input data is a concatenation of a Label,  | 
221  |  |      * a separation indicator 0x00, the Context, and L.  | 
222  |  |      * One or more of these fixed input data fields may be omitted.  | 
223  |  |      *  | 
224  |  |      * has_separator == 0 means that the separator is omitted.  | 
225  |  |      * Passing a value of l == 0 means that L is omitted.  | 
226  |  |      * The Context and L are omitted automatically if a NULL buffer is passed.  | 
227  |  |      */  | 
228  | 0  |     int has_l = (l != 0);  | 
229  |  |  | 
230  |  |     /* Setup K(0) for feedback mode. */  | 
231  | 0  |     if (iv_len > 0)  | 
232  | 0  |         memcpy(k_i, iv, iv_len);  | 
233  |  | 
  | 
234  | 0  |     for (counter = 1; written < ko_len; counter++) { | 
235  | 0  |         i = be32(counter);  | 
236  |  | 
  | 
237  | 0  |         ctx = EVP_MAC_CTX_dup(ctx_init);  | 
238  | 0  |         if (ctx == NULL)  | 
239  | 0  |             goto done;  | 
240  |  |  | 
241  |  |         /* Perform feedback, if appropriate. */  | 
242  | 0  |         if (mode == FEEDBACK && !EVP_MAC_update(ctx, k_i, k_i_len))  | 
243  | 0  |             goto done;  | 
244  |  |  | 
245  | 0  |         if (!EVP_MAC_update(ctx, 4 - (r / 8) + (unsigned char *)&i, r / 8)  | 
246  | 0  |             || !EVP_MAC_update(ctx, label, label_len)  | 
247  | 0  |             || (has_separator && !EVP_MAC_update(ctx, &zero, 1))  | 
248  | 0  |             || !EVP_MAC_update(ctx, context, context_len)  | 
249  | 0  |             || (has_l && !EVP_MAC_update(ctx, (unsigned char *)&l, 4))  | 
250  | 0  |             || !EVP_MAC_final(ctx, k_i, NULL, h))  | 
251  | 0  |             goto done;  | 
252  |  |  | 
253  | 0  |         to_write = ko_len - written;  | 
254  | 0  |         memcpy(ko + written, k_i, ossl_min(to_write, h));  | 
255  | 0  |         written += h;  | 
256  |  | 
  | 
257  | 0  |         k_i_len = h;  | 
258  | 0  |         EVP_MAC_CTX_free(ctx);  | 
259  | 0  |         ctx = NULL;  | 
260  | 0  |     }  | 
261  |  |  | 
262  | 0  |     ret = 1;  | 
263  | 0  | done:  | 
264  | 0  |     EVP_MAC_CTX_free(ctx);  | 
265  | 0  |     return ret;  | 
266  | 0  | }  | 
267  |  |  | 
268  |  | /* This must be run before the key is set */  | 
269  |  | static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom, size_t customlen)  | 
270  | 0  | { | 
271  | 0  |     OSSL_PARAM params[2];  | 
272  |  | 
  | 
273  | 0  |     if (custom == NULL || customlen == 0)  | 
274  | 0  |         return 1;  | 
275  | 0  |     params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,  | 
276  | 0  |                                                   (void *)custom, customlen);  | 
277  | 0  |     params[1] = OSSL_PARAM_construct_end();  | 
278  | 0  |     return EVP_MAC_CTX_set_params(ctx, params) > 0;  | 
279  | 0  | }  | 
280  |  |  | 
281  |  | static int kmac_derive(EVP_MAC_CTX *ctx, unsigned char *out, size_t outlen,  | 
282  |  |                        const unsigned char *context, size_t contextlen)  | 
283  | 0  | { | 
284  | 0  |     OSSL_PARAM params[2];  | 
285  |  | 
  | 
286  | 0  |     params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &outlen);  | 
287  | 0  |     params[1] = OSSL_PARAM_construct_end();  | 
288  | 0  |     return EVP_MAC_CTX_set_params(ctx, params) > 0  | 
289  | 0  |            && EVP_MAC_update(ctx, context, contextlen)  | 
290  | 0  |            && EVP_MAC_final(ctx, out, NULL, outlen);  | 
291  | 0  | }  | 
292  |  |  | 
293  |  | static int kbkdf_derive(void *vctx, unsigned char *key, size_t keylen,  | 
294  |  |                         const OSSL_PARAM params[])  | 
295  | 0  | { | 
296  | 0  |     KBKDF *ctx = (KBKDF *)vctx;  | 
297  | 0  |     int ret = 0;  | 
298  | 0  |     unsigned char *k_i = NULL;  | 
299  | 0  |     uint32_t l = 0;  | 
300  | 0  |     size_t h = 0;  | 
301  | 0  |     uint64_t counter_max;  | 
302  |  | 
  | 
303  | 0  |     if (!ossl_prov_is_running() || !kbkdf_set_ctx_params(ctx, params))  | 
304  | 0  |         return 0;  | 
305  |  |  | 
306  |  |     /* label, context, and iv are permitted to be empty.  Check everything  | 
307  |  |      * else. */  | 
308  | 0  |     if (ctx->ctx_init == NULL) { | 
309  | 0  |         if (ctx->ki_len == 0 || ctx->ki == NULL) { | 
310  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);  | 
311  | 0  |             return 0;  | 
312  | 0  |         }  | 
313  |  |         /* Could either be missing MAC or missing message digest or missing  | 
314  |  |          * cipher - arbitrarily, I pick this one. */  | 
315  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);  | 
316  | 0  |         return 0;  | 
317  | 0  |     }  | 
318  |  |  | 
319  |  |     /* Fail if the output length is zero */  | 
320  | 0  |     if (keylen == 0) { | 
321  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);  | 
322  | 0  |         return 0;  | 
323  | 0  |     }  | 
324  |  |  | 
325  | 0  |     if (ctx->is_kmac) { | 
326  | 0  |         ret = kmac_derive(ctx->ctx_init, key, keylen,  | 
327  | 0  |                           ctx->context, ctx->context_len);  | 
328  | 0  |         goto done;  | 
329  | 0  |     }  | 
330  |  |  | 
331  | 0  |     h = EVP_MAC_CTX_get_mac_size(ctx->ctx_init);  | 
332  | 0  |     if (h == 0)  | 
333  | 0  |         goto done;  | 
334  |  |  | 
335  | 0  |     if (ctx->iv_len != 0 && ctx->iv_len != h) { | 
336  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);  | 
337  | 0  |         goto done;  | 
338  | 0  |     }  | 
339  |  |  | 
340  | 0  |     if (ctx->mode == COUNTER) { | 
341  |  |         /* Fail if keylen is too large for r */  | 
342  | 0  |         counter_max = (uint64_t)1 << (uint64_t)ctx->r;  | 
343  | 0  |         if ((uint64_t)(keylen / h) >= counter_max) { | 
344  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);  | 
345  | 0  |             goto done;  | 
346  | 0  |         }  | 
347  | 0  |     }  | 
348  |  |  | 
349  | 0  |     if (ctx->use_l != 0)  | 
350  | 0  |         l = be32(keylen * 8);  | 
351  |  | 
  | 
352  | 0  |     k_i = OPENSSL_zalloc(h);  | 
353  | 0  |     if (k_i == NULL)  | 
354  | 0  |         goto done;  | 
355  |  |  | 
356  | 0  |     ret = derive(ctx->ctx_init, ctx->mode, ctx->iv, ctx->iv_len, ctx->label,  | 
357  | 0  |                  ctx->label_len, ctx->context, ctx->context_len, k_i, h, l,  | 
358  | 0  |                  ctx->use_separator, key, keylen, ctx->r);  | 
359  | 0  | done:  | 
360  | 0  |     if (ret != 1)  | 
361  | 0  |         OPENSSL_cleanse(key, keylen);  | 
362  | 0  |     OPENSSL_clear_free(k_i, h);  | 
363  | 0  |     return ret;  | 
364  | 0  | }  | 
365  |  |  | 
366  |  | static int kbkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])  | 
367  | 0  | { | 
368  | 0  |     KBKDF *ctx = (KBKDF *)vctx;  | 
369  | 0  |     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);  | 
370  | 0  |     const OSSL_PARAM *p;  | 
371  |  | 
  | 
372  | 0  |     if (ossl_param_is_empty(params))  | 
373  | 0  |         return 1;  | 
374  |  |  | 
375  | 0  |     if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, params,  | 
376  | 0  |                                      OSSL_KDF_PARAM_FIPS_KEY_CHECK))  | 
377  | 0  |         return 0;  | 
378  |  |  | 
379  | 0  |     if (!ossl_prov_macctx_load_from_params(&ctx->ctx_init, params, NULL,  | 
380  | 0  |                                            NULL, NULL, libctx))  | 
381  | 0  |         return 0;  | 
382  | 0  |     if (ctx->ctx_init != NULL) { | 
383  | 0  |         ctx->is_kmac = 0;  | 
384  | 0  |         if (EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),  | 
385  | 0  |                          OSSL_MAC_NAME_KMAC128)  | 
386  | 0  |             || EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),  | 
387  | 0  |                             OSSL_MAC_NAME_KMAC256)) { | 
388  | 0  |             ctx->is_kmac = 1;  | 
389  | 0  |         } else if (!EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),  | 
390  | 0  |                                  OSSL_MAC_NAME_HMAC)  | 
391  | 0  |                    && !EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),  | 
392  | 0  |                                     OSSL_MAC_NAME_CMAC)) { | 
393  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MAC);  | 
394  | 0  |             return 0;  | 
395  | 0  |         }  | 
396  | 0  |     }  | 
397  |  |  | 
398  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MODE);  | 
399  | 0  |     if (p != NULL  | 
400  | 0  |         && OPENSSL_strncasecmp("counter", p->data, p->data_size) == 0) { | 
401  | 0  |         ctx->mode = COUNTER;  | 
402  | 0  |     } else if (p != NULL  | 
403  | 0  |                && OPENSSL_strncasecmp("feedback", p->data, p->data_size) == 0) { | 
404  | 0  |         ctx->mode = FEEDBACK;  | 
405  | 0  |     } else if (p != NULL) { | 
406  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);  | 
407  | 0  |         return 0;  | 
408  | 0  |     }  | 
409  |  |  | 
410  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY);  | 
411  | 0  |     if (p != NULL) { | 
412  | 0  |         if (ossl_param_get1_octet_string(p, OSSL_KDF_PARAM_KEY,  | 
413  | 0  |                                          &ctx->ki, &ctx->ki_len) == 0)  | 
414  | 0  |             return 0;  | 
415  |  | #ifdef FIPS_MODULE  | 
416  |  |         if (!fips_kbkdf_key_check_passed(ctx))  | 
417  |  |             return 0;  | 
418  |  | #endif  | 
419  | 0  |     }  | 
420  |  |  | 
421  | 0  |     if (ossl_param_get1_octet_string(params, OSSL_KDF_PARAM_SALT,  | 
422  | 0  |                                      &ctx->label, &ctx->label_len) == 0)  | 
423  | 0  |             return 0;  | 
424  |  |  | 
425  | 0  |     if (ossl_param_get1_concat_octet_string(params, OSSL_KDF_PARAM_INFO,  | 
426  | 0  |                                             &ctx->context, &ctx->context_len,  | 
427  | 0  |                                             0) == 0)  | 
428  | 0  |         return 0;  | 
429  |  |  | 
430  | 0  |     if (ossl_param_get1_octet_string(params, OSSL_KDF_PARAM_SEED,  | 
431  | 0  |                                      &ctx->iv, &ctx->iv_len) == 0)  | 
432  | 0  |             return 0;  | 
433  |  |  | 
434  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KBKDF_USE_L);  | 
435  | 0  |     if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->use_l))  | 
436  | 0  |         return 0;  | 
437  |  |  | 
438  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KBKDF_R);  | 
439  | 0  |     if (p != NULL) { | 
440  | 0  |         int new_r = 0;  | 
441  |  | 
  | 
442  | 0  |         if (!OSSL_PARAM_get_int(p, &new_r))  | 
443  | 0  |             return 0;  | 
444  | 0  |         if (new_r != 8 && new_r != 16 && new_r != 24 && new_r != 32)  | 
445  | 0  |             return 0;  | 
446  | 0  |         ctx->r = new_r;  | 
447  | 0  |     }  | 
448  |  |  | 
449  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR);  | 
450  | 0  |     if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->use_separator))  | 
451  | 0  |         return 0;  | 
452  |  |  | 
453  |  |     /* Set up digest context, if we can. */  | 
454  | 0  |     if (ctx->ctx_init != NULL && ctx->ki_len != 0) { | 
455  | 0  |         if ((ctx->is_kmac && !kmac_init(ctx->ctx_init, ctx->label, ctx->label_len))  | 
456  | 0  |             || !EVP_MAC_init(ctx->ctx_init, ctx->ki, ctx->ki_len, NULL))  | 
457  | 0  |             return 0;  | 
458  | 0  |     }  | 
459  | 0  |     return 1;  | 
460  | 0  | }  | 
461  |  |  | 
462  |  | static const OSSL_PARAM *kbkdf_settable_ctx_params(ossl_unused void *ctx,  | 
463  |  |                                                    ossl_unused void *provctx)  | 
464  | 0  | { | 
465  | 0  |     static const OSSL_PARAM known_settable_ctx_params[] = { | 
466  | 0  |         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),  | 
467  | 0  |         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),  | 
468  | 0  |         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),  | 
469  | 0  |         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),  | 
470  | 0  |         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),  | 
471  | 0  |         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CIPHER, NULL, 0),  | 
472  | 0  |         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),  | 
473  | 0  |         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MODE, NULL, 0),  | 
474  | 0  |         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),  | 
475  | 0  |         OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_USE_L, NULL),  | 
476  | 0  |         OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR, NULL),  | 
477  | 0  |         OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_R, NULL),  | 
478  | 0  |         OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_KEY_CHECK)  | 
479  | 0  |         OSSL_PARAM_END,  | 
480  | 0  |     };  | 
481  | 0  |     return known_settable_ctx_params;  | 
482  | 0  | }  | 
483  |  |  | 
484  |  | static int kbkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])  | 
485  | 0  | { | 
486  |  | #ifdef FIPS_MODULE  | 
487  |  |     KBKDF *ctx = (KBKDF *)vctx;  | 
488  |  | #endif  | 
489  | 0  |     OSSL_PARAM *p;  | 
490  |  |  | 
491  |  |     /* KBKDF can produce results as large as you like. */  | 
492  | 0  |     p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE);  | 
493  | 0  |     if (p != NULL && !OSSL_PARAM_set_size_t(p, SIZE_MAX))  | 
494  | 0  |         return 0;  | 
495  |  |  | 
496  | 0  |     if (!OSSL_FIPS_IND_GET_CTX_PARAM(ctx, params))  | 
497  | 0  |         return 0;  | 
498  | 0  |     return 1;  | 
499  | 0  | }  | 
500  |  |  | 
501  |  | static const OSSL_PARAM *kbkdf_gettable_ctx_params(ossl_unused void *ctx,  | 
502  |  |                                                    ossl_unused void *provctx)  | 
503  | 0  | { | 
504  | 0  |     static const OSSL_PARAM known_gettable_ctx_params[] = { | 
505  | 0  |         OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),  | 
506  | 0  |         OSSL_FIPS_IND_GETTABLE_CTX_PARAM()  | 
507  | 0  |         OSSL_PARAM_END  | 
508  | 0  |     };  | 
509  | 0  |     return known_gettable_ctx_params;  | 
510  | 0  | }  | 
511  |  |  | 
512  |  | const OSSL_DISPATCH ossl_kdf_kbkdf_functions[] = { | 
513  |  |     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kbkdf_new }, | 
514  |  |     { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kbkdf_dup }, | 
515  |  |     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kbkdf_free }, | 
516  |  |     { OSSL_FUNC_KDF_RESET, (void(*)(void))kbkdf_reset }, | 
517  |  |     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kbkdf_derive }, | 
518  |  |     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, | 
519  |  |       (void(*)(void))kbkdf_settable_ctx_params },  | 
520  |  |     { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kbkdf_set_ctx_params }, | 
521  |  |     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, | 
522  |  |       (void(*)(void))kbkdf_gettable_ctx_params },  | 
523  |  |     { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kbkdf_get_ctx_params }, | 
524  |  |     OSSL_DISPATCH_END,  | 
525  |  | };  |