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: /* Fast modular reduction for p384 = 2^384 - 2^128 - 2^96 + 2^32 - 1. a can be r. andre@0: * Uses algorithm 2.30 from Hankerson, Menezes, Vanstone. Guide to andre@0: * Elliptic Curve Cryptography. */ andre@0: static mp_err andre@0: ec_GFp_nistp384_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 m[10]; andre@0: andre@0: #ifdef ECL_THIRTY_TWO_BIT andre@0: mp_digit s[10][12]; andre@0: for (i = 0; i < 10; i++) { andre@0: MP_SIGN(&m[i]) = MP_ZPOS; andre@0: MP_ALLOC(&m[i]) = 12; andre@0: MP_USED(&m[i]) = 12; andre@0: MP_DIGITS(&m[i]) = s[i]; andre@0: } andre@0: #else andre@0: mp_digit s[10][6]; andre@0: for (i = 0; i < 10; i++) { andre@0: MP_SIGN(&m[i]) = MP_ZPOS; andre@0: MP_ALLOC(&m[i]) = 6; andre@0: MP_USED(&m[i]) = 6; andre@0: MP_DIGITS(&m[i]) = s[i]; andre@0: } andre@0: #endif andre@0: andre@0: #ifdef ECL_THIRTY_TWO_BIT 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 > 768) || (a_bits <= 736)) { andre@0: MP_CHECKOK(mp_mod(a, &meth->irr, r)); andre@0: } else { andre@0: for (i = 0; i < 12; i++) { andre@0: s[0][i] = MP_DIGIT(a, i); andre@0: } andre@0: s[1][0] = 0; andre@0: s[1][1] = 0; andre@0: s[1][2] = 0; andre@0: s[1][3] = 0; andre@0: s[1][4] = MP_DIGIT(a, 21); andre@0: s[1][5] = MP_DIGIT(a, 22); andre@0: s[1][6] = MP_DIGIT(a, 23); andre@0: s[1][7] = 0; andre@0: s[1][8] = 0; andre@0: s[1][9] = 0; andre@0: s[1][10] = 0; andre@0: s[1][11] = 0; andre@0: for (i = 0; i < 12; i++) { andre@0: s[2][i] = MP_DIGIT(a, i+12); andre@0: } andre@0: s[3][0] = MP_DIGIT(a, 21); andre@0: s[3][1] = MP_DIGIT(a, 22); andre@0: s[3][2] = MP_DIGIT(a, 23); andre@0: for (i = 3; i < 12; i++) { andre@0: s[3][i] = MP_DIGIT(a, i+9); andre@0: } andre@0: s[4][0] = 0; andre@0: s[4][1] = MP_DIGIT(a, 23); andre@0: s[4][2] = 0; andre@0: s[4][3] = MP_DIGIT(a, 20); andre@0: for (i = 4; i < 12; i++) { andre@0: s[4][i] = MP_DIGIT(a, i+8); andre@0: } andre@0: s[5][0] = 0; andre@0: s[5][1] = 0; andre@0: s[5][2] = 0; andre@0: s[5][3] = 0; andre@0: s[5][4] = MP_DIGIT(a, 20); andre@0: s[5][5] = MP_DIGIT(a, 21); andre@0: s[5][6] = MP_DIGIT(a, 22); andre@0: s[5][7] = MP_DIGIT(a, 23); andre@0: s[5][8] = 0; andre@0: s[5][9] = 0; andre@0: s[5][10] = 0; andre@0: s[5][11] = 0; andre@0: s[6][0] = MP_DIGIT(a, 20); andre@0: s[6][1] = 0; andre@0: s[6][2] = 0; andre@0: s[6][3] = MP_DIGIT(a, 21); andre@0: s[6][4] = MP_DIGIT(a, 22); andre@0: s[6][5] = MP_DIGIT(a, 23); andre@0: s[6][6] = 0; andre@0: s[6][7] = 0; andre@0: s[6][8] = 0; andre@0: s[6][9] = 0; andre@0: s[6][10] = 0; andre@0: s[6][11] = 0; andre@0: s[7][0] = MP_DIGIT(a, 23); andre@0: for (i = 1; i < 12; i++) { andre@0: s[7][i] = MP_DIGIT(a, i+11); andre@0: } andre@0: s[8][0] = 0; andre@0: s[8][1] = MP_DIGIT(a, 20); andre@0: s[8][2] = MP_DIGIT(a, 21); andre@0: s[8][3] = MP_DIGIT(a, 22); andre@0: s[8][4] = MP_DIGIT(a, 23); andre@0: s[8][5] = 0; andre@0: s[8][6] = 0; andre@0: s[8][7] = 0; andre@0: s[8][8] = 0; andre@0: s[8][9] = 0; andre@0: s[8][10] = 0; andre@0: s[8][11] = 0; andre@0: s[9][0] = 0; andre@0: s[9][1] = 0; andre@0: s[9][2] = 0; andre@0: s[9][3] = MP_DIGIT(a, 23); andre@0: s[9][4] = MP_DIGIT(a, 23); andre@0: s[9][5] = 0; andre@0: s[9][6] = 0; andre@0: s[9][7] = 0; andre@0: s[9][8] = 0; andre@0: s[9][9] = 0; andre@0: s[9][10] = 0; andre@0: s[9][11] = 0; andre@0: andre@0: MP_CHECKOK(mp_add(&m[0], &m[1], r)); andre@0: MP_CHECKOK(mp_add(r, &m[1], r)); andre@0: MP_CHECKOK(mp_add(r, &m[2], r)); andre@0: MP_CHECKOK(mp_add(r, &m[3], r)); andre@0: MP_CHECKOK(mp_add(r, &m[4], r)); andre@0: MP_CHECKOK(mp_add(r, &m[5], r)); andre@0: MP_CHECKOK(mp_add(r, &m[6], r)); andre@0: MP_CHECKOK(mp_sub(r, &m[7], r)); andre@0: MP_CHECKOK(mp_sub(r, &m[8], r)); andre@0: MP_CHECKOK(mp_submod(r, &m[9], &meth->irr, r)); andre@0: s_mp_clamp(r); andre@0: } andre@0: #else 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 > 768) || (a_bits <= 736)) { andre@0: MP_CHECKOK(mp_mod(a, &meth->irr, r)); andre@0: } else { andre@0: for (i = 0; i < 6; i++) { andre@0: s[0][i] = MP_DIGIT(a, i); andre@0: } andre@0: s[1][0] = 0; andre@0: s[1][1] = 0; andre@0: s[1][2] = (MP_DIGIT(a, 10) >> 32) | (MP_DIGIT(a, 11) << 32); andre@0: s[1][3] = MP_DIGIT(a, 11) >> 32; andre@0: s[1][4] = 0; andre@0: s[1][5] = 0; andre@0: for (i = 0; i < 6; i++) { andre@0: s[2][i] = MP_DIGIT(a, i+6); andre@0: } andre@0: s[3][0] = (MP_DIGIT(a, 10) >> 32) | (MP_DIGIT(a, 11) << 32); andre@0: s[3][1] = (MP_DIGIT(a, 11) >> 32) | (MP_DIGIT(a, 6) << 32); andre@0: for (i = 2; i < 6; i++) { andre@0: s[3][i] = (MP_DIGIT(a, i+4) >> 32) | (MP_DIGIT(a, i+5) << 32); andre@0: } andre@0: s[4][0] = (MP_DIGIT(a, 11) >> 32) << 32; andre@0: s[4][1] = MP_DIGIT(a, 10) << 32; andre@0: for (i = 2; i < 6; i++) { andre@0: s[4][i] = MP_DIGIT(a, i+4); andre@0: } andre@0: s[5][0] = 0; andre@0: s[5][1] = 0; andre@0: s[5][2] = MP_DIGIT(a, 10); andre@0: s[5][3] = MP_DIGIT(a, 11); andre@0: s[5][4] = 0; andre@0: s[5][5] = 0; andre@0: s[6][0] = (MP_DIGIT(a, 10) << 32) >> 32; andre@0: s[6][1] = (MP_DIGIT(a, 10) >> 32) << 32; andre@0: s[6][2] = MP_DIGIT(a, 11); andre@0: s[6][3] = 0; andre@0: s[6][4] = 0; andre@0: s[6][5] = 0; andre@0: s[7][0] = (MP_DIGIT(a, 11) >> 32) | (MP_DIGIT(a, 6) << 32); andre@0: for (i = 1; i < 6; i++) { andre@0: s[7][i] = (MP_DIGIT(a, i+5) >> 32) | (MP_DIGIT(a, i+6) << 32); andre@0: } andre@0: s[8][0] = MP_DIGIT(a, 10) << 32; andre@0: s[8][1] = (MP_DIGIT(a, 10) >> 32) | (MP_DIGIT(a, 11) << 32); andre@0: s[8][2] = MP_DIGIT(a, 11) >> 32; andre@0: s[8][3] = 0; andre@0: s[8][4] = 0; andre@0: s[8][5] = 0; andre@0: s[9][0] = 0; andre@0: s[9][1] = (MP_DIGIT(a, 11) >> 32) << 32; andre@0: s[9][2] = MP_DIGIT(a, 11) >> 32; andre@0: s[9][3] = 0; andre@0: s[9][4] = 0; andre@0: s[9][5] = 0; andre@0: andre@0: MP_CHECKOK(mp_add(&m[0], &m[1], r)); andre@0: MP_CHECKOK(mp_add(r, &m[1], r)); andre@0: MP_CHECKOK(mp_add(r, &m[2], r)); andre@0: MP_CHECKOK(mp_add(r, &m[3], r)); andre@0: MP_CHECKOK(mp_add(r, &m[4], r)); andre@0: MP_CHECKOK(mp_add(r, &m[5], r)); andre@0: MP_CHECKOK(mp_add(r, &m[6], r)); andre@0: MP_CHECKOK(mp_sub(r, &m[7], r)); andre@0: MP_CHECKOK(mp_sub(r, &m[8], r)); andre@0: MP_CHECKOK(mp_submod(r, &m[9], &meth->irr, r)); andre@0: s_mp_clamp(r); andre@0: } andre@0: #endif andre@0: andre@0: CLEANUP: andre@0: return res; andre@0: } andre@0: andre@0: /* Compute the square of polynomial a, reduce modulo p384. Store the andre@0: * result in r. r could be a. Uses optimized modular reduction for p384. andre@0: */ andre@0: static mp_err andre@0: ec_GFp_nistp384_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_nistp384_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 p384. andre@0: * Store the result in r. r could be a or b; a could be b. Uses andre@0: * optimized modular reduction for p384. */ andre@0: static mp_err andre@0: ec_GFp_nistp384_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_nistp384_mod(r, r, meth)); andre@0: CLEANUP: andre@0: return res; 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_gfp384(ECGroup *group, ECCurveName name) andre@0: { andre@0: if (name == ECCurve_NIST_P384) { andre@0: group->meth->field_mod = &ec_GFp_nistp384_mod; andre@0: group->meth->field_mul = &ec_GFp_nistp384_mul; andre@0: group->meth->field_sqr = &ec_GFp_nistp384_sqr; andre@0: } andre@0: return MP_OKAY; andre@0: }