mul.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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 <openssl/type_check.h>
  62. #include "internal.h"
  63. #include "../../internal.h"
  64. #define BN_MUL_RECURSIVE_SIZE_NORMAL 16
  65. #define BN_SQR_RECURSIVE_SIZE_NORMAL BN_MUL_RECURSIVE_SIZE_NORMAL
  66. static void bn_abs_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  67. size_t num, BN_ULONG *tmp) {
  68. BN_ULONG borrow = bn_sub_words(tmp, a, b, num);
  69. bn_sub_words(r, b, a, num);
  70. bn_select_words(r, 0 - borrow, r /* tmp < 0 */, tmp /* tmp >= 0 */, num);
  71. }
  72. static void bn_mul_normal(BN_ULONG *r, const BN_ULONG *a, size_t na,
  73. const BN_ULONG *b, size_t nb) {
  74. if (na < nb) {
  75. size_t itmp = na;
  76. na = nb;
  77. nb = itmp;
  78. const BN_ULONG *ltmp = a;
  79. a = b;
  80. b = ltmp;
  81. }
  82. BN_ULONG *rr = &(r[na]);
  83. if (nb == 0) {
  84. OPENSSL_memset(r, 0, na * sizeof(BN_ULONG));
  85. return;
  86. }
  87. rr[0] = bn_mul_words(r, a, na, b[0]);
  88. for (;;) {
  89. if (--nb == 0) {
  90. return;
  91. }
  92. rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
  93. if (--nb == 0) {
  94. return;
  95. }
  96. rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
  97. if (--nb == 0) {
  98. return;
  99. }
  100. rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
  101. if (--nb == 0) {
  102. return;
  103. }
  104. rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
  105. rr += 4;
  106. r += 4;
  107. b += 4;
  108. }
  109. }
  110. #if !defined(OPENSSL_X86) || defined(OPENSSL_NO_ASM)
  111. // Here follows specialised variants of bn_add_words() and bn_sub_words(). They
  112. // have the property performing operations on arrays of different sizes. The
  113. // sizes of those arrays is expressed through cl, which is the common length (
  114. // basicall, min(len(a),len(b)) ), and dl, which is the delta between the two
  115. // lengths, calculated as len(a)-len(b). All lengths are the number of
  116. // BN_ULONGs... For the operations that require a result array as parameter,
  117. // it must have the length cl+abs(dl). These functions should probably end up
  118. // in bn_asm.c as soon as there are assembler counterparts for the systems that
  119. // use assembler files.
  120. static BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a,
  121. const BN_ULONG *b, int cl, int dl) {
  122. BN_ULONG c, t;
  123. assert(cl >= 0);
  124. c = bn_sub_words(r, a, b, cl);
  125. if (dl == 0) {
  126. return c;
  127. }
  128. r += cl;
  129. a += cl;
  130. b += cl;
  131. if (dl < 0) {
  132. for (;;) {
  133. t = b[0];
  134. r[0] = 0 - t - c;
  135. if (t != 0) {
  136. c = 1;
  137. }
  138. if (++dl >= 0) {
  139. break;
  140. }
  141. t = b[1];
  142. r[1] = 0 - t - c;
  143. if (t != 0) {
  144. c = 1;
  145. }
  146. if (++dl >= 0) {
  147. break;
  148. }
  149. t = b[2];
  150. r[2] = 0 - t - c;
  151. if (t != 0) {
  152. c = 1;
  153. }
  154. if (++dl >= 0) {
  155. break;
  156. }
  157. t = b[3];
  158. r[3] = 0 - t - c;
  159. if (t != 0) {
  160. c = 1;
  161. }
  162. if (++dl >= 0) {
  163. break;
  164. }
  165. b += 4;
  166. r += 4;
  167. }
  168. } else {
  169. int save_dl = dl;
  170. while (c) {
  171. t = a[0];
  172. r[0] = t - c;
  173. if (t != 0) {
  174. c = 0;
  175. }
  176. if (--dl <= 0) {
  177. break;
  178. }
  179. t = a[1];
  180. r[1] = t - c;
  181. if (t != 0) {
  182. c = 0;
  183. }
  184. if (--dl <= 0) {
  185. break;
  186. }
  187. t = a[2];
  188. r[2] = t - c;
  189. if (t != 0) {
  190. c = 0;
  191. }
  192. if (--dl <= 0) {
  193. break;
  194. }
  195. t = a[3];
  196. r[3] = t - c;
  197. if (t != 0) {
  198. c = 0;
  199. }
  200. if (--dl <= 0) {
  201. break;
  202. }
  203. save_dl = dl;
  204. a += 4;
  205. r += 4;
  206. }
  207. if (dl > 0) {
  208. if (save_dl > dl) {
  209. switch (save_dl - dl) {
  210. case 1:
  211. r[1] = a[1];
  212. if (--dl <= 0) {
  213. break;
  214. }
  215. OPENSSL_FALLTHROUGH;
  216. case 2:
  217. r[2] = a[2];
  218. if (--dl <= 0) {
  219. break;
  220. }
  221. OPENSSL_FALLTHROUGH;
  222. case 3:
  223. r[3] = a[3];
  224. if (--dl <= 0) {
  225. break;
  226. }
  227. }
  228. a += 4;
  229. r += 4;
  230. }
  231. }
  232. if (dl > 0) {
  233. for (;;) {
  234. r[0] = a[0];
  235. if (--dl <= 0) {
  236. break;
  237. }
  238. r[1] = a[1];
  239. if (--dl <= 0) {
  240. break;
  241. }
  242. r[2] = a[2];
  243. if (--dl <= 0) {
  244. break;
  245. }
  246. r[3] = a[3];
  247. if (--dl <= 0) {
  248. break;
  249. }
  250. a += 4;
  251. r += 4;
  252. }
  253. }
  254. }
  255. return c;
  256. }
  257. #else
  258. // On other platforms the function is defined in asm.
  259. BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  260. int cl, int dl);
  261. #endif
  262. // bn_abs_sub_part_words computes |r| = |a| - |b|, storing the absolute value
  263. // and returning a mask of all ones if the result was negative and all zeros if
  264. // the result was positive. |cl| and |dl| follow the |bn_sub_part_words| calling
  265. // convention.
  266. //
  267. // TODO(davidben): Make this take |size_t|. The |cl| + |dl| calling convention
  268. // is confusing. The trouble is 32-bit x86 implements |bn_sub_part_words| in
  269. // assembly, but we can probably just delete it?
  270. static BN_ULONG bn_abs_sub_part_words(BN_ULONG *r, const BN_ULONG *a,
  271. const BN_ULONG *b, int cl, int dl,
  272. BN_ULONG *tmp) {
  273. BN_ULONG borrow = bn_sub_part_words(tmp, a, b, cl, dl);
  274. bn_sub_part_words(r, b, a, cl, -dl);
  275. int r_len = cl + (dl < 0 ? -dl : dl);
  276. borrow = 0 - borrow;
  277. bn_select_words(r, borrow, r /* tmp < 0 */, tmp /* tmp >= 0 */, r_len);
  278. return borrow;
  279. }
  280. int bn_abs_sub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  281. BN_CTX *ctx) {
  282. int cl = a->width < b->width ? a->width : b->width;
  283. int dl = a->width - b->width;
  284. int r_len = a->width < b->width ? b->width : a->width;
  285. BN_CTX_start(ctx);
  286. BIGNUM *tmp = BN_CTX_get(ctx);
  287. int ok = tmp != NULL &&
  288. bn_wexpand(r, r_len) &&
  289. bn_wexpand(tmp, r_len);
  290. if (ok) {
  291. bn_abs_sub_part_words(r->d, a->d, b->d, cl, dl, tmp->d);
  292. r->width = r_len;
  293. }
  294. BN_CTX_end(ctx);
  295. return ok;
  296. }
  297. // Karatsuba recursive multiplication algorithm
  298. // (cf. Knuth, The Art of Computer Programming, Vol. 2)
  299. // bn_mul_recursive sets |r| to |a| * |b|, using |t| as scratch space. |r| has
  300. // length 2*|n2|, |a| has length |n2| + |dna|, |b| has length |n2| + |dnb|, and
  301. // |t| has length 4*|n2|. |n2| must be a power of two. Finally, we must have
  302. // -|BN_MUL_RECURSIVE_SIZE_NORMAL|/2 <= |dna| <= 0 and
  303. // -|BN_MUL_RECURSIVE_SIZE_NORMAL|/2 <= |dnb| <= 0.
  304. //
  305. // TODO(davidben): Simplify and |size_t| the calling convention around lengths
  306. // here.
  307. static void bn_mul_recursive(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  308. int n2, int dna, int dnb, BN_ULONG *t) {
  309. // |n2| is a power of two.
  310. assert(n2 != 0 && (n2 & (n2 - 1)) == 0);
  311. // Check |dna| and |dnb| are in range.
  312. assert(-BN_MUL_RECURSIVE_SIZE_NORMAL/2 <= dna && dna <= 0);
  313. assert(-BN_MUL_RECURSIVE_SIZE_NORMAL/2 <= dnb && dnb <= 0);
  314. // Only call bn_mul_comba 8 if n2 == 8 and the
  315. // two arrays are complete [steve]
  316. if (n2 == 8 && dna == 0 && dnb == 0) {
  317. bn_mul_comba8(r, a, b);
  318. return;
  319. }
  320. // Else do normal multiply
  321. if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
  322. bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
  323. if (dna + dnb < 0) {
  324. OPENSSL_memset(&r[2 * n2 + dna + dnb], 0,
  325. sizeof(BN_ULONG) * -(dna + dnb));
  326. }
  327. return;
  328. }
  329. // Split |a| and |b| into a0,a1 and b0,b1, where a0 and b0 have size |n|.
  330. // Split |t| into t0,t1,t2,t3, each of size |n|, with the remaining 4*|n| used
  331. // for recursive calls.
  332. // Split |r| into r0,r1,r2,r3. We must contribute a0*b0 to r0,r1, a0*a1+b0*b1
  333. // to r1,r2, and a1*b1 to r2,r3. The middle term we will compute as:
  334. //
  335. // a0*a1 + b0*b1 = (a0 - a1)*(b1 - b0) + a1*b1 + a0*b0
  336. //
  337. // Note that we know |n| >= |BN_MUL_RECURSIVE_SIZE_NORMAL|/2 above, so
  338. // |tna| and |tnb| are non-negative.
  339. int n = n2 / 2, tna = n + dna, tnb = n + dnb;
  340. // t0 = a0 - a1 and t1 = b1 - b0. The result will be multiplied, so we XOR
  341. // their sign masks, giving the sign of (a0 - a1)*(b1 - b0). t0 and t1
  342. // themselves store the absolute value.
  343. BN_ULONG neg = bn_abs_sub_part_words(t, a, &a[n], tna, n - tna, &t[n2]);
  344. neg ^= bn_abs_sub_part_words(&t[n], &b[n], b, tnb, tnb - n, &t[n2]);
  345. // Compute:
  346. // t2,t3 = t0 * t1 = |(a0 - a1)*(b1 - b0)|
  347. // r0,r1 = a0 * b0
  348. // r2,r3 = a1 * b1
  349. if (n == 4 && dna == 0 && dnb == 0) {
  350. bn_mul_comba4(&t[n2], t, &t[n]);
  351. bn_mul_comba4(r, a, b);
  352. bn_mul_comba4(&r[n2], &a[n], &b[n]);
  353. } else if (n == 8 && dna == 0 && dnb == 0) {
  354. bn_mul_comba8(&t[n2], t, &t[n]);
  355. bn_mul_comba8(r, a, b);
  356. bn_mul_comba8(&r[n2], &a[n], &b[n]);
  357. } else {
  358. BN_ULONG *p = &t[n2 * 2];
  359. bn_mul_recursive(&t[n2], t, &t[n], n, 0, 0, p);
  360. bn_mul_recursive(r, a, b, n, 0, 0, p);
  361. bn_mul_recursive(&r[n2], &a[n], &b[n], n, dna, dnb, p);
  362. }
  363. // t0,t1,c = r0,r1 + r2,r3 = a0*b0 + a1*b1
  364. BN_ULONG c = bn_add_words(t, r, &r[n2], n2);
  365. // t2,t3,c = t0,t1,c + neg*t2,t3 = (a0 - a1)*(b1 - b0) + a1*b1 + a0*b0.
  366. // The second term is stored as the absolute value, so we do this with a
  367. // constant-time select.
  368. BN_ULONG c_neg = c - bn_sub_words(&t[n2 * 2], t, &t[n2], n2);
  369. BN_ULONG c_pos = c + bn_add_words(&t[n2], t, &t[n2], n2);
  370. bn_select_words(&t[n2], neg, &t[n2 * 2], &t[n2], n2);
  371. OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
  372. crypto_word_t_too_small);
  373. c = constant_time_select_w(neg, c_neg, c_pos);
  374. // We now have our three components. Add them together.
  375. // r1,r2,c = r1,r2 + t2,t3,c
  376. c += bn_add_words(&r[n], &r[n], &t[n2], n2);
  377. // Propagate the carry bit to the end.
  378. for (int i = n + n2; i < n2 + n2; i++) {
  379. BN_ULONG old = r[i];
  380. r[i] = old + c;
  381. c = r[i] < old;
  382. }
  383. // The product should fit without carries.
  384. assert(c == 0);
  385. }
  386. // bn_mul_part_recursive sets |r| to |a| * |b|, using |t| as scratch space. |r|
  387. // has length 4*|n|, |a| has length |n| + |tna|, |b| has length |n| + |tnb|, and
  388. // |t| has length 8*|n|. |n| must be a power of two. Additionally, we must have
  389. // 0 <= tna < n and 0 <= tnb < n, and |tna| and |tnb| must differ by at most
  390. // one.
  391. //
  392. // TODO(davidben): Make this take |size_t| and perhaps the actual lengths of |a|
  393. // and |b|.
  394. static void bn_mul_part_recursive(BN_ULONG *r, const BN_ULONG *a,
  395. const BN_ULONG *b, int n, int tna, int tnb,
  396. BN_ULONG *t) {
  397. // |n| is a power of two.
  398. assert(n != 0 && (n & (n - 1)) == 0);
  399. // Check |tna| and |tnb| are in range.
  400. assert(0 <= tna && tna < n);
  401. assert(0 <= tnb && tnb < n);
  402. assert(-1 <= tna - tnb && tna - tnb <= 1);
  403. int n2 = n * 2;
  404. if (n < 8) {
  405. bn_mul_normal(r, a, n + tna, b, n + tnb);
  406. OPENSSL_memset(r + n2 + tna + tnb, 0, n2 - tna - tnb);
  407. return;
  408. }
  409. // Split |a| and |b| into a0,a1 and b0,b1, where a0 and b0 have size |n|. |a1|
  410. // and |b1| have size |tna| and |tnb|, respectively.
  411. // Split |t| into t0,t1,t2,t3, each of size |n|, with the remaining 4*|n| used
  412. // for recursive calls.
  413. // Split |r| into r0,r1,r2,r3. We must contribute a0*b0 to r0,r1, a0*a1+b0*b1
  414. // to r1,r2, and a1*b1 to r2,r3. The middle term we will compute as:
  415. //
  416. // a0*a1 + b0*b1 = (a0 - a1)*(b1 - b0) + a1*b1 + a0*b0
  417. // t0 = a0 - a1 and t1 = b1 - b0. The result will be multiplied, so we XOR
  418. // their sign masks, giving the sign of (a0 - a1)*(b1 - b0). t0 and t1
  419. // themselves store the absolute value.
  420. BN_ULONG neg = bn_abs_sub_part_words(t, a, &a[n], tna, n - tna, &t[n2]);
  421. neg ^= bn_abs_sub_part_words(&t[n], &b[n], b, tnb, tnb - n, &t[n2]);
  422. // Compute:
  423. // t2,t3 = t0 * t1 = |(a0 - a1)*(b1 - b0)|
  424. // r0,r1 = a0 * b0
  425. // r2,r3 = a1 * b1
  426. if (n == 8) {
  427. bn_mul_comba8(&t[n2], t, &t[n]);
  428. bn_mul_comba8(r, a, b);
  429. bn_mul_normal(&r[n2], &a[n], tna, &b[n], tnb);
  430. // |bn_mul_normal| only writes |tna| + |tna| words. Zero the rest.
  431. OPENSSL_memset(&r[n2 + tna + tnb], 0, sizeof(BN_ULONG) * (n2 - tna - tnb));
  432. } else {
  433. BN_ULONG *p = &t[n2 * 2];
  434. bn_mul_recursive(&t[n2], t, &t[n], n, 0, 0, p);
  435. bn_mul_recursive(r, a, b, n, 0, 0, p);
  436. OPENSSL_memset(&r[n2], 0, sizeof(BN_ULONG) * n2);
  437. if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL &&
  438. tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {
  439. bn_mul_normal(&r[n2], &a[n], tna, &b[n], tnb);
  440. } else {
  441. int i = n;
  442. for (;;) {
  443. i /= 2;
  444. if (i < tna || i < tnb) {
  445. // E.g., n == 16, i == 8 and tna == 11. |tna| and |tnb| are within one
  446. // of each other, so if |tna| is larger and tna > i, then we know
  447. // tnb >= i, and this call is valid.
  448. bn_mul_part_recursive(&r[n2], &a[n], &b[n], i, tna - i, tnb - i, p);
  449. break;
  450. }
  451. if (i == tna || i == tnb) {
  452. // If there is only a bottom half to the number, just do it. We know
  453. // the larger of |tna - i| and |tnb - i| is zero. The other is zero or
  454. // -1 by because of |tna| and |tnb| differ by at most one.
  455. bn_mul_recursive(&r[n2], &a[n], &b[n], i, tna - i, tnb - i, p);
  456. break;
  457. }
  458. // This loop will eventually terminate when |i| falls below
  459. // |BN_MUL_RECURSIVE_SIZE_NORMAL| because we know one of |tna| and |tnb|
  460. // exceeds that.
  461. }
  462. }
  463. }
  464. // t0,t1,c = r0,r1 + r2,r3 = a0*b0 + a1*b1
  465. BN_ULONG c = bn_add_words(t, r, &r[n2], n2);
  466. // t2,t3,c = t0,t1,c + neg*t2,t3 = (a0 - a1)*(b1 - b0) + a1*b1 + a0*b0.
  467. // The second term is stored as the absolute value, so we do this with a
  468. // constant-time select.
  469. BN_ULONG c_neg = c - bn_sub_words(&t[n2 * 2], t, &t[n2], n2);
  470. BN_ULONG c_pos = c + bn_add_words(&t[n2], t, &t[n2], n2);
  471. bn_select_words(&t[n2], neg, &t[n2 * 2], &t[n2], n2);
  472. OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
  473. crypto_word_t_too_small);
  474. c = constant_time_select_w(neg, c_neg, c_pos);
  475. // We now have our three components. Add them together.
  476. // r1,r2,c = r1,r2 + t2,t3,c
  477. c += bn_add_words(&r[n], &r[n], &t[n2], n2);
  478. // Propagate the carry bit to the end.
  479. for (int i = n + n2; i < n2 + n2; i++) {
  480. BN_ULONG old = r[i];
  481. r[i] = old + c;
  482. c = r[i] < old;
  483. }
  484. // The product should fit without carries.
  485. assert(c == 0);
  486. }
  487. // bn_mul_impl implements |BN_mul| and |bn_mul_consttime|. Note this function
  488. // breaks |BIGNUM| invariants and may return a negative zero. This is handled by
  489. // the callers.
  490. static int bn_mul_impl(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  491. BN_CTX *ctx) {
  492. int al = a->width;
  493. int bl = b->width;
  494. if (al == 0 || bl == 0) {
  495. BN_zero(r);
  496. return 1;
  497. }
  498. int ret = 0;
  499. BIGNUM *rr;
  500. BN_CTX_start(ctx);
  501. if (r == a || r == b) {
  502. rr = BN_CTX_get(ctx);
  503. if (r == NULL) {
  504. goto err;
  505. }
  506. } else {
  507. rr = r;
  508. }
  509. rr->neg = a->neg ^ b->neg;
  510. int i = al - bl;
  511. if (i == 0) {
  512. if (al == 8) {
  513. if (!bn_wexpand(rr, 16)) {
  514. goto err;
  515. }
  516. rr->width = 16;
  517. bn_mul_comba8(rr->d, a->d, b->d);
  518. goto end;
  519. }
  520. }
  521. int top = al + bl;
  522. static const int kMulNormalSize = 16;
  523. if (al >= kMulNormalSize && bl >= kMulNormalSize) {
  524. if (-1 <= i && i <= 1) {
  525. // Find the larger power of two less than or equal to the larger length.
  526. int j;
  527. if (i >= 0) {
  528. j = BN_num_bits_word((BN_ULONG)al);
  529. } else {
  530. j = BN_num_bits_word((BN_ULONG)bl);
  531. }
  532. j = 1 << (j - 1);
  533. assert(j <= al || j <= bl);
  534. BIGNUM *t = BN_CTX_get(ctx);
  535. if (t == NULL) {
  536. goto err;
  537. }
  538. if (al > j || bl > j) {
  539. // We know |al| and |bl| are at most one from each other, so if al > j,
  540. // bl >= j, and vice versa. Thus we can use |bn_mul_part_recursive|.
  541. assert(al >= j && bl >= j);
  542. if (!bn_wexpand(t, j * 8) ||
  543. !bn_wexpand(rr, j * 4)) {
  544. goto err;
  545. }
  546. bn_mul_part_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
  547. } else {
  548. // al <= j && bl <= j. Additionally, we know j <= al or j <= bl, so one
  549. // of al - j or bl - j is zero. The other, by the bound on |i| above, is
  550. // zero or -1. Thus, we can use |bn_mul_recursive|.
  551. if (!bn_wexpand(t, j * 4) ||
  552. !bn_wexpand(rr, j * 2)) {
  553. goto err;
  554. }
  555. bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
  556. }
  557. rr->width = top;
  558. goto end;
  559. }
  560. }
  561. if (!bn_wexpand(rr, top)) {
  562. goto err;
  563. }
  564. rr->width = top;
  565. bn_mul_normal(rr->d, a->d, al, b->d, bl);
  566. end:
  567. if (r != rr && !BN_copy(r, rr)) {
  568. goto err;
  569. }
  570. ret = 1;
  571. err:
  572. BN_CTX_end(ctx);
  573. return ret;
  574. }
  575. int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
  576. if (!bn_mul_impl(r, a, b, ctx)) {
  577. return 0;
  578. }
  579. // This additionally fixes any negative zeros created by |bn_mul_impl|.
  580. bn_set_minimal_width(r);
  581. return 1;
  582. }
  583. int bn_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
  584. // Prevent negative zeros.
  585. if (a->neg || b->neg) {
  586. OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
  587. return 0;
  588. }
  589. return bn_mul_impl(r, a, b, ctx);
  590. }
  591. int bn_mul_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a,
  592. const BN_ULONG *b, size_t num_b) {
  593. if (num_r != num_a + num_b) {
  594. OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  595. return 0;
  596. }
  597. // TODO(davidben): Should this call |bn_mul_comba4| too? |BN_mul| does not
  598. // hit that code.
  599. if (num_a == 8 && num_b == 8) {
  600. bn_mul_comba8(r, a, b);
  601. } else {
  602. bn_mul_normal(r, a, num_a, b, num_b);
  603. }
  604. return 1;
  605. }
  606. // tmp must have 2*n words
  607. static void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, size_t n,
  608. BN_ULONG *tmp) {
  609. if (n == 0) {
  610. return;
  611. }
  612. size_t max = n * 2;
  613. const BN_ULONG *ap = a;
  614. BN_ULONG *rp = r;
  615. rp[0] = rp[max - 1] = 0;
  616. rp++;
  617. // Compute the contribution of a[i] * a[j] for all i < j.
  618. if (n > 1) {
  619. ap++;
  620. rp[n - 1] = bn_mul_words(rp, ap, n - 1, ap[-1]);
  621. rp += 2;
  622. }
  623. if (n > 2) {
  624. for (size_t i = n - 2; i > 0; i--) {
  625. ap++;
  626. rp[i] = bn_mul_add_words(rp, ap, i, ap[-1]);
  627. rp += 2;
  628. }
  629. }
  630. // The final result fits in |max| words, so none of the following operations
  631. // will overflow.
  632. // Double |r|, giving the contribution of a[i] * a[j] for all i != j.
  633. bn_add_words(r, r, r, max);
  634. // Add in the contribution of a[i] * a[i] for all i.
  635. bn_sqr_words(tmp, a, n);
  636. bn_add_words(r, r, tmp, max);
  637. }
  638. // bn_sqr_recursive sets |r| to |a|^2, using |t| as scratch space. |r| has
  639. // length 2*|n2|, |a| has length |n2|, and |t| has length 4*|n2|. |n2| must be
  640. // a power of two.
  641. static void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, size_t n2,
  642. BN_ULONG *t) {
  643. // |n2| is a power of two.
  644. assert(n2 != 0 && (n2 & (n2 - 1)) == 0);
  645. if (n2 == 4) {
  646. bn_sqr_comba4(r, a);
  647. return;
  648. }
  649. if (n2 == 8) {
  650. bn_sqr_comba8(r, a);
  651. return;
  652. }
  653. if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) {
  654. bn_sqr_normal(r, a, n2, t);
  655. return;
  656. }
  657. // Split |a| into a0,a1, each of size |n|.
  658. // Split |t| into t0,t1,t2,t3, each of size |n|, with the remaining 4*|n| used
  659. // for recursive calls.
  660. // Split |r| into r0,r1,r2,r3. We must contribute a0^2 to r0,r1, 2*a0*a1 to
  661. // r1,r2, and a1^2 to r2,r3.
  662. size_t n = n2 / 2;
  663. BN_ULONG *t_recursive = &t[n2 * 2];
  664. // t0 = |a0 - a1|.
  665. bn_abs_sub_words(t, a, &a[n], n, &t[n]);
  666. // t2,t3 = t0^2 = |a0 - a1|^2 = a0^2 - 2*a0*a1 + a1^2
  667. bn_sqr_recursive(&t[n2], t, n, t_recursive);
  668. // r0,r1 = a0^2
  669. bn_sqr_recursive(r, a, n, t_recursive);
  670. // r2,r3 = a1^2
  671. bn_sqr_recursive(&r[n2], &a[n], n, t_recursive);
  672. // t0,t1,c = r0,r1 + r2,r3 = a0^2 + a1^2
  673. BN_ULONG c = bn_add_words(t, r, &r[n2], n2);
  674. // t2,t3,c = t0,t1,c - t2,t3 = 2*a0*a1
  675. c -= bn_sub_words(&t[n2], t, &t[n2], n2);
  676. // We now have our three components. Add them together.
  677. // r1,r2,c = r1,r2 + t2,t3,c
  678. c += bn_add_words(&r[n], &r[n], &t[n2], n2);
  679. // Propagate the carry bit to the end.
  680. for (size_t i = n + n2; i < n2 + n2; i++) {
  681. BN_ULONG old = r[i];
  682. r[i] = old + c;
  683. c = r[i] < old;
  684. }
  685. // The square should fit without carries.
  686. assert(c == 0);
  687. }
  688. int BN_mul_word(BIGNUM *bn, BN_ULONG w) {
  689. if (!bn->width) {
  690. return 1;
  691. }
  692. if (w == 0) {
  693. BN_zero(bn);
  694. return 1;
  695. }
  696. BN_ULONG ll = bn_mul_words(bn->d, bn->d, bn->width, w);
  697. if (ll) {
  698. if (!bn_wexpand(bn, bn->width + 1)) {
  699. return 0;
  700. }
  701. bn->d[bn->width++] = ll;
  702. }
  703. return 1;
  704. }
  705. int bn_sqr_consttime(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) {
  706. int al = a->width;
  707. if (al <= 0) {
  708. r->width = 0;
  709. r->neg = 0;
  710. return 1;
  711. }
  712. int ret = 0;
  713. BN_CTX_start(ctx);
  714. BIGNUM *rr = (a != r) ? r : BN_CTX_get(ctx);
  715. BIGNUM *tmp = BN_CTX_get(ctx);
  716. if (!rr || !tmp) {
  717. goto err;
  718. }
  719. int max = 2 * al; // Non-zero (from above)
  720. if (!bn_wexpand(rr, max)) {
  721. goto err;
  722. }
  723. if (al == 4) {
  724. bn_sqr_comba4(rr->d, a->d);
  725. } else if (al == 8) {
  726. bn_sqr_comba8(rr->d, a->d);
  727. } else {
  728. if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {
  729. BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];
  730. bn_sqr_normal(rr->d, a->d, al, t);
  731. } else {
  732. // If |al| is a power of two, we can use |bn_sqr_recursive|.
  733. if (al != 0 && (al & (al - 1)) == 0) {
  734. if (!bn_wexpand(tmp, al * 4)) {
  735. goto err;
  736. }
  737. bn_sqr_recursive(rr->d, a->d, al, tmp->d);
  738. } else {
  739. if (!bn_wexpand(tmp, max)) {
  740. goto err;
  741. }
  742. bn_sqr_normal(rr->d, a->d, al, tmp->d);
  743. }
  744. }
  745. }
  746. rr->neg = 0;
  747. rr->width = max;
  748. if (rr != r && !BN_copy(r, rr)) {
  749. goto err;
  750. }
  751. ret = 1;
  752. err:
  753. BN_CTX_end(ctx);
  754. return ret;
  755. }
  756. int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) {
  757. if (!bn_sqr_consttime(r, a, ctx)) {
  758. return 0;
  759. }
  760. bn_set_minimal_width(r);
  761. return 1;
  762. }
  763. int bn_sqr_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a) {
  764. if (num_r != 2 * num_a || num_a > BN_SMALL_MAX_WORDS) {
  765. OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  766. return 0;
  767. }
  768. if (num_a == 4) {
  769. bn_sqr_comba4(r, a);
  770. } else if (num_a == 8) {
  771. bn_sqr_comba8(r, a);
  772. } else {
  773. BN_ULONG tmp[2 * BN_SMALL_MAX_WORDS];
  774. bn_sqr_normal(r, a, num_a, tmp);
  775. OPENSSL_cleanse(tmp, 2 * num_a * sizeof(BN_ULONG));
  776. }
  777. return 1;
  778. }