2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
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
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 "internal/packet.h"
18 #include "internal/der.h"
19 #include "prov/provider_ctx.h"
20 #include "prov/providercommon.h"
21 #include "prov/providercommonerr.h"
22 #include "prov/implementations.h"
23 #include "prov/provider_util.h"
24 #include "prov/der_wrap.h"
26 #define X942KDF_MAX_INLEN (1 << 30)
28 static OSSL_FUNC_kdf_newctx_fn x942kdf_new;
29 static OSSL_FUNC_kdf_freectx_fn x942kdf_free;
30 static OSSL_FUNC_kdf_reset_fn x942kdf_reset;
31 static OSSL_FUNC_kdf_derive_fn x942kdf_derive;
32 static OSSL_FUNC_kdf_settable_ctx_params_fn x942kdf_settable_ctx_params;
33 static OSSL_FUNC_kdf_set_ctx_params_fn x942kdf_set_ctx_params;
34 static OSSL_FUNC_kdf_gettable_ctx_params_fn x942kdf_gettable_ctx_params;
35 static OSSL_FUNC_kdf_get_ctx_params_fn x942kdf_get_ctx_params;
40 unsigned char *secret;
42 unsigned char *partyuinfo, *partyvinfo, *supp_pubinfo, *supp_privinfo;
43 size_t partyuinfo_len, partyvinfo_len, supp_pubinfo_len, supp_privinfo_len;
45 const unsigned char *cek_oid;
51 * A table of allowed wrapping algorithms, oids and the associated output
53 * NOTE: RC2wrap and camellia128_wrap have been removed as there are no
54 * corresponding ciphers for these operations.
58 const unsigned char *oid;
60 size_t keklen; /* size in bytes */
62 { "AES-128-WRAP", ossl_der_oid_id_aes128_wrap, DER_OID_SZ_id_aes128_wrap,
64 { "AES-192-WRAP", ossl_der_oid_id_aes192_wrap, DER_OID_SZ_id_aes192_wrap,
66 { "AES-256-WRAP", ossl_der_oid_id_aes256_wrap, DER_OID_SZ_id_aes256_wrap,
69 { "DES3-WRAP", ossl_der_oid_id_alg_CMS3DESwrap,
70 DER_OID_SZ_id_alg_CMS3DESwrap, 24 },
74 static int find_alg_id(OSSL_LIB_CTX *libctx, const char *algname,
75 const char *propq, size_t *id)
81 cipher = EVP_CIPHER_fetch(libctx, algname, propq);
83 for (i = 0; i < OSSL_NELEM(kek_algs); i++) {
84 if (EVP_CIPHER_is_a(cipher, kek_algs[i].name)) {
91 ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_CEK_ALG);
93 EVP_CIPHER_free(cipher);
97 static int DER_w_keyinfo(WPACKET *pkt,
98 const unsigned char *der_oid, size_t der_oidlen,
99 unsigned char **pcounter)
101 return ossl_DER_w_begin_sequence(pkt, -1)
102 /* Store the initial value of 1 into the counter */
103 && ossl_DER_w_octet_string_uint32(pkt, -1, 1)
104 /* Remember where we stored the counter in the buffer */
106 || (*pcounter = WPACKET_get_curr(pkt)) != NULL)
107 && ossl_DER_w_precompiled(pkt, -1, der_oid, der_oidlen)
108 && ossl_DER_w_end_sequence(pkt, -1);
111 static int der_encode_sharedinfo(WPACKET *pkt, unsigned char *buf, size_t buflen,
112 const unsigned char *der_oid, size_t der_oidlen,
113 const unsigned char *partyu, size_t partyulen,
114 const unsigned char *partyv, size_t partyvlen,
115 const unsigned char *supp_pub, size_t supp_publen,
116 const unsigned char *supp_priv, size_t supp_privlen,
117 uint32_t keylen_bits, unsigned char **pcounter)
119 return (buf != NULL ? WPACKET_init_der(pkt, buf, buflen) :
120 WPACKET_init_null_der(pkt))
121 && ossl_DER_w_begin_sequence(pkt, -1)
122 && (supp_priv == NULL
123 || ossl_DER_w_octet_string(pkt, 3, supp_priv, supp_privlen))
125 || ossl_DER_w_octet_string(pkt, 2, supp_pub, supp_publen))
127 || ossl_DER_w_octet_string_uint32(pkt, 2, keylen_bits))
128 && (partyv == NULL || ossl_DER_w_octet_string(pkt, 1, partyv, partyvlen))
129 && (partyu == NULL || ossl_DER_w_octet_string(pkt, 0, partyu, partyulen))
130 && DER_w_keyinfo(pkt, der_oid, der_oidlen, pcounter)
131 && ossl_DER_w_end_sequence(pkt, -1)
132 && WPACKET_finish(pkt);
136 * Encode the other info structure.
138 * The ANS X9.42-2003 standard uses OtherInfo:
140 * OtherInfo ::= SEQUENCE {
141 * keyInfo KeySpecificInfo,
142 * partyUInfo [0] OCTET STRING OPTIONAL,
143 * partyVInfo [1] OCTET STRING OPTIONAL,
144 * suppPubInfo [2] OCTET STRING OPTIONAL,
145 * suppPrivInfo [3] OCTET STRING OPTIONAL
148 * KeySpecificInfo ::= SEQUENCE {
149 * algorithm OBJECT IDENTIFIER,
150 * counter OCTET STRING SIZE (4..4)
153 * RFC2631 Section 2.1.2 Contains the following definition for OtherInfo
155 * OtherInfo ::= SEQUENCE {
156 * keyInfo KeySpecificInfo,
157 * partyAInfo [0] OCTET STRING OPTIONAL,
158 * suppPubInfo [2] OCTET STRING
160 * Where suppPubInfo is the key length (in bits) (stored into 4 bytes)
164 * |keylen| is the length (in bytes) of the generated KEK. It is stored into
165 * suppPubInfo (in bits). It is ignored if the value is 0.
166 * |cek_oid| The oid of the key wrapping algorithm.
167 * |cek_oidlen| The length (in bytes) of the key wrapping algorithm oid,
168 * |partyu| is the optional public info contributed by the initiator. It
169 * can be NULL. (It is also used as the ukm by CMS).
170 * |partyu_len| is the |partyu| length (in bytes).
171 * |partyv| is the optional public info contributed by the responder. It
173 * |partyv_len| is the |partyv| length (in bytes).
174 * |supp_pub| is the optional additional, mutually-known public information. It
175 * can be NULL. |keylen| should be 0 if this is not NULL.
176 * |supp_pub_len| is the |supp_pub| length (in bytes).
177 * |supp_priv| is the optional additional, mutually-known private information. It
179 * |supp_priv_len| is the |supp_priv| length (in bytes).
180 * |der| is the returned encoded data. It must be freed by the caller.
181 * |der_len| is the returned size of the encoded data.
182 * |out_ctr| returns a pointer to the counter data which is embedded inside the
183 * encoded data. This allows the counter bytes to be updated without re-encoding.
185 * Returns: 1 if successfully encoded, or 0 otherwise.
186 * Assumptions: |der|, |der_len| & |out_ctr| are not NULL.
189 x942_encode_otherinfo(size_t keylen,
190 const unsigned char *cek_oid, size_t cek_oidlen,
191 const unsigned char *partyu, size_t partyu_len,
192 const unsigned char *partyv, size_t partyv_len,
193 const unsigned char *supp_pub, size_t supp_pub_len,
194 const unsigned char *supp_priv, size_t supp_priv_len,
195 unsigned char **der, size_t *der_len,
196 unsigned char **out_ctr)
199 unsigned char *pcounter = NULL, *der_buf = NULL;
200 size_t der_buflen = 0;
202 uint32_t keylen_bits;
204 /* keylenbits must fit into 4 bytes */
205 if (keylen > 0xFFFFFF)
207 keylen_bits = 8 * keylen;
209 /* Calculate the size of the buffer */
210 if (!der_encode_sharedinfo(&pkt, NULL, 0, cek_oid, cek_oidlen,
211 partyu, partyu_len, partyv, partyv_len,
212 supp_pub, supp_pub_len, supp_priv, supp_priv_len,
214 || !WPACKET_get_total_written(&pkt, &der_buflen))
216 WPACKET_cleanup(&pkt);
217 /* Alloc the buffer */
218 der_buf = OPENSSL_zalloc(der_buflen);
221 /* Encode into the buffer */
222 if (!der_encode_sharedinfo(&pkt, der_buf, der_buflen, cek_oid, cek_oidlen,
223 partyu, partyu_len, partyv, partyv_len,
224 supp_pub, supp_pub_len, supp_priv, supp_priv_len,
225 keylen_bits, &pcounter))
228 * Since we allocated the exact size required, the buffer should point to the
229 * start of the alllocated buffer at this point.
231 if (WPACKET_get_curr(&pkt) != der_buf)
235 * The data for the DER encoded octet string of a 32 bit counter = 1
236 * should be 04 04 00 00 00 01
237 * So just check the header is correct and skip over it.
238 * This counter will be incremented in the kdf update loop.
241 || pcounter[0] != 0x04
242 || pcounter[1] != 0x04)
244 *out_ctr = (pcounter + 2);
246 *der_len = der_buflen;
249 WPACKET_cleanup(&pkt);
253 static int x942kdf_hash_kdm(const EVP_MD *kdf_md,
254 const unsigned char *z, size_t z_len,
255 const unsigned char *other, size_t other_len,
257 unsigned char *derived_key, size_t derived_key_len)
260 size_t counter, out_len, len = derived_key_len;
261 unsigned char mac[EVP_MAX_MD_SIZE];
262 unsigned char *out = derived_key;
263 EVP_MD_CTX *ctx = NULL, *ctx_init = NULL;
265 if (z_len > X942KDF_MAX_INLEN || other_len > X942KDF_MAX_INLEN
266 || derived_key_len > X942KDF_MAX_INLEN
267 || derived_key_len == 0) {
268 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
272 hlen = EVP_MD_size(kdf_md);
275 out_len = (size_t)hlen;
277 ctx = EVP_MD_CTX_create();
278 ctx_init = EVP_MD_CTX_create();
279 if (ctx == NULL || ctx_init == NULL)
282 if (!EVP_DigestInit(ctx_init, kdf_md))
285 for (counter = 1;; counter++) {
286 /* updating the ctr modifies 4 bytes in the 'other' buffer */
287 ctr[0] = (unsigned char)((counter >> 24) & 0xff);
288 ctr[1] = (unsigned char)((counter >> 16) & 0xff);
289 ctr[2] = (unsigned char)((counter >> 8) & 0xff);
290 ctr[3] = (unsigned char)(counter & 0xff);
292 if (!EVP_MD_CTX_copy_ex(ctx, ctx_init)
293 || !EVP_DigestUpdate(ctx, z, z_len)
294 || !EVP_DigestUpdate(ctx, other, other_len))
296 if (len >= out_len) {
297 if (!EVP_DigestFinal_ex(ctx, out, NULL))
304 if (!EVP_DigestFinal_ex(ctx, mac, NULL))
306 memcpy(out, mac, len);
312 EVP_MD_CTX_free(ctx);
313 EVP_MD_CTX_free(ctx_init);
314 OPENSSL_cleanse(mac, sizeof(mac));
318 static void *x942kdf_new(void *provctx)
322 if (!ossl_prov_is_running())
325 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
326 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
327 ctx->provctx = provctx;
328 ctx->use_keybits = 1;
332 static void x942kdf_reset(void *vctx)
334 KDF_X942 *ctx = (KDF_X942 *)vctx;
335 void *provctx = ctx->provctx;
337 ossl_prov_digest_reset(&ctx->digest);
338 OPENSSL_clear_free(ctx->secret, ctx->secret_len);
339 OPENSSL_clear_free(ctx->partyuinfo, ctx->partyuinfo_len);
340 OPENSSL_clear_free(ctx->partyvinfo, ctx->partyvinfo_len);
341 OPENSSL_clear_free(ctx->supp_pubinfo, ctx->supp_pubinfo_len);
342 OPENSSL_clear_free(ctx->supp_privinfo, ctx->supp_privinfo_len);
343 memset(ctx, 0, sizeof(*ctx));
344 ctx->provctx = provctx;
345 ctx->use_keybits = 1;
348 static void x942kdf_free(void *vctx)
350 KDF_X942 *ctx = (KDF_X942 *)vctx;
358 static int x942kdf_set_buffer(unsigned char **out, size_t *out_len,
361 if (p->data_size == 0 || p->data == NULL)
366 return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
369 static size_t x942kdf_size(KDF_X942 *ctx)
372 const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
375 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
378 len = EVP_MD_size(md);
379 return (len <= 0) ? 0 : (size_t)len;
382 static int x942kdf_derive(void *vctx, unsigned char *key, size_t keylen)
384 KDF_X942 *ctx = (KDF_X942 *)vctx;
388 unsigned char *der = NULL;
391 if (!ossl_prov_is_running())
395 * These 2 options encode to the same field so only one of them should be
398 if (ctx->use_keybits && ctx->supp_pubinfo != NULL) {
399 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PUBINFO);
403 if (ctx->secret == NULL) {
404 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
407 md = ossl_prov_digest_md(&ctx->digest);
409 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
412 if (ctx->cek_oid == NULL || ctx->cek_oid_len == 0) {
413 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CEK_ALG);
416 if (ctx->partyuinfo != NULL && ctx->partyuinfo_len >= X942KDF_MAX_INLEN) {
418 * Note the ukm length MUST be 512 bits if it is used.
419 * For backwards compatibility the old check is being done.
421 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_UKM_LENGTH);
424 /* generate the otherinfo der */
425 if (!x942_encode_otherinfo(ctx->use_keybits ? ctx->dkm_len : 0,
426 ctx->cek_oid, ctx->cek_oid_len,
427 ctx->partyuinfo, ctx->partyuinfo_len,
428 ctx->partyvinfo, ctx->partyvinfo_len,
429 ctx->supp_pubinfo, ctx->supp_pubinfo_len,
430 ctx->supp_privinfo, ctx->supp_privinfo_len,
431 &der, &der_len, &ctr)) {
432 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_ENCODING);
435 ret = x942kdf_hash_kdm(md, ctx->secret, ctx->secret_len,
436 der, der_len, ctr, key, keylen);
441 static int x942kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
443 const OSSL_PARAM *p, *pq;
444 KDF_X942 *ctx = vctx;
445 OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
446 const char *propq = NULL;
449 if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
452 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET);
454 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY);
455 if (p != NULL && !x942kdf_set_buffer(&ctx->secret, &ctx->secret_len, p))
458 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_X942_PARTYUINFO);
460 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_UKM);
462 && !x942kdf_set_buffer(&ctx->partyuinfo, &ctx->partyuinfo_len, p))
465 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_X942_PARTYVINFO);
467 && !x942kdf_set_buffer(&ctx->partyvinfo, &ctx->partyvinfo_len, p))
470 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_X942_USE_KEYBITS);
471 if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->use_keybits))
474 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_X942_SUPP_PUBINFO);
476 if (!x942kdf_set_buffer(&ctx->supp_pubinfo, &ctx->supp_pubinfo_len, p))
478 ctx->use_keybits = 0;
481 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_X942_SUPP_PRIVINFO);
483 && !x942kdf_set_buffer(&ctx->supp_privinfo, &ctx->supp_privinfo_len, p))
486 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CEK_ALG);
488 if (p->data_type != OSSL_PARAM_UTF8_STRING)
490 pq = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
492 * We already grab the properties during ossl_prov_digest_load_from_params()
493 * so there is no need to check the validity again..
497 if (find_alg_id(provctx, p->data, propq, &id) == 0)
499 ctx->cek_oid = kek_algs[id].oid;
500 ctx->cek_oid_len = kek_algs[id].oid_len;
501 ctx->dkm_len = kek_algs[id].keklen;
506 static const OSSL_PARAM *x942kdf_settable_ctx_params(ossl_unused void *provctx)
508 static const OSSL_PARAM known_settable_ctx_params[] = {
509 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
510 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
511 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
512 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
513 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_UKM, NULL, 0),
514 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_X942_PARTYUINFO, NULL, 0),
515 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_X942_PARTYVINFO, NULL, 0),
516 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_X942_SUPP_PUBINFO, NULL, 0),
517 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_X942_SUPP_PRIVINFO, NULL, 0),
518 OSSL_PARAM_int(OSSL_KDF_PARAM_X942_USE_KEYBITS, NULL),
519 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CEK_ALG, NULL, 0),
522 return known_settable_ctx_params;
525 static int x942kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
527 KDF_X942 *ctx = (KDF_X942 *)vctx;
530 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
531 return OSSL_PARAM_set_size_t(p, x942kdf_size(ctx));
535 static const OSSL_PARAM *x942kdf_gettable_ctx_params(ossl_unused void *provctx)
537 static const OSSL_PARAM known_gettable_ctx_params[] = {
538 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
541 return known_gettable_ctx_params;
544 const OSSL_DISPATCH ossl_kdf_x942_kdf_functions[] = {
545 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))x942kdf_new },
546 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))x942kdf_free },
547 { OSSL_FUNC_KDF_RESET, (void(*)(void))x942kdf_reset },
548 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))x942kdf_derive },
549 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
550 (void(*)(void))x942kdf_settable_ctx_params },
551 { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))x942kdf_set_ctx_params },
552 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
553 (void(*)(void))x942kdf_gettable_ctx_params },
554 { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))x942kdf_get_ctx_params },