2
0

bn_div.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Copyright 1995-2017 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 <openssl/bn.h>
  10. #include "internal/cryptlib.h"
  11. #include "bn_lcl.h"
  12. /* The old slow way */
  13. #if 0
  14. int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
  15. BN_CTX *ctx)
  16. {
  17. int i, nm, nd;
  18. int ret = 0;
  19. BIGNUM *D;
  20. bn_check_top(m);
  21. bn_check_top(d);
  22. if (BN_is_zero(d)) {
  23. BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);
  24. return 0;
  25. }
  26. if (BN_ucmp(m, d) < 0) {
  27. if (rem != NULL) {
  28. if (BN_copy(rem, m) == NULL)
  29. return 0;
  30. }
  31. if (dv != NULL)
  32. BN_zero(dv);
  33. return 1;
  34. }
  35. BN_CTX_start(ctx);
  36. D = BN_CTX_get(ctx);
  37. if (dv == NULL)
  38. dv = BN_CTX_get(ctx);
  39. if (rem == NULL)
  40. rem = BN_CTX_get(ctx);
  41. if (D == NULL || dv == NULL || rem == NULL)
  42. goto end;
  43. nd = BN_num_bits(d);
  44. nm = BN_num_bits(m);
  45. if (BN_copy(D, d) == NULL)
  46. goto end;
  47. if (BN_copy(rem, m) == NULL)
  48. goto end;
  49. /*
  50. * The next 2 are needed so we can do a dv->d[0]|=1 later since
  51. * BN_lshift1 will only work once there is a value :-)
  52. */
  53. BN_zero(dv);
  54. if (bn_wexpand(dv, 1) == NULL)
  55. goto end;
  56. dv->top = 1;
  57. if (!BN_lshift(D, D, nm - nd))
  58. goto end;
  59. for (i = nm - nd; i >= 0; i--) {
  60. if (!BN_lshift1(dv, dv))
  61. goto end;
  62. if (BN_ucmp(rem, D) >= 0) {
  63. dv->d[0] |= 1;
  64. if (!BN_usub(rem, rem, D))
  65. goto end;
  66. }
  67. /* CAN IMPROVE (and have now :=) */
  68. if (!BN_rshift1(D, D))
  69. goto end;
  70. }
  71. rem->neg = BN_is_zero(rem) ? 0 : m->neg;
  72. dv->neg = m->neg ^ d->neg;
  73. ret = 1;
  74. end:
  75. BN_CTX_end(ctx);
  76. return ret;
  77. }
  78. #else
  79. # if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) \
  80. && !defined(PEDANTIC) && !defined(BN_DIV3W)
  81. # if defined(__GNUC__) && __GNUC__>=2
  82. # if defined(__i386) || defined (__i386__)
  83. /*-
  84. * There were two reasons for implementing this template:
  85. * - GNU C generates a call to a function (__udivdi3 to be exact)
  86. * in reply to ((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0 (I fail to
  87. * understand why...);
  88. * - divl doesn't only calculate quotient, but also leaves
  89. * remainder in %edx which we can definitely use here:-)
  90. */
  91. # undef bn_div_words
  92. # define bn_div_words(n0,n1,d0) \
  93. ({ asm volatile ( \
  94. "divl %4" \
  95. : "=a"(q), "=d"(rem) \
  96. : "a"(n1), "d"(n0), "r"(d0) \
  97. : "cc"); \
  98. q; \
  99. })
  100. # define REMAINDER_IS_ALREADY_CALCULATED
  101. # elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG)
  102. /*
  103. * Same story here, but it's 128-bit by 64-bit division. Wow!
  104. */
  105. # undef bn_div_words
  106. # define bn_div_words(n0,n1,d0) \
  107. ({ asm volatile ( \
  108. "divq %4" \
  109. : "=a"(q), "=d"(rem) \
  110. : "a"(n1), "d"(n0), "r"(d0) \
  111. : "cc"); \
  112. q; \
  113. })
  114. # define REMAINDER_IS_ALREADY_CALCULATED
  115. # endif /* __<cpu> */
  116. # endif /* __GNUC__ */
  117. # endif /* OPENSSL_NO_ASM */
  118. /*-
  119. * BN_div computes dv := num / divisor, rounding towards
  120. * zero, and sets up rm such that dv*divisor + rm = num holds.
  121. * Thus:
  122. * dv->neg == num->neg ^ divisor->neg (unless the result is zero)
  123. * rm->neg == num->neg (unless the remainder is zero)
  124. * If 'dv' or 'rm' is NULL, the respective value is not returned.
  125. */
  126. int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
  127. BN_CTX *ctx)
  128. {
  129. int norm_shift, i, loop;
  130. BIGNUM *tmp, wnum, *snum, *sdiv, *res;
  131. BN_ULONG *resp, *wnump;
  132. BN_ULONG d0, d1;
  133. int num_n, div_n;
  134. int no_branch = 0;
  135. /*
  136. * Invalid zero-padding would have particularly bad consequences so don't
  137. * just rely on bn_check_top() here (bn_check_top() works only for
  138. * BN_DEBUG builds)
  139. */
  140. if ((num->top > 0 && num->d[num->top - 1] == 0) ||
  141. (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {
  142. BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);
  143. return 0;
  144. }
  145. bn_check_top(num);
  146. bn_check_top(divisor);
  147. if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)
  148. || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {
  149. no_branch = 1;
  150. }
  151. bn_check_top(dv);
  152. bn_check_top(rm);
  153. /*- bn_check_top(num); *//*
  154. * 'num' has been checked already
  155. */
  156. /*- bn_check_top(divisor); *//*
  157. * 'divisor' has been checked already
  158. */
  159. if (BN_is_zero(divisor)) {
  160. BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);
  161. return 0;
  162. }
  163. if (!no_branch && BN_ucmp(num, divisor) < 0) {
  164. if (rm != NULL) {
  165. if (BN_copy(rm, num) == NULL)
  166. return 0;
  167. }
  168. if (dv != NULL)
  169. BN_zero(dv);
  170. return 1;
  171. }
  172. BN_CTX_start(ctx);
  173. res = (dv == NULL) ? BN_CTX_get(ctx) : dv;
  174. tmp = BN_CTX_get(ctx);
  175. snum = BN_CTX_get(ctx);
  176. sdiv = BN_CTX_get(ctx);
  177. if (sdiv == NULL)
  178. goto err;
  179. /* First we normalise the numbers */
  180. norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);
  181. if (!(BN_lshift(sdiv, divisor, norm_shift)))
  182. goto err;
  183. sdiv->neg = 0;
  184. norm_shift += BN_BITS2;
  185. if (!(BN_lshift(snum, num, norm_shift)))
  186. goto err;
  187. snum->neg = 0;
  188. if (no_branch) {
  189. /*
  190. * Since we don't know whether snum is larger than sdiv, we pad snum
  191. * with enough zeroes without changing its value.
  192. */
  193. if (snum->top <= sdiv->top + 1) {
  194. if (bn_wexpand(snum, sdiv->top + 2) == NULL)
  195. goto err;
  196. for (i = snum->top; i < sdiv->top + 2; i++)
  197. snum->d[i] = 0;
  198. snum->top = sdiv->top + 2;
  199. } else {
  200. if (bn_wexpand(snum, snum->top + 1) == NULL)
  201. goto err;
  202. snum->d[snum->top] = 0;
  203. snum->top++;
  204. }
  205. }
  206. div_n = sdiv->top;
  207. num_n = snum->top;
  208. loop = num_n - div_n;
  209. /*
  210. * Lets setup a 'window' into snum This is the part that corresponds to
  211. * the current 'area' being divided
  212. */
  213. wnum.neg = 0;
  214. wnum.d = &(snum->d[loop]);
  215. wnum.top = div_n;
  216. /*
  217. * only needed when BN_ucmp messes up the values between top and max
  218. */
  219. wnum.dmax = snum->dmax - loop; /* so we don't step out of bounds */
  220. /* Get the top 2 words of sdiv */
  221. /* div_n=sdiv->top; */
  222. d0 = sdiv->d[div_n - 1];
  223. d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];
  224. /* pointer to the 'top' of snum */
  225. wnump = &(snum->d[num_n - 1]);
  226. /* Setup to 'res' */
  227. if (!bn_wexpand(res, (loop + 1)))
  228. goto err;
  229. res->neg = (num->neg ^ divisor->neg);
  230. res->top = loop - no_branch;
  231. resp = &(res->d[loop - 1]);
  232. /* space for temp */
  233. if (!bn_wexpand(tmp, (div_n + 1)))
  234. goto err;
  235. if (!no_branch) {
  236. if (BN_ucmp(&wnum, sdiv) >= 0) {
  237. /*
  238. * If BN_DEBUG_RAND is defined BN_ucmp changes (via bn_pollute)
  239. * the const bignum arguments => clean the values between top and
  240. * max again
  241. */
  242. bn_clear_top2max(&wnum);
  243. bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);
  244. *resp = 1;
  245. } else
  246. res->top--;
  247. }
  248. /* Increase the resp pointer so that we never create an invalid pointer. */
  249. resp++;
  250. /*
  251. * if res->top == 0 then clear the neg value otherwise decrease the resp
  252. * pointer
  253. */
  254. if (res->top == 0)
  255. res->neg = 0;
  256. else
  257. resp--;
  258. for (i = 0; i < loop - 1; i++, wnump--) {
  259. BN_ULONG q, l0;
  260. /*
  261. * the first part of the loop uses the top two words of snum and sdiv
  262. * to calculate a BN_ULONG q such that | wnum - sdiv * q | < sdiv
  263. */
  264. # if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)
  265. BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);
  266. q = bn_div_3_words(wnump, d1, d0);
  267. # else
  268. BN_ULONG n0, n1, rem = 0;
  269. n0 = wnump[0];
  270. n1 = wnump[-1];
  271. if (n0 == d0)
  272. q = BN_MASK2;
  273. else { /* n0 < d0 */
  274. # ifdef BN_LLONG
  275. BN_ULLONG t2;
  276. # if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)
  277. q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);
  278. # else
  279. q = bn_div_words(n0, n1, d0);
  280. # endif
  281. # ifndef REMAINDER_IS_ALREADY_CALCULATED
  282. /*
  283. * rem doesn't have to be BN_ULLONG. The least we
  284. * know it's less that d0, isn't it?
  285. */
  286. rem = (n1 - q * d0) & BN_MASK2;
  287. # endif
  288. t2 = (BN_ULLONG) d1 *q;
  289. for (;;) {
  290. if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))
  291. break;
  292. q--;
  293. rem += d0;
  294. if (rem < d0)
  295. break; /* don't let rem overflow */
  296. t2 -= d1;
  297. }
  298. # else /* !BN_LLONG */
  299. BN_ULONG t2l, t2h;
  300. q = bn_div_words(n0, n1, d0);
  301. # ifndef REMAINDER_IS_ALREADY_CALCULATED
  302. rem = (n1 - q * d0) & BN_MASK2;
  303. # endif
  304. # if defined(BN_UMULT_LOHI)
  305. BN_UMULT_LOHI(t2l, t2h, d1, q);
  306. # elif defined(BN_UMULT_HIGH)
  307. t2l = d1 * q;
  308. t2h = BN_UMULT_HIGH(d1, q);
  309. # else
  310. {
  311. BN_ULONG ql, qh;
  312. t2l = LBITS(d1);
  313. t2h = HBITS(d1);
  314. ql = LBITS(q);
  315. qh = HBITS(q);
  316. mul64(t2l, t2h, ql, qh); /* t2=(BN_ULLONG)d1*q; */
  317. }
  318. # endif
  319. for (;;) {
  320. if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))
  321. break;
  322. q--;
  323. rem += d0;
  324. if (rem < d0)
  325. break; /* don't let rem overflow */
  326. if (t2l < d1)
  327. t2h--;
  328. t2l -= d1;
  329. }
  330. # endif /* !BN_LLONG */
  331. }
  332. # endif /* !BN_DIV3W */
  333. l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);
  334. tmp->d[div_n] = l0;
  335. wnum.d--;
  336. /*
  337. * ingore top values of the bignums just sub the two BN_ULONG arrays
  338. * with bn_sub_words
  339. */
  340. if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {
  341. /*
  342. * Note: As we have considered only the leading two BN_ULONGs in
  343. * the calculation of q, sdiv * q might be greater than wnum (but
  344. * then (q-1) * sdiv is less or equal than wnum)
  345. */
  346. q--;
  347. if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))
  348. /*
  349. * we can't have an overflow here (assuming that q != 0, but
  350. * if q == 0 then tmp is zero anyway)
  351. */
  352. (*wnump)++;
  353. }
  354. /* store part of the result */
  355. resp--;
  356. *resp = q;
  357. }
  358. bn_correct_top(snum);
  359. if (rm != NULL) {
  360. /*
  361. * Keep a copy of the neg flag in num because if rm==num BN_rshift()
  362. * will overwrite it.
  363. */
  364. int neg = num->neg;
  365. BN_rshift(rm, snum, norm_shift);
  366. if (!BN_is_zero(rm))
  367. rm->neg = neg;
  368. bn_check_top(rm);
  369. }
  370. if (no_branch)
  371. bn_correct_top(res);
  372. BN_CTX_end(ctx);
  373. return 1;
  374. err:
  375. bn_check_top(rm);
  376. BN_CTX_end(ctx);
  377. return 0;
  378. }
  379. #endif