• Home
  • Raw
  • Download

Lines Matching +full:pk +full:- +full:pk

91 	p->x = ecc_alloc_digits_space(ndigits);  in ecc_alloc_point()
92 if (!p->x) in ecc_alloc_point()
95 p->y = ecc_alloc_digits_space(ndigits); in ecc_alloc_point()
96 if (!p->y) in ecc_alloc_point()
99 p->ndigits = ndigits; in ecc_alloc_point()
104 ecc_free_digits_space(p->x); in ecc_alloc_point()
116 kfree_sensitive(p->x); in ecc_free_point()
117 kfree_sensitive(p->y); in ecc_free_point()
152 return vli_test_bit(vli, ndigits * 64 - 1); in vli_is_negative()
155 /* Counts the number of 64-bit "digits" in vli. */
160 /* Search from the end until we find a non-zero digit. in vli_num_digits()
164 for (i = ndigits - 1; i >= 0 && vli[i] == 0; i--); in vli_num_digits()
179 digit = vli[num_digits - 1]; in vli_num_bits()
183 return ((num_digits - 1) * 64 + i); in vli_num_bits()
194 dest[i] = get_unaligned_be64(&from[ndigits - 1 - i]); in vli_from_be64()
217 /* Returns sign of left - right. */
222 for (i = ndigits - 1; i >= 0; i--) { in vli_cmp()
226 return -1; in vli_cmp()
246 carry = temp >> (64 - shift); in vli_lshift()
260 while (vli-- > end) { in vli_rshift1()
309 /* Computes result = left - right, returning borrow. Can modify in place. */
319 diff = left[i] - right[i] - borrow; in vli_sub()
330 /* Computes result = left - right, returning borrow. Can modify in place. */
340 diff = left[i] - borrow; in vli_usub()
401 for (k = 0; k < ndigits * 2 - 1; k++) { in vli_mult()
407 min = (k + 1) - ndigits; in vli_mult()
412 product = mul_64_64(left[i], right[k - i]); in vli_mult()
424 result[ndigits * 2 - 1] = r01.m_low; in vli_mult()
455 for (k = 0; k < ndigits * 2 - 1; k++) { in vli_square()
461 min = (k + 1) - ndigits; in vli_square()
463 for (i = min; i <= k && i <= k - i; i++) { in vli_square()
466 product = mul_64_64(left[i], left[k - i]); in vli_square()
468 if (i < k - i) { in vli_square()
485 result[ndigits * 2 - 1] = r01.m_low; in vli_square()
505 /* Computes result = (left - right) % mod.
513 /* In this case, p_result == -diff == (max int) - diff. in vli_mod_sub()
514 * Since -x % d == d - x, we can get the correct result from in vli_mod_sub()
523 * for special form moduli: p = 2^k-c, for small c (note the minus sign)
527 * 9 Fast Algorithms for Large-Integer Arithmetic. 9.2.3 Moduli of special form
528 * Algorithm 9.2.13 (Fast mod operation for special-form moduli).
533 u64 c = -mod[0]; in vli_mmod_special()
552 * for special form moduli: p = 2^{k-1}+c, for small c (note the plus sign)
553 * where k-1 does not fit into qword boundary by -1 bit (such as 255).
583 r[ndigits - 1] &= (1ull << 63) - 1; in vli_mmod_special2()
594 qc[ndigits - 1] &= (1ull << 63) - 1; in vli_mmod_special2()
610 * Reference: Ken MacKay's micro-ecc.
622 int shift = (ndigits * 2 * 64) - vli_num_bits(mod, ndigits); in vli_mmod_slow()
630 carry = mod[i] >> (64 - bit_shift); in vli_mmod_slow()
635 for (i = 1; shift >= 0; --shift) { in vli_mmod_slow()
640 u64 diff = v[i][j] - mod_m[j] - borrow; in vli_mmod_slow()
644 v[1 - i][j] = diff; in vli_mmod_slow()
648 mod_m[ndigits - 1] |= mod_m[ndigits] << (64 - 1); in vli_mmod_slow()
656 * length ndigits + 1, where mu * (2^w - 1) should not overflow ndigits
676 vli_cmp(r, mod, ndigits) != -1) { in vli_mmod_barrett()
687 * http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf
710 carry -= vli_sub(result, result, curve_prime, ndigits); in vli_mmod_fast_192()
714 * from http://www.nsa.gov/ia/_files/nist-routines.pdf
759 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_256()
766 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_256()
773 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_256()
780 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_256()
788 carry -= vli_sub(result, result, curve_prime, ndigits); in vli_mmod_fast_256()
870 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_384()
879 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_384()
888 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_384()
896 carry -= vli_sub(result, result, curve_prime, ndigits); in vli_mmod_fast_384()
914 const u64 *curve_prime = curve->p; in vli_mmod_fast()
915 const unsigned int ndigits = curve->g.ndigits; in vli_mmod_fast()
918 if (strncmp(curve->name, "nist_", 5) != 0) { in vli_mmod_fast()
919 /* Try to handle Pseudo-Marsenne primes. */ in vli_mmod_fast()
920 if (curve_prime[ndigits - 1] == -1ull) { in vli_mmod_fast()
924 } else if (curve_prime[ndigits - 1] == 1ull << 63 && in vli_mmod_fast()
925 curve_prime[ndigits - 2] == 0) { in vli_mmod_fast()
971 vli_mult(product, left, right, curve->g.ndigits); in vli_mod_mult_fast()
981 vli_square(product, left, curve->g.ndigits); in vli_mod_square_fast()
988 * https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf
1020 u[ndigits - 1] |= 0x8000000000000000ull; in vli_mod_inv()
1029 v[ndigits - 1] |= 0x8000000000000000ull; in vli_mod_inv()
1043 u[ndigits - 1] |= 0x8000000000000000ull; in vli_mod_inv()
1057 v[ndigits - 1] |= 0x8000000000000000ull; in vli_mod_inv()
1065 /* ------ Point operations ------ */
1070 return (vli_is_zero(point->x, point->ndigits) && in ecc_point_is_zero()
1071 vli_is_zero(point->y, point->ndigits)); in ecc_point_is_zero()
1075 /* Point multiplication algorithm using Montgomery's ladder with co-Z
1086 const u64 *curve_prime = curve->p; in ecc_point_double_jacobian()
1087 const unsigned int ndigits = curve->g.ndigits; in ecc_point_double_jacobian()
1107 /* t3 = x1 - z1^2 */ in ecc_point_double_jacobian()
1109 /* t1 = x1^2 - z1^4 */ in ecc_point_double_jacobian()
1112 /* t3 = 2*(x1^2 - z1^4) */ in ecc_point_double_jacobian()
1114 /* t1 = 3*(x1^2 - z1^4) */ in ecc_point_double_jacobian()
1120 x1[ndigits - 1] |= carry << 63; in ecc_point_double_jacobian()
1124 /* t1 = 3/2*(x1^2 - z1^4) = B */ in ecc_point_double_jacobian()
1128 /* t3 = B^2 - A */ in ecc_point_double_jacobian()
1130 /* t3 = B^2 - 2A = x3 */ in ecc_point_double_jacobian()
1132 /* t5 = A - x3 */ in ecc_point_double_jacobian()
1134 /* t1 = B * (A - x3) */ in ecc_point_double_jacobian()
1136 /* t4 = B * (A - x3) - y1^4 = y3 */ in ecc_point_double_jacobian()
1160 const unsigned int ndigits = curve->g.ndigits; in xycz_initial_double()
1187 const u64 *curve_prime = curve->p; in xycz_add()
1188 const unsigned int ndigits = curve->g.ndigits; in xycz_add()
1190 /* t5 = x2 - x1 */ in xycz_add()
1192 /* t5 = (x2 - x1)^2 = A */ in xycz_add()
1198 /* t4 = y2 - y1 */ in xycz_add()
1200 /* t5 = (y2 - y1)^2 = D */ in xycz_add()
1203 /* t5 = D - B */ in xycz_add()
1205 /* t5 = D - B - C = x3 */ in xycz_add()
1207 /* t3 = C - B */ in xycz_add()
1209 /* t2 = y1*(C - B) */ in xycz_add()
1211 /* t3 = B - x3 */ in xycz_add()
1213 /* t4 = (y2 - y1)*(B - x3) */ in xycz_add()
1222 * Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
1223 * or P => P - Q, Q => P + Q
1232 const u64 *curve_prime = curve->p; in xycz_add_c()
1233 const unsigned int ndigits = curve->g.ndigits; in xycz_add_c()
1235 /* t5 = x2 - x1 */ in xycz_add_c()
1237 /* t5 = (x2 - x1)^2 = A */ in xycz_add_c()
1245 /* t4 = y2 - y1 */ in xycz_add_c()
1248 /* t6 = C - B */ in xycz_add_c()
1250 /* t2 = y1 * (C - B) */ in xycz_add_c()
1254 /* t3 = (y2 - y1)^2 */ in xycz_add_c()
1259 /* t7 = B - x3 */ in xycz_add_c()
1261 /* t4 = (y2 - y1)*(B - x3) */ in xycz_add_c()
1270 /* t6 = x3' - B */ in xycz_add_c()
1272 /* t6 = (y2 + y1)*(x3' - B) */ in xycz_add_c()
1290 u64 *curve_prime = curve->p; in ecc_point_mult()
1295 carry = vli_add(sk[0], scalar, curve->n, ndigits); in ecc_point_mult()
1296 vli_add(sk[1], sk[0], curve->n, ndigits); in ecc_point_mult()
1300 vli_set(rx[1], point->x, ndigits); in ecc_point_mult()
1301 vli_set(ry[1], point->y, ndigits); in ecc_point_mult()
1305 for (i = num_bits - 2; i > 0; i--) { in ecc_point_mult()
1307 xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve); in ecc_point_mult()
1308 xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve); in ecc_point_mult()
1312 xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve); in ecc_point_mult()
1315 /* X1 - X0 */ in ecc_point_mult()
1317 /* Yb * (X1 - X0) */ in ecc_point_mult()
1318 vli_mod_mult_fast(z, z, ry[1 - nb], curve); in ecc_point_mult()
1319 /* xP * Yb * (X1 - X0) */ in ecc_point_mult()
1320 vli_mod_mult_fast(z, z, point->x, curve); in ecc_point_mult()
1322 /* 1 / (xP * Yb * (X1 - X0)) */ in ecc_point_mult()
1323 vli_mod_inv(z, z, curve_prime, point->ndigits); in ecc_point_mult()
1325 /* yP / (xP * Yb * (X1 - X0)) */ in ecc_point_mult()
1326 vli_mod_mult_fast(z, z, point->y, curve); in ecc_point_mult()
1327 /* Xb * yP / (xP * Yb * (X1 - X0)) */ in ecc_point_mult()
1328 vli_mod_mult_fast(z, z, rx[1 - nb], curve); in ecc_point_mult()
1331 xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve); in ecc_point_mult()
1335 vli_set(result->x, rx[0], ndigits); in ecc_point_mult()
1336 vli_set(result->y, ry[0], ndigits); in ecc_point_mult()
1347 unsigned int ndigits = curve->g.ndigits; in ecc_point_add()
1349 vli_set(result->x, q->x, ndigits); in ecc_point_add()
1350 vli_set(result->y, q->y, ndigits); in ecc_point_add()
1351 vli_mod_sub(z, result->x, p->x, curve->p, ndigits); in ecc_point_add()
1352 vli_set(px, p->x, ndigits); in ecc_point_add()
1353 vli_set(py, p->y, ndigits); in ecc_point_add()
1354 xycz_add(px, py, result->x, result->y, curve); in ecc_point_add()
1355 vli_mod_inv(z, z, curve->p, ndigits); in ecc_point_add()
1356 apply_z(result->x, result->y, z, curve); in ecc_point_add()
1360 * Based on: Kenneth MacKay's micro-ecc (2014).
1369 u64 *rx = result->x; in ecc_point_mult_shamir()
1370 u64 *ry = result->y; in ecc_point_mult_shamir()
1371 unsigned int ndigits = curve->g.ndigits; in ecc_point_mult_shamir()
1386 i = num_bits - 1; in ecc_point_mult_shamir()
1391 vli_set(rx, point->x, ndigits); in ecc_point_mult_shamir()
1392 vli_set(ry, point->y, ndigits); in ecc_point_mult_shamir()
1393 vli_clear(z + 1, ndigits - 1); in ecc_point_mult_shamir()
1396 for (--i; i >= 0; i--) { in ecc_point_mult_shamir()
1406 vli_set(tx, point->x, ndigits); in ecc_point_mult_shamir()
1407 vli_set(ty, point->y, ndigits); in ecc_point_mult_shamir()
1409 vli_mod_sub(tz, rx, tx, curve->p, ndigits); in ecc_point_mult_shamir()
1414 vli_mod_inv(z, z, curve->p, ndigits); in ecc_point_mult_shamir()
1426 return -EINVAL; in __ecc_is_key_valid()
1428 if (curve->g.ndigits != ndigits) in __ecc_is_key_valid()
1429 return -EINVAL; in __ecc_is_key_valid()
1431 /* Make sure the private key is in the range [2, n-3]. */ in __ecc_is_key_valid()
1432 if (vli_cmp(one, private_key, ndigits) != -1) in __ecc_is_key_valid()
1433 return -EINVAL; in __ecc_is_key_valid()
1434 vli_sub(res, curve->n, one, ndigits); in __ecc_is_key_valid()
1437 return -EINVAL; in __ecc_is_key_valid()
1451 return -EINVAL; in ecc_is_key_valid()
1459 * equivalent to that described in FIPS 186-4, Appendix B.4.1.
1463 * 0 <= c mod(n-1) <= n-2 and implies that
1464 * 1 <= d <= n-1
1467 * [1, n-1].
1474 unsigned int nbits = vli_num_bits(curve->n, ndigits); in ecc_gen_privkey()
1477 /* Check that N is included in Table 1 of FIPS 186-4, section 6.1.1 */ in ecc_gen_privkey()
1479 return -EINVAL; in ecc_gen_privkey()
1482 * FIPS 186-4 recommends that the private key should be obtained from a in ecc_gen_privkey()
1486 * The maximum security strength identified by NIST SP800-57pt1r4 for in ecc_gen_privkey()
1493 return -EFAULT; in ecc_gen_privkey()
1502 return -EINVAL; in ecc_gen_privkey()
1514 struct ecc_point *pk; in ecc_make_pub_key() local
1519 ret = -EINVAL; in ecc_make_pub_key()
1525 pk = ecc_alloc_point(ndigits); in ecc_make_pub_key()
1526 if (!pk) { in ecc_make_pub_key()
1527 ret = -ENOMEM; in ecc_make_pub_key()
1531 ecc_point_mult(pk, &curve->g, priv, NULL, curve, ndigits); in ecc_make_pub_key()
1533 /* SP800-56A rev 3 5.6.2.1.3 key check */ in ecc_make_pub_key()
1534 if (ecc_is_pubkey_valid_full(curve, pk)) { in ecc_make_pub_key()
1535 ret = -EAGAIN; in ecc_make_pub_key()
1539 ecc_swap_digits(pk->x, public_key, ndigits); in ecc_make_pub_key()
1540 ecc_swap_digits(pk->y, &public_key[ndigits], ndigits); in ecc_make_pub_key()
1543 ecc_free_point(pk); in ecc_make_pub_key()
1549 /* SP800-56A section 5.6.2.3.4 partial verification: ephemeral keys only */
1551 struct ecc_point *pk) in ecc_is_pubkey_valid_partial() argument
1555 if (WARN_ON(pk->ndigits != curve->g.ndigits)) in ecc_is_pubkey_valid_partial()
1556 return -EINVAL; in ecc_is_pubkey_valid_partial()
1559 if (ecc_point_is_zero(pk)) in ecc_is_pubkey_valid_partial()
1560 return -EINVAL; in ecc_is_pubkey_valid_partial()
1562 /* Check 2: Verify key is in the range [1, p-1]. */ in ecc_is_pubkey_valid_partial()
1563 if (vli_cmp(curve->p, pk->x, pk->ndigits) != 1) in ecc_is_pubkey_valid_partial()
1564 return -EINVAL; in ecc_is_pubkey_valid_partial()
1565 if (vli_cmp(curve->p, pk->y, pk->ndigits) != 1) in ecc_is_pubkey_valid_partial()
1566 return -EINVAL; in ecc_is_pubkey_valid_partial()
1569 vli_mod_square_fast(yy, pk->y, curve); /* y^2 */ in ecc_is_pubkey_valid_partial()
1570 vli_mod_square_fast(xxx, pk->x, curve); /* x^2 */ in ecc_is_pubkey_valid_partial()
1571 vli_mod_mult_fast(xxx, xxx, pk->x, curve); /* x^3 */ in ecc_is_pubkey_valid_partial()
1572 vli_mod_mult_fast(w, curve->a, pk->x, curve); /* a·x */ in ecc_is_pubkey_valid_partial()
1573 vli_mod_add(w, w, curve->b, curve->p, pk->ndigits); /* a·x + b */ in ecc_is_pubkey_valid_partial()
1574 vli_mod_add(w, w, xxx, curve->p, pk->ndigits); /* x^3 + a·x + b */ in ecc_is_pubkey_valid_partial()
1575 if (vli_cmp(yy, w, pk->ndigits) != 0) /* Equation */ in ecc_is_pubkey_valid_partial()
1576 return -EINVAL; in ecc_is_pubkey_valid_partial()
1582 /* SP800-56A section 5.6.2.3.3 full verification */
1584 struct ecc_point *pk) in ecc_is_pubkey_valid_full() argument
1589 int ret = ecc_is_pubkey_valid_partial(curve, pk); in ecc_is_pubkey_valid_full()
1595 nQ = ecc_alloc_point(pk->ndigits); in ecc_is_pubkey_valid_full()
1597 return -ENOMEM; in ecc_is_pubkey_valid_full()
1599 ecc_point_mult(nQ, pk, curve->n, NULL, curve, pk->ndigits); in ecc_is_pubkey_valid_full()
1601 ret = -EINVAL; in ecc_is_pubkey_valid_full()
1614 struct ecc_point *product, *pk; in crypto_ecdh_shared_secret() local
1622 ret = -EINVAL; in crypto_ecdh_shared_secret()
1630 pk = ecc_alloc_point(ndigits); in crypto_ecdh_shared_secret()
1631 if (!pk) { in crypto_ecdh_shared_secret()
1632 ret = -ENOMEM; in crypto_ecdh_shared_secret()
1636 ecc_swap_digits(public_key, pk->x, ndigits); in crypto_ecdh_shared_secret()
1637 ecc_swap_digits(&public_key[ndigits], pk->y, ndigits); in crypto_ecdh_shared_secret()
1638 ret = ecc_is_pubkey_valid_partial(curve, pk); in crypto_ecdh_shared_secret()
1646 ret = -ENOMEM; in crypto_ecdh_shared_secret()
1650 ecc_point_mult(product, pk, priv, rand_z, curve, ndigits); in crypto_ecdh_shared_secret()
1653 ret = -EFAULT; in crypto_ecdh_shared_secret()
1657 ecc_swap_digits(product->x, secret, ndigits); in crypto_ecdh_shared_secret()
1664 ecc_free_point(pk); in crypto_ecdh_shared_secret()