Coverage Report

Created: 2025-08-25 06:30

/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
# if defined(FIPS_MODULE)
385
    OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_KEY_CHECK, NULL),
386
# endif
387
    OSSL_PARAM_END
388
};
389
#endif
390
391
#ifndef kbkdf_set_ctx_params_st
392
struct kbkdf_set_ctx_params_st {
393
    OSSL_PARAM *cipher;
394
    OSSL_PARAM *digest;
395
    OSSL_PARAM *engine;
396
# if defined(FIPS_MODULE)
397
    OSSL_PARAM *ind_k;
398
# endif
399
    OSSL_PARAM *info[KBKDF_MAX_INFOS];
400
    int num_info;
401
    OSSL_PARAM *key;
402
    OSSL_PARAM *mac;
403
    OSSL_PARAM *mode;
404
    OSSL_PARAM *propq;
405
    OSSL_PARAM *r;
406
    OSSL_PARAM *salt;
407
    OSSL_PARAM *seed;
408
    OSSL_PARAM *sep;
409
    OSSL_PARAM *use_l;
410
};
411
#endif
412
413
#ifndef kbkdf_set_ctx_params_decoder
414
static int kbkdf_set_ctx_params_decoder
415
    (const OSSL_PARAM *p, struct kbkdf_set_ctx_params_st *r)
416
0
{
417
0
    const char *s;
418
419
0
    memset(r, 0, sizeof(*r));
420
0
    if (p != NULL)
421
0
        for (; (s = p->key) != NULL; p++)
422
0
            switch(s[0]) {
423
0
            default:
424
0
                break;
425
0
            case 'c':
426
0
                if (ossl_likely(strcmp("ipher", s + 1) == 0)) {
427
                    /* KDF_PARAM_CIPHER */
428
0
                    if (ossl_unlikely(r->cipher != NULL)) {
429
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
430
0
                                       "param %s is repeated", s);
431
0
                        return 0;
432
0
                    }
433
0
                    r->cipher = (OSSL_PARAM *)p;
434
0
                }
435
0
                break;
436
0
            case 'd':
437
0
                if (ossl_likely(strcmp("igest", s + 1) == 0)) {
438
                    /* KDF_PARAM_DIGEST */
439
0
                    if (ossl_unlikely(r->digest != NULL)) {
440
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
441
0
                                       "param %s is repeated", s);
442
0
                        return 0;
443
0
                    }
444
0
                    r->digest = (OSSL_PARAM *)p;
445
0
                }
446
0
                break;
447
0
            case 'e':
448
0
                if (ossl_likely(strcmp("ngine", s + 1) == 0)) {
449
                    /* ALG_PARAM_ENGINE */
450
0
                    if (ossl_unlikely(r->engine != NULL)) {
451
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
452
0
                                       "param %s is repeated", s);
453
0
                        return 0;
454
0
                    }
455
0
                    r->engine = (OSSL_PARAM *)p;
456
0
                }
457
0
                break;
458
0
            case 'i':
459
0
                if (ossl_likely(strcmp("nfo", s + 1) == 0)) {
460
                    /* KDF_PARAM_INFO */
461
0
                    if (ossl_unlikely(r->num_info >= KBKDF_MAX_INFOS)) {
462
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_TOO_MANY_RECORDS,
463
0
                                       "param %s present >%d times", s, KBKDF_MAX_INFOS);
464
0
                        return 0;
465
0
                    }
466
0
                    r->info[r->num_info++] = (OSSL_PARAM *)p;
467
0
                }
468
0
                break;
469
0
            case 'k':
470
0
                switch(s[1]) {
471
0
                default:
472
0
                    break;
473
0
                case 'e':
474
0
                    switch(s[2]) {
475
0
                    default:
476
0
                        break;
477
0
                    case 'y':
478
0
                        switch(s[3]) {
479
0
                        default:
480
0
                            break;
481
0
                        case '-':
482
# if defined(FIPS_MODULE)
483
                            if (ossl_likely(strcmp("check", s + 4) == 0)) {
484
                                /* KDF_PARAM_FIPS_KEY_CHECK */
485
                                if (ossl_unlikely(r->ind_k != NULL)) {
486
                                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
487
                                                   "param %s is repeated", s);
488
                                    return 0;
489
                                }
490
                                r->ind_k = (OSSL_PARAM *)p;
491
                            }
492
# endif
493
0
                            break;
494
0
                        case '\0':
495
0
                            if (ossl_unlikely(r->key != NULL)) {
496
0
                                ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
497
0
                                               "param %s is repeated", s);
498
0
                                return 0;
499
0
                            }
500
0
                            r->key = (OSSL_PARAM *)p;
501
0
                        }
502
0
                    }
503
0
                }
504
0
                break;
505
0
            case 'm':
506
0
                switch(s[1]) {
507
0
                default:
508
0
                    break;
509
0
                case 'a':
510
0
                    if (ossl_likely(strcmp("c", s + 2) == 0)) {
511
                        /* KDF_PARAM_MAC */
512
0
                        if (ossl_unlikely(r->mac != NULL)) {
513
0
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
514
0
                                           "param %s is repeated", s);
515
0
                            return 0;
516
0
                        }
517
0
                        r->mac = (OSSL_PARAM *)p;
518
0
                    }
519
0
                    break;
520
0
                case 'o':
521
0
                    if (ossl_likely(strcmp("de", s + 2) == 0)) {
522
                        /* KDF_PARAM_MODE */
523
0
                        if (ossl_unlikely(r->mode != NULL)) {
524
0
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
525
0
                                           "param %s is repeated", s);
526
0
                            return 0;
527
0
                        }
528
0
                        r->mode = (OSSL_PARAM *)p;
529
0
                    }
530
0
                }
531
0
                break;
532
0
            case 'p':
533
0
                if (ossl_likely(strcmp("roperties", s + 1) == 0)) {
534
                    /* KDF_PARAM_PROPERTIES */
535
0
                    if (ossl_unlikely(r->propq != NULL)) {
536
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
537
0
                                       "param %s is repeated", s);
538
0
                        return 0;
539
0
                    }
540
0
                    r->propq = (OSSL_PARAM *)p;
541
0
                }
542
0
                break;
543
0
            case 'r':
544
0
                switch(s[1]) {
545
0
                default:
546
0
                    break;
547
0
                case '\0':
548
0
                    if (ossl_unlikely(r->r != NULL)) {
549
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
550
0
                                       "param %s is repeated", s);
551
0
                        return 0;
552
0
                    }
553
0
                    r->r = (OSSL_PARAM *)p;
554
0
                }
555
0
                break;
556
0
            case 's':
557
0
                switch(s[1]) {
558
0
                default:
559
0
                    break;
560
0
                case 'a':
561
0
                    if (ossl_likely(strcmp("lt", s + 2) == 0)) {
562
                        /* KDF_PARAM_SALT */
563
0
                        if (ossl_unlikely(r->salt != NULL)) {
564
0
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
565
0
                                           "param %s is repeated", s);
566
0
                            return 0;
567
0
                        }
568
0
                        r->salt = (OSSL_PARAM *)p;
569
0
                    }
570
0
                    break;
571
0
                case 'e':
572
0
                    if (ossl_likely(strcmp("ed", s + 2) == 0)) {
573
                        /* KDF_PARAM_SEED */
574
0
                        if (ossl_unlikely(r->seed != NULL)) {
575
0
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
576
0
                                           "param %s is repeated", s);
577
0
                            return 0;
578
0
                        }
579
0
                        r->seed = (OSSL_PARAM *)p;
580
0
                    }
581
0
                }
582
0
                break;
583
0
            case 'u':
584
0
                switch(s[1]) {
585
0
                default:
586
0
                    break;
587
0
                case 's':
588
0
                    switch(s[2]) {
589
0
                    default:
590
0
                        break;
591
0
                    case 'e':
592
0
                        switch(s[3]) {
593
0
                        default:
594
0
                            break;
595
0
                        case '-':
596
0
                            switch(s[4]) {
597
0
                            default:
598
0
                                break;
599
0
                            case 'l':
600
0
                                switch(s[5]) {
601
0
                                default:
602
0
                                    break;
603
0
                                case '\0':
604
0
                                    if (ossl_unlikely(r->use_l != NULL)) {
605
0
                                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
606
0
                                                       "param %s is repeated", s);
607
0
                                        return 0;
608
0
                                    }
609
0
                                    r->use_l = (OSSL_PARAM *)p;
610
0
                                }
611
0
                                break;
612
0
                            case 's':
613
0
                                if (ossl_likely(strcmp("eparator", s + 5) == 0)) {
614
                                    /* KDF_PARAM_KBKDF_USE_SEPARATOR */
615
0
                                    if (ossl_unlikely(r->sep != NULL)) {
616
0
                                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
617
0
                                                       "param %s is repeated", s);
618
0
                                        return 0;
619
0
                                    }
620
0
                                    r->sep = (OSSL_PARAM *)p;
621
0
                                }
622
0
                            }
623
0
                        }
624
0
                    }
625
0
                }
626
0
            }
627
0
    return 1;
628
0
}
629
#endif
630
/* End of machine generated */
631
632
static int kbkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
633
0
{
634
0
    KBKDF *ctx = (KBKDF *)vctx;
635
0
    OSSL_LIB_CTX *libctx;
636
0
    struct kbkdf_set_ctx_params_st p;
637
0
    const char *s;
638
639
0
    if (ctx == NULL || !kbkdf_set_ctx_params_decoder(params, &p))
640
0
        return 0;
641
642
0
    libctx = PROV_LIBCTX_OF(ctx->provctx);
643
644
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k))
645
0
        return 0;
646
647
0
    if (!ossl_prov_macctx_load(&ctx->ctx_init, p.mac, p.cipher,
648
0
                               p.digest, p.propq, p.engine,
649
0
                               NULL, NULL, NULL, libctx))
650
0
        return 0;
651
652
0
    if (ctx->ctx_init != NULL) {
653
0
        ctx->is_kmac = 0;
654
0
        if (EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
655
0
                         OSSL_MAC_NAME_KMAC128)
656
0
            || EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
657
0
                            OSSL_MAC_NAME_KMAC256)) {
658
0
            ctx->is_kmac = 1;
659
0
        } else if (!EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
660
0
                                 OSSL_MAC_NAME_HMAC)
661
0
                   && !EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
662
0
                                    OSSL_MAC_NAME_CMAC)) {
663
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MAC);
664
0
            return 0;
665
0
        }
666
0
    }
667
668
0
    if (p.mode != NULL) {
669
0
        if (!OSSL_PARAM_get_utf8_string_ptr(p.mode, &s))
670
0
            return 0;
671
0
        if (OPENSSL_strncasecmp("counter", s, p.mode->data_size) == 0) {
672
0
            ctx->mode = COUNTER;
673
0
        } else if (OPENSSL_strncasecmp("feedback", s, p.mode->data_size) == 0) {
674
0
            ctx->mode = FEEDBACK;
675
0
        } else {
676
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
677
0
            return 0;
678
0
        }
679
0
    }
680
681
0
    if (ossl_param_get1_octet_string_from_param(p.key, &ctx->ki,
682
0
                                                &ctx->ki_len) == 0)
683
0
        return 0;
684
#ifdef FIPS_MODULE
685
    if (p.key != NULL && !fips_kbkdf_key_check_passed(ctx))
686
        return 0;
687
#endif
688
689
0
    if (ossl_param_get1_octet_string_from_param(p.salt, &ctx->label,
690
0
                                                &ctx->label_len) == 0)
691
0
        return 0;
692
693
0
    if (ossl_param_get1_concat_octet_string(p.num_info, p.info, &ctx->context,
694
0
                                            &ctx->context_len) == 0)
695
0
        return 0;
696
697
0
    if (ossl_param_get1_octet_string_from_param(p.seed, &ctx->iv,
698
0
                                                &ctx->iv_len) == 0)
699
0
        return 0;
700
701
0
    if (p.use_l != NULL && !OSSL_PARAM_get_int(p.use_l, &ctx->use_l))
702
0
        return 0;
703
704
0
    if (p.r != NULL) {
705
0
        int new_r = 0;
706
707
0
        if (!OSSL_PARAM_get_int(p.r, &new_r))
708
0
            return 0;
709
0
        if (new_r != 8 && new_r != 16 && new_r != 24 && new_r != 32)
710
0
            return 0;
711
0
        ctx->r = new_r;
712
0
    }
713
714
0
    if (p.sep != NULL && !OSSL_PARAM_get_int(p.sep, &ctx->use_separator))
715
0
        return 0;
716
717
    /* Set up digest context, if we can. */
718
0
    if (ctx->ctx_init != NULL && ctx->ki_len != 0) {
719
0
        if ((ctx->is_kmac && !kmac_init(ctx->ctx_init, ctx->label, ctx->label_len))
720
0
            || !EVP_MAC_init(ctx->ctx_init, ctx->ki, ctx->ki_len, NULL))
721
0
            return 0;
722
0
    }
723
0
    return 1;
724
0
}
725
726
static const OSSL_PARAM *kbkdf_settable_ctx_params(ossl_unused void *ctx,
727
                                                   ossl_unused void *provctx)
728
0
{
729
0
    return kbkdf_set_ctx_params_list;
730
0
}
731
732
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
733
#ifndef kbkdf_get_ctx_params_list
734
static const OSSL_PARAM kbkdf_get_ctx_params_list[] = {
735
    OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
736
# if defined(FIPS_MODULE)
737
    OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR, NULL),
738
# endif
739
    OSSL_PARAM_END
740
};
741
#endif
742
743
#ifndef kbkdf_get_ctx_params_st
744
struct kbkdf_get_ctx_params_st {
745
# if defined(FIPS_MODULE)
746
    OSSL_PARAM *ind;
747
# endif
748
    OSSL_PARAM *size;
749
};
750
#endif
751
752
#ifndef kbkdf_get_ctx_params_decoder
753
static int kbkdf_get_ctx_params_decoder
754
    (const OSSL_PARAM *p, struct kbkdf_get_ctx_params_st *r)
755
0
{
756
0
    const char *s;
757
758
0
    memset(r, 0, sizeof(*r));
759
0
    if (p != NULL)
760
0
        for (; (s = p->key) != NULL; p++)
761
0
            switch(s[0]) {
762
0
            default:
763
0
                break;
764
0
            case 'f':
765
# if defined(FIPS_MODULE)
766
                if (ossl_likely(strcmp("ips-indicator", s + 1) == 0)) {
767
                    /* KDF_PARAM_FIPS_APPROVED_INDICATOR */
768
                    if (ossl_unlikely(r->ind != NULL)) {
769
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
770
                                       "param %s is repeated", s);
771
                        return 0;
772
                    }
773
                    r->ind = (OSSL_PARAM *)p;
774
                }
775
# endif
776
0
                break;
777
0
            case 's':
778
0
                if (ossl_likely(strcmp("ize", s + 1) == 0)) {
779
                    /* KDF_PARAM_SIZE */
780
0
                    if (ossl_unlikely(r->size != NULL)) {
781
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
782
0
                                       "param %s is repeated", s);
783
0
                        return 0;
784
0
                    }
785
0
                    r->size = (OSSL_PARAM *)p;
786
0
                }
787
0
            }
788
0
    return 1;
789
0
}
790
#endif
791
/* End of machine generated */
792
793
static int kbkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
794
0
{
795
0
    struct kbkdf_get_ctx_params_st p;
796
0
    KBKDF *ctx = (KBKDF *)vctx;
797
798
0
    if (ctx == NULL || !kbkdf_get_ctx_params_decoder(params, &p))
799
0
        return 0;
800
801
    /* KBKDF can produce results as large as you like. */
802
0
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX))
803
0
        return 0;
804
805
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind))
806
0
        return 0;
807
0
    return 1;
808
0
}
809
810
static const OSSL_PARAM *kbkdf_gettable_ctx_params(ossl_unused void *ctx,
811
                                                   ossl_unused void *provctx)
812
0
{
813
0
    return kbkdf_get_ctx_params_list;
814
0
}
815
816
const OSSL_DISPATCH ossl_kdf_kbkdf_functions[] = {
817
    { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kbkdf_new },
818
    { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kbkdf_dup },
819
    { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kbkdf_free },
820
    { OSSL_FUNC_KDF_RESET, (void(*)(void))kbkdf_reset },
821
    { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kbkdf_derive },
822
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
823
      (void(*)(void))kbkdf_settable_ctx_params },
824
    { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kbkdf_set_ctx_params },
825
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
826
      (void(*)(void))kbkdf_gettable_ctx_params },
827
    { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kbkdf_get_ctx_params },
828
    OSSL_DISPATCH_END,
829
};