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