sl@0
|
1 |
/* crypto/bn/bn_gcd.c */
|
sl@0
|
2 |
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
*
|
sl@0
|
5 |
* This package is an SSL implementation written
|
sl@0
|
6 |
* by Eric Young (eay@cryptsoft.com).
|
sl@0
|
7 |
* The implementation was written so as to conform with Netscapes SSL.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* This library is free for commercial and non-commercial use as long as
|
sl@0
|
10 |
* the following conditions are aheared to. The following conditions
|
sl@0
|
11 |
* apply to all code found in this distribution, be it the RC4, RSA,
|
sl@0
|
12 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
sl@0
|
13 |
* included with this distribution is covered by the same copyright terms
|
sl@0
|
14 |
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
sl@0
|
15 |
*
|
sl@0
|
16 |
* Copyright remains Eric Young's, and as such any Copyright notices in
|
sl@0
|
17 |
* the code are not to be removed.
|
sl@0
|
18 |
* If this package is used in a product, Eric Young should be given attribution
|
sl@0
|
19 |
* as the author of the parts of the library used.
|
sl@0
|
20 |
* This can be in the form of a textual message at program startup or
|
sl@0
|
21 |
* in documentation (online or textual) provided with the package.
|
sl@0
|
22 |
*
|
sl@0
|
23 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
24 |
* modification, are permitted provided that the following conditions
|
sl@0
|
25 |
* are met:
|
sl@0
|
26 |
* 1. Redistributions of source code must retain the copyright
|
sl@0
|
27 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
28 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
29 |
* notice, this list of conditions and the following disclaimer in the
|
sl@0
|
30 |
* documentation and/or other materials provided with the distribution.
|
sl@0
|
31 |
* 3. All advertising materials mentioning features or use of this software
|
sl@0
|
32 |
* must display the following acknowledgement:
|
sl@0
|
33 |
* "This product includes cryptographic software written by
|
sl@0
|
34 |
* Eric Young (eay@cryptsoft.com)"
|
sl@0
|
35 |
* The word 'cryptographic' can be left out if the rouines from the library
|
sl@0
|
36 |
* being used are not cryptographic related :-).
|
sl@0
|
37 |
* 4. If you include any Windows specific code (or a derivative thereof) from
|
sl@0
|
38 |
* the apps directory (application code) you must include an acknowledgement:
|
sl@0
|
39 |
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
sl@0
|
40 |
*
|
sl@0
|
41 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
sl@0
|
42 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
43 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
sl@0
|
44 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
sl@0
|
45 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
sl@0
|
46 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
sl@0
|
47 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
48 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
sl@0
|
49 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
sl@0
|
50 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
sl@0
|
51 |
* SUCH DAMAGE.
|
sl@0
|
52 |
*
|
sl@0
|
53 |
* The licence and distribution terms for any publically available version or
|
sl@0
|
54 |
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
sl@0
|
55 |
* copied and put under another distribution licence
|
sl@0
|
56 |
* [including the GNU Public Licence.]
|
sl@0
|
57 |
*/
|
sl@0
|
58 |
/* ====================================================================
|
sl@0
|
59 |
* Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
|
sl@0
|
60 |
*
|
sl@0
|
61 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
62 |
* modification, are permitted provided that the following conditions
|
sl@0
|
63 |
* are met:
|
sl@0
|
64 |
*
|
sl@0
|
65 |
* 1. Redistributions of source code must retain the above copyright
|
sl@0
|
66 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
67 |
*
|
sl@0
|
68 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
69 |
* notice, this list of conditions and the following disclaimer in
|
sl@0
|
70 |
* the documentation and/or other materials provided with the
|
sl@0
|
71 |
* distribution.
|
sl@0
|
72 |
*
|
sl@0
|
73 |
* 3. All advertising materials mentioning features or use of this
|
sl@0
|
74 |
* software must display the following acknowledgment:
|
sl@0
|
75 |
* "This product includes software developed by the OpenSSL Project
|
sl@0
|
76 |
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
|
sl@0
|
77 |
*
|
sl@0
|
78 |
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
sl@0
|
79 |
* endorse or promote products derived from this software without
|
sl@0
|
80 |
* prior written permission. For written permission, please contact
|
sl@0
|
81 |
* openssl-core@openssl.org.
|
sl@0
|
82 |
*
|
sl@0
|
83 |
* 5. Products derived from this software may not be called "OpenSSL"
|
sl@0
|
84 |
* nor may "OpenSSL" appear in their names without prior written
|
sl@0
|
85 |
* permission of the OpenSSL Project.
|
sl@0
|
86 |
*
|
sl@0
|
87 |
* 6. Redistributions of any form whatsoever must retain the following
|
sl@0
|
88 |
* acknowledgment:
|
sl@0
|
89 |
* "This product includes software developed by the OpenSSL Project
|
sl@0
|
90 |
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
|
sl@0
|
91 |
*
|
sl@0
|
92 |
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
sl@0
|
93 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
94 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
sl@0
|
95 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
sl@0
|
96 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
sl@0
|
97 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
sl@0
|
98 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
sl@0
|
99 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
100 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
sl@0
|
101 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
sl@0
|
102 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
sl@0
|
103 |
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
sl@0
|
104 |
* ====================================================================
|
sl@0
|
105 |
*
|
sl@0
|
106 |
* This product includes cryptographic software written by Eric Young
|
sl@0
|
107 |
* (eay@cryptsoft.com). This product includes software written by Tim
|
sl@0
|
108 |
* Hudson (tjh@cryptsoft.com).
|
sl@0
|
109 |
*
|
sl@0
|
110 |
*/
|
sl@0
|
111 |
|
sl@0
|
112 |
#include "cryptlib.h"
|
sl@0
|
113 |
#include "bn_lcl.h"
|
sl@0
|
114 |
|
sl@0
|
115 |
static BIGNUM *euclid(BIGNUM *a, BIGNUM *b);
|
sl@0
|
116 |
|
sl@0
|
117 |
EXPORT_C int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
|
sl@0
|
118 |
{
|
sl@0
|
119 |
BIGNUM *a,*b,*t;
|
sl@0
|
120 |
int ret=0;
|
sl@0
|
121 |
|
sl@0
|
122 |
bn_check_top(in_a);
|
sl@0
|
123 |
bn_check_top(in_b);
|
sl@0
|
124 |
|
sl@0
|
125 |
BN_CTX_start(ctx);
|
sl@0
|
126 |
a = BN_CTX_get(ctx);
|
sl@0
|
127 |
b = BN_CTX_get(ctx);
|
sl@0
|
128 |
if (a == NULL || b == NULL) goto err;
|
sl@0
|
129 |
|
sl@0
|
130 |
if (BN_copy(a,in_a) == NULL) goto err;
|
sl@0
|
131 |
if (BN_copy(b,in_b) == NULL) goto err;
|
sl@0
|
132 |
a->neg = 0;
|
sl@0
|
133 |
b->neg = 0;
|
sl@0
|
134 |
|
sl@0
|
135 |
if (BN_cmp(a,b) < 0) { t=a; a=b; b=t; }
|
sl@0
|
136 |
t=euclid(a,b);
|
sl@0
|
137 |
if (t == NULL) goto err;
|
sl@0
|
138 |
|
sl@0
|
139 |
if (BN_copy(r,t) == NULL) goto err;
|
sl@0
|
140 |
ret=1;
|
sl@0
|
141 |
err:
|
sl@0
|
142 |
BN_CTX_end(ctx);
|
sl@0
|
143 |
bn_check_top(r);
|
sl@0
|
144 |
return(ret);
|
sl@0
|
145 |
}
|
sl@0
|
146 |
|
sl@0
|
147 |
static BIGNUM *euclid(BIGNUM *a, BIGNUM *b)
|
sl@0
|
148 |
{
|
sl@0
|
149 |
BIGNUM *t;
|
sl@0
|
150 |
int shifts=0;
|
sl@0
|
151 |
|
sl@0
|
152 |
bn_check_top(a);
|
sl@0
|
153 |
bn_check_top(b);
|
sl@0
|
154 |
|
sl@0
|
155 |
/* 0 <= b <= a */
|
sl@0
|
156 |
while (!BN_is_zero(b))
|
sl@0
|
157 |
{
|
sl@0
|
158 |
/* 0 < b <= a */
|
sl@0
|
159 |
|
sl@0
|
160 |
if (BN_is_odd(a))
|
sl@0
|
161 |
{
|
sl@0
|
162 |
if (BN_is_odd(b))
|
sl@0
|
163 |
{
|
sl@0
|
164 |
if (!BN_sub(a,a,b)) goto err;
|
sl@0
|
165 |
if (!BN_rshift1(a,a)) goto err;
|
sl@0
|
166 |
if (BN_cmp(a,b) < 0)
|
sl@0
|
167 |
{ t=a; a=b; b=t; }
|
sl@0
|
168 |
}
|
sl@0
|
169 |
else /* a odd - b even */
|
sl@0
|
170 |
{
|
sl@0
|
171 |
if (!BN_rshift1(b,b)) goto err;
|
sl@0
|
172 |
if (BN_cmp(a,b) < 0)
|
sl@0
|
173 |
{ t=a; a=b; b=t; }
|
sl@0
|
174 |
}
|
sl@0
|
175 |
}
|
sl@0
|
176 |
else /* a is even */
|
sl@0
|
177 |
{
|
sl@0
|
178 |
if (BN_is_odd(b))
|
sl@0
|
179 |
{
|
sl@0
|
180 |
if (!BN_rshift1(a,a)) goto err;
|
sl@0
|
181 |
if (BN_cmp(a,b) < 0)
|
sl@0
|
182 |
{ t=a; a=b; b=t; }
|
sl@0
|
183 |
}
|
sl@0
|
184 |
else /* a even - b even */
|
sl@0
|
185 |
{
|
sl@0
|
186 |
if (!BN_rshift1(a,a)) goto err;
|
sl@0
|
187 |
if (!BN_rshift1(b,b)) goto err;
|
sl@0
|
188 |
shifts++;
|
sl@0
|
189 |
}
|
sl@0
|
190 |
}
|
sl@0
|
191 |
/* 0 <= b <= a */
|
sl@0
|
192 |
}
|
sl@0
|
193 |
|
sl@0
|
194 |
if (shifts)
|
sl@0
|
195 |
{
|
sl@0
|
196 |
if (!BN_lshift(a,a,shifts)) goto err;
|
sl@0
|
197 |
}
|
sl@0
|
198 |
bn_check_top(a);
|
sl@0
|
199 |
return(a);
|
sl@0
|
200 |
err:
|
sl@0
|
201 |
return(NULL);
|
sl@0
|
202 |
}
|
sl@0
|
203 |
|
sl@0
|
204 |
|
sl@0
|
205 |
/* solves ax == 1 (mod n) */
|
sl@0
|
206 |
static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
|
sl@0
|
207 |
const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
|
sl@0
|
208 |
EXPORT_C BIGNUM *BN_mod_inverse(BIGNUM *in,
|
sl@0
|
209 |
const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
|
sl@0
|
210 |
{
|
sl@0
|
211 |
BIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;
|
sl@0
|
212 |
BIGNUM *ret=NULL;
|
sl@0
|
213 |
int sign;
|
sl@0
|
214 |
if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0))
|
sl@0
|
215 |
{
|
sl@0
|
216 |
return BN_mod_inverse_no_branch(in, a, n, ctx);
|
sl@0
|
217 |
}
|
sl@0
|
218 |
|
sl@0
|
219 |
bn_check_top(a);
|
sl@0
|
220 |
bn_check_top(n);
|
sl@0
|
221 |
|
sl@0
|
222 |
BN_CTX_start(ctx);
|
sl@0
|
223 |
A = BN_CTX_get(ctx);
|
sl@0
|
224 |
B = BN_CTX_get(ctx);
|
sl@0
|
225 |
X = BN_CTX_get(ctx);
|
sl@0
|
226 |
D = BN_CTX_get(ctx);
|
sl@0
|
227 |
M = BN_CTX_get(ctx);
|
sl@0
|
228 |
Y = BN_CTX_get(ctx);
|
sl@0
|
229 |
T = BN_CTX_get(ctx);
|
sl@0
|
230 |
if (T == NULL) goto err;
|
sl@0
|
231 |
|
sl@0
|
232 |
if (in == NULL)
|
sl@0
|
233 |
R=BN_new();
|
sl@0
|
234 |
else
|
sl@0
|
235 |
R=in;
|
sl@0
|
236 |
if (R == NULL) goto err;
|
sl@0
|
237 |
|
sl@0
|
238 |
BN_one(X);
|
sl@0
|
239 |
BN_zero(Y);
|
sl@0
|
240 |
if (BN_copy(B,a) == NULL) goto err;
|
sl@0
|
241 |
if (BN_copy(A,n) == NULL) goto err;
|
sl@0
|
242 |
A->neg = 0;
|
sl@0
|
243 |
if (B->neg || (BN_ucmp(B, A) >= 0))
|
sl@0
|
244 |
{
|
sl@0
|
245 |
if (!BN_nnmod(B, B, A, ctx)) goto err;
|
sl@0
|
246 |
}
|
sl@0
|
247 |
sign = -1;
|
sl@0
|
248 |
/* From B = a mod |n|, A = |n| it follows that
|
sl@0
|
249 |
*
|
sl@0
|
250 |
* 0 <= B < A,
|
sl@0
|
251 |
* -sign*X*a == B (mod |n|),
|
sl@0
|
252 |
* sign*Y*a == A (mod |n|).
|
sl@0
|
253 |
*/
|
sl@0
|
254 |
|
sl@0
|
255 |
if (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048)))
|
sl@0
|
256 |
{
|
sl@0
|
257 |
/* Binary inversion algorithm; requires odd modulus.
|
sl@0
|
258 |
* This is faster than the general algorithm if the modulus
|
sl@0
|
259 |
* is sufficiently small (about 400 .. 500 bits on 32-bit
|
sl@0
|
260 |
* sytems, but much more on 64-bit systems) */
|
sl@0
|
261 |
int shift;
|
sl@0
|
262 |
|
sl@0
|
263 |
while (!BN_is_zero(B))
|
sl@0
|
264 |
{
|
sl@0
|
265 |
/*
|
sl@0
|
266 |
* 0 < B < |n|,
|
sl@0
|
267 |
* 0 < A <= |n|,
|
sl@0
|
268 |
* (1) -sign*X*a == B (mod |n|),
|
sl@0
|
269 |
* (2) sign*Y*a == A (mod |n|)
|
sl@0
|
270 |
*/
|
sl@0
|
271 |
|
sl@0
|
272 |
/* Now divide B by the maximum possible power of two in the integers,
|
sl@0
|
273 |
* and divide X by the same value mod |n|.
|
sl@0
|
274 |
* When we're done, (1) still holds. */
|
sl@0
|
275 |
shift = 0;
|
sl@0
|
276 |
while (!BN_is_bit_set(B, shift)) /* note that 0 < B */
|
sl@0
|
277 |
{
|
sl@0
|
278 |
shift++;
|
sl@0
|
279 |
|
sl@0
|
280 |
if (BN_is_odd(X))
|
sl@0
|
281 |
{
|
sl@0
|
282 |
if (!BN_uadd(X, X, n)) goto err;
|
sl@0
|
283 |
}
|
sl@0
|
284 |
/* now X is even, so we can easily divide it by two */
|
sl@0
|
285 |
if (!BN_rshift1(X, X)) goto err;
|
sl@0
|
286 |
}
|
sl@0
|
287 |
if (shift > 0)
|
sl@0
|
288 |
{
|
sl@0
|
289 |
if (!BN_rshift(B, B, shift)) goto err;
|
sl@0
|
290 |
}
|
sl@0
|
291 |
|
sl@0
|
292 |
|
sl@0
|
293 |
/* Same for A and Y. Afterwards, (2) still holds. */
|
sl@0
|
294 |
shift = 0;
|
sl@0
|
295 |
while (!BN_is_bit_set(A, shift)) /* note that 0 < A */
|
sl@0
|
296 |
{
|
sl@0
|
297 |
shift++;
|
sl@0
|
298 |
|
sl@0
|
299 |
if (BN_is_odd(Y))
|
sl@0
|
300 |
{
|
sl@0
|
301 |
if (!BN_uadd(Y, Y, n)) goto err;
|
sl@0
|
302 |
}
|
sl@0
|
303 |
/* now Y is even */
|
sl@0
|
304 |
if (!BN_rshift1(Y, Y)) goto err;
|
sl@0
|
305 |
}
|
sl@0
|
306 |
if (shift > 0)
|
sl@0
|
307 |
{
|
sl@0
|
308 |
if (!BN_rshift(A, A, shift)) goto err;
|
sl@0
|
309 |
}
|
sl@0
|
310 |
|
sl@0
|
311 |
|
sl@0
|
312 |
/* We still have (1) and (2).
|
sl@0
|
313 |
* Both A and B are odd.
|
sl@0
|
314 |
* The following computations ensure that
|
sl@0
|
315 |
*
|
sl@0
|
316 |
* 0 <= B < |n|,
|
sl@0
|
317 |
* 0 < A < |n|,
|
sl@0
|
318 |
* (1) -sign*X*a == B (mod |n|),
|
sl@0
|
319 |
* (2) sign*Y*a == A (mod |n|),
|
sl@0
|
320 |
*
|
sl@0
|
321 |
* and that either A or B is even in the next iteration.
|
sl@0
|
322 |
*/
|
sl@0
|
323 |
if (BN_ucmp(B, A) >= 0)
|
sl@0
|
324 |
{
|
sl@0
|
325 |
/* -sign*(X + Y)*a == B - A (mod |n|) */
|
sl@0
|
326 |
if (!BN_uadd(X, X, Y)) goto err;
|
sl@0
|
327 |
/* NB: we could use BN_mod_add_quick(X, X, Y, n), but that
|
sl@0
|
328 |
* actually makes the algorithm slower */
|
sl@0
|
329 |
if (!BN_usub(B, B, A)) goto err;
|
sl@0
|
330 |
}
|
sl@0
|
331 |
else
|
sl@0
|
332 |
{
|
sl@0
|
333 |
/* sign*(X + Y)*a == A - B (mod |n|) */
|
sl@0
|
334 |
if (!BN_uadd(Y, Y, X)) goto err;
|
sl@0
|
335 |
/* as above, BN_mod_add_quick(Y, Y, X, n) would slow things down */
|
sl@0
|
336 |
if (!BN_usub(A, A, B)) goto err;
|
sl@0
|
337 |
}
|
sl@0
|
338 |
}
|
sl@0
|
339 |
}
|
sl@0
|
340 |
else
|
sl@0
|
341 |
{
|
sl@0
|
342 |
/* general inversion algorithm */
|
sl@0
|
343 |
|
sl@0
|
344 |
while (!BN_is_zero(B))
|
sl@0
|
345 |
{
|
sl@0
|
346 |
BIGNUM *tmp;
|
sl@0
|
347 |
|
sl@0
|
348 |
/*
|
sl@0
|
349 |
* 0 < B < A,
|
sl@0
|
350 |
* (*) -sign*X*a == B (mod |n|),
|
sl@0
|
351 |
* sign*Y*a == A (mod |n|)
|
sl@0
|
352 |
*/
|
sl@0
|
353 |
|
sl@0
|
354 |
/* (D, M) := (A/B, A%B) ... */
|
sl@0
|
355 |
if (BN_num_bits(A) == BN_num_bits(B))
|
sl@0
|
356 |
{
|
sl@0
|
357 |
if (!BN_one(D)) goto err;
|
sl@0
|
358 |
if (!BN_sub(M,A,B)) goto err;
|
sl@0
|
359 |
}
|
sl@0
|
360 |
else if (BN_num_bits(A) == BN_num_bits(B) + 1)
|
sl@0
|
361 |
{
|
sl@0
|
362 |
/* A/B is 1, 2, or 3 */
|
sl@0
|
363 |
if (!BN_lshift1(T,B)) goto err;
|
sl@0
|
364 |
if (BN_ucmp(A,T) < 0)
|
sl@0
|
365 |
{
|
sl@0
|
366 |
/* A < 2*B, so D=1 */
|
sl@0
|
367 |
if (!BN_one(D)) goto err;
|
sl@0
|
368 |
if (!BN_sub(M,A,B)) goto err;
|
sl@0
|
369 |
}
|
sl@0
|
370 |
else
|
sl@0
|
371 |
{
|
sl@0
|
372 |
/* A >= 2*B, so D=2 or D=3 */
|
sl@0
|
373 |
if (!BN_sub(M,A,T)) goto err;
|
sl@0
|
374 |
if (!BN_add(D,T,B)) goto err; /* use D (:= 3*B) as temp */
|
sl@0
|
375 |
if (BN_ucmp(A,D) < 0)
|
sl@0
|
376 |
{
|
sl@0
|
377 |
/* A < 3*B, so D=2 */
|
sl@0
|
378 |
if (!BN_set_word(D,2)) goto err;
|
sl@0
|
379 |
/* M (= A - 2*B) already has the correct value */
|
sl@0
|
380 |
}
|
sl@0
|
381 |
else
|
sl@0
|
382 |
{
|
sl@0
|
383 |
/* only D=3 remains */
|
sl@0
|
384 |
if (!BN_set_word(D,3)) goto err;
|
sl@0
|
385 |
/* currently M = A - 2*B, but we need M = A - 3*B */
|
sl@0
|
386 |
if (!BN_sub(M,M,B)) goto err;
|
sl@0
|
387 |
}
|
sl@0
|
388 |
}
|
sl@0
|
389 |
}
|
sl@0
|
390 |
else
|
sl@0
|
391 |
{
|
sl@0
|
392 |
if (!BN_div(D,M,A,B,ctx)) goto err;
|
sl@0
|
393 |
}
|
sl@0
|
394 |
|
sl@0
|
395 |
/* Now
|
sl@0
|
396 |
* A = D*B + M;
|
sl@0
|
397 |
* thus we have
|
sl@0
|
398 |
* (**) sign*Y*a == D*B + M (mod |n|).
|
sl@0
|
399 |
*/
|
sl@0
|
400 |
|
sl@0
|
401 |
tmp=A; /* keep the BIGNUM object, the value does not matter */
|
sl@0
|
402 |
|
sl@0
|
403 |
/* (A, B) := (B, A mod B) ... */
|
sl@0
|
404 |
A=B;
|
sl@0
|
405 |
B=M;
|
sl@0
|
406 |
/* ... so we have 0 <= B < A again */
|
sl@0
|
407 |
|
sl@0
|
408 |
/* Since the former M is now B and the former B is now A,
|
sl@0
|
409 |
* (**) translates into
|
sl@0
|
410 |
* sign*Y*a == D*A + B (mod |n|),
|
sl@0
|
411 |
* i.e.
|
sl@0
|
412 |
* sign*Y*a - D*A == B (mod |n|).
|
sl@0
|
413 |
* Similarly, (*) translates into
|
sl@0
|
414 |
* -sign*X*a == A (mod |n|).
|
sl@0
|
415 |
*
|
sl@0
|
416 |
* Thus,
|
sl@0
|
417 |
* sign*Y*a + D*sign*X*a == B (mod |n|),
|
sl@0
|
418 |
* i.e.
|
sl@0
|
419 |
* sign*(Y + D*X)*a == B (mod |n|).
|
sl@0
|
420 |
*
|
sl@0
|
421 |
* So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
|
sl@0
|
422 |
* -sign*X*a == B (mod |n|),
|
sl@0
|
423 |
* sign*Y*a == A (mod |n|).
|
sl@0
|
424 |
* Note that X and Y stay non-negative all the time.
|
sl@0
|
425 |
*/
|
sl@0
|
426 |
|
sl@0
|
427 |
/* most of the time D is very small, so we can optimize tmp := D*X+Y */
|
sl@0
|
428 |
if (BN_is_one(D))
|
sl@0
|
429 |
{
|
sl@0
|
430 |
if (!BN_add(tmp,X,Y)) goto err;
|
sl@0
|
431 |
}
|
sl@0
|
432 |
else
|
sl@0
|
433 |
{
|
sl@0
|
434 |
if (BN_is_word(D,2))
|
sl@0
|
435 |
{
|
sl@0
|
436 |
if (!BN_lshift1(tmp,X)) goto err;
|
sl@0
|
437 |
}
|
sl@0
|
438 |
else if (BN_is_word(D,4))
|
sl@0
|
439 |
{
|
sl@0
|
440 |
if (!BN_lshift(tmp,X,2)) goto err;
|
sl@0
|
441 |
}
|
sl@0
|
442 |
else if (D->top == 1)
|
sl@0
|
443 |
{
|
sl@0
|
444 |
if (!BN_copy(tmp,X)) goto err;
|
sl@0
|
445 |
if (!BN_mul_word(tmp,D->d[0])) goto err;
|
sl@0
|
446 |
}
|
sl@0
|
447 |
else
|
sl@0
|
448 |
{
|
sl@0
|
449 |
if (!BN_mul(tmp,D,X,ctx)) goto err;
|
sl@0
|
450 |
}
|
sl@0
|
451 |
if (!BN_add(tmp,tmp,Y)) goto err;
|
sl@0
|
452 |
}
|
sl@0
|
453 |
|
sl@0
|
454 |
M=Y; /* keep the BIGNUM object, the value does not matter */
|
sl@0
|
455 |
Y=X;
|
sl@0
|
456 |
X=tmp;
|
sl@0
|
457 |
sign = -sign;
|
sl@0
|
458 |
}
|
sl@0
|
459 |
}
|
sl@0
|
460 |
|
sl@0
|
461 |
/*
|
sl@0
|
462 |
* The while loop (Euclid's algorithm) ends when
|
sl@0
|
463 |
* A == gcd(a,n);
|
sl@0
|
464 |
* we have
|
sl@0
|
465 |
* sign*Y*a == A (mod |n|),
|
sl@0
|
466 |
* where Y is non-negative.
|
sl@0
|
467 |
*/
|
sl@0
|
468 |
|
sl@0
|
469 |
if (sign < 0)
|
sl@0
|
470 |
{
|
sl@0
|
471 |
if (!BN_sub(Y,n,Y)) goto err;
|
sl@0
|
472 |
}
|
sl@0
|
473 |
/* Now Y*a == A (mod |n|). */
|
sl@0
|
474 |
|
sl@0
|
475 |
|
sl@0
|
476 |
if (BN_is_one(A))
|
sl@0
|
477 |
{
|
sl@0
|
478 |
/* Y*a == 1 (mod |n|) */
|
sl@0
|
479 |
if (!Y->neg && BN_ucmp(Y,n) < 0)
|
sl@0
|
480 |
{
|
sl@0
|
481 |
if (!BN_copy(R,Y)) goto err;
|
sl@0
|
482 |
}
|
sl@0
|
483 |
else
|
sl@0
|
484 |
{
|
sl@0
|
485 |
if (!BN_nnmod(R,Y,n,ctx)) goto err;
|
sl@0
|
486 |
}
|
sl@0
|
487 |
}
|
sl@0
|
488 |
else
|
sl@0
|
489 |
{
|
sl@0
|
490 |
BNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);
|
sl@0
|
491 |
goto err;
|
sl@0
|
492 |
}
|
sl@0
|
493 |
ret=R;
|
sl@0
|
494 |
err:
|
sl@0
|
495 |
if ((ret == NULL) && (in == NULL)) BN_free(R);
|
sl@0
|
496 |
BN_CTX_end(ctx);
|
sl@0
|
497 |
bn_check_top(ret);
|
sl@0
|
498 |
return(ret);
|
sl@0
|
499 |
}
|
sl@0
|
500 |
|
sl@0
|
501 |
|
sl@0
|
502 |
/* BN_mod_inverse_no_branch is a special version of BN_mod_inverse.
|
sl@0
|
503 |
* It does not contain branches that may leak sensitive information.
|
sl@0
|
504 |
*/
|
sl@0
|
505 |
static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
|
sl@0
|
506 |
const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
|
sl@0
|
507 |
{
|
sl@0
|
508 |
BIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;
|
sl@0
|
509 |
BIGNUM local_A, local_B;
|
sl@0
|
510 |
BIGNUM *pA, *pB;
|
sl@0
|
511 |
BIGNUM *ret=NULL;
|
sl@0
|
512 |
int sign;
|
sl@0
|
513 |
|
sl@0
|
514 |
bn_check_top(a);
|
sl@0
|
515 |
bn_check_top(n);
|
sl@0
|
516 |
|
sl@0
|
517 |
BN_CTX_start(ctx);
|
sl@0
|
518 |
A = BN_CTX_get(ctx);
|
sl@0
|
519 |
B = BN_CTX_get(ctx);
|
sl@0
|
520 |
X = BN_CTX_get(ctx);
|
sl@0
|
521 |
D = BN_CTX_get(ctx);
|
sl@0
|
522 |
M = BN_CTX_get(ctx);
|
sl@0
|
523 |
Y = BN_CTX_get(ctx);
|
sl@0
|
524 |
T = BN_CTX_get(ctx);
|
sl@0
|
525 |
if (T == NULL) goto err;
|
sl@0
|
526 |
|
sl@0
|
527 |
if (in == NULL)
|
sl@0
|
528 |
R=BN_new();
|
sl@0
|
529 |
else
|
sl@0
|
530 |
R=in;
|
sl@0
|
531 |
if (R == NULL) goto err;
|
sl@0
|
532 |
|
sl@0
|
533 |
BN_one(X);
|
sl@0
|
534 |
BN_zero(Y);
|
sl@0
|
535 |
if (BN_copy(B,a) == NULL) goto err;
|
sl@0
|
536 |
if (BN_copy(A,n) == NULL) goto err;
|
sl@0
|
537 |
A->neg = 0;
|
sl@0
|
538 |
|
sl@0
|
539 |
if (B->neg || (BN_ucmp(B, A) >= 0))
|
sl@0
|
540 |
{
|
sl@0
|
541 |
/* Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
|
sl@0
|
542 |
* BN_div_no_branch will be called eventually.
|
sl@0
|
543 |
*/
|
sl@0
|
544 |
pB = &local_B;
|
sl@0
|
545 |
BN_with_flags(pB, B, BN_FLG_CONSTTIME);
|
sl@0
|
546 |
if (!BN_nnmod(B, pB, A, ctx)) goto err;
|
sl@0
|
547 |
}
|
sl@0
|
548 |
sign = -1;
|
sl@0
|
549 |
/* From B = a mod |n|, A = |n| it follows that
|
sl@0
|
550 |
*
|
sl@0
|
551 |
* 0 <= B < A,
|
sl@0
|
552 |
* -sign*X*a == B (mod |n|),
|
sl@0
|
553 |
* sign*Y*a == A (mod |n|).
|
sl@0
|
554 |
*/
|
sl@0
|
555 |
|
sl@0
|
556 |
while (!BN_is_zero(B))
|
sl@0
|
557 |
{
|
sl@0
|
558 |
BIGNUM *tmp;
|
sl@0
|
559 |
|
sl@0
|
560 |
/*
|
sl@0
|
561 |
* 0 < B < A,
|
sl@0
|
562 |
* (*) -sign*X*a == B (mod |n|),
|
sl@0
|
563 |
* sign*Y*a == A (mod |n|)
|
sl@0
|
564 |
*/
|
sl@0
|
565 |
|
sl@0
|
566 |
/* Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
|
sl@0
|
567 |
* BN_div_no_branch will be called eventually.
|
sl@0
|
568 |
*/
|
sl@0
|
569 |
pA = &local_A;
|
sl@0
|
570 |
BN_with_flags(pA, A, BN_FLG_CONSTTIME);
|
sl@0
|
571 |
|
sl@0
|
572 |
/* (D, M) := (A/B, A%B) ... */
|
sl@0
|
573 |
if (!BN_div(D,M,pA,B,ctx)) goto err;
|
sl@0
|
574 |
|
sl@0
|
575 |
/* Now
|
sl@0
|
576 |
* A = D*B + M;
|
sl@0
|
577 |
* thus we have
|
sl@0
|
578 |
* (**) sign*Y*a == D*B + M (mod |n|).
|
sl@0
|
579 |
*/
|
sl@0
|
580 |
|
sl@0
|
581 |
tmp=A; /* keep the BIGNUM object, the value does not matter */
|
sl@0
|
582 |
|
sl@0
|
583 |
/* (A, B) := (B, A mod B) ... */
|
sl@0
|
584 |
A=B;
|
sl@0
|
585 |
B=M;
|
sl@0
|
586 |
/* ... so we have 0 <= B < A again */
|
sl@0
|
587 |
|
sl@0
|
588 |
/* Since the former M is now B and the former B is now A,
|
sl@0
|
589 |
* (**) translates into
|
sl@0
|
590 |
* sign*Y*a == D*A + B (mod |n|),
|
sl@0
|
591 |
* i.e.
|
sl@0
|
592 |
* sign*Y*a - D*A == B (mod |n|).
|
sl@0
|
593 |
* Similarly, (*) translates into
|
sl@0
|
594 |
* -sign*X*a == A (mod |n|).
|
sl@0
|
595 |
*
|
sl@0
|
596 |
* Thus,
|
sl@0
|
597 |
* sign*Y*a + D*sign*X*a == B (mod |n|),
|
sl@0
|
598 |
* i.e.
|
sl@0
|
599 |
* sign*(Y + D*X)*a == B (mod |n|).
|
sl@0
|
600 |
*
|
sl@0
|
601 |
* So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
|
sl@0
|
602 |
* -sign*X*a == B (mod |n|),
|
sl@0
|
603 |
* sign*Y*a == A (mod |n|).
|
sl@0
|
604 |
* Note that X and Y stay non-negative all the time.
|
sl@0
|
605 |
*/
|
sl@0
|
606 |
|
sl@0
|
607 |
if (!BN_mul(tmp,D,X,ctx)) goto err;
|
sl@0
|
608 |
if (!BN_add(tmp,tmp,Y)) goto err;
|
sl@0
|
609 |
|
sl@0
|
610 |
M=Y; /* keep the BIGNUM object, the value does not matter */
|
sl@0
|
611 |
Y=X;
|
sl@0
|
612 |
X=tmp;
|
sl@0
|
613 |
sign = -sign;
|
sl@0
|
614 |
}
|
sl@0
|
615 |
|
sl@0
|
616 |
/*
|
sl@0
|
617 |
* The while loop (Euclid's algorithm) ends when
|
sl@0
|
618 |
* A == gcd(a,n);
|
sl@0
|
619 |
* we have
|
sl@0
|
620 |
* sign*Y*a == A (mod |n|),
|
sl@0
|
621 |
* where Y is non-negative.
|
sl@0
|
622 |
*/
|
sl@0
|
623 |
|
sl@0
|
624 |
if (sign < 0)
|
sl@0
|
625 |
{
|
sl@0
|
626 |
if (!BN_sub(Y,n,Y)) goto err;
|
sl@0
|
627 |
}
|
sl@0
|
628 |
/* Now Y*a == A (mod |n|). */
|
sl@0
|
629 |
|
sl@0
|
630 |
if (BN_is_one(A))
|
sl@0
|
631 |
{
|
sl@0
|
632 |
/* Y*a == 1 (mod |n|) */
|
sl@0
|
633 |
if (!Y->neg && BN_ucmp(Y,n) < 0)
|
sl@0
|
634 |
{
|
sl@0
|
635 |
if (!BN_copy(R,Y)) goto err;
|
sl@0
|
636 |
}
|
sl@0
|
637 |
else
|
sl@0
|
638 |
{
|
sl@0
|
639 |
if (!BN_nnmod(R,Y,n,ctx)) goto err;
|
sl@0
|
640 |
}
|
sl@0
|
641 |
}
|
sl@0
|
642 |
else
|
sl@0
|
643 |
{
|
sl@0
|
644 |
BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH,BN_R_NO_INVERSE);
|
sl@0
|
645 |
goto err;
|
sl@0
|
646 |
}
|
sl@0
|
647 |
ret=R;
|
sl@0
|
648 |
err:
|
sl@0
|
649 |
if ((ret == NULL) && (in == NULL)) BN_free(R);
|
sl@0
|
650 |
BN_CTX_end(ctx);
|
sl@0
|
651 |
bn_check_top(ret);
|
sl@0
|
652 |
return(ret);
|
sl@0
|
653 |
}
|