xref: /onnv-gate/usr/src/common/openssl/crypto/des/asm/des686.pl (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!/usr/local/bin/perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gate$prog="des686.pl";
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gate# base code is in microsft
6*0Sstevel@tonic-gate# op dest, source
7*0Sstevel@tonic-gate# format.
8*0Sstevel@tonic-gate#
9*0Sstevel@tonic-gate
10*0Sstevel@tonic-gate# WILL NOT WORK ANYMORE WITH desboth.pl
11*0Sstevel@tonic-gaterequire "desboth.pl";
12*0Sstevel@tonic-gate
13*0Sstevel@tonic-gateif (	($ARGV[0] eq "elf"))
14*0Sstevel@tonic-gate	{ require "x86unix.pl"; }
15*0Sstevel@tonic-gateelsif (	($ARGV[0] eq "a.out"))
16*0Sstevel@tonic-gate	{ $aout=1; require "x86unix.pl"; }
17*0Sstevel@tonic-gateelsif (	($ARGV[0] eq "sol"))
18*0Sstevel@tonic-gate	{ $sol=1; require "x86unix.pl"; }
19*0Sstevel@tonic-gateelsif ( ($ARGV[0] eq "cpp"))
20*0Sstevel@tonic-gate	{ $cpp=1; require "x86unix.pl"; }
21*0Sstevel@tonic-gateelsif (	($ARGV[0] eq "win32"))
22*0Sstevel@tonic-gate	{ require "x86ms.pl"; }
23*0Sstevel@tonic-gateelse
24*0Sstevel@tonic-gate	{
25*0Sstevel@tonic-gate	print STDERR <<"EOF";
26*0Sstevel@tonic-gatePick one target type from
27*0Sstevel@tonic-gate	elf	- linux, FreeBSD etc
28*0Sstevel@tonic-gate	a.out	- old linux
29*0Sstevel@tonic-gate	sol	- x86 solaris
30*0Sstevel@tonic-gate	cpp	- format so x86unix.cpp can be used
31*0Sstevel@tonic-gate	win32	- Windows 95/Windows NT
32*0Sstevel@tonic-gateEOF
33*0Sstevel@tonic-gate	exit(1);
34*0Sstevel@tonic-gate	}
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate&comment("Don't even think of reading this code");
37*0Sstevel@tonic-gate&comment("It was automatically generated by $prog");
38*0Sstevel@tonic-gate&comment("Which is a perl program used to generate the x86 assember for");
39*0Sstevel@tonic-gate&comment("any of elf, a.out, Win32, or Solaris");
40*0Sstevel@tonic-gate&comment("It can be found in SSLeay 0.6.5+ or in libdes 3.26+");
41*0Sstevel@tonic-gate&comment("eric <eay\@cryptsoft.com>");
42*0Sstevel@tonic-gate&comment("");
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate&file("dx86xxxx");
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate$L="edi";
47*0Sstevel@tonic-gate$R="esi";
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate&DES_encrypt("DES_encrypt1",1);
50*0Sstevel@tonic-gate&DES_encrypt("DES_encrypt2",0);
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate&DES_encrypt3("DES_encrypt3",1);
53*0Sstevel@tonic-gate&DES_encrypt3("DES_decrypt3",0);
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate&file_end();
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gatesub DES_encrypt
58*0Sstevel@tonic-gate	{
59*0Sstevel@tonic-gate	local($name,$do_ip)=@_;
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate	&function_begin($name,"EXTRN   _DES_SPtrans:DWORD");
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate	&comment("");
64*0Sstevel@tonic-gate	&comment("Load the 2 words");
65*0Sstevel@tonic-gate	&mov("eax",&wparam(0));
66*0Sstevel@tonic-gate	&mov($L,&DWP(0,"eax","",0));
67*0Sstevel@tonic-gate	&mov($R,&DWP(4,"eax","",0));
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate	$ksp=&wparam(1);
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate	if ($do_ip)
72*0Sstevel@tonic-gate		{
73*0Sstevel@tonic-gate		&comment("");
74*0Sstevel@tonic-gate		&comment("IP");
75*0Sstevel@tonic-gate		&IP_new($L,$R,"eax");
76*0Sstevel@tonic-gate		}
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate	&comment("");
79*0Sstevel@tonic-gate	&comment("fixup rotate");
80*0Sstevel@tonic-gate	&rotl($R,3);
81*0Sstevel@tonic-gate	&rotl($L,3);
82*0Sstevel@tonic-gate	&exch($L,$R);
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gate	&comment("");
85*0Sstevel@tonic-gate	&comment("load counter, key_schedule and enc flag");
86*0Sstevel@tonic-gate	&mov("eax",&wparam(2));	# get encrypt flag
87*0Sstevel@tonic-gate	&mov("ebp",&wparam(1));	# get ks
88*0Sstevel@tonic-gate	&cmp("eax","0");
89*0Sstevel@tonic-gate	&je(&label("start_decrypt"));
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate	# encrypting part
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate	for ($i=0; $i<16; $i+=2)
94*0Sstevel@tonic-gate		{
95*0Sstevel@tonic-gate		&comment("");
96*0Sstevel@tonic-gate		&comment("Round $i");
97*0Sstevel@tonic-gate		&D_ENCRYPT($L,$R,$i*2,"ebp","DES_SPtrans","ecx","edx","eax","ebx");
98*0Sstevel@tonic-gate
99*0Sstevel@tonic-gate		&comment("");
100*0Sstevel@tonic-gate		&comment("Round ".sprintf("%d",$i+1));
101*0Sstevel@tonic-gate		&D_ENCRYPT($R,$L,($i+1)*2,"ebp","DES_SPtrans","ecx","edx","eax","ebx");
102*0Sstevel@tonic-gate		}
103*0Sstevel@tonic-gate	&jmp(&label("end"));
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate	&set_label("start_decrypt");
106*0Sstevel@tonic-gate
107*0Sstevel@tonic-gate	for ($i=15; $i>0; $i-=2)
108*0Sstevel@tonic-gate		{
109*0Sstevel@tonic-gate		&comment("");
110*0Sstevel@tonic-gate		&comment("Round $i");
111*0Sstevel@tonic-gate		&D_ENCRYPT($L,$R,$i*2,"ebp","DES_SPtrans","ecx","edx","eax","ebx");
112*0Sstevel@tonic-gate		&comment("");
113*0Sstevel@tonic-gate		&comment("Round ".sprintf("%d",$i-1));
114*0Sstevel@tonic-gate		&D_ENCRYPT($R,$L,($i-1)*2,"ebp","DES_SPtrans","ecx","edx","eax","ebx");
115*0Sstevel@tonic-gate		}
116*0Sstevel@tonic-gate
117*0Sstevel@tonic-gate	&set_label("end");
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate	&comment("");
120*0Sstevel@tonic-gate	&comment("Fixup");
121*0Sstevel@tonic-gate	&rotr($L,3);		# r
122*0Sstevel@tonic-gate	&rotr($R,3);		# l
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate	if ($do_ip)
125*0Sstevel@tonic-gate		{
126*0Sstevel@tonic-gate		&comment("");
127*0Sstevel@tonic-gate		&comment("FP");
128*0Sstevel@tonic-gate		&FP_new($R,$L,"eax");
129*0Sstevel@tonic-gate		}
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate	&mov("eax",&wparam(0));
132*0Sstevel@tonic-gate	&mov(&DWP(0,"eax","",0),$L);
133*0Sstevel@tonic-gate	&mov(&DWP(4,"eax","",0),$R);
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate	&function_end($name);
136*0Sstevel@tonic-gate	}
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate# The logic is to load R into 2 registers and operate on both at the same time.
140*0Sstevel@tonic-gate# We also load the 2 R's into 2 more registers so we can do the 'move word down a byte'
141*0Sstevel@tonic-gate# while also masking the other copy and doing a lookup.  We then also accumulate the
142*0Sstevel@tonic-gate# L value in 2 registers then combine them at the end.
143*0Sstevel@tonic-gatesub D_ENCRYPT
144*0Sstevel@tonic-gate	{
145*0Sstevel@tonic-gate	local($L,$R,$S,$ks,$desSP,$u,$t,$tmp1,$tmp2,$tmp3)=@_;
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate	&mov(	$u,		&DWP(&n2a($S*4),$ks,"",0));
148*0Sstevel@tonic-gate	&mov(	$t,		&DWP(&n2a(($S+1)*4),$ks,"",0));
149*0Sstevel@tonic-gate	&xor(	$u,		$R		);
150*0Sstevel@tonic-gate	&xor(	$t,		$R		);
151*0Sstevel@tonic-gate	&rotr(	$t,		4		);
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate	# the numbers at the end of the line are origional instruction order
154*0Sstevel@tonic-gate	&mov(	$tmp2,		$u		);			# 1 2
155*0Sstevel@tonic-gate	&mov(	$tmp1,		$t		);			# 1 1
156*0Sstevel@tonic-gate	&and(	$tmp2,		"0xfc"		);			# 1 4
157*0Sstevel@tonic-gate	&and(	$tmp1,		"0xfc"		);			# 1 3
158*0Sstevel@tonic-gate	&shr(	$t,		8		);			# 1 5
159*0Sstevel@tonic-gate	&xor(	$L,		&DWP("0x100+$desSP",$tmp1,"",0));	# 1 7
160*0Sstevel@tonic-gate	&shr(	$u,		8		);			# 1 6
161*0Sstevel@tonic-gate	&mov(	$tmp1,		&DWP("      $desSP",$tmp2,"",0));	# 1 8
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gate	&mov(	$tmp2,		$u		);			# 2 2
164*0Sstevel@tonic-gate	&xor(	$L,		$tmp1		);			# 1 9
165*0Sstevel@tonic-gate	&and(	$tmp2,		"0xfc"		);			# 2 4
166*0Sstevel@tonic-gate	&mov(	$tmp1,		$t		);			# 2 1
167*0Sstevel@tonic-gate	&and(	$tmp1,		"0xfc"		);			# 2 3
168*0Sstevel@tonic-gate	&shr(	$t,		8		);			# 2 5
169*0Sstevel@tonic-gate	&xor(	$L,		&DWP("0x300+$desSP",$tmp1,"",0));	# 2 7
170*0Sstevel@tonic-gate	&shr(	$u,		8		);			# 2 6
171*0Sstevel@tonic-gate	&mov(	$tmp1,		&DWP("0x200+$desSP",$tmp2,"",0));	# 2 8
172*0Sstevel@tonic-gate	&mov(	$tmp2,		$u		);			# 3 2
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate	&xor(	$L,		$tmp1		);			# 2 9
175*0Sstevel@tonic-gate	&and(	$tmp2,		"0xfc"		);			# 3 4
176*0Sstevel@tonic-gate
177*0Sstevel@tonic-gate	&mov(	$tmp1,		$t		);			# 3 1
178*0Sstevel@tonic-gate	&shr(	$u,		8		);			# 3 6
179*0Sstevel@tonic-gate	&and(	$tmp1,		"0xfc"		);			# 3 3
180*0Sstevel@tonic-gate	&shr(	$t,		8		);			# 3 5
181*0Sstevel@tonic-gate	&xor(	$L,		&DWP("0x500+$desSP",$tmp1,"",0));	# 3 7
182*0Sstevel@tonic-gate	&mov(	$tmp1,		&DWP("0x400+$desSP",$tmp2,"",0));	# 3 8
183*0Sstevel@tonic-gate
184*0Sstevel@tonic-gate	&and(	$t,		"0xfc"		);			# 4 1
185*0Sstevel@tonic-gate	&xor(	$L,		$tmp1		);			# 3 9
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gate	&and(	$u,		"0xfc"		);			# 4 2
188*0Sstevel@tonic-gate	&xor(	$L,		&DWP("0x700+$desSP",$t,"",0));		# 4 3
189*0Sstevel@tonic-gate	&xor(	$L,		&DWP("0x600+$desSP",$u,"",0));		# 4 4
190*0Sstevel@tonic-gate	}
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gatesub PERM_OP
193*0Sstevel@tonic-gate	{
194*0Sstevel@tonic-gate	local($a,$b,$tt,$shift,$mask)=@_;
195*0Sstevel@tonic-gate
196*0Sstevel@tonic-gate	&mov(	$tt,		$a		);
197*0Sstevel@tonic-gate	&shr(	$tt,		$shift		);
198*0Sstevel@tonic-gate	&xor(	$tt,		$b		);
199*0Sstevel@tonic-gate	&and(	$tt,		$mask		);
200*0Sstevel@tonic-gate	&xor(	$b,		$tt		);
201*0Sstevel@tonic-gate	&shl(	$tt,		$shift		);
202*0Sstevel@tonic-gate	&xor(	$a,		$tt		);
203*0Sstevel@tonic-gate	}
204*0Sstevel@tonic-gate
205*0Sstevel@tonic-gatesub IP_new
206*0Sstevel@tonic-gate	{
207*0Sstevel@tonic-gate	local($l,$r,$tt)=@_;
208*0Sstevel@tonic-gate
209*0Sstevel@tonic-gate	&PERM_OP($r,$l,$tt, 4,"0x0f0f0f0f");
210*0Sstevel@tonic-gate	&PERM_OP($l,$r,$tt,16,"0x0000ffff");
211*0Sstevel@tonic-gate	&PERM_OP($r,$l,$tt, 2,"0x33333333");
212*0Sstevel@tonic-gate	&PERM_OP($l,$r,$tt, 8,"0x00ff00ff");
213*0Sstevel@tonic-gate	&PERM_OP($r,$l,$tt, 1,"0x55555555");
214*0Sstevel@tonic-gate	}
215*0Sstevel@tonic-gate
216*0Sstevel@tonic-gatesub FP_new
217*0Sstevel@tonic-gate	{
218*0Sstevel@tonic-gate	local($l,$r,$tt)=@_;
219*0Sstevel@tonic-gate
220*0Sstevel@tonic-gate	&PERM_OP($l,$r,$tt, 1,"0x55555555");
221*0Sstevel@tonic-gate        &PERM_OP($r,$l,$tt, 8,"0x00ff00ff");
222*0Sstevel@tonic-gate        &PERM_OP($l,$r,$tt, 2,"0x33333333");
223*0Sstevel@tonic-gate        &PERM_OP($r,$l,$tt,16,"0x0000ffff");
224*0Sstevel@tonic-gate        &PERM_OP($l,$r,$tt, 4,"0x0f0f0f0f");
225*0Sstevel@tonic-gate	}
226*0Sstevel@tonic-gate
227*0Sstevel@tonic-gatesub n2a
228*0Sstevel@tonic-gate	{
229*0Sstevel@tonic-gate	sprintf("%d",$_[0]);
230*0Sstevel@tonic-gate	}
231