Import of old SSLeay release: SSLeay 0.8.1b
[openssl.git] / crypto / des / asm / des586.pl
1 #!/usr/local/bin/perl
2 \r
3 $prog="des586.pl";\r
4 \r
5 # base code is in microsft\r
6 # op dest, source\r
7 # format.\r
8 #\r
9 \r
10 # WILL NOT WORK ANYMORE WITH desboth.pl\r
11 require "desboth.pl";\r
12 \r
13 if (    ($ARGV[0] eq "elf"))\r
14         { require "x86unix.pl"; }\r
15 elsif ( ($ARGV[0] eq "a.out"))\r
16         { $aout=1; require "x86unix.pl"; }\r
17 elsif ( ($ARGV[0] eq "sol"))\r
18         { $sol=1; require "x86unix.pl"; }\r
19 elsif ( ($ARGV[0] eq "cpp"))\r
20         { $cpp=1; require "x86unix.pl"; }\r
21 elsif ( ($ARGV[0] eq "win32"))\r
22         { require "x86ms.pl"; }\r
23 else\r
24         {\r
25         print STDERR <<"EOF";\r
26 Pick one target type from\r
27         elf     - linux, FreeBSD etc\r
28         a.out   - old linux\r
29         sol     - x86 solaris\r
30         cpp     - format so x86unix.cpp can be used\r
31         win32   - Windows 95/Windows NT\r
32 EOF\r
33         exit(1);\r
34         }\r
35 \r
36 &comment("Don't even think of reading this code");\r
37 &comment("It was automatically generated by $prog");\r
38 &comment("Which is a perl program used to generate the x86 assember for");\r
39 &comment("any of elf, a.out, Win32, or Solaris");\r
40 &comment("It can be found in SSLeay 0.6.5+ or in libdes 3.26+");\r
41 &comment("eric <eay\@cryptsoft.com>");\r
42 &comment("");\r
43 \r
44 &file("dx86xxxx");\r
45 \r
46 $L="edi";\r
47 $R="esi";\r
48 \r
49 &des_encrypt("des_encrypt",1);\r
50 &des_encrypt("des_encrypt2",0);\r
51 \r
52 &des_encrypt3("des_encrypt3",1);\r
53 &des_encrypt3("des_decrypt3",0);\r
54 \r
55 &file_end();\r
56 \r
57 sub des_encrypt\r
58         {\r
59         local($name,$do_ip)=@_;\r
60 \r
61         &function_begin($name,3,"EXTRN   _des_SPtrans:DWORD");\r
62 \r
63         &comment("");\r
64         &comment("Load the 2 words");\r
65         &mov("eax",&wparam(0));\r
66         &mov($R,&DWP(0,"eax","",0));\r
67         &mov($L,&DWP(4,"eax","",0));\r
68 \r
69         if ($do_ip)\r
70                 {\r
71                 &comment("");\r
72                 &comment("IP");\r
73                 &IP($R,$L,"eax");\r
74                 }\r
75 \r
76         &comment("");\r
77         &comment("fixup rotate");\r
78         &rotl($R,3);\r
79         &rotl($L,3);\r
80 \r
81         &comment("");\r
82         &comment("load counter, key_schedule and enc flag");\r
83 \r
84         # encrypting part\r
85 \r
86         $ks="ebp";\r
87         &xor(   "ebx",          "ebx"           );\r
88         &mov("eax",&wparam(2)); # get encrypt flag\r
89         &xor(   "ecx",          "ecx"           );\r
90         &cmp("eax","0");\r
91         &mov(   $ks,            &wparam(1)      );\r
92         &je(&label("start_decrypt"));\r
93 \r
94         for ($i=0; $i<16; $i+=2)\r
95                 {\r
96                 &comment("");\r
97                 &comment("Round $i");\r
98                 &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");\r
99 \r
100                 &comment("");\r
101                 &comment("Round ".sprintf("%d",$i+1));\r
102                 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");\r
103                 }\r
104         &jmp(&label("end"));\r
105 \r
106         &set_label("start_decrypt");\r
107 \r
108         for ($i=15; $i>0; $i-=2)\r
109                 {\r
110                 &comment("");\r
111                 &comment("Round $i");\r
112                 &D_ENCRYPT(15-$i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");\r
113                 &comment("");\r
114                 &comment("Round ".sprintf("%d",$i-1));\r
115                 &D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");\r
116                 }\r
117 \r
118         &set_label("end");\r
119 \r
120         &comment("");\r
121         &comment("Fixup");\r
122         &rotr($L,3);            # r\r
123         &rotr($R,3);            # l\r
124 \r
125         if ($do_ip)\r
126                 {\r
127                 &comment("");\r
128                 &comment("FP");\r
129                 &FP($R,$L,"eax");\r
130                 }\r
131 \r
132         &mov("eax",&wparam(0));\r
133         &mov(&DWP(0,"eax","",0),$L);\r
134         &mov(&DWP(4,"eax","",0),$R);\r
135 \r
136         &function_end($name);\r
137         }\r
138 \r
139 sub D_ENCRYPT\r
140         {\r
141         local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_;\r
142 \r
143          &mov(  $t,             &DWP(&n2a(($S+1)*4),$ks,"",0));\r
144         &mov(   $u,             &DWP(&n2a($S*4),$ks,"",0));\r
145          &xor(  $t,             $R);\r
146         &xor(   $u,             $R);\r
147          &rotr( $t,             4               );\r
148         &and(   $u,             "0xfcfcfcfc"    );\r
149          &and(  $t,             "0xfcfcfcfc"    );\r
150         &movb(  &LB($tmp1),     &LB($u) );\r
151         &movb(  &LB($tmp2),     &HB($u) );\r
152         &xor(   $L,             &DWP("      $desSP",$tmp1,"",0));\r
153         &shr(   $u,             16);\r
154         &xor(   $L,             &DWP("0x200+$desSP",$tmp2,"",0));\r
155         &movb(  &LB($tmp1),     &LB($u) );\r
156         &movb(  &LB($tmp2),     &HB($u) );\r
157         &xor(   $L,             &DWP("0x400+$desSP",$tmp1,"",0));\r
158         &mov(   $u,             &DWP("0x600+$desSP",$tmp2,"",0));\r
159 \r
160          &movb( &LB($tmp1),     &LB($t) );\r
161          &movb( &LB($tmp2),     &HB($t) );\r
162          &xor(  $L,             &DWP("0x100+$desSP",$tmp1,"",0));\r
163          &shr(  $t,             16);\r
164          &xor(  $u,             &DWP("0x300+$desSP",$tmp2,"",0));\r
165          &movb( &LB($tmp1),     &LB($t) );\r
166          &movb( &LB($tmp2),     &HB($t) );\r
167          &xor(  $L,             &DWP("0x500+$desSP",$tmp1,"",0));\r
168          &xor(  $u,             &DWP("0x700+$desSP",$tmp2,"",0));\r
169          &xor(  $L,     $u);\r
170         }\r
171 \r
172 sub PERM_OP\r
173         {\r
174         local($a,$b,$tt,$shift,$mask)=@_;\r
175 \r
176         &mov(   $tt,            $a              );\r
177         &shr(   $tt,            $shift          );\r
178         &xor(   $tt,            $b              );\r
179         &and(   $tt,            $mask           );\r
180         &xor(   $b,             $tt             );\r
181         &shl(   $tt,            $shift          );\r
182         &xor(   $a,             $tt             );\r
183         }\r
184 \r
185 sub IP\r
186         {\r
187         local($l,$r,$tt)=@_;\r
188 \r
189         &PERM_OP($r,$l,$tt, 4,"0x0f0f0f0f");\r
190         &PERM_OP($l,$r,$tt,16,"0x0000ffff");\r
191         &PERM_OP($r,$l,$tt, 2,"0x33333333");\r
192         &PERM_OP($l,$r,$tt, 8,"0x00ff00ff");\r
193         &PERM_OP($r,$l,$tt, 1,"0x55555555");\r
194         }\r
195 \r
196 sub FP\r
197         {\r
198         local($l,$r,$tt)=@_;\r
199 \r
200         &PERM_OP($l,$r,$tt, 1,"0x55555555");\r
201         &PERM_OP($r,$l,$tt, 8,"0x00ff00ff");\r
202         &PERM_OP($l,$r,$tt, 2,"0x33333333");\r
203         &PERM_OP($r,$l,$tt,16,"0x0000ffff");\r
204         &PERM_OP($l,$r,$tt, 4,"0x0f0f0f0f");\r
205         }\r
206 \r
207 sub n2a\r
208         {\r
209         sprintf("%d",$_[0]);\r
210         }\r