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