xref: /linux/include/crypto/internal/ecc.h (revision c6ab5c91)
1 /*
2  * Copyright (c) 2013, Kenneth MacKay
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *  * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #ifndef _CRYPTO_ECC_H
27 #define _CRYPTO_ECC_H
28 
29 #include <crypto/ecc_curve.h>
30 #include <asm/unaligned.h>
31 
32 /* One digit is u64 qword. */
33 #define ECC_CURVE_NIST_P192_DIGITS  3
34 #define ECC_CURVE_NIST_P256_DIGITS  4
35 #define ECC_CURVE_NIST_P384_DIGITS  6
36 #define ECC_CURVE_NIST_P521_DIGITS  9
37 #define ECC_MAX_DIGITS              DIV_ROUND_UP(521, 64) /* NIST P521 */
38 
39 #define ECC_DIGITS_TO_BYTES_SHIFT 3
40 
41 #define ECC_MAX_BYTES (ECC_MAX_DIGITS << ECC_DIGITS_TO_BYTES_SHIFT)
42 
43 #define ECC_POINT_INIT(x, y, ndigits)	(struct ecc_point) { x, y, ndigits }
44 
45 /**
46  * ecc_swap_digits() - Copy ndigits from big endian array to native array
47  * @in:       Input array
48  * @out:      Output array
49  * @ndigits:  Number of digits to copy
50  */
ecc_swap_digits(const void * in,u64 * out,unsigned int ndigits)51 static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigits)
52 {
53 	const __be64 *src = (__force __be64 *)in;
54 	int i;
55 
56 	for (i = 0; i < ndigits; i++)
57 		out[i] = get_unaligned_be64(&src[ndigits - 1 - i]);
58 }
59 
60 /**
61  * ecc_digits_from_bytes() - Create ndigits-sized digits array from byte array
62  * @in:       Input byte array
63  * @nbytes    Size of input byte array
64  * @out       Output digits array
65  * @ndigits:  Number of digits to create from byte array
66  */
67 void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,
68 			   u64 *out, unsigned int ndigits);
69 
70 /**
71  * ecc_is_key_valid() - Validate a given ECDH private key
72  *
73  * @curve_id:		id representing the curve to use
74  * @ndigits:		curve's number of digits
75  * @private_key:	private key to be used for the given curve
76  * @private_key_len:	private key length
77  *
78  * Returns 0 if the key is acceptable, a negative value otherwise
79  */
80 int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
81 		     const u64 *private_key, unsigned int private_key_len);
82 
83 /**
84  * ecc_gen_privkey() -  Generates an ECC private key.
85  * The private key is a random integer in the range 0 < random < n, where n is a
86  * prime that is the order of the cyclic subgroup generated by the distinguished
87  * point G.
88  * @curve_id:		id representing the curve to use
89  * @ndigits:		curve number of digits
90  * @private_key:	buffer for storing the generated private key
91  *
92  * Returns 0 if the private key was generated successfully, a negative value
93  * if an error occurred.
94  */
95 int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits,
96 		    u64 *private_key);
97 
98 /**
99  * ecc_make_pub_key() - Compute an ECC public key
100  *
101  * @curve_id:		id representing the curve to use
102  * @ndigits:		curve's number of digits
103  * @private_key:	pregenerated private key for the given curve
104  * @public_key:		buffer for storing the generated public key
105  *
106  * Returns 0 if the public key was generated successfully, a negative value
107  * if an error occurred.
108  */
109 int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits,
110 		     const u64 *private_key, u64 *public_key);
111 
112 /**
113  * crypto_ecdh_shared_secret() - Compute a shared secret
114  *
115  * @curve_id:		id representing the curve to use
116  * @ndigits:		curve's number of digits
117  * @private_key:	private key of part A
118  * @public_key:		public key of counterpart B
119  * @secret:		buffer for storing the calculated shared secret
120  *
121  * Note: It is recommended that you hash the result of crypto_ecdh_shared_secret
122  * before using it for symmetric encryption or HMAC.
123  *
124  * Returns 0 if the shared secret was generated successfully, a negative value
125  * if an error occurred.
126  */
127 int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
128 			      const u64 *private_key, const u64 *public_key,
129 			      u64 *secret);
130 
131 /**
132  * ecc_is_pubkey_valid_partial() - Partial public key validation
133  *
134  * @curve:		elliptic curve domain parameters
135  * @pk:			public key as a point
136  *
137  * Valdiate public key according to SP800-56A section 5.6.2.3.4 ECC Partial
138  * Public-Key Validation Routine.
139  *
140  * Note: There is no check that the public key is in the correct elliptic curve
141  * subgroup.
142  *
143  * Return: 0 if validation is successful, -EINVAL if validation is failed.
144  */
145 int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,
146 				struct ecc_point *pk);
147 
148 /**
149  * ecc_is_pubkey_valid_full() - Full public key validation
150  *
151  * @curve:		elliptic curve domain parameters
152  * @pk:			public key as a point
153  *
154  * Valdiate public key according to SP800-56A section 5.6.2.3.3 ECC Full
155  * Public-Key Validation Routine.
156  *
157  * Return: 0 if validation is successful, -EINVAL if validation is failed.
158  */
159 int ecc_is_pubkey_valid_full(const struct ecc_curve *curve,
160 			     struct ecc_point *pk);
161 
162 /**
163  * vli_is_zero() - Determine is vli is zero
164  *
165  * @vli:		vli to check.
166  * @ndigits:		length of the @vli
167  */
168 bool vli_is_zero(const u64 *vli, unsigned int ndigits);
169 
170 /**
171  * vli_cmp() - compare left and right vlis
172  *
173  * @left:		vli
174  * @right:		vli
175  * @ndigits:		length of both vlis
176  *
177  * Returns sign of @left - @right, i.e. -1 if @left < @right,
178  * 0 if @left == @right, 1 if @left > @right.
179  */
180 int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits);
181 
182 /**
183  * vli_sub() - Subtracts right from left
184  *
185  * @result:		where to write result
186  * @left:		vli
187  * @right		vli
188  * @ndigits:		length of all vlis
189  *
190  * Note: can modify in-place.
191  *
192  * Return: carry bit.
193  */
194 u64 vli_sub(u64 *result, const u64 *left, const u64 *right,
195 	    unsigned int ndigits);
196 
197 /**
198  * vli_from_be64() - Load vli from big-endian u64 array
199  *
200  * @dest:		destination vli
201  * @src:		source array of u64 BE values
202  * @ndigits:		length of both vli and array
203  */
204 void vli_from_be64(u64 *dest, const void *src, unsigned int ndigits);
205 
206 /**
207  * vli_from_le64() - Load vli from little-endian u64 array
208  *
209  * @dest:		destination vli
210  * @src:		source array of u64 LE values
211  * @ndigits:		length of both vli and array
212  */
213 void vli_from_le64(u64 *dest, const void *src, unsigned int ndigits);
214 
215 /**
216  * vli_mod_inv() - Modular inversion
217  *
218  * @result:		where to write vli number
219  * @input:		vli value to operate on
220  * @mod:		modulus
221  * @ndigits:		length of all vlis
222  */
223 void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,
224 		 unsigned int ndigits);
225 
226 /**
227  * vli_mod_mult_slow() - Modular multiplication
228  *
229  * @result:		where to write result value
230  * @left:		vli number to multiply with @right
231  * @right:		vli number to multiply with @left
232  * @mod:		modulus
233  * @ndigits:		length of all vlis
234  *
235  * Note: Assumes that mod is big enough curve order.
236  */
237 void vli_mod_mult_slow(u64 *result, const u64 *left, const u64 *right,
238 		       const u64 *mod, unsigned int ndigits);
239 
240 /**
241  * vli_num_bits() - Counts the number of bits required for vli.
242  *
243  * @vli:		vli to check.
244  * @ndigits:		Length of the @vli
245  *
246  * Return: The number of bits required to represent @vli.
247  */
248 unsigned int vli_num_bits(const u64 *vli, unsigned int ndigits);
249 
250 /**
251  * ecc_aloc_point() - Allocate ECC point.
252  *
253  * @ndigits:		Length of vlis in u64 qwords.
254  *
255  * Return: Pointer to the allocated point or NULL if allocation failed.
256  */
257 struct ecc_point *ecc_alloc_point(unsigned int ndigits);
258 
259 /**
260  * ecc_free_point() - Free ECC point.
261  *
262  * @p:			The point to free.
263  */
264 void ecc_free_point(struct ecc_point *p);
265 
266 /**
267  * ecc_point_is_zero() - Check if point is zero.
268  *
269  * @p:			Point to check for zero.
270  *
271  * Return: true if point is the point at infinity, false otherwise.
272  */
273 bool ecc_point_is_zero(const struct ecc_point *point);
274 
275 /**
276  * ecc_point_mult_shamir() - Add two points multiplied by scalars
277  *
278  * @result:		resulting point
279  * @x:			scalar to multiply with @p
280  * @p:			point to multiply with @x
281  * @y:			scalar to multiply with @q
282  * @q:			point to multiply with @y
283  * @curve:		curve
284  *
285  * Returns result = x * p + x * q over the curve.
286  * This works faster than two multiplications and addition.
287  */
288 void ecc_point_mult_shamir(const struct ecc_point *result,
289 			   const u64 *x, const struct ecc_point *p,
290 			   const u64 *y, const struct ecc_point *q,
291 			   const struct ecc_curve *curve);
292 
293 #endif
294