Coverage Report

Created: 2026-04-09 06:50

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