Complete the des_encrypt to des_encrypt1 rename in the main
[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
22 &external_label("des_SPtrans");
23 &des_encrypt("des_encrypt1",1);
24 &des_encrypt("des_encrypt2",0);
25 &des_encrypt3("des_encrypt3",1);
26 &des_encrypt3("des_decrypt3",0);
27 &cbc("des_ncbc_encrypt","des_encrypt1","des_encrypt1",0,4,5,3,5,-1);
28 &cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",0,6,7,3,4,5);
29
30 &asm_finish();
31
32 sub des_encrypt
33         {
34         local($name,$do_ip)=@_;
35
36         &function_begin_B($name,"EXTRN   _des_SPtrans:DWORD");
37
38         &push("esi");
39         &push("edi");
40
41         &comment("");
42         &comment("Load the 2 words");
43         $ks="ebp";
44
45         if ($do_ip)
46                 {
47                 &mov($R,&wparam(0));
48                  &xor(  "ecx",          "ecx"           );
49
50                 &push("ebx");
51                 &push("ebp");
52
53                 &mov("eax",&DWP(0,$R,"",0));
54                  &mov("ebx",&wparam(2));        # get encrypt flag
55                 &mov($L,&DWP(4,$R,"",0));
56                 &comment("");
57                 &comment("IP");
58                 &IP_new("eax",$L,$R,3);
59                 }
60         else
61                 {
62                 &mov("eax",&wparam(0));
63                  &xor(  "ecx",          "ecx"           );
64
65                 &push("ebx");
66                 &push("ebp");
67
68                 &mov($R,&DWP(0,"eax","",0));
69                  &mov("ebx",&wparam(2));        # get encrypt flag
70                 &rotl($R,3);
71                 &mov($L,&DWP(4,"eax","",0));
72                 &rotl($L,3);
73                 }
74
75         &mov(   $ks,            &wparam(1)      );
76         &cmp("ebx","0");
77         &je(&label("start_decrypt"));
78
79         for ($i=0; $i<16; $i+=2)
80                 {
81                 &comment("");
82                 &comment("Round $i");
83                 &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
84
85                 &comment("");
86                 &comment("Round ".sprintf("%d",$i+1));
87                 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
88                 }
89         &jmp(&label("end"));
90
91         &set_label("start_decrypt");
92
93         for ($i=15; $i>0; $i-=2)
94                 {
95                 &comment("");
96                 &comment("Round $i");
97                 &D_ENCRYPT(15-$i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
98                 &comment("");
99                 &comment("Round ".sprintf("%d",$i-1));
100                 &D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
101                 }
102
103         &set_label("end");
104
105         if ($do_ip)
106                 {
107                 &comment("");
108                 &comment("FP");
109                 &mov("edx",&wparam(0));
110                 &FP_new($L,$R,"eax",3);
111
112                 &mov(&DWP(0,"edx","",0),"eax");
113                 &mov(&DWP(4,"edx","",0),$R);
114                 }
115         else
116                 {
117                 &comment("");
118                 &comment("Fixup");
119                 &rotr($L,3);            # r
120                  &mov("eax",&wparam(0));
121                 &rotr($R,3);            # l
122                  &mov(&DWP(0,"eax","",0),$L);
123                  &mov(&DWP(4,"eax","",0),$R);
124                 }
125
126         &pop("ebp");
127         &pop("ebx");
128         &pop("edi");
129         &pop("esi");
130         &ret();
131
132         &function_end_B($name);
133         }
134
135 sub D_ENCRYPT
136         {
137         local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_;
138
139          &mov(  $u,             &DWP(&n2a($S*4),$ks,"",0));
140         &xor(   $tmp1,          $tmp1);
141          &mov(  $t,             &DWP(&n2a(($S+1)*4),$ks,"",0));
142         &xor(   $u,             $R);
143          &xor(  $t,             $R);
144         &and(   $u,             "0xfcfcfcfc"    );
145          &and(  $t,             "0xcfcfcfcf"    );
146         &movb(  &LB($tmp1),     &LB($u) );
147          &movb( &LB($tmp2),     &HB($u) );
148         &rotr(  $t,             4               );
149         &mov(   $ks,            &DWP("      $desSP",$tmp1,"",0));
150          &movb( &LB($tmp1),     &LB($t) );
151         &xor(   $L,             $ks);
152          &mov(  $ks,            &DWP("0x200+$desSP",$tmp2,"",0));
153         &xor(   $L,             $ks); ######
154          &movb( &LB($tmp2),     &HB($t) );
155         &shr(   $u,             16);
156          &mov(  $ks,            &DWP("0x100+$desSP",$tmp1,"",0));
157         &xor(   $L,             $ks); ######
158          &movb( &LB($tmp1),     &HB($u) );
159         &shr(   $t,             16);
160          &mov(  $ks,            &DWP("0x300+$desSP",$tmp2,"",0));
161         &xor(   $L,             $ks);
162          &mov(  $ks,            &wparam(1)      );
163         &movb(  &LB($tmp2),     &HB($t) );
164          &and(  $u,             "0xff"  );
165         &and(   $t,             "0xff"  );
166          &mov(  $tmp1,          &DWP("0x600+$desSP",$tmp1,"",0));
167         &xor(   $L,             $tmp1);
168          &mov(  $tmp1,          &DWP("0x700+$desSP",$tmp2,"",0));
169         &xor(   $L,             $tmp1);
170          &mov(  $tmp1,          &DWP("0x400+$desSP",$u,"",0));
171         &xor(   $L,             $tmp1);
172          &mov(  $tmp1,          &DWP("0x500+$desSP",$t,"",0));
173         &xor(   $L,             $tmp1);
174         }
175
176 sub n2a
177         {
178         sprintf("%d",$_[0]);
179         }
180
181 # now has a side affect of rotating $a by $shift
182 sub R_PERM_OP
183         {
184         local($a,$b,$tt,$shift,$mask,$last)=@_;
185
186         &rotl(  $a,             $shift          ) if ($shift != 0);
187         &mov(   $tt,            $a              );
188         &xor(   $a,             $b              );
189         &and(   $a,             $mask           );
190         # This can never succeed, and besides it is difficult to see what the
191         # idea was - Ben 13 Feb 99
192         if (!$last eq $b)
193                 {
194                 &xor(   $b,             $a              );
195                 &xor(   $tt,            $a              );
196                 }
197         else
198                 {
199                 &xor(   $tt,            $a              );
200                 &xor(   $b,             $a              );
201                 }
202         &comment("");
203         }
204
205 sub IP_new
206         {
207         local($l,$r,$tt,$lr)=@_;
208
209         &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);
210         &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l);
211         &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r);
212         &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r);
213         &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);
214         
215         if ($lr != 3)
216                 {
217                 if (($lr-3) < 0)
218                         { &rotr($tt,    3-$lr); }
219                 else    { &rotl($tt,    $lr-3); }
220                 }
221         if ($lr != 2)
222                 {
223                 if (($lr-2) < 0)
224                         { &rotr($r,     2-$lr); }
225                 else    { &rotl($r,     $lr-2); }
226                 }
227         }
228
229 sub FP_new
230         {
231         local($l,$r,$tt,$lr)=@_;
232
233         if ($lr != 2)
234                 {
235                 if (($lr-2) < 0)
236                         { &rotl($r,     2-$lr); }
237                 else    { &rotr($r,     $lr-2); }
238                 }
239         if ($lr != 3)
240                 {
241                 if (($lr-3) < 0)
242                         { &rotl($l,     3-$lr); }
243                 else    { &rotr($l,     $lr-3); }
244                 }
245
246         &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);
247         &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r);
248         &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);
249         &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l);
250         &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r);
251         &rotr($tt       , 4);
252         }
253