]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/des/asm/des-som2.pl
Import of old SSLeay release: SSLeay 0.8.1b
[thirdparty/openssl.git] / crypto / des / asm / des-som2.pl
1 #!/usr/local/bin/perl
2 #
3 # The inner loop instruction sequence and the IP/FP modifications are from
4 # Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>
5 #
6
7 $prog="des-som2.pl";
8
9 # base code is in microsft
10 # op dest, source
11 # format.
12 #
13
14 require "desboth.pl";
15
16 if ( ($ARGV[0] eq "elf"))
17 { require "x86unix.pl"; }
18 elsif ( ($ARGV[0] eq "a.out"))
19 { $aout=1; require "x86unix.pl"; }
20 elsif ( ($ARGV[0] eq "sol"))
21 { $sol=1; require "x86unix.pl"; }
22 elsif ( ($ARGV[0] eq "cpp"))
23 { $cpp=1; require "x86unix.pl"; }
24 elsif ( ($ARGV[0] eq "win32"))
25 { require "x86ms.pl"; }
26 else
27 {
28 print STDERR <<"EOF";
29 Pick one target type from
30 elf - linux, FreeBSD etc
31 a.out - old linux
32 sol - x86 solaris
33 cpp - format so x86unix.cpp can be used
34 win32 - Windows 95/Windows NT
35 EOF
36 exit(1);
37 }
38
39 &comment("Don't even think of reading this code");
40 &comment("It was automatically generated by $prog");
41 &comment("Which is a perl program used to generate the x86 assember for");
42 &comment("any of elf, a.out, Win32, or Solaris");
43 &comment("It can be found in SSLeay 0.6.5+ or in libdes 3.26+");
44 &comment("eric <eay\@cryptsoft.com>");
45 &comment("The inner loop instruction sequence and the IP/FP modifications");
46 &comment("are from Svend Olaf Mikkelsen <svolaf\@inet.uni-c.dk>");
47
48 &comment("");
49
50 &file("dx86xxxx");
51
52 $L="edi";
53 $R="esi";
54
55 &des_encrypt("des_encrypt",1);
56 &des_encrypt("des_encrypt2",0);
57
58 &des_encrypt3("des_encrypt3",1);
59 &des_encrypt3("des_decrypt3",0);
60
61 &file_end();
62
63 sub des_encrypt
64 {
65 local($name,$do_ip)=@_;
66
67 &function_begin($name,3,"EXTRN _des_SPtrans:DWORD");
68
69 &comment("");
70 &comment("Load the 2 words");
71 &mov("eax",&wparam(0));
72 &mov($R,&DWP(0,"eax","",0));
73 &mov($L,&DWP(4,"eax","",0));
74
75 if ($do_ip)
76 {
77 &comment("");
78 &comment("IP");
79 &IP_new($R,$L,"eax",3);
80 # &comment("");
81 # &comment("fixup rotate");
82 # &rotl($R,3);
83 # &rotl($L,3);
84 }
85 else
86 {
87 &comment("");
88 &comment("fixup rotate");
89 &rotl($R,3);
90 &rotl($L,3);
91 }
92
93 &comment("");
94 &comment("load counter, key_schedule and enc flag");
95
96 # encrypting part
97
98 $ks="ebp";
99 # &xor( "ebx", "ebx" );
100 &mov("eax",&wparam(2)); # get encrypt flag
101 &xor( "ecx", "ecx" );
102 &cmp("eax","0");
103 &mov( $ks, &wparam(1) );
104 &je(&label("start_decrypt"));
105
106 for ($i=0; $i<16; $i+=2)
107 {
108 &comment("");
109 &comment("Round $i");
110 &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
111
112 &comment("");
113 &comment("Round ".sprintf("%d",$i+1));
114 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
115 }
116 &jmp(&label("end"));
117
118 &set_label("start_decrypt");
119
120 for ($i=15; $i>0; $i-=2)
121 {
122 &comment("");
123 &comment("Round $i");
124 &D_ENCRYPT(15-$i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
125 &comment("");
126 &comment("Round ".sprintf("%d",$i-1));
127 &D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
128 }
129
130 &set_label("end");
131
132 if ($do_ip)
133 {
134 # &comment("");
135 # &comment("Fixup");
136 # &rotr($L,3); # r
137 # &rotr($R,3); # l
138 &comment("");
139 &comment("FP");
140 &FP_new($R,$L,"eax",3);
141 }
142 else
143 {
144 &comment("");
145 &comment("Fixup");
146 &rotr($L,3); # r
147 &rotr($R,3); # l
148 }
149
150 &mov("eax",&wparam(0));
151 &mov(&DWP(0,"eax","",0),$L);
152 &mov(&DWP(4,"eax","",0),$R);
153
154 &function_end($name);
155 }
156
157 sub D_ENCRYPT
158 {
159 local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_;
160
161 &mov( $u, &DWP(&n2a($S*4),$ks,"",0));
162 &xor( $tmp1, $tmp1);
163 &mov( $t, &DWP(&n2a(($S+1)*4),$ks,"",0));
164 &xor( $u, $R);
165 &xor( $t, $R);
166 &and( $u, "0xfcfcfcfc" );
167 &and( $t, "0xcfcfcfcf" );
168 &movb( &LB($tmp1), &LB($u) );
169 &movb( &LB($tmp2), &HB($u) );
170 &rotr( $t, 4 );
171 &mov( $ks, &DWP(" $desSP",$tmp1,"",0));
172 &movb( &LB($tmp1), &LB($t) );
173 &xor( $L, $ks);
174 &mov( $ks, &DWP("0x200+$desSP",$tmp2,"",0));
175 &xor( $L, $ks); ######
176 &movb( &LB($tmp2), &HB($t) );
177 &shr( $u, 16);
178 &mov( $ks, &DWP("0x100+$desSP",$tmp1,"",0));
179 &xor( $L, $ks); ######
180 &movb( &LB($tmp1), &HB($u) );
181 &shr( $t, 16);
182 &mov( $ks, &DWP("0x300+$desSP",$tmp2,"",0));
183 &xor( $L, $ks);
184 &mov( $ks, &DWP(24,"esp","",0)); ####
185 &movb( &LB($tmp2), &HB($t) );
186 &and( $u, "0xff" );
187 &and( $t, "0xff" );
188 &mov( $tmp1, &DWP("0x600+$desSP",$tmp1,"",0));
189 &xor( $L, $tmp1);
190 &mov( $tmp1, &DWP("0x700+$desSP",$tmp2,"",0));
191 &xor( $L, $tmp1);
192 &mov( $tmp1, &DWP("0x400+$desSP",$u,"",0));
193 &xor( $L, $tmp1);
194 &mov( $tmp1, &DWP("0x500+$desSP",$t,"",0));
195 &xor( $L, $tmp1);
196 }
197
198 sub PERM_OP
199 {
200 local($a,$b,$tt,$shift,$mask)=@_;
201
202 &mov( $tt, $a );
203 &shr( $tt, $shift );
204 &xor( $tt, $b );
205 &and( $tt, $mask );
206 &xor( $b, $tt );
207 &shl( $tt, $shift );
208 &xor( $a, $tt );
209 }
210
211 sub IP
212 {
213 local($l,$r,$tt)=@_;
214
215 &PERM_OP($r,$l,$tt, 4,"0x0f0f0f0f");
216 &PERM_OP($l,$r,$tt,16,"0x0000ffff");
217 &PERM_OP($r,$l,$tt, 2,"0x33333333");
218 &PERM_OP($l,$r,$tt, 8,"0x00ff00ff");
219 &PERM_OP($r,$l,$tt, 1,"0x55555555");
220 }
221
222 sub FP
223 {
224 local($l,$r,$tt)=@_;
225
226 &PERM_OP($l,$r,$tt, 1,"0x55555555");
227 &PERM_OP($r,$l,$tt, 8,"0x00ff00ff");
228 &PERM_OP($l,$r,$tt, 2,"0x33333333");
229 &PERM_OP($r,$l,$tt,16,"0x0000ffff");
230 &PERM_OP($l,$r,$tt, 4,"0x0f0f0f0f");
231 }
232
233 sub n2a
234 {
235 sprintf("%d",$_[0]);
236 }
237
238 # now has a side affect of rotating $a by $shift
239 sub R_PERM_OP
240 {
241 local($a,$b,$tt,$shift,$mask,$last)=@_;
242
243 &rotl( $a, $shift ) if ($shift != 0);
244 &mov( $tt, $b );
245 &xor( $tt, $a );
246 &and( $tt, $mask );
247 if ($last eq $b)
248 {
249 &xor( $a, $tt );
250 &xor( $b, $tt );
251 }
252 else
253 {
254 &xor( $b, $tt );
255 &xor( $a, $tt );
256 }
257 &comment("");
258 }
259
260 sub IP_new
261 {
262 local($l,$r,$tt,$lr)=@_;
263
264 &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);
265 &R_PERM_OP($r,$l,$tt,20,"0xfff0000f",$l);
266 &R_PERM_OP($r,$l,$tt,14,"0x33333333",$r);
267 &R_PERM_OP($l,$r,$tt,22,"0x03fc03fc",$r);
268 &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);
269
270 if ($lr != 3)
271 {
272 if (($lr-3) < 0)
273 { &rotr($l, 3-$lr); }
274 else { &rotl($l, $lr-3); }
275 }
276 if ($lr != 2)
277 {
278 if (($lr-2) < 0)
279 { &rotr($r, 2-$lr); }
280 else { &rotl($r, $lr-2); }
281 }
282 }
283
284 sub FP_new
285 {
286 local($r,$l,$tt,$lr)=@_;
287
288 if ($lr != 2)
289 {
290 if (($lr-2) < 0)
291 { &rotl($r, 2-$lr); }
292 else { &rotr($r, $lr-2); }
293 }
294 if ($lr != 3)
295 {
296 if (($lr-3) < 0)
297 { &rotl($l, 3-$lr); }
298 else { &rotr($l, $lr-3); }
299 }
300
301 &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);
302 &R_PERM_OP($l,$r,$tt,23,"0x03fc03fc",$r);
303 &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);
304 &R_PERM_OP($r,$l,$tt,18,"0xfff0000f",$l);
305 &R_PERM_OP($r,$l,$tt,12,"0xf0f0f0f0",$r);
306 &rotr($l , 4);
307 }
308