SHA256/512 for x86.
[openssl.git] / crypto / sha / asm / sha256-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 # SHA256 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           46      36      41      27      26
16 # icc           57      33      38      25      23      
17 # x86 asm       40      30      35      20      20
18 # x86_64 asm(*) -       -       21      15.8    16.5
19 #
20 # (*) x86_64 assembler performance is presented for reference
21 #     purposes.
22 #
23 # Performance improvement over compiler generated code varies from
24 # 10% to 40% [see above]. Not very impressive on some ยต-archs, but
25 # it's 5 times smaller and optimizies amount of writes.
26
27 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
28 push(@INC,"${dir}","${dir}../../perlasm");
29 require "x86asm.pl";
30
31 &asm_init($ARGV[0],"sha512-586.pl",$ARGV[$#ARGV] eq "386");
32
33 $A="eax";
34 $E="edx";
35 $T="ebx";
36 $Aoff=&DWP(0,"esp");
37 $Boff=&DWP(4,"esp");
38 $Coff=&DWP(8,"esp");
39 $Doff=&DWP(12,"esp");
40 $Eoff=&DWP(16,"esp");
41 $Foff=&DWP(20,"esp");
42 $Goff=&DWP(24,"esp");
43 $Hoff=&DWP(28,"esp");
44 $Xoff=&DWP(32,"esp");
45 $K256="ebp";
46
47 sub BODY_00_15() {
48         &mov    ("ecx",$E);
49         &ror    ("ecx",6);
50         &mov    ("edi",$E);
51         &ror    ("edi",11);
52          &mov   ("esi",$Foff);
53         &xor    ("ecx","edi");
54         &ror    ("edi",25-11);
55         &xor    ("ecx","edi");  # Sigma1(e)
56          &mov   ("edi",$Goff);
57         &add    ($T,"ecx");     # T += Sigma1(e)
58          &mov   ($Eoff,$E);     # modulo-scheduled
59
60         &xor    ("esi","edi");
61          &mov   ("ecx",$A);
62         &and    ("esi",$E);
63          &mov   ($E,$Doff);     # e becomes d, which is e in next iteration
64         &xor    ("esi","edi");  # Ch(e,f,g)
65          &mov   ("edi",$A);
66         &add    ($T,"esi");     # T += Ch(e,f,g)
67
68         &ror    ("ecx",2);
69          &add   ($T,$Hoff);     # T += h
70         &ror    ("edi",13);
71          &mov   ("esi",$Boff);
72         &xor    ("ecx","edi");
73         &ror    ("edi",22-13);
74          &add   ($E,$T);        # d += T
75         &xor    ("ecx","edi");  # Sigma0(a)
76          &mov   ("edi",$Coff);
77
78         &add    ($T,"ecx");     # T += Sigma0(a)
79          &mov   ($Aoff,$A);     # modulo-scheduled
80
81         &mov    ("ecx",$A);
82          &sub   ("esp",4);
83         &or     ($A,"esi");     # a becomes h, which is a in next iteration
84         &and    ("ecx","esi");
85         &and    ($A,"edi");
86          &mov   ("esi",&DWP(0,$K256));
87         &or     ($A,"ecx");     # h=Maj(a,b,c)
88
89         &add    ($K256,4);
90         &add    ($A,$T);        # h += T
91         &add    ($E,"esi");     # d += K256[i]
92         &add    ($A,"esi");     # h += K256[i]
93 }
94
95 &function_begin("sha256_block_data_order",16);
96         &mov    ("esi",wparam(0));      # ctx
97         &mov    ("edi",wparam(1));      # inp
98         &mov    ("eax",wparam(2));      # num
99         &mov    ("ebx","esp");          # saved sp
100
101         &call   (&label("pic_point"));  # make it PIC!
102 &set_label("pic_point");
103         &blindpop($K256);
104         &lea    ($K256,&DWP(&label("K256")."-".&label("pic_point"),$K256));
105
106         &sub    ("esp",16);
107         &and    ("esp",-64);
108
109         &shl    ("eax",6);
110         &add    ("eax","edi");
111         &mov    (&DWP(0,"esp"),"esi");  # ctx
112         &mov    (&DWP(4,"esp"),"edi");  # inp
113         &mov    (&DWP(8,"esp"),"eax");  # inp+num*128
114         &mov    (&DWP(12,"esp"),"ebx"); # saved sp
115
116 &set_label("loop",16);
117     # copy input block to stack reversing byte and dword order
118     for($i=0;$i<4;$i++) {
119         &mov    ("eax",&DWP($i*16+0,"edi"));
120         &mov    ("ebx",&DWP($i*16+4,"edi"));
121         &mov    ("ecx",&DWP($i*16+8,"edi"));
122         &mov    ("edx",&DWP($i*16+12,"edi"));
123         &bswap  ("eax");
124         &bswap  ("ebx");
125         &bswap  ("ecx");
126         &bswap  ("edx");
127         &push   ("eax");
128         &push   ("ebx");
129         &push   ("ecx");
130         &push   ("edx");
131     }
132         &add    ("edi",64);
133         &sub    ("esp",4*8);            # place for A,B,C,D,E,F,G,H
134         &mov    (&DWP(4*(8+16)+4,"esp"),"edi");
135
136         # copy ctx->h[0-7] to A,B,C,D,E,F,G,H on stack
137         &mov    ($A,&DWP(0,"esi"));
138         &mov    ("ebx",&DWP(4,"esi"));
139         &mov    ("ecx",&DWP(8,"esi"));
140         &mov    ("edi",&DWP(12,"esi"));
141         # &mov  ($Aoff,$A);
142         &mov    ($Boff,"ebx");
143         &mov    ($Coff,"ecx");
144         &mov    ($Doff,"edi");
145         &mov    ($E,&DWP(16,"esi"));    
146         &mov    ("ebx",&DWP(20,"esi"));
147         &mov    ("ecx",&DWP(24,"esi"));
148         &mov    ("edi",&DWP(28,"esi"));
149         # &mov  ($Eoff,$E);
150         &mov    ($Foff,"ebx");
151         &mov    ($Goff,"ecx");
152         &mov    ($Hoff,"edi");
153
154 &set_label("00_15",16);
155         &mov    ($T,&DWP(4*(8+15),"esp"));
156
157         &BODY_00_15();
158
159         &cmp    ("esi",0xc19bf174);
160         &jne    (&label("00_15"));
161
162 &set_label("16_63",16);
163         &mov    ($T,&DWP(4*(8+15+16-1),"esp"));
164          &mov   ("ecx",&DWP(4*(8+15+16-14),"esp"));
165         &mov    ("esi",$T);
166         &shr    ($T,3);
167         &ror    ("esi",7);
168         &xor    ($T,"esi");
169         &ror    ("esi",18-7);
170          &mov   ("edi","ecx");
171         &xor    ($T,"esi");                     # T = sigma0(X[-15])
172
173         &shr    ("ecx",10);
174          &mov   ("esi",&DWP(4*(8+15+16),"esp"));
175         &ror    ("edi",17);
176         &xor    ("ecx","edi");
177         &ror    ("edi",19-17);
178          &add   ($T,"esi");                     # T += X[-16]
179         &xor    ("ecx","edi")                   # sigma1(X[-2])
180
181         &add    ($T,"ecx");                     # T += sigma1(X[-2])
182         &add    ($T,&DWP(4*(8+15+16-9),"esp")); # T += X[-7]
183         &mov    (&DWP(4*(8+15),"esp"),$T);      # save X[0]
184
185         &BODY_00_15();
186
187         &cmp    ("esi",0xc67178f2);
188         &jne    (&label("16_63"));
189
190         &mov    ("esi",&DWP(4*(8+16+64)+0,"esp"));#ctx
191         # &mov  ($A,$Aoff);
192         &mov    ("ebx",$Boff);
193         &mov    ("ecx",$Coff);
194         &mov    ("edi",$Doff);
195         &add    ($A,&DWP(0,"esi"));
196         &add    ("ebx",&DWP(4,"esi"));
197         &add    ("ecx",&DWP(8,"esi"));
198         &add    ("edi",&DWP(12,"esi"));
199         &mov    (&DWP(0,"esi"),$A);
200         &mov    (&DWP(4,"esi"),"ebx");
201         &mov    (&DWP(8,"esi"),"ecx");
202         &mov    (&DWP(12,"esi"),"edi");
203         # &mov  ($E,$Eoff);
204         &mov    ("eax",$Foff);
205         &mov    ("ebx",$Goff);
206         &mov    ("ecx",$Hoff);
207         &mov    ("edi",&DWP(4*(8+16+64)+4,"esp"));#inp
208         &add    ($E,&DWP(16,"esi"));
209         &add    ("eax",&DWP(20,"esi"));
210         &add    ("ebx",&DWP(24,"esi"));
211         &add    ("ecx",&DWP(28,"esi"));
212         &mov    (&DWP(16,"esi"),$E);
213         &mov    (&DWP(20,"esi"),"eax");
214         &mov    (&DWP(24,"esi"),"ebx");
215         &mov    (&DWP(28,"esi"),"ecx");
216
217         &add    ("esp",4*(8+16+64));            # destroy frame
218         &sub    ($K256,4*64);                   # rewind K
219
220         &cmp    ("edi",&DWP(8,"esp"));          # are we done yet?
221         &jb     (&label("loop"));
222
223         &mov    ("esp",&DWP(12,"esp"));         # restore sp
224 &function_end_A();
225
226 &set_label("K256",64);  # Yes! I keep it in the code segment!
227         &data_word(0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5);
228         &data_word(0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5);
229         &data_word(0xd807aa98,0x12835b01,0x243185be,0x550c7dc3);
230         &data_word(0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174);
231         &data_word(0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc);
232         &data_word(0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da);
233         &data_word(0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7);
234         &data_word(0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967);
235         &data_word(0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13);
236         &data_word(0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85);
237         &data_word(0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3);
238         &data_word(0xd192e819,0xd6990624,0xf40e3585,0x106aa070);
239         &data_word(0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5);
240         &data_word(0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3);
241         &data_word(0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208);
242         &data_word(0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2);
243 &function_end_B("sha256_block_data_order");
244 &asciz("SHA256 block transform for x86, CRYPTOGAMS by <appro\@openssl.org>");
245
246 &asm_finish();