Coverage Report

Created: 2026-02-22 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
11
/*
12
 * This implements https://csrc.nist.gov/publications/detail/sp/800-108/final
13
 * section 5.1 ("counter mode") and section 5.2 ("feedback mode") in both HMAC
14
 * and CMAC.  That document does not name the KDFs it defines; the name is
15
 * derived from
16
 * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Key-Derivation
17
 *
18
 * Note that section 5.3 ("double-pipeline mode") is not implemented, though
19
 * it would be possible to do so in the future.
20
 *
21
 * These versions all assume the counter is used.  It would be relatively
22
 * straightforward to expose a configuration handle should the need arise.
23
 *
24
 * Variable names attempt to match those of SP800-108.
25
 */
26
27
#include <stdarg.h>
28
#include <stdlib.h>
29
#include <string.h>
30
31
#include <openssl/core_names.h>
32
#include <openssl/evp.h>
33
#include <openssl/hmac.h>
34
#include <openssl/kdf.h>
35
#include <openssl/params.h>
36
#include <openssl/proverr.h>
37
38
#include "internal/cryptlib.h"
39
#include "crypto/evp.h"
40
#include "internal/numbers.h"
41
#include "internal/endian.h"
42
#include "prov/implementations.h"
43
#include "prov/provider_ctx.h"
44
#include "prov/provider_util.h"
45
#include "prov/providercommon.h"
46
#include "prov/securitycheck.h"
47
#include "internal/e_os.h"
48
#include "internal/fips.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
#include "providers/implementations/kdfs/kbkdf.inc"
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
0
{
98
0
    uint32_t big = 0;
99
0
    DECLARE_IS_ENDIAN;
100
101
0
    if (!IS_LITTLE_ENDIAN)
102
0
        return host;
103
104
0
    big |= (host & 0xff000000) >> 24;
105
0
    big |= (host & 0x00ff0000) >> 8;
106
0
    big |= (host & 0x0000ff00) << 8;
107
0
    big |= (host & 0x000000ff) << 24;
108
0
    return big;
109
0
}
110
111
static void init(KBKDF *ctx)
112
0
{
113
0
    ctx->r = 32;
114
0
    ctx->use_l = 1;
115
0
    ctx->use_separator = 1;
116
0
    ctx->is_kmac = 0;
117
0
}
118
119
static void *kbkdf_new(void *provctx)
120
0
{
121
0
    KBKDF *ctx;
122
123
0
    if (!ossl_prov_is_running())
124
0
        return NULL;
125
126
#ifdef FIPS_MODULE
127
    if (!ossl_deferred_self_test(PROV_LIBCTX_OF(provctx),
128
            ST_ID_KDF_KBKDF))
129
        return NULL;
130
#endif
131
132
0
    ctx = OPENSSL_zalloc(sizeof(*ctx));
133
0
    if (ctx == NULL)
134
0
        return NULL;
135
136
0
    ctx->provctx = provctx;
137
0
    OSSL_FIPS_IND_INIT(ctx)
138
0
    init(ctx);
139
0
    return ctx;
140
0
}
141
142
static void kbkdf_free(void *vctx)
143
0
{
144
0
    KBKDF *ctx = (KBKDF *)vctx;
145
146
0
    if (ctx != NULL) {
147
0
        kbkdf_reset(ctx);
148
0
        OPENSSL_free(ctx);
149
0
    }
150
0
}
151
152
static void kbkdf_reset(void *vctx)
153
0
{
154
0
    KBKDF *ctx = (KBKDF *)vctx;
155
0
    void *provctx = ctx->provctx;
156
157
0
    EVP_MAC_CTX_free(ctx->ctx_init);
158
0
    OPENSSL_clear_free(ctx->context, ctx->context_len);
159
0
    OPENSSL_clear_free(ctx->label, ctx->label_len);
160
0
    OPENSSL_clear_free(ctx->ki, ctx->ki_len);
161
0
    OPENSSL_clear_free(ctx->iv, ctx->iv_len);
162
0
    memset(ctx, 0, sizeof(*ctx));
163
0
    ctx->provctx = provctx;
164
0
    init(ctx);
165
0
}
166
167
static void *kbkdf_dup(void *vctx)
168
0
{
169
0
    const KBKDF *src = (const KBKDF *)vctx;
170
0
    KBKDF *dest;
171
172
0
    dest = kbkdf_new(src->provctx);
173
0
    if (dest != NULL) {
174
0
        dest->ctx_init = EVP_MAC_CTX_dup(src->ctx_init);
175
0
        if (dest->ctx_init == NULL
176
0
            || !ossl_prov_memdup(src->ki, src->ki_len,
177
0
                &dest->ki, &dest->ki_len)
178
0
            || !ossl_prov_memdup(src->label, src->label_len,
179
0
                &dest->label, &dest->label_len)
180
0
            || !ossl_prov_memdup(src->context, src->context_len,
181
0
                &dest->context, &dest->context_len)
182
0
            || !ossl_prov_memdup(src->iv, src->iv_len,
183
0
                &dest->iv, &dest->iv_len))
184
0
            goto err;
185
0
        dest->mode = src->mode;
186
0
        dest->r = src->r;
187
0
        dest->use_l = src->use_l;
188
0
        dest->use_separator = src->use_separator;
189
0
        dest->is_kmac = src->is_kmac;
190
0
        OSSL_FIPS_IND_COPY(dest, src)
191
0
    }
192
0
    return dest;
193
194
0
err:
195
0
    kbkdf_free(dest);
196
0
    return NULL;
197
0
}
198
199
#ifdef FIPS_MODULE
200
static int fips_kbkdf_key_check_passed(KBKDF *ctx)
201
{
202
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
203
    int key_approved = ossl_kdf_check_key_size(ctx->ki_len);
204
205
    if (!key_approved) {
206
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,
207
                libctx, "KBKDF", "Key size",
208
                ossl_fips_config_kbkdf_key_check)) {
209
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
210
            return 0;
211
        }
212
    }
213
    return 1;
214
}
215
#endif
216
217
/* SP800-108 section 5.1 or section 5.2 depending on mode. */
218
static int derive(EVP_MAC_CTX *ctx_init, kbkdf_mode mode, unsigned char *iv,
219
    size_t iv_len, unsigned char *label, size_t label_len,
220
    unsigned char *context, size_t context_len,
221
    unsigned char *k_i, size_t h, uint32_t l, int has_separator,
222
    unsigned char *ko, size_t ko_len, int r)
223
0
{
224
0
    int ret = 0;
225
0
    EVP_MAC_CTX *ctx = NULL;
226
0
    size_t written = 0, to_write, k_i_len = iv_len;
227
0
    const unsigned char zero = 0;
228
0
    uint32_t counter, i;
229
    /*
230
     * From SP800-108:
231
     * The fixed input data is a concatenation of a Label,
232
     * a separation indicator 0x00, the Context, and L.
233
     * One or more of these fixed input data fields may be omitted.
234
     *
235
     * has_separator == 0 means that the separator is omitted.
236
     * Passing a value of l == 0 means that L is omitted.
237
     * The Context and L are omitted automatically if a NULL buffer is passed.
238
     */
239
0
    int has_l = (l != 0);
240
241
    /* Setup K(0) for feedback mode. */
242
0
    if (iv_len > 0)
243
0
        memcpy(k_i, iv, iv_len);
244
245
0
    for (counter = 1; written < ko_len; counter++) {
246
0
        i = be32(counter);
247
248
0
        ctx = EVP_MAC_CTX_dup(ctx_init);
249
0
        if (ctx == NULL)
250
0
            goto done;
251
252
        /* Perform feedback, if appropriate. */
253
0
        if (mode == FEEDBACK && !EVP_MAC_update(ctx, k_i, k_i_len))
254
0
            goto done;
255
256
0
        if (!EVP_MAC_update(ctx, 4 - (r / 8) + (unsigned char *)&i, r / 8)
257
0
            || !EVP_MAC_update(ctx, label, label_len)
258
0
            || (has_separator && !EVP_MAC_update(ctx, &zero, 1))
259
0
            || !EVP_MAC_update(ctx, context, context_len)
260
0
            || (has_l && !EVP_MAC_update(ctx, (unsigned char *)&l, 4))
261
0
            || !EVP_MAC_final(ctx, k_i, NULL, h))
262
0
            goto done;
263
264
0
        to_write = ko_len - written;
265
0
        memcpy(ko + written, k_i, ossl_min(to_write, h));
266
0
        written += h;
267
268
0
        k_i_len = h;
269
0
        EVP_MAC_CTX_free(ctx);
270
0
        ctx = NULL;
271
0
    }
272
273
0
    ret = 1;
274
0
done:
275
0
    EVP_MAC_CTX_free(ctx);
276
0
    return ret;
277
0
}
278
279
/* This must be run before the key is set */
280
static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom, size_t customlen)
281
0
{
282
0
    OSSL_PARAM params[2];
283
284
0
    if (custom == NULL || customlen == 0)
285
0
        return 1;
286
0
    params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
287
0
        (void *)custom, customlen);
288
0
    params[1] = OSSL_PARAM_construct_end();
289
0
    return EVP_MAC_CTX_set_params(ctx, params) > 0;
290
0
}
291
292
static int kmac_derive(EVP_MAC_CTX *ctx, unsigned char *out, size_t outlen,
293
    const unsigned char *context, size_t contextlen)
294
0
{
295
0
    OSSL_PARAM params[2];
296
297
0
    params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &outlen);
298
0
    params[1] = OSSL_PARAM_construct_end();
299
0
    return EVP_MAC_CTX_set_params(ctx, params) > 0
300
0
        && EVP_MAC_update(ctx, context, contextlen)
301
0
        && EVP_MAC_final(ctx, out, NULL, outlen);
302
0
}
303
304
static int kbkdf_derive(void *vctx, unsigned char *key, size_t keylen,
305
    const OSSL_PARAM params[])
306
0
{
307
0
    KBKDF *ctx = (KBKDF *)vctx;
308
0
    int ret = 0;
309
0
    unsigned char *k_i = NULL;
310
0
    uint32_t l = 0;
311
0
    size_t h = 0;
312
0
    uint64_t counter_max;
313
314
0
    if (!ossl_prov_is_running() || !kbkdf_set_ctx_params(ctx, params))
315
0
        return 0;
316
317
    /* label, context, and iv are permitted to be empty.  Check everything
318
     * else. */
319
0
    if (ctx->ctx_init == NULL) {
320
0
        if (ctx->ki_len == 0 || ctx->ki == NULL) {
321
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
322
0
            return 0;
323
0
        }
324
        /* Could either be missing MAC or missing message digest or missing
325
         * cipher - arbitrarily, I pick this one. */
326
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);
327
0
        return 0;
328
0
    }
329
330
    /* Fail if the output length is zero */
331
0
    if (keylen == 0) {
332
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
333
0
        return 0;
334
0
    }
335
336
0
    if (ctx->is_kmac) {
337
0
        ret = kmac_derive(ctx->ctx_init, key, keylen,
338
0
            ctx->context, ctx->context_len);
339
0
        goto done;
340
0
    }
341
342
0
    h = EVP_MAC_CTX_get_mac_size(ctx->ctx_init);
343
0
    if (h == 0)
344
0
        goto done;
345
346
0
    if (ctx->iv_len != 0 && ctx->iv_len != h) {
347
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);
348
0
        goto done;
349
0
    }
350
351
0
    if (ctx->mode == COUNTER) {
352
        /* Fail if keylen is too large for r */
353
0
        counter_max = (uint64_t)1 << (uint64_t)ctx->r;
354
0
        if ((uint64_t)(keylen / h) >= counter_max) {
355
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
356
0
            goto done;
357
0
        }
358
0
    }
359
360
0
    if (ctx->use_l != 0)
361
0
        l = be32((uint32_t)(keylen * 8));
362
363
0
    k_i = OPENSSL_zalloc(h);
364
0
    if (k_i == NULL)
365
0
        goto done;
366
367
0
    ret = derive(ctx->ctx_init, ctx->mode, ctx->iv, ctx->iv_len, ctx->label,
368
0
        ctx->label_len, ctx->context, ctx->context_len, k_i, h, l,
369
0
        ctx->use_separator, key, keylen, ctx->r);
370
0
done:
371
0
    if (ret != 1)
372
0
        OPENSSL_cleanse(key, keylen);
373
0
    OPENSSL_clear_free(k_i, h);
374
0
    return ret;
375
0
}
376
377
static int kbkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
378
0
{
379
0
    KBKDF *ctx = (KBKDF *)vctx;
380
0
    OSSL_LIB_CTX *libctx;
381
0
    struct kbkdf_set_ctx_params_st p;
382
0
    const char *s;
383
384
0
    if (ctx == NULL || !kbkdf_set_ctx_params_decoder(params, &p))
385
0
        return 0;
386
387
0
    libctx = PROV_LIBCTX_OF(ctx->provctx);
388
389
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k))
390
0
        return 0;
391
392
0
    if (!ossl_prov_macctx_load(&ctx->ctx_init, p.mac, p.cipher,
393
0
            p.digest, p.propq, NULL, NULL, NULL, libctx))
394
0
        return 0;
395
396
0
    if (ctx->ctx_init != NULL) {
397
0
        ctx->is_kmac = 0;
398
0
        if (EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
399
0
                OSSL_MAC_NAME_KMAC128)
400
0
            || EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
401
0
                OSSL_MAC_NAME_KMAC256)) {
402
0
            ctx->is_kmac = 1;
403
0
        } else if (!EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
404
0
                       OSSL_MAC_NAME_HMAC)
405
0
            && !EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
406
0
                OSSL_MAC_NAME_CMAC)) {
407
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MAC);
408
0
            return 0;
409
0
        }
410
0
    }
411
412
0
    if (p.mode != NULL) {
413
0
        if (!OSSL_PARAM_get_utf8_string_ptr(p.mode, &s))
414
0
            return 0;
415
0
        if (OPENSSL_strncasecmp("counter", s, p.mode->data_size) == 0) {
416
0
            ctx->mode = COUNTER;
417
0
        } else if (OPENSSL_strncasecmp("feedback", s, p.mode->data_size) == 0) {
418
0
            ctx->mode = FEEDBACK;
419
0
        } else {
420
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
421
0
            return 0;
422
0
        }
423
0
    }
424
425
0
    if (ossl_param_get1_octet_string_from_param(p.key, &ctx->ki,
426
0
            &ctx->ki_len)
427
0
        == 0)
428
0
        return 0;
429
#ifdef FIPS_MODULE
430
    if (p.key != NULL && !fips_kbkdf_key_check_passed(ctx))
431
        return 0;
432
#endif
433
434
0
    if (ossl_param_get1_octet_string_from_param(p.salt, &ctx->label,
435
0
            &ctx->label_len)
436
0
        == 0)
437
0
        return 0;
438
439
0
    if (ossl_param_get1_concat_octet_string(p.num_info, p.info, &ctx->context,
440
0
            &ctx->context_len)
441
0
        == 0)
442
0
        return 0;
443
444
0
    if (ossl_param_get1_octet_string_from_param(p.seed, &ctx->iv,
445
0
            &ctx->iv_len)
446
0
        == 0)
447
0
        return 0;
448
449
0
    if (p.use_l != NULL && !OSSL_PARAM_get_int(p.use_l, &ctx->use_l))
450
0
        return 0;
451
452
0
    if (p.r != NULL) {
453
0
        int new_r = 0;
454
455
0
        if (!OSSL_PARAM_get_int(p.r, &new_r))
456
0
            return 0;
457
0
        if (new_r != 8 && new_r != 16 && new_r != 24 && new_r != 32)
458
0
            return 0;
459
0
        ctx->r = new_r;
460
0
    }
461
462
0
    if (p.sep != NULL && !OSSL_PARAM_get_int(p.sep, &ctx->use_separator))
463
0
        return 0;
464
465
    /* Set up digest context, if we can. */
466
0
    if (ctx->ctx_init != NULL && ctx->ki_len != 0) {
467
0
        if ((ctx->is_kmac && !kmac_init(ctx->ctx_init, ctx->label, ctx->label_len))
468
0
            || !EVP_MAC_init(ctx->ctx_init, ctx->ki, ctx->ki_len, NULL))
469
0
            return 0;
470
0
    }
471
0
    return 1;
472
0
}
473
474
static const OSSL_PARAM *kbkdf_settable_ctx_params(ossl_unused void *ctx,
475
    ossl_unused void *provctx)
476
0
{
477
0
    return kbkdf_set_ctx_params_list;
478
0
}
479
480
static int kbkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
481
0
{
482
0
    struct kbkdf_get_ctx_params_st p;
483
0
    KBKDF *ctx = (KBKDF *)vctx;
484
485
0
    if (ctx == NULL || !kbkdf_get_ctx_params_decoder(params, &p))
486
0
        return 0;
487
488
    /* KBKDF can produce results as large as you like. */
489
0
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX))
490
0
        return 0;
491
492
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind))
493
0
        return 0;
494
0
    return 1;
495
0
}
496
497
static const OSSL_PARAM *kbkdf_gettable_ctx_params(ossl_unused void *ctx,
498
    ossl_unused void *provctx)
499
0
{
500
0
    return kbkdf_get_ctx_params_list;
501
0
}
502
503
const OSSL_DISPATCH ossl_kdf_kbkdf_functions[] = {
504
    { OSSL_FUNC_KDF_NEWCTX, (void (*)(void))kbkdf_new },
505
    { OSSL_FUNC_KDF_DUPCTX, (void (*)(void))kbkdf_dup },
506
    { OSSL_FUNC_KDF_FREECTX, (void (*)(void))kbkdf_free },
507
    { OSSL_FUNC_KDF_RESET, (void (*)(void))kbkdf_reset },
508
    { OSSL_FUNC_KDF_DERIVE, (void (*)(void))kbkdf_derive },
509
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
510
        (void (*)(void))kbkdf_settable_ctx_params },
511
    { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void (*)(void))kbkdf_set_ctx_params },
512
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
513
        (void (*)(void))kbkdf_gettable_ctx_params },
514
    { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void (*)(void))kbkdf_get_ctx_params },
515
    OSSL_DISPATCH_END,
516
};