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