1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
57 /* ====================================================================
58 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
109 #include <openssl/bn.h>
113 #include <openssl/err.h>
115 #include "internal.h"
118 static BN_ULONG word_is_odd_mask(BN_ULONG a) { return (BN_ULONG)0 - (a & 1); }
120 static void maybe_rshift1_words(BN_ULONG *a, BN_ULONG mask, BN_ULONG *tmp,
122 bn_rshift1_words(tmp, a, num);
123 bn_select_words(a, mask, tmp, a, num);
126 static void maybe_rshift1_words_carry(BN_ULONG *a, BN_ULONG carry,
127 BN_ULONG mask, BN_ULONG *tmp,
129 maybe_rshift1_words(a, mask, tmp, num);
132 a[num - 1] |= carry << (BN_BITS2-1);
136 static BN_ULONG maybe_add_words(BN_ULONG *a, BN_ULONG mask, const BN_ULONG *b,
137 BN_ULONG *tmp, size_t num) {
138 BN_ULONG carry = bn_add_words(tmp, a, b, num);
139 bn_select_words(a, mask, tmp, a, num);
143 static int bn_gcd_consttime(BIGNUM *r, unsigned *out_shift, const BIGNUM *x,
144 const BIGNUM *y, BN_CTX *ctx) {
145 size_t width = x->width > y->width ? x->width : y->width;
152 // This is a constant-time implementation of Stein's algorithm (binary GCD).
155 BIGNUM *u = BN_CTX_get(ctx);
156 BIGNUM *v = BN_CTX_get(ctx);
157 BIGNUM *tmp = BN_CTX_get(ctx);
158 if (u == NULL || v == NULL || tmp == NULL ||
161 !bn_resize_words(u, width) ||
162 !bn_resize_words(v, width) ||
163 !bn_resize_words(tmp, width)) {
167 // Each loop iteration halves at least one of |u| and |v|. Thus we need at
168 // most the combined bit width of inputs for at least one value to be zero.
169 unsigned x_bits = x->width * BN_BITS2, y_bits = y->width * BN_BITS2;
170 unsigned num_iters = x_bits + y_bits;
171 if (num_iters < x_bits) {
172 OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
177 for (unsigned i = 0; i < num_iters; i++) {
178 BN_ULONG both_odd = word_is_odd_mask(u->d[0]) & word_is_odd_mask(v->d[0]);
180 // If both |u| and |v| are odd, subtract the smaller from the larger.
181 BN_ULONG u_less_than_v =
182 (BN_ULONG)0 - bn_sub_words(tmp->d, u->d, v->d, width);
183 bn_select_words(u->d, both_odd & ~u_less_than_v, tmp->d, u->d, width);
184 bn_sub_words(tmp->d, v->d, u->d, width);
185 bn_select_words(v->d, both_odd & u_less_than_v, tmp->d, v->d, width);
187 // At least one of |u| and |v| is now even.
188 BN_ULONG u_is_odd = word_is_odd_mask(u->d[0]);
189 BN_ULONG v_is_odd = word_is_odd_mask(v->d[0]);
190 assert(!(u_is_odd & v_is_odd));
192 // If both are even, the final GCD gains a factor of two.
193 shift += 1 & (~u_is_odd & ~v_is_odd);
195 // Halve any which are even.
196 maybe_rshift1_words(u->d, ~u_is_odd, tmp->d, width);
197 maybe_rshift1_words(v->d, ~v_is_odd, tmp->d, width);
200 // One of |u| or |v| is zero at this point. The algorithm usually makes |u|
201 // zero, unless |y| was already zero on input. Fix this by combining the
203 assert(BN_is_zero(u) || BN_is_zero(v));
204 for (size_t i = 0; i < width; i++) {
209 ret = bn_set_words(r, v->d, width);
216 int BN_gcd(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx) {
218 return bn_gcd_consttime(r, &shift, x, y, ctx) &&
219 BN_lshift(r, r, shift);
222 int bn_is_relatively_prime(int *out_relatively_prime, const BIGNUM *x,
223 const BIGNUM *y, BN_CTX *ctx) {
227 BIGNUM *gcd = BN_CTX_get(ctx);
229 !bn_gcd_consttime(gcd, &shift, x, y, ctx)) {
233 // Check that 2^|shift| * |gcd| is one.
234 if (gcd->width == 0) {
235 *out_relatively_prime = 0;
237 BN_ULONG mask = shift | (gcd->d[0] ^ 1);
238 for (int i = 1; i < gcd->width; i++) {
241 *out_relatively_prime = mask == 0;
250 int bn_lcm_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
253 BIGNUM *gcd = BN_CTX_get(ctx);
254 int ret = gcd != NULL &&
255 bn_mul_consttime(r, a, b, ctx) &&
256 bn_gcd_consttime(gcd, &shift, a, b, ctx) &&
257 bn_div_consttime(r, NULL, r, gcd, ctx) &&
258 bn_rshift_secret_shift(r, r, shift, ctx);
263 int bn_mod_inverse_consttime(BIGNUM *r, int *out_no_inverse, const BIGNUM *a,
264 const BIGNUM *n, BN_CTX *ctx) {
266 if (BN_is_negative(a) || BN_ucmp(a, n) >= 0) {
267 OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
276 OPENSSL_PUT_ERROR(BN, BN_R_NO_INVERSE);
280 // This is a constant-time implementation of the extended binary GCD
281 // algorithm. It is adapted from the Handbook of Applied Cryptography, section
282 // 14.4.3, algorithm 14.51, and modified to bound coefficients and avoid
285 // For more details and proof of correctness, see
286 // https://github.com/mit-plv/fiat-crypto/pull/333. In particular, see |step|
287 // and |mod_inverse_consttime| for the algorithm in Gallina and see
288 // |mod_inverse_consttime_spec| for the correctness result.
290 if (!BN_is_odd(a) && !BN_is_odd(n)) {
292 OPENSSL_PUT_ERROR(BN, BN_R_NO_INVERSE);
296 // This function exists to compute the RSA private exponent, where |a| is one
297 // word. We'll thus use |a_width| when available.
298 size_t n_width = n->width, a_width = a->width;
299 if (a_width > n_width) {
305 BIGNUM *u = BN_CTX_get(ctx);
306 BIGNUM *v = BN_CTX_get(ctx);
307 BIGNUM *A = BN_CTX_get(ctx);
308 BIGNUM *B = BN_CTX_get(ctx);
309 BIGNUM *C = BN_CTX_get(ctx);
310 BIGNUM *D = BN_CTX_get(ctx);
311 BIGNUM *tmp = BN_CTX_get(ctx);
312 BIGNUM *tmp2 = BN_CTX_get(ctx);
313 if (u == NULL || v == NULL || A == NULL || B == NULL || C == NULL ||
314 D == NULL || tmp == NULL || tmp2 == NULL ||
319 // For convenience, size |u| and |v| equivalently.
320 !bn_resize_words(u, n_width) ||
321 !bn_resize_words(v, n_width) ||
322 // |A| and |C| are bounded by |m|.
323 !bn_resize_words(A, n_width) ||
324 !bn_resize_words(C, n_width) ||
325 // |B| and |D| are bounded by |a|.
326 !bn_resize_words(B, a_width) ||
327 !bn_resize_words(D, a_width) ||
328 // |tmp| and |tmp2| may be used at either size.
329 !bn_resize_words(tmp, n_width) ||
330 !bn_resize_words(tmp2, n_width)) {
334 // Each loop iteration halves at least one of |u| and |v|. Thus we need at
335 // most the combined bit width of inputs for at least one value to be zero.
336 unsigned a_bits = a_width * BN_BITS2, n_bits = n_width * BN_BITS2;
337 unsigned num_iters = a_bits + n_bits;
338 if (num_iters < a_bits) {
339 OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
343 // Before and after each loop iteration, the following hold:
354 // After each loop iteration, u and v only get smaller, and at least one of
355 // them shrinks by at least a factor of two.
356 for (unsigned i = 0; i < num_iters; i++) {
357 BN_ULONG both_odd = word_is_odd_mask(u->d[0]) & word_is_odd_mask(v->d[0]);
359 // If both |u| and |v| are odd, subtract the smaller from the larger.
360 BN_ULONG v_less_than_u =
361 (BN_ULONG)0 - bn_sub_words(tmp->d, v->d, u->d, n_width);
362 bn_select_words(v->d, both_odd & ~v_less_than_u, tmp->d, v->d, n_width);
363 bn_sub_words(tmp->d, u->d, v->d, n_width);
364 bn_select_words(u->d, both_odd & v_less_than_u, tmp->d, u->d, n_width);
366 // If we updated one of the values, update the corresponding coefficient.
367 BN_ULONG carry = bn_add_words(tmp->d, A->d, C->d, n_width);
368 carry -= bn_sub_words(tmp2->d, tmp->d, n->d, n_width);
369 bn_select_words(tmp->d, carry, tmp->d, tmp2->d, n_width);
370 bn_select_words(A->d, both_odd & v_less_than_u, tmp->d, A->d, n_width);
371 bn_select_words(C->d, both_odd & ~v_less_than_u, tmp->d, C->d, n_width);
373 bn_add_words(tmp->d, B->d, D->d, a_width);
374 bn_sub_words(tmp2->d, tmp->d, a->d, a_width);
375 bn_select_words(tmp->d, carry, tmp->d, tmp2->d, a_width);
376 bn_select_words(B->d, both_odd & v_less_than_u, tmp->d, B->d, a_width);
377 bn_select_words(D->d, both_odd & ~v_less_than_u, tmp->d, D->d, a_width);
379 // Our loop invariants hold at this point. Additionally, exactly one of |u|
380 // and |v| is now even.
381 BN_ULONG u_is_even = ~word_is_odd_mask(u->d[0]);
382 BN_ULONG v_is_even = ~word_is_odd_mask(v->d[0]);
383 assert(u_is_even != v_is_even);
385 // Halve the even one and adjust the corresponding coefficient.
386 maybe_rshift1_words(u->d, u_is_even, tmp->d, n_width);
387 BN_ULONG A_or_B_is_odd =
388 word_is_odd_mask(A->d[0]) | word_is_odd_mask(B->d[0]);
390 maybe_add_words(A->d, A_or_B_is_odd & u_is_even, n->d, tmp->d, n_width);
392 maybe_add_words(B->d, A_or_B_is_odd & u_is_even, a->d, tmp->d, a_width);
393 maybe_rshift1_words_carry(A->d, A_carry, u_is_even, tmp->d, n_width);
394 maybe_rshift1_words_carry(B->d, B_carry, u_is_even, tmp->d, a_width);
396 maybe_rshift1_words(v->d, v_is_even, tmp->d, n_width);
397 BN_ULONG C_or_D_is_odd =
398 word_is_odd_mask(C->d[0]) | word_is_odd_mask(D->d[0]);
400 maybe_add_words(C->d, C_or_D_is_odd & v_is_even, n->d, tmp->d, n_width);
402 maybe_add_words(D->d, C_or_D_is_odd & v_is_even, a->d, tmp->d, a_width);
403 maybe_rshift1_words_carry(C->d, C_carry, v_is_even, tmp->d, n_width);
404 maybe_rshift1_words_carry(D->d, D_carry, v_is_even, tmp->d, a_width);
407 assert(BN_is_zero(v));
410 OPENSSL_PUT_ERROR(BN, BN_R_NO_INVERSE);
414 ret = BN_copy(r, A) != NULL;
421 int BN_mod_inverse_odd(BIGNUM *out, int *out_no_inverse, const BIGNUM *a,
422 const BIGNUM *n, BN_CTX *ctx) {
426 OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
430 if (BN_is_negative(a) || BN_cmp(a, n) >= 0) {
431 OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
435 BIGNUM *A, *B, *X, *Y;
451 if (!BN_one(X) || BN_copy(B, a) == NULL || BN_copy(A, n) == NULL) {
456 // From B = a mod |n|, A = |n| it follows that
459 // -sign*X*a == B (mod |n|),
460 // sign*Y*a == A (mod |n|).
462 // Binary inversion algorithm; requires odd modulus. This is faster than the
463 // general algorithm if the modulus is sufficiently small (about 400 .. 500
464 // bits on 32-bit systems, but much more on 64-bit systems)
467 while (!BN_is_zero(B)) {
470 // (1) -sign*X*a == B (mod |n|),
471 // (2) sign*Y*a == A (mod |n|)
473 // Now divide B by the maximum possible power of two in the integers,
474 // and divide X by the same value mod |n|.
475 // When we're done, (1) still holds.
477 while (!BN_is_bit_set(B, shift)) {
482 if (!BN_uadd(X, X, n)) {
486 // now X is even, so we can easily divide it by two
487 if (!BN_rshift1(X, X)) {
492 if (!BN_rshift(B, B, shift)) {
497 // Same for A and Y. Afterwards, (2) still holds.
499 while (!BN_is_bit_set(A, shift)) {
504 if (!BN_uadd(Y, Y, n)) {
509 if (!BN_rshift1(Y, Y)) {
514 if (!BN_rshift(A, A, shift)) {
519 // We still have (1) and (2).
520 // Both A and B are odd.
521 // The following computations ensure that
525 // (1) -sign*X*a == B (mod |n|),
526 // (2) sign*Y*a == A (mod |n|),
528 // and that either A or B is even in the next iteration.
529 if (BN_ucmp(B, A) >= 0) {
530 // -sign*(X + Y)*a == B - A (mod |n|)
531 if (!BN_uadd(X, X, Y)) {
534 // NB: we could use BN_mod_add_quick(X, X, Y, n), but that
535 // actually makes the algorithm slower
536 if (!BN_usub(B, B, A)) {
540 // sign*(X + Y)*a == A - B (mod |n|)
541 if (!BN_uadd(Y, Y, X)) {
544 // as above, BN_mod_add_quick(Y, Y, X, n) would slow things down
545 if (!BN_usub(A, A, B)) {
553 OPENSSL_PUT_ERROR(BN, BN_R_NO_INVERSE);
557 // The while loop (Euclid's algorithm) ends when
560 // sign*Y*a == A (mod |n|),
561 // where Y is non-negative.
564 if (!BN_sub(Y, n, Y)) {
568 // Now Y*a == A (mod |n|).
570 // Y*a == 1 (mod |n|)
571 if (!Y->neg && BN_ucmp(Y, n) < 0) {
572 if (!BN_copy(R, Y)) {
576 if (!BN_nnmod(R, Y, n, ctx)) {
588 BIGNUM *BN_mod_inverse(BIGNUM *out, const BIGNUM *a, const BIGNUM *n,
590 BIGNUM *new_out = NULL;
593 if (new_out == NULL) {
594 OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
601 BIGNUM *a_reduced = NULL;
602 if (a->neg || BN_ucmp(a, n) >= 0) {
603 a_reduced = BN_dup(a);
604 if (a_reduced == NULL) {
607 if (!BN_nnmod(a_reduced, a_reduced, n, ctx)) {
615 if (!bn_mod_inverse_consttime(out, &no_inverse, a, n, ctx)) {
618 } else if (!BN_mod_inverse_odd(out, &no_inverse, a, n, ctx)) {
633 int BN_mod_inverse_blinded(BIGNUM *out, int *out_no_inverse, const BIGNUM *a,
634 const BN_MONT_CTX *mont, BN_CTX *ctx) {
637 if (BN_is_negative(a) || BN_cmp(a, &mont->N) >= 0) {
638 OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
643 BIGNUM blinding_factor;
644 BN_init(&blinding_factor);
646 if (!BN_rand_range_ex(&blinding_factor, 1, &mont->N) ||
647 !BN_mod_mul_montgomery(out, &blinding_factor, a, mont, ctx) ||
648 !BN_mod_inverse_odd(out, out_no_inverse, out, &mont->N, ctx) ||
649 !BN_mod_mul_montgomery(out, &blinding_factor, out, mont, ctx)) {
650 OPENSSL_PUT_ERROR(BN, ERR_R_BN_LIB);
657 BN_free(&blinding_factor);
661 int bn_mod_inverse_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
662 BN_CTX *ctx, const BN_MONT_CTX *mont_p) {
664 BIGNUM *p_minus_2 = BN_CTX_get(ctx);
665 int ok = p_minus_2 != NULL &&
666 BN_copy(p_minus_2, p) &&
667 BN_sub_word(p_minus_2, 2) &&
668 BN_mod_exp_mont(out, a, p_minus_2, p, ctx, mont_p);
673 int bn_mod_inverse_secret_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
674 BN_CTX *ctx, const BN_MONT_CTX *mont_p) {
676 BIGNUM *p_minus_2 = BN_CTX_get(ctx);
677 int ok = p_minus_2 != NULL &&
678 BN_copy(p_minus_2, p) &&
679 BN_sub_word(p_minus_2, 2) &&
680 BN_mod_exp_mont_consttime(out, a, p_minus_2, p, ctx, mont_p);