Coverage Report

Created: 2025-08-03 07:12

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