Lines Matching +full:pk +full:- +full:pk
83 p->x = ecc_alloc_digits_space(ndigits); in ecc_alloc_point()
84 if (!p->x) in ecc_alloc_point()
87 p->y = ecc_alloc_digits_space(ndigits); in ecc_alloc_point()
88 if (!p->y) in ecc_alloc_point()
91 p->ndigits = ndigits; in ecc_alloc_point()
96 ecc_free_digits_space(p->x); in ecc_alloc_point()
107 kfree_sensitive(p->x); in ecc_free_point()
108 kfree_sensitive(p->y); in ecc_free_point()
142 return vli_test_bit(vli, ndigits * 64 - 1); in vli_is_negative()
145 /* Counts the number of 64-bit "digits" in vli. */
150 /* Search from the end until we find a non-zero digit. in vli_num_digits()
154 for (i = ndigits - 1; i >= 0 && vli[i] == 0; i--); in vli_num_digits()
169 digit = vli[num_digits - 1]; in vli_num_bits()
173 return ((num_digits - 1) * 64 + i); in vli_num_bits()
183 dest[i] = get_unaligned_be64(&from[ndigits - 1 - i]); in vli_from_be64()
206 /* Returns sign of left - right. */
211 for (i = ndigits - 1; i >= 0; i--) { in vli_cmp()
215 return -1; in vli_cmp()
235 carry = temp >> (64 - shift); in vli_lshift()
249 while (vli-- > end) { in vli_rshift1()
298 /* Computes result = left - right, returning borrow. Can modify in place. */
308 diff = left[i] - right[i] - borrow; in vli_sub()
319 /* Computes result = left - right, returning borrow. Can modify in place. */
329 diff = left[i] - borrow; in vli_usub()
390 for (k = 0; k < ndigits * 2 - 1; k++) { in vli_mult()
396 min = (k + 1) - ndigits; in vli_mult()
401 product = mul_64_64(left[i], right[k - i]); in vli_mult()
413 result[ndigits * 2 - 1] = r01.m_low; in vli_mult()
444 for (k = 0; k < ndigits * 2 - 1; k++) { in vli_square()
450 min = (k + 1) - ndigits; in vli_square()
452 for (i = min; i <= k && i <= k - i; i++) { in vli_square()
455 product = mul_64_64(left[i], left[k - i]); in vli_square()
457 if (i < k - i) { in vli_square()
474 result[ndigits * 2 - 1] = r01.m_low; in vli_square()
494 /* Computes result = (left - right) % mod.
502 /* In this case, p_result == -diff == (max int) - diff. in vli_mod_sub()
503 * Since -x % d == d - x, we can get the correct result from in vli_mod_sub()
512 * for special form moduli: p = 2^k-c, for small c (note the minus sign)
516 * 9 Fast Algorithms for Large-Integer Arithmetic. 9.2.3 Moduli of special form
517 * Algorithm 9.2.13 (Fast mod operation for special-form moduli).
522 u64 c = -mod[0]; in vli_mmod_special()
541 * for special form moduli: p = 2^{k-1}+c, for small c (note the plus sign)
542 * where k-1 does not fit into qword boundary by -1 bit (such as 255).
572 r[ndigits - 1] &= (1ull << 63) - 1; in vli_mmod_special2()
583 qc[ndigits - 1] &= (1ull << 63) - 1; in vli_mmod_special2()
599 * Reference: Ken MacKay's micro-ecc.
611 int shift = (ndigits * 2 * 64) - vli_num_bits(mod, ndigits); in vli_mmod_slow()
619 carry = mod[i] >> (64 - bit_shift); in vli_mmod_slow()
624 for (i = 1; shift >= 0; --shift) { in vli_mmod_slow()
629 u64 diff = v[i][j] - mod_m[j] - borrow; in vli_mmod_slow()
633 v[1 - i][j] = diff; in vli_mmod_slow()
637 mod_m[ndigits - 1] |= mod_m[ndigits] << (64 - 1); in vli_mmod_slow()
645 * length ndigits + 1, where mu * (2^w - 1) should not overflow ndigits
665 vli_cmp(r, mod, ndigits) != -1) { in vli_mmod_barrett()
676 * http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf
699 carry -= vli_sub(result, result, curve_prime, ndigits); in vli_mmod_fast_192()
703 * from http://www.nsa.gov/ia/_files/nist-routines.pdf
748 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_256()
755 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_256()
762 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_256()
769 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_256()
777 carry -= vli_sub(result, result, curve_prime, ndigits); in vli_mmod_fast_256()
859 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_384()
868 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_384()
877 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_384()
885 carry -= vli_sub(result, result, curve_prime, ndigits); in vli_mmod_fast_384()
903 const u64 *curve_prime = curve->p; in vli_mmod_fast()
904 const unsigned int ndigits = curve->g.ndigits; in vli_mmod_fast()
907 if (strncmp(curve->name, "nist_", 5) != 0) { in vli_mmod_fast()
908 /* Try to handle Pseudo-Marsenne primes. */ in vli_mmod_fast()
909 if (curve_prime[ndigits - 1] == -1ull) { in vli_mmod_fast()
913 } else if (curve_prime[ndigits - 1] == 1ull << 63 && in vli_mmod_fast()
914 curve_prime[ndigits - 2] == 0) { in vli_mmod_fast()
960 vli_mult(product, left, right, curve->g.ndigits); in vli_mod_mult_fast()
970 vli_square(product, left, curve->g.ndigits); in vli_mod_square_fast()
977 * https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf
1009 u[ndigits - 1] |= 0x8000000000000000ull; in vli_mod_inv()
1018 v[ndigits - 1] |= 0x8000000000000000ull; in vli_mod_inv()
1032 u[ndigits - 1] |= 0x8000000000000000ull; in vli_mod_inv()
1046 v[ndigits - 1] |= 0x8000000000000000ull; in vli_mod_inv()
1054 /* ------ Point operations ------ */
1059 return (vli_is_zero(point->x, point->ndigits) && in ecc_point_is_zero()
1060 vli_is_zero(point->y, point->ndigits)); in ecc_point_is_zero()
1063 /* Point multiplication algorithm using Montgomery's ladder with co-Z
1074 const u64 *curve_prime = curve->p; in ecc_point_double_jacobian()
1075 const unsigned int ndigits = curve->g.ndigits; in ecc_point_double_jacobian()
1095 /* t3 = x1 - z1^2 */ in ecc_point_double_jacobian()
1097 /* t1 = x1^2 - z1^4 */ in ecc_point_double_jacobian()
1100 /* t3 = 2*(x1^2 - z1^4) */ in ecc_point_double_jacobian()
1102 /* t1 = 3*(x1^2 - z1^4) */ in ecc_point_double_jacobian()
1108 x1[ndigits - 1] |= carry << 63; in ecc_point_double_jacobian()
1112 /* t1 = 3/2*(x1^2 - z1^4) = B */ in ecc_point_double_jacobian()
1116 /* t3 = B^2 - A */ in ecc_point_double_jacobian()
1118 /* t3 = B^2 - 2A = x3 */ in ecc_point_double_jacobian()
1120 /* t5 = A - x3 */ in ecc_point_double_jacobian()
1122 /* t1 = B * (A - x3) */ in ecc_point_double_jacobian()
1124 /* t4 = B * (A - x3) - y1^4 = y3 */ in ecc_point_double_jacobian()
1148 const unsigned int ndigits = curve->g.ndigits; in xycz_initial_double()
1175 const u64 *curve_prime = curve->p; in xycz_add()
1176 const unsigned int ndigits = curve->g.ndigits; in xycz_add()
1178 /* t5 = x2 - x1 */ in xycz_add()
1180 /* t5 = (x2 - x1)^2 = A */ in xycz_add()
1186 /* t4 = y2 - y1 */ in xycz_add()
1188 /* t5 = (y2 - y1)^2 = D */ in xycz_add()
1191 /* t5 = D - B */ in xycz_add()
1193 /* t5 = D - B - C = x3 */ in xycz_add()
1195 /* t3 = C - B */ in xycz_add()
1197 /* t2 = y1*(C - B) */ in xycz_add()
1199 /* t3 = B - x3 */ in xycz_add()
1201 /* t4 = (y2 - y1)*(B - x3) */ in xycz_add()
1210 * Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
1211 * or P => P - Q, Q => P + Q
1220 const u64 *curve_prime = curve->p; in xycz_add_c()
1221 const unsigned int ndigits = curve->g.ndigits; in xycz_add_c()
1223 /* t5 = x2 - x1 */ in xycz_add_c()
1225 /* t5 = (x2 - x1)^2 = A */ in xycz_add_c()
1233 /* t4 = y2 - y1 */ in xycz_add_c()
1236 /* t6 = C - B */ in xycz_add_c()
1238 /* t2 = y1 * (C - B) */ in xycz_add_c()
1242 /* t3 = (y2 - y1)^2 */ in xycz_add_c()
1247 /* t7 = B - x3 */ in xycz_add_c()
1249 /* t4 = (y2 - y1)*(B - x3) */ in xycz_add_c()
1258 /* t6 = x3' - B */ in xycz_add_c()
1260 /* t6 = (y2 + y1)*(x3' - B) */ in xycz_add_c()
1278 u64 *curve_prime = curve->p; in ecc_point_mult()
1283 carry = vli_add(sk[0], scalar, curve->n, ndigits); in ecc_point_mult()
1284 vli_add(sk[1], sk[0], curve->n, ndigits); in ecc_point_mult()
1288 vli_set(rx[1], point->x, ndigits); in ecc_point_mult()
1289 vli_set(ry[1], point->y, ndigits); in ecc_point_mult()
1293 for (i = num_bits - 2; i > 0; i--) { in ecc_point_mult()
1295 xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve); in ecc_point_mult()
1296 xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve); in ecc_point_mult()
1300 xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve); in ecc_point_mult()
1303 /* X1 - X0 */ in ecc_point_mult()
1305 /* Yb * (X1 - X0) */ in ecc_point_mult()
1306 vli_mod_mult_fast(z, z, ry[1 - nb], curve); in ecc_point_mult()
1307 /* xP * Yb * (X1 - X0) */ in ecc_point_mult()
1308 vli_mod_mult_fast(z, z, point->x, curve); in ecc_point_mult()
1310 /* 1 / (xP * Yb * (X1 - X0)) */ in ecc_point_mult()
1311 vli_mod_inv(z, z, curve_prime, point->ndigits); in ecc_point_mult()
1313 /* yP / (xP * Yb * (X1 - X0)) */ in ecc_point_mult()
1314 vli_mod_mult_fast(z, z, point->y, curve); in ecc_point_mult()
1315 /* Xb * yP / (xP * Yb * (X1 - X0)) */ in ecc_point_mult()
1316 vli_mod_mult_fast(z, z, rx[1 - nb], curve); in ecc_point_mult()
1319 xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve); in ecc_point_mult()
1323 vli_set(result->x, rx[0], ndigits); in ecc_point_mult()
1324 vli_set(result->y, ry[0], ndigits); in ecc_point_mult()
1335 unsigned int ndigits = curve->g.ndigits; in ecc_point_add()
1337 vli_set(result->x, q->x, ndigits); in ecc_point_add()
1338 vli_set(result->y, q->y, ndigits); in ecc_point_add()
1339 vli_mod_sub(z, result->x, p->x, curve->p, ndigits); in ecc_point_add()
1340 vli_set(px, p->x, ndigits); in ecc_point_add()
1341 vli_set(py, p->y, ndigits); in ecc_point_add()
1342 xycz_add(px, py, result->x, result->y, curve); in ecc_point_add()
1343 vli_mod_inv(z, z, curve->p, ndigits); in ecc_point_add()
1344 apply_z(result->x, result->y, z, curve); in ecc_point_add()
1348 * Based on: Kenneth MacKay's micro-ecc (2014).
1357 u64 *rx = result->x; in ecc_point_mult_shamir()
1358 u64 *ry = result->y; in ecc_point_mult_shamir()
1359 unsigned int ndigits = curve->g.ndigits; in ecc_point_mult_shamir()
1374 i = num_bits - 1; in ecc_point_mult_shamir()
1378 vli_set(rx, point->x, ndigits); in ecc_point_mult_shamir()
1379 vli_set(ry, point->y, ndigits); in ecc_point_mult_shamir()
1380 vli_clear(z + 1, ndigits - 1); in ecc_point_mult_shamir()
1383 for (--i; i >= 0; i--) { in ecc_point_mult_shamir()
1392 vli_set(tx, point->x, ndigits); in ecc_point_mult_shamir()
1393 vli_set(ty, point->y, ndigits); in ecc_point_mult_shamir()
1395 vli_mod_sub(tz, rx, tx, curve->p, ndigits); in ecc_point_mult_shamir()
1400 vli_mod_inv(z, z, curve->p, ndigits); in ecc_point_mult_shamir()
1412 return -EINVAL; in __ecc_is_key_valid()
1414 if (curve->g.ndigits != ndigits) in __ecc_is_key_valid()
1415 return -EINVAL; in __ecc_is_key_valid()
1417 /* Make sure the private key is in the range [2, n-3]. */ in __ecc_is_key_valid()
1418 if (vli_cmp(one, private_key, ndigits) != -1) in __ecc_is_key_valid()
1419 return -EINVAL; in __ecc_is_key_valid()
1420 vli_sub(res, curve->n, one, ndigits); in __ecc_is_key_valid()
1423 return -EINVAL; in __ecc_is_key_valid()
1437 return -EINVAL; in ecc_is_key_valid()
1445 * equivalent to that described in FIPS 186-4, Appendix B.4.1.
1449 * 0 <= c mod(n-1) <= n-2 and implies that
1450 * 1 <= d <= n-1
1453 * [1, n-1].
1460 unsigned int nbits = vli_num_bits(curve->n, ndigits); in ecc_gen_privkey()
1463 /* Check that N is included in Table 1 of FIPS 186-4, section 6.1.1 */ in ecc_gen_privkey()
1465 return -EINVAL; in ecc_gen_privkey()
1468 * FIPS 186-4 recommends that the private key should be obtained from a in ecc_gen_privkey()
1472 * The maximum security strength identified by NIST SP800-57pt1r4 for in ecc_gen_privkey()
1479 return -EFAULT; in ecc_gen_privkey()
1488 return -EINVAL; in ecc_gen_privkey()
1500 struct ecc_point *pk; in ecc_make_pub_key() local
1505 ret = -EINVAL; in ecc_make_pub_key()
1511 pk = ecc_alloc_point(ndigits); in ecc_make_pub_key()
1512 if (!pk) { in ecc_make_pub_key()
1513 ret = -ENOMEM; in ecc_make_pub_key()
1517 ecc_point_mult(pk, &curve->g, priv, NULL, curve, ndigits); in ecc_make_pub_key()
1519 /* SP800-56A rev 3 5.6.2.1.3 key check */ in ecc_make_pub_key()
1520 if (ecc_is_pubkey_valid_full(curve, pk)) { in ecc_make_pub_key()
1521 ret = -EAGAIN; in ecc_make_pub_key()
1525 ecc_swap_digits(pk->x, public_key, ndigits); in ecc_make_pub_key()
1526 ecc_swap_digits(pk->y, &public_key[ndigits], ndigits); in ecc_make_pub_key()
1529 ecc_free_point(pk); in ecc_make_pub_key()
1535 /* SP800-56A section 5.6.2.3.4 partial verification: ephemeral keys only */
1537 struct ecc_point *pk) in ecc_is_pubkey_valid_partial() argument
1541 if (WARN_ON(pk->ndigits != curve->g.ndigits)) in ecc_is_pubkey_valid_partial()
1542 return -EINVAL; in ecc_is_pubkey_valid_partial()
1545 if (ecc_point_is_zero(pk)) in ecc_is_pubkey_valid_partial()
1546 return -EINVAL; in ecc_is_pubkey_valid_partial()
1548 /* Check 2: Verify key is in the range [1, p-1]. */ in ecc_is_pubkey_valid_partial()
1549 if (vli_cmp(curve->p, pk->x, pk->ndigits) != 1) in ecc_is_pubkey_valid_partial()
1550 return -EINVAL; in ecc_is_pubkey_valid_partial()
1551 if (vli_cmp(curve->p, pk->y, pk->ndigits) != 1) in ecc_is_pubkey_valid_partial()
1552 return -EINVAL; in ecc_is_pubkey_valid_partial()
1555 vli_mod_square_fast(yy, pk->y, curve); /* y^2 */ in ecc_is_pubkey_valid_partial()
1556 vli_mod_square_fast(xxx, pk->x, curve); /* x^2 */ in ecc_is_pubkey_valid_partial()
1557 vli_mod_mult_fast(xxx, xxx, pk->x, curve); /* x^3 */ in ecc_is_pubkey_valid_partial()
1558 vli_mod_mult_fast(w, curve->a, pk->x, curve); /* a·x */ in ecc_is_pubkey_valid_partial()
1559 vli_mod_add(w, w, curve->b, curve->p, pk->ndigits); /* a·x + b */ in ecc_is_pubkey_valid_partial()
1560 vli_mod_add(w, w, xxx, curve->p, pk->ndigits); /* x^3 + a·x + b */ in ecc_is_pubkey_valid_partial()
1561 if (vli_cmp(yy, w, pk->ndigits) != 0) /* Equation */ in ecc_is_pubkey_valid_partial()
1562 return -EINVAL; in ecc_is_pubkey_valid_partial()
1568 /* SP800-56A section 5.6.2.3.3 full verification */
1570 struct ecc_point *pk) in ecc_is_pubkey_valid_full() argument
1575 int ret = ecc_is_pubkey_valid_partial(curve, pk); in ecc_is_pubkey_valid_full()
1581 nQ = ecc_alloc_point(pk->ndigits); in ecc_is_pubkey_valid_full()
1583 return -ENOMEM; in ecc_is_pubkey_valid_full()
1585 ecc_point_mult(nQ, pk, curve->n, NULL, curve, pk->ndigits); in ecc_is_pubkey_valid_full()
1587 ret = -EINVAL; in ecc_is_pubkey_valid_full()
1600 struct ecc_point *product, *pk; in crypto_ecdh_shared_secret() local
1608 ret = -EINVAL; in crypto_ecdh_shared_secret()
1616 pk = ecc_alloc_point(ndigits); in crypto_ecdh_shared_secret()
1617 if (!pk) { in crypto_ecdh_shared_secret()
1618 ret = -ENOMEM; in crypto_ecdh_shared_secret()
1622 ecc_swap_digits(public_key, pk->x, ndigits); in crypto_ecdh_shared_secret()
1623 ecc_swap_digits(&public_key[ndigits], pk->y, ndigits); in crypto_ecdh_shared_secret()
1624 ret = ecc_is_pubkey_valid_partial(curve, pk); in crypto_ecdh_shared_secret()
1632 ret = -ENOMEM; in crypto_ecdh_shared_secret()
1636 ecc_point_mult(product, pk, priv, rand_z, curve, ndigits); in crypto_ecdh_shared_secret()
1639 ret = -EFAULT; in crypto_ecdh_shared_secret()
1643 ecc_swap_digits(product->x, secret, ndigits); in crypto_ecdh_shared_secret()
1650 ecc_free_point(pk); in crypto_ecdh_shared_secret()