bn_gcd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/cryptlib.h"
  10. #include "bn_lcl.h"
  11. static BIGNUM *euclid(BIGNUM *a, BIGNUM *b);
  12. int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
  13. {
  14. BIGNUM *a, *b, *t;
  15. int ret = 0;
  16. bn_check_top(in_a);
  17. bn_check_top(in_b);
  18. BN_CTX_start(ctx);
  19. a = BN_CTX_get(ctx);
  20. b = BN_CTX_get(ctx);
  21. if (b == NULL)
  22. goto err;
  23. if (BN_copy(a, in_a) == NULL)
  24. goto err;
  25. if (BN_copy(b, in_b) == NULL)
  26. goto err;
  27. a->neg = 0;
  28. b->neg = 0;
  29. if (BN_cmp(a, b) < 0) {
  30. t = a;
  31. a = b;
  32. b = t;
  33. }
  34. t = euclid(a, b);
  35. if (t == NULL)
  36. goto err;
  37. if (BN_copy(r, t) == NULL)
  38. goto err;
  39. ret = 1;
  40. err:
  41. BN_CTX_end(ctx);
  42. bn_check_top(r);
  43. return ret;
  44. }
  45. static BIGNUM *euclid(BIGNUM *a, BIGNUM *b)
  46. {
  47. BIGNUM *t;
  48. int shifts = 0;
  49. bn_check_top(a);
  50. bn_check_top(b);
  51. /* 0 <= b <= a */
  52. while (!BN_is_zero(b)) {
  53. /* 0 < b <= a */
  54. if (BN_is_odd(a)) {
  55. if (BN_is_odd(b)) {
  56. if (!BN_sub(a, a, b))
  57. goto err;
  58. if (!BN_rshift1(a, a))
  59. goto err;
  60. if (BN_cmp(a, b) < 0) {
  61. t = a;
  62. a = b;
  63. b = t;
  64. }
  65. } else { /* a odd - b even */
  66. if (!BN_rshift1(b, b))
  67. goto err;
  68. if (BN_cmp(a, b) < 0) {
  69. t = a;
  70. a = b;
  71. b = t;
  72. }
  73. }
  74. } else { /* a is even */
  75. if (BN_is_odd(b)) {
  76. if (!BN_rshift1(a, a))
  77. goto err;
  78. if (BN_cmp(a, b) < 0) {
  79. t = a;
  80. a = b;
  81. b = t;
  82. }
  83. } else { /* a even - b even */
  84. if (!BN_rshift1(a, a))
  85. goto err;
  86. if (!BN_rshift1(b, b))
  87. goto err;
  88. shifts++;
  89. }
  90. }
  91. /* 0 <= b <= a */
  92. }
  93. if (shifts) {
  94. if (!BN_lshift(a, a, shifts))
  95. goto err;
  96. }
  97. bn_check_top(a);
  98. return a;
  99. err:
  100. return NULL;
  101. }
  102. /* solves ax == 1 (mod n) */
  103. static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
  104. const BIGNUM *a, const BIGNUM *n,
  105. BN_CTX *ctx);
  106. BIGNUM *BN_mod_inverse(BIGNUM *in,
  107. const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
  108. {
  109. BIGNUM *rv;
  110. int noinv;
  111. rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);
  112. if (noinv)
  113. BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);
  114. return rv;
  115. }
  116. BIGNUM *int_bn_mod_inverse(BIGNUM *in,
  117. const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
  118. int *pnoinv)
  119. {
  120. BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;
  121. BIGNUM *ret = NULL;
  122. int sign;
  123. /* This is invalid input so we don't worry about constant time here */
  124. if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {
  125. if (pnoinv != NULL)
  126. *pnoinv = 1;
  127. return NULL;
  128. }
  129. if (pnoinv != NULL)
  130. *pnoinv = 0;
  131. if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)
  132. || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {
  133. return BN_mod_inverse_no_branch(in, a, n, ctx);
  134. }
  135. bn_check_top(a);
  136. bn_check_top(n);
  137. BN_CTX_start(ctx);
  138. A = BN_CTX_get(ctx);
  139. B = BN_CTX_get(ctx);
  140. X = BN_CTX_get(ctx);
  141. D = BN_CTX_get(ctx);
  142. M = BN_CTX_get(ctx);
  143. Y = BN_CTX_get(ctx);
  144. T = BN_CTX_get(ctx);
  145. if (T == NULL)
  146. goto err;
  147. if (in == NULL)
  148. R = BN_new();
  149. else
  150. R = in;
  151. if (R == NULL)
  152. goto err;
  153. BN_one(X);
  154. BN_zero(Y);
  155. if (BN_copy(B, a) == NULL)
  156. goto err;
  157. if (BN_copy(A, n) == NULL)
  158. goto err;
  159. A->neg = 0;
  160. if (B->neg || (BN_ucmp(B, A) >= 0)) {
  161. if (!BN_nnmod(B, B, A, ctx))
  162. goto err;
  163. }
  164. sign = -1;
  165. /*-
  166. * From B = a mod |n|, A = |n| it follows that
  167. *
  168. * 0 <= B < A,
  169. * -sign*X*a == B (mod |n|),
  170. * sign*Y*a == A (mod |n|).
  171. */
  172. if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {
  173. /*
  174. * Binary inversion algorithm; requires odd modulus. This is faster
  175. * than the general algorithm if the modulus is sufficiently small
  176. * (about 400 .. 500 bits on 32-bit systems, but much more on 64-bit
  177. * systems)
  178. */
  179. int shift;
  180. while (!BN_is_zero(B)) {
  181. /*-
  182. * 0 < B < |n|,
  183. * 0 < A <= |n|,
  184. * (1) -sign*X*a == B (mod |n|),
  185. * (2) sign*Y*a == A (mod |n|)
  186. */
  187. /*
  188. * Now divide B by the maximum possible power of two in the
  189. * integers, and divide X by the same value mod |n|. When we're
  190. * done, (1) still holds.
  191. */
  192. shift = 0;
  193. while (!BN_is_bit_set(B, shift)) { /* note that 0 < B */
  194. shift++;
  195. if (BN_is_odd(X)) {
  196. if (!BN_uadd(X, X, n))
  197. goto err;
  198. }
  199. /*
  200. * now X is even, so we can easily divide it by two
  201. */
  202. if (!BN_rshift1(X, X))
  203. goto err;
  204. }
  205. if (shift > 0) {
  206. if (!BN_rshift(B, B, shift))
  207. goto err;
  208. }
  209. /*
  210. * Same for A and Y. Afterwards, (2) still holds.
  211. */
  212. shift = 0;
  213. while (!BN_is_bit_set(A, shift)) { /* note that 0 < A */
  214. shift++;
  215. if (BN_is_odd(Y)) {
  216. if (!BN_uadd(Y, Y, n))
  217. goto err;
  218. }
  219. /* now Y is even */
  220. if (!BN_rshift1(Y, Y))
  221. goto err;
  222. }
  223. if (shift > 0) {
  224. if (!BN_rshift(A, A, shift))
  225. goto err;
  226. }
  227. /*-
  228. * We still have (1) and (2).
  229. * Both A and B are odd.
  230. * The following computations ensure that
  231. *
  232. * 0 <= B < |n|,
  233. * 0 < A < |n|,
  234. * (1) -sign*X*a == B (mod |n|),
  235. * (2) sign*Y*a == A (mod |n|),
  236. *
  237. * and that either A or B is even in the next iteration.
  238. */
  239. if (BN_ucmp(B, A) >= 0) {
  240. /* -sign*(X + Y)*a == B - A (mod |n|) */
  241. if (!BN_uadd(X, X, Y))
  242. goto err;
  243. /*
  244. * NB: we could use BN_mod_add_quick(X, X, Y, n), but that
  245. * actually makes the algorithm slower
  246. */
  247. if (!BN_usub(B, B, A))
  248. goto err;
  249. } else {
  250. /* sign*(X + Y)*a == A - B (mod |n|) */
  251. if (!BN_uadd(Y, Y, X))
  252. goto err;
  253. /*
  254. * as above, BN_mod_add_quick(Y, Y, X, n) would slow things down
  255. */
  256. if (!BN_usub(A, A, B))
  257. goto err;
  258. }
  259. }
  260. } else {
  261. /* general inversion algorithm */
  262. while (!BN_is_zero(B)) {
  263. BIGNUM *tmp;
  264. /*-
  265. * 0 < B < A,
  266. * (*) -sign*X*a == B (mod |n|),
  267. * sign*Y*a == A (mod |n|)
  268. */
  269. /* (D, M) := (A/B, A%B) ... */
  270. if (BN_num_bits(A) == BN_num_bits(B)) {
  271. if (!BN_one(D))
  272. goto err;
  273. if (!BN_sub(M, A, B))
  274. goto err;
  275. } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {
  276. /* A/B is 1, 2, or 3 */
  277. if (!BN_lshift1(T, B))
  278. goto err;
  279. if (BN_ucmp(A, T) < 0) {
  280. /* A < 2*B, so D=1 */
  281. if (!BN_one(D))
  282. goto err;
  283. if (!BN_sub(M, A, B))
  284. goto err;
  285. } else {
  286. /* A >= 2*B, so D=2 or D=3 */
  287. if (!BN_sub(M, A, T))
  288. goto err;
  289. if (!BN_add(D, T, B))
  290. goto err; /* use D (:= 3*B) as temp */
  291. if (BN_ucmp(A, D) < 0) {
  292. /* A < 3*B, so D=2 */
  293. if (!BN_set_word(D, 2))
  294. goto err;
  295. /*
  296. * M (= A - 2*B) already has the correct value
  297. */
  298. } else {
  299. /* only D=3 remains */
  300. if (!BN_set_word(D, 3))
  301. goto err;
  302. /*
  303. * currently M = A - 2*B, but we need M = A - 3*B
  304. */
  305. if (!BN_sub(M, M, B))
  306. goto err;
  307. }
  308. }
  309. } else {
  310. if (!BN_div(D, M, A, B, ctx))
  311. goto err;
  312. }
  313. /*-
  314. * Now
  315. * A = D*B + M;
  316. * thus we have
  317. * (**) sign*Y*a == D*B + M (mod |n|).
  318. */
  319. tmp = A; /* keep the BIGNUM object, the value does not matter */
  320. /* (A, B) := (B, A mod B) ... */
  321. A = B;
  322. B = M;
  323. /* ... so we have 0 <= B < A again */
  324. /*-
  325. * Since the former M is now B and the former B is now A,
  326. * (**) translates into
  327. * sign*Y*a == D*A + B (mod |n|),
  328. * i.e.
  329. * sign*Y*a - D*A == B (mod |n|).
  330. * Similarly, (*) translates into
  331. * -sign*X*a == A (mod |n|).
  332. *
  333. * Thus,
  334. * sign*Y*a + D*sign*X*a == B (mod |n|),
  335. * i.e.
  336. * sign*(Y + D*X)*a == B (mod |n|).
  337. *
  338. * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
  339. * -sign*X*a == B (mod |n|),
  340. * sign*Y*a == A (mod |n|).
  341. * Note that X and Y stay non-negative all the time.
  342. */
  343. /*
  344. * most of the time D is very small, so we can optimize tmp := D*X+Y
  345. */
  346. if (BN_is_one(D)) {
  347. if (!BN_add(tmp, X, Y))
  348. goto err;
  349. } else {
  350. if (BN_is_word(D, 2)) {
  351. if (!BN_lshift1(tmp, X))
  352. goto err;
  353. } else if (BN_is_word(D, 4)) {
  354. if (!BN_lshift(tmp, X, 2))
  355. goto err;
  356. } else if (D->top == 1) {
  357. if (!BN_copy(tmp, X))
  358. goto err;
  359. if (!BN_mul_word(tmp, D->d[0]))
  360. goto err;
  361. } else {
  362. if (!BN_mul(tmp, D, X, ctx))
  363. goto err;
  364. }
  365. if (!BN_add(tmp, tmp, Y))
  366. goto err;
  367. }
  368. M = Y; /* keep the BIGNUM object, the value does not matter */
  369. Y = X;
  370. X = tmp;
  371. sign = -sign;
  372. }
  373. }
  374. /*-
  375. * The while loop (Euclid's algorithm) ends when
  376. * A == gcd(a,n);
  377. * we have
  378. * sign*Y*a == A (mod |n|),
  379. * where Y is non-negative.
  380. */
  381. if (sign < 0) {
  382. if (!BN_sub(Y, n, Y))
  383. goto err;
  384. }
  385. /* Now Y*a == A (mod |n|). */
  386. if (BN_is_one(A)) {
  387. /* Y*a == 1 (mod |n|) */
  388. if (!Y->neg && BN_ucmp(Y, n) < 0) {
  389. if (!BN_copy(R, Y))
  390. goto err;
  391. } else {
  392. if (!BN_nnmod(R, Y, n, ctx))
  393. goto err;
  394. }
  395. } else {
  396. if (pnoinv)
  397. *pnoinv = 1;
  398. goto err;
  399. }
  400. ret = R;
  401. err:
  402. if ((ret == NULL) && (in == NULL))
  403. BN_free(R);
  404. BN_CTX_end(ctx);
  405. bn_check_top(ret);
  406. return ret;
  407. }
  408. /*
  409. * BN_mod_inverse_no_branch is a special version of BN_mod_inverse. It does
  410. * not contain branches that may leak sensitive information.
  411. */
  412. static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
  413. const BIGNUM *a, const BIGNUM *n,
  414. BN_CTX *ctx)
  415. {
  416. BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;
  417. BIGNUM *ret = NULL;
  418. int sign;
  419. bn_check_top(a);
  420. bn_check_top(n);
  421. BN_CTX_start(ctx);
  422. A = BN_CTX_get(ctx);
  423. B = BN_CTX_get(ctx);
  424. X = BN_CTX_get(ctx);
  425. D = BN_CTX_get(ctx);
  426. M = BN_CTX_get(ctx);
  427. Y = BN_CTX_get(ctx);
  428. T = BN_CTX_get(ctx);
  429. if (T == NULL)
  430. goto err;
  431. if (in == NULL)
  432. R = BN_new();
  433. else
  434. R = in;
  435. if (R == NULL)
  436. goto err;
  437. BN_one(X);
  438. BN_zero(Y);
  439. if (BN_copy(B, a) == NULL)
  440. goto err;
  441. if (BN_copy(A, n) == NULL)
  442. goto err;
  443. A->neg = 0;
  444. if (B->neg || (BN_ucmp(B, A) >= 0)) {
  445. /*
  446. * Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
  447. * BN_div_no_branch will be called eventually.
  448. */
  449. {
  450. BIGNUM local_B;
  451. bn_init(&local_B);
  452. BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);
  453. if (!BN_nnmod(B, &local_B, A, ctx))
  454. goto err;
  455. /* Ensure local_B goes out of scope before any further use of B */
  456. }
  457. }
  458. sign = -1;
  459. /*-
  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. /*
  474. * Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
  475. * BN_div_no_branch will be called eventually.
  476. */
  477. {
  478. BIGNUM local_A;
  479. bn_init(&local_A);
  480. BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);
  481. /* (D, M) := (A/B, A%B) ... */
  482. if (!BN_div(D, M, &local_A, B, ctx))
  483. goto err;
  484. /* Ensure local_A goes out of scope before any further use of A */
  485. }
  486. /*-
  487. * Now
  488. * A = D*B + M;
  489. * thus we have
  490. * (**) sign*Y*a == D*B + M (mod |n|).
  491. */
  492. tmp = A; /* keep the BIGNUM object, the value does not
  493. * matter */
  494. /* (A, B) := (B, A mod B) ... */
  495. A = B;
  496. B = M;
  497. /* ... so we have 0 <= B < A again */
  498. /*-
  499. * Since the former M is now B and the former B is now A,
  500. * (**) translates into
  501. * sign*Y*a == D*A + B (mod |n|),
  502. * i.e.
  503. * sign*Y*a - D*A == B (mod |n|).
  504. * Similarly, (*) translates into
  505. * -sign*X*a == A (mod |n|).
  506. *
  507. * Thus,
  508. * sign*Y*a + D*sign*X*a == B (mod |n|),
  509. * i.e.
  510. * sign*(Y + D*X)*a == B (mod |n|).
  511. *
  512. * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
  513. * -sign*X*a == B (mod |n|),
  514. * sign*Y*a == A (mod |n|).
  515. * Note that X and Y stay non-negative all the time.
  516. */
  517. if (!BN_mul(tmp, D, X, ctx))
  518. goto err;
  519. if (!BN_add(tmp, tmp, Y))
  520. goto err;
  521. M = Y; /* keep the BIGNUM object, the value does not
  522. * matter */
  523. Y = X;
  524. X = tmp;
  525. sign = -sign;
  526. }
  527. /*-
  528. * The while loop (Euclid's algorithm) ends when
  529. * A == gcd(a,n);
  530. * we have
  531. * sign*Y*a == A (mod |n|),
  532. * where Y is non-negative.
  533. */
  534. if (sign < 0) {
  535. if (!BN_sub(Y, n, Y))
  536. goto err;
  537. }
  538. /* Now Y*a == A (mod |n|). */
  539. if (BN_is_one(A)) {
  540. /* Y*a == 1 (mod |n|) */
  541. if (!Y->neg && BN_ucmp(Y, n) < 0) {
  542. if (!BN_copy(R, Y))
  543. goto err;
  544. } else {
  545. if (!BN_nnmod(R, Y, n, ctx))
  546. goto err;
  547. }
  548. } else {
  549. BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);
  550. goto err;
  551. }
  552. ret = R;
  553. err:
  554. if ((ret == NULL) && (in == NULL))
  555. BN_free(R);
  556. BN_CTX_end(ctx);
  557. bn_check_top(ret);
  558. return ret;
  559. }