xref: /openbmc/linux/include/crypto/dh.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2802c7f1cSSalvatore Benedetto /*
3802c7f1cSSalvatore Benedetto  * Diffie-Hellman secret to be used with kpp API along with helper functions
4802c7f1cSSalvatore Benedetto  *
5802c7f1cSSalvatore Benedetto  * Copyright (c) 2016, Intel Corporation
6802c7f1cSSalvatore Benedetto  * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com>
7802c7f1cSSalvatore Benedetto  */
8802c7f1cSSalvatore Benedetto #ifndef _CRYPTO_DH_
9802c7f1cSSalvatore Benedetto #define _CRYPTO_DH_
10802c7f1cSSalvatore Benedetto 
118d23da22SStephan Mueller /**
128d23da22SStephan Mueller  * DOC: DH Helper Functions
138d23da22SStephan Mueller  *
148d23da22SStephan Mueller  * To use DH with the KPP cipher API, the following data structure and
158d23da22SStephan Mueller  * functions should be used.
168d23da22SStephan Mueller  *
178d23da22SStephan Mueller  * To use DH with KPP, the following functions should be used to operate on
188d23da22SStephan Mueller  * a DH private key. The packet private key that can be set with
198d23da22SStephan Mueller  * the KPP API function call of crypto_kpp_set_secret.
208d23da22SStephan Mueller  */
218d23da22SStephan Mueller 
228d23da22SStephan Mueller /**
238d23da22SStephan Mueller  * struct dh - define a DH private key
248d23da22SStephan Mueller  *
258d23da22SStephan Mueller  * @key:	Private DH key
268d23da22SStephan Mueller  * @p:		Diffie-Hellman parameter P
278d23da22SStephan Mueller  * @g:		Diffie-Hellman generator G
288d23da22SStephan Mueller  * @key_size:	Size of the private DH key
298d23da22SStephan Mueller  * @p_size:	Size of DH parameter P
308d23da22SStephan Mueller  * @g_size:	Size of DH generator G
318d23da22SStephan Mueller  */
32802c7f1cSSalvatore Benedetto struct dh {
33215bebc8SNicolai Stange 	const void *key;
34215bebc8SNicolai Stange 	const void *p;
35215bebc8SNicolai Stange 	const void *g;
36802c7f1cSSalvatore Benedetto 	unsigned int key_size;
37802c7f1cSSalvatore Benedetto 	unsigned int p_size;
38802c7f1cSSalvatore Benedetto 	unsigned int g_size;
39802c7f1cSSalvatore Benedetto };
40802c7f1cSSalvatore Benedetto 
418d23da22SStephan Mueller /**
428d23da22SStephan Mueller  * crypto_dh_key_len() - Obtain the size of the private DH key
438d23da22SStephan Mueller  * @params:	private DH key
448d23da22SStephan Mueller  *
458d23da22SStephan Mueller  * This function returns the packet DH key size. A caller can use that
468d23da22SStephan Mueller  * with the provided DH private key reference to obtain the required
478d23da22SStephan Mueller  * memory size to hold a packet key.
488d23da22SStephan Mueller  *
498d23da22SStephan Mueller  * Return: size of the key in bytes
508d23da22SStephan Mueller  */
515b3f3a8bSTudor-Dan Ambarus unsigned int crypto_dh_key_len(const struct dh *params);
528d23da22SStephan Mueller 
538d23da22SStephan Mueller /**
548d23da22SStephan Mueller  * crypto_dh_encode_key() - encode the private key
558d23da22SStephan Mueller  * @buf:	Buffer allocated by the caller to hold the packet DH
568d23da22SStephan Mueller  *		private key. The buffer should be at least crypto_dh_key_len
578d23da22SStephan Mueller  *		bytes in size.
588d23da22SStephan Mueller  * @len:	Length of the packet private key buffer
598d23da22SStephan Mueller  * @params:	Buffer with the caller-specified private key
608d23da22SStephan Mueller  *
618d23da22SStephan Mueller  * The DH implementations operate on a packet representation of the private
628d23da22SStephan Mueller  * key.
638d23da22SStephan Mueller  *
648d23da22SStephan Mueller  * Return:	-EINVAL if buffer has insufficient size, 0 on success
658d23da22SStephan Mueller  */
66802c7f1cSSalvatore Benedetto int crypto_dh_encode_key(char *buf, unsigned int len, const struct dh *params);
678d23da22SStephan Mueller 
688d23da22SStephan Mueller /**
698d23da22SStephan Mueller  * crypto_dh_decode_key() - decode a private key
708d23da22SStephan Mueller  * @buf:	Buffer holding a packet key that should be decoded
71c0ca1215STudor-Dan Ambarus  * @len:	Length of the packet private key buffer
728d23da22SStephan Mueller  * @params:	Buffer allocated by the caller that is filled with the
73c0ca1215STudor-Dan Ambarus  *		unpacked DH private key.
748d23da22SStephan Mueller  *
758d23da22SStephan Mueller  * The unpacking obtains the private key by pointing @p to the correct location
768d23da22SStephan Mueller  * in @buf. Thus, both pointers refer to the same memory.
778d23da22SStephan Mueller  *
788d23da22SStephan Mueller  * Return:	-EINVAL if buffer has insufficient size, 0 on success
798d23da22SStephan Mueller  */
80802c7f1cSSalvatore Benedetto int crypto_dh_decode_key(const char *buf, unsigned int len, struct dh *params);
81802c7f1cSSalvatore Benedetto 
82*fae19893SNicolai Stange /**
83*fae19893SNicolai Stange  * __crypto_dh_decode_key() - decode a private key without parameter checks
84*fae19893SNicolai Stange  * @buf:	Buffer holding a packet key that should be decoded
85*fae19893SNicolai Stange  * @len:	Length of the packet private key buffer
86*fae19893SNicolai Stange  * @params:	Buffer allocated by the caller that is filled with the
87*fae19893SNicolai Stange  *		unpacked DH private key.
88*fae19893SNicolai Stange  *
89*fae19893SNicolai Stange  * Internal function providing the same services as the exported
90*fae19893SNicolai Stange  * crypto_dh_decode_key(), but without any of those basic parameter
91*fae19893SNicolai Stange  * checks conducted by the latter.
92*fae19893SNicolai Stange  *
93*fae19893SNicolai Stange  * Return:	-EINVAL if buffer has insufficient size, 0 on success
94*fae19893SNicolai Stange  */
95*fae19893SNicolai Stange int __crypto_dh_decode_key(const char *buf, unsigned int len,
96*fae19893SNicolai Stange 			   struct dh *params);
97*fae19893SNicolai Stange 
98802c7f1cSSalvatore Benedetto #endif
99