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