div.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. #include <openssl/bn.h>
  57. #include <assert.h>
  58. #include <limits.h>
  59. #include <openssl/err.h>
  60. #include "internal.h"
  61. #if !defined(BN_ULLONG)
  62. // bn_div_words divides a double-width |h|,|l| by |d| and returns the result,
  63. // which must fit in a |BN_ULONG|.
  64. static BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d) {
  65. BN_ULONG dh, dl, q, ret = 0, th, tl, t;
  66. int i, count = 2;
  67. if (d == 0) {
  68. return BN_MASK2;
  69. }
  70. i = BN_num_bits_word(d);
  71. assert((i == BN_BITS2) || (h <= (BN_ULONG)1 << i));
  72. i = BN_BITS2 - i;
  73. if (h >= d) {
  74. h -= d;
  75. }
  76. if (i) {
  77. d <<= i;
  78. h = (h << i) | (l >> (BN_BITS2 - i));
  79. l <<= i;
  80. }
  81. dh = (d & BN_MASK2h) >> BN_BITS4;
  82. dl = (d & BN_MASK2l);
  83. for (;;) {
  84. if ((h >> BN_BITS4) == dh) {
  85. q = BN_MASK2l;
  86. } else {
  87. q = h / dh;
  88. }
  89. th = q * dh;
  90. tl = dl * q;
  91. for (;;) {
  92. t = h - th;
  93. if ((t & BN_MASK2h) ||
  94. ((tl) <= ((t << BN_BITS4) | ((l & BN_MASK2h) >> BN_BITS4)))) {
  95. break;
  96. }
  97. q--;
  98. th -= dh;
  99. tl -= dl;
  100. }
  101. t = (tl >> BN_BITS4);
  102. tl = (tl << BN_BITS4) & BN_MASK2h;
  103. th += t;
  104. if (l < tl) {
  105. th++;
  106. }
  107. l -= tl;
  108. if (h < th) {
  109. h += d;
  110. q--;
  111. }
  112. h -= th;
  113. if (--count == 0) {
  114. break;
  115. }
  116. ret = q << BN_BITS4;
  117. h = (h << BN_BITS4) | (l >> BN_BITS4);
  118. l = (l & BN_MASK2l) << BN_BITS4;
  119. }
  120. ret |= q;
  121. return ret;
  122. }
  123. #endif // !defined(BN_ULLONG)
  124. static inline void bn_div_rem_words(BN_ULONG *quotient_out, BN_ULONG *rem_out,
  125. BN_ULONG n0, BN_ULONG n1, BN_ULONG d0) {
  126. // GCC and Clang generate function calls to |__udivdi3| and |__umoddi3| when
  127. // the |BN_ULLONG|-based C code is used.
  128. //
  129. // GCC bugs:
  130. // * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14224
  131. // * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43721
  132. // * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54183
  133. // * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58897
  134. // * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65668
  135. //
  136. // Clang bugs:
  137. // * https://llvm.org/bugs/show_bug.cgi?id=6397
  138. // * https://llvm.org/bugs/show_bug.cgi?id=12418
  139. //
  140. // These issues aren't specific to x86 and x86_64, so it might be worthwhile
  141. // to add more assembly language implementations.
  142. #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86) && \
  143. (defined(__GNUC__) || defined(__clang__))
  144. __asm__ volatile("divl %4"
  145. : "=a"(*quotient_out), "=d"(*rem_out)
  146. : "a"(n1), "d"(n0), "rm"(d0)
  147. : "cc");
  148. #elif !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
  149. (defined(__GNUC__) || defined(__clang__))
  150. __asm__ volatile("divq %4"
  151. : "=a"(*quotient_out), "=d"(*rem_out)
  152. : "a"(n1), "d"(n0), "rm"(d0)
  153. : "cc");
  154. #else
  155. #if defined(BN_ULLONG)
  156. BN_ULLONG n = (((BN_ULLONG)n0) << BN_BITS2) | n1;
  157. *quotient_out = (BN_ULONG)(n / d0);
  158. #else
  159. *quotient_out = bn_div_words(n0, n1, d0);
  160. #endif
  161. *rem_out = n1 - (*quotient_out * d0);
  162. #endif
  163. }
  164. // BN_div computes "quotient := numerator / divisor", rounding towards zero,
  165. // and sets up |rem| such that "quotient * divisor + rem = numerator" holds.
  166. //
  167. // Thus:
  168. //
  169. // quotient->neg == numerator->neg ^ divisor->neg
  170. // (unless the result is zero)
  171. // rem->neg == numerator->neg
  172. // (unless the remainder is zero)
  173. //
  174. // If |quotient| or |rem| is NULL, the respective value is not returned.
  175. //
  176. // This was specifically designed to contain fewer branches that may leak
  177. // sensitive information; see "New Branch Prediction Vulnerabilities in OpenSSL
  178. // and Necessary Software Countermeasures" by Onur Acıçmez, Shay Gueron, and
  179. // Jean-Pierre Seifert.
  180. int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
  181. const BIGNUM *divisor, BN_CTX *ctx) {
  182. int norm_shift, loop;
  183. BIGNUM wnum;
  184. BN_ULONG *resp, *wnump;
  185. BN_ULONG d0, d1;
  186. int num_n, div_n;
  187. // This function relies on the historical minimal-width |BIGNUM| invariant.
  188. // It is already not constant-time (constant-time reductions should use
  189. // Montgomery logic), so we shrink all inputs and intermediate values to
  190. // retain the previous behavior.
  191. // Invalid zero-padding would have particularly bad consequences.
  192. int numerator_width = bn_minimal_width(numerator);
  193. int divisor_width = bn_minimal_width(divisor);
  194. if ((numerator_width > 0 && numerator->d[numerator_width - 1] == 0) ||
  195. (divisor_width > 0 && divisor->d[divisor_width - 1] == 0)) {
  196. OPENSSL_PUT_ERROR(BN, BN_R_NOT_INITIALIZED);
  197. return 0;
  198. }
  199. if (BN_is_zero(divisor)) {
  200. OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO);
  201. return 0;
  202. }
  203. BN_CTX_start(ctx);
  204. BIGNUM *tmp = BN_CTX_get(ctx);
  205. BIGNUM *snum = BN_CTX_get(ctx);
  206. BIGNUM *sdiv = BN_CTX_get(ctx);
  207. BIGNUM *res = NULL;
  208. if (quotient == NULL) {
  209. res = BN_CTX_get(ctx);
  210. } else {
  211. res = quotient;
  212. }
  213. if (sdiv == NULL || res == NULL) {
  214. goto err;
  215. }
  216. // First we normalise the numbers
  217. norm_shift = BN_BITS2 - (BN_num_bits(divisor) % BN_BITS2);
  218. if (!BN_lshift(sdiv, divisor, norm_shift)) {
  219. goto err;
  220. }
  221. bn_set_minimal_width(sdiv);
  222. sdiv->neg = 0;
  223. norm_shift += BN_BITS2;
  224. if (!BN_lshift(snum, numerator, norm_shift)) {
  225. goto err;
  226. }
  227. bn_set_minimal_width(snum);
  228. snum->neg = 0;
  229. // Since we don't want to have special-case logic for the case where snum is
  230. // larger than sdiv, we pad snum with enough zeroes without changing its
  231. // value.
  232. if (snum->width <= sdiv->width + 1) {
  233. if (!bn_wexpand(snum, sdiv->width + 2)) {
  234. goto err;
  235. }
  236. for (int i = snum->width; i < sdiv->width + 2; i++) {
  237. snum->d[i] = 0;
  238. }
  239. snum->width = sdiv->width + 2;
  240. } else {
  241. if (!bn_wexpand(snum, snum->width + 1)) {
  242. goto err;
  243. }
  244. snum->d[snum->width] = 0;
  245. snum->width++;
  246. }
  247. div_n = sdiv->width;
  248. num_n = snum->width;
  249. loop = num_n - div_n;
  250. // Lets setup a 'window' into snum
  251. // This is the part that corresponds to the current
  252. // 'area' being divided
  253. wnum.neg = 0;
  254. wnum.d = &(snum->d[loop]);
  255. wnum.width = div_n;
  256. // only needed when BN_ucmp messes up the values between width and max
  257. wnum.dmax = snum->dmax - loop; // so we don't step out of bounds
  258. // Get the top 2 words of sdiv
  259. // div_n=sdiv->width;
  260. d0 = sdiv->d[div_n - 1];
  261. d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];
  262. // pointer to the 'top' of snum
  263. wnump = &(snum->d[num_n - 1]);
  264. // Setup to 'res'
  265. res->neg = (numerator->neg ^ divisor->neg);
  266. if (!bn_wexpand(res, loop + 1)) {
  267. goto err;
  268. }
  269. res->width = loop - 1;
  270. resp = &(res->d[loop - 1]);
  271. // space for temp
  272. if (!bn_wexpand(tmp, div_n + 1)) {
  273. goto err;
  274. }
  275. // if res->width == 0 then clear the neg value otherwise decrease
  276. // the resp pointer
  277. if (res->width == 0) {
  278. res->neg = 0;
  279. } else {
  280. resp--;
  281. }
  282. for (int i = 0; i < loop - 1; i++, wnump--, resp--) {
  283. BN_ULONG q, l0;
  284. // the first part of the loop uses the top two words of snum and sdiv to
  285. // calculate a BN_ULONG q such that | wnum - sdiv * q | < sdiv
  286. BN_ULONG n0, n1, rm = 0;
  287. n0 = wnump[0];
  288. n1 = wnump[-1];
  289. if (n0 == d0) {
  290. q = BN_MASK2;
  291. } else {
  292. // n0 < d0
  293. bn_div_rem_words(&q, &rm, n0, n1, d0);
  294. #ifdef BN_ULLONG
  295. BN_ULLONG t2 = (BN_ULLONG)d1 * q;
  296. for (;;) {
  297. if (t2 <= ((((BN_ULLONG)rm) << BN_BITS2) | wnump[-2])) {
  298. break;
  299. }
  300. q--;
  301. rm += d0;
  302. if (rm < d0) {
  303. break; // don't let rm overflow
  304. }
  305. t2 -= d1;
  306. }
  307. #else // !BN_ULLONG
  308. BN_ULONG t2l, t2h;
  309. BN_UMULT_LOHI(t2l, t2h, d1, q);
  310. for (;;) {
  311. if (t2h < rm ||
  312. (t2h == rm && t2l <= wnump[-2])) {
  313. break;
  314. }
  315. q--;
  316. rm += d0;
  317. if (rm < d0) {
  318. break; // don't let rm overflow
  319. }
  320. if (t2l < d1) {
  321. t2h--;
  322. }
  323. t2l -= d1;
  324. }
  325. #endif // !BN_ULLONG
  326. }
  327. l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);
  328. tmp->d[div_n] = l0;
  329. wnum.d--;
  330. // ingore top values of the bignums just sub the two
  331. // BN_ULONG arrays with bn_sub_words
  332. if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {
  333. // Note: As we have considered only the leading
  334. // two BN_ULONGs in the calculation of q, sdiv * q
  335. // might be greater than wnum (but then (q-1) * sdiv
  336. // is less or equal than wnum)
  337. q--;
  338. if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n)) {
  339. // we can't have an overflow here (assuming
  340. // that q != 0, but if q == 0 then tmp is
  341. // zero anyway)
  342. (*wnump)++;
  343. }
  344. }
  345. // store part of the result
  346. *resp = q;
  347. }
  348. bn_set_minimal_width(snum);
  349. if (rem != NULL) {
  350. // Keep a copy of the neg flag in numerator because if |rem| == |numerator|
  351. // |BN_rshift| will overwrite it.
  352. int neg = numerator->neg;
  353. if (!BN_rshift(rem, snum, norm_shift)) {
  354. goto err;
  355. }
  356. if (!BN_is_zero(rem)) {
  357. rem->neg = neg;
  358. }
  359. }
  360. bn_set_minimal_width(res);
  361. BN_CTX_end(ctx);
  362. return 1;
  363. err:
  364. BN_CTX_end(ctx);
  365. return 0;
  366. }
  367. int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx) {
  368. if (!(BN_mod(r, m, d, ctx))) {
  369. return 0;
  370. }
  371. if (!r->neg) {
  372. return 1;
  373. }
  374. // now -|d| < r < 0, so we have to set r := r + |d|.
  375. return (d->neg ? BN_sub : BN_add)(r, r, d);
  376. }
  377. // bn_mod_sub_words sets |r| to |a| - |b| (mod |m|), using |tmp| as scratch
  378. // space. Each array is |num| words long. |a| and |b| must be < |m|. Any pair of
  379. // |r|, |a|, and |b| may alias.
  380. static void bn_mod_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  381. const BN_ULONG *m, BN_ULONG *tmp, size_t num) {
  382. // r = a - b
  383. BN_ULONG borrow = bn_sub_words(r, a, b, num);
  384. // tmp = a - b + m
  385. bn_add_words(tmp, r, m, num);
  386. bn_select_words(r, 0 - borrow, tmp /* r < 0 */, r /* r >= 0 */, num);
  387. }
  388. // bn_mod_add_words sets |r| to |a| + |b| (mod |m|), using |tmp| as scratch
  389. // space. Each array is |num| words long. |a| and |b| must be < |m|. Any pair of
  390. // |r|, |a|, and |b| may alias.
  391. static void bn_mod_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  392. const BN_ULONG *m, BN_ULONG *tmp, size_t num) {
  393. // tmp = a + b. Note the result fits in |num|+1 words. We store the extra word
  394. // in |carry|.
  395. BN_ULONG carry = bn_add_words(tmp, a, b, num);
  396. // r = a + b - m. We use |bn_sub_words| to perform the bulk of the
  397. // subtraction, and then apply the borrow to |carry|.
  398. carry -= bn_sub_words(r, tmp, m, num);
  399. // |a| and |b| were both fully-reduced, so we know:
  400. //
  401. // 0 + 0 - m <= r < m + m - m
  402. // -m <= r < m
  403. //
  404. // If 0 <= |r| < |m|, |r| fits in |num| words and |carry| is zero. We then
  405. // wish to select |r| as the answer. Otherwise -m <= r < 0 and we wish to
  406. // return |r| + |m|, or |tmp|. |carry| must then be -1 or all ones. In both
  407. // cases, |carry| is a suitable input to |bn_select_words|.
  408. //
  409. // Although |carry| may be one if |bn_add_words| returns one and
  410. // |bn_sub_words| returns zero, this would give |r| > |m|, which violates are
  411. // input assumptions.
  412. assert(carry == 0 || carry == (BN_ULONG)-1);
  413. bn_select_words(r, carry, tmp /* r < 0 */, r /* r >= 0 */, num);
  414. }
  415. int bn_div_consttime(BIGNUM *quotient, BIGNUM *remainder,
  416. const BIGNUM *numerator, const BIGNUM *divisor,
  417. BN_CTX *ctx) {
  418. if (BN_is_negative(numerator) || BN_is_negative(divisor)) {
  419. OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
  420. return 0;
  421. }
  422. if (BN_is_zero(divisor)) {
  423. OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO);
  424. return 0;
  425. }
  426. // This function implements long division in binary. It is not very efficient,
  427. // but it is simple, easy to make constant-time, and performant enough for RSA
  428. // key generation.
  429. int ret = 0;
  430. BN_CTX_start(ctx);
  431. BIGNUM *q = quotient, *r = remainder;
  432. if (quotient == NULL || quotient == numerator || quotient == divisor) {
  433. q = BN_CTX_get(ctx);
  434. }
  435. if (remainder == NULL || remainder == numerator || remainder == divisor) {
  436. r = BN_CTX_get(ctx);
  437. }
  438. BIGNUM *tmp = BN_CTX_get(ctx);
  439. if (q == NULL || r == NULL || tmp == NULL ||
  440. !bn_wexpand(q, numerator->width) ||
  441. !bn_wexpand(r, divisor->width) ||
  442. !bn_wexpand(tmp, divisor->width)) {
  443. goto err;
  444. }
  445. OPENSSL_memset(q->d, 0, numerator->width * sizeof(BN_ULONG));
  446. q->width = numerator->width;
  447. q->neg = 0;
  448. OPENSSL_memset(r->d, 0, divisor->width * sizeof(BN_ULONG));
  449. r->width = divisor->width;
  450. r->neg = 0;
  451. // Incorporate |numerator| into |r|, one bit at a time, reducing after each
  452. // step. At the start of each loop iteration, |r| < |divisor|
  453. for (int i = numerator->width - 1; i >= 0; i--) {
  454. for (int bit = BN_BITS2 - 1; bit >= 0; bit--) {
  455. // Incorporate the next bit of the numerator, by computing
  456. // r = 2*r or 2*r + 1. Note the result fits in one more word. We store the
  457. // extra word in |carry|.
  458. BN_ULONG carry = bn_add_words(r->d, r->d, r->d, divisor->width);
  459. r->d[0] |= (numerator->d[i] >> bit) & 1;
  460. // tmp = r - divisor. We use |bn_sub_words| to perform the bulk of the
  461. // subtraction, and then apply the borrow to |carry|.
  462. carry -= bn_sub_words(tmp->d, r->d, divisor->d, divisor->width);
  463. // |r| was previously fully-reduced, so we know:
  464. //
  465. // 2*0 - divisor <= tmp <= 2*(divisor-1) + 1 - divisor
  466. // -divisor <= tmp < divisor
  467. //
  468. // If 0 <= |tmp| < |divisor|, |tmp| fits in |divisor->width| and |carry|
  469. // is zero. We then wish to select |tmp|. Otherwise,
  470. // -|divisor| <= |tmp| < 0 and we wish to select |tmp| + |divisor|, which
  471. // is |r|. |carry| must then be -1 (all ones). In both cases, |carry| is a
  472. // suitable input to |bn_select_words|.
  473. //
  474. // Although |carry| may be one if |bn_add_words| returns one and
  475. // |bn_sub_words| returns zero, this would give |r| > |d|, which violates
  476. // the loop invariant.
  477. bn_select_words(r->d, carry, r->d /* tmp < 0 */, tmp->d /* tmp >= 0 */,
  478. divisor->width);
  479. // The corresponding bit of the quotient is set iff we needed to subtract.
  480. q->d[i] |= (~carry & 1) << bit;
  481. }
  482. }
  483. if ((quotient != NULL && !BN_copy(quotient, q)) ||
  484. (remainder != NULL && !BN_copy(remainder, r))) {
  485. goto err;
  486. }
  487. ret = 1;
  488. err:
  489. BN_CTX_end(ctx);
  490. return ret;
  491. }
  492. static BIGNUM *bn_scratch_space_from_ctx(size_t width, BN_CTX *ctx) {
  493. BIGNUM *ret = BN_CTX_get(ctx);
  494. if (ret == NULL ||
  495. !bn_wexpand(ret, width)) {
  496. return NULL;
  497. }
  498. ret->neg = 0;
  499. ret->width = width;
  500. return ret;
  501. }
  502. // bn_resized_from_ctx returns |bn| with width at least |width| or NULL on
  503. // error. This is so it may be used with low-level "words" functions. If
  504. // necessary, it allocates a new |BIGNUM| with a lifetime of the current scope
  505. // in |ctx|, so the caller does not need to explicitly free it. |bn| must fit in
  506. // |width| words.
  507. static const BIGNUM *bn_resized_from_ctx(const BIGNUM *bn, size_t width,
  508. BN_CTX *ctx) {
  509. if ((size_t)bn->width >= width) {
  510. // Any excess words must be zero.
  511. assert(bn_fits_in_words(bn, width));
  512. return bn;
  513. }
  514. BIGNUM *ret = bn_scratch_space_from_ctx(width, ctx);
  515. if (ret == NULL ||
  516. !BN_copy(ret, bn) ||
  517. !bn_resize_words(ret, width)) {
  518. return NULL;
  519. }
  520. return ret;
  521. }
  522. int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
  523. BN_CTX *ctx) {
  524. if (!BN_add(r, a, b)) {
  525. return 0;
  526. }
  527. return BN_nnmod(r, r, m, ctx);
  528. }
  529. int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  530. const BIGNUM *m) {
  531. BN_CTX *ctx = BN_CTX_new();
  532. int ok = ctx != NULL &&
  533. bn_mod_add_consttime(r, a, b, m, ctx);
  534. BN_CTX_free(ctx);
  535. return ok;
  536. }
  537. int bn_mod_add_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  538. const BIGNUM *m, BN_CTX *ctx) {
  539. BN_CTX_start(ctx);
  540. a = bn_resized_from_ctx(a, m->width, ctx);
  541. b = bn_resized_from_ctx(b, m->width, ctx);
  542. BIGNUM *tmp = bn_scratch_space_from_ctx(m->width, ctx);
  543. int ok = a != NULL && b != NULL && tmp != NULL &&
  544. bn_wexpand(r, m->width);
  545. if (ok) {
  546. bn_mod_add_words(r->d, a->d, b->d, m->d, tmp->d, m->width);
  547. r->width = m->width;
  548. }
  549. BN_CTX_end(ctx);
  550. return ok;
  551. }
  552. int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
  553. BN_CTX *ctx) {
  554. if (!BN_sub(r, a, b)) {
  555. return 0;
  556. }
  557. return BN_nnmod(r, r, m, ctx);
  558. }
  559. int bn_mod_sub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  560. const BIGNUM *m, BN_CTX *ctx) {
  561. BN_CTX_start(ctx);
  562. a = bn_resized_from_ctx(a, m->width, ctx);
  563. b = bn_resized_from_ctx(b, m->width, ctx);
  564. BIGNUM *tmp = bn_scratch_space_from_ctx(m->width, ctx);
  565. int ok = a != NULL && b != NULL && tmp != NULL &&
  566. bn_wexpand(r, m->width);
  567. if (ok) {
  568. bn_mod_sub_words(r->d, a->d, b->d, m->d, tmp->d, m->width);
  569. r->width = m->width;
  570. }
  571. BN_CTX_end(ctx);
  572. return ok;
  573. }
  574. int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  575. const BIGNUM *m) {
  576. BN_CTX *ctx = BN_CTX_new();
  577. int ok = ctx != NULL &&
  578. bn_mod_sub_consttime(r, a, b, m, ctx);
  579. BN_CTX_free(ctx);
  580. return ok;
  581. }
  582. int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
  583. BN_CTX *ctx) {
  584. BIGNUM *t;
  585. int ret = 0;
  586. BN_CTX_start(ctx);
  587. t = BN_CTX_get(ctx);
  588. if (t == NULL) {
  589. goto err;
  590. }
  591. if (a == b) {
  592. if (!BN_sqr(t, a, ctx)) {
  593. goto err;
  594. }
  595. } else {
  596. if (!BN_mul(t, a, b, ctx)) {
  597. goto err;
  598. }
  599. }
  600. if (!BN_nnmod(r, t, m, ctx)) {
  601. goto err;
  602. }
  603. ret = 1;
  604. err:
  605. BN_CTX_end(ctx);
  606. return ret;
  607. }
  608. int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) {
  609. if (!BN_sqr(r, a, ctx)) {
  610. return 0;
  611. }
  612. // r->neg == 0, thus we don't need BN_nnmod
  613. return BN_mod(r, r, m, ctx);
  614. }
  615. int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
  616. BN_CTX *ctx) {
  617. BIGNUM *abs_m = NULL;
  618. int ret;
  619. if (!BN_nnmod(r, a, m, ctx)) {
  620. return 0;
  621. }
  622. if (m->neg) {
  623. abs_m = BN_dup(m);
  624. if (abs_m == NULL) {
  625. return 0;
  626. }
  627. abs_m->neg = 0;
  628. }
  629. ret = bn_mod_lshift_consttime(r, r, n, (abs_m ? abs_m : m), ctx);
  630. BN_free(abs_m);
  631. return ret;
  632. }
  633. int bn_mod_lshift_consttime(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
  634. BN_CTX *ctx) {
  635. if (!BN_copy(r, a)) {
  636. return 0;
  637. }
  638. for (int i = 0; i < n; i++) {
  639. if (!bn_mod_lshift1_consttime(r, r, m, ctx)) {
  640. return 0;
  641. }
  642. }
  643. return 1;
  644. }
  645. int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m) {
  646. BN_CTX *ctx = BN_CTX_new();
  647. int ok = ctx != NULL &&
  648. bn_mod_lshift_consttime(r, a, n, m, ctx);
  649. BN_CTX_free(ctx);
  650. return ok;
  651. }
  652. int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) {
  653. if (!BN_lshift1(r, a)) {
  654. return 0;
  655. }
  656. return BN_nnmod(r, r, m, ctx);
  657. }
  658. int bn_mod_lshift1_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *m,
  659. BN_CTX *ctx) {
  660. return bn_mod_add_consttime(r, a, a, m, ctx);
  661. }
  662. int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m) {
  663. BN_CTX *ctx = BN_CTX_new();
  664. int ok = ctx != NULL &&
  665. bn_mod_lshift1_consttime(r, a, m, ctx);
  666. BN_CTX_free(ctx);
  667. return ok;
  668. }
  669. BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) {
  670. BN_ULONG ret = 0;
  671. int i, j;
  672. if (!w) {
  673. // actually this an error (division by zero)
  674. return (BN_ULONG) - 1;
  675. }
  676. if (a->width == 0) {
  677. return 0;
  678. }
  679. // normalize input for |bn_div_rem_words|.
  680. j = BN_BITS2 - BN_num_bits_word(w);
  681. w <<= j;
  682. if (!BN_lshift(a, a, j)) {
  683. return (BN_ULONG) - 1;
  684. }
  685. for (i = a->width - 1; i >= 0; i--) {
  686. BN_ULONG l = a->d[i];
  687. BN_ULONG d;
  688. BN_ULONG unused_rem;
  689. bn_div_rem_words(&d, &unused_rem, ret, l, w);
  690. ret = l - (d * w);
  691. a->d[i] = d;
  692. }
  693. bn_set_minimal_width(a);
  694. ret >>= j;
  695. return ret;
  696. }
  697. BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) {
  698. #ifndef BN_CAN_DIVIDE_ULLONG
  699. BN_ULONG ret = 0;
  700. #else
  701. BN_ULLONG ret = 0;
  702. #endif
  703. int i;
  704. if (w == 0) {
  705. return (BN_ULONG) -1;
  706. }
  707. #ifndef BN_CAN_DIVIDE_ULLONG
  708. // If |w| is too long and we don't have |BN_ULLONG| division then we need to
  709. // fall back to using |BN_div_word|.
  710. if (w > ((BN_ULONG)1 << BN_BITS4)) {
  711. BIGNUM *tmp = BN_dup(a);
  712. if (tmp == NULL) {
  713. return (BN_ULONG)-1;
  714. }
  715. ret = BN_div_word(tmp, w);
  716. BN_free(tmp);
  717. return ret;
  718. }
  719. #endif
  720. for (i = a->width - 1; i >= 0; i--) {
  721. #ifndef BN_CAN_DIVIDE_ULLONG
  722. ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w;
  723. ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w;
  724. #else
  725. ret = (BN_ULLONG)(((ret << (BN_ULLONG)BN_BITS2) | a->d[i]) % (BN_ULLONG)w);
  726. #endif
  727. }
  728. return (BN_ULONG)ret;
  729. }
  730. int BN_mod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) {
  731. if (e == 0 || a->width == 0) {
  732. BN_zero(r);
  733. return 1;
  734. }
  735. size_t num_words = 1 + ((e - 1) / BN_BITS2);
  736. // If |a| definitely has less than |e| bits, just BN_copy.
  737. if ((size_t) a->width < num_words) {
  738. return BN_copy(r, a) != NULL;
  739. }
  740. // Otherwise, first make sure we have enough space in |r|.
  741. // Note that this will fail if num_words > INT_MAX.
  742. if (!bn_wexpand(r, num_words)) {
  743. return 0;
  744. }
  745. // Copy the content of |a| into |r|.
  746. OPENSSL_memcpy(r->d, a->d, num_words * sizeof(BN_ULONG));
  747. // If |e| isn't word-aligned, we have to mask off some of our bits.
  748. size_t top_word_exponent = e % (sizeof(BN_ULONG) * 8);
  749. if (top_word_exponent != 0) {
  750. r->d[num_words - 1] &= (((BN_ULONG) 1) << top_word_exponent) - 1;
  751. }
  752. // Fill in the remaining fields of |r|.
  753. r->neg = a->neg;
  754. r->width = (int) num_words;
  755. bn_set_minimal_width(r);
  756. return 1;
  757. }
  758. int BN_nnmod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) {
  759. if (!BN_mod_pow2(r, a, e)) {
  760. return 0;
  761. }
  762. // If the returned value was non-negative, we're done.
  763. if (BN_is_zero(r) || !r->neg) {
  764. return 1;
  765. }
  766. size_t num_words = 1 + (e - 1) / BN_BITS2;
  767. // Expand |r| to the size of our modulus.
  768. if (!bn_wexpand(r, num_words)) {
  769. return 0;
  770. }
  771. // Clear the upper words of |r|.
  772. OPENSSL_memset(&r->d[r->width], 0, (num_words - r->width) * BN_BYTES);
  773. // Set parameters of |r|.
  774. r->neg = 0;
  775. r->width = (int) num_words;
  776. // Now, invert every word. The idea here is that we want to compute 2^e-|x|,
  777. // which is actually equivalent to the twos-complement representation of |x|
  778. // in |e| bits, which is -x = ~x + 1.
  779. for (int i = 0; i < r->width; i++) {
  780. r->d[i] = ~r->d[i];
  781. }
  782. // If our exponent doesn't span the top word, we have to mask the rest.
  783. size_t top_word_exponent = e % BN_BITS2;
  784. if (top_word_exponent != 0) {
  785. r->d[r->width - 1] &= (((BN_ULONG) 1) << top_word_exponent) - 1;
  786. }
  787. // Keep the minimal-width invariant for |BIGNUM|.
  788. bn_set_minimal_width(r);
  789. // Finally, add one, for the reason described above.
  790. return BN_add(r, r, BN_value_one());
  791. }