1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*
11e1051a39Sopenharmony_ci * ECDH low level APIs are deprecated for public use, but still ok for
12e1051a39Sopenharmony_ci * internal use.
13e1051a39Sopenharmony_ci */
14e1051a39Sopenharmony_ci#include "internal/deprecated.h"
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ci#include <string.h>
17e1051a39Sopenharmony_ci#include <openssl/core_names.h>
18e1051a39Sopenharmony_ci#include <openssl/ec.h>
19e1051a39Sopenharmony_ci#include <openssl/evp.h>
20e1051a39Sopenharmony_ci#include <openssl/kdf.h>
21e1051a39Sopenharmony_ci#include "ec_local.h"
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_ci/* Key derivation function from X9.63/SECG */
24e1051a39Sopenharmony_ciint ossl_ecdh_kdf_X9_63(unsigned char *out, size_t outlen,
25e1051a39Sopenharmony_ci                        const unsigned char *Z, size_t Zlen,
26e1051a39Sopenharmony_ci                        const unsigned char *sinfo, size_t sinfolen,
27e1051a39Sopenharmony_ci                        const EVP_MD *md,
28e1051a39Sopenharmony_ci                        OSSL_LIB_CTX *libctx, const char *propq)
29e1051a39Sopenharmony_ci{
30e1051a39Sopenharmony_ci    int ret = 0;
31e1051a39Sopenharmony_ci    EVP_KDF_CTX *kctx = NULL;
32e1051a39Sopenharmony_ci    OSSL_PARAM params[4], *p = params;
33e1051a39Sopenharmony_ci    const char *mdname = EVP_MD_get0_name(md);
34e1051a39Sopenharmony_ci    EVP_KDF *kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_X963KDF, propq);
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_ci    if ((kctx = EVP_KDF_CTX_new(kdf)) != NULL) {
37e1051a39Sopenharmony_ci        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
38e1051a39Sopenharmony_ci                                                (char *)mdname, 0);
39e1051a39Sopenharmony_ci        *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
40e1051a39Sopenharmony_ci                                                 (void *)Z, Zlen);
41e1051a39Sopenharmony_ci        *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
42e1051a39Sopenharmony_ci                                                 (void *)sinfo, sinfolen);
43e1051a39Sopenharmony_ci        *p = OSSL_PARAM_construct_end();
44e1051a39Sopenharmony_ci
45e1051a39Sopenharmony_ci        ret = EVP_KDF_derive(kctx, out, outlen, params) > 0;
46e1051a39Sopenharmony_ci        EVP_KDF_CTX_free(kctx);
47e1051a39Sopenharmony_ci    }
48e1051a39Sopenharmony_ci    EVP_KDF_free(kdf);
49e1051a39Sopenharmony_ci    return ret;
50e1051a39Sopenharmony_ci}
51e1051a39Sopenharmony_ci
52e1051a39Sopenharmony_ci/*-
53e1051a39Sopenharmony_ci * The old name for ecdh_KDF_X9_63
54e1051a39Sopenharmony_ci * Retained for ABI compatibility
55e1051a39Sopenharmony_ci */
56e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
57e1051a39Sopenharmony_ciint ECDH_KDF_X9_62(unsigned char *out, size_t outlen,
58e1051a39Sopenharmony_ci                   const unsigned char *Z, size_t Zlen,
59e1051a39Sopenharmony_ci                   const unsigned char *sinfo, size_t sinfolen,
60e1051a39Sopenharmony_ci                   const EVP_MD *md)
61e1051a39Sopenharmony_ci{
62e1051a39Sopenharmony_ci    return ossl_ecdh_kdf_X9_63(out, outlen, Z, Zlen, sinfo, sinfolen, md, NULL,
63e1051a39Sopenharmony_ci                               NULL);
64e1051a39Sopenharmony_ci}
65e1051a39Sopenharmony_ci#endif
66