sha512-586.pl 26 KB

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