Coverage Report

Created: 2024-07-27 06:36

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