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