Coverage Report

Created: 2026-02-11 06:19

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