Coverage Report

Created: 2025-12-31 06:58

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-2022 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
144
#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
66
{
79
66
    int ret = 1;
80
66
    size_t i;
81
66
    EVP_CIPHER *cipher;
82
83
66
    cipher = EVP_CIPHER_fetch(libctx, algname, propq);
84
66
    if (cipher != NULL) {
85
83
        for (i = 0; i < OSSL_NELEM(kek_algs); i++) {
86
81
            if (EVP_CIPHER_is_a(cipher, kek_algs[i].name)) {
87
27
                *id = i;
88
27
                goto end;
89
27
            }
90
81
        }
91
29
    }
92
39
    ret = 0;
93
39
    ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_CEK_ALG);
94
66
end:
95
66
    EVP_CIPHER_free(cipher);
96
66
    return ret;
97
39
}
98
99
static int DER_w_keyinfo(WPACKET *pkt,
100
    const unsigned char *der_oid, size_t der_oidlen,
101
    unsigned char **pcounter)
102
48
{
103
48
    return ossl_DER_w_begin_sequence(pkt, -1)
104
        /* Store the initial value of 1 into the counter */
105
48
        && ossl_DER_w_octet_string_uint32(pkt, -1, 1)
106
        /* Remember where we stored the counter in the buffer */
107
48
        && (pcounter == NULL
108
24
            || (*pcounter = WPACKET_get_curr(pkt)) != NULL)
109
48
        && ossl_DER_w_precompiled(pkt, -1, der_oid, der_oidlen)
110
48
        && ossl_DER_w_end_sequence(pkt, -1);
111
48
}
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
48
{
122
48
    return (buf != NULL ? WPACKET_init_der(pkt, buf, buflen) : WPACKET_init_null_der(pkt))
123
48
        && ossl_DER_w_begin_sequence(pkt, -1)
124
48
        && (supp_priv == NULL
125
0
            || ossl_DER_w_octet_string(pkt, 3, supp_priv, supp_privlen))
126
48
        && (supp_pub == NULL
127
0
            || ossl_DER_w_octet_string(pkt, 2, supp_pub, supp_publen))
128
48
        && (keylen_bits == 0
129
0
            || ossl_DER_w_octet_string_uint32(pkt, 2, keylen_bits))
130
48
        && (partyv == NULL || ossl_DER_w_octet_string(pkt, 1, partyv, partyvlen))
131
48
        && (partyu == NULL || ossl_DER_w_octet_string(pkt, 0, partyu, partyulen))
132
48
        && (acvp == NULL || ossl_DER_w_precompiled(pkt, -1, acvp, acvplen))
133
48
        && DER_w_keyinfo(pkt, der_oid, der_oidlen, pcounter)
134
48
        && ossl_DER_w_end_sequence(pkt, -1)
135
48
        && WPACKET_finish(pkt);
136
48
}
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 noramlly 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
24
{
206
24
    int ret = 0;
207
24
    unsigned char *pcounter = NULL, *der_buf = NULL;
208
24
    size_t der_buflen = 0;
209
24
    WPACKET pkt;
210
24
    uint32_t keylen_bits;
211
212
    /* keylenbits must fit into 4 bytes */
213
24
    if (keylen > 0xFFFFFF)
214
0
        return 0;
215
24
    keylen_bits = 8 * keylen;
216
217
    /* Calculate the size of the buffer */
218
24
    if (!der_encode_sharedinfo(&pkt, NULL, 0, cek_oid, cek_oid_len,
219
24
            acvp, acvp_len,
220
24
            partyu, partyu_len, partyv, partyv_len,
221
24
            supp_pub, supp_pub_len, supp_priv, supp_priv_len,
222
24
            keylen_bits, NULL)
223
24
        || !WPACKET_get_total_written(&pkt, &der_buflen))
224
0
        goto err;
225
24
    WPACKET_cleanup(&pkt);
226
    /* Alloc the buffer */
227
24
    der_buf = OPENSSL_zalloc(der_buflen);
228
24
    if (der_buf == NULL)
229
0
        goto err;
230
    /* Encode into the buffer */
231
24
    if (!der_encode_sharedinfo(&pkt, der_buf, der_buflen, cek_oid, cek_oid_len,
232
24
            acvp, acvp_len,
233
24
            partyu, partyu_len, partyv, partyv_len,
234
24
            supp_pub, supp_pub_len, supp_priv, supp_priv_len,
235
24
            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 alllocated buffer at this point.
240
     */
241
24
    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
24
    if (pcounter == NULL
251
24
        || pcounter[0] != 0x04
252
24
        || pcounter[1] != 0x04)
253
0
        goto err;
254
24
    *out_ctr = (pcounter + 2);
255
24
    *der = der_buf;
256
24
    *der_len = der_buflen;
257
24
    ret = 1;
258
24
err:
259
24
    WPACKET_cleanup(&pkt);
260
24
    return ret;
261
24
}
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
24
{
269
24
    int ret = 0, hlen;
270
24
    size_t counter, out_len, len = derived_key_len;
271
24
    unsigned char mac[EVP_MAX_MD_SIZE];
272
24
    unsigned char *out = derived_key;
273
24
    EVP_MD_CTX *ctx = NULL, *ctx_init = NULL;
274
275
24
    if (z_len > X942KDF_MAX_INLEN
276
24
        || other_len > X942KDF_MAX_INLEN
277
24
        || derived_key_len > X942KDF_MAX_INLEN
278
24
        || derived_key_len == 0) {
279
0
        ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
280
0
        return 0;
281
0
    }
282
283
24
    hlen = EVP_MD_get_size(kdf_md);
284
24
    if (hlen <= 0)
285
1
        return 0;
286
23
    out_len = (size_t)hlen;
287
288
23
    ctx = EVP_MD_CTX_create();
289
23
    ctx_init = EVP_MD_CTX_create();
290
23
    if (ctx == NULL || ctx_init == NULL)
291
0
        goto end;
292
293
23
    if (!EVP_DigestInit(ctx_init, kdf_md))
294
0
        goto end;
295
296
30
    for (counter = 1;; counter++) {
297
        /* updating the ctr modifies 4 bytes in the 'other' buffer */
298
30
        ctr[0] = (unsigned char)((counter >> 24) & 0xff);
299
30
        ctr[1] = (unsigned char)((counter >> 16) & 0xff);
300
30
        ctr[2] = (unsigned char)((counter >> 8) & 0xff);
301
30
        ctr[3] = (unsigned char)(counter & 0xff);
302
303
30
        if (!EVP_MD_CTX_copy_ex(ctx, ctx_init)
304
30
            || !EVP_DigestUpdate(ctx, z, z_len)
305
30
            || !EVP_DigestUpdate(ctx, other, other_len))
306
0
            goto end;
307
30
        if (len >= out_len) {
308
17
            if (!EVP_DigestFinal_ex(ctx, out, NULL))
309
0
                goto end;
310
17
            out += out_len;
311
17
            len -= out_len;
312
17
            if (len == 0)
313
10
                break;
314
17
        } 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
30
    }
321
23
    ret = 1;
322
23
end:
323
23
    EVP_MD_CTX_free(ctx);
324
23
    EVP_MD_CTX_free(ctx_init);
325
23
    OPENSSL_cleanse(mac, sizeof(mac));
326
23
    return ret;
327
23
}
328
329
static void *x942kdf_new(void *provctx)
330
92
{
331
92
    KDF_X942 *ctx;
332
333
92
    if (!ossl_prov_is_running())
334
0
        return NULL;
335
336
92
    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
92
    ctx->provctx = provctx;
341
92
    ctx->use_keybits = 1;
342
92
    return ctx;
343
92
}
344
345
static void x942kdf_reset(void *vctx)
346
92
{
347
92
    KDF_X942 *ctx = (KDF_X942 *)vctx;
348
92
    void *provctx = ctx->provctx;
349
350
92
    ossl_prov_digest_reset(&ctx->digest);
351
92
    OPENSSL_clear_free(ctx->secret, ctx->secret_len);
352
92
    OPENSSL_clear_free(ctx->acvpinfo, ctx->acvpinfo_len);
353
92
    OPENSSL_clear_free(ctx->partyuinfo, ctx->partyuinfo_len);
354
92
    OPENSSL_clear_free(ctx->partyvinfo, ctx->partyvinfo_len);
355
92
    OPENSSL_clear_free(ctx->supp_pubinfo, ctx->supp_pubinfo_len);
356
92
    OPENSSL_clear_free(ctx->supp_privinfo, ctx->supp_privinfo_len);
357
92
    memset(ctx, 0, sizeof(*ctx));
358
92
    ctx->provctx = provctx;
359
92
    ctx->use_keybits = 1;
360
92
}
361
362
static void x942kdf_free(void *vctx)
363
92
{
364
92
    KDF_X942 *ctx = (KDF_X942 *)vctx;
365
366
92
    if (ctx != NULL) {
367
92
        x942kdf_reset(ctx);
368
92
        OPENSSL_free(ctx);
369
92
    }
370
92
}
371
372
static int x942kdf_set_buffer(unsigned char **out, size_t *out_len,
373
    const OSSL_PARAM *p)
374
396
{
375
396
    if (p->data_size == 0 || p->data == NULL)
376
282
        return 1;
377
378
114
    OPENSSL_free(*out);
379
114
    *out = NULL;
380
114
    return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
381
396
}
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
27
{
399
27
    KDF_X942 *ctx = (KDF_X942 *)vctx;
400
27
    const EVP_MD *md;
401
27
    int ret = 0;
402
27
    unsigned char *ctr;
403
27
    unsigned char *der = NULL;
404
27
    size_t der_len = 0;
405
406
27
    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
27
    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
27
    if (ctx->acvpinfo != NULL
422
23
        && (ctx->partyuinfo != NULL
423
21
            || ctx->partyvinfo != NULL
424
21
            || ctx->supp_pubinfo != NULL
425
21
            || ctx->supp_privinfo != NULL)) {
426
2
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
427
2
        return 0;
428
2
    }
429
25
    if (ctx->secret == NULL) {
430
1
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
431
1
        return 0;
432
1
    }
433
24
    md = ossl_prov_digest_md(&ctx->digest);
434
24
    if (md == NULL) {
435
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
436
0
        return 0;
437
0
    }
438
24
    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
24
    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
24
    if (!x942_encode_otherinfo(ctx->use_keybits ? ctx->dkm_len : 0,
452
24
            ctx->cek_oid, ctx->cek_oid_len,
453
24
            ctx->acvpinfo, ctx->acvpinfo_len,
454
24
            ctx->partyuinfo, ctx->partyuinfo_len,
455
24
            ctx->partyvinfo, ctx->partyvinfo_len,
456
24
            ctx->supp_pubinfo, ctx->supp_pubinfo_len,
457
24
            ctx->supp_privinfo, ctx->supp_privinfo_len,
458
24
            &der, &der_len, &ctr)) {
459
0
        ERR_raise(ERR_LIB_PROV, PROV_R_BAD_ENCODING);
460
0
        return 0;
461
0
    }
462
24
    ret = x942kdf_hash_kdm(md, ctx->secret, ctx->secret_len,
463
24
        der, der_len, ctr, key, keylen);
464
24
    OPENSSL_free(der);
465
24
    return ret;
466
24
}
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
92
{
543
92
    static const OSSL_PARAM known_settable_ctx_params[] = {
544
92
        OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
545
92
        OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
546
92
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
547
92
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
548
92
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_UKM, NULL, 0),
549
92
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_X942_ACVPINFO, NULL, 0),
550
92
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_X942_PARTYUINFO, NULL, 0),
551
92
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_X942_PARTYVINFO, NULL, 0),
552
92
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_X942_SUPP_PUBINFO, NULL, 0),
553
92
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_X942_SUPP_PRIVINFO, NULL, 0),
554
92
        OSSL_PARAM_int(OSSL_KDF_PARAM_X942_USE_KEYBITS, NULL),
555
92
        OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CEK_ALG, NULL, 0),
556
92
        OSSL_PARAM_END
557
92
    };
558
92
    return known_settable_ctx_params;
559
92
}
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
};