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