Coverage Report

Created: 2026-02-14 07:20

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
488
#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
645
{
98
645
    uint32_t big = 0;
99
645
    DECLARE_IS_ENDIAN;
100
101
645
    if (!IS_LITTLE_ENDIAN)
102
0
        return host;
103
104
645
    big |= (host & 0xff000000) >> 24;
105
645
    big |= (host & 0x00ff0000) >> 8;
106
645
    big |= (host & 0x0000ff00) << 8;
107
645
    big |= (host & 0x000000ff) << 24;
108
645
    return big;
109
645
}
110
111
static void init(KBKDF *ctx)
112
996
{
113
996
    ctx->r = 32;
114
996
    ctx->use_l = 1;
115
996
    ctx->use_separator = 1;
116
996
    ctx->is_kmac = 0;
117
996
}
118
119
static void *kbkdf_new(void *provctx)
120
498
{
121
498
    KBKDF *ctx;
122
123
498
    if (!ossl_prov_is_running())
124
0
        return NULL;
125
126
498
    ctx = OPENSSL_zalloc(sizeof(*ctx));
127
498
    if (ctx == NULL)
128
0
        return NULL;
129
130
498
    ctx->provctx = provctx;
131
498
    OSSL_FIPS_IND_INIT(ctx)
132
498
    init(ctx);
133
498
    return ctx;
134
498
}
135
136
static void kbkdf_free(void *vctx)
137
498
{
138
498
    KBKDF *ctx = (KBKDF *)vctx;
139
140
498
    if (ctx != NULL) {
141
498
        kbkdf_reset(ctx);
142
498
        OPENSSL_free(ctx);
143
498
    }
144
498
}
145
146
static void kbkdf_reset(void *vctx)
147
498
{
148
498
    KBKDF *ctx = (KBKDF *)vctx;
149
498
    void *provctx = ctx->provctx;
150
151
498
    EVP_MAC_CTX_free(ctx->ctx_init);
152
498
    OPENSSL_clear_free(ctx->context, ctx->context_len);
153
498
    OPENSSL_clear_free(ctx->label, ctx->label_len);
154
498
    OPENSSL_clear_free(ctx->ki, ctx->ki_len);
155
498
    OPENSSL_clear_free(ctx->iv, ctx->iv_len);
156
498
    memset(ctx, 0, sizeof(*ctx));
157
498
    ctx->provctx = provctx;
158
498
    init(ctx);
159
498
}
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
237
{
218
237
    int ret = 0;
219
237
    EVP_MAC_CTX *ctx = NULL;
220
237
    size_t written = 0, to_write, k_i_len = iv_len;
221
237
    const unsigned char zero = 0;
222
237
    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
237
    int has_l = (l != 0);
234
235
    /* Setup K(0) for feedback mode. */
236
237
    if (iv_len > 0)
237
18
        memcpy(k_i, iv, iv_len);
238
239
725
    for (counter = 1; written < ko_len; counter++) {
240
488
        i = be32(counter);
241
242
488
        ctx = EVP_MAC_CTX_dup(ctx_init);
243
488
        if (ctx == NULL)
244
0
            goto done;
245
246
        /* Perform feedback, if appropriate. */
247
488
        if (mode == FEEDBACK && !EVP_MAC_update(ctx, k_i, k_i_len))
248
0
            goto done;
249
250
488
        if (!EVP_MAC_update(ctx, 4 - (r / 8) + (unsigned char *)&i, r / 8)
251
488
            || !EVP_MAC_update(ctx, label, label_len)
252
488
            || (has_separator && !EVP_MAC_update(ctx, &zero, 1))
253
488
            || !EVP_MAC_update(ctx, context, context_len)
254
488
            || (has_l && !EVP_MAC_update(ctx, (unsigned char *)&l, 4))
255
488
            || !EVP_MAC_final(ctx, k_i, NULL, h))
256
0
            goto done;
257
258
488
        to_write = ko_len - written;
259
488
        memcpy(ko + written, k_i, ossl_min(to_write, h));
260
488
        written += h;
261
262
488
        k_i_len = h;
263
488
        EVP_MAC_CTX_free(ctx);
264
488
        ctx = NULL;
265
488
    }
266
267
237
    ret = 1;
268
237
done:
269
237
    EVP_MAC_CTX_free(ctx);
270
237
    return ret;
271
237
}
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
62
{
276
62
    OSSL_PARAM params[2];
277
278
62
    if (custom == NULL || customlen == 0)
279
8
        return 1;
280
54
    params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
281
54
        (void *)custom, customlen);
282
54
    params[1] = OSSL_PARAM_construct_end();
283
54
    return EVP_MAC_CTX_set_params(ctx, params) > 0;
284
62
}
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
40
{
289
40
    OSSL_PARAM params[2];
290
291
40
    params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &outlen);
292
40
    params[1] = OSSL_PARAM_construct_end();
293
40
    return EVP_MAC_CTX_set_params(ctx, params) > 0
294
40
        && EVP_MAC_update(ctx, context, contextlen)
295
29
        && EVP_MAC_final(ctx, out, NULL, outlen);
296
40
}
297
298
static int kbkdf_derive(void *vctx, unsigned char *key, size_t keylen,
299
    const OSSL_PARAM params[])
300
370
{
301
370
    KBKDF *ctx = (KBKDF *)vctx;
302
370
    int ret = 0;
303
370
    unsigned char *k_i = NULL;
304
370
    uint32_t l = 0;
305
370
    size_t h = 0;
306
370
    uint64_t counter_max;
307
308
370
    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
370
    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
370
    if (keylen == 0) {
326
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
327
0
        return 0;
328
0
    }
329
330
370
    if (ctx->is_kmac) {
331
40
        ret = kmac_derive(ctx->ctx_init, key, keylen,
332
40
            ctx->context, ctx->context_len);
333
40
        goto done;
334
40
    }
335
336
330
    h = EVP_MAC_CTX_get_mac_size(ctx->ctx_init);
337
330
    if (h == 0)
338
17
        goto done;
339
340
313
    if (ctx->iv_len != 0 && ctx->iv_len != h) {
341
76
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);
342
76
        goto done;
343
76
    }
344
345
237
    if (ctx->mode == COUNTER) {
346
        /* Fail if keylen is too large for r */
347
237
        counter_max = (uint64_t)1 << (uint64_t)ctx->r;
348
237
        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
237
    }
353
354
237
    if (ctx->use_l != 0)
355
157
        l = be32((uint32_t)(keylen * 8));
356
357
237
    k_i = OPENSSL_zalloc(h);
358
237
    if (k_i == NULL)
359
0
        goto done;
360
361
237
    ret = derive(ctx->ctx_init, ctx->mode, ctx->iv, ctx->iv_len, ctx->label,
362
237
        ctx->label_len, ctx->context, ctx->context_len, k_i, h, l,
363
237
        ctx->use_separator, key, keylen, ctx->r);
364
370
done:
365
370
    if (ret != 1)
366
111
        OPENSSL_cleanse(key, keylen);
367
370
    OPENSSL_clear_free(k_i, h);
368
370
    return ret;
369
237
}
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
292
{
420
292
    const char *s;
421
422
292
    memset(r, 0, sizeof(*r));
423
292
    if (p != NULL)
424
2.23k
        for (; (s = p->key) != NULL; p++)
425
2.06k
            switch(s[0]) {
426
0
            default:
427
0
                break;
428
172
            case 'c':
429
172
                if (ossl_likely(strcmp("ipher", s + 1) == 0)) {
430
                    /* OSSL_KDF_PARAM_CIPHER */
431
172
                    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
172
                    r->cipher = (OSSL_PARAM *)p;
437
172
                }
438
172
                break;
439
172
            case 'd':
440
172
                if (ossl_likely(strcmp("igest", s + 1) == 0)) {
441
                    /* OSSL_KDF_PARAM_DIGEST */
442
172
                    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
172
                    r->digest = (OSSL_PARAM *)p;
448
172
                }
449
172
                break;
450
172
            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
172
            case 'i':
462
172
                if (ossl_likely(strcmp("nfo", s + 1) == 0)) {
463
                    /* OSSL_KDF_PARAM_INFO */
464
172
                    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
172
                    r->info[r->num_info++] = (OSSL_PARAM *)p;
470
172
                }
471
172
                break;
472
172
            case 'k':
473
172
                switch(s[1]) {
474
0
                default:
475
0
                    break;
476
172
                case 'e':
477
172
                    switch(s[2]) {
478
0
                    default:
479
0
                        break;
480
172
                    case 'y':
481
172
                        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
172
                        case '\0':
498
172
                            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
172
                            r->key = (OSSL_PARAM *)p;
504
172
                        }
505
172
                    }
506
172
                }
507
172
                break;
508
344
            case 'm':
509
344
                switch(s[1]) {
510
0
                default:
511
0
                    break;
512
172
                case 'a':
513
172
                    if (ossl_likely(strcmp("c", s + 2) == 0)) {
514
                        /* OSSL_KDF_PARAM_MAC */
515
172
                        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
172
                        r->mac = (OSSL_PARAM *)p;
521
172
                    }
522
172
                    break;
523
172
                case 'o':
524
172
                    if (ossl_likely(strcmp("de", s + 2) == 0)) {
525
                        /* OSSL_KDF_PARAM_MODE */
526
172
                        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
172
                        r->mode = (OSSL_PARAM *)p;
532
172
                    }
533
344
                }
534
344
                break;
535
344
            case 'p':
536
172
                if (ossl_likely(strcmp("roperties", s + 1) == 0)) {
537
                    /* OSSL_KDF_PARAM_PROPERTIES */
538
172
                    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
172
                    r->propq = (OSSL_PARAM *)p;
544
172
                }
545
172
                break;
546
172
            case 'r':
547
172
                switch(s[1]) {
548
0
                default:
549
0
                    break;
550
172
                case '\0':
551
172
                    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
172
                    r->r = (OSSL_PARAM *)p;
557
172
                }
558
172
                break;
559
344
            case 's':
560
344
                switch(s[1]) {
561
0
                default:
562
0
                    break;
563
172
                case 'a':
564
172
                    if (ossl_likely(strcmp("lt", s + 2) == 0)) {
565
                        /* OSSL_KDF_PARAM_SALT */
566
172
                        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
172
                        r->salt = (OSSL_PARAM *)p;
572
172
                    }
573
172
                    break;
574
172
                case 'e':
575
172
                    if (ossl_likely(strcmp("ed", s + 2) == 0)) {
576
                        /* OSSL_KDF_PARAM_SEED */
577
172
                        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
172
                        r->seed = (OSSL_PARAM *)p;
583
172
                    }
584
344
                }
585
344
                break;
586
344
            case 'u':
587
344
                switch(s[1]) {
588
0
                default:
589
0
                    break;
590
344
                case 's':
591
344
                    switch(s[2]) {
592
0
                    default:
593
0
                        break;
594
344
                    case 'e':
595
344
                        switch(s[3]) {
596
0
                        default:
597
0
                            break;
598
344
                        case '-':
599
344
                            switch(s[4]) {
600
0
                            default:
601
0
                                break;
602
172
                            case 'l':
603
172
                                switch(s[5]) {
604
0
                                default:
605
0
                                    break;
606
172
                                case '\0':
607
172
                                    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
172
                                    r->use_l = (OSSL_PARAM *)p;
613
172
                                }
614
172
                                break;
615
172
                            case 's':
616
172
                                if (ossl_likely(strcmp("eparator", s + 5) == 0)) {
617
                                    /* OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR */
618
172
                                    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
172
                                    r->sep = (OSSL_PARAM *)p;
624
172
                                }
625
344
                            }
626
344
                        }
627
344
                    }
628
344
                }
629
2.06k
            }
630
292
    return 1;
631
292
}
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
441
{
638
441
    KBKDF *ctx = (KBKDF *)vctx;
639
441
    OSSL_LIB_CTX *libctx;
640
441
    struct kbkdf_set_ctx_params_st p;
641
441
    const char *s;
642
643
441
    if (ctx == NULL || !kbkdf_set_ctx_params_decoder(params, &p))
644
0
        return 0;
645
646
441
    libctx = PROV_LIBCTX_OF(ctx->provctx);
647
648
441
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k))
649
0
        return 0;
650
651
441
    if (!ossl_prov_macctx_load(&ctx->ctx_init, p.mac, p.cipher,
652
441
            p.digest, p.propq, p.engine,
653
441
            NULL, NULL, NULL, libctx))
654
37
        return 0;
655
656
404
    if (ctx->ctx_init != NULL) {
657
404
        ctx->is_kmac = 0;
658
404
        if (EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
659
404
                OSSL_MAC_NAME_KMAC128)
660
380
            || EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
661
380
                OSSL_MAC_NAME_KMAC256)) {
662
67
            ctx->is_kmac = 1;
663
337
        } else if (!EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
664
337
                       OSSL_MAC_NAME_HMAC)
665
220
            && !EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
666
220
                OSSL_MAC_NAME_CMAC)) {
667
6
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MAC);
668
6
            return 0;
669
6
        }
670
404
    }
671
672
398
    if (p.mode != NULL) {
673
216
        if (!OSSL_PARAM_get_utf8_string_ptr(p.mode, &s))
674
0
            return 0;
675
216
        if (OPENSSL_strncasecmp("counter", s, p.mode->data_size) == 0) {
676
203
            ctx->mode = COUNTER;
677
203
        } else if (OPENSSL_strncasecmp("feedback", s, p.mode->data_size) == 0) {
678
0
            ctx->mode = FEEDBACK;
679
13
        } else {
680
13
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
681
13
            return 0;
682
13
        }
683
216
    }
684
685
385
    if (ossl_param_get1_octet_string_from_param(p.key, &ctx->ki,
686
385
            &ctx->ki_len)
687
385
        == 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
385
    if (ossl_param_get1_octet_string_from_param(p.salt, &ctx->label,
695
385
            &ctx->label_len)
696
385
        == 0)
697
0
        return 0;
698
699
385
    if (ossl_param_get1_concat_octet_string(p.num_info, p.info, &ctx->context,
700
385
            &ctx->context_len)
701
385
        == 0)
702
0
        return 0;
703
704
385
    if (ossl_param_get1_octet_string_from_param(p.seed, &ctx->iv,
705
385
            &ctx->iv_len)
706
385
        == 0)
707
0
        return 0;
708
709
385
    if (p.use_l != NULL && !OSSL_PARAM_get_int(p.use_l, &ctx->use_l))
710
0
        return 0;
711
712
385
    if (p.r != NULL) {
713
203
        int new_r = 0;
714
715
203
        if (!OSSL_PARAM_get_int(p.r, &new_r))
716
0
            return 0;
717
203
        if (new_r != 8 && new_r != 16 && new_r != 24 && new_r != 32)
718
0
            return 0;
719
203
        ctx->r = new_r;
720
203
    }
721
722
385
    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
385
    if (ctx->ctx_init != NULL && ctx->ki_len != 0) {
727
347
        if ((ctx->is_kmac && !kmac_init(ctx->ctx_init, ctx->label, ctx->label_len))
728
343
            || !EVP_MAC_init(ctx->ctx_init, ctx->ki, ctx->ki_len, NULL))
729
21
            return 0;
730
347
    }
731
364
    return 1;
732
385
}
733
734
static const OSSL_PARAM *kbkdf_settable_ctx_params(ossl_unused void *ctx,
735
    ossl_unused void *provctx)
736
498
{
737
498
    return kbkdf_set_ctx_params_list;
738
498
}
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
};