diff options
author | beck <> | 2002-05-15 02:29:21 +0000 |
---|---|---|
committer | beck <> | 2002-05-15 02:29:21 +0000 |
commit | b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9 (patch) | |
tree | fa27cf82a1250b64ed3bf5f4a18c7354d470bbcc /src/lib/libcrypto/bn/bn_gcd.c | |
parent | e471e1ea98d673597b182ea85f29e30c97cd08b5 (diff) | |
download | openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.tar.gz openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.tar.bz2 openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.zip |
OpenSSL 0.9.7 stable 2002 05 08 merge
Diffstat (limited to 'src/lib/libcrypto/bn/bn_gcd.c')
-rw-r--r-- | src/lib/libcrypto/bn/bn_gcd.c | 338 |
1 files changed, 309 insertions, 29 deletions
diff --git a/src/lib/libcrypto/bn/bn_gcd.c b/src/lib/libcrypto/bn/bn_gcd.c index 398207196b..7649f63fd2 100644 --- a/src/lib/libcrypto/bn/bn_gcd.c +++ b/src/lib/libcrypto/bn/bn_gcd.c | |||
@@ -55,14 +55,66 @@ | |||
55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
56 | * [including the GNU Public Licence.] | 56 | * [including the GNU Public Licence.] |
57 | */ | 57 | */ |
58 | /* ==================================================================== | ||
59 | * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. | ||
60 | * | ||
61 | * Redistribution and use in source and binary forms, with or without | ||
62 | * modification, are permitted provided that the following conditions | ||
63 | * are met: | ||
64 | * | ||
65 | * 1. Redistributions of source code must retain the above copyright | ||
66 | * notice, this list of conditions and the following disclaimer. | ||
67 | * | ||
68 | * 2. Redistributions in binary form must reproduce the above copyright | ||
69 | * notice, this list of conditions and the following disclaimer in | ||
70 | * the documentation and/or other materials provided with the | ||
71 | * distribution. | ||
72 | * | ||
73 | * 3. All advertising materials mentioning features or use of this | ||
74 | * software must display the following acknowledgment: | ||
75 | * "This product includes software developed by the OpenSSL Project | ||
76 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
77 | * | ||
78 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
79 | * endorse or promote products derived from this software without | ||
80 | * prior written permission. For written permission, please contact | ||
81 | * openssl-core@openssl.org. | ||
82 | * | ||
83 | * 5. Products derived from this software may not be called "OpenSSL" | ||
84 | * nor may "OpenSSL" appear in their names without prior written | ||
85 | * permission of the OpenSSL Project. | ||
86 | * | ||
87 | * 6. Redistributions of any form whatsoever must retain the following | ||
88 | * acknowledgment: | ||
89 | * "This product includes software developed by the OpenSSL Project | ||
90 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
91 | * | ||
92 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
93 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
94 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
95 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
96 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
97 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
98 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
99 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
100 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
101 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
102 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
103 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
104 | * ==================================================================== | ||
105 | * | ||
106 | * This product includes cryptographic software written by Eric Young | ||
107 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
108 | * Hudson (tjh@cryptsoft.com). | ||
109 | * | ||
110 | */ | ||
58 | 111 | ||
59 | #include <stdio.h> | ||
60 | #include "cryptlib.h" | 112 | #include "cryptlib.h" |
61 | #include "bn_lcl.h" | 113 | #include "bn_lcl.h" |
62 | 114 | ||
63 | static BIGNUM *euclid(BIGNUM *a, BIGNUM *b); | 115 | static BIGNUM *euclid(BIGNUM *a, BIGNUM *b); |
64 | 116 | ||
65 | int BN_gcd(BIGNUM *r, BIGNUM *in_a, BIGNUM *in_b, BN_CTX *ctx) | 117 | int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx) |
66 | { | 118 | { |
67 | BIGNUM *a,*b,*t; | 119 | BIGNUM *a,*b,*t; |
68 | int ret=0; | 120 | int ret=0; |
@@ -77,6 +129,8 @@ int BN_gcd(BIGNUM *r, BIGNUM *in_a, BIGNUM *in_b, BN_CTX *ctx) | |||
77 | 129 | ||
78 | if (BN_copy(a,in_a) == NULL) goto err; | 130 | if (BN_copy(a,in_a) == NULL) goto err; |
79 | if (BN_copy(b,in_b) == NULL) goto err; | 131 | if (BN_copy(b,in_b) == NULL) goto err; |
132 | a->neg = 0; | ||
133 | b->neg = 0; | ||
80 | 134 | ||
81 | if (BN_cmp(a,b) < 0) { t=a; a=b; b=t; } | 135 | if (BN_cmp(a,b) < 0) { t=a; a=b; b=t; } |
82 | t=euclid(a,b); | 136 | t=euclid(a,b); |
@@ -97,10 +151,10 @@ static BIGNUM *euclid(BIGNUM *a, BIGNUM *b) | |||
97 | bn_check_top(a); | 151 | bn_check_top(a); |
98 | bn_check_top(b); | 152 | bn_check_top(b); |
99 | 153 | ||
100 | for (;;) | 154 | /* 0 <= b <= a */ |
155 | while (!BN_is_zero(b)) | ||
101 | { | 156 | { |
102 | if (BN_is_zero(b)) | 157 | /* 0 < b <= a */ |
103 | break; | ||
104 | 158 | ||
105 | if (BN_is_odd(a)) | 159 | if (BN_is_odd(a)) |
106 | { | 160 | { |
@@ -133,7 +187,9 @@ static BIGNUM *euclid(BIGNUM *a, BIGNUM *b) | |||
133 | shifts++; | 187 | shifts++; |
134 | } | 188 | } |
135 | } | 189 | } |
190 | /* 0 <= b <= a */ | ||
136 | } | 191 | } |
192 | |||
137 | if (shifts) | 193 | if (shifts) |
138 | { | 194 | { |
139 | if (!BN_lshift(a,a,shifts)) goto err; | 195 | if (!BN_lshift(a,a,shifts)) goto err; |
@@ -143,11 +199,13 @@ err: | |||
143 | return(NULL); | 199 | return(NULL); |
144 | } | 200 | } |
145 | 201 | ||
202 | |||
146 | /* solves ax == 1 (mod n) */ | 203 | /* solves ax == 1 (mod n) */ |
147 | BIGNUM *BN_mod_inverse(BIGNUM *in, BIGNUM *a, const BIGNUM *n, BN_CTX *ctx) | 204 | BIGNUM *BN_mod_inverse(BIGNUM *in, |
205 | const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx) | ||
148 | { | 206 | { |
149 | BIGNUM *A,*B,*X,*Y,*M,*D,*R=NULL; | 207 | BIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL; |
150 | BIGNUM *T,*ret=NULL; | 208 | BIGNUM *ret=NULL; |
151 | int sign; | 209 | int sign; |
152 | 210 | ||
153 | bn_check_top(a); | 211 | bn_check_top(a); |
@@ -160,7 +218,8 @@ BIGNUM *BN_mod_inverse(BIGNUM *in, BIGNUM *a, const BIGNUM *n, BN_CTX *ctx) | |||
160 | D = BN_CTX_get(ctx); | 218 | D = BN_CTX_get(ctx); |
161 | M = BN_CTX_get(ctx); | 219 | M = BN_CTX_get(ctx); |
162 | Y = BN_CTX_get(ctx); | 220 | Y = BN_CTX_get(ctx); |
163 | if (Y == NULL) goto err; | 221 | T = BN_CTX_get(ctx); |
222 | if (T == NULL) goto err; | ||
164 | 223 | ||
165 | if (in == NULL) | 224 | if (in == NULL) |
166 | R=BN_new(); | 225 | R=BN_new(); |
@@ -168,34 +227,256 @@ BIGNUM *BN_mod_inverse(BIGNUM *in, BIGNUM *a, const BIGNUM *n, BN_CTX *ctx) | |||
168 | R=in; | 227 | R=in; |
169 | if (R == NULL) goto err; | 228 | if (R == NULL) goto err; |
170 | 229 | ||
171 | BN_zero(X); | 230 | BN_one(X); |
172 | BN_one(Y); | 231 | BN_zero(Y); |
173 | if (BN_copy(A,a) == NULL) goto err; | 232 | if (BN_copy(B,a) == NULL) goto err; |
174 | if (BN_copy(B,n) == NULL) goto err; | 233 | if (BN_copy(A,n) == NULL) goto err; |
175 | sign=1; | 234 | A->neg = 0; |
235 | if (B->neg || (BN_ucmp(B, A) >= 0)) | ||
236 | { | ||
237 | if (!BN_nnmod(B, B, A, ctx)) goto err; | ||
238 | } | ||
239 | sign = -1; | ||
240 | /* From B = a mod |n|, A = |n| it follows that | ||
241 | * | ||
242 | * 0 <= B < A, | ||
243 | * -sign*X*a == B (mod |n|), | ||
244 | * sign*Y*a == A (mod |n|). | ||
245 | */ | ||
176 | 246 | ||
177 | while (!BN_is_zero(B)) | 247 | if (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048))) |
178 | { | 248 | { |
179 | if (!BN_div(D,M,A,B,ctx)) goto err; | 249 | /* Binary inversion algorithm; requires odd modulus. |
180 | T=A; | 250 | * This is faster than the general algorithm if the modulus |
181 | A=B; | 251 | * is sufficiently small (about 400 .. 500 bits on 32-bit |
182 | B=M; | 252 | * sytems, but much more on 64-bit systems) */ |
183 | /* T has a struct, M does not */ | 253 | int shift; |
184 | 254 | ||
185 | if (!BN_mul(T,D,X,ctx)) goto err; | 255 | while (!BN_is_zero(B)) |
186 | if (!BN_add(T,T,Y)) goto err; | 256 | { |
187 | M=Y; | 257 | /* |
188 | Y=X; | 258 | * 0 < B < |n|, |
189 | X=T; | 259 | * 0 < A <= |n|, |
190 | sign= -sign; | 260 | * (1) -sign*X*a == B (mod |n|), |
261 | * (2) sign*Y*a == A (mod |n|) | ||
262 | */ | ||
263 | |||
264 | /* Now divide B by the maximum possible power of two in the integers, | ||
265 | * and divide X by the same value mod |n|. | ||
266 | * When we're done, (1) still holds. */ | ||
267 | shift = 0; | ||
268 | while (!BN_is_bit_set(B, shift)) /* note that 0 < B */ | ||
269 | { | ||
270 | shift++; | ||
271 | |||
272 | if (BN_is_odd(X)) | ||
273 | { | ||
274 | if (!BN_uadd(X, X, n)) goto err; | ||
275 | } | ||
276 | /* now X is even, so we can easily divide it by two */ | ||
277 | if (!BN_rshift1(X, X)) goto err; | ||
278 | } | ||
279 | if (shift > 0) | ||
280 | { | ||
281 | if (!BN_rshift(B, B, shift)) goto err; | ||
282 | } | ||
283 | |||
284 | |||
285 | /* Same for A and Y. Afterwards, (2) still holds. */ | ||
286 | shift = 0; | ||
287 | while (!BN_is_bit_set(A, shift)) /* note that 0 < A */ | ||
288 | { | ||
289 | shift++; | ||
290 | |||
291 | if (BN_is_odd(Y)) | ||
292 | { | ||
293 | if (!BN_uadd(Y, Y, n)) goto err; | ||
294 | } | ||
295 | /* now Y is even */ | ||
296 | if (!BN_rshift1(Y, Y)) goto err; | ||
297 | } | ||
298 | if (shift > 0) | ||
299 | { | ||
300 | if (!BN_rshift(A, A, shift)) goto err; | ||
301 | } | ||
302 | |||
303 | |||
304 | /* We still have (1) and (2). | ||
305 | * Both A and B are odd. | ||
306 | * The following computations ensure that | ||
307 | * | ||
308 | * 0 <= B < |n|, | ||
309 | * 0 < A < |n|, | ||
310 | * (1) -sign*X*a == B (mod |n|), | ||
311 | * (2) sign*Y*a == A (mod |n|), | ||
312 | * | ||
313 | * and that either A or B is even in the next iteration. | ||
314 | */ | ||
315 | if (BN_ucmp(B, A) >= 0) | ||
316 | { | ||
317 | /* -sign*(X + Y)*a == B - A (mod |n|) */ | ||
318 | if (!BN_uadd(X, X, Y)) goto err; | ||
319 | /* NB: we could use BN_mod_add_quick(X, X, Y, n), but that | ||
320 | * actually makes the algorithm slower */ | ||
321 | if (!BN_usub(B, B, A)) goto err; | ||
322 | } | ||
323 | else | ||
324 | { | ||
325 | /* sign*(X + Y)*a == A - B (mod |n|) */ | ||
326 | if (!BN_uadd(Y, Y, X)) goto err; | ||
327 | /* as above, BN_mod_add_quick(Y, Y, X, n) would slow things down */ | ||
328 | if (!BN_usub(A, A, B)) goto err; | ||
329 | } | ||
330 | } | ||
331 | } | ||
332 | else | ||
333 | { | ||
334 | /* general inversion algorithm */ | ||
335 | |||
336 | while (!BN_is_zero(B)) | ||
337 | { | ||
338 | BIGNUM *tmp; | ||
339 | |||
340 | /* | ||
341 | * 0 < B < A, | ||
342 | * (*) -sign*X*a == B (mod |n|), | ||
343 | * sign*Y*a == A (mod |n|) | ||
344 | */ | ||
345 | |||
346 | /* (D, M) := (A/B, A%B) ... */ | ||
347 | if (BN_num_bits(A) == BN_num_bits(B)) | ||
348 | { | ||
349 | if (!BN_one(D)) goto err; | ||
350 | if (!BN_sub(M,A,B)) goto err; | ||
351 | } | ||
352 | else if (BN_num_bits(A) == BN_num_bits(B) + 1) | ||
353 | { | ||
354 | /* A/B is 1, 2, or 3 */ | ||
355 | if (!BN_lshift1(T,B)) goto err; | ||
356 | if (BN_ucmp(A,T) < 0) | ||
357 | { | ||
358 | /* A < 2*B, so D=1 */ | ||
359 | if (!BN_one(D)) goto err; | ||
360 | if (!BN_sub(M,A,B)) goto err; | ||
361 | } | ||
362 | else | ||
363 | { | ||
364 | /* A >= 2*B, so D=2 or D=3 */ | ||
365 | if (!BN_sub(M,A,T)) goto err; | ||
366 | if (!BN_add(D,T,B)) goto err; /* use D (:= 3*B) as temp */ | ||
367 | if (BN_ucmp(A,D) < 0) | ||
368 | { | ||
369 | /* A < 3*B, so D=2 */ | ||
370 | if (!BN_set_word(D,2)) goto err; | ||
371 | /* M (= A - 2*B) already has the correct value */ | ||
372 | } | ||
373 | else | ||
374 | { | ||
375 | /* only D=3 remains */ | ||
376 | if (!BN_set_word(D,3)) goto err; | ||
377 | /* currently M = A - 2*B, but we need M = A - 3*B */ | ||
378 | if (!BN_sub(M,M,B)) goto err; | ||
379 | } | ||
380 | } | ||
381 | } | ||
382 | else | ||
383 | { | ||
384 | if (!BN_div(D,M,A,B,ctx)) goto err; | ||
385 | } | ||
386 | |||
387 | /* Now | ||
388 | * A = D*B + M; | ||
389 | * thus we have | ||
390 | * (**) sign*Y*a == D*B + M (mod |n|). | ||
391 | */ | ||
392 | |||
393 | tmp=A; /* keep the BIGNUM object, the value does not matter */ | ||
394 | |||
395 | /* (A, B) := (B, A mod B) ... */ | ||
396 | A=B; | ||
397 | B=M; | ||
398 | /* ... so we have 0 <= B < A again */ | ||
399 | |||
400 | /* Since the former M is now B and the former B is now A, | ||
401 | * (**) translates into | ||
402 | * sign*Y*a == D*A + B (mod |n|), | ||
403 | * i.e. | ||
404 | * sign*Y*a - D*A == B (mod |n|). | ||
405 | * Similarly, (*) translates into | ||
406 | * -sign*X*a == A (mod |n|). | ||
407 | * | ||
408 | * Thus, | ||
409 | * sign*Y*a + D*sign*X*a == B (mod |n|), | ||
410 | * i.e. | ||
411 | * sign*(Y + D*X)*a == B (mod |n|). | ||
412 | * | ||
413 | * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at | ||
414 | * -sign*X*a == B (mod |n|), | ||
415 | * sign*Y*a == A (mod |n|). | ||
416 | * Note that X and Y stay non-negative all the time. | ||
417 | */ | ||
418 | |||
419 | /* most of the time D is very small, so we can optimize tmp := D*X+Y */ | ||
420 | if (BN_is_one(D)) | ||
421 | { | ||
422 | if (!BN_add(tmp,X,Y)) goto err; | ||
423 | } | ||
424 | else | ||
425 | { | ||
426 | if (BN_is_word(D,2)) | ||
427 | { | ||
428 | if (!BN_lshift1(tmp,X)) goto err; | ||
429 | } | ||
430 | else if (BN_is_word(D,4)) | ||
431 | { | ||
432 | if (!BN_lshift(tmp,X,2)) goto err; | ||
433 | } | ||
434 | else if (D->top == 1) | ||
435 | { | ||
436 | if (!BN_copy(tmp,X)) goto err; | ||
437 | if (!BN_mul_word(tmp,D->d[0])) goto err; | ||
438 | } | ||
439 | else | ||
440 | { | ||
441 | if (!BN_mul(tmp,D,X,ctx)) goto err; | ||
442 | } | ||
443 | if (!BN_add(tmp,tmp,Y)) goto err; | ||
444 | } | ||
445 | |||
446 | M=Y; /* keep the BIGNUM object, the value does not matter */ | ||
447 | Y=X; | ||
448 | X=tmp; | ||
449 | sign = -sign; | ||
450 | } | ||
191 | } | 451 | } |
452 | |||
453 | /* | ||
454 | * The while loop (Euclid's algorithm) ends when | ||
455 | * A == gcd(a,n); | ||
456 | * we have | ||
457 | * sign*Y*a == A (mod |n|), | ||
458 | * where Y is non-negative. | ||
459 | */ | ||
460 | |||
192 | if (sign < 0) | 461 | if (sign < 0) |
193 | { | 462 | { |
194 | if (!BN_sub(Y,n,Y)) goto err; | 463 | if (!BN_sub(Y,n,Y)) goto err; |
195 | } | 464 | } |
465 | /* Now Y*a == A (mod |n|). */ | ||
466 | |||
196 | 467 | ||
197 | if (BN_is_one(A)) | 468 | if (BN_is_one(A)) |
198 | { if (!BN_mod(R,Y,n,ctx)) goto err; } | 469 | { |
470 | /* Y*a == 1 (mod |n|) */ | ||
471 | if (!Y->neg && BN_ucmp(Y,n) < 0) | ||
472 | { | ||
473 | if (!BN_copy(R,Y)) goto err; | ||
474 | } | ||
475 | else | ||
476 | { | ||
477 | if (!BN_nnmod(R,Y,n,ctx)) goto err; | ||
478 | } | ||
479 | } | ||
199 | else | 480 | else |
200 | { | 481 | { |
201 | BNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE); | 482 | BNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE); |
@@ -207,4 +488,3 @@ err: | |||
207 | BN_CTX_end(ctx); | 488 | BN_CTX_end(ctx); |
208 | return(ret); | 489 | return(ret); |
209 | } | 490 | } |
210 | |||