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