Lines Matching +full:0 +full:- +full:9 +full:a +full:- +full:z

17  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
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()
126 for (i = 0; i < ndigits; i++) in vli_clear()
127 vli[i] = 0; in vli_clear()
130 /* Returns true if vli == 0, false otherwise. */
135 for (i = 0; i < ndigits; i++) { in vli_is_zero()
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()
176 if (num_digits == 0) in vli_num_bits()
177 return 0; in vli_num_bits()
179 digit = vli[num_digits - 1]; in vli_num_bits()
180 for (i = 0; digit; i++) in vli_num_bits()
183 return ((num_digits - 1) * 64 + i); in vli_num_bits()
193 for (i = 0; i < ndigits; i++) in vli_from_be64()
194 dest[i] = get_unaligned_be64(&from[ndigits - 1 - i]); in vli_from_be64()
203 for (i = 0; i < ndigits; i++) in vli_from_le64()
213 for (i = 0; i < ndigits; i++) in vli_set()
217 /* Returns sign of left - right. */
222 for (i = ndigits - 1; i >= 0; i--) { in vli_cmp()
226 return -1; in vli_cmp()
229 return 0; in vli_cmp()
234 * (if result == in). 0 < shift < 64.
239 u64 carry = 0; in vli_lshift()
242 for (i = 0; i < ndigits; i++) { in vli_lshift()
246 carry = temp >> (64 - shift); in vli_lshift()
256 u64 carry = 0; in vli_rshift1()
260 while (vli-- > end) { in vli_rshift1()
271 u64 carry = 0; in vli_add()
274 for (i = 0; i < ndigits; i++) { in vli_add()
294 for (i = 0; i < ndigits; i++) { in vli_uadd()
309 /* Computes result = left - right, returning borrow. Can modify in place. */
313 u64 borrow = 0; in vli_sub()
316 for (i = 0; i < ndigits; i++) { in vli_sub()
319 diff = left[i] - right[i] - borrow; in vli_sub()
330 /* Computes result = left - right, returning borrow. Can modify in place. */
337 for (i = 0; i < ndigits; i++) { in vli_usub()
340 diff = left[i] - borrow; in vli_usub()
359 u64 a0 = left & 0xffffffffull; in mul_64_64()
361 u64 b0 = right & 0xffffffffull; in mul_64_64()
373 m3 += 0x100000000ull; in mul_64_64()
375 result.m_low = (m0 & 0xffffffffull) | (m2 << 32); in mul_64_64()
381 static uint128_t add_128_128(uint128_t a, uint128_t b) in add_128_128() argument
385 result.m_low = a.m_low + b.m_low; in add_128_128()
386 result.m_high = a.m_high + b.m_high + (result.m_low < a.m_low); in add_128_128()
394 uint128_t r01 = { 0, 0 }; in vli_mult()
395 u64 r2 = 0; in vli_mult()
401 for (k = 0; k < ndigits * 2 - 1; k++) { in vli_mult()
405 min = 0; in vli_mult()
407 min = (k + 1) - ndigits; in vli_mult()
412 product = mul_64_64(left[i], right[k - i]); in vli_mult()
421 r2 = 0; in vli_mult()
424 result[ndigits * 2 - 1] = r01.m_low; in vli_mult()
427 /* Compute product = left * right, for a small right value. */
431 uint128_t r01 = { 0 }; in vli_umult()
434 for (k = 0; k < ndigits; k++) { in vli_umult()
442 r01.m_high = 0; in vli_umult()
446 result[k] = 0; in vli_umult()
451 uint128_t r01 = { 0, 0 }; in vli_square()
452 u64 r2 = 0; in vli_square()
455 for (k = 0; k < ndigits * 2 - 1; k++) { in vli_square()
459 min = 0; 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()
482 r2 = 0; in vli_square()
485 result[ndigits * 2 - 1] = r01.m_low; in vli_square()
501 if (carry || vli_cmp(result, mod, ndigits) >= 0) in vli_mod_add()
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)
526 * R. Crandall, C. Pomerance. Prime Numbers: A Computational Perspective.
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()
545 while (vli_cmp(r, t, ndigits * 2) >= 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).
556 * A. Menezes, P. van Oorschot, S. Vanstone. Handbook of Applied Cryptography.
567 u64 c2 = mod[0] * 2; in vli_mmod_special2()
583 r[ndigits - 1] &= (1ull << 63) - 1; in vli_mmod_special2()
589 vli_uadd(qc, qc, mod[0], ndigits * 2); in vli_mmod_special2()
594 qc[ndigits - 1] &= (1ull << 63) - 1; in vli_mmod_special2()
602 while (vli_cmp(r, m, ndigits * 2) >= 0) in vli_mmod_special2()
610 * Reference: Ken MacKay's micro-ecc.
619 u64 carry = 0; in vli_mmod_slow()
622 int shift = (ndigits * 2 * 64) - vli_num_bits(mod, ndigits); in vli_mmod_slow()
627 if (bit_shift > 0) { in vli_mmod_slow()
628 for (i = 0; i < ndigits; ++i) { 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()
636 u64 borrow = 0; in vli_mmod_slow()
639 for (j = 0; j < ndigits * 2; ++j) { 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
700 tmp[0] = 0; in vli_mmod_fast_192()
705 tmp[0] = tmp[1] = product[5]; in vli_mmod_fast_192()
706 tmp[2] = 0; in vli_mmod_fast_192()
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
726 tmp[0] = 0; in vli_mmod_fast_256()
727 tmp[1] = product[5] & 0xffffffff00000000ull; in vli_mmod_fast_256()
741 tmp[0] = product[4]; in vli_mmod_fast_256()
742 tmp[1] = product[5] & 0xffffffff; in vli_mmod_fast_256()
743 tmp[2] = 0; in vli_mmod_fast_256()
748 tmp[0] = (product[4] >> 32) | (product[5] << 32); in vli_mmod_fast_256()
749 tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull); in vli_mmod_fast_256()
755 tmp[0] = (product[5] >> 32) | (product[6] << 32); in vli_mmod_fast_256()
757 tmp[2] = 0; in vli_mmod_fast_256()
758 tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32); in vli_mmod_fast_256()
759 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_256()
762 tmp[0] = product[6]; in vli_mmod_fast_256()
764 tmp[2] = 0; in vli_mmod_fast_256()
765 tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull); in vli_mmod_fast_256()
766 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_256()
769 tmp[0] = (product[6] >> 32) | (product[7] << 32); in vli_mmod_fast_256()
773 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_256()
776 tmp[0] = product[7]; in vli_mmod_fast_256()
777 tmp[1] = product[4] & 0xffffffff00000000ull; in vli_mmod_fast_256()
779 tmp[3] = product[6] & 0xffffffff00000000ull; in vli_mmod_fast_256()
780 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_256()
782 if (carry < 0) { in vli_mmod_fast_256()
785 } while (carry < 0); in vli_mmod_fast_256()
788 carry -= vli_sub(result, result, curve_prime, ndigits); in vli_mmod_fast_256()
793 #define AND64H(x64) (x64 & 0xffFFffFF00000000ull)
794 #define AND64L(x64) (x64 & 0x00000000ffFFffFFull)
809 tmp[0] = 0; // 0 || 0 in vli_mmod_fast_384()
810 tmp[1] = 0; // 0 || 0 in vli_mmod_fast_384()
812 tmp[3] = product[11]>>32; // 0 ||a23 in vli_mmod_fast_384()
813 tmp[4] = 0; // 0 || 0 in vli_mmod_fast_384()
814 tmp[5] = 0; // 0 || 0 in vli_mmod_fast_384()
819 tmp[0] = product[6]; //a13||a12 in vli_mmod_fast_384()
822 tmp[3] = product[9]; //a19||a18 in vli_mmod_fast_384()
828 tmp[0] = SL32OR32(product[11], (product[10]>>32)); //a22||a21 in vli_mmod_fast_384()
832 tmp[4] = SL32OR32(product[9], (product[8]>>32)); //a18||a17 in vli_mmod_fast_384()
833 tmp[5] = SL32OR32(product[10], (product[9]>>32)); //a20||a19 in vli_mmod_fast_384()
837 tmp[0] = AND64H(product[11]); //a23|| 0 in vli_mmod_fast_384()
838 tmp[1] = (product[10]<<32); //a20|| 0 in vli_mmod_fast_384()
842 tmp[5] = product[9]; //a19||a18 in vli_mmod_fast_384()
846 tmp[0] = 0; // 0|| 0 in vli_mmod_fast_384()
847 tmp[1] = 0; // 0|| 0 in vli_mmod_fast_384()
850 tmp[4] = 0; // 0|| 0 in vli_mmod_fast_384()
851 tmp[5] = 0; // 0|| 0 in vli_mmod_fast_384()
855 tmp[0] = AND64L(product[10]); // 0 ||a20 in vli_mmod_fast_384()
856 tmp[1] = AND64H(product[10]); //a21|| 0 in vli_mmod_fast_384()
858 tmp[3] = 0; // 0 || 0 in vli_mmod_fast_384()
859 tmp[4] = 0; // 0 || 0 in vli_mmod_fast_384()
860 tmp[5] = 0; // 0 || 0 in vli_mmod_fast_384()
864 tmp[0] = SL32OR32(product[6], (product[11]>>32)); //a12||a23 in vli_mmod_fast_384()
867 tmp[3] = SL32OR32(product[9], (product[8]>>32)); //a18||a17 in vli_mmod_fast_384()
868 tmp[4] = SL32OR32(product[10], (product[9]>>32)); //a20||a19 in vli_mmod_fast_384()
870 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_384()
873 tmp[0] = (product[10]<<32); //a20|| 0 in vli_mmod_fast_384()
875 tmp[2] = (product[11]>>32); // 0 ||a23 in vli_mmod_fast_384()
876 tmp[3] = 0; // 0 || 0 in vli_mmod_fast_384()
877 tmp[4] = 0; // 0 || 0 in vli_mmod_fast_384()
878 tmp[5] = 0; // 0 || 0 in vli_mmod_fast_384()
879 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_384()
882 tmp[0] = 0; // 0 || 0 in vli_mmod_fast_384()
883 tmp[1] = AND64H(product[11]); //a23|| 0 in vli_mmod_fast_384()
884 tmp[2] = product[11]>>32; // 0 ||a23 in vli_mmod_fast_384()
885 tmp[3] = 0; // 0 || 0 in vli_mmod_fast_384()
886 tmp[4] = 0; // 0 || 0 in vli_mmod_fast_384()
887 tmp[5] = 0; // 0 || 0 in vli_mmod_fast_384()
888 carry -= vli_sub(result, result, tmp, ndigits); in vli_mmod_fast_384()
890 if (carry < 0) { in vli_mmod_fast_384()
893 } while (carry < 0); 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()
985 #define EVEN(vli) (!(vli[0] & 1))
988 * https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf
993 u64 a[ECC_MAX_DIGITS], b[ECC_MAX_DIGITS]; in vli_mod_inv() local
1003 vli_set(a, input, ndigits); in vli_mod_inv()
1006 u[0] = 1; in vli_mod_inv()
1009 while ((cmp_result = vli_cmp(a, b, ndigits)) != 0) { in vli_mod_inv()
1010 carry = 0; in vli_mod_inv()
1012 if (EVEN(a)) { in vli_mod_inv()
1013 vli_rshift1(a, ndigits); in vli_mod_inv()
1020 u[ndigits - 1] |= 0x8000000000000000ull; in vli_mod_inv()
1029 v[ndigits - 1] |= 0x8000000000000000ull; in vli_mod_inv()
1030 } else if (cmp_result > 0) { in vli_mod_inv()
1031 vli_sub(a, a, b, ndigits); in vli_mod_inv()
1032 vli_rshift1(a, ndigits); in vli_mod_inv()
1034 if (vli_cmp(u, v, ndigits) < 0) in vli_mod_inv()
1043 u[ndigits - 1] |= 0x8000000000000000ull; in vli_mod_inv()
1045 vli_sub(b, b, a, ndigits); in vli_mod_inv()
1048 if (vli_cmp(v, u, ndigits) < 0) 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
1083 /* t1 = x, t2 = y, t3 = z */ in ecc_point_double_jacobian()
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()
1094 /* t5 = x1*y1^2 = A */ 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()
1116 if (vli_test_bit(x1, 0)) { 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()
1144 /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
1145 static void apply_z(u64 *x1, u64 *y1, u64 *z, const struct ecc_curve *curve) in apply_z() argument
1149 vli_mod_square_fast(t1, z, curve); /* z^2 */ in apply_z()
1150 vli_mod_mult_fast(x1, x1, t1, curve); /* x1 * z^2 */ in apply_z()
1151 vli_mod_mult_fast(t1, t1, z, curve); /* z^3 */ in apply_z()
1152 vli_mod_mult_fast(y1, y1, t1, curve); /* y1 * z^3 */ in apply_z()
1159 u64 z[ECC_MAX_DIGITS]; in xycz_initial_double() local
1160 const unsigned int ndigits = curve->g.ndigits; in xycz_initial_double()
1165 vli_clear(z, ndigits); in xycz_initial_double()
1166 z[0] = 1; in xycz_initial_double()
1169 vli_set(z, p_initial_z, ndigits); in xycz_initial_double()
1171 apply_z(x1, y1, z, curve); in xycz_initial_double()
1173 ecc_point_double_jacobian(x1, y1, z, curve); in xycz_initial_double()
1175 apply_z(x2, y2, z, curve); in xycz_initial_double()
1178 /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
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()
1194 /* t1 = x1*A = B */ in xycz_add()
1196 /* t3 = x2*A = C */ 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()
1221 /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
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()
1239 /* t1 = x1*A = B */ in xycz_add_c()
1241 /* t3 = x2*A = C */ 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()
1288 u64 z[ECC_MAX_DIGITS]; in ecc_point_mult() local
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()
1303 xycz_initial_double(rx[1], ry[1], rx[0], ry[0], initial_z, curve); 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()
1311 nb = !vli_test_bit(scalar, 0); in ecc_point_mult()
1312 xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve); in ecc_point_mult()
1314 /* Find final 1/Z value. */ in ecc_point_mult()
1315 /* X1 - X0 */ in ecc_point_mult()
1316 vli_mod_sub(z, rx[1], rx[0], curve_prime, ndigits); 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()
1329 /* End 1/Z calculation */ in ecc_point_mult()
1331 xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve); in ecc_point_mult()
1333 apply_z(rx[0], ry[0], z, 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()
1344 u64 z[ECC_MAX_DIGITS]; in ecc_point_add() local
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).
1367 u64 z[ECC_MAX_DIGITS]; in ecc_point_mult_shamir() local
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()
1373 struct ecc_point sum = ECC_POINT_INIT(sump[0], sump[1], ndigits); in ecc_point_mult_shamir()
1380 points[0] = NULL; 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()
1394 z[0] = 1; in ecc_point_mult_shamir()
1396 for (--i; i >= 0; i--) { in ecc_point_mult_shamir()
1397 ecc_point_double_jacobian(rx, ry, z, curve); 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()
1408 apply_z(tx, ty, z, curve); in ecc_point_mult_shamir()
1409 vli_mod_sub(tz, rx, tx, curve->p, ndigits); in ecc_point_mult_shamir()
1411 vli_mod_mult_fast(z, z, tz, curve); in ecc_point_mult_shamir()
1414 vli_mod_inv(z, z, curve->p, ndigits); in ecc_point_mult_shamir()
1415 apply_z(rx, ry, z, curve); 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()
1439 return 0; 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.
1461 * d = (c mod(n–1)) + 1 where c is a string of random bits, 64 bits longer
1463 * 0 <= c mod(n-1) <= n-2 and implies that
1464 * 1 <= d <= n-1
1466 * This method generates a private key uniformly distributed in the range
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()
1483 * RBG with a security strength equal to or greater than the security in ecc_gen_privkey()
1486 * The maximum security strength identified by NIST SP800-57pt1r4 for in ecc_gen_privkey()
1489 * This condition is met by the default RNG because it selects a favored in ecc_gen_privkey()
1490 * DRBG with a security strength of 256. in ecc_gen_privkey()
1493 return -EFAULT; in ecc_gen_privkey()
1502 return -EINVAL; in ecc_gen_privkey()
1506 return 0; in ecc_gen_privkey()
1513 int ret = 0; in ecc_make_pub_key()
1519 ret = -EINVAL; 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()
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()
1549 /* SP800-56A section 5.6.2.3.4 partial verification: ephemeral keys only */
1555 if (WARN_ON(pk->ndigits != curve->g.ndigits)) in ecc_is_pubkey_valid_partial()
1556 return -EINVAL; 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()
1568 /* Check 3: Verify that y^2 == (x^3 + a·x + b) mod p */ 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()
1578 return 0; in ecc_is_pubkey_valid_partial()
1582 /* SP800-56A section 5.6.2.3.3 full verification */
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()
1613 int ret = 0; in crypto_ecdh_shared_secret()
1622 ret = -EINVAL; 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()
1646 ret = -ENOMEM; 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()