Linux Audio

Check our new training course

Loading...
v5.4
   1/*
   2 * Copyright (c) 2013, 2014 Kenneth MacKay. All rights reserved.
   3 * Copyright (c) 2019 Vitaly Chikunov <vt@altlinux.org>
   4 *
   5 * Redistribution and use in source and binary forms, with or without
   6 * modification, are permitted provided that the following conditions are
   7 * met:
   8 *  * Redistributions of source code must retain the above copyright
   9 *   notice, this list of conditions and the following disclaimer.
  10 *  * Redistributions in binary form must reproduce the above copyright
  11 *    notice, this list of conditions and the following disclaimer in the
  12 *    documentation and/or other materials provided with the distribution.
  13 *
  14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25 */
  26
 
  27#include <linux/module.h>
  28#include <linux/random.h>
  29#include <linux/slab.h>
  30#include <linux/swab.h>
  31#include <linux/fips.h>
  32#include <crypto/ecdh.h>
  33#include <crypto/rng.h>
  34#include <asm/unaligned.h>
  35#include <linux/ratelimit.h>
  36
  37#include "ecc.h"
  38#include "ecc_curve_defs.h"
  39
  40typedef struct {
  41	u64 m_low;
  42	u64 m_high;
  43} uint128_t;
  44
  45static inline const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
 
 
 
 
 
 
 
  46{
  47	switch (curve_id) {
  48	/* In FIPS mode only allow P256 and higher */
  49	case ECC_CURVE_NIST_P192:
  50		return fips_enabled ? NULL : &nist_p192;
  51	case ECC_CURVE_NIST_P256:
  52		return &nist_p256;
 
 
  53	default:
  54		return NULL;
  55	}
  56}
 
  57
  58static u64 *ecc_alloc_digits_space(unsigned int ndigits)
  59{
  60	size_t len = ndigits * sizeof(u64);
  61
  62	if (!len)
  63		return NULL;
  64
  65	return kmalloc(len, GFP_KERNEL);
  66}
  67
  68static void ecc_free_digits_space(u64 *space)
  69{
  70	kzfree(space);
  71}
  72
  73static struct ecc_point *ecc_alloc_point(unsigned int ndigits)
  74{
  75	struct ecc_point *p = kmalloc(sizeof(*p), GFP_KERNEL);
  76
  77	if (!p)
  78		return NULL;
  79
  80	p->x = ecc_alloc_digits_space(ndigits);
  81	if (!p->x)
  82		goto err_alloc_x;
  83
  84	p->y = ecc_alloc_digits_space(ndigits);
  85	if (!p->y)
  86		goto err_alloc_y;
  87
  88	p->ndigits = ndigits;
  89
  90	return p;
  91
  92err_alloc_y:
  93	ecc_free_digits_space(p->x);
  94err_alloc_x:
  95	kfree(p);
  96	return NULL;
  97}
  98
  99static void ecc_free_point(struct ecc_point *p)
 100{
 101	if (!p)
 102		return;
 103
 104	kzfree(p->x);
 105	kzfree(p->y);
 106	kzfree(p);
 107}
 108
 109static void vli_clear(u64 *vli, unsigned int ndigits)
 110{
 111	int i;
 112
 113	for (i = 0; i < ndigits; i++)
 114		vli[i] = 0;
 115}
 116
 117/* Returns true if vli == 0, false otherwise. */
 118bool vli_is_zero(const u64 *vli, unsigned int ndigits)
 119{
 120	int i;
 121
 122	for (i = 0; i < ndigits; i++) {
 123		if (vli[i])
 124			return false;
 125	}
 126
 127	return true;
 128}
 129EXPORT_SYMBOL(vli_is_zero);
 130
 131/* Returns nonzero if bit bit of vli is set. */
 132static u64 vli_test_bit(const u64 *vli, unsigned int bit)
 133{
 134	return (vli[bit / 64] & ((u64)1 << (bit % 64)));
 135}
 136
 137static bool vli_is_negative(const u64 *vli, unsigned int ndigits)
 138{
 139	return vli_test_bit(vli, ndigits * 64 - 1);
 140}
 141
 142/* Counts the number of 64-bit "digits" in vli. */
 143static unsigned int vli_num_digits(const u64 *vli, unsigned int ndigits)
 144{
 145	int i;
 146
 147	/* Search from the end until we find a non-zero digit.
 148	 * We do it in reverse because we expect that most digits will
 149	 * be nonzero.
 150	 */
 151	for (i = ndigits - 1; i >= 0 && vli[i] == 0; i--);
 152
 153	return (i + 1);
 154}
 155
 156/* Counts the number of bits required for vli. */
 157static unsigned int vli_num_bits(const u64 *vli, unsigned int ndigits)
 158{
 159	unsigned int i, num_digits;
 160	u64 digit;
 161
 162	num_digits = vli_num_digits(vli, ndigits);
 163	if (num_digits == 0)
 164		return 0;
 165
 166	digit = vli[num_digits - 1];
 167	for (i = 0; digit; i++)
 168		digit >>= 1;
 169
 170	return ((num_digits - 1) * 64 + i);
 171}
 172
 173/* Set dest from unaligned bit string src. */
 174void vli_from_be64(u64 *dest, const void *src, unsigned int ndigits)
 175{
 176	int i;
 177	const u64 *from = src;
 178
 179	for (i = 0; i < ndigits; i++)
 180		dest[i] = get_unaligned_be64(&from[ndigits - 1 - i]);
 181}
 182EXPORT_SYMBOL(vli_from_be64);
 183
 184void vli_from_le64(u64 *dest, const void *src, unsigned int ndigits)
 185{
 186	int i;
 187	const u64 *from = src;
 188
 189	for (i = 0; i < ndigits; i++)
 190		dest[i] = get_unaligned_le64(&from[i]);
 191}
 192EXPORT_SYMBOL(vli_from_le64);
 193
 194/* Sets dest = src. */
 195static void vli_set(u64 *dest, const u64 *src, unsigned int ndigits)
 196{
 197	int i;
 198
 199	for (i = 0; i < ndigits; i++)
 200		dest[i] = src[i];
 201}
 202
 203/* Returns sign of left - right. */
 204int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits)
 205{
 206	int i;
 207
 208	for (i = ndigits - 1; i >= 0; i--) {
 209		if (left[i] > right[i])
 210			return 1;
 211		else if (left[i] < right[i])
 212			return -1;
 213	}
 214
 215	return 0;
 216}
 217EXPORT_SYMBOL(vli_cmp);
 218
 219/* Computes result = in << c, returning carry. Can modify in place
 220 * (if result == in). 0 < shift < 64.
 221 */
 222static u64 vli_lshift(u64 *result, const u64 *in, unsigned int shift,
 223		      unsigned int ndigits)
 224{
 225	u64 carry = 0;
 226	int i;
 227
 228	for (i = 0; i < ndigits; i++) {
 229		u64 temp = in[i];
 230
 231		result[i] = (temp << shift) | carry;
 232		carry = temp >> (64 - shift);
 233	}
 234
 235	return carry;
 236}
 237
 238/* Computes vli = vli >> 1. */
 239static void vli_rshift1(u64 *vli, unsigned int ndigits)
 240{
 241	u64 *end = vli;
 242	u64 carry = 0;
 243
 244	vli += ndigits;
 245
 246	while (vli-- > end) {
 247		u64 temp = *vli;
 248		*vli = (temp >> 1) | carry;
 249		carry = temp << 63;
 250	}
 251}
 252
 253/* Computes result = left + right, returning carry. Can modify in place. */
 254static u64 vli_add(u64 *result, const u64 *left, const u64 *right,
 255		   unsigned int ndigits)
 256{
 257	u64 carry = 0;
 258	int i;
 259
 260	for (i = 0; i < ndigits; i++) {
 261		u64 sum;
 262
 263		sum = left[i] + right[i] + carry;
 264		if (sum != left[i])
 265			carry = (sum < left[i]);
 266
 267		result[i] = sum;
 268	}
 269
 270	return carry;
 271}
 272
 273/* Computes result = left + right, returning carry. Can modify in place. */
 274static u64 vli_uadd(u64 *result, const u64 *left, u64 right,
 275		    unsigned int ndigits)
 276{
 277	u64 carry = right;
 278	int i;
 279
 280	for (i = 0; i < ndigits; i++) {
 281		u64 sum;
 282
 283		sum = left[i] + carry;
 284		if (sum != left[i])
 285			carry = (sum < left[i]);
 286		else
 287			carry = !!carry;
 288
 289		result[i] = sum;
 290	}
 291
 292	return carry;
 293}
 294
 295/* Computes result = left - right, returning borrow. Can modify in place. */
 296u64 vli_sub(u64 *result, const u64 *left, const u64 *right,
 297		   unsigned int ndigits)
 298{
 299	u64 borrow = 0;
 300	int i;
 301
 302	for (i = 0; i < ndigits; i++) {
 303		u64 diff;
 304
 305		diff = left[i] - right[i] - borrow;
 306		if (diff != left[i])
 307			borrow = (diff > left[i]);
 308
 309		result[i] = diff;
 310	}
 311
 312	return borrow;
 313}
 314EXPORT_SYMBOL(vli_sub);
 315
 316/* Computes result = left - right, returning borrow. Can modify in place. */
 317static u64 vli_usub(u64 *result, const u64 *left, u64 right,
 318	     unsigned int ndigits)
 319{
 320	u64 borrow = right;
 321	int i;
 322
 323	for (i = 0; i < ndigits; i++) {
 324		u64 diff;
 325
 326		diff = left[i] - borrow;
 327		if (diff != left[i])
 328			borrow = (diff > left[i]);
 329
 330		result[i] = diff;
 331	}
 332
 333	return borrow;
 334}
 335
 336static uint128_t mul_64_64(u64 left, u64 right)
 337{
 338	uint128_t result;
 339#if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
 340	unsigned __int128 m = (unsigned __int128)left * right;
 341
 342	result.m_low  = m;
 343	result.m_high = m >> 64;
 344#else
 345	u64 a0 = left & 0xffffffffull;
 346	u64 a1 = left >> 32;
 347	u64 b0 = right & 0xffffffffull;
 348	u64 b1 = right >> 32;
 349	u64 m0 = a0 * b0;
 350	u64 m1 = a0 * b1;
 351	u64 m2 = a1 * b0;
 352	u64 m3 = a1 * b1;
 353
 354	m2 += (m0 >> 32);
 355	m2 += m1;
 356
 357	/* Overflow */
 358	if (m2 < m1)
 359		m3 += 0x100000000ull;
 360
 361	result.m_low = (m0 & 0xffffffffull) | (m2 << 32);
 362	result.m_high = m3 + (m2 >> 32);
 363#endif
 364	return result;
 365}
 366
 367static uint128_t add_128_128(uint128_t a, uint128_t b)
 368{
 369	uint128_t result;
 370
 371	result.m_low = a.m_low + b.m_low;
 372	result.m_high = a.m_high + b.m_high + (result.m_low < a.m_low);
 373
 374	return result;
 375}
 376
 377static void vli_mult(u64 *result, const u64 *left, const u64 *right,
 378		     unsigned int ndigits)
 379{
 380	uint128_t r01 = { 0, 0 };
 381	u64 r2 = 0;
 382	unsigned int i, k;
 383
 384	/* Compute each digit of result in sequence, maintaining the
 385	 * carries.
 386	 */
 387	for (k = 0; k < ndigits * 2 - 1; k++) {
 388		unsigned int min;
 389
 390		if (k < ndigits)
 391			min = 0;
 392		else
 393			min = (k + 1) - ndigits;
 394
 395		for (i = min; i <= k && i < ndigits; i++) {
 396			uint128_t product;
 397
 398			product = mul_64_64(left[i], right[k - i]);
 399
 400			r01 = add_128_128(r01, product);
 401			r2 += (r01.m_high < product.m_high);
 402		}
 403
 404		result[k] = r01.m_low;
 405		r01.m_low = r01.m_high;
 406		r01.m_high = r2;
 407		r2 = 0;
 408	}
 409
 410	result[ndigits * 2 - 1] = r01.m_low;
 411}
 412
 413/* Compute product = left * right, for a small right value. */
 414static void vli_umult(u64 *result, const u64 *left, u32 right,
 415		      unsigned int ndigits)
 416{
 417	uint128_t r01 = { 0 };
 418	unsigned int k;
 419
 420	for (k = 0; k < ndigits; k++) {
 421		uint128_t product;
 422
 423		product = mul_64_64(left[k], right);
 424		r01 = add_128_128(r01, product);
 425		/* no carry */
 426		result[k] = r01.m_low;
 427		r01.m_low = r01.m_high;
 428		r01.m_high = 0;
 429	}
 430	result[k] = r01.m_low;
 431	for (++k; k < ndigits * 2; k++)
 432		result[k] = 0;
 433}
 434
 435static void vli_square(u64 *result, const u64 *left, unsigned int ndigits)
 436{
 437	uint128_t r01 = { 0, 0 };
 438	u64 r2 = 0;
 439	int i, k;
 440
 441	for (k = 0; k < ndigits * 2 - 1; k++) {
 442		unsigned int min;
 443
 444		if (k < ndigits)
 445			min = 0;
 446		else
 447			min = (k + 1) - ndigits;
 448
 449		for (i = min; i <= k && i <= k - i; i++) {
 450			uint128_t product;
 451
 452			product = mul_64_64(left[i], left[k - i]);
 453
 454			if (i < k - i) {
 455				r2 += product.m_high >> 63;
 456				product.m_high = (product.m_high << 1) |
 457						 (product.m_low >> 63);
 458				product.m_low <<= 1;
 459			}
 460
 461			r01 = add_128_128(r01, product);
 462			r2 += (r01.m_high < product.m_high);
 463		}
 464
 465		result[k] = r01.m_low;
 466		r01.m_low = r01.m_high;
 467		r01.m_high = r2;
 468		r2 = 0;
 469	}
 470
 471	result[ndigits * 2 - 1] = r01.m_low;
 472}
 473
 474/* Computes result = (left + right) % mod.
 475 * Assumes that left < mod and right < mod, result != mod.
 476 */
 477static void vli_mod_add(u64 *result, const u64 *left, const u64 *right,
 478			const u64 *mod, unsigned int ndigits)
 479{
 480	u64 carry;
 481
 482	carry = vli_add(result, left, right, ndigits);
 483
 484	/* result > mod (result = mod + remainder), so subtract mod to
 485	 * get remainder.
 486	 */
 487	if (carry || vli_cmp(result, mod, ndigits) >= 0)
 488		vli_sub(result, result, mod, ndigits);
 489}
 490
 491/* Computes result = (left - right) % mod.
 492 * Assumes that left < mod and right < mod, result != mod.
 493 */
 494static void vli_mod_sub(u64 *result, const u64 *left, const u64 *right,
 495			const u64 *mod, unsigned int ndigits)
 496{
 497	u64 borrow = vli_sub(result, left, right, ndigits);
 498
 499	/* In this case, p_result == -diff == (max int) - diff.
 500	 * Since -x % d == d - x, we can get the correct result from
 501	 * result + mod (with overflow).
 502	 */
 503	if (borrow)
 504		vli_add(result, result, mod, ndigits);
 505}
 506
 507/*
 508 * Computes result = product % mod
 509 * for special form moduli: p = 2^k-c, for small c (note the minus sign)
 510 *
 511 * References:
 512 * R. Crandall, C. Pomerance. Prime Numbers: A Computational Perspective.
 513 * 9 Fast Algorithms for Large-Integer Arithmetic. 9.2.3 Moduli of special form
 514 * Algorithm 9.2.13 (Fast mod operation for special-form moduli).
 515 */
 516static void vli_mmod_special(u64 *result, const u64 *product,
 517			      const u64 *mod, unsigned int ndigits)
 518{
 519	u64 c = -mod[0];
 520	u64 t[ECC_MAX_DIGITS * 2];
 521	u64 r[ECC_MAX_DIGITS * 2];
 522
 523	vli_set(r, product, ndigits * 2);
 524	while (!vli_is_zero(r + ndigits, ndigits)) {
 525		vli_umult(t, r + ndigits, c, ndigits);
 526		vli_clear(r + ndigits, ndigits);
 527		vli_add(r, r, t, ndigits * 2);
 528	}
 529	vli_set(t, mod, ndigits);
 530	vli_clear(t + ndigits, ndigits);
 531	while (vli_cmp(r, t, ndigits * 2) >= 0)
 532		vli_sub(r, r, t, ndigits * 2);
 533	vli_set(result, r, ndigits);
 534}
 535
 536/*
 537 * Computes result = product % mod
 538 * for special form moduli: p = 2^{k-1}+c, for small c (note the plus sign)
 539 * where k-1 does not fit into qword boundary by -1 bit (such as 255).
 540
 541 * References (loosely based on):
 542 * A. Menezes, P. van Oorschot, S. Vanstone. Handbook of Applied Cryptography.
 543 * 14.3.4 Reduction methods for moduli of special form. Algorithm 14.47.
 544 * URL: http://cacr.uwaterloo.ca/hac/about/chap14.pdf
 545 *
 546 * H. Cohen, G. Frey, R. Avanzi, C. Doche, T. Lange, K. Nguyen, F. Vercauteren.
 547 * Handbook of Elliptic and Hyperelliptic Curve Cryptography.
 548 * Algorithm 10.25 Fast reduction for special form moduli
 549 */
 550static void vli_mmod_special2(u64 *result, const u64 *product,
 551			       const u64 *mod, unsigned int ndigits)
 552{
 553	u64 c2 = mod[0] * 2;
 554	u64 q[ECC_MAX_DIGITS];
 555	u64 r[ECC_MAX_DIGITS * 2];
 556	u64 m[ECC_MAX_DIGITS * 2]; /* expanded mod */
 557	int carry; /* last bit that doesn't fit into q */
 558	int i;
 559
 560	vli_set(m, mod, ndigits);
 561	vli_clear(m + ndigits, ndigits);
 562
 563	vli_set(r, product, ndigits);
 564	/* q and carry are top bits */
 565	vli_set(q, product + ndigits, ndigits);
 566	vli_clear(r + ndigits, ndigits);
 567	carry = vli_is_negative(r, ndigits);
 568	if (carry)
 569		r[ndigits - 1] &= (1ull << 63) - 1;
 570	for (i = 1; carry || !vli_is_zero(q, ndigits); i++) {
 571		u64 qc[ECC_MAX_DIGITS * 2];
 572
 573		vli_umult(qc, q, c2, ndigits);
 574		if (carry)
 575			vli_uadd(qc, qc, mod[0], ndigits * 2);
 576		vli_set(q, qc + ndigits, ndigits);
 577		vli_clear(qc + ndigits, ndigits);
 578		carry = vli_is_negative(qc, ndigits);
 579		if (carry)
 580			qc[ndigits - 1] &= (1ull << 63) - 1;
 581		if (i & 1)
 582			vli_sub(r, r, qc, ndigits * 2);
 583		else
 584			vli_add(r, r, qc, ndigits * 2);
 585	}
 586	while (vli_is_negative(r, ndigits * 2))
 587		vli_add(r, r, m, ndigits * 2);
 588	while (vli_cmp(r, m, ndigits * 2) >= 0)
 589		vli_sub(r, r, m, ndigits * 2);
 590
 591	vli_set(result, r, ndigits);
 592}
 593
 594/*
 595 * Computes result = product % mod, where product is 2N words long.
 596 * Reference: Ken MacKay's micro-ecc.
 597 * Currently only designed to work for curve_p or curve_n.
 598 */
 599static void vli_mmod_slow(u64 *result, u64 *product, const u64 *mod,
 600			  unsigned int ndigits)
 601{
 602	u64 mod_m[2 * ECC_MAX_DIGITS];
 603	u64 tmp[2 * ECC_MAX_DIGITS];
 604	u64 *v[2] = { tmp, product };
 605	u64 carry = 0;
 606	unsigned int i;
 607	/* Shift mod so its highest set bit is at the maximum position. */
 608	int shift = (ndigits * 2 * 64) - vli_num_bits(mod, ndigits);
 609	int word_shift = shift / 64;
 610	int bit_shift = shift % 64;
 611
 612	vli_clear(mod_m, word_shift);
 613	if (bit_shift > 0) {
 614		for (i = 0; i < ndigits; ++i) {
 615			mod_m[word_shift + i] = (mod[i] << bit_shift) | carry;
 616			carry = mod[i] >> (64 - bit_shift);
 617		}
 618	} else
 619		vli_set(mod_m + word_shift, mod, ndigits);
 620
 621	for (i = 1; shift >= 0; --shift) {
 622		u64 borrow = 0;
 623		unsigned int j;
 624
 625		for (j = 0; j < ndigits * 2; ++j) {
 626			u64 diff = v[i][j] - mod_m[j] - borrow;
 627
 628			if (diff != v[i][j])
 629				borrow = (diff > v[i][j]);
 630			v[1 - i][j] = diff;
 631		}
 632		i = !(i ^ borrow); /* Swap the index if there was no borrow */
 633		vli_rshift1(mod_m, ndigits);
 634		mod_m[ndigits - 1] |= mod_m[ndigits] << (64 - 1);
 635		vli_rshift1(mod_m + ndigits, ndigits);
 636	}
 637	vli_set(result, v[i], ndigits);
 638}
 639
 640/* Computes result = product % mod using Barrett's reduction with precomputed
 641 * value mu appended to the mod after ndigits, mu = (2^{2w} / mod) and have
 642 * length ndigits + 1, where mu * (2^w - 1) should not overflow ndigits
 643 * boundary.
 644 *
 645 * Reference:
 646 * R. Brent, P. Zimmermann. Modern Computer Arithmetic. 2010.
 647 * 2.4.1 Barrett's algorithm. Algorithm 2.5.
 648 */
 649static void vli_mmod_barrett(u64 *result, u64 *product, const u64 *mod,
 650			     unsigned int ndigits)
 651{
 652	u64 q[ECC_MAX_DIGITS * 2];
 653	u64 r[ECC_MAX_DIGITS * 2];
 654	const u64 *mu = mod + ndigits;
 655
 656	vli_mult(q, product + ndigits, mu, ndigits);
 657	if (mu[ndigits])
 658		vli_add(q + ndigits, q + ndigits, product + ndigits, ndigits);
 659	vli_mult(r, mod, q + ndigits, ndigits);
 660	vli_sub(r, product, r, ndigits * 2);
 661	while (!vli_is_zero(r + ndigits, ndigits) ||
 662	       vli_cmp(r, mod, ndigits) != -1) {
 663		u64 carry;
 664
 665		carry = vli_sub(r, r, mod, ndigits);
 666		vli_usub(r + ndigits, r + ndigits, carry, ndigits);
 667	}
 668	vli_set(result, r, ndigits);
 669}
 670
 671/* Computes p_result = p_product % curve_p.
 672 * See algorithm 5 and 6 from
 673 * http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf
 674 */
 675static void vli_mmod_fast_192(u64 *result, const u64 *product,
 676			      const u64 *curve_prime, u64 *tmp)
 677{
 678	const unsigned int ndigits = 3;
 679	int carry;
 680
 681	vli_set(result, product, ndigits);
 682
 683	vli_set(tmp, &product[3], ndigits);
 684	carry = vli_add(result, result, tmp, ndigits);
 685
 686	tmp[0] = 0;
 687	tmp[1] = product[3];
 688	tmp[2] = product[4];
 689	carry += vli_add(result, result, tmp, ndigits);
 690
 691	tmp[0] = tmp[1] = product[5];
 692	tmp[2] = 0;
 693	carry += vli_add(result, result, tmp, ndigits);
 694
 695	while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
 696		carry -= vli_sub(result, result, curve_prime, ndigits);
 697}
 698
 699/* Computes result = product % curve_prime
 700 * from http://www.nsa.gov/ia/_files/nist-routines.pdf
 701 */
 702static void vli_mmod_fast_256(u64 *result, const u64 *product,
 703			      const u64 *curve_prime, u64 *tmp)
 704{
 705	int carry;
 706	const unsigned int ndigits = 4;
 707
 708	/* t */
 709	vli_set(result, product, ndigits);
 710
 711	/* s1 */
 712	tmp[0] = 0;
 713	tmp[1] = product[5] & 0xffffffff00000000ull;
 714	tmp[2] = product[6];
 715	tmp[3] = product[7];
 716	carry = vli_lshift(tmp, tmp, 1, ndigits);
 717	carry += vli_add(result, result, tmp, ndigits);
 718
 719	/* s2 */
 720	tmp[1] = product[6] << 32;
 721	tmp[2] = (product[6] >> 32) | (product[7] << 32);
 722	tmp[3] = product[7] >> 32;
 723	carry += vli_lshift(tmp, tmp, 1, ndigits);
 724	carry += vli_add(result, result, tmp, ndigits);
 725
 726	/* s3 */
 727	tmp[0] = product[4];
 728	tmp[1] = product[5] & 0xffffffff;
 729	tmp[2] = 0;
 730	tmp[3] = product[7];
 731	carry += vli_add(result, result, tmp, ndigits);
 732
 733	/* s4 */
 734	tmp[0] = (product[4] >> 32) | (product[5] << 32);
 735	tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull);
 736	tmp[2] = product[7];
 737	tmp[3] = (product[6] >> 32) | (product[4] << 32);
 738	carry += vli_add(result, result, tmp, ndigits);
 739
 740	/* d1 */
 741	tmp[0] = (product[5] >> 32) | (product[6] << 32);
 742	tmp[1] = (product[6] >> 32);
 743	tmp[2] = 0;
 744	tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32);
 745	carry -= vli_sub(result, result, tmp, ndigits);
 746
 747	/* d2 */
 748	tmp[0] = product[6];
 749	tmp[1] = product[7];
 750	tmp[2] = 0;
 751	tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull);
 752	carry -= vli_sub(result, result, tmp, ndigits);
 753
 754	/* d3 */
 755	tmp[0] = (product[6] >> 32) | (product[7] << 32);
 756	tmp[1] = (product[7] >> 32) | (product[4] << 32);
 757	tmp[2] = (product[4] >> 32) | (product[5] << 32);
 758	tmp[3] = (product[6] << 32);
 759	carry -= vli_sub(result, result, tmp, ndigits);
 760
 761	/* d4 */
 762	tmp[0] = product[7];
 763	tmp[1] = product[4] & 0xffffffff00000000ull;
 764	tmp[2] = product[5];
 765	tmp[3] = product[6] & 0xffffffff00000000ull;
 766	carry -= vli_sub(result, result, tmp, ndigits);
 767
 768	if (carry < 0) {
 769		do {
 770			carry += vli_add(result, result, curve_prime, ndigits);
 771		} while (carry < 0);
 772	} else {
 773		while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
 774			carry -= vli_sub(result, result, curve_prime, ndigits);
 775	}
 776}
 777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 778/* Computes result = product % curve_prime for different curve_primes.
 779 *
 780 * Note that curve_primes are distinguished just by heuristic check and
 781 * not by complete conformance check.
 782 */
 783static bool vli_mmod_fast(u64 *result, u64 *product,
 784			  const u64 *curve_prime, unsigned int ndigits)
 785{
 786	u64 tmp[2 * ECC_MAX_DIGITS];
 
 
 787
 788	/* Currently, both NIST primes have -1 in lowest qword. */
 789	if (curve_prime[0] != -1ull) {
 790		/* Try to handle Pseudo-Marsenne primes. */
 791		if (curve_prime[ndigits - 1] == -1ull) {
 792			vli_mmod_special(result, product, curve_prime,
 793					 ndigits);
 794			return true;
 795		} else if (curve_prime[ndigits - 1] == 1ull << 63 &&
 796			   curve_prime[ndigits - 2] == 0) {
 797			vli_mmod_special2(result, product, curve_prime,
 798					  ndigits);
 799			return true;
 800		}
 801		vli_mmod_barrett(result, product, curve_prime, ndigits);
 802		return true;
 803	}
 804
 805	switch (ndigits) {
 806	case 3:
 807		vli_mmod_fast_192(result, product, curve_prime, tmp);
 808		break;
 809	case 4:
 810		vli_mmod_fast_256(result, product, curve_prime, tmp);
 811		break;
 
 
 
 812	default:
 813		pr_err_ratelimited("ecc: unsupported digits size!\n");
 814		return false;
 815	}
 816
 817	return true;
 818}
 819
 820/* Computes result = (left * right) % mod.
 821 * Assumes that mod is big enough curve order.
 822 */
 823void vli_mod_mult_slow(u64 *result, const u64 *left, const u64 *right,
 824		       const u64 *mod, unsigned int ndigits)
 825{
 826	u64 product[ECC_MAX_DIGITS * 2];
 827
 828	vli_mult(product, left, right, ndigits);
 829	vli_mmod_slow(result, product, mod, ndigits);
 830}
 831EXPORT_SYMBOL(vli_mod_mult_slow);
 832
 833/* Computes result = (left * right) % curve_prime. */
 834static void vli_mod_mult_fast(u64 *result, const u64 *left, const u64 *right,
 835			      const u64 *curve_prime, unsigned int ndigits)
 836{
 837	u64 product[2 * ECC_MAX_DIGITS];
 838
 839	vli_mult(product, left, right, ndigits);
 840	vli_mmod_fast(result, product, curve_prime, ndigits);
 841}
 842
 843/* Computes result = left^2 % curve_prime. */
 844static void vli_mod_square_fast(u64 *result, const u64 *left,
 845				const u64 *curve_prime, unsigned int ndigits)
 846{
 847	u64 product[2 * ECC_MAX_DIGITS];
 848
 849	vli_square(product, left, ndigits);
 850	vli_mmod_fast(result, product, curve_prime, ndigits);
 851}
 852
 853#define EVEN(vli) (!(vli[0] & 1))
 854/* Computes result = (1 / p_input) % mod. All VLIs are the same size.
 855 * See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
 856 * https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf
 857 */
 858void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,
 859			unsigned int ndigits)
 860{
 861	u64 a[ECC_MAX_DIGITS], b[ECC_MAX_DIGITS];
 862	u64 u[ECC_MAX_DIGITS], v[ECC_MAX_DIGITS];
 863	u64 carry;
 864	int cmp_result;
 865
 866	if (vli_is_zero(input, ndigits)) {
 867		vli_clear(result, ndigits);
 868		return;
 869	}
 870
 871	vli_set(a, input, ndigits);
 872	vli_set(b, mod, ndigits);
 873	vli_clear(u, ndigits);
 874	u[0] = 1;
 875	vli_clear(v, ndigits);
 876
 877	while ((cmp_result = vli_cmp(a, b, ndigits)) != 0) {
 878		carry = 0;
 879
 880		if (EVEN(a)) {
 881			vli_rshift1(a, ndigits);
 882
 883			if (!EVEN(u))
 884				carry = vli_add(u, u, mod, ndigits);
 885
 886			vli_rshift1(u, ndigits);
 887			if (carry)
 888				u[ndigits - 1] |= 0x8000000000000000ull;
 889		} else if (EVEN(b)) {
 890			vli_rshift1(b, ndigits);
 891
 892			if (!EVEN(v))
 893				carry = vli_add(v, v, mod, ndigits);
 894
 895			vli_rshift1(v, ndigits);
 896			if (carry)
 897				v[ndigits - 1] |= 0x8000000000000000ull;
 898		} else if (cmp_result > 0) {
 899			vli_sub(a, a, b, ndigits);
 900			vli_rshift1(a, ndigits);
 901
 902			if (vli_cmp(u, v, ndigits) < 0)
 903				vli_add(u, u, mod, ndigits);
 904
 905			vli_sub(u, u, v, ndigits);
 906			if (!EVEN(u))
 907				carry = vli_add(u, u, mod, ndigits);
 908
 909			vli_rshift1(u, ndigits);
 910			if (carry)
 911				u[ndigits - 1] |= 0x8000000000000000ull;
 912		} else {
 913			vli_sub(b, b, a, ndigits);
 914			vli_rshift1(b, ndigits);
 915
 916			if (vli_cmp(v, u, ndigits) < 0)
 917				vli_add(v, v, mod, ndigits);
 918
 919			vli_sub(v, v, u, ndigits);
 920			if (!EVEN(v))
 921				carry = vli_add(v, v, mod, ndigits);
 922
 923			vli_rshift1(v, ndigits);
 924			if (carry)
 925				v[ndigits - 1] |= 0x8000000000000000ull;
 926		}
 927	}
 928
 929	vli_set(result, u, ndigits);
 930}
 931EXPORT_SYMBOL(vli_mod_inv);
 932
 933/* ------ Point operations ------ */
 934
 935/* Returns true if p_point is the point at infinity, false otherwise. */
 936static bool ecc_point_is_zero(const struct ecc_point *point)
 937{
 938	return (vli_is_zero(point->x, point->ndigits) &&
 939		vli_is_zero(point->y, point->ndigits));
 940}
 941
 942/* Point multiplication algorithm using Montgomery's ladder with co-Z
 943 * coordinates. From http://eprint.iacr.org/2011/338.pdf
 944 */
 945
 946/* Double in place */
 947static void ecc_point_double_jacobian(u64 *x1, u64 *y1, u64 *z1,
 948				      u64 *curve_prime, unsigned int ndigits)
 949{
 950	/* t1 = x, t2 = y, t3 = z */
 951	u64 t4[ECC_MAX_DIGITS];
 952	u64 t5[ECC_MAX_DIGITS];
 
 
 953
 954	if (vli_is_zero(z1, ndigits))
 955		return;
 956
 957	/* t4 = y1^2 */
 958	vli_mod_square_fast(t4, y1, curve_prime, ndigits);
 959	/* t5 = x1*y1^2 = A */
 960	vli_mod_mult_fast(t5, x1, t4, curve_prime, ndigits);
 961	/* t4 = y1^4 */
 962	vli_mod_square_fast(t4, t4, curve_prime, ndigits);
 963	/* t2 = y1*z1 = z3 */
 964	vli_mod_mult_fast(y1, y1, z1, curve_prime, ndigits);
 965	/* t3 = z1^2 */
 966	vli_mod_square_fast(z1, z1, curve_prime, ndigits);
 967
 968	/* t1 = x1 + z1^2 */
 969	vli_mod_add(x1, x1, z1, curve_prime, ndigits);
 970	/* t3 = 2*z1^2 */
 971	vli_mod_add(z1, z1, z1, curve_prime, ndigits);
 972	/* t3 = x1 - z1^2 */
 973	vli_mod_sub(z1, x1, z1, curve_prime, ndigits);
 974	/* t1 = x1^2 - z1^4 */
 975	vli_mod_mult_fast(x1, x1, z1, curve_prime, ndigits);
 976
 977	/* t3 = 2*(x1^2 - z1^4) */
 978	vli_mod_add(z1, x1, x1, curve_prime, ndigits);
 979	/* t1 = 3*(x1^2 - z1^4) */
 980	vli_mod_add(x1, x1, z1, curve_prime, ndigits);
 981	if (vli_test_bit(x1, 0)) {
 982		u64 carry = vli_add(x1, x1, curve_prime, ndigits);
 983
 984		vli_rshift1(x1, ndigits);
 985		x1[ndigits - 1] |= carry << 63;
 986	} else {
 987		vli_rshift1(x1, ndigits);
 988	}
 989	/* t1 = 3/2*(x1^2 - z1^4) = B */
 990
 991	/* t3 = B^2 */
 992	vli_mod_square_fast(z1, x1, curve_prime, ndigits);
 993	/* t3 = B^2 - A */
 994	vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
 995	/* t3 = B^2 - 2A = x3 */
 996	vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
 997	/* t5 = A - x3 */
 998	vli_mod_sub(t5, t5, z1, curve_prime, ndigits);
 999	/* t1 = B * (A - x3) */
1000	vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
1001	/* t4 = B * (A - x3) - y1^4 = y3 */
1002	vli_mod_sub(t4, x1, t4, curve_prime, ndigits);
1003
1004	vli_set(x1, z1, ndigits);
1005	vli_set(z1, y1, ndigits);
1006	vli_set(y1, t4, ndigits);
1007}
1008
1009/* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
1010static void apply_z(u64 *x1, u64 *y1, u64 *z, u64 *curve_prime,
1011		    unsigned int ndigits)
1012{
1013	u64 t1[ECC_MAX_DIGITS];
1014
1015	vli_mod_square_fast(t1, z, curve_prime, ndigits);    /* z^2 */
1016	vli_mod_mult_fast(x1, x1, t1, curve_prime, ndigits); /* x1 * z^2 */
1017	vli_mod_mult_fast(t1, t1, z, curve_prime, ndigits);  /* z^3 */
1018	vli_mod_mult_fast(y1, y1, t1, curve_prime, ndigits); /* y1 * z^3 */
1019}
1020
1021/* P = (x1, y1) => 2P, (x2, y2) => P' */
1022static void xycz_initial_double(u64 *x1, u64 *y1, u64 *x2, u64 *y2,
1023				u64 *p_initial_z, u64 *curve_prime,
1024				unsigned int ndigits)
1025{
1026	u64 z[ECC_MAX_DIGITS];
 
1027
1028	vli_set(x2, x1, ndigits);
1029	vli_set(y2, y1, ndigits);
1030
1031	vli_clear(z, ndigits);
1032	z[0] = 1;
1033
1034	if (p_initial_z)
1035		vli_set(z, p_initial_z, ndigits);
1036
1037	apply_z(x1, y1, z, curve_prime, ndigits);
1038
1039	ecc_point_double_jacobian(x1, y1, z, curve_prime, ndigits);
1040
1041	apply_z(x2, y2, z, curve_prime, ndigits);
1042}
1043
1044/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
1045 * Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
1046 * or P => P', Q => P + Q
1047 */
1048static void xycz_add(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,
1049		     unsigned int ndigits)
1050{
1051	/* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
1052	u64 t5[ECC_MAX_DIGITS];
 
 
1053
1054	/* t5 = x2 - x1 */
1055	vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
1056	/* t5 = (x2 - x1)^2 = A */
1057	vli_mod_square_fast(t5, t5, curve_prime, ndigits);
1058	/* t1 = x1*A = B */
1059	vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
1060	/* t3 = x2*A = C */
1061	vli_mod_mult_fast(x2, x2, t5, curve_prime, ndigits);
1062	/* t4 = y2 - y1 */
1063	vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
1064	/* t5 = (y2 - y1)^2 = D */
1065	vli_mod_square_fast(t5, y2, curve_prime, ndigits);
1066
1067	/* t5 = D - B */
1068	vli_mod_sub(t5, t5, x1, curve_prime, ndigits);
1069	/* t5 = D - B - C = x3 */
1070	vli_mod_sub(t5, t5, x2, curve_prime, ndigits);
1071	/* t3 = C - B */
1072	vli_mod_sub(x2, x2, x1, curve_prime, ndigits);
1073	/* t2 = y1*(C - B) */
1074	vli_mod_mult_fast(y1, y1, x2, curve_prime, ndigits);
1075	/* t3 = B - x3 */
1076	vli_mod_sub(x2, x1, t5, curve_prime, ndigits);
1077	/* t4 = (y2 - y1)*(B - x3) */
1078	vli_mod_mult_fast(y2, y2, x2, curve_prime, ndigits);
1079	/* t4 = y3 */
1080	vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
1081
1082	vli_set(x2, t5, ndigits);
1083}
1084
1085/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
1086 * Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
1087 * or P => P - Q, Q => P + Q
1088 */
1089static void xycz_add_c(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,
1090		       unsigned int ndigits)
1091{
1092	/* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
1093	u64 t5[ECC_MAX_DIGITS];
1094	u64 t6[ECC_MAX_DIGITS];
1095	u64 t7[ECC_MAX_DIGITS];
 
 
1096
1097	/* t5 = x2 - x1 */
1098	vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
1099	/* t5 = (x2 - x1)^2 = A */
1100	vli_mod_square_fast(t5, t5, curve_prime, ndigits);
1101	/* t1 = x1*A = B */
1102	vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
1103	/* t3 = x2*A = C */
1104	vli_mod_mult_fast(x2, x2, t5, curve_prime, ndigits);
1105	/* t4 = y2 + y1 */
1106	vli_mod_add(t5, y2, y1, curve_prime, ndigits);
1107	/* t4 = y2 - y1 */
1108	vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
1109
1110	/* t6 = C - B */
1111	vli_mod_sub(t6, x2, x1, curve_prime, ndigits);
1112	/* t2 = y1 * (C - B) */
1113	vli_mod_mult_fast(y1, y1, t6, curve_prime, ndigits);
1114	/* t6 = B + C */
1115	vli_mod_add(t6, x1, x2, curve_prime, ndigits);
1116	/* t3 = (y2 - y1)^2 */
1117	vli_mod_square_fast(x2, y2, curve_prime, ndigits);
1118	/* t3 = x3 */
1119	vli_mod_sub(x2, x2, t6, curve_prime, ndigits);
1120
1121	/* t7 = B - x3 */
1122	vli_mod_sub(t7, x1, x2, curve_prime, ndigits);
1123	/* t4 = (y2 - y1)*(B - x3) */
1124	vli_mod_mult_fast(y2, y2, t7, curve_prime, ndigits);
1125	/* t4 = y3 */
1126	vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
1127
1128	/* t7 = (y2 + y1)^2 = F */
1129	vli_mod_square_fast(t7, t5, curve_prime, ndigits);
1130	/* t7 = x3' */
1131	vli_mod_sub(t7, t7, t6, curve_prime, ndigits);
1132	/* t6 = x3' - B */
1133	vli_mod_sub(t6, t7, x1, curve_prime, ndigits);
1134	/* t6 = (y2 + y1)*(x3' - B) */
1135	vli_mod_mult_fast(t6, t6, t5, curve_prime, ndigits);
1136	/* t2 = y3' */
1137	vli_mod_sub(y1, t6, y1, curve_prime, ndigits);
1138
1139	vli_set(x1, t7, ndigits);
1140}
1141
1142static void ecc_point_mult(struct ecc_point *result,
1143			   const struct ecc_point *point, const u64 *scalar,
1144			   u64 *initial_z, const struct ecc_curve *curve,
1145			   unsigned int ndigits)
1146{
1147	/* R0 and R1 */
1148	u64 rx[2][ECC_MAX_DIGITS];
1149	u64 ry[2][ECC_MAX_DIGITS];
1150	u64 z[ECC_MAX_DIGITS];
1151	u64 sk[2][ECC_MAX_DIGITS];
1152	u64 *curve_prime = curve->p;
1153	int i, nb;
1154	int num_bits;
1155	int carry;
1156
1157	carry = vli_add(sk[0], scalar, curve->n, ndigits);
1158	vli_add(sk[1], sk[0], curve->n, ndigits);
1159	scalar = sk[!carry];
1160	num_bits = sizeof(u64) * ndigits * 8 + 1;
1161
1162	vli_set(rx[1], point->x, ndigits);
1163	vli_set(ry[1], point->y, ndigits);
1164
1165	xycz_initial_double(rx[1], ry[1], rx[0], ry[0], initial_z, curve_prime,
1166			    ndigits);
1167
1168	for (i = num_bits - 2; i > 0; i--) {
1169		nb = !vli_test_bit(scalar, i);
1170		xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve_prime,
1171			   ndigits);
1172		xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve_prime,
1173			 ndigits);
1174	}
1175
1176	nb = !vli_test_bit(scalar, 0);
1177	xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve_prime,
1178		   ndigits);
1179
1180	/* Find final 1/Z value. */
1181	/* X1 - X0 */
1182	vli_mod_sub(z, rx[1], rx[0], curve_prime, ndigits);
1183	/* Yb * (X1 - X0) */
1184	vli_mod_mult_fast(z, z, ry[1 - nb], curve_prime, ndigits);
1185	/* xP * Yb * (X1 - X0) */
1186	vli_mod_mult_fast(z, z, point->x, curve_prime, ndigits);
1187
1188	/* 1 / (xP * Yb * (X1 - X0)) */
1189	vli_mod_inv(z, z, curve_prime, point->ndigits);
1190
1191	/* yP / (xP * Yb * (X1 - X0)) */
1192	vli_mod_mult_fast(z, z, point->y, curve_prime, ndigits);
1193	/* Xb * yP / (xP * Yb * (X1 - X0)) */
1194	vli_mod_mult_fast(z, z, rx[1 - nb], curve_prime, ndigits);
1195	/* End 1/Z calculation */
1196
1197	xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve_prime, ndigits);
1198
1199	apply_z(rx[0], ry[0], z, curve_prime, ndigits);
1200
1201	vli_set(result->x, rx[0], ndigits);
1202	vli_set(result->y, ry[0], ndigits);
1203}
1204
1205/* Computes R = P + Q mod p */
1206static void ecc_point_add(const struct ecc_point *result,
1207		   const struct ecc_point *p, const struct ecc_point *q,
1208		   const struct ecc_curve *curve)
1209{
1210	u64 z[ECC_MAX_DIGITS];
1211	u64 px[ECC_MAX_DIGITS];
1212	u64 py[ECC_MAX_DIGITS];
1213	unsigned int ndigits = curve->g.ndigits;
1214
1215	vli_set(result->x, q->x, ndigits);
1216	vli_set(result->y, q->y, ndigits);
1217	vli_mod_sub(z, result->x, p->x, curve->p, ndigits);
1218	vli_set(px, p->x, ndigits);
1219	vli_set(py, p->y, ndigits);
1220	xycz_add(px, py, result->x, result->y, curve->p, ndigits);
1221	vli_mod_inv(z, z, curve->p, ndigits);
1222	apply_z(result->x, result->y, z, curve->p, ndigits);
1223}
1224
1225/* Computes R = u1P + u2Q mod p using Shamir's trick.
1226 * Based on: Kenneth MacKay's micro-ecc (2014).
1227 */
1228void ecc_point_mult_shamir(const struct ecc_point *result,
1229			   const u64 *u1, const struct ecc_point *p,
1230			   const u64 *u2, const struct ecc_point *q,
1231			   const struct ecc_curve *curve)
1232{
1233	u64 z[ECC_MAX_DIGITS];
1234	u64 sump[2][ECC_MAX_DIGITS];
1235	u64 *rx = result->x;
1236	u64 *ry = result->y;
1237	unsigned int ndigits = curve->g.ndigits;
1238	unsigned int num_bits;
1239	struct ecc_point sum = ECC_POINT_INIT(sump[0], sump[1], ndigits);
1240	const struct ecc_point *points[4];
1241	const struct ecc_point *point;
1242	unsigned int idx;
1243	int i;
1244
1245	ecc_point_add(&sum, p, q, curve);
1246	points[0] = NULL;
1247	points[1] = p;
1248	points[2] = q;
1249	points[3] = &sum;
1250
1251	num_bits = max(vli_num_bits(u1, ndigits),
1252		       vli_num_bits(u2, ndigits));
1253	i = num_bits - 1;
1254	idx = (!!vli_test_bit(u1, i)) | ((!!vli_test_bit(u2, i)) << 1);
1255	point = points[idx];
1256
1257	vli_set(rx, point->x, ndigits);
1258	vli_set(ry, point->y, ndigits);
1259	vli_clear(z + 1, ndigits - 1);
1260	z[0] = 1;
1261
1262	for (--i; i >= 0; i--) {
1263		ecc_point_double_jacobian(rx, ry, z, curve->p, ndigits);
1264		idx = (!!vli_test_bit(u1, i)) | ((!!vli_test_bit(u2, i)) << 1);
1265		point = points[idx];
1266		if (point) {
1267			u64 tx[ECC_MAX_DIGITS];
1268			u64 ty[ECC_MAX_DIGITS];
1269			u64 tz[ECC_MAX_DIGITS];
1270
1271			vli_set(tx, point->x, ndigits);
1272			vli_set(ty, point->y, ndigits);
1273			apply_z(tx, ty, z, curve->p, ndigits);
1274			vli_mod_sub(tz, rx, tx, curve->p, ndigits);
1275			xycz_add(tx, ty, rx, ry, curve->p, ndigits);
1276			vli_mod_mult_fast(z, z, tz, curve->p, ndigits);
1277		}
1278	}
1279	vli_mod_inv(z, z, curve->p, ndigits);
1280	apply_z(rx, ry, z, curve->p, ndigits);
1281}
1282EXPORT_SYMBOL(ecc_point_mult_shamir);
1283
1284static inline void ecc_swap_digits(const u64 *in, u64 *out,
1285				   unsigned int ndigits)
1286{
1287	int i;
1288
1289	for (i = 0; i < ndigits; i++)
1290		out[i] = __swab64(in[ndigits - 1 - i]);
1291}
1292
1293static int __ecc_is_key_valid(const struct ecc_curve *curve,
1294			      const u64 *private_key, unsigned int ndigits)
1295{
1296	u64 one[ECC_MAX_DIGITS] = { 1, };
1297	u64 res[ECC_MAX_DIGITS];
1298
1299	if (!private_key)
1300		return -EINVAL;
1301
1302	if (curve->g.ndigits != ndigits)
1303		return -EINVAL;
1304
1305	/* Make sure the private key is in the range [2, n-3]. */
1306	if (vli_cmp(one, private_key, ndigits) != -1)
1307		return -EINVAL;
1308	vli_sub(res, curve->n, one, ndigits);
1309	vli_sub(res, res, one, ndigits);
1310	if (vli_cmp(res, private_key, ndigits) != 1)
1311		return -EINVAL;
1312
1313	return 0;
1314}
1315
1316int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
1317		     const u64 *private_key, unsigned int private_key_len)
1318{
1319	int nbytes;
1320	const struct ecc_curve *curve = ecc_get_curve(curve_id);
1321
1322	nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
1323
1324	if (private_key_len != nbytes)
1325		return -EINVAL;
1326
1327	return __ecc_is_key_valid(curve, private_key, ndigits);
1328}
1329EXPORT_SYMBOL(ecc_is_key_valid);
1330
1331/*
1332 * ECC private keys are generated using the method of extra random bits,
1333 * equivalent to that described in FIPS 186-4, Appendix B.4.1.
1334 *
1335 * d = (c mod(n–1)) + 1    where c is a string of random bits, 64 bits longer
1336 *                         than requested
1337 * 0 <= c mod(n-1) <= n-2  and implies that
1338 * 1 <= d <= n-1
1339 *
1340 * This method generates a private key uniformly distributed in the range
1341 * [1, n-1].
1342 */
1343int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey)
1344{
1345	const struct ecc_curve *curve = ecc_get_curve(curve_id);
1346	u64 priv[ECC_MAX_DIGITS];
1347	unsigned int nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
1348	unsigned int nbits = vli_num_bits(curve->n, ndigits);
1349	int err;
1350
1351	/* Check that N is included in Table 1 of FIPS 186-4, section 6.1.1 */
1352	if (nbits < 160 || ndigits > ARRAY_SIZE(priv))
1353		return -EINVAL;
1354
1355	/*
1356	 * FIPS 186-4 recommends that the private key should be obtained from a
1357	 * RBG with a security strength equal to or greater than the security
1358	 * strength associated with N.
1359	 *
1360	 * The maximum security strength identified by NIST SP800-57pt1r4 for
1361	 * ECC is 256 (N >= 512).
1362	 *
1363	 * This condition is met by the default RNG because it selects a favored
1364	 * DRBG with a security strength of 256.
1365	 */
1366	if (crypto_get_default_rng())
1367		return -EFAULT;
1368
1369	err = crypto_rng_get_bytes(crypto_default_rng, (u8 *)priv, nbytes);
1370	crypto_put_default_rng();
1371	if (err)
1372		return err;
1373
1374	/* Make sure the private key is in the valid range. */
1375	if (__ecc_is_key_valid(curve, priv, ndigits))
1376		return -EINVAL;
1377
1378	ecc_swap_digits(priv, privkey, ndigits);
1379
1380	return 0;
1381}
1382EXPORT_SYMBOL(ecc_gen_privkey);
1383
1384int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
1385		     const u64 *private_key, u64 *public_key)
1386{
1387	int ret = 0;
1388	struct ecc_point *pk;
1389	u64 priv[ECC_MAX_DIGITS];
1390	const struct ecc_curve *curve = ecc_get_curve(curve_id);
1391
1392	if (!private_key || !curve || ndigits > ARRAY_SIZE(priv)) {
1393		ret = -EINVAL;
1394		goto out;
1395	}
1396
1397	ecc_swap_digits(private_key, priv, ndigits);
1398
1399	pk = ecc_alloc_point(ndigits);
1400	if (!pk) {
1401		ret = -ENOMEM;
1402		goto out;
1403	}
1404
1405	ecc_point_mult(pk, &curve->g, priv, NULL, curve, ndigits);
1406	if (ecc_point_is_zero(pk)) {
 
 
1407		ret = -EAGAIN;
1408		goto err_free_point;
1409	}
1410
1411	ecc_swap_digits(pk->x, public_key, ndigits);
1412	ecc_swap_digits(pk->y, &public_key[ndigits], ndigits);
1413
1414err_free_point:
1415	ecc_free_point(pk);
1416out:
1417	return ret;
1418}
1419EXPORT_SYMBOL(ecc_make_pub_key);
1420
1421/* SP800-56A section 5.6.2.3.4 partial verification: ephemeral keys only */
1422int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,
1423				struct ecc_point *pk)
1424{
1425	u64 yy[ECC_MAX_DIGITS], xxx[ECC_MAX_DIGITS], w[ECC_MAX_DIGITS];
1426
1427	if (WARN_ON(pk->ndigits != curve->g.ndigits))
1428		return -EINVAL;
1429
1430	/* Check 1: Verify key is not the zero point. */
1431	if (ecc_point_is_zero(pk))
1432		return -EINVAL;
1433
1434	/* Check 2: Verify key is in the range [1, p-1]. */
1435	if (vli_cmp(curve->p, pk->x, pk->ndigits) != 1)
1436		return -EINVAL;
1437	if (vli_cmp(curve->p, pk->y, pk->ndigits) != 1)
1438		return -EINVAL;
1439
1440	/* Check 3: Verify that y^2 == (x^3 + a·x + b) mod p */
1441	vli_mod_square_fast(yy, pk->y, curve->p, pk->ndigits); /* y^2 */
1442	vli_mod_square_fast(xxx, pk->x, curve->p, pk->ndigits); /* x^2 */
1443	vli_mod_mult_fast(xxx, xxx, pk->x, curve->p, pk->ndigits); /* x^3 */
1444	vli_mod_mult_fast(w, curve->a, pk->x, curve->p, pk->ndigits); /* a·x */
1445	vli_mod_add(w, w, curve->b, curve->p, pk->ndigits); /* a·x + b */
1446	vli_mod_add(w, w, xxx, curve->p, pk->ndigits); /* x^3 + a·x + b */
1447	if (vli_cmp(yy, w, pk->ndigits) != 0) /* Equation */
1448		return -EINVAL;
1449
1450	return 0;
1451}
1452EXPORT_SYMBOL(ecc_is_pubkey_valid_partial);
1453
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1454int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
1455			      const u64 *private_key, const u64 *public_key,
1456			      u64 *secret)
1457{
1458	int ret = 0;
1459	struct ecc_point *product, *pk;
1460	u64 priv[ECC_MAX_DIGITS];
1461	u64 rand_z[ECC_MAX_DIGITS];
1462	unsigned int nbytes;
1463	const struct ecc_curve *curve = ecc_get_curve(curve_id);
1464
1465	if (!private_key || !public_key || !curve ||
1466	    ndigits > ARRAY_SIZE(priv) || ndigits > ARRAY_SIZE(rand_z)) {
1467		ret = -EINVAL;
1468		goto out;
1469	}
1470
1471	nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
1472
1473	get_random_bytes(rand_z, nbytes);
1474
1475	pk = ecc_alloc_point(ndigits);
1476	if (!pk) {
1477		ret = -ENOMEM;
1478		goto out;
1479	}
1480
1481	ecc_swap_digits(public_key, pk->x, ndigits);
1482	ecc_swap_digits(&public_key[ndigits], pk->y, ndigits);
1483	ret = ecc_is_pubkey_valid_partial(curve, pk);
1484	if (ret)
1485		goto err_alloc_product;
1486
1487	ecc_swap_digits(private_key, priv, ndigits);
1488
1489	product = ecc_alloc_point(ndigits);
1490	if (!product) {
1491		ret = -ENOMEM;
1492		goto err_alloc_product;
1493	}
1494
1495	ecc_point_mult(product, pk, priv, rand_z, curve, ndigits);
1496
1497	ecc_swap_digits(product->x, secret, ndigits);
1498
1499	if (ecc_point_is_zero(product))
1500		ret = -EFAULT;
 
 
 
 
1501
 
 
 
1502	ecc_free_point(product);
1503err_alloc_product:
1504	ecc_free_point(pk);
1505out:
1506	return ret;
1507}
1508EXPORT_SYMBOL(crypto_ecdh_shared_secret);
1509
1510MODULE_LICENSE("Dual BSD/GPL");
v5.14.15
   1/*
   2 * Copyright (c) 2013, 2014 Kenneth MacKay. All rights reserved.
   3 * Copyright (c) 2019 Vitaly Chikunov <vt@altlinux.org>
   4 *
   5 * Redistribution and use in source and binary forms, with or without
   6 * modification, are permitted provided that the following conditions are
   7 * met:
   8 *  * Redistributions of source code must retain the above copyright
   9 *   notice, this list of conditions and the following disclaimer.
  10 *  * Redistributions in binary form must reproduce the above copyright
  11 *    notice, this list of conditions and the following disclaimer in the
  12 *    documentation and/or other materials provided with the distribution.
  13 *
  14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25 */
  26
  27#include <crypto/ecc_curve.h>
  28#include <linux/module.h>
  29#include <linux/random.h>
  30#include <linux/slab.h>
  31#include <linux/swab.h>
  32#include <linux/fips.h>
  33#include <crypto/ecdh.h>
  34#include <crypto/rng.h>
  35#include <asm/unaligned.h>
  36#include <linux/ratelimit.h>
  37
  38#include "ecc.h"
  39#include "ecc_curve_defs.h"
  40
  41typedef struct {
  42	u64 m_low;
  43	u64 m_high;
  44} uint128_t;
  45
  46/* Returns curv25519 curve param */
  47const struct ecc_curve *ecc_get_curve25519(void)
  48{
  49	return &ecc_25519;
  50}
  51EXPORT_SYMBOL(ecc_get_curve25519);
  52
  53const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
  54{
  55	switch (curve_id) {
  56	/* In FIPS mode only allow P256 and higher */
  57	case ECC_CURVE_NIST_P192:
  58		return fips_enabled ? NULL : &nist_p192;
  59	case ECC_CURVE_NIST_P256:
  60		return &nist_p256;
  61	case ECC_CURVE_NIST_P384:
  62		return &nist_p384;
  63	default:
  64		return NULL;
  65	}
  66}
  67EXPORT_SYMBOL(ecc_get_curve);
  68
  69static u64 *ecc_alloc_digits_space(unsigned int ndigits)
  70{
  71	size_t len = ndigits * sizeof(u64);
  72
  73	if (!len)
  74		return NULL;
  75
  76	return kmalloc(len, GFP_KERNEL);
  77}
  78
  79static void ecc_free_digits_space(u64 *space)
  80{
  81	kfree_sensitive(space);
  82}
  83
  84static struct ecc_point *ecc_alloc_point(unsigned int ndigits)
  85{
  86	struct ecc_point *p = kmalloc(sizeof(*p), GFP_KERNEL);
  87
  88	if (!p)
  89		return NULL;
  90
  91	p->x = ecc_alloc_digits_space(ndigits);
  92	if (!p->x)
  93		goto err_alloc_x;
  94
  95	p->y = ecc_alloc_digits_space(ndigits);
  96	if (!p->y)
  97		goto err_alloc_y;
  98
  99	p->ndigits = ndigits;
 100
 101	return p;
 102
 103err_alloc_y:
 104	ecc_free_digits_space(p->x);
 105err_alloc_x:
 106	kfree(p);
 107	return NULL;
 108}
 109
 110static void ecc_free_point(struct ecc_point *p)
 111{
 112	if (!p)
 113		return;
 114
 115	kfree_sensitive(p->x);
 116	kfree_sensitive(p->y);
 117	kfree_sensitive(p);
 118}
 119
 120static void vli_clear(u64 *vli, unsigned int ndigits)
 121{
 122	int i;
 123
 124	for (i = 0; i < ndigits; i++)
 125		vli[i] = 0;
 126}
 127
 128/* Returns true if vli == 0, false otherwise. */
 129bool vli_is_zero(const u64 *vli, unsigned int ndigits)
 130{
 131	int i;
 132
 133	for (i = 0; i < ndigits; i++) {
 134		if (vli[i])
 135			return false;
 136	}
 137
 138	return true;
 139}
 140EXPORT_SYMBOL(vli_is_zero);
 141
 142/* Returns nonzero if bit of vli is set. */
 143static u64 vli_test_bit(const u64 *vli, unsigned int bit)
 144{
 145	return (vli[bit / 64] & ((u64)1 << (bit % 64)));
 146}
 147
 148static bool vli_is_negative(const u64 *vli, unsigned int ndigits)
 149{
 150	return vli_test_bit(vli, ndigits * 64 - 1);
 151}
 152
 153/* Counts the number of 64-bit "digits" in vli. */
 154static unsigned int vli_num_digits(const u64 *vli, unsigned int ndigits)
 155{
 156	int i;
 157
 158	/* Search from the end until we find a non-zero digit.
 159	 * We do it in reverse because we expect that most digits will
 160	 * be nonzero.
 161	 */
 162	for (i = ndigits - 1; i >= 0 && vli[i] == 0; i--);
 163
 164	return (i + 1);
 165}
 166
 167/* Counts the number of bits required for vli. */
 168static unsigned int vli_num_bits(const u64 *vli, unsigned int ndigits)
 169{
 170	unsigned int i, num_digits;
 171	u64 digit;
 172
 173	num_digits = vli_num_digits(vli, ndigits);
 174	if (num_digits == 0)
 175		return 0;
 176
 177	digit = vli[num_digits - 1];
 178	for (i = 0; digit; i++)
 179		digit >>= 1;
 180
 181	return ((num_digits - 1) * 64 + i);
 182}
 183
 184/* Set dest from unaligned bit string src. */
 185void vli_from_be64(u64 *dest, const void *src, unsigned int ndigits)
 186{
 187	int i;
 188	const u64 *from = src;
 189
 190	for (i = 0; i < ndigits; i++)
 191		dest[i] = get_unaligned_be64(&from[ndigits - 1 - i]);
 192}
 193EXPORT_SYMBOL(vli_from_be64);
 194
 195void vli_from_le64(u64 *dest, const void *src, unsigned int ndigits)
 196{
 197	int i;
 198	const u64 *from = src;
 199
 200	for (i = 0; i < ndigits; i++)
 201		dest[i] = get_unaligned_le64(&from[i]);
 202}
 203EXPORT_SYMBOL(vli_from_le64);
 204
 205/* Sets dest = src. */
 206static void vli_set(u64 *dest, const u64 *src, unsigned int ndigits)
 207{
 208	int i;
 209
 210	for (i = 0; i < ndigits; i++)
 211		dest[i] = src[i];
 212}
 213
 214/* Returns sign of left - right. */
 215int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits)
 216{
 217	int i;
 218
 219	for (i = ndigits - 1; i >= 0; i--) {
 220		if (left[i] > right[i])
 221			return 1;
 222		else if (left[i] < right[i])
 223			return -1;
 224	}
 225
 226	return 0;
 227}
 228EXPORT_SYMBOL(vli_cmp);
 229
 230/* Computes result = in << c, returning carry. Can modify in place
 231 * (if result == in). 0 < shift < 64.
 232 */
 233static u64 vli_lshift(u64 *result, const u64 *in, unsigned int shift,
 234		      unsigned int ndigits)
 235{
 236	u64 carry = 0;
 237	int i;
 238
 239	for (i = 0; i < ndigits; i++) {
 240		u64 temp = in[i];
 241
 242		result[i] = (temp << shift) | carry;
 243		carry = temp >> (64 - shift);
 244	}
 245
 246	return carry;
 247}
 248
 249/* Computes vli = vli >> 1. */
 250static void vli_rshift1(u64 *vli, unsigned int ndigits)
 251{
 252	u64 *end = vli;
 253	u64 carry = 0;
 254
 255	vli += ndigits;
 256
 257	while (vli-- > end) {
 258		u64 temp = *vli;
 259		*vli = (temp >> 1) | carry;
 260		carry = temp << 63;
 261	}
 262}
 263
 264/* Computes result = left + right, returning carry. Can modify in place. */
 265static u64 vli_add(u64 *result, const u64 *left, const u64 *right,
 266		   unsigned int ndigits)
 267{
 268	u64 carry = 0;
 269	int i;
 270
 271	for (i = 0; i < ndigits; i++) {
 272		u64 sum;
 273
 274		sum = left[i] + right[i] + carry;
 275		if (sum != left[i])
 276			carry = (sum < left[i]);
 277
 278		result[i] = sum;
 279	}
 280
 281	return carry;
 282}
 283
 284/* Computes result = left + right, returning carry. Can modify in place. */
 285static u64 vli_uadd(u64 *result, const u64 *left, u64 right,
 286		    unsigned int ndigits)
 287{
 288	u64 carry = right;
 289	int i;
 290
 291	for (i = 0; i < ndigits; i++) {
 292		u64 sum;
 293
 294		sum = left[i] + carry;
 295		if (sum != left[i])
 296			carry = (sum < left[i]);
 297		else
 298			carry = !!carry;
 299
 300		result[i] = sum;
 301	}
 302
 303	return carry;
 304}
 305
 306/* Computes result = left - right, returning borrow. Can modify in place. */
 307u64 vli_sub(u64 *result, const u64 *left, const u64 *right,
 308		   unsigned int ndigits)
 309{
 310	u64 borrow = 0;
 311	int i;
 312
 313	for (i = 0; i < ndigits; i++) {
 314		u64 diff;
 315
 316		diff = left[i] - right[i] - borrow;
 317		if (diff != left[i])
 318			borrow = (diff > left[i]);
 319
 320		result[i] = diff;
 321	}
 322
 323	return borrow;
 324}
 325EXPORT_SYMBOL(vli_sub);
 326
 327/* Computes result = left - right, returning borrow. Can modify in place. */
 328static u64 vli_usub(u64 *result, const u64 *left, u64 right,
 329	     unsigned int ndigits)
 330{
 331	u64 borrow = right;
 332	int i;
 333
 334	for (i = 0; i < ndigits; i++) {
 335		u64 diff;
 336
 337		diff = left[i] - borrow;
 338		if (diff != left[i])
 339			borrow = (diff > left[i]);
 340
 341		result[i] = diff;
 342	}
 343
 344	return borrow;
 345}
 346
 347static uint128_t mul_64_64(u64 left, u64 right)
 348{
 349	uint128_t result;
 350#if defined(CONFIG_ARCH_SUPPORTS_INT128)
 351	unsigned __int128 m = (unsigned __int128)left * right;
 352
 353	result.m_low  = m;
 354	result.m_high = m >> 64;
 355#else
 356	u64 a0 = left & 0xffffffffull;
 357	u64 a1 = left >> 32;
 358	u64 b0 = right & 0xffffffffull;
 359	u64 b1 = right >> 32;
 360	u64 m0 = a0 * b0;
 361	u64 m1 = a0 * b1;
 362	u64 m2 = a1 * b0;
 363	u64 m3 = a1 * b1;
 364
 365	m2 += (m0 >> 32);
 366	m2 += m1;
 367
 368	/* Overflow */
 369	if (m2 < m1)
 370		m3 += 0x100000000ull;
 371
 372	result.m_low = (m0 & 0xffffffffull) | (m2 << 32);
 373	result.m_high = m3 + (m2 >> 32);
 374#endif
 375	return result;
 376}
 377
 378static uint128_t add_128_128(uint128_t a, uint128_t b)
 379{
 380	uint128_t result;
 381
 382	result.m_low = a.m_low + b.m_low;
 383	result.m_high = a.m_high + b.m_high + (result.m_low < a.m_low);
 384
 385	return result;
 386}
 387
 388static void vli_mult(u64 *result, const u64 *left, const u64 *right,
 389		     unsigned int ndigits)
 390{
 391	uint128_t r01 = { 0, 0 };
 392	u64 r2 = 0;
 393	unsigned int i, k;
 394
 395	/* Compute each digit of result in sequence, maintaining the
 396	 * carries.
 397	 */
 398	for (k = 0; k < ndigits * 2 - 1; k++) {
 399		unsigned int min;
 400
 401		if (k < ndigits)
 402			min = 0;
 403		else
 404			min = (k + 1) - ndigits;
 405
 406		for (i = min; i <= k && i < ndigits; i++) {
 407			uint128_t product;
 408
 409			product = mul_64_64(left[i], right[k - i]);
 410
 411			r01 = add_128_128(r01, product);
 412			r2 += (r01.m_high < product.m_high);
 413		}
 414
 415		result[k] = r01.m_low;
 416		r01.m_low = r01.m_high;
 417		r01.m_high = r2;
 418		r2 = 0;
 419	}
 420
 421	result[ndigits * 2 - 1] = r01.m_low;
 422}
 423
 424/* Compute product = left * right, for a small right value. */
 425static void vli_umult(u64 *result, const u64 *left, u32 right,
 426		      unsigned int ndigits)
 427{
 428	uint128_t r01 = { 0 };
 429	unsigned int k;
 430
 431	for (k = 0; k < ndigits; k++) {
 432		uint128_t product;
 433
 434		product = mul_64_64(left[k], right);
 435		r01 = add_128_128(r01, product);
 436		/* no carry */
 437		result[k] = r01.m_low;
 438		r01.m_low = r01.m_high;
 439		r01.m_high = 0;
 440	}
 441	result[k] = r01.m_low;
 442	for (++k; k < ndigits * 2; k++)
 443		result[k] = 0;
 444}
 445
 446static void vli_square(u64 *result, const u64 *left, unsigned int ndigits)
 447{
 448	uint128_t r01 = { 0, 0 };
 449	u64 r2 = 0;
 450	int i, k;
 451
 452	for (k = 0; k < ndigits * 2 - 1; k++) {
 453		unsigned int min;
 454
 455		if (k < ndigits)
 456			min = 0;
 457		else
 458			min = (k + 1) - ndigits;
 459
 460		for (i = min; i <= k && i <= k - i; i++) {
 461			uint128_t product;
 462
 463			product = mul_64_64(left[i], left[k - i]);
 464
 465			if (i < k - i) {
 466				r2 += product.m_high >> 63;
 467				product.m_high = (product.m_high << 1) |
 468						 (product.m_low >> 63);
 469				product.m_low <<= 1;
 470			}
 471
 472			r01 = add_128_128(r01, product);
 473			r2 += (r01.m_high < product.m_high);
 474		}
 475
 476		result[k] = r01.m_low;
 477		r01.m_low = r01.m_high;
 478		r01.m_high = r2;
 479		r2 = 0;
 480	}
 481
 482	result[ndigits * 2 - 1] = r01.m_low;
 483}
 484
 485/* Computes result = (left + right) % mod.
 486 * Assumes that left < mod and right < mod, result != mod.
 487 */
 488static void vli_mod_add(u64 *result, const u64 *left, const u64 *right,
 489			const u64 *mod, unsigned int ndigits)
 490{
 491	u64 carry;
 492
 493	carry = vli_add(result, left, right, ndigits);
 494
 495	/* result > mod (result = mod + remainder), so subtract mod to
 496	 * get remainder.
 497	 */
 498	if (carry || vli_cmp(result, mod, ndigits) >= 0)
 499		vli_sub(result, result, mod, ndigits);
 500}
 501
 502/* Computes result = (left - right) % mod.
 503 * Assumes that left < mod and right < mod, result != mod.
 504 */
 505static void vli_mod_sub(u64 *result, const u64 *left, const u64 *right,
 506			const u64 *mod, unsigned int ndigits)
 507{
 508	u64 borrow = vli_sub(result, left, right, ndigits);
 509
 510	/* In this case, p_result == -diff == (max int) - diff.
 511	 * Since -x % d == d - x, we can get the correct result from
 512	 * result + mod (with overflow).
 513	 */
 514	if (borrow)
 515		vli_add(result, result, mod, ndigits);
 516}
 517
 518/*
 519 * Computes result = product % mod
 520 * for special form moduli: p = 2^k-c, for small c (note the minus sign)
 521 *
 522 * References:
 523 * R. Crandall, C. Pomerance. Prime Numbers: A Computational Perspective.
 524 * 9 Fast Algorithms for Large-Integer Arithmetic. 9.2.3 Moduli of special form
 525 * Algorithm 9.2.13 (Fast mod operation for special-form moduli).
 526 */
 527static void vli_mmod_special(u64 *result, const u64 *product,
 528			      const u64 *mod, unsigned int ndigits)
 529{
 530	u64 c = -mod[0];
 531	u64 t[ECC_MAX_DIGITS * 2];
 532	u64 r[ECC_MAX_DIGITS * 2];
 533
 534	vli_set(r, product, ndigits * 2);
 535	while (!vli_is_zero(r + ndigits, ndigits)) {
 536		vli_umult(t, r + ndigits, c, ndigits);
 537		vli_clear(r + ndigits, ndigits);
 538		vli_add(r, r, t, ndigits * 2);
 539	}
 540	vli_set(t, mod, ndigits);
 541	vli_clear(t + ndigits, ndigits);
 542	while (vli_cmp(r, t, ndigits * 2) >= 0)
 543		vli_sub(r, r, t, ndigits * 2);
 544	vli_set(result, r, ndigits);
 545}
 546
 547/*
 548 * Computes result = product % mod
 549 * for special form moduli: p = 2^{k-1}+c, for small c (note the plus sign)
 550 * where k-1 does not fit into qword boundary by -1 bit (such as 255).
 551
 552 * References (loosely based on):
 553 * A. Menezes, P. van Oorschot, S. Vanstone. Handbook of Applied Cryptography.
 554 * 14.3.4 Reduction methods for moduli of special form. Algorithm 14.47.
 555 * URL: http://cacr.uwaterloo.ca/hac/about/chap14.pdf
 556 *
 557 * H. Cohen, G. Frey, R. Avanzi, C. Doche, T. Lange, K. Nguyen, F. Vercauteren.
 558 * Handbook of Elliptic and Hyperelliptic Curve Cryptography.
 559 * Algorithm 10.25 Fast reduction for special form moduli
 560 */
 561static void vli_mmod_special2(u64 *result, const u64 *product,
 562			       const u64 *mod, unsigned int ndigits)
 563{
 564	u64 c2 = mod[0] * 2;
 565	u64 q[ECC_MAX_DIGITS];
 566	u64 r[ECC_MAX_DIGITS * 2];
 567	u64 m[ECC_MAX_DIGITS * 2]; /* expanded mod */
 568	int carry; /* last bit that doesn't fit into q */
 569	int i;
 570
 571	vli_set(m, mod, ndigits);
 572	vli_clear(m + ndigits, ndigits);
 573
 574	vli_set(r, product, ndigits);
 575	/* q and carry are top bits */
 576	vli_set(q, product + ndigits, ndigits);
 577	vli_clear(r + ndigits, ndigits);
 578	carry = vli_is_negative(r, ndigits);
 579	if (carry)
 580		r[ndigits - 1] &= (1ull << 63) - 1;
 581	for (i = 1; carry || !vli_is_zero(q, ndigits); i++) {
 582		u64 qc[ECC_MAX_DIGITS * 2];
 583
 584		vli_umult(qc, q, c2, ndigits);
 585		if (carry)
 586			vli_uadd(qc, qc, mod[0], ndigits * 2);
 587		vli_set(q, qc + ndigits, ndigits);
 588		vli_clear(qc + ndigits, ndigits);
 589		carry = vli_is_negative(qc, ndigits);
 590		if (carry)
 591			qc[ndigits - 1] &= (1ull << 63) - 1;
 592		if (i & 1)
 593			vli_sub(r, r, qc, ndigits * 2);
 594		else
 595			vli_add(r, r, qc, ndigits * 2);
 596	}
 597	while (vli_is_negative(r, ndigits * 2))
 598		vli_add(r, r, m, ndigits * 2);
 599	while (vli_cmp(r, m, ndigits * 2) >= 0)
 600		vli_sub(r, r, m, ndigits * 2);
 601
 602	vli_set(result, r, ndigits);
 603}
 604
 605/*
 606 * Computes result = product % mod, where product is 2N words long.
 607 * Reference: Ken MacKay's micro-ecc.
 608 * Currently only designed to work for curve_p or curve_n.
 609 */
 610static void vli_mmod_slow(u64 *result, u64 *product, const u64 *mod,
 611			  unsigned int ndigits)
 612{
 613	u64 mod_m[2 * ECC_MAX_DIGITS];
 614	u64 tmp[2 * ECC_MAX_DIGITS];
 615	u64 *v[2] = { tmp, product };
 616	u64 carry = 0;
 617	unsigned int i;
 618	/* Shift mod so its highest set bit is at the maximum position. */
 619	int shift = (ndigits * 2 * 64) - vli_num_bits(mod, ndigits);
 620	int word_shift = shift / 64;
 621	int bit_shift = shift % 64;
 622
 623	vli_clear(mod_m, word_shift);
 624	if (bit_shift > 0) {
 625		for (i = 0; i < ndigits; ++i) {
 626			mod_m[word_shift + i] = (mod[i] << bit_shift) | carry;
 627			carry = mod[i] >> (64 - bit_shift);
 628		}
 629	} else
 630		vli_set(mod_m + word_shift, mod, ndigits);
 631
 632	for (i = 1; shift >= 0; --shift) {
 633		u64 borrow = 0;
 634		unsigned int j;
 635
 636		for (j = 0; j < ndigits * 2; ++j) {
 637			u64 diff = v[i][j] - mod_m[j] - borrow;
 638
 639			if (diff != v[i][j])
 640				borrow = (diff > v[i][j]);
 641			v[1 - i][j] = diff;
 642		}
 643		i = !(i ^ borrow); /* Swap the index if there was no borrow */
 644		vli_rshift1(mod_m, ndigits);
 645		mod_m[ndigits - 1] |= mod_m[ndigits] << (64 - 1);
 646		vli_rshift1(mod_m + ndigits, ndigits);
 647	}
 648	vli_set(result, v[i], ndigits);
 649}
 650
 651/* Computes result = product % mod using Barrett's reduction with precomputed
 652 * value mu appended to the mod after ndigits, mu = (2^{2w} / mod) and have
 653 * length ndigits + 1, where mu * (2^w - 1) should not overflow ndigits
 654 * boundary.
 655 *
 656 * Reference:
 657 * R. Brent, P. Zimmermann. Modern Computer Arithmetic. 2010.
 658 * 2.4.1 Barrett's algorithm. Algorithm 2.5.
 659 */
 660static void vli_mmod_barrett(u64 *result, u64 *product, const u64 *mod,
 661			     unsigned int ndigits)
 662{
 663	u64 q[ECC_MAX_DIGITS * 2];
 664	u64 r[ECC_MAX_DIGITS * 2];
 665	const u64 *mu = mod + ndigits;
 666
 667	vli_mult(q, product + ndigits, mu, ndigits);
 668	if (mu[ndigits])
 669		vli_add(q + ndigits, q + ndigits, product + ndigits, ndigits);
 670	vli_mult(r, mod, q + ndigits, ndigits);
 671	vli_sub(r, product, r, ndigits * 2);
 672	while (!vli_is_zero(r + ndigits, ndigits) ||
 673	       vli_cmp(r, mod, ndigits) != -1) {
 674		u64 carry;
 675
 676		carry = vli_sub(r, r, mod, ndigits);
 677		vli_usub(r + ndigits, r + ndigits, carry, ndigits);
 678	}
 679	vli_set(result, r, ndigits);
 680}
 681
 682/* Computes p_result = p_product % curve_p.
 683 * See algorithm 5 and 6 from
 684 * http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf
 685 */
 686static void vli_mmod_fast_192(u64 *result, const u64 *product,
 687			      const u64 *curve_prime, u64 *tmp)
 688{
 689	const unsigned int ndigits = 3;
 690	int carry;
 691
 692	vli_set(result, product, ndigits);
 693
 694	vli_set(tmp, &product[3], ndigits);
 695	carry = vli_add(result, result, tmp, ndigits);
 696
 697	tmp[0] = 0;
 698	tmp[1] = product[3];
 699	tmp[2] = product[4];
 700	carry += vli_add(result, result, tmp, ndigits);
 701
 702	tmp[0] = tmp[1] = product[5];
 703	tmp[2] = 0;
 704	carry += vli_add(result, result, tmp, ndigits);
 705
 706	while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
 707		carry -= vli_sub(result, result, curve_prime, ndigits);
 708}
 709
 710/* Computes result = product % curve_prime
 711 * from http://www.nsa.gov/ia/_files/nist-routines.pdf
 712 */
 713static void vli_mmod_fast_256(u64 *result, const u64 *product,
 714			      const u64 *curve_prime, u64 *tmp)
 715{
 716	int carry;
 717	const unsigned int ndigits = 4;
 718
 719	/* t */
 720	vli_set(result, product, ndigits);
 721
 722	/* s1 */
 723	tmp[0] = 0;
 724	tmp[1] = product[5] & 0xffffffff00000000ull;
 725	tmp[2] = product[6];
 726	tmp[3] = product[7];
 727	carry = vli_lshift(tmp, tmp, 1, ndigits);
 728	carry += vli_add(result, result, tmp, ndigits);
 729
 730	/* s2 */
 731	tmp[1] = product[6] << 32;
 732	tmp[2] = (product[6] >> 32) | (product[7] << 32);
 733	tmp[3] = product[7] >> 32;
 734	carry += vli_lshift(tmp, tmp, 1, ndigits);
 735	carry += vli_add(result, result, tmp, ndigits);
 736
 737	/* s3 */
 738	tmp[0] = product[4];
 739	tmp[1] = product[5] & 0xffffffff;
 740	tmp[2] = 0;
 741	tmp[3] = product[7];
 742	carry += vli_add(result, result, tmp, ndigits);
 743
 744	/* s4 */
 745	tmp[0] = (product[4] >> 32) | (product[5] << 32);
 746	tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull);
 747	tmp[2] = product[7];
 748	tmp[3] = (product[6] >> 32) | (product[4] << 32);
 749	carry += vli_add(result, result, tmp, ndigits);
 750
 751	/* d1 */
 752	tmp[0] = (product[5] >> 32) | (product[6] << 32);
 753	tmp[1] = (product[6] >> 32);
 754	tmp[2] = 0;
 755	tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32);
 756	carry -= vli_sub(result, result, tmp, ndigits);
 757
 758	/* d2 */
 759	tmp[0] = product[6];
 760	tmp[1] = product[7];
 761	tmp[2] = 0;
 762	tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull);
 763	carry -= vli_sub(result, result, tmp, ndigits);
 764
 765	/* d3 */
 766	tmp[0] = (product[6] >> 32) | (product[7] << 32);
 767	tmp[1] = (product[7] >> 32) | (product[4] << 32);
 768	tmp[2] = (product[4] >> 32) | (product[5] << 32);
 769	tmp[3] = (product[6] << 32);
 770	carry -= vli_sub(result, result, tmp, ndigits);
 771
 772	/* d4 */
 773	tmp[0] = product[7];
 774	tmp[1] = product[4] & 0xffffffff00000000ull;
 775	tmp[2] = product[5];
 776	tmp[3] = product[6] & 0xffffffff00000000ull;
 777	carry -= vli_sub(result, result, tmp, ndigits);
 778
 779	if (carry < 0) {
 780		do {
 781			carry += vli_add(result, result, curve_prime, ndigits);
 782		} while (carry < 0);
 783	} else {
 784		while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
 785			carry -= vli_sub(result, result, curve_prime, ndigits);
 786	}
 787}
 788
 789#define SL32OR32(x32, y32) (((u64)x32 << 32) | y32)
 790#define AND64H(x64)  (x64 & 0xffFFffFF00000000ull)
 791#define AND64L(x64)  (x64 & 0x00000000ffFFffFFull)
 792
 793/* Computes result = product % curve_prime
 794 * from "Mathematical routines for the NIST prime elliptic curves"
 795 */
 796static void vli_mmod_fast_384(u64 *result, const u64 *product,
 797				const u64 *curve_prime, u64 *tmp)
 798{
 799	int carry;
 800	const unsigned int ndigits = 6;
 801
 802	/* t */
 803	vli_set(result, product, ndigits);
 804
 805	/* s1 */
 806	tmp[0] = 0;		// 0 || 0
 807	tmp[1] = 0;		// 0 || 0
 808	tmp[2] = SL32OR32(product[11], (product[10]>>32));	//a22||a21
 809	tmp[3] = product[11]>>32;	// 0 ||a23
 810	tmp[4] = 0;		// 0 || 0
 811	tmp[5] = 0;		// 0 || 0
 812	carry = vli_lshift(tmp, tmp, 1, ndigits);
 813	carry += vli_add(result, result, tmp, ndigits);
 814
 815	/* s2 */
 816	tmp[0] = product[6];	//a13||a12
 817	tmp[1] = product[7];	//a15||a14
 818	tmp[2] = product[8];	//a17||a16
 819	tmp[3] = product[9];	//a19||a18
 820	tmp[4] = product[10];	//a21||a20
 821	tmp[5] = product[11];	//a23||a22
 822	carry += vli_add(result, result, tmp, ndigits);
 823
 824	/* s3 */
 825	tmp[0] = SL32OR32(product[11], (product[10]>>32));	//a22||a21
 826	tmp[1] = SL32OR32(product[6], (product[11]>>32));	//a12||a23
 827	tmp[2] = SL32OR32(product[7], (product[6])>>32);	//a14||a13
 828	tmp[3] = SL32OR32(product[8], (product[7]>>32));	//a16||a15
 829	tmp[4] = SL32OR32(product[9], (product[8]>>32));	//a18||a17
 830	tmp[5] = SL32OR32(product[10], (product[9]>>32));	//a20||a19
 831	carry += vli_add(result, result, tmp, ndigits);
 832
 833	/* s4 */
 834	tmp[0] = AND64H(product[11]);	//a23|| 0
 835	tmp[1] = (product[10]<<32);	//a20|| 0
 836	tmp[2] = product[6];	//a13||a12
 837	tmp[3] = product[7];	//a15||a14
 838	tmp[4] = product[8];	//a17||a16
 839	tmp[5] = product[9];	//a19||a18
 840	carry += vli_add(result, result, tmp, ndigits);
 841
 842	/* s5 */
 843	tmp[0] = 0;		//  0|| 0
 844	tmp[1] = 0;		//  0|| 0
 845	tmp[2] = product[10];	//a21||a20
 846	tmp[3] = product[11];	//a23||a22
 847	tmp[4] = 0;		//  0|| 0
 848	tmp[5] = 0;		//  0|| 0
 849	carry += vli_add(result, result, tmp, ndigits);
 850
 851	/* s6 */
 852	tmp[0] = AND64L(product[10]);	// 0 ||a20
 853	tmp[1] = AND64H(product[10]);	//a21|| 0
 854	tmp[2] = product[11];	//a23||a22
 855	tmp[3] = 0;		// 0 || 0
 856	tmp[4] = 0;		// 0 || 0
 857	tmp[5] = 0;		// 0 || 0
 858	carry += vli_add(result, result, tmp, ndigits);
 859
 860	/* d1 */
 861	tmp[0] = SL32OR32(product[6], (product[11]>>32));	//a12||a23
 862	tmp[1] = SL32OR32(product[7], (product[6]>>32));	//a14||a13
 863	tmp[2] = SL32OR32(product[8], (product[7]>>32));	//a16||a15
 864	tmp[3] = SL32OR32(product[9], (product[8]>>32));	//a18||a17
 865	tmp[4] = SL32OR32(product[10], (product[9]>>32));	//a20||a19
 866	tmp[5] = SL32OR32(product[11], (product[10]>>32));	//a22||a21
 867	carry -= vli_sub(result, result, tmp, ndigits);
 868
 869	/* d2 */
 870	tmp[0] = (product[10]<<32);	//a20|| 0
 871	tmp[1] = SL32OR32(product[11], (product[10]>>32));	//a22||a21
 872	tmp[2] = (product[11]>>32);	// 0 ||a23
 873	tmp[3] = 0;		// 0 || 0
 874	tmp[4] = 0;		// 0 || 0
 875	tmp[5] = 0;		// 0 || 0
 876	carry -= vli_sub(result, result, tmp, ndigits);
 877
 878	/* d3 */
 879	tmp[0] = 0;		// 0 || 0
 880	tmp[1] = AND64H(product[11]);	//a23|| 0
 881	tmp[2] = product[11]>>32;	// 0 ||a23
 882	tmp[3] = 0;		// 0 || 0
 883	tmp[4] = 0;		// 0 || 0
 884	tmp[5] = 0;		// 0 || 0
 885	carry -= vli_sub(result, result, tmp, ndigits);
 886
 887	if (carry < 0) {
 888		do {
 889			carry += vli_add(result, result, curve_prime, ndigits);
 890		} while (carry < 0);
 891	} else {
 892		while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
 893			carry -= vli_sub(result, result, curve_prime, ndigits);
 894	}
 895
 896}
 897
 898#undef SL32OR32
 899#undef AND64H
 900#undef AND64L
 901
 902/* Computes result = product % curve_prime for different curve_primes.
 903 *
 904 * Note that curve_primes are distinguished just by heuristic check and
 905 * not by complete conformance check.
 906 */
 907static bool vli_mmod_fast(u64 *result, u64 *product,
 908			  const struct ecc_curve *curve)
 909{
 910	u64 tmp[2 * ECC_MAX_DIGITS];
 911	const u64 *curve_prime = curve->p;
 912	const unsigned int ndigits = curve->g.ndigits;
 913
 914	/* All NIST curves have name prefix 'nist_' */
 915	if (strncmp(curve->name, "nist_", 5) != 0) {
 916		/* Try to handle Pseudo-Marsenne primes. */
 917		if (curve_prime[ndigits - 1] == -1ull) {
 918			vli_mmod_special(result, product, curve_prime,
 919					 ndigits);
 920			return true;
 921		} else if (curve_prime[ndigits - 1] == 1ull << 63 &&
 922			   curve_prime[ndigits - 2] == 0) {
 923			vli_mmod_special2(result, product, curve_prime,
 924					  ndigits);
 925			return true;
 926		}
 927		vli_mmod_barrett(result, product, curve_prime, ndigits);
 928		return true;
 929	}
 930
 931	switch (ndigits) {
 932	case 3:
 933		vli_mmod_fast_192(result, product, curve_prime, tmp);
 934		break;
 935	case 4:
 936		vli_mmod_fast_256(result, product, curve_prime, tmp);
 937		break;
 938	case 6:
 939		vli_mmod_fast_384(result, product, curve_prime, tmp);
 940		break;
 941	default:
 942		pr_err_ratelimited("ecc: unsupported digits size!\n");
 943		return false;
 944	}
 945
 946	return true;
 947}
 948
 949/* Computes result = (left * right) % mod.
 950 * Assumes that mod is big enough curve order.
 951 */
 952void vli_mod_mult_slow(u64 *result, const u64 *left, const u64 *right,
 953		       const u64 *mod, unsigned int ndigits)
 954{
 955	u64 product[ECC_MAX_DIGITS * 2];
 956
 957	vli_mult(product, left, right, ndigits);
 958	vli_mmod_slow(result, product, mod, ndigits);
 959}
 960EXPORT_SYMBOL(vli_mod_mult_slow);
 961
 962/* Computes result = (left * right) % curve_prime. */
 963static void vli_mod_mult_fast(u64 *result, const u64 *left, const u64 *right,
 964			      const struct ecc_curve *curve)
 965{
 966	u64 product[2 * ECC_MAX_DIGITS];
 967
 968	vli_mult(product, left, right, curve->g.ndigits);
 969	vli_mmod_fast(result, product, curve);
 970}
 971
 972/* Computes result = left^2 % curve_prime. */
 973static void vli_mod_square_fast(u64 *result, const u64 *left,
 974				const struct ecc_curve *curve)
 975{
 976	u64 product[2 * ECC_MAX_DIGITS];
 977
 978	vli_square(product, left, curve->g.ndigits);
 979	vli_mmod_fast(result, product, curve);
 980}
 981
 982#define EVEN(vli) (!(vli[0] & 1))
 983/* Computes result = (1 / p_input) % mod. All VLIs are the same size.
 984 * See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
 985 * https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf
 986 */
 987void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,
 988			unsigned int ndigits)
 989{
 990	u64 a[ECC_MAX_DIGITS], b[ECC_MAX_DIGITS];
 991	u64 u[ECC_MAX_DIGITS], v[ECC_MAX_DIGITS];
 992	u64 carry;
 993	int cmp_result;
 994
 995	if (vli_is_zero(input, ndigits)) {
 996		vli_clear(result, ndigits);
 997		return;
 998	}
 999
1000	vli_set(a, input, ndigits);
1001	vli_set(b, mod, ndigits);
1002	vli_clear(u, ndigits);
1003	u[0] = 1;
1004	vli_clear(v, ndigits);
1005
1006	while ((cmp_result = vli_cmp(a, b, ndigits)) != 0) {
1007		carry = 0;
1008
1009		if (EVEN(a)) {
1010			vli_rshift1(a, ndigits);
1011
1012			if (!EVEN(u))
1013				carry = vli_add(u, u, mod, ndigits);
1014
1015			vli_rshift1(u, ndigits);
1016			if (carry)
1017				u[ndigits - 1] |= 0x8000000000000000ull;
1018		} else if (EVEN(b)) {
1019			vli_rshift1(b, ndigits);
1020
1021			if (!EVEN(v))
1022				carry = vli_add(v, v, mod, ndigits);
1023
1024			vli_rshift1(v, ndigits);
1025			if (carry)
1026				v[ndigits - 1] |= 0x8000000000000000ull;
1027		} else if (cmp_result > 0) {
1028			vli_sub(a, a, b, ndigits);
1029			vli_rshift1(a, ndigits);
1030
1031			if (vli_cmp(u, v, ndigits) < 0)
1032				vli_add(u, u, mod, ndigits);
1033
1034			vli_sub(u, u, v, ndigits);
1035			if (!EVEN(u))
1036				carry = vli_add(u, u, mod, ndigits);
1037
1038			vli_rshift1(u, ndigits);
1039			if (carry)
1040				u[ndigits - 1] |= 0x8000000000000000ull;
1041		} else {
1042			vli_sub(b, b, a, ndigits);
1043			vli_rshift1(b, ndigits);
1044
1045			if (vli_cmp(v, u, ndigits) < 0)
1046				vli_add(v, v, mod, ndigits);
1047
1048			vli_sub(v, v, u, ndigits);
1049			if (!EVEN(v))
1050				carry = vli_add(v, v, mod, ndigits);
1051
1052			vli_rshift1(v, ndigits);
1053			if (carry)
1054				v[ndigits - 1] |= 0x8000000000000000ull;
1055		}
1056	}
1057
1058	vli_set(result, u, ndigits);
1059}
1060EXPORT_SYMBOL(vli_mod_inv);
1061
1062/* ------ Point operations ------ */
1063
1064/* Returns true if p_point is the point at infinity, false otherwise. */
1065static bool ecc_point_is_zero(const struct ecc_point *point)
1066{
1067	return (vli_is_zero(point->x, point->ndigits) &&
1068		vli_is_zero(point->y, point->ndigits));
1069}
1070
1071/* Point multiplication algorithm using Montgomery's ladder with co-Z
1072 * coordinates. From https://eprint.iacr.org/2011/338.pdf
1073 */
1074
1075/* Double in place */
1076static void ecc_point_double_jacobian(u64 *x1, u64 *y1, u64 *z1,
1077					const struct ecc_curve *curve)
1078{
1079	/* t1 = x, t2 = y, t3 = z */
1080	u64 t4[ECC_MAX_DIGITS];
1081	u64 t5[ECC_MAX_DIGITS];
1082	const u64 *curve_prime = curve->p;
1083	const unsigned int ndigits = curve->g.ndigits;
1084
1085	if (vli_is_zero(z1, ndigits))
1086		return;
1087
1088	/* t4 = y1^2 */
1089	vli_mod_square_fast(t4, y1, curve);
1090	/* t5 = x1*y1^2 = A */
1091	vli_mod_mult_fast(t5, x1, t4, curve);
1092	/* t4 = y1^4 */
1093	vli_mod_square_fast(t4, t4, curve);
1094	/* t2 = y1*z1 = z3 */
1095	vli_mod_mult_fast(y1, y1, z1, curve);
1096	/* t3 = z1^2 */
1097	vli_mod_square_fast(z1, z1, curve);
1098
1099	/* t1 = x1 + z1^2 */
1100	vli_mod_add(x1, x1, z1, curve_prime, ndigits);
1101	/* t3 = 2*z1^2 */
1102	vli_mod_add(z1, z1, z1, curve_prime, ndigits);
1103	/* t3 = x1 - z1^2 */
1104	vli_mod_sub(z1, x1, z1, curve_prime, ndigits);
1105	/* t1 = x1^2 - z1^4 */
1106	vli_mod_mult_fast(x1, x1, z1, curve);
1107
1108	/* t3 = 2*(x1^2 - z1^4) */
1109	vli_mod_add(z1, x1, x1, curve_prime, ndigits);
1110	/* t1 = 3*(x1^2 - z1^4) */
1111	vli_mod_add(x1, x1, z1, curve_prime, ndigits);
1112	if (vli_test_bit(x1, 0)) {
1113		u64 carry = vli_add(x1, x1, curve_prime, ndigits);
1114
1115		vli_rshift1(x1, ndigits);
1116		x1[ndigits - 1] |= carry << 63;
1117	} else {
1118		vli_rshift1(x1, ndigits);
1119	}
1120	/* t1 = 3/2*(x1^2 - z1^4) = B */
1121
1122	/* t3 = B^2 */
1123	vli_mod_square_fast(z1, x1, curve);
1124	/* t3 = B^2 - A */
1125	vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
1126	/* t3 = B^2 - 2A = x3 */
1127	vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
1128	/* t5 = A - x3 */
1129	vli_mod_sub(t5, t5, z1, curve_prime, ndigits);
1130	/* t1 = B * (A - x3) */
1131	vli_mod_mult_fast(x1, x1, t5, curve);
1132	/* t4 = B * (A - x3) - y1^4 = y3 */
1133	vli_mod_sub(t4, x1, t4, curve_prime, ndigits);
1134
1135	vli_set(x1, z1, ndigits);
1136	vli_set(z1, y1, ndigits);
1137	vli_set(y1, t4, ndigits);
1138}
1139
1140/* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
1141static void apply_z(u64 *x1, u64 *y1, u64 *z, const struct ecc_curve *curve)
 
1142{
1143	u64 t1[ECC_MAX_DIGITS];
1144
1145	vli_mod_square_fast(t1, z, curve);		/* z^2 */
1146	vli_mod_mult_fast(x1, x1, t1, curve);	/* x1 * z^2 */
1147	vli_mod_mult_fast(t1, t1, z, curve);	/* z^3 */
1148	vli_mod_mult_fast(y1, y1, t1, curve);	/* y1 * z^3 */
1149}
1150
1151/* P = (x1, y1) => 2P, (x2, y2) => P' */
1152static void xycz_initial_double(u64 *x1, u64 *y1, u64 *x2, u64 *y2,
1153				u64 *p_initial_z, const struct ecc_curve *curve)
 
1154{
1155	u64 z[ECC_MAX_DIGITS];
1156	const unsigned int ndigits = curve->g.ndigits;
1157
1158	vli_set(x2, x1, ndigits);
1159	vli_set(y2, y1, ndigits);
1160
1161	vli_clear(z, ndigits);
1162	z[0] = 1;
1163
1164	if (p_initial_z)
1165		vli_set(z, p_initial_z, ndigits);
1166
1167	apply_z(x1, y1, z, curve);
1168
1169	ecc_point_double_jacobian(x1, y1, z, curve);
1170
1171	apply_z(x2, y2, z, curve);
1172}
1173
1174/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
1175 * Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
1176 * or P => P', Q => P + Q
1177 */
1178static void xycz_add(u64 *x1, u64 *y1, u64 *x2, u64 *y2,
1179			const struct ecc_curve *curve)
1180{
1181	/* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
1182	u64 t5[ECC_MAX_DIGITS];
1183	const u64 *curve_prime = curve->p;
1184	const unsigned int ndigits = curve->g.ndigits;
1185
1186	/* t5 = x2 - x1 */
1187	vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
1188	/* t5 = (x2 - x1)^2 = A */
1189	vli_mod_square_fast(t5, t5, curve);
1190	/* t1 = x1*A = B */
1191	vli_mod_mult_fast(x1, x1, t5, curve);
1192	/* t3 = x2*A = C */
1193	vli_mod_mult_fast(x2, x2, t5, curve);
1194	/* t4 = y2 - y1 */
1195	vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
1196	/* t5 = (y2 - y1)^2 = D */
1197	vli_mod_square_fast(t5, y2, curve);
1198
1199	/* t5 = D - B */
1200	vli_mod_sub(t5, t5, x1, curve_prime, ndigits);
1201	/* t5 = D - B - C = x3 */
1202	vli_mod_sub(t5, t5, x2, curve_prime, ndigits);
1203	/* t3 = C - B */
1204	vli_mod_sub(x2, x2, x1, curve_prime, ndigits);
1205	/* t2 = y1*(C - B) */
1206	vli_mod_mult_fast(y1, y1, x2, curve);
1207	/* t3 = B - x3 */
1208	vli_mod_sub(x2, x1, t5, curve_prime, ndigits);
1209	/* t4 = (y2 - y1)*(B - x3) */
1210	vli_mod_mult_fast(y2, y2, x2, curve);
1211	/* t4 = y3 */
1212	vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
1213
1214	vli_set(x2, t5, ndigits);
1215}
1216
1217/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
1218 * Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
1219 * or P => P - Q, Q => P + Q
1220 */
1221static void xycz_add_c(u64 *x1, u64 *y1, u64 *x2, u64 *y2,
1222			const struct ecc_curve *curve)
1223{
1224	/* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
1225	u64 t5[ECC_MAX_DIGITS];
1226	u64 t6[ECC_MAX_DIGITS];
1227	u64 t7[ECC_MAX_DIGITS];
1228	const u64 *curve_prime = curve->p;
1229	const unsigned int ndigits = curve->g.ndigits;
1230
1231	/* t5 = x2 - x1 */
1232	vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
1233	/* t5 = (x2 - x1)^2 = A */
1234	vli_mod_square_fast(t5, t5, curve);
1235	/* t1 = x1*A = B */
1236	vli_mod_mult_fast(x1, x1, t5, curve);
1237	/* t3 = x2*A = C */
1238	vli_mod_mult_fast(x2, x2, t5, curve);
1239	/* t4 = y2 + y1 */
1240	vli_mod_add(t5, y2, y1, curve_prime, ndigits);
1241	/* t4 = y2 - y1 */
1242	vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
1243
1244	/* t6 = C - B */
1245	vli_mod_sub(t6, x2, x1, curve_prime, ndigits);
1246	/* t2 = y1 * (C - B) */
1247	vli_mod_mult_fast(y1, y1, t6, curve);
1248	/* t6 = B + C */
1249	vli_mod_add(t6, x1, x2, curve_prime, ndigits);
1250	/* t3 = (y2 - y1)^2 */
1251	vli_mod_square_fast(x2, y2, curve);
1252	/* t3 = x3 */
1253	vli_mod_sub(x2, x2, t6, curve_prime, ndigits);
1254
1255	/* t7 = B - x3 */
1256	vli_mod_sub(t7, x1, x2, curve_prime, ndigits);
1257	/* t4 = (y2 - y1)*(B - x3) */
1258	vli_mod_mult_fast(y2, y2, t7, curve);
1259	/* t4 = y3 */
1260	vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
1261
1262	/* t7 = (y2 + y1)^2 = F */
1263	vli_mod_square_fast(t7, t5, curve);
1264	/* t7 = x3' */
1265	vli_mod_sub(t7, t7, t6, curve_prime, ndigits);
1266	/* t6 = x3' - B */
1267	vli_mod_sub(t6, t7, x1, curve_prime, ndigits);
1268	/* t6 = (y2 + y1)*(x3' - B) */
1269	vli_mod_mult_fast(t6, t6, t5, curve);
1270	/* t2 = y3' */
1271	vli_mod_sub(y1, t6, y1, curve_prime, ndigits);
1272
1273	vli_set(x1, t7, ndigits);
1274}
1275
1276static void ecc_point_mult(struct ecc_point *result,
1277			   const struct ecc_point *point, const u64 *scalar,
1278			   u64 *initial_z, const struct ecc_curve *curve,
1279			   unsigned int ndigits)
1280{
1281	/* R0 and R1 */
1282	u64 rx[2][ECC_MAX_DIGITS];
1283	u64 ry[2][ECC_MAX_DIGITS];
1284	u64 z[ECC_MAX_DIGITS];
1285	u64 sk[2][ECC_MAX_DIGITS];
1286	u64 *curve_prime = curve->p;
1287	int i, nb;
1288	int num_bits;
1289	int carry;
1290
1291	carry = vli_add(sk[0], scalar, curve->n, ndigits);
1292	vli_add(sk[1], sk[0], curve->n, ndigits);
1293	scalar = sk[!carry];
1294	num_bits = sizeof(u64) * ndigits * 8 + 1;
1295
1296	vli_set(rx[1], point->x, ndigits);
1297	vli_set(ry[1], point->y, ndigits);
1298
1299	xycz_initial_double(rx[1], ry[1], rx[0], ry[0], initial_z, curve);
 
1300
1301	for (i = num_bits - 2; i > 0; i--) {
1302		nb = !vli_test_bit(scalar, i);
1303		xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve);
1304		xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve);
 
 
1305	}
1306
1307	nb = !vli_test_bit(scalar, 0);
1308	xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve);
 
1309
1310	/* Find final 1/Z value. */
1311	/* X1 - X0 */
1312	vli_mod_sub(z, rx[1], rx[0], curve_prime, ndigits);
1313	/* Yb * (X1 - X0) */
1314	vli_mod_mult_fast(z, z, ry[1 - nb], curve);
1315	/* xP * Yb * (X1 - X0) */
1316	vli_mod_mult_fast(z, z, point->x, curve);
1317
1318	/* 1 / (xP * Yb * (X1 - X0)) */
1319	vli_mod_inv(z, z, curve_prime, point->ndigits);
1320
1321	/* yP / (xP * Yb * (X1 - X0)) */
1322	vli_mod_mult_fast(z, z, point->y, curve);
1323	/* Xb * yP / (xP * Yb * (X1 - X0)) */
1324	vli_mod_mult_fast(z, z, rx[1 - nb], curve);
1325	/* End 1/Z calculation */
1326
1327	xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve);
1328
1329	apply_z(rx[0], ry[0], z, curve);
1330
1331	vli_set(result->x, rx[0], ndigits);
1332	vli_set(result->y, ry[0], ndigits);
1333}
1334
1335/* Computes R = P + Q mod p */
1336static void ecc_point_add(const struct ecc_point *result,
1337		   const struct ecc_point *p, const struct ecc_point *q,
1338		   const struct ecc_curve *curve)
1339{
1340	u64 z[ECC_MAX_DIGITS];
1341	u64 px[ECC_MAX_DIGITS];
1342	u64 py[ECC_MAX_DIGITS];
1343	unsigned int ndigits = curve->g.ndigits;
1344
1345	vli_set(result->x, q->x, ndigits);
1346	vli_set(result->y, q->y, ndigits);
1347	vli_mod_sub(z, result->x, p->x, curve->p, ndigits);
1348	vli_set(px, p->x, ndigits);
1349	vli_set(py, p->y, ndigits);
1350	xycz_add(px, py, result->x, result->y, curve);
1351	vli_mod_inv(z, z, curve->p, ndigits);
1352	apply_z(result->x, result->y, z, curve);
1353}
1354
1355/* Computes R = u1P + u2Q mod p using Shamir's trick.
1356 * Based on: Kenneth MacKay's micro-ecc (2014).
1357 */
1358void ecc_point_mult_shamir(const struct ecc_point *result,
1359			   const u64 *u1, const struct ecc_point *p,
1360			   const u64 *u2, const struct ecc_point *q,
1361			   const struct ecc_curve *curve)
1362{
1363	u64 z[ECC_MAX_DIGITS];
1364	u64 sump[2][ECC_MAX_DIGITS];
1365	u64 *rx = result->x;
1366	u64 *ry = result->y;
1367	unsigned int ndigits = curve->g.ndigits;
1368	unsigned int num_bits;
1369	struct ecc_point sum = ECC_POINT_INIT(sump[0], sump[1], ndigits);
1370	const struct ecc_point *points[4];
1371	const struct ecc_point *point;
1372	unsigned int idx;
1373	int i;
1374
1375	ecc_point_add(&sum, p, q, curve);
1376	points[0] = NULL;
1377	points[1] = p;
1378	points[2] = q;
1379	points[3] = &sum;
1380
1381	num_bits = max(vli_num_bits(u1, ndigits), vli_num_bits(u2, ndigits));
 
1382	i = num_bits - 1;
1383	idx = (!!vli_test_bit(u1, i)) | ((!!vli_test_bit(u2, i)) << 1);
1384	point = points[idx];
1385
1386	vli_set(rx, point->x, ndigits);
1387	vli_set(ry, point->y, ndigits);
1388	vli_clear(z + 1, ndigits - 1);
1389	z[0] = 1;
1390
1391	for (--i; i >= 0; i--) {
1392		ecc_point_double_jacobian(rx, ry, z, curve);
1393		idx = (!!vli_test_bit(u1, i)) | ((!!vli_test_bit(u2, i)) << 1);
1394		point = points[idx];
1395		if (point) {
1396			u64 tx[ECC_MAX_DIGITS];
1397			u64 ty[ECC_MAX_DIGITS];
1398			u64 tz[ECC_MAX_DIGITS];
1399
1400			vli_set(tx, point->x, ndigits);
1401			vli_set(ty, point->y, ndigits);
1402			apply_z(tx, ty, z, curve);
1403			vli_mod_sub(tz, rx, tx, curve->p, ndigits);
1404			xycz_add(tx, ty, rx, ry, curve);
1405			vli_mod_mult_fast(z, z, tz, curve);
1406		}
1407	}
1408	vli_mod_inv(z, z, curve->p, ndigits);
1409	apply_z(rx, ry, z, curve);
1410}
1411EXPORT_SYMBOL(ecc_point_mult_shamir);
1412
 
 
 
 
 
 
 
 
 
1413static int __ecc_is_key_valid(const struct ecc_curve *curve,
1414			      const u64 *private_key, unsigned int ndigits)
1415{
1416	u64 one[ECC_MAX_DIGITS] = { 1, };
1417	u64 res[ECC_MAX_DIGITS];
1418
1419	if (!private_key)
1420		return -EINVAL;
1421
1422	if (curve->g.ndigits != ndigits)
1423		return -EINVAL;
1424
1425	/* Make sure the private key is in the range [2, n-3]. */
1426	if (vli_cmp(one, private_key, ndigits) != -1)
1427		return -EINVAL;
1428	vli_sub(res, curve->n, one, ndigits);
1429	vli_sub(res, res, one, ndigits);
1430	if (vli_cmp(res, private_key, ndigits) != 1)
1431		return -EINVAL;
1432
1433	return 0;
1434}
1435
1436int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
1437		     const u64 *private_key, unsigned int private_key_len)
1438{
1439	int nbytes;
1440	const struct ecc_curve *curve = ecc_get_curve(curve_id);
1441
1442	nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
1443
1444	if (private_key_len != nbytes)
1445		return -EINVAL;
1446
1447	return __ecc_is_key_valid(curve, private_key, ndigits);
1448}
1449EXPORT_SYMBOL(ecc_is_key_valid);
1450
1451/*
1452 * ECC private keys are generated using the method of extra random bits,
1453 * equivalent to that described in FIPS 186-4, Appendix B.4.1.
1454 *
1455 * d = (c mod(n–1)) + 1    where c is a string of random bits, 64 bits longer
1456 *                         than requested
1457 * 0 <= c mod(n-1) <= n-2  and implies that
1458 * 1 <= d <= n-1
1459 *
1460 * This method generates a private key uniformly distributed in the range
1461 * [1, n-1].
1462 */
1463int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey)
1464{
1465	const struct ecc_curve *curve = ecc_get_curve(curve_id);
1466	u64 priv[ECC_MAX_DIGITS];
1467	unsigned int nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
1468	unsigned int nbits = vli_num_bits(curve->n, ndigits);
1469	int err;
1470
1471	/* Check that N is included in Table 1 of FIPS 186-4, section 6.1.1 */
1472	if (nbits < 160 || ndigits > ARRAY_SIZE(priv))
1473		return -EINVAL;
1474
1475	/*
1476	 * FIPS 186-4 recommends that the private key should be obtained from a
1477	 * RBG with a security strength equal to or greater than the security
1478	 * strength associated with N.
1479	 *
1480	 * The maximum security strength identified by NIST SP800-57pt1r4 for
1481	 * ECC is 256 (N >= 512).
1482	 *
1483	 * This condition is met by the default RNG because it selects a favored
1484	 * DRBG with a security strength of 256.
1485	 */
1486	if (crypto_get_default_rng())
1487		return -EFAULT;
1488
1489	err = crypto_rng_get_bytes(crypto_default_rng, (u8 *)priv, nbytes);
1490	crypto_put_default_rng();
1491	if (err)
1492		return err;
1493
1494	/* Make sure the private key is in the valid range. */
1495	if (__ecc_is_key_valid(curve, priv, ndigits))
1496		return -EINVAL;
1497
1498	ecc_swap_digits(priv, privkey, ndigits);
1499
1500	return 0;
1501}
1502EXPORT_SYMBOL(ecc_gen_privkey);
1503
1504int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
1505		     const u64 *private_key, u64 *public_key)
1506{
1507	int ret = 0;
1508	struct ecc_point *pk;
1509	u64 priv[ECC_MAX_DIGITS];
1510	const struct ecc_curve *curve = ecc_get_curve(curve_id);
1511
1512	if (!private_key || !curve || ndigits > ARRAY_SIZE(priv)) {
1513		ret = -EINVAL;
1514		goto out;
1515	}
1516
1517	ecc_swap_digits(private_key, priv, ndigits);
1518
1519	pk = ecc_alloc_point(ndigits);
1520	if (!pk) {
1521		ret = -ENOMEM;
1522		goto out;
1523	}
1524
1525	ecc_point_mult(pk, &curve->g, priv, NULL, curve, ndigits);
1526
1527	/* SP800-56A rev 3 5.6.2.1.3 key check */
1528	if (ecc_is_pubkey_valid_full(curve, pk)) {
1529		ret = -EAGAIN;
1530		goto err_free_point;
1531	}
1532
1533	ecc_swap_digits(pk->x, public_key, ndigits);
1534	ecc_swap_digits(pk->y, &public_key[ndigits], ndigits);
1535
1536err_free_point:
1537	ecc_free_point(pk);
1538out:
1539	return ret;
1540}
1541EXPORT_SYMBOL(ecc_make_pub_key);
1542
1543/* SP800-56A section 5.6.2.3.4 partial verification: ephemeral keys only */
1544int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,
1545				struct ecc_point *pk)
1546{
1547	u64 yy[ECC_MAX_DIGITS], xxx[ECC_MAX_DIGITS], w[ECC_MAX_DIGITS];
1548
1549	if (WARN_ON(pk->ndigits != curve->g.ndigits))
1550		return -EINVAL;
1551
1552	/* Check 1: Verify key is not the zero point. */
1553	if (ecc_point_is_zero(pk))
1554		return -EINVAL;
1555
1556	/* Check 2: Verify key is in the range [1, p-1]. */
1557	if (vli_cmp(curve->p, pk->x, pk->ndigits) != 1)
1558		return -EINVAL;
1559	if (vli_cmp(curve->p, pk->y, pk->ndigits) != 1)
1560		return -EINVAL;
1561
1562	/* Check 3: Verify that y^2 == (x^3 + a·x + b) mod p */
1563	vli_mod_square_fast(yy, pk->y, curve); /* y^2 */
1564	vli_mod_square_fast(xxx, pk->x, curve); /* x^2 */
1565	vli_mod_mult_fast(xxx, xxx, pk->x, curve); /* x^3 */
1566	vli_mod_mult_fast(w, curve->a, pk->x, curve); /* a·x */
1567	vli_mod_add(w, w, curve->b, curve->p, pk->ndigits); /* a·x + b */
1568	vli_mod_add(w, w, xxx, curve->p, pk->ndigits); /* x^3 + a·x + b */
1569	if (vli_cmp(yy, w, pk->ndigits) != 0) /* Equation */
1570		return -EINVAL;
1571
1572	return 0;
1573}
1574EXPORT_SYMBOL(ecc_is_pubkey_valid_partial);
1575
1576/* SP800-56A section 5.6.2.3.3 full verification */
1577int ecc_is_pubkey_valid_full(const struct ecc_curve *curve,
1578			     struct ecc_point *pk)
1579{
1580	struct ecc_point *nQ;
1581
1582	/* Checks 1 through 3 */
1583	int ret = ecc_is_pubkey_valid_partial(curve, pk);
1584
1585	if (ret)
1586		return ret;
1587
1588	/* Check 4: Verify that nQ is the zero point. */
1589	nQ = ecc_alloc_point(pk->ndigits);
1590	if (!nQ)
1591		return -ENOMEM;
1592
1593	ecc_point_mult(nQ, pk, curve->n, NULL, curve, pk->ndigits);
1594	if (!ecc_point_is_zero(nQ))
1595		ret = -EINVAL;
1596
1597	ecc_free_point(nQ);
1598
1599	return ret;
1600}
1601EXPORT_SYMBOL(ecc_is_pubkey_valid_full);
1602
1603int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
1604			      const u64 *private_key, const u64 *public_key,
1605			      u64 *secret)
1606{
1607	int ret = 0;
1608	struct ecc_point *product, *pk;
1609	u64 priv[ECC_MAX_DIGITS];
1610	u64 rand_z[ECC_MAX_DIGITS];
1611	unsigned int nbytes;
1612	const struct ecc_curve *curve = ecc_get_curve(curve_id);
1613
1614	if (!private_key || !public_key || !curve ||
1615	    ndigits > ARRAY_SIZE(priv) || ndigits > ARRAY_SIZE(rand_z)) {
1616		ret = -EINVAL;
1617		goto out;
1618	}
1619
1620	nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
1621
1622	get_random_bytes(rand_z, nbytes);
1623
1624	pk = ecc_alloc_point(ndigits);
1625	if (!pk) {
1626		ret = -ENOMEM;
1627		goto out;
1628	}
1629
1630	ecc_swap_digits(public_key, pk->x, ndigits);
1631	ecc_swap_digits(&public_key[ndigits], pk->y, ndigits);
1632	ret = ecc_is_pubkey_valid_partial(curve, pk);
1633	if (ret)
1634		goto err_alloc_product;
1635
1636	ecc_swap_digits(private_key, priv, ndigits);
1637
1638	product = ecc_alloc_point(ndigits);
1639	if (!product) {
1640		ret = -ENOMEM;
1641		goto err_alloc_product;
1642	}
1643
1644	ecc_point_mult(product, pk, priv, rand_z, curve, ndigits);
1645
1646	if (ecc_point_is_zero(product)) {
 
 
1647		ret = -EFAULT;
1648		goto err_validity;
1649	}
1650
1651	ecc_swap_digits(product->x, secret, ndigits);
1652
1653err_validity:
1654	memzero_explicit(priv, sizeof(priv));
1655	memzero_explicit(rand_z, sizeof(rand_z));
1656	ecc_free_point(product);
1657err_alloc_product:
1658	ecc_free_point(pk);
1659out:
1660	return ret;
1661}
1662EXPORT_SYMBOL(crypto_ecdh_shared_secret);
1663
1664MODULE_LICENSE("Dual BSD/GPL");