Whoops, missed one.
[oweals/openssl.git] / crypto / des / asm / crypt.pl
1 #!/usr/local/bin/perl
2 #\r
3 # The inner loop instruction sequence and the IP/FP modifications are from\r
4 # Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>\r
5 # I've added the stuff needed for crypt() but I've not worried about making\r
6 # things perfect.\r
7 #\r
8 \r
9 $prog="crypt.pl";\r
10 \r
11 # base code is in microsft\r
12 # op dest, source\r
13 # format.\r
14 #\r
15 \r
16 require "desboth.pl";\r
17 \r
18 if (    ($ARGV[0] eq "elf"))\r
19         { require "x86unix.pl"; }\r
20 elsif ( ($ARGV[0] eq "a.out"))\r
21         { $aout=1; require "x86unix.pl"; }\r
22 elsif ( ($ARGV[0] eq "sol"))\r
23         { $sol=1; require "x86unix.pl"; }\r
24 elsif ( ($ARGV[0] eq "cpp"))\r
25         { $cpp=1; require "x86unix.pl"; }\r
26 elsif ( ($ARGV[0] eq "win32"))\r
27         { require "x86ms.pl"; }\r
28 else\r
29         {\r
30         print STDERR <<"EOF";\r
31 Pick one target type from\r
32         elf     - linux, FreeBSD etc\r
33         a.out   - old linux\r
34         sol     - x86 solaris\r
35         cpp     - format so x86unix.cpp can be used\r
36         win32   - Windows 95/Windows NT\r
37 EOF\r
38         exit(1);\r
39         }\r
40 \r
41 &comment("Don't even think of reading this code");\r
42 &comment("It was automatically generated by $prog");\r
43 &comment("Which is a perl program used to generate the x86 assember for");\r
44 &comment("any of elf, a.out, Win32, or Solaris");\r
45 &comment("It can be found in SSLeay 0.6.5+ or in libdes 3.26+");\r
46 &comment("eric <eay\@cryptsoft.com>");\r
47 &comment("The inner loop instruction sequence and the IP/FP modifications");\r
48 &comment("are from Svend Olaf Mikkelsen <svolaf\@inet.uni-c.dk>");\r
49 \r
50 &comment("");\r
51 \r
52 &file("dx86xxxx");\r
53 \r
54 $L="edi";\r
55 $R="esi";\r
56 \r
57 &fcrypt_body("fcrypt_body");\r
58 \r
59 &file_end();\r
60 \r
61 sub fcrypt_body\r
62         {\r
63         local($name,$do_ip)=@_;\r
64 \r
65         &function_begin($name,3,"EXTRN   _des_SPtrans:DWORD");\r
66 \r
67         &comment("");\r
68         &comment("Load the 2 words");\r
69         $ks="ebp";\r
70 \r
71         &xor(   $L,     $L);\r
72         &xor(   $R,     $R);\r
73         &mov($ks,&wparam(1));\r
74 \r
75         &mov(&wtmp(1),  25);\r
76 \r
77         &set_label("start");\r
78         for ($i=0; $i<16; $i+=2)\r
79                 {\r
80                 &comment("");\r
81                 &comment("Round $i");\r
82                 &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");\r
83 \r
84                 &comment("");\r
85                 &comment("Round ".sprintf("%d",$i+1));\r
86                 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");\r
87                 }\r
88         &mov("eax",     $L);\r
89         &dec(&wtmp(1));\r
90         &mov($L,        $R);\r
91         &mov($R,        "eax");\r
92         &jnz(&label("start"));\r
93 \r
94         &comment("");\r
95         &comment("FP");\r
96         &mov("edx",&wparam(0));\r
97 \r
98         &FP_new($R,$L,"eax",3);\r
99         &mov(&DWP(0,"edx","",0),"eax");\r
100         &mov(&DWP(4,"edx","",0),$L);\r
101 \r
102         &function_end($name);\r
103         }\r
104 \r
105 sub D_ENCRYPT\r
106         {\r
107         local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_;\r
108 \r
109         &mov(   $u,             &wparam(2));                    # 2\r
110         &mov(   $t,             $R);\r
111         &shr(   $t,             16);                            # 1\r
112         &mov(   $tmp2,          &wparam(3));                    # 2\r
113         &xor(   $t,             $R);                            # 1\r
114 \r
115         &and(   $u,             $t);                            # 2\r
116         &and(   $t,             $tmp2);                         # 2\r
117 \r
118         &mov(   $tmp1,          $u);\r
119         &shl(   $tmp1,          16);                            # 1\r
120         &mov(   $tmp2,          $t);\r
121         &shl(   $tmp2,          16);                            # 1\r
122         &xor(   $u,             $tmp1);                         # 2\r
123         &xor(   $t,             $tmp2);                         # 2\r
124         &mov(   $tmp1,          &DWP(&n2a($S*4),$ks,"",0));     # 2\r
125         &xor(   $u,             $tmp1);\r
126         &mov(   $tmp2,          &DWP(&n2a(($S+1)*4),$ks,"",0)); # 2\r
127         &xor(   $u,             $R);\r
128         &xor(   $t,             $R);\r
129         &xor(   $t,             $tmp2);\r
130 \r
131         &and(   $u,             "0xfcfcfcfc"    );              # 2\r
132         &xor(   $tmp1,          $tmp1);                         # 1\r
133         &and(   $t,             "0xcfcfcfcf"    );              # 2\r
134         &xor(   $tmp2,          $tmp2); \r
135         &movb(  &LB($tmp1),     &LB($u) );\r
136         &movb(  &LB($tmp2),     &HB($u) );\r
137         &rotr(  $t,             4               );\r
138         &mov(   $ks,            &DWP("      $desSP",$tmp1,"",0));\r
139         &movb(  &LB($tmp1),     &LB($t) );\r
140         &xor(   $L,             $ks);\r
141         &mov(   $ks,            &DWP("0x200+$desSP",$tmp2,"",0));\r
142         &xor(   $L,             $ks);\r
143         &movb(  &LB($tmp2),     &HB($t) );\r
144         &shr(   $u,             16);\r
145         &mov(   $ks,            &DWP("0x100+$desSP",$tmp1,"",0));\r
146         &xor(   $L,             $ks); \r
147         &movb(  &LB($tmp1),     &HB($u) );\r
148         &shr(   $t,             16);\r
149         &mov(   $ks,            &DWP("0x300+$desSP",$tmp2,"",0));\r
150         &xor(   $L,             $ks);\r
151         &mov(   $ks,            &DWP(24,"esp","",0));\r
152         &movb(  &LB($tmp2),     &HB($t) );\r
153         &and(   $u,             "0xff"  );\r
154         &and(   $t,             "0xff"  );\r
155         &mov(   $tmp1,          &DWP("0x600+$desSP",$tmp1,"",0));\r
156         &xor(   $L,             $tmp1);\r
157         &mov(   $tmp1,          &DWP("0x700+$desSP",$tmp2,"",0));\r
158         &xor(   $L,             $tmp1);\r
159         &mov(   $tmp1,          &DWP("0x400+$desSP",$u,"",0));\r
160         &xor(   $L,             $tmp1);\r
161         &mov(   $tmp1,          &DWP("0x500+$desSP",$t,"",0));\r
162         &xor(   $L,             $tmp1);\r
163         }\r
164 \r
165 sub n2a\r
166         {\r
167         sprintf("%d",$_[0]);\r
168         }\r
169 \r
170 # now has a side affect of rotating $a by $shift\r
171 sub R_PERM_OP\r
172         {\r
173         local($a,$b,$tt,$shift,$mask,$last)=@_;\r
174 \r
175         &rotl(  $a,             $shift          ) if ($shift != 0);\r
176         &mov(   $tt,            $a              );\r
177         &xor(   $a,             $b              );\r
178         &and(   $a,             $mask           );\r
179         if ($notlast eq $b)\r
180                 {\r
181                 &xor(   $b,             $a              );\r
182                 &xor(   $tt,            $a              );\r
183                 }\r
184         else\r
185                 {\r
186                 &xor(   $tt,            $a              );\r
187                 &xor(   $b,             $a              );\r
188                 }\r
189         &comment("");\r
190         }\r
191 \r
192 sub IP_new\r
193         {\r
194         local($l,$r,$tt,$lr)=@_;\r
195 \r
196         &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);\r
197         &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l);\r
198         &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r);\r
199         &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r);\r
200         &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);\r
201         \r
202         if ($lr != 3)\r
203                 {\r
204                 if (($lr-3) < 0)\r
205                         { &rotr($tt,    3-$lr); }\r
206                 else    { &rotl($tt,    $lr-3); }\r
207                 }\r
208         if ($lr != 2)\r
209                 {\r
210                 if (($lr-2) < 0)\r
211                         { &rotr($r,     2-$lr); }\r
212                 else    { &rotl($r,     $lr-2); }\r
213                 }\r
214         }\r
215 \r
216 sub FP_new\r
217         {\r
218         local($l,$r,$tt,$lr)=@_;\r
219 \r
220         if ($lr != 2)\r
221                 {\r
222                 if (($lr-2) < 0)\r
223                         { &rotl($r,     2-$lr); }\r
224                 else    { &rotr($r,     $lr-2); }\r
225                 }\r
226         if ($lr != 3)\r
227                 {\r
228                 if (($lr-3) < 0)\r
229                         { &rotl($l,     3-$lr); }\r
230                 else    { &rotr($l,     $lr-3); }\r
231                 }\r
232 \r
233         &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);\r
234         &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r);\r
235         &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);\r
236         &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l);\r
237         &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r);\r
238         &rotr($tt       , 4);\r
239         }\r
240 \r