sha512-586.pl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. #!/usr/bin/env perl
  2. #
  3. # ====================================================================
  4. # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
  5. # project. The module is, however, dual licensed under OpenSSL and
  6. # CRYPTOGAMS licenses depending on where you obtain it. For further
  7. # details see http://www.openssl.org/~appro/cryptogams/.
  8. # ====================================================================
  9. #
  10. # SHA512 block transform for x86. September 2007.
  11. #
  12. # Performance in clock cycles per processed byte (less is better):
  13. #
  14. # Pentium PIII P4 AMD K8 Core2
  15. # gcc 100 75 116 54 66
  16. # icc 97 77 95 55 57
  17. # x86 asm 61 56 82 36 40
  18. # SSE2 asm - - 38 24 20
  19. # x86_64 asm(*) - - 30 10.0 10.5
  20. #
  21. # (*) x86_64 assembler performance is presented for reference
  22. # purposes.
  23. #
  24. # IALU code-path is optimized for elder Pentiums. On vanilla Pentium
  25. # performance improvement over compiler generated code reaches ~60%,
  26. # while on PIII - ~35%. On newer µ-archs improvement varies from 15%
  27. # to 50%, but it's less important as they are expected to execute SSE2
  28. # code-path, which is commonly ~2-3x faster [than compiler generated
  29. # code]. SSE2 code-path is as fast as original sha512-sse2.pl, even
  30. # though it does not use 128-bit operations. The latter means that
  31. # SSE2-aware kernel is no longer required to execute the code. Another
  32. # difference is that new code optimizes amount of writes, but at the
  33. # cost of increased data cache "footprint" by 1/2KB.
  34. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  35. push(@INC,"${dir}","${dir}../../perlasm");
  36. require "x86asm.pl";
  37. &asm_init($ARGV[0],"sha512-586.pl",$ARGV[$#ARGV] eq "386");
  38. $sse2=0;
  39. for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
  40. &external_label("OPENSSL_ia32cap_P") if ($sse2);
  41. $Tlo=&DWP(0,"esp"); $Thi=&DWP(4,"esp");
  42. $Alo=&DWP(8,"esp"); $Ahi=&DWP(8+4,"esp");
  43. $Blo=&DWP(16,"esp"); $Bhi=&DWP(16+4,"esp");
  44. $Clo=&DWP(24,"esp"); $Chi=&DWP(24+4,"esp");
  45. $Dlo=&DWP(32,"esp"); $Dhi=&DWP(32+4,"esp");
  46. $Elo=&DWP(40,"esp"); $Ehi=&DWP(40+4,"esp");
  47. $Flo=&DWP(48,"esp"); $Fhi=&DWP(48+4,"esp");
  48. $Glo=&DWP(56,"esp"); $Ghi=&DWP(56+4,"esp");
  49. $Hlo=&DWP(64,"esp"); $Hhi=&DWP(64+4,"esp");
  50. $K512="ebp";
  51. $Asse2=&QWP(0,"esp");
  52. $Bsse2=&QWP(8,"esp");
  53. $Csse2=&QWP(16,"esp");
  54. $Dsse2=&QWP(24,"esp");
  55. $Esse2=&QWP(32,"esp");
  56. $Fsse2=&QWP(40,"esp");
  57. $Gsse2=&QWP(48,"esp");
  58. $Hsse2=&QWP(56,"esp");
  59. $A="mm0"; # B-D and
  60. $E="mm4"; # F-H are commonly loaded to respectively mm1-mm3 and
  61. # mm5-mm7, but it's done on on-demand basis...
  62. sub BODY_00_15_sse2 {
  63. my $prefetch=shift;
  64. &movq ("mm5",$Fsse2); # load f
  65. &movq ("mm6",$Gsse2); # load g
  66. &movq ("mm7",$Hsse2); # load h
  67. &movq ("mm1",$E); # %mm1 is sliding right
  68. &movq ("mm2",$E); # %mm2 is sliding left
  69. &psrlq ("mm1",14);
  70. &movq ($Esse2,$E); # modulo-scheduled save e
  71. &psllq ("mm2",23);
  72. &movq ("mm3","mm1"); # %mm3 is T1
  73. &psrlq ("mm1",4);
  74. &pxor ("mm3","mm2");
  75. &psllq ("mm2",23);
  76. &pxor ("mm3","mm1");
  77. &psrlq ("mm1",23);
  78. &pxor ("mm3","mm2");
  79. &psllq ("mm2",4);
  80. &pxor ("mm3","mm1");
  81. &paddq ("mm7",QWP(0,$K512)); # h+=K512[i]
  82. &pxor ("mm3","mm2"); # T1=Sigma1_512(e)
  83. &pxor ("mm5","mm6"); # f^=g
  84. &movq ("mm1",$Bsse2); # load b
  85. &pand ("mm5",$E); # f&=e
  86. &movq ("mm2",$Csse2); # load c
  87. &pxor ("mm5","mm6"); # f^=g
  88. &movq ($E,$Dsse2); # e = load d
  89. &paddq ("mm3","mm5"); # T1+=Ch(e,f,g)
  90. &movq (&QWP(0,"esp"),$A); # modulo-scheduled save a
  91. &paddq ("mm3","mm7"); # T1+=h
  92. &movq ("mm5",$A); # %mm5 is sliding right
  93. &movq ("mm6",$A); # %mm6 is sliding left
  94. &paddq ("mm3",&QWP(8*9,"esp")); # T1+=X[0]
  95. &psrlq ("mm5",28);
  96. &paddq ($E,"mm3"); # e += T1
  97. &psllq ("mm6",25);
  98. &movq ("mm7","mm5"); # %mm7 is T2
  99. &psrlq ("mm5",6);
  100. &pxor ("mm7","mm6");
  101. &psllq ("mm6",5);
  102. &pxor ("mm7","mm5");
  103. &psrlq ("mm5",5);
  104. &pxor ("mm7","mm6");
  105. &psllq ("mm6",6);
  106. &pxor ("mm7","mm5");
  107. &sub ("esp",8);
  108. &pxor ("mm7","mm6"); # T2=Sigma0_512(a)
  109. &movq ("mm5",$A); # %mm5=a
  110. &por ($A,"mm2"); # a=a|c
  111. &movq ("mm6",&QWP(8*(9+16-14),"esp")) if ($prefetch);
  112. &pand ("mm5","mm2"); # %mm5=a&c
  113. &pand ($A,"mm1"); # a=(a|c)&b
  114. &movq ("mm2",&QWP(8*(9+16-1),"esp")) if ($prefetch);
  115. &por ("mm5",$A); # %mm5=(a&c)|((a|c)&b)
  116. &paddq ("mm7","mm5"); # T2+=Maj(a,b,c)
  117. &movq ($A,"mm3"); # a=T1
  118. &mov (&LB("edx"),&BP(0,$K512));
  119. &paddq ($A,"mm7"); # a+=T2
  120. &add ($K512,8);
  121. }
  122. sub BODY_00_15_x86 {
  123. #define Sigma1(x) (ROTR((x),14) ^ ROTR((x),18) ^ ROTR((x),41))
  124. # LO lo>>14^hi<<18 ^ lo>>18^hi<<14 ^ hi>>9^lo<<23
  125. # HI hi>>14^lo<<18 ^ hi>>18^lo<<14 ^ lo>>9^hi<<23
  126. &mov ("ecx",$Elo);
  127. &mov ("edx",$Ehi);
  128. &mov ("esi","ecx");
  129. &shr ("ecx",9) # lo>>9
  130. &mov ("edi","edx");
  131. &shr ("edx",9) # hi>>9
  132. &mov ("ebx","ecx");
  133. &shl ("esi",14); # lo<<14
  134. &mov ("eax","edx");
  135. &shl ("edi",14); # hi<<14
  136. &xor ("ebx","esi");
  137. &shr ("ecx",14-9); # lo>>14
  138. &xor ("eax","edi");
  139. &shr ("edx",14-9); # hi>>14
  140. &xor ("eax","ecx");
  141. &shl ("esi",18-14); # lo<<18
  142. &xor ("ebx","edx");
  143. &shl ("edi",18-14); # hi<<18
  144. &xor ("ebx","esi");
  145. &shr ("ecx",18-14); # lo>>18
  146. &xor ("eax","edi");
  147. &shr ("edx",18-14); # hi>>18
  148. &xor ("eax","ecx");
  149. &shl ("esi",23-18); # lo<<23
  150. &xor ("ebx","edx");
  151. &shl ("edi",23-18); # hi<<23
  152. &xor ("eax","esi");
  153. &xor ("ebx","edi"); # T1 = Sigma1(e)
  154. &mov ("ecx",$Flo);
  155. &mov ("edx",$Fhi);
  156. &mov ("esi",$Glo);
  157. &mov ("edi",$Ghi);
  158. &add ("eax",$Hlo);
  159. &adc ("ebx",$Hhi); # T1 += h
  160. &xor ("ecx","esi");
  161. &xor ("edx","edi");
  162. &and ("ecx",$Elo);
  163. &and ("edx",$Ehi);
  164. &add ("eax",&DWP(8*(9+15)+0,"esp"));
  165. &adc ("ebx",&DWP(8*(9+15)+4,"esp")); # T1 += X[0]
  166. &xor ("ecx","esi");
  167. &xor ("edx","edi"); # Ch(e,f,g) = (f^g)&e)^g
  168. &mov ("esi",&DWP(0,$K512));
  169. &mov ("edi",&DWP(4,$K512)); # K[i]
  170. &add ("eax","ecx");
  171. &adc ("ebx","edx"); # T1 += Ch(e,f,g)
  172. &mov ("ecx",$Dlo);
  173. &mov ("edx",$Dhi);
  174. &add ("eax","esi");
  175. &adc ("ebx","edi"); # T1 += K[i]
  176. &mov ($Tlo,"eax");
  177. &mov ($Thi,"ebx"); # put T1 away
  178. &add ("eax","ecx");
  179. &adc ("ebx","edx"); # d += T1
  180. #define Sigma0(x) (ROTR((x),28) ^ ROTR((x),34) ^ ROTR((x),39))
  181. # LO lo>>28^hi<<4 ^ hi>>2^lo<<30 ^ hi>>7^lo<<25
  182. # HI hi>>28^lo<<4 ^ lo>>2^hi<<30 ^ lo>>7^hi<<25
  183. &mov ("ecx",$Alo);
  184. &mov ("edx",$Ahi);
  185. &mov ($Dlo,"eax");
  186. &mov ($Dhi,"ebx");
  187. &mov ("esi","ecx");
  188. &shr ("ecx",2) # lo>>2
  189. &mov ("edi","edx");
  190. &shr ("edx",2) # hi>>2
  191. &mov ("ebx","ecx");
  192. &shl ("esi",4); # lo<<4
  193. &mov ("eax","edx");
  194. &shl ("edi",4); # hi<<4
  195. &xor ("ebx","esi");
  196. &shr ("ecx",7-2); # lo>>7
  197. &xor ("eax","edi");
  198. &shr ("edx",7-2); # hi>>7
  199. &xor ("ebx","ecx");
  200. &shl ("esi",25-4); # lo<<25
  201. &xor ("eax","edx");
  202. &shl ("edi",25-4); # hi<<25
  203. &xor ("eax","esi");
  204. &shr ("ecx",28-7); # lo>>28
  205. &xor ("ebx","edi");
  206. &shr ("edx",28-7); # hi>>28
  207. &xor ("eax","ecx");
  208. &shl ("esi",30-25); # lo<<30
  209. &xor ("ebx","edx");
  210. &shl ("edi",30-25); # hi<<30
  211. &xor ("eax","esi");
  212. &xor ("ebx","edi"); # Sigma0(a)
  213. &mov ("ecx",$Alo);
  214. &mov ("edx",$Ahi);
  215. &mov ("esi",$Blo);
  216. &mov ("edi",$Bhi);
  217. &add ("eax",$Tlo);
  218. &adc ("ebx",$Thi); # T1 = Sigma0(a)+T1
  219. &or ("ecx","esi");
  220. &or ("edx","edi");
  221. &and ("ecx",$Clo);
  222. &and ("edx",$Chi);
  223. &and ("esi",$Alo);
  224. &and ("edi",$Ahi);
  225. &or ("ecx","esi");
  226. &or ("edx","edi"); # Maj(a,b,c) = ((a|b)&c)|(a&b)
  227. &add ("eax","ecx");
  228. &adc ("ebx","edx"); # T1 += Maj(a,b,c)
  229. &mov ($Tlo,"eax");
  230. &mov ($Thi,"ebx");
  231. &mov (&LB("edx"),&BP(0,$K512)); # pre-fetch LSB of *K
  232. &sub ("esp",8);
  233. &lea ($K512,&DWP(8,$K512)); # K++
  234. }
  235. &function_begin("sha512_block_data_order",16);
  236. &mov ("esi",wparam(0)); # ctx
  237. &mov ("edi",wparam(1)); # inp
  238. &mov ("eax",wparam(2)); # num
  239. &mov ("ebx","esp"); # saved sp
  240. &call (&label("pic_point")); # make it PIC!
  241. &set_label("pic_point");
  242. &blindpop($K512);
  243. &lea ($K512,&DWP(&label("K512")."-".&label("pic_point"),$K512));
  244. &sub ("esp",16);
  245. &and ("esp",-64);
  246. &shl ("eax",7);
  247. &add ("eax","edi");
  248. &mov (&DWP(0,"esp"),"esi"); # ctx
  249. &mov (&DWP(4,"esp"),"edi"); # inp
  250. &mov (&DWP(8,"esp"),"eax"); # inp+num*128
  251. &mov (&DWP(12,"esp"),"ebx"); # saved sp
  252. if ($sse2) {
  253. &picmeup("edx","OPENSSL_ia32cap_P",$K512,&label("K512"));
  254. &bt (&DWP(0,"edx"),26);
  255. &jnc (&label("loop_x86"));
  256. # load ctx->h[0-7]
  257. &movq ($A,&QWP(0,"esi"));
  258. &movq ("mm1",&QWP(8,"esi"));
  259. &movq ("mm2",&QWP(16,"esi"));
  260. &movq ("mm3",&QWP(24,"esi"));
  261. &movq ($E,&QWP(32,"esi"));
  262. &movq ("mm5",&QWP(40,"esi"));
  263. &movq ("mm6",&QWP(48,"esi"));
  264. &movq ("mm7",&QWP(56,"esi"));
  265. &sub ("esp",8*10);
  266. &set_label("loop_sse2",16);
  267. # &movq ($Asse2,$A);
  268. &movq ($Bsse2,"mm1");
  269. &movq ($Csse2,"mm2");
  270. &movq ($Dsse2,"mm3");
  271. # &movq ($Esse2,$E);
  272. &movq ($Fsse2,"mm5");
  273. &movq ($Gsse2,"mm6");
  274. &movq ($Hsse2,"mm7");
  275. &mov ("ecx",&DWP(0,"edi"));
  276. &mov ("edx",&DWP(4,"edi"));
  277. &add ("edi",8);
  278. &bswap ("ecx");
  279. &bswap ("edx");
  280. &mov (&DWP(8*9+4,"esp"),"ecx");
  281. &mov (&DWP(8*9+0,"esp"),"edx");
  282. &set_label("00_14_sse2",16);
  283. &mov ("eax",&DWP(0,"edi"));
  284. &mov ("ebx",&DWP(4,"edi"));
  285. &add ("edi",8);
  286. &bswap ("eax");
  287. &bswap ("ebx");
  288. &mov (&DWP(8*8+4,"esp"),"eax");
  289. &mov (&DWP(8*8+0,"esp"),"ebx");
  290. &BODY_00_15_sse2();
  291. &cmp (&LB("edx"),0x35);
  292. &jne (&label("00_14_sse2"));
  293. &BODY_00_15_sse2(1);
  294. &set_label("16_79_sse2",16);
  295. #&movq ("mm2",&QWP(8*(9+16-1),"esp")); #prefetched in BODY_00_15
  296. #&movq ("mm6",&QWP(8*(9+16-14),"esp"));
  297. &movq ("mm1","mm2");
  298. &psrlq ("mm2",1);
  299. &movq ("mm7","mm6");
  300. &psrlq ("mm6",6);
  301. &movq ("mm3","mm2");
  302. &psrlq ("mm2",7-1);
  303. &movq ("mm5","mm6");
  304. &psrlq ("mm6",19-6);
  305. &pxor ("mm3","mm2");
  306. &psrlq ("mm2",8-7);
  307. &pxor ("mm5","mm6");
  308. &psrlq ("mm6",61-19);
  309. &pxor ("mm3","mm2");
  310. &movq ("mm2",&QWP(8*(9+16),"esp"));
  311. &psllq ("mm1",56);
  312. &pxor ("mm5","mm6");
  313. &psllq ("mm7",3);
  314. &pxor ("mm3","mm1");
  315. &paddq ("mm2",&QWP(8*(9+16-9),"esp"));
  316. &psllq ("mm1",63-56);
  317. &pxor ("mm5","mm7");
  318. &psllq ("mm7",45-3);
  319. &pxor ("mm3","mm1");
  320. &pxor ("mm5","mm7");
  321. &paddq ("mm3","mm5");
  322. &paddq ("mm3","mm2");
  323. &movq (&QWP(8*9,"esp"),"mm3");
  324. &BODY_00_15_sse2(1);
  325. &cmp (&LB("edx"),0x17);
  326. &jne (&label("16_79_sse2"));
  327. # &movq ($A,$Asse2);
  328. &movq ("mm1",$Bsse2);
  329. &movq ("mm2",$Csse2);
  330. &movq ("mm3",$Dsse2);
  331. # &movq ($E,$Esse2);
  332. &movq ("mm5",$Fsse2);
  333. &movq ("mm6",$Gsse2);
  334. &movq ("mm7",$Hsse2);
  335. &paddq ($A,&QWP(0,"esi"));
  336. &paddq ("mm1",&QWP(8,"esi"));
  337. &paddq ("mm2",&QWP(16,"esi"));
  338. &paddq ("mm3",&QWP(24,"esi"));
  339. &paddq ($E,&QWP(32,"esi"));
  340. &paddq ("mm5",&QWP(40,"esi"));
  341. &paddq ("mm6",&QWP(48,"esi"));
  342. &paddq ("mm7",&QWP(56,"esi"));
  343. &movq (&QWP(0,"esi"),$A);
  344. &movq (&QWP(8,"esi"),"mm1");
  345. &movq (&QWP(16,"esi"),"mm2");
  346. &movq (&QWP(24,"esi"),"mm3");
  347. &movq (&QWP(32,"esi"),$E);
  348. &movq (&QWP(40,"esi"),"mm5");
  349. &movq (&QWP(48,"esi"),"mm6");
  350. &movq (&QWP(56,"esi"),"mm7");
  351. &add ("esp",8*80); # destroy frame
  352. &sub ($K512,8*80); # rewind K
  353. &cmp ("edi",&DWP(8*10+8,"esp")); # are we done yet?
  354. &jb (&label("loop_sse2"));
  355. &emms ();
  356. &mov ("esp",&DWP(8*10+12,"esp")); # restore sp
  357. &function_end_A();
  358. }
  359. &set_label("loop_x86",16);
  360. # copy input block to stack reversing byte and qword order
  361. for ($i=0;$i<8;$i++) {
  362. &mov ("eax",&DWP($i*16+0,"edi"));
  363. &mov ("ebx",&DWP($i*16+4,"edi"));
  364. &mov ("ecx",&DWP($i*16+8,"edi"));
  365. &mov ("edx",&DWP($i*16+12,"edi"));
  366. &bswap ("eax");
  367. &bswap ("ebx");
  368. &bswap ("ecx");
  369. &bswap ("edx");
  370. &push ("eax");
  371. &push ("ebx");
  372. &push ("ecx");
  373. &push ("edx");
  374. }
  375. &add ("edi",128);
  376. &sub ("esp",9*8); # place for T,A,B,C,D,E,F,G,H
  377. &mov (&DWP(8*(9+16)+4,"esp"),"edi");
  378. # copy ctx->h[0-7] to A,B,C,D,E,F,G,H on stack
  379. &lea ("edi",&DWP(8,"esp"));
  380. &mov ("ecx",16);
  381. &data_word(0xA5F3F689); # rep movsd
  382. &set_label("00_15_x86",16);
  383. &BODY_00_15_x86();
  384. &cmp (&LB("edx"),0x94);
  385. &jne (&label("00_15_x86"));
  386. &set_label("16_79_x86",16);
  387. #define sigma0(x) (ROTR((x),1) ^ ROTR((x),8) ^ ((x)>>7))
  388. # LO lo>>1^hi<<31 ^ lo>>8^hi<<24 ^ lo>>7^hi<<25
  389. # HI hi>>1^lo<<31 ^ hi>>8^lo<<24 ^ hi>>7
  390. &mov ("ecx",&DWP(8*(9+15+16-1)+0,"esp"));
  391. &mov ("edx",&DWP(8*(9+15+16-1)+4,"esp"));
  392. &mov ("esi","ecx");
  393. &shr ("ecx",1) # lo>>1
  394. &mov ("edi","edx");
  395. &shr ("edx",1) # hi>>1
  396. &mov ("eax","ecx");
  397. &shl ("esi",24); # lo<<24
  398. &mov ("ebx","edx");
  399. &shl ("edi",24); # hi<<24
  400. &xor ("ebx","esi");
  401. &shr ("ecx",7-1); # lo>>7
  402. &xor ("eax","edi");
  403. &shr ("edx",7-1); # hi>>7
  404. &xor ("eax","ecx");
  405. &shl ("esi",31-24); # lo<<31
  406. &xor ("ebx","edx");
  407. &shl ("edi",25-24); # hi<<25
  408. &xor ("ebx","esi");
  409. &shr ("ecx",8-7); # lo>>8
  410. &xor ("eax","edi");
  411. &shr ("edx",8-7); # hi>>8
  412. &xor ("eax","ecx");
  413. &shl ("edi",31-25); # hi<<31
  414. &xor ("ebx","edx");
  415. &xor ("eax","edi"); # T1 = sigma0(X[-15])
  416. &mov (&DWP(0,"esp"),"eax");
  417. &mov (&DWP(4,"esp"),"ebx"); # put T1 away
  418. #define sigma1(x) (ROTR((x),19) ^ ROTR((x),61) ^ ((x)>>6))
  419. # LO lo>>19^hi<<13 ^ hi>>29^lo<<3 ^ lo>>6^hi<<26
  420. # HI hi>>19^lo<<13 ^ lo>>29^hi<<3 ^ hi>>6
  421. &mov ("ecx",&DWP(8*(9+15+16-14)+0,"esp"));
  422. &mov ("edx",&DWP(8*(9+15+16-14)+4,"esp"));
  423. &mov ("esi","ecx");
  424. &shr ("ecx",6) # lo>>6
  425. &mov ("edi","edx");
  426. &shr ("edx",6) # hi>>6
  427. &mov ("eax","ecx");
  428. &shl ("esi",3); # lo<<3
  429. &mov ("ebx","edx");
  430. &shl ("edi",3); # hi<<3
  431. &xor ("eax","esi");
  432. &shr ("ecx",19-6); # lo>>19
  433. &xor ("ebx","edi");
  434. &shr ("edx",19-6); # hi>>19
  435. &xor ("eax","ecx");
  436. &shl ("esi",13-3); # lo<<13
  437. &xor ("ebx","edx");
  438. &shl ("edi",13-3); # hi<<13
  439. &xor ("ebx","esi");
  440. &shr ("ecx",29-19); # lo>>29
  441. &xor ("eax","edi");
  442. &shr ("edx",29-19); # hi>>29
  443. &xor ("ebx","ecx");
  444. &shl ("edi",26-13); # hi<<26
  445. &xor ("eax","edx");
  446. &xor ("eax","edi"); # sigma1(X[-2])
  447. &mov ("ecx",&DWP(8*(9+15+16)+0,"esp"));
  448. &mov ("edx",&DWP(8*(9+15+16)+4,"esp"));
  449. &add ("eax",&DWP(0,"esp"));
  450. &adc ("ebx",&DWP(4,"esp")); # T1 = sigma1(X[-2])+T1
  451. &mov ("esi",&DWP(8*(9+15+16-9)+0,"esp"));
  452. &mov ("edi",&DWP(8*(9+15+16-9)+4,"esp"));
  453. &add ("eax","ecx");
  454. &adc ("ebx","edx"); # T1 += X[-16]
  455. &add ("eax","esi");
  456. &adc ("ebx","edi"); # T1 += X[-7]
  457. &mov (&DWP(8*(9+15)+0,"esp"),"eax");
  458. &mov (&DWP(8*(9+15)+4,"esp"),"ebx"); # save X[0]
  459. &BODY_00_15_x86();
  460. &cmp (&LB("edx"),0x17);
  461. &jne (&label("16_79_x86"));
  462. &mov ("esi",&DWP(8*(9+16+80)+0,"esp"));# ctx
  463. &mov ("edi",&DWP(8*(9+16+80)+4,"esp"));# inp
  464. for($i=0;$i<4;$i++) {
  465. &mov ("eax",&DWP($i*16+0,"esi"));
  466. &mov ("ebx",&DWP($i*16+4,"esi"));
  467. &mov ("ecx",&DWP($i*16+8,"esi"));
  468. &mov ("edx",&DWP($i*16+12,"esi"));
  469. &add ("eax",&DWP(8+($i*16)+0,"esp"));
  470. &adc ("ebx",&DWP(8+($i*16)+4,"esp"));
  471. &mov (&DWP($i*16+0,"esi"),"eax");
  472. &mov (&DWP($i*16+4,"esi"),"ebx");
  473. &add ("ecx",&DWP(8+($i*16)+8,"esp"));
  474. &adc ("edx",&DWP(8+($i*16)+12,"esp"));
  475. &mov (&DWP($i*16+8,"esi"),"ecx");
  476. &mov (&DWP($i*16+12,"esi"),"edx");
  477. }
  478. &add ("esp",8*(9+16+80)); # destroy frame
  479. &sub ($K512,8*80); # rewind K
  480. &cmp ("edi",&DWP(8,"esp")); # are we done yet?
  481. &jb (&label("loop_x86"));
  482. &mov ("esp",&DWP(12,"esp")); # restore sp
  483. &function_end_A();
  484. &set_label("K512",64); # Yes! I keep it in the code segment!
  485. &data_word(0xd728ae22,0x428a2f98); # u64
  486. &data_word(0x23ef65cd,0x71374491); # u64
  487. &data_word(0xec4d3b2f,0xb5c0fbcf); # u64
  488. &data_word(0x8189dbbc,0xe9b5dba5); # u64
  489. &data_word(0xf348b538,0x3956c25b); # u64
  490. &data_word(0xb605d019,0x59f111f1); # u64
  491. &data_word(0xaf194f9b,0x923f82a4); # u64
  492. &data_word(0xda6d8118,0xab1c5ed5); # u64
  493. &data_word(0xa3030242,0xd807aa98); # u64
  494. &data_word(0x45706fbe,0x12835b01); # u64
  495. &data_word(0x4ee4b28c,0x243185be); # u64
  496. &data_word(0xd5ffb4e2,0x550c7dc3); # u64
  497. &data_word(0xf27b896f,0x72be5d74); # u64
  498. &data_word(0x3b1696b1,0x80deb1fe); # u64
  499. &data_word(0x25c71235,0x9bdc06a7); # u64
  500. &data_word(0xcf692694,0xc19bf174); # u64
  501. &data_word(0x9ef14ad2,0xe49b69c1); # u64
  502. &data_word(0x384f25e3,0xefbe4786); # u64
  503. &data_word(0x8b8cd5b5,0x0fc19dc6); # u64
  504. &data_word(0x77ac9c65,0x240ca1cc); # u64
  505. &data_word(0x592b0275,0x2de92c6f); # u64
  506. &data_word(0x6ea6e483,0x4a7484aa); # u64
  507. &data_word(0xbd41fbd4,0x5cb0a9dc); # u64
  508. &data_word(0x831153b5,0x76f988da); # u64
  509. &data_word(0xee66dfab,0x983e5152); # u64
  510. &data_word(0x2db43210,0xa831c66d); # u64
  511. &data_word(0x98fb213f,0xb00327c8); # u64
  512. &data_word(0xbeef0ee4,0xbf597fc7); # u64
  513. &data_word(0x3da88fc2,0xc6e00bf3); # u64
  514. &data_word(0x930aa725,0xd5a79147); # u64
  515. &data_word(0xe003826f,0x06ca6351); # u64
  516. &data_word(0x0a0e6e70,0x14292967); # u64
  517. &data_word(0x46d22ffc,0x27b70a85); # u64
  518. &data_word(0x5c26c926,0x2e1b2138); # u64
  519. &data_word(0x5ac42aed,0x4d2c6dfc); # u64
  520. &data_word(0x9d95b3df,0x53380d13); # u64
  521. &data_word(0x8baf63de,0x650a7354); # u64
  522. &data_word(0x3c77b2a8,0x766a0abb); # u64
  523. &data_word(0x47edaee6,0x81c2c92e); # u64
  524. &data_word(0x1482353b,0x92722c85); # u64
  525. &data_word(0x4cf10364,0xa2bfe8a1); # u64
  526. &data_word(0xbc423001,0xa81a664b); # u64
  527. &data_word(0xd0f89791,0xc24b8b70); # u64
  528. &data_word(0x0654be30,0xc76c51a3); # u64
  529. &data_word(0xd6ef5218,0xd192e819); # u64
  530. &data_word(0x5565a910,0xd6990624); # u64
  531. &data_word(0x5771202a,0xf40e3585); # u64
  532. &data_word(0x32bbd1b8,0x106aa070); # u64
  533. &data_word(0xb8d2d0c8,0x19a4c116); # u64
  534. &data_word(0x5141ab53,0x1e376c08); # u64
  535. &data_word(0xdf8eeb99,0x2748774c); # u64
  536. &data_word(0xe19b48a8,0x34b0bcb5); # u64
  537. &data_word(0xc5c95a63,0x391c0cb3); # u64
  538. &data_word(0xe3418acb,0x4ed8aa4a); # u64
  539. &data_word(0x7763e373,0x5b9cca4f); # u64
  540. &data_word(0xd6b2b8a3,0x682e6ff3); # u64
  541. &data_word(0x5defb2fc,0x748f82ee); # u64
  542. &data_word(0x43172f60,0x78a5636f); # u64
  543. &data_word(0xa1f0ab72,0x84c87814); # u64
  544. &data_word(0x1a6439ec,0x8cc70208); # u64
  545. &data_word(0x23631e28,0x90befffa); # u64
  546. &data_word(0xde82bde9,0xa4506ceb); # u64
  547. &data_word(0xb2c67915,0xbef9a3f7); # u64
  548. &data_word(0xe372532b,0xc67178f2); # u64
  549. &data_word(0xea26619c,0xca273ece); # u64
  550. &data_word(0x21c0c207,0xd186b8c7); # u64
  551. &data_word(0xcde0eb1e,0xeada7dd6); # u64
  552. &data_word(0xee6ed178,0xf57d4f7f); # u64
  553. &data_word(0x72176fba,0x06f067aa); # u64
  554. &data_word(0xa2c898a6,0x0a637dc5); # u64
  555. &data_word(0xbef90dae,0x113f9804); # u64
  556. &data_word(0x131c471b,0x1b710b35); # u64
  557. &data_word(0x23047d84,0x28db77f5); # u64
  558. &data_word(0x40c72493,0x32caab7b); # u64
  559. &data_word(0x15c9bebc,0x3c9ebe0a); # u64
  560. &data_word(0x9c100d4c,0x431d67c4); # u64
  561. &data_word(0xcb3e42b6,0x4cc5d4be); # u64
  562. &data_word(0xfc657e2a,0x597f299c); # u64
  563. &data_word(0x3ad6faec,0x5fcb6fab); # u64
  564. &data_word(0x4a475817,0x6c44198c); # u64
  565. &function_end_B("sha512_block_data_order");
  566. &asciz("SHA512 block transform for x86, CRYPTOGAMS by <appro\@openssl.org>");
  567. &asm_finish();