andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "ecp.h" andre@0: #include "mpi.h" andre@0: #include "mplogic.h" andre@0: #include "mpi-priv.h" andre@0: andre@0: #define ECP521_DIGITS ECL_CURVE_DIGITS(521) andre@0: andre@0: /* Fast modular reduction for p521 = 2^521 - 1. a can be r. Uses andre@0: * algorithm 2.31 from Hankerson, Menezes, Vanstone. Guide to andre@0: * Elliptic Curve Cryptography. */ andre@0: static mp_err andre@0: ec_GFp_nistp521_mod(const mp_int *a, mp_int *r, const GFMethod *meth) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: int a_bits = mpl_significant_bits(a); andre@0: int i; andre@0: andre@0: /* m1, m2 are statically-allocated mp_int of exactly the size we need */ andre@0: mp_int m1; andre@0: andre@0: mp_digit s1[ECP521_DIGITS] = { 0 }; andre@0: andre@0: MP_SIGN(&m1) = MP_ZPOS; andre@0: MP_ALLOC(&m1) = ECP521_DIGITS; andre@0: MP_USED(&m1) = ECP521_DIGITS; andre@0: MP_DIGITS(&m1) = s1; andre@0: andre@0: if (a_bits < 521) { andre@0: if (a==r) return MP_OKAY; andre@0: return mp_copy(a, r); andre@0: } andre@0: /* for polynomials larger than twice the field size or polynomials andre@0: * not using all words, use regular reduction */ andre@0: if (a_bits > (521*2)) { andre@0: MP_CHECKOK(mp_mod(a, &meth->irr, r)); andre@0: } else { andre@0: #define FIRST_DIGIT (ECP521_DIGITS-1) andre@0: for (i = FIRST_DIGIT; i < MP_USED(a)-1; i++) { andre@0: s1[i-FIRST_DIGIT] = (MP_DIGIT(a, i) >> 9) andre@0: | (MP_DIGIT(a, 1+i) << (MP_DIGIT_BIT-9)); andre@0: } andre@0: s1[i-FIRST_DIGIT] = MP_DIGIT(a, i) >> 9; andre@0: andre@0: if ( a != r ) { andre@0: MP_CHECKOK(s_mp_pad(r,ECP521_DIGITS)); andre@0: for (i = 0; i < ECP521_DIGITS; i++) { andre@0: MP_DIGIT(r,i) = MP_DIGIT(a, i); andre@0: } andre@0: } andre@0: MP_USED(r) = ECP521_DIGITS; andre@0: MP_DIGIT(r,FIRST_DIGIT) &= 0x1FF; andre@0: andre@0: MP_CHECKOK(s_mp_add(r, &m1)); andre@0: if (MP_DIGIT(r, FIRST_DIGIT) & 0x200) { andre@0: MP_CHECKOK(s_mp_add_d(r,1)); andre@0: MP_DIGIT(r,FIRST_DIGIT) &= 0x1FF; andre@0: } else if (s_mp_cmp(r, &meth->irr) == 0) { andre@0: mp_zero(r); andre@0: } andre@0: s_mp_clamp(r); andre@0: } andre@0: andre@0: CLEANUP: andre@0: return res; andre@0: } andre@0: andre@0: /* Compute the square of polynomial a, reduce modulo p521. Store the andre@0: * result in r. r could be a. Uses optimized modular reduction for p521. andre@0: */ andre@0: static mp_err andre@0: ec_GFp_nistp521_sqr(const mp_int *a, mp_int *r, const GFMethod *meth) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: andre@0: MP_CHECKOK(mp_sqr(a, r)); andre@0: MP_CHECKOK(ec_GFp_nistp521_mod(r, r, meth)); andre@0: CLEANUP: andre@0: return res; andre@0: } andre@0: andre@0: /* Compute the product of two polynomials a and b, reduce modulo p521. andre@0: * Store the result in r. r could be a or b; a could be b. Uses andre@0: * optimized modular reduction for p521. */ andre@0: static mp_err andre@0: ec_GFp_nistp521_mul(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: andre@0: MP_CHECKOK(mp_mul(a, b, r)); andre@0: MP_CHECKOK(ec_GFp_nistp521_mod(r, r, meth)); andre@0: CLEANUP: andre@0: return res; andre@0: } andre@0: andre@0: /* Divides two field elements. If a is NULL, then returns the inverse of andre@0: * b. */ andre@0: static mp_err andre@0: ec_GFp_nistp521_div(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: mp_int t; andre@0: andre@0: /* If a is NULL, then return the inverse of b, otherwise return a/b. */ andre@0: if (a == NULL) { andre@0: return mp_invmod(b, &meth->irr, r); andre@0: } else { andre@0: /* MPI doesn't support divmod, so we implement it using invmod and andre@0: * mulmod. */ andre@0: MP_CHECKOK(mp_init(&t)); andre@0: MP_CHECKOK(mp_invmod(b, &meth->irr, &t)); andre@0: MP_CHECKOK(mp_mul(a, &t, r)); andre@0: MP_CHECKOK(ec_GFp_nistp521_mod(r, r, meth)); andre@0: CLEANUP: andre@0: mp_clear(&t); andre@0: return res; andre@0: } andre@0: } andre@0: andre@0: /* Wire in fast field arithmetic and precomputation of base point for andre@0: * named curves. */ andre@0: mp_err andre@0: ec_group_set_gfp521(ECGroup *group, ECCurveName name) andre@0: { andre@0: if (name == ECCurve_NIST_P521) { andre@0: group->meth->field_mod = &ec_GFp_nistp521_mod; andre@0: group->meth->field_mul = &ec_GFp_nistp521_mul; andre@0: group->meth->field_sqr = &ec_GFp_nistp521_sqr; andre@0: group->meth->field_div = &ec_GFp_nistp521_div; andre@0: } andre@0: return MP_OKAY; andre@0: }