Coverage Report

Created: 2025-06-13 06:56

/src/openssl/providers/implementations/kdfs/sskdf.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
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
 * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final
13
 * Section 4.1.
14
 *
15
 * The Single Step KDF algorithm is given by:
16
 *
17
 * Result(0) = empty bit string (i.e., the null string).
18
 * For i = 1 to reps, do the following:
19
 *   Increment counter by 1.
20
 *   Result(i) = Result(i - 1) || H(counter || Z || FixedInfo).
21
 * DKM = LeftmostBits(Result(reps), L))
22
 *
23
 * NOTES:
24
 *   Z is a shared secret required to produce the derived key material.
25
 *   counter is a 4 byte buffer.
26
 *   FixedInfo is a bit string containing context specific data.
27
 *   DKM is the output derived key material.
28
 *   L is the required size of the DKM.
29
 *   reps = [L / H_outputBits]
30
 *   H(x) is the auxiliary function that can be either a hash, HMAC or KMAC.
31
 *   H_outputBits is the length of the output of the auxiliary function H(x).
32
 *
33
 * Currently there is not a comprehensive list of test vectors for this
34
 * algorithm, especially for H(x) = HMAC and H(x) = KMAC.
35
 * Test vectors for H(x) = Hash are indirectly used by CAVS KAS tests.
36
 */
37
#include <stdlib.h>
38
#include <stdarg.h>
39
#include <string.h>
40
#include <openssl/hmac.h>
41
#include <openssl/evp.h>
42
#include <openssl/kdf.h>
43
#include <openssl/core_names.h>
44
#include <openssl/params.h>
45
#include <openssl/proverr.h>
46
#include "internal/cryptlib.h"
47
#include "internal/numbers.h"
48
#include "crypto/evp.h"
49
#include "prov/provider_ctx.h"
50
#include "prov/providercommon.h"
51
#include "prov/implementations.h"
52
#include "prov/provider_util.h"
53
#include "prov/securitycheck.h"
54
#include "internal/params.h"
55
56
typedef struct {
57
    void *provctx;
58
    EVP_MAC_CTX *macctx;         /* H(x) = HMAC_hash OR H(x) = KMAC */
59
    PROV_DIGEST digest;          /* H(x) = hash(x) */
60
    unsigned char *secret;
61
    size_t secret_len;
62
    unsigned char *info;
63
    size_t info_len;
64
    unsigned char *salt;
65
    size_t salt_len;
66
    size_t out_len; /* optional KMAC parameter */
67
    int is_kmac;
68
    OSSL_FIPS_IND_DECLARE
69
} KDF_SSKDF;
70
71
0
#define SSKDF_MAX_INLEN (1<<30)
72
0
#define SSKDF_KMAC128_DEFAULT_SALT_SIZE (168 - 4)
73
0
#define SSKDF_KMAC256_DEFAULT_SALT_SIZE (136 - 4)
74
75
/* KMAC uses a Customisation string of 'KDF' */
76
static const unsigned char kmac_custom_str[] = { 0x4B, 0x44, 0x46 };
77
78
static OSSL_FUNC_kdf_newctx_fn sskdf_new;
79
static OSSL_FUNC_kdf_dupctx_fn sskdf_dup;
80
static OSSL_FUNC_kdf_freectx_fn sskdf_free;
81
static OSSL_FUNC_kdf_reset_fn sskdf_reset;
82
static OSSL_FUNC_kdf_derive_fn sskdf_derive;
83
static OSSL_FUNC_kdf_settable_ctx_params_fn sskdf_settable_ctx_params;
84
static OSSL_FUNC_kdf_set_ctx_params_fn sskdf_set_ctx_params;
85
static OSSL_FUNC_kdf_gettable_ctx_params_fn sskdf_gettable_ctx_params;
86
static OSSL_FUNC_kdf_get_ctx_params_fn sskdf_get_ctx_params;
87
static OSSL_FUNC_kdf_derive_fn x963kdf_derive;
88
static OSSL_FUNC_kdf_settable_ctx_params_fn x963kdf_settable_ctx_params;
89
static OSSL_FUNC_kdf_set_ctx_params_fn x963kdf_set_ctx_params;
90
static OSSL_FUNC_kdf_gettable_ctx_params_fn x963kdf_gettable_ctx_params;
91
static OSSL_FUNC_kdf_get_ctx_params_fn x963kdf_get_ctx_params;
92
93
/* Settable context parameters that are common across SSKDF and X963 KDF */
94
#define SSKDF_COMMON_SETTABLES                                      \
95
0
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),        \
96
0
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),           \
97
0
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),          \
98
0
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),     \
99
0
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),         \
100
0
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),            \
101
0
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),          \
102
0
    OSSL_PARAM_size_t(OSSL_KDF_PARAM_MAC_SIZE, NULL)
103
104
/* Gettable context parameters that are common across SSKDF and X963 KDF */
105
#define SSKDF_COMMON_GETTABLES                                          \
106
0
    OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL)
107
108
/*
109
 * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final
110
 * Section 4. One-Step Key Derivation using H(x) = hash(x)
111
 * Note: X9.63 also uses this code with the only difference being that the
112
 * counter is appended to the secret 'z'.
113
 * i.e.
114
 *   result[i] = Hash(counter || z || info) for One Step OR
115
 *   result[i] = Hash(z || counter || info) for X9.63.
116
 */
117
static int SSKDF_hash_kdm(const EVP_MD *kdf_md,
118
                          const unsigned char *z, size_t z_len,
119
                          const unsigned char *info, size_t info_len,
120
                          unsigned int append_ctr,
121
                          unsigned char *derived_key, size_t derived_key_len)
122
0
{
123
0
    int ret = 0, hlen;
124
0
    size_t counter, out_len, len = derived_key_len;
125
0
    unsigned char c[4];
126
0
    unsigned char mac[EVP_MAX_MD_SIZE];
127
0
    unsigned char *out = derived_key;
128
0
    EVP_MD_CTX *ctx = NULL, *ctx_init = NULL;
129
130
0
    if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN
131
0
            || derived_key_len > SSKDF_MAX_INLEN
132
0
            || derived_key_len == 0)
133
0
        return 0;
134
135
0
    hlen = EVP_MD_get_size(kdf_md);
136
0
    if (hlen <= 0)
137
0
        return 0;
138
0
    out_len = (size_t)hlen;
139
140
0
    ctx = EVP_MD_CTX_create();
141
0
    ctx_init = EVP_MD_CTX_create();
142
0
    if (ctx == NULL || ctx_init == NULL)
143
0
        goto end;
144
145
0
    if (!EVP_DigestInit(ctx_init, kdf_md))
146
0
        goto end;
147
148
0
    for (counter = 1;; counter++) {
149
0
        c[0] = (unsigned char)((counter >> 24) & 0xff);
150
0
        c[1] = (unsigned char)((counter >> 16) & 0xff);
151
0
        c[2] = (unsigned char)((counter >> 8) & 0xff);
152
0
        c[3] = (unsigned char)(counter & 0xff);
153
154
0
        if (!(EVP_MD_CTX_copy_ex(ctx, ctx_init)
155
0
                && (append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c)))
156
0
                && EVP_DigestUpdate(ctx, z, z_len)
157
0
                && (!append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c)))
158
0
                && EVP_DigestUpdate(ctx, info, info_len)))
159
0
            goto end;
160
0
        if (len >= out_len) {
161
0
            if (!EVP_DigestFinal_ex(ctx, out, NULL))
162
0
                goto end;
163
0
            out += out_len;
164
0
            len -= out_len;
165
0
            if (len == 0)
166
0
                break;
167
0
        } else {
168
0
            if (!EVP_DigestFinal_ex(ctx, mac, NULL))
169
0
                goto end;
170
0
            memcpy(out, mac, len);
171
0
            break;
172
0
        }
173
0
    }
174
0
    ret = 1;
175
0
end:
176
0
    EVP_MD_CTX_destroy(ctx);
177
0
    EVP_MD_CTX_destroy(ctx_init);
178
0
    OPENSSL_cleanse(mac, sizeof(mac));
179
0
    return ret;
180
0
}
181
182
static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom,
183
                     size_t custom_len, size_t kmac_out_len,
184
                     size_t derived_key_len, unsigned char **out)
185
0
{
186
0
    OSSL_PARAM params[2];
187
188
    /* Only KMAC has custom data - so return if not KMAC */
189
0
    if (custom == NULL)
190
0
        return 1;
191
192
0
    params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
193
0
                                                  (void *)custom, custom_len);
194
0
    params[1] = OSSL_PARAM_construct_end();
195
196
0
    if (!EVP_MAC_CTX_set_params(ctx, params))
197
0
        return 0;
198
199
    /* By default only do one iteration if kmac_out_len is not specified */
200
0
    if (kmac_out_len == 0)
201
0
        kmac_out_len = derived_key_len;
202
    /* otherwise check the size is valid */
203
0
    else if (!(kmac_out_len == derived_key_len
204
0
            || kmac_out_len == 20
205
0
            || kmac_out_len == 28
206
0
            || kmac_out_len == 32
207
0
            || kmac_out_len == 48
208
0
            || kmac_out_len == 64))
209
0
        return 0;
210
211
0
    params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE,
212
0
                                            &kmac_out_len);
213
214
0
    if (EVP_MAC_CTX_set_params(ctx, params) <= 0)
215
0
        return 0;
216
217
    /*
218
     * For kmac the output buffer can be larger than EVP_MAX_MD_SIZE: so
219
     * alloc a buffer for this case.
220
     */
221
0
    if (kmac_out_len > EVP_MAX_MD_SIZE) {
222
0
        *out = OPENSSL_zalloc(kmac_out_len);
223
0
        if (*out == NULL)
224
0
            return 0;
225
0
    }
226
0
    return 1;
227
0
}
228
229
/*
230
 * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final
231
 * Section 4. One-Step Key Derivation using MAC: i.e either
232
 *     H(x) = HMAC-hash(salt, x) OR
233
 *     H(x) = KMAC#(salt, x, outbits, CustomString='KDF')
234
 */
235
static int SSKDF_mac_kdm(EVP_MAC_CTX *ctx_init,
236
                         const unsigned char *kmac_custom,
237
                         size_t kmac_custom_len, size_t kmac_out_len,
238
                         const unsigned char *salt, size_t salt_len,
239
                         const unsigned char *z, size_t z_len,
240
                         const unsigned char *info, size_t info_len,
241
                         unsigned char *derived_key, size_t derived_key_len)
242
0
{
243
0
    int ret = 0;
244
0
    size_t counter, out_len, len;
245
0
    unsigned char c[4];
246
0
    unsigned char mac_buf[EVP_MAX_MD_SIZE];
247
0
    unsigned char *out = derived_key;
248
0
    EVP_MAC_CTX *ctx = NULL;
249
0
    unsigned char *mac = mac_buf, *kmac_buffer = NULL;
250
251
0
    if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN
252
0
            || derived_key_len > SSKDF_MAX_INLEN
253
0
            || derived_key_len == 0)
254
0
        return 0;
255
256
0
    if (!kmac_init(ctx_init, kmac_custom, kmac_custom_len, kmac_out_len,
257
0
                   derived_key_len, &kmac_buffer))
258
0
        goto end;
259
0
    if (kmac_buffer != NULL)
260
0
        mac = kmac_buffer;
261
262
0
    if (!EVP_MAC_init(ctx_init, salt, salt_len, NULL))
263
0
        goto end;
264
265
0
    out_len = EVP_MAC_CTX_get_mac_size(ctx_init); /* output size */
266
0
    if (out_len <= 0 || (mac == mac_buf && out_len > sizeof(mac_buf)))
267
0
        goto end;
268
0
    len = derived_key_len;
269
270
0
    for (counter = 1;; counter++) {
271
0
        c[0] = (unsigned char)((counter >> 24) & 0xff);
272
0
        c[1] = (unsigned char)((counter >> 16) & 0xff);
273
0
        c[2] = (unsigned char)((counter >> 8) & 0xff);
274
0
        c[3] = (unsigned char)(counter & 0xff);
275
276
0
        ctx = EVP_MAC_CTX_dup(ctx_init);
277
0
        if (!(ctx != NULL
278
0
                && EVP_MAC_update(ctx, c, sizeof(c))
279
0
                && EVP_MAC_update(ctx, z, z_len)
280
0
                && EVP_MAC_update(ctx, info, info_len)))
281
0
            goto end;
282
0
        if (len >= out_len) {
283
0
            if (!EVP_MAC_final(ctx, out, NULL, len))
284
0
                goto end;
285
0
            out += out_len;
286
0
            len -= out_len;
287
0
            if (len == 0)
288
0
                break;
289
0
        } else {
290
0
            if (!EVP_MAC_final(ctx, mac, NULL, out_len))
291
0
                goto end;
292
0
            memcpy(out, mac, len);
293
0
            break;
294
0
        }
295
0
        EVP_MAC_CTX_free(ctx);
296
0
        ctx = NULL;
297
0
    }
298
0
    ret = 1;
299
0
end:
300
0
    if (kmac_buffer != NULL)
301
0
        OPENSSL_clear_free(kmac_buffer, kmac_out_len);
302
0
    else
303
0
        OPENSSL_cleanse(mac_buf, sizeof(mac_buf));
304
305
0
    EVP_MAC_CTX_free(ctx);
306
0
    return ret;
307
0
}
308
309
static void *sskdf_new(void *provctx)
310
0
{
311
0
    KDF_SSKDF *ctx;
312
313
0
    if (!ossl_prov_is_running())
314
0
        return NULL;
315
316
0
    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL) {
317
0
        ctx->provctx = provctx;
318
0
        OSSL_FIPS_IND_INIT(ctx)
319
0
    }
320
0
    return ctx;
321
0
}
322
323
static void sskdf_reset(void *vctx)
324
0
{
325
0
    KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
326
0
    void *provctx = ctx->provctx;
327
328
0
    EVP_MAC_CTX_free(ctx->macctx);
329
0
    ossl_prov_digest_reset(&ctx->digest);
330
0
    OPENSSL_clear_free(ctx->secret, ctx->secret_len);
331
0
    OPENSSL_clear_free(ctx->info, ctx->info_len);
332
0
    OPENSSL_clear_free(ctx->salt, ctx->salt_len);
333
0
    memset(ctx, 0, sizeof(*ctx));
334
0
    ctx->provctx = provctx;
335
0
}
336
337
static void sskdf_free(void *vctx)
338
0
{
339
0
    KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
340
341
0
    if (ctx != NULL) {
342
0
        sskdf_reset(ctx);
343
0
        OPENSSL_free(ctx);
344
0
    }
345
0
}
346
347
static void *sskdf_dup(void *vctx)
348
0
{
349
0
    const KDF_SSKDF *src = (const KDF_SSKDF *)vctx;
350
0
    KDF_SSKDF *dest;
351
352
0
    dest = sskdf_new(src->provctx);
353
0
    if (dest != NULL) {
354
0
        if (src->macctx != NULL) {
355
0
            dest->macctx = EVP_MAC_CTX_dup(src->macctx);
356
0
            if (dest->macctx == NULL)
357
0
                goto err;
358
0
        }
359
0
        if (!ossl_prov_memdup(src->info, src->info_len,
360
0
                              &dest->info, &dest->info_len)
361
0
                || !ossl_prov_memdup(src->salt, src->salt_len,
362
0
                                     &dest->salt , &dest->salt_len)
363
0
                || !ossl_prov_memdup(src->secret, src->secret_len,
364
0
                                     &dest->secret, &dest->secret_len)
365
0
                || !ossl_prov_digest_copy(&dest->digest, &src->digest))
366
0
            goto err;
367
0
        dest->out_len = src->out_len;
368
0
        dest->is_kmac = src->is_kmac;
369
0
        OSSL_FIPS_IND_COPY(dest, src)
370
0
    }
371
0
    return dest;
372
373
0
 err:
374
0
    sskdf_free(dest);
375
0
    return NULL;
376
0
}
377
378
static size_t sskdf_size(KDF_SSKDF *ctx)
379
0
{
380
0
    int len;
381
0
    const EVP_MD *md = NULL;
382
383
0
    if (ctx->is_kmac)
384
0
        return SIZE_MAX;
385
386
0
    md = ossl_prov_digest_md(&ctx->digest);
387
0
    if (md == NULL) {
388
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
389
0
        return 0;
390
0
    }
391
0
    len = EVP_MD_get_size(md);
392
0
    return (len <= 0) ? 0 : (size_t)len;
393
0
}
394
395
#ifdef FIPS_MODULE
396
static int fips_sskdf_key_check_passed(KDF_SSKDF *ctx)
397
{
398
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
399
    int key_approved = ossl_kdf_check_key_size(ctx->secret_len);
400
401
    if (!key_approved) {
402
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,
403
                                         libctx, "SSKDF", "Key size",
404
                                         ossl_fips_config_sskdf_key_check)) {
405
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
406
            return 0;
407
        }
408
    }
409
    return 1;
410
}
411
#endif
412
413
static int sskdf_derive(void *vctx, unsigned char *key, size_t keylen,
414
                        const OSSL_PARAM params[])
415
0
{
416
0
    KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
417
0
    const EVP_MD *md;
418
419
0
    if (!ossl_prov_is_running() || !sskdf_set_ctx_params(ctx, params))
420
0
        return 0;
421
0
    if (ctx->secret == NULL) {
422
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
423
0
        return 0;
424
0
    }
425
426
0
    md = ossl_prov_digest_md(&ctx->digest);
427
428
0
    if (ctx->macctx != NULL) {
429
        /* H(x) = KMAC or H(x) = HMAC */
430
0
        int ret;
431
0
        const unsigned char *custom = NULL;
432
0
        size_t custom_len = 0;
433
0
        int default_salt_len;
434
0
        EVP_MAC *mac = EVP_MAC_CTX_get0_mac(ctx->macctx);
435
436
0
        if (EVP_MAC_is_a(mac, OSSL_MAC_NAME_HMAC)) {
437
            /* H(x) = HMAC(x, salt, hash) */
438
0
            if (md == NULL) {
439
0
                ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
440
0
                return 0;
441
0
            }
442
0
            default_salt_len = EVP_MD_get_size(md);
443
0
            if (default_salt_len <= 0)
444
0
                return 0;
445
0
        } else if (ctx->is_kmac) {
446
            /* H(x) = KMACzzz(x, salt, custom) */
447
0
            custom = kmac_custom_str;
448
0
            custom_len = sizeof(kmac_custom_str);
449
0
            if (EVP_MAC_is_a(mac, OSSL_MAC_NAME_KMAC128))
450
0
                default_salt_len = SSKDF_KMAC128_DEFAULT_SALT_SIZE;
451
0
            else
452
0
                default_salt_len = SSKDF_KMAC256_DEFAULT_SALT_SIZE;
453
0
        } else {
454
0
            ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_MAC_TYPE);
455
0
            return 0;
456
0
        }
457
        /* If no salt is set then use a default_salt of zeros */
458
0
        if (ctx->salt == NULL || ctx->salt_len <= 0) {
459
0
            ctx->salt = OPENSSL_zalloc(default_salt_len);
460
0
            if (ctx->salt == NULL)
461
0
                return 0;
462
0
            ctx->salt_len = default_salt_len;
463
0
        }
464
0
        ret = SSKDF_mac_kdm(ctx->macctx,
465
0
                            custom, custom_len, ctx->out_len,
466
0
                            ctx->salt, ctx->salt_len,
467
0
                            ctx->secret, ctx->secret_len,
468
0
                            ctx->info, ctx->info_len, key, keylen);
469
0
        return ret;
470
0
    } else {
471
        /* H(x) = hash */
472
0
        if (md == NULL) {
473
0
            ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
474
0
            return 0;
475
0
        }
476
0
        return SSKDF_hash_kdm(md, ctx->secret, ctx->secret_len,
477
0
                              ctx->info, ctx->info_len, 0, key, keylen);
478
0
    }
479
0
}
480
481
#ifdef FIPS_MODULE
482
static int fips_x963kdf_digest_check_passed(KDF_SSKDF *ctx, const EVP_MD *md)
483
{
484
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
485
    /*
486
     * Perform digest check
487
     *
488
     * X963KDF is a KDF defined in ANSI-X9.63. According to ACVP specification
489
     * section 7.3.1, only SHA-2 and SHA-3 can be regarded as valid hash
490
     * functions.
491
     */
492
    int digest_unapproved = (ctx->is_kmac != 1) && EVP_MD_is_a(md, SN_sha1);
493
494
    if (digest_unapproved) {
495
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,
496
                                         libctx, "X963KDF", "Digest",
497
                                         ossl_fips_config_x963kdf_digest_check)) {
498
            ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
499
            return 0;
500
        }
501
    }
502
    return 1;
503
}
504
505
static int fips_x963kdf_key_check_passed(KDF_SSKDF *ctx)
506
{
507
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
508
    int key_approved = ossl_kdf_check_key_size(ctx->secret_len);
509
510
    if (!key_approved) {
511
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE1,
512
                                         libctx, "X963KDF", "Key size",
513
                                         ossl_fips_config_x963kdf_key_check)) {
514
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
515
            return 0;
516
        }
517
    }
518
    return 1;
519
}
520
#endif
521
522
static int x963kdf_derive(void *vctx, unsigned char *key, size_t keylen,
523
                          const OSSL_PARAM params[])
524
0
{
525
0
    KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
526
0
    const EVP_MD *md;
527
528
0
    if (!ossl_prov_is_running() || !x963kdf_set_ctx_params(ctx, params))
529
0
        return 0;
530
531
0
    if (ctx->secret == NULL) {
532
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
533
0
        return 0;
534
0
    }
535
536
0
    if (ctx->macctx != NULL) {
537
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED);
538
0
        return 0;
539
0
    }
540
541
    /* H(x) = hash */
542
0
    md = ossl_prov_digest_md(&ctx->digest);
543
0
    if (md == NULL) {
544
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
545
0
        return 0;
546
0
    }
547
548
0
    return SSKDF_hash_kdm(md, ctx->secret, ctx->secret_len,
549
0
                          ctx->info, ctx->info_len, 1, key, keylen);
550
0
}
551
552
static int sskdf_common_set_ctx_params(KDF_SSKDF *ctx, const OSSL_PARAM params[])
553
0
{
554
0
    const OSSL_PARAM *p;
555
0
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
556
0
    const EVP_MD *md = NULL;
557
0
    size_t sz;
558
0
    int r;
559
560
0
    if (ossl_param_is_empty(params))
561
0
        return 1;
562
563
0
    if (!ossl_prov_macctx_load_from_params(&ctx->macctx, params,
564
0
                                           NULL, NULL, NULL, libctx))
565
0
        return 0;
566
0
    if (ctx->macctx != NULL) {
567
0
         if (EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->macctx),
568
0
                          OSSL_MAC_NAME_KMAC128)
569
0
             || EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->macctx),
570
0
                             OSSL_MAC_NAME_KMAC256)) {
571
0
             ctx->is_kmac = 1;
572
0
         }
573
0
    }
574
575
0
    if (OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST) != NULL) {
576
0
        if (!ossl_prov_digest_load_from_params(&ctx->digest, params, libctx))
577
0
            return 0;
578
579
0
        md = ossl_prov_digest_md(&ctx->digest);
580
0
        if (EVP_MD_xof(md)) {
581
0
            ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
582
0
            return 0;
583
0
        }
584
0
    }
585
586
0
    r = ossl_param_get1_octet_string(params, OSSL_KDF_PARAM_SECRET,
587
0
                                     &ctx->secret, &ctx->secret_len);
588
0
    if (r == -1)
589
0
        r = ossl_param_get1_octet_string(params, OSSL_KDF_PARAM_KEY,
590
0
                                         &ctx->secret, &ctx->secret_len);
591
0
    if (r == 0)
592
0
        return 0;
593
594
0
    if (ossl_param_get1_concat_octet_string(params, OSSL_KDF_PARAM_INFO,
595
0
                                            &ctx->info, &ctx->info_len, 0) == 0)
596
0
        return 0;
597
598
0
    if (ossl_param_get1_octet_string(params, OSSL_KDF_PARAM_SALT,
599
0
                                     &ctx->salt, &ctx->salt_len) == 0)
600
0
            return 0;
601
602
0
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MAC_SIZE))
603
0
        != NULL) {
604
0
        if (!OSSL_PARAM_get_size_t(p, &sz) || sz == 0)
605
0
            return 0;
606
0
        ctx->out_len = sz;
607
0
    }
608
0
    return 1;
609
0
}
610
611
static int sskdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
612
0
{
613
0
    KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
614
615
0
    if (ossl_param_is_empty(params))
616
0
        return 1;
617
618
0
    if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, params,
619
0
                                     OSSL_KDF_PARAM_FIPS_KEY_CHECK))
620
0
        return 0;
621
622
0
    if (!sskdf_common_set_ctx_params(ctx, params))
623
0
        return 0;
624
625
#ifdef FIPS_MODULE
626
    if ((OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY) != NULL) ||
627
        (OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET) != NULL))
628
        if (!fips_sskdf_key_check_passed(ctx))
629
            return 0;
630
#endif
631
632
0
    return 1;
633
0
}
634
635
static const OSSL_PARAM *sskdf_settable_ctx_params(ossl_unused void *ctx,
636
                                                   ossl_unused void *provctx)
637
0
{
638
0
    static const OSSL_PARAM known_settable_ctx_params[] = {
639
0
        SSKDF_COMMON_SETTABLES,
640
0
        OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_KEY_CHECK)
641
0
        OSSL_PARAM_END
642
0
    };
643
0
    return known_settable_ctx_params;
644
0
}
645
646
static int sskdf_common_get_ctx_params(KDF_SSKDF *ctx, OSSL_PARAM params[])
647
0
{
648
0
    OSSL_PARAM *p;
649
650
0
    if (ossl_param_is_empty(params))
651
0
        return 1;
652
653
0
    if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL) {
654
0
        if (!OSSL_PARAM_set_size_t(p, sskdf_size(ctx)))
655
0
            return 0;
656
0
    }
657
658
0
    return 1;
659
0
}
660
661
static int sskdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
662
0
{
663
0
    KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
664
665
0
    if (ossl_param_is_empty(params))
666
0
        return 1;
667
668
0
    if (!sskdf_common_get_ctx_params(ctx, params))
669
0
        return 0;
670
671
0
    if (!OSSL_FIPS_IND_GET_CTX_PARAM(ctx, params))
672
0
        return 0;
673
674
0
    return 1;
675
0
}
676
677
static const OSSL_PARAM *sskdf_gettable_ctx_params(ossl_unused void *ctx,
678
                                                   ossl_unused void *provctx)
679
0
{
680
0
    static const OSSL_PARAM known_gettable_ctx_params[] = {
681
0
        SSKDF_COMMON_GETTABLES,
682
0
        OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
683
0
        OSSL_PARAM_END
684
0
    };
685
0
    return known_gettable_ctx_params;
686
0
}
687
688
static int x963kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
689
0
{
690
0
    KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
691
692
0
    if (ossl_param_is_empty(params))
693
0
        return 1;
694
695
0
    if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, params,
696
0
                                     OSSL_KDF_PARAM_FIPS_DIGEST_CHECK))
697
0
        return 0;
698
0
    if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE1, params,
699
0
                                     OSSL_KDF_PARAM_FIPS_KEY_CHECK))
700
0
        return 0;
701
702
0
    if (!sskdf_common_set_ctx_params(ctx, params))
703
0
        return 0;
704
705
#ifdef FIPS_MODULE
706
    if (OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST) != NULL) {
707
        const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
708
709
        if (!fips_x963kdf_digest_check_passed(ctx, md))
710
            return 0;
711
    }
712
713
    if ((OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY) != NULL) ||
714
        (OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET) != NULL))
715
        if (!fips_x963kdf_key_check_passed(ctx))
716
            return 0;
717
#endif
718
719
0
    return 1;
720
0
}
721
722
static const OSSL_PARAM *x963kdf_settable_ctx_params(ossl_unused void *ctx,
723
                                                     ossl_unused void *provctx)
724
0
{
725
0
    static const OSSL_PARAM known_settable_ctx_params[] = {
726
0
        SSKDF_COMMON_SETTABLES,
727
0
        OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_DIGEST_CHECK)
728
0
        OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_KEY_CHECK)
729
0
        OSSL_PARAM_END
730
0
    };
731
0
    return known_settable_ctx_params;
732
0
}
733
734
static int x963kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
735
0
{
736
0
    KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
737
738
0
    if (!sskdf_common_get_ctx_params(ctx, params))
739
0
        return 0;
740
741
0
    if (!OSSL_FIPS_IND_GET_CTX_PARAM(ctx, params))
742
0
        return 0;
743
744
0
    return 1;
745
0
}
746
747
static const OSSL_PARAM *x963kdf_gettable_ctx_params(ossl_unused void *ctx,
748
                                                     ossl_unused void *provctx)
749
0
{
750
0
    static const OSSL_PARAM known_gettable_ctx_params[] = {
751
0
        SSKDF_COMMON_GETTABLES,
752
0
        OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
753
0
        OSSL_PARAM_END
754
0
    };
755
0
    return known_gettable_ctx_params;
756
0
}
757
758
const OSSL_DISPATCH ossl_kdf_sskdf_functions[] = {
759
    { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))sskdf_new },
760
    { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))sskdf_dup },
761
    { OSSL_FUNC_KDF_FREECTX, (void(*)(void))sskdf_free },
762
    { OSSL_FUNC_KDF_RESET, (void(*)(void))sskdf_reset },
763
    { OSSL_FUNC_KDF_DERIVE, (void(*)(void))sskdf_derive },
764
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
765
      (void(*)(void))sskdf_settable_ctx_params },
766
    { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))sskdf_set_ctx_params },
767
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
768
      (void(*)(void))sskdf_gettable_ctx_params },
769
    { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))sskdf_get_ctx_params },
770
    OSSL_DISPATCH_END
771
};
772
773
const OSSL_DISPATCH ossl_kdf_x963_kdf_functions[] = {
774
    { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))sskdf_new },
775
    { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))sskdf_dup },
776
    { OSSL_FUNC_KDF_FREECTX, (void(*)(void))sskdf_free },
777
    { OSSL_FUNC_KDF_RESET, (void(*)(void))sskdf_reset },
778
    { OSSL_FUNC_KDF_DERIVE, (void(*)(void))x963kdf_derive },
779
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
780
      (void(*)(void))x963kdf_settable_ctx_params },
781
    { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))x963kdf_set_ctx_params },
782
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
783
      (void(*)(void))x963kdf_gettable_ctx_params },
784
    { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))x963kdf_get_ctx_params },
785
    OSSL_DISPATCH_END
786
};