/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* Diffie-Hellman secret to be used with kpp API along with helper functions3*4* Copyright (c) 2016, Intel Corporation5* Authors: Salvatore Benedetto <[email protected]>6*/7#ifndef _CRYPTO_DH_8#define _CRYPTO_DH_910/**11* DOC: DH Helper Functions12*13* To use DH with the KPP cipher API, the following data structure and14* functions should be used.15*16* To use DH with KPP, the following functions should be used to operate on17* a DH private key. The packet private key that can be set with18* the KPP API function call of crypto_kpp_set_secret.19*/2021/**22* struct dh - define a DH private key23*24* @key: Private DH key25* @p: Diffie-Hellman parameter P26* @g: Diffie-Hellman generator G27* @key_size: Size of the private DH key28* @p_size: Size of DH parameter P29* @g_size: Size of DH generator G30*/31struct dh {32const void *key;33const void *p;34const void *g;35unsigned int key_size;36unsigned int p_size;37unsigned int g_size;38};3940/**41* crypto_dh_key_len() - Obtain the size of the private DH key42* @params: private DH key43*44* This function returns the packet DH key size. A caller can use that45* with the provided DH private key reference to obtain the required46* memory size to hold a packet key.47*48* Return: size of the key in bytes49*/50unsigned int crypto_dh_key_len(const struct dh *params);5152/**53* crypto_dh_encode_key() - encode the private key54* @buf: Buffer allocated by the caller to hold the packet DH55* private key. The buffer should be at least crypto_dh_key_len56* bytes in size.57* @len: Length of the packet private key buffer58* @params: Buffer with the caller-specified private key59*60* The DH implementations operate on a packet representation of the private61* key.62*63* Return: -EINVAL if buffer has insufficient size, 0 on success64*/65int crypto_dh_encode_key(char *buf, unsigned int len, const struct dh *params);6667/**68* crypto_dh_decode_key() - decode a private key69* @buf: Buffer holding a packet key that should be decoded70* @len: Length of the packet private key buffer71* @params: Buffer allocated by the caller that is filled with the72* unpacked DH private key.73*74* The unpacking obtains the private key by pointing @p to the correct location75* in @buf. Thus, both pointers refer to the same memory.76*77* Return: -EINVAL if buffer has insufficient size, 0 on success78*/79int crypto_dh_decode_key(const char *buf, unsigned int len, struct dh *params);8081/**82* __crypto_dh_decode_key() - decode a private key without parameter checks83* @buf: Buffer holding a packet key that should be decoded84* @len: Length of the packet private key buffer85* @params: Buffer allocated by the caller that is filled with the86* unpacked DH private key.87*88* Internal function providing the same services as the exported89* crypto_dh_decode_key(), but without any of those basic parameter90* checks conducted by the latter.91*92* Return: -EINVAL if buffer has insufficient size, 0 on success93*/94int __crypto_dh_decode_key(const char *buf, unsigned int len,95struct dh *params);9697#endif9899100