Coverage Report

Created: 2026-02-22 06:11

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