Coverage Report

Created: 2025-06-13 06:58

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