Unified - adapt the generation of des assembler to use GENERATE
[openssl.git] / crypto / des / asm / crypt586.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 # I've added the stuff needed for crypt() but I've not worried about making
6 # things perfect.
7 #
8
9 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
10 push(@INC,"${dir}","${dir}../../perlasm");
11 require "x86asm.pl";
12
13 $output=pop;
14 open STDOUT,">$output";
15
16 &asm_init($ARGV[0],"crypt586.pl");
17
18 $L="edi";
19 $R="esi";
20
21 &external_label("DES_SPtrans");
22 &fcrypt_body("fcrypt_body");
23 &asm_finish();
24
25 close STDOUT;
26
27 sub fcrypt_body
28         {
29         local($name,$do_ip)=@_;
30
31         &function_begin($name);
32
33         &comment("");
34         &comment("Load the 2 words");
35         $trans="ebp";
36
37         &xor(   $L,     $L);
38         &xor(   $R,     $R);
39
40         # PIC-ification:-)
41         &picmeup("edx","DES_SPtrans");
42         #if ($cpp)      { &picmeup("edx","DES_SPtrans");   }
43         #else           { &lea("edx",&DWP("DES_SPtrans")); }
44         &push("edx");   # becomes &swtmp(1)
45         #
46         &mov($trans,&wparam(1)); # reloaded with DES_SPtrans in D_ENCRYPT
47
48         &push(&DWC(25)); # add a variable
49
50         &set_label("start");
51         for ($i=0; $i<16; $i+=2)
52                 {
53                 &comment("");
54                 &comment("Round $i");
55                 &D_ENCRYPT($i,$L,$R,$i*2,$trans,"eax","ebx","ecx","edx");
56
57                 &comment("");
58                 &comment("Round ".sprintf("%d",$i+1));
59                 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$trans,"eax","ebx","ecx","edx");
60                 }
61          &mov("ebx",    &swtmp(0));
62         &mov("eax",     $L);
63          &dec("ebx");
64         &mov($L,        $R);
65          &mov($R,       "eax");
66         &mov(&swtmp(0), "ebx");
67          &jnz(&label("start"));
68
69         &comment("");
70         &comment("FP");
71         &mov("edx",&wparam(0));
72
73         &FP_new($R,$L,"eax",3);
74         &mov(&DWP(0,"edx","",0),"eax");
75         &mov(&DWP(4,"edx","",0),$L);
76
77         &add("esp",8);  # remove variables
78
79         &function_end($name);
80         }
81
82 sub D_ENCRYPT
83         {
84         local($r,$L,$R,$S,$trans,$u,$tmp1,$tmp2,$t)=@_;
85
86         &mov(   $u,             &wparam(2));                    # 2
87         &mov(   $t,             $R);
88         &shr(   $t,             16);                            # 1
89         &mov(   $tmp2,          &wparam(3));                    # 2
90         &xor(   $t,             $R);                            # 1
91
92         &and(   $u,             $t);                            # 2
93         &and(   $t,             $tmp2);                         # 2
94
95         &mov(   $tmp1,          $u);
96         &shl(   $tmp1,          16);                            # 1
97         &mov(   $tmp2,          $t);
98         &shl(   $tmp2,          16);                            # 1
99         &xor(   $u,             $tmp1);                         # 2
100         &xor(   $t,             $tmp2);                         # 2
101         &mov(   $tmp1,          &DWP(&n2a($S*4),$trans,"",0));  # 2
102         &xor(   $u,             $tmp1);
103         &mov(   $tmp2,          &DWP(&n2a(($S+1)*4),$trans,"",0));      # 2
104         &xor(   $u,             $R);
105         &xor(   $t,             $R);
106         &xor(   $t,             $tmp2);
107
108         &and(   $u,             "0xfcfcfcfc"    );              # 2
109         &xor(   $tmp1,          $tmp1);                         # 1
110         &and(   $t,             "0xcfcfcfcf"    );              # 2
111         &xor(   $tmp2,          $tmp2); 
112         &movb(  &LB($tmp1),     &LB($u) );
113         &movb(  &LB($tmp2),     &HB($u) );
114         &rotr(  $t,             4               );
115         &mov(   $trans,         &swtmp(1));
116         &xor(   $L,             &DWP("     ",$trans,$tmp1,0));
117         &movb(  &LB($tmp1),     &LB($t) );
118         &xor(   $L,             &DWP("0x200",$trans,$tmp2,0));
119         &movb(  &LB($tmp2),     &HB($t) );
120         &shr(   $u,             16);
121         &xor(   $L,             &DWP("0x100",$trans,$tmp1,0));
122         &movb(  &LB($tmp1),     &HB($u) );
123         &shr(   $t,             16);
124         &xor(   $L,             &DWP("0x300",$trans,$tmp2,0));
125         &movb(  &LB($tmp2),     &HB($t) );
126         &and(   $u,             "0xff"  );
127         &and(   $t,             "0xff"  );
128         &mov(   $tmp1,          &DWP("0x600",$trans,$tmp1,0));
129         &xor(   $L,             $tmp1);
130         &mov(   $tmp1,          &DWP("0x700",$trans,$tmp2,0));
131         &xor(   $L,             $tmp1);
132         &mov(   $tmp1,          &DWP("0x400",$trans,$u,0));
133         &xor(   $L,             $tmp1);
134         &mov(   $tmp1,          &DWP("0x500",$trans,$t,0));
135         &xor(   $L,             $tmp1);
136         &mov(   $trans,         &wparam(1));
137         }
138
139 sub n2a
140         {
141         sprintf("%d",$_[0]);
142         }
143
144 # now has a side affect of rotating $a by $shift
145 sub R_PERM_OP
146         {
147         local($a,$b,$tt,$shift,$mask,$last)=@_;
148
149         &rotl(  $a,             $shift          ) if ($shift != 0);
150         &mov(   $tt,            $a              );
151         &xor(   $a,             $b              );
152         &and(   $a,             $mask           );
153         if ($notlast eq $b)
154                 {
155                 &xor(   $b,             $a              );
156                 &xor(   $tt,            $a              );
157                 }
158         else
159                 {
160                 &xor(   $tt,            $a              );
161                 &xor(   $b,             $a              );
162                 }
163         &comment("");
164         }
165
166 sub IP_new
167         {
168         local($l,$r,$tt,$lr)=@_;
169
170         &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);
171         &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l);
172         &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r);
173         &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r);
174         &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);
175         
176         if ($lr != 3)
177                 {
178                 if (($lr-3) < 0)
179                         { &rotr($tt,    3-$lr); }
180                 else    { &rotl($tt,    $lr-3); }
181                 }
182         if ($lr != 2)
183                 {
184                 if (($lr-2) < 0)
185                         { &rotr($r,     2-$lr); }
186                 else    { &rotl($r,     $lr-2); }
187                 }
188         }
189
190 sub FP_new
191         {
192         local($l,$r,$tt,$lr)=@_;
193
194         if ($lr != 2)
195                 {
196                 if (($lr-2) < 0)
197                         { &rotl($r,     2-$lr); }
198                 else    { &rotr($r,     $lr-2); }
199                 }
200         if ($lr != 3)
201                 {
202                 if (($lr-3) < 0)
203                         { &rotl($l,     3-$lr); }
204                 else    { &rotr($l,     $lr-3); }
205                 }
206
207         &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);
208         &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r);
209         &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);
210         &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l);
211         &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r);
212         &rotr($tt       , 4);
213         }
214