3 # The inner loop instruction sequence and the IP/FP modifications are from
4 # Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>
9 # base code is in microsft
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"; }
29 Pick one target type from
30 elf - linux, FreeBSD etc
33 cpp - format so x86unix.cpp can be used
34 win32 - Windows 95/Windows NT
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\@mincom.oz.au>");
45 &comment("The inner loop instruction sequence and the IP/FP modifications");
46 &comment("are from Svend Olaf Mikkelsen <svolaf\@inet.uni-c.dk>");
55 &des_encrypt("des_encrypt",1);
56 &des_encrypt("des_encrypt2",0);
58 &des_encrypt3("des_encrypt3",1);
59 &des_encrypt3("des_decrypt3",0);
65 local($name,$do_ip)=@_;
67 &function_begin($name,3);
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));
79 &IP_new($R,$L,"eax",3);
81 # &comment("fixup rotate");
88 &comment("fixup rotate");
94 &comment("load counter, key_schedule and enc flag");
99 # &xor( "ebx", "ebx" );
100 &mov("eax",&wparam(2)); # get encrypt flag
101 &xor( "ecx", "ecx" );
103 &mov( $ks, &wparam(1) );
104 &je(&label("start_decrypt"));
106 for ($i=0; $i<16; $i+=2)
109 &comment("Round $i");
110 &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
113 &comment("Round ".sprintf("%d",$i+1));
114 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
118 &set_label("start_decrypt");
120 for ($i=15; $i>0; $i-=2)
123 &comment("Round $i");
124 &D_ENCRYPT(15-$i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
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");
140 &FP_new($R,$L,"eax",3);
150 &mov("eax",&wparam(0));
151 &mov(&DWP(0,"eax","",0),$L);
152 &mov(&DWP(4,"eax","",0),$R);
154 &function_end($name);
159 local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_;
161 &mov( $u, &DWP(&n2a($S*4),$ks,"",0));
163 &mov( $t, &DWP(&n2a(($S+1)*4),$ks,"",0));
166 &and( $u, "0xfcfcfcfc" );
167 &and( $t, "0xcfcfcfcf" );
168 &movb( &LB($tmp1), &LB($u) );
169 &movb( &LB($tmp2), &HB($u) );
171 &mov( $ks, &DWP(" $desSP",$tmp1,"",0));
172 &movb( &LB($tmp1), &LB($t) );
174 &mov( $ks, &DWP("0x200+$desSP",$tmp2,"",0));
175 &xor( $L, $ks); ######
176 &movb( &LB($tmp2), &HB($t) );
178 &mov( $ks, &DWP("0x100+$desSP",$tmp1,"",0));
179 &xor( $L, $ks); ######
180 &movb( &LB($tmp1), &HB($u) );
182 &mov( $ks, &DWP("0x300+$desSP",$tmp2,"",0));
184 &mov( $ks, &DWP(24,"esp","",0)); ####
185 &movb( &LB($tmp2), &HB($t) );
188 &mov( $tmp1, &DWP("0x600+$desSP",$tmp1,"",0));
190 &mov( $tmp1, &DWP("0x700+$desSP",$tmp2,"",0));
192 &mov( $tmp1, &DWP("0x400+$desSP",$u,"",0));
194 &mov( $tmp1, &DWP("0x500+$desSP",$t,"",0));
200 local($a,$b,$tt,$shift,$mask)=@_;
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");
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");
238 # now has a side affect of rotating $a by $shift
241 local($a,$b,$tt,$shift,$mask,$last)=@_;
243 &rotl( $a, $shift ) if ($shift != 0);
262 local($l,$r,$tt,$lr)=@_;
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);
273 { &rotr($l, 3-$lr); }
274 else { &rotl($l, $lr-3); }
279 { &rotr($r, 2-$lr); }
280 else { &rotl($r, $lr-2); }
286 local($r,$l,$tt,$lr)=@_;
291 { &rotl($r, 2-$lr); }
292 else { &rotr($r, $lr-2); }
297 { &rotl($l, 3-$lr); }
298 else { &rotr($l, $lr-3); }
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);