Coverage Report

Created: 2025-10-28 06:56

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