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;
45 const unsigned char *cek_oid;
50 * A table of allowed wrapping algorithms, oids and the associated output
52 * NOTE: RC2wrap and camellia128_wrap have been removed as there are no
53 * corresponding ciphers for these operations.
57 const unsigned char *oid;
59 size_t keklen; /* size in bytes */
61 { "AES-128-WRAP", der_oid_id_aes128_wrap, DER_OID_SZ_id_aes128_wrap, 16 },
62 { "AES-192-WRAP", der_oid_id_aes192_wrap, DER_OID_SZ_id_aes192_wrap, 24 },
63 { "AES-256-WRAP", der_oid_id_aes256_wrap, DER_OID_SZ_id_aes256_wrap, 32 },
65 { "DES3-WRAP", der_oid_id_alg_CMS3DESwrap, DER_OID_SZ_id_alg_CMS3DESwrap,
70 static int find_alg_id(OPENSSL_CTX *libctx, const char *algname,
71 const char *propq, size_t *id)
77 cipher = EVP_CIPHER_fetch(libctx, algname, propq);
79 for (i = 0; i < OSSL_NELEM(kek_algs); i++) {
80 if (EVP_CIPHER_is_a(cipher, kek_algs[i].name)) {
87 ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_CEK_ALG);
89 EVP_CIPHER_free(cipher);
93 static int DER_w_keyinfo(WPACKET *pkt,
94 const unsigned char *der_oid, size_t der_oidlen,
95 unsigned char **pcounter)
97 return DER_w_begin_sequence(pkt, -1)
98 /* Store the initial value of 1 into the counter */
99 && DER_w_octet_string_uint32(pkt, -1, 1)
100 /* Remember where we stored the counter in the buffer */
102 || (*pcounter = WPACKET_get_curr(pkt)) != NULL)
103 && DER_w_precompiled(pkt, -1, der_oid, der_oidlen)
104 && DER_w_end_sequence(pkt, -1);
107 static int der_encode_sharedinfo(WPACKET *pkt, unsigned char *buf, size_t buflen,
108 const unsigned char *der_oid, size_t der_oidlen,
109 const unsigned char *ukm, size_t ukmlen,
110 uint32_t keylen_bits, unsigned char **pcounter)
112 return (buf != NULL ? WPACKET_init_der(pkt, buf, buflen) :
113 WPACKET_init_null_der(pkt))
114 && DER_w_begin_sequence(pkt, -1)
115 && DER_w_octet_string_uint32(pkt, 2, keylen_bits)
116 && (ukm == NULL || DER_w_octet_string(pkt, 0, ukm, ukmlen))
117 && DER_w_keyinfo(pkt, der_oid, der_oidlen, pcounter)
118 && DER_w_end_sequence(pkt, -1)
119 && WPACKET_finish(pkt);
123 * Encode the other info structure.
125 * RFC2631 Section 2.1.2 Contains the following definition for otherinfo
127 * OtherInfo ::= SEQUENCE {
128 * keyInfo KeySpecificInfo,
129 * partyAInfo [0] OCTET STRING OPTIONAL,
130 * suppPubInfo [2] OCTET STRING
132 * Note suppPubInfo is the key length (in bits) (stored into 4 bytes)
135 * KeySpecificInfo ::= SEQUENCE {
136 * algorithm OBJECT IDENTIFIER,
137 * counter OCTET STRING SIZE (4..4)
140 * |keylen| is the length (in bytes) of the generated KEK. It is stored into
141 * suppPubInfo (in bits).
142 * |cek_oid| The oid of the key wrapping algorithm.
143 * |cek_oidlen| The length (in bytes) of the key wrapping algorithm oid,
144 * |ukm| is the optional user keying material that is stored into partyAInfo. It
146 * |ukmlen| is the user keying material length (in bytes).
147 * |der| is the returned encoded data. It must be freed by the caller.
148 * |der_len| is the returned size of the encoded data.
149 * |out_ctr| returns a pointer to the counter data which is embedded inside the
150 * encoded data. This allows the counter bytes to be updated without re-encoding.
152 * Returns: 1 if successfully encoded, or 0 otherwise.
153 * Assumptions: |der|, |der_len| & |out_ctr| are not NULL.
155 static int x942_encode_otherinfo(size_t keylen,
156 const unsigned char *cek_oid, size_t cek_oidlen,
157 const unsigned char *ukm, size_t ukmlen,
158 unsigned char **der, size_t *der_len,
159 unsigned char **out_ctr)
162 unsigned char *pcounter = NULL, *der_buf = NULL;
163 size_t der_buflen = 0;
165 uint32_t keylen_bits;
167 /* keylenbits must fit into 4 bytes */
168 if (keylen > 0xFFFFFF)
170 keylen_bits = 8 * keylen;
172 /* Calculate the size of the buffer */
173 if (!der_encode_sharedinfo(&pkt, NULL, 0, cek_oid, cek_oidlen, ukm, ukmlen,
175 || !WPACKET_get_total_written(&pkt, &der_buflen))
177 WPACKET_cleanup(&pkt);
178 /* Alloc the buffer */
179 der_buf = OPENSSL_zalloc(der_buflen);
182 /* Encode into the buffer */
183 if (!der_encode_sharedinfo(&pkt, der_buf, der_buflen, cek_oid, cek_oidlen,
184 ukm, ukmlen, keylen_bits, &pcounter))
187 * Since we allocated the exact size required, the buffer should point to the
188 * start of the alllocated buffer at this point.
190 if (WPACKET_get_curr(&pkt) != der_buf)
194 * The data for the DER encoded octet string of a 32 bit counter = 1
195 * should be 04 04 00 00 00 01
196 * So just check the header is correct and skip over it.
197 * This counter will be incremented in the kdf update loop.
200 || pcounter[0] != 0x04
201 || pcounter[1] != 0x04)
203 *out_ctr = (pcounter + 2);
205 *der_len = der_buflen;
208 WPACKET_cleanup(&pkt);
212 static int x942kdf_hash_kdm(const EVP_MD *kdf_md,
213 const unsigned char *z, size_t z_len,
214 const unsigned char *other, size_t other_len,
216 unsigned char *derived_key, size_t derived_key_len)
219 size_t counter, out_len, len = derived_key_len;
220 unsigned char mac[EVP_MAX_MD_SIZE];
221 unsigned char *out = derived_key;
222 EVP_MD_CTX *ctx = NULL, *ctx_init = NULL;
224 if (z_len > X942KDF_MAX_INLEN || other_len > X942KDF_MAX_INLEN
225 || derived_key_len > X942KDF_MAX_INLEN
226 || derived_key_len == 0) {
227 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
231 hlen = EVP_MD_size(kdf_md);
234 out_len = (size_t)hlen;
236 ctx = EVP_MD_CTX_create();
237 ctx_init = EVP_MD_CTX_create();
238 if (ctx == NULL || ctx_init == NULL)
241 if (!EVP_DigestInit(ctx_init, kdf_md))
244 for (counter = 1;; counter++) {
245 /* updating the ctr modifies 4 bytes in the 'other' buffer */
246 ctr[0] = (unsigned char)((counter >> 24) & 0xff);
247 ctr[1] = (unsigned char)((counter >> 16) & 0xff);
248 ctr[2] = (unsigned char)((counter >> 8) & 0xff);
249 ctr[3] = (unsigned char)(counter & 0xff);
251 if (!EVP_MD_CTX_copy_ex(ctx, ctx_init)
252 || !EVP_DigestUpdate(ctx, z, z_len)
253 || !EVP_DigestUpdate(ctx, other, other_len))
255 if (len >= out_len) {
256 if (!EVP_DigestFinal_ex(ctx, out, NULL))
263 if (!EVP_DigestFinal_ex(ctx, mac, NULL))
265 memcpy(out, mac, len);
271 EVP_MD_CTX_free(ctx);
272 EVP_MD_CTX_free(ctx_init);
273 OPENSSL_cleanse(mac, sizeof(mac));
277 static void *x942kdf_new(void *provctx)
281 if (!ossl_prov_is_running())
284 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
285 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
286 ctx->provctx = provctx;
290 static void x942kdf_reset(void *vctx)
292 KDF_X942 *ctx = (KDF_X942 *)vctx;
293 void *provctx = ctx->provctx;
295 ossl_prov_digest_reset(&ctx->digest);
296 OPENSSL_clear_free(ctx->secret, ctx->secret_len);
297 OPENSSL_clear_free(ctx->ukm, ctx->ukm_len);
298 memset(ctx, 0, sizeof(*ctx));
299 ctx->provctx = provctx;
302 static void x942kdf_free(void *vctx)
304 KDF_X942 *ctx = (KDF_X942 *)vctx;
312 static int x942kdf_set_buffer(unsigned char **out, size_t *out_len,
315 if (p->data_size == 0 || p->data == NULL)
320 return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
323 static size_t x942kdf_size(KDF_X942 *ctx)
326 const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
329 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
332 len = EVP_MD_size(md);
333 return (len <= 0) ? 0 : (size_t)len;
336 static int x942kdf_derive(void *vctx, unsigned char *key, size_t keylen)
338 KDF_X942 *ctx = (KDF_X942 *)vctx;
342 unsigned char *der = NULL;
345 if (!ossl_prov_is_running())
348 if (ctx->secret == NULL) {
349 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
352 md = ossl_prov_digest_md(&ctx->digest);
354 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
357 if (ctx->cek_oid == NULL || ctx->cek_oid_len == 0) {
358 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CEK_ALG);
361 if (ctx->ukm != NULL && ctx->ukm_len >= X942KDF_MAX_INLEN) {
363 * Note the ukm length MUST be 512 bits.
364 * For backwards compatibility the old check is being done.
366 ERR_raise(ERR_LIB_PROV, PROV_R_INAVLID_UKM_LENGTH);
369 /* generate the otherinfo der */
370 if (!x942_encode_otherinfo(ctx->dkm_len,
371 ctx->cek_oid, ctx->cek_oid_len,
372 ctx->ukm, ctx->ukm_len,
373 &der, &der_len, &ctr)) {
374 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_ENCODING);
377 ret = x942kdf_hash_kdm(md, ctx->secret, ctx->secret_len,
378 der, der_len, ctr, key, keylen);
383 static int x942kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
385 const OSSL_PARAM *p, *pq;
386 KDF_X942 *ctx = vctx;
387 OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
388 const char *propq = NULL;
391 if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
394 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL
395 || (p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
396 if (!x942kdf_set_buffer(&ctx->secret, &ctx->secret_len, p))
399 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_UKM)) != NULL)
400 if (!x942kdf_set_buffer(&ctx->ukm, &ctx->ukm_len, p))
403 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CEK_ALG)) != NULL) {
404 if (p->data_type != OSSL_PARAM_UTF8_STRING)
406 pq = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
408 * We already grab the properties during ossl_prov_digest_load_from_params()
409 * so there is no need to check the validity again..
413 if (find_alg_id(provctx, p->data, propq, &id) == 0)
415 ctx->cek_oid = kek_algs[id].oid;
416 ctx->cek_oid_len = kek_algs[id].oid_len;
417 ctx->dkm_len = kek_algs[id].keklen;
422 static const OSSL_PARAM *x942kdf_settable_ctx_params(ossl_unused void *provctx)
424 static const OSSL_PARAM known_settable_ctx_params[] = {
425 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
426 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
427 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
428 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
429 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_UKM, NULL, 0),
430 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CEK_ALG, NULL, 0),
433 return known_settable_ctx_params;
436 static int x942kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
438 KDF_X942 *ctx = (KDF_X942 *)vctx;
441 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
442 return OSSL_PARAM_set_size_t(p, x942kdf_size(ctx));
446 static const OSSL_PARAM *x942kdf_gettable_ctx_params(ossl_unused void *provctx)
448 static const OSSL_PARAM known_gettable_ctx_params[] = {
449 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
452 return known_gettable_ctx_params;
455 const OSSL_DISPATCH kdf_x942_kdf_functions[] = {
456 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))x942kdf_new },
457 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))x942kdf_free },
458 { OSSL_FUNC_KDF_RESET, (void(*)(void))x942kdf_reset },
459 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))x942kdf_derive },
460 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
461 (void(*)(void))x942kdf_settable_ctx_params },
462 { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))x942kdf_set_ctx_params },
463 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
464 (void(*)(void))x942kdf_gettable_ctx_params },
465 { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))x942kdf_get_ctx_params },