bn_mul.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <assert.h>
  10. #include "internal/cryptlib.h"
  11. #include "bn_lcl.h"
  12. #if defined(OPENSSL_NO_ASM) || !defined(OPENSSL_BN_ASM_PART_WORDS)
  13. /*
  14. * Here follows specialised variants of bn_add_words() and bn_sub_words().
  15. * They have the property performing operations on arrays of different sizes.
  16. * The sizes of those arrays is expressed through cl, which is the common
  17. * length ( basically, min(len(a),len(b)) ), and dl, which is the delta
  18. * between the two lengths, calculated as len(a)-len(b). All lengths are the
  19. * number of BN_ULONGs... For the operations that require a result array as
  20. * parameter, it must have the length cl+abs(dl). These functions should
  21. * probably end up in bn_asm.c as soon as there are assembler counterparts
  22. * for the systems that use assembler files.
  23. */
  24. BN_ULONG bn_sub_part_words(BN_ULONG *r,
  25. const BN_ULONG *a, const BN_ULONG *b,
  26. int cl, int dl)
  27. {
  28. BN_ULONG c, t;
  29. assert(cl >= 0);
  30. c = bn_sub_words(r, a, b, cl);
  31. if (dl == 0)
  32. return c;
  33. r += cl;
  34. a += cl;
  35. b += cl;
  36. if (dl < 0) {
  37. for (;;) {
  38. t = b[0];
  39. r[0] = (0 - t - c) & BN_MASK2;
  40. if (t != 0)
  41. c = 1;
  42. if (++dl >= 0)
  43. break;
  44. t = b[1];
  45. r[1] = (0 - t - c) & BN_MASK2;
  46. if (t != 0)
  47. c = 1;
  48. if (++dl >= 0)
  49. break;
  50. t = b[2];
  51. r[2] = (0 - t - c) & BN_MASK2;
  52. if (t != 0)
  53. c = 1;
  54. if (++dl >= 0)
  55. break;
  56. t = b[3];
  57. r[3] = (0 - t - c) & BN_MASK2;
  58. if (t != 0)
  59. c = 1;
  60. if (++dl >= 0)
  61. break;
  62. b += 4;
  63. r += 4;
  64. }
  65. } else {
  66. int save_dl = dl;
  67. while (c) {
  68. t = a[0];
  69. r[0] = (t - c) & BN_MASK2;
  70. if (t != 0)
  71. c = 0;
  72. if (--dl <= 0)
  73. break;
  74. t = a[1];
  75. r[1] = (t - c) & BN_MASK2;
  76. if (t != 0)
  77. c = 0;
  78. if (--dl <= 0)
  79. break;
  80. t = a[2];
  81. r[2] = (t - c) & BN_MASK2;
  82. if (t != 0)
  83. c = 0;
  84. if (--dl <= 0)
  85. break;
  86. t = a[3];
  87. r[3] = (t - c) & BN_MASK2;
  88. if (t != 0)
  89. c = 0;
  90. if (--dl <= 0)
  91. break;
  92. save_dl = dl;
  93. a += 4;
  94. r += 4;
  95. }
  96. if (dl > 0) {
  97. if (save_dl > dl) {
  98. switch (save_dl - dl) {
  99. case 1:
  100. r[1] = a[1];
  101. if (--dl <= 0)
  102. break;
  103. case 2:
  104. r[2] = a[2];
  105. if (--dl <= 0)
  106. break;
  107. case 3:
  108. r[3] = a[3];
  109. if (--dl <= 0)
  110. break;
  111. }
  112. a += 4;
  113. r += 4;
  114. }
  115. }
  116. if (dl > 0) {
  117. for (;;) {
  118. r[0] = a[0];
  119. if (--dl <= 0)
  120. break;
  121. r[1] = a[1];
  122. if (--dl <= 0)
  123. break;
  124. r[2] = a[2];
  125. if (--dl <= 0)
  126. break;
  127. r[3] = a[3];
  128. if (--dl <= 0)
  129. break;
  130. a += 4;
  131. r += 4;
  132. }
  133. }
  134. }
  135. return c;
  136. }
  137. #endif
  138. #ifdef BN_RECURSION
  139. /*
  140. * Karatsuba recursive multiplication algorithm (cf. Knuth, The Art of
  141. * Computer Programming, Vol. 2)
  142. */
  143. /*-
  144. * r is 2*n2 words in size,
  145. * a and b are both n2 words in size.
  146. * n2 must be a power of 2.
  147. * We multiply and return the result.
  148. * t must be 2*n2 words in size
  149. * We calculate
  150. * a[0]*b[0]
  151. * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
  152. * a[1]*b[1]
  153. */
  154. /* dnX may not be positive, but n2/2+dnX has to be */
  155. void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
  156. int dna, int dnb, BN_ULONG *t)
  157. {
  158. int n = n2 / 2, c1, c2;
  159. int tna = n + dna, tnb = n + dnb;
  160. unsigned int neg, zero;
  161. BN_ULONG ln, lo, *p;
  162. # ifdef BN_MUL_COMBA
  163. # if 0
  164. if (n2 == 4) {
  165. bn_mul_comba4(r, a, b);
  166. return;
  167. }
  168. # endif
  169. /*
  170. * Only call bn_mul_comba 8 if n2 == 8 and the two arrays are complete
  171. * [steve]
  172. */
  173. if (n2 == 8 && dna == 0 && dnb == 0) {
  174. bn_mul_comba8(r, a, b);
  175. return;
  176. }
  177. # endif /* BN_MUL_COMBA */
  178. /* Else do normal multiply */
  179. if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
  180. bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
  181. if ((dna + dnb) < 0)
  182. memset(&r[2 * n2 + dna + dnb], 0,
  183. sizeof(BN_ULONG) * -(dna + dnb));
  184. return;
  185. }
  186. /* r=(a[0]-a[1])*(b[1]-b[0]) */
  187. c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
  188. c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
  189. zero = neg = 0;
  190. switch (c1 * 3 + c2) {
  191. case -4:
  192. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  193. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  194. break;
  195. case -3:
  196. zero = 1;
  197. break;
  198. case -2:
  199. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  200. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
  201. neg = 1;
  202. break;
  203. case -1:
  204. case 0:
  205. case 1:
  206. zero = 1;
  207. break;
  208. case 2:
  209. bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
  210. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  211. neg = 1;
  212. break;
  213. case 3:
  214. zero = 1;
  215. break;
  216. case 4:
  217. bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
  218. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
  219. break;
  220. }
  221. # ifdef BN_MUL_COMBA
  222. if (n == 4 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba4 could take
  223. * extra args to do this well */
  224. if (!zero)
  225. bn_mul_comba4(&(t[n2]), t, &(t[n]));
  226. else
  227. memset(&t[n2], 0, sizeof(*t) * 8);
  228. bn_mul_comba4(r, a, b);
  229. bn_mul_comba4(&(r[n2]), &(a[n]), &(b[n]));
  230. } else if (n == 8 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba8 could
  231. * take extra args to do
  232. * this well */
  233. if (!zero)
  234. bn_mul_comba8(&(t[n2]), t, &(t[n]));
  235. else
  236. memset(&t[n2], 0, sizeof(*t) * 16);
  237. bn_mul_comba8(r, a, b);
  238. bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));
  239. } else
  240. # endif /* BN_MUL_COMBA */
  241. {
  242. p = &(t[n2 * 2]);
  243. if (!zero)
  244. bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
  245. else
  246. memset(&t[n2], 0, sizeof(*t) * n2);
  247. bn_mul_recursive(r, a, b, n, 0, 0, p);
  248. bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p);
  249. }
  250. /*-
  251. * t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
  252. * r[10] holds (a[0]*b[0])
  253. * r[32] holds (b[1]*b[1])
  254. */
  255. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  256. if (neg) { /* if t[32] is negative */
  257. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  258. } else {
  259. /* Might have a carry */
  260. c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
  261. }
  262. /*-
  263. * t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
  264. * r[10] holds (a[0]*b[0])
  265. * r[32] holds (b[1]*b[1])
  266. * c1 holds the carry bits
  267. */
  268. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  269. if (c1) {
  270. p = &(r[n + n2]);
  271. lo = *p;
  272. ln = (lo + c1) & BN_MASK2;
  273. *p = ln;
  274. /*
  275. * The overflow will stop before we over write words we should not
  276. * overwrite
  277. */
  278. if (ln < (BN_ULONG)c1) {
  279. do {
  280. p++;
  281. lo = *p;
  282. ln = (lo + 1) & BN_MASK2;
  283. *p = ln;
  284. } while (ln == 0);
  285. }
  286. }
  287. }
  288. /*
  289. * n+tn is the word length t needs to be n*4 is size, as does r
  290. */
  291. /* tnX may not be negative but less than n */
  292. void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,
  293. int tna, int tnb, BN_ULONG *t)
  294. {
  295. int i, j, n2 = n * 2;
  296. int c1, c2, neg;
  297. BN_ULONG ln, lo, *p;
  298. if (n < 8) {
  299. bn_mul_normal(r, a, n + tna, b, n + tnb);
  300. return;
  301. }
  302. /* r=(a[0]-a[1])*(b[1]-b[0]) */
  303. c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
  304. c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
  305. neg = 0;
  306. switch (c1 * 3 + c2) {
  307. case -4:
  308. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  309. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  310. break;
  311. case -3:
  312. case -2:
  313. bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
  314. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
  315. neg = 1;
  316. break;
  317. case -1:
  318. case 0:
  319. case 1:
  320. case 2:
  321. bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
  322. bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
  323. neg = 1;
  324. break;
  325. case 3:
  326. case 4:
  327. bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
  328. bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
  329. break;
  330. }
  331. /*
  332. * The zero case isn't yet implemented here. The speedup would probably
  333. * be negligible.
  334. */
  335. # if 0
  336. if (n == 4) {
  337. bn_mul_comba4(&(t[n2]), t, &(t[n]));
  338. bn_mul_comba4(r, a, b);
  339. bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);
  340. memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));
  341. } else
  342. # endif
  343. if (n == 8) {
  344. bn_mul_comba8(&(t[n2]), t, &(t[n]));
  345. bn_mul_comba8(r, a, b);
  346. bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
  347. memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));
  348. } else {
  349. p = &(t[n2 * 2]);
  350. bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
  351. bn_mul_recursive(r, a, b, n, 0, 0, p);
  352. i = n / 2;
  353. /*
  354. * If there is only a bottom half to the number, just do it
  355. */
  356. if (tna > tnb)
  357. j = tna - i;
  358. else
  359. j = tnb - i;
  360. if (j == 0) {
  361. bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),
  362. i, tna - i, tnb - i, p);
  363. memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));
  364. } else if (j > 0) { /* eg, n == 16, i == 8 and tn == 11 */
  365. bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),
  366. i, tna - i, tnb - i, p);
  367. memset(&(r[n2 + tna + tnb]), 0,
  368. sizeof(BN_ULONG) * (n2 - tna - tnb));
  369. } else { /* (j < 0) eg, n == 16, i == 8 and tn == 5 */
  370. memset(&r[n2], 0, sizeof(*r) * n2);
  371. if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL
  372. && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {
  373. bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
  374. } else {
  375. for (;;) {
  376. i /= 2;
  377. /*
  378. * these simplified conditions work exclusively because
  379. * difference between tna and tnb is 1 or 0
  380. */
  381. if (i < tna || i < tnb) {
  382. bn_mul_part_recursive(&(r[n2]),
  383. &(a[n]), &(b[n]),
  384. i, tna - i, tnb - i, p);
  385. break;
  386. } else if (i == tna || i == tnb) {
  387. bn_mul_recursive(&(r[n2]),
  388. &(a[n]), &(b[n]),
  389. i, tna - i, tnb - i, p);
  390. break;
  391. }
  392. }
  393. }
  394. }
  395. }
  396. /*-
  397. * t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
  398. * r[10] holds (a[0]*b[0])
  399. * r[32] holds (b[1]*b[1])
  400. */
  401. c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
  402. if (neg) { /* if t[32] is negative */
  403. c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
  404. } else {
  405. /* Might have a carry */
  406. c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
  407. }
  408. /*-
  409. * t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
  410. * r[10] holds (a[0]*b[0])
  411. * r[32] holds (b[1]*b[1])
  412. * c1 holds the carry bits
  413. */
  414. c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
  415. if (c1) {
  416. p = &(r[n + n2]);
  417. lo = *p;
  418. ln = (lo + c1) & BN_MASK2;
  419. *p = ln;
  420. /*
  421. * The overflow will stop before we over write words we should not
  422. * overwrite
  423. */
  424. if (ln < (BN_ULONG)c1) {
  425. do {
  426. p++;
  427. lo = *p;
  428. ln = (lo + 1) & BN_MASK2;
  429. *p = ln;
  430. } while (ln == 0);
  431. }
  432. }
  433. }
  434. /*-
  435. * a and b must be the same size, which is n2.
  436. * r needs to be n2 words and t needs to be n2*2
  437. */
  438. void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
  439. BN_ULONG *t)
  440. {
  441. int n = n2 / 2;
  442. bn_mul_recursive(r, a, b, n, 0, 0, &(t[0]));
  443. if (n >= BN_MUL_LOW_RECURSIVE_SIZE_NORMAL) {
  444. bn_mul_low_recursive(&(t[0]), &(a[0]), &(b[n]), n, &(t[n2]));
  445. bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
  446. bn_mul_low_recursive(&(t[0]), &(a[n]), &(b[0]), n, &(t[n2]));
  447. bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
  448. } else {
  449. bn_mul_low_normal(&(t[0]), &(a[0]), &(b[n]), n);
  450. bn_mul_low_normal(&(t[n]), &(a[n]), &(b[0]), n);
  451. bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
  452. bn_add_words(&(r[n]), &(r[n]), &(t[n]), n);
  453. }
  454. }
  455. #endif /* BN_RECURSION */
  456. int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  457. {
  458. int ret = 0;
  459. int top, al, bl;
  460. BIGNUM *rr;
  461. #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
  462. int i;
  463. #endif
  464. #ifdef BN_RECURSION
  465. BIGNUM *t = NULL;
  466. int j = 0, k;
  467. #endif
  468. bn_check_top(a);
  469. bn_check_top(b);
  470. bn_check_top(r);
  471. al = a->top;
  472. bl = b->top;
  473. if ((al == 0) || (bl == 0)) {
  474. BN_zero(r);
  475. return (1);
  476. }
  477. top = al + bl;
  478. BN_CTX_start(ctx);
  479. if ((r == a) || (r == b)) {
  480. if ((rr = BN_CTX_get(ctx)) == NULL)
  481. goto err;
  482. } else
  483. rr = r;
  484. #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
  485. i = al - bl;
  486. #endif
  487. #ifdef BN_MUL_COMBA
  488. if (i == 0) {
  489. # if 0
  490. if (al == 4) {
  491. if (bn_wexpand(rr, 8) == NULL)
  492. goto err;
  493. rr->top = 8;
  494. bn_mul_comba4(rr->d, a->d, b->d);
  495. goto end;
  496. }
  497. # endif
  498. if (al == 8) {
  499. if (bn_wexpand(rr, 16) == NULL)
  500. goto err;
  501. rr->top = 16;
  502. bn_mul_comba8(rr->d, a->d, b->d);
  503. goto end;
  504. }
  505. }
  506. #endif /* BN_MUL_COMBA */
  507. #ifdef BN_RECURSION
  508. if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {
  509. if (i >= -1 && i <= 1) {
  510. /*
  511. * Find out the power of two lower or equal to the longest of the
  512. * two numbers
  513. */
  514. if (i >= 0) {
  515. j = BN_num_bits_word((BN_ULONG)al);
  516. }
  517. if (i == -1) {
  518. j = BN_num_bits_word((BN_ULONG)bl);
  519. }
  520. j = 1 << (j - 1);
  521. assert(j <= al || j <= bl);
  522. k = j + j;
  523. t = BN_CTX_get(ctx);
  524. if (t == NULL)
  525. goto err;
  526. if (al > j || bl > j) {
  527. if (bn_wexpand(t, k * 4) == NULL)
  528. goto err;
  529. if (bn_wexpand(rr, k * 4) == NULL)
  530. goto err;
  531. bn_mul_part_recursive(rr->d, a->d, b->d,
  532. j, al - j, bl - j, t->d);
  533. } else { /* al <= j || bl <= j */
  534. if (bn_wexpand(t, k * 2) == NULL)
  535. goto err;
  536. if (bn_wexpand(rr, k * 2) == NULL)
  537. goto err;
  538. bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
  539. }
  540. rr->top = top;
  541. goto end;
  542. }
  543. # if 0
  544. if (i == 1 && !BN_get_flags(b, BN_FLG_STATIC_DATA)) {
  545. BIGNUM *tmp_bn = (BIGNUM *)b;
  546. if (bn_wexpand(tmp_bn, al) == NULL)
  547. goto err;
  548. tmp_bn->d[bl] = 0;
  549. bl++;
  550. i--;
  551. } else if (i == -1 && !BN_get_flags(a, BN_FLG_STATIC_DATA)) {
  552. BIGNUM *tmp_bn = (BIGNUM *)a;
  553. if (bn_wexpand(tmp_bn, bl) == NULL)
  554. goto err;
  555. tmp_bn->d[al] = 0;
  556. al++;
  557. i++;
  558. }
  559. if (i == 0) {
  560. /* symmetric and > 4 */
  561. /* 16 or larger */
  562. j = BN_num_bits_word((BN_ULONG)al);
  563. j = 1 << (j - 1);
  564. k = j + j;
  565. t = BN_CTX_get(ctx);
  566. if (al == j) { /* exact multiple */
  567. if (bn_wexpand(t, k * 2) == NULL)
  568. goto err;
  569. if (bn_wexpand(rr, k * 2) == NULL)
  570. goto err;
  571. bn_mul_recursive(rr->d, a->d, b->d, al, t->d);
  572. } else {
  573. if (bn_wexpand(t, k * 4) == NULL)
  574. goto err;
  575. if (bn_wexpand(rr, k * 4) == NULL)
  576. goto err;
  577. bn_mul_part_recursive(rr->d, a->d, b->d, al - j, j, t->d);
  578. }
  579. rr->top = top;
  580. goto end;
  581. }
  582. # endif
  583. }
  584. #endif /* BN_RECURSION */
  585. if (bn_wexpand(rr, top) == NULL)
  586. goto err;
  587. rr->top = top;
  588. bn_mul_normal(rr->d, a->d, al, b->d, bl);
  589. #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
  590. end:
  591. #endif
  592. rr->neg = a->neg ^ b->neg;
  593. bn_correct_top(rr);
  594. if (r != rr && BN_copy(r, rr) == NULL)
  595. goto err;
  596. ret = 1;
  597. err:
  598. bn_check_top(r);
  599. BN_CTX_end(ctx);
  600. return (ret);
  601. }
  602. void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
  603. {
  604. BN_ULONG *rr;
  605. if (na < nb) {
  606. int itmp;
  607. BN_ULONG *ltmp;
  608. itmp = na;
  609. na = nb;
  610. nb = itmp;
  611. ltmp = a;
  612. a = b;
  613. b = ltmp;
  614. }
  615. rr = &(r[na]);
  616. if (nb <= 0) {
  617. (void)bn_mul_words(r, a, na, 0);
  618. return;
  619. } else
  620. rr[0] = bn_mul_words(r, a, na, b[0]);
  621. for (;;) {
  622. if (--nb <= 0)
  623. return;
  624. rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
  625. if (--nb <= 0)
  626. return;
  627. rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
  628. if (--nb <= 0)
  629. return;
  630. rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
  631. if (--nb <= 0)
  632. return;
  633. rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
  634. rr += 4;
  635. r += 4;
  636. b += 4;
  637. }
  638. }
  639. void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
  640. {
  641. bn_mul_words(r, a, n, b[0]);
  642. for (;;) {
  643. if (--n <= 0)
  644. return;
  645. bn_mul_add_words(&(r[1]), a, n, b[1]);
  646. if (--n <= 0)
  647. return;
  648. bn_mul_add_words(&(r[2]), a, n, b[2]);
  649. if (--n <= 0)
  650. return;
  651. bn_mul_add_words(&(r[3]), a, n, b[3]);
  652. if (--n <= 0)
  653. return;
  654. bn_mul_add_words(&(r[4]), a, n, b[4]);
  655. r += 4;
  656. b += 4;
  657. }
  658. }