xref: /OK3568_Linux_fs/kernel/include/crypto/ecdh.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * ECDH params to be used with kpp API
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2016, Intel Corporation
6*4882a593Smuzhiyun  * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun #ifndef _CRYPTO_ECDH_
9*4882a593Smuzhiyun #define _CRYPTO_ECDH_
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /**
12*4882a593Smuzhiyun  * DOC: ECDH Helper Functions
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * To use ECDH with the KPP cipher API, the following data structure and
15*4882a593Smuzhiyun  * functions should be used.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * The ECC curves known to the ECDH implementation are specified in this
18*4882a593Smuzhiyun  * header file.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * To use ECDH with KPP, the following functions should be used to operate on
21*4882a593Smuzhiyun  * an ECDH private key. The packet private key that can be set with
22*4882a593Smuzhiyun  * the KPP API function call of crypto_kpp_set_secret.
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /* Curves IDs */
26*4882a593Smuzhiyun #define ECC_CURVE_NIST_P192	0x0001
27*4882a593Smuzhiyun #define ECC_CURVE_NIST_P256	0x0002
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /**
30*4882a593Smuzhiyun  * struct ecdh - define an ECDH private key
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * @curve_id:	ECC curve the key is based on.
33*4882a593Smuzhiyun  * @key:	Private ECDH key
34*4882a593Smuzhiyun  * @key_size:	Size of the private ECDH key
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun struct ecdh {
37*4882a593Smuzhiyun 	unsigned short curve_id;
38*4882a593Smuzhiyun 	char *key;
39*4882a593Smuzhiyun 	unsigned short key_size;
40*4882a593Smuzhiyun };
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /**
43*4882a593Smuzhiyun  * crypto_ecdh_key_len() - Obtain the size of the private ECDH key
44*4882a593Smuzhiyun  * @params:	private ECDH key
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * This function returns the packet ECDH key size. A caller can use that
47*4882a593Smuzhiyun  * with the provided ECDH private key reference to obtain the required
48*4882a593Smuzhiyun  * memory size to hold a packet key.
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * Return: size of the key in bytes
51*4882a593Smuzhiyun  */
52*4882a593Smuzhiyun unsigned int crypto_ecdh_key_len(const struct ecdh *params);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /**
55*4882a593Smuzhiyun  * crypto_ecdh_encode_key() - encode the private key
56*4882a593Smuzhiyun  * @buf:	Buffer allocated by the caller to hold the packet ECDH
57*4882a593Smuzhiyun  *		private key. The buffer should be at least crypto_ecdh_key_len
58*4882a593Smuzhiyun  *		bytes in size.
59*4882a593Smuzhiyun  * @len:	Length of the packet private key buffer
60*4882a593Smuzhiyun  * @p:		Buffer with the caller-specified private key
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * The ECDH implementations operate on a packet representation of the private
63*4882a593Smuzhiyun  * key.
64*4882a593Smuzhiyun  *
65*4882a593Smuzhiyun  * Return:	-EINVAL if buffer has insufficient size, 0 on success
66*4882a593Smuzhiyun  */
67*4882a593Smuzhiyun int crypto_ecdh_encode_key(char *buf, unsigned int len, const struct ecdh *p);
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /**
70*4882a593Smuzhiyun  * crypto_ecdh_decode_key() - decode a private key
71*4882a593Smuzhiyun  * @buf:	Buffer holding a packet key that should be decoded
72*4882a593Smuzhiyun  * @len:	Length of the packet private key buffer
73*4882a593Smuzhiyun  * @p:		Buffer allocated by the caller that is filled with the
74*4882a593Smuzhiyun  *		unpacked ECDH private key.
75*4882a593Smuzhiyun  *
76*4882a593Smuzhiyun  * The unpacking obtains the private key by pointing @p to the correct location
77*4882a593Smuzhiyun  * in @buf. Thus, both pointers refer to the same memory.
78*4882a593Smuzhiyun  *
79*4882a593Smuzhiyun  * Return:	-EINVAL if buffer has insufficient size, 0 on success
80*4882a593Smuzhiyun  */
81*4882a593Smuzhiyun int crypto_ecdh_decode_key(const char *buf, unsigned int len, struct ecdh *p);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun #endif
84