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