Coverage Report

Created: 2025-12-31 06:58

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