blob: bd0fa6f5de0ab9dc7f582da1dc0065cff925cc8c [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
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.
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 (tjh@cryptsoft.com).
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 (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)"
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 * openssl-core@openssl.org.
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 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
109#include <openssl/bn.h>
110
111#include <openssl/err.h>
112
113#include "internal.h"
114
Adam Langleyd9e397b2015-01-22 14:27:53 -0800115
David Benjaminc895d6b2016-08-11 13:26:41 -0400116int 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;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800119
David Benjaminc895d6b2016-08-11 13:26:41 -0400120 if (!BN_is_odd(n)) {
121 OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
122 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800123 }
124
David Benjaminc895d6b2016-08-11 13:26:41 -0400125 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;
Kenny Rootb8494592015-09-25 02:29:14 +0000133
Adam Langleyd9e397b2015-01-22 14:27:53 -0800134 BN_CTX_start(ctx);
135 A = BN_CTX_get(ctx);
136 B = BN_CTX_get(ctx);
137 X = BN_CTX_get(ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800138 Y = BN_CTX_get(ctx);
David Benjaminc895d6b2016-08-11 13:26:41 -0400139 if (Y == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800140 goto err;
141 }
142
David Benjaminc895d6b2016-08-11 13:26:41 -0400143 BIGNUM *R = out;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800144
Adam Langleyd9e397b2015-01-22 14:27:53 -0800145 BN_zero(Y);
Adam Langleye9ada862015-05-11 17:20:37 -0700146 if (!BN_one(X) || BN_copy(B, a) == NULL || BN_copy(A, n) == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800147 goto err;
148 }
149 A->neg = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800150 sign = -1;
Robert Sloan8f860b12017-08-28 07:37:06 -0700151 // 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|).
Adam Langleyd9e397b2015-01-22 14:27:53 -0800156
Robert Sloan8f860b12017-08-28 07:37:06 -0700157 // 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)
David Benjaminc895d6b2016-08-11 13:26:41 -0400160 int shift;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800161
David Benjaminc895d6b2016-08-11 13:26:41 -0400162 while (!BN_is_zero(B)) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700163 // 0 < B < |n|,
164 // 0 < A <= |n|,
165 // (1) -sign*X*a == B (mod |n|),
166 // (2) sign*Y*a == A (mod |n|)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800167
Robert Sloan8f860b12017-08-28 07:37:06 -0700168 // 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.
David Benjaminc895d6b2016-08-11 13:26:41 -0400171 shift = 0;
172 while (!BN_is_bit_set(B, shift)) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700173 // note that 0 < B
David Benjaminc895d6b2016-08-11 13:26:41 -0400174 shift++;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800175
David Benjaminc895d6b2016-08-11 13:26:41 -0400176 if (BN_is_odd(X)) {
177 if (!BN_uadd(X, X, n)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800178 goto err;
179 }
180 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700181 // now X is even, so we can easily divide it by two
David Benjaminc895d6b2016-08-11 13:26:41 -0400182 if (!BN_rshift1(X, X)) {
183 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800184 }
185 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400186 if (shift > 0) {
187 if (!BN_rshift(B, B, shift)) {
188 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800189 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800190 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400191
Robert Sloan8f860b12017-08-28 07:37:06 -0700192 // Same for A and Y. Afterwards, (2) still holds.
David Benjaminc895d6b2016-08-11 13:26:41 -0400193 shift = 0;
194 while (!BN_is_bit_set(A, shift)) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700195 // note that 0 < A
David Benjaminc895d6b2016-08-11 13:26:41 -0400196 shift++;
197
198 if (BN_is_odd(Y)) {
199 if (!BN_uadd(Y, Y, n)) {
200 goto err;
201 }
202 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700203 // now Y is even
David Benjaminc895d6b2016-08-11 13:26:41 -0400204 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
Robert Sloan8f860b12017-08-28 07:37:06 -0700214 // 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.
David Benjaminc895d6b2016-08-11 13:26:41 -0400224 if (BN_ucmp(B, A) >= 0) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700225 // -sign*(X + Y)*a == B - A (mod |n|)
David Benjaminc895d6b2016-08-11 13:26:41 -0400226 if (!BN_uadd(X, X, Y)) {
227 goto err;
228 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700229 // NB: we could use BN_mod_add_quick(X, X, Y, n), but that
230 // actually makes the algorithm slower
David Benjaminc895d6b2016-08-11 13:26:41 -0400231 if (!BN_usub(B, B, A)) {
232 goto err;
233 }
234 } else {
Robert Sloan8f860b12017-08-28 07:37:06 -0700235 // sign*(X + Y)*a == A - B (mod |n|)
David Benjaminc895d6b2016-08-11 13:26:41 -0400236 if (!BN_uadd(Y, Y, X)) {
237 goto err;
238 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700239 // as above, BN_mod_add_quick(Y, Y, X, n) would slow things down
David Benjaminc895d6b2016-08-11 13:26:41 -0400240 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;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800250 }
251
Robert Sloan8f860b12017-08-28 07:37:06 -0700252 // 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.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800257
258 if (sign < 0) {
259 if (!BN_sub(Y, n, Y)) {
260 goto err;
261 }
262 }
Robert Sloan8f860b12017-08-28 07:37:06 -0700263 // Now Y*a == A (mod |n|).
Adam Langleyd9e397b2015-01-22 14:27:53 -0800264
Robert Sloan8f860b12017-08-28 07:37:06 -0700265 // Y*a == 1 (mod |n|)
David Benjaminc895d6b2016-08-11 13:26:41 -0400266 if (!Y->neg && BN_ucmp(Y, n) < 0) {
267 if (!BN_copy(R, Y)) {
268 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800269 }
270 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -0400271 if (!BN_nnmod(R, Y, n, ctx)) {
272 goto err;
273 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800274 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400275
276 ret = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800277
278err:
Adam Langleyd9e397b2015-01-22 14:27:53 -0800279 BN_CTX_end(ctx);
280 return ret;
281}
282
Kenny Rootb8494592015-09-25 02:29:14 +0000283BIGNUM *BN_mod_inverse(BIGNUM *out, const BIGNUM *a, const BIGNUM *n,
284 BN_CTX *ctx) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400285 BIGNUM *new_out = NULL;
286 if (out == NULL) {
287 new_out = BN_new();
288 if (new_out == NULL) {
289 OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
290 return NULL;
291 }
292 out = new_out;
293 }
294
295 int ok = 0;
Steven Valdezb0b45c62017-01-17 16:23:54 -0500296 BIGNUM *a_reduced = NULL;
David Benjaminc895d6b2016-08-11 13:26:41 -0400297 if (a->neg || BN_ucmp(a, n) >= 0) {
298 a_reduced = BN_dup(a);
299 if (a_reduced == NULL) {
300 goto err;
301 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400302 if (!BN_nnmod(a_reduced, a_reduced, n, ctx)) {
303 goto err;
304 }
305 a = a_reduced;
306 }
307
Steven Valdezb0b45c62017-01-17 16:23:54 -0500308 int no_inverse;
309 if (!BN_is_odd(n)) {
Robert Sloan49d063b2018-04-03 11:30:38 -0700310 if (!bn_mod_inverse_consttime(out, &no_inverse, a, n, ctx)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400311 goto err;
312 }
313 } else if (!BN_mod_inverse_odd(out, &no_inverse, a, n, ctx)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400314 goto err;
315 }
316
317 ok = 1;
318
319err:
320 if (!ok) {
321 BN_free(new_out);
322 out = NULL;
323 }
324 BN_free(a_reduced);
325 return out;
Kenny Rootb8494592015-09-25 02:29:14 +0000326}
327
David Benjaminc895d6b2016-08-11 13:26:41 -0400328int BN_mod_inverse_blinded(BIGNUM *out, int *out_no_inverse, const BIGNUM *a,
329 const BN_MONT_CTX *mont, BN_CTX *ctx) {
330 *out_no_inverse = 0;
331
332 if (BN_is_negative(a) || BN_cmp(a, &mont->N) >= 0) {
333 OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
334 return 0;
335 }
336
337 int ret = 0;
338 BIGNUM blinding_factor;
339 BN_init(&blinding_factor);
340
341 if (!BN_rand_range_ex(&blinding_factor, 1, &mont->N) ||
342 !BN_mod_mul_montgomery(out, &blinding_factor, a, mont, ctx) ||
343 !BN_mod_inverse_odd(out, out_no_inverse, out, &mont->N, ctx) ||
344 !BN_mod_mul_montgomery(out, &blinding_factor, out, mont, ctx)) {
345 OPENSSL_PUT_ERROR(BN, ERR_R_BN_LIB);
346 goto err;
347 }
348
349 ret = 1;
350
351err:
352 BN_free(&blinding_factor);
353 return ret;
354}
355
Robert Sloan69939df2017-01-09 10:53:07 -0800356int bn_mod_inverse_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
357 BN_CTX *ctx, const BN_MONT_CTX *mont_p) {
358 BN_CTX_start(ctx);
359 BIGNUM *p_minus_2 = BN_CTX_get(ctx);
360 int ok = p_minus_2 != NULL &&
361 BN_copy(p_minus_2, p) &&
362 BN_sub_word(p_minus_2, 2) &&
363 BN_mod_exp_mont(out, a, p_minus_2, p, ctx, mont_p);
364 BN_CTX_end(ctx);
365 return ok;
366}
367
368int bn_mod_inverse_secret_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
369 BN_CTX *ctx, const BN_MONT_CTX *mont_p) {
370 BN_CTX_start(ctx);
371 BIGNUM *p_minus_2 = BN_CTX_get(ctx);
372 int ok = p_minus_2 != NULL &&
373 BN_copy(p_minus_2, p) &&
374 BN_sub_word(p_minus_2, 2) &&
375 BN_mod_exp_mont_consttime(out, a, p_minus_2, p, ctx, mont_p);
376 BN_CTX_end(ctx);
377 return ok;
378}