Lines Matching +full:pk +full:- +full:pk
74 int diff = ndigits - DIV_ROUND_UP(nbytes, sizeof(u64)); in ecc_digits_from_bytes()
80 ndigits -= diff; in ecc_digits_from_bytes()
85 memcpy((u8 *)&msd + sizeof(msd) - o, in, o); in ecc_digits_from_bytes()
86 out[--ndigits] = be64_to_cpu(msd); in ecc_digits_from_bytes()
115 p->x = ecc_alloc_digits_space(ndigits); in ecc_alloc_point()
116 if (!p->x) in ecc_alloc_point()
119 p->y = ecc_alloc_digits_space(ndigits); in ecc_alloc_point()
120 if (!p->y) in ecc_alloc_point()
123 p->ndigits = ndigits; in ecc_alloc_point()
128 ecc_free_digits_space(p->x); in ecc_alloc_point()
140 kfree_sensitive(p->x); in ecc_free_point()
141 kfree_sensitive(p->y); in ecc_free_point()
176 return vli_test_bit(vli, ndigits * 64 - 1); in vli_is_negative()
179 /* Counts the number of 64-bit "digits" in vli. */
184 /* Search from the end until we find a non-zero digit. in vli_num_digits()
188 for (i = ndigits - 1; i >= 0 && vli[i] == 0; i--); in vli_num_digits()
203 digit = vli[num_digits - 1]; in vli_num_bits()
207 return ((num_digits - 1) * 64 + i); in vli_num_bits()
218 dest[i] = get_unaligned_be64(&from[ndigits - 1 - i]); in vli_from_be64()
241 /* Returns sign of left - right. */
246 for (i = ndigits - 1; i >= 0; i--) { in vli_cmp()
250 return -1; in vli_cmp()
270 carry = temp >> (64 - shift); in vli_lshift()
284 while (vli-- > end) { in vli_rshift1()
333 /* Computes result = left - right, returning borrow. Can modify in place. */
343 diff = left[i] - right[i] - borrow; in vli_sub()
354 /* Computes result = left - right, returning borrow. Can modify in place. */
364 diff = left[i] - borrow; in vli_usub()
425 for (k = 0; k < ndigits * 2 - 1; k++) { in vli_mult()
431 min = (k + 1) - ndigits; in vli_mult()
436 product = mul_64_64(left[i], right[k - i]); in vli_mult()
448 result[ndigits * 2 - 1] = r01.m_low; in vli_mult()
479 for (k = 0; k < ndigits * 2 - 1; k++) { in vli_square()
485 min = (k + 1) - ndigits; in vli_square()
487 for (i = min; i <= k && i <= k - i; i++) { in vli_square()
490 product = mul_64_64(left[i], left[k - i]); in vli_square()
492 if (i < k - i) { in vli_square()
509 result[ndigits * 2 - 1] = r01.m_low; in vli_square()
529 /* Computes result = (left - right) % mod.
537 /* In this case, p_result == -diff == (max int) - diff. in vli_mod_sub()
538 * Since -x % d == d - x, we can get the correct result from in vli_mod_sub()
547 * for special form moduli: p = 2^k-c, for small c (note the minus sign)
551 * 9 Fast Algorithms for Large-Integer Arithmetic. 9.2.3 Moduli of special form
552 * Algorithm 9.2.13 (Fast mod operation for special-form moduli).
557 u64 c = -mod[0]; in vli_mmod_special()
576 * for special form moduli: p = 2^{k-1}+c, for small c (note the plus sign)
577 * where k-1 does not fit into qword boundary by -1 bit (such as 255).
607 r[ndigits - 1] &= (1ull << 63) - 1; in vli_mmod_special2()
618 qc[ndigits - 1] &= (1ull << 63) - 1; in vli_mmod_special2()
634 * Reference: Ken MacKay's micro-ecc.
646 int shift = (ndigits * 2 * 64) - vli_num_bits(mod, ndigits); in vli_mmod_slow()
654 carry = mod[i] >> (64 - bit_shift); in vli_mmod_slow()
659 for (i = 1; shift >= 0; --shift) { in vli_mmod_slow()
664 u64 diff = v[i][j] - mod_m[j] - borrow; in vli_mmod_slow()
668 v[1 - i][j] = diff; in vli_mmod_slow()
672 mod_m[ndigits - 1] |= mod_m[ndigits] << (64 - 1); in vli_mmod_slow()
680 * length ndigits + 1, where mu * (2^w - 1) should not overflow ndigits
700 vli_cmp(r, mod, ndigits) != -1) { in vli_mmod_barrett()
711 * http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf
734 carry -= vli_sub(result, result, curve_prime, ndigits); in vli_mmod_fast_192()
738 * from http://www.nsa.gov/ia/_files/nist-routines.pdf
783 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_256()
790 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_256()
797 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_256()
804 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_256()
812 carry -= vli_sub(result, result, curve_prime, ndigits); in vli_mmod_fast_256()
894 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_384()
903 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_384()
912 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_384()
920 carry -= vli_sub(result, result, curve_prime, ndigits); in vli_mmod_fast_384()
931 * from "Recommendations for Discrete Logarithm-Based Cryptography:
960 const u64 *curve_prime = curve->p; in vli_mmod_fast()
961 const unsigned int ndigits = curve->g.ndigits; in vli_mmod_fast()
964 if (strncmp(curve->name, "nist_", 5) != 0) { in vli_mmod_fast()
965 /* Try to handle Pseudo-Marsenne primes. */ in vli_mmod_fast()
966 if (curve_prime[ndigits - 1] == -1ull) { in vli_mmod_fast()
970 } else if (curve_prime[ndigits - 1] == 1ull << 63 && in vli_mmod_fast()
971 curve_prime[ndigits - 2] == 0) { in vli_mmod_fast()
1020 vli_mult(product, left, right, curve->g.ndigits); in vli_mod_mult_fast()
1030 vli_square(product, left, curve->g.ndigits); in vli_mod_square_fast()
1037 * https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf
1069 u[ndigits - 1] |= 0x8000000000000000ull; in vli_mod_inv()
1078 v[ndigits - 1] |= 0x8000000000000000ull; in vli_mod_inv()
1092 u[ndigits - 1] |= 0x8000000000000000ull; in vli_mod_inv()
1106 v[ndigits - 1] |= 0x8000000000000000ull; in vli_mod_inv()
1114 /* ------ Point operations ------ */
1119 return (vli_is_zero(point->x, point->ndigits) && in ecc_point_is_zero()
1120 vli_is_zero(point->y, point->ndigits)); in ecc_point_is_zero()
1124 /* Point multiplication algorithm using Montgomery's ladder with co-Z
1135 const u64 *curve_prime = curve->p; in ecc_point_double_jacobian()
1136 const unsigned int ndigits = curve->g.ndigits; in ecc_point_double_jacobian()
1156 /* t3 = x1 - z1^2 */ in ecc_point_double_jacobian()
1158 /* t1 = x1^2 - z1^4 */ in ecc_point_double_jacobian()
1161 /* t3 = 2*(x1^2 - z1^4) */ in ecc_point_double_jacobian()
1163 /* t1 = 3*(x1^2 - z1^4) */ in ecc_point_double_jacobian()
1169 x1[ndigits - 1] |= carry << 63; in ecc_point_double_jacobian()
1173 /* t1 = 3/2*(x1^2 - z1^4) = B */ in ecc_point_double_jacobian()
1177 /* t3 = B^2 - A */ in ecc_point_double_jacobian()
1179 /* t3 = B^2 - 2A = x3 */ in ecc_point_double_jacobian()
1181 /* t5 = A - x3 */ in ecc_point_double_jacobian()
1183 /* t1 = B * (A - x3) */ in ecc_point_double_jacobian()
1185 /* t4 = B * (A - x3) - y1^4 = y3 */ in ecc_point_double_jacobian()
1209 const unsigned int ndigits = curve->g.ndigits; in xycz_initial_double()
1236 const u64 *curve_prime = curve->p; in xycz_add()
1237 const unsigned int ndigits = curve->g.ndigits; in xycz_add()
1239 /* t5 = x2 - x1 */ in xycz_add()
1241 /* t5 = (x2 - x1)^2 = A */ in xycz_add()
1247 /* t4 = y2 - y1 */ in xycz_add()
1249 /* t5 = (y2 - y1)^2 = D */ in xycz_add()
1252 /* t5 = D - B */ in xycz_add()
1254 /* t5 = D - B - C = x3 */ in xycz_add()
1256 /* t3 = C - B */ in xycz_add()
1258 /* t2 = y1*(C - B) */ in xycz_add()
1260 /* t3 = B - x3 */ in xycz_add()
1262 /* t4 = (y2 - y1)*(B - x3) */ in xycz_add()
1271 * Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
1272 * or P => P - Q, Q => P + Q
1281 const u64 *curve_prime = curve->p; in xycz_add_c()
1282 const unsigned int ndigits = curve->g.ndigits; in xycz_add_c()
1284 /* t5 = x2 - x1 */ in xycz_add_c()
1286 /* t5 = (x2 - x1)^2 = A */ in xycz_add_c()
1294 /* t4 = y2 - y1 */ in xycz_add_c()
1297 /* t6 = C - B */ in xycz_add_c()
1299 /* t2 = y1 * (C - B) */ in xycz_add_c()
1303 /* t3 = (y2 - y1)^2 */ in xycz_add_c()
1308 /* t7 = B - x3 */ in xycz_add_c()
1310 /* t4 = (y2 - y1)*(B - x3) */ in xycz_add_c()
1319 /* t6 = x3' - B */ in xycz_add_c()
1321 /* t6 = (y2 + y1)*(x3' - B) */ in xycz_add_c()
1339 u64 *curve_prime = curve->p; in ecc_point_mult()
1344 carry = vli_add(sk[0], scalar, curve->n, ndigits); in ecc_point_mult()
1345 vli_add(sk[1], sk[0], curve->n, ndigits); in ecc_point_mult()
1347 if (curve->nbits == 521) /* NIST P521 */ in ecc_point_mult()
1348 num_bits = curve->nbits + 2; in ecc_point_mult()
1352 vli_set(rx[1], point->x, ndigits); in ecc_point_mult()
1353 vli_set(ry[1], point->y, ndigits); in ecc_point_mult()
1357 for (i = num_bits - 2; i > 0; i--) { in ecc_point_mult()
1359 xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve); in ecc_point_mult()
1360 xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve); in ecc_point_mult()
1364 xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve); in ecc_point_mult()
1367 /* X1 - X0 */ in ecc_point_mult()
1369 /* Yb * (X1 - X0) */ in ecc_point_mult()
1370 vli_mod_mult_fast(z, z, ry[1 - nb], curve); in ecc_point_mult()
1371 /* xP * Yb * (X1 - X0) */ in ecc_point_mult()
1372 vli_mod_mult_fast(z, z, point->x, curve); in ecc_point_mult()
1374 /* 1 / (xP * Yb * (X1 - X0)) */ in ecc_point_mult()
1375 vli_mod_inv(z, z, curve_prime, point->ndigits); in ecc_point_mult()
1377 /* yP / (xP * Yb * (X1 - X0)) */ in ecc_point_mult()
1378 vli_mod_mult_fast(z, z, point->y, curve); in ecc_point_mult()
1379 /* Xb * yP / (xP * Yb * (X1 - X0)) */ in ecc_point_mult()
1380 vli_mod_mult_fast(z, z, rx[1 - nb], curve); in ecc_point_mult()
1383 xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve); in ecc_point_mult()
1387 vli_set(result->x, rx[0], ndigits); in ecc_point_mult()
1388 vli_set(result->y, ry[0], ndigits); in ecc_point_mult()
1399 unsigned int ndigits = curve->g.ndigits; in ecc_point_add()
1401 vli_set(result->x, q->x, ndigits); in ecc_point_add()
1402 vli_set(result->y, q->y, ndigits); in ecc_point_add()
1403 vli_mod_sub(z, result->x, p->x, curve->p, ndigits); in ecc_point_add()
1404 vli_set(px, p->x, ndigits); in ecc_point_add()
1405 vli_set(py, p->y, ndigits); in ecc_point_add()
1406 xycz_add(px, py, result->x, result->y, curve); in ecc_point_add()
1407 vli_mod_inv(z, z, curve->p, ndigits); in ecc_point_add()
1408 apply_z(result->x, result->y, z, curve); in ecc_point_add()
1412 * Based on: Kenneth MacKay's micro-ecc (2014).
1421 u64 *rx = result->x; in ecc_point_mult_shamir()
1422 u64 *ry = result->y; in ecc_point_mult_shamir()
1423 unsigned int ndigits = curve->g.ndigits; in ecc_point_mult_shamir()
1438 i = num_bits - 1; in ecc_point_mult_shamir()
1443 vli_set(rx, point->x, ndigits); in ecc_point_mult_shamir()
1444 vli_set(ry, point->y, ndigits); in ecc_point_mult_shamir()
1445 vli_clear(z + 1, ndigits - 1); in ecc_point_mult_shamir()
1448 for (--i; i >= 0; i--) { in ecc_point_mult_shamir()
1458 vli_set(tx, point->x, ndigits); in ecc_point_mult_shamir()
1459 vli_set(ty, point->y, ndigits); in ecc_point_mult_shamir()
1461 vli_mod_sub(tz, rx, tx, curve->p, ndigits); in ecc_point_mult_shamir()
1466 vli_mod_inv(z, z, curve->p, ndigits); in ecc_point_mult_shamir()
1472 * This function performs checks equivalent to Appendix A.4.2 of FIPS 186-5.
1473 * Whereas A.4.2 results in an integer in the interval [1, n-1], this function
1474 * ensures that the integer is in the range of [2, n-3]. We are slightly
1484 return -EINVAL; in __ecc_is_key_valid()
1486 if (curve->g.ndigits != ndigits) in __ecc_is_key_valid()
1487 return -EINVAL; in __ecc_is_key_valid()
1489 /* Make sure the private key is in the range [2, n-3]. */ in __ecc_is_key_valid()
1490 if (vli_cmp(one, private_key, ndigits) != -1) in __ecc_is_key_valid()
1491 return -EINVAL; in __ecc_is_key_valid()
1492 vli_sub(res, curve->n, one, ndigits); in __ecc_is_key_valid()
1495 return -EINVAL; in __ecc_is_key_valid()
1509 return -EINVAL; in ecc_is_key_valid()
1517 * equivalent to that described in FIPS 186-5, Appendix A.2.2.
1520 * [2, n-3].
1527 unsigned int nbits = vli_num_bits(curve->n, ndigits); in ecc_gen_privkey()
1531 * Step 1 & 2: check that N is included in Table 1 of FIPS 186-5, in ecc_gen_privkey()
1535 return -EINVAL; in ecc_gen_privkey()
1538 * FIPS 186-5 recommends that the private key should be obtained from a in ecc_gen_privkey()
1542 * The maximum security strength identified by NIST SP800-57pt1r4 for in ecc_gen_privkey()
1549 return -EFAULT; in ecc_gen_privkey()
1560 return -EINVAL; in ecc_gen_privkey()
1570 struct ecc_point *pk; in ecc_make_pub_key() local
1574 ret = -EINVAL; in ecc_make_pub_key()
1578 pk = ecc_alloc_point(ndigits); in ecc_make_pub_key()
1579 if (!pk) { in ecc_make_pub_key()
1580 ret = -ENOMEM; in ecc_make_pub_key()
1584 ecc_point_mult(pk, &curve->g, private_key, NULL, curve, ndigits); in ecc_make_pub_key()
1586 /* SP800-56A rev 3 5.6.2.1.3 key check */ in ecc_make_pub_key()
1587 if (ecc_is_pubkey_valid_full(curve, pk)) { in ecc_make_pub_key()
1588 ret = -EAGAIN; in ecc_make_pub_key()
1592 ecc_swap_digits(pk->x, public_key, ndigits); in ecc_make_pub_key()
1593 ecc_swap_digits(pk->y, &public_key[ndigits], ndigits); in ecc_make_pub_key()
1596 ecc_free_point(pk); in ecc_make_pub_key()
1602 /* SP800-56A section 5.6.2.3.4 partial verification: ephemeral keys only */
1604 struct ecc_point *pk) in ecc_is_pubkey_valid_partial() argument
1608 if (WARN_ON(pk->ndigits != curve->g.ndigits)) in ecc_is_pubkey_valid_partial()
1609 return -EINVAL; in ecc_is_pubkey_valid_partial()
1612 if (ecc_point_is_zero(pk)) in ecc_is_pubkey_valid_partial()
1613 return -EINVAL; in ecc_is_pubkey_valid_partial()
1615 /* Check 2: Verify key is in the range [1, p-1]. */ in ecc_is_pubkey_valid_partial()
1616 if (vli_cmp(curve->p, pk->x, pk->ndigits) != 1) in ecc_is_pubkey_valid_partial()
1617 return -EINVAL; in ecc_is_pubkey_valid_partial()
1618 if (vli_cmp(curve->p, pk->y, pk->ndigits) != 1) in ecc_is_pubkey_valid_partial()
1619 return -EINVAL; in ecc_is_pubkey_valid_partial()
1622 vli_mod_square_fast(yy, pk->y, curve); /* y^2 */ in ecc_is_pubkey_valid_partial()
1623 vli_mod_square_fast(xxx, pk->x, curve); /* x^2 */ in ecc_is_pubkey_valid_partial()
1624 vli_mod_mult_fast(xxx, xxx, pk->x, curve); /* x^3 */ in ecc_is_pubkey_valid_partial()
1625 vli_mod_mult_fast(w, curve->a, pk->x, curve); /* a·x */ in ecc_is_pubkey_valid_partial()
1626 vli_mod_add(w, w, curve->b, curve->p, pk->ndigits); /* a·x + b */ in ecc_is_pubkey_valid_partial()
1627 vli_mod_add(w, w, xxx, curve->p, pk->ndigits); /* x^3 + a·x + b */ in ecc_is_pubkey_valid_partial()
1628 if (vli_cmp(yy, w, pk->ndigits) != 0) /* Equation */ in ecc_is_pubkey_valid_partial()
1629 return -EINVAL; in ecc_is_pubkey_valid_partial()
1635 /* SP800-56A section 5.6.2.3.3 full verification */
1637 struct ecc_point *pk) in ecc_is_pubkey_valid_full() argument
1642 int ret = ecc_is_pubkey_valid_partial(curve, pk); in ecc_is_pubkey_valid_full()
1648 nQ = ecc_alloc_point(pk->ndigits); in ecc_is_pubkey_valid_full()
1650 return -ENOMEM; in ecc_is_pubkey_valid_full()
1652 ecc_point_mult(nQ, pk, curve->n, NULL, curve, pk->ndigits); in ecc_is_pubkey_valid_full()
1654 ret = -EINVAL; in ecc_is_pubkey_valid_full()
1667 struct ecc_point *product, *pk; in crypto_ecdh_shared_secret() local
1673 ret = -EINVAL; in crypto_ecdh_shared_secret()
1681 pk = ecc_alloc_point(ndigits); in crypto_ecdh_shared_secret()
1682 if (!pk) { in crypto_ecdh_shared_secret()
1683 ret = -ENOMEM; in crypto_ecdh_shared_secret()
1687 ecc_swap_digits(public_key, pk->x, ndigits); in crypto_ecdh_shared_secret()
1688 ecc_swap_digits(&public_key[ndigits], pk->y, ndigits); in crypto_ecdh_shared_secret()
1689 ret = ecc_is_pubkey_valid_partial(curve, pk); in crypto_ecdh_shared_secret()
1695 ret = -ENOMEM; in crypto_ecdh_shared_secret()
1699 ecc_point_mult(product, pk, private_key, rand_z, curve, ndigits); in crypto_ecdh_shared_secret()
1702 ret = -EFAULT; in crypto_ecdh_shared_secret()
1706 ecc_swap_digits(product->x, secret, ndigits); in crypto_ecdh_shared_secret()
1712 ecc_free_point(pk); in crypto_ecdh_shared_secret()