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