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