1a745d3acSDaniele Alessandrelli /* 2a745d3acSDaniele Alessandrelli * Copyright (c) 2013, Kenneth MacKay 3a745d3acSDaniele Alessandrelli * All rights reserved. 4a745d3acSDaniele Alessandrelli * 5a745d3acSDaniele Alessandrelli * Redistribution and use in source and binary forms, with or without 6a745d3acSDaniele Alessandrelli * modification, are permitted provided that the following conditions are 7a745d3acSDaniele Alessandrelli * met: 8a745d3acSDaniele Alessandrelli * * Redistributions of source code must retain the above copyright 9a745d3acSDaniele Alessandrelli * notice, this list of conditions and the following disclaimer. 10a745d3acSDaniele Alessandrelli * * Redistributions in binary form must reproduce the above copyright 11a745d3acSDaniele Alessandrelli * notice, this list of conditions and the following disclaimer in the 12a745d3acSDaniele Alessandrelli * documentation and/or other materials provided with the distribution. 13a745d3acSDaniele Alessandrelli * 14a745d3acSDaniele Alessandrelli * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 15a745d3acSDaniele Alessandrelli * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 16a745d3acSDaniele Alessandrelli * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 17a745d3acSDaniele Alessandrelli * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 18a745d3acSDaniele Alessandrelli * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 19a745d3acSDaniele Alessandrelli * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 20a745d3acSDaniele Alessandrelli * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21a745d3acSDaniele Alessandrelli * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22a745d3acSDaniele Alessandrelli * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23a745d3acSDaniele Alessandrelli * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24a745d3acSDaniele Alessandrelli * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25a745d3acSDaniele Alessandrelli */ 26a745d3acSDaniele Alessandrelli #ifndef _CRYPTO_ECC_H 27a745d3acSDaniele Alessandrelli #define _CRYPTO_ECC_H 28a745d3acSDaniele Alessandrelli 29a745d3acSDaniele Alessandrelli #include <crypto/ecc_curve.h> 30a745d3acSDaniele Alessandrelli #include <asm/unaligned.h> 31a745d3acSDaniele Alessandrelli 32a745d3acSDaniele Alessandrelli /* One digit is u64 qword. */ 33a745d3acSDaniele Alessandrelli #define ECC_CURVE_NIST_P192_DIGITS 3 34a745d3acSDaniele Alessandrelli #define ECC_CURVE_NIST_P256_DIGITS 4 35a745d3acSDaniele Alessandrelli #define ECC_CURVE_NIST_P384_DIGITS 6 36a745d3acSDaniele Alessandrelli #define ECC_MAX_DIGITS (512 / 64) /* due to ecrdsa */ 37a745d3acSDaniele Alessandrelli 38a745d3acSDaniele Alessandrelli #define ECC_DIGITS_TO_BYTES_SHIFT 3 39a745d3acSDaniele Alessandrelli 40a745d3acSDaniele Alessandrelli #define ECC_MAX_BYTES (ECC_MAX_DIGITS << ECC_DIGITS_TO_BYTES_SHIFT) 41a745d3acSDaniele Alessandrelli 42a745d3acSDaniele Alessandrelli #define ECC_POINT_INIT(x, y, ndigits) (struct ecc_point) { x, y, ndigits } 43a745d3acSDaniele Alessandrelli 44a745d3acSDaniele Alessandrelli /** 45a745d3acSDaniele Alessandrelli * ecc_swap_digits() - Copy ndigits from big endian array to native array 46a745d3acSDaniele Alessandrelli * @in: Input array 47a745d3acSDaniele Alessandrelli * @out: Output array 48a745d3acSDaniele Alessandrelli * @ndigits: Number of digits to copy 49a745d3acSDaniele Alessandrelli */ 50a745d3acSDaniele Alessandrelli static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigits) 51a745d3acSDaniele Alessandrelli { 52a745d3acSDaniele Alessandrelli const __be64 *src = (__force __be64 *)in; 53a745d3acSDaniele Alessandrelli int i; 54a745d3acSDaniele Alessandrelli 55a745d3acSDaniele Alessandrelli for (i = 0; i < ndigits; i++) 56a745d3acSDaniele Alessandrelli out[i] = get_unaligned_be64(&src[ndigits - 1 - i]); 57a745d3acSDaniele Alessandrelli } 58a745d3acSDaniele Alessandrelli 59a745d3acSDaniele Alessandrelli /** 60a745d3acSDaniele Alessandrelli * ecc_is_key_valid() - Validate a given ECDH private key 61a745d3acSDaniele Alessandrelli * 62a745d3acSDaniele Alessandrelli * @curve_id: id representing the curve to use 63a745d3acSDaniele Alessandrelli * @ndigits: curve's number of digits 64a745d3acSDaniele Alessandrelli * @private_key: private key to be used for the given curve 65a745d3acSDaniele Alessandrelli * @private_key_len: private key length 66a745d3acSDaniele Alessandrelli * 67a745d3acSDaniele Alessandrelli * Returns 0 if the key is acceptable, a negative value otherwise 68a745d3acSDaniele Alessandrelli */ 69a745d3acSDaniele Alessandrelli int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits, 70a745d3acSDaniele Alessandrelli const u64 *private_key, unsigned int private_key_len); 71a745d3acSDaniele Alessandrelli 72a745d3acSDaniele Alessandrelli /** 73a745d3acSDaniele Alessandrelli * ecc_gen_privkey() - Generates an ECC private key. 74a745d3acSDaniele Alessandrelli * The private key is a random integer in the range 0 < random < n, where n is a 75a745d3acSDaniele Alessandrelli * prime that is the order of the cyclic subgroup generated by the distinguished 76a745d3acSDaniele Alessandrelli * point G. 77a745d3acSDaniele Alessandrelli * @curve_id: id representing the curve to use 78a745d3acSDaniele Alessandrelli * @ndigits: curve number of digits 79a745d3acSDaniele Alessandrelli * @private_key: buffer for storing the generated private key 80a745d3acSDaniele Alessandrelli * 81a745d3acSDaniele Alessandrelli * Returns 0 if the private key was generated successfully, a negative value 82a745d3acSDaniele Alessandrelli * if an error occurred. 83a745d3acSDaniele Alessandrelli */ 84a745d3acSDaniele Alessandrelli int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey); 85a745d3acSDaniele Alessandrelli 86a745d3acSDaniele Alessandrelli /** 87a745d3acSDaniele Alessandrelli * ecc_make_pub_key() - Compute an ECC public key 88a745d3acSDaniele Alessandrelli * 89a745d3acSDaniele Alessandrelli * @curve_id: id representing the curve to use 90a745d3acSDaniele Alessandrelli * @ndigits: curve's number of digits 91a745d3acSDaniele Alessandrelli * @private_key: pregenerated private key for the given curve 92a745d3acSDaniele Alessandrelli * @public_key: buffer for storing the generated public key 93a745d3acSDaniele Alessandrelli * 94a745d3acSDaniele Alessandrelli * Returns 0 if the public key was generated successfully, a negative value 95a745d3acSDaniele Alessandrelli * if an error occurred. 96a745d3acSDaniele Alessandrelli */ 97a745d3acSDaniele Alessandrelli int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits, 98a745d3acSDaniele Alessandrelli const u64 *private_key, u64 *public_key); 99a745d3acSDaniele Alessandrelli 100a745d3acSDaniele Alessandrelli /** 101a745d3acSDaniele Alessandrelli * crypto_ecdh_shared_secret() - Compute a shared secret 102a745d3acSDaniele Alessandrelli * 103a745d3acSDaniele Alessandrelli * @curve_id: id representing the curve to use 104a745d3acSDaniele Alessandrelli * @ndigits: curve's number of digits 105a745d3acSDaniele Alessandrelli * @private_key: private key of part A 106a745d3acSDaniele Alessandrelli * @public_key: public key of counterpart B 107a745d3acSDaniele Alessandrelli * @secret: buffer for storing the calculated shared secret 108a745d3acSDaniele Alessandrelli * 109a745d3acSDaniele Alessandrelli * Note: It is recommended that you hash the result of crypto_ecdh_shared_secret 110a745d3acSDaniele Alessandrelli * before using it for symmetric encryption or HMAC. 111a745d3acSDaniele Alessandrelli * 112a745d3acSDaniele Alessandrelli * Returns 0 if the shared secret was generated successfully, a negative value 113a745d3acSDaniele Alessandrelli * if an error occurred. 114a745d3acSDaniele Alessandrelli */ 115a745d3acSDaniele Alessandrelli int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, 116a745d3acSDaniele Alessandrelli const u64 *private_key, const u64 *public_key, 117a745d3acSDaniele Alessandrelli u64 *secret); 118a745d3acSDaniele Alessandrelli 119a745d3acSDaniele Alessandrelli /** 120a745d3acSDaniele Alessandrelli * ecc_is_pubkey_valid_partial() - Partial public key validation 121a745d3acSDaniele Alessandrelli * 122a745d3acSDaniele Alessandrelli * @curve: elliptic curve domain parameters 123a745d3acSDaniele Alessandrelli * @pk: public key as a point 124a745d3acSDaniele Alessandrelli * 125a745d3acSDaniele Alessandrelli * Valdiate public key according to SP800-56A section 5.6.2.3.4 ECC Partial 126a745d3acSDaniele Alessandrelli * Public-Key Validation Routine. 127a745d3acSDaniele Alessandrelli * 128a745d3acSDaniele Alessandrelli * Note: There is no check that the public key is in the correct elliptic curve 129a745d3acSDaniele Alessandrelli * subgroup. 130a745d3acSDaniele Alessandrelli * 131a745d3acSDaniele Alessandrelli * Return: 0 if validation is successful, -EINVAL if validation is failed. 132a745d3acSDaniele Alessandrelli */ 133a745d3acSDaniele Alessandrelli int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve, 134a745d3acSDaniele Alessandrelli struct ecc_point *pk); 135a745d3acSDaniele Alessandrelli 136a745d3acSDaniele Alessandrelli /** 137a745d3acSDaniele Alessandrelli * ecc_is_pubkey_valid_full() - Full public key validation 138a745d3acSDaniele Alessandrelli * 139a745d3acSDaniele Alessandrelli * @curve: elliptic curve domain parameters 140a745d3acSDaniele Alessandrelli * @pk: public key as a point 141a745d3acSDaniele Alessandrelli * 142a745d3acSDaniele Alessandrelli * Valdiate public key according to SP800-56A section 5.6.2.3.3 ECC Full 143a745d3acSDaniele Alessandrelli * Public-Key Validation Routine. 144a745d3acSDaniele Alessandrelli * 145a745d3acSDaniele Alessandrelli * Return: 0 if validation is successful, -EINVAL if validation is failed. 146a745d3acSDaniele Alessandrelli */ 147a745d3acSDaniele Alessandrelli int ecc_is_pubkey_valid_full(const struct ecc_curve *curve, 148a745d3acSDaniele Alessandrelli struct ecc_point *pk); 149a745d3acSDaniele Alessandrelli 150a745d3acSDaniele Alessandrelli /** 151a745d3acSDaniele Alessandrelli * vli_is_zero() - Determine is vli is zero 152a745d3acSDaniele Alessandrelli * 153a745d3acSDaniele Alessandrelli * @vli: vli to check. 154a745d3acSDaniele Alessandrelli * @ndigits: length of the @vli 155a745d3acSDaniele Alessandrelli */ 156a745d3acSDaniele Alessandrelli bool vli_is_zero(const u64 *vli, unsigned int ndigits); 157a745d3acSDaniele Alessandrelli 158a745d3acSDaniele Alessandrelli /** 159a745d3acSDaniele Alessandrelli * vli_cmp() - compare left and right vlis 160a745d3acSDaniele Alessandrelli * 161a745d3acSDaniele Alessandrelli * @left: vli 162a745d3acSDaniele Alessandrelli * @right: vli 163a745d3acSDaniele Alessandrelli * @ndigits: length of both vlis 164a745d3acSDaniele Alessandrelli * 165a745d3acSDaniele Alessandrelli * Returns sign of @left - @right, i.e. -1 if @left < @right, 166a745d3acSDaniele Alessandrelli * 0 if @left == @right, 1 if @left > @right. 167a745d3acSDaniele Alessandrelli */ 168a745d3acSDaniele Alessandrelli int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits); 169a745d3acSDaniele Alessandrelli 170a745d3acSDaniele Alessandrelli /** 171a745d3acSDaniele Alessandrelli * vli_sub() - Subtracts right from left 172a745d3acSDaniele Alessandrelli * 173a745d3acSDaniele Alessandrelli * @result: where to write result 174a745d3acSDaniele Alessandrelli * @left: vli 175a745d3acSDaniele Alessandrelli * @right vli 176a745d3acSDaniele Alessandrelli * @ndigits: length of all vlis 177a745d3acSDaniele Alessandrelli * 178a745d3acSDaniele Alessandrelli * Note: can modify in-place. 179a745d3acSDaniele Alessandrelli * 180a745d3acSDaniele Alessandrelli * Return: carry bit. 181a745d3acSDaniele Alessandrelli */ 182a745d3acSDaniele Alessandrelli u64 vli_sub(u64 *result, const u64 *left, const u64 *right, 183a745d3acSDaniele Alessandrelli unsigned int ndigits); 184a745d3acSDaniele Alessandrelli 185a745d3acSDaniele Alessandrelli /** 186a745d3acSDaniele Alessandrelli * vli_from_be64() - Load vli from big-endian u64 array 187a745d3acSDaniele Alessandrelli * 188a745d3acSDaniele Alessandrelli * @dest: destination vli 189a745d3acSDaniele Alessandrelli * @src: source array of u64 BE values 190a745d3acSDaniele Alessandrelli * @ndigits: length of both vli and array 191a745d3acSDaniele Alessandrelli */ 192a745d3acSDaniele Alessandrelli void vli_from_be64(u64 *dest, const void *src, unsigned int ndigits); 193a745d3acSDaniele Alessandrelli 194a745d3acSDaniele Alessandrelli /** 195a745d3acSDaniele Alessandrelli * vli_from_le64() - Load vli from little-endian u64 array 196a745d3acSDaniele Alessandrelli * 197a745d3acSDaniele Alessandrelli * @dest: destination vli 198a745d3acSDaniele Alessandrelli * @src: source array of u64 LE values 199a745d3acSDaniele Alessandrelli * @ndigits: length of both vli and array 200a745d3acSDaniele Alessandrelli */ 201a745d3acSDaniele Alessandrelli void vli_from_le64(u64 *dest, const void *src, unsigned int ndigits); 202a745d3acSDaniele Alessandrelli 203a745d3acSDaniele Alessandrelli /** 204a745d3acSDaniele Alessandrelli * vli_mod_inv() - Modular inversion 205a745d3acSDaniele Alessandrelli * 206a745d3acSDaniele Alessandrelli * @result: where to write vli number 207a745d3acSDaniele Alessandrelli * @input: vli value to operate on 208a745d3acSDaniele Alessandrelli * @mod: modulus 209a745d3acSDaniele Alessandrelli * @ndigits: length of all vlis 210a745d3acSDaniele Alessandrelli */ 211a745d3acSDaniele Alessandrelli void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod, 212a745d3acSDaniele Alessandrelli unsigned int ndigits); 213a745d3acSDaniele Alessandrelli 214a745d3acSDaniele Alessandrelli /** 215a745d3acSDaniele Alessandrelli * vli_mod_mult_slow() - Modular multiplication 216a745d3acSDaniele Alessandrelli * 217a745d3acSDaniele Alessandrelli * @result: where to write result value 218a745d3acSDaniele Alessandrelli * @left: vli number to multiply with @right 219a745d3acSDaniele Alessandrelli * @right: vli number to multiply with @left 220a745d3acSDaniele Alessandrelli * @mod: modulus 221a745d3acSDaniele Alessandrelli * @ndigits: length of all vlis 222a745d3acSDaniele Alessandrelli * 223a745d3acSDaniele Alessandrelli * Note: Assumes that mod is big enough curve order. 224a745d3acSDaniele Alessandrelli */ 225a745d3acSDaniele Alessandrelli void vli_mod_mult_slow(u64 *result, const u64 *left, const u64 *right, 226a745d3acSDaniele Alessandrelli const u64 *mod, unsigned int ndigits); 227a745d3acSDaniele Alessandrelli 228a745d3acSDaniele Alessandrelli /** 229*eaffe377SDaniele Alessandrelli * vli_num_bits() - Counts the number of bits required for vli. 230*eaffe377SDaniele Alessandrelli * 231*eaffe377SDaniele Alessandrelli * @vli: vli to check. 232*eaffe377SDaniele Alessandrelli * @ndigits: Length of the @vli 233*eaffe377SDaniele Alessandrelli * 234*eaffe377SDaniele Alessandrelli * Return: The number of bits required to represent @vli. 235*eaffe377SDaniele Alessandrelli */ 236*eaffe377SDaniele Alessandrelli unsigned int vli_num_bits(const u64 *vli, unsigned int ndigits); 237*eaffe377SDaniele Alessandrelli 238*eaffe377SDaniele Alessandrelli /** 239*eaffe377SDaniele Alessandrelli * ecc_aloc_point() - Allocate ECC point. 240*eaffe377SDaniele Alessandrelli * 241*eaffe377SDaniele Alessandrelli * @ndigits: Length of vlis in u64 qwords. 242*eaffe377SDaniele Alessandrelli * 243*eaffe377SDaniele Alessandrelli * Return: Pointer to the allocated point or NULL if allocation failed. 244*eaffe377SDaniele Alessandrelli */ 245*eaffe377SDaniele Alessandrelli struct ecc_point *ecc_alloc_point(unsigned int ndigits); 246*eaffe377SDaniele Alessandrelli 247*eaffe377SDaniele Alessandrelli /** 248*eaffe377SDaniele Alessandrelli * ecc_free_point() - Free ECC point. 249*eaffe377SDaniele Alessandrelli * 250*eaffe377SDaniele Alessandrelli * @p: The point to free. 251*eaffe377SDaniele Alessandrelli */ 252*eaffe377SDaniele Alessandrelli void ecc_free_point(struct ecc_point *p); 253*eaffe377SDaniele Alessandrelli 254*eaffe377SDaniele Alessandrelli /** 255*eaffe377SDaniele Alessandrelli * ecc_point_is_zero() - Check if point is zero. 256*eaffe377SDaniele Alessandrelli * 257*eaffe377SDaniele Alessandrelli * @p: Point to check for zero. 258*eaffe377SDaniele Alessandrelli * 259*eaffe377SDaniele Alessandrelli * Return: true if point is the point at infinity, false otherwise. 260*eaffe377SDaniele Alessandrelli */ 261*eaffe377SDaniele Alessandrelli bool ecc_point_is_zero(const struct ecc_point *point); 262*eaffe377SDaniele Alessandrelli 263*eaffe377SDaniele Alessandrelli /** 264a745d3acSDaniele Alessandrelli * ecc_point_mult_shamir() - Add two points multiplied by scalars 265a745d3acSDaniele Alessandrelli * 266a745d3acSDaniele Alessandrelli * @result: resulting point 267a745d3acSDaniele Alessandrelli * @x: scalar to multiply with @p 268a745d3acSDaniele Alessandrelli * @p: point to multiply with @x 269a745d3acSDaniele Alessandrelli * @y: scalar to multiply with @q 270a745d3acSDaniele Alessandrelli * @q: point to multiply with @y 271a745d3acSDaniele Alessandrelli * @curve: curve 272a745d3acSDaniele Alessandrelli * 273a745d3acSDaniele Alessandrelli * Returns result = x * p + x * q over the curve. 274a745d3acSDaniele Alessandrelli * This works faster than two multiplications and addition. 275a745d3acSDaniele Alessandrelli */ 276a745d3acSDaniele Alessandrelli void ecc_point_mult_shamir(const struct ecc_point *result, 277a745d3acSDaniele Alessandrelli const u64 *x, const struct ecc_point *p, 278a745d3acSDaniele Alessandrelli const u64 *y, const struct ecc_point *q, 279a745d3acSDaniele Alessandrelli const struct ecc_curve *curve); 280*eaffe377SDaniele Alessandrelli 281a745d3acSDaniele Alessandrelli #endif 282