2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/cryptlib.h"
13 static BIGNUM *euclid(BIGNUM *a, BIGNUM *b);
15 int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
26 if (a == NULL || b == NULL)
29 if (BN_copy(a, in_a) == NULL)
31 if (BN_copy(b, in_b) == NULL)
36 if (BN_cmp(a, b) < 0) {
45 if (BN_copy(r, t) == NULL)
54 static BIGNUM *euclid(BIGNUM *a, BIGNUM *b)
63 while (!BN_is_zero(b)) {
70 if (!BN_rshift1(a, a))
72 if (BN_cmp(a, b) < 0) {
77 } else { /* a odd - b even */
79 if (!BN_rshift1(b, b))
81 if (BN_cmp(a, b) < 0) {
87 } else { /* a is even */
90 if (!BN_rshift1(a, a))
92 if (BN_cmp(a, b) < 0) {
97 } else { /* a even - b even */
99 if (!BN_rshift1(a, a))
101 if (!BN_rshift1(b, b))
110 if (!BN_lshift(a, a, shifts))
119 /* solves ax == 1 (mod n) */
120 static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
121 const BIGNUM *a, const BIGNUM *n,
124 BIGNUM *BN_mod_inverse(BIGNUM *in,
125 const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
129 rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);
131 BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);
135 BIGNUM *int_bn_mod_inverse(BIGNUM *in,
136 const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
139 BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;
146 if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)
147 || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {
148 return BN_mod_inverse_no_branch(in, a, n, ctx);
174 if (BN_copy(B, a) == NULL)
176 if (BN_copy(A, n) == NULL)
179 if (B->neg || (BN_ucmp(B, A) >= 0)) {
180 if (!BN_nnmod(B, B, A, ctx))
185 * From B = a mod |n|, A = |n| it follows that
188 * -sign*X*a == B (mod |n|),
189 * sign*Y*a == A (mod |n|).
192 if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {
194 * Binary inversion algorithm; requires odd modulus. This is faster
195 * than the general algorithm if the modulus is sufficiently small
196 * (about 400 .. 500 bits on 32-bit systems, but much more on 64-bit
201 while (!BN_is_zero(B)) {
205 * (1) -sign*X*a == B (mod |n|),
206 * (2) sign*Y*a == A (mod |n|)
210 * Now divide B by the maximum possible power of two in the
211 * integers, and divide X by the same value mod |n|. When we're
212 * done, (1) still holds.
215 while (!BN_is_bit_set(B, shift)) { /* note that 0 < B */
219 if (!BN_uadd(X, X, n))
223 * now X is even, so we can easily divide it by two
225 if (!BN_rshift1(X, X))
229 if (!BN_rshift(B, B, shift))
234 * Same for A and Y. Afterwards, (2) still holds.
237 while (!BN_is_bit_set(A, shift)) { /* note that 0 < A */
241 if (!BN_uadd(Y, Y, n))
245 if (!BN_rshift1(Y, Y))
249 if (!BN_rshift(A, A, shift))
254 * We still have (1) and (2).
255 * Both A and B are odd.
256 * The following computations ensure that
260 * (1) -sign*X*a == B (mod |n|),
261 * (2) sign*Y*a == A (mod |n|),
263 * and that either A or B is even in the next iteration.
265 if (BN_ucmp(B, A) >= 0) {
266 /* -sign*(X + Y)*a == B - A (mod |n|) */
267 if (!BN_uadd(X, X, Y))
270 * NB: we could use BN_mod_add_quick(X, X, Y, n), but that
271 * actually makes the algorithm slower
273 if (!BN_usub(B, B, A))
276 /* sign*(X + Y)*a == A - B (mod |n|) */
277 if (!BN_uadd(Y, Y, X))
280 * as above, BN_mod_add_quick(Y, Y, X, n) would slow things
283 if (!BN_usub(A, A, B))
288 /* general inversion algorithm */
290 while (!BN_is_zero(B)) {
295 * (*) -sign*X*a == B (mod |n|),
296 * sign*Y*a == A (mod |n|)
299 /* (D, M) := (A/B, A%B) ... */
300 if (BN_num_bits(A) == BN_num_bits(B)) {
303 if (!BN_sub(M, A, B))
305 } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {
306 /* A/B is 1, 2, or 3 */
307 if (!BN_lshift1(T, B))
309 if (BN_ucmp(A, T) < 0) {
310 /* A < 2*B, so D=1 */
313 if (!BN_sub(M, A, B))
316 /* A >= 2*B, so D=2 or D=3 */
317 if (!BN_sub(M, A, T))
319 if (!BN_add(D, T, B))
320 goto err; /* use D (:= 3*B) as temp */
321 if (BN_ucmp(A, D) < 0) {
322 /* A < 3*B, so D=2 */
323 if (!BN_set_word(D, 2))
326 * M (= A - 2*B) already has the correct value
329 /* only D=3 remains */
330 if (!BN_set_word(D, 3))
333 * currently M = A - 2*B, but we need M = A - 3*B
335 if (!BN_sub(M, M, B))
340 if (!BN_div(D, M, A, B, ctx))
348 * (**) sign*Y*a == D*B + M (mod |n|).
351 tmp = A; /* keep the BIGNUM object, the value does not
354 /* (A, B) := (B, A mod B) ... */
357 /* ... so we have 0 <= B < A again */
360 * Since the former M is now B and the former B is now A,
361 * (**) translates into
362 * sign*Y*a == D*A + B (mod |n|),
364 * sign*Y*a - D*A == B (mod |n|).
365 * Similarly, (*) translates into
366 * -sign*X*a == A (mod |n|).
369 * sign*Y*a + D*sign*X*a == B (mod |n|),
371 * sign*(Y + D*X)*a == B (mod |n|).
373 * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
374 * -sign*X*a == B (mod |n|),
375 * sign*Y*a == A (mod |n|).
376 * Note that X and Y stay non-negative all the time.
380 * most of the time D is very small, so we can optimize tmp :=
384 if (!BN_add(tmp, X, Y))
387 if (BN_is_word(D, 2)) {
388 if (!BN_lshift1(tmp, X))
390 } else if (BN_is_word(D, 4)) {
391 if (!BN_lshift(tmp, X, 2))
393 } else if (D->top == 1) {
394 if (!BN_copy(tmp, X))
396 if (!BN_mul_word(tmp, D->d[0]))
399 if (!BN_mul(tmp, D, X, ctx))
402 if (!BN_add(tmp, tmp, Y))
406 M = Y; /* keep the BIGNUM object, the value does not
415 * The while loop (Euclid's algorithm) ends when
418 * sign*Y*a == A (mod |n|),
419 * where Y is non-negative.
423 if (!BN_sub(Y, n, Y))
426 /* Now Y*a == A (mod |n|). */
429 /* Y*a == 1 (mod |n|) */
430 if (!Y->neg && BN_ucmp(Y, n) < 0) {
434 if (!BN_nnmod(R, Y, n, ctx))
444 if ((ret == NULL) && (in == NULL))
452 * BN_mod_inverse_no_branch is a special version of BN_mod_inverse. It does
453 * not contain branches that may leak sensitive information.
455 static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
456 const BIGNUM *a, const BIGNUM *n,
459 BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;
486 if (BN_copy(B, a) == NULL)
488 if (BN_copy(A, n) == NULL)
492 if (B->neg || (BN_ucmp(B, A) >= 0)) {
494 * Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
495 * BN_div_no_branch will be called eventually.
500 BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);
501 if (!BN_nnmod(B, &local_B, A, ctx))
503 /* Ensure local_B goes out of scope before any further use of B */
508 * From B = a mod |n|, A = |n| it follows that
511 * -sign*X*a == B (mod |n|),
512 * sign*Y*a == A (mod |n|).
515 while (!BN_is_zero(B)) {
520 * (*) -sign*X*a == B (mod |n|),
521 * sign*Y*a == A (mod |n|)
525 * Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
526 * BN_div_no_branch will be called eventually.
531 BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);
533 /* (D, M) := (A/B, A%B) ... */
534 if (!BN_div(D, M, &local_A, B, ctx))
536 /* Ensure local_A goes out of scope before any further use of A */
543 * (**) sign*Y*a == D*B + M (mod |n|).
546 tmp = A; /* keep the BIGNUM object, the value does not
549 /* (A, B) := (B, A mod B) ... */
552 /* ... so we have 0 <= B < A again */
555 * Since the former M is now B and the former B is now A,
556 * (**) translates into
557 * sign*Y*a == D*A + B (mod |n|),
559 * sign*Y*a - D*A == B (mod |n|).
560 * Similarly, (*) translates into
561 * -sign*X*a == A (mod |n|).
564 * sign*Y*a + D*sign*X*a == B (mod |n|),
566 * sign*(Y + D*X)*a == B (mod |n|).
568 * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
569 * -sign*X*a == B (mod |n|),
570 * sign*Y*a == A (mod |n|).
571 * Note that X and Y stay non-negative all the time.
574 if (!BN_mul(tmp, D, X, ctx))
576 if (!BN_add(tmp, tmp, Y))
579 M = Y; /* keep the BIGNUM object, the value does not
587 * The while loop (Euclid's algorithm) ends when
590 * sign*Y*a == A (mod |n|),
591 * where Y is non-negative.
595 if (!BN_sub(Y, n, Y))
598 /* Now Y*a == A (mod |n|). */
601 /* Y*a == 1 (mod |n|) */
602 if (!Y->neg && BN_ucmp(Y, n) < 0) {
606 if (!BN_nnmod(R, Y, n, ctx))
610 BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);
615 if ((ret == NULL) && (in == NULL))