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