18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * ECDH params to be used with kpp API 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2016, Intel Corporation 68c2ecf20Sopenharmony_ci * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com> 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci#ifndef _CRYPTO_ECDH_ 98c2ecf20Sopenharmony_ci#define _CRYPTO_ECDH_ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci/** 128c2ecf20Sopenharmony_ci * DOC: ECDH Helper Functions 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * To use ECDH with the KPP cipher API, the following data structure and 158c2ecf20Sopenharmony_ci * functions should be used. 168c2ecf20Sopenharmony_ci * 178c2ecf20Sopenharmony_ci * The ECC curves known to the ECDH implementation are specified in this 188c2ecf20Sopenharmony_ci * header file. 198c2ecf20Sopenharmony_ci * 208c2ecf20Sopenharmony_ci * To use ECDH with KPP, the following functions should be used to operate on 218c2ecf20Sopenharmony_ci * an ECDH private key. The packet private key that can be set with 228c2ecf20Sopenharmony_ci * the KPP API function call of crypto_kpp_set_secret. 238c2ecf20Sopenharmony_ci */ 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci/* Curves IDs */ 268c2ecf20Sopenharmony_ci#define ECC_CURVE_NIST_P192 0x0001 278c2ecf20Sopenharmony_ci#define ECC_CURVE_NIST_P256 0x0002 288c2ecf20Sopenharmony_ci#define ECC_CURVE_NIST_P384 0x0003 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci/** 318c2ecf20Sopenharmony_ci * struct ecdh - define an ECDH private key 328c2ecf20Sopenharmony_ci * 338c2ecf20Sopenharmony_ci * @curve_id: ECC curve the key is based on. 348c2ecf20Sopenharmony_ci * @key: Private ECDH key 358c2ecf20Sopenharmony_ci * @key_size: Size of the private ECDH key 368c2ecf20Sopenharmony_ci */ 378c2ecf20Sopenharmony_cistruct ecdh { 388c2ecf20Sopenharmony_ci unsigned short curve_id; 398c2ecf20Sopenharmony_ci char *key; 408c2ecf20Sopenharmony_ci unsigned short key_size; 418c2ecf20Sopenharmony_ci}; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci/** 448c2ecf20Sopenharmony_ci * crypto_ecdh_key_len() - Obtain the size of the private ECDH key 458c2ecf20Sopenharmony_ci * @params: private ECDH key 468c2ecf20Sopenharmony_ci * 478c2ecf20Sopenharmony_ci * This function returns the packet ECDH key size. A caller can use that 488c2ecf20Sopenharmony_ci * with the provided ECDH private key reference to obtain the required 498c2ecf20Sopenharmony_ci * memory size to hold a packet key. 508c2ecf20Sopenharmony_ci * 518c2ecf20Sopenharmony_ci * Return: size of the key in bytes 528c2ecf20Sopenharmony_ci */ 538c2ecf20Sopenharmony_ciunsigned int crypto_ecdh_key_len(const struct ecdh *params); 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci/** 568c2ecf20Sopenharmony_ci * crypto_ecdh_encode_key() - encode the private key 578c2ecf20Sopenharmony_ci * @buf: Buffer allocated by the caller to hold the packet ECDH 588c2ecf20Sopenharmony_ci * private key. The buffer should be at least crypto_ecdh_key_len 598c2ecf20Sopenharmony_ci * bytes in size. 608c2ecf20Sopenharmony_ci * @len: Length of the packet private key buffer 618c2ecf20Sopenharmony_ci * @p: Buffer with the caller-specified private key 628c2ecf20Sopenharmony_ci * 638c2ecf20Sopenharmony_ci * The ECDH implementations operate on a packet representation of the private 648c2ecf20Sopenharmony_ci * key. 658c2ecf20Sopenharmony_ci * 668c2ecf20Sopenharmony_ci * Return: -EINVAL if buffer has insufficient size, 0 on success 678c2ecf20Sopenharmony_ci */ 688c2ecf20Sopenharmony_ciint crypto_ecdh_encode_key(char *buf, unsigned int len, const struct ecdh *p); 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci/** 718c2ecf20Sopenharmony_ci * crypto_ecdh_decode_key() - decode a private key 728c2ecf20Sopenharmony_ci * @buf: Buffer holding a packet key that should be decoded 738c2ecf20Sopenharmony_ci * @len: Length of the packet private key buffer 748c2ecf20Sopenharmony_ci * @p: Buffer allocated by the caller that is filled with the 758c2ecf20Sopenharmony_ci * unpacked ECDH private key. 768c2ecf20Sopenharmony_ci * 778c2ecf20Sopenharmony_ci * The unpacking obtains the private key by pointing @p to the correct location 788c2ecf20Sopenharmony_ci * in @buf. Thus, both pointers refer to the same memory. 798c2ecf20Sopenharmony_ci * 808c2ecf20Sopenharmony_ci * Return: -EINVAL if buffer has insufficient size, 0 on success 818c2ecf20Sopenharmony_ci */ 828c2ecf20Sopenharmony_ciint crypto_ecdh_decode_key(const char *buf, unsigned int len, struct ecdh *p); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci#endif 85