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
75 &mov($R,&wparam(0));
\r
76 &xor( "ecx", "ecx" );
\r
77 &mov("eax",&DWP(0,$R,"",0));
\r
78 &mov("ebx",&wparam(2)); # get encrypt flag
\r
79 &mov($L,&DWP(4,$R,"",0));
\r
82 &IP_new("eax",$L,$R,3);
\r
86 &mov("eax",&wparam(0));
\r
87 &xor( "ecx", "ecx" );
\r
88 &mov($R,&DWP(0,"eax","",0));
\r
89 &mov("ebx",&wparam(2)); # get encrypt flag
\r
91 &mov($L,&DWP(4,"eax","",0));
\r
96 &mov( $ks, &wparam(1) );
\r
97 &je(&label("start_decrypt"));
\r
99 for ($i=0; $i<16; $i+=2)
\r
102 &comment("Round $i");
\r
103 &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
\r
106 &comment("Round ".sprintf("%d",$i+1));
\r
107 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
\r
109 &jmp(&label("end"));
\r
111 &set_label("start_decrypt");
\r
113 for ($i=15; $i>0; $i-=2)
\r
116 &comment("Round $i");
\r
117 &D_ENCRYPT(15-$i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
\r
119 &comment("Round ".sprintf("%d",$i-1));
\r
120 &D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
\r
129 &mov("edx",&wparam(0));
\r
130 &FP_new($L,$R,"eax",3);
\r
132 &mov(&DWP(0,"edx","",0),"eax");
\r
133 &mov(&DWP(4,"edx","",0),$R);
\r
140 &mov("eax",&wparam(0));
\r
142 &mov(&DWP(0,"eax","",0),$L);
\r
143 &mov(&DWP(4,"eax","",0),$R);
\r
147 &function_end($name);
\r
152 local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_;
\r
154 &mov( $u, &DWP(&n2a($S*4),$ks,"",0));
\r
155 &xor( $tmp1, $tmp1);
\r
156 &mov( $t, &DWP(&n2a(($S+1)*4),$ks,"",0));
\r
159 &and( $u, "0xfcfcfcfc" );
\r
160 &and( $t, "0xcfcfcfcf" );
\r
161 &movb( &LB($tmp1), &LB($u) );
\r
162 &movb( &LB($tmp2), &HB($u) );
\r
164 &mov( $ks, &DWP(" $desSP",$tmp1,"",0));
\r
165 &movb( &LB($tmp1), &LB($t) );
\r
167 &mov( $ks, &DWP("0x200+$desSP",$tmp2,"",0));
\r
168 &xor( $L, $ks); ######
\r
169 &movb( &LB($tmp2), &HB($t) );
\r
171 &mov( $ks, &DWP("0x100+$desSP",$tmp1,"",0));
\r
172 &xor( $L, $ks); ######
\r
173 &movb( &LB($tmp1), &HB($u) );
\r
175 &mov( $ks, &DWP("0x300+$desSP",$tmp2,"",0));
\r
177 &mov( $ks, &DWP(24,"esp","",0)); ####
\r
178 &movb( &LB($tmp2), &HB($t) );
\r
179 &and( $u, "0xff" );
\r
180 &and( $t, "0xff" );
\r
181 &mov( $tmp1, &DWP("0x600+$desSP",$tmp1,"",0));
\r
183 &mov( $tmp1, &DWP("0x700+$desSP",$tmp2,"",0));
\r
185 &mov( $tmp1, &DWP("0x400+$desSP",$u,"",0));
\r
187 &mov( $tmp1, &DWP("0x500+$desSP",$t,"",0));
\r
193 sprintf("%d",$_[0]);
\r
196 # now has a side affect of rotating $a by $shift
\r
199 local($a,$b,$tt,$shift,$mask,$last)=@_;
\r
201 &rotl( $a, $shift ) if ($shift != 0);
\r
205 if ($notlast eq $b)
\r
220 local($l,$r,$tt,$lr)=@_;
\r
222 &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);
\r
223 &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l);
\r
224 &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r);
\r
225 &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r);
\r
226 &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);
\r
231 { &rotr($tt, 3-$lr); }
\r
232 else { &rotl($tt, $lr-3); }
\r
237 { &rotr($r, 2-$lr); }
\r
238 else { &rotl($r, $lr-2); }
\r
244 local($l,$r,$tt,$lr)=@_;
\r
249 { &rotl($r, 2-$lr); }
\r
250 else { &rotr($r, $lr-2); }
\r
255 { &rotl($l, 3-$lr); }
\r
256 else { &rotr($l, $lr-3); }
\r
259 &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);
\r
260 &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r);
\r
261 &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);
\r
262 &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l);
\r
263 &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r);
\r