xref: /openbmc/linux/include/crypto/internal/ecc.h (revision 9144f784f852f9a125cabe9927b986d909bfa439)
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_MAX_DIGITS              (512 / 64) /* due to ecrdsa */
37 
38 #define ECC_DIGITS_TO_BYTES_SHIFT 3
39 
40 #define ECC_MAX_BYTES (ECC_MAX_DIGITS << ECC_DIGITS_TO_BYTES_SHIFT)
41 
42 #define ECC_POINT_INIT(x, y, ndigits)	(struct ecc_point) { x, y, ndigits }
43 
44 /**
45  * ecc_swap_digits() - Copy ndigits from big endian array to native array
46  * @in:       Input array
47  * @out:      Output array
48  * @ndigits:  Number of digits to copy
49  */
ecc_swap_digits(const void * in,u64 * out,unsigned int ndigits)50 static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigits)
51 {
52 	const __be64 *src = (__force __be64 *)in;
53 	int i;
54 
55 	for (i = 0; i < ndigits; i++)
56 		out[i] = get_unaligned_be64(&src[ndigits - 1 - i]);
57 }
58 
59 /**
60  * ecc_digits_from_bytes() - Create ndigits-sized digits array from byte array
61  * @in:       Input byte array
62  * @nbytes    Size of input byte array
63  * @out       Output digits array
64  * @ndigits:  Number of digits to create from byte array
65  */
66 void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,
67 			   u64 *out, unsigned int ndigits);
68 
69 /**
70  * ecc_is_key_valid() - Validate a given ECDH private key
71  *
72  * @curve_id:		id representing the curve to use
73  * @ndigits:		curve's number of digits
74  * @private_key:	private key to be used for the given curve
75  * @private_key_len:	private key length
76  *
77  * Returns 0 if the key is acceptable, a negative value otherwise
78  */
79 int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
80 		     const u64 *private_key, unsigned int private_key_len);
81 
82 /**
83  * ecc_gen_privkey() -  Generates an ECC private key.
84  * The private key is a random integer in the range 0 < random < n, where n is a
85  * prime that is the order of the cyclic subgroup generated by the distinguished
86  * point G.
87  * @curve_id:		id representing the curve to use
88  * @ndigits:		curve number of digits
89  * @private_key:	buffer for storing the generated private key
90  *
91  * Returns 0 if the private key was generated successfully, a negative value
92  * if an error occurred.
93  */
94 int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey);
95 
96 /**
97  * ecc_make_pub_key() - Compute an ECC public key
98  *
99  * @curve_id:		id representing the curve to use
100  * @ndigits:		curve's number of digits
101  * @private_key:	pregenerated private key for the given curve
102  * @public_key:		buffer for storing the generated public key
103  *
104  * Returns 0 if the public key was generated successfully, a negative value
105  * if an error occurred.
106  */
107 int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits,
108 		     const u64 *private_key, u64 *public_key);
109 
110 /**
111  * crypto_ecdh_shared_secret() - Compute a shared secret
112  *
113  * @curve_id:		id representing the curve to use
114  * @ndigits:		curve's number of digits
115  * @private_key:	private key of part A
116  * @public_key:		public key of counterpart B
117  * @secret:		buffer for storing the calculated shared secret
118  *
119  * Note: It is recommended that you hash the result of crypto_ecdh_shared_secret
120  * before using it for symmetric encryption or HMAC.
121  *
122  * Returns 0 if the shared secret was generated successfully, a negative value
123  * if an error occurred.
124  */
125 int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
126 			      const u64 *private_key, const u64 *public_key,
127 			      u64 *secret);
128 
129 /**
130  * ecc_is_pubkey_valid_partial() - Partial public key validation
131  *
132  * @curve:		elliptic curve domain parameters
133  * @pk:			public key as a point
134  *
135  * Valdiate public key according to SP800-56A section 5.6.2.3.4 ECC Partial
136  * Public-Key Validation Routine.
137  *
138  * Note: There is no check that the public key is in the correct elliptic curve
139  * subgroup.
140  *
141  * Return: 0 if validation is successful, -EINVAL if validation is failed.
142  */
143 int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,
144 				struct ecc_point *pk);
145 
146 /**
147  * ecc_is_pubkey_valid_full() - Full public key validation
148  *
149  * @curve:		elliptic curve domain parameters
150  * @pk:			public key as a point
151  *
152  * Valdiate public key according to SP800-56A section 5.6.2.3.3 ECC Full
153  * Public-Key Validation Routine.
154  *
155  * Return: 0 if validation is successful, -EINVAL if validation is failed.
156  */
157 int ecc_is_pubkey_valid_full(const struct ecc_curve *curve,
158 			     struct ecc_point *pk);
159 
160 /**
161  * vli_is_zero() - Determine is vli is zero
162  *
163  * @vli:		vli to check.
164  * @ndigits:		length of the @vli
165  */
166 bool vli_is_zero(const u64 *vli, unsigned int ndigits);
167 
168 /**
169  * vli_cmp() - compare left and right vlis
170  *
171  * @left:		vli
172  * @right:		vli
173  * @ndigits:		length of both vlis
174  *
175  * Returns sign of @left - @right, i.e. -1 if @left < @right,
176  * 0 if @left == @right, 1 if @left > @right.
177  */
178 int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits);
179 
180 /**
181  * vli_sub() - Subtracts right from left
182  *
183  * @result:		where to write result
184  * @left:		vli
185  * @right		vli
186  * @ndigits:		length of all vlis
187  *
188  * Note: can modify in-place.
189  *
190  * Return: carry bit.
191  */
192 u64 vli_sub(u64 *result, const u64 *left, const u64 *right,
193 	    unsigned int ndigits);
194 
195 /**
196  * vli_from_be64() - Load vli from big-endian u64 array
197  *
198  * @dest:		destination vli
199  * @src:		source array of u64 BE values
200  * @ndigits:		length of both vli and array
201  */
202 void vli_from_be64(u64 *dest, const void *src, unsigned int ndigits);
203 
204 /**
205  * vli_from_le64() - Load vli from little-endian u64 array
206  *
207  * @dest:		destination vli
208  * @src:		source array of u64 LE values
209  * @ndigits:		length of both vli and array
210  */
211 void vli_from_le64(u64 *dest, const void *src, unsigned int ndigits);
212 
213 /**
214  * vli_mod_inv() - Modular inversion
215  *
216  * @result:		where to write vli number
217  * @input:		vli value to operate on
218  * @mod:		modulus
219  * @ndigits:		length of all vlis
220  */
221 void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,
222 		 unsigned int ndigits);
223 
224 /**
225  * vli_mod_mult_slow() - Modular multiplication
226  *
227  * @result:		where to write result value
228  * @left:		vli number to multiply with @right
229  * @right:		vli number to multiply with @left
230  * @mod:		modulus
231  * @ndigits:		length of all vlis
232  *
233  * Note: Assumes that mod is big enough curve order.
234  */
235 void vli_mod_mult_slow(u64 *result, const u64 *left, const u64 *right,
236 		       const u64 *mod, unsigned int ndigits);
237 
238 /**
239  * vli_num_bits() - Counts the number of bits required for vli.
240  *
241  * @vli:		vli to check.
242  * @ndigits:		Length of the @vli
243  *
244  * Return: The number of bits required to represent @vli.
245  */
246 unsigned int vli_num_bits(const u64 *vli, unsigned int ndigits);
247 
248 /**
249  * ecc_aloc_point() - Allocate ECC point.
250  *
251  * @ndigits:		Length of vlis in u64 qwords.
252  *
253  * Return: Pointer to the allocated point or NULL if allocation failed.
254  */
255 struct ecc_point *ecc_alloc_point(unsigned int ndigits);
256 
257 /**
258  * ecc_free_point() - Free ECC point.
259  *
260  * @p:			The point to free.
261  */
262 void ecc_free_point(struct ecc_point *p);
263 
264 /**
265  * ecc_point_is_zero() - Check if point is zero.
266  *
267  * @p:			Point to check for zero.
268  *
269  * Return: true if point is the point at infinity, false otherwise.
270  */
271 bool ecc_point_is_zero(const struct ecc_point *point);
272 
273 /**
274  * ecc_point_mult_shamir() - Add two points multiplied by scalars
275  *
276  * @result:		resulting point
277  * @x:			scalar to multiply with @p
278  * @p:			point to multiply with @x
279  * @y:			scalar to multiply with @q
280  * @q:			point to multiply with @y
281  * @curve:		curve
282  *
283  * Returns result = x * p + x * q over the curve.
284  * This works faster than two multiplications and addition.
285  */
286 void ecc_point_mult_shamir(const struct ecc_point *result,
287 			   const u64 *x, const struct ecc_point *p,
288 			   const u64 *y, const struct ecc_point *q,
289 			   const struct ecc_curve *curve);
290 
291 #endif
292