2013-07-31 01:05:08 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 2013-2020 The OpenSSL Project Authors. All Rights Reserved.
|
2013-07-31 01:05:08 +08:00
|
|
|
*
|
2018-12-06 20:36:05 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 03:38:09 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2013-07-31 01:05:08 +08:00
|
|
|
*/
|
|
|
|
|
2020-02-03 17:05:31 +08:00
|
|
|
/*
|
|
|
|
* DH low level APIs are deprecated for public use, but still ok for
|
|
|
|
* internal use.
|
|
|
|
*/
|
|
|
|
#include "internal/deprecated.h"
|
|
|
|
|
2017-08-22 23:07:56 +08:00
|
|
|
#include "e_os.h"
|
2020-08-04 09:21:21 +08:00
|
|
|
#include "e_os.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <openssl/core_names.h>
|
|
|
|
#include <openssl/dh.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/asn1.h>
|
|
|
|
#include <openssl/kdf.h>
|
|
|
|
#include <internal/provider.h>
|
|
|
|
#include <crypto/dh.h>
|
2015-08-04 00:45:26 +08:00
|
|
|
|
2020-08-04 09:21:21 +08:00
|
|
|
/* Key derivation function from X9.63/SECG */
|
|
|
|
int dh_KDF_X9_42_asn1(unsigned char *out, size_t outlen,
|
|
|
|
const unsigned char *Z, size_t Zlen,
|
|
|
|
const char *cek_alg,
|
|
|
|
const unsigned char *ukm, size_t ukmlen, const EVP_MD *md,
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *libctx, const char *propq)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2020-08-04 09:21:21 +08:00
|
|
|
int ret = 0;
|
2019-07-09 07:33:18 +08:00
|
|
|
EVP_KDF_CTX *kctx = NULL;
|
2019-08-21 06:06:29 +08:00
|
|
|
EVP_KDF *kdf = NULL;
|
|
|
|
OSSL_PARAM params[5], *p = params;
|
|
|
|
const char *mdname = EVP_MD_name(md);
|
2019-07-09 07:33:18 +08:00
|
|
|
|
2020-08-04 09:21:21 +08:00
|
|
|
kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_X942KDF, propq);
|
|
|
|
kctx = EVP_KDF_CTX_new(kdf);
|
|
|
|
if (kctx == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2020-08-04 09:21:21 +08:00
|
|
|
|
2019-08-21 06:06:29 +08:00
|
|
|
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
|
2020-02-05 13:13:49 +08:00
|
|
|
(char *)mdname, 0);
|
2019-08-21 06:06:29 +08:00
|
|
|
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
|
|
|
|
(unsigned char *)Z, Zlen);
|
|
|
|
if (ukm != NULL)
|
|
|
|
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_UKM,
|
|
|
|
(unsigned char *)ukm, ukmlen);
|
|
|
|
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG,
|
2020-08-04 09:21:21 +08:00
|
|
|
(char *)cek_alg, 0);
|
2019-08-21 06:06:29 +08:00
|
|
|
*p = OSSL_PARAM_construct_end();
|
2020-06-18 16:30:48 +08:00
|
|
|
ret = EVP_KDF_CTX_set_params(kctx, params) > 0
|
2020-08-04 09:21:21 +08:00
|
|
|
&& EVP_KDF_derive(kctx, out, outlen) > 0;
|
2019-07-09 07:33:18 +08:00
|
|
|
err:
|
2020-06-18 16:30:48 +08:00
|
|
|
EVP_KDF_CTX_free(kctx);
|
2019-08-21 06:06:29 +08:00
|
|
|
EVP_KDF_free(kdf);
|
2019-07-09 07:33:18 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2020-08-04 09:21:21 +08:00
|
|
|
|
2020-08-19 11:27:31 +08:00
|
|
|
#if !defined(FIPS_MODULE)
|
2020-08-04 09:21:21 +08:00
|
|
|
int DH_KDF_X9_42(unsigned char *out, size_t outlen,
|
|
|
|
const unsigned char *Z, size_t Zlen,
|
|
|
|
ASN1_OBJECT *key_oid,
|
|
|
|
const unsigned char *ukm, size_t ukmlen, const EVP_MD *md)
|
|
|
|
{
|
|
|
|
int nid;
|
|
|
|
const char *key_alg = NULL;
|
|
|
|
const OSSL_PROVIDER *prov = EVP_MD_provider(md);
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
|
2020-08-04 09:21:21 +08:00
|
|
|
|
|
|
|
nid = OBJ_obj2nid(key_oid);
|
|
|
|
if (nid == NID_undef)
|
|
|
|
return 0;
|
|
|
|
key_alg = OBJ_nid2sn(nid);
|
|
|
|
if (key_alg == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return dh_KDF_X9_42_asn1(out, outlen, Z, Zlen, key_alg,
|
|
|
|
ukm, ukmlen, md, libctx, NULL);
|
|
|
|
}
|
2020-08-19 11:27:31 +08:00
|
|
|
#endif /* !defined(FIPS_MODULE) */
|