sha512-586.pl 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. #! /usr/bin/env perl
  2. # Copyright 2007-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. # ====================================================================
  10. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  11. # project. The module is, however, dual licensed under OpenSSL and
  12. # CRYPTOGAMS licenses depending on where you obtain it. For further
  13. # details see http://www.openssl.org/~appro/cryptogams/.
  14. # ====================================================================
  15. #
  16. # SHA512 block transform for x86. September 2007.
  17. #
  18. # May 2013.
  19. #
  20. # Add SSSE3 code path, 20-25% improvement [over original SSE2 code].
  21. #
  22. # Performance in clock cycles per processed byte (less is better):
  23. #
  24. # gcc icc x86 asm SIMD(*) x86_64(**)
  25. # Pentium 100 97 61 - -
  26. # PIII 75 77 56 - -
  27. # P4 116 95 82 34.6 30.8
  28. # AMD K8 54 55 36 20.7 9.57
  29. # Core2 66 57 40 15.9 9.97
  30. # Westmere 70 - 38 12.2 9.58
  31. # Sandy Bridge 58 - 35 11.9 11.2
  32. # Ivy Bridge 50 - 33 11.5 8.17
  33. # Haswell 46 - 29 11.3 7.66
  34. # Skylake 40 - 26 13.3 7.25
  35. # Bulldozer 121 - 50 14.0 13.5
  36. # VIA Nano 91 - 52 33 14.7
  37. # Atom 126 - 68 48(***) 14.7
  38. # Silvermont 97 - 58 42(***) 17.5
  39. # Goldmont 80 - 48 19.5 12.0
  40. #
  41. # (*) whichever best applicable.
  42. # (**) x86_64 assembler performance is presented for reference
  43. # purposes, the results are for integer-only code.
  44. # (***) paddq is incredibly slow on Atom.
  45. #
  46. # IALU code-path is optimized for elder Pentiums. On vanilla Pentium
  47. # performance improvement over compiler generated code reaches ~60%,
  48. # while on PIII - ~35%. On newer µ-archs improvement varies from 15%
  49. # to 50%, but it's less important as they are expected to execute SSE2
  50. # code-path, which is commonly ~2-3x faster [than compiler generated
  51. # code]. SSE2 code-path is as fast as original sha512-sse2.pl, even
  52. # though it does not use 128-bit operations. The latter means that
  53. # SSE2-aware kernel is no longer required to execute the code. Another
  54. # difference is that new code optimizes amount of writes, but at the
  55. # cost of increased data cache "footprint" by 1/2KB.
  56. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  57. push(@INC,"${dir}","${dir}../../perlasm");
  58. require "x86asm.pl";
  59. $output=pop;
  60. open STDOUT,">$output";
  61. &asm_init($ARGV[0],$ARGV[$#ARGV] eq "386");
  62. $sse2=0;
  63. for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
  64. &external_label("OPENSSL_ia32cap_P") if ($sse2);
  65. $Tlo=&DWP(0,"esp"); $Thi=&DWP(4,"esp");
  66. $Alo=&DWP(8,"esp"); $Ahi=&DWP(8+4,"esp");
  67. $Blo=&DWP(16,"esp"); $Bhi=&DWP(16+4,"esp");
  68. $Clo=&DWP(24,"esp"); $Chi=&DWP(24+4,"esp");
  69. $Dlo=&DWP(32,"esp"); $Dhi=&DWP(32+4,"esp");
  70. $Elo=&DWP(40,"esp"); $Ehi=&DWP(40+4,"esp");
  71. $Flo=&DWP(48,"esp"); $Fhi=&DWP(48+4,"esp");
  72. $Glo=&DWP(56,"esp"); $Ghi=&DWP(56+4,"esp");
  73. $Hlo=&DWP(64,"esp"); $Hhi=&DWP(64+4,"esp");
  74. $K512="ebp";
  75. $Asse2=&QWP(0,"esp");
  76. $Bsse2=&QWP(8,"esp");
  77. $Csse2=&QWP(16,"esp");
  78. $Dsse2=&QWP(24,"esp");
  79. $Esse2=&QWP(32,"esp");
  80. $Fsse2=&QWP(40,"esp");
  81. $Gsse2=&QWP(48,"esp");
  82. $Hsse2=&QWP(56,"esp");
  83. $A="mm0"; # B-D and
  84. $E="mm4"; # F-H are commonly loaded to respectively mm1-mm3 and
  85. # mm5-mm7, but it's done on on-demand basis...
  86. $BxC="mm2"; # ... except for B^C
  87. sub BODY_00_15_sse2 {
  88. my $phase=shift;
  89. #&movq ("mm5",$Fsse2); # load f
  90. #&movq ("mm6",$Gsse2); # load g
  91. &movq ("mm1",$E); # %mm1 is sliding right
  92. &pxor ("mm5","mm6"); # f^=g
  93. &psrlq ("mm1",14);
  94. &movq ($Esse2,$E); # modulo-scheduled save e
  95. &pand ("mm5",$E); # f&=e
  96. &psllq ($E,23); # $E is sliding left
  97. &movq ($A,"mm3") if ($phase<2);
  98. &movq (&QWP(8*9,"esp"),"mm7") # save X[i]
  99. &movq ("mm3","mm1"); # %mm3 is T1
  100. &psrlq ("mm1",4);
  101. &pxor ("mm5","mm6"); # Ch(e,f,g)
  102. &pxor ("mm3",$E);
  103. &psllq ($E,23);
  104. &pxor ("mm3","mm1");
  105. &movq ($Asse2,$A); # modulo-scheduled save a
  106. &paddq ("mm7","mm5"); # X[i]+=Ch(e,f,g)
  107. &pxor ("mm3",$E);
  108. &psrlq ("mm1",23);
  109. &paddq ("mm7",$Hsse2); # X[i]+=h
  110. &pxor ("mm3","mm1");
  111. &psllq ($E,4);
  112. &paddq ("mm7",QWP(0,$K512)); # X[i]+=K512[i]
  113. &pxor ("mm3",$E); # T1=Sigma1_512(e)
  114. &movq ($E,$Dsse2); # e = load d, e in next round
  115. &paddq ("mm3","mm7"); # T1+=X[i]
  116. &movq ("mm5",$A); # %mm5 is sliding right
  117. &psrlq ("mm5",28);
  118. &paddq ($E,"mm3"); # d += T1
  119. &movq ("mm6",$A); # %mm6 is sliding left
  120. &movq ("mm7","mm5");
  121. &psllq ("mm6",25);
  122. &movq ("mm1",$Bsse2); # load b
  123. &psrlq ("mm5",6);
  124. &pxor ("mm7","mm6");
  125. &sub ("esp",8);
  126. &psllq ("mm6",5);
  127. &pxor ("mm7","mm5");
  128. &pxor ($A,"mm1"); # a^b, b^c in next round
  129. &psrlq ("mm5",5);
  130. &pxor ("mm7","mm6");
  131. &pand ($BxC,$A); # (b^c)&(a^b)
  132. &psllq ("mm6",6);
  133. &pxor ("mm7","mm5");
  134. &pxor ($BxC,"mm1"); # [h=]Maj(a,b,c)
  135. &pxor ("mm6","mm7"); # Sigma0_512(a)
  136. &movq ("mm7",&QWP(8*(9+16-1),"esp")) if ($phase!=0); # pre-fetch
  137. &movq ("mm5",$Fsse2) if ($phase==0); # load f
  138. if ($phase>1) {
  139. &paddq ($BxC,"mm6"); # h+=Sigma0(a)
  140. &add ($K512,8);
  141. #&paddq ($BxC,"mm3"); # h+=T1
  142. ($A,$BxC) = ($BxC,$A); # rotate registers
  143. } else {
  144. &paddq ("mm3",$BxC); # T1+=Maj(a,b,c)
  145. &movq ($BxC,$A);
  146. &add ($K512,8);
  147. &paddq ("mm3","mm6"); # T1+=Sigma0(a)
  148. &movq ("mm6",$Gsse2) if ($phase==0); # load g
  149. #&movq ($A,"mm3"); # h=T1
  150. }
  151. }
  152. sub BODY_00_15_x86 {
  153. #define Sigma1(x) (ROTR((x),14) ^ ROTR((x),18) ^ ROTR((x),41))
  154. # LO lo>>14^hi<<18 ^ lo>>18^hi<<14 ^ hi>>9^lo<<23
  155. # HI hi>>14^lo<<18 ^ hi>>18^lo<<14 ^ lo>>9^hi<<23
  156. &mov ("ecx",$Elo);
  157. &mov ("edx",$Ehi);
  158. &mov ("esi","ecx");
  159. &shr ("ecx",9); # lo>>9
  160. &mov ("edi","edx");
  161. &shr ("edx",9); # hi>>9
  162. &mov ("ebx","ecx");
  163. &shl ("esi",14); # lo<<14
  164. &mov ("eax","edx");
  165. &shl ("edi",14); # hi<<14
  166. &xor ("ebx","esi");
  167. &shr ("ecx",14-9); # lo>>14
  168. &xor ("eax","edi");
  169. &shr ("edx",14-9); # hi>>14
  170. &xor ("eax","ecx");
  171. &shl ("esi",18-14); # lo<<18
  172. &xor ("ebx","edx");
  173. &shl ("edi",18-14); # hi<<18
  174. &xor ("ebx","esi");
  175. &shr ("ecx",18-14); # lo>>18
  176. &xor ("eax","edi");
  177. &shr ("edx",18-14); # hi>>18
  178. &xor ("eax","ecx");
  179. &shl ("esi",23-18); # lo<<23
  180. &xor ("ebx","edx");
  181. &shl ("edi",23-18); # hi<<23
  182. &xor ("eax","esi");
  183. &xor ("ebx","edi"); # T1 = Sigma1(e)
  184. &mov ("ecx",$Flo);
  185. &mov ("edx",$Fhi);
  186. &mov ("esi",$Glo);
  187. &mov ("edi",$Ghi);
  188. &add ("eax",$Hlo);
  189. &adc ("ebx",$Hhi); # T1 += h
  190. &xor ("ecx","esi");
  191. &xor ("edx","edi");
  192. &and ("ecx",$Elo);
  193. &and ("edx",$Ehi);
  194. &add ("eax",&DWP(8*(9+15)+0,"esp"));
  195. &adc ("ebx",&DWP(8*(9+15)+4,"esp")); # T1 += X[0]
  196. &xor ("ecx","esi");
  197. &xor ("edx","edi"); # Ch(e,f,g) = (f^g)&e)^g
  198. &mov ("esi",&DWP(0,$K512));
  199. &mov ("edi",&DWP(4,$K512)); # K[i]
  200. &add ("eax","ecx");
  201. &adc ("ebx","edx"); # T1 += Ch(e,f,g)
  202. &mov ("ecx",$Dlo);
  203. &mov ("edx",$Dhi);
  204. &add ("eax","esi");
  205. &adc ("ebx","edi"); # T1 += K[i]
  206. &mov ($Tlo,"eax");
  207. &mov ($Thi,"ebx"); # put T1 away
  208. &add ("eax","ecx");
  209. &adc ("ebx","edx"); # d += T1
  210. #define Sigma0(x) (ROTR((x),28) ^ ROTR((x),34) ^ ROTR((x),39))
  211. # LO lo>>28^hi<<4 ^ hi>>2^lo<<30 ^ hi>>7^lo<<25
  212. # HI hi>>28^lo<<4 ^ lo>>2^hi<<30 ^ lo>>7^hi<<25
  213. &mov ("ecx",$Alo);
  214. &mov ("edx",$Ahi);
  215. &mov ($Dlo,"eax");
  216. &mov ($Dhi,"ebx");
  217. &mov ("esi","ecx");
  218. &shr ("ecx",2); # lo>>2
  219. &mov ("edi","edx");
  220. &shr ("edx",2); # hi>>2
  221. &mov ("ebx","ecx");
  222. &shl ("esi",4); # lo<<4
  223. &mov ("eax","edx");
  224. &shl ("edi",4); # hi<<4
  225. &xor ("ebx","esi");
  226. &shr ("ecx",7-2); # lo>>7
  227. &xor ("eax","edi");
  228. &shr ("edx",7-2); # hi>>7
  229. &xor ("ebx","ecx");
  230. &shl ("esi",25-4); # lo<<25
  231. &xor ("eax","edx");
  232. &shl ("edi",25-4); # hi<<25
  233. &xor ("eax","esi");
  234. &shr ("ecx",28-7); # lo>>28
  235. &xor ("ebx","edi");
  236. &shr ("edx",28-7); # hi>>28
  237. &xor ("eax","ecx");
  238. &shl ("esi",30-25); # lo<<30
  239. &xor ("ebx","edx");
  240. &shl ("edi",30-25); # hi<<30
  241. &xor ("eax","esi");
  242. &xor ("ebx","edi"); # Sigma0(a)
  243. &mov ("ecx",$Alo);
  244. &mov ("edx",$Ahi);
  245. &mov ("esi",$Blo);
  246. &mov ("edi",$Bhi);
  247. &add ("eax",$Tlo);
  248. &adc ("ebx",$Thi); # T1 = Sigma0(a)+T1
  249. &or ("ecx","esi");
  250. &or ("edx","edi");
  251. &and ("ecx",$Clo);
  252. &and ("edx",$Chi);
  253. &and ("esi",$Alo);
  254. &and ("edi",$Ahi);
  255. &or ("ecx","esi");
  256. &or ("edx","edi"); # Maj(a,b,c) = ((a|b)&c)|(a&b)
  257. &add ("eax","ecx");
  258. &adc ("ebx","edx"); # T1 += Maj(a,b,c)
  259. &mov ($Tlo,"eax");
  260. &mov ($Thi,"ebx");
  261. &mov (&LB("edx"),&BP(0,$K512)); # pre-fetch LSB of *K
  262. &sub ("esp",8);
  263. &lea ($K512,&DWP(8,$K512)); # K++
  264. }
  265. &function_begin("sha512_block_data_order");
  266. &mov ("esi",wparam(0)); # ctx
  267. &mov ("edi",wparam(1)); # inp
  268. &mov ("eax",wparam(2)); # num
  269. &mov ("ebx","esp"); # saved sp
  270. &call (&label("pic_point")); # make it PIC!
  271. &set_label("pic_point");
  272. &blindpop($K512);
  273. &lea ($K512,&DWP(&label("K512")."-".&label("pic_point"),$K512));
  274. &sub ("esp",16);
  275. &and ("esp",-64);
  276. &shl ("eax",7);
  277. &add ("eax","edi");
  278. &mov (&DWP(0,"esp"),"esi"); # ctx
  279. &mov (&DWP(4,"esp"),"edi"); # inp
  280. &mov (&DWP(8,"esp"),"eax"); # inp+num*128
  281. &mov (&DWP(12,"esp"),"ebx"); # saved sp
  282. if ($sse2) {
  283. &picmeup("edx","OPENSSL_ia32cap_P",$K512,&label("K512"));
  284. &mov ("ecx",&DWP(0,"edx"));
  285. &test ("ecx",1<<26);
  286. &jz (&label("loop_x86"));
  287. &mov ("edx",&DWP(4,"edx"));
  288. # load ctx->h[0-7]
  289. &movq ($A,&QWP(0,"esi"));
  290. &and ("ecx",1<<24); # XMM registers availability
  291. &movq ("mm1",&QWP(8,"esi"));
  292. &and ("edx",1<<9); # SSSE3 bit
  293. &movq ($BxC,&QWP(16,"esi"));
  294. &or ("ecx","edx");
  295. &movq ("mm3",&QWP(24,"esi"));
  296. &movq ($E,&QWP(32,"esi"));
  297. &movq ("mm5",&QWP(40,"esi"));
  298. &movq ("mm6",&QWP(48,"esi"));
  299. &movq ("mm7",&QWP(56,"esi"));
  300. &cmp ("ecx",1<<24|1<<9);
  301. &je (&label("SSSE3"));
  302. &sub ("esp",8*10);
  303. &jmp (&label("loop_sse2"));
  304. &set_label("loop_sse2",16);
  305. #&movq ($Asse2,$A);
  306. &movq ($Bsse2,"mm1");
  307. &movq ($Csse2,$BxC);
  308. &movq ($Dsse2,"mm3");
  309. #&movq ($Esse2,$E);
  310. &movq ($Fsse2,"mm5");
  311. &movq ($Gsse2,"mm6");
  312. &pxor ($BxC,"mm1"); # magic
  313. &movq ($Hsse2,"mm7");
  314. &movq ("mm3",$A); # magic
  315. &mov ("eax",&DWP(0,"edi"));
  316. &mov ("ebx",&DWP(4,"edi"));
  317. &add ("edi",8);
  318. &mov ("edx",15); # counter
  319. &bswap ("eax");
  320. &bswap ("ebx");
  321. &jmp (&label("00_14_sse2"));
  322. &set_label("00_14_sse2",16);
  323. &movd ("mm1","eax");
  324. &mov ("eax",&DWP(0,"edi"));
  325. &movd ("mm7","ebx");
  326. &mov ("ebx",&DWP(4,"edi"));
  327. &add ("edi",8);
  328. &bswap ("eax");
  329. &bswap ("ebx");
  330. &punpckldq("mm7","mm1");
  331. &BODY_00_15_sse2();
  332. &dec ("edx");
  333. &jnz (&label("00_14_sse2"));
  334. &movd ("mm1","eax");
  335. &movd ("mm7","ebx");
  336. &punpckldq("mm7","mm1");
  337. &BODY_00_15_sse2(1);
  338. &pxor ($A,$A); # A is in %mm3
  339. &mov ("edx",32); # counter
  340. &jmp (&label("16_79_sse2"));
  341. &set_label("16_79_sse2",16);
  342. for ($j=0;$j<2;$j++) { # 2x unroll
  343. #&movq ("mm7",&QWP(8*(9+16-1),"esp")); # prefetched in BODY_00_15
  344. &movq ("mm5",&QWP(8*(9+16-14),"esp"));
  345. &movq ("mm1","mm7");
  346. &psrlq ("mm7",1);
  347. &movq ("mm6","mm5");
  348. &psrlq ("mm5",6);
  349. &psllq ("mm1",56);
  350. &paddq ($A,"mm3"); # from BODY_00_15
  351. &movq ("mm3","mm7");
  352. &psrlq ("mm7",7-1);
  353. &pxor ("mm3","mm1");
  354. &psllq ("mm1",63-56);
  355. &pxor ("mm3","mm7");
  356. &psrlq ("mm7",8-7);
  357. &pxor ("mm3","mm1");
  358. &movq ("mm1","mm5");
  359. &psrlq ("mm5",19-6);
  360. &pxor ("mm7","mm3"); # sigma0
  361. &psllq ("mm6",3);
  362. &pxor ("mm1","mm5");
  363. &paddq ("mm7",&QWP(8*(9+16),"esp"));
  364. &pxor ("mm1","mm6");
  365. &psrlq ("mm5",61-19);
  366. &paddq ("mm7",&QWP(8*(9+16-9),"esp"));
  367. &pxor ("mm1","mm5");
  368. &psllq ("mm6",45-3);
  369. &movq ("mm5",$Fsse2); # load f
  370. &pxor ("mm1","mm6"); # sigma1
  371. &movq ("mm6",$Gsse2); # load g
  372. &paddq ("mm7","mm1"); # X[i]
  373. #&movq (&QWP(8*9,"esp"),"mm7"); # moved to BODY_00_15
  374. &BODY_00_15_sse2(2);
  375. }
  376. &dec ("edx");
  377. &jnz (&label("16_79_sse2"));
  378. #&movq ($A,$Asse2);
  379. &paddq ($A,"mm3"); # from BODY_00_15
  380. &movq ("mm1",$Bsse2);
  381. #&movq ($BxC,$Csse2);
  382. &movq ("mm3",$Dsse2);
  383. #&movq ($E,$Esse2);
  384. &movq ("mm5",$Fsse2);
  385. &movq ("mm6",$Gsse2);
  386. &movq ("mm7",$Hsse2);
  387. &pxor ($BxC,"mm1"); # de-magic
  388. &paddq ($A,&QWP(0,"esi"));
  389. &paddq ("mm1",&QWP(8,"esi"));
  390. &paddq ($BxC,&QWP(16,"esi"));
  391. &paddq ("mm3",&QWP(24,"esi"));
  392. &paddq ($E,&QWP(32,"esi"));
  393. &paddq ("mm5",&QWP(40,"esi"));
  394. &paddq ("mm6",&QWP(48,"esi"));
  395. &paddq ("mm7",&QWP(56,"esi"));
  396. &mov ("eax",8*80);
  397. &movq (&QWP(0,"esi"),$A);
  398. &movq (&QWP(8,"esi"),"mm1");
  399. &movq (&QWP(16,"esi"),$BxC);
  400. &movq (&QWP(24,"esi"),"mm3");
  401. &movq (&QWP(32,"esi"),$E);
  402. &movq (&QWP(40,"esi"),"mm5");
  403. &movq (&QWP(48,"esi"),"mm6");
  404. &movq (&QWP(56,"esi"),"mm7");
  405. &lea ("esp",&DWP(0,"esp","eax")); # destroy frame
  406. &sub ($K512,"eax"); # rewind K
  407. &cmp ("edi",&DWP(8*10+8,"esp")); # are we done yet?
  408. &jb (&label("loop_sse2"));
  409. &mov ("esp",&DWP(8*10+12,"esp")); # restore sp
  410. &emms ();
  411. &function_end_A();
  412. &set_label("SSSE3",32);
  413. { my ($cnt,$frame)=("ecx","edx");
  414. my @X=map("xmm$_",(0..7));
  415. my $j;
  416. my $i=0;
  417. &lea ($frame,&DWP(-64,"esp"));
  418. &sub ("esp",256);
  419. # fixed stack frame layout
  420. #
  421. # +0 A B C D E F G H # backing store
  422. # +64 X[0]+K[i] .. X[15]+K[i] # XMM->MM xfer area
  423. # +192 # XMM off-load ring buffer
  424. # +256 # saved parameters
  425. &movdqa (@X[1],&QWP(80*8,$K512)); # byte swap mask
  426. &movdqu (@X[0],&QWP(0,"edi"));
  427. &pshufb (@X[0],@X[1]);
  428. for ($j=0;$j<8;$j++) {
  429. &movdqa (&QWP(16*(($j-1)%4),$frame),@X[3]) if ($j>4); # off-load
  430. &movdqa (@X[3],&QWP(16*($j%8),$K512));
  431. &movdqa (@X[2],@X[1]) if ($j<7); # perpetuate byte swap mask
  432. &movdqu (@X[1],&QWP(16*($j+1),"edi")) if ($j<7); # next input
  433. &movdqa (@X[1],&QWP(16*(($j+1)%4),$frame)) if ($j==7);# restore @X[0]
  434. &paddq (@X[3],@X[0]);
  435. &pshufb (@X[1],@X[2]) if ($j<7);
  436. &movdqa (&QWP(16*($j%8)-128,$frame),@X[3]); # xfer X[i]+K[i]
  437. push(@X,shift(@X)); # rotate(@X)
  438. }
  439. #&jmp (&label("loop_ssse3"));
  440. &nop ();
  441. &set_label("loop_ssse3",32);
  442. &movdqa (@X[2],&QWP(16*(($j+1)%4),$frame)); # pre-restore @X[1]
  443. &movdqa (&QWP(16*(($j-1)%4),$frame),@X[3]); # off-load @X[3]
  444. &lea ($K512,&DWP(16*8,$K512));
  445. #&movq ($Asse2,$A); # off-load A-H
  446. &movq ($Bsse2,"mm1");
  447. &mov ("ebx","edi");
  448. &movq ($Csse2,$BxC);
  449. &lea ("edi",&DWP(128,"edi")); # advance input
  450. &movq ($Dsse2,"mm3");
  451. &cmp ("edi","eax");
  452. #&movq ($Esse2,$E);
  453. &movq ($Fsse2,"mm5");
  454. &cmovb ("ebx","edi");
  455. &movq ($Gsse2,"mm6");
  456. &mov ("ecx",4); # loop counter
  457. &pxor ($BxC,"mm1"); # magic
  458. &movq ($Hsse2,"mm7");
  459. &pxor ("mm3","mm3"); # magic
  460. &jmp (&label("00_47_ssse3"));
  461. sub BODY_00_15_ssse3 { # "phase-less" copy of BODY_00_15_sse2
  462. (
  463. '&movq ("mm1",$E)', # %mm1 is sliding right
  464. '&movq ("mm7",&QWP(((-8*$i)%128)-128,$frame))',# X[i]+K[i]
  465. '&pxor ("mm5","mm6")', # f^=g
  466. '&psrlq ("mm1",14)',
  467. '&movq (&QWP(8*($i+4)%64,"esp"),$E)', # modulo-scheduled save e
  468. '&pand ("mm5",$E)', # f&=e
  469. '&psllq ($E,23)', # $E is sliding left
  470. '&paddq ($A,"mm3")', # [h+=Maj(a,b,c)]
  471. '&movq ("mm3","mm1")', # %mm3 is T1
  472. '&psrlq("mm1",4)',
  473. '&pxor ("mm5","mm6")', # Ch(e,f,g)
  474. '&pxor ("mm3",$E)',
  475. '&psllq($E,23)',
  476. '&pxor ("mm3","mm1")',
  477. '&movq (&QWP(8*$i%64,"esp"),$A)', # modulo-scheduled save a
  478. '&paddq("mm7","mm5")', # X[i]+=Ch(e,f,g)
  479. '&pxor ("mm3",$E)',
  480. '&psrlq("mm1",23)',
  481. '&paddq("mm7",&QWP(8*($i+7)%64,"esp"))', # X[i]+=h
  482. '&pxor ("mm3","mm1")',
  483. '&psllq($E,4)',
  484. '&pxor ("mm3",$E)', # T1=Sigma1_512(e)
  485. '&movq ($E,&QWP(8*($i+3)%64,"esp"))', # e = load d, e in next round
  486. '&paddq ("mm3","mm7")', # T1+=X[i]
  487. '&movq ("mm5",$A)', # %mm5 is sliding right
  488. '&psrlq("mm5",28)',
  489. '&paddq ($E,"mm3")', # d += T1
  490. '&movq ("mm6",$A)', # %mm6 is sliding left
  491. '&movq ("mm7","mm5")',
  492. '&psllq("mm6",25)',
  493. '&movq ("mm1",&QWP(8*($i+1)%64,"esp"))', # load b
  494. '&psrlq("mm5",6)',
  495. '&pxor ("mm7","mm6")',
  496. '&psllq("mm6",5)',
  497. '&pxor ("mm7","mm5")',
  498. '&pxor ($A,"mm1")', # a^b, b^c in next round
  499. '&psrlq("mm5",5)',
  500. '&pxor ("mm7","mm6")',
  501. '&pand ($BxC,$A)', # (b^c)&(a^b)
  502. '&psllq("mm6",6)',
  503. '&pxor ("mm7","mm5")',
  504. '&pxor ($BxC,"mm1")', # [h=]Maj(a,b,c)
  505. '&pxor ("mm6","mm7")', # Sigma0_512(a)
  506. '&movq ("mm5",&QWP(8*($i+5-1)%64,"esp"))', # pre-load f
  507. '&paddq ($BxC,"mm6")', # h+=Sigma0(a)
  508. '&movq ("mm6",&QWP(8*($i+6-1)%64,"esp"))', # pre-load g
  509. '($A,$BxC) = ($BxC,$A); $i--;'
  510. );
  511. }
  512. &set_label("00_47_ssse3",32);
  513. for(;$j<16;$j++) {
  514. my ($t0,$t2,$t1)=@X[2..4];
  515. my @insns = (&BODY_00_15_ssse3(),&BODY_00_15_ssse3());
  516. &movdqa ($t2,@X[5]);
  517. &movdqa (@X[1],$t0); # restore @X[1]
  518. &palignr ($t0,@X[0],8); # X[1..2]
  519. &movdqa (&QWP(16*($j%4),$frame),@X[4]); # off-load @X[4]
  520. &palignr ($t2,@X[4],8); # X[9..10]
  521. &movdqa ($t1,$t0);
  522. &psrlq ($t0,7);
  523. &paddq (@X[0],$t2); # X[0..1] += X[9..10]
  524. &movdqa ($t2,$t1);
  525. &psrlq ($t1,1);
  526. &psllq ($t2,64-8);
  527. &pxor ($t0,$t1);
  528. &psrlq ($t1,8-1);
  529. &pxor ($t0,$t2);
  530. &psllq ($t2,8-1);
  531. &pxor ($t0,$t1);
  532. &movdqa ($t1,@X[7]);
  533. &pxor ($t0,$t2); # sigma0(X[1..2])
  534. &movdqa ($t2,@X[7]);
  535. &psrlq ($t1,6);
  536. &paddq (@X[0],$t0); # X[0..1] += sigma0(X[1..2])
  537. &movdqa ($t0,@X[7]);
  538. &psrlq ($t2,19);
  539. &psllq ($t0,64-61);
  540. &pxor ($t1,$t2);
  541. &psrlq ($t2,61-19);
  542. &pxor ($t1,$t0);
  543. &psllq ($t0,61-19);
  544. &pxor ($t1,$t2);
  545. &movdqa ($t2,&QWP(16*(($j+2)%4),$frame));# pre-restore @X[1]
  546. &pxor ($t1,$t0); # sigma0(X[1..2])
  547. &movdqa ($t0,&QWP(16*($j%8),$K512));
  548. eval(shift(@insns));
  549. &paddq (@X[0],$t1); # X[0..1] += sigma0(X[14..15])
  550. eval(shift(@insns));
  551. eval(shift(@insns));
  552. eval(shift(@insns));
  553. eval(shift(@insns));
  554. &paddq ($t0,@X[0]);
  555. foreach(@insns) { eval; }
  556. &movdqa (&QWP(16*($j%8)-128,$frame),$t0);# xfer X[i]+K[i]
  557. push(@X,shift(@X)); # rotate(@X)
  558. }
  559. &lea ($K512,&DWP(16*8,$K512));
  560. &dec ("ecx");
  561. &jnz (&label("00_47_ssse3"));
  562. &movdqa (@X[1],&QWP(0,$K512)); # byte swap mask
  563. &lea ($K512,&DWP(-80*8,$K512)); # rewind
  564. &movdqu (@X[0],&QWP(0,"ebx"));
  565. &pshufb (@X[0],@X[1]);
  566. for ($j=0;$j<8;$j++) { # load next or same block
  567. my @insns = (&BODY_00_15_ssse3(),&BODY_00_15_ssse3());
  568. &movdqa (&QWP(16*(($j-1)%4),$frame),@X[3]) if ($j>4); # off-load
  569. &movdqa (@X[3],&QWP(16*($j%8),$K512));
  570. &movdqa (@X[2],@X[1]) if ($j<7); # perpetuate byte swap mask
  571. &movdqu (@X[1],&QWP(16*($j+1),"ebx")) if ($j<7); # next input
  572. &movdqa (@X[1],&QWP(16*(($j+1)%4),$frame)) if ($j==7);# restore @X[0]
  573. &paddq (@X[3],@X[0]);
  574. &pshufb (@X[1],@X[2]) if ($j<7);
  575. foreach(@insns) { eval; }
  576. &movdqa (&QWP(16*($j%8)-128,$frame),@X[3]);# xfer X[i]+K[i]
  577. push(@X,shift(@X)); # rotate(@X)
  578. }
  579. #&movq ($A,$Asse2); # load A-H
  580. &movq ("mm1",$Bsse2);
  581. &paddq ($A,"mm3"); # from BODY_00_15
  582. #&movq ($BxC,$Csse2);
  583. &movq ("mm3",$Dsse2);
  584. #&movq ($E,$Esse2);
  585. #&movq ("mm5",$Fsse2);
  586. #&movq ("mm6",$Gsse2);
  587. &movq ("mm7",$Hsse2);
  588. &pxor ($BxC,"mm1"); # de-magic
  589. &paddq ($A,&QWP(0,"esi"));
  590. &paddq ("mm1",&QWP(8,"esi"));
  591. &paddq ($BxC,&QWP(16,"esi"));
  592. &paddq ("mm3",&QWP(24,"esi"));
  593. &paddq ($E,&QWP(32,"esi"));
  594. &paddq ("mm5",&QWP(40,"esi"));
  595. &paddq ("mm6",&QWP(48,"esi"));
  596. &paddq ("mm7",&QWP(56,"esi"));
  597. &movq (&QWP(0,"esi"),$A);
  598. &movq (&QWP(8,"esi"),"mm1");
  599. &movq (&QWP(16,"esi"),$BxC);
  600. &movq (&QWP(24,"esi"),"mm3");
  601. &movq (&QWP(32,"esi"),$E);
  602. &movq (&QWP(40,"esi"),"mm5");
  603. &movq (&QWP(48,"esi"),"mm6");
  604. &movq (&QWP(56,"esi"),"mm7");
  605. &cmp ("edi","eax") # are we done yet?
  606. &jb (&label("loop_ssse3"));
  607. &mov ("esp",&DWP(64+12,$frame)); # restore sp
  608. &emms ();
  609. }
  610. &function_end_A();
  611. }
  612. &set_label("loop_x86",16);
  613. # copy input block to stack reversing byte and qword order
  614. for ($i=0;$i<8;$i++) {
  615. &mov ("eax",&DWP($i*16+0,"edi"));
  616. &mov ("ebx",&DWP($i*16+4,"edi"));
  617. &mov ("ecx",&DWP($i*16+8,"edi"));
  618. &mov ("edx",&DWP($i*16+12,"edi"));
  619. &bswap ("eax");
  620. &bswap ("ebx");
  621. &bswap ("ecx");
  622. &bswap ("edx");
  623. &push ("eax");
  624. &push ("ebx");
  625. &push ("ecx");
  626. &push ("edx");
  627. }
  628. &add ("edi",128);
  629. &sub ("esp",9*8); # place for T,A,B,C,D,E,F,G,H
  630. &mov (&DWP(8*(9+16)+4,"esp"),"edi");
  631. # copy ctx->h[0-7] to A,B,C,D,E,F,G,H on stack
  632. &lea ("edi",&DWP(8,"esp"));
  633. &mov ("ecx",16);
  634. &data_word(0xA5F3F689); # rep movsd
  635. &set_label("00_15_x86",16);
  636. &BODY_00_15_x86();
  637. &cmp (&LB("edx"),0x94);
  638. &jne (&label("00_15_x86"));
  639. &set_label("16_79_x86",16);
  640. #define sigma0(x) (ROTR((x),1) ^ ROTR((x),8) ^ ((x)>>7))
  641. # LO lo>>1^hi<<31 ^ lo>>8^hi<<24 ^ lo>>7^hi<<25
  642. # HI hi>>1^lo<<31 ^ hi>>8^lo<<24 ^ hi>>7
  643. &mov ("ecx",&DWP(8*(9+15+16-1)+0,"esp"));
  644. &mov ("edx",&DWP(8*(9+15+16-1)+4,"esp"));
  645. &mov ("esi","ecx");
  646. &shr ("ecx",1); # lo>>1
  647. &mov ("edi","edx");
  648. &shr ("edx",1); # hi>>1
  649. &mov ("eax","ecx");
  650. &shl ("esi",24); # lo<<24
  651. &mov ("ebx","edx");
  652. &shl ("edi",24); # hi<<24
  653. &xor ("ebx","esi");
  654. &shr ("ecx",7-1); # lo>>7
  655. &xor ("eax","edi");
  656. &shr ("edx",7-1); # hi>>7
  657. &xor ("eax","ecx");
  658. &shl ("esi",31-24); # lo<<31
  659. &xor ("ebx","edx");
  660. &shl ("edi",25-24); # hi<<25
  661. &xor ("ebx","esi");
  662. &shr ("ecx",8-7); # lo>>8
  663. &xor ("eax","edi");
  664. &shr ("edx",8-7); # hi>>8
  665. &xor ("eax","ecx");
  666. &shl ("edi",31-25); # hi<<31
  667. &xor ("ebx","edx");
  668. &xor ("eax","edi"); # T1 = sigma0(X[-15])
  669. &mov (&DWP(0,"esp"),"eax");
  670. &mov (&DWP(4,"esp"),"ebx"); # put T1 away
  671. #define sigma1(x) (ROTR((x),19) ^ ROTR((x),61) ^ ((x)>>6))
  672. # LO lo>>19^hi<<13 ^ hi>>29^lo<<3 ^ lo>>6^hi<<26
  673. # HI hi>>19^lo<<13 ^ lo>>29^hi<<3 ^ hi>>6
  674. &mov ("ecx",&DWP(8*(9+15+16-14)+0,"esp"));
  675. &mov ("edx",&DWP(8*(9+15+16-14)+4,"esp"));
  676. &mov ("esi","ecx");
  677. &shr ("ecx",6); # lo>>6
  678. &mov ("edi","edx");
  679. &shr ("edx",6); # hi>>6
  680. &mov ("eax","ecx");
  681. &shl ("esi",3); # lo<<3
  682. &mov ("ebx","edx");
  683. &shl ("edi",3); # hi<<3
  684. &xor ("eax","esi");
  685. &shr ("ecx",19-6); # lo>>19
  686. &xor ("ebx","edi");
  687. &shr ("edx",19-6); # hi>>19
  688. &xor ("eax","ecx");
  689. &shl ("esi",13-3); # lo<<13
  690. &xor ("ebx","edx");
  691. &shl ("edi",13-3); # hi<<13
  692. &xor ("ebx","esi");
  693. &shr ("ecx",29-19); # lo>>29
  694. &xor ("eax","edi");
  695. &shr ("edx",29-19); # hi>>29
  696. &xor ("ebx","ecx");
  697. &shl ("edi",26-13); # hi<<26
  698. &xor ("eax","edx");
  699. &xor ("eax","edi"); # sigma1(X[-2])
  700. &mov ("ecx",&DWP(8*(9+15+16)+0,"esp"));
  701. &mov ("edx",&DWP(8*(9+15+16)+4,"esp"));
  702. &add ("eax",&DWP(0,"esp"));
  703. &adc ("ebx",&DWP(4,"esp")); # T1 = sigma1(X[-2])+T1
  704. &mov ("esi",&DWP(8*(9+15+16-9)+0,"esp"));
  705. &mov ("edi",&DWP(8*(9+15+16-9)+4,"esp"));
  706. &add ("eax","ecx");
  707. &adc ("ebx","edx"); # T1 += X[-16]
  708. &add ("eax","esi");
  709. &adc ("ebx","edi"); # T1 += X[-7]
  710. &mov (&DWP(8*(9+15)+0,"esp"),"eax");
  711. &mov (&DWP(8*(9+15)+4,"esp"),"ebx"); # save X[0]
  712. &BODY_00_15_x86();
  713. &cmp (&LB("edx"),0x17);
  714. &jne (&label("16_79_x86"));
  715. &mov ("esi",&DWP(8*(9+16+80)+0,"esp"));# ctx
  716. &mov ("edi",&DWP(8*(9+16+80)+4,"esp"));# inp
  717. for($i=0;$i<4;$i++) {
  718. &mov ("eax",&DWP($i*16+0,"esi"));
  719. &mov ("ebx",&DWP($i*16+4,"esi"));
  720. &mov ("ecx",&DWP($i*16+8,"esi"));
  721. &mov ("edx",&DWP($i*16+12,"esi"));
  722. &add ("eax",&DWP(8+($i*16)+0,"esp"));
  723. &adc ("ebx",&DWP(8+($i*16)+4,"esp"));
  724. &mov (&DWP($i*16+0,"esi"),"eax");
  725. &mov (&DWP($i*16+4,"esi"),"ebx");
  726. &add ("ecx",&DWP(8+($i*16)+8,"esp"));
  727. &adc ("edx",&DWP(8+($i*16)+12,"esp"));
  728. &mov (&DWP($i*16+8,"esi"),"ecx");
  729. &mov (&DWP($i*16+12,"esi"),"edx");
  730. }
  731. &add ("esp",8*(9+16+80)); # destroy frame
  732. &sub ($K512,8*80); # rewind K
  733. &cmp ("edi",&DWP(8,"esp")); # are we done yet?
  734. &jb (&label("loop_x86"));
  735. &mov ("esp",&DWP(12,"esp")); # restore sp
  736. &function_end_A();
  737. &set_label("K512",64); # Yes! I keep it in the code segment!
  738. &data_word(0xd728ae22,0x428a2f98); # u64
  739. &data_word(0x23ef65cd,0x71374491); # u64
  740. &data_word(0xec4d3b2f,0xb5c0fbcf); # u64
  741. &data_word(0x8189dbbc,0xe9b5dba5); # u64
  742. &data_word(0xf348b538,0x3956c25b); # u64
  743. &data_word(0xb605d019,0x59f111f1); # u64
  744. &data_word(0xaf194f9b,0x923f82a4); # u64
  745. &data_word(0xda6d8118,0xab1c5ed5); # u64
  746. &data_word(0xa3030242,0xd807aa98); # u64
  747. &data_word(0x45706fbe,0x12835b01); # u64
  748. &data_word(0x4ee4b28c,0x243185be); # u64
  749. &data_word(0xd5ffb4e2,0x550c7dc3); # u64
  750. &data_word(0xf27b896f,0x72be5d74); # u64
  751. &data_word(0x3b1696b1,0x80deb1fe); # u64
  752. &data_word(0x25c71235,0x9bdc06a7); # u64
  753. &data_word(0xcf692694,0xc19bf174); # u64
  754. &data_word(0x9ef14ad2,0xe49b69c1); # u64
  755. &data_word(0x384f25e3,0xefbe4786); # u64
  756. &data_word(0x8b8cd5b5,0x0fc19dc6); # u64
  757. &data_word(0x77ac9c65,0x240ca1cc); # u64
  758. &data_word(0x592b0275,0x2de92c6f); # u64
  759. &data_word(0x6ea6e483,0x4a7484aa); # u64
  760. &data_word(0xbd41fbd4,0x5cb0a9dc); # u64
  761. &data_word(0x831153b5,0x76f988da); # u64
  762. &data_word(0xee66dfab,0x983e5152); # u64
  763. &data_word(0x2db43210,0xa831c66d); # u64
  764. &data_word(0x98fb213f,0xb00327c8); # u64
  765. &data_word(0xbeef0ee4,0xbf597fc7); # u64
  766. &data_word(0x3da88fc2,0xc6e00bf3); # u64
  767. &data_word(0x930aa725,0xd5a79147); # u64
  768. &data_word(0xe003826f,0x06ca6351); # u64
  769. &data_word(0x0a0e6e70,0x14292967); # u64
  770. &data_word(0x46d22ffc,0x27b70a85); # u64
  771. &data_word(0x5c26c926,0x2e1b2138); # u64
  772. &data_word(0x5ac42aed,0x4d2c6dfc); # u64
  773. &data_word(0x9d95b3df,0x53380d13); # u64
  774. &data_word(0x8baf63de,0x650a7354); # u64
  775. &data_word(0x3c77b2a8,0x766a0abb); # u64
  776. &data_word(0x47edaee6,0x81c2c92e); # u64
  777. &data_word(0x1482353b,0x92722c85); # u64
  778. &data_word(0x4cf10364,0xa2bfe8a1); # u64
  779. &data_word(0xbc423001,0xa81a664b); # u64
  780. &data_word(0xd0f89791,0xc24b8b70); # u64
  781. &data_word(0x0654be30,0xc76c51a3); # u64
  782. &data_word(0xd6ef5218,0xd192e819); # u64
  783. &data_word(0x5565a910,0xd6990624); # u64
  784. &data_word(0x5771202a,0xf40e3585); # u64
  785. &data_word(0x32bbd1b8,0x106aa070); # u64
  786. &data_word(0xb8d2d0c8,0x19a4c116); # u64
  787. &data_word(0x5141ab53,0x1e376c08); # u64
  788. &data_word(0xdf8eeb99,0x2748774c); # u64
  789. &data_word(0xe19b48a8,0x34b0bcb5); # u64
  790. &data_word(0xc5c95a63,0x391c0cb3); # u64
  791. &data_word(0xe3418acb,0x4ed8aa4a); # u64
  792. &data_word(0x7763e373,0x5b9cca4f); # u64
  793. &data_word(0xd6b2b8a3,0x682e6ff3); # u64
  794. &data_word(0x5defb2fc,0x748f82ee); # u64
  795. &data_word(0x43172f60,0x78a5636f); # u64
  796. &data_word(0xa1f0ab72,0x84c87814); # u64
  797. &data_word(0x1a6439ec,0x8cc70208); # u64
  798. &data_word(0x23631e28,0x90befffa); # u64
  799. &data_word(0xde82bde9,0xa4506ceb); # u64
  800. &data_word(0xb2c67915,0xbef9a3f7); # u64
  801. &data_word(0xe372532b,0xc67178f2); # u64
  802. &data_word(0xea26619c,0xca273ece); # u64
  803. &data_word(0x21c0c207,0xd186b8c7); # u64
  804. &data_word(0xcde0eb1e,0xeada7dd6); # u64
  805. &data_word(0xee6ed178,0xf57d4f7f); # u64
  806. &data_word(0x72176fba,0x06f067aa); # u64
  807. &data_word(0xa2c898a6,0x0a637dc5); # u64
  808. &data_word(0xbef90dae,0x113f9804); # u64
  809. &data_word(0x131c471b,0x1b710b35); # u64
  810. &data_word(0x23047d84,0x28db77f5); # u64
  811. &data_word(0x40c72493,0x32caab7b); # u64
  812. &data_word(0x15c9bebc,0x3c9ebe0a); # u64
  813. &data_word(0x9c100d4c,0x431d67c4); # u64
  814. &data_word(0xcb3e42b6,0x4cc5d4be); # u64
  815. &data_word(0xfc657e2a,0x597f299c); # u64
  816. &data_word(0x3ad6faec,0x5fcb6fab); # u64
  817. &data_word(0x4a475817,0x6c44198c); # u64
  818. &data_word(0x04050607,0x00010203); # byte swap
  819. &data_word(0x0c0d0e0f,0x08090a0b); # mask
  820. &function_end_B("sha512_block_data_order");
  821. &asciz("SHA512 block transform for x86, CRYPTOGAMS by <appro\@openssl.org>");
  822. &asm_finish();
  823. close STDOUT;