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