1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young ([email protected]).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
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 ([email protected]).
14 *
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.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
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 ([email protected])"
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 ([email protected])"
39 *
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
50 * SUCH DAMAGE.
51 *
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.]
56 */
57 /* ====================================================================
58 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
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
70 * distribution.
71 *
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/)"
76 *
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 * [email protected].
81 *
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.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
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 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * ([email protected]). This product includes software written by Tim
107 * Hudson ([email protected]). */
108
109 #include <openssl/bn.h>
110
111 #include <openssl/err.h>
112
113 #include "internal.h"
114
115
BN_mod_inverse_odd(BIGNUM * out,int * out_no_inverse,const BIGNUM * a,const BIGNUM * n,BN_CTX * ctx)116 int BN_mod_inverse_odd(BIGNUM *out, int *out_no_inverse, const BIGNUM *a,
117 const BIGNUM *n, BN_CTX *ctx) {
118 *out_no_inverse = 0;
119
120 if (!BN_is_odd(n)) {
121 OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
122 return 0;
123 }
124
125 if (BN_is_negative(a) || BN_cmp(a, n) >= 0) {
126 OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
127 return 0;
128 }
129
130 BIGNUM *A, *B, *X, *Y;
131 int ret = 0;
132 int sign;
133
134 BN_CTX_start(ctx);
135 A = BN_CTX_get(ctx);
136 B = BN_CTX_get(ctx);
137 X = BN_CTX_get(ctx);
138 Y = BN_CTX_get(ctx);
139 if (Y == NULL) {
140 goto err;
141 }
142
143 BIGNUM *R = out;
144
145 BN_zero(Y);
146 if (!BN_one(X) || BN_copy(B, a) == NULL || BN_copy(A, n) == NULL) {
147 goto err;
148 }
149 A->neg = 0;
150 sign = -1;
151 // From B = a mod |n|, A = |n| it follows that
152 //
153 // 0 <= B < A,
154 // -sign*X*a == B (mod |n|),
155 // sign*Y*a == A (mod |n|).
156
157 // Binary inversion algorithm; requires odd modulus. This is faster than the
158 // general algorithm if the modulus is sufficiently small (about 400 .. 500
159 // bits on 32-bit systems, but much more on 64-bit systems)
160 int shift;
161
162 while (!BN_is_zero(B)) {
163 // 0 < B < |n|,
164 // 0 < A <= |n|,
165 // (1) -sign*X*a == B (mod |n|),
166 // (2) sign*Y*a == A (mod |n|)
167
168 // Now divide B by the maximum possible power of two in the integers,
169 // and divide X by the same value mod |n|.
170 // When we're done, (1) still holds.
171 shift = 0;
172 while (!BN_is_bit_set(B, shift)) {
173 // note that 0 < B
174 shift++;
175
176 if (BN_is_odd(X)) {
177 if (!BN_uadd(X, X, n)) {
178 goto err;
179 }
180 }
181 // now X is even, so we can easily divide it by two
182 if (!BN_rshift1(X, X)) {
183 goto err;
184 }
185 }
186 if (shift > 0) {
187 if (!BN_rshift(B, B, shift)) {
188 goto err;
189 }
190 }
191
192 // Same for A and Y. Afterwards, (2) still holds.
193 shift = 0;
194 while (!BN_is_bit_set(A, shift)) {
195 // note that 0 < A
196 shift++;
197
198 if (BN_is_odd(Y)) {
199 if (!BN_uadd(Y, Y, n)) {
200 goto err;
201 }
202 }
203 // now Y is even
204 if (!BN_rshift1(Y, Y)) {
205 goto err;
206 }
207 }
208 if (shift > 0) {
209 if (!BN_rshift(A, A, shift)) {
210 goto err;
211 }
212 }
213
214 // We still have (1) and (2).
215 // Both A and B are odd.
216 // The following computations ensure that
217 //
218 // 0 <= B < |n|,
219 // 0 < A < |n|,
220 // (1) -sign*X*a == B (mod |n|),
221 // (2) sign*Y*a == A (mod |n|),
222 //
223 // and that either A or B is even in the next iteration.
224 if (BN_ucmp(B, A) >= 0) {
225 // -sign*(X + Y)*a == B - A (mod |n|)
226 if (!BN_uadd(X, X, Y)) {
227 goto err;
228 }
229 // NB: we could use BN_mod_add_quick(X, X, Y, n), but that
230 // actually makes the algorithm slower
231 if (!BN_usub(B, B, A)) {
232 goto err;
233 }
234 } else {
235 // sign*(X + Y)*a == A - B (mod |n|)
236 if (!BN_uadd(Y, Y, X)) {
237 goto err;
238 }
239 // as above, BN_mod_add_quick(Y, Y, X, n) would slow things down
240 if (!BN_usub(A, A, B)) {
241 goto err;
242 }
243 }
244 }
245
246 if (!BN_is_one(A)) {
247 *out_no_inverse = 1;
248 OPENSSL_PUT_ERROR(BN, BN_R_NO_INVERSE);
249 goto err;
250 }
251
252 // The while loop (Euclid's algorithm) ends when
253 // A == gcd(a,n);
254 // we have
255 // sign*Y*a == A (mod |n|),
256 // where Y is non-negative.
257
258 if (sign < 0) {
259 if (!BN_sub(Y, n, Y)) {
260 goto err;
261 }
262 }
263 // Now Y*a == A (mod |n|).
264
265 // Y*a == 1 (mod |n|)
266 if (Y->neg || BN_ucmp(Y, n) >= 0) {
267 if (!BN_nnmod(Y, Y, n, ctx)) {
268 goto err;
269 }
270 }
271 if (!BN_copy(R, Y)) {
272 goto err;
273 }
274
275 ret = 1;
276
277 err:
278 BN_CTX_end(ctx);
279 return ret;
280 }
281
BN_mod_inverse(BIGNUM * out,const BIGNUM * a,const BIGNUM * n,BN_CTX * ctx)282 BIGNUM *BN_mod_inverse(BIGNUM *out, const BIGNUM *a, const BIGNUM *n,
283 BN_CTX *ctx) {
284 BIGNUM *new_out = NULL;
285 if (out == NULL) {
286 new_out = BN_new();
287 if (new_out == NULL) {
288 return NULL;
289 }
290 out = new_out;
291 }
292
293 int ok = 0;
294 BIGNUM *a_reduced = NULL;
295 if (a->neg || BN_ucmp(a, n) >= 0) {
296 a_reduced = BN_dup(a);
297 if (a_reduced == NULL) {
298 goto err;
299 }
300 if (!BN_nnmod(a_reduced, a_reduced, n, ctx)) {
301 goto err;
302 }
303 a = a_reduced;
304 }
305
306 int no_inverse;
307 if (!BN_is_odd(n)) {
308 if (!bn_mod_inverse_consttime(out, &no_inverse, a, n, ctx)) {
309 goto err;
310 }
311 } else if (!BN_mod_inverse_odd(out, &no_inverse, a, n, ctx)) {
312 goto err;
313 }
314
315 ok = 1;
316
317 err:
318 if (!ok) {
319 BN_free(new_out);
320 out = NULL;
321 }
322 BN_free(a_reduced);
323 return out;
324 }
325
BN_mod_inverse_blinded(BIGNUM * out,int * out_no_inverse,const BIGNUM * a,const BN_MONT_CTX * mont,BN_CTX * ctx)326 int BN_mod_inverse_blinded(BIGNUM *out, int *out_no_inverse, const BIGNUM *a,
327 const BN_MONT_CTX *mont, BN_CTX *ctx) {
328 *out_no_inverse = 0;
329
330 // |a| is secret, but it is required to be in range, so these comparisons may
331 // be leaked.
332 if (BN_is_negative(a) ||
333 constant_time_declassify_int(BN_cmp(a, &mont->N) >= 0)) {
334 OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
335 return 0;
336 }
337
338 int ret = 0;
339 BIGNUM blinding_factor;
340 BN_init(&blinding_factor);
341
342 // |BN_mod_inverse_odd| is leaky, so generate a secret blinding factor and
343 // blind |a|. This works because (ar)^-1 * r = a^-1, supposing r is
344 // invertible. If r is not invertible, this function will fail. However, we
345 // only use this in RSA, where stumbling on an uninvertible element means
346 // stumbling on the key's factorization. That is, if this function fails, the
347 // RSA key was not actually a product of two large primes.
348 //
349 // TODO(crbug.com/boringssl/677): When the PRNG output is marked secret by
350 // default, the explicit |bn_secret| call can be removed.
351 if (!BN_rand_range_ex(&blinding_factor, 1, &mont->N)) {
352 goto err;
353 }
354 bn_secret(&blinding_factor);
355 if (!BN_mod_mul_montgomery(out, &blinding_factor, a, mont, ctx)) {
356 goto err;
357 }
358
359 // Once blinded, |out| is no longer secret, so it may be passed to a leaky
360 // mod inverse function. Note |blinding_factor| is secret, so |out| will be
361 // secret again after multiplying.
362 bn_declassify(out);
363 if (!BN_mod_inverse_odd(out, out_no_inverse, out, &mont->N, ctx) ||
364 !BN_mod_mul_montgomery(out, &blinding_factor, out, mont, ctx)) {
365 goto err;
366 }
367
368 ret = 1;
369
370 err:
371 BN_free(&blinding_factor);
372 return ret;
373 }
374
bn_mod_inverse_prime(BIGNUM * out,const BIGNUM * a,const BIGNUM * p,BN_CTX * ctx,const BN_MONT_CTX * mont_p)375 int bn_mod_inverse_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
376 BN_CTX *ctx, const BN_MONT_CTX *mont_p) {
377 BN_CTX_start(ctx);
378 BIGNUM *p_minus_2 = BN_CTX_get(ctx);
379 int ok = p_minus_2 != NULL &&
380 BN_copy(p_minus_2, p) &&
381 BN_sub_word(p_minus_2, 2) &&
382 BN_mod_exp_mont(out, a, p_minus_2, p, ctx, mont_p);
383 BN_CTX_end(ctx);
384 return ok;
385 }
386
bn_mod_inverse_secret_prime(BIGNUM * out,const BIGNUM * a,const BIGNUM * p,BN_CTX * ctx,const BN_MONT_CTX * mont_p)387 int bn_mod_inverse_secret_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
388 BN_CTX *ctx, const BN_MONT_CTX *mont_p) {
389 BN_CTX_start(ctx);
390 BIGNUM *p_minus_2 = BN_CTX_get(ctx);
391 int ok = p_minus_2 != NULL &&
392 BN_copy(p_minus_2, p) &&
393 BN_sub_word(p_minus_2, 2) &&
394 BN_mod_exp_mont_consttime(out, a, p_minus_2, p, ctx, mont_p);
395 BN_CTX_end(ctx);
396 return ok;
397 }
398