xref: /onnv-gate/usr/src/common/openssl/crypto/des/asm/crypt586.pl (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!/usr/local/bin/perl
2*0Sstevel@tonic-gate#
3*0Sstevel@tonic-gate# The inner loop instruction sequence and the IP/FP modifications are from
4*0Sstevel@tonic-gate# Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>
5*0Sstevel@tonic-gate# I've added the stuff needed for crypt() but I've not worried about making
6*0Sstevel@tonic-gate# things perfect.
7*0Sstevel@tonic-gate#
8*0Sstevel@tonic-gate
9*0Sstevel@tonic-gatepush(@INC,"perlasm","../../perlasm");
10*0Sstevel@tonic-gaterequire "x86asm.pl";
11*0Sstevel@tonic-gate
12*0Sstevel@tonic-gate&asm_init($ARGV[0],"crypt586.pl");
13*0Sstevel@tonic-gate
14*0Sstevel@tonic-gate$L="edi";
15*0Sstevel@tonic-gate$R="esi";
16*0Sstevel@tonic-gate
17*0Sstevel@tonic-gate&external_label("DES_SPtrans");
18*0Sstevel@tonic-gate&fcrypt_body("fcrypt_body");
19*0Sstevel@tonic-gate&asm_finish();
20*0Sstevel@tonic-gate
21*0Sstevel@tonic-gatesub fcrypt_body
22*0Sstevel@tonic-gate	{
23*0Sstevel@tonic-gate	local($name,$do_ip)=@_;
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate	&function_begin($name,"EXTRN   _DES_SPtrans:DWORD");
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate	&comment("");
28*0Sstevel@tonic-gate	&comment("Load the 2 words");
29*0Sstevel@tonic-gate	$trans="ebp";
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate	&xor(	$L,	$L);
32*0Sstevel@tonic-gate	&xor(	$R,	$R);
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate	# PIC-ification:-)
35*0Sstevel@tonic-gate	&picmeup("edx","DES_SPtrans");
36*0Sstevel@tonic-gate	#if ($cpp)	{ &picmeup("edx","DES_SPtrans");   }
37*0Sstevel@tonic-gate	#else		{ &lea("edx",&DWP("DES_SPtrans")); }
38*0Sstevel@tonic-gate	&push("edx");	# becomes &swtmp(1)
39*0Sstevel@tonic-gate	#
40*0Sstevel@tonic-gate	&mov($trans,&wparam(1)); # reloaded with DES_SPtrans in D_ENCRYPT
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate	&push(&DWC(25)); # add a variable
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate	&set_label("start");
45*0Sstevel@tonic-gate	for ($i=0; $i<16; $i+=2)
46*0Sstevel@tonic-gate		{
47*0Sstevel@tonic-gate		&comment("");
48*0Sstevel@tonic-gate		&comment("Round $i");
49*0Sstevel@tonic-gate		&D_ENCRYPT($i,$L,$R,$i*2,$trans,"eax","ebx","ecx","edx");
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate		&comment("");
52*0Sstevel@tonic-gate		&comment("Round ".sprintf("%d",$i+1));
53*0Sstevel@tonic-gate		&D_ENCRYPT($i+1,$R,$L,($i+1)*2,$trans,"eax","ebx","ecx","edx");
54*0Sstevel@tonic-gate		}
55*0Sstevel@tonic-gate	 &mov("ebx",	&swtmp(0));
56*0Sstevel@tonic-gate	&mov("eax",	$L);
57*0Sstevel@tonic-gate	 &dec("ebx");
58*0Sstevel@tonic-gate	&mov($L,	$R);
59*0Sstevel@tonic-gate	 &mov($R,	"eax");
60*0Sstevel@tonic-gate	&mov(&swtmp(0),	"ebx");
61*0Sstevel@tonic-gate	 &jnz(&label("start"));
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate	&comment("");
64*0Sstevel@tonic-gate	&comment("FP");
65*0Sstevel@tonic-gate	&mov("edx",&wparam(0));
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate	&FP_new($R,$L,"eax",3);
68*0Sstevel@tonic-gate	&mov(&DWP(0,"edx","",0),"eax");
69*0Sstevel@tonic-gate	&mov(&DWP(4,"edx","",0),$L);
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate	&add("esp",8);	# remove variables
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate	&function_end($name);
74*0Sstevel@tonic-gate	}
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gatesub D_ENCRYPT
77*0Sstevel@tonic-gate	{
78*0Sstevel@tonic-gate	local($r,$L,$R,$S,$trans,$u,$tmp1,$tmp2,$t)=@_;
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate	&mov(	$u,		&wparam(2));			# 2
81*0Sstevel@tonic-gate	&mov(	$t,		$R);
82*0Sstevel@tonic-gate	&shr(	$t,		16);				# 1
83*0Sstevel@tonic-gate	&mov(	$tmp2,		&wparam(3));			# 2
84*0Sstevel@tonic-gate	&xor(	$t,		$R);				# 1
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate	&and(	$u,		$t);				# 2
87*0Sstevel@tonic-gate	&and(	$t,		$tmp2);				# 2
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate	&mov(	$tmp1,		$u);
90*0Sstevel@tonic-gate	&shl(	$tmp1,		16); 				# 1
91*0Sstevel@tonic-gate	&mov(	$tmp2,		$t);
92*0Sstevel@tonic-gate	&shl(	$tmp2,		16); 				# 1
93*0Sstevel@tonic-gate	&xor(	$u,		$tmp1);				# 2
94*0Sstevel@tonic-gate	&xor(	$t,		$tmp2);				# 2
95*0Sstevel@tonic-gate	&mov(	$tmp1,		&DWP(&n2a($S*4),$trans,"",0));	# 2
96*0Sstevel@tonic-gate	&xor(	$u,		$tmp1);
97*0Sstevel@tonic-gate	&mov(	$tmp2,		&DWP(&n2a(($S+1)*4),$trans,"",0));	# 2
98*0Sstevel@tonic-gate	&xor(	$u,		$R);
99*0Sstevel@tonic-gate	&xor(	$t,		$R);
100*0Sstevel@tonic-gate	&xor(	$t,		$tmp2);
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate	&and(	$u,		"0xfcfcfcfc"	);		# 2
103*0Sstevel@tonic-gate	&xor(	$tmp1,		$tmp1);				# 1
104*0Sstevel@tonic-gate	&and(	$t,		"0xcfcfcfcf"	);		# 2
105*0Sstevel@tonic-gate	&xor(	$tmp2,		$tmp2);
106*0Sstevel@tonic-gate	&movb(	&LB($tmp1),	&LB($u)	);
107*0Sstevel@tonic-gate	&movb(	&LB($tmp2),	&HB($u)	);
108*0Sstevel@tonic-gate	&rotr(	$t,		4		);
109*0Sstevel@tonic-gate	&mov(	$trans,		&swtmp(1));
110*0Sstevel@tonic-gate	&xor(	$L,		&DWP("     ",$trans,$tmp1,0));
111*0Sstevel@tonic-gate	&movb(	&LB($tmp1),	&LB($t)	);
112*0Sstevel@tonic-gate	&xor(	$L,		&DWP("0x200",$trans,$tmp2,0));
113*0Sstevel@tonic-gate	&movb(	&LB($tmp2),	&HB($t)	);
114*0Sstevel@tonic-gate	&shr(	$u,		16);
115*0Sstevel@tonic-gate	&xor(	$L,		&DWP("0x100",$trans,$tmp1,0));
116*0Sstevel@tonic-gate	&movb(	&LB($tmp1),	&HB($u)	);
117*0Sstevel@tonic-gate	&shr(	$t,		16);
118*0Sstevel@tonic-gate	&xor(	$L,		&DWP("0x300",$trans,$tmp2,0));
119*0Sstevel@tonic-gate	&movb(	&LB($tmp2),	&HB($t)	);
120*0Sstevel@tonic-gate	&and(	$u,		"0xff"	);
121*0Sstevel@tonic-gate	&and(	$t,		"0xff"	);
122*0Sstevel@tonic-gate	&mov(	$tmp1,		&DWP("0x600",$trans,$tmp1,0));
123*0Sstevel@tonic-gate	&xor(	$L,		$tmp1);
124*0Sstevel@tonic-gate	&mov(	$tmp1,		&DWP("0x700",$trans,$tmp2,0));
125*0Sstevel@tonic-gate	&xor(	$L,		$tmp1);
126*0Sstevel@tonic-gate	&mov(	$tmp1,		&DWP("0x400",$trans,$u,0));
127*0Sstevel@tonic-gate	&xor(	$L,		$tmp1);
128*0Sstevel@tonic-gate	&mov(	$tmp1,		&DWP("0x500",$trans,$t,0));
129*0Sstevel@tonic-gate	&xor(	$L,		$tmp1);
130*0Sstevel@tonic-gate	&mov(	$trans,		&wparam(1));
131*0Sstevel@tonic-gate	}
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gatesub n2a
134*0Sstevel@tonic-gate	{
135*0Sstevel@tonic-gate	sprintf("%d",$_[0]);
136*0Sstevel@tonic-gate	}
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate# now has a side affect of rotating $a by $shift
139*0Sstevel@tonic-gatesub R_PERM_OP
140*0Sstevel@tonic-gate	{
141*0Sstevel@tonic-gate	local($a,$b,$tt,$shift,$mask,$last)=@_;
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate	&rotl(	$a,		$shift		) if ($shift != 0);
144*0Sstevel@tonic-gate	&mov(	$tt,		$a		);
145*0Sstevel@tonic-gate	&xor(	$a,		$b		);
146*0Sstevel@tonic-gate	&and(	$a,		$mask		);
147*0Sstevel@tonic-gate	if ($notlast eq $b)
148*0Sstevel@tonic-gate		{
149*0Sstevel@tonic-gate		&xor(	$b,		$a		);
150*0Sstevel@tonic-gate		&xor(	$tt,		$a		);
151*0Sstevel@tonic-gate		}
152*0Sstevel@tonic-gate	else
153*0Sstevel@tonic-gate		{
154*0Sstevel@tonic-gate		&xor(	$tt,		$a		);
155*0Sstevel@tonic-gate		&xor(	$b,		$a		);
156*0Sstevel@tonic-gate		}
157*0Sstevel@tonic-gate	&comment("");
158*0Sstevel@tonic-gate	}
159*0Sstevel@tonic-gate
160*0Sstevel@tonic-gatesub IP_new
161*0Sstevel@tonic-gate	{
162*0Sstevel@tonic-gate	local($l,$r,$tt,$lr)=@_;
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gate	&R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);
165*0Sstevel@tonic-gate	&R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l);
166*0Sstevel@tonic-gate	&R_PERM_OP($l,$tt,$r,14,"0x33333333",$r);
167*0Sstevel@tonic-gate	&R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r);
168*0Sstevel@tonic-gate	&R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate	if ($lr != 3)
171*0Sstevel@tonic-gate		{
172*0Sstevel@tonic-gate		if (($lr-3) < 0)
173*0Sstevel@tonic-gate			{ &rotr($tt,	3-$lr); }
174*0Sstevel@tonic-gate		else	{ &rotl($tt,	$lr-3); }
175*0Sstevel@tonic-gate		}
176*0Sstevel@tonic-gate	if ($lr != 2)
177*0Sstevel@tonic-gate		{
178*0Sstevel@tonic-gate		if (($lr-2) < 0)
179*0Sstevel@tonic-gate			{ &rotr($r,	2-$lr); }
180*0Sstevel@tonic-gate		else	{ &rotl($r,	$lr-2); }
181*0Sstevel@tonic-gate		}
182*0Sstevel@tonic-gate	}
183*0Sstevel@tonic-gate
184*0Sstevel@tonic-gatesub FP_new
185*0Sstevel@tonic-gate	{
186*0Sstevel@tonic-gate	local($l,$r,$tt,$lr)=@_;
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate	if ($lr != 2)
189*0Sstevel@tonic-gate		{
190*0Sstevel@tonic-gate		if (($lr-2) < 0)
191*0Sstevel@tonic-gate			{ &rotl($r,	2-$lr); }
192*0Sstevel@tonic-gate		else	{ &rotr($r,	$lr-2); }
193*0Sstevel@tonic-gate		}
194*0Sstevel@tonic-gate	if ($lr != 3)
195*0Sstevel@tonic-gate		{
196*0Sstevel@tonic-gate		if (($lr-3) < 0)
197*0Sstevel@tonic-gate			{ &rotl($l,	3-$lr); }
198*0Sstevel@tonic-gate		else	{ &rotr($l,	$lr-3); }
199*0Sstevel@tonic-gate		}
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate	&R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);
202*0Sstevel@tonic-gate	&R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r);
203*0Sstevel@tonic-gate	&R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);
204*0Sstevel@tonic-gate	&R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l);
205*0Sstevel@tonic-gate	&R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r);
206*0Sstevel@tonic-gate	&rotr($tt	, 4);
207*0Sstevel@tonic-gate	}
208*0Sstevel@tonic-gate
209