Coverage Report

Created: 2025-06-13 06:58

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