Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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