xref: /openssl/crypto/des/asm/des-586.pl (revision 33388b44)
1#! /usr/bin/env perl
2# Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3#
4# Licensed under the Apache License 2.0 (the "License").  You may not use
5# this file except in compliance with the License.  You can obtain a copy
6# in the file LICENSE in the source distribution or at
7# https://www.openssl.org/source/license.html
8
9# The inner loop instruction sequence and the IP/FP modifications are from
10# Svend Olaf Mikkelsen.
11
12$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
13push(@INC,"${dir}","${dir}../../perlasm");
14require "x86asm.pl";
15require "cbc.pl";
16require "desboth.pl";
17
18# base code is in Microsoft
19# op dest, source
20# format.
21#
22
23$output=pop and open STDOUT,">$output";
24
25&asm_init($ARGV[0]);
26
27$L="edi";
28$R="esi";
29$trans="ebp";
30$small_footprint=1 if (grep(/\-DOPENSSL_SMALL_FOOTPRINT/,@ARGV));
31# one can discuss setting this variable to 1 unconditionally, as
32# the folded loop is only 3% slower than unrolled, but >7 times smaller
33
34&public_label("DES_SPtrans");
35&static_label("des_sptrans");
36
37&DES_encrypt_internal();
38&DES_decrypt_internal();
39&DES_encrypt("DES_encrypt1",1);
40&DES_encrypt("DES_encrypt2",0);
41&DES_encrypt3("DES_encrypt3",1);
42&DES_encrypt3("DES_decrypt3",0);
43&cbc("DES_ncbc_encrypt","DES_encrypt1","DES_encrypt1",0,4,5,3,5,-1);
44&cbc("DES_ede3_cbc_encrypt","DES_encrypt3","DES_decrypt3",0,6,7,3,4,5);
45&DES_SPtrans();
46
47&asm_finish();
48
49close STDOUT or die "error closing STDOUT: $!";
50
51sub DES_encrypt_internal()
52	{
53	&function_begin_B("_x86_DES_encrypt");
54
55	if ($small_footprint)
56	    {
57	    &lea("edx",&DWP(128,"ecx"));
58	    &push("edx");
59	    &push("ecx");
60	    &set_label("eloop");
61		&D_ENCRYPT(0,$L,$R,0,$trans,"eax","ebx","ecx","edx",&swtmp(0));
62		&comment("");
63		&D_ENCRYPT(1,$R,$L,2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
64		&comment("");
65		&add("ecx",16);
66		&cmp("ecx",&swtmp(1));
67		&mov(&swtmp(0),"ecx");
68		&jb(&label("eloop"));
69	    &add("esp",8);
70	    }
71	else
72	    {
73	    &push("ecx");
74	    for ($i=0; $i<16; $i+=2)
75		{
76		&comment("Round $i");
77		&D_ENCRYPT($i,$L,$R,$i*2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
78		&comment("Round ".sprintf("%d",$i+1));
79		&D_ENCRYPT($i+1,$R,$L,($i+1)*2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
80		}
81	    &add("esp",4);
82	}
83	&ret();
84
85	&function_end_B("_x86_DES_encrypt");
86	}
87
88sub DES_decrypt_internal()
89	{
90	&function_begin_B("_x86_DES_decrypt");
91
92	if ($small_footprint)
93	    {
94	    &push("ecx");
95	    &lea("ecx",&DWP(128,"ecx"));
96	    &push("ecx");
97	    &set_label("dloop");
98		&D_ENCRYPT(0,$L,$R,-2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
99		&comment("");
100		&D_ENCRYPT(1,$R,$L,-4,$trans,"eax","ebx","ecx","edx",&swtmp(0));
101		&comment("");
102		&sub("ecx",16);
103		&cmp("ecx",&swtmp(1));
104		&mov(&swtmp(0),"ecx");
105		&ja(&label("dloop"));
106	    &add("esp",8);
107	    }
108	else
109	    {
110	    &push("ecx");
111	    for ($i=15; $i>0; $i-=2)
112		{
113		&comment("Round $i");
114		&D_ENCRYPT(15-$i,$L,$R,$i*2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
115		&comment("Round ".sprintf("%d",$i-1));
116		&D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
117		}
118	    &add("esp",4);
119	    }
120	&ret();
121
122	&function_end_B("_x86_DES_decrypt");
123	}
124
125sub DES_encrypt
126	{
127	local($name,$do_ip)=@_;
128
129	&function_begin_B($name);
130
131	&push("esi");
132	&push("edi");
133
134	&comment("");
135	&comment("Load the 2 words");
136
137	if ($do_ip)
138		{
139		&mov($R,&wparam(0));
140		 &xor(	"ecx",		"ecx"		);
141
142		&push("ebx");
143		&push("ebp");
144
145		&mov("eax",&DWP(0,$R,"",0));
146		 &mov("ebx",&wparam(2));	# get encrypt flag
147		&mov($L,&DWP(4,$R,"",0));
148		&comment("");
149		&comment("IP");
150		&IP_new("eax",$L,$R,3);
151		}
152	else
153		{
154		&mov("eax",&wparam(0));
155		 &xor(	"ecx",		"ecx"		);
156
157		&push("ebx");
158		&push("ebp");
159
160		&mov($R,&DWP(0,"eax","",0));
161		 &mov("ebx",&wparam(2));	# get encrypt flag
162		&rotl($R,3);
163		&mov($L,&DWP(4,"eax","",0));
164		&rotl($L,3);
165		}
166
167	# PIC-ification:-)
168	&call	(&label("pic_point"));
169	&set_label("pic_point");
170	&blindpop($trans);
171	&lea	($trans,&DWP(&label("des_sptrans")."-".&label("pic_point"),$trans));
172
173	&mov(	"ecx",	&wparam(1)	);
174
175	&cmp("ebx","0");
176	&je(&label("decrypt"));
177	&call("_x86_DES_encrypt");
178	&jmp(&label("done"));
179	&set_label("decrypt");
180	&call("_x86_DES_decrypt");
181	&set_label("done");
182
183	if ($do_ip)
184		{
185		&comment("");
186		&comment("FP");
187		&mov("edx",&wparam(0));
188		&FP_new($L,$R,"eax",3);
189
190		&mov(&DWP(0,"edx","",0),"eax");
191		&mov(&DWP(4,"edx","",0),$R);
192		}
193	else
194		{
195		&comment("");
196		&comment("Fixup");
197		&rotr($L,3);		# r
198		 &mov("eax",&wparam(0));
199		&rotr($R,3);		# l
200		 &mov(&DWP(0,"eax","",0),$L);
201		 &mov(&DWP(4,"eax","",0),$R);
202		}
203
204	&pop("ebp");
205	&pop("ebx");
206	&pop("edi");
207	&pop("esi");
208	&ret();
209
210	&function_end_B($name);
211	}
212
213sub D_ENCRYPT
214	{
215	local($r,$L,$R,$S,$trans,$u,$tmp1,$tmp2,$t,$wp1)=@_;
216
217	 &mov(	$u,		&DWP(&n2a($S*4),$tmp2,"",0));
218	&xor(	$tmp1,		$tmp1);
219	 &mov(	$t,		&DWP(&n2a(($S+1)*4),$tmp2,"",0));
220	&xor(	$u,		$R);
221	&xor(	$tmp2,		$tmp2);
222	 &xor(	$t,		$R);
223	&and(	$u,		"0xfcfcfcfc"	);
224	 &and(	$t,		"0xcfcfcfcf"	);
225	&movb(	&LB($tmp1),	&LB($u)	);
226	 &movb(	&LB($tmp2),	&HB($u)	);
227	&rotr(	$t,		4		);
228	&xor(	$L,		&DWP("     ",$trans,$tmp1,0));
229	 &movb(	&LB($tmp1),	&LB($t)	);
230	 &xor(	$L,		&DWP("0x200",$trans,$tmp2,0));
231	 &movb(	&LB($tmp2),	&HB($t)	);
232	&shr(	$u,		16);
233	 &xor(	$L,		&DWP("0x100",$trans,$tmp1,0));
234	 &movb(	&LB($tmp1),	&HB($u)	);
235	&shr(	$t,		16);
236	 &xor(	$L,		&DWP("0x300",$trans,$tmp2,0));
237	&movb(	&LB($tmp2),	&HB($t)	);
238	 &and(	$u,		"0xff"	);
239	&and(	$t,		"0xff"	);
240	 &xor(	$L,		&DWP("0x600",$trans,$tmp1,0));
241	 &xor(	$L,		&DWP("0x700",$trans,$tmp2,0));
242	&mov(	$tmp2,		$wp1	);
243	 &xor(	$L,		&DWP("0x400",$trans,$u,0));
244	 &xor(	$L,		&DWP("0x500",$trans,$t,0));
245	}
246
247sub n2a
248	{
249	sprintf("%d",$_[0]);
250	}
251
252# now has a side affect of rotating $a by $shift
253sub R_PERM_OP
254	{
255	local($a,$b,$tt,$shift,$mask,$last)=@_;
256
257	&rotl(	$a,		$shift		) if ($shift != 0);
258	&mov(	$tt,		$a		);
259	&xor(	$a,		$b		);
260	&and(	$a,		$mask		);
261	# This can never succeed, and besides it is difficult to see what the
262	# idea was - Ben 13 Feb 99
263	if (!$last eq $b)
264		{
265		&xor(	$b,		$a		);
266		&xor(	$tt,		$a		);
267		}
268	else
269		{
270		&xor(	$tt,		$a		);
271		&xor(	$b,		$a		);
272		}
273	&comment("");
274	}
275
276sub IP_new
277	{
278	local($l,$r,$tt,$lr)=@_;
279
280	&R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);
281	&R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l);
282	&R_PERM_OP($l,$tt,$r,14,"0x33333333",$r);
283	&R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r);
284	&R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);
285
286	if ($lr != 3)
287		{
288		if (($lr-3) < 0)
289			{ &rotr($tt,	3-$lr); }
290		else	{ &rotl($tt,	$lr-3); }
291		}
292	if ($lr != 2)
293		{
294		if (($lr-2) < 0)
295			{ &rotr($r,	2-$lr); }
296		else	{ &rotl($r,	$lr-2); }
297		}
298	}
299
300sub FP_new
301	{
302	local($l,$r,$tt,$lr)=@_;
303
304	if ($lr != 2)
305		{
306		if (($lr-2) < 0)
307			{ &rotl($r,	2-$lr); }
308		else	{ &rotr($r,	$lr-2); }
309		}
310	if ($lr != 3)
311		{
312		if (($lr-3) < 0)
313			{ &rotl($l,	3-$lr); }
314		else	{ &rotr($l,	$lr-3); }
315		}
316
317	&R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);
318	&R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r);
319	&R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);
320	&R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l);
321	&R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r);
322	&rotr($tt	, 4);
323	}
324
325sub DES_SPtrans
326	{
327	&set_label("DES_SPtrans",64);
328	&set_label("des_sptrans");
329	&data_word(0x02080800, 0x00080000, 0x02000002, 0x02080802);
330	&data_word(0x02000000, 0x00080802, 0x00080002, 0x02000002);
331	&data_word(0x00080802, 0x02080800, 0x02080000, 0x00000802);
332	&data_word(0x02000802, 0x02000000, 0x00000000, 0x00080002);
333	&data_word(0x00080000, 0x00000002, 0x02000800, 0x00080800);
334	&data_word(0x02080802, 0x02080000, 0x00000802, 0x02000800);
335	&data_word(0x00000002, 0x00000800, 0x00080800, 0x02080002);
336	&data_word(0x00000800, 0x02000802, 0x02080002, 0x00000000);
337	&data_word(0x00000000, 0x02080802, 0x02000800, 0x00080002);
338	&data_word(0x02080800, 0x00080000, 0x00000802, 0x02000800);
339	&data_word(0x02080002, 0x00000800, 0x00080800, 0x02000002);
340	&data_word(0x00080802, 0x00000002, 0x02000002, 0x02080000);
341	&data_word(0x02080802, 0x00080800, 0x02080000, 0x02000802);
342	&data_word(0x02000000, 0x00000802, 0x00080002, 0x00000000);
343	&data_word(0x00080000, 0x02000000, 0x02000802, 0x02080800);
344	&data_word(0x00000002, 0x02080002, 0x00000800, 0x00080802);
345	# nibble 1
346	&data_word(0x40108010, 0x00000000, 0x00108000, 0x40100000);
347	&data_word(0x40000010, 0x00008010, 0x40008000, 0x00108000);
348	&data_word(0x00008000, 0x40100010, 0x00000010, 0x40008000);
349	&data_word(0x00100010, 0x40108000, 0x40100000, 0x00000010);
350	&data_word(0x00100000, 0x40008010, 0x40100010, 0x00008000);
351	&data_word(0x00108010, 0x40000000, 0x00000000, 0x00100010);
352	&data_word(0x40008010, 0x00108010, 0x40108000, 0x40000010);
353	&data_word(0x40000000, 0x00100000, 0x00008010, 0x40108010);
354	&data_word(0x00100010, 0x40108000, 0x40008000, 0x00108010);
355	&data_word(0x40108010, 0x00100010, 0x40000010, 0x00000000);
356	&data_word(0x40000000, 0x00008010, 0x00100000, 0x40100010);
357	&data_word(0x00008000, 0x40000000, 0x00108010, 0x40008010);
358	&data_word(0x40108000, 0x00008000, 0x00000000, 0x40000010);
359	&data_word(0x00000010, 0x40108010, 0x00108000, 0x40100000);
360	&data_word(0x40100010, 0x00100000, 0x00008010, 0x40008000);
361	&data_word(0x40008010, 0x00000010, 0x40100000, 0x00108000);
362	# nibble 2
363	&data_word(0x04000001, 0x04040100, 0x00000100, 0x04000101);
364	&data_word(0x00040001, 0x04000000, 0x04000101, 0x00040100);
365	&data_word(0x04000100, 0x00040000, 0x04040000, 0x00000001);
366	&data_word(0x04040101, 0x00000101, 0x00000001, 0x04040001);
367	&data_word(0x00000000, 0x00040001, 0x04040100, 0x00000100);
368	&data_word(0x00000101, 0x04040101, 0x00040000, 0x04000001);
369	&data_word(0x04040001, 0x04000100, 0x00040101, 0x04040000);
370	&data_word(0x00040100, 0x00000000, 0x04000000, 0x00040101);
371	&data_word(0x04040100, 0x00000100, 0x00000001, 0x00040000);
372	&data_word(0x00000101, 0x00040001, 0x04040000, 0x04000101);
373	&data_word(0x00000000, 0x04040100, 0x00040100, 0x04040001);
374	&data_word(0x00040001, 0x04000000, 0x04040101, 0x00000001);
375	&data_word(0x00040101, 0x04000001, 0x04000000, 0x04040101);
376	&data_word(0x00040000, 0x04000100, 0x04000101, 0x00040100);
377	&data_word(0x04000100, 0x00000000, 0x04040001, 0x00000101);
378	&data_word(0x04000001, 0x00040101, 0x00000100, 0x04040000);
379	# nibble 3
380	&data_word(0x00401008, 0x10001000, 0x00000008, 0x10401008);
381	&data_word(0x00000000, 0x10400000, 0x10001008, 0x00400008);
382	&data_word(0x10401000, 0x10000008, 0x10000000, 0x00001008);
383	&data_word(0x10000008, 0x00401008, 0x00400000, 0x10000000);
384	&data_word(0x10400008, 0x00401000, 0x00001000, 0x00000008);
385	&data_word(0x00401000, 0x10001008, 0x10400000, 0x00001000);
386	&data_word(0x00001008, 0x00000000, 0x00400008, 0x10401000);
387	&data_word(0x10001000, 0x10400008, 0x10401008, 0x00400000);
388	&data_word(0x10400008, 0x00001008, 0x00400000, 0x10000008);
389	&data_word(0x00401000, 0x10001000, 0x00000008, 0x10400000);
390	&data_word(0x10001008, 0x00000000, 0x00001000, 0x00400008);
391	&data_word(0x00000000, 0x10400008, 0x10401000, 0x00001000);
392	&data_word(0x10000000, 0x10401008, 0x00401008, 0x00400000);
393	&data_word(0x10401008, 0x00000008, 0x10001000, 0x00401008);
394	&data_word(0x00400008, 0x00401000, 0x10400000, 0x10001008);
395	&data_word(0x00001008, 0x10000000, 0x10000008, 0x10401000);
396	# nibble 4
397	&data_word(0x08000000, 0x00010000, 0x00000400, 0x08010420);
398	&data_word(0x08010020, 0x08000400, 0x00010420, 0x08010000);
399	&data_word(0x00010000, 0x00000020, 0x08000020, 0x00010400);
400	&data_word(0x08000420, 0x08010020, 0x08010400, 0x00000000);
401	&data_word(0x00010400, 0x08000000, 0x00010020, 0x00000420);
402	&data_word(0x08000400, 0x00010420, 0x00000000, 0x08000020);
403	&data_word(0x00000020, 0x08000420, 0x08010420, 0x00010020);
404	&data_word(0x08010000, 0x00000400, 0x00000420, 0x08010400);
405	&data_word(0x08010400, 0x08000420, 0x00010020, 0x08010000);
406	&data_word(0x00010000, 0x00000020, 0x08000020, 0x08000400);
407	&data_word(0x08000000, 0x00010400, 0x08010420, 0x00000000);
408	&data_word(0x00010420, 0x08000000, 0x00000400, 0x00010020);
409	&data_word(0x08000420, 0x00000400, 0x00000000, 0x08010420);
410	&data_word(0x08010020, 0x08010400, 0x00000420, 0x00010000);
411	&data_word(0x00010400, 0x08010020, 0x08000400, 0x00000420);
412	&data_word(0x00000020, 0x00010420, 0x08010000, 0x08000020);
413	# nibble 5
414	&data_word(0x80000040, 0x00200040, 0x00000000, 0x80202000);
415	&data_word(0x00200040, 0x00002000, 0x80002040, 0x00200000);
416	&data_word(0x00002040, 0x80202040, 0x00202000, 0x80000000);
417	&data_word(0x80002000, 0x80000040, 0x80200000, 0x00202040);
418	&data_word(0x00200000, 0x80002040, 0x80200040, 0x00000000);
419	&data_word(0x00002000, 0x00000040, 0x80202000, 0x80200040);
420	&data_word(0x80202040, 0x80200000, 0x80000000, 0x00002040);
421	&data_word(0x00000040, 0x00202000, 0x00202040, 0x80002000);
422	&data_word(0x00002040, 0x80000000, 0x80002000, 0x00202040);
423	&data_word(0x80202000, 0x00200040, 0x00000000, 0x80002000);
424	&data_word(0x80000000, 0x00002000, 0x80200040, 0x00200000);
425	&data_word(0x00200040, 0x80202040, 0x00202000, 0x00000040);
426	&data_word(0x80202040, 0x00202000, 0x00200000, 0x80002040);
427	&data_word(0x80000040, 0x80200000, 0x00202040, 0x00000000);
428	&data_word(0x00002000, 0x80000040, 0x80002040, 0x80202000);
429	&data_word(0x80200000, 0x00002040, 0x00000040, 0x80200040);
430	# nibble 6
431	&data_word(0x00004000, 0x00000200, 0x01000200, 0x01000004);
432	&data_word(0x01004204, 0x00004004, 0x00004200, 0x00000000);
433	&data_word(0x01000000, 0x01000204, 0x00000204, 0x01004000);
434	&data_word(0x00000004, 0x01004200, 0x01004000, 0x00000204);
435	&data_word(0x01000204, 0x00004000, 0x00004004, 0x01004204);
436	&data_word(0x00000000, 0x01000200, 0x01000004, 0x00004200);
437	&data_word(0x01004004, 0x00004204, 0x01004200, 0x00000004);
438	&data_word(0x00004204, 0x01004004, 0x00000200, 0x01000000);
439	&data_word(0x00004204, 0x01004000, 0x01004004, 0x00000204);
440	&data_word(0x00004000, 0x00000200, 0x01000000, 0x01004004);
441	&data_word(0x01000204, 0x00004204, 0x00004200, 0x00000000);
442	&data_word(0x00000200, 0x01000004, 0x00000004, 0x01000200);
443	&data_word(0x00000000, 0x01000204, 0x01000200, 0x00004200);
444	&data_word(0x00000204, 0x00004000, 0x01004204, 0x01000000);
445	&data_word(0x01004200, 0x00000004, 0x00004004, 0x01004204);
446	&data_word(0x01000004, 0x01004200, 0x01004000, 0x00004004);
447	# nibble 7
448	&data_word(0x20800080, 0x20820000, 0x00020080, 0x00000000);
449	&data_word(0x20020000, 0x00800080, 0x20800000, 0x20820080);
450	&data_word(0x00000080, 0x20000000, 0x00820000, 0x00020080);
451	&data_word(0x00820080, 0x20020080, 0x20000080, 0x20800000);
452	&data_word(0x00020000, 0x00820080, 0x00800080, 0x20020000);
453	&data_word(0x20820080, 0x20000080, 0x00000000, 0x00820000);
454	&data_word(0x20000000, 0x00800000, 0x20020080, 0x20800080);
455	&data_word(0x00800000, 0x00020000, 0x20820000, 0x00000080);
456	&data_word(0x00800000, 0x00020000, 0x20000080, 0x20820080);
457	&data_word(0x00020080, 0x20000000, 0x00000000, 0x00820000);
458	&data_word(0x20800080, 0x20020080, 0x20020000, 0x00800080);
459	&data_word(0x20820000, 0x00000080, 0x00800080, 0x20020000);
460	&data_word(0x20820080, 0x00800000, 0x20800000, 0x20000080);
461	&data_word(0x00820000, 0x00020080, 0x20020080, 0x20800000);
462	&data_word(0x00000080, 0x20820000, 0x00820080, 0x00000000);
463	&data_word(0x20000000, 0x20800080, 0x00020000, 0x00820080);
464	}
465