bn_gcd.c 17 KB

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