bn_asm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /* crypto/bn/bn_asm.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #ifndef BN_DEBUG
  59. # undef NDEBUG /* avoid conflicting definitions */
  60. # define NDEBUG
  61. #endif
  62. #include <stdio.h>
  63. #include <assert.h>
  64. #include <openssl/crypto.h>
  65. #include "cryptlib.h"
  66. #include "bn_lcl.h"
  67. #if defined(BN_LLONG) || defined(BN_UMULT_HIGH)
  68. BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
  69. {
  70. BN_ULONG c1=0;
  71. assert(num >= 0);
  72. if (num <= 0) return(c1);
  73. #ifndef OPENSSL_SMALL_FOOTPRINT
  74. while (num&~3)
  75. {
  76. mul_add(rp[0],ap[0],w,c1);
  77. mul_add(rp[1],ap[1],w,c1);
  78. mul_add(rp[2],ap[2],w,c1);
  79. mul_add(rp[3],ap[3],w,c1);
  80. ap+=4; rp+=4; num-=4;
  81. }
  82. #endif
  83. while (num)
  84. {
  85. mul_add(rp[0],ap[0],w,c1);
  86. ap++; rp++; num--;
  87. }
  88. return(c1);
  89. }
  90. BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
  91. {
  92. BN_ULONG c1=0;
  93. assert(num >= 0);
  94. if (num <= 0) return(c1);
  95. #ifndef OPENSSL_SMALL_FOOTPRINT
  96. while (num&~3)
  97. {
  98. mul(rp[0],ap[0],w,c1);
  99. mul(rp[1],ap[1],w,c1);
  100. mul(rp[2],ap[2],w,c1);
  101. mul(rp[3],ap[3],w,c1);
  102. ap+=4; rp+=4; num-=4;
  103. }
  104. #endif
  105. while (num)
  106. {
  107. mul(rp[0],ap[0],w,c1);
  108. ap++; rp++; num--;
  109. }
  110. return(c1);
  111. }
  112. void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
  113. {
  114. assert(n >= 0);
  115. if (n <= 0) return;
  116. #ifndef OPENSSL_SMALL_FOOTPRINT
  117. while (n&~3)
  118. {
  119. sqr(r[0],r[1],a[0]);
  120. sqr(r[2],r[3],a[1]);
  121. sqr(r[4],r[5],a[2]);
  122. sqr(r[6],r[7],a[3]);
  123. a+=4; r+=8; n-=4;
  124. }
  125. #endif
  126. while (n)
  127. {
  128. sqr(r[0],r[1],a[0]);
  129. a++; r+=2; n--;
  130. }
  131. }
  132. #else /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */
  133. BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
  134. {
  135. BN_ULONG c=0;
  136. BN_ULONG bl,bh;
  137. assert(num >= 0);
  138. if (num <= 0) return((BN_ULONG)0);
  139. bl=LBITS(w);
  140. bh=HBITS(w);
  141. #ifndef OPENSSL_SMALL_FOOTPRINT
  142. while (num&~3)
  143. {
  144. mul_add(rp[0],ap[0],bl,bh,c);
  145. mul_add(rp[1],ap[1],bl,bh,c);
  146. mul_add(rp[2],ap[2],bl,bh,c);
  147. mul_add(rp[3],ap[3],bl,bh,c);
  148. ap+=4; rp+=4; num-=4;
  149. }
  150. #endif
  151. while (num)
  152. {
  153. mul_add(rp[0],ap[0],bl,bh,c);
  154. ap++; rp++; num--;
  155. }
  156. return(c);
  157. }
  158. BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
  159. {
  160. BN_ULONG carry=0;
  161. BN_ULONG bl,bh;
  162. assert(num >= 0);
  163. if (num <= 0) return((BN_ULONG)0);
  164. bl=LBITS(w);
  165. bh=HBITS(w);
  166. #ifndef OPENSSL_SMALL_FOOTPRINT
  167. while (num&~3)
  168. {
  169. mul(rp[0],ap[0],bl,bh,carry);
  170. mul(rp[1],ap[1],bl,bh,carry);
  171. mul(rp[2],ap[2],bl,bh,carry);
  172. mul(rp[3],ap[3],bl,bh,carry);
  173. ap+=4; rp+=4; num-=4;
  174. }
  175. #endif
  176. while (num)
  177. {
  178. mul(rp[0],ap[0],bl,bh,carry);
  179. ap++; rp++; num--;
  180. }
  181. return(carry);
  182. }
  183. void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
  184. {
  185. assert(n >= 0);
  186. if (n <= 0) return;
  187. #ifndef OPENSSL_SMALL_FOOTPRINT
  188. while (n&~3)
  189. {
  190. sqr64(r[0],r[1],a[0]);
  191. sqr64(r[2],r[3],a[1]);
  192. sqr64(r[4],r[5],a[2]);
  193. sqr64(r[6],r[7],a[3]);
  194. a+=4; r+=8; n-=4;
  195. }
  196. #endif
  197. while (n)
  198. {
  199. sqr64(r[0],r[1],a[0]);
  200. a++; r+=2; n--;
  201. }
  202. }
  203. #endif /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */
  204. #if defined(BN_LLONG) && defined(BN_DIV2W)
  205. BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
  206. {
  207. return((BN_ULONG)(((((BN_ULLONG)h)<<BN_BITS2)|l)/(BN_ULLONG)d));
  208. }
  209. #else
  210. /* Divide h,l by d and return the result. */
  211. /* I need to test this some more :-( */
  212. BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
  213. {
  214. BN_ULONG dh,dl,q,ret=0,th,tl,t;
  215. int i,count=2;
  216. if (d == 0) return(BN_MASK2);
  217. i=BN_num_bits_word(d);
  218. assert((i == BN_BITS2) || (h <= (BN_ULONG)1<<i));
  219. i=BN_BITS2-i;
  220. if (h >= d) h-=d;
  221. if (i)
  222. {
  223. d<<=i;
  224. h=(h<<i)|(l>>(BN_BITS2-i));
  225. l<<=i;
  226. }
  227. dh=(d&BN_MASK2h)>>BN_BITS4;
  228. dl=(d&BN_MASK2l);
  229. for (;;)
  230. {
  231. if ((h>>BN_BITS4) == dh)
  232. q=BN_MASK2l;
  233. else
  234. q=h/dh;
  235. th=q*dh;
  236. tl=dl*q;
  237. for (;;)
  238. {
  239. t=h-th;
  240. if ((t&BN_MASK2h) ||
  241. ((tl) <= (
  242. (t<<BN_BITS4)|
  243. ((l&BN_MASK2h)>>BN_BITS4))))
  244. break;
  245. q--;
  246. th-=dh;
  247. tl-=dl;
  248. }
  249. t=(tl>>BN_BITS4);
  250. tl=(tl<<BN_BITS4)&BN_MASK2h;
  251. th+=t;
  252. if (l < tl) th++;
  253. l-=tl;
  254. if (h < th)
  255. {
  256. h+=d;
  257. q--;
  258. }
  259. h-=th;
  260. if (--count == 0) break;
  261. ret=q<<BN_BITS4;
  262. h=((h<<BN_BITS4)|(l>>BN_BITS4))&BN_MASK2;
  263. l=(l&BN_MASK2l)<<BN_BITS4;
  264. }
  265. ret|=q;
  266. return(ret);
  267. }
  268. #endif /* !defined(BN_LLONG) && defined(BN_DIV2W) */
  269. #ifdef BN_LLONG
  270. BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
  271. {
  272. BN_ULLONG ll=0;
  273. assert(n >= 0);
  274. if (n <= 0) return((BN_ULONG)0);
  275. #ifndef OPENSSL_SMALL_FOOTPRINT
  276. while (n&~3)
  277. {
  278. ll+=(BN_ULLONG)a[0]+b[0];
  279. r[0]=(BN_ULONG)ll&BN_MASK2;
  280. ll>>=BN_BITS2;
  281. ll+=(BN_ULLONG)a[1]+b[1];
  282. r[1]=(BN_ULONG)ll&BN_MASK2;
  283. ll>>=BN_BITS2;
  284. ll+=(BN_ULLONG)a[2]+b[2];
  285. r[2]=(BN_ULONG)ll&BN_MASK2;
  286. ll>>=BN_BITS2;
  287. ll+=(BN_ULLONG)a[3]+b[3];
  288. r[3]=(BN_ULONG)ll&BN_MASK2;
  289. ll>>=BN_BITS2;
  290. a+=4; b+=4; r+=4; n-=4;
  291. }
  292. #endif
  293. while (n)
  294. {
  295. ll+=(BN_ULLONG)a[0]+b[0];
  296. r[0]=(BN_ULONG)ll&BN_MASK2;
  297. ll>>=BN_BITS2;
  298. a++; b++; r++; n--;
  299. }
  300. return((BN_ULONG)ll);
  301. }
  302. #else /* !BN_LLONG */
  303. BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
  304. {
  305. BN_ULONG c,l,t;
  306. assert(n >= 0);
  307. if (n <= 0) return((BN_ULONG)0);
  308. c=0;
  309. #ifndef OPENSSL_SMALL_FOOTPRINT
  310. while (n&~3)
  311. {
  312. t=a[0];
  313. t=(t+c)&BN_MASK2;
  314. c=(t < c);
  315. l=(t+b[0])&BN_MASK2;
  316. c+=(l < t);
  317. r[0]=l;
  318. t=a[1];
  319. t=(t+c)&BN_MASK2;
  320. c=(t < c);
  321. l=(t+b[1])&BN_MASK2;
  322. c+=(l < t);
  323. r[1]=l;
  324. t=a[2];
  325. t=(t+c)&BN_MASK2;
  326. c=(t < c);
  327. l=(t+b[2])&BN_MASK2;
  328. c+=(l < t);
  329. r[2]=l;
  330. t=a[3];
  331. t=(t+c)&BN_MASK2;
  332. c=(t < c);
  333. l=(t+b[3])&BN_MASK2;
  334. c+=(l < t);
  335. r[3]=l;
  336. a+=4; b+=4; r+=4; n-=4;
  337. }
  338. #endif
  339. while(n)
  340. {
  341. t=a[0];
  342. t=(t+c)&BN_MASK2;
  343. c=(t < c);
  344. l=(t+b[0])&BN_MASK2;
  345. c+=(l < t);
  346. r[0]=l;
  347. a++; b++; r++; n--;
  348. }
  349. return((BN_ULONG)c);
  350. }
  351. #endif /* !BN_LLONG */
  352. BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
  353. {
  354. BN_ULONG t1,t2;
  355. int c=0;
  356. assert(n >= 0);
  357. if (n <= 0) return((BN_ULONG)0);
  358. #ifndef OPENSSL_SMALL_FOOTPRINT
  359. while (n&~3)
  360. {
  361. t1=a[0]; t2=b[0];
  362. r[0]=(t1-t2-c)&BN_MASK2;
  363. if (t1 != t2) c=(t1 < t2);
  364. t1=a[1]; t2=b[1];
  365. r[1]=(t1-t2-c)&BN_MASK2;
  366. if (t1 != t2) c=(t1 < t2);
  367. t1=a[2]; t2=b[2];
  368. r[2]=(t1-t2-c)&BN_MASK2;
  369. if (t1 != t2) c=(t1 < t2);
  370. t1=a[3]; t2=b[3];
  371. r[3]=(t1-t2-c)&BN_MASK2;
  372. if (t1 != t2) c=(t1 < t2);
  373. a+=4; b+=4; r+=4; n-=4;
  374. }
  375. #endif
  376. while (n)
  377. {
  378. t1=a[0]; t2=b[0];
  379. r[0]=(t1-t2-c)&BN_MASK2;
  380. if (t1 != t2) c=(t1 < t2);
  381. a++; b++; r++; n--;
  382. }
  383. return(c);
  384. }
  385. #if defined(BN_MUL_COMBA) && !defined(OPENSSL_SMALL_FOOTPRINT)
  386. #ifndef OPENSSL_FIPSCANISTER
  387. #undef bn_mul_comba8
  388. #undef bn_mul_comba4
  389. #undef bn_sqr_comba8
  390. #undef bn_sqr_comba4
  391. #endif
  392. /* mul_add_c(a,b,c0,c1,c2) -- c+=a*b for three word number c=(c2,c1,c0) */
  393. /* mul_add_c2(a,b,c0,c1,c2) -- c+=2*a*b for three word number c=(c2,c1,c0) */
  394. /* sqr_add_c(a,i,c0,c1,c2) -- c+=a[i]^2 for three word number c=(c2,c1,c0) */
  395. /* sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number c=(c2,c1,c0) */
  396. #ifdef BN_LLONG
  397. #define mul_add_c(a,b,c0,c1,c2) \
  398. t=(BN_ULLONG)a*b; \
  399. t1=(BN_ULONG)Lw(t); \
  400. t2=(BN_ULONG)Hw(t); \
  401. c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
  402. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  403. #define mul_add_c2(a,b,c0,c1,c2) \
  404. t=(BN_ULLONG)a*b; \
  405. tt=(t+t)&BN_MASK; \
  406. if (tt < t) c2++; \
  407. t1=(BN_ULONG)Lw(tt); \
  408. t2=(BN_ULONG)Hw(tt); \
  409. c0=(c0+t1)&BN_MASK2; \
  410. if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \
  411. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  412. #define sqr_add_c(a,i,c0,c1,c2) \
  413. t=(BN_ULLONG)a[i]*a[i]; \
  414. t1=(BN_ULONG)Lw(t); \
  415. t2=(BN_ULONG)Hw(t); \
  416. c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
  417. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  418. #define sqr_add_c2(a,i,j,c0,c1,c2) \
  419. mul_add_c2((a)[i],(a)[j],c0,c1,c2)
  420. #elif defined(BN_UMULT_LOHI)
  421. #define mul_add_c(a,b,c0,c1,c2) { \
  422. BN_ULONG ta=(a),tb=(b); \
  423. BN_UMULT_LOHI(t1,t2,ta,tb); \
  424. c0 += t1; t2 += (c0<t1)?1:0; \
  425. c1 += t2; c2 += (c1<t2)?1:0; \
  426. }
  427. #define mul_add_c2(a,b,c0,c1,c2) { \
  428. BN_ULONG ta=(a),tb=(b),t0; \
  429. BN_UMULT_LOHI(t0,t1,ta,tb); \
  430. t2 = t1+t1; c2 += (t2<t1)?1:0; \
  431. t1 = t0+t0; t2 += (t1<t0)?1:0; \
  432. c0 += t1; t2 += (c0<t1)?1:0; \
  433. c1 += t2; c2 += (c1<t2)?1:0; \
  434. }
  435. #define sqr_add_c(a,i,c0,c1,c2) { \
  436. BN_ULONG ta=(a)[i]; \
  437. BN_UMULT_LOHI(t1,t2,ta,ta); \
  438. c0 += t1; t2 += (c0<t1)?1:0; \
  439. c1 += t2; c2 += (c1<t2)?1:0; \
  440. }
  441. #define sqr_add_c2(a,i,j,c0,c1,c2) \
  442. mul_add_c2((a)[i],(a)[j],c0,c1,c2)
  443. #elif defined(BN_UMULT_HIGH)
  444. #define mul_add_c(a,b,c0,c1,c2) { \
  445. BN_ULONG ta=(a),tb=(b); \
  446. t1 = ta * tb; \
  447. t2 = BN_UMULT_HIGH(ta,tb); \
  448. c0 += t1; t2 += (c0<t1)?1:0; \
  449. c1 += t2; c2 += (c1<t2)?1:0; \
  450. }
  451. #define mul_add_c2(a,b,c0,c1,c2) { \
  452. BN_ULONG ta=(a),tb=(b),t0; \
  453. t1 = BN_UMULT_HIGH(ta,tb); \
  454. t0 = ta * tb; \
  455. t2 = t1+t1; c2 += (t2<t1)?1:0; \
  456. t1 = t0+t0; t2 += (t1<t0)?1:0; \
  457. c0 += t1; t2 += (c0<t1)?1:0; \
  458. c1 += t2; c2 += (c1<t2)?1:0; \
  459. }
  460. #define sqr_add_c(a,i,c0,c1,c2) { \
  461. BN_ULONG ta=(a)[i]; \
  462. t1 = ta * ta; \
  463. t2 = BN_UMULT_HIGH(ta,ta); \
  464. c0 += t1; t2 += (c0<t1)?1:0; \
  465. c1 += t2; c2 += (c1<t2)?1:0; \
  466. }
  467. #define sqr_add_c2(a,i,j,c0,c1,c2) \
  468. mul_add_c2((a)[i],(a)[j],c0,c1,c2)
  469. #else /* !BN_LLONG */
  470. #define mul_add_c(a,b,c0,c1,c2) \
  471. t1=LBITS(a); t2=HBITS(a); \
  472. bl=LBITS(b); bh=HBITS(b); \
  473. mul64(t1,t2,bl,bh); \
  474. c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
  475. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  476. #define mul_add_c2(a,b,c0,c1,c2) \
  477. t1=LBITS(a); t2=HBITS(a); \
  478. bl=LBITS(b); bh=HBITS(b); \
  479. mul64(t1,t2,bl,bh); \
  480. if (t2 & BN_TBIT) c2++; \
  481. t2=(t2+t2)&BN_MASK2; \
  482. if (t1 & BN_TBIT) t2++; \
  483. t1=(t1+t1)&BN_MASK2; \
  484. c0=(c0+t1)&BN_MASK2; \
  485. if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \
  486. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  487. #define sqr_add_c(a,i,c0,c1,c2) \
  488. sqr64(t1,t2,(a)[i]); \
  489. c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
  490. c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
  491. #define sqr_add_c2(a,i,j,c0,c1,c2) \
  492. mul_add_c2((a)[i],(a)[j],c0,c1,c2)
  493. #endif /* !BN_LLONG */
  494. void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  495. {
  496. #ifdef BN_LLONG
  497. BN_ULLONG t;
  498. #else
  499. BN_ULONG bl,bh;
  500. #endif
  501. BN_ULONG t1,t2;
  502. BN_ULONG c1,c2,c3;
  503. c1=0;
  504. c2=0;
  505. c3=0;
  506. mul_add_c(a[0],b[0],c1,c2,c3);
  507. r[0]=c1;
  508. c1=0;
  509. mul_add_c(a[0],b[1],c2,c3,c1);
  510. mul_add_c(a[1],b[0],c2,c3,c1);
  511. r[1]=c2;
  512. c2=0;
  513. mul_add_c(a[2],b[0],c3,c1,c2);
  514. mul_add_c(a[1],b[1],c3,c1,c2);
  515. mul_add_c(a[0],b[2],c3,c1,c2);
  516. r[2]=c3;
  517. c3=0;
  518. mul_add_c(a[0],b[3],c1,c2,c3);
  519. mul_add_c(a[1],b[2],c1,c2,c3);
  520. mul_add_c(a[2],b[1],c1,c2,c3);
  521. mul_add_c(a[3],b[0],c1,c2,c3);
  522. r[3]=c1;
  523. c1=0;
  524. mul_add_c(a[4],b[0],c2,c3,c1);
  525. mul_add_c(a[3],b[1],c2,c3,c1);
  526. mul_add_c(a[2],b[2],c2,c3,c1);
  527. mul_add_c(a[1],b[3],c2,c3,c1);
  528. mul_add_c(a[0],b[4],c2,c3,c1);
  529. r[4]=c2;
  530. c2=0;
  531. mul_add_c(a[0],b[5],c3,c1,c2);
  532. mul_add_c(a[1],b[4],c3,c1,c2);
  533. mul_add_c(a[2],b[3],c3,c1,c2);
  534. mul_add_c(a[3],b[2],c3,c1,c2);
  535. mul_add_c(a[4],b[1],c3,c1,c2);
  536. mul_add_c(a[5],b[0],c3,c1,c2);
  537. r[5]=c3;
  538. c3=0;
  539. mul_add_c(a[6],b[0],c1,c2,c3);
  540. mul_add_c(a[5],b[1],c1,c2,c3);
  541. mul_add_c(a[4],b[2],c1,c2,c3);
  542. mul_add_c(a[3],b[3],c1,c2,c3);
  543. mul_add_c(a[2],b[4],c1,c2,c3);
  544. mul_add_c(a[1],b[5],c1,c2,c3);
  545. mul_add_c(a[0],b[6],c1,c2,c3);
  546. r[6]=c1;
  547. c1=0;
  548. mul_add_c(a[0],b[7],c2,c3,c1);
  549. mul_add_c(a[1],b[6],c2,c3,c1);
  550. mul_add_c(a[2],b[5],c2,c3,c1);
  551. mul_add_c(a[3],b[4],c2,c3,c1);
  552. mul_add_c(a[4],b[3],c2,c3,c1);
  553. mul_add_c(a[5],b[2],c2,c3,c1);
  554. mul_add_c(a[6],b[1],c2,c3,c1);
  555. mul_add_c(a[7],b[0],c2,c3,c1);
  556. r[7]=c2;
  557. c2=0;
  558. mul_add_c(a[7],b[1],c3,c1,c2);
  559. mul_add_c(a[6],b[2],c3,c1,c2);
  560. mul_add_c(a[5],b[3],c3,c1,c2);
  561. mul_add_c(a[4],b[4],c3,c1,c2);
  562. mul_add_c(a[3],b[5],c3,c1,c2);
  563. mul_add_c(a[2],b[6],c3,c1,c2);
  564. mul_add_c(a[1],b[7],c3,c1,c2);
  565. r[8]=c3;
  566. c3=0;
  567. mul_add_c(a[2],b[7],c1,c2,c3);
  568. mul_add_c(a[3],b[6],c1,c2,c3);
  569. mul_add_c(a[4],b[5],c1,c2,c3);
  570. mul_add_c(a[5],b[4],c1,c2,c3);
  571. mul_add_c(a[6],b[3],c1,c2,c3);
  572. mul_add_c(a[7],b[2],c1,c2,c3);
  573. r[9]=c1;
  574. c1=0;
  575. mul_add_c(a[7],b[3],c2,c3,c1);
  576. mul_add_c(a[6],b[4],c2,c3,c1);
  577. mul_add_c(a[5],b[5],c2,c3,c1);
  578. mul_add_c(a[4],b[6],c2,c3,c1);
  579. mul_add_c(a[3],b[7],c2,c3,c1);
  580. r[10]=c2;
  581. c2=0;
  582. mul_add_c(a[4],b[7],c3,c1,c2);
  583. mul_add_c(a[5],b[6],c3,c1,c2);
  584. mul_add_c(a[6],b[5],c3,c1,c2);
  585. mul_add_c(a[7],b[4],c3,c1,c2);
  586. r[11]=c3;
  587. c3=0;
  588. mul_add_c(a[7],b[5],c1,c2,c3);
  589. mul_add_c(a[6],b[6],c1,c2,c3);
  590. mul_add_c(a[5],b[7],c1,c2,c3);
  591. r[12]=c1;
  592. c1=0;
  593. mul_add_c(a[6],b[7],c2,c3,c1);
  594. mul_add_c(a[7],b[6],c2,c3,c1);
  595. r[13]=c2;
  596. c2=0;
  597. mul_add_c(a[7],b[7],c3,c1,c2);
  598. r[14]=c3;
  599. r[15]=c1;
  600. }
  601. void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  602. {
  603. #ifdef BN_LLONG
  604. BN_ULLONG t;
  605. #else
  606. BN_ULONG bl,bh;
  607. #endif
  608. BN_ULONG t1,t2;
  609. BN_ULONG c1,c2,c3;
  610. c1=0;
  611. c2=0;
  612. c3=0;
  613. mul_add_c(a[0],b[0],c1,c2,c3);
  614. r[0]=c1;
  615. c1=0;
  616. mul_add_c(a[0],b[1],c2,c3,c1);
  617. mul_add_c(a[1],b[0],c2,c3,c1);
  618. r[1]=c2;
  619. c2=0;
  620. mul_add_c(a[2],b[0],c3,c1,c2);
  621. mul_add_c(a[1],b[1],c3,c1,c2);
  622. mul_add_c(a[0],b[2],c3,c1,c2);
  623. r[2]=c3;
  624. c3=0;
  625. mul_add_c(a[0],b[3],c1,c2,c3);
  626. mul_add_c(a[1],b[2],c1,c2,c3);
  627. mul_add_c(a[2],b[1],c1,c2,c3);
  628. mul_add_c(a[3],b[0],c1,c2,c3);
  629. r[3]=c1;
  630. c1=0;
  631. mul_add_c(a[3],b[1],c2,c3,c1);
  632. mul_add_c(a[2],b[2],c2,c3,c1);
  633. mul_add_c(a[1],b[3],c2,c3,c1);
  634. r[4]=c2;
  635. c2=0;
  636. mul_add_c(a[2],b[3],c3,c1,c2);
  637. mul_add_c(a[3],b[2],c3,c1,c2);
  638. r[5]=c3;
  639. c3=0;
  640. mul_add_c(a[3],b[3],c1,c2,c3);
  641. r[6]=c1;
  642. r[7]=c2;
  643. }
  644. void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
  645. {
  646. #ifdef BN_LLONG
  647. BN_ULLONG t,tt;
  648. #else
  649. BN_ULONG bl,bh;
  650. #endif
  651. BN_ULONG t1,t2;
  652. BN_ULONG c1,c2,c3;
  653. c1=0;
  654. c2=0;
  655. c3=0;
  656. sqr_add_c(a,0,c1,c2,c3);
  657. r[0]=c1;
  658. c1=0;
  659. sqr_add_c2(a,1,0,c2,c3,c1);
  660. r[1]=c2;
  661. c2=0;
  662. sqr_add_c(a,1,c3,c1,c2);
  663. sqr_add_c2(a,2,0,c3,c1,c2);
  664. r[2]=c3;
  665. c3=0;
  666. sqr_add_c2(a,3,0,c1,c2,c3);
  667. sqr_add_c2(a,2,1,c1,c2,c3);
  668. r[3]=c1;
  669. c1=0;
  670. sqr_add_c(a,2,c2,c3,c1);
  671. sqr_add_c2(a,3,1,c2,c3,c1);
  672. sqr_add_c2(a,4,0,c2,c3,c1);
  673. r[4]=c2;
  674. c2=0;
  675. sqr_add_c2(a,5,0,c3,c1,c2);
  676. sqr_add_c2(a,4,1,c3,c1,c2);
  677. sqr_add_c2(a,3,2,c3,c1,c2);
  678. r[5]=c3;
  679. c3=0;
  680. sqr_add_c(a,3,c1,c2,c3);
  681. sqr_add_c2(a,4,2,c1,c2,c3);
  682. sqr_add_c2(a,5,1,c1,c2,c3);
  683. sqr_add_c2(a,6,0,c1,c2,c3);
  684. r[6]=c1;
  685. c1=0;
  686. sqr_add_c2(a,7,0,c2,c3,c1);
  687. sqr_add_c2(a,6,1,c2,c3,c1);
  688. sqr_add_c2(a,5,2,c2,c3,c1);
  689. sqr_add_c2(a,4,3,c2,c3,c1);
  690. r[7]=c2;
  691. c2=0;
  692. sqr_add_c(a,4,c3,c1,c2);
  693. sqr_add_c2(a,5,3,c3,c1,c2);
  694. sqr_add_c2(a,6,2,c3,c1,c2);
  695. sqr_add_c2(a,7,1,c3,c1,c2);
  696. r[8]=c3;
  697. c3=0;
  698. sqr_add_c2(a,7,2,c1,c2,c3);
  699. sqr_add_c2(a,6,3,c1,c2,c3);
  700. sqr_add_c2(a,5,4,c1,c2,c3);
  701. r[9]=c1;
  702. c1=0;
  703. sqr_add_c(a,5,c2,c3,c1);
  704. sqr_add_c2(a,6,4,c2,c3,c1);
  705. sqr_add_c2(a,7,3,c2,c3,c1);
  706. r[10]=c2;
  707. c2=0;
  708. sqr_add_c2(a,7,4,c3,c1,c2);
  709. sqr_add_c2(a,6,5,c3,c1,c2);
  710. r[11]=c3;
  711. c3=0;
  712. sqr_add_c(a,6,c1,c2,c3);
  713. sqr_add_c2(a,7,5,c1,c2,c3);
  714. r[12]=c1;
  715. c1=0;
  716. sqr_add_c2(a,7,6,c2,c3,c1);
  717. r[13]=c2;
  718. c2=0;
  719. sqr_add_c(a,7,c3,c1,c2);
  720. r[14]=c3;
  721. r[15]=c1;
  722. }
  723. void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
  724. {
  725. #ifdef BN_LLONG
  726. BN_ULLONG t,tt;
  727. #else
  728. BN_ULONG bl,bh;
  729. #endif
  730. BN_ULONG t1,t2;
  731. BN_ULONG c1,c2,c3;
  732. c1=0;
  733. c2=0;
  734. c3=0;
  735. sqr_add_c(a,0,c1,c2,c3);
  736. r[0]=c1;
  737. c1=0;
  738. sqr_add_c2(a,1,0,c2,c3,c1);
  739. r[1]=c2;
  740. c2=0;
  741. sqr_add_c(a,1,c3,c1,c2);
  742. sqr_add_c2(a,2,0,c3,c1,c2);
  743. r[2]=c3;
  744. c3=0;
  745. sqr_add_c2(a,3,0,c1,c2,c3);
  746. sqr_add_c2(a,2,1,c1,c2,c3);
  747. r[3]=c1;
  748. c1=0;
  749. sqr_add_c(a,2,c2,c3,c1);
  750. sqr_add_c2(a,3,1,c2,c3,c1);
  751. r[4]=c2;
  752. c2=0;
  753. sqr_add_c2(a,3,2,c3,c1,c2);
  754. r[5]=c3;
  755. c3=0;
  756. sqr_add_c(a,3,c1,c2,c3);
  757. r[6]=c1;
  758. r[7]=c2;
  759. }
  760. #ifdef OPENSSL_NO_ASM
  761. #ifdef OPENSSL_BN_ASM_MONT
  762. #include <alloca.h>
  763. /*
  764. * This is essentially reference implementation, which may or may not
  765. * result in performance improvement. E.g. on IA-32 this routine was
  766. * observed to give 40% faster rsa1024 private key operations and 10%
  767. * faster rsa4096 ones, while on AMD64 it improves rsa1024 sign only
  768. * by 10% and *worsens* rsa4096 sign by 15%. Once again, it's a
  769. * reference implementation, one to be used as starting point for
  770. * platform-specific assembler. Mentioned numbers apply to compiler
  771. * generated code compiled with and without -DOPENSSL_BN_ASM_MONT and
  772. * can vary not only from platform to platform, but even for compiler
  773. * versions. Assembler vs. assembler improvement coefficients can
  774. * [and are known to] differ and are to be documented elsewhere.
  775. */
  776. int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0p, int num)
  777. {
  778. BN_ULONG c0,c1,ml,*tp,n0;
  779. #ifdef mul64
  780. BN_ULONG mh;
  781. #endif
  782. volatile BN_ULONG *vp;
  783. int i=0,j;
  784. #if 0 /* template for platform-specific implementation */
  785. if (ap==bp) return bn_sqr_mont(rp,ap,np,n0p,num);
  786. #endif
  787. vp = tp = alloca((num+2)*sizeof(BN_ULONG));
  788. n0 = *n0p;
  789. c0 = 0;
  790. ml = bp[0];
  791. #ifdef mul64
  792. mh = HBITS(ml);
  793. ml = LBITS(ml);
  794. for (j=0;j<num;++j)
  795. mul(tp[j],ap[j],ml,mh,c0);
  796. #else
  797. for (j=0;j<num;++j)
  798. mul(tp[j],ap[j],ml,c0);
  799. #endif
  800. tp[num] = c0;
  801. tp[num+1] = 0;
  802. goto enter;
  803. for(i=0;i<num;i++)
  804. {
  805. c0 = 0;
  806. ml = bp[i];
  807. #ifdef mul64
  808. mh = HBITS(ml);
  809. ml = LBITS(ml);
  810. for (j=0;j<num;++j)
  811. mul_add(tp[j],ap[j],ml,mh,c0);
  812. #else
  813. for (j=0;j<num;++j)
  814. mul_add(tp[j],ap[j],ml,c0);
  815. #endif
  816. c1 = (tp[num] + c0)&BN_MASK2;
  817. tp[num] = c1;
  818. tp[num+1] = (c1<c0?1:0);
  819. enter:
  820. c1 = tp[0];
  821. ml = (c1*n0)&BN_MASK2;
  822. c0 = 0;
  823. #ifdef mul64
  824. mh = HBITS(ml);
  825. ml = LBITS(ml);
  826. mul_add(c1,np[0],ml,mh,c0);
  827. #else
  828. mul_add(c1,ml,np[0],c0);
  829. #endif
  830. for(j=1;j<num;j++)
  831. {
  832. c1 = tp[j];
  833. #ifdef mul64
  834. mul_add(c1,np[j],ml,mh,c0);
  835. #else
  836. mul_add(c1,ml,np[j],c0);
  837. #endif
  838. tp[j-1] = c1&BN_MASK2;
  839. }
  840. c1 = (tp[num] + c0)&BN_MASK2;
  841. tp[num-1] = c1;
  842. tp[num] = tp[num+1] + (c1<c0?1:0);
  843. }
  844. if (tp[num]!=0 || tp[num-1]>=np[num-1])
  845. {
  846. c0 = bn_sub_words(rp,tp,np,num);
  847. if (tp[num]!=0 || c0==0)
  848. {
  849. for(i=0;i<num+2;i++) vp[i] = 0;
  850. return 1;
  851. }
  852. }
  853. for(i=0;i<num;i++) rp[i] = tp[i], vp[i] = 0;
  854. vp[num] = 0;
  855. vp[num+1] = 0;
  856. return 1;
  857. }
  858. #else
  859. /*
  860. * Return value of 0 indicates that multiplication/convolution was not
  861. * performed to signal the caller to fall down to alternative/original
  862. * code-path.
  863. */
  864. int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0, int num)
  865. { return 0; }
  866. #endif /* OPENSSL_BN_ASM_MONT */
  867. #endif
  868. #else /* !BN_MUL_COMBA */
  869. /* hmm... is it faster just to do a multiply? */
  870. #ifndef OPENSSL_FIPSCANISTER
  871. #undef bn_sqr_comba4
  872. #undef bn_sqr_comba8
  873. #endif
  874. void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
  875. {
  876. BN_ULONG t[8];
  877. bn_sqr_normal(r,a,4,t);
  878. }
  879. void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
  880. {
  881. BN_ULONG t[16];
  882. bn_sqr_normal(r,a,8,t);
  883. }
  884. void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  885. {
  886. r[4]=bn_mul_words( &(r[0]),a,4,b[0]);
  887. r[5]=bn_mul_add_words(&(r[1]),a,4,b[1]);
  888. r[6]=bn_mul_add_words(&(r[2]),a,4,b[2]);
  889. r[7]=bn_mul_add_words(&(r[3]),a,4,b[3]);
  890. }
  891. void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  892. {
  893. r[ 8]=bn_mul_words( &(r[0]),a,8,b[0]);
  894. r[ 9]=bn_mul_add_words(&(r[1]),a,8,b[1]);
  895. r[10]=bn_mul_add_words(&(r[2]),a,8,b[2]);
  896. r[11]=bn_mul_add_words(&(r[3]),a,8,b[3]);
  897. r[12]=bn_mul_add_words(&(r[4]),a,8,b[4]);
  898. r[13]=bn_mul_add_words(&(r[5]),a,8,b[5]);
  899. r[14]=bn_mul_add_words(&(r[6]),a,8,b[6]);
  900. r[15]=bn_mul_add_words(&(r[7]),a,8,b[7]);
  901. }
  902. #ifdef OPENSSL_NO_ASM
  903. #ifdef OPENSSL_BN_ASM_MONT
  904. #include <alloca.h>
  905. int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0p, int num)
  906. {
  907. BN_ULONG c0,c1,*tp,n0=*n0p;
  908. volatile BN_ULONG *vp;
  909. int i=0,j;
  910. vp = tp = alloca((num+2)*sizeof(BN_ULONG));
  911. for(i=0;i<=num;i++) tp[i]=0;
  912. for(i=0;i<num;i++)
  913. {
  914. c0 = bn_mul_add_words(tp,ap,num,bp[i]);
  915. c1 = (tp[num] + c0)&BN_MASK2;
  916. tp[num] = c1;
  917. tp[num+1] = (c1<c0?1:0);
  918. c0 = bn_mul_add_words(tp,np,num,tp[0]*n0);
  919. c1 = (tp[num] + c0)&BN_MASK2;
  920. tp[num] = c1;
  921. tp[num+1] += (c1<c0?1:0);
  922. for(j=0;j<=num;j++) tp[j]=tp[j+1];
  923. }
  924. if (tp[num]!=0 || tp[num-1]>=np[num-1])
  925. {
  926. c0 = bn_sub_words(rp,tp,np,num);
  927. if (tp[num]!=0 || c0==0)
  928. {
  929. for(i=0;i<num+2;i++) vp[i] = 0;
  930. return 1;
  931. }
  932. }
  933. for(i=0;i<num;i++) rp[i] = tp[i], vp[i] = 0;
  934. vp[num] = 0;
  935. vp[num+1] = 0;
  936. return 1;
  937. }
  938. #else
  939. int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np,const BN_ULONG *n0, int num)
  940. { return 0; }
  941. #endif /* OPENSSL_BN_ASM_MONT */
  942. #endif
  943. #endif /* !BN_MUL_COMBA */