Coverage Report

Created: 2025-12-04 06:33

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/params.h"
49
50
144
#define ossl_min(a, b) ((a) < (b)) ? (a) : (b)
51
52
0
#define KBKDF_MAX_INFOS 5
53
54
#include "providers/implementations/kdfs/kbkdf.inc"
55
56
typedef enum {
57
    COUNTER = 0,
58
    FEEDBACK
59
} kbkdf_mode;
60
61
/* Our context structure. */
62
typedef struct {
63
    void *provctx;
64
    kbkdf_mode mode;
65
    EVP_MAC_CTX *ctx_init;
66
67
    /* Names are lowercased versions of those found in SP800-108. */
68
    int r;
69
    unsigned char *ki;
70
    size_t ki_len;
71
    unsigned char *label;
72
    size_t label_len;
73
    unsigned char *context;
74
    size_t context_len;
75
    unsigned char *iv;
76
    size_t iv_len;
77
    int use_l;
78
    int is_kmac;
79
    int use_separator;
80
    OSSL_FIPS_IND_DECLARE
81
} KBKDF;
82
83
/* Definitions needed for typechecking. */
84
static OSSL_FUNC_kdf_newctx_fn kbkdf_new;
85
static OSSL_FUNC_kdf_dupctx_fn kbkdf_dup;
86
static OSSL_FUNC_kdf_freectx_fn kbkdf_free;
87
static OSSL_FUNC_kdf_reset_fn kbkdf_reset;
88
static OSSL_FUNC_kdf_derive_fn kbkdf_derive;
89
static OSSL_FUNC_kdf_settable_ctx_params_fn kbkdf_settable_ctx_params;
90
static OSSL_FUNC_kdf_set_ctx_params_fn kbkdf_set_ctx_params;
91
static OSSL_FUNC_kdf_gettable_ctx_params_fn kbkdf_gettable_ctx_params;
92
static OSSL_FUNC_kdf_get_ctx_params_fn kbkdf_get_ctx_params;
93
94
/* Not all platforms have htobe32(). */
95
static uint32_t be32(uint32_t host)
96
203
{
97
203
    uint32_t big = 0;
98
203
    DECLARE_IS_ENDIAN;
99
100
203
    if (!IS_LITTLE_ENDIAN)
101
0
        return host;
102
103
203
    big |= (host & 0xff000000) >> 24;
104
203
    big |= (host & 0x00ff0000) >> 8;
105
203
    big |= (host & 0x0000ff00) << 8;
106
203
    big |= (host & 0x000000ff) << 24;
107
203
    return big;
108
203
}
109
110
static void init(KBKDF *ctx)
111
600
{
112
600
    ctx->r = 32;
113
600
    ctx->use_l = 1;
114
600
    ctx->use_separator = 1;
115
600
    ctx->is_kmac = 0;
116
600
}
117
118
static void *kbkdf_new(void *provctx)
119
300
{
120
300
    KBKDF *ctx;
121
122
300
    if (!ossl_prov_is_running())
123
0
        return NULL;
124
125
300
    ctx = OPENSSL_zalloc(sizeof(*ctx));
126
300
    if (ctx == NULL)
127
0
        return NULL;
128
129
300
    ctx->provctx = provctx;
130
300
    OSSL_FIPS_IND_INIT(ctx)
131
300
    init(ctx);
132
300
    return ctx;
133
300
}
134
135
static void kbkdf_free(void *vctx)
136
300
{
137
300
    KBKDF *ctx = (KBKDF *)vctx;
138
139
300
    if (ctx != NULL) {
140
300
        kbkdf_reset(ctx);
141
300
        OPENSSL_free(ctx);
142
300
    }
143
300
}
144
145
static void kbkdf_reset(void *vctx)
146
300
{
147
300
    KBKDF *ctx = (KBKDF *)vctx;
148
300
    void *provctx = ctx->provctx;
149
150
300
    EVP_MAC_CTX_free(ctx->ctx_init);
151
300
    OPENSSL_clear_free(ctx->context, ctx->context_len);
152
300
    OPENSSL_clear_free(ctx->label, ctx->label_len);
153
300
    OPENSSL_clear_free(ctx->ki, ctx->ki_len);
154
300
    OPENSSL_clear_free(ctx->iv, ctx->iv_len);
155
300
    memset(ctx, 0, sizeof(*ctx));
156
300
    ctx->provctx = provctx;
157
300
    init(ctx);
158
300
}
159
160
static void *kbkdf_dup(void *vctx)
161
0
{
162
0
    const KBKDF *src = (const KBKDF *)vctx;
163
0
    KBKDF *dest;
164
165
0
    dest = kbkdf_new(src->provctx);
166
0
    if (dest != NULL) {
167
0
        dest->ctx_init = EVP_MAC_CTX_dup(src->ctx_init);
168
0
        if (dest->ctx_init == NULL
169
0
                || !ossl_prov_memdup(src->ki, src->ki_len,
170
0
                                     &dest->ki, &dest->ki_len)
171
0
                || !ossl_prov_memdup(src->label, src->label_len,
172
0
                                     &dest->label, &dest->label_len)
173
0
                || !ossl_prov_memdup(src->context, src->context_len,
174
0
                                     &dest->context, &dest->context_len)
175
0
                || !ossl_prov_memdup(src->iv, src->iv_len,
176
0
                                     &dest->iv, &dest->iv_len))
177
0
            goto err;
178
0
        dest->mode = src->mode;
179
0
        dest->r = src->r;
180
0
        dest->use_l = src->use_l;
181
0
        dest->use_separator = src->use_separator;
182
0
        dest->is_kmac = src->is_kmac;
183
0
        OSSL_FIPS_IND_COPY(dest, src)
184
0
    }
185
0
    return dest;
186
187
0
 err:
188
0
    kbkdf_free(dest);
189
0
    return NULL;
190
0
}
191
192
#ifdef FIPS_MODULE
193
static int fips_kbkdf_key_check_passed(KBKDF *ctx)
194
{
195
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
196
    int key_approved = ossl_kdf_check_key_size(ctx->ki_len);
197
198
    if (!key_approved) {
199
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,
200
                                         libctx, "KBKDF", "Key size",
201
                                         ossl_fips_config_kbkdf_key_check)) {
202
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
203
            return 0;
204
        }
205
    }
206
    return 1;
207
}
208
#endif
209
210
/* SP800-108 section 5.1 or section 5.2 depending on mode. */
211
static int derive(EVP_MAC_CTX *ctx_init, kbkdf_mode mode, unsigned char *iv,
212
                  size_t iv_len, unsigned char *label, size_t label_len,
213
                  unsigned char *context, size_t context_len,
214
                  unsigned char *k_i, size_t h, uint32_t l, int has_separator,
215
                  unsigned char *ko, size_t ko_len, int r)
216
93
{
217
93
    int ret = 0;
218
93
    EVP_MAC_CTX *ctx = NULL;
219
93
    size_t written = 0, to_write, k_i_len = iv_len;
220
93
    const unsigned char zero = 0;
221
93
    uint32_t counter, i;
222
    /*
223
     * From SP800-108:
224
     * The fixed input data is a concatenation of a Label,
225
     * a separation indicator 0x00, the Context, and L.
226
     * One or more of these fixed input data fields may be omitted.
227
     *
228
     * has_separator == 0 means that the separator is omitted.
229
     * Passing a value of l == 0 means that L is omitted.
230
     * The Context and L are omitted automatically if a NULL buffer is passed.
231
     */
232
93
    int has_l = (l != 0);
233
234
    /* Setup K(0) for feedback mode. */
235
93
    if (iv_len > 0)
236
15
        memcpy(k_i, iv, iv_len);
237
238
237
    for (counter = 1; written < ko_len; counter++) {
239
144
        i = be32(counter);
240
241
144
        ctx = EVP_MAC_CTX_dup(ctx_init);
242
144
        if (ctx == NULL)
243
0
            goto done;
244
245
        /* Perform feedback, if appropriate. */
246
144
        if (mode == FEEDBACK && !EVP_MAC_update(ctx, k_i, k_i_len))
247
0
            goto done;
248
249
144
        if (!EVP_MAC_update(ctx, 4 - (r / 8) + (unsigned char *)&i, r / 8)
250
144
            || !EVP_MAC_update(ctx, label, label_len)
251
144
            || (has_separator && !EVP_MAC_update(ctx, &zero, 1))
252
144
            || !EVP_MAC_update(ctx, context, context_len)
253
144
            || (has_l && !EVP_MAC_update(ctx, (unsigned char *)&l, 4))
254
144
            || !EVP_MAC_final(ctx, k_i, NULL, h))
255
0
            goto done;
256
257
144
        to_write = ko_len - written;
258
144
        memcpy(ko + written, k_i, ossl_min(to_write, h));
259
144
        written += h;
260
261
144
        k_i_len = h;
262
144
        EVP_MAC_CTX_free(ctx);
263
144
        ctx = NULL;
264
144
    }
265
266
93
    ret = 1;
267
93
done:
268
93
    EVP_MAC_CTX_free(ctx);
269
93
    return ret;
270
93
}
271
272
/* This must be run before the key is set */
273
static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom, size_t customlen)
274
41
{
275
41
    OSSL_PARAM params[2];
276
277
41
    if (custom == NULL || customlen == 0)
278
9
        return 1;
279
32
    params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
280
32
                                                  (void *)custom, customlen);
281
32
    params[1] = OSSL_PARAM_construct_end();
282
32
    return EVP_MAC_CTX_set_params(ctx, params) > 0;
283
41
}
284
285
static int kmac_derive(EVP_MAC_CTX *ctx, unsigned char *out, size_t outlen,
286
                       const unsigned char *context, size_t contextlen)
287
35
{
288
35
    OSSL_PARAM params[2];
289
290
35
    params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &outlen);
291
35
    params[1] = OSSL_PARAM_construct_end();
292
35
    return EVP_MAC_CTX_set_params(ctx, params) > 0
293
35
           && EVP_MAC_update(ctx, context, contextlen)
294
22
           && EVP_MAC_final(ctx, out, NULL, outlen);
295
35
}
296
297
static int kbkdf_derive(void *vctx, unsigned char *key, size_t keylen,
298
                        const OSSL_PARAM params[])
299
200
{
300
200
    KBKDF *ctx = (KBKDF *)vctx;
301
200
    int ret = 0;
302
200
    unsigned char *k_i = NULL;
303
200
    uint32_t l = 0;
304
200
    size_t h = 0;
305
200
    uint64_t counter_max;
306
307
200
    if (!ossl_prov_is_running() || !kbkdf_set_ctx_params(ctx, params))
308
0
        return 0;
309
310
    /* label, context, and iv are permitted to be empty.  Check everything
311
     * else. */
312
200
    if (ctx->ctx_init == NULL) {
313
0
        if (ctx->ki_len == 0 || ctx->ki == NULL) {
314
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
315
0
            return 0;
316
0
        }
317
        /* Could either be missing MAC or missing message digest or missing
318
         * cipher - arbitrarily, I pick this one. */
319
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);
320
0
        return 0;
321
0
    }
322
323
    /* Fail if the output length is zero */
324
200
    if (keylen == 0) {
325
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
326
0
        return 0;
327
0
    }
328
329
200
    if (ctx->is_kmac) {
330
35
        ret = kmac_derive(ctx->ctx_init, key, keylen,
331
35
                          ctx->context, ctx->context_len);
332
35
        goto done;
333
35
    }
334
335
165
    h = EVP_MAC_CTX_get_mac_size(ctx->ctx_init);
336
165
    if (h == 0)
337
7
        goto done;
338
339
158
    if (ctx->iv_len != 0 && ctx->iv_len != h) {
340
65
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);
341
65
        goto done;
342
65
    }
343
344
93
    if (ctx->mode == COUNTER) {
345
        /* Fail if keylen is too large for r */
346
93
        counter_max = (uint64_t)1 << (uint64_t)ctx->r;
347
93
        if ((uint64_t)(keylen / h) >= counter_max) {
348
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
349
0
            goto done;
350
0
        }
351
93
    }
352
353
93
    if (ctx->use_l != 0)
354
59
        l = be32((uint32_t)(keylen * 8));
355
356
93
    k_i = OPENSSL_zalloc(h);
357
93
    if (k_i == NULL)
358
0
        goto done;
359
360
93
    ret = derive(ctx->ctx_init, ctx->mode, ctx->iv, ctx->iv_len, ctx->label,
361
93
                 ctx->label_len, ctx->context, ctx->context_len, k_i, h, l,
362
93
                 ctx->use_separator, key, keylen, ctx->r);
363
200
done:
364
200
    if (ret != 1)
365
91
        OPENSSL_cleanse(key, keylen);
366
200
    OPENSSL_clear_free(k_i, h);
367
200
    return ret;
368
93
}
369
370
static int kbkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
371
203
{
372
203
    KBKDF *ctx = (KBKDF *)vctx;
373
203
    OSSL_LIB_CTX *libctx;
374
203
    struct kbkdf_set_ctx_params_st p;
375
203
    const char *s;
376
377
203
    if (ctx == NULL || !kbkdf_set_ctx_params_decoder(params, &p))
378
0
        return 0;
379
380
203
    libctx = PROV_LIBCTX_OF(ctx->provctx);
381
382
203
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k))
383
0
        return 0;
384
385
203
    if (!ossl_prov_macctx_load(&ctx->ctx_init, p.mac, p.cipher,
386
203
                               p.digest, p.propq, p.engine,
387
203
                               NULL, NULL, NULL, libctx))
388
34
        return 0;
389
390
169
    if (ctx->ctx_init != NULL) {
391
169
        ctx->is_kmac = 0;
392
169
        if (EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
393
169
                         OSSL_MAC_NAME_KMAC128)
394
134
            || EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
395
134
                            OSSL_MAC_NAME_KMAC256)) {
396
47
            ctx->is_kmac = 1;
397
122
        } else if (!EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
398
122
                                 OSSL_MAC_NAME_HMAC)
399
7
                   && !EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
400
7
                                    OSSL_MAC_NAME_CMAC)) {
401
7
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MAC);
402
7
            return 0;
403
7
        }
404
169
    }
405
406
162
    if (p.mode != NULL) {
407
91
        if (!OSSL_PARAM_get_utf8_string_ptr(p.mode, &s))
408
0
            return 0;
409
91
        if (OPENSSL_strncasecmp("counter", s, p.mode->data_size) == 0) {
410
83
            ctx->mode = COUNTER;
411
83
        } else if (OPENSSL_strncasecmp("feedback", s, p.mode->data_size) == 0) {
412
0
            ctx->mode = FEEDBACK;
413
8
        } else {
414
8
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
415
8
            return 0;
416
8
        }
417
91
    }
418
419
154
    if (ossl_param_get1_octet_string_from_param(p.key, &ctx->ki,
420
154
                                                &ctx->ki_len) == 0)
421
0
        return 0;
422
#ifdef FIPS_MODULE
423
    if (p.key != NULL && !fips_kbkdf_key_check_passed(ctx))
424
        return 0;
425
#endif
426
427
154
    if (ossl_param_get1_octet_string_from_param(p.salt, &ctx->label,
428
154
                                                &ctx->label_len) == 0)
429
0
        return 0;
430
431
154
    if (ossl_param_get1_concat_octet_string(p.num_info, p.info, &ctx->context,
432
154
                                            &ctx->context_len) == 0)
433
0
        return 0;
434
435
154
    if (ossl_param_get1_octet_string_from_param(p.seed, &ctx->iv,
436
154
                                                &ctx->iv_len) == 0)
437
0
        return 0;
438
439
154
    if (p.use_l != NULL && !OSSL_PARAM_get_int(p.use_l, &ctx->use_l))
440
0
        return 0;
441
442
154
    if (p.r != NULL) {
443
83
        int new_r = 0;
444
445
83
        if (!OSSL_PARAM_get_int(p.r, &new_r))
446
0
            return 0;
447
83
        if (new_r != 8 && new_r != 16 && new_r != 24 && new_r != 32)
448
0
            return 0;
449
83
        ctx->r = new_r;
450
83
    }
451
452
154
    if (p.sep != NULL && !OSSL_PARAM_get_int(p.sep, &ctx->use_separator))
453
0
        return 0;
454
455
    /* Set up digest context, if we can. */
456
154
    if (ctx->ctx_init != NULL && ctx->ki_len != 0) {
457
128
        if ((ctx->is_kmac && !kmac_init(ctx->ctx_init, ctx->label, ctx->label_len))
458
125
            || !EVP_MAC_init(ctx->ctx_init, ctx->ki, ctx->ki_len, NULL))
459
12
            return 0;
460
128
    }
461
142
    return 1;
462
154
}
463
464
static const OSSL_PARAM *kbkdf_settable_ctx_params(ossl_unused void *ctx,
465
                                                   ossl_unused void *provctx)
466
300
{
467
300
    return kbkdf_set_ctx_params_list;
468
300
}
469
470
static int kbkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
471
0
{
472
0
    struct kbkdf_get_ctx_params_st p;
473
0
    KBKDF *ctx = (KBKDF *)vctx;
474
475
0
    if (ctx == NULL || !kbkdf_get_ctx_params_decoder(params, &p))
476
0
        return 0;
477
478
    /* KBKDF can produce results as large as you like. */
479
0
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX))
480
0
        return 0;
481
482
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind))
483
0
        return 0;
484
0
    return 1;
485
0
}
486
487
static const OSSL_PARAM *kbkdf_gettable_ctx_params(ossl_unused void *ctx,
488
                                                   ossl_unused void *provctx)
489
0
{
490
0
    return kbkdf_get_ctx_params_list;
491
0
}
492
493
const OSSL_DISPATCH ossl_kdf_kbkdf_functions[] = {
494
    { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kbkdf_new },
495
    { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kbkdf_dup },
496
    { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kbkdf_free },
497
    { OSSL_FUNC_KDF_RESET, (void(*)(void))kbkdf_reset },
498
    { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kbkdf_derive },
499
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
500
      (void(*)(void))kbkdf_settable_ctx_params },
501
    { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kbkdf_set_ctx_params },
502
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
503
      (void(*)(void))kbkdf_gettable_ctx_params },
504
    { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kbkdf_get_ctx_params },
505
    OSSL_DISPATCH_END,
506
};