mul.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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 <openssl/err.h>
  60. #include <openssl/mem.h>
  61. #include "internal.h"
  62. #include "../../internal.h"
  63. #define BN_MUL_RECURSIVE_SIZE_NORMAL 16
  64. #define BN_SQR_RECURSIVE_SIZE_NORMAL BN_MUL_RECURSIVE_SIZE_NORMAL
  65. static void bn_mul_normal(BN_ULONG *r, const BN_ULONG *a, size_t na,
  66. const BN_ULONG *b, size_t nb) {
  67. if (na < nb) {
  68. size_t itmp = na;
  69. na = nb;
  70. nb = itmp;
  71. const BN_ULONG *ltmp = a;
  72. a = b;
  73. b = ltmp;
  74. }
  75. BN_ULONG *rr = &(r[na]);
  76. if (nb == 0) {
  77. OPENSSL_memset(r, 0, na * sizeof(BN_ULONG));
  78. return;
  79. }
  80. rr[0] = bn_mul_words(r, a, na, b[0]);
  81. for (;;) {
  82. if (--nb == 0) {
  83. return;
  84. }
  85. rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
  86. if (--nb == 0) {
  87. return;
  88. }
  89. rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
  90. if (--nb == 0) {
  91. return;
  92. }
  93. rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
  94. if (--nb == 0) {
  95. return;
  96. }
  97. rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
  98. rr += 4;
  99. r += 4;
  100. b += 4;
  101. }
  102. }
  103. #if !defined(OPENSSL_X86) || defined(OPENSSL_NO_ASM)
  104. // Here follows specialised variants of bn_add_words() and bn_sub_words(). They
  105. // have the property performing operations on arrays of different sizes. The
  106. // sizes of those arrays is expressed through cl, which is the common length (
  107. // basicall, min(len(a),len(b)) ), and dl, which is the delta between the two
  108. // lengths, calculated as len(a)-len(b). All lengths are the number of
  109. // BN_ULONGs... For the operations that require a result array as parameter,
  110. // it must have the length cl+abs(dl). These functions should probably end up
  111. // in bn_asm.c as soon as there are assembler counterparts for the systems that
  112. // use assembler files.
  113. static BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a,
  114. const BN_ULONG *b, int cl, int dl) {
  115. BN_ULONG c, t;
  116. assert(cl >= 0);
  117. c = bn_sub_words(r, a, b, cl);
  118. if (dl == 0) {
  119. return c;
  120. }
  121. r += cl;
  122. a += cl;
  123. b += cl;
  124. if (dl < 0) {
  125. for (;;) {
  126. t = b[0];
  127. r[0] = 0 - t - c;
  128. if (t != 0) {
  129. c = 1;
  130. }
  131. if (++dl >= 0) {
  132. break;
  133. }
  134. t = b[1];
  135. r[1] = 0 - t - c;
  136. if (t != 0) {
  137. c = 1;
  138. }
  139. if (++dl >= 0) {
  140. break;
  141. }
  142. t = b[2];
  143. r[2] = 0 - t - c;
  144. if (t != 0) {
  145. c = 1;
  146. }
  147. if (++dl >= 0) {
  148. break;
  149. }
  150. t = b[3];
  151. r[3] = 0 - t - c;
  152. if (t != 0) {
  153. c = 1;
  154. }
  155. if (++dl >= 0) {
  156. break;
  157. }
  158. b += 4;
  159. r += 4;
  160. }
  161. } else {
  162. int save_dl = dl;
  163. while (c) {
  164. t = a[0];
  165. r[0] = t - c;
  166. if (t != 0) {
  167. c = 0;
  168. }
  169. if (--dl <= 0) {
  170. break;
  171. }
  172. t = a[1];
  173. r[1] = t - c;
  174. if (t != 0) {
  175. c = 0;
  176. }
  177. if (--dl <= 0) {
  178. break;
  179. }
  180. t = a[2];
  181. r[2] = t - c;
  182. if (t != 0) {
  183. c = 0;
  184. }
  185. if (--dl <= 0) {
  186. break;
  187. }
  188. t = a[3];
  189. r[3] = t - c;
  190. if (t != 0) {
  191. c = 0;
  192. }
  193. if (--dl <= 0) {
  194. break;
  195. }
  196. save_dl = dl;
  197. a += 4;
  198. r += 4;
  199. }
  200. if (dl > 0) {
  201. if (save_dl > dl) {
  202. switch (save_dl - dl) {
  203. case 1:
  204. r[1] = a[1];
  205. if (--dl <= 0) {
  206. break;
  207. }
  208. OPENSSL_FALLTHROUGH;
  209. case 2:
  210. r[2] = a[2];
  211. if (--dl <= 0) {
  212. break;
  213. }
  214. OPENSSL_FALLTHROUGH;
  215. case 3:
  216. r[3] = a[3];
  217. if (--dl <= 0) {
  218. break;
  219. }
  220. }
  221. a += 4;
  222. r += 4;
  223. }
  224. }
  225. if (dl > 0) {
  226. for (;;) {
  227. r[0] = a[0];
  228. if (--dl <= 0) {
  229. break;
  230. }
  231. r[1] = a[1];
  232. if (--dl <= 0) {
  233. break;
  234. }
  235. r[2] = a[2];
  236. if (--dl <= 0) {
  237. break;
  238. }
  239. r[3] = a[3];
  240. if (--dl <= 0) {
  241. break;
  242. }
  243. a += 4;
  244. r += 4;
  245. }
  246. }
  247. }
  248. return c;
  249. }
  250. #else
  251. // On other platforms the function is defined in asm.
  252. BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  253. int cl, int dl);
  254. #endif
  255. // Karatsuba recursive multiplication algorithm
  256. // (cf. Knuth, The Art of Computer Programming, Vol. 2)
  257. // r is 2*n2 words in size,
  258. // a and b are both n2 words in size.
  259. // n2 must be a power of 2.
  260. // We multiply and return the result.
  261. // t must be 2*n2 words in size
  262. // We calculate
  263. // a[0]*b[0]
  264. // a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
  265. // a[1]*b[1]
  266. // dnX may not be positive, but n2/2+dnX has to be
  267. static void bn_mul_recursive(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  268. int n2, 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. if (n2 == 8 && dna == 0 && dnb == 0) {
  276. bn_mul_comba8(r, a, b);
  277. return;
  278. }
  279. // Else do normal multiply
  280. if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
  281. bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
  282. if ((dna + dnb) < 0) {
  283. OPENSSL_memset(&r[2 * n2 + dna + dnb], 0,
  284. 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. OPENSSL_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. OPENSSL_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. OPENSSL_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;
  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;
  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, const BN_ULONG *a,
  388. const BN_ULONG *b, int n, int tna, int tnb,
  389. 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. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  483. if (neg) {
  484. // if t[32] is negative
  485. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  486. } else {
  487. // Might have a carry
  488. c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
  489. }
  490. // t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
  491. // r[10] holds (a[0]*b[0])
  492. // r[32] holds (b[1]*b[1])
  493. // c1 holds the carry bits
  494. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  495. if (c1) {
  496. p = &(r[n + n2]);
  497. lo = *p;
  498. ln = lo + c1;
  499. *p = ln;
  500. // The overflow will stop before we over write
  501. // words we should not overwrite
  502. if (ln < (BN_ULONG)c1) {
  503. do {
  504. p++;
  505. lo = *p;
  506. ln = lo + 1;
  507. *p = ln;
  508. } while (ln == 0);
  509. }
  510. }
  511. }
  512. int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
  513. int ret = 0;
  514. int top, al, bl;
  515. BIGNUM *rr;
  516. int i;
  517. BIGNUM *t = NULL;
  518. int j = 0, k;
  519. al = a->top;
  520. bl = b->top;
  521. if ((al == 0) || (bl == 0)) {
  522. BN_zero(r);
  523. return 1;
  524. }
  525. top = al + bl;
  526. BN_CTX_start(ctx);
  527. if ((r == a) || (r == b)) {
  528. if ((rr = BN_CTX_get(ctx)) == NULL) {
  529. goto err;
  530. }
  531. } else {
  532. rr = r;
  533. }
  534. rr->neg = a->neg ^ b->neg;
  535. i = al - bl;
  536. if (i == 0) {
  537. if (al == 8) {
  538. if (!bn_wexpand(rr, 16)) {
  539. goto err;
  540. }
  541. rr->top = 16;
  542. bn_mul_comba8(rr->d, a->d, b->d);
  543. goto end;
  544. }
  545. }
  546. static const int kMulNormalSize = 16;
  547. if (al >= kMulNormalSize && bl >= kMulNormalSize) {
  548. if (i >= -1 && i <= 1) {
  549. /* Find out the power of two lower or equal
  550. to the longest of the two numbers */
  551. if (i >= 0) {
  552. j = BN_num_bits_word((BN_ULONG)al);
  553. }
  554. if (i == -1) {
  555. j = BN_num_bits_word((BN_ULONG)bl);
  556. }
  557. j = 1 << (j - 1);
  558. assert(j <= al || j <= bl);
  559. k = j + j;
  560. t = BN_CTX_get(ctx);
  561. if (t == NULL) {
  562. goto err;
  563. }
  564. if (al > j || bl > j) {
  565. if (!bn_wexpand(t, k * 4)) {
  566. goto err;
  567. }
  568. if (!bn_wexpand(rr, k * 4)) {
  569. goto err;
  570. }
  571. bn_mul_part_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
  572. } else {
  573. // al <= j || bl <= j
  574. if (!bn_wexpand(t, k * 2)) {
  575. goto err;
  576. }
  577. if (!bn_wexpand(rr, k * 2)) {
  578. goto err;
  579. }
  580. bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
  581. }
  582. rr->top = top;
  583. goto end;
  584. }
  585. }
  586. if (!bn_wexpand(rr, top)) {
  587. goto err;
  588. }
  589. rr->top = top;
  590. bn_mul_normal(rr->d, a->d, al, b->d, bl);
  591. end:
  592. bn_correct_top(rr);
  593. if (r != rr && !BN_copy(r, rr)) {
  594. goto err;
  595. }
  596. ret = 1;
  597. err:
  598. BN_CTX_end(ctx);
  599. return ret;
  600. }
  601. int bn_mul_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a,
  602. const BN_ULONG *b, size_t num_b) {
  603. if (num_r != num_a + num_b) {
  604. OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  605. return 0;
  606. }
  607. // TODO(davidben): Should this call |bn_mul_comba4| too? |BN_mul| does not
  608. // hit that code.
  609. if (num_a == 8 && num_b == 8) {
  610. bn_mul_comba8(r, a, b);
  611. } else {
  612. bn_mul_normal(r, a, num_a, b, num_b);
  613. }
  614. return 1;
  615. }
  616. // tmp must have 2*n words
  617. static void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, size_t n,
  618. BN_ULONG *tmp) {
  619. if (n == 0) {
  620. return;
  621. }
  622. size_t max = n * 2;
  623. const BN_ULONG *ap = a;
  624. BN_ULONG *rp = r;
  625. rp[0] = rp[max - 1] = 0;
  626. rp++;
  627. // Compute the contribution of a[i] * a[j] for all i < j.
  628. if (n > 1) {
  629. ap++;
  630. rp[n - 1] = bn_mul_words(rp, ap, n - 1, ap[-1]);
  631. rp += 2;
  632. }
  633. if (n > 2) {
  634. for (size_t i = n - 2; i > 0; i--) {
  635. ap++;
  636. rp[i] = bn_mul_add_words(rp, ap, i, ap[-1]);
  637. rp += 2;
  638. }
  639. }
  640. // The final result fits in |max| words, so none of the following operations
  641. // will overflow.
  642. // Double |r|, giving the contribution of a[i] * a[j] for all i != j.
  643. bn_add_words(r, r, r, max);
  644. // Add in the contribution of a[i] * a[i] for all i.
  645. bn_sqr_words(tmp, a, n);
  646. bn_add_words(r, r, tmp, max);
  647. }
  648. // r is 2*n words in size,
  649. // a and b are both n words in size. (There's not actually a 'b' here ...)
  650. // n must be a power of 2.
  651. // We multiply and return the result.
  652. // t must be 2*n words in size
  653. // We calculate
  654. // a[0]*b[0]
  655. // a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
  656. // a[1]*b[1]
  657. static void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2,
  658. BN_ULONG *t) {
  659. int n = n2 / 2;
  660. int zero, c1;
  661. BN_ULONG ln, lo, *p;
  662. if (n2 == 4) {
  663. bn_sqr_comba4(r, a);
  664. return;
  665. } else if (n2 == 8) {
  666. bn_sqr_comba8(r, a);
  667. return;
  668. }
  669. if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) {
  670. bn_sqr_normal(r, a, n2, t);
  671. return;
  672. }
  673. // r=(a[0]-a[1])*(a[1]-a[0])
  674. c1 = bn_cmp_words(a, &(a[n]), n);
  675. zero = 0;
  676. if (c1 > 0) {
  677. bn_sub_words(t, a, &(a[n]), n);
  678. } else if (c1 < 0) {
  679. bn_sub_words(t, &(a[n]), a, n);
  680. } else {
  681. zero = 1;
  682. }
  683. // The result will always be negative unless it is zero
  684. p = &(t[n2 * 2]);
  685. if (!zero) {
  686. bn_sqr_recursive(&(t[n2]), t, n, p);
  687. } else {
  688. OPENSSL_memset(&(t[n2]), 0, n2 * sizeof(BN_ULONG));
  689. }
  690. bn_sqr_recursive(r, a, n, p);
  691. bn_sqr_recursive(&(r[n2]), &(a[n]), n, p);
  692. // t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero
  693. // r[10] holds (a[0]*b[0])
  694. // r[32] holds (b[1]*b[1])
  695. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  696. // t[32] is negative
  697. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  698. // t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1])
  699. // r[10] holds (a[0]*a[0])
  700. // r[32] holds (a[1]*a[1])
  701. // c1 holds the carry bits
  702. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  703. if (c1) {
  704. p = &(r[n + n2]);
  705. lo = *p;
  706. ln = lo + c1;
  707. *p = ln;
  708. // The overflow will stop before we over write
  709. // words we should not overwrite
  710. if (ln < (BN_ULONG)c1) {
  711. do {
  712. p++;
  713. lo = *p;
  714. ln = lo + 1;
  715. *p = ln;
  716. } while (ln == 0);
  717. }
  718. }
  719. }
  720. int BN_mul_word(BIGNUM *bn, BN_ULONG w) {
  721. if (!bn->top) {
  722. return 1;
  723. }
  724. if (w == 0) {
  725. BN_zero(bn);
  726. return 1;
  727. }
  728. BN_ULONG ll = bn_mul_words(bn->d, bn->d, bn->top, w);
  729. if (ll) {
  730. if (!bn_wexpand(bn, bn->top + 1)) {
  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)) {
  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)) {
  772. goto err;
  773. }
  774. bn_sqr_recursive(rr->d, a->d, al, tmp->d);
  775. } else {
  776. if (!bn_wexpand(tmp, max)) {
  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 && !BN_copy(r, rr)) {
  792. goto err;
  793. }
  794. ret = 1;
  795. err:
  796. BN_CTX_end(ctx);
  797. return ret;
  798. }
  799. int bn_sqr_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a) {
  800. if (num_r != 2 * num_a || num_a > BN_SMALL_MAX_WORDS) {
  801. OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  802. return 0;
  803. }
  804. if (num_a == 4) {
  805. bn_sqr_comba4(r, a);
  806. } else if (num_a == 8) {
  807. bn_sqr_comba8(r, a);
  808. } else {
  809. BN_ULONG tmp[2 * BN_SMALL_MAX_WORDS];
  810. bn_sqr_normal(r, a, num_a, tmp);
  811. OPENSSL_cleanse(tmp, 2 * num_a * sizeof(BN_ULONG));
  812. }
  813. return 1;
  814. }