Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

872 řádky
21 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. #include <openssl/bn.h>
  57. #include <assert.h>
  58. #include <string.h>
  59. #include "internal.h"
  60. #define BN_MUL_RECURSIVE_SIZE_NORMAL 16
  61. #define BN_SQR_RECURSIVE_SIZE_NORMAL BN_MUL_RECURSIVE_SIZE_NORMAL
  62. static void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b,
  63. int nb) {
  64. BN_ULONG *rr;
  65. if (na < nb) {
  66. int itmp;
  67. BN_ULONG *ltmp;
  68. itmp = na;
  69. na = nb;
  70. nb = itmp;
  71. ltmp = a;
  72. a = b;
  73. b = ltmp;
  74. }
  75. rr = &(r[na]);
  76. if (nb <= 0) {
  77. (void)bn_mul_words(r, a, na, 0);
  78. return;
  79. } else {
  80. rr[0] = bn_mul_words(r, a, na, b[0]);
  81. }
  82. for (;;) {
  83. if (--nb <= 0) {
  84. return;
  85. }
  86. rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
  87. if (--nb <= 0) {
  88. return;
  89. }
  90. rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
  91. if (--nb <= 0) {
  92. return;
  93. }
  94. rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
  95. if (--nb <= 0) {
  96. return;
  97. }
  98. rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
  99. rr += 4;
  100. r += 4;
  101. b += 4;
  102. }
  103. }
  104. #if !defined(OPENSSL_X86) || defined(OPENSSL_NO_ASM)
  105. /* Here follows specialised variants of bn_add_words() and bn_sub_words(). They
  106. * have the property performing operations on arrays of different sizes. The
  107. * sizes of those arrays is expressed through cl, which is the common length (
  108. * basicall, min(len(a),len(b)) ), and dl, which is the delta between the two
  109. * lengths, calculated as len(a)-len(b). All lengths are the number of
  110. * BN_ULONGs... For the operations that require a result array as parameter,
  111. * it must have the length cl+abs(dl). These functions should probably end up
  112. * in bn_asm.c as soon as there are assembler counterparts for the systems that
  113. * use assembler files. */
  114. static BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a,
  115. const BN_ULONG *b, int cl, int dl) {
  116. BN_ULONG c, t;
  117. assert(cl >= 0);
  118. c = bn_sub_words(r, a, b, cl);
  119. if (dl == 0) {
  120. return c;
  121. }
  122. r += cl;
  123. a += cl;
  124. b += cl;
  125. if (dl < 0) {
  126. for (;;) {
  127. t = b[0];
  128. r[0] = (0 - t - c) & BN_MASK2;
  129. if (t != 0) {
  130. c = 1;
  131. }
  132. if (++dl >= 0) {
  133. break;
  134. }
  135. t = b[1];
  136. r[1] = (0 - t - c) & BN_MASK2;
  137. if (t != 0) {
  138. c = 1;
  139. }
  140. if (++dl >= 0) {
  141. break;
  142. }
  143. t = b[2];
  144. r[2] = (0 - t - c) & BN_MASK2;
  145. if (t != 0) {
  146. c = 1;
  147. }
  148. if (++dl >= 0) {
  149. break;
  150. }
  151. t = b[3];
  152. r[3] = (0 - t - c) & BN_MASK2;
  153. if (t != 0) {
  154. c = 1;
  155. }
  156. if (++dl >= 0) {
  157. break;
  158. }
  159. b += 4;
  160. r += 4;
  161. }
  162. } else {
  163. int save_dl = dl;
  164. while (c) {
  165. t = a[0];
  166. r[0] = (t - c) & BN_MASK2;
  167. if (t != 0) {
  168. c = 0;
  169. }
  170. if (--dl <= 0) {
  171. break;
  172. }
  173. t = a[1];
  174. r[1] = (t - c) & BN_MASK2;
  175. if (t != 0) {
  176. c = 0;
  177. }
  178. if (--dl <= 0) {
  179. break;
  180. }
  181. t = a[2];
  182. r[2] = (t - c) & BN_MASK2;
  183. if (t != 0) {
  184. c = 0;
  185. }
  186. if (--dl <= 0) {
  187. break;
  188. }
  189. t = a[3];
  190. r[3] = (t - c) & BN_MASK2;
  191. if (t != 0) {
  192. c = 0;
  193. }
  194. if (--dl <= 0) {
  195. break;
  196. }
  197. save_dl = dl;
  198. a += 4;
  199. r += 4;
  200. }
  201. if (dl > 0) {
  202. if (save_dl > dl) {
  203. switch (save_dl - dl) {
  204. case 1:
  205. r[1] = a[1];
  206. if (--dl <= 0) {
  207. break;
  208. }
  209. case 2:
  210. r[2] = a[2];
  211. if (--dl <= 0) {
  212. break;
  213. }
  214. case 3:
  215. r[3] = a[3];
  216. if (--dl <= 0) {
  217. break;
  218. }
  219. }
  220. a += 4;
  221. r += 4;
  222. }
  223. }
  224. if (dl > 0) {
  225. for (;;) {
  226. r[0] = a[0];
  227. if (--dl <= 0) {
  228. break;
  229. }
  230. r[1] = a[1];
  231. if (--dl <= 0) {
  232. break;
  233. }
  234. r[2] = a[2];
  235. if (--dl <= 0) {
  236. break;
  237. }
  238. r[3] = a[3];
  239. if (--dl <= 0) {
  240. break;
  241. }
  242. a += 4;
  243. r += 4;
  244. }
  245. }
  246. }
  247. return c;
  248. }
  249. #else
  250. /* On other platforms the function is defined in asm. */
  251. BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  252. int cl, int dl);
  253. #endif
  254. /* Karatsuba recursive multiplication algorithm
  255. * (cf. Knuth, The Art of Computer Programming, Vol. 2) */
  256. /* r is 2*n2 words in size,
  257. * a and b are both n2 words in size.
  258. * n2 must be a power of 2.
  259. * We multiply and return the result.
  260. * t must be 2*n2 words in size
  261. * We calculate
  262. * a[0]*b[0]
  263. * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
  264. * a[1]*b[1]
  265. */
  266. /* dnX may not be positive, but n2/2+dnX has to be */
  267. static void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
  268. int dna, int dnb, BN_ULONG *t) {
  269. int n = n2 / 2, c1, c2;
  270. int tna = n + dna, tnb = n + dnb;
  271. unsigned int neg, zero;
  272. BN_ULONG ln, lo, *p;
  273. /* Only call bn_mul_comba 8 if n2 == 8 and the
  274. * two arrays are complete [steve]
  275. */
  276. if (n2 == 8 && dna == 0 && dnb == 0) {
  277. bn_mul_comba8(r, a, b);
  278. return;
  279. }
  280. /* Else do normal multiply */
  281. if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
  282. bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
  283. if ((dna + dnb) < 0) {
  284. OPENSSL_memset(&r[2 * n2 + dna + dnb], 0,
  285. sizeof(BN_ULONG) * -(dna + dnb));
  286. }
  287. return;
  288. }
  289. /* r=(a[0]-a[1])*(b[1]-b[0]) */
  290. c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
  291. c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
  292. zero = neg = 0;
  293. switch (c1 * 3 + c2) {
  294. case -4:
  295. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  296. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  297. break;
  298. case -3:
  299. zero = 1;
  300. break;
  301. case -2:
  302. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  303. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
  304. neg = 1;
  305. break;
  306. case -1:
  307. case 0:
  308. case 1:
  309. zero = 1;
  310. break;
  311. case 2:
  312. bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
  313. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  314. neg = 1;
  315. break;
  316. case 3:
  317. zero = 1;
  318. break;
  319. case 4:
  320. bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
  321. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
  322. break;
  323. }
  324. if (n == 4 && dna == 0 && dnb == 0) {
  325. /* XXX: bn_mul_comba4 could take extra args to do this well */
  326. if (!zero) {
  327. bn_mul_comba4(&(t[n2]), t, &(t[n]));
  328. } else {
  329. OPENSSL_memset(&(t[n2]), 0, 8 * sizeof(BN_ULONG));
  330. }
  331. bn_mul_comba4(r, a, b);
  332. bn_mul_comba4(&(r[n2]), &(a[n]), &(b[n]));
  333. } else if (n == 8 && dna == 0 && dnb == 0) {
  334. /* XXX: bn_mul_comba8 could take extra args to do this well */
  335. if (!zero) {
  336. bn_mul_comba8(&(t[n2]), t, &(t[n]));
  337. } else {
  338. OPENSSL_memset(&(t[n2]), 0, 16 * sizeof(BN_ULONG));
  339. }
  340. bn_mul_comba8(r, a, b);
  341. bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));
  342. } else {
  343. p = &(t[n2 * 2]);
  344. if (!zero) {
  345. bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
  346. } else {
  347. OPENSSL_memset(&(t[n2]), 0, n2 * sizeof(BN_ULONG));
  348. }
  349. bn_mul_recursive(r, a, b, n, 0, 0, p);
  350. bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p);
  351. }
  352. /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
  353. * r[10] holds (a[0]*b[0])
  354. * r[32] holds (b[1]*b[1]) */
  355. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  356. if (neg) {
  357. /* if t[32] is negative */
  358. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  359. } else {
  360. /* Might have a carry */
  361. c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
  362. }
  363. /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
  364. * r[10] holds (a[0]*b[0])
  365. * r[32] holds (b[1]*b[1])
  366. * c1 holds the carry bits */
  367. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  368. if (c1) {
  369. p = &(r[n + n2]);
  370. lo = *p;
  371. ln = (lo + c1) & BN_MASK2;
  372. *p = ln;
  373. /* The overflow will stop before we over write
  374. * words we should not overwrite */
  375. if (ln < (BN_ULONG)c1) {
  376. do {
  377. p++;
  378. lo = *p;
  379. ln = (lo + 1) & BN_MASK2;
  380. *p = ln;
  381. } while (ln == 0);
  382. }
  383. }
  384. }
  385. /* n+tn is the word length
  386. * t needs to be n*4 is size, as does r */
  387. /* tnX may not be negative but less than n */
  388. static void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,
  389. int tna, int tnb, BN_ULONG *t) {
  390. int i, j, n2 = n * 2;
  391. int c1, c2, neg;
  392. BN_ULONG ln, lo, *p;
  393. if (n < 8) {
  394. bn_mul_normal(r, a, n + tna, b, n + tnb);
  395. return;
  396. }
  397. /* r=(a[0]-a[1])*(b[1]-b[0]) */
  398. c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
  399. c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
  400. neg = 0;
  401. switch (c1 * 3 + c2) {
  402. case -4:
  403. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  404. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  405. break;
  406. case -3:
  407. /* break; */
  408. case -2:
  409. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  410. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
  411. neg = 1;
  412. break;
  413. case -1:
  414. case 0:
  415. case 1:
  416. /* break; */
  417. case 2:
  418. bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
  419. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  420. neg = 1;
  421. break;
  422. case 3:
  423. /* break; */
  424. case 4:
  425. bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
  426. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
  427. break;
  428. }
  429. if (n == 8) {
  430. bn_mul_comba8(&(t[n2]), t, &(t[n]));
  431. bn_mul_comba8(r, a, b);
  432. bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
  433. OPENSSL_memset(&(r[n2 + tna + tnb]), 0, sizeof(BN_ULONG) * (n2 - tna - tnb));
  434. } else {
  435. p = &(t[n2 * 2]);
  436. bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
  437. bn_mul_recursive(r, a, b, n, 0, 0, p);
  438. i = n / 2;
  439. /* If there is only a bottom half to the number,
  440. * just do it */
  441. if (tna > tnb) {
  442. j = tna - i;
  443. } else {
  444. j = tnb - i;
  445. }
  446. if (j == 0) {
  447. bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i, tnb - i, p);
  448. OPENSSL_memset(&(r[n2 + i * 2]), 0, sizeof(BN_ULONG) * (n2 - i * 2));
  449. } else if (j > 0) {
  450. /* eg, n == 16, i == 8 and tn == 11 */
  451. bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i, tnb - i, p);
  452. OPENSSL_memset(&(r[n2 + tna + tnb]), 0,
  453. sizeof(BN_ULONG) * (n2 - tna - tnb));
  454. } else {
  455. /* (j < 0) eg, n == 16, i == 8 and tn == 5 */
  456. OPENSSL_memset(&(r[n2]), 0, sizeof(BN_ULONG) * n2);
  457. if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL &&
  458. tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {
  459. bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
  460. } else {
  461. for (;;) {
  462. i /= 2;
  463. /* these simplified conditions work
  464. * exclusively because difference
  465. * between tna and tnb is 1 or 0 */
  466. if (i < tna || i < tnb) {
  467. bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i,
  468. tnb - i, p);
  469. break;
  470. } else if (i == tna || i == tnb) {
  471. bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i, tnb - i,
  472. p);
  473. break;
  474. }
  475. }
  476. }
  477. }
  478. }
  479. /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
  480. * r[10] holds (a[0]*b[0])
  481. * r[32] holds (b[1]*b[1])
  482. */
  483. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  484. if (neg) {
  485. /* if t[32] is negative */
  486. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  487. } else {
  488. /* Might have a carry */
  489. c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
  490. }
  491. /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
  492. * r[10] holds (a[0]*b[0])
  493. * r[32] holds (b[1]*b[1])
  494. * c1 holds the carry bits */
  495. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  496. if (c1) {
  497. p = &(r[n + n2]);
  498. lo = *p;
  499. ln = (lo + c1) & BN_MASK2;
  500. *p = ln;
  501. /* The overflow will stop before we over write
  502. * words we should not overwrite */
  503. if (ln < (BN_ULONG)c1) {
  504. do {
  505. p++;
  506. lo = *p;
  507. ln = (lo + 1) & BN_MASK2;
  508. *p = ln;
  509. } while (ln == 0);
  510. }
  511. }
  512. }
  513. int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
  514. int ret = 0;
  515. int top, al, bl;
  516. BIGNUM *rr;
  517. int i;
  518. BIGNUM *t = NULL;
  519. int j = 0, k;
  520. al = a->top;
  521. bl = b->top;
  522. if ((al == 0) || (bl == 0)) {
  523. BN_zero(r);
  524. return 1;
  525. }
  526. top = al + bl;
  527. BN_CTX_start(ctx);
  528. if ((r == a) || (r == b)) {
  529. if ((rr = BN_CTX_get(ctx)) == NULL) {
  530. goto err;
  531. }
  532. } else {
  533. rr = r;
  534. }
  535. rr->neg = a->neg ^ b->neg;
  536. i = al - bl;
  537. if (i == 0) {
  538. if (al == 8) {
  539. if (bn_wexpand(rr, 16) == NULL) {
  540. goto err;
  541. }
  542. rr->top = 16;
  543. bn_mul_comba8(rr->d, a->d, b->d);
  544. goto end;
  545. }
  546. }
  547. static const int kMulNormalSize = 16;
  548. if (al >= kMulNormalSize && bl >= kMulNormalSize) {
  549. if (i >= -1 && i <= 1) {
  550. /* Find out the power of two lower or equal
  551. to the longest of the two numbers */
  552. if (i >= 0) {
  553. j = BN_num_bits_word((BN_ULONG)al);
  554. }
  555. if (i == -1) {
  556. j = BN_num_bits_word((BN_ULONG)bl);
  557. }
  558. j = 1 << (j - 1);
  559. assert(j <= al || j <= bl);
  560. k = j + j;
  561. t = BN_CTX_get(ctx);
  562. if (t == NULL) {
  563. goto err;
  564. }
  565. if (al > j || bl > j) {
  566. if (bn_wexpand(t, k * 4) == NULL) {
  567. goto err;
  568. }
  569. if (bn_wexpand(rr, k * 4) == NULL) {
  570. goto err;
  571. }
  572. bn_mul_part_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
  573. } else {
  574. /* al <= j || bl <= j */
  575. if (bn_wexpand(t, k * 2) == NULL) {
  576. goto err;
  577. }
  578. if (bn_wexpand(rr, k * 2) == NULL) {
  579. goto err;
  580. }
  581. bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
  582. }
  583. rr->top = top;
  584. goto end;
  585. }
  586. }
  587. if (bn_wexpand(rr, top) == NULL) {
  588. goto err;
  589. }
  590. rr->top = top;
  591. bn_mul_normal(rr->d, a->d, al, b->d, bl);
  592. end:
  593. bn_correct_top(rr);
  594. if (r != rr && !BN_copy(r, rr)) {
  595. goto err;
  596. }
  597. ret = 1;
  598. err:
  599. BN_CTX_end(ctx);
  600. return ret;
  601. }
  602. /* tmp must have 2*n words */
  603. static void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) {
  604. int i, j, max;
  605. const BN_ULONG *ap;
  606. BN_ULONG *rp;
  607. max = n * 2;
  608. ap = a;
  609. rp = r;
  610. rp[0] = rp[max - 1] = 0;
  611. rp++;
  612. j = n;
  613. if (--j > 0) {
  614. ap++;
  615. rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
  616. rp += 2;
  617. }
  618. for (i = n - 2; i > 0; i--) {
  619. j--;
  620. ap++;
  621. rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
  622. rp += 2;
  623. }
  624. bn_add_words(r, r, r, max);
  625. /* There will not be a carry */
  626. bn_sqr_words(tmp, a, n);
  627. bn_add_words(r, r, tmp, max);
  628. }
  629. /* r is 2*n words in size,
  630. * a and b are both n words in size. (There's not actually a 'b' here ...)
  631. * n must be a power of 2.
  632. * We multiply and return the result.
  633. * t must be 2*n words in size
  634. * We calculate
  635. * a[0]*b[0]
  636. * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
  637. * a[1]*b[1]
  638. */
  639. static void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t) {
  640. int n = n2 / 2;
  641. int zero, c1;
  642. BN_ULONG ln, lo, *p;
  643. if (n2 == 4) {
  644. bn_sqr_comba4(r, a);
  645. return;
  646. } else if (n2 == 8) {
  647. bn_sqr_comba8(r, a);
  648. return;
  649. }
  650. if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) {
  651. bn_sqr_normal(r, a, n2, t);
  652. return;
  653. }
  654. /* r=(a[0]-a[1])*(a[1]-a[0]) */
  655. c1 = bn_cmp_words(a, &(a[n]), n);
  656. zero = 0;
  657. if (c1 > 0) {
  658. bn_sub_words(t, a, &(a[n]), n);
  659. } else if (c1 < 0) {
  660. bn_sub_words(t, &(a[n]), a, n);
  661. } else {
  662. zero = 1;
  663. }
  664. /* The result will always be negative unless it is zero */
  665. p = &(t[n2 * 2]);
  666. if (!zero) {
  667. bn_sqr_recursive(&(t[n2]), t, n, p);
  668. } else {
  669. OPENSSL_memset(&(t[n2]), 0, n2 * sizeof(BN_ULONG));
  670. }
  671. bn_sqr_recursive(r, a, n, p);
  672. bn_sqr_recursive(&(r[n2]), &(a[n]), n, p);
  673. /* t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero
  674. * r[10] holds (a[0]*b[0])
  675. * r[32] holds (b[1]*b[1]) */
  676. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  677. /* t[32] is negative */
  678. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  679. /* t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1])
  680. * r[10] holds (a[0]*a[0])
  681. * r[32] holds (a[1]*a[1])
  682. * c1 holds the carry bits */
  683. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  684. if (c1) {
  685. p = &(r[n + n2]);
  686. lo = *p;
  687. ln = (lo + c1) & BN_MASK2;
  688. *p = ln;
  689. /* The overflow will stop before we over write
  690. * words we should not overwrite */
  691. if (ln < (BN_ULONG)c1) {
  692. do {
  693. p++;
  694. lo = *p;
  695. ln = (lo + 1) & BN_MASK2;
  696. *p = ln;
  697. } while (ln == 0);
  698. }
  699. }
  700. }
  701. int BN_mul_word(BIGNUM *bn, BN_ULONG w) {
  702. BN_ULONG ll;
  703. w &= BN_MASK2;
  704. if (!bn->top) {
  705. return 1;
  706. }
  707. if (w == 0) {
  708. BN_zero(bn);
  709. return 1;
  710. }
  711. ll = bn_mul_words(bn->d, bn->d, bn->top, w);
  712. if (ll) {
  713. if (bn_wexpand(bn, bn->top + 1) == NULL) {
  714. return 0;
  715. }
  716. bn->d[bn->top++] = ll;
  717. }
  718. return 1;
  719. }
  720. int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) {
  721. int max, al;
  722. int ret = 0;
  723. BIGNUM *tmp, *rr;
  724. al = a->top;
  725. if (al <= 0) {
  726. r->top = 0;
  727. r->neg = 0;
  728. return 1;
  729. }
  730. BN_CTX_start(ctx);
  731. rr = (a != r) ? r : BN_CTX_get(ctx);
  732. tmp = BN_CTX_get(ctx);
  733. if (!rr || !tmp) {
  734. goto err;
  735. }
  736. max = 2 * al; /* Non-zero (from above) */
  737. if (bn_wexpand(rr, max) == NULL) {
  738. goto err;
  739. }
  740. if (al == 4) {
  741. bn_sqr_comba4(rr->d, a->d);
  742. } else if (al == 8) {
  743. bn_sqr_comba8(rr->d, a->d);
  744. } else {
  745. if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {
  746. BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];
  747. bn_sqr_normal(rr->d, a->d, al, t);
  748. } else {
  749. int j, k;
  750. j = BN_num_bits_word((BN_ULONG)al);
  751. j = 1 << (j - 1);
  752. k = j + j;
  753. if (al == j) {
  754. if (bn_wexpand(tmp, k * 2) == NULL) {
  755. goto err;
  756. }
  757. bn_sqr_recursive(rr->d, a->d, al, tmp->d);
  758. } else {
  759. if (bn_wexpand(tmp, max) == NULL) {
  760. goto err;
  761. }
  762. bn_sqr_normal(rr->d, a->d, al, tmp->d);
  763. }
  764. }
  765. }
  766. rr->neg = 0;
  767. /* If the most-significant half of the top word of 'a' is zero, then
  768. * the square of 'a' will max-1 words. */
  769. if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l)) {
  770. rr->top = max - 1;
  771. } else {
  772. rr->top = max;
  773. }
  774. if (rr != r && !BN_copy(r, rr)) {
  775. goto err;
  776. }
  777. ret = 1;
  778. err:
  779. BN_CTX_end(ctx);
  780. return ret;
  781. }