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 /* This file implements moduluar exponentiation using Montgomery's | 5 /* This file implements moduluar exponentiation using Montgomery's |
6 * method for modular reduction. This file implements the method | 6 * method for modular reduction. This file implements the method |
7 * described as "Improvement 2" in the paper "A Cryptogrpahic Library for | 7 * described as "Improvement 2" in the paper "A Cryptogrpahic Library for |
8 * the Motorola DSP56000" by Stephen R. Dusse' and Burton S. Kaliski Jr. | 8 * the Motorola DSP56000" by Stephen R. Dusse' and Burton S. Kaliski Jr. |
9 * published in "Advances in Cryptology: Proceedings of EUROCRYPT '90" | 9 * published in "Advances in Cryptology: Proceedings of EUROCRYPT '90" |
10 * "Lecture Notes in Computer Science" volume 473, 1991, pg 230-244, | 10 * "Lecture Notes in Computer Science" volume 473, 1991, pg 230-244, |
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 MP_DIGITS(&power2) = 0; | 364 MP_DIGITS(&power2) = 0; |
365 for (i = 0; i < MAX_ODD_INTS; ++i) { | 365 for (i = 0; i < MAX_ODD_INTS; ++i) { |
366 MP_DIGITS(oddPowers + i) = 0; | 366 MP_DIGITS(oddPowers + i) = 0; |
367 } | 367 } |
368 | 368 |
369 MP_CHECKOK( mp_init_size(&accum1, 3 * nLen + 2) ); | 369 MP_CHECKOK( mp_init_size(&accum1, 3 * nLen + 2) ); |
370 MP_CHECKOK( mp_init_size(&accum2, 3 * nLen + 2) ); | 370 MP_CHECKOK( mp_init_size(&accum2, 3 * nLen + 2) ); |
371 | 371 |
372 MP_CHECKOK( mp_init_copy(&oddPowers[0], montBase) ); | 372 MP_CHECKOK( mp_init_copy(&oddPowers[0], montBase) ); |
373 | 373 |
374 mp_init_size(&power2, nLen + 2 * MP_USED(montBase) + 2); | 374 MP_CHECKOK( mp_init_size(&power2, nLen + 2 * MP_USED(montBase) + 2) ); |
375 MP_CHECKOK( mp_sqr(montBase, &power2) ); /* power2 = montBase ** 2 */ | 375 MP_CHECKOK( mp_sqr(montBase, &power2) ); /* power2 = montBase ** 2 */ |
376 MP_CHECKOK( s_mp_redc(&power2, mmm) ); | 376 MP_CHECKOK( s_mp_redc(&power2, mmm) ); |
377 | 377 |
378 for (i = 1; i < odd_ints; ++i) { | 378 for (i = 1; i < odd_ints; ++i) { |
379 mp_init_size(oddPowers + i, nLen + 2 * MP_USED(&power2) + 2); | 379 MP_CHECKOK( mp_init_size(oddPowers + i, nLen + 2 * MP_USED(&power2) + 2) ); |
380 MP_CHECKOK( mp_mul(oddPowers + (i - 1), &power2, oddPowers + i) ); | 380 MP_CHECKOK( mp_mul(oddPowers + (i - 1), &power2, oddPowers + i) ); |
381 MP_CHECKOK( s_mp_redc(oddPowers + i, mmm) ); | 381 MP_CHECKOK( s_mp_redc(oddPowers + i, mmm) ); |
382 } | 382 } |
383 | 383 |
384 /* set accumulator to montgomery residue of 1 */ | 384 /* set accumulator to montgomery residue of 1 */ |
385 mp_set(&accum1, 1); | 385 mp_set(&accum1, 1); |
386 MP_CHECKOK( s_mp_to_mont(&accum1, mmm, &accum1) ); | 386 MP_CHECKOK( s_mp_to_mont(&accum1, mmm, &accum1) ); |
387 pa1 = &accum1; | 387 pa1 = &accum1; |
388 pa2 = &accum2; | 388 pa2 = &accum2; |
389 | 389 |
(...skipping 779 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1169 | 1169 |
1170 CLEANUP: | 1170 CLEANUP: |
1171 mp_clear(&montBase); | 1171 mp_clear(&montBase); |
1172 mp_clear(&goodBase); | 1172 mp_clear(&goodBase); |
1173 /* Don't mp_clear mmm.N because it is merely a copy of modulus. | 1173 /* Don't mp_clear mmm.N because it is merely a copy of modulus. |
1174 ** Just zap it. | 1174 ** Just zap it. |
1175 */ | 1175 */ |
1176 memset(&mmm, 0, sizeof mmm); | 1176 memset(&mmm, 0, sizeof mmm); |
1177 return res; | 1177 return res; |
1178 } | 1178 } |
OLD | NEW |