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