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