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