Pointer to Ariel Glenn's SSLeay documentation.
[openssl.git] / crypto / des / asm / des-som3.pl
1 #!/usr/local/bin/perl
2 #\r
3 # The inner loop instruction sequence and the IP/FP modifications are from\r
4 # Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>\r
5 #\r
6 \r
7 $prog="des-som3.pl";\r
8 \r
9 # base code is in microsft\r
10 # op dest, source\r
11 # format.\r
12 #\r
13 \r
14 require "desboth.pl";\r
15 \r
16 if (    ($ARGV[0] eq "elf"))\r
17         { require "x86unix.pl"; }\r
18 elsif ( ($ARGV[0] eq "a.out"))\r
19         { $aout=1; require "x86unix.pl"; }\r
20 elsif ( ($ARGV[0] eq "sol"))\r
21         { $sol=1; require "x86unix.pl"; }\r
22 elsif ( ($ARGV[0] eq "cpp"))\r
23         { $cpp=1; require "x86unix.pl"; }\r
24 elsif ( ($ARGV[0] eq "win32"))\r
25         { require "x86ms.pl"; }\r
26 else\r
27         {\r
28         print STDERR <<"EOF";\r
29 Pick one target type from\r
30         elf     - linux, FreeBSD etc\r
31         a.out   - old linux\r
32         sol     - x86 solaris\r
33         cpp     - format so x86unix.cpp can be used\r
34         win32   - Windows 95/Windows NT\r
35 EOF\r
36         exit(1);\r
37         }\r
38 \r
39 &comment("Don't even think of reading this code");\r
40 &comment("It was automatically generated by $prog");\r
41 &comment("Which is a perl program used to generate the x86 assember for");\r
42 &comment("any of elf, a.out, Win32, or Solaris");\r
43 &comment("It can be found in SSLeay 0.6.5+ or in libdes 3.26+");\r
44 &comment("eric <eay\@cryptsoft.com>");\r
45 &comment("The inner loop instruction sequence and the IP/FP modifications");\r
46 &comment("are from Svend Olaf Mikkelsen <svolaf\@inet.uni-c.dk>");\r
47 \r
48 &comment("");\r
49 \r
50 &file("dx86xxxx");\r
51 \r
52 $L="edi";\r
53 $R="esi";\r
54 \r
55 &des_encrypt("des_encrypt",1);\r
56 &des_encrypt("des_encrypt2",0);\r
57 \r
58 &des_encrypt3("des_encrypt3",1);\r
59 &des_encrypt3("des_decrypt3",0);\r
60 \r
61 &file_end();\r
62 \r
63 sub des_encrypt\r
64         {\r
65         local($name,$do_ip)=@_;\r
66 \r
67         &function_begin($name,3,EXTRN   _des_SPtrans:DWORD");\r
68 \r
69         &comment("");\r
70         &comment("Load the 2 words");\r
71         $ks="ebp";\r
72 \r
73         if ($do_ip)\r
74                 {\r
75                 &mov($R,&wparam(0));\r
76                  &xor(  "ecx",          "ecx"           );\r
77                 &mov("eax",&DWP(0,$R,"",0));\r
78                  &mov("ebx",&wparam(2));        # get encrypt flag\r
79                 &mov($L,&DWP(4,$R,"",0));\r
80                 &comment("");\r
81                 &comment("IP");\r
82                 &IP_new("eax",$L,$R,3);\r
83                 }\r
84         else\r
85                 {\r
86                 &mov("eax",&wparam(0));\r
87                  &xor(  "ecx",          "ecx"           );\r
88                 &mov($R,&DWP(0,"eax","",0));\r
89                  &mov("ebx",&wparam(2));        # get encrypt flag\r
90                 &rotl($R,3);\r
91                 &mov($L,&DWP(4,"eax","",0));\r
92                 &rotl($L,3);\r
93                 }\r
94 \r
95         &cmp("ebx","0");\r
96         &mov(   $ks,            &wparam(1)      );\r
97         &je(&label("start_decrypt"));\r
98 \r
99         for ($i=0; $i<16; $i+=2)\r
100                 {\r
101                 &comment("");\r
102                 &comment("Round $i");\r
103                 &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");\r
104 \r
105                 &comment("");\r
106                 &comment("Round ".sprintf("%d",$i+1));\r
107                 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");\r
108                 }\r
109         &jmp(&label("end"));\r
110 \r
111         &set_label("start_decrypt");\r
112 \r
113         for ($i=15; $i>0; $i-=2)\r
114                 {\r
115                 &comment("");\r
116                 &comment("Round $i");\r
117                 &D_ENCRYPT(15-$i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");\r
118                 &comment("");\r
119                 &comment("Round ".sprintf("%d",$i-1));\r
120                 &D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");\r
121                 }\r
122 \r
123         &set_label("end");\r
124 \r
125         if ($do_ip)\r
126                 {\r
127                 &comment("");\r
128                 &comment("FP");\r
129                 &mov("edx",&wparam(0));\r
130                 &FP_new($L,$R,"eax",3);\r
131 \r
132                 &mov(&DWP(0,"edx","",0),"eax");\r
133                 &mov(&DWP(4,"edx","",0),$R);\r
134                 }\r
135         else\r
136                 {\r
137                 &comment("");\r
138                 &comment("Fixup");\r
139                 &rotr($L,3);            # r\r
140                  &mov("eax",&wparam(0));\r
141                 &rotr($R,3);            # l\r
142                  &mov(&DWP(0,"eax","",0),$L);\r
143                  &mov(&DWP(4,"eax","",0),$R);\r
144                 }\r
145 \r
146 \r
147         &function_end($name);\r
148         }\r
149 \r
150 sub D_ENCRYPT\r
151         {\r
152         local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_;\r
153 \r
154         &mov(   $u,             &DWP(&n2a($S*4),$ks,"",0));\r
155         &xor(   $tmp1,          $tmp1);\r
156         &mov(   $t,             &DWP(&n2a(($S+1)*4),$ks,"",0));\r
157         &xor(   $u,             $R);\r
158         &xor(   $t,             $R);\r
159         &and(   $u,             "0xfcfcfcfc"    );\r
160         &and(   $t,             "0xcfcfcfcf"    );\r
161         &movb(  &LB($tmp1),     &LB($u) );\r
162         &movb(  &LB($tmp2),     &HB($u) );\r
163         &rotr(  $t,             4               );\r
164         &mov(   $ks,            &DWP("      $desSP",$tmp1,"",0));\r
165         &movb(  &LB($tmp1),     &LB($t) );\r
166         &xor(   $L,             $ks);\r
167         &mov(   $ks,            &DWP("0x200+$desSP",$tmp2,"",0));\r
168         &xor(   $L,             $ks); ######\r
169         &movb(  &LB($tmp2),     &HB($t) );\r
170         &shr(   $u,             16);\r
171         &mov(   $ks,            &DWP("0x100+$desSP",$tmp1,"",0));\r
172         &xor(   $L,             $ks); ######\r
173         &movb(  &LB($tmp1),     &HB($u) );\r
174         &shr(   $t,             16);\r
175         &mov(   $ks,            &DWP("0x300+$desSP",$tmp2,"",0));\r
176         &xor(   $L,             $ks);\r
177         &mov(   $ks,            &DWP(24,"esp","",0)); ####\r
178         &movb(  &LB($tmp2),     &HB($t) );\r
179         &and(   $u,             "0xff"  );\r
180         &and(   $t,             "0xff"  );\r
181         &mov(   $tmp1,          &DWP("0x600+$desSP",$tmp1,"",0));\r
182         &xor(   $L,             $tmp1);\r
183         &mov(   $tmp1,          &DWP("0x700+$desSP",$tmp2,"",0));\r
184         &xor(   $L,             $tmp1);\r
185         &mov(   $tmp1,          &DWP("0x400+$desSP",$u,"",0));\r
186         &xor(   $L,             $tmp1);\r
187         &mov(   $tmp1,          &DWP("0x500+$desSP",$t,"",0));\r
188         &xor(   $L,             $tmp1);\r
189         }\r
190 \r
191 sub n2a\r
192         {\r
193         sprintf("%d",$_[0]);\r
194         }\r
195 \r
196 # now has a side affect of rotating $a by $shift\r
197 sub R_PERM_OP\r
198         {\r
199         local($a,$b,$tt,$shift,$mask,$last)=@_;\r
200 \r
201         &rotl(  $a,             $shift          ) if ($shift != 0);\r
202         &mov(   $tt,            $a              );\r
203         &xor(   $a,             $b              );\r
204         &and(   $a,             $mask           );\r
205         if ($notlast eq $b)\r
206                 {\r
207                 &xor(   $b,             $a              );\r
208                 &xor(   $tt,            $a              );\r
209                 }\r
210         else\r
211                 {\r
212                 &xor(   $tt,            $a              );\r
213                 &xor(   $b,             $a              );\r
214                 }\r
215         &comment("");\r
216         }\r
217 \r
218 sub IP_new\r
219         {\r
220         local($l,$r,$tt,$lr)=@_;\r
221 \r
222         &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);\r
223         &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l);\r
224         &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r);\r
225         &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r);\r
226         &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);\r
227         \r
228         if ($lr != 3)\r
229                 {\r
230                 if (($lr-3) < 0)\r
231                         { &rotr($tt,    3-$lr); }\r
232                 else    { &rotl($tt,    $lr-3); }\r
233                 }\r
234         if ($lr != 2)\r
235                 {\r
236                 if (($lr-2) < 0)\r
237                         { &rotr($r,     2-$lr); }\r
238                 else    { &rotl($r,     $lr-2); }\r
239                 }\r
240         }\r
241 \r
242 sub FP_new\r
243         {\r
244         local($l,$r,$tt,$lr)=@_;\r
245 \r
246         if ($lr != 2)\r
247                 {\r
248                 if (($lr-2) < 0)\r
249                         { &rotl($r,     2-$lr); }\r
250                 else    { &rotr($r,     $lr-2); }\r
251                 }\r
252         if ($lr != 3)\r
253                 {\r
254                 if (($lr-3) < 0)\r
255                         { &rotl($l,     3-$lr); }\r
256                 else    { &rotr($l,     $lr-3); }\r
257                 }\r
258 \r
259         &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);\r
260         &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r);\r
261         &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);\r
262         &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l);\r
263         &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r);\r
264         &rotr($tt       , 4);\r
265         }\r
266 \r