ifdef cleanup part 3: OPENSSL_SYSNAME
[openssl.git] / crypto / des / asm / des-586.pl
1 #!/usr/local/bin/perl
2 #
3 # The inner loop instruction sequence and the IP/FP modifications are from
4 # Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>
5 #
6
7 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
8 push(@INC,"${dir}","${dir}../../perlasm");
9 require "x86asm.pl";
10 require "cbc.pl";
11 require "desboth.pl";
12
13 # base code is in microsft
14 # op dest, source
15 # format.
16 #
17
18 &asm_init($ARGV[0],"des-586.pl");
19
20 $L="edi";
21 $R="esi";
22 $trans="ebp";
23 $small_footprint=1 if (grep(/\-DOPENSSL_SMALL_FOOTPRINT/,@ARGV));
24 # one can discuss setting this variable to 1 unconditionally, as
25 # the folded loop is only 3% slower than unrolled, but >7 times smaller
26
27 &public_label("DES_SPtrans");
28 &static_label("des_sptrans");
29
30 &DES_encrypt_internal();
31 &DES_decrypt_internal();
32 &DES_encrypt("DES_encrypt1",1);
33 &DES_encrypt("DES_encrypt2",0);
34 &DES_encrypt3("DES_encrypt3",1);
35 &DES_encrypt3("DES_decrypt3",0);
36 &cbc("DES_ncbc_encrypt","DES_encrypt1","DES_encrypt1",0,4,5,3,5,-1);
37 &cbc("DES_ede3_cbc_encrypt","DES_encrypt3","DES_decrypt3",0,6,7,3,4,5);
38 &DES_SPtrans();
39
40 &asm_finish();
41
42 sub DES_encrypt_internal()
43         {
44         &function_begin_B("_x86_DES_encrypt");
45
46         if ($small_footprint)
47             {
48             &lea("edx",&DWP(128,"ecx"));
49             &push("edx");
50             &push("ecx");
51             &set_label("eloop");
52                 &D_ENCRYPT(0,$L,$R,0,$trans,"eax","ebx","ecx","edx",&swtmp(0));
53                 &comment("");
54                 &D_ENCRYPT(1,$R,$L,2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
55                 &comment("");
56                 &add("ecx",16);
57                 &cmp("ecx",&swtmp(1));
58                 &mov(&swtmp(0),"ecx");
59                 &jb(&label("eloop"));
60             &add("esp",8);
61             }
62         else
63             {
64             &push("ecx");
65             for ($i=0; $i<16; $i+=2)
66                 {
67                 &comment("Round $i");
68                 &D_ENCRYPT($i,$L,$R,$i*2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
69                 &comment("Round ".sprintf("%d",$i+1));
70                 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
71                 }
72             &add("esp",4);
73         }
74         &ret();
75
76         &function_end_B("_x86_DES_encrypt");
77         }
78         
79 sub DES_decrypt_internal()
80         {
81         &function_begin_B("_x86_DES_decrypt");
82
83         if ($small_footprint)
84             {
85             &push("ecx");
86             &lea("ecx",&DWP(128,"ecx"));
87             &push("ecx");
88             &set_label("dloop");
89                 &D_ENCRYPT(0,$L,$R,-2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
90                 &comment("");
91                 &D_ENCRYPT(1,$R,$L,-4,$trans,"eax","ebx","ecx","edx",&swtmp(0));
92                 &comment("");
93                 &sub("ecx",16);
94                 &cmp("ecx",&swtmp(1));
95                 &mov(&swtmp(0),"ecx");
96                 &ja(&label("dloop"));
97             &add("esp",8);
98             }
99         else
100             {
101             &push("ecx");
102             for ($i=15; $i>0; $i-=2)
103                 {
104                 &comment("Round $i");
105                 &D_ENCRYPT(15-$i,$L,$R,$i*2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
106                 &comment("Round ".sprintf("%d",$i-1));
107                 &D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
108                 }
109             &add("esp",4);
110             }
111         &ret();
112
113         &function_end_B("_x86_DES_decrypt");
114         }
115         
116 sub DES_encrypt
117         {
118         local($name,$do_ip)=@_;
119
120         &function_begin_B($name);
121
122         &push("esi");
123         &push("edi");
124
125         &comment("");
126         &comment("Load the 2 words");
127
128         if ($do_ip)
129                 {
130                 &mov($R,&wparam(0));
131                  &xor(  "ecx",          "ecx"           );
132
133                 &push("ebx");
134                 &push("ebp");
135
136                 &mov("eax",&DWP(0,$R,"",0));
137                  &mov("ebx",&wparam(2));        # get encrypt flag
138                 &mov($L,&DWP(4,$R,"",0));
139                 &comment("");
140                 &comment("IP");
141                 &IP_new("eax",$L,$R,3);
142                 }
143         else
144                 {
145                 &mov("eax",&wparam(0));
146                  &xor(  "ecx",          "ecx"           );
147
148                 &push("ebx");
149                 &push("ebp");
150
151                 &mov($R,&DWP(0,"eax","",0));
152                  &mov("ebx",&wparam(2));        # get encrypt flag
153                 &rotl($R,3);
154                 &mov($L,&DWP(4,"eax","",0));
155                 &rotl($L,3);
156                 }
157
158         # PIC-ification:-)
159         &call   (&label("pic_point"));
160         &set_label("pic_point");
161         &blindpop($trans);
162         &lea    ($trans,&DWP(&label("des_sptrans")."-".&label("pic_point"),$trans));
163
164         &mov(   "ecx",  &wparam(1)      );
165
166         &cmp("ebx","0");
167         &je(&label("decrypt"));
168         &call("_x86_DES_encrypt");
169         &jmp(&label("done"));
170         &set_label("decrypt");
171         &call("_x86_DES_decrypt");
172         &set_label("done");
173
174         if ($do_ip)
175                 {
176                 &comment("");
177                 &comment("FP");
178                 &mov("edx",&wparam(0));
179                 &FP_new($L,$R,"eax",3);
180
181                 &mov(&DWP(0,"edx","",0),"eax");
182                 &mov(&DWP(4,"edx","",0),$R);
183                 }
184         else
185                 {
186                 &comment("");
187                 &comment("Fixup");
188                 &rotr($L,3);            # r
189                  &mov("eax",&wparam(0));
190                 &rotr($R,3);            # l
191                  &mov(&DWP(0,"eax","",0),$L);
192                  &mov(&DWP(4,"eax","",0),$R);
193                 }
194
195         &pop("ebp");
196         &pop("ebx");
197         &pop("edi");
198         &pop("esi");
199         &ret();
200
201         &function_end_B($name);
202         }
203
204 sub D_ENCRYPT
205         {
206         local($r,$L,$R,$S,$trans,$u,$tmp1,$tmp2,$t,$wp1)=@_;
207
208          &mov(  $u,             &DWP(&n2a($S*4),$tmp2,"",0));
209         &xor(   $tmp1,          $tmp1);
210          &mov(  $t,             &DWP(&n2a(($S+1)*4),$tmp2,"",0));
211         &xor(   $u,             $R);
212         &xor(   $tmp2,          $tmp2);
213          &xor(  $t,             $R);
214         &and(   $u,             "0xfcfcfcfc"    );
215          &and(  $t,             "0xcfcfcfcf"    );
216         &movb(  &LB($tmp1),     &LB($u) );
217          &movb( &LB($tmp2),     &HB($u) );
218         &rotr(  $t,             4               );
219         &xor(   $L,             &DWP("     ",$trans,$tmp1,0));
220          &movb( &LB($tmp1),     &LB($t) );
221          &xor(  $L,             &DWP("0x200",$trans,$tmp2,0));
222          &movb( &LB($tmp2),     &HB($t) );
223         &shr(   $u,             16);
224          &xor(  $L,             &DWP("0x100",$trans,$tmp1,0));
225          &movb( &LB($tmp1),     &HB($u) );
226         &shr(   $t,             16);
227          &xor(  $L,             &DWP("0x300",$trans,$tmp2,0));
228         &movb(  &LB($tmp2),     &HB($t) );
229          &and(  $u,             "0xff"  );
230         &and(   $t,             "0xff"  );
231          &xor(  $L,             &DWP("0x600",$trans,$tmp1,0));
232          &xor(  $L,             &DWP("0x700",$trans,$tmp2,0));
233         &mov(   $tmp2,          $wp1    );
234          &xor(  $L,             &DWP("0x400",$trans,$u,0));
235          &xor(  $L,             &DWP("0x500",$trans,$t,0));
236         }
237
238 sub n2a
239         {
240         sprintf("%d",$_[0]);
241         }
242
243 # now has a side affect of rotating $a by $shift
244 sub R_PERM_OP
245         {
246         local($a,$b,$tt,$shift,$mask,$last)=@_;
247
248         &rotl(  $a,             $shift          ) if ($shift != 0);
249         &mov(   $tt,            $a              );
250         &xor(   $a,             $b              );
251         &and(   $a,             $mask           );
252         # This can never succeed, and besides it is difficult to see what the
253         # idea was - Ben 13 Feb 99
254         if (!$last eq $b)
255                 {
256                 &xor(   $b,             $a              );
257                 &xor(   $tt,            $a              );
258                 }
259         else
260                 {
261                 &xor(   $tt,            $a              );
262                 &xor(   $b,             $a              );
263                 }
264         &comment("");
265         }
266
267 sub IP_new
268         {
269         local($l,$r,$tt,$lr)=@_;
270
271         &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);
272         &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l);
273         &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r);
274         &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r);
275         &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);
276         
277         if ($lr != 3)
278                 {
279                 if (($lr-3) < 0)
280                         { &rotr($tt,    3-$lr); }
281                 else    { &rotl($tt,    $lr-3); }
282                 }
283         if ($lr != 2)
284                 {
285                 if (($lr-2) < 0)
286                         { &rotr($r,     2-$lr); }
287                 else    { &rotl($r,     $lr-2); }
288                 }
289         }
290
291 sub FP_new
292         {
293         local($l,$r,$tt,$lr)=@_;
294
295         if ($lr != 2)
296                 {
297                 if (($lr-2) < 0)
298                         { &rotl($r,     2-$lr); }
299                 else    { &rotr($r,     $lr-2); }
300                 }
301         if ($lr != 3)
302                 {
303                 if (($lr-3) < 0)
304                         { &rotl($l,     3-$lr); }
305                 else    { &rotr($l,     $lr-3); }
306                 }
307
308         &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);
309         &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r);
310         &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);
311         &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l);
312         &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r);
313         &rotr($tt       , 4);
314         }
315
316 sub DES_SPtrans
317         {
318         &set_label("DES_SPtrans",64);
319         &set_label("des_sptrans");
320         &data_word(0x02080800, 0x00080000, 0x02000002, 0x02080802);
321         &data_word(0x02000000, 0x00080802, 0x00080002, 0x02000002);
322         &data_word(0x00080802, 0x02080800, 0x02080000, 0x00000802);
323         &data_word(0x02000802, 0x02000000, 0x00000000, 0x00080002);
324         &data_word(0x00080000, 0x00000002, 0x02000800, 0x00080800);
325         &data_word(0x02080802, 0x02080000, 0x00000802, 0x02000800);
326         &data_word(0x00000002, 0x00000800, 0x00080800, 0x02080002);
327         &data_word(0x00000800, 0x02000802, 0x02080002, 0x00000000);
328         &data_word(0x00000000, 0x02080802, 0x02000800, 0x00080002);
329         &data_word(0x02080800, 0x00080000, 0x00000802, 0x02000800);
330         &data_word(0x02080002, 0x00000800, 0x00080800, 0x02000002);
331         &data_word(0x00080802, 0x00000002, 0x02000002, 0x02080000);
332         &data_word(0x02080802, 0x00080800, 0x02080000, 0x02000802);
333         &data_word(0x02000000, 0x00000802, 0x00080002, 0x00000000);
334         &data_word(0x00080000, 0x02000000, 0x02000802, 0x02080800);
335         &data_word(0x00000002, 0x02080002, 0x00000800, 0x00080802);
336         # nibble 1
337         &data_word(0x40108010, 0x00000000, 0x00108000, 0x40100000);
338         &data_word(0x40000010, 0x00008010, 0x40008000, 0x00108000);
339         &data_word(0x00008000, 0x40100010, 0x00000010, 0x40008000);
340         &data_word(0x00100010, 0x40108000, 0x40100000, 0x00000010);
341         &data_word(0x00100000, 0x40008010, 0x40100010, 0x00008000);
342         &data_word(0x00108010, 0x40000000, 0x00000000, 0x00100010);
343         &data_word(0x40008010, 0x00108010, 0x40108000, 0x40000010);
344         &data_word(0x40000000, 0x00100000, 0x00008010, 0x40108010);
345         &data_word(0x00100010, 0x40108000, 0x40008000, 0x00108010);
346         &data_word(0x40108010, 0x00100010, 0x40000010, 0x00000000);
347         &data_word(0x40000000, 0x00008010, 0x00100000, 0x40100010);
348         &data_word(0x00008000, 0x40000000, 0x00108010, 0x40008010);
349         &data_word(0x40108000, 0x00008000, 0x00000000, 0x40000010);
350         &data_word(0x00000010, 0x40108010, 0x00108000, 0x40100000);
351         &data_word(0x40100010, 0x00100000, 0x00008010, 0x40008000);
352         &data_word(0x40008010, 0x00000010, 0x40100000, 0x00108000);
353         # nibble 2
354         &data_word(0x04000001, 0x04040100, 0x00000100, 0x04000101);
355         &data_word(0x00040001, 0x04000000, 0x04000101, 0x00040100);
356         &data_word(0x04000100, 0x00040000, 0x04040000, 0x00000001);
357         &data_word(0x04040101, 0x00000101, 0x00000001, 0x04040001);
358         &data_word(0x00000000, 0x00040001, 0x04040100, 0x00000100);
359         &data_word(0x00000101, 0x04040101, 0x00040000, 0x04000001);
360         &data_word(0x04040001, 0x04000100, 0x00040101, 0x04040000);
361         &data_word(0x00040100, 0x00000000, 0x04000000, 0x00040101);
362         &data_word(0x04040100, 0x00000100, 0x00000001, 0x00040000);
363         &data_word(0x00000101, 0x00040001, 0x04040000, 0x04000101);
364         &data_word(0x00000000, 0x04040100, 0x00040100, 0x04040001);
365         &data_word(0x00040001, 0x04000000, 0x04040101, 0x00000001);
366         &data_word(0x00040101, 0x04000001, 0x04000000, 0x04040101);
367         &data_word(0x00040000, 0x04000100, 0x04000101, 0x00040100);
368         &data_word(0x04000100, 0x00000000, 0x04040001, 0x00000101);
369         &data_word(0x04000001, 0x00040101, 0x00000100, 0x04040000);
370         # nibble 3
371         &data_word(0x00401008, 0x10001000, 0x00000008, 0x10401008);
372         &data_word(0x00000000, 0x10400000, 0x10001008, 0x00400008);
373         &data_word(0x10401000, 0x10000008, 0x10000000, 0x00001008);
374         &data_word(0x10000008, 0x00401008, 0x00400000, 0x10000000);
375         &data_word(0x10400008, 0x00401000, 0x00001000, 0x00000008);
376         &data_word(0x00401000, 0x10001008, 0x10400000, 0x00001000);
377         &data_word(0x00001008, 0x00000000, 0x00400008, 0x10401000);
378         &data_word(0x10001000, 0x10400008, 0x10401008, 0x00400000);
379         &data_word(0x10400008, 0x00001008, 0x00400000, 0x10000008);
380         &data_word(0x00401000, 0x10001000, 0x00000008, 0x10400000);
381         &data_word(0x10001008, 0x00000000, 0x00001000, 0x00400008);
382         &data_word(0x00000000, 0x10400008, 0x10401000, 0x00001000);
383         &data_word(0x10000000, 0x10401008, 0x00401008, 0x00400000);
384         &data_word(0x10401008, 0x00000008, 0x10001000, 0x00401008);
385         &data_word(0x00400008, 0x00401000, 0x10400000, 0x10001008);
386         &data_word(0x00001008, 0x10000000, 0x10000008, 0x10401000);
387         # nibble 4
388         &data_word(0x08000000, 0x00010000, 0x00000400, 0x08010420);
389         &data_word(0x08010020, 0x08000400, 0x00010420, 0x08010000);
390         &data_word(0x00010000, 0x00000020, 0x08000020, 0x00010400);
391         &data_word(0x08000420, 0x08010020, 0x08010400, 0x00000000);
392         &data_word(0x00010400, 0x08000000, 0x00010020, 0x00000420);
393         &data_word(0x08000400, 0x00010420, 0x00000000, 0x08000020);
394         &data_word(0x00000020, 0x08000420, 0x08010420, 0x00010020);
395         &data_word(0x08010000, 0x00000400, 0x00000420, 0x08010400);
396         &data_word(0x08010400, 0x08000420, 0x00010020, 0x08010000);
397         &data_word(0x00010000, 0x00000020, 0x08000020, 0x08000400);
398         &data_word(0x08000000, 0x00010400, 0x08010420, 0x00000000);
399         &data_word(0x00010420, 0x08000000, 0x00000400, 0x00010020);
400         &data_word(0x08000420, 0x00000400, 0x00000000, 0x08010420);
401         &data_word(0x08010020, 0x08010400, 0x00000420, 0x00010000);
402         &data_word(0x00010400, 0x08010020, 0x08000400, 0x00000420);
403         &data_word(0x00000020, 0x00010420, 0x08010000, 0x08000020);
404         # nibble 5
405         &data_word(0x80000040, 0x00200040, 0x00000000, 0x80202000);
406         &data_word(0x00200040, 0x00002000, 0x80002040, 0x00200000);
407         &data_word(0x00002040, 0x80202040, 0x00202000, 0x80000000);
408         &data_word(0x80002000, 0x80000040, 0x80200000, 0x00202040);
409         &data_word(0x00200000, 0x80002040, 0x80200040, 0x00000000);
410         &data_word(0x00002000, 0x00000040, 0x80202000, 0x80200040);
411         &data_word(0x80202040, 0x80200000, 0x80000000, 0x00002040);
412         &data_word(0x00000040, 0x00202000, 0x00202040, 0x80002000);
413         &data_word(0x00002040, 0x80000000, 0x80002000, 0x00202040);
414         &data_word(0x80202000, 0x00200040, 0x00000000, 0x80002000);
415         &data_word(0x80000000, 0x00002000, 0x80200040, 0x00200000);
416         &data_word(0x00200040, 0x80202040, 0x00202000, 0x00000040);
417         &data_word(0x80202040, 0x00202000, 0x00200000, 0x80002040);
418         &data_word(0x80000040, 0x80200000, 0x00202040, 0x00000000);
419         &data_word(0x00002000, 0x80000040, 0x80002040, 0x80202000);
420         &data_word(0x80200000, 0x00002040, 0x00000040, 0x80200040);
421         # nibble 6
422         &data_word(0x00004000, 0x00000200, 0x01000200, 0x01000004);
423         &data_word(0x01004204, 0x00004004, 0x00004200, 0x00000000);
424         &data_word(0x01000000, 0x01000204, 0x00000204, 0x01004000);
425         &data_word(0x00000004, 0x01004200, 0x01004000, 0x00000204);
426         &data_word(0x01000204, 0x00004000, 0x00004004, 0x01004204);
427         &data_word(0x00000000, 0x01000200, 0x01000004, 0x00004200);
428         &data_word(0x01004004, 0x00004204, 0x01004200, 0x00000004);
429         &data_word(0x00004204, 0x01004004, 0x00000200, 0x01000000);
430         &data_word(0x00004204, 0x01004000, 0x01004004, 0x00000204);
431         &data_word(0x00004000, 0x00000200, 0x01000000, 0x01004004);
432         &data_word(0x01000204, 0x00004204, 0x00004200, 0x00000000);
433         &data_word(0x00000200, 0x01000004, 0x00000004, 0x01000200);
434         &data_word(0x00000000, 0x01000204, 0x01000200, 0x00004200);
435         &data_word(0x00000204, 0x00004000, 0x01004204, 0x01000000);
436         &data_word(0x01004200, 0x00000004, 0x00004004, 0x01004204);
437         &data_word(0x01000004, 0x01004200, 0x01004000, 0x00004004);
438         # nibble 7
439         &data_word(0x20800080, 0x20820000, 0x00020080, 0x00000000);
440         &data_word(0x20020000, 0x00800080, 0x20800000, 0x20820080);
441         &data_word(0x00000080, 0x20000000, 0x00820000, 0x00020080);
442         &data_word(0x00820080, 0x20020080, 0x20000080, 0x20800000);
443         &data_word(0x00020000, 0x00820080, 0x00800080, 0x20020000);
444         &data_word(0x20820080, 0x20000080, 0x00000000, 0x00820000);
445         &data_word(0x20000000, 0x00800000, 0x20020080, 0x20800080);
446         &data_word(0x00800000, 0x00020000, 0x20820000, 0x00000080);
447         &data_word(0x00800000, 0x00020000, 0x20000080, 0x20820080);
448         &data_word(0x00020080, 0x20000000, 0x00000000, 0x00820000);
449         &data_word(0x20800080, 0x20020080, 0x20020000, 0x00800080);
450         &data_word(0x20820000, 0x00000080, 0x00800080, 0x20020000);
451         &data_word(0x20820080, 0x00800000, 0x20800000, 0x20000080);
452         &data_word(0x00820000, 0x00020080, 0x20020080, 0x20800000);
453         &data_word(0x00000080, 0x20820000, 0x00820080, 0x00000000);
454         &data_word(0x20000000, 0x20800080, 0x00020000, 0x00820080);
455         }