162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Diffie-Hellman secret to be used with kpp API along with helper functions 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (c) 2016, Intel Corporation 662306a36Sopenharmony_ci * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com> 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci#ifndef _CRYPTO_DH_ 962306a36Sopenharmony_ci#define _CRYPTO_DH_ 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci/** 1262306a36Sopenharmony_ci * DOC: DH Helper Functions 1362306a36Sopenharmony_ci * 1462306a36Sopenharmony_ci * To use DH with the KPP cipher API, the following data structure and 1562306a36Sopenharmony_ci * functions should be used. 1662306a36Sopenharmony_ci * 1762306a36Sopenharmony_ci * To use DH with KPP, the following functions should be used to operate on 1862306a36Sopenharmony_ci * a DH private key. The packet private key that can be set with 1962306a36Sopenharmony_ci * the KPP API function call of crypto_kpp_set_secret. 2062306a36Sopenharmony_ci */ 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci/** 2362306a36Sopenharmony_ci * struct dh - define a DH private key 2462306a36Sopenharmony_ci * 2562306a36Sopenharmony_ci * @key: Private DH key 2662306a36Sopenharmony_ci * @p: Diffie-Hellman parameter P 2762306a36Sopenharmony_ci * @g: Diffie-Hellman generator G 2862306a36Sopenharmony_ci * @key_size: Size of the private DH key 2962306a36Sopenharmony_ci * @p_size: Size of DH parameter P 3062306a36Sopenharmony_ci * @g_size: Size of DH generator G 3162306a36Sopenharmony_ci */ 3262306a36Sopenharmony_cistruct dh { 3362306a36Sopenharmony_ci const void *key; 3462306a36Sopenharmony_ci const void *p; 3562306a36Sopenharmony_ci const void *g; 3662306a36Sopenharmony_ci unsigned int key_size; 3762306a36Sopenharmony_ci unsigned int p_size; 3862306a36Sopenharmony_ci unsigned int g_size; 3962306a36Sopenharmony_ci}; 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci/** 4262306a36Sopenharmony_ci * crypto_dh_key_len() - Obtain the size of the private DH key 4362306a36Sopenharmony_ci * @params: private DH key 4462306a36Sopenharmony_ci * 4562306a36Sopenharmony_ci * This function returns the packet DH key size. A caller can use that 4662306a36Sopenharmony_ci * with the provided DH 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_dh_key_len(const struct dh *params); 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci/** 5462306a36Sopenharmony_ci * crypto_dh_encode_key() - encode the private key 5562306a36Sopenharmony_ci * @buf: Buffer allocated by the caller to hold the packet DH 5662306a36Sopenharmony_ci * private key. The buffer should be at least crypto_dh_key_len 5762306a36Sopenharmony_ci * bytes in size. 5862306a36Sopenharmony_ci * @len: Length of the packet private key buffer 5962306a36Sopenharmony_ci * @params: Buffer with the caller-specified private key 6062306a36Sopenharmony_ci * 6162306a36Sopenharmony_ci * The DH 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_dh_encode_key(char *buf, unsigned int len, const struct dh *params); 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci/** 6962306a36Sopenharmony_ci * crypto_dh_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 * @params: Buffer allocated by the caller that is filled with the 7362306a36Sopenharmony_ci * unpacked DH 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_dh_decode_key(const char *buf, unsigned int len, struct dh *params); 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci/** 8362306a36Sopenharmony_ci * __crypto_dh_decode_key() - decode a private key without parameter checks 8462306a36Sopenharmony_ci * @buf: Buffer holding a packet key that should be decoded 8562306a36Sopenharmony_ci * @len: Length of the packet private key buffer 8662306a36Sopenharmony_ci * @params: Buffer allocated by the caller that is filled with the 8762306a36Sopenharmony_ci * unpacked DH private key. 8862306a36Sopenharmony_ci * 8962306a36Sopenharmony_ci * Internal function providing the same services as the exported 9062306a36Sopenharmony_ci * crypto_dh_decode_key(), but without any of those basic parameter 9162306a36Sopenharmony_ci * checks conducted by the latter. 9262306a36Sopenharmony_ci * 9362306a36Sopenharmony_ci * Return: -EINVAL if buffer has insufficient size, 0 on success 9462306a36Sopenharmony_ci */ 9562306a36Sopenharmony_ciint __crypto_dh_decode_key(const char *buf, unsigned int len, 9662306a36Sopenharmony_ci struct dh *params); 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci#endif 99