Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/kdfs/x942kdf.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
#include "internal/e_os.h"
12
#include <openssl/core_names.h>
13
#include <openssl/core_dispatch.h>
14
#include <openssl/err.h>
15
#include <openssl/evp.h>
16
#include <openssl/params.h>
17
#include <openssl/proverr.h>
18
#include "internal/common.h"
19
#include "internal/packet.h"
20
#include "internal/der.h"
21
#include "internal/nelem.h"
22
#include "prov/provider_ctx.h"
23
#include "prov/providercommon.h"
24
#include "prov/implementations.h"
25
#include "prov/provider_util.h"
26
#include "prov/securitycheck.h"
27
#include "prov/der_wrap.h"
28
#include "providers/implementations/kdfs/x942kdf.inc"
29
30
0
#define X942KDF_MAX_INLEN (1 << 30)
31
32
static OSSL_FUNC_kdf_newctx_fn x942kdf_new;
33
static OSSL_FUNC_kdf_dupctx_fn x942kdf_dup;
34
static OSSL_FUNC_kdf_freectx_fn x942kdf_free;
35
static OSSL_FUNC_kdf_reset_fn x942kdf_reset;
36
static OSSL_FUNC_kdf_derive_fn x942kdf_derive;
37
static OSSL_FUNC_kdf_settable_ctx_params_fn x942kdf_settable_ctx_params;
38
static OSSL_FUNC_kdf_set_ctx_params_fn x942kdf_set_ctx_params;
39
static OSSL_FUNC_kdf_gettable_ctx_params_fn x942kdf_gettable_ctx_params;
40
static OSSL_FUNC_kdf_get_ctx_params_fn x942kdf_get_ctx_params;
41
42
typedef struct {
43
    void *provctx;
44
    PROV_DIGEST digest;
45
    unsigned char *secret;
46
    size_t secret_len;
47
    unsigned char *acvpinfo;
48
    size_t acvpinfo_len;
49
    unsigned char *partyuinfo, *partyvinfo, *supp_pubinfo, *supp_privinfo;
50
    size_t partyuinfo_len, partyvinfo_len, supp_pubinfo_len, supp_privinfo_len;
51
    size_t dkm_len;
52
    const unsigned char *cek_oid;
53
    size_t cek_oid_len;
54
    int use_keybits;
55
    OSSL_FIPS_IND_DECLARE
56
} KDF_X942;
57
58
/*
59
 * A table of allowed wrapping algorithms, oids and the associated output
60
 * lengths.
61
 * NOTE: RC2wrap and camellia128_wrap have been removed as there are no
62
 * corresponding ciphers for these operations.
63
 */
64
static const struct {
65
    const char *name;
66
    const unsigned char *oid;
67
    size_t oid_len;
68
    size_t keklen; /* size in bytes */
69
} kek_algs[] = {
70
    { "AES-128-WRAP", ossl_der_oid_id_aes128_wrap, DER_OID_SZ_id_aes128_wrap,
71
        16 },
72
    { "AES-192-WRAP", ossl_der_oid_id_aes192_wrap, DER_OID_SZ_id_aes192_wrap,
73
        24 },
74
    { "AES-256-WRAP", ossl_der_oid_id_aes256_wrap, DER_OID_SZ_id_aes256_wrap,
75
        32 },
76
#ifndef FIPS_MODULE
77
    { "DES3-WRAP", ossl_der_oid_id_alg_CMS3DESwrap,
78
        DER_OID_SZ_id_alg_CMS3DESwrap, 24 },
79
#endif
80
};
81
82
static int find_alg_id(OSSL_LIB_CTX *libctx, const char *algname,
83
    const char *propq, size_t *id)
84
0
{
85
0
    int ret = 1;
86
0
    size_t i;
87
0
    EVP_CIPHER *cipher;
88
89
0
    cipher = EVP_CIPHER_fetch(libctx, algname, propq);
90
0
    if (cipher != NULL) {
91
0
        for (i = 0; i < OSSL_NELEM(kek_algs); i++) {
92
0
            if (EVP_CIPHER_is_a(cipher, kek_algs[i].name)) {
93
0
                *id = i;
94
0
                goto end;
95
0
            }
96
0
        }
97
0
    }
98
0
    ret = 0;
99
0
    ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_CEK_ALG);
100
0
end:
101
0
    EVP_CIPHER_free(cipher);
102
0
    return ret;
103
0
}
104
105
static int DER_w_keyinfo(WPACKET *pkt,
106
    const unsigned char *der_oid, size_t der_oidlen,
107
    unsigned char **pcounter)
108
0
{
109
0
    return ossl_DER_w_begin_sequence(pkt, -1)
110
        /* Store the initial value of 1 into the counter */
111
0
        && ossl_DER_w_octet_string_uint32(pkt, -1, 1)
112
        /* Remember where we stored the counter in the buffer */
113
0
        && (pcounter == NULL
114
0
            || (*pcounter = WPACKET_get_curr(pkt)) != NULL)
115
0
        && ossl_DER_w_precompiled(pkt, -1, der_oid, der_oidlen)
116
0
        && ossl_DER_w_end_sequence(pkt, -1);
117
0
}
118
119
static int der_encode_sharedinfo(WPACKET *pkt, unsigned char *buf, size_t buflen,
120
    const unsigned char *der_oid, size_t der_oidlen,
121
    const unsigned char *acvp, size_t acvplen,
122
    const unsigned char *partyu, size_t partyulen,
123
    const unsigned char *partyv, size_t partyvlen,
124
    const unsigned char *supp_pub, size_t supp_publen,
125
    const unsigned char *supp_priv, size_t supp_privlen,
126
    uint32_t keylen_bits, unsigned char **pcounter)
127
0
{
128
0
    return (buf != NULL ? WPACKET_init_der(pkt, buf, buflen) : WPACKET_init_null_der(pkt))
129
0
        && ossl_DER_w_begin_sequence(pkt, -1)
130
0
        && (supp_priv == NULL
131
0
            || ossl_DER_w_octet_string(pkt, 3, supp_priv, supp_privlen))
132
0
        && (supp_pub == NULL
133
0
            || ossl_DER_w_octet_string(pkt, 2, supp_pub, supp_publen))
134
0
        && (keylen_bits == 0
135
0
            || ossl_DER_w_octet_string_uint32(pkt, 2, keylen_bits))
136
0
        && (partyv == NULL || ossl_DER_w_octet_string(pkt, 1, partyv, partyvlen))
137
0
        && (partyu == NULL || ossl_DER_w_octet_string(pkt, 0, partyu, partyulen))
138
0
        && (acvp == NULL || ossl_DER_w_precompiled(pkt, -1, acvp, acvplen))
139
0
        && DER_w_keyinfo(pkt, der_oid, der_oidlen, pcounter)
140
0
        && ossl_DER_w_end_sequence(pkt, -1)
141
0
        && WPACKET_finish(pkt);
142
0
}
143
144
/*
145
 * Encode the other info structure.
146
 *
147
 * The ANS X9.42-2003 standard uses OtherInfo:
148
 *
149
 *  OtherInfo ::= SEQUENCE {
150
 *      keyInfo KeySpecificInfo,
151
 *      partyUInfo [0] OCTET STRING OPTIONAL,
152
 *      partyVInfo [1] OCTET STRING OPTIONAL,
153
 *      suppPubInfo [2] OCTET STRING OPTIONAL,
154
 *      suppPrivInfo [3] OCTET STRING OPTIONAL
155
 *  }
156
 *
157
 *  KeySpecificInfo ::= SEQUENCE {
158
 *      algorithm OBJECT IDENTIFIER,
159
 *      counter OCTET STRING SIZE (4..4)
160
 *  }
161
 *
162
 *  RFC2631 Section 2.1.2 Contains the following definition for OtherInfo
163
 *
164
 *  OtherInfo ::= SEQUENCE {
165
 *      keyInfo KeySpecificInfo,
166
 *      partyAInfo [0] OCTET STRING OPTIONAL,
167
 *      suppPubInfo [2] OCTET STRING
168
 *  }
169
 *  Where suppPubInfo is the key length (in bits) (stored into 4 bytes)
170
 *
171
 * |keylen| is the length (in bytes) of the generated KEK. It is stored into
172
 *   suppPubInfo (in bits). It is ignored if the value is 0.
173
 * |cek_oid| The oid of the key wrapping algorithm.
174
 * |cek_oidlen| The length (in bytes) of the key wrapping algorithm oid,
175
 * |acvp| is the optional blob of DER data representing one or more of the
176
 *   OtherInfo fields related to |partyu|, |partyv|, |supp_pub| and |supp_priv|.
177
 *   This field should normally be NULL. If |acvp| is non NULL then |partyu|,
178
 *   |partyv|, |supp_pub| and |supp_priv| should all be NULL.
179
 * |acvp_len| is the |acvp| length (in bytes).
180
 * |partyu| is the optional public info contributed by the initiator.
181
 *   It can be NULL. (It is also used as the ukm by CMS).
182
 * |partyu_len| is the |partyu| length (in bytes).
183
 * |partyv| is the optional public info contributed by the responder.
184
 *   It can be NULL.
185
 * |partyv_len| is the |partyv| length (in bytes).
186
 * |supp_pub| is the optional additional, mutually-known public information.
187
 *   It can be NULL. |keylen| should be 0 if this is not NULL.
188
 * |supp_pub_len| is the |supp_pub| length (in bytes).
189
 * |supp_priv| is the optional additional, mutually-known private information.
190
 *   It can be NULL.
191
 * |supp_priv_len| is the |supp_priv| length (in bytes).
192
 * |der| is the returned encoded data. It must be freed by the caller.
193
 * |der_len| is the returned size of the encoded data.
194
 * |out_ctr| returns a pointer to the counter data which is embedded inside the
195
 *   encoded data. This allows the counter bytes to be updated without
196
 *   re-encoding.
197
 *
198
 * Returns: 1 if successfully encoded, or 0 otherwise.
199
 * Assumptions: |der|, |der_len| & |out_ctr| are not NULL.
200
 */
201
static int
202
x942_encode_otherinfo(size_t keylen,
203
    const unsigned char *cek_oid, size_t cek_oid_len,
204
    const unsigned char *acvp, size_t acvp_len,
205
    const unsigned char *partyu, size_t partyu_len,
206
    const unsigned char *partyv, size_t partyv_len,
207
    const unsigned char *supp_pub, size_t supp_pub_len,
208
    const unsigned char *supp_priv, size_t supp_priv_len,
209
    unsigned char **der, size_t *der_len,
210
    unsigned char **out_ctr)
211
0
{
212
0
    int ret = 0;
213
0
    unsigned char *pcounter = NULL, *der_buf = NULL;
214
0
    size_t der_buflen = 0;
215
0
    WPACKET pkt;
216
0
    uint32_t keylen_bits;
217
218
    /* keylenbits must fit into 4 bytes */
219
0
    if (keylen > 0xFFFFFF)
220
0
        return 0;
221
0
    keylen_bits = (uint32_t)(8 * keylen);
222
223
    /* Calculate the size of the buffer */
224
0
    if (!der_encode_sharedinfo(&pkt, NULL, 0, cek_oid, cek_oid_len,
225
0
            acvp, acvp_len,
226
0
            partyu, partyu_len, partyv, partyv_len,
227
0
            supp_pub, supp_pub_len, supp_priv, supp_priv_len,
228
0
            keylen_bits, NULL)
229
0
        || !WPACKET_get_total_written(&pkt, &der_buflen))
230
0
        goto err;
231
0
    WPACKET_cleanup(&pkt);
232
    /* Alloc the buffer */
233
0
    der_buf = OPENSSL_zalloc(der_buflen);
234
0
    if (der_buf == NULL)
235
0
        goto err;
236
    /* Encode into the buffer */
237
0
    if (!der_encode_sharedinfo(&pkt, der_buf, der_buflen, cek_oid, cek_oid_len,
238
0
            acvp, acvp_len,
239
0
            partyu, partyu_len, partyv, partyv_len,
240
0
            supp_pub, supp_pub_len, supp_priv, supp_priv_len,
241
0
            keylen_bits, &pcounter))
242
0
        goto err;
243
    /*
244
     * Since we allocated the exact size required, the buffer should point to the
245
     * start of the allocated buffer at this point.
246
     */
247
0
    if (WPACKET_get_curr(&pkt) != der_buf)
248
0
        goto err;
249
250
    /*
251
     * The data for the DER encoded octet string of a 32 bit counter = 1
252
     * should be 04 04 00 00 00 01
253
     * So just check the header is correct and skip over it.
254
     * This counter will be incremented in the kdf update loop.
255
     */
256
0
    if (pcounter == NULL
257
0
        || pcounter[0] != 0x04
258
0
        || pcounter[1] != 0x04)
259
0
        goto err;
260
0
    *out_ctr = (pcounter + 2);
261
0
    *der = der_buf;
262
0
    *der_len = der_buflen;
263
0
    ret = 1;
264
0
err:
265
0
    WPACKET_cleanup(&pkt);
266
0
    return ret;
267
0
}
268
269
static int x942kdf_hash_kdm(const EVP_MD *kdf_md,
270
    const unsigned char *z, size_t z_len,
271
    const unsigned char *other, size_t other_len,
272
    unsigned char *ctr,
273
    unsigned char *derived_key, size_t derived_key_len)
274
0
{
275
0
    int ret = 0, hlen;
276
0
    size_t counter, out_len, len = derived_key_len;
277
0
    unsigned char mac[EVP_MAX_MD_SIZE];
278
0
    unsigned char *out = derived_key;
279
0
    EVP_MD_CTX *ctx = NULL, *ctx_init = NULL;
280
281
0
    if (z_len > X942KDF_MAX_INLEN
282
0
        || other_len > X942KDF_MAX_INLEN
283
0
        || derived_key_len > X942KDF_MAX_INLEN
284
0
        || derived_key_len == 0) {
285
0
        ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
286
0
        return 0;
287
0
    }
288
289
0
    hlen = EVP_MD_get_size(kdf_md);
290
0
    if (hlen <= 0)
291
0
        return 0;
292
0
    out_len = (size_t)hlen;
293
294
0
    ctx = EVP_MD_CTX_create();
295
0
    ctx_init = EVP_MD_CTX_create();
296
0
    if (ctx == NULL || ctx_init == NULL)
297
0
        goto end;
298
299
0
    if (!EVP_DigestInit(ctx_init, kdf_md))
300
0
        goto end;
301
302
0
    for (counter = 1;; counter++) {
303
        /* updating the ctr modifies 4 bytes in the 'other' buffer */
304
0
        ctr[0] = (unsigned char)((counter >> 24) & 0xff);
305
0
        ctr[1] = (unsigned char)((counter >> 16) & 0xff);
306
0
        ctr[2] = (unsigned char)((counter >> 8) & 0xff);
307
0
        ctr[3] = (unsigned char)(counter & 0xff);
308
309
0
        if (!EVP_MD_CTX_copy_ex(ctx, ctx_init)
310
0
            || !EVP_DigestUpdate(ctx, z, z_len)
311
0
            || !EVP_DigestUpdate(ctx, other, other_len))
312
0
            goto end;
313
0
        if (len >= out_len) {
314
0
            if (!EVP_DigestFinal_ex(ctx, out, NULL))
315
0
                goto end;
316
0
            out += out_len;
317
0
            len -= out_len;
318
0
            if (len == 0)
319
0
                break;
320
0
        } else {
321
0
            if (!EVP_DigestFinal_ex(ctx, mac, NULL))
322
0
                goto end;
323
0
            memcpy(out, mac, len);
324
0
            break;
325
0
        }
326
0
    }
327
0
    ret = 1;
328
0
end:
329
0
    EVP_MD_CTX_free(ctx);
330
0
    EVP_MD_CTX_free(ctx_init);
331
0
    OPENSSL_cleanse(mac, sizeof(mac));
332
0
    return ret;
333
0
}
334
335
static void *x942kdf_new(void *provctx)
336
0
{
337
0
    KDF_X942 *ctx;
338
339
0
    if (!ossl_prov_is_running())
340
0
        return NULL;
341
342
0
    ctx = OPENSSL_zalloc(sizeof(*ctx));
343
0
    if (ctx == NULL)
344
0
        return NULL;
345
346
0
    ctx->provctx = provctx;
347
0
    OSSL_FIPS_IND_INIT(ctx)
348
0
    ctx->use_keybits = 1;
349
0
    return ctx;
350
0
}
351
352
static void x942kdf_reset(void *vctx)
353
0
{
354
0
    KDF_X942 *ctx = (KDF_X942 *)vctx;
355
0
    void *provctx = ctx->provctx;
356
357
0
    ossl_prov_digest_reset(&ctx->digest);
358
0
    OPENSSL_clear_free(ctx->secret, ctx->secret_len);
359
0
    OPENSSL_clear_free(ctx->acvpinfo, ctx->acvpinfo_len);
360
0
    OPENSSL_clear_free(ctx->partyuinfo, ctx->partyuinfo_len);
361
0
    OPENSSL_clear_free(ctx->partyvinfo, ctx->partyvinfo_len);
362
0
    OPENSSL_clear_free(ctx->supp_pubinfo, ctx->supp_pubinfo_len);
363
0
    OPENSSL_clear_free(ctx->supp_privinfo, ctx->supp_privinfo_len);
364
0
    memset(ctx, 0, sizeof(*ctx));
365
0
    ctx->provctx = provctx;
366
0
    ctx->use_keybits = 1;
367
0
}
368
369
static void x942kdf_free(void *vctx)
370
0
{
371
0
    KDF_X942 *ctx = (KDF_X942 *)vctx;
372
373
0
    if (ctx != NULL) {
374
0
        x942kdf_reset(ctx);
375
0
        OPENSSL_free(ctx);
376
0
    }
377
0
}
378
379
static void *x942kdf_dup(void *vctx)
380
0
{
381
0
    const KDF_X942 *src = (const KDF_X942 *)vctx;
382
0
    KDF_X942 *dest;
383
384
0
    dest = x942kdf_new(src->provctx);
385
0
    if (dest != NULL) {
386
0
        if (!ossl_prov_memdup(src->secret, src->secret_len,
387
0
                &dest->secret, &dest->secret_len)
388
0
            || !ossl_prov_memdup(src->acvpinfo, src->acvpinfo_len,
389
0
                &dest->acvpinfo, &dest->acvpinfo_len)
390
0
            || !ossl_prov_memdup(src->partyuinfo, src->partyuinfo_len,
391
0
                &dest->partyuinfo, &dest->partyuinfo_len)
392
0
            || !ossl_prov_memdup(src->partyvinfo, src->partyvinfo_len,
393
0
                &dest->partyvinfo, &dest->partyvinfo_len)
394
0
            || !ossl_prov_memdup(src->supp_pubinfo, src->supp_pubinfo_len,
395
0
                &dest->supp_pubinfo,
396
0
                &dest->supp_pubinfo_len)
397
0
            || !ossl_prov_memdup(src->supp_privinfo, src->supp_privinfo_len,
398
0
                &dest->supp_privinfo,
399
0
                &dest->supp_privinfo_len)
400
0
            || !ossl_prov_digest_copy(&dest->digest, &src->digest))
401
0
            goto err;
402
0
        dest->cek_oid = src->cek_oid;
403
0
        dest->cek_oid_len = src->cek_oid_len;
404
0
        dest->dkm_len = src->dkm_len;
405
0
        dest->use_keybits = src->use_keybits;
406
0
        OSSL_FIPS_IND_COPY(dest, src)
407
0
    }
408
0
    return dest;
409
410
0
err:
411
0
    x942kdf_free(dest);
412
0
    return NULL;
413
0
}
414
415
static int x942kdf_set_buffer(unsigned char **out, size_t *out_len,
416
    const OSSL_PARAM *p)
417
0
{
418
0
    if (p->data_size == 0 || p->data == NULL)
419
0
        return 1;
420
421
0
    OPENSSL_free(*out);
422
0
    *out = NULL;
423
0
    return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
424
0
}
425
426
static size_t x942kdf_size(KDF_X942 *ctx)
427
0
{
428
0
    int len;
429
0
    const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
430
431
0
    if (md == NULL) {
432
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
433
0
        return 0;
434
0
    }
435
0
    len = EVP_MD_get_size(md);
436
0
    return (len <= 0) ? 0 : (size_t)len;
437
0
}
438
439
#ifdef FIPS_MODULE
440
static int fips_x942kdf_key_check_passed(KDF_X942 *ctx)
441
{
442
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
443
    int key_approved = ossl_kdf_check_key_size(ctx->secret_len);
444
445
    if (!key_approved) {
446
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,
447
                libctx, "X942KDF", "Key size",
448
                ossl_fips_config_x942kdf_key_check)) {
449
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
450
            return 0;
451
        }
452
    }
453
    return 1;
454
}
455
#endif
456
457
static int x942kdf_derive(void *vctx, unsigned char *key, size_t keylen,
458
    const OSSL_PARAM params[])
459
0
{
460
0
    KDF_X942 *ctx = (KDF_X942 *)vctx;
461
0
    const EVP_MD *md;
462
0
    int ret = 0;
463
0
    unsigned char *ctr;
464
0
    unsigned char *der = NULL;
465
0
    size_t der_len = 0;
466
467
0
    if (!ossl_prov_is_running() || !x942kdf_set_ctx_params(ctx, params))
468
0
        return 0;
469
470
    /*
471
     * These 2 options encode to the same field so only one of them should be
472
     * active at once.
473
     */
474
0
    if (ctx->use_keybits && ctx->supp_pubinfo != NULL) {
475
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PUBINFO);
476
0
        return 0;
477
0
    }
478
    /*
479
     * If the blob of acvp data is used then the individual info fields that it
480
     * replaces should not also be defined.
481
     */
482
0
    if (ctx->acvpinfo != NULL
483
0
        && (ctx->partyuinfo != NULL
484
0
            || ctx->partyvinfo != NULL
485
0
            || ctx->supp_pubinfo != NULL
486
0
            || ctx->supp_privinfo != NULL)) {
487
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
488
0
        return 0;
489
0
    }
490
0
    if (ctx->secret == NULL) {
491
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
492
0
        return 0;
493
0
    }
494
0
    md = ossl_prov_digest_md(&ctx->digest);
495
0
    if (md == NULL) {
496
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
497
0
        return 0;
498
0
    }
499
0
    if (ctx->cek_oid == NULL || ctx->cek_oid_len == 0) {
500
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CEK_ALG);
501
0
        return 0;
502
0
    }
503
0
    if (ctx->partyuinfo != NULL && ctx->partyuinfo_len >= X942KDF_MAX_INLEN) {
504
        /*
505
         * Note the ukm length MUST be 512 bits if it is used.
506
         * For backwards compatibility the old check is being done.
507
         */
508
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_UKM_LENGTH);
509
0
        return 0;
510
0
    }
511
    /* generate the otherinfo der */
512
0
    if (!x942_encode_otherinfo(ctx->use_keybits ? ctx->dkm_len : 0,
513
0
            ctx->cek_oid, ctx->cek_oid_len,
514
0
            ctx->acvpinfo, ctx->acvpinfo_len,
515
0
            ctx->partyuinfo, ctx->partyuinfo_len,
516
0
            ctx->partyvinfo, ctx->partyvinfo_len,
517
0
            ctx->supp_pubinfo, ctx->supp_pubinfo_len,
518
0
            ctx->supp_privinfo, ctx->supp_privinfo_len,
519
0
            &der, &der_len, &ctr)) {
520
0
        ERR_raise(ERR_LIB_PROV, PROV_R_BAD_ENCODING);
521
0
        return 0;
522
0
    }
523
0
    ret = x942kdf_hash_kdm(md, ctx->secret, ctx->secret_len,
524
0
        der, der_len, ctr, key, keylen);
525
0
    OPENSSL_free(der);
526
0
    return ret;
527
0
}
528
529
static int x942kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
530
0
{
531
0
    struct sshkdf_set_ctx_params_st p;
532
0
    KDF_X942 *ctx = vctx;
533
0
    OSSL_LIB_CTX *provctx;
534
0
    const char *cekalg, *propq = NULL;
535
0
    const EVP_MD *md;
536
0
    size_t id;
537
538
0
    if (ctx == NULL || !sshkdf_set_ctx_params_decoder(params, &p))
539
0
        return 0;
540
541
0
    provctx = PROV_LIBCTX_OF(ctx->provctx);
542
543
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k))
544
0
        return 0;
545
546
0
    if (p.digest != NULL) {
547
0
        if (!ossl_prov_digest_load(&ctx->digest, p.digest, p.propq, provctx))
548
0
            return 0;
549
0
        md = ossl_prov_digest_md(&ctx->digest);
550
0
        if (EVP_MD_xof(md)) {
551
0
            ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
552
0
            return 0;
553
0
        }
554
0
    }
555
556
0
    if (p.secret != NULL) {
557
0
        if (!x942kdf_set_buffer(&ctx->secret, &ctx->secret_len, p.secret))
558
0
            return 0;
559
#ifdef FIPS_MODULE
560
        if (!fips_x942kdf_key_check_passed(ctx))
561
            return 0;
562
#endif
563
0
    }
564
565
0
    if (p.acvp != NULL
566
0
        && !x942kdf_set_buffer(&ctx->acvpinfo, &ctx->acvpinfo_len, p.acvp))
567
0
        return 0;
568
569
0
    if (p.uinfo != NULL
570
0
        && !x942kdf_set_buffer(&ctx->partyuinfo, &ctx->partyuinfo_len, p.uinfo))
571
0
        return 0;
572
573
0
    if (p.vinfo != NULL
574
0
        && !x942kdf_set_buffer(&ctx->partyvinfo, &ctx->partyvinfo_len, p.vinfo))
575
0
        return 0;
576
577
0
    if (p.kbits != NULL && !OSSL_PARAM_get_int(p.kbits, &ctx->use_keybits))
578
0
        return 0;
579
580
0
    if (p.pub != NULL) {
581
0
        if (!x942kdf_set_buffer(&ctx->supp_pubinfo, &ctx->supp_pubinfo_len, p.pub))
582
0
            return 0;
583
0
        ctx->use_keybits = 0;
584
0
    }
585
586
0
    if (p.priv != NULL
587
0
        && !x942kdf_set_buffer(&ctx->supp_privinfo, &ctx->supp_privinfo_len, p.priv))
588
0
        return 0;
589
590
0
    if (p.cekalg != NULL) {
591
0
        if (!OSSL_PARAM_get_utf8_string_ptr(p.cekalg, &cekalg))
592
0
            return 0;
593
0
        if (p.propq != NULL && !OSSL_PARAM_get_utf8_string_ptr(p.propq, &propq))
594
0
            return 0;
595
0
        if (find_alg_id(provctx, cekalg, propq, &id) == 0)
596
0
            return 0;
597
0
        ctx->cek_oid = kek_algs[id].oid;
598
0
        ctx->cek_oid_len = kek_algs[id].oid_len;
599
0
        ctx->dkm_len = kek_algs[id].keklen;
600
0
    }
601
0
    return 1;
602
0
}
603
604
static const OSSL_PARAM *x942kdf_settable_ctx_params(ossl_unused void *ctx,
605
    ossl_unused void *provctx)
606
0
{
607
0
    return sshkdf_set_ctx_params_list;
608
0
}
609
610
static int x942kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
611
0
{
612
0
    KDF_X942 *ctx = (KDF_X942 *)vctx;
613
0
    struct sshkdf_get_ctx_params_st p;
614
615
0
    if (ctx == NULL || !sshkdf_get_ctx_params_decoder(params, &p))
616
0
        return 0;
617
618
0
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, x942kdf_size(ctx)))
619
0
        return 0;
620
621
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind))
622
0
        return 0;
623
0
    return 1;
624
0
}
625
626
static const OSSL_PARAM *x942kdf_gettable_ctx_params(ossl_unused void *ctx,
627
    ossl_unused void *provctx)
628
0
{
629
0
    return sshkdf_get_ctx_params_list;
630
0
}
631
632
const OSSL_DISPATCH ossl_kdf_x942_kdf_functions[] = {
633
    { OSSL_FUNC_KDF_NEWCTX, (void (*)(void))x942kdf_new },
634
    { OSSL_FUNC_KDF_DUPCTX, (void (*)(void))x942kdf_dup },
635
    { OSSL_FUNC_KDF_FREECTX, (void (*)(void))x942kdf_free },
636
    { OSSL_FUNC_KDF_RESET, (void (*)(void))x942kdf_reset },
637
    { OSSL_FUNC_KDF_DERIVE, (void (*)(void))x942kdf_derive },
638
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
639
        (void (*)(void))x942kdf_settable_ctx_params },
640
    { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void (*)(void))x942kdf_set_ctx_params },
641
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
642
        (void (*)(void))x942kdf_gettable_ctx_params },
643
    { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void (*)(void))x942kdf_get_ctx_params },
644
    OSSL_DISPATCH_END
645
};