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