Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/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
/* clang-format off */
11
12
/* clang-format on */
13
14
/*
15
 * This implements https://csrc.nist.gov/publications/detail/sp/800-108/final
16
 * section 5.1 ("counter mode") and section 5.2 ("feedback mode") in both HMAC
17
 * and CMAC.  That document does not name the KDFs it defines; the name is
18
 * derived from
19
 * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Key-Derivation
20
 *
21
 * Note that section 5.3 ("double-pipeline mode") is not implemented, though
22
 * it would be possible to do so in the future.
23
 *
24
 * These versions all assume the counter is used.  It would be relatively
25
 * straightforward to expose a configuration handle should the need arise.
26
 *
27
 * Variable names attempt to match those of SP800-108.
28
 */
29
30
#include <stdarg.h>
31
#include <stdlib.h>
32
#include <string.h>
33
34
#include <openssl/core_names.h>
35
#include <openssl/evp.h>
36
#include <openssl/hmac.h>
37
#include <openssl/kdf.h>
38
#include <openssl/params.h>
39
#include <openssl/proverr.h>
40
41
#include "internal/cryptlib.h"
42
#include "crypto/evp.h"
43
#include "internal/numbers.h"
44
#include "internal/endian.h"
45
#include "prov/implementations.h"
46
#include "prov/provider_ctx.h"
47
#include "prov/provider_util.h"
48
#include "prov/providercommon.h"
49
#include "prov/securitycheck.h"
50
#include "internal/e_os.h"
51
#include "internal/params.h"
52
53
324
#define ossl_min(a, b) ((a) < (b)) ? (a) : (b)
54
55
0
#define KBKDF_MAX_INFOS 5
56
57
typedef enum {
58
    COUNTER = 0,
59
    FEEDBACK
60
} kbkdf_mode;
61
62
/* Our context structure. */
63
typedef struct {
64
    void *provctx;
65
    kbkdf_mode mode;
66
    EVP_MAC_CTX *ctx_init;
67
68
    /* Names are lowercased versions of those found in SP800-108. */
69
    int r;
70
    unsigned char *ki;
71
    size_t ki_len;
72
    unsigned char *label;
73
    size_t label_len;
74
    unsigned char *context;
75
    size_t context_len;
76
    unsigned char *iv;
77
    size_t iv_len;
78
    int use_l;
79
    int is_kmac;
80
    int use_separator;
81
    OSSL_FIPS_IND_DECLARE
82
} KBKDF;
83
84
/* Definitions needed for typechecking. */
85
static OSSL_FUNC_kdf_newctx_fn kbkdf_new;
86
static OSSL_FUNC_kdf_dupctx_fn kbkdf_dup;
87
static OSSL_FUNC_kdf_freectx_fn kbkdf_free;
88
static OSSL_FUNC_kdf_reset_fn kbkdf_reset;
89
static OSSL_FUNC_kdf_derive_fn kbkdf_derive;
90
static OSSL_FUNC_kdf_settable_ctx_params_fn kbkdf_settable_ctx_params;
91
static OSSL_FUNC_kdf_set_ctx_params_fn kbkdf_set_ctx_params;
92
static OSSL_FUNC_kdf_gettable_ctx_params_fn kbkdf_gettable_ctx_params;
93
static OSSL_FUNC_kdf_get_ctx_params_fn kbkdf_get_ctx_params;
94
95
/* Not all platforms have htobe32(). */
96
static uint32_t be32(uint32_t host)
97
398
{
98
398
    uint32_t big = 0;
99
398
    DECLARE_IS_ENDIAN;
100
101
398
    if (!IS_LITTLE_ENDIAN)
102
0
        return host;
103
104
398
    big |= (host & 0xff000000) >> 24;
105
398
    big |= (host & 0x00ff0000) >> 8;
106
398
    big |= (host & 0x0000ff00) << 8;
107
398
    big |= (host & 0x000000ff) << 24;
108
398
    return big;
109
398
}
110
111
static void init(KBKDF *ctx)
112
788
{
113
788
    ctx->r = 32;
114
788
    ctx->use_l = 1;
115
788
    ctx->use_separator = 1;
116
788
    ctx->is_kmac = 0;
117
788
}
118
119
static void *kbkdf_new(void *provctx)
120
394
{
121
394
    KBKDF *ctx;
122
123
394
    if (!ossl_prov_is_running())
124
0
        return NULL;
125
126
394
    ctx = OPENSSL_zalloc(sizeof(*ctx));
127
394
    if (ctx == NULL)
128
0
        return NULL;
129
130
394
    ctx->provctx = provctx;
131
394
    OSSL_FIPS_IND_INIT(ctx)
132
394
    init(ctx);
133
394
    return ctx;
134
394
}
135
136
static void kbkdf_free(void *vctx)
137
394
{
138
394
    KBKDF *ctx = (KBKDF *)vctx;
139
140
394
    if (ctx != NULL) {
141
394
        kbkdf_reset(ctx);
142
394
        OPENSSL_free(ctx);
143
394
    }
144
394
}
145
146
static void kbkdf_reset(void *vctx)
147
394
{
148
394
    KBKDF *ctx = (KBKDF *)vctx;
149
394
    void *provctx = ctx->provctx;
150
151
394
    EVP_MAC_CTX_free(ctx->ctx_init);
152
394
    OPENSSL_clear_free(ctx->context, ctx->context_len);
153
394
    OPENSSL_clear_free(ctx->label, ctx->label_len);
154
394
    OPENSSL_clear_free(ctx->ki, ctx->ki_len);
155
394
    OPENSSL_clear_free(ctx->iv, ctx->iv_len);
156
394
    memset(ctx, 0, sizeof(*ctx));
157
394
    ctx->provctx = provctx;
158
394
    init(ctx);
159
394
}
160
161
static void *kbkdf_dup(void *vctx)
162
0
{
163
0
    const KBKDF *src = (const KBKDF *)vctx;
164
0
    KBKDF *dest;
165
166
0
    dest = kbkdf_new(src->provctx);
167
0
    if (dest != NULL) {
168
0
        dest->ctx_init = EVP_MAC_CTX_dup(src->ctx_init);
169
0
        if (dest->ctx_init == NULL
170
0
            || !ossl_prov_memdup(src->ki, src->ki_len,
171
0
                &dest->ki, &dest->ki_len)
172
0
            || !ossl_prov_memdup(src->label, src->label_len,
173
0
                &dest->label, &dest->label_len)
174
0
            || !ossl_prov_memdup(src->context, src->context_len,
175
0
                &dest->context, &dest->context_len)
176
0
            || !ossl_prov_memdup(src->iv, src->iv_len,
177
0
                &dest->iv, &dest->iv_len))
178
0
            goto err;
179
0
        dest->mode = src->mode;
180
0
        dest->r = src->r;
181
0
        dest->use_l = src->use_l;
182
0
        dest->use_separator = src->use_separator;
183
0
        dest->is_kmac = src->is_kmac;
184
0
        OSSL_FIPS_IND_COPY(dest, src)
185
0
    }
186
0
    return dest;
187
188
0
err:
189
0
    kbkdf_free(dest);
190
0
    return NULL;
191
0
}
192
193
#ifdef FIPS_MODULE
194
static int fips_kbkdf_key_check_passed(KBKDF *ctx)
195
{
196
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
197
    int key_approved = ossl_kdf_check_key_size(ctx->ki_len);
198
199
    if (!key_approved) {
200
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,
201
                libctx, "KBKDF", "Key size",
202
                ossl_fips_config_kbkdf_key_check)) {
203
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
204
            return 0;
205
        }
206
    }
207
    return 1;
208
}
209
#endif
210
211
/* SP800-108 section 5.1 or section 5.2 depending on mode. */
212
static int derive(EVP_MAC_CTX *ctx_init, kbkdf_mode mode, unsigned char *iv,
213
    size_t iv_len, unsigned char *label, size_t label_len,
214
    unsigned char *context, size_t context_len,
215
    unsigned char *k_i, size_t h, uint32_t l, int has_separator,
216
    unsigned char *ko, size_t ko_len, int r)
217
146
{
218
146
    int ret = 0;
219
146
    EVP_MAC_CTX *ctx = NULL;
220
146
    size_t written = 0, to_write, k_i_len = iv_len;
221
146
    const unsigned char zero = 0;
222
146
    uint32_t counter, i;
223
    /*
224
     * From SP800-108:
225
     * The fixed input data is a concatenation of a Label,
226
     * a separation indicator 0x00, the Context, and L.
227
     * One or more of these fixed input data fields may be omitted.
228
     *
229
     * has_separator == 0 means that the separator is omitted.
230
     * Passing a value of l == 0 means that L is omitted.
231
     * The Context and L are omitted automatically if a NULL buffer is passed.
232
     */
233
146
    int has_l = (l != 0);
234
235
    /* Setup K(0) for feedback mode. */
236
146
    if (iv_len > 0)
237
13
        memcpy(k_i, iv, iv_len);
238
239
470
    for (counter = 1; written < ko_len; counter++) {
240
324
        i = be32(counter);
241
242
324
        ctx = EVP_MAC_CTX_dup(ctx_init);
243
324
        if (ctx == NULL)
244
0
            goto done;
245
246
        /* Perform feedback, if appropriate. */
247
324
        if (mode == FEEDBACK && !EVP_MAC_update(ctx, k_i, k_i_len))
248
0
            goto done;
249
250
324
        if (!EVP_MAC_update(ctx, 4 - (r / 8) + (unsigned char *)&i, r / 8)
251
324
            || !EVP_MAC_update(ctx, label, label_len)
252
324
            || (has_separator && !EVP_MAC_update(ctx, &zero, 1))
253
324
            || !EVP_MAC_update(ctx, context, context_len)
254
324
            || (has_l && !EVP_MAC_update(ctx, (unsigned char *)&l, 4))
255
324
            || !EVP_MAC_final(ctx, k_i, NULL, h))
256
0
            goto done;
257
258
324
        to_write = ko_len - written;
259
324
        memcpy(ko + written, k_i, ossl_min(to_write, h));
260
324
        written += h;
261
262
324
        k_i_len = h;
263
324
        EVP_MAC_CTX_free(ctx);
264
324
        ctx = NULL;
265
324
    }
266
267
146
    ret = 1;
268
146
done:
269
146
    EVP_MAC_CTX_free(ctx);
270
146
    return ret;
271
146
}
272
273
/* This must be run before the key is set */
274
static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom, size_t customlen)
275
59
{
276
59
    OSSL_PARAM params[2];
277
278
59
    if (custom == NULL || customlen == 0)
279
13
        return 1;
280
46
    params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
281
46
        (void *)custom, customlen);
282
46
    params[1] = OSSL_PARAM_construct_end();
283
46
    return EVP_MAC_CTX_set_params(ctx, params) > 0;
284
59
}
285
286
static int kmac_derive(EVP_MAC_CTX *ctx, unsigned char *out, size_t outlen,
287
    const unsigned char *context, size_t contextlen)
288
41
{
289
41
    OSSL_PARAM params[2];
290
291
41
    params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &outlen);
292
41
    params[1] = OSSL_PARAM_construct_end();
293
41
    return EVP_MAC_CTX_set_params(ctx, params) > 0
294
41
        && EVP_MAC_update(ctx, context, contextlen)
295
31
        && EVP_MAC_final(ctx, out, NULL, outlen);
296
41
}
297
298
static int kbkdf_derive(void *vctx, unsigned char *key, size_t keylen,
299
    const OSSL_PARAM params[])
300
274
{
301
274
    KBKDF *ctx = (KBKDF *)vctx;
302
274
    int ret = 0;
303
274
    unsigned char *k_i = NULL;
304
274
    uint32_t l = 0;
305
274
    size_t h = 0;
306
274
    uint64_t counter_max;
307
308
274
    if (!ossl_prov_is_running() || !kbkdf_set_ctx_params(ctx, params))
309
0
        return 0;
310
311
    /* label, context, and iv are permitted to be empty.  Check everything
312
     * else. */
313
274
    if (ctx->ctx_init == NULL) {
314
0
        if (ctx->ki_len == 0 || ctx->ki == NULL) {
315
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
316
0
            return 0;
317
0
        }
318
        /* Could either be missing MAC or missing message digest or missing
319
         * cipher - arbitrarily, I pick this one. */
320
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);
321
0
        return 0;
322
0
    }
323
324
    /* Fail if the output length is zero */
325
274
    if (keylen == 0) {
326
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
327
0
        return 0;
328
0
    }
329
330
274
    if (ctx->is_kmac) {
331
41
        ret = kmac_derive(ctx->ctx_init, key, keylen,
332
41
            ctx->context, ctx->context_len);
333
41
        goto done;
334
41
    }
335
336
233
    h = EVP_MAC_CTX_get_mac_size(ctx->ctx_init);
337
233
    if (h == 0)
338
8
        goto done;
339
340
225
    if (ctx->iv_len != 0 && ctx->iv_len != h) {
341
79
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);
342
79
        goto done;
343
79
    }
344
345
146
    if (ctx->mode == COUNTER) {
346
        /* Fail if keylen is too large for r */
347
146
        counter_max = (uint64_t)1 << (uint64_t)ctx->r;
348
146
        if ((uint64_t)(keylen / h) >= counter_max) {
349
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
350
0
            goto done;
351
0
        }
352
146
    }
353
354
146
    if (ctx->use_l != 0)
355
74
        l = be32((uint32_t)(keylen * 8));
356
357
146
    k_i = OPENSSL_zalloc(h);
358
146
    if (k_i == NULL)
359
0
        goto done;
360
361
146
    ret = derive(ctx->ctx_init, ctx->mode, ctx->iv, ctx->iv_len, ctx->label,
362
146
        ctx->label_len, ctx->context, ctx->context_len, k_i, h, l,
363
146
        ctx->use_separator, key, keylen, ctx->r);
364
274
done:
365
274
    if (ret != 1)
366
103
        OPENSSL_cleanse(key, keylen);
367
274
    OPENSSL_clear_free(k_i, h);
368
274
    return ret;
369
146
}
370
371
/* clang-format off */
372
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
373
#ifndef kbkdf_set_ctx_params_list
374
static const OSSL_PARAM kbkdf_set_ctx_params_list[] = {
375
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),
376
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
377
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
378
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
379
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
380
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CIPHER, NULL, 0),
381
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),
382
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MODE, NULL, 0),
383
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
384
    OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_USE_L, NULL),
385
    OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR, NULL),
386
    OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_R, NULL),
387
# if defined(FIPS_MODULE)
388
    OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_KEY_CHECK, NULL),
389
# endif
390
    OSSL_PARAM_END
391
};
392
#endif
393
394
#ifndef kbkdf_set_ctx_params_st
395
struct kbkdf_set_ctx_params_st {
396
    OSSL_PARAM *cipher;
397
    OSSL_PARAM *digest;
398
    OSSL_PARAM *engine;
399
# if defined(FIPS_MODULE)
400
    OSSL_PARAM *ind_k;
401
# endif
402
    OSSL_PARAM *info[KBKDF_MAX_INFOS];
403
    int num_info;
404
    OSSL_PARAM *key;
405
    OSSL_PARAM *mac;
406
    OSSL_PARAM *mode;
407
    OSSL_PARAM *propq;
408
    OSSL_PARAM *r;
409
    OSSL_PARAM *salt;
410
    OSSL_PARAM *seed;
411
    OSSL_PARAM *sep;
412
    OSSL_PARAM *use_l;
413
};
414
#endif
415
416
#ifndef kbkdf_set_ctx_params_decoder
417
static int kbkdf_set_ctx_params_decoder
418
    (const OSSL_PARAM *p, struct kbkdf_set_ctx_params_st *r)
419
126
{
420
126
    const char *s;
421
422
126
    memset(r, 0, sizeof(*r));
423
126
    if (p != NULL)
424
1.06k
        for (; (s = p->key) != NULL; p++)
425
984
            switch(s[0]) {
426
0
            default:
427
0
                break;
428
82
            case 'c':
429
82
                if (ossl_likely(strcmp("ipher", s + 1) == 0)) {
430
                    /* OSSL_KDF_PARAM_CIPHER */
431
82
                    if (ossl_unlikely(r->cipher != NULL)) {
432
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
433
0
                                       "param %s is repeated", s);
434
0
                        return 0;
435
0
                    }
436
82
                    r->cipher = (OSSL_PARAM *)p;
437
82
                }
438
82
                break;
439
82
            case 'd':
440
82
                if (ossl_likely(strcmp("igest", s + 1) == 0)) {
441
                    /* OSSL_KDF_PARAM_DIGEST */
442
82
                    if (ossl_unlikely(r->digest != NULL)) {
443
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
444
0
                                       "param %s is repeated", s);
445
0
                        return 0;
446
0
                    }
447
82
                    r->digest = (OSSL_PARAM *)p;
448
82
                }
449
82
                break;
450
82
            case 'e':
451
0
                if (ossl_likely(strcmp("ngine", s + 1) == 0)) {
452
                    /* OSSL_ALG_PARAM_ENGINE */
453
0
                    if (ossl_unlikely(r->engine != NULL)) {
454
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
455
0
                                       "param %s is repeated", s);
456
0
                        return 0;
457
0
                    }
458
0
                    r->engine = (OSSL_PARAM *)p;
459
0
                }
460
0
                break;
461
82
            case 'i':
462
82
                if (ossl_likely(strcmp("nfo", s + 1) == 0)) {
463
                    /* OSSL_KDF_PARAM_INFO */
464
82
                    if (ossl_unlikely(r->num_info >= KBKDF_MAX_INFOS)) {
465
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_TOO_MANY_RECORDS,
466
0
                                       "param %s present >%d times", s, KBKDF_MAX_INFOS);
467
0
                        return 0;
468
0
                    }
469
82
                    r->info[r->num_info++] = (OSSL_PARAM *)p;
470
82
                }
471
82
                break;
472
82
            case 'k':
473
82
                switch(s[1]) {
474
0
                default:
475
0
                    break;
476
82
                case 'e':
477
82
                    switch(s[2]) {
478
0
                    default:
479
0
                        break;
480
82
                    case 'y':
481
82
                        switch(s[3]) {
482
0
                        default:
483
0
                            break;
484
0
                        case '-':
485
# if defined(FIPS_MODULE)
486
                            if (ossl_likely(strcmp("check", s + 4) == 0)) {
487
                                /* OSSL_KDF_PARAM_FIPS_KEY_CHECK */
488
                                if (ossl_unlikely(r->ind_k != NULL)) {
489
                                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
490
                                                   "param %s is repeated", s);
491
                                    return 0;
492
                                }
493
                                r->ind_k = (OSSL_PARAM *)p;
494
                            }
495
# endif
496
0
                            break;
497
82
                        case '\0':
498
82
                            if (ossl_unlikely(r->key != NULL)) {
499
0
                                ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
500
0
                                               "param %s is repeated", s);
501
0
                                return 0;
502
0
                            }
503
82
                            r->key = (OSSL_PARAM *)p;
504
82
                        }
505
82
                    }
506
82
                }
507
82
                break;
508
164
            case 'm':
509
164
                switch(s[1]) {
510
0
                default:
511
0
                    break;
512
82
                case 'a':
513
82
                    if (ossl_likely(strcmp("c", s + 2) == 0)) {
514
                        /* OSSL_KDF_PARAM_MAC */
515
82
                        if (ossl_unlikely(r->mac != NULL)) {
516
0
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
517
0
                                           "param %s is repeated", s);
518
0
                            return 0;
519
0
                        }
520
82
                        r->mac = (OSSL_PARAM *)p;
521
82
                    }
522
82
                    break;
523
82
                case 'o':
524
82
                    if (ossl_likely(strcmp("de", s + 2) == 0)) {
525
                        /* OSSL_KDF_PARAM_MODE */
526
82
                        if (ossl_unlikely(r->mode != NULL)) {
527
0
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
528
0
                                           "param %s is repeated", s);
529
0
                            return 0;
530
0
                        }
531
82
                        r->mode = (OSSL_PARAM *)p;
532
82
                    }
533
164
                }
534
164
                break;
535
164
            case 'p':
536
82
                if (ossl_likely(strcmp("roperties", s + 1) == 0)) {
537
                    /* OSSL_KDF_PARAM_PROPERTIES */
538
82
                    if (ossl_unlikely(r->propq != NULL)) {
539
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
540
0
                                       "param %s is repeated", s);
541
0
                        return 0;
542
0
                    }
543
82
                    r->propq = (OSSL_PARAM *)p;
544
82
                }
545
82
                break;
546
82
            case 'r':
547
82
                switch(s[1]) {
548
0
                default:
549
0
                    break;
550
82
                case '\0':
551
82
                    if (ossl_unlikely(r->r != NULL)) {
552
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
553
0
                                       "param %s is repeated", s);
554
0
                        return 0;
555
0
                    }
556
82
                    r->r = (OSSL_PARAM *)p;
557
82
                }
558
82
                break;
559
164
            case 's':
560
164
                switch(s[1]) {
561
0
                default:
562
0
                    break;
563
82
                case 'a':
564
82
                    if (ossl_likely(strcmp("lt", s + 2) == 0)) {
565
                        /* OSSL_KDF_PARAM_SALT */
566
82
                        if (ossl_unlikely(r->salt != NULL)) {
567
0
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
568
0
                                           "param %s is repeated", s);
569
0
                            return 0;
570
0
                        }
571
82
                        r->salt = (OSSL_PARAM *)p;
572
82
                    }
573
82
                    break;
574
82
                case 'e':
575
82
                    if (ossl_likely(strcmp("ed", s + 2) == 0)) {
576
                        /* OSSL_KDF_PARAM_SEED */
577
82
                        if (ossl_unlikely(r->seed != NULL)) {
578
0
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
579
0
                                           "param %s is repeated", s);
580
0
                            return 0;
581
0
                        }
582
82
                        r->seed = (OSSL_PARAM *)p;
583
82
                    }
584
164
                }
585
164
                break;
586
164
            case 'u':
587
164
                switch(s[1]) {
588
0
                default:
589
0
                    break;
590
164
                case 's':
591
164
                    switch(s[2]) {
592
0
                    default:
593
0
                        break;
594
164
                    case 'e':
595
164
                        switch(s[3]) {
596
0
                        default:
597
0
                            break;
598
164
                        case '-':
599
164
                            switch(s[4]) {
600
0
                            default:
601
0
                                break;
602
82
                            case 'l':
603
82
                                switch(s[5]) {
604
0
                                default:
605
0
                                    break;
606
82
                                case '\0':
607
82
                                    if (ossl_unlikely(r->use_l != NULL)) {
608
0
                                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
609
0
                                                       "param %s is repeated", s);
610
0
                                        return 0;
611
0
                                    }
612
82
                                    r->use_l = (OSSL_PARAM *)p;
613
82
                                }
614
82
                                break;
615
82
                            case 's':
616
82
                                if (ossl_likely(strcmp("eparator", s + 5) == 0)) {
617
                                    /* OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR */
618
82
                                    if (ossl_unlikely(r->sep != NULL)) {
619
0
                                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
620
0
                                                       "param %s is repeated", s);
621
0
                                        return 0;
622
0
                                    }
623
82
                                    r->sep = (OSSL_PARAM *)p;
624
82
                                }
625
164
                            }
626
164
                        }
627
164
                    }
628
164
                }
629
984
            }
630
126
    return 1;
631
126
}
632
#endif
633
/* End of machine generated */
634
/* clang-format on */
635
636
static int kbkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
637
311
{
638
311
    KBKDF *ctx = (KBKDF *)vctx;
639
311
    OSSL_LIB_CTX *libctx;
640
311
    struct kbkdf_set_ctx_params_st p;
641
311
    const char *s;
642
643
311
    if (ctx == NULL || !kbkdf_set_ctx_params_decoder(params, &p))
644
0
        return 0;
645
646
311
    libctx = PROV_LIBCTX_OF(ctx->provctx);
647
648
311
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k))
649
0
        return 0;
650
651
311
    if (!ossl_prov_macctx_load(&ctx->ctx_init, p.mac, p.cipher,
652
311
            p.digest, p.propq, p.engine,
653
311
            NULL, NULL, NULL, libctx))
654
41
        return 0;
655
656
270
    if (ctx->ctx_init != NULL) {
657
270
        ctx->is_kmac = 0;
658
270
        if (EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
659
270
                OSSL_MAC_NAME_KMAC128)
660
230
            || EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
661
230
                OSSL_MAC_NAME_KMAC256)) {
662
59
            ctx->is_kmac = 1;
663
211
        } else if (!EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
664
211
                       OSSL_MAC_NAME_HMAC)
665
88
            && !EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
666
88
                OSSL_MAC_NAME_CMAC)) {
667
6
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MAC);
668
6
            return 0;
669
6
        }
670
270
    }
671
672
264
    if (p.mode != NULL) {
673
146
        if (!OSSL_PARAM_get_utf8_string_ptr(p.mode, &s))
674
0
            return 0;
675
146
        if (OPENSSL_strncasecmp("counter", s, p.mode->data_size) == 0) {
676
136
            ctx->mode = COUNTER;
677
136
        } else if (OPENSSL_strncasecmp("feedback", s, p.mode->data_size) == 0) {
678
0
            ctx->mode = FEEDBACK;
679
10
        } else {
680
10
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
681
10
            return 0;
682
10
        }
683
146
    }
684
685
254
    if (ossl_param_get1_octet_string_from_param(p.key, &ctx->ki,
686
254
            &ctx->ki_len)
687
254
        == 0)
688
0
        return 0;
689
#ifdef FIPS_MODULE
690
    if (p.key != NULL && !fips_kbkdf_key_check_passed(ctx))
691
        return 0;
692
#endif
693
694
254
    if (ossl_param_get1_octet_string_from_param(p.salt, &ctx->label,
695
254
            &ctx->label_len)
696
254
        == 0)
697
0
        return 0;
698
699
254
    if (ossl_param_get1_concat_octet_string(p.num_info, p.info, &ctx->context,
700
254
            &ctx->context_len)
701
254
        == 0)
702
0
        return 0;
703
704
254
    if (ossl_param_get1_octet_string_from_param(p.seed, &ctx->iv,
705
254
            &ctx->iv_len)
706
254
        == 0)
707
0
        return 0;
708
709
254
    if (p.use_l != NULL && !OSSL_PARAM_get_int(p.use_l, &ctx->use_l))
710
0
        return 0;
711
712
254
    if (p.r != NULL) {
713
136
        int new_r = 0;
714
715
136
        if (!OSSL_PARAM_get_int(p.r, &new_r))
716
0
            return 0;
717
136
        if (new_r != 8 && new_r != 16 && new_r != 24 && new_r != 32)
718
0
            return 0;
719
136
        ctx->r = new_r;
720
136
    }
721
722
254
    if (p.sep != NULL && !OSSL_PARAM_get_int(p.sep, &ctx->use_separator))
723
0
        return 0;
724
725
    /* Set up digest context, if we can. */
726
254
    if (ctx->ctx_init != NULL && ctx->ki_len != 0) {
727
232
        if ((ctx->is_kmac && !kmac_init(ctx->ctx_init, ctx->label, ctx->label_len))
728
227
            || !EVP_MAC_init(ctx->ctx_init, ctx->ki, ctx->ki_len, NULL))
729
18
            return 0;
730
232
    }
731
236
    return 1;
732
254
}
733
734
static const OSSL_PARAM *kbkdf_settable_ctx_params(ossl_unused void *ctx,
735
    ossl_unused void *provctx)
736
394
{
737
394
    return kbkdf_set_ctx_params_list;
738
394
}
739
740
/* clang-format off */
741
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
742
#ifndef kbkdf_get_ctx_params_list
743
static const OSSL_PARAM kbkdf_get_ctx_params_list[] = {
744
    OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
745
# if defined(FIPS_MODULE)
746
    OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR, NULL),
747
# endif
748
    OSSL_PARAM_END
749
};
750
#endif
751
752
#ifndef kbkdf_get_ctx_params_st
753
struct kbkdf_get_ctx_params_st {
754
# if defined(FIPS_MODULE)
755
    OSSL_PARAM *ind;
756
# endif
757
    OSSL_PARAM *size;
758
};
759
#endif
760
761
#ifndef kbkdf_get_ctx_params_decoder
762
static int kbkdf_get_ctx_params_decoder
763
    (const OSSL_PARAM *p, struct kbkdf_get_ctx_params_st *r)
764
0
{
765
0
    const char *s;
766
767
0
    memset(r, 0, sizeof(*r));
768
0
    if (p != NULL)
769
0
        for (; (s = p->key) != NULL; p++)
770
0
            switch(s[0]) {
771
0
            default:
772
0
                break;
773
0
            case 'f':
774
# if defined(FIPS_MODULE)
775
                if (ossl_likely(strcmp("ips-indicator", s + 1) == 0)) {
776
                    /* OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR */
777
                    if (ossl_unlikely(r->ind != NULL)) {
778
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
779
                                       "param %s is repeated", s);
780
                        return 0;
781
                    }
782
                    r->ind = (OSSL_PARAM *)p;
783
                }
784
# endif
785
0
                break;
786
0
            case 's':
787
0
                if (ossl_likely(strcmp("ize", s + 1) == 0)) {
788
                    /* OSSL_KDF_PARAM_SIZE */
789
0
                    if (ossl_unlikely(r->size != NULL)) {
790
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
791
0
                                       "param %s is repeated", s);
792
0
                        return 0;
793
0
                    }
794
0
                    r->size = (OSSL_PARAM *)p;
795
0
                }
796
0
            }
797
0
    return 1;
798
0
}
799
#endif
800
/* End of machine generated */
801
/* clang-format on */
802
803
static int kbkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
804
0
{
805
0
    struct kbkdf_get_ctx_params_st p;
806
0
    KBKDF *ctx = (KBKDF *)vctx;
807
808
0
    if (ctx == NULL || !kbkdf_get_ctx_params_decoder(params, &p))
809
0
        return 0;
810
811
    /* KBKDF can produce results as large as you like. */
812
0
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX))
813
0
        return 0;
814
815
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind))
816
0
        return 0;
817
0
    return 1;
818
0
}
819
820
static const OSSL_PARAM *kbkdf_gettable_ctx_params(ossl_unused void *ctx,
821
    ossl_unused void *provctx)
822
0
{
823
0
    return kbkdf_get_ctx_params_list;
824
0
}
825
826
const OSSL_DISPATCH ossl_kdf_kbkdf_functions[] = {
827
    { OSSL_FUNC_KDF_NEWCTX, (void (*)(void))kbkdf_new },
828
    { OSSL_FUNC_KDF_DUPCTX, (void (*)(void))kbkdf_dup },
829
    { OSSL_FUNC_KDF_FREECTX, (void (*)(void))kbkdf_free },
830
    { OSSL_FUNC_KDF_RESET, (void (*)(void))kbkdf_reset },
831
    { OSSL_FUNC_KDF_DERIVE, (void (*)(void))kbkdf_derive },
832
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
833
        (void (*)(void))kbkdf_settable_ctx_params },
834
    { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void (*)(void))kbkdf_set_ctx_params },
835
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
836
        (void (*)(void))kbkdf_gettable_ctx_params },
837
    { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void (*)(void))kbkdf_get_ctx_params },
838
    OSSL_DISPATCH_END,
839
};