Coverage Report

Created: 2025-06-13 06:58

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