Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/bn/gcd.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected])
2 // Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
4
5 #include <openssl/bn.h>
6
7 #include <openssl/err.h>
8
9 #include "internal.h"
10
11
12 int BN_mod_inverse_odd(BIGNUM *out, int *out_no_inverse, const BIGNUM *a,
13 const BIGNUM *n, BN_CTX *ctx) {
14 *out_no_inverse = 0;
15
16 if (!BN_is_odd(n)) {
17 OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
18 return 0;
19 }
20
21 if (BN_is_negative(a) || BN_cmp(a, n) >= 0) {
22 OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
23 return 0;
24 }
25
26 BIGNUM *A, *B, *X, *Y;
27 int ret = 0;
28 int sign;
29
30 BN_CTX_start(ctx);
31 A = BN_CTX_get(ctx);
32 B = BN_CTX_get(ctx);
33 X = BN_CTX_get(ctx);
34 Y = BN_CTX_get(ctx);
35 if (Y == NULL) {
36 goto err;
37 }
38
39 BIGNUM *R = out;
40
41 BN_zero(Y);
42 if (!BN_one(X) || BN_copy(B, a) == NULL || BN_copy(A, n) == NULL) {
43 goto err;
44 }
45 A->neg = 0;
46 sign = -1;
47 // From B = a mod |n|, A = |n| it follows that
48 //
49 // 0 <= B < A,
50 // -sign*X*a == B (mod |n|),
51 // sign*Y*a == A (mod |n|).
52
53 // Binary inversion algorithm; requires odd modulus. This is faster than the
54 // general algorithm if the modulus is sufficiently small (about 400 .. 500
55 // bits on 32-bit systems, but much more on 64-bit systems)
56 int shift;
57
58 while (!BN_is_zero(B)) {
59 // 0 < B < |n|,
60 // 0 < A <= |n|,
61 // (1) -sign*X*a == B (mod |n|),
62 // (2) sign*Y*a == A (mod |n|)
63
64 // Now divide B by the maximum possible power of two in the integers,
65 // and divide X by the same value mod |n|.
66 // When we're done, (1) still holds.
67 shift = 0;
68 while (!BN_is_bit_set(B, shift)) {
69 // note that 0 < B
70 shift++;
71
72 if (BN_is_odd(X)) {
73 if (!BN_uadd(X, X, n)) {
74 goto err;
75 }
76 }
77 // now X is even, so we can easily divide it by two
78 if (!BN_rshift1(X, X)) {
79 goto err;
80 }
81 }
82 if (shift > 0) {
83 if (!BN_rshift(B, B, shift)) {
84 goto err;
85 }
86 }
87
88 // Same for A and Y. Afterwards, (2) still holds.
89 shift = 0;
90 while (!BN_is_bit_set(A, shift)) {
91 // note that 0 < A
92 shift++;
93
94 if (BN_is_odd(Y)) {
95 if (!BN_uadd(Y, Y, n)) {
96 goto err;
97 }
98 }
99 // now Y is even
100 if (!BN_rshift1(Y, Y)) {
101 goto err;
102 }
103 }
104 if (shift > 0) {
105 if (!BN_rshift(A, A, shift)) {
106 goto err;
107 }
108 }
109
110 // We still have (1) and (2).
111 // Both A and B are odd.
112 // The following computations ensure that
113 //
114 // 0 <= B < |n|,
115 // 0 < A < |n|,
116 // (1) -sign*X*a == B (mod |n|),
117 // (2) sign*Y*a == A (mod |n|),
118 //
119 // and that either A or B is even in the next iteration.
120 if (BN_ucmp(B, A) >= 0) {
121 // -sign*(X + Y)*a == B - A (mod |n|)
122 if (!BN_uadd(X, X, Y)) {
123 goto err;
124 }
125 // NB: we could use BN_mod_add_quick(X, X, Y, n), but that
126 // actually makes the algorithm slower
127 if (!BN_usub(B, B, A)) {
128 goto err;
129 }
130 } else {
131 // sign*(X + Y)*a == A - B (mod |n|)
132 if (!BN_uadd(Y, Y, X)) {
133 goto err;
134 }
135 // as above, BN_mod_add_quick(Y, Y, X, n) would slow things down
136 if (!BN_usub(A, A, B)) {
137 goto err;
138 }
139 }
140 }
141
142 if (!BN_is_one(A)) {
143 *out_no_inverse = 1;
144 OPENSSL_PUT_ERROR(BN, BN_R_NO_INVERSE);
145 goto err;
146 }
147
148 // The while loop (Euclid's algorithm) ends when
149 // A == gcd(a,n);
150 // we have
151 // sign*Y*a == A (mod |n|),
152 // where Y is non-negative.
153
154 if (sign < 0) {
155 if (!BN_sub(Y, n, Y)) {
156 goto err;
157 }
158 }
159 // Now Y*a == A (mod |n|).
160
161 // Y*a == 1 (mod |n|)
162 if (Y->neg || BN_ucmp(Y, n) >= 0) {
163 if (!BN_nnmod(Y, Y, n, ctx)) {
164 goto err;
165 }
166 }
167 if (!BN_copy(R, Y)) {
168 goto err;
169 }
170
171 ret = 1;
172
173 err:
174 BN_CTX_end(ctx);
175 return ret;
176 }
177
178 BIGNUM *BN_mod_inverse(BIGNUM *out, const BIGNUM *a, const BIGNUM *n,
179 BN_CTX *ctx) {
180 BIGNUM *new_out = NULL;
181 if (out == NULL) {
182 new_out = BN_new();
183 if (new_out == NULL) {
184 return NULL;
185 }
186 out = new_out;
187 }
188
189 int ok = 0;
190 BIGNUM *a_reduced = NULL;
191 if (a->neg || BN_ucmp(a, n) >= 0) {
192 a_reduced = BN_dup(a);
193 if (a_reduced == NULL) {
194 goto err;
195 }
196 if (!BN_nnmod(a_reduced, a_reduced, n, ctx)) {
197 goto err;
198 }
199 a = a_reduced;
200 }
201
202 int no_inverse;
203 if (!BN_is_odd(n)) {
204 if (!bn_mod_inverse_consttime(out, &no_inverse, a, n, ctx)) {
205 goto err;
206 }
207 } else if (!BN_mod_inverse_odd(out, &no_inverse, a, n, ctx)) {
208 goto err;
209 }
210
211 ok = 1;
212
213 err:
214 if (!ok) {
215 BN_free(new_out);
216 out = NULL;
217 }
218 BN_free(a_reduced);
219 return out;
220 }
221
222 int BN_mod_inverse_blinded(BIGNUM *out, int *out_no_inverse, const BIGNUM *a,
223 const BN_MONT_CTX *mont, BN_CTX *ctx) {
224 *out_no_inverse = 0;
225
226 // |a| is secret, but it is required to be in range, so these comparisons may
227 // be leaked.
228 if (BN_is_negative(a) ||
229 constant_time_declassify_int(BN_cmp(a, &mont->N) >= 0)) {
230 OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
231 return 0;
232 }
233
234 int ret = 0;
235 BIGNUM blinding_factor;
236 BN_init(&blinding_factor);
237
238 // |BN_mod_inverse_odd| is leaky, so generate a secret blinding factor and
239 // blind |a|. This works because (ar)^-1 * r = a^-1, supposing r is
240 // invertible. If r is not invertible, this function will fail. However, we
241 // only use this in RSA, where stumbling on an uninvertible element means
242 // stumbling on the key's factorization. That is, if this function fails, the
243 // RSA key was not actually a product of two large primes.
244 //
245 // TODO(crbug.com/boringssl/677): When the PRNG output is marked secret by
246 // default, the explicit |bn_secret| call can be removed.
247 if (!BN_rand_range_ex(&blinding_factor, 1, &mont->N)) {
248 goto err;
249 }
250 bn_secret(&blinding_factor);
251 if (!BN_mod_mul_montgomery(out, &blinding_factor, a, mont, ctx)) {
252 goto err;
253 }
254
255 // Once blinded, |out| is no longer secret, so it may be passed to a leaky
256 // mod inverse function. Note |blinding_factor| is secret, so |out| will be
257 // secret again after multiplying.
258 bn_declassify(out);
259 if (!BN_mod_inverse_odd(out, out_no_inverse, out, &mont->N, ctx) ||
260 !BN_mod_mul_montgomery(out, &blinding_factor, out, mont, ctx)) {
261 goto err;
262 }
263
264 ret = 1;
265
266 err:
267 BN_free(&blinding_factor);
268 return ret;
269 }
270
271 int bn_mod_inverse_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
272 BN_CTX *ctx, const BN_MONT_CTX *mont_p) {
273 BN_CTX_start(ctx);
274 BIGNUM *p_minus_2 = BN_CTX_get(ctx);
275 int ok = p_minus_2 != NULL &&
276 BN_copy(p_minus_2, p) &&
277 BN_sub_word(p_minus_2, 2) &&
278 BN_mod_exp_mont(out, a, p_minus_2, p, ctx, mont_p);
279 BN_CTX_end(ctx);
280 return ok;
281 }
282
283 int bn_mod_inverse_secret_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
284 BN_CTX *ctx, const BN_MONT_CTX *mont_p) {
285 BN_CTX_start(ctx);
286 BIGNUM *p_minus_2 = BN_CTX_get(ctx);
287 int ok = p_minus_2 != NULL &&
288 BN_copy(p_minus_2, p) &&
289 BN_sub_word(p_minus_2, 2) &&
290 BN_mod_exp_mont_consttime(out, a, p_minus_2, p, ctx, mont_p);
291 BN_CTX_end(ctx);
292 return ok;
293 }