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