You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

629 regels
17 KiB

  1. /* 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. #include <openssl/bn.h>
  109. #include <assert.h>
  110. #include <openssl/err.h>
  111. #include "internal.h"
  112. static BIGNUM *euclid(BIGNUM *a, BIGNUM *b) {
  113. BIGNUM *t;
  114. int shifts = 0;
  115. /* 0 <= b <= a */
  116. while (!BN_is_zero(b)) {
  117. /* 0 < b <= a */
  118. if (BN_is_odd(a)) {
  119. if (BN_is_odd(b)) {
  120. if (!BN_sub(a, a, b)) {
  121. goto err;
  122. }
  123. if (!BN_rshift1(a, a)) {
  124. goto err;
  125. }
  126. if (BN_cmp(a, b) < 0) {
  127. t = a;
  128. a = b;
  129. b = t;
  130. }
  131. } else {
  132. /* a odd - b even */
  133. if (!BN_rshift1(b, b)) {
  134. goto err;
  135. }
  136. if (BN_cmp(a, b) < 0) {
  137. t = a;
  138. a = b;
  139. b = t;
  140. }
  141. }
  142. } else {
  143. /* a is even */
  144. if (BN_is_odd(b)) {
  145. if (!BN_rshift1(a, a)) {
  146. goto err;
  147. }
  148. if (BN_cmp(a, b) < 0) {
  149. t = a;
  150. a = b;
  151. b = t;
  152. }
  153. } else {
  154. /* a even - b even */
  155. if (!BN_rshift1(a, a)) {
  156. goto err;
  157. }
  158. if (!BN_rshift1(b, b)) {
  159. goto err;
  160. }
  161. shifts++;
  162. }
  163. }
  164. /* 0 <= b <= a */
  165. }
  166. if (shifts) {
  167. if (!BN_lshift(a, a, shifts)) {
  168. goto err;
  169. }
  170. }
  171. return a;
  172. err:
  173. return NULL;
  174. }
  175. int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx) {
  176. BIGNUM *a, *b, *t;
  177. int ret = 0;
  178. BN_CTX_start(ctx);
  179. a = BN_CTX_get(ctx);
  180. b = BN_CTX_get(ctx);
  181. if (a == NULL || b == NULL) {
  182. goto err;
  183. }
  184. if (BN_copy(a, in_a) == NULL) {
  185. goto err;
  186. }
  187. if (BN_copy(b, in_b) == NULL) {
  188. goto err;
  189. }
  190. a->neg = 0;
  191. b->neg = 0;
  192. if (BN_cmp(a, b) < 0) {
  193. t = a;
  194. a = b;
  195. b = t;
  196. }
  197. t = euclid(a, b);
  198. if (t == NULL) {
  199. goto err;
  200. }
  201. if (BN_copy(r, t) == NULL) {
  202. goto err;
  203. }
  204. ret = 1;
  205. err:
  206. BN_CTX_end(ctx);
  207. return ret;
  208. }
  209. /* solves ax == 1 (mod n) */
  210. static int bn_mod_inverse_general(BIGNUM *out, int *out_no_inverse,
  211. const BIGNUM *a, const BIGNUM *n,
  212. BN_CTX *ctx);
  213. int BN_mod_inverse_odd(BIGNUM *out, int *out_no_inverse, const BIGNUM *a,
  214. const BIGNUM *n, BN_CTX *ctx) {
  215. *out_no_inverse = 0;
  216. if (!BN_is_odd(n)) {
  217. OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
  218. return 0;
  219. }
  220. if (BN_is_negative(a) || BN_cmp(a, n) >= 0) {
  221. OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
  222. return 0;
  223. }
  224. BIGNUM *A, *B, *X, *Y;
  225. int ret = 0;
  226. int sign;
  227. BN_CTX_start(ctx);
  228. A = BN_CTX_get(ctx);
  229. B = BN_CTX_get(ctx);
  230. X = BN_CTX_get(ctx);
  231. Y = BN_CTX_get(ctx);
  232. if (Y == NULL) {
  233. goto err;
  234. }
  235. BIGNUM *R = out;
  236. BN_zero(Y);
  237. if (!BN_one(X) || BN_copy(B, a) == NULL || BN_copy(A, n) == NULL) {
  238. goto err;
  239. }
  240. A->neg = 0;
  241. sign = -1;
  242. /* From B = a mod |n|, A = |n| it follows that
  243. *
  244. * 0 <= B < A,
  245. * -sign*X*a == B (mod |n|),
  246. * sign*Y*a == A (mod |n|).
  247. */
  248. /* Binary inversion algorithm; requires odd modulus. This is faster than the
  249. * general algorithm if the modulus is sufficiently small (about 400 .. 500
  250. * bits on 32-bit systems, but much more on 64-bit systems) */
  251. int shift;
  252. while (!BN_is_zero(B)) {
  253. /* 0 < B < |n|,
  254. * 0 < A <= |n|,
  255. * (1) -sign*X*a == B (mod |n|),
  256. * (2) sign*Y*a == A (mod |n|) */
  257. /* Now divide B by the maximum possible power of two in the integers,
  258. * and divide X by the same value mod |n|.
  259. * When we're done, (1) still holds. */
  260. shift = 0;
  261. while (!BN_is_bit_set(B, shift)) {
  262. /* note that 0 < B */
  263. shift++;
  264. if (BN_is_odd(X)) {
  265. if (!BN_uadd(X, X, n)) {
  266. goto err;
  267. }
  268. }
  269. /* now X is even, so we can easily divide it by two */
  270. if (!BN_rshift1(X, X)) {
  271. goto err;
  272. }
  273. }
  274. if (shift > 0) {
  275. if (!BN_rshift(B, B, shift)) {
  276. goto err;
  277. }
  278. }
  279. /* Same for A and Y. Afterwards, (2) still holds. */
  280. shift = 0;
  281. while (!BN_is_bit_set(A, shift)) {
  282. /* note that 0 < A */
  283. shift++;
  284. if (BN_is_odd(Y)) {
  285. if (!BN_uadd(Y, Y, n)) {
  286. goto err;
  287. }
  288. }
  289. /* now Y is even */
  290. if (!BN_rshift1(Y, Y)) {
  291. goto err;
  292. }
  293. }
  294. if (shift > 0) {
  295. if (!BN_rshift(A, A, shift)) {
  296. goto err;
  297. }
  298. }
  299. /* We still have (1) and (2).
  300. * Both A and B are odd.
  301. * The following computations ensure that
  302. *
  303. * 0 <= B < |n|,
  304. * 0 < A < |n|,
  305. * (1) -sign*X*a == B (mod |n|),
  306. * (2) sign*Y*a == A (mod |n|),
  307. *
  308. * and that either A or B is even in the next iteration. */
  309. if (BN_ucmp(B, A) >= 0) {
  310. /* -sign*(X + Y)*a == B - A (mod |n|) */
  311. if (!BN_uadd(X, X, Y)) {
  312. goto err;
  313. }
  314. /* NB: we could use BN_mod_add_quick(X, X, Y, n), but that
  315. * actually makes the algorithm slower */
  316. if (!BN_usub(B, B, A)) {
  317. goto err;
  318. }
  319. } else {
  320. /* sign*(X + Y)*a == A - B (mod |n|) */
  321. if (!BN_uadd(Y, Y, X)) {
  322. goto err;
  323. }
  324. /* as above, BN_mod_add_quick(Y, Y, X, n) would slow things down */
  325. if (!BN_usub(A, A, B)) {
  326. goto err;
  327. }
  328. }
  329. }
  330. if (!BN_is_one(A)) {
  331. *out_no_inverse = 1;
  332. OPENSSL_PUT_ERROR(BN, BN_R_NO_INVERSE);
  333. goto err;
  334. }
  335. /* The while loop (Euclid's algorithm) ends when
  336. * A == gcd(a,n);
  337. * we have
  338. * sign*Y*a == A (mod |n|),
  339. * where Y is non-negative. */
  340. if (sign < 0) {
  341. if (!BN_sub(Y, n, Y)) {
  342. goto err;
  343. }
  344. }
  345. /* Now Y*a == A (mod |n|). */
  346. /* Y*a == 1 (mod |n|) */
  347. if (!Y->neg && BN_ucmp(Y, n) < 0) {
  348. if (!BN_copy(R, Y)) {
  349. goto err;
  350. }
  351. } else {
  352. if (!BN_nnmod(R, Y, n, ctx)) {
  353. goto err;
  354. }
  355. }
  356. ret = 1;
  357. err:
  358. BN_CTX_end(ctx);
  359. return ret;
  360. }
  361. BIGNUM *BN_mod_inverse(BIGNUM *out, const BIGNUM *a, const BIGNUM *n,
  362. BN_CTX *ctx) {
  363. int no_inverse;
  364. BIGNUM *a_reduced = NULL;
  365. BIGNUM *new_out = NULL;
  366. if (out == NULL) {
  367. new_out = BN_new();
  368. if (new_out == NULL) {
  369. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  370. return NULL;
  371. }
  372. out = new_out;
  373. }
  374. int ok = 0;
  375. int no_branch =
  376. (a->flags & BN_FLG_CONSTTIME) != 0 || (n->flags & BN_FLG_CONSTTIME) != 0;
  377. if (a->neg || BN_ucmp(a, n) >= 0) {
  378. a_reduced = BN_dup(a);
  379. if (a_reduced == NULL) {
  380. goto err;
  381. }
  382. if (no_branch) {
  383. BN_set_flags(a_reduced, BN_FLG_CONSTTIME);
  384. }
  385. if (!BN_nnmod(a_reduced, a_reduced, n, ctx)) {
  386. goto err;
  387. }
  388. a = a_reduced;
  389. }
  390. if (no_branch || !BN_is_odd(n)) {
  391. if (!bn_mod_inverse_general(out, &no_inverse, a, n, ctx)) {
  392. goto err;
  393. }
  394. } else if (!BN_mod_inverse_odd(out, &no_inverse, a, n, ctx)) {
  395. goto err;
  396. }
  397. ok = 1;
  398. err:
  399. if (!ok) {
  400. BN_free(new_out);
  401. out = NULL;
  402. }
  403. BN_free(a_reduced);
  404. return out;
  405. }
  406. int BN_mod_inverse_blinded(BIGNUM *out, int *out_no_inverse, const BIGNUM *a,
  407. const BN_MONT_CTX *mont, BN_CTX *ctx) {
  408. *out_no_inverse = 0;
  409. if (BN_is_negative(a) || BN_cmp(a, &mont->N) >= 0) {
  410. OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
  411. return 0;
  412. }
  413. int ret = 0;
  414. BIGNUM blinding_factor;
  415. BN_init(&blinding_factor);
  416. if (!BN_rand_range_ex(&blinding_factor, 1, &mont->N) ||
  417. !BN_mod_mul_montgomery(out, &blinding_factor, a, mont, ctx) ||
  418. !BN_mod_inverse_odd(out, out_no_inverse, out, &mont->N, ctx) ||
  419. !BN_mod_mul_montgomery(out, &blinding_factor, out, mont, ctx)) {
  420. OPENSSL_PUT_ERROR(BN, ERR_R_BN_LIB);
  421. goto err;
  422. }
  423. ret = 1;
  424. err:
  425. BN_free(&blinding_factor);
  426. return ret;
  427. }
  428. /* bn_mod_inverse_general is the general inversion algorithm that works for
  429. * both even and odd |n|. It was specifically designed to contain fewer
  430. * branches that may leak sensitive information. See "New Branch Prediction
  431. * Vulnerabilities in OpenSSL and Necessary Software Countermeasures" by
  432. * Onur Acıçmez, Shay Gueron, and Jean-Pierre Seifert. */
  433. static int bn_mod_inverse_general(BIGNUM *out, int *out_no_inverse,
  434. const BIGNUM *a, const BIGNUM *n,
  435. BN_CTX *ctx) {
  436. BIGNUM *A, *B, *X, *Y, *M, *D, *T;
  437. BIGNUM local_A;
  438. BIGNUM *pA;
  439. int ret = 0;
  440. int sign;
  441. *out_no_inverse = 0;
  442. BN_CTX_start(ctx);
  443. A = BN_CTX_get(ctx);
  444. B = BN_CTX_get(ctx);
  445. X = BN_CTX_get(ctx);
  446. D = BN_CTX_get(ctx);
  447. M = BN_CTX_get(ctx);
  448. Y = BN_CTX_get(ctx);
  449. T = BN_CTX_get(ctx);
  450. if (T == NULL) {
  451. goto err;
  452. }
  453. BIGNUM *R = out;
  454. BN_zero(Y);
  455. if (!BN_one(X) || BN_copy(B, a) == NULL || BN_copy(A, n) == NULL) {
  456. goto err;
  457. }
  458. A->neg = 0;
  459. sign = -1;
  460. /* From B = a mod |n|, A = |n| it follows that
  461. *
  462. * 0 <= B < A,
  463. * -sign*X*a == B (mod |n|),
  464. * sign*Y*a == A (mod |n|).
  465. */
  466. while (!BN_is_zero(B)) {
  467. BIGNUM *tmp;
  468. /*
  469. * 0 < B < A,
  470. * (*) -sign*X*a == B (mod |n|),
  471. * sign*Y*a == A (mod |n|)
  472. */
  473. /* Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
  474. * BN_div_no_branch will be called eventually.
  475. */
  476. pA = &local_A;
  477. BN_with_flags(pA, A, BN_FLG_CONSTTIME);
  478. /* (D, M) := (A/B, A%B) ... */
  479. if (!BN_div(D, M, pA, B, ctx)) {
  480. goto err;
  481. }
  482. /* Now
  483. * A = D*B + M;
  484. * thus we have
  485. * (**) sign*Y*a == D*B + M (mod |n|).
  486. */
  487. tmp = A; /* keep the BIGNUM object, the value does not matter */
  488. /* (A, B) := (B, A mod B) ... */
  489. A = B;
  490. B = M;
  491. /* ... so we have 0 <= B < A again */
  492. /* Since the former M is now B and the former B is now A,
  493. * (**) translates into
  494. * sign*Y*a == D*A + B (mod |n|),
  495. * i.e.
  496. * sign*Y*a - D*A == B (mod |n|).
  497. * Similarly, (*) translates into
  498. * -sign*X*a == A (mod |n|).
  499. *
  500. * Thus,
  501. * sign*Y*a + D*sign*X*a == B (mod |n|),
  502. * i.e.
  503. * sign*(Y + D*X)*a == B (mod |n|).
  504. *
  505. * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
  506. * -sign*X*a == B (mod |n|),
  507. * sign*Y*a == A (mod |n|).
  508. * Note that X and Y stay non-negative all the time.
  509. */
  510. if (!BN_mul(tmp, D, X, ctx)) {
  511. goto err;
  512. }
  513. if (!BN_add(tmp, tmp, Y)) {
  514. goto err;
  515. }
  516. M = Y; /* keep the BIGNUM object, the value does not matter */
  517. Y = X;
  518. X = tmp;
  519. sign = -sign;
  520. }
  521. if (!BN_is_one(A)) {
  522. *out_no_inverse = 1;
  523. OPENSSL_PUT_ERROR(BN, BN_R_NO_INVERSE);
  524. goto err;
  525. }
  526. /*
  527. * The while loop (Euclid's algorithm) ends when
  528. * A == gcd(a,n);
  529. * we have
  530. * sign*Y*a == A (mod |n|),
  531. * where Y is non-negative.
  532. */
  533. if (sign < 0) {
  534. if (!BN_sub(Y, n, Y)) {
  535. goto err;
  536. }
  537. }
  538. /* Now Y*a == A (mod |n|). */
  539. /* Y*a == 1 (mod |n|) */
  540. if (!Y->neg && BN_ucmp(Y, n) < 0) {
  541. if (!BN_copy(R, Y)) {
  542. goto err;
  543. }
  544. } else {
  545. if (!BN_nnmod(R, Y, n, ctx)) {
  546. goto err;
  547. }
  548. }
  549. ret = 1;
  550. err:
  551. BN_CTX_end(ctx);
  552. return ret;
  553. }