1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */ 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * Diffie-Hellman secret to be used with kpp API along with helper functions 4*4882a593Smuzhiyun * 5*4882a593Smuzhiyun * Copyright (c) 2016, Intel Corporation 6*4882a593Smuzhiyun * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com> 7*4882a593Smuzhiyun */ 8*4882a593Smuzhiyun #ifndef _CRYPTO_DH_ 9*4882a593Smuzhiyun #define _CRYPTO_DH_ 10*4882a593Smuzhiyun 11*4882a593Smuzhiyun /** 12*4882a593Smuzhiyun * DOC: DH Helper Functions 13*4882a593Smuzhiyun * 14*4882a593Smuzhiyun * To use DH with the KPP cipher API, the following data structure and 15*4882a593Smuzhiyun * functions should be used. 16*4882a593Smuzhiyun * 17*4882a593Smuzhiyun * To use DH with KPP, the following functions should be used to operate on 18*4882a593Smuzhiyun * a DH private key. The packet private key that can be set with 19*4882a593Smuzhiyun * the KPP API function call of crypto_kpp_set_secret. 20*4882a593Smuzhiyun */ 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun /** 23*4882a593Smuzhiyun * struct dh - define a DH private key 24*4882a593Smuzhiyun * 25*4882a593Smuzhiyun * @key: Private DH key 26*4882a593Smuzhiyun * @p: Diffie-Hellman parameter P 27*4882a593Smuzhiyun * @q: Diffie-Hellman parameter Q 28*4882a593Smuzhiyun * @g: Diffie-Hellman generator G 29*4882a593Smuzhiyun * @key_size: Size of the private DH key 30*4882a593Smuzhiyun * @p_size: Size of DH parameter P 31*4882a593Smuzhiyun * @q_size: Size of DH parameter Q 32*4882a593Smuzhiyun * @g_size: Size of DH generator G 33*4882a593Smuzhiyun */ 34*4882a593Smuzhiyun struct dh { 35*4882a593Smuzhiyun void *key; 36*4882a593Smuzhiyun void *p; 37*4882a593Smuzhiyun void *q; 38*4882a593Smuzhiyun void *g; 39*4882a593Smuzhiyun unsigned int key_size; 40*4882a593Smuzhiyun unsigned int p_size; 41*4882a593Smuzhiyun unsigned int q_size; 42*4882a593Smuzhiyun unsigned int g_size; 43*4882a593Smuzhiyun }; 44*4882a593Smuzhiyun 45*4882a593Smuzhiyun /** 46*4882a593Smuzhiyun * crypto_dh_key_len() - Obtain the size of the private DH key 47*4882a593Smuzhiyun * @params: private DH key 48*4882a593Smuzhiyun * 49*4882a593Smuzhiyun * This function returns the packet DH key size. A caller can use that 50*4882a593Smuzhiyun * with the provided DH private key reference to obtain the required 51*4882a593Smuzhiyun * memory size to hold a packet key. 52*4882a593Smuzhiyun * 53*4882a593Smuzhiyun * Return: size of the key in bytes 54*4882a593Smuzhiyun */ 55*4882a593Smuzhiyun unsigned int crypto_dh_key_len(const struct dh *params); 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun /** 58*4882a593Smuzhiyun * crypto_dh_encode_key() - encode the private key 59*4882a593Smuzhiyun * @buf: Buffer allocated by the caller to hold the packet DH 60*4882a593Smuzhiyun * private key. The buffer should be at least crypto_dh_key_len 61*4882a593Smuzhiyun * bytes in size. 62*4882a593Smuzhiyun * @len: Length of the packet private key buffer 63*4882a593Smuzhiyun * @params: Buffer with the caller-specified private key 64*4882a593Smuzhiyun * 65*4882a593Smuzhiyun * The DH implementations operate on a packet representation of the private 66*4882a593Smuzhiyun * key. 67*4882a593Smuzhiyun * 68*4882a593Smuzhiyun * Return: -EINVAL if buffer has insufficient size, 0 on success 69*4882a593Smuzhiyun */ 70*4882a593Smuzhiyun int crypto_dh_encode_key(char *buf, unsigned int len, const struct dh *params); 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun /** 73*4882a593Smuzhiyun * crypto_dh_decode_key() - decode a private key 74*4882a593Smuzhiyun * @buf: Buffer holding a packet key that should be decoded 75*4882a593Smuzhiyun * @len: Length of the packet private key buffer 76*4882a593Smuzhiyun * @params: Buffer allocated by the caller that is filled with the 77*4882a593Smuzhiyun * unpacked DH private key. 78*4882a593Smuzhiyun * 79*4882a593Smuzhiyun * The unpacking obtains the private key by pointing @p to the correct location 80*4882a593Smuzhiyun * in @buf. Thus, both pointers refer to the same memory. 81*4882a593Smuzhiyun * 82*4882a593Smuzhiyun * Return: -EINVAL if buffer has insufficient size, 0 on success 83*4882a593Smuzhiyun */ 84*4882a593Smuzhiyun int crypto_dh_decode_key(const char *buf, unsigned int len, struct dh *params); 85*4882a593Smuzhiyun 86*4882a593Smuzhiyun #endif 87