162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * ECDH params to be used with kpp API 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (c) 2016, Intel Corporation 662306a36Sopenharmony_ci * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com> 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci#ifndef _CRYPTO_ECDH_ 962306a36Sopenharmony_ci#define _CRYPTO_ECDH_ 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci/** 1262306a36Sopenharmony_ci * DOC: ECDH Helper Functions 1362306a36Sopenharmony_ci * 1462306a36Sopenharmony_ci * To use ECDH with the KPP cipher API, the following data structure and 1562306a36Sopenharmony_ci * functions should be used. 1662306a36Sopenharmony_ci * 1762306a36Sopenharmony_ci * The ECC curves known to the ECDH implementation are specified in this 1862306a36Sopenharmony_ci * header file. 1962306a36Sopenharmony_ci * 2062306a36Sopenharmony_ci * To use ECDH with KPP, the following functions should be used to operate on 2162306a36Sopenharmony_ci * an ECDH private key. The packet private key that can be set with 2262306a36Sopenharmony_ci * the KPP API function call of crypto_kpp_set_secret. 2362306a36Sopenharmony_ci */ 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci/* Curves IDs */ 2662306a36Sopenharmony_ci#define ECC_CURVE_NIST_P192 0x0001 2762306a36Sopenharmony_ci#define ECC_CURVE_NIST_P256 0x0002 2862306a36Sopenharmony_ci#define ECC_CURVE_NIST_P384 0x0003 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci/** 3162306a36Sopenharmony_ci * struct ecdh - define an ECDH private key 3262306a36Sopenharmony_ci * 3362306a36Sopenharmony_ci * @key: Private ECDH key 3462306a36Sopenharmony_ci * @key_size: Size of the private ECDH key 3562306a36Sopenharmony_ci */ 3662306a36Sopenharmony_cistruct ecdh { 3762306a36Sopenharmony_ci char *key; 3862306a36Sopenharmony_ci unsigned short key_size; 3962306a36Sopenharmony_ci}; 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci/** 4262306a36Sopenharmony_ci * crypto_ecdh_key_len() - Obtain the size of the private ECDH key 4362306a36Sopenharmony_ci * @params: private ECDH key 4462306a36Sopenharmony_ci * 4562306a36Sopenharmony_ci * This function returns the packet ECDH key size. A caller can use that 4662306a36Sopenharmony_ci * with the provided ECDH private key reference to obtain the required 4762306a36Sopenharmony_ci * memory size to hold a packet key. 4862306a36Sopenharmony_ci * 4962306a36Sopenharmony_ci * Return: size of the key in bytes 5062306a36Sopenharmony_ci */ 5162306a36Sopenharmony_ciunsigned int crypto_ecdh_key_len(const struct ecdh *params); 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci/** 5462306a36Sopenharmony_ci * crypto_ecdh_encode_key() - encode the private key 5562306a36Sopenharmony_ci * @buf: Buffer allocated by the caller to hold the packet ECDH 5662306a36Sopenharmony_ci * private key. The buffer should be at least crypto_ecdh_key_len 5762306a36Sopenharmony_ci * bytes in size. 5862306a36Sopenharmony_ci * @len: Length of the packet private key buffer 5962306a36Sopenharmony_ci * @p: Buffer with the caller-specified private key 6062306a36Sopenharmony_ci * 6162306a36Sopenharmony_ci * The ECDH implementations operate on a packet representation of the private 6262306a36Sopenharmony_ci * key. 6362306a36Sopenharmony_ci * 6462306a36Sopenharmony_ci * Return: -EINVAL if buffer has insufficient size, 0 on success 6562306a36Sopenharmony_ci */ 6662306a36Sopenharmony_ciint crypto_ecdh_encode_key(char *buf, unsigned int len, const struct ecdh *p); 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci/** 6962306a36Sopenharmony_ci * crypto_ecdh_decode_key() - decode a private key 7062306a36Sopenharmony_ci * @buf: Buffer holding a packet key that should be decoded 7162306a36Sopenharmony_ci * @len: Length of the packet private key buffer 7262306a36Sopenharmony_ci * @p: Buffer allocated by the caller that is filled with the 7362306a36Sopenharmony_ci * unpacked ECDH private key. 7462306a36Sopenharmony_ci * 7562306a36Sopenharmony_ci * The unpacking obtains the private key by pointing @p to the correct location 7662306a36Sopenharmony_ci * in @buf. Thus, both pointers refer to the same memory. 7762306a36Sopenharmony_ci * 7862306a36Sopenharmony_ci * Return: -EINVAL if buffer has insufficient size, 0 on success 7962306a36Sopenharmony_ci */ 8062306a36Sopenharmony_ciint crypto_ecdh_decode_key(const char *buf, unsigned int len, struct ecdh *p); 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci#endif 83