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