OLD | NEW |
1 /* This Source Code Form is subject to the terms of the Mozilla Public | 1 /* This Source Code Form is subject to the terms of the Mozilla Public |
2 * License, v. 2.0. If a copy of the MPL was not distributed with this | 2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
5 /* A 32-bit implementation of the NIST P-256 elliptic curve. */ | 5 /* A 32-bit implementation of the NIST P-256 elliptic curve. */ |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "prtypes.h" | 9 #include "prtypes.h" |
10 #include "mpi.h" | 10 #include "mpi.h" |
(...skipping 1284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1295 static mp_err to_montgomery(felem out, const mp_int *in, const ECGroup *group) | 1295 static mp_err to_montgomery(felem out, const mp_int *in, const ECGroup *group) |
1296 { | 1296 { |
1297 /* There are no MPI functions for bitshift operations and we wish to shift | 1297 /* There are no MPI functions for bitshift operations and we wish to shift |
1298 * in 257 bits left so we move the digits 256-bits left and then multiply | 1298 * in 257 bits left so we move the digits 256-bits left and then multiply |
1299 * by two. | 1299 * by two. |
1300 */ | 1300 */ |
1301 mp_int in_shifted; | 1301 mp_int in_shifted; |
1302 int i; | 1302 int i; |
1303 mp_err res; | 1303 mp_err res; |
1304 | 1304 |
1305 mp_init(&in_shifted); | 1305 MP_CHECKOK(mp_init(&in_shifted)); |
1306 s_mp_pad(&in_shifted, MP_USED(in) + MP_DIGITS_IN_256_BITS); | 1306 MP_CHECKOK(s_mp_pad(&in_shifted, MP_USED(in) + MP_DIGITS_IN_256_BITS)); |
1307 memcpy(&MP_DIGIT(&in_shifted, MP_DIGITS_IN_256_BITS), | 1307 memcpy(&MP_DIGIT(&in_shifted, MP_DIGITS_IN_256_BITS), |
1308 MP_DIGITS(in), | 1308 MP_DIGITS(in), |
1309 MP_USED(in)*sizeof(mp_digit)); | 1309 MP_USED(in)*sizeof(mp_digit)); |
1310 mp_mul_2(&in_shifted, &in_shifted); | 1310 MP_CHECKOK(mp_mul_2(&in_shifted, &in_shifted)); |
1311 MP_CHECKOK(group->meth->field_mod(&in_shifted, &in_shifted, group->meth)); | 1311 MP_CHECKOK(group->meth->field_mod(&in_shifted, &in_shifted, group->meth)); |
1312 | 1312 |
1313 for (i = 0;; i++) { | 1313 for (i = 0;; i++) { |
1314 out[i] = MP_DIGIT(&in_shifted, 0) & kBottom29Bits; | 1314 out[i] = MP_DIGIT(&in_shifted, 0) & kBottom29Bits; |
1315 » mp_div_d(&in_shifted, kTwo29, &in_shifted, NULL); | 1315 » MP_CHECKOK(mp_div_d(&in_shifted, kTwo29, &in_shifted, NULL)); |
1316 | 1316 |
1317 i++; | 1317 i++; |
1318 if (i == NLIMBS) | 1318 if (i == NLIMBS) |
1319 break; | 1319 break; |
1320 out[i] = MP_DIGIT(&in_shifted, 0) & kBottom28Bits; | 1320 out[i] = MP_DIGIT(&in_shifted, 0) & kBottom28Bits; |
1321 » mp_div_d(&in_shifted, kTwo28, &in_shifted, NULL); | 1321 » MP_CHECKOK(mp_div_d(&in_shifted, kTwo28, &in_shifted, NULL)); |
1322 } | 1322 } |
1323 | 1323 |
1324 CLEANUP: | 1324 CLEANUP: |
1325 mp_clear(&in_shifted); | 1325 mp_clear(&in_shifted); |
1326 return res; | 1326 return res; |
1327 } | 1327 } |
1328 | 1328 |
1329 /* from_montgomery sets out=in/R. */ | 1329 /* from_montgomery sets out=in/R. */ |
1330 static mp_err from_montgomery(mp_int *out, const felem in, | 1330 static mp_err from_montgomery(mp_int *out, const felem in, |
1331 const ECGroup *group) | 1331 const ECGroup *group) |
1332 { | 1332 { |
1333 mp_int result, tmp; | 1333 mp_int result, tmp; |
1334 mp_err res; | 1334 mp_err res; |
1335 int i; | 1335 int i; |
1336 | 1336 |
1337 mp_init(&result); | 1337 MP_CHECKOK(mp_init(&result)); |
1338 mp_init(&tmp); | 1338 MP_CHECKOK(mp_init(&tmp)); |
1339 | 1339 |
1340 MP_CHECKOK(mp_add_d(&tmp, in[NLIMBS-1], &result)); | 1340 MP_CHECKOK(mp_add_d(&tmp, in[NLIMBS-1], &result)); |
1341 for (i = NLIMBS-2; i >= 0; i--) { | 1341 for (i = NLIMBS-2; i >= 0; i--) { |
1342 if ((i & 1) == 0) { | 1342 if ((i & 1) == 0) { |
1343 MP_CHECKOK(mp_mul_d(&result, kTwo29, &tmp)); | 1343 MP_CHECKOK(mp_mul_d(&result, kTwo29, &tmp)); |
1344 } else { | 1344 } else { |
1345 MP_CHECKOK(mp_mul_d(&result, kTwo28, &tmp)); | 1345 MP_CHECKOK(mp_mul_d(&result, kTwo28, &tmp)); |
1346 } | 1346 } |
1347 MP_CHECKOK(mp_add_d(&tmp, in[i], &result)); | 1347 MP_CHECKOK(mp_add_d(&tmp, in[i], &result)); |
1348 } | 1348 } |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1495 /* Wire in fast point multiplication for named curves. */ | 1495 /* Wire in fast point multiplication for named curves. */ |
1496 mp_err ec_group_set_gfp256_32(ECGroup *group, ECCurveName name) | 1496 mp_err ec_group_set_gfp256_32(ECGroup *group, ECCurveName name) |
1497 { | 1497 { |
1498 if (name == ECCurve_NIST_P256) { | 1498 if (name == ECCurve_NIST_P256) { |
1499 group->base_point_mul = &ec_GFp_nistp256_base_point_mul; | 1499 group->base_point_mul = &ec_GFp_nistp256_base_point_mul; |
1500 group->point_mul = &ec_GFp_nistp256_point_mul; | 1500 group->point_mul = &ec_GFp_nistp256_point_mul; |
1501 group->points_mul = &ec_GFp_nistp256_points_mul_vartime; | 1501 group->points_mul = &ec_GFp_nistp256_points_mul_vartime; |
1502 } | 1502 } |
1503 return MP_OKAY; | 1503 return MP_OKAY; |
1504 } | 1504 } |
OLD | NEW |