3 # The inner loop instruction sequence and the IP/FP modifications are from
\r
4 # Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>
\r
9 # base code is in microsft
\r
14 require "desboth.pl";
\r
16 if ( ($ARGV[0] eq "elf"))
\r
17 { require "x86unix.pl"; }
\r
18 elsif ( ($ARGV[0] eq "a.out"))
\r
19 { $aout=1; require "x86unix.pl"; }
\r
20 elsif ( ($ARGV[0] eq "sol"))
\r
21 { $sol=1; require "x86unix.pl"; }
\r
22 elsif ( ($ARGV[0] eq "cpp"))
\r
23 { $cpp=1; require "x86unix.pl"; }
\r
24 elsif ( ($ARGV[0] eq "win32"))
\r
25 { require "x86ms.pl"; }
\r
28 print STDERR <<"EOF";
\r
29 Pick one target type from
\r
30 elf - linux, FreeBSD etc
\r
33 cpp - format so x86unix.cpp can be used
\r
34 win32 - Windows 95/Windows NT
\r
39 &comment("Don't even think of reading this code");
\r
40 &comment("It was automatically generated by $prog");
\r
41 &comment("Which is a perl program used to generate the x86 assember for");
\r
42 &comment("any of elf, a.out, Win32, or Solaris");
\r
43 &comment("It can be found in SSLeay 0.6.5+ or in libdes 3.26+");
\r
44 &comment("eric <eay\@cryptsoft.com>");
\r
45 &comment("The inner loop instruction sequence and the IP/FP modifications");
\r
46 &comment("are from Svend Olaf Mikkelsen <svolaf\@inet.uni-c.dk>");
\r
55 &des_encrypt("des_encrypt",1);
\r
56 &des_encrypt("des_encrypt2",0);
\r
58 &des_encrypt3("des_encrypt3",1);
\r
59 &des_encrypt3("des_decrypt3",0);
\r
65 local($name,$do_ip)=@_;
\r
67 &function_begin($name,3,"EXTRN _des_SPtrans:DWORD");
\r
70 &comment("Load the 2 words");
\r
71 &mov("eax",&wparam(0));
\r
72 &mov($R,&DWP(0,"eax","",0));
\r
73 &mov($L,&DWP(4,"eax","",0));
\r
79 &IP_new($R,$L,"eax",3);
\r
81 # &comment("fixup rotate");
\r
88 &comment("fixup rotate");
\r
94 &comment("load counter, key_schedule and enc flag");
\r
99 # &xor( "ebx", "ebx" );
\r
100 &mov("eax",&wparam(2)); # get encrypt flag
\r
101 &xor( "ecx", "ecx" );
\r
103 &mov( $ks, &wparam(1) );
\r
104 &je(&label("start_decrypt"));
\r
106 for ($i=0; $i<16; $i+=2)
\r
109 &comment("Round $i");
\r
110 &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
\r
113 &comment("Round ".sprintf("%d",$i+1));
\r
114 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
\r
116 &jmp(&label("end"));
\r
118 &set_label("start_decrypt");
\r
120 for ($i=15; $i>0; $i-=2)
\r
123 &comment("Round $i");
\r
124 &D_ENCRYPT(15-$i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
\r
126 &comment("Round ".sprintf("%d",$i-1));
\r
127 &D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
\r
135 # &comment("Fixup");
\r
140 &FP_new($R,$L,"eax",3);
\r
150 &mov("eax",&wparam(0));
\r
151 &mov(&DWP(0,"eax","",0),$L);
\r
152 &mov(&DWP(4,"eax","",0),$R);
\r
154 &function_end($name);
\r
159 local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_;
\r
161 &mov( $u, &DWP(&n2a($S*4),$ks,"",0));
\r
162 &xor( $tmp1, $tmp1);
\r
163 &mov( $t, &DWP(&n2a(($S+1)*4),$ks,"",0));
\r
166 &and( $u, "0xfcfcfcfc" );
\r
167 &and( $t, "0xcfcfcfcf" );
\r
168 &movb( &LB($tmp1), &LB($u) );
\r
169 &movb( &LB($tmp2), &HB($u) );
\r
171 &mov( $ks, &DWP(" $desSP",$tmp1,"",0));
\r
172 &movb( &LB($tmp1), &LB($t) );
\r
174 &mov( $ks, &DWP("0x200+$desSP",$tmp2,"",0));
\r
175 &xor( $L, $ks); ######
\r
176 &movb( &LB($tmp2), &HB($t) );
\r
178 &mov( $ks, &DWP("0x100+$desSP",$tmp1,"",0));
\r
179 &xor( $L, $ks); ######
\r
180 &movb( &LB($tmp1), &HB($u) );
\r
182 &mov( $ks, &DWP("0x300+$desSP",$tmp2,"",0));
\r
184 &mov( $ks, &DWP(24,"esp","",0)); ####
\r
185 &movb( &LB($tmp2), &HB($t) );
\r
186 &and( $u, "0xff" );
\r
187 &and( $t, "0xff" );
\r
188 &mov( $tmp1, &DWP("0x600+$desSP",$tmp1,"",0));
\r
190 &mov( $tmp1, &DWP("0x700+$desSP",$tmp2,"",0));
\r
192 &mov( $tmp1, &DWP("0x400+$desSP",$u,"",0));
\r
194 &mov( $tmp1, &DWP("0x500+$desSP",$t,"",0));
\r
200 local($a,$b,$tt,$shift,$mask)=@_;
\r
203 &shr( $tt, $shift );
\r
205 &and( $tt, $mask );
\r
207 &shl( $tt, $shift );
\r
213 local($l,$r,$tt)=@_;
\r
215 &PERM_OP($r,$l,$tt, 4,"0x0f0f0f0f");
\r
216 &PERM_OP($l,$r,$tt,16,"0x0000ffff");
\r
217 &PERM_OP($r,$l,$tt, 2,"0x33333333");
\r
218 &PERM_OP($l,$r,$tt, 8,"0x00ff00ff");
\r
219 &PERM_OP($r,$l,$tt, 1,"0x55555555");
\r
224 local($l,$r,$tt)=@_;
\r
226 &PERM_OP($l,$r,$tt, 1,"0x55555555");
\r
227 &PERM_OP($r,$l,$tt, 8,"0x00ff00ff");
\r
228 &PERM_OP($l,$r,$tt, 2,"0x33333333");
\r
229 &PERM_OP($r,$l,$tt,16,"0x0000ffff");
\r
230 &PERM_OP($l,$r,$tt, 4,"0x0f0f0f0f");
\r
235 sprintf("%d",$_[0]);
\r
238 # now has a side affect of rotating $a by $shift
\r
241 local($a,$b,$tt,$shift,$mask,$last)=@_;
\r
243 &rotl( $a, $shift ) if ($shift != 0);
\r
246 &and( $tt, $mask );
\r
262 local($l,$r,$tt,$lr)=@_;
\r
264 &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);
\r
265 &R_PERM_OP($r,$l,$tt,20,"0xfff0000f",$l);
\r
266 &R_PERM_OP($r,$l,$tt,14,"0x33333333",$r);
\r
267 &R_PERM_OP($l,$r,$tt,22,"0x03fc03fc",$r);
\r
268 &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);
\r
273 { &rotr($l, 3-$lr); }
\r
274 else { &rotl($l, $lr-3); }
\r
279 { &rotr($r, 2-$lr); }
\r
280 else { &rotl($r, $lr-2); }
\r
286 local($r,$l,$tt,$lr)=@_;
\r
291 { &rotl($r, 2-$lr); }
\r
292 else { &rotr($r, $lr-2); }
\r
297 { &rotl($l, 3-$lr); }
\r
298 else { &rotr($l, $lr-3); }
\r
301 &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);
\r
302 &R_PERM_OP($l,$r,$tt,23,"0x03fc03fc",$r);
\r
303 &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);
\r
304 &R_PERM_OP($r,$l,$tt,18,"0xfff0000f",$l);
\r
305 &R_PERM_OP($r,$l,$tt,12,"0xf0f0f0f0",$r);
\r