Coverage Report

Created: 2023-04-12 06:22

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