/*1* Copyright (c) 2013, Kenneth MacKay2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions are6* met:7* * Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* * Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS14* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT15* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR16* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT17* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,18* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT19* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE23* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/25#ifndef _CRYPTO_ECC_H26#define _CRYPTO_ECC_H2728#include <crypto/ecc_curve.h>29#include <linux/unaligned.h>3031/* One digit is u64 qword. */32#define ECC_CURVE_NIST_P192_DIGITS 333#define ECC_CURVE_NIST_P256_DIGITS 434#define ECC_CURVE_NIST_P384_DIGITS 635#define ECC_CURVE_NIST_P521_DIGITS 936#define ECC_MAX_DIGITS DIV_ROUND_UP(521, 64) /* NIST P521 */3738#define ECC_DIGITS_TO_BYTES_SHIFT 33940#define ECC_MAX_BYTES (ECC_MAX_DIGITS << ECC_DIGITS_TO_BYTES_SHIFT)4142#define ECC_POINT_INIT(x, y, ndigits) (struct ecc_point) { x, y, ndigits }4344/*45* The integers r and s making up the signature are expected to be46* formatted as two consecutive u64 arrays of size ECC_MAX_BYTES.47* The bytes within each u64 digit are in native endianness,48* but the order of the u64 digits themselves is little endian.49* This format allows direct use by internal vli_*() functions.50*/51struct ecdsa_raw_sig {52u64 r[ECC_MAX_DIGITS];53u64 s[ECC_MAX_DIGITS];54};5556/**57* ecc_swap_digits() - Copy ndigits from big endian array to native array58* @in: Input array59* @out: Output array60* @ndigits: Number of digits to copy61*/62static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigits)63{64const __be64 *src = (__force __be64 *)in;65int i;6667for (i = 0; i < ndigits; i++)68out[i] = get_unaligned_be64(&src[ndigits - 1 - i]);69}7071/**72* ecc_digits_from_bytes() - Create ndigits-sized digits array from byte array73* @in: Input byte array74* @nbytes Size of input byte array75* @out Output digits array76* @ndigits: Number of digits to create from byte array77*78* The first byte in the input byte array is expected to hold the most79* significant bits of the large integer.80*/81void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,82u64 *out, unsigned int ndigits);8384/**85* ecc_is_key_valid() - Validate a given ECDH private key86*87* @curve_id: id representing the curve to use88* @ndigits: curve's number of digits89* @private_key: private key to be used for the given curve90* @private_key_len: private key length91*92* Returns 0 if the key is acceptable, a negative value otherwise93*/94int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,95const u64 *private_key, unsigned int private_key_len);9697/**98* ecc_gen_privkey() - Generates an ECC private key.99* The private key is a random integer in the range 0 < random < n, where n is a100* prime that is the order of the cyclic subgroup generated by the distinguished101* point G.102* @curve_id: id representing the curve to use103* @ndigits: curve number of digits104* @private_key: buffer for storing the generated private key105*106* Returns 0 if the private key was generated successfully, a negative value107* if an error occurred.108*/109int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits,110u64 *private_key);111112/**113* ecc_make_pub_key() - Compute an ECC public key114*115* @curve_id: id representing the curve to use116* @ndigits: curve's number of digits117* @private_key: pregenerated private key for the given curve118* @public_key: buffer for storing the generated public key119*120* Returns 0 if the public key was generated successfully, a negative value121* if an error occurred.122*/123int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits,124const u64 *private_key, u64 *public_key);125126/**127* crypto_ecdh_shared_secret() - Compute a shared secret128*129* @curve_id: id representing the curve to use130* @ndigits: curve's number of digits131* @private_key: private key of part A132* @public_key: public key of counterpart B133* @secret: buffer for storing the calculated shared secret134*135* Note: It is recommended that you hash the result of crypto_ecdh_shared_secret136* before using it for symmetric encryption or HMAC.137*138* Returns 0 if the shared secret was generated successfully, a negative value139* if an error occurred.140*/141int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,142const u64 *private_key, const u64 *public_key,143u64 *secret);144145/**146* ecc_is_pubkey_valid_partial() - Partial public key validation147*148* @curve: elliptic curve domain parameters149* @pk: public key as a point150*151* Valdiate public key according to SP800-56A section 5.6.2.3.4 ECC Partial152* Public-Key Validation Routine.153*154* Note: There is no check that the public key is in the correct elliptic curve155* subgroup.156*157* Return: 0 if validation is successful, -EINVAL if validation is failed.158*/159int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,160struct ecc_point *pk);161162/**163* ecc_is_pubkey_valid_full() - Full public key validation164*165* @curve: elliptic curve domain parameters166* @pk: public key as a point167*168* Valdiate public key according to SP800-56A section 5.6.2.3.3 ECC Full169* Public-Key Validation Routine.170*171* Return: 0 if validation is successful, -EINVAL if validation is failed.172*/173int ecc_is_pubkey_valid_full(const struct ecc_curve *curve,174struct ecc_point *pk);175176/**177* vli_is_zero() - Determine is vli is zero178*179* @vli: vli to check.180* @ndigits: length of the @vli181*/182bool vli_is_zero(const u64 *vli, unsigned int ndigits);183184/**185* vli_cmp() - compare left and right vlis186*187* @left: vli188* @right: vli189* @ndigits: length of both vlis190*191* Returns sign of @left - @right, i.e. -1 if @left < @right,192* 0 if @left == @right, 1 if @left > @right.193*/194int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits);195196/**197* vli_sub() - Subtracts right from left198*199* @result: where to write result200* @left: vli201* @right vli202* @ndigits: length of all vlis203*204* Note: can modify in-place.205*206* Return: carry bit.207*/208u64 vli_sub(u64 *result, const u64 *left, const u64 *right,209unsigned int ndigits);210211/**212* vli_from_be64() - Load vli from big-endian u64 array213*214* @dest: destination vli215* @src: source array of u64 BE values216* @ndigits: length of both vli and array217*/218void vli_from_be64(u64 *dest, const void *src, unsigned int ndigits);219220/**221* vli_from_le64() - Load vli from little-endian u64 array222*223* @dest: destination vli224* @src: source array of u64 LE values225* @ndigits: length of both vli and array226*/227void vli_from_le64(u64 *dest, const void *src, unsigned int ndigits);228229/**230* vli_mod_inv() - Modular inversion231*232* @result: where to write vli number233* @input: vli value to operate on234* @mod: modulus235* @ndigits: length of all vlis236*/237void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,238unsigned int ndigits);239240/**241* vli_mod_mult_slow() - Modular multiplication242*243* @result: where to write result value244* @left: vli number to multiply with @right245* @right: vli number to multiply with @left246* @mod: modulus247* @ndigits: length of all vlis248*249* Note: Assumes that mod is big enough curve order.250*/251void vli_mod_mult_slow(u64 *result, const u64 *left, const u64 *right,252const u64 *mod, unsigned int ndigits);253254/**255* vli_num_bits() - Counts the number of bits required for vli.256*257* @vli: vli to check.258* @ndigits: Length of the @vli259*260* Return: The number of bits required to represent @vli.261*/262unsigned int vli_num_bits(const u64 *vli, unsigned int ndigits);263264/**265* ecc_aloc_point() - Allocate ECC point.266*267* @ndigits: Length of vlis in u64 qwords.268*269* Return: Pointer to the allocated point or NULL if allocation failed.270*/271struct ecc_point *ecc_alloc_point(unsigned int ndigits);272273/**274* ecc_free_point() - Free ECC point.275*276* @p: The point to free.277*/278void ecc_free_point(struct ecc_point *p);279280/**281* ecc_point_is_zero() - Check if point is zero.282*283* @p: Point to check for zero.284*285* Return: true if point is the point at infinity, false otherwise.286*/287bool ecc_point_is_zero(const struct ecc_point *point);288289/**290* ecc_point_mult_shamir() - Add two points multiplied by scalars291*292* @result: resulting point293* @x: scalar to multiply with @p294* @p: point to multiply with @x295* @y: scalar to multiply with @q296* @q: point to multiply with @y297* @curve: curve298*299* Returns result = x * p + x * q over the curve.300* This works faster than two multiplications and addition.301*/302void ecc_point_mult_shamir(const struct ecc_point *result,303const u64 *x, const struct ecc_point *p,304const u64 *y, const struct ecc_point *q,305const struct ecc_curve *curve);306307extern struct crypto_template ecdsa_x962_tmpl;308extern struct crypto_template ecdsa_p1363_tmpl;309#endif310311312