|
1 /* ********************************************************************* |
|
2 * |
|
3 * Sun elects to have this file available under and governed by the |
|
4 * Mozilla Public License Version 1.1 ("MPL") (see |
|
5 * http://www.mozilla.org/MPL/ for full license text). For the avoidance |
|
6 * of doubt and subject to the following, Sun also elects to allow |
|
7 * licensees to use this file under the MPL, the GNU General Public |
|
8 * License version 2 only or the Lesser General Public License version |
|
9 * 2.1 only. Any references to the "GNU General Public License version 2 |
|
10 * or later" or "GPL" in the following shall be construed to mean the |
|
11 * GNU General Public License version 2 only. Any references to the "GNU |
|
12 * Lesser General Public License version 2.1 or later" or "LGPL" in the |
|
13 * following shall be construed to mean the GNU Lesser General Public |
|
14 * License version 2.1 only. However, the following notice accompanied |
|
15 * the original version of this file: |
|
16 * |
|
17 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
|
18 * |
|
19 * The contents of this file are subject to the Mozilla Public License Version |
|
20 * 1.1 (the "License"); you may not use this file except in compliance with |
|
21 * the License. You may obtain a copy of the License at |
|
22 * http://www.mozilla.org/MPL/ |
|
23 * |
|
24 * Software distributed under the License is distributed on an "AS IS" basis, |
|
25 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
|
26 * for the specific language governing rights and limitations under the |
|
27 * License. |
|
28 * |
|
29 * The Original Code is the Netscape security libraries. |
|
30 * |
|
31 * The Initial Developer of the Original Code is |
|
32 * Netscape Communications Corporation. |
|
33 * Portions created by the Initial Developer are Copyright (C) 2000 |
|
34 * the Initial Developer. All Rights Reserved. |
|
35 * |
|
36 * Contributor(s): |
|
37 * Sheueling Chang Shantz <sheueling.chang@sun.com>, |
|
38 * Stephen Fung <stephen.fung@sun.com>, and |
|
39 * Douglas Stebila <douglas@stebila.ca> of Sun Laboratories. |
|
40 * |
|
41 * Alternatively, the contents of this file may be used under the terms of |
|
42 * either the GNU General Public License Version 2 or later (the "GPL"), or |
|
43 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
|
44 * in which case the provisions of the GPL or the LGPL are applicable instead |
|
45 * of those above. If you wish to allow use of your version of this file only |
|
46 * under the terms of either the GPL or the LGPL, and not to allow others to |
|
47 * use your version of this file under the terms of the MPL, indicate your |
|
48 * decision by deleting the provisions above and replace them with the notice |
|
49 * and other provisions required by the GPL or the LGPL. If you do not delete |
|
50 * the provisions above, a recipient may use your version of this file under |
|
51 * the terms of any one of the MPL, the GPL or the LGPL. |
|
52 * |
|
53 *********************************************************************** */ |
|
54 /* |
|
55 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. |
|
56 * Use is subject to license terms. |
|
57 */ |
|
58 |
|
59 #pragma ident "%Z%%M% %I% %E% SMI" |
|
60 |
|
61 /* $Id: mpmontg.c,v 1.20 2006/08/29 02:41:38 nelson%bolyard.com Exp $ */ |
|
62 |
|
63 /* This file implements moduluar exponentiation using Montgomery's |
|
64 * method for modular reduction. This file implements the method |
|
65 * described as "Improvement 1" in the paper "A Cryptogrpahic Library for |
|
66 * the Motorola DSP56000" by Stephen R. Dusse' and Burton S. Kaliski Jr. |
|
67 * published in "Advances in Cryptology: Proceedings of EUROCRYPT '90" |
|
68 * "Lecture Notes in Computer Science" volume 473, 1991, pg 230-244, |
|
69 * published by Springer Verlag. |
|
70 */ |
|
71 |
|
72 #define MP_USING_CACHE_SAFE_MOD_EXP 1 |
|
73 #ifndef _KERNEL |
|
74 #include <string.h> |
|
75 #include <stddef.h> /* ptrdiff_t */ |
|
76 #endif |
|
77 #include "mpi-priv.h" |
|
78 #include "mplogic.h" |
|
79 #include "mpprime.h" |
|
80 #ifdef MP_USING_MONT_MULF |
|
81 #include "montmulf.h" |
|
82 #endif |
|
83 |
|
84 /* if MP_CHAR_STORE_SLOW is defined, we */ |
|
85 /* need to know endianness of this platform. */ |
|
86 #ifdef MP_CHAR_STORE_SLOW |
|
87 #if !defined(MP_IS_BIG_ENDIAN) && !defined(MP_IS_LITTLE_ENDIAN) |
|
88 #error "You must define MP_IS_BIG_ENDIAN or MP_IS_LITTLE_ENDIAN\n" \ |
|
89 " if you define MP_CHAR_STORE_SLOW." |
|
90 #endif |
|
91 #endif |
|
92 |
|
93 #ifndef STATIC |
|
94 #define STATIC |
|
95 #endif |
|
96 |
|
97 #define MAX_ODD_INTS 32 /* 2 ** (WINDOW_BITS - 1) */ |
|
98 |
|
99 #ifndef _KERNEL |
|
100 #if defined(_WIN32_WCE) |
|
101 #define ABORT res = MP_UNDEF; goto CLEANUP |
|
102 #else |
|
103 #define ABORT abort() |
|
104 #endif |
|
105 #else |
|
106 #define ABORT res = MP_UNDEF; goto CLEANUP |
|
107 #endif /* _KERNEL */ |
|
108 |
|
109 /* computes T = REDC(T), 2^b == R */ |
|
110 mp_err s_mp_redc(mp_int *T, mp_mont_modulus *mmm) |
|
111 { |
|
112 mp_err res; |
|
113 mp_size i; |
|
114 |
|
115 i = MP_USED(T) + MP_USED(&mmm->N) + 2; |
|
116 MP_CHECKOK( s_mp_pad(T, i) ); |
|
117 for (i = 0; i < MP_USED(&mmm->N); ++i ) { |
|
118 mp_digit m_i = MP_DIGIT(T, i) * mmm->n0prime; |
|
119 /* T += N * m_i * (MP_RADIX ** i); */ |
|
120 MP_CHECKOK( s_mp_mul_d_add_offset(&mmm->N, m_i, T, i) ); |
|
121 } |
|
122 s_mp_clamp(T); |
|
123 |
|
124 /* T /= R */ |
|
125 s_mp_div_2d(T, mmm->b); |
|
126 |
|
127 if ((res = s_mp_cmp(T, &mmm->N)) >= 0) { |
|
128 /* T = T - N */ |
|
129 MP_CHECKOK( s_mp_sub(T, &mmm->N) ); |
|
130 #ifdef DEBUG |
|
131 if ((res = mp_cmp(T, &mmm->N)) >= 0) { |
|
132 res = MP_UNDEF; |
|
133 goto CLEANUP; |
|
134 } |
|
135 #endif |
|
136 } |
|
137 res = MP_OKAY; |
|
138 CLEANUP: |
|
139 return res; |
|
140 } |
|
141 |
|
142 #if !defined(MP_ASSEMBLY_MUL_MONT) && !defined(MP_MONT_USE_MP_MUL) |
|
143 mp_err s_mp_mul_mont(const mp_int *a, const mp_int *b, mp_int *c, |
|
144 mp_mont_modulus *mmm) |
|
145 { |
|
146 mp_digit *pb; |
|
147 mp_digit m_i; |
|
148 mp_err res; |
|
149 mp_size ib; |
|
150 mp_size useda, usedb; |
|
151 |
|
152 ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); |
|
153 |
|
154 if (MP_USED(a) < MP_USED(b)) { |
|
155 const mp_int *xch = b; /* switch a and b, to do fewer outer loops */ |
|
156 b = a; |
|
157 a = xch; |
|
158 } |
|
159 |
|
160 MP_USED(c) = 1; MP_DIGIT(c, 0) = 0; |
|
161 ib = MP_USED(a) + MP_MAX(MP_USED(b), MP_USED(&mmm->N)) + 2; |
|
162 if((res = s_mp_pad(c, ib)) != MP_OKAY) |
|
163 goto CLEANUP; |
|
164 |
|
165 useda = MP_USED(a); |
|
166 pb = MP_DIGITS(b); |
|
167 s_mpv_mul_d(MP_DIGITS(a), useda, *pb++, MP_DIGITS(c)); |
|
168 s_mp_setz(MP_DIGITS(c) + useda + 1, ib - (useda + 1)); |
|
169 m_i = MP_DIGIT(c, 0) * mmm->n0prime; |
|
170 s_mp_mul_d_add_offset(&mmm->N, m_i, c, 0); |
|
171 |
|
172 /* Outer loop: Digits of b */ |
|
173 usedb = MP_USED(b); |
|
174 for (ib = 1; ib < usedb; ib++) { |
|
175 mp_digit b_i = *pb++; |
|
176 |
|
177 /* Inner product: Digits of a */ |
|
178 if (b_i) |
|
179 s_mpv_mul_d_add_prop(MP_DIGITS(a), useda, b_i, MP_DIGITS(c) + ib); |
|
180 m_i = MP_DIGIT(c, ib) * mmm->n0prime; |
|
181 s_mp_mul_d_add_offset(&mmm->N, m_i, c, ib); |
|
182 } |
|
183 if (usedb < MP_USED(&mmm->N)) { |
|
184 for (usedb = MP_USED(&mmm->N); ib < usedb; ++ib ) { |
|
185 m_i = MP_DIGIT(c, ib) * mmm->n0prime; |
|
186 s_mp_mul_d_add_offset(&mmm->N, m_i, c, ib); |
|
187 } |
|
188 } |
|
189 s_mp_clamp(c); |
|
190 s_mp_div_2d(c, mmm->b); |
|
191 if (s_mp_cmp(c, &mmm->N) >= 0) { |
|
192 MP_CHECKOK( s_mp_sub(c, &mmm->N) ); |
|
193 } |
|
194 res = MP_OKAY; |
|
195 |
|
196 CLEANUP: |
|
197 return res; |
|
198 } |
|
199 #endif |