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