Pointer to Ariel Glenn's SSLeay documentation.
[openssl.git] / crypto / des / asm / des-som2.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 #\r
6 \r
7 $prog="des-som2.pl";\r
8 \r
9 # base code is in microsft\r
10 # op dest, source\r
11 # format.\r
12 #\r
13 \r
14 require "desboth.pl";\r
15 \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
26 else\r
27         {\r
28         print STDERR <<"EOF";\r
29 Pick one target type from\r
30         elf     - linux, FreeBSD etc\r
31         a.out   - old linux\r
32         sol     - x86 solaris\r
33         cpp     - format so x86unix.cpp can be used\r
34         win32   - Windows 95/Windows NT\r
35 EOF\r
36         exit(1);\r
37         }\r
38 \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
47 \r
48 &comment("");\r
49 \r
50 &file("dx86xxxx");\r
51 \r
52 $L="edi";\r
53 $R="esi";\r
54 \r
55 &des_encrypt("des_encrypt",1);\r
56 &des_encrypt("des_encrypt2",0);\r
57 \r
58 &des_encrypt3("des_encrypt3",1);\r
59 &des_encrypt3("des_decrypt3",0);\r
60 \r
61 &file_end();\r
62 \r
63 sub des_encrypt\r
64         {\r
65         local($name,$do_ip)=@_;\r
66 \r
67         &function_begin($name,3,"EXTRN   _des_SPtrans:DWORD");\r
68 \r
69         &comment("");\r
70         &comment("Load the 2 words");\r
71         &mov("eax",&wparam(0));\r
72         &mov($R,&DWP(0,"eax","",0));\r
73         &mov($L,&DWP(4,"eax","",0));\r
74 \r
75         if ($do_ip)\r
76                 {\r
77                 &comment("");\r
78                 &comment("IP");\r
79                 &IP_new($R,$L,"eax",3);\r
80 #               &comment("");\r
81 #               &comment("fixup rotate");\r
82 #               &rotl($R,3);\r
83 #               &rotl($L,3);\r
84                 }\r
85         else\r
86                 {\r
87                 &comment("");\r
88                 &comment("fixup rotate");\r
89                 &rotl($R,3);\r
90                 &rotl($L,3);\r
91                 }\r
92 \r
93         &comment("");\r
94         &comment("load counter, key_schedule and enc flag");\r
95 \r
96         # encrypting part\r
97 \r
98         $ks="ebp";\r
99 #       &xor(   "ebx",          "ebx"           );\r
100         &mov("eax",&wparam(2)); # get encrypt flag\r
101         &xor(   "ecx",          "ecx"           );\r
102         &cmp("eax","0");\r
103         &mov(   $ks,            &wparam(1)      );\r
104         &je(&label("start_decrypt"));\r
105 \r
106         for ($i=0; $i<16; $i+=2)\r
107                 {\r
108                 &comment("");\r
109                 &comment("Round $i");\r
110                 &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");\r
111 \r
112                 &comment("");\r
113                 &comment("Round ".sprintf("%d",$i+1));\r
114                 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");\r
115                 }\r
116         &jmp(&label("end"));\r
117 \r
118         &set_label("start_decrypt");\r
119 \r
120         for ($i=15; $i>0; $i-=2)\r
121                 {\r
122                 &comment("");\r
123                 &comment("Round $i");\r
124                 &D_ENCRYPT(15-$i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");\r
125                 &comment("");\r
126                 &comment("Round ".sprintf("%d",$i-1));\r
127                 &D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");\r
128                 }\r
129 \r
130         &set_label("end");\r
131 \r
132         if ($do_ip)\r
133                 {\r
134 #               &comment("");\r
135 #               &comment("Fixup");\r
136 #               &rotr($L,3);            # r\r
137 #               &rotr($R,3);            # l\r
138                 &comment("");\r
139                 &comment("FP");\r
140                 &FP_new($R,$L,"eax",3);\r
141                 }\r
142         else\r
143                 {\r
144                 &comment("");\r
145                 &comment("Fixup");\r
146                 &rotr($L,3);            # r\r
147                 &rotr($R,3);            # l\r
148                 }\r
149 \r
150         &mov("eax",&wparam(0));\r
151         &mov(&DWP(0,"eax","",0),$L);\r
152         &mov(&DWP(4,"eax","",0),$R);\r
153 \r
154         &function_end($name);\r
155         }\r
156 \r
157 sub D_ENCRYPT\r
158         {\r
159         local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_;\r
160 \r
161         &mov(   $u,             &DWP(&n2a($S*4),$ks,"",0));\r
162         &xor(   $tmp1,          $tmp1);\r
163         &mov(   $t,             &DWP(&n2a(($S+1)*4),$ks,"",0));\r
164         &xor(   $u,             $R);\r
165         &xor(   $t,             $R);\r
166         &and(   $u,             "0xfcfcfcfc"    );\r
167         &and(   $t,             "0xcfcfcfcf"    );\r
168         &movb(  &LB($tmp1),     &LB($u) );\r
169         &movb(  &LB($tmp2),     &HB($u) );\r
170         &rotr(  $t,             4               );\r
171         &mov(   $ks,            &DWP("      $desSP",$tmp1,"",0));\r
172         &movb(  &LB($tmp1),     &LB($t) );\r
173         &xor(   $L,             $ks);\r
174         &mov(   $ks,            &DWP("0x200+$desSP",$tmp2,"",0));\r
175         &xor(   $L,             $ks); ######\r
176         &movb(  &LB($tmp2),     &HB($t) );\r
177         &shr(   $u,             16);\r
178         &mov(   $ks,            &DWP("0x100+$desSP",$tmp1,"",0));\r
179         &xor(   $L,             $ks); ######\r
180         &movb(  &LB($tmp1),     &HB($u) );\r
181         &shr(   $t,             16);\r
182         &mov(   $ks,            &DWP("0x300+$desSP",$tmp2,"",0));\r
183         &xor(   $L,             $ks);\r
184         &mov(   $ks,            &DWP(24,"esp","",0)); ####\r
185         &movb(  &LB($tmp2),     &HB($t) );\r
186         &and(   $u,             "0xff"  );\r
187         &and(   $t,             "0xff"  );\r
188         &mov(   $tmp1,          &DWP("0x600+$desSP",$tmp1,"",0));\r
189         &xor(   $L,             $tmp1);\r
190         &mov(   $tmp1,          &DWP("0x700+$desSP",$tmp2,"",0));\r
191         &xor(   $L,             $tmp1);\r
192         &mov(   $tmp1,          &DWP("0x400+$desSP",$u,"",0));\r
193         &xor(   $L,             $tmp1);\r
194         &mov(   $tmp1,          &DWP("0x500+$desSP",$t,"",0));\r
195         &xor(   $L,             $tmp1);\r
196         }\r
197 \r
198 sub PERM_OP\r
199         {\r
200         local($a,$b,$tt,$shift,$mask)=@_;\r
201 \r
202         &mov(   $tt,            $a              );\r
203         &shr(   $tt,            $shift          );\r
204         &xor(   $tt,            $b              );\r
205         &and(   $tt,            $mask           );\r
206         &xor(   $b,             $tt             );\r
207         &shl(   $tt,            $shift          );\r
208         &xor(   $a,             $tt             );\r
209         }\r
210 \r
211 sub IP\r
212         {\r
213         local($l,$r,$tt)=@_;\r
214 \r
215         &PERM_OP($r,$l,$tt, 4,"0x0f0f0f0f");\r
216         &PERM_OP($l,$r,$tt,16,"0x0000ffff");\r
217         &PERM_OP($r,$l,$tt, 2,"0x33333333");\r
218         &PERM_OP($l,$r,$tt, 8,"0x00ff00ff");\r
219         &PERM_OP($r,$l,$tt, 1,"0x55555555");\r
220         }\r
221 \r
222 sub FP\r
223         {\r
224         local($l,$r,$tt)=@_;\r
225 \r
226         &PERM_OP($l,$r,$tt, 1,"0x55555555");\r
227         &PERM_OP($r,$l,$tt, 8,"0x00ff00ff");\r
228         &PERM_OP($l,$r,$tt, 2,"0x33333333");\r
229         &PERM_OP($r,$l,$tt,16,"0x0000ffff");\r
230         &PERM_OP($l,$r,$tt, 4,"0x0f0f0f0f");\r
231         }\r
232 \r
233 sub n2a\r
234         {\r
235         sprintf("%d",$_[0]);\r
236         }\r
237 \r
238 # now has a side affect of rotating $a by $shift\r
239 sub R_PERM_OP\r
240         {\r
241         local($a,$b,$tt,$shift,$mask,$last)=@_;\r
242 \r
243         &rotl(  $a,             $shift          ) if ($shift != 0);\r
244         &mov(   $tt,            $b              );\r
245         &xor(   $tt,            $a              );\r
246         &and(   $tt,            $mask           );\r
247         if ($last eq $b)\r
248                 {\r
249                 &xor(   $a,             $tt             );\r
250                 &xor(   $b,             $tt             );\r
251                 }\r
252         else\r
253                 {\r
254                 &xor(   $b,             $tt             );\r
255                 &xor(   $a,             $tt             );\r
256                 }\r
257         &comment("");\r
258         }\r
259 \r
260 sub IP_new\r
261         {\r
262         local($l,$r,$tt,$lr)=@_;\r
263 \r
264         &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);\r
265         &R_PERM_OP($r,$l,$tt,20,"0xfff0000f",$l);\r
266         &R_PERM_OP($r,$l,$tt,14,"0x33333333",$r);\r
267         &R_PERM_OP($l,$r,$tt,22,"0x03fc03fc",$r);\r
268         &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);\r
269         \r
270         if ($lr != 3)\r
271                 {\r
272                 if (($lr-3) < 0)\r
273                         { &rotr($l,     3-$lr); }\r
274                 else    { &rotl($l,     $lr-3); }\r
275                 }\r
276         if ($lr != 2)\r
277                 {\r
278                 if (($lr-2) < 0)\r
279                         { &rotr($r,     2-$lr); }\r
280                 else    { &rotl($r,     $lr-2); }\r
281                 }\r
282         }\r
283 \r
284 sub FP_new\r
285         {\r
286         local($r,$l,$tt,$lr)=@_;\r
287 \r
288         if ($lr != 2)\r
289                 {\r
290                 if (($lr-2) < 0)\r
291                         { &rotl($r,     2-$lr); }\r
292                 else    { &rotr($r,     $lr-2); }\r
293                 }\r
294         if ($lr != 3)\r
295                 {\r
296                 if (($lr-3) < 0)\r
297                         { &rotl($l,     3-$lr); }\r
298                 else    { &rotr($l,     $lr-3); }\r
299                 }\r
300 \r
301         &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);\r
302         &R_PERM_OP($l,$r,$tt,23,"0x03fc03fc",$r);\r
303         &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);\r
304         &R_PERM_OP($r,$l,$tt,18,"0xfff0000f",$l);\r
305         &R_PERM_OP($r,$l,$tt,12,"0xf0f0f0f0",$r);\r
306         &rotr($l        , 4);\r
307         }\r
308 \r