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