Coverage Report

Created: 2025-06-13 06:57

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