blob: 1350e8eb6ac238d5bc67c785d4253fa887c587d8 [file] [log] [blame]
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01001/*
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
Meng Yu14bb7672021-03-04 14:35:47 +080029#include <crypto/ecc_curve.h>
Mian Yousaf Kaukab0469ded2021-07-21 10:39:05 +020030#include <asm/unaligned.h>
Meng Yu14bb7672021-03-04 14:35:47 +080031
Vitaly Chikunov0d7a7862019-04-11 18:51:20 +030032/* One digit is u64 qword. */
Kees Cookd5c3b172018-03-30 09:55:44 -070033#define ECC_CURVE_NIST_P192_DIGITS 3
34#define ECC_CURVE_NIST_P256_DIGITS 4
Saulo Alessandre149ca162021-03-16 17:07:34 -040035#define ECC_CURVE_NIST_P384_DIGITS 6
36#define ECC_MAX_DIGITS (512 / 64) /* due to ecrdsa */
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010037
38#define ECC_DIGITS_TO_BYTES_SHIFT 3
39
Stefan Berger4e660292021-03-16 17:07:32 -040040#define ECC_MAX_BYTES (ECC_MAX_DIGITS << ECC_DIGITS_TO_BYTES_SHIFT)
41
Vitaly Chikunov0d7a7862019-04-11 18:51:20 +030042#define ECC_POINT_INIT(x, y, ndigits) (struct ecc_point) { x, y, ndigits }
43
Vitaly Chikunov4a2289d2019-04-11 18:51:19 +030044/**
Stefan Berger4e660292021-03-16 17:07:32 -040045 * ecc_swap_digits() - Copy ndigits from big endian array to native array
46 * @in: Input array
47 * @out: Output array
48 * @ndigits: Number of digits to copy
49 */
Mian Yousaf Kaukab0469ded2021-07-21 10:39:05 +020050static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigits)
Stefan Berger4e660292021-03-16 17:07:32 -040051{
52 const __be64 *src = (__force __be64 *)in;
53 int i;
54
55 for (i = 0; i < ndigits; i++)
Mian Yousaf Kaukab0469ded2021-07-21 10:39:05 +020056 out[i] = get_unaligned_be64(&src[ndigits - 1 - i]);
Stefan Berger4e660292021-03-16 17:07:32 -040057}
58
59/**
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010060 * ecc_is_key_valid() - Validate a given ECDH private key
61 *
62 * @curve_id: id representing the curve to use
Tudor-Dan Ambarusc0ca1212017-05-25 10:18:03 +030063 * @ndigits: curve's number of digits
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010064 * @private_key: private key to be used for the given curve
Tudor-Dan Ambarusc0ca1212017-05-25 10:18:03 +030065 * @private_key_len: private key length
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010066 *
67 * Returns 0 if the key is acceptable, a negative value otherwise
68 */
69int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
Tudor-Dan Ambarusad269592017-05-25 10:18:05 +030070 const u64 *private_key, unsigned int private_key_len);
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010071
72/**
Tudor-Dan Ambarus6755fd22017-05-30 17:52:48 +030073 * ecc_gen_privkey() - Generates an ECC private key.
74 * The private key is a random integer in the range 0 < random < n, where n is a
75 * prime that is the order of the cyclic subgroup generated by the distinguished
76 * point G.
77 * @curve_id: id representing the curve to use
78 * @ndigits: curve number of digits
79 * @private_key: buffer for storing the generated private key
80 *
81 * Returns 0 if the private key was generated successfully, a negative value
82 * if an error occurred.
83 */
84int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey);
85
86/**
Tudor-Dan Ambarus7380c562017-05-30 15:37:56 +030087 * ecc_make_pub_key() - Compute an ECC public key
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010088 *
89 * @curve_id: id representing the curve to use
Tudor-Dan Ambarusc0ca1212017-05-25 10:18:03 +030090 * @ndigits: curve's number of digits
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010091 * @private_key: pregenerated private key for the given curve
Tudor-Dan Ambarusc0ca1212017-05-25 10:18:03 +030092 * @public_key: buffer for storing the generated public key
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010093 *
94 * Returns 0 if the public key was generated successfully, a negative value
95 * if an error occurred.
96 */
Tudor-Dan Ambarus7380c562017-05-30 15:37:56 +030097int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits,
98 const u64 *private_key, u64 *public_key);
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010099
100/**
Stephen Rothwell8f44df12016-06-24 16:20:22 +1000101 * crypto_ecdh_shared_secret() - Compute a shared secret
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +0100102 *
103 * @curve_id: id representing the curve to use
Tudor-Dan Ambarusc0ca1212017-05-25 10:18:03 +0300104 * @ndigits: curve's number of digits
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +0100105 * @private_key: private key of part A
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +0100106 * @public_key: public key of counterpart B
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +0100107 * @secret: buffer for storing the calculated shared secret
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +0100108 *
Stephen Rothwell8f44df12016-06-24 16:20:22 +1000109 * Note: It is recommended that you hash the result of crypto_ecdh_shared_secret
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +0100110 * before using it for symmetric encryption or HMAC.
111 *
112 * Returns 0 if the shared secret was generated successfully, a negative value
113 * if an error occurred.
114 */
Stephen Rothwell8f44df12016-06-24 16:20:22 +1000115int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
Tudor-Dan Ambarusad269592017-05-25 10:18:05 +0300116 const u64 *private_key, const u64 *public_key,
117 u64 *secret);
Vitaly Chikunov4a2289d2019-04-11 18:51:19 +0300118
119/**
120 * ecc_is_pubkey_valid_partial() - Partial public key validation
121 *
122 * @curve: elliptic curve domain parameters
123 * @pk: public key as a point
124 *
125 * Valdiate public key according to SP800-56A section 5.6.2.3.4 ECC Partial
126 * Public-Key Validation Routine.
127 *
128 * Note: There is no check that the public key is in the correct elliptic curve
129 * subgroup.
130 *
131 * Return: 0 if validation is successful, -EINVAL if validation is failed.
132 */
133int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,
134 struct ecc_point *pk);
135
136/**
Stephan Müller6914dd52020-07-20 19:09:23 +0200137 * ecc_is_pubkey_valid_full() - Full public key validation
138 *
139 * @curve: elliptic curve domain parameters
140 * @pk: public key as a point
141 *
142 * Valdiate public key according to SP800-56A section 5.6.2.3.3 ECC Full
143 * Public-Key Validation Routine.
144 *
145 * Return: 0 if validation is successful, -EINVAL if validation is failed.
146 */
147int ecc_is_pubkey_valid_full(const struct ecc_curve *curve,
148 struct ecc_point *pk);
149
150/**
Vitaly Chikunov4a2289d2019-04-11 18:51:19 +0300151 * vli_is_zero() - Determine is vli is zero
152 *
153 * @vli: vli to check.
154 * @ndigits: length of the @vli
155 */
156bool vli_is_zero(const u64 *vli, unsigned int ndigits);
157
158/**
159 * vli_cmp() - compare left and right vlis
160 *
161 * @left: vli
162 * @right: vli
163 * @ndigits: length of both vlis
164 *
165 * Returns sign of @left - @right, i.e. -1 if @left < @right,
166 * 0 if @left == @right, 1 if @left > @right.
167 */
168int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits);
169
170/**
171 * vli_sub() - Subtracts right from left
172 *
173 * @result: where to write result
174 * @left: vli
175 * @right vli
176 * @ndigits: length of all vlis
177 *
178 * Note: can modify in-place.
179 *
180 * Return: carry bit.
181 */
182u64 vli_sub(u64 *result, const u64 *left, const u64 *right,
183 unsigned int ndigits);
184
185/**
Vitaly Chikunov0d7a7862019-04-11 18:51:20 +0300186 * vli_from_be64() - Load vli from big-endian u64 array
187 *
188 * @dest: destination vli
189 * @src: source array of u64 BE values
190 * @ndigits: length of both vli and array
191 */
192void vli_from_be64(u64 *dest, const void *src, unsigned int ndigits);
193
194/**
195 * vli_from_le64() - Load vli from little-endian u64 array
196 *
197 * @dest: destination vli
198 * @src: source array of u64 LE values
199 * @ndigits: length of both vli and array
200 */
201void vli_from_le64(u64 *dest, const void *src, unsigned int ndigits);
202
203/**
Vitaly Chikunov4a2289d2019-04-11 18:51:19 +0300204 * vli_mod_inv() - Modular inversion
205 *
206 * @result: where to write vli number
207 * @input: vli value to operate on
208 * @mod: modulus
209 * @ndigits: length of all vlis
210 */
211void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,
212 unsigned int ndigits);
213
Vitaly Chikunov0d7a7862019-04-11 18:51:20 +0300214/**
215 * vli_mod_mult_slow() - Modular multiplication
216 *
217 * @result: where to write result value
218 * @left: vli number to multiply with @right
219 * @right: vli number to multiply with @left
220 * @mod: modulus
221 * @ndigits: length of all vlis
222 *
223 * Note: Assumes that mod is big enough curve order.
224 */
225void vli_mod_mult_slow(u64 *result, const u64 *left, const u64 *right,
226 const u64 *mod, unsigned int ndigits);
227
228/**
229 * ecc_point_mult_shamir() - Add two points multiplied by scalars
230 *
231 * @result: resulting point
232 * @x: scalar to multiply with @p
233 * @p: point to multiply with @x
234 * @y: scalar to multiply with @q
235 * @q: point to multiply with @y
236 * @curve: curve
237 *
238 * Returns result = x * p + x * q over the curve.
239 * This works faster than two multiplications and addition.
240 */
241void ecc_point_mult_shamir(const struct ecc_point *result,
242 const u64 *x, const struct ecc_point *p,
243 const u64 *y, const struct ecc_point *q,
244 const struct ecc_curve *curve);
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +0100245#endif