xref: /openbsd-src/lib/libcrypto/sha/asm/sha256-586.pl (revision 0bd8ca4ac38b898e216d95544f90f11cd082dd63)
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	33	20	18
18# x86_64 asm(*)	-	-	21	16	16
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;
28push(@INC,"${dir}","${dir}../../perlasm");
29require "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
47sub BODY_00_15() {
48    my $in_16_63=shift;
49
50	&mov	("ecx",$E);
51	 &add	($T,"edi")			if ($in_16_63);	# T += sigma1(X[-2])
52	&ror	("ecx",25-11);
53	 &mov	("esi",$Foff);
54	&xor	("ecx",$E);
55	&ror	("ecx",11-6);
56	 &mov	(&DWP(4*(8+15),"esp"),$T)	if ($in_16_63);	# save X[0]
57	&xor	("ecx",$E);
58	&ror	("ecx",6);	# Sigma1(e)
59	 &mov	("edi",$Goff);
60	&add	($T,"ecx");	# T += Sigma1(e)
61
62	&xor	("esi","edi");
63	 &mov	($Eoff,$E);	# modulo-scheduled
64	 &mov	("ecx",$A);
65	&and	("esi",$E);
66	 &mov	($E,$Doff);	# e becomes d, which is e in next iteration
67	&xor	("esi","edi");	# Ch(e,f,g)
68	 &mov	("edi",$A);
69	&add	($T,"esi");	# T += Ch(e,f,g)
70
71	&ror	("ecx",22-13);
72	 &add	($T,$Hoff);	# T += h
73	&xor	("ecx",$A);
74	&ror	("ecx",13-2);
75	 &mov	("esi",$Boff);
76	&xor	("ecx",$A);
77	&ror	("ecx",2);	# Sigma0(a)
78	 &add	($E,$T);	# d += T
79	 &mov	("edi",$Coff);
80
81	&add	($T,"ecx");	# T += Sigma0(a)
82	 &mov	($Aoff,$A);	# modulo-scheduled
83
84	&mov	("ecx",$A);
85	 &sub	("esp",4);
86	&or	($A,"esi");	# a becomes h, which is a in next iteration
87	&and	("ecx","esi");
88	&and	($A,"edi");
89	 &mov	("esi",&DWP(0,$K256));
90	&or	($A,"ecx");	# h=Maj(a,b,c)
91
92	&add	($K256,4);
93	&add	($A,$T);	# h += T
94	 &mov	($T,&DWP(4*(8+15+16-1),"esp"))	if ($in_16_63);	# preload T
95	&add	($E,"esi");	# d += K256[i]
96	&add	($A,"esi");	# h += K256[i]
97}
98
99&static_label("K256");
100&function_begin("sha256_block_data_order");
101	&mov	("esi",wparam(0));	# ctx
102	&mov	("edi",wparam(1));	# inp
103	&mov	("eax",wparam(2));	# num
104	&mov	("ebx","esp");		# saved sp
105
106	&picsetup($K256);
107	&picsymbol($K256, &label("K256"), $K256);
108
109	&sub	("esp",16);
110	&and	("esp",-64);
111
112	&shl	("eax",6);
113	&add	("eax","edi");
114	&mov	(&DWP(0,"esp"),"esi");	# ctx
115	&mov	(&DWP(4,"esp"),"edi");	# inp
116	&mov	(&DWP(8,"esp"),"eax");	# inp+num*128
117	&mov	(&DWP(12,"esp"),"ebx");	# saved sp
118
119&set_label("loop",16);
120    # copy input block to stack reversing byte and dword order
121    for($i=0;$i<4;$i++) {
122	&mov	("eax",&DWP($i*16+0,"edi"));
123	&mov	("ebx",&DWP($i*16+4,"edi"));
124	&mov	("ecx",&DWP($i*16+8,"edi"));
125	&mov	("edx",&DWP($i*16+12,"edi"));
126	&bswap	("eax");
127	&bswap	("ebx");
128	&bswap	("ecx");
129	&bswap	("edx");
130	&push	("eax");
131	&push	("ebx");
132	&push	("ecx");
133	&push	("edx");
134    }
135	&add	("edi",64);
136	&sub	("esp",4*8);		# place for A,B,C,D,E,F,G,H
137	&mov	(&DWP(4*(8+16)+4,"esp"),"edi");
138
139	# copy ctx->h[0-7] to A,B,C,D,E,F,G,H on stack
140	&mov	($A,&DWP(0,"esi"));
141	&mov	("ebx",&DWP(4,"esi"));
142	&mov	("ecx",&DWP(8,"esi"));
143	&mov	("edi",&DWP(12,"esi"));
144	# &mov	($Aoff,$A);
145	&mov	($Boff,"ebx");
146	&mov	($Coff,"ecx");
147	&mov	($Doff,"edi");
148	&mov	($E,&DWP(16,"esi"));
149	&mov	("ebx",&DWP(20,"esi"));
150	&mov	("ecx",&DWP(24,"esi"));
151	&mov	("edi",&DWP(28,"esi"));
152	# &mov	($Eoff,$E);
153	&mov	($Foff,"ebx");
154	&mov	($Goff,"ecx");
155	&mov	($Hoff,"edi");
156
157&set_label("00_15",16);
158	&mov	($T,&DWP(4*(8+15),"esp"));
159
160	&BODY_00_15();
161
162	&cmp	("esi",0xc19bf174);
163	&jne	(&label("00_15"));
164
165	&mov	($T,&DWP(4*(8+15+16-1),"esp"));	# preloaded in BODY_00_15(1)
166&set_label("16_63",16);
167	&mov	("esi",$T);
168	 &mov	("ecx",&DWP(4*(8+15+16-14),"esp"));
169	&ror	("esi",18-7);
170	 &mov	("edi","ecx");
171	&xor	("esi",$T);
172	&ror	("esi",7);
173	&shr	($T,3);
174
175	&ror	("edi",19-17);
176	 &xor	($T,"esi");			# T = sigma0(X[-15])
177	&xor	("edi","ecx");
178	&ror	("edi",17);
179	&shr	("ecx",10);
180	 &add	($T,&DWP(4*(8+15+16),"esp"));	# T += X[-16]
181	&xor	("edi","ecx");			# sigma1(X[-2])
182
183	 &add	($T,&DWP(4*(8+15+16-9),"esp"));	# T += X[-7]
184	# &add	($T,"edi");			# T += sigma1(X[-2])
185	# &mov	(&DWP(4*(8+15),"esp"),$T);	# save X[0]
186
187	&BODY_00_15(1);
188
189	&cmp	("esi",0xc67178f2);
190	&jne	(&label("16_63"));
191
192	&mov	("esi",&DWP(4*(8+16+64)+0,"esp"));#ctx
193	# &mov	($A,$Aoff);
194	&mov	("ebx",$Boff);
195	&mov	("ecx",$Coff);
196	&mov	("edi",$Doff);
197	&add	($A,&DWP(0,"esi"));
198	&add	("ebx",&DWP(4,"esi"));
199	&add	("ecx",&DWP(8,"esi"));
200	&add	("edi",&DWP(12,"esi"));
201	&mov	(&DWP(0,"esi"),$A);
202	&mov	(&DWP(4,"esi"),"ebx");
203	&mov	(&DWP(8,"esi"),"ecx");
204	&mov	(&DWP(12,"esi"),"edi");
205	# &mov	($E,$Eoff);
206	&mov	("eax",$Foff);
207	&mov	("ebx",$Goff);
208	&mov	("ecx",$Hoff);
209	&mov	("edi",&DWP(4*(8+16+64)+4,"esp"));#inp
210	&add	($E,&DWP(16,"esi"));
211	&add	("eax",&DWP(20,"esi"));
212	&add	("ebx",&DWP(24,"esi"));
213	&add	("ecx",&DWP(28,"esi"));
214	&mov	(&DWP(16,"esi"),$E);
215	&mov	(&DWP(20,"esi"),"eax");
216	&mov	(&DWP(24,"esi"),"ebx");
217	&mov	(&DWP(28,"esi"),"ecx");
218
219	&add	("esp",4*(8+16+64));		# destroy frame
220	&sub	($K256,4*64);			# rewind K
221
222	&cmp	("edi",&DWP(8,"esp"));		# are we done yet?
223	&jb	(&label("loop"));
224
225	&mov	("esp",&DWP(12,"esp"));		# restore sp
226&function_end_A();
227&function_end_B("sha256_block_data_order");
228
229	&rodataseg();
230&set_label("K256",64);
231	&data_word(0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5);
232	&data_word(0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5);
233	&data_word(0xd807aa98,0x12835b01,0x243185be,0x550c7dc3);
234	&data_word(0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174);
235	&data_word(0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc);
236	&data_word(0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da);
237	&data_word(0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7);
238	&data_word(0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967);
239	&data_word(0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13);
240	&data_word(0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85);
241	&data_word(0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3);
242	&data_word(0xd192e819,0xd6990624,0xf40e3585,0x106aa070);
243	&data_word(0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5);
244	&data_word(0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3);
245	&data_word(0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208);
246	&data_word(0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2);
247	&previous();
248
249&asm_finish();
250