23 * Stephen Fung <stephen.fung@sun.com>, and
24 * Douglas Stebila <douglas@stebila.ca> of Sun Laboratories.
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39 /*
40 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
41 * Use is subject to license terms.
42 *
43 * Sun elects to use this software under the MPL license.
44 */
45
46 #pragma ident "%Z%%M% %I% %E% SMI"
47
48 /* $Id: mpmontg.c,v 1.20 2006/08/29 02:41:38 nelson%bolyard.com Exp $ */
49
50 /* This file implements moduluar exponentiation using Montgomery's
51 * method for modular reduction. This file implements the method
52 * described as "Improvement 1" in the paper "A Cryptogrpahic Library for
53 * the Motorola DSP56000" by Stephen R. Dusse' and Burton S. Kaliski Jr.
54 * published in "Advances in Cryptology: Proceedings of EUROCRYPT '90"
55 * "Lecture Notes in Computer Science" volume 473, 1991, pg 230-244,
56 * published by Springer Verlag.
57 */
58
59 #define MP_USING_CACHE_SAFE_MOD_EXP 1
60 #ifndef _KERNEL
61 #include <string.h>
62 #include <stddef.h> /* ptrdiff_t */
63 #endif
64 #include "mpi-priv.h"
65 #include "mplogic.h"
66 #include "mpprime.h"
67 #ifdef MP_USING_MONT_MULF
87 #if defined(_WIN32_WCE)
88 #define ABORT res = MP_UNDEF; goto CLEANUP
89 #else
90 #define ABORT abort()
91 #endif
92 #else
93 #define ABORT res = MP_UNDEF; goto CLEANUP
94 #endif /* _KERNEL */
95
96 /* computes T = REDC(T), 2^b == R */
97 mp_err s_mp_redc(mp_int *T, mp_mont_modulus *mmm)
98 {
99 mp_err res;
100 mp_size i;
101
102 i = MP_USED(T) + MP_USED(&mmm->N) + 2;
103 MP_CHECKOK( s_mp_pad(T, i) );
104 for (i = 0; i < MP_USED(&mmm->N); ++i ) {
105 mp_digit m_i = MP_DIGIT(T, i) * mmm->n0prime;
106 /* T += N * m_i * (MP_RADIX ** i); */
107 MP_CHECKOK( s_mp_mul_d_add_offset(&mmm->N, m_i, T, i) );
108 }
109 s_mp_clamp(T);
110
111 /* T /= R */
112 s_mp_div_2d(T, mmm->b);
113
114 if ((res = s_mp_cmp(T, &mmm->N)) >= 0) {
115 /* T = T - N */
116 MP_CHECKOK( s_mp_sub(T, &mmm->N) );
117 #ifdef DEBUG
118 if ((res = mp_cmp(T, &mmm->N)) >= 0) {
119 res = MP_UNDEF;
120 goto CLEANUP;
121 }
122 #endif
123 }
124 res = MP_OKAY;
125 CLEANUP:
126 return res;
127 }
|
23 * Stephen Fung <stephen.fung@sun.com>, and
24 * Douglas Stebila <douglas@stebila.ca> of Sun Laboratories.
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39 /*
40 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
41 * Use is subject to license terms.
42 *
43 * Copyright 2017 RackTop Systems.
44 *
45 * Sun elects to use this software under the MPL license.
46 */
47
48 /* $Id: mpmontg.c,v 1.20 2006/08/29 02:41:38 nelson%bolyard.com Exp $ */
49
50 /* This file implements moduluar exponentiation using Montgomery's
51 * method for modular reduction. This file implements the method
52 * described as "Improvement 1" in the paper "A Cryptogrpahic Library for
53 * the Motorola DSP56000" by Stephen R. Dusse' and Burton S. Kaliski Jr.
54 * published in "Advances in Cryptology: Proceedings of EUROCRYPT '90"
55 * "Lecture Notes in Computer Science" volume 473, 1991, pg 230-244,
56 * published by Springer Verlag.
57 */
58
59 #define MP_USING_CACHE_SAFE_MOD_EXP 1
60 #ifndef _KERNEL
61 #include <string.h>
62 #include <stddef.h> /* ptrdiff_t */
63 #endif
64 #include "mpi-priv.h"
65 #include "mplogic.h"
66 #include "mpprime.h"
67 #ifdef MP_USING_MONT_MULF
87 #if defined(_WIN32_WCE)
88 #define ABORT res = MP_UNDEF; goto CLEANUP
89 #else
90 #define ABORT abort()
91 #endif
92 #else
93 #define ABORT res = MP_UNDEF; goto CLEANUP
94 #endif /* _KERNEL */
95
96 /* computes T = REDC(T), 2^b == R */
97 mp_err s_mp_redc(mp_int *T, mp_mont_modulus *mmm)
98 {
99 mp_err res;
100 mp_size i;
101
102 i = MP_USED(T) + MP_USED(&mmm->N) + 2;
103 MP_CHECKOK( s_mp_pad(T, i) );
104 for (i = 0; i < MP_USED(&mmm->N); ++i ) {
105 mp_digit m_i = MP_DIGIT(T, i) * mmm->n0prime;
106 /* T += N * m_i * (MP_RADIX ** i); */
107 s_mp_mul_d_add_offset(&mmm->N, m_i, T, i);
108 }
109 s_mp_clamp(T);
110
111 /* T /= R */
112 s_mp_div_2d(T, mmm->b);
113
114 if ((res = s_mp_cmp(T, &mmm->N)) >= 0) {
115 /* T = T - N */
116 MP_CHECKOK( s_mp_sub(T, &mmm->N) );
117 #ifdef DEBUG
118 if ((res = mp_cmp(T, &mmm->N)) >= 0) {
119 res = MP_UNDEF;
120 goto CLEANUP;
121 }
122 #endif
123 }
124 res = MP_OKAY;
125 CLEANUP:
126 return res;
127 }
|