Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

mul.c 21 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. memset(&r[2 * n2 + dna + dnb], 0, sizeof(BN_ULONG) * -(dna + dnb));
  285. }
  286. return;
  287. }
  288. /* r=(a[0]-a[1])*(b[1]-b[0]) */
  289. c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
  290. c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
  291. zero = neg = 0;
  292. switch (c1 * 3 + c2) {
  293. case -4:
  294. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  295. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  296. break;
  297. case -3:
  298. zero = 1;
  299. break;
  300. case -2:
  301. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  302. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
  303. neg = 1;
  304. break;
  305. case -1:
  306. case 0:
  307. case 1:
  308. zero = 1;
  309. break;
  310. case 2:
  311. bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
  312. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  313. neg = 1;
  314. break;
  315. case 3:
  316. zero = 1;
  317. break;
  318. case 4:
  319. bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
  320. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
  321. break;
  322. }
  323. if (n == 4 && dna == 0 && dnb == 0) {
  324. /* XXX: bn_mul_comba4 could take extra args to do this well */
  325. if (!zero) {
  326. bn_mul_comba4(&(t[n2]), t, &(t[n]));
  327. } else {
  328. memset(&(t[n2]), 0, 8 * sizeof(BN_ULONG));
  329. }
  330. bn_mul_comba4(r, a, b);
  331. bn_mul_comba4(&(r[n2]), &(a[n]), &(b[n]));
  332. } else if (n == 8 && dna == 0 && dnb == 0) {
  333. /* XXX: bn_mul_comba8 could take extra args to do this well */
  334. if (!zero) {
  335. bn_mul_comba8(&(t[n2]), t, &(t[n]));
  336. } else {
  337. memset(&(t[n2]), 0, 16 * sizeof(BN_ULONG));
  338. }
  339. bn_mul_comba8(r, a, b);
  340. bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));
  341. } else {
  342. p = &(t[n2 * 2]);
  343. if (!zero) {
  344. bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
  345. } else {
  346. memset(&(t[n2]), 0, n2 * sizeof(BN_ULONG));
  347. }
  348. bn_mul_recursive(r, a, b, n, 0, 0, p);
  349. bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p);
  350. }
  351. /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
  352. * r[10] holds (a[0]*b[0])
  353. * r[32] holds (b[1]*b[1]) */
  354. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  355. if (neg) {
  356. /* if t[32] is negative */
  357. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  358. } else {
  359. /* Might have a carry */
  360. c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
  361. }
  362. /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
  363. * r[10] holds (a[0]*b[0])
  364. * r[32] holds (b[1]*b[1])
  365. * c1 holds the carry bits */
  366. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  367. if (c1) {
  368. p = &(r[n + n2]);
  369. lo = *p;
  370. ln = (lo + c1) & BN_MASK2;
  371. *p = ln;
  372. /* The overflow will stop before we over write
  373. * words we should not overwrite */
  374. if (ln < (BN_ULONG)c1) {
  375. do {
  376. p++;
  377. lo = *p;
  378. ln = (lo + 1) & BN_MASK2;
  379. *p = ln;
  380. } while (ln == 0);
  381. }
  382. }
  383. }
  384. /* n+tn is the word length
  385. * t needs to be n*4 is size, as does r */
  386. /* tnX may not be negative but less than n */
  387. static void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,
  388. int tna, int tnb, BN_ULONG *t) {
  389. int i, j, n2 = n * 2;
  390. int c1, c2, neg;
  391. BN_ULONG ln, lo, *p;
  392. if (n < 8) {
  393. bn_mul_normal(r, a, n + tna, b, n + tnb);
  394. return;
  395. }
  396. /* r=(a[0]-a[1])*(b[1]-b[0]) */
  397. c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
  398. c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
  399. neg = 0;
  400. switch (c1 * 3 + c2) {
  401. case -4:
  402. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  403. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  404. break;
  405. case -3:
  406. /* break; */
  407. case -2:
  408. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  409. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
  410. neg = 1;
  411. break;
  412. case -1:
  413. case 0:
  414. case 1:
  415. /* break; */
  416. case 2:
  417. bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
  418. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  419. neg = 1;
  420. break;
  421. case 3:
  422. /* break; */
  423. case 4:
  424. bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
  425. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
  426. break;
  427. }
  428. if (n == 8) {
  429. bn_mul_comba8(&(t[n2]), t, &(t[n]));
  430. bn_mul_comba8(r, a, b);
  431. bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
  432. memset(&(r[n2 + tna + tnb]), 0, sizeof(BN_ULONG) * (n2 - tna - tnb));
  433. } else {
  434. p = &(t[n2 * 2]);
  435. bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
  436. bn_mul_recursive(r, a, b, n, 0, 0, p);
  437. i = n / 2;
  438. /* If there is only a bottom half to the number,
  439. * just do it */
  440. if (tna > tnb) {
  441. j = tna - i;
  442. } else {
  443. j = tnb - i;
  444. }
  445. if (j == 0) {
  446. bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i, tnb - i, p);
  447. memset(&(r[n2 + i * 2]), 0, sizeof(BN_ULONG) * (n2 - i * 2));
  448. } else if (j > 0) {
  449. /* eg, n == 16, i == 8 and tn == 11 */
  450. bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i, tnb - i, p);
  451. memset(&(r[n2 + tna + tnb]), 0, sizeof(BN_ULONG) * (n2 - tna - tnb));
  452. } else {
  453. /* (j < 0) eg, n == 16, i == 8 and tn == 5 */
  454. memset(&(r[n2]), 0, sizeof(BN_ULONG) * n2);
  455. if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL &&
  456. tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {
  457. bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
  458. } else {
  459. for (;;) {
  460. i /= 2;
  461. /* these simplified conditions work
  462. * exclusively because difference
  463. * between tna and tnb is 1 or 0 */
  464. if (i < tna || i < tnb) {
  465. bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i,
  466. tnb - i, p);
  467. break;
  468. } else if (i == tna || i == tnb) {
  469. bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i, tnb - i,
  470. p);
  471. break;
  472. }
  473. }
  474. }
  475. }
  476. }
  477. /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
  478. * r[10] holds (a[0]*b[0])
  479. * r[32] holds (b[1]*b[1])
  480. */
  481. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  482. if (neg) {
  483. /* if t[32] is negative */
  484. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  485. } else {
  486. /* Might have a carry */
  487. c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
  488. }
  489. /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
  490. * r[10] holds (a[0]*b[0])
  491. * r[32] holds (b[1]*b[1])
  492. * c1 holds the carry bits */
  493. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  494. if (c1) {
  495. p = &(r[n + n2]);
  496. lo = *p;
  497. ln = (lo + c1) & BN_MASK2;
  498. *p = ln;
  499. /* The overflow will stop before we over write
  500. * words we should not overwrite */
  501. if (ln < (BN_ULONG)c1) {
  502. do {
  503. p++;
  504. lo = *p;
  505. ln = (lo + 1) & BN_MASK2;
  506. *p = ln;
  507. } while (ln == 0);
  508. }
  509. }
  510. }
  511. int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
  512. int ret = 0;
  513. int top, al, bl;
  514. BIGNUM *rr;
  515. int i;
  516. BIGNUM *t = NULL;
  517. int j = 0, k;
  518. al = a->top;
  519. bl = b->top;
  520. if ((al == 0) || (bl == 0)) {
  521. BN_zero(r);
  522. return 1;
  523. }
  524. top = al + bl;
  525. BN_CTX_start(ctx);
  526. if ((r == a) || (r == b)) {
  527. if ((rr = BN_CTX_get(ctx)) == NULL) {
  528. goto err;
  529. }
  530. } else {
  531. rr = r;
  532. }
  533. rr->neg = a->neg ^ b->neg;
  534. i = al - bl;
  535. if (i == 0) {
  536. if (al == 8) {
  537. if (bn_wexpand(rr, 16) == NULL) {
  538. goto err;
  539. }
  540. rr->top = 16;
  541. bn_mul_comba8(rr->d, a->d, b->d);
  542. goto end;
  543. }
  544. }
  545. static const int kMulNormalSize = 16;
  546. if (al >= kMulNormalSize && bl >= kMulNormalSize) {
  547. if (i >= -1 && i <= 1) {
  548. /* Find out the power of two lower or equal
  549. to the longest of the two numbers */
  550. if (i >= 0) {
  551. j = BN_num_bits_word((BN_ULONG)al);
  552. }
  553. if (i == -1) {
  554. j = BN_num_bits_word((BN_ULONG)bl);
  555. }
  556. j = 1 << (j - 1);
  557. assert(j <= al || j <= bl);
  558. k = j + j;
  559. t = BN_CTX_get(ctx);
  560. if (t == NULL) {
  561. goto err;
  562. }
  563. if (al > j || bl > j) {
  564. if (bn_wexpand(t, k * 4) == NULL) {
  565. goto err;
  566. }
  567. if (bn_wexpand(rr, k * 4) == NULL) {
  568. goto err;
  569. }
  570. bn_mul_part_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
  571. } else {
  572. /* al <= j || bl <= j */
  573. if (bn_wexpand(t, k * 2) == NULL) {
  574. goto err;
  575. }
  576. if (bn_wexpand(rr, k * 2) == NULL) {
  577. goto err;
  578. }
  579. bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
  580. }
  581. rr->top = top;
  582. goto end;
  583. }
  584. }
  585. if (bn_wexpand(rr, top) == NULL) {
  586. goto err;
  587. }
  588. rr->top = top;
  589. bn_mul_normal(rr->d, a->d, al, b->d, bl);
  590. end:
  591. bn_correct_top(rr);
  592. if (r != rr && !BN_copy(r, rr)) {
  593. goto err;
  594. }
  595. ret = 1;
  596. err:
  597. BN_CTX_end(ctx);
  598. return ret;
  599. }
  600. /* tmp must have 2*n words */
  601. static void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) {
  602. int i, j, max;
  603. const BN_ULONG *ap;
  604. BN_ULONG *rp;
  605. max = n * 2;
  606. ap = a;
  607. rp = r;
  608. rp[0] = rp[max - 1] = 0;
  609. rp++;
  610. j = n;
  611. if (--j > 0) {
  612. ap++;
  613. rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
  614. rp += 2;
  615. }
  616. for (i = n - 2; i > 0; i--) {
  617. j--;
  618. ap++;
  619. rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
  620. rp += 2;
  621. }
  622. bn_add_words(r, r, r, max);
  623. /* There will not be a carry */
  624. bn_sqr_words(tmp, a, n);
  625. bn_add_words(r, r, tmp, max);
  626. }
  627. /* r is 2*n words in size,
  628. * a and b are both n words in size. (There's not actually a 'b' here ...)
  629. * n must be a power of 2.
  630. * We multiply and return the result.
  631. * t must be 2*n words in size
  632. * We calculate
  633. * a[0]*b[0]
  634. * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
  635. * a[1]*b[1]
  636. */
  637. static void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t) {
  638. int n = n2 / 2;
  639. int zero, c1;
  640. BN_ULONG ln, lo, *p;
  641. if (n2 == 4) {
  642. bn_sqr_comba4(r, a);
  643. return;
  644. } else if (n2 == 8) {
  645. bn_sqr_comba8(r, a);
  646. return;
  647. }
  648. if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) {
  649. bn_sqr_normal(r, a, n2, t);
  650. return;
  651. }
  652. /* r=(a[0]-a[1])*(a[1]-a[0]) */
  653. c1 = bn_cmp_words(a, &(a[n]), n);
  654. zero = 0;
  655. if (c1 > 0) {
  656. bn_sub_words(t, a, &(a[n]), n);
  657. } else if (c1 < 0) {
  658. bn_sub_words(t, &(a[n]), a, n);
  659. } else {
  660. zero = 1;
  661. }
  662. /* The result will always be negative unless it is zero */
  663. p = &(t[n2 * 2]);
  664. if (!zero) {
  665. bn_sqr_recursive(&(t[n2]), t, n, p);
  666. } else {
  667. memset(&(t[n2]), 0, n2 * sizeof(BN_ULONG));
  668. }
  669. bn_sqr_recursive(r, a, n, p);
  670. bn_sqr_recursive(&(r[n2]), &(a[n]), n, p);
  671. /* t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero
  672. * r[10] holds (a[0]*b[0])
  673. * r[32] holds (b[1]*b[1]) */
  674. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  675. /* t[32] is negative */
  676. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  677. /* t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1])
  678. * r[10] holds (a[0]*a[0])
  679. * r[32] holds (a[1]*a[1])
  680. * c1 holds the carry bits */
  681. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  682. if (c1) {
  683. p = &(r[n + n2]);
  684. lo = *p;
  685. ln = (lo + c1) & BN_MASK2;
  686. *p = ln;
  687. /* The overflow will stop before we over write
  688. * words we should not overwrite */
  689. if (ln < (BN_ULONG)c1) {
  690. do {
  691. p++;
  692. lo = *p;
  693. ln = (lo + 1) & BN_MASK2;
  694. *p = ln;
  695. } while (ln == 0);
  696. }
  697. }
  698. }
  699. int BN_mul_word(BIGNUM *bn, BN_ULONG w) {
  700. BN_ULONG ll;
  701. w &= BN_MASK2;
  702. if (!bn->top) {
  703. return 1;
  704. }
  705. if (w == 0) {
  706. BN_zero(bn);
  707. return 1;
  708. }
  709. ll = bn_mul_words(bn->d, bn->d, bn->top, w);
  710. if (ll) {
  711. if (bn_wexpand(bn, bn->top + 1) == NULL) {
  712. return 0;
  713. }
  714. bn->d[bn->top++] = ll;
  715. }
  716. return 1;
  717. }
  718. int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) {
  719. int max, al;
  720. int ret = 0;
  721. BIGNUM *tmp, *rr;
  722. al = a->top;
  723. if (al <= 0) {
  724. r->top = 0;
  725. r->neg = 0;
  726. return 1;
  727. }
  728. BN_CTX_start(ctx);
  729. rr = (a != r) ? r : BN_CTX_get(ctx);
  730. tmp = BN_CTX_get(ctx);
  731. if (!rr || !tmp) {
  732. goto err;
  733. }
  734. max = 2 * al; /* Non-zero (from above) */
  735. if (bn_wexpand(rr, max) == NULL) {
  736. goto err;
  737. }
  738. if (al == 4) {
  739. bn_sqr_comba4(rr->d, a->d);
  740. } else if (al == 8) {
  741. bn_sqr_comba8(rr->d, a->d);
  742. } else {
  743. if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {
  744. BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];
  745. bn_sqr_normal(rr->d, a->d, al, t);
  746. } else {
  747. int j, k;
  748. j = BN_num_bits_word((BN_ULONG)al);
  749. j = 1 << (j - 1);
  750. k = j + j;
  751. if (al == j) {
  752. if (bn_wexpand(tmp, k * 2) == NULL) {
  753. goto err;
  754. }
  755. bn_sqr_recursive(rr->d, a->d, al, tmp->d);
  756. } else {
  757. if (bn_wexpand(tmp, max) == NULL) {
  758. goto err;
  759. }
  760. bn_sqr_normal(rr->d, a->d, al, tmp->d);
  761. }
  762. }
  763. }
  764. rr->neg = 0;
  765. /* If the most-significant half of the top word of 'a' is zero, then
  766. * the square of 'a' will max-1 words. */
  767. if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l)) {
  768. rr->top = max - 1;
  769. } else {
  770. rr->top = max;
  771. }
  772. if (rr != r && !BN_copy(r, rr)) {
  773. goto err;
  774. }
  775. ret = 1;
  776. err:
  777. BN_CTX_end(ctx);
  778. return ret;
  779. }