Import of old SSLeay release: SSLeay 0.8.1b
[openssl.git] / crypto / bf / asm / bf586.pl
1 #!/usr/local/bin/perl
2 #!/usr/local/bin/perl\r
3 \r
4 $prog="bf586.pl";\r
5 \r
6 # base code is in microsft\r
7 # op dest, source\r
8 # format.\r
9 #\r
10 \r
11 if (    ($ARGV[0] eq "elf"))\r
12         { require "x86unix.pl"; }\r
13 elsif ( ($ARGV[0] eq "a.out"))\r
14         { $aout=1; require "x86unix.pl"; }\r
15 elsif ( ($ARGV[0] eq "sol"))\r
16         { $sol=1; require "x86unix.pl"; }\r
17 elsif ( ($ARGV[0] eq "cpp"))\r
18         { $cpp=1; require "x86unix.pl"; }\r
19 elsif ( ($ARGV[0] eq "win32"))\r
20         { require "x86ms.pl"; }\r
21 else\r
22         {\r
23         print STDERR <<"EOF";\r
24 Pick one target type from\r
25         elf     - linux, FreeBSD etc\r
26         a.out   - old linux\r
27         sol     - x86 solaris\r
28         cpp     - format so x86unix.cpp can be used\r
29         win32   - Windows 95/Windows NT\r
30 EOF\r
31         exit(1);\r
32         }\r
33 \r
34 &comment("Don't even think of reading this code");\r
35 &comment("It was automatically generated by $prog");\r
36 &comment("Which is a perl program used to generate the x86 assember for");\r
37 &comment("any of elf, a.out, Win32, or Solaris");\r
38 &comment("It can be found in SSLeay 0.7.0+");\r
39 &comment("eric <eay\@cryptsoft.com>");\r
40 &comment("");\r
41 \r
42 &file("bfx86xxxx");\r
43 \r
44 $BF_ROUNDS=16;\r
45 $BF_OFF=($BF_ROUNDS+2)*4;\r
46 $L="ecx";\r
47 $R="edx";\r
48 $P="edi";\r
49 $tot="esi";\r
50 $tmp1="eax";\r
51 $tmp2="ebx";\r
52 $tmp3="ebp";\r
53 \r
54 &des_encrypt("BF_encrypt");\r
55 \r
56 &file_end();\r
57 \r
58 sub des_encrypt\r
59         {\r
60         local($name)=@_;\r
61 \r
62         &function_begin($name,3,"");\r
63 \r
64         &comment("");\r
65         &comment("Load the 2 words");\r
66         &mov("eax",&wparam(0));\r
67         &mov($L,&DWP(0,"eax","",0));\r
68         &mov($R,&DWP(4,"eax","",0));\r
69 \r
70         &comment("");\r
71         &comment("P pointer, s and enc flag");\r
72         &mov($P,&wparam(1));\r
73 \r
74         &xor(   $tmp1,  $tmp1);\r
75         &xor(   $tmp2,  $tmp2);\r
76 \r
77         # encrypting part\r
78 \r
79         &mov("ebp",&wparam(2)); # get encrypt flag\r
80         &cmp("ebp","0");\r
81         &je(&label("start_decrypt"));\r
82 \r
83         &xor($L,&DWP(0,$P,"",0));\r
84         for ($i=0; $i<$BF_ROUNDS; $i+=2)\r
85                 {\r
86                 &comment("");\r
87                 &comment("Round $i");\r
88                 &BF_ENCRYPT($i+1,$R,$L,$P,$tot,$tmp1,$tmp2,$tmp3);\r
89 \r
90                 &comment("");\r
91                 &comment("Round ".sprintf("%d",$i+1));\r
92                 &BF_ENCRYPT($i+2,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3);\r
93                 }\r
94         &xor($R,&DWP(($BF_ROUNDS+1)*4,$P,"",0));\r
95 \r
96         &mov("eax",&wparam(0));\r
97         &mov(&DWP(0,"eax","",0),$R);\r
98         &mov(&DWP(4,"eax","",0),$L);\r
99         &function_end_A($name);\r
100 \r
101         &set_label("start_decrypt");\r
102 \r
103         &xor($L,&DWP(($BF_ROUNDS+1)*4,$P,"",0));\r
104         for ($i=$BF_ROUNDS; $i>0; $i-=2)\r
105                 {\r
106                 &comment("");\r
107                 &comment("Round $i");\r
108                 &BF_ENCRYPT($i,$R,$L,$P,$tot,$tmp1,$tmp2,$tmp3);\r
109                 &comment("");\r
110                 &comment("Round ".sprintf("%d",$i-1));\r
111                 &BF_ENCRYPT($i-1,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3);\r
112                 }\r
113         &xor($R,&DWP(0,$P,"",0));\r
114 \r
115         &mov("eax",&wparam(0));\r
116         &mov(&DWP(0,"eax","",0),$R);\r
117         &mov(&DWP(4,"eax","",0),$L);\r
118         &function_end_A($name);\r
119 \r
120         &function_end_B($name);\r
121         }\r
122 \r
123 sub BF_ENCRYPT\r
124         {\r
125         local($i,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3)=@_;\r
126 \r
127         &rotr(  $R,             16);\r
128         &mov(   $tot,           &DWP(&n2a($i*4),$P,"",0));\r
129 \r
130         &movb(  &LB($tmp1),     &HB($R));\r
131         &movb(  &LB($tmp2),     &LB($R));\r
132 \r
133         &rotr(  $R,             16);\r
134         &xor(   $L,             $tot);\r
135 \r
136         &mov(   $tot,           &DWP(&n2a($BF_OFF+0x0000),$P,$tmp1,4));\r
137         &mov(   $tmp3,          &DWP(&n2a($BF_OFF+0x0400),$P,$tmp2,4));\r
138 \r
139         &movb(  &LB($tmp1),     &HB($R));\r
140         &movb(  &LB($tmp2),     &LB($R));\r
141 \r
142         &add(   $tot,           $tmp3);\r
143         &mov(   $tmp1,          &DWP(&n2a($BF_OFF+0x0800),$P,$tmp1,4)); # delay\r
144 \r
145         &xor(   $tot,           $tmp1);\r
146         &mov(   $tmp3,          &DWP(&n2a($BF_OFF+0x0C00),$P,$tmp2,4));\r
147 \r
148         &add(   $tot,           $tmp3);\r
149         &xor(   $tmp1,          $tmp1);\r
150 \r
151         &xor(   $L,             $tot);                                  \r
152         # delay\r
153         }\r
154 \r
155 sub n2a\r
156         {\r
157         sprintf("%d",$_[0]);\r
158         }\r
159 \r