xref: /openbmc/linux/crypto/ecc.c (revision 9144f784f852f9a125cabe9927b986d909bfa439)
13c4b2390SSalvatore Benedetto /*
20d7a7864SVitaly Chikunov  * Copyright (c) 2013, 2014 Kenneth MacKay. All rights reserved.
30d7a7864SVitaly Chikunov  * Copyright (c) 2019 Vitaly Chikunov <vt@altlinux.org>
43c4b2390SSalvatore Benedetto  *
53c4b2390SSalvatore Benedetto  * Redistribution and use in source and binary forms, with or without
63c4b2390SSalvatore Benedetto  * modification, are permitted provided that the following conditions are
73c4b2390SSalvatore Benedetto  * met:
83c4b2390SSalvatore Benedetto  *  * Redistributions of source code must retain the above copyright
93c4b2390SSalvatore Benedetto  *   notice, this list of conditions and the following disclaimer.
103c4b2390SSalvatore Benedetto  *  * Redistributions in binary form must reproduce the above copyright
113c4b2390SSalvatore Benedetto  *    notice, this list of conditions and the following disclaimer in the
123c4b2390SSalvatore Benedetto  *    documentation and/or other materials provided with the distribution.
133c4b2390SSalvatore Benedetto  *
143c4b2390SSalvatore Benedetto  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
153c4b2390SSalvatore Benedetto  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
163c4b2390SSalvatore Benedetto  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
173c4b2390SSalvatore Benedetto  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
183c4b2390SSalvatore Benedetto  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
193c4b2390SSalvatore Benedetto  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
203c4b2390SSalvatore Benedetto  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
213c4b2390SSalvatore Benedetto  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
223c4b2390SSalvatore Benedetto  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
233c4b2390SSalvatore Benedetto  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
243c4b2390SSalvatore Benedetto  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
253c4b2390SSalvatore Benedetto  */
263c4b2390SSalvatore Benedetto 
2714bb7676SMeng Yu #include <crypto/ecc_curve.h>
284a2289daSVitaly Chikunov #include <linux/module.h>
293c4b2390SSalvatore Benedetto #include <linux/random.h>
303c4b2390SSalvatore Benedetto #include <linux/slab.h>
313c4b2390SSalvatore Benedetto #include <linux/swab.h>
323c4b2390SSalvatore Benedetto #include <linux/fips.h>
333c4b2390SSalvatore Benedetto #include <crypto/ecdh.h>
346755fd26STudor-Dan Ambarus #include <crypto/rng.h>
35a745d3acSDaniele Alessandrelli #include <crypto/internal/ecc.h>
360d7a7864SVitaly Chikunov #include <asm/unaligned.h>
370d7a7864SVitaly Chikunov #include <linux/ratelimit.h>
383c4b2390SSalvatore Benedetto 
393c4b2390SSalvatore Benedetto #include "ecc_curve_defs.h"
403c4b2390SSalvatore Benedetto 
413c4b2390SSalvatore Benedetto typedef struct {
423c4b2390SSalvatore Benedetto 	u64 m_low;
433c4b2390SSalvatore Benedetto 	u64 m_high;
443c4b2390SSalvatore Benedetto } uint128_t;
453c4b2390SSalvatore Benedetto 
468fb9340eSMeng Yu /* Returns curv25519 curve param */
ecc_get_curve25519(void)478fb9340eSMeng Yu const struct ecc_curve *ecc_get_curve25519(void)
488fb9340eSMeng Yu {
498fb9340eSMeng Yu 	return &ecc_25519;
508fb9340eSMeng Yu }
518fb9340eSMeng Yu EXPORT_SYMBOL(ecc_get_curve25519);
5214bb7676SMeng Yu 
ecc_get_curve(unsigned int curve_id)5314bb7676SMeng Yu const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
543c4b2390SSalvatore Benedetto {
553c4b2390SSalvatore Benedetto 	switch (curve_id) {
563c4b2390SSalvatore Benedetto 	/* In FIPS mode only allow P256 and higher */
573c4b2390SSalvatore Benedetto 	case ECC_CURVE_NIST_P192:
583c4b2390SSalvatore Benedetto 		return fips_enabled ? NULL : &nist_p192;
593c4b2390SSalvatore Benedetto 	case ECC_CURVE_NIST_P256:
603c4b2390SSalvatore Benedetto 		return &nist_p256;
61703c748dSSaulo Alessandre 	case ECC_CURVE_NIST_P384:
62703c748dSSaulo Alessandre 		return &nist_p384;
633c4b2390SSalvatore Benedetto 	default:
643c4b2390SSalvatore Benedetto 		return NULL;
653c4b2390SSalvatore Benedetto 	}
663c4b2390SSalvatore Benedetto }
6714bb7676SMeng Yu EXPORT_SYMBOL(ecc_get_curve);
683c4b2390SSalvatore Benedetto 
ecc_digits_from_bytes(const u8 * in,unsigned int nbytes,u64 * out,unsigned int ndigits)69*55779f26SStefan Berger void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,
70*55779f26SStefan Berger 			   u64 *out, unsigned int ndigits)
71*55779f26SStefan Berger {
72*55779f26SStefan Berger 	int diff = ndigits - DIV_ROUND_UP(nbytes, sizeof(u64));
73*55779f26SStefan Berger 	unsigned int o = nbytes & 7;
74*55779f26SStefan Berger 	__be64 msd = 0;
75*55779f26SStefan Berger 
76*55779f26SStefan Berger 	/* diff > 0: not enough input bytes: set most significant digits to 0 */
77*55779f26SStefan Berger 	if (diff > 0) {
78*55779f26SStefan Berger 		ndigits -= diff;
79*55779f26SStefan Berger 		memset(&out[ndigits - 1], 0, diff * sizeof(u64));
80*55779f26SStefan Berger 	}
81*55779f26SStefan Berger 
82*55779f26SStefan Berger 	if (o) {
83*55779f26SStefan Berger 		memcpy((u8 *)&msd + sizeof(msd) - o, in, o);
84*55779f26SStefan Berger 		out[--ndigits] = be64_to_cpu(msd);
85*55779f26SStefan Berger 		in += o;
86*55779f26SStefan Berger 	}
87*55779f26SStefan Berger 	ecc_swap_digits(in, out, ndigits);
88*55779f26SStefan Berger }
89*55779f26SStefan Berger EXPORT_SYMBOL(ecc_digits_from_bytes);
90*55779f26SStefan Berger 
ecc_alloc_digits_space(unsigned int ndigits)913c4b2390SSalvatore Benedetto static u64 *ecc_alloc_digits_space(unsigned int ndigits)
923c4b2390SSalvatore Benedetto {
933c4b2390SSalvatore Benedetto 	size_t len = ndigits * sizeof(u64);
943c4b2390SSalvatore Benedetto 
953c4b2390SSalvatore Benedetto 	if (!len)
963c4b2390SSalvatore Benedetto 		return NULL;
973c4b2390SSalvatore Benedetto 
983c4b2390SSalvatore Benedetto 	return kmalloc(len, GFP_KERNEL);
993c4b2390SSalvatore Benedetto }
1003c4b2390SSalvatore Benedetto 
ecc_free_digits_space(u64 * space)1013c4b2390SSalvatore Benedetto static void ecc_free_digits_space(u64 *space)
1023c4b2390SSalvatore Benedetto {
103453431a5SWaiman Long 	kfree_sensitive(space);
1043c4b2390SSalvatore Benedetto }
1053c4b2390SSalvatore Benedetto 
ecc_alloc_point(unsigned int ndigits)106eaffe377SDaniele Alessandrelli struct ecc_point *ecc_alloc_point(unsigned int ndigits)
1073c4b2390SSalvatore Benedetto {
1083c4b2390SSalvatore Benedetto 	struct ecc_point *p = kmalloc(sizeof(*p), GFP_KERNEL);
1093c4b2390SSalvatore Benedetto 
1103c4b2390SSalvatore Benedetto 	if (!p)
1113c4b2390SSalvatore Benedetto 		return NULL;
1123c4b2390SSalvatore Benedetto 
1133c4b2390SSalvatore Benedetto 	p->x = ecc_alloc_digits_space(ndigits);
1143c4b2390SSalvatore Benedetto 	if (!p->x)
1153c4b2390SSalvatore Benedetto 		goto err_alloc_x;
1163c4b2390SSalvatore Benedetto 
1173c4b2390SSalvatore Benedetto 	p->y = ecc_alloc_digits_space(ndigits);
1183c4b2390SSalvatore Benedetto 	if (!p->y)
1193c4b2390SSalvatore Benedetto 		goto err_alloc_y;
1203c4b2390SSalvatore Benedetto 
1213c4b2390SSalvatore Benedetto 	p->ndigits = ndigits;
1223c4b2390SSalvatore Benedetto 
1233c4b2390SSalvatore Benedetto 	return p;
1243c4b2390SSalvatore Benedetto 
1253c4b2390SSalvatore Benedetto err_alloc_y:
1263c4b2390SSalvatore Benedetto 	ecc_free_digits_space(p->x);
1273c4b2390SSalvatore Benedetto err_alloc_x:
1283c4b2390SSalvatore Benedetto 	kfree(p);
1293c4b2390SSalvatore Benedetto 	return NULL;
1303c4b2390SSalvatore Benedetto }
131eaffe377SDaniele Alessandrelli EXPORT_SYMBOL(ecc_alloc_point);
1323c4b2390SSalvatore Benedetto 
ecc_free_point(struct ecc_point * p)133eaffe377SDaniele Alessandrelli void ecc_free_point(struct ecc_point *p)
1343c4b2390SSalvatore Benedetto {
1353c4b2390SSalvatore Benedetto 	if (!p)
1363c4b2390SSalvatore Benedetto 		return;
1373c4b2390SSalvatore Benedetto 
138453431a5SWaiman Long 	kfree_sensitive(p->x);
139453431a5SWaiman Long 	kfree_sensitive(p->y);
140453431a5SWaiman Long 	kfree_sensitive(p);
1413c4b2390SSalvatore Benedetto }
142eaffe377SDaniele Alessandrelli EXPORT_SYMBOL(ecc_free_point);
1433c4b2390SSalvatore Benedetto 
vli_clear(u64 * vli,unsigned int ndigits)1443c4b2390SSalvatore Benedetto static void vli_clear(u64 *vli, unsigned int ndigits)
1453c4b2390SSalvatore Benedetto {
1463c4b2390SSalvatore Benedetto 	int i;
1473c4b2390SSalvatore Benedetto 
1483c4b2390SSalvatore Benedetto 	for (i = 0; i < ndigits; i++)
1493c4b2390SSalvatore Benedetto 		vli[i] = 0;
1503c4b2390SSalvatore Benedetto }
1513c4b2390SSalvatore Benedetto 
1523c4b2390SSalvatore Benedetto /* Returns true if vli == 0, false otherwise. */
vli_is_zero(const u64 * vli,unsigned int ndigits)1534a2289daSVitaly Chikunov bool vli_is_zero(const u64 *vli, unsigned int ndigits)
1543c4b2390SSalvatore Benedetto {
1553c4b2390SSalvatore Benedetto 	int i;
1563c4b2390SSalvatore Benedetto 
1573c4b2390SSalvatore Benedetto 	for (i = 0; i < ndigits; i++) {
1583c4b2390SSalvatore Benedetto 		if (vli[i])
1593c4b2390SSalvatore Benedetto 			return false;
1603c4b2390SSalvatore Benedetto 	}
1613c4b2390SSalvatore Benedetto 
1623c4b2390SSalvatore Benedetto 	return true;
1633c4b2390SSalvatore Benedetto }
1644a2289daSVitaly Chikunov EXPORT_SYMBOL(vli_is_zero);
1653c4b2390SSalvatore Benedetto 
1660193b32fSMeng Yu /* Returns nonzero if bit of vli is set. */
vli_test_bit(const u64 * vli,unsigned int bit)1673c4b2390SSalvatore Benedetto static u64 vli_test_bit(const u64 *vli, unsigned int bit)
1683c4b2390SSalvatore Benedetto {
1693c4b2390SSalvatore Benedetto 	return (vli[bit / 64] & ((u64)1 << (bit % 64)));
1703c4b2390SSalvatore Benedetto }
1713c4b2390SSalvatore Benedetto 
vli_is_negative(const u64 * vli,unsigned int ndigits)1720d7a7864SVitaly Chikunov static bool vli_is_negative(const u64 *vli, unsigned int ndigits)
1730d7a7864SVitaly Chikunov {
1740d7a7864SVitaly Chikunov 	return vli_test_bit(vli, ndigits * 64 - 1);
1750d7a7864SVitaly Chikunov }
1760d7a7864SVitaly Chikunov 
1773c4b2390SSalvatore Benedetto /* Counts the number of 64-bit "digits" in vli. */
vli_num_digits(const u64 * vli,unsigned int ndigits)1783c4b2390SSalvatore Benedetto static unsigned int vli_num_digits(const u64 *vli, unsigned int ndigits)
1793c4b2390SSalvatore Benedetto {
1803c4b2390SSalvatore Benedetto 	int i;
1813c4b2390SSalvatore Benedetto 
1823c4b2390SSalvatore Benedetto 	/* Search from the end until we find a non-zero digit.
1833c4b2390SSalvatore Benedetto 	 * We do it in reverse because we expect that most digits will
1843c4b2390SSalvatore Benedetto 	 * be nonzero.
1853c4b2390SSalvatore Benedetto 	 */
1863c4b2390SSalvatore Benedetto 	for (i = ndigits - 1; i >= 0 && vli[i] == 0; i--);
1873c4b2390SSalvatore Benedetto 
1883c4b2390SSalvatore Benedetto 	return (i + 1);
1893c4b2390SSalvatore Benedetto }
1903c4b2390SSalvatore Benedetto 
1913c4b2390SSalvatore Benedetto /* Counts the number of bits required for vli. */
vli_num_bits(const u64 * vli,unsigned int ndigits)192eaffe377SDaniele Alessandrelli unsigned int vli_num_bits(const u64 *vli, unsigned int ndigits)
1933c4b2390SSalvatore Benedetto {
1943c4b2390SSalvatore Benedetto 	unsigned int i, num_digits;
1953c4b2390SSalvatore Benedetto 	u64 digit;
1963c4b2390SSalvatore Benedetto 
1973c4b2390SSalvatore Benedetto 	num_digits = vli_num_digits(vli, ndigits);
1983c4b2390SSalvatore Benedetto 	if (num_digits == 0)
1993c4b2390SSalvatore Benedetto 		return 0;
2003c4b2390SSalvatore Benedetto 
2013c4b2390SSalvatore Benedetto 	digit = vli[num_digits - 1];
2023c4b2390SSalvatore Benedetto 	for (i = 0; digit; i++)
2033c4b2390SSalvatore Benedetto 		digit >>= 1;
2043c4b2390SSalvatore Benedetto 
2053c4b2390SSalvatore Benedetto 	return ((num_digits - 1) * 64 + i);
2063c4b2390SSalvatore Benedetto }
207eaffe377SDaniele Alessandrelli EXPORT_SYMBOL(vli_num_bits);
2083c4b2390SSalvatore Benedetto 
2090d7a7864SVitaly Chikunov /* Set dest from unaligned bit string src. */
vli_from_be64(u64 * dest,const void * src,unsigned int ndigits)2100d7a7864SVitaly Chikunov void vli_from_be64(u64 *dest, const void *src, unsigned int ndigits)
2110d7a7864SVitaly Chikunov {
2120d7a7864SVitaly Chikunov 	int i;
2130d7a7864SVitaly Chikunov 	const u64 *from = src;
2140d7a7864SVitaly Chikunov 
2150d7a7864SVitaly Chikunov 	for (i = 0; i < ndigits; i++)
2160d7a7864SVitaly Chikunov 		dest[i] = get_unaligned_be64(&from[ndigits - 1 - i]);
2170d7a7864SVitaly Chikunov }
2180d7a7864SVitaly Chikunov EXPORT_SYMBOL(vli_from_be64);
2190d7a7864SVitaly Chikunov 
vli_from_le64(u64 * dest,const void * src,unsigned int ndigits)2200d7a7864SVitaly Chikunov void vli_from_le64(u64 *dest, const void *src, unsigned int ndigits)
2210d7a7864SVitaly Chikunov {
2220d7a7864SVitaly Chikunov 	int i;
2230d7a7864SVitaly Chikunov 	const u64 *from = src;
2240d7a7864SVitaly Chikunov 
2250d7a7864SVitaly Chikunov 	for (i = 0; i < ndigits; i++)
2260d7a7864SVitaly Chikunov 		dest[i] = get_unaligned_le64(&from[i]);
2270d7a7864SVitaly Chikunov }
2280d7a7864SVitaly Chikunov EXPORT_SYMBOL(vli_from_le64);
2290d7a7864SVitaly Chikunov 
2303c4b2390SSalvatore Benedetto /* Sets dest = src. */
vli_set(u64 * dest,const u64 * src,unsigned int ndigits)2313c4b2390SSalvatore Benedetto static void vli_set(u64 *dest, const u64 *src, unsigned int ndigits)
2323c4b2390SSalvatore Benedetto {
2333c4b2390SSalvatore Benedetto 	int i;
2343c4b2390SSalvatore Benedetto 
2353c4b2390SSalvatore Benedetto 	for (i = 0; i < ndigits; i++)
2363c4b2390SSalvatore Benedetto 		dest[i] = src[i];
2373c4b2390SSalvatore Benedetto }
2383c4b2390SSalvatore Benedetto 
2393c4b2390SSalvatore Benedetto /* Returns sign of left - right. */
vli_cmp(const u64 * left,const u64 * right,unsigned int ndigits)2404a2289daSVitaly Chikunov int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits)
2413c4b2390SSalvatore Benedetto {
2423c4b2390SSalvatore Benedetto 	int i;
2433c4b2390SSalvatore Benedetto 
2443c4b2390SSalvatore Benedetto 	for (i = ndigits - 1; i >= 0; i--) {
2453c4b2390SSalvatore Benedetto 		if (left[i] > right[i])
2463c4b2390SSalvatore Benedetto 			return 1;
2473c4b2390SSalvatore Benedetto 		else if (left[i] < right[i])
2483c4b2390SSalvatore Benedetto 			return -1;
2493c4b2390SSalvatore Benedetto 	}
2503c4b2390SSalvatore Benedetto 
2513c4b2390SSalvatore Benedetto 	return 0;
2523c4b2390SSalvatore Benedetto }
2534a2289daSVitaly Chikunov EXPORT_SYMBOL(vli_cmp);
2543c4b2390SSalvatore Benedetto 
2553c4b2390SSalvatore Benedetto /* Computes result = in << c, returning carry. Can modify in place
2563c4b2390SSalvatore Benedetto  * (if result == in). 0 < shift < 64.
2573c4b2390SSalvatore Benedetto  */
vli_lshift(u64 * result,const u64 * in,unsigned int shift,unsigned int ndigits)2583c4b2390SSalvatore Benedetto static u64 vli_lshift(u64 *result, const u64 *in, unsigned int shift,
2593c4b2390SSalvatore Benedetto 		      unsigned int ndigits)
2603c4b2390SSalvatore Benedetto {
2613c4b2390SSalvatore Benedetto 	u64 carry = 0;
2623c4b2390SSalvatore Benedetto 	int i;
2633c4b2390SSalvatore Benedetto 
2643c4b2390SSalvatore Benedetto 	for (i = 0; i < ndigits; i++) {
2653c4b2390SSalvatore Benedetto 		u64 temp = in[i];
2663c4b2390SSalvatore Benedetto 
2673c4b2390SSalvatore Benedetto 		result[i] = (temp << shift) | carry;
2683c4b2390SSalvatore Benedetto 		carry = temp >> (64 - shift);
2693c4b2390SSalvatore Benedetto 	}
2703c4b2390SSalvatore Benedetto 
2713c4b2390SSalvatore Benedetto 	return carry;
2723c4b2390SSalvatore Benedetto }
2733c4b2390SSalvatore Benedetto 
2743c4b2390SSalvatore Benedetto /* Computes vli = vli >> 1. */
vli_rshift1(u64 * vli,unsigned int ndigits)2753c4b2390SSalvatore Benedetto static void vli_rshift1(u64 *vli, unsigned int ndigits)
2763c4b2390SSalvatore Benedetto {
2773c4b2390SSalvatore Benedetto 	u64 *end = vli;
2783c4b2390SSalvatore Benedetto 	u64 carry = 0;
2793c4b2390SSalvatore Benedetto 
2803c4b2390SSalvatore Benedetto 	vli += ndigits;
2813c4b2390SSalvatore Benedetto 
2823c4b2390SSalvatore Benedetto 	while (vli-- > end) {
2833c4b2390SSalvatore Benedetto 		u64 temp = *vli;
2843c4b2390SSalvatore Benedetto 		*vli = (temp >> 1) | carry;
2853c4b2390SSalvatore Benedetto 		carry = temp << 63;
2863c4b2390SSalvatore Benedetto 	}
2873c4b2390SSalvatore Benedetto }
2883c4b2390SSalvatore Benedetto 
2893c4b2390SSalvatore Benedetto /* Computes result = left + right, returning carry. Can modify in place. */
vli_add(u64 * result,const u64 * left,const u64 * right,unsigned int ndigits)2903c4b2390SSalvatore Benedetto static u64 vli_add(u64 *result, const u64 *left, const u64 *right,
2913c4b2390SSalvatore Benedetto 		   unsigned int ndigits)
2923c4b2390SSalvatore Benedetto {
2933c4b2390SSalvatore Benedetto 	u64 carry = 0;
2943c4b2390SSalvatore Benedetto 	int i;
2953c4b2390SSalvatore Benedetto 
2963c4b2390SSalvatore Benedetto 	for (i = 0; i < ndigits; i++) {
2973c4b2390SSalvatore Benedetto 		u64 sum;
2983c4b2390SSalvatore Benedetto 
2993c4b2390SSalvatore Benedetto 		sum = left[i] + right[i] + carry;
3003c4b2390SSalvatore Benedetto 		if (sum != left[i])
3013c4b2390SSalvatore Benedetto 			carry = (sum < left[i]);
3023c4b2390SSalvatore Benedetto 
3033c4b2390SSalvatore Benedetto 		result[i] = sum;
3043c4b2390SSalvatore Benedetto 	}
3053c4b2390SSalvatore Benedetto 
3063c4b2390SSalvatore Benedetto 	return carry;
3073c4b2390SSalvatore Benedetto }
3083c4b2390SSalvatore Benedetto 
3090d7a7864SVitaly Chikunov /* Computes result = left + right, returning carry. Can modify in place. */
vli_uadd(u64 * result,const u64 * left,u64 right,unsigned int ndigits)3100d7a7864SVitaly Chikunov static u64 vli_uadd(u64 *result, const u64 *left, u64 right,
3110d7a7864SVitaly Chikunov 		    unsigned int ndigits)
3120d7a7864SVitaly Chikunov {
3130d7a7864SVitaly Chikunov 	u64 carry = right;
3140d7a7864SVitaly Chikunov 	int i;
3150d7a7864SVitaly Chikunov 
3160d7a7864SVitaly Chikunov 	for (i = 0; i < ndigits; i++) {
3170d7a7864SVitaly Chikunov 		u64 sum;
3180d7a7864SVitaly Chikunov 
3190d7a7864SVitaly Chikunov 		sum = left[i] + carry;
3200d7a7864SVitaly Chikunov 		if (sum != left[i])
3210d7a7864SVitaly Chikunov 			carry = (sum < left[i]);
3220d7a7864SVitaly Chikunov 		else
3230d7a7864SVitaly Chikunov 			carry = !!carry;
3240d7a7864SVitaly Chikunov 
3250d7a7864SVitaly Chikunov 		result[i] = sum;
3260d7a7864SVitaly Chikunov 	}
3270d7a7864SVitaly Chikunov 
3280d7a7864SVitaly Chikunov 	return carry;
3290d7a7864SVitaly Chikunov }
3300d7a7864SVitaly Chikunov 
3313c4b2390SSalvatore Benedetto /* Computes result = left - right, returning borrow. Can modify in place. */
vli_sub(u64 * result,const u64 * left,const u64 * right,unsigned int ndigits)3324a2289daSVitaly Chikunov u64 vli_sub(u64 *result, const u64 *left, const u64 *right,
3333c4b2390SSalvatore Benedetto 		   unsigned int ndigits)
3343c4b2390SSalvatore Benedetto {
3353c4b2390SSalvatore Benedetto 	u64 borrow = 0;
3363c4b2390SSalvatore Benedetto 	int i;
3373c4b2390SSalvatore Benedetto 
3383c4b2390SSalvatore Benedetto 	for (i = 0; i < ndigits; i++) {
3393c4b2390SSalvatore Benedetto 		u64 diff;
3403c4b2390SSalvatore Benedetto 
3413c4b2390SSalvatore Benedetto 		diff = left[i] - right[i] - borrow;
3423c4b2390SSalvatore Benedetto 		if (diff != left[i])
3433c4b2390SSalvatore Benedetto 			borrow = (diff > left[i]);
3443c4b2390SSalvatore Benedetto 
3453c4b2390SSalvatore Benedetto 		result[i] = diff;
3463c4b2390SSalvatore Benedetto 	}
3473c4b2390SSalvatore Benedetto 
3483c4b2390SSalvatore Benedetto 	return borrow;
3493c4b2390SSalvatore Benedetto }
3504a2289daSVitaly Chikunov EXPORT_SYMBOL(vli_sub);
3513c4b2390SSalvatore Benedetto 
3520d7a7864SVitaly Chikunov /* Computes result = left - right, returning borrow. Can modify in place. */
vli_usub(u64 * result,const u64 * left,u64 right,unsigned int ndigits)3530d7a7864SVitaly Chikunov static u64 vli_usub(u64 *result, const u64 *left, u64 right,
3540d7a7864SVitaly Chikunov 	     unsigned int ndigits)
3550d7a7864SVitaly Chikunov {
3560d7a7864SVitaly Chikunov 	u64 borrow = right;
3570d7a7864SVitaly Chikunov 	int i;
3580d7a7864SVitaly Chikunov 
3590d7a7864SVitaly Chikunov 	for (i = 0; i < ndigits; i++) {
3600d7a7864SVitaly Chikunov 		u64 diff;
3610d7a7864SVitaly Chikunov 
3620d7a7864SVitaly Chikunov 		diff = left[i] - borrow;
3630d7a7864SVitaly Chikunov 		if (diff != left[i])
3640d7a7864SVitaly Chikunov 			borrow = (diff > left[i]);
3650d7a7864SVitaly Chikunov 
3660d7a7864SVitaly Chikunov 		result[i] = diff;
3670d7a7864SVitaly Chikunov 	}
3680d7a7864SVitaly Chikunov 
3690d7a7864SVitaly Chikunov 	return borrow;
3700d7a7864SVitaly Chikunov }
3710d7a7864SVitaly Chikunov 
mul_64_64(u64 left,u64 right)3723c4b2390SSalvatore Benedetto static uint128_t mul_64_64(u64 left, u64 right)
3733c4b2390SSalvatore Benedetto {
3740d7a7864SVitaly Chikunov 	uint128_t result;
375c12d3362SArd Biesheuvel #if defined(CONFIG_ARCH_SUPPORTS_INT128)
3760d7a7864SVitaly Chikunov 	unsigned __int128 m = (unsigned __int128)left * right;
3770d7a7864SVitaly Chikunov 
3780d7a7864SVitaly Chikunov 	result.m_low  = m;
3790d7a7864SVitaly Chikunov 	result.m_high = m >> 64;
3800d7a7864SVitaly Chikunov #else
3813c4b2390SSalvatore Benedetto 	u64 a0 = left & 0xffffffffull;
3823c4b2390SSalvatore Benedetto 	u64 a1 = left >> 32;
3833c4b2390SSalvatore Benedetto 	u64 b0 = right & 0xffffffffull;
3843c4b2390SSalvatore Benedetto 	u64 b1 = right >> 32;
3853c4b2390SSalvatore Benedetto 	u64 m0 = a0 * b0;
3863c4b2390SSalvatore Benedetto 	u64 m1 = a0 * b1;
3873c4b2390SSalvatore Benedetto 	u64 m2 = a1 * b0;
3883c4b2390SSalvatore Benedetto 	u64 m3 = a1 * b1;
3893c4b2390SSalvatore Benedetto 
3903c4b2390SSalvatore Benedetto 	m2 += (m0 >> 32);
3913c4b2390SSalvatore Benedetto 	m2 += m1;
3923c4b2390SSalvatore Benedetto 
3933c4b2390SSalvatore Benedetto 	/* Overflow */
3943c4b2390SSalvatore Benedetto 	if (m2 < m1)
3953c4b2390SSalvatore Benedetto 		m3 += 0x100000000ull;
3963c4b2390SSalvatore Benedetto 
3973c4b2390SSalvatore Benedetto 	result.m_low = (m0 & 0xffffffffull) | (m2 << 32);
3983c4b2390SSalvatore Benedetto 	result.m_high = m3 + (m2 >> 32);
3990d7a7864SVitaly Chikunov #endif
4003c4b2390SSalvatore Benedetto 	return result;
4013c4b2390SSalvatore Benedetto }
4023c4b2390SSalvatore Benedetto 
add_128_128(uint128_t a,uint128_t b)4033c4b2390SSalvatore Benedetto static uint128_t add_128_128(uint128_t a, uint128_t b)
4043c4b2390SSalvatore Benedetto {
4053c4b2390SSalvatore Benedetto 	uint128_t result;
4063c4b2390SSalvatore Benedetto 
4073c4b2390SSalvatore Benedetto 	result.m_low = a.m_low + b.m_low;
4083c4b2390SSalvatore Benedetto 	result.m_high = a.m_high + b.m_high + (result.m_low < a.m_low);
4093c4b2390SSalvatore Benedetto 
4103c4b2390SSalvatore Benedetto 	return result;
4113c4b2390SSalvatore Benedetto }
4123c4b2390SSalvatore Benedetto 
vli_mult(u64 * result,const u64 * left,const u64 * right,unsigned int ndigits)4133c4b2390SSalvatore Benedetto static void vli_mult(u64 *result, const u64 *left, const u64 *right,
4143c4b2390SSalvatore Benedetto 		     unsigned int ndigits)
4153c4b2390SSalvatore Benedetto {
4163c4b2390SSalvatore Benedetto 	uint128_t r01 = { 0, 0 };
4173c4b2390SSalvatore Benedetto 	u64 r2 = 0;
4183c4b2390SSalvatore Benedetto 	unsigned int i, k;
4193c4b2390SSalvatore Benedetto 
4203c4b2390SSalvatore Benedetto 	/* Compute each digit of result in sequence, maintaining the
4213c4b2390SSalvatore Benedetto 	 * carries.
4223c4b2390SSalvatore Benedetto 	 */
4233c4b2390SSalvatore Benedetto 	for (k = 0; k < ndigits * 2 - 1; k++) {
4243c4b2390SSalvatore Benedetto 		unsigned int min;
4253c4b2390SSalvatore Benedetto 
4263c4b2390SSalvatore Benedetto 		if (k < ndigits)
4273c4b2390SSalvatore Benedetto 			min = 0;
4283c4b2390SSalvatore Benedetto 		else
4293c4b2390SSalvatore Benedetto 			min = (k + 1) - ndigits;
4303c4b2390SSalvatore Benedetto 
4313c4b2390SSalvatore Benedetto 		for (i = min; i <= k && i < ndigits; i++) {
4323c4b2390SSalvatore Benedetto 			uint128_t product;
4333c4b2390SSalvatore Benedetto 
4343c4b2390SSalvatore Benedetto 			product = mul_64_64(left[i], right[k - i]);
4353c4b2390SSalvatore Benedetto 
4363c4b2390SSalvatore Benedetto 			r01 = add_128_128(r01, product);
4373c4b2390SSalvatore Benedetto 			r2 += (r01.m_high < product.m_high);
4383c4b2390SSalvatore Benedetto 		}
4393c4b2390SSalvatore Benedetto 
4403c4b2390SSalvatore Benedetto 		result[k] = r01.m_low;
4413c4b2390SSalvatore Benedetto 		r01.m_low = r01.m_high;
4423c4b2390SSalvatore Benedetto 		r01.m_high = r2;
4433c4b2390SSalvatore Benedetto 		r2 = 0;
4443c4b2390SSalvatore Benedetto 	}
4453c4b2390SSalvatore Benedetto 
4463c4b2390SSalvatore Benedetto 	result[ndigits * 2 - 1] = r01.m_low;
4473c4b2390SSalvatore Benedetto }
4483c4b2390SSalvatore Benedetto 
4490d7a7864SVitaly Chikunov /* Compute product = left * right, for a small right value. */
vli_umult(u64 * result,const u64 * left,u32 right,unsigned int ndigits)4500d7a7864SVitaly Chikunov static void vli_umult(u64 *result, const u64 *left, u32 right,
4510d7a7864SVitaly Chikunov 		      unsigned int ndigits)
4520d7a7864SVitaly Chikunov {
4530d7a7864SVitaly Chikunov 	uint128_t r01 = { 0 };
4540d7a7864SVitaly Chikunov 	unsigned int k;
4550d7a7864SVitaly Chikunov 
4560d7a7864SVitaly Chikunov 	for (k = 0; k < ndigits; k++) {
4570d7a7864SVitaly Chikunov 		uint128_t product;
4580d7a7864SVitaly Chikunov 
4590d7a7864SVitaly Chikunov 		product = mul_64_64(left[k], right);
4600d7a7864SVitaly Chikunov 		r01 = add_128_128(r01, product);
4610d7a7864SVitaly Chikunov 		/* no carry */
4620d7a7864SVitaly Chikunov 		result[k] = r01.m_low;
4630d7a7864SVitaly Chikunov 		r01.m_low = r01.m_high;
4640d7a7864SVitaly Chikunov 		r01.m_high = 0;
4650d7a7864SVitaly Chikunov 	}
4660d7a7864SVitaly Chikunov 	result[k] = r01.m_low;
4670d7a7864SVitaly Chikunov 	for (++k; k < ndigits * 2; k++)
4680d7a7864SVitaly Chikunov 		result[k] = 0;
4690d7a7864SVitaly Chikunov }
4700d7a7864SVitaly Chikunov 
vli_square(u64 * result,const u64 * left,unsigned int ndigits)4713c4b2390SSalvatore Benedetto static void vli_square(u64 *result, const u64 *left, unsigned int ndigits)
4723c4b2390SSalvatore Benedetto {
4733c4b2390SSalvatore Benedetto 	uint128_t r01 = { 0, 0 };
4743c4b2390SSalvatore Benedetto 	u64 r2 = 0;
4753c4b2390SSalvatore Benedetto 	int i, k;
4763c4b2390SSalvatore Benedetto 
4773c4b2390SSalvatore Benedetto 	for (k = 0; k < ndigits * 2 - 1; k++) {
4783c4b2390SSalvatore Benedetto 		unsigned int min;
4793c4b2390SSalvatore Benedetto 
4803c4b2390SSalvatore Benedetto 		if (k < ndigits)
4813c4b2390SSalvatore Benedetto 			min = 0;
4823c4b2390SSalvatore Benedetto 		else
4833c4b2390SSalvatore Benedetto 			min = (k + 1) - ndigits;
4843c4b2390SSalvatore Benedetto 
4853c4b2390SSalvatore Benedetto 		for (i = min; i <= k && i <= k - i; i++) {
4863c4b2390SSalvatore Benedetto 			uint128_t product;
4873c4b2390SSalvatore Benedetto 
4883c4b2390SSalvatore Benedetto 			product = mul_64_64(left[i], left[k - i]);
4893c4b2390SSalvatore Benedetto 
4903c4b2390SSalvatore Benedetto 			if (i < k - i) {
4913c4b2390SSalvatore Benedetto 				r2 += product.m_high >> 63;
4923c4b2390SSalvatore Benedetto 				product.m_high = (product.m_high << 1) |
4933c4b2390SSalvatore Benedetto 						 (product.m_low >> 63);
4943c4b2390SSalvatore Benedetto 				product.m_low <<= 1;
4953c4b2390SSalvatore Benedetto 			}
4963c4b2390SSalvatore Benedetto 
4973c4b2390SSalvatore Benedetto 			r01 = add_128_128(r01, product);
4983c4b2390SSalvatore Benedetto 			r2 += (r01.m_high < product.m_high);
4993c4b2390SSalvatore Benedetto 		}
5003c4b2390SSalvatore Benedetto 
5013c4b2390SSalvatore Benedetto 		result[k] = r01.m_low;
5023c4b2390SSalvatore Benedetto 		r01.m_low = r01.m_high;
5033c4b2390SSalvatore Benedetto 		r01.m_high = r2;
5043c4b2390SSalvatore Benedetto 		r2 = 0;
5053c4b2390SSalvatore Benedetto 	}
5063c4b2390SSalvatore Benedetto 
5073c4b2390SSalvatore Benedetto 	result[ndigits * 2 - 1] = r01.m_low;
5083c4b2390SSalvatore Benedetto }
5093c4b2390SSalvatore Benedetto 
5103c4b2390SSalvatore Benedetto /* Computes result = (left + right) % mod.
5113c4b2390SSalvatore Benedetto  * Assumes that left < mod and right < mod, result != mod.
5123c4b2390SSalvatore Benedetto  */
vli_mod_add(u64 * result,const u64 * left,const u64 * right,const u64 * mod,unsigned int ndigits)5133c4b2390SSalvatore Benedetto static void vli_mod_add(u64 *result, const u64 *left, const u64 *right,
5143c4b2390SSalvatore Benedetto 			const u64 *mod, unsigned int ndigits)
5153c4b2390SSalvatore Benedetto {
5163c4b2390SSalvatore Benedetto 	u64 carry;
5173c4b2390SSalvatore Benedetto 
5183c4b2390SSalvatore Benedetto 	carry = vli_add(result, left, right, ndigits);
5193c4b2390SSalvatore Benedetto 
5203c4b2390SSalvatore Benedetto 	/* result > mod (result = mod + remainder), so subtract mod to
5213c4b2390SSalvatore Benedetto 	 * get remainder.
5223c4b2390SSalvatore Benedetto 	 */
5233c4b2390SSalvatore Benedetto 	if (carry || vli_cmp(result, mod, ndigits) >= 0)
5243c4b2390SSalvatore Benedetto 		vli_sub(result, result, mod, ndigits);
5253c4b2390SSalvatore Benedetto }
5263c4b2390SSalvatore Benedetto 
5273c4b2390SSalvatore Benedetto /* Computes result = (left - right) % mod.
5283c4b2390SSalvatore Benedetto  * Assumes that left < mod and right < mod, result != mod.
5293c4b2390SSalvatore Benedetto  */
vli_mod_sub(u64 * result,const u64 * left,const u64 * right,const u64 * mod,unsigned int ndigits)5303c4b2390SSalvatore Benedetto static void vli_mod_sub(u64 *result, const u64 *left, const u64 *right,
5313c4b2390SSalvatore Benedetto 			const u64 *mod, unsigned int ndigits)
5323c4b2390SSalvatore Benedetto {
5333c4b2390SSalvatore Benedetto 	u64 borrow = vli_sub(result, left, right, ndigits);
5343c4b2390SSalvatore Benedetto 
5353c4b2390SSalvatore Benedetto 	/* In this case, p_result == -diff == (max int) - diff.
5363c4b2390SSalvatore Benedetto 	 * Since -x % d == d - x, we can get the correct result from
5373c4b2390SSalvatore Benedetto 	 * result + mod (with overflow).
5383c4b2390SSalvatore Benedetto 	 */
5393c4b2390SSalvatore Benedetto 	if (borrow)
5403c4b2390SSalvatore Benedetto 		vli_add(result, result, mod, ndigits);
5413c4b2390SSalvatore Benedetto }
5423c4b2390SSalvatore Benedetto 
5430d7a7864SVitaly Chikunov /*
5440d7a7864SVitaly Chikunov  * Computes result = product % mod
5450d7a7864SVitaly Chikunov  * for special form moduli: p = 2^k-c, for small c (note the minus sign)
5460d7a7864SVitaly Chikunov  *
5470d7a7864SVitaly Chikunov  * References:
5480d7a7864SVitaly Chikunov  * R. Crandall, C. Pomerance. Prime Numbers: A Computational Perspective.
5490d7a7864SVitaly Chikunov  * 9 Fast Algorithms for Large-Integer Arithmetic. 9.2.3 Moduli of special form
5500d7a7864SVitaly Chikunov  * Algorithm 9.2.13 (Fast mod operation for special-form moduli).
5510d7a7864SVitaly Chikunov  */
vli_mmod_special(u64 * result,const u64 * product,const u64 * mod,unsigned int ndigits)5520d7a7864SVitaly Chikunov static void vli_mmod_special(u64 *result, const u64 *product,
5530d7a7864SVitaly Chikunov 			      const u64 *mod, unsigned int ndigits)
5540d7a7864SVitaly Chikunov {
5550d7a7864SVitaly Chikunov 	u64 c = -mod[0];
5560d7a7864SVitaly Chikunov 	u64 t[ECC_MAX_DIGITS * 2];
5570d7a7864SVitaly Chikunov 	u64 r[ECC_MAX_DIGITS * 2];
5580d7a7864SVitaly Chikunov 
5590d7a7864SVitaly Chikunov 	vli_set(r, product, ndigits * 2);
5600d7a7864SVitaly Chikunov 	while (!vli_is_zero(r + ndigits, ndigits)) {
5610d7a7864SVitaly Chikunov 		vli_umult(t, r + ndigits, c, ndigits);
5620d7a7864SVitaly Chikunov 		vli_clear(r + ndigits, ndigits);
5630d7a7864SVitaly Chikunov 		vli_add(r, r, t, ndigits * 2);
5640d7a7864SVitaly Chikunov 	}
5650d7a7864SVitaly Chikunov 	vli_set(t, mod, ndigits);
5660d7a7864SVitaly Chikunov 	vli_clear(t + ndigits, ndigits);
5670d7a7864SVitaly Chikunov 	while (vli_cmp(r, t, ndigits * 2) >= 0)
5680d7a7864SVitaly Chikunov 		vli_sub(r, r, t, ndigits * 2);
5690d7a7864SVitaly Chikunov 	vli_set(result, r, ndigits);
5700d7a7864SVitaly Chikunov }
5710d7a7864SVitaly Chikunov 
5720d7a7864SVitaly Chikunov /*
5730d7a7864SVitaly Chikunov  * Computes result = product % mod
5740d7a7864SVitaly Chikunov  * for special form moduli: p = 2^{k-1}+c, for small c (note the plus sign)
5750d7a7864SVitaly Chikunov  * where k-1 does not fit into qword boundary by -1 bit (such as 255).
5760d7a7864SVitaly Chikunov 
5770d7a7864SVitaly Chikunov  * References (loosely based on):
5780d7a7864SVitaly Chikunov  * A. Menezes, P. van Oorschot, S. Vanstone. Handbook of Applied Cryptography.
5790d7a7864SVitaly Chikunov  * 14.3.4 Reduction methods for moduli of special form. Algorithm 14.47.
5800d7a7864SVitaly Chikunov  * URL: http://cacr.uwaterloo.ca/hac/about/chap14.pdf
5810d7a7864SVitaly Chikunov  *
5820d7a7864SVitaly Chikunov  * H. Cohen, G. Frey, R. Avanzi, C. Doche, T. Lange, K. Nguyen, F. Vercauteren.
5830d7a7864SVitaly Chikunov  * Handbook of Elliptic and Hyperelliptic Curve Cryptography.
5840d7a7864SVitaly Chikunov  * Algorithm 10.25 Fast reduction for special form moduli
5850d7a7864SVitaly Chikunov  */
vli_mmod_special2(u64 * result,const u64 * product,const u64 * mod,unsigned int ndigits)5860d7a7864SVitaly Chikunov static void vli_mmod_special2(u64 *result, const u64 *product,
5870d7a7864SVitaly Chikunov 			       const u64 *mod, unsigned int ndigits)
5880d7a7864SVitaly Chikunov {
5890d7a7864SVitaly Chikunov 	u64 c2 = mod[0] * 2;
5900d7a7864SVitaly Chikunov 	u64 q[ECC_MAX_DIGITS];
5910d7a7864SVitaly Chikunov 	u64 r[ECC_MAX_DIGITS * 2];
5920d7a7864SVitaly Chikunov 	u64 m[ECC_MAX_DIGITS * 2]; /* expanded mod */
5930d7a7864SVitaly Chikunov 	int carry; /* last bit that doesn't fit into q */
5940d7a7864SVitaly Chikunov 	int i;
5950d7a7864SVitaly Chikunov 
5960d7a7864SVitaly Chikunov 	vli_set(m, mod, ndigits);
5970d7a7864SVitaly Chikunov 	vli_clear(m + ndigits, ndigits);
5980d7a7864SVitaly Chikunov 
5990d7a7864SVitaly Chikunov 	vli_set(r, product, ndigits);
6000d7a7864SVitaly Chikunov 	/* q and carry are top bits */
6010d7a7864SVitaly Chikunov 	vli_set(q, product + ndigits, ndigits);
6020d7a7864SVitaly Chikunov 	vli_clear(r + ndigits, ndigits);
6030d7a7864SVitaly Chikunov 	carry = vli_is_negative(r, ndigits);
6040d7a7864SVitaly Chikunov 	if (carry)
6050d7a7864SVitaly Chikunov 		r[ndigits - 1] &= (1ull << 63) - 1;
6060d7a7864SVitaly Chikunov 	for (i = 1; carry || !vli_is_zero(q, ndigits); i++) {
6070d7a7864SVitaly Chikunov 		u64 qc[ECC_MAX_DIGITS * 2];
6080d7a7864SVitaly Chikunov 
6090d7a7864SVitaly Chikunov 		vli_umult(qc, q, c2, ndigits);
6100d7a7864SVitaly Chikunov 		if (carry)
6110d7a7864SVitaly Chikunov 			vli_uadd(qc, qc, mod[0], ndigits * 2);
6120d7a7864SVitaly Chikunov 		vli_set(q, qc + ndigits, ndigits);
6130d7a7864SVitaly Chikunov 		vli_clear(qc + ndigits, ndigits);
6140d7a7864SVitaly Chikunov 		carry = vli_is_negative(qc, ndigits);
6150d7a7864SVitaly Chikunov 		if (carry)
6160d7a7864SVitaly Chikunov 			qc[ndigits - 1] &= (1ull << 63) - 1;
6170d7a7864SVitaly Chikunov 		if (i & 1)
6180d7a7864SVitaly Chikunov 			vli_sub(r, r, qc, ndigits * 2);
6190d7a7864SVitaly Chikunov 		else
6200d7a7864SVitaly Chikunov 			vli_add(r, r, qc, ndigits * 2);
6210d7a7864SVitaly Chikunov 	}
6220d7a7864SVitaly Chikunov 	while (vli_is_negative(r, ndigits * 2))
6230d7a7864SVitaly Chikunov 		vli_add(r, r, m, ndigits * 2);
6240d7a7864SVitaly Chikunov 	while (vli_cmp(r, m, ndigits * 2) >= 0)
6250d7a7864SVitaly Chikunov 		vli_sub(r, r, m, ndigits * 2);
6260d7a7864SVitaly Chikunov 
6270d7a7864SVitaly Chikunov 	vli_set(result, r, ndigits);
6280d7a7864SVitaly Chikunov }
6290d7a7864SVitaly Chikunov 
6300d7a7864SVitaly Chikunov /*
6310d7a7864SVitaly Chikunov  * Computes result = product % mod, where product is 2N words long.
6320d7a7864SVitaly Chikunov  * Reference: Ken MacKay's micro-ecc.
6330d7a7864SVitaly Chikunov  * Currently only designed to work for curve_p or curve_n.
6340d7a7864SVitaly Chikunov  */
vli_mmod_slow(u64 * result,u64 * product,const u64 * mod,unsigned int ndigits)6350d7a7864SVitaly Chikunov static void vli_mmod_slow(u64 *result, u64 *product, const u64 *mod,
6360d7a7864SVitaly Chikunov 			  unsigned int ndigits)
6370d7a7864SVitaly Chikunov {
6380d7a7864SVitaly Chikunov 	u64 mod_m[2 * ECC_MAX_DIGITS];
6390d7a7864SVitaly Chikunov 	u64 tmp[2 * ECC_MAX_DIGITS];
6400d7a7864SVitaly Chikunov 	u64 *v[2] = { tmp, product };
6410d7a7864SVitaly Chikunov 	u64 carry = 0;
6420d7a7864SVitaly Chikunov 	unsigned int i;
6430d7a7864SVitaly Chikunov 	/* Shift mod so its highest set bit is at the maximum position. */
6440d7a7864SVitaly Chikunov 	int shift = (ndigits * 2 * 64) - vli_num_bits(mod, ndigits);
6450d7a7864SVitaly Chikunov 	int word_shift = shift / 64;
6460d7a7864SVitaly Chikunov 	int bit_shift = shift % 64;
6470d7a7864SVitaly Chikunov 
6480d7a7864SVitaly Chikunov 	vli_clear(mod_m, word_shift);
6490d7a7864SVitaly Chikunov 	if (bit_shift > 0) {
6500d7a7864SVitaly Chikunov 		for (i = 0; i < ndigits; ++i) {
6510d7a7864SVitaly Chikunov 			mod_m[word_shift + i] = (mod[i] << bit_shift) | carry;
6520d7a7864SVitaly Chikunov 			carry = mod[i] >> (64 - bit_shift);
6530d7a7864SVitaly Chikunov 		}
6540d7a7864SVitaly Chikunov 	} else
6550d7a7864SVitaly Chikunov 		vli_set(mod_m + word_shift, mod, ndigits);
6560d7a7864SVitaly Chikunov 
6570d7a7864SVitaly Chikunov 	for (i = 1; shift >= 0; --shift) {
6580d7a7864SVitaly Chikunov 		u64 borrow = 0;
6590d7a7864SVitaly Chikunov 		unsigned int j;
6600d7a7864SVitaly Chikunov 
6610d7a7864SVitaly Chikunov 		for (j = 0; j < ndigits * 2; ++j) {
6620d7a7864SVitaly Chikunov 			u64 diff = v[i][j] - mod_m[j] - borrow;
6630d7a7864SVitaly Chikunov 
6640d7a7864SVitaly Chikunov 			if (diff != v[i][j])
6650d7a7864SVitaly Chikunov 				borrow = (diff > v[i][j]);
6660d7a7864SVitaly Chikunov 			v[1 - i][j] = diff;
6670d7a7864SVitaly Chikunov 		}
6680d7a7864SVitaly Chikunov 		i = !(i ^ borrow); /* Swap the index if there was no borrow */
6690d7a7864SVitaly Chikunov 		vli_rshift1(mod_m, ndigits);
6700d7a7864SVitaly Chikunov 		mod_m[ndigits - 1] |= mod_m[ndigits] << (64 - 1);
6710d7a7864SVitaly Chikunov 		vli_rshift1(mod_m + ndigits, ndigits);
6720d7a7864SVitaly Chikunov 	}
6730d7a7864SVitaly Chikunov 	vli_set(result, v[i], ndigits);
6740d7a7864SVitaly Chikunov }
6750d7a7864SVitaly Chikunov 
6760d7a7864SVitaly Chikunov /* Computes result = product % mod using Barrett's reduction with precomputed
6770d7a7864SVitaly Chikunov  * value mu appended to the mod after ndigits, mu = (2^{2w} / mod) and have
6780d7a7864SVitaly Chikunov  * length ndigits + 1, where mu * (2^w - 1) should not overflow ndigits
6790d7a7864SVitaly Chikunov  * boundary.
6800d7a7864SVitaly Chikunov  *
6810d7a7864SVitaly Chikunov  * Reference:
6820d7a7864SVitaly Chikunov  * R. Brent, P. Zimmermann. Modern Computer Arithmetic. 2010.
6830d7a7864SVitaly Chikunov  * 2.4.1 Barrett's algorithm. Algorithm 2.5.
6840d7a7864SVitaly Chikunov  */
vli_mmod_barrett(u64 * result,u64 * product,const u64 * mod,unsigned int ndigits)6850d7a7864SVitaly Chikunov static void vli_mmod_barrett(u64 *result, u64 *product, const u64 *mod,
6860d7a7864SVitaly Chikunov 			     unsigned int ndigits)
6870d7a7864SVitaly Chikunov {
6880d7a7864SVitaly Chikunov 	u64 q[ECC_MAX_DIGITS * 2];
6890d7a7864SVitaly Chikunov 	u64 r[ECC_MAX_DIGITS * 2];
6900d7a7864SVitaly Chikunov 	const u64 *mu = mod + ndigits;
6910d7a7864SVitaly Chikunov 
6920d7a7864SVitaly Chikunov 	vli_mult(q, product + ndigits, mu, ndigits);
6930d7a7864SVitaly Chikunov 	if (mu[ndigits])
6940d7a7864SVitaly Chikunov 		vli_add(q + ndigits, q + ndigits, product + ndigits, ndigits);
6950d7a7864SVitaly Chikunov 	vli_mult(r, mod, q + ndigits, ndigits);
6960d7a7864SVitaly Chikunov 	vli_sub(r, product, r, ndigits * 2);
6970d7a7864SVitaly Chikunov 	while (!vli_is_zero(r + ndigits, ndigits) ||
6980d7a7864SVitaly Chikunov 	       vli_cmp(r, mod, ndigits) != -1) {
6990d7a7864SVitaly Chikunov 		u64 carry;
7000d7a7864SVitaly Chikunov 
7010d7a7864SVitaly Chikunov 		carry = vli_sub(r, r, mod, ndigits);
7020d7a7864SVitaly Chikunov 		vli_usub(r + ndigits, r + ndigits, carry, ndigits);
7030d7a7864SVitaly Chikunov 	}
7040d7a7864SVitaly Chikunov 	vli_set(result, r, ndigits);
7050d7a7864SVitaly Chikunov }
7060d7a7864SVitaly Chikunov 
7073c4b2390SSalvatore Benedetto /* Computes p_result = p_product % curve_p.
7083c4b2390SSalvatore Benedetto  * See algorithm 5 and 6 from
7093c4b2390SSalvatore Benedetto  * http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf
7103c4b2390SSalvatore Benedetto  */
vli_mmod_fast_192(u64 * result,const u64 * product,const u64 * curve_prime,u64 * tmp)7113c4b2390SSalvatore Benedetto static void vli_mmod_fast_192(u64 *result, const u64 *product,
7123c4b2390SSalvatore Benedetto 			      const u64 *curve_prime, u64 *tmp)
7133c4b2390SSalvatore Benedetto {
7143c4b2390SSalvatore Benedetto 	const unsigned int ndigits = 3;
7153c4b2390SSalvatore Benedetto 	int carry;
7163c4b2390SSalvatore Benedetto 
7173c4b2390SSalvatore Benedetto 	vli_set(result, product, ndigits);
7183c4b2390SSalvatore Benedetto 
7193c4b2390SSalvatore Benedetto 	vli_set(tmp, &product[3], ndigits);
7203c4b2390SSalvatore Benedetto 	carry = vli_add(result, result, tmp, ndigits);
7213c4b2390SSalvatore Benedetto 
7223c4b2390SSalvatore Benedetto 	tmp[0] = 0;
7233c4b2390SSalvatore Benedetto 	tmp[1] = product[3];
7243c4b2390SSalvatore Benedetto 	tmp[2] = product[4];
7253c4b2390SSalvatore Benedetto 	carry += vli_add(result, result, tmp, ndigits);
7263c4b2390SSalvatore Benedetto 
7273c4b2390SSalvatore Benedetto 	tmp[0] = tmp[1] = product[5];
7283c4b2390SSalvatore Benedetto 	tmp[2] = 0;
7293c4b2390SSalvatore Benedetto 	carry += vli_add(result, result, tmp, ndigits);
7303c4b2390SSalvatore Benedetto 
7313c4b2390SSalvatore Benedetto 	while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
7323c4b2390SSalvatore Benedetto 		carry -= vli_sub(result, result, curve_prime, ndigits);
7333c4b2390SSalvatore Benedetto }
7343c4b2390SSalvatore Benedetto 
7353c4b2390SSalvatore Benedetto /* Computes result = product % curve_prime
7363c4b2390SSalvatore Benedetto  * from http://www.nsa.gov/ia/_files/nist-routines.pdf
7373c4b2390SSalvatore Benedetto  */
vli_mmod_fast_256(u64 * result,const u64 * product,const u64 * curve_prime,u64 * tmp)7383c4b2390SSalvatore Benedetto static void vli_mmod_fast_256(u64 *result, const u64 *product,
7393c4b2390SSalvatore Benedetto 			      const u64 *curve_prime, u64 *tmp)
7403c4b2390SSalvatore Benedetto {
7413c4b2390SSalvatore Benedetto 	int carry;
7423c4b2390SSalvatore Benedetto 	const unsigned int ndigits = 4;
7433c4b2390SSalvatore Benedetto 
7443c4b2390SSalvatore Benedetto 	/* t */
7453c4b2390SSalvatore Benedetto 	vli_set(result, product, ndigits);
7463c4b2390SSalvatore Benedetto 
7473c4b2390SSalvatore Benedetto 	/* s1 */
7483c4b2390SSalvatore Benedetto 	tmp[0] = 0;
7493c4b2390SSalvatore Benedetto 	tmp[1] = product[5] & 0xffffffff00000000ull;
7503c4b2390SSalvatore Benedetto 	tmp[2] = product[6];
7513c4b2390SSalvatore Benedetto 	tmp[3] = product[7];
7523c4b2390SSalvatore Benedetto 	carry = vli_lshift(tmp, tmp, 1, ndigits);
7533c4b2390SSalvatore Benedetto 	carry += vli_add(result, result, tmp, ndigits);
7543c4b2390SSalvatore Benedetto 
7553c4b2390SSalvatore Benedetto 	/* s2 */
7563c4b2390SSalvatore Benedetto 	tmp[1] = product[6] << 32;
7573c4b2390SSalvatore Benedetto 	tmp[2] = (product[6] >> 32) | (product[7] << 32);
7583c4b2390SSalvatore Benedetto 	tmp[3] = product[7] >> 32;
7593c4b2390SSalvatore Benedetto 	carry += vli_lshift(tmp, tmp, 1, ndigits);
7603c4b2390SSalvatore Benedetto 	carry += vli_add(result, result, tmp, ndigits);
7613c4b2390SSalvatore Benedetto 
7623c4b2390SSalvatore Benedetto 	/* s3 */
7633c4b2390SSalvatore Benedetto 	tmp[0] = product[4];
7643c4b2390SSalvatore Benedetto 	tmp[1] = product[5] & 0xffffffff;
7653c4b2390SSalvatore Benedetto 	tmp[2] = 0;
7663c4b2390SSalvatore Benedetto 	tmp[3] = product[7];
7673c4b2390SSalvatore Benedetto 	carry += vli_add(result, result, tmp, ndigits);
7683c4b2390SSalvatore Benedetto 
7693c4b2390SSalvatore Benedetto 	/* s4 */
7703c4b2390SSalvatore Benedetto 	tmp[0] = (product[4] >> 32) | (product[5] << 32);
7713c4b2390SSalvatore Benedetto 	tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull);
7723c4b2390SSalvatore Benedetto 	tmp[2] = product[7];
7733c4b2390SSalvatore Benedetto 	tmp[3] = (product[6] >> 32) | (product[4] << 32);
7743c4b2390SSalvatore Benedetto 	carry += vli_add(result, result, tmp, ndigits);
7753c4b2390SSalvatore Benedetto 
7763c4b2390SSalvatore Benedetto 	/* d1 */
7773c4b2390SSalvatore Benedetto 	tmp[0] = (product[5] >> 32) | (product[6] << 32);
7783c4b2390SSalvatore Benedetto 	tmp[1] = (product[6] >> 32);
7793c4b2390SSalvatore Benedetto 	tmp[2] = 0;
7803c4b2390SSalvatore Benedetto 	tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32);
7813c4b2390SSalvatore Benedetto 	carry -= vli_sub(result, result, tmp, ndigits);
7823c4b2390SSalvatore Benedetto 
7833c4b2390SSalvatore Benedetto 	/* d2 */
7843c4b2390SSalvatore Benedetto 	tmp[0] = product[6];
7853c4b2390SSalvatore Benedetto 	tmp[1] = product[7];
7863c4b2390SSalvatore Benedetto 	tmp[2] = 0;
7873c4b2390SSalvatore Benedetto 	tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull);
7883c4b2390SSalvatore Benedetto 	carry -= vli_sub(result, result, tmp, ndigits);
7893c4b2390SSalvatore Benedetto 
7903c4b2390SSalvatore Benedetto 	/* d3 */
7913c4b2390SSalvatore Benedetto 	tmp[0] = (product[6] >> 32) | (product[7] << 32);
7923c4b2390SSalvatore Benedetto 	tmp[1] = (product[7] >> 32) | (product[4] << 32);
7933c4b2390SSalvatore Benedetto 	tmp[2] = (product[4] >> 32) | (product[5] << 32);
7943c4b2390SSalvatore Benedetto 	tmp[3] = (product[6] << 32);
7953c4b2390SSalvatore Benedetto 	carry -= vli_sub(result, result, tmp, ndigits);
7963c4b2390SSalvatore Benedetto 
7973c4b2390SSalvatore Benedetto 	/* d4 */
7983c4b2390SSalvatore Benedetto 	tmp[0] = product[7];
7993c4b2390SSalvatore Benedetto 	tmp[1] = product[4] & 0xffffffff00000000ull;
8003c4b2390SSalvatore Benedetto 	tmp[2] = product[5];
8013c4b2390SSalvatore Benedetto 	tmp[3] = product[6] & 0xffffffff00000000ull;
8023c4b2390SSalvatore Benedetto 	carry -= vli_sub(result, result, tmp, ndigits);
8033c4b2390SSalvatore Benedetto 
8043c4b2390SSalvatore Benedetto 	if (carry < 0) {
8053c4b2390SSalvatore Benedetto 		do {
8063c4b2390SSalvatore Benedetto 			carry += vli_add(result, result, curve_prime, ndigits);
8073c4b2390SSalvatore Benedetto 		} while (carry < 0);
8083c4b2390SSalvatore Benedetto 	} else {
8093c4b2390SSalvatore Benedetto 		while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
8103c4b2390SSalvatore Benedetto 			carry -= vli_sub(result, result, curve_prime, ndigits);
8113c4b2390SSalvatore Benedetto 	}
8123c4b2390SSalvatore Benedetto }
8133c4b2390SSalvatore Benedetto 
814149ca161SSaulo Alessandre #define SL32OR32(x32, y32) (((u64)x32 << 32) | y32)
815149ca161SSaulo Alessandre #define AND64H(x64)  (x64 & 0xffFFffFF00000000ull)
816149ca161SSaulo Alessandre #define AND64L(x64)  (x64 & 0x00000000ffFFffFFull)
817149ca161SSaulo Alessandre 
818149ca161SSaulo Alessandre /* Computes result = product % curve_prime
819149ca161SSaulo Alessandre  * from "Mathematical routines for the NIST prime elliptic curves"
820149ca161SSaulo Alessandre  */
vli_mmod_fast_384(u64 * result,const u64 * product,const u64 * curve_prime,u64 * tmp)821149ca161SSaulo Alessandre static void vli_mmod_fast_384(u64 *result, const u64 *product,
822149ca161SSaulo Alessandre 				const u64 *curve_prime, u64 *tmp)
823149ca161SSaulo Alessandre {
824149ca161SSaulo Alessandre 	int carry;
825149ca161SSaulo Alessandre 	const unsigned int ndigits = 6;
826149ca161SSaulo Alessandre 
827149ca161SSaulo Alessandre 	/* t */
828149ca161SSaulo Alessandre 	vli_set(result, product, ndigits);
829149ca161SSaulo Alessandre 
830149ca161SSaulo Alessandre 	/* s1 */
831149ca161SSaulo Alessandre 	tmp[0] = 0;		// 0 || 0
832149ca161SSaulo Alessandre 	tmp[1] = 0;		// 0 || 0
833149ca161SSaulo Alessandre 	tmp[2] = SL32OR32(product[11], (product[10]>>32));	//a22||a21
834149ca161SSaulo Alessandre 	tmp[3] = product[11]>>32;	// 0 ||a23
835149ca161SSaulo Alessandre 	tmp[4] = 0;		// 0 || 0
836149ca161SSaulo Alessandre 	tmp[5] = 0;		// 0 || 0
837149ca161SSaulo Alessandre 	carry = vli_lshift(tmp, tmp, 1, ndigits);
838149ca161SSaulo Alessandre 	carry += vli_add(result, result, tmp, ndigits);
839149ca161SSaulo Alessandre 
840149ca161SSaulo Alessandre 	/* s2 */
841149ca161SSaulo Alessandre 	tmp[0] = product[6];	//a13||a12
842149ca161SSaulo Alessandre 	tmp[1] = product[7];	//a15||a14
843149ca161SSaulo Alessandre 	tmp[2] = product[8];	//a17||a16
844149ca161SSaulo Alessandre 	tmp[3] = product[9];	//a19||a18
845149ca161SSaulo Alessandre 	tmp[4] = product[10];	//a21||a20
846149ca161SSaulo Alessandre 	tmp[5] = product[11];	//a23||a22
847149ca161SSaulo Alessandre 	carry += vli_add(result, result, tmp, ndigits);
848149ca161SSaulo Alessandre 
849149ca161SSaulo Alessandre 	/* s3 */
850149ca161SSaulo Alessandre 	tmp[0] = SL32OR32(product[11], (product[10]>>32));	//a22||a21
851149ca161SSaulo Alessandre 	tmp[1] = SL32OR32(product[6], (product[11]>>32));	//a12||a23
852149ca161SSaulo Alessandre 	tmp[2] = SL32OR32(product[7], (product[6])>>32);	//a14||a13
853149ca161SSaulo Alessandre 	tmp[3] = SL32OR32(product[8], (product[7]>>32));	//a16||a15
854149ca161SSaulo Alessandre 	tmp[4] = SL32OR32(product[9], (product[8]>>32));	//a18||a17
855149ca161SSaulo Alessandre 	tmp[5] = SL32OR32(product[10], (product[9]>>32));	//a20||a19
856149ca161SSaulo Alessandre 	carry += vli_add(result, result, tmp, ndigits);
857149ca161SSaulo Alessandre 
858149ca161SSaulo Alessandre 	/* s4 */
859149ca161SSaulo Alessandre 	tmp[0] = AND64H(product[11]);	//a23|| 0
860149ca161SSaulo Alessandre 	tmp[1] = (product[10]<<32);	//a20|| 0
861149ca161SSaulo Alessandre 	tmp[2] = product[6];	//a13||a12
862149ca161SSaulo Alessandre 	tmp[3] = product[7];	//a15||a14
863149ca161SSaulo Alessandre 	tmp[4] = product[8];	//a17||a16
864149ca161SSaulo Alessandre 	tmp[5] = product[9];	//a19||a18
865149ca161SSaulo Alessandre 	carry += vli_add(result, result, tmp, ndigits);
866149ca161SSaulo Alessandre 
867149ca161SSaulo Alessandre 	/* s5 */
868149ca161SSaulo Alessandre 	tmp[0] = 0;		//  0|| 0
869149ca161SSaulo Alessandre 	tmp[1] = 0;		//  0|| 0
870149ca161SSaulo Alessandre 	tmp[2] = product[10];	//a21||a20
871149ca161SSaulo Alessandre 	tmp[3] = product[11];	//a23||a22
872149ca161SSaulo Alessandre 	tmp[4] = 0;		//  0|| 0
873149ca161SSaulo Alessandre 	tmp[5] = 0;		//  0|| 0
874149ca161SSaulo Alessandre 	carry += vli_add(result, result, tmp, ndigits);
875149ca161SSaulo Alessandre 
876149ca161SSaulo Alessandre 	/* s6 */
877149ca161SSaulo Alessandre 	tmp[0] = AND64L(product[10]);	// 0 ||a20
878149ca161SSaulo Alessandre 	tmp[1] = AND64H(product[10]);	//a21|| 0
879149ca161SSaulo Alessandre 	tmp[2] = product[11];	//a23||a22
880149ca161SSaulo Alessandre 	tmp[3] = 0;		// 0 || 0
881149ca161SSaulo Alessandre 	tmp[4] = 0;		// 0 || 0
882149ca161SSaulo Alessandre 	tmp[5] = 0;		// 0 || 0
883149ca161SSaulo Alessandre 	carry += vli_add(result, result, tmp, ndigits);
884149ca161SSaulo Alessandre 
885149ca161SSaulo Alessandre 	/* d1 */
886149ca161SSaulo Alessandre 	tmp[0] = SL32OR32(product[6], (product[11]>>32));	//a12||a23
887149ca161SSaulo Alessandre 	tmp[1] = SL32OR32(product[7], (product[6]>>32));	//a14||a13
888149ca161SSaulo Alessandre 	tmp[2] = SL32OR32(product[8], (product[7]>>32));	//a16||a15
889149ca161SSaulo Alessandre 	tmp[3] = SL32OR32(product[9], (product[8]>>32));	//a18||a17
890149ca161SSaulo Alessandre 	tmp[4] = SL32OR32(product[10], (product[9]>>32));	//a20||a19
891149ca161SSaulo Alessandre 	tmp[5] = SL32OR32(product[11], (product[10]>>32));	//a22||a21
892149ca161SSaulo Alessandre 	carry -= vli_sub(result, result, tmp, ndigits);
893149ca161SSaulo Alessandre 
894149ca161SSaulo Alessandre 	/* d2 */
895149ca161SSaulo Alessandre 	tmp[0] = (product[10]<<32);	//a20|| 0
896149ca161SSaulo Alessandre 	tmp[1] = SL32OR32(product[11], (product[10]>>32));	//a22||a21
897149ca161SSaulo Alessandre 	tmp[2] = (product[11]>>32);	// 0 ||a23
898149ca161SSaulo Alessandre 	tmp[3] = 0;		// 0 || 0
899149ca161SSaulo Alessandre 	tmp[4] = 0;		// 0 || 0
900149ca161SSaulo Alessandre 	tmp[5] = 0;		// 0 || 0
901149ca161SSaulo Alessandre 	carry -= vli_sub(result, result, tmp, ndigits);
902149ca161SSaulo Alessandre 
903149ca161SSaulo Alessandre 	/* d3 */
904149ca161SSaulo Alessandre 	tmp[0] = 0;		// 0 || 0
905149ca161SSaulo Alessandre 	tmp[1] = AND64H(product[11]);	//a23|| 0
906149ca161SSaulo Alessandre 	tmp[2] = product[11]>>32;	// 0 ||a23
907149ca161SSaulo Alessandre 	tmp[3] = 0;		// 0 || 0
908149ca161SSaulo Alessandre 	tmp[4] = 0;		// 0 || 0
909149ca161SSaulo Alessandre 	tmp[5] = 0;		// 0 || 0
910149ca161SSaulo Alessandre 	carry -= vli_sub(result, result, tmp, ndigits);
911149ca161SSaulo Alessandre 
912149ca161SSaulo Alessandre 	if (carry < 0) {
913149ca161SSaulo Alessandre 		do {
914149ca161SSaulo Alessandre 			carry += vli_add(result, result, curve_prime, ndigits);
915149ca161SSaulo Alessandre 		} while (carry < 0);
916149ca161SSaulo Alessandre 	} else {
917149ca161SSaulo Alessandre 		while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
918149ca161SSaulo Alessandre 			carry -= vli_sub(result, result, curve_prime, ndigits);
919149ca161SSaulo Alessandre 	}
920149ca161SSaulo Alessandre 
921149ca161SSaulo Alessandre }
922149ca161SSaulo Alessandre 
923149ca161SSaulo Alessandre #undef SL32OR32
924149ca161SSaulo Alessandre #undef AND64H
925149ca161SSaulo Alessandre #undef AND64L
926149ca161SSaulo Alessandre 
9270d7a7864SVitaly Chikunov /* Computes result = product % curve_prime for different curve_primes.
9280d7a7864SVitaly Chikunov  *
9290d7a7864SVitaly Chikunov  * Note that curve_primes are distinguished just by heuristic check and
9300d7a7864SVitaly Chikunov  * not by complete conformance check.
9313c4b2390SSalvatore Benedetto  */
vli_mmod_fast(u64 * result,u64 * product,const struct ecc_curve * curve)9323c4b2390SSalvatore Benedetto static bool vli_mmod_fast(u64 *result, u64 *product,
933149ca161SSaulo Alessandre 			  const struct ecc_curve *curve)
9343c4b2390SSalvatore Benedetto {
935d5c3b178SKees Cook 	u64 tmp[2 * ECC_MAX_DIGITS];
936149ca161SSaulo Alessandre 	const u64 *curve_prime = curve->p;
937149ca161SSaulo Alessandre 	const unsigned int ndigits = curve->g.ndigits;
9383c4b2390SSalvatore Benedetto 
939149ca161SSaulo Alessandre 	/* All NIST curves have name prefix 'nist_' */
940149ca161SSaulo Alessandre 	if (strncmp(curve->name, "nist_", 5) != 0) {
9410d7a7864SVitaly Chikunov 		/* Try to handle Pseudo-Marsenne primes. */
9420d7a7864SVitaly Chikunov 		if (curve_prime[ndigits - 1] == -1ull) {
9430d7a7864SVitaly Chikunov 			vli_mmod_special(result, product, curve_prime,
9440d7a7864SVitaly Chikunov 					 ndigits);
9450d7a7864SVitaly Chikunov 			return true;
9460d7a7864SVitaly Chikunov 		} else if (curve_prime[ndigits - 1] == 1ull << 63 &&
9470d7a7864SVitaly Chikunov 			   curve_prime[ndigits - 2] == 0) {
9480d7a7864SVitaly Chikunov 			vli_mmod_special2(result, product, curve_prime,
9490d7a7864SVitaly Chikunov 					  ndigits);
9500d7a7864SVitaly Chikunov 			return true;
9510d7a7864SVitaly Chikunov 		}
9520d7a7864SVitaly Chikunov 		vli_mmod_barrett(result, product, curve_prime, ndigits);
9530d7a7864SVitaly Chikunov 		return true;
9540d7a7864SVitaly Chikunov 	}
9550d7a7864SVitaly Chikunov 
9563c4b2390SSalvatore Benedetto 	switch (ndigits) {
9573c4b2390SSalvatore Benedetto 	case 3:
9583c4b2390SSalvatore Benedetto 		vli_mmod_fast_192(result, product, curve_prime, tmp);
9593c4b2390SSalvatore Benedetto 		break;
9603c4b2390SSalvatore Benedetto 	case 4:
9613c4b2390SSalvatore Benedetto 		vli_mmod_fast_256(result, product, curve_prime, tmp);
9623c4b2390SSalvatore Benedetto 		break;
963149ca161SSaulo Alessandre 	case 6:
964149ca161SSaulo Alessandre 		vli_mmod_fast_384(result, product, curve_prime, tmp);
965149ca161SSaulo Alessandre 		break;
9663c4b2390SSalvatore Benedetto 	default:
9670d7a7864SVitaly Chikunov 		pr_err_ratelimited("ecc: unsupported digits size!\n");
9683c4b2390SSalvatore Benedetto 		return false;
9693c4b2390SSalvatore Benedetto 	}
9703c4b2390SSalvatore Benedetto 
9713c4b2390SSalvatore Benedetto 	return true;
9723c4b2390SSalvatore Benedetto }
9733c4b2390SSalvatore Benedetto 
9740d7a7864SVitaly Chikunov /* Computes result = (left * right) % mod.
9750d7a7864SVitaly Chikunov  * Assumes that mod is big enough curve order.
9760d7a7864SVitaly Chikunov  */
vli_mod_mult_slow(u64 * result,const u64 * left,const u64 * right,const u64 * mod,unsigned int ndigits)9770d7a7864SVitaly Chikunov void vli_mod_mult_slow(u64 *result, const u64 *left, const u64 *right,
9780d7a7864SVitaly Chikunov 		       const u64 *mod, unsigned int ndigits)
9790d7a7864SVitaly Chikunov {
9800d7a7864SVitaly Chikunov 	u64 product[ECC_MAX_DIGITS * 2];
9810d7a7864SVitaly Chikunov 
9820d7a7864SVitaly Chikunov 	vli_mult(product, left, right, ndigits);
9830d7a7864SVitaly Chikunov 	vli_mmod_slow(result, product, mod, ndigits);
9840d7a7864SVitaly Chikunov }
9850d7a7864SVitaly Chikunov EXPORT_SYMBOL(vli_mod_mult_slow);
9860d7a7864SVitaly Chikunov 
9873c4b2390SSalvatore Benedetto /* Computes result = (left * right) % curve_prime. */
vli_mod_mult_fast(u64 * result,const u64 * left,const u64 * right,const struct ecc_curve * curve)9883c4b2390SSalvatore Benedetto static void vli_mod_mult_fast(u64 *result, const u64 *left, const u64 *right,
989149ca161SSaulo Alessandre 			      const struct ecc_curve *curve)
9903c4b2390SSalvatore Benedetto {
991d5c3b178SKees Cook 	u64 product[2 * ECC_MAX_DIGITS];
9923c4b2390SSalvatore Benedetto 
993149ca161SSaulo Alessandre 	vli_mult(product, left, right, curve->g.ndigits);
994149ca161SSaulo Alessandre 	vli_mmod_fast(result, product, curve);
9953c4b2390SSalvatore Benedetto }
9963c4b2390SSalvatore Benedetto 
9973c4b2390SSalvatore Benedetto /* Computes result = left^2 % curve_prime. */
vli_mod_square_fast(u64 * result,const u64 * left,const struct ecc_curve * curve)9983c4b2390SSalvatore Benedetto static void vli_mod_square_fast(u64 *result, const u64 *left,
999149ca161SSaulo Alessandre 				const struct ecc_curve *curve)
10003c4b2390SSalvatore Benedetto {
1001d5c3b178SKees Cook 	u64 product[2 * ECC_MAX_DIGITS];
10023c4b2390SSalvatore Benedetto 
1003149ca161SSaulo Alessandre 	vli_square(product, left, curve->g.ndigits);
1004149ca161SSaulo Alessandre 	vli_mmod_fast(result, product, curve);
10053c4b2390SSalvatore Benedetto }
10063c4b2390SSalvatore Benedetto 
10073c4b2390SSalvatore Benedetto #define EVEN(vli) (!(vli[0] & 1))
10083c4b2390SSalvatore Benedetto /* Computes result = (1 / p_input) % mod. All VLIs are the same size.
10093c4b2390SSalvatore Benedetto  * See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
10103c4b2390SSalvatore Benedetto  * https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf
10113c4b2390SSalvatore Benedetto  */
vli_mod_inv(u64 * result,const u64 * input,const u64 * mod,unsigned int ndigits)10124a2289daSVitaly Chikunov void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,
10133c4b2390SSalvatore Benedetto 			unsigned int ndigits)
10143c4b2390SSalvatore Benedetto {
1015d5c3b178SKees Cook 	u64 a[ECC_MAX_DIGITS], b[ECC_MAX_DIGITS];
1016d5c3b178SKees Cook 	u64 u[ECC_MAX_DIGITS], v[ECC_MAX_DIGITS];
10173c4b2390SSalvatore Benedetto 	u64 carry;
10183c4b2390SSalvatore Benedetto 	int cmp_result;
10193c4b2390SSalvatore Benedetto 
10203c4b2390SSalvatore Benedetto 	if (vli_is_zero(input, ndigits)) {
10213c4b2390SSalvatore Benedetto 		vli_clear(result, ndigits);
10223c4b2390SSalvatore Benedetto 		return;
10233c4b2390SSalvatore Benedetto 	}
10243c4b2390SSalvatore Benedetto 
10253c4b2390SSalvatore Benedetto 	vli_set(a, input, ndigits);
10263c4b2390SSalvatore Benedetto 	vli_set(b, mod, ndigits);
10273c4b2390SSalvatore Benedetto 	vli_clear(u, ndigits);
10283c4b2390SSalvatore Benedetto 	u[0] = 1;
10293c4b2390SSalvatore Benedetto 	vli_clear(v, ndigits);
10303c4b2390SSalvatore Benedetto 
10313c4b2390SSalvatore Benedetto 	while ((cmp_result = vli_cmp(a, b, ndigits)) != 0) {
10323c4b2390SSalvatore Benedetto 		carry = 0;
10333c4b2390SSalvatore Benedetto 
10343c4b2390SSalvatore Benedetto 		if (EVEN(a)) {
10353c4b2390SSalvatore Benedetto 			vli_rshift1(a, ndigits);
10363c4b2390SSalvatore Benedetto 
10373c4b2390SSalvatore Benedetto 			if (!EVEN(u))
10383c4b2390SSalvatore Benedetto 				carry = vli_add(u, u, mod, ndigits);
10393c4b2390SSalvatore Benedetto 
10403c4b2390SSalvatore Benedetto 			vli_rshift1(u, ndigits);
10413c4b2390SSalvatore Benedetto 			if (carry)
10423c4b2390SSalvatore Benedetto 				u[ndigits - 1] |= 0x8000000000000000ull;
10433c4b2390SSalvatore Benedetto 		} else if (EVEN(b)) {
10443c4b2390SSalvatore Benedetto 			vli_rshift1(b, ndigits);
10453c4b2390SSalvatore Benedetto 
10463c4b2390SSalvatore Benedetto 			if (!EVEN(v))
10473c4b2390SSalvatore Benedetto 				carry = vli_add(v, v, mod, ndigits);
10483c4b2390SSalvatore Benedetto 
10493c4b2390SSalvatore Benedetto 			vli_rshift1(v, ndigits);
10503c4b2390SSalvatore Benedetto 			if (carry)
10513c4b2390SSalvatore Benedetto 				v[ndigits - 1] |= 0x8000000000000000ull;
10523c4b2390SSalvatore Benedetto 		} else if (cmp_result > 0) {
10533c4b2390SSalvatore Benedetto 			vli_sub(a, a, b, ndigits);
10543c4b2390SSalvatore Benedetto 			vli_rshift1(a, ndigits);
10553c4b2390SSalvatore Benedetto 
10563c4b2390SSalvatore Benedetto 			if (vli_cmp(u, v, ndigits) < 0)
10573c4b2390SSalvatore Benedetto 				vli_add(u, u, mod, ndigits);
10583c4b2390SSalvatore Benedetto 
10593c4b2390SSalvatore Benedetto 			vli_sub(u, u, v, ndigits);
10603c4b2390SSalvatore Benedetto 			if (!EVEN(u))
10613c4b2390SSalvatore Benedetto 				carry = vli_add(u, u, mod, ndigits);
10623c4b2390SSalvatore Benedetto 
10633c4b2390SSalvatore Benedetto 			vli_rshift1(u, ndigits);
10643c4b2390SSalvatore Benedetto 			if (carry)
10653c4b2390SSalvatore Benedetto 				u[ndigits - 1] |= 0x8000000000000000ull;
10663c4b2390SSalvatore Benedetto 		} else {
10673c4b2390SSalvatore Benedetto 			vli_sub(b, b, a, ndigits);
10683c4b2390SSalvatore Benedetto 			vli_rshift1(b, ndigits);
10693c4b2390SSalvatore Benedetto 
10703c4b2390SSalvatore Benedetto 			if (vli_cmp(v, u, ndigits) < 0)
10713c4b2390SSalvatore Benedetto 				vli_add(v, v, mod, ndigits);
10723c4b2390SSalvatore Benedetto 
10733c4b2390SSalvatore Benedetto 			vli_sub(v, v, u, ndigits);
10743c4b2390SSalvatore Benedetto 			if (!EVEN(v))
10753c4b2390SSalvatore Benedetto 				carry = vli_add(v, v, mod, ndigits);
10763c4b2390SSalvatore Benedetto 
10773c4b2390SSalvatore Benedetto 			vli_rshift1(v, ndigits);
10783c4b2390SSalvatore Benedetto 			if (carry)
10793c4b2390SSalvatore Benedetto 				v[ndigits - 1] |= 0x8000000000000000ull;
10803c4b2390SSalvatore Benedetto 		}
10813c4b2390SSalvatore Benedetto 	}
10823c4b2390SSalvatore Benedetto 
10833c4b2390SSalvatore Benedetto 	vli_set(result, u, ndigits);
10843c4b2390SSalvatore Benedetto }
10854a2289daSVitaly Chikunov EXPORT_SYMBOL(vli_mod_inv);
10863c4b2390SSalvatore Benedetto 
10873c4b2390SSalvatore Benedetto /* ------ Point operations ------ */
10883c4b2390SSalvatore Benedetto 
10893c4b2390SSalvatore Benedetto /* Returns true if p_point is the point at infinity, false otherwise. */
ecc_point_is_zero(const struct ecc_point * point)1090eaffe377SDaniele Alessandrelli bool ecc_point_is_zero(const struct ecc_point *point)
10913c4b2390SSalvatore Benedetto {
10923c4b2390SSalvatore Benedetto 	return (vli_is_zero(point->x, point->ndigits) &&
10933c4b2390SSalvatore Benedetto 		vli_is_zero(point->y, point->ndigits));
10943c4b2390SSalvatore Benedetto }
1095eaffe377SDaniele Alessandrelli EXPORT_SYMBOL(ecc_point_is_zero);
10963c4b2390SSalvatore Benedetto 
10973c4b2390SSalvatore Benedetto /* Point multiplication algorithm using Montgomery's ladder with co-Z
10989332a9e7SAlexander A. Klimov  * coordinates. From https://eprint.iacr.org/2011/338.pdf
10993c4b2390SSalvatore Benedetto  */
11003c4b2390SSalvatore Benedetto 
11013c4b2390SSalvatore Benedetto /* Double in place */
ecc_point_double_jacobian(u64 * x1,u64 * y1,u64 * z1,const struct ecc_curve * curve)11023c4b2390SSalvatore Benedetto static void ecc_point_double_jacobian(u64 *x1, u64 *y1, u64 *z1,
1103149ca161SSaulo Alessandre 					const struct ecc_curve *curve)
11043c4b2390SSalvatore Benedetto {
11053c4b2390SSalvatore Benedetto 	/* t1 = x, t2 = y, t3 = z */
1106d5c3b178SKees Cook 	u64 t4[ECC_MAX_DIGITS];
1107d5c3b178SKees Cook 	u64 t5[ECC_MAX_DIGITS];
1108149ca161SSaulo Alessandre 	const u64 *curve_prime = curve->p;
1109149ca161SSaulo Alessandre 	const unsigned int ndigits = curve->g.ndigits;
11103c4b2390SSalvatore Benedetto 
11113c4b2390SSalvatore Benedetto 	if (vli_is_zero(z1, ndigits))
11123c4b2390SSalvatore Benedetto 		return;
11133c4b2390SSalvatore Benedetto 
11143c4b2390SSalvatore Benedetto 	/* t4 = y1^2 */
1115149ca161SSaulo Alessandre 	vli_mod_square_fast(t4, y1, curve);
11163c4b2390SSalvatore Benedetto 	/* t5 = x1*y1^2 = A */
1117149ca161SSaulo Alessandre 	vli_mod_mult_fast(t5, x1, t4, curve);
11183c4b2390SSalvatore Benedetto 	/* t4 = y1^4 */
1119149ca161SSaulo Alessandre 	vli_mod_square_fast(t4, t4, curve);
11203c4b2390SSalvatore Benedetto 	/* t2 = y1*z1 = z3 */
1121149ca161SSaulo Alessandre 	vli_mod_mult_fast(y1, y1, z1, curve);
11223c4b2390SSalvatore Benedetto 	/* t3 = z1^2 */
1123149ca161SSaulo Alessandre 	vli_mod_square_fast(z1, z1, curve);
11243c4b2390SSalvatore Benedetto 
11253c4b2390SSalvatore Benedetto 	/* t1 = x1 + z1^2 */
11263c4b2390SSalvatore Benedetto 	vli_mod_add(x1, x1, z1, curve_prime, ndigits);
11273c4b2390SSalvatore Benedetto 	/* t3 = 2*z1^2 */
11283c4b2390SSalvatore Benedetto 	vli_mod_add(z1, z1, z1, curve_prime, ndigits);
11293c4b2390SSalvatore Benedetto 	/* t3 = x1 - z1^2 */
11303c4b2390SSalvatore Benedetto 	vli_mod_sub(z1, x1, z1, curve_prime, ndigits);
11313c4b2390SSalvatore Benedetto 	/* t1 = x1^2 - z1^4 */
1132149ca161SSaulo Alessandre 	vli_mod_mult_fast(x1, x1, z1, curve);
11333c4b2390SSalvatore Benedetto 
11343c4b2390SSalvatore Benedetto 	/* t3 = 2*(x1^2 - z1^4) */
11353c4b2390SSalvatore Benedetto 	vli_mod_add(z1, x1, x1, curve_prime, ndigits);
11363c4b2390SSalvatore Benedetto 	/* t1 = 3*(x1^2 - z1^4) */
11373c4b2390SSalvatore Benedetto 	vli_mod_add(x1, x1, z1, curve_prime, ndigits);
11383c4b2390SSalvatore Benedetto 	if (vli_test_bit(x1, 0)) {
11393c4b2390SSalvatore Benedetto 		u64 carry = vli_add(x1, x1, curve_prime, ndigits);
11403c4b2390SSalvatore Benedetto 
11413c4b2390SSalvatore Benedetto 		vli_rshift1(x1, ndigits);
11423c4b2390SSalvatore Benedetto 		x1[ndigits - 1] |= carry << 63;
11433c4b2390SSalvatore Benedetto 	} else {
11443c4b2390SSalvatore Benedetto 		vli_rshift1(x1, ndigits);
11453c4b2390SSalvatore Benedetto 	}
11463c4b2390SSalvatore Benedetto 	/* t1 = 3/2*(x1^2 - z1^4) = B */
11473c4b2390SSalvatore Benedetto 
11483c4b2390SSalvatore Benedetto 	/* t3 = B^2 */
1149149ca161SSaulo Alessandre 	vli_mod_square_fast(z1, x1, curve);
11503c4b2390SSalvatore Benedetto 	/* t3 = B^2 - A */
11513c4b2390SSalvatore Benedetto 	vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
11523c4b2390SSalvatore Benedetto 	/* t3 = B^2 - 2A = x3 */
11533c4b2390SSalvatore Benedetto 	vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
11543c4b2390SSalvatore Benedetto 	/* t5 = A - x3 */
11553c4b2390SSalvatore Benedetto 	vli_mod_sub(t5, t5, z1, curve_prime, ndigits);
11563c4b2390SSalvatore Benedetto 	/* t1 = B * (A - x3) */
1157149ca161SSaulo Alessandre 	vli_mod_mult_fast(x1, x1, t5, curve);
11583c4b2390SSalvatore Benedetto 	/* t4 = B * (A - x3) - y1^4 = y3 */
11593c4b2390SSalvatore Benedetto 	vli_mod_sub(t4, x1, t4, curve_prime, ndigits);
11603c4b2390SSalvatore Benedetto 
11613c4b2390SSalvatore Benedetto 	vli_set(x1, z1, ndigits);
11623c4b2390SSalvatore Benedetto 	vli_set(z1, y1, ndigits);
11633c4b2390SSalvatore Benedetto 	vli_set(y1, t4, ndigits);
11643c4b2390SSalvatore Benedetto }
11653c4b2390SSalvatore Benedetto 
11663c4b2390SSalvatore Benedetto /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
apply_z(u64 * x1,u64 * y1,u64 * z,const struct ecc_curve * curve)1167149ca161SSaulo Alessandre static void apply_z(u64 *x1, u64 *y1, u64 *z, const struct ecc_curve *curve)
11683c4b2390SSalvatore Benedetto {
1169d5c3b178SKees Cook 	u64 t1[ECC_MAX_DIGITS];
11703c4b2390SSalvatore Benedetto 
1171149ca161SSaulo Alessandre 	vli_mod_square_fast(t1, z, curve);		/* z^2 */
1172149ca161SSaulo Alessandre 	vli_mod_mult_fast(x1, x1, t1, curve);	/* x1 * z^2 */
1173149ca161SSaulo Alessandre 	vli_mod_mult_fast(t1, t1, z, curve);	/* z^3 */
1174149ca161SSaulo Alessandre 	vli_mod_mult_fast(y1, y1, t1, curve);	/* y1 * z^3 */
11753c4b2390SSalvatore Benedetto }
11763c4b2390SSalvatore Benedetto 
11773c4b2390SSalvatore Benedetto /* P = (x1, y1) => 2P, (x2, y2) => P' */
xycz_initial_double(u64 * x1,u64 * y1,u64 * x2,u64 * y2,u64 * p_initial_z,const struct ecc_curve * curve)11783c4b2390SSalvatore Benedetto static void xycz_initial_double(u64 *x1, u64 *y1, u64 *x2, u64 *y2,
1179149ca161SSaulo Alessandre 				u64 *p_initial_z, const struct ecc_curve *curve)
11803c4b2390SSalvatore Benedetto {
1181d5c3b178SKees Cook 	u64 z[ECC_MAX_DIGITS];
1182149ca161SSaulo Alessandre 	const unsigned int ndigits = curve->g.ndigits;
11833c4b2390SSalvatore Benedetto 
11843c4b2390SSalvatore Benedetto 	vli_set(x2, x1, ndigits);
11853c4b2390SSalvatore Benedetto 	vli_set(y2, y1, ndigits);
11863c4b2390SSalvatore Benedetto 
11873c4b2390SSalvatore Benedetto 	vli_clear(z, ndigits);
11883c4b2390SSalvatore Benedetto 	z[0] = 1;
11893c4b2390SSalvatore Benedetto 
11903c4b2390SSalvatore Benedetto 	if (p_initial_z)
11913c4b2390SSalvatore Benedetto 		vli_set(z, p_initial_z, ndigits);
11923c4b2390SSalvatore Benedetto 
1193149ca161SSaulo Alessandre 	apply_z(x1, y1, z, curve);
11943c4b2390SSalvatore Benedetto 
1195149ca161SSaulo Alessandre 	ecc_point_double_jacobian(x1, y1, z, curve);
11963c4b2390SSalvatore Benedetto 
1197149ca161SSaulo Alessandre 	apply_z(x2, y2, z, curve);
11983c4b2390SSalvatore Benedetto }
11993c4b2390SSalvatore Benedetto 
12003c4b2390SSalvatore Benedetto /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
12013c4b2390SSalvatore Benedetto  * Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
12023c4b2390SSalvatore Benedetto  * or P => P', Q => P + Q
12033c4b2390SSalvatore Benedetto  */
xycz_add(u64 * x1,u64 * y1,u64 * x2,u64 * y2,const struct ecc_curve * curve)1204149ca161SSaulo Alessandre static void xycz_add(u64 *x1, u64 *y1, u64 *x2, u64 *y2,
1205149ca161SSaulo Alessandre 			const struct ecc_curve *curve)
12063c4b2390SSalvatore Benedetto {
12073c4b2390SSalvatore Benedetto 	/* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
1208d5c3b178SKees Cook 	u64 t5[ECC_MAX_DIGITS];
1209149ca161SSaulo Alessandre 	const u64 *curve_prime = curve->p;
1210149ca161SSaulo Alessandre 	const unsigned int ndigits = curve->g.ndigits;
12113c4b2390SSalvatore Benedetto 
12123c4b2390SSalvatore Benedetto 	/* t5 = x2 - x1 */
12133c4b2390SSalvatore Benedetto 	vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
12143c4b2390SSalvatore Benedetto 	/* t5 = (x2 - x1)^2 = A */
1215149ca161SSaulo Alessandre 	vli_mod_square_fast(t5, t5, curve);
12163c4b2390SSalvatore Benedetto 	/* t1 = x1*A = B */
1217149ca161SSaulo Alessandre 	vli_mod_mult_fast(x1, x1, t5, curve);
12183c4b2390SSalvatore Benedetto 	/* t3 = x2*A = C */
1219149ca161SSaulo Alessandre 	vli_mod_mult_fast(x2, x2, t5, curve);
12203c4b2390SSalvatore Benedetto 	/* t4 = y2 - y1 */
12213c4b2390SSalvatore Benedetto 	vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
12223c4b2390SSalvatore Benedetto 	/* t5 = (y2 - y1)^2 = D */
1223149ca161SSaulo Alessandre 	vli_mod_square_fast(t5, y2, curve);
12243c4b2390SSalvatore Benedetto 
12253c4b2390SSalvatore Benedetto 	/* t5 = D - B */
12263c4b2390SSalvatore Benedetto 	vli_mod_sub(t5, t5, x1, curve_prime, ndigits);
12273c4b2390SSalvatore Benedetto 	/* t5 = D - B - C = x3 */
12283c4b2390SSalvatore Benedetto 	vli_mod_sub(t5, t5, x2, curve_prime, ndigits);
12293c4b2390SSalvatore Benedetto 	/* t3 = C - B */
12303c4b2390SSalvatore Benedetto 	vli_mod_sub(x2, x2, x1, curve_prime, ndigits);
12313c4b2390SSalvatore Benedetto 	/* t2 = y1*(C - B) */
1232149ca161SSaulo Alessandre 	vli_mod_mult_fast(y1, y1, x2, curve);
12333c4b2390SSalvatore Benedetto 	/* t3 = B - x3 */
12343c4b2390SSalvatore Benedetto 	vli_mod_sub(x2, x1, t5, curve_prime, ndigits);
12353c4b2390SSalvatore Benedetto 	/* t4 = (y2 - y1)*(B - x3) */
1236149ca161SSaulo Alessandre 	vli_mod_mult_fast(y2, y2, x2, curve);
12373c4b2390SSalvatore Benedetto 	/* t4 = y3 */
12383c4b2390SSalvatore Benedetto 	vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
12393c4b2390SSalvatore Benedetto 
12403c4b2390SSalvatore Benedetto 	vli_set(x2, t5, ndigits);
12413c4b2390SSalvatore Benedetto }
12423c4b2390SSalvatore Benedetto 
12433c4b2390SSalvatore Benedetto /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
12443c4b2390SSalvatore Benedetto  * Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
12453c4b2390SSalvatore Benedetto  * or P => P - Q, Q => P + Q
12463c4b2390SSalvatore Benedetto  */
xycz_add_c(u64 * x1,u64 * y1,u64 * x2,u64 * y2,const struct ecc_curve * curve)1247149ca161SSaulo Alessandre static void xycz_add_c(u64 *x1, u64 *y1, u64 *x2, u64 *y2,
1248149ca161SSaulo Alessandre 			const struct ecc_curve *curve)
12493c4b2390SSalvatore Benedetto {
12503c4b2390SSalvatore Benedetto 	/* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
1251d5c3b178SKees Cook 	u64 t5[ECC_MAX_DIGITS];
1252d5c3b178SKees Cook 	u64 t6[ECC_MAX_DIGITS];
1253d5c3b178SKees Cook 	u64 t7[ECC_MAX_DIGITS];
1254149ca161SSaulo Alessandre 	const u64 *curve_prime = curve->p;
1255149ca161SSaulo Alessandre 	const unsigned int ndigits = curve->g.ndigits;
12563c4b2390SSalvatore Benedetto 
12573c4b2390SSalvatore Benedetto 	/* t5 = x2 - x1 */
12583c4b2390SSalvatore Benedetto 	vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
12593c4b2390SSalvatore Benedetto 	/* t5 = (x2 - x1)^2 = A */
1260149ca161SSaulo Alessandre 	vli_mod_square_fast(t5, t5, curve);
12613c4b2390SSalvatore Benedetto 	/* t1 = x1*A = B */
1262149ca161SSaulo Alessandre 	vli_mod_mult_fast(x1, x1, t5, curve);
12633c4b2390SSalvatore Benedetto 	/* t3 = x2*A = C */
1264149ca161SSaulo Alessandre 	vli_mod_mult_fast(x2, x2, t5, curve);
12653c4b2390SSalvatore Benedetto 	/* t4 = y2 + y1 */
12663c4b2390SSalvatore Benedetto 	vli_mod_add(t5, y2, y1, curve_prime, ndigits);
12673c4b2390SSalvatore Benedetto 	/* t4 = y2 - y1 */
12683c4b2390SSalvatore Benedetto 	vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
12693c4b2390SSalvatore Benedetto 
12703c4b2390SSalvatore Benedetto 	/* t6 = C - B */
12713c4b2390SSalvatore Benedetto 	vli_mod_sub(t6, x2, x1, curve_prime, ndigits);
12723c4b2390SSalvatore Benedetto 	/* t2 = y1 * (C - B) */
1273149ca161SSaulo Alessandre 	vli_mod_mult_fast(y1, y1, t6, curve);
12743c4b2390SSalvatore Benedetto 	/* t6 = B + C */
12753c4b2390SSalvatore Benedetto 	vli_mod_add(t6, x1, x2, curve_prime, ndigits);
12763c4b2390SSalvatore Benedetto 	/* t3 = (y2 - y1)^2 */
1277149ca161SSaulo Alessandre 	vli_mod_square_fast(x2, y2, curve);
12783c4b2390SSalvatore Benedetto 	/* t3 = x3 */
12793c4b2390SSalvatore Benedetto 	vli_mod_sub(x2, x2, t6, curve_prime, ndigits);
12803c4b2390SSalvatore Benedetto 
12813c4b2390SSalvatore Benedetto 	/* t7 = B - x3 */
12823c4b2390SSalvatore Benedetto 	vli_mod_sub(t7, x1, x2, curve_prime, ndigits);
12833c4b2390SSalvatore Benedetto 	/* t4 = (y2 - y1)*(B - x3) */
1284149ca161SSaulo Alessandre 	vli_mod_mult_fast(y2, y2, t7, curve);
12853c4b2390SSalvatore Benedetto 	/* t4 = y3 */
12863c4b2390SSalvatore Benedetto 	vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
12873c4b2390SSalvatore Benedetto 
12883c4b2390SSalvatore Benedetto 	/* t7 = (y2 + y1)^2 = F */
1289149ca161SSaulo Alessandre 	vli_mod_square_fast(t7, t5, curve);
12903c4b2390SSalvatore Benedetto 	/* t7 = x3' */
12913c4b2390SSalvatore Benedetto 	vli_mod_sub(t7, t7, t6, curve_prime, ndigits);
12923c4b2390SSalvatore Benedetto 	/* t6 = x3' - B */
12933c4b2390SSalvatore Benedetto 	vli_mod_sub(t6, t7, x1, curve_prime, ndigits);
12943c4b2390SSalvatore Benedetto 	/* t6 = (y2 + y1)*(x3' - B) */
1295149ca161SSaulo Alessandre 	vli_mod_mult_fast(t6, t6, t5, curve);
12963c4b2390SSalvatore Benedetto 	/* t2 = y3' */
12973c4b2390SSalvatore Benedetto 	vli_mod_sub(y1, t6, y1, curve_prime, ndigits);
12983c4b2390SSalvatore Benedetto 
12993c4b2390SSalvatore Benedetto 	vli_set(x1, t7, ndigits);
13003c4b2390SSalvatore Benedetto }
13013c4b2390SSalvatore Benedetto 
ecc_point_mult(struct ecc_point * result,const struct ecc_point * point,const u64 * scalar,u64 * initial_z,const struct ecc_curve * curve,unsigned int ndigits)13023c4b2390SSalvatore Benedetto static void ecc_point_mult(struct ecc_point *result,
13033c4b2390SSalvatore Benedetto 			   const struct ecc_point *point, const u64 *scalar,
13043da2c1dfSVitaly Chikunov 			   u64 *initial_z, const struct ecc_curve *curve,
13053c4b2390SSalvatore Benedetto 			   unsigned int ndigits)
13063c4b2390SSalvatore Benedetto {
13073c4b2390SSalvatore Benedetto 	/* R0 and R1 */
1308d5c3b178SKees Cook 	u64 rx[2][ECC_MAX_DIGITS];
1309d5c3b178SKees Cook 	u64 ry[2][ECC_MAX_DIGITS];
1310d5c3b178SKees Cook 	u64 z[ECC_MAX_DIGITS];
13113da2c1dfSVitaly Chikunov 	u64 sk[2][ECC_MAX_DIGITS];
13123da2c1dfSVitaly Chikunov 	u64 *curve_prime = curve->p;
13133c4b2390SSalvatore Benedetto 	int i, nb;
13143da2c1dfSVitaly Chikunov 	int num_bits;
13153da2c1dfSVitaly Chikunov 	int carry;
13163da2c1dfSVitaly Chikunov 
13173da2c1dfSVitaly Chikunov 	carry = vli_add(sk[0], scalar, curve->n, ndigits);
13183da2c1dfSVitaly Chikunov 	vli_add(sk[1], sk[0], curve->n, ndigits);
13193da2c1dfSVitaly Chikunov 	scalar = sk[!carry];
13203da2c1dfSVitaly Chikunov 	num_bits = sizeof(u64) * ndigits * 8 + 1;
13213c4b2390SSalvatore Benedetto 
13223c4b2390SSalvatore Benedetto 	vli_set(rx[1], point->x, ndigits);
13233c4b2390SSalvatore Benedetto 	vli_set(ry[1], point->y, ndigits);
13243c4b2390SSalvatore Benedetto 
1325149ca161SSaulo Alessandre 	xycz_initial_double(rx[1], ry[1], rx[0], ry[0], initial_z, curve);
13263c4b2390SSalvatore Benedetto 
13273c4b2390SSalvatore Benedetto 	for (i = num_bits - 2; i > 0; i--) {
13283c4b2390SSalvatore Benedetto 		nb = !vli_test_bit(scalar, i);
1329149ca161SSaulo Alessandre 		xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve);
1330149ca161SSaulo Alessandre 		xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve);
13313c4b2390SSalvatore Benedetto 	}
13323c4b2390SSalvatore Benedetto 
13333c4b2390SSalvatore Benedetto 	nb = !vli_test_bit(scalar, 0);
1334149ca161SSaulo Alessandre 	xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve);
13353c4b2390SSalvatore Benedetto 
13363c4b2390SSalvatore Benedetto 	/* Find final 1/Z value. */
13373c4b2390SSalvatore Benedetto 	/* X1 - X0 */
13383c4b2390SSalvatore Benedetto 	vli_mod_sub(z, rx[1], rx[0], curve_prime, ndigits);
13393c4b2390SSalvatore Benedetto 	/* Yb * (X1 - X0) */
1340149ca161SSaulo Alessandre 	vli_mod_mult_fast(z, z, ry[1 - nb], curve);
13413c4b2390SSalvatore Benedetto 	/* xP * Yb * (X1 - X0) */
1342149ca161SSaulo Alessandre 	vli_mod_mult_fast(z, z, point->x, curve);
13433c4b2390SSalvatore Benedetto 
13443c4b2390SSalvatore Benedetto 	/* 1 / (xP * Yb * (X1 - X0)) */
13453c4b2390SSalvatore Benedetto 	vli_mod_inv(z, z, curve_prime, point->ndigits);
13463c4b2390SSalvatore Benedetto 
13473c4b2390SSalvatore Benedetto 	/* yP / (xP * Yb * (X1 - X0)) */
1348149ca161SSaulo Alessandre 	vli_mod_mult_fast(z, z, point->y, curve);
13493c4b2390SSalvatore Benedetto 	/* Xb * yP / (xP * Yb * (X1 - X0)) */
1350149ca161SSaulo Alessandre 	vli_mod_mult_fast(z, z, rx[1 - nb], curve);
13513c4b2390SSalvatore Benedetto 	/* End 1/Z calculation */
13523c4b2390SSalvatore Benedetto 
1353149ca161SSaulo Alessandre 	xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve);
13543c4b2390SSalvatore Benedetto 
1355149ca161SSaulo Alessandre 	apply_z(rx[0], ry[0], z, curve);
13563c4b2390SSalvatore Benedetto 
13573c4b2390SSalvatore Benedetto 	vli_set(result->x, rx[0], ndigits);
13583c4b2390SSalvatore Benedetto 	vli_set(result->y, ry[0], ndigits);
13593c4b2390SSalvatore Benedetto }
13603c4b2390SSalvatore Benedetto 
13610d7a7864SVitaly Chikunov /* Computes R = P + Q mod p */
ecc_point_add(const struct ecc_point * result,const struct ecc_point * p,const struct ecc_point * q,const struct ecc_curve * curve)13620d7a7864SVitaly Chikunov static void ecc_point_add(const struct ecc_point *result,
13630d7a7864SVitaly Chikunov 		   const struct ecc_point *p, const struct ecc_point *q,
13640d7a7864SVitaly Chikunov 		   const struct ecc_curve *curve)
13650d7a7864SVitaly Chikunov {
13660d7a7864SVitaly Chikunov 	u64 z[ECC_MAX_DIGITS];
13670d7a7864SVitaly Chikunov 	u64 px[ECC_MAX_DIGITS];
13680d7a7864SVitaly Chikunov 	u64 py[ECC_MAX_DIGITS];
13690d7a7864SVitaly Chikunov 	unsigned int ndigits = curve->g.ndigits;
13700d7a7864SVitaly Chikunov 
13710d7a7864SVitaly Chikunov 	vli_set(result->x, q->x, ndigits);
13720d7a7864SVitaly Chikunov 	vli_set(result->y, q->y, ndigits);
13730d7a7864SVitaly Chikunov 	vli_mod_sub(z, result->x, p->x, curve->p, ndigits);
13740d7a7864SVitaly Chikunov 	vli_set(px, p->x, ndigits);
13750d7a7864SVitaly Chikunov 	vli_set(py, p->y, ndigits);
1376149ca161SSaulo Alessandre 	xycz_add(px, py, result->x, result->y, curve);
13770d7a7864SVitaly Chikunov 	vli_mod_inv(z, z, curve->p, ndigits);
1378149ca161SSaulo Alessandre 	apply_z(result->x, result->y, z, curve);
13790d7a7864SVitaly Chikunov }
13800d7a7864SVitaly Chikunov 
13810d7a7864SVitaly Chikunov /* Computes R = u1P + u2Q mod p using Shamir's trick.
13820d7a7864SVitaly Chikunov  * Based on: Kenneth MacKay's micro-ecc (2014).
13830d7a7864SVitaly Chikunov  */
ecc_point_mult_shamir(const struct ecc_point * result,const u64 * u1,const struct ecc_point * p,const u64 * u2,const struct ecc_point * q,const struct ecc_curve * curve)13840d7a7864SVitaly Chikunov void ecc_point_mult_shamir(const struct ecc_point *result,
13850d7a7864SVitaly Chikunov 			   const u64 *u1, const struct ecc_point *p,
13860d7a7864SVitaly Chikunov 			   const u64 *u2, const struct ecc_point *q,
13870d7a7864SVitaly Chikunov 			   const struct ecc_curve *curve)
13880d7a7864SVitaly Chikunov {
13890d7a7864SVitaly Chikunov 	u64 z[ECC_MAX_DIGITS];
13900d7a7864SVitaly Chikunov 	u64 sump[2][ECC_MAX_DIGITS];
13910d7a7864SVitaly Chikunov 	u64 *rx = result->x;
13920d7a7864SVitaly Chikunov 	u64 *ry = result->y;
13930d7a7864SVitaly Chikunov 	unsigned int ndigits = curve->g.ndigits;
13940d7a7864SVitaly Chikunov 	unsigned int num_bits;
13950d7a7864SVitaly Chikunov 	struct ecc_point sum = ECC_POINT_INIT(sump[0], sump[1], ndigits);
13960d7a7864SVitaly Chikunov 	const struct ecc_point *points[4];
13970d7a7864SVitaly Chikunov 	const struct ecc_point *point;
13980d7a7864SVitaly Chikunov 	unsigned int idx;
13990d7a7864SVitaly Chikunov 	int i;
14000d7a7864SVitaly Chikunov 
14010d7a7864SVitaly Chikunov 	ecc_point_add(&sum, p, q, curve);
14020d7a7864SVitaly Chikunov 	points[0] = NULL;
14030d7a7864SVitaly Chikunov 	points[1] = p;
14040d7a7864SVitaly Chikunov 	points[2] = q;
14050d7a7864SVitaly Chikunov 	points[3] = &sum;
14060d7a7864SVitaly Chikunov 
1407149ca161SSaulo Alessandre 	num_bits = max(vli_num_bits(u1, ndigits), vli_num_bits(u2, ndigits));
14080d7a7864SVitaly Chikunov 	i = num_bits - 1;
14095072b1c2SHerbert Xu 	idx = !!vli_test_bit(u1, i);
14105072b1c2SHerbert Xu 	idx |= (!!vli_test_bit(u2, i)) << 1;
14110d7a7864SVitaly Chikunov 	point = points[idx];
14120d7a7864SVitaly Chikunov 
14130d7a7864SVitaly Chikunov 	vli_set(rx, point->x, ndigits);
14140d7a7864SVitaly Chikunov 	vli_set(ry, point->y, ndigits);
14150d7a7864SVitaly Chikunov 	vli_clear(z + 1, ndigits - 1);
14160d7a7864SVitaly Chikunov 	z[0] = 1;
14170d7a7864SVitaly Chikunov 
14180d7a7864SVitaly Chikunov 	for (--i; i >= 0; i--) {
1419149ca161SSaulo Alessandre 		ecc_point_double_jacobian(rx, ry, z, curve);
14205072b1c2SHerbert Xu 		idx = !!vli_test_bit(u1, i);
14215072b1c2SHerbert Xu 		idx |= (!!vli_test_bit(u2, i)) << 1;
14220d7a7864SVitaly Chikunov 		point = points[idx];
14230d7a7864SVitaly Chikunov 		if (point) {
14240d7a7864SVitaly Chikunov 			u64 tx[ECC_MAX_DIGITS];
14250d7a7864SVitaly Chikunov 			u64 ty[ECC_MAX_DIGITS];
14260d7a7864SVitaly Chikunov 			u64 tz[ECC_MAX_DIGITS];
14270d7a7864SVitaly Chikunov 
14280d7a7864SVitaly Chikunov 			vli_set(tx, point->x, ndigits);
14290d7a7864SVitaly Chikunov 			vli_set(ty, point->y, ndigits);
1430149ca161SSaulo Alessandre 			apply_z(tx, ty, z, curve);
14310d7a7864SVitaly Chikunov 			vli_mod_sub(tz, rx, tx, curve->p, ndigits);
1432149ca161SSaulo Alessandre 			xycz_add(tx, ty, rx, ry, curve);
1433149ca161SSaulo Alessandre 			vli_mod_mult_fast(z, z, tz, curve);
14340d7a7864SVitaly Chikunov 		}
14350d7a7864SVitaly Chikunov 	}
14360d7a7864SVitaly Chikunov 	vli_mod_inv(z, z, curve->p, ndigits);
1437149ca161SSaulo Alessandre 	apply_z(rx, ry, z, curve);
14380d7a7864SVitaly Chikunov }
14390d7a7864SVitaly Chikunov EXPORT_SYMBOL(ecc_point_mult_shamir);
14400d7a7864SVitaly Chikunov 
__ecc_is_key_valid(const struct ecc_curve * curve,const u64 * private_key,unsigned int ndigits)14412eb4942bSVitaly Chikunov static int __ecc_is_key_valid(const struct ecc_curve *curve,
14422eb4942bSVitaly Chikunov 			      const u64 *private_key, unsigned int ndigits)
14432eb4942bSVitaly Chikunov {
14442eb4942bSVitaly Chikunov 	u64 one[ECC_MAX_DIGITS] = { 1, };
14452eb4942bSVitaly Chikunov 	u64 res[ECC_MAX_DIGITS];
14462eb4942bSVitaly Chikunov 
14472eb4942bSVitaly Chikunov 	if (!private_key)
14482eb4942bSVitaly Chikunov 		return -EINVAL;
14492eb4942bSVitaly Chikunov 
14502eb4942bSVitaly Chikunov 	if (curve->g.ndigits != ndigits)
14512eb4942bSVitaly Chikunov 		return -EINVAL;
14522eb4942bSVitaly Chikunov 
14532eb4942bSVitaly Chikunov 	/* Make sure the private key is in the range [2, n-3]. */
14542eb4942bSVitaly Chikunov 	if (vli_cmp(one, private_key, ndigits) != -1)
14552eb4942bSVitaly Chikunov 		return -EINVAL;
14562eb4942bSVitaly Chikunov 	vli_sub(res, curve->n, one, ndigits);
14572eb4942bSVitaly Chikunov 	vli_sub(res, res, one, ndigits);
14582eb4942bSVitaly Chikunov 	if (vli_cmp(res, private_key, ndigits) != 1)
14592eb4942bSVitaly Chikunov 		return -EINVAL;
14602eb4942bSVitaly Chikunov 
14612eb4942bSVitaly Chikunov 	return 0;
14622eb4942bSVitaly Chikunov }
14632eb4942bSVitaly Chikunov 
ecc_is_key_valid(unsigned int curve_id,unsigned int ndigits,const u64 * private_key,unsigned int private_key_len)14643c4b2390SSalvatore Benedetto int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
1465ad269597STudor-Dan Ambarus 		     const u64 *private_key, unsigned int private_key_len)
14663c4b2390SSalvatore Benedetto {
14673c4b2390SSalvatore Benedetto 	int nbytes;
14683c4b2390SSalvatore Benedetto 	const struct ecc_curve *curve = ecc_get_curve(curve_id);
14693c4b2390SSalvatore Benedetto 
14703c4b2390SSalvatore Benedetto 	nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
14713c4b2390SSalvatore Benedetto 
14723c4b2390SSalvatore Benedetto 	if (private_key_len != nbytes)
14733c4b2390SSalvatore Benedetto 		return -EINVAL;
14743c4b2390SSalvatore Benedetto 
14752eb4942bSVitaly Chikunov 	return __ecc_is_key_valid(curve, private_key, ndigits);
14763c4b2390SSalvatore Benedetto }
14774a2289daSVitaly Chikunov EXPORT_SYMBOL(ecc_is_key_valid);
14783c4b2390SSalvatore Benedetto 
14796755fd26STudor-Dan Ambarus /*
14806755fd26STudor-Dan Ambarus  * ECC private keys are generated using the method of extra random bits,
14816755fd26STudor-Dan Ambarus  * equivalent to that described in FIPS 186-4, Appendix B.4.1.
14826755fd26STudor-Dan Ambarus  *
14836755fd26STudor-Dan Ambarus  * d = (c mod(n–1)) + 1    where c is a string of random bits, 64 bits longer
14846755fd26STudor-Dan Ambarus  *                         than requested
14856755fd26STudor-Dan Ambarus  * 0 <= c mod(n-1) <= n-2  and implies that
14866755fd26STudor-Dan Ambarus  * 1 <= d <= n-1
14876755fd26STudor-Dan Ambarus  *
14886755fd26STudor-Dan Ambarus  * This method generates a private key uniformly distributed in the range
14896755fd26STudor-Dan Ambarus  * [1, n-1].
14906755fd26STudor-Dan Ambarus  */
ecc_gen_privkey(unsigned int curve_id,unsigned int ndigits,u64 * privkey)14916755fd26STudor-Dan Ambarus int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey)
14926755fd26STudor-Dan Ambarus {
14936755fd26STudor-Dan Ambarus 	const struct ecc_curve *curve = ecc_get_curve(curve_id);
1494d5c3b178SKees Cook 	u64 priv[ECC_MAX_DIGITS];
14956755fd26STudor-Dan Ambarus 	unsigned int nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
14966755fd26STudor-Dan Ambarus 	unsigned int nbits = vli_num_bits(curve->n, ndigits);
14976755fd26STudor-Dan Ambarus 	int err;
14986755fd26STudor-Dan Ambarus 
14996755fd26STudor-Dan Ambarus 	/* Check that N is included in Table 1 of FIPS 186-4, section 6.1.1 */
1500d5c3b178SKees Cook 	if (nbits < 160 || ndigits > ARRAY_SIZE(priv))
15016755fd26STudor-Dan Ambarus 		return -EINVAL;
15026755fd26STudor-Dan Ambarus 
15036755fd26STudor-Dan Ambarus 	/*
15046755fd26STudor-Dan Ambarus 	 * FIPS 186-4 recommends that the private key should be obtained from a
15056755fd26STudor-Dan Ambarus 	 * RBG with a security strength equal to or greater than the security
15066755fd26STudor-Dan Ambarus 	 * strength associated with N.
15076755fd26STudor-Dan Ambarus 	 *
15086755fd26STudor-Dan Ambarus 	 * The maximum security strength identified by NIST SP800-57pt1r4 for
15096755fd26STudor-Dan Ambarus 	 * ECC is 256 (N >= 512).
15106755fd26STudor-Dan Ambarus 	 *
15116755fd26STudor-Dan Ambarus 	 * This condition is met by the default RNG because it selects a favored
15126755fd26STudor-Dan Ambarus 	 * DRBG with a security strength of 256.
15136755fd26STudor-Dan Ambarus 	 */
15146755fd26STudor-Dan Ambarus 	if (crypto_get_default_rng())
15154c0e22c9SPierre 		return -EFAULT;
15166755fd26STudor-Dan Ambarus 
15176755fd26STudor-Dan Ambarus 	err = crypto_rng_get_bytes(crypto_default_rng, (u8 *)priv, nbytes);
15186755fd26STudor-Dan Ambarus 	crypto_put_default_rng();
15196755fd26STudor-Dan Ambarus 	if (err)
15206755fd26STudor-Dan Ambarus 		return err;
15216755fd26STudor-Dan Ambarus 
15222eb4942bSVitaly Chikunov 	/* Make sure the private key is in the valid range. */
15232eb4942bSVitaly Chikunov 	if (__ecc_is_key_valid(curve, priv, ndigits))
15246755fd26STudor-Dan Ambarus 		return -EINVAL;
15256755fd26STudor-Dan Ambarus 
15266755fd26STudor-Dan Ambarus 	ecc_swap_digits(priv, privkey, ndigits);
15276755fd26STudor-Dan Ambarus 
15286755fd26STudor-Dan Ambarus 	return 0;
15296755fd26STudor-Dan Ambarus }
15304a2289daSVitaly Chikunov EXPORT_SYMBOL(ecc_gen_privkey);
15316755fd26STudor-Dan Ambarus 
ecc_make_pub_key(unsigned int curve_id,unsigned int ndigits,const u64 * private_key,u64 * public_key)15327380c56dSTudor-Dan Ambarus int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
1533ad269597STudor-Dan Ambarus 		     const u64 *private_key, u64 *public_key)
15343c4b2390SSalvatore Benedetto {
15353c4b2390SSalvatore Benedetto 	int ret = 0;
15363c4b2390SSalvatore Benedetto 	struct ecc_point *pk;
1537d5c3b178SKees Cook 	u64 priv[ECC_MAX_DIGITS];
15383c4b2390SSalvatore Benedetto 	const struct ecc_curve *curve = ecc_get_curve(curve_id);
15393c4b2390SSalvatore Benedetto 
1540d5c3b178SKees Cook 	if (!private_key || !curve || ndigits > ARRAY_SIZE(priv)) {
15413c4b2390SSalvatore Benedetto 		ret = -EINVAL;
15423c4b2390SSalvatore Benedetto 		goto out;
15433c4b2390SSalvatore Benedetto 	}
15443c4b2390SSalvatore Benedetto 
1545ad269597STudor-Dan Ambarus 	ecc_swap_digits(private_key, priv, ndigits);
15463c4b2390SSalvatore Benedetto 
15473c4b2390SSalvatore Benedetto 	pk = ecc_alloc_point(ndigits);
15483c4b2390SSalvatore Benedetto 	if (!pk) {
15493c4b2390SSalvatore Benedetto 		ret = -ENOMEM;
15503c4b2390SSalvatore Benedetto 		goto out;
15513c4b2390SSalvatore Benedetto 	}
15523c4b2390SSalvatore Benedetto 
15533da2c1dfSVitaly Chikunov 	ecc_point_mult(pk, &curve->g, priv, NULL, curve, ndigits);
15546914dd53SStephan Müller 
15556914dd53SStephan Müller 	/* SP800-56A rev 3 5.6.2.1.3 key check */
15566914dd53SStephan Müller 	if (ecc_is_pubkey_valid_full(curve, pk)) {
15573c4b2390SSalvatore Benedetto 		ret = -EAGAIN;
15583c4b2390SSalvatore Benedetto 		goto err_free_point;
15593c4b2390SSalvatore Benedetto 	}
15603c4b2390SSalvatore Benedetto 
1561ad269597STudor-Dan Ambarus 	ecc_swap_digits(pk->x, public_key, ndigits);
1562ad269597STudor-Dan Ambarus 	ecc_swap_digits(pk->y, &public_key[ndigits], ndigits);
15633c4b2390SSalvatore Benedetto 
15643c4b2390SSalvatore Benedetto err_free_point:
15653c4b2390SSalvatore Benedetto 	ecc_free_point(pk);
15663c4b2390SSalvatore Benedetto out:
15673c4b2390SSalvatore Benedetto 	return ret;
15683c4b2390SSalvatore Benedetto }
15694a2289daSVitaly Chikunov EXPORT_SYMBOL(ecc_make_pub_key);
15703c4b2390SSalvatore Benedetto 
1571ea169a30SStephan Mueller /* SP800-56A section 5.6.2.3.4 partial verification: ephemeral keys only */
ecc_is_pubkey_valid_partial(const struct ecc_curve * curve,struct ecc_point * pk)15724a2289daSVitaly Chikunov int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,
1573ea169a30SStephan Mueller 				struct ecc_point *pk)
1574ea169a30SStephan Mueller {
1575ea169a30SStephan Mueller 	u64 yy[ECC_MAX_DIGITS], xxx[ECC_MAX_DIGITS], w[ECC_MAX_DIGITS];
1576ea169a30SStephan Mueller 
15770d7a7864SVitaly Chikunov 	if (WARN_ON(pk->ndigits != curve->g.ndigits))
15780d7a7864SVitaly Chikunov 		return -EINVAL;
15790d7a7864SVitaly Chikunov 
1580ea169a30SStephan Mueller 	/* Check 1: Verify key is not the zero point. */
1581ea169a30SStephan Mueller 	if (ecc_point_is_zero(pk))
1582ea169a30SStephan Mueller 		return -EINVAL;
1583ea169a30SStephan Mueller 
1584ea169a30SStephan Mueller 	/* Check 2: Verify key is in the range [1, p-1]. */
1585ea169a30SStephan Mueller 	if (vli_cmp(curve->p, pk->x, pk->ndigits) != 1)
1586ea169a30SStephan Mueller 		return -EINVAL;
1587ea169a30SStephan Mueller 	if (vli_cmp(curve->p, pk->y, pk->ndigits) != 1)
1588ea169a30SStephan Mueller 		return -EINVAL;
1589ea169a30SStephan Mueller 
1590ea169a30SStephan Mueller 	/* Check 3: Verify that y^2 == (x^3 + a·x + b) mod p */
1591149ca161SSaulo Alessandre 	vli_mod_square_fast(yy, pk->y, curve); /* y^2 */
1592149ca161SSaulo Alessandre 	vli_mod_square_fast(xxx, pk->x, curve); /* x^2 */
1593149ca161SSaulo Alessandre 	vli_mod_mult_fast(xxx, xxx, pk->x, curve); /* x^3 */
1594149ca161SSaulo Alessandre 	vli_mod_mult_fast(w, curve->a, pk->x, curve); /* a·x */
1595ea169a30SStephan Mueller 	vli_mod_add(w, w, curve->b, curve->p, pk->ndigits); /* a·x + b */
1596ea169a30SStephan Mueller 	vli_mod_add(w, w, xxx, curve->p, pk->ndigits); /* x^3 + a·x + b */
1597ea169a30SStephan Mueller 	if (vli_cmp(yy, w, pk->ndigits) != 0) /* Equation */
1598ea169a30SStephan Mueller 		return -EINVAL;
1599ea169a30SStephan Mueller 
1600ea169a30SStephan Mueller 	return 0;
1601ea169a30SStephan Mueller }
16024a2289daSVitaly Chikunov EXPORT_SYMBOL(ecc_is_pubkey_valid_partial);
1603ea169a30SStephan Mueller 
16046914dd53SStephan Müller /* SP800-56A section 5.6.2.3.3 full verification */
ecc_is_pubkey_valid_full(const struct ecc_curve * curve,struct ecc_point * pk)16056914dd53SStephan Müller int ecc_is_pubkey_valid_full(const struct ecc_curve *curve,
16066914dd53SStephan Müller 			     struct ecc_point *pk)
16076914dd53SStephan Müller {
16086914dd53SStephan Müller 	struct ecc_point *nQ;
16096914dd53SStephan Müller 
16106914dd53SStephan Müller 	/* Checks 1 through 3 */
16116914dd53SStephan Müller 	int ret = ecc_is_pubkey_valid_partial(curve, pk);
16126914dd53SStephan Müller 
16136914dd53SStephan Müller 	if (ret)
16146914dd53SStephan Müller 		return ret;
16156914dd53SStephan Müller 
16166914dd53SStephan Müller 	/* Check 4: Verify that nQ is the zero point. */
16176914dd53SStephan Müller 	nQ = ecc_alloc_point(pk->ndigits);
16186914dd53SStephan Müller 	if (!nQ)
16196914dd53SStephan Müller 		return -ENOMEM;
16206914dd53SStephan Müller 
16216914dd53SStephan Müller 	ecc_point_mult(nQ, pk, curve->n, NULL, curve, pk->ndigits);
16226914dd53SStephan Müller 	if (!ecc_point_is_zero(nQ))
16236914dd53SStephan Müller 		ret = -EINVAL;
16246914dd53SStephan Müller 
16256914dd53SStephan Müller 	ecc_free_point(nQ);
16266914dd53SStephan Müller 
16276914dd53SStephan Müller 	return ret;
16286914dd53SStephan Müller }
16296914dd53SStephan Müller EXPORT_SYMBOL(ecc_is_pubkey_valid_full);
16306914dd53SStephan Müller 
crypto_ecdh_shared_secret(unsigned int curve_id,unsigned int ndigits,const u64 * private_key,const u64 * public_key,u64 * secret)16318f44df15SStephen Rothwell int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
1632ad269597STudor-Dan Ambarus 			      const u64 *private_key, const u64 *public_key,
1633ad269597STudor-Dan Ambarus 			      u64 *secret)
16343c4b2390SSalvatore Benedetto {
16353c4b2390SSalvatore Benedetto 	int ret = 0;
16363c4b2390SSalvatore Benedetto 	struct ecc_point *product, *pk;
1637d5c3b178SKees Cook 	u64 priv[ECC_MAX_DIGITS];
1638d5c3b178SKees Cook 	u64 rand_z[ECC_MAX_DIGITS];
1639d5c3b178SKees Cook 	unsigned int nbytes;
16403c4b2390SSalvatore Benedetto 	const struct ecc_curve *curve = ecc_get_curve(curve_id);
16413c4b2390SSalvatore Benedetto 
1642d5c3b178SKees Cook 	if (!private_key || !public_key || !curve ||
1643d5c3b178SKees Cook 	    ndigits > ARRAY_SIZE(priv) || ndigits > ARRAY_SIZE(rand_z)) {
16443c4b2390SSalvatore Benedetto 		ret = -EINVAL;
16453c4b2390SSalvatore Benedetto 		goto out;
16463c4b2390SSalvatore Benedetto 	}
16473c4b2390SSalvatore Benedetto 
1648d5c3b178SKees Cook 	nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
16493c4b2390SSalvatore Benedetto 
1650d5c3b178SKees Cook 	get_random_bytes(rand_z, nbytes);
16513c4b2390SSalvatore Benedetto 
16523c4b2390SSalvatore Benedetto 	pk = ecc_alloc_point(ndigits);
16533c4b2390SSalvatore Benedetto 	if (!pk) {
16543c4b2390SSalvatore Benedetto 		ret = -ENOMEM;
1655d5c3b178SKees Cook 		goto out;
16563c4b2390SSalvatore Benedetto 	}
16573c4b2390SSalvatore Benedetto 
1658ea169a30SStephan Mueller 	ecc_swap_digits(public_key, pk->x, ndigits);
1659ea169a30SStephan Mueller 	ecc_swap_digits(&public_key[ndigits], pk->y, ndigits);
1660ea169a30SStephan Mueller 	ret = ecc_is_pubkey_valid_partial(curve, pk);
1661ea169a30SStephan Mueller 	if (ret)
1662ea169a30SStephan Mueller 		goto err_alloc_product;
1663ea169a30SStephan Mueller 
1664ea169a30SStephan Mueller 	ecc_swap_digits(private_key, priv, ndigits);
1665ea169a30SStephan Mueller 
16663c4b2390SSalvatore Benedetto 	product = ecc_alloc_point(ndigits);
16673c4b2390SSalvatore Benedetto 	if (!product) {
16683c4b2390SSalvatore Benedetto 		ret = -ENOMEM;
16693c4b2390SSalvatore Benedetto 		goto err_alloc_product;
16703c4b2390SSalvatore Benedetto 	}
16713c4b2390SSalvatore Benedetto 
16723da2c1dfSVitaly Chikunov 	ecc_point_mult(product, pk, priv, rand_z, curve, ndigits);
16733c4b2390SSalvatore Benedetto 
1674e7d2b41eSStephan Müller 	if (ecc_point_is_zero(product)) {
1675e7d2b41eSStephan Müller 		ret = -EFAULT;
1676e7d2b41eSStephan Müller 		goto err_validity;
1677e7d2b41eSStephan Müller 	}
1678e7d2b41eSStephan Müller 
1679ad269597STudor-Dan Ambarus 	ecc_swap_digits(product->x, secret, ndigits);
16803c4b2390SSalvatore Benedetto 
1681e7d2b41eSStephan Müller err_validity:
1682e7d2b41eSStephan Müller 	memzero_explicit(priv, sizeof(priv));
1683e7d2b41eSStephan Müller 	memzero_explicit(rand_z, sizeof(rand_z));
16843c4b2390SSalvatore Benedetto 	ecc_free_point(product);
16853c4b2390SSalvatore Benedetto err_alloc_product:
16863c4b2390SSalvatore Benedetto 	ecc_free_point(pk);
16873c4b2390SSalvatore Benedetto out:
16883c4b2390SSalvatore Benedetto 	return ret;
16893c4b2390SSalvatore Benedetto }
16904a2289daSVitaly Chikunov EXPORT_SYMBOL(crypto_ecdh_shared_secret);
16914a2289daSVitaly Chikunov 
16924a2289daSVitaly Chikunov MODULE_LICENSE("Dual BSD/GPL");
1693