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