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