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ů.
 
 
 
 
 
 

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