]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/des/asm/des586.pl
Import of old SSLeay release: SSLeay 0.8.1b
[thirdparty/openssl.git] / crypto / des / asm / des586.pl
1 #!/usr/local/bin/perl
2
3 $prog="des586.pl";
4
5 # base code is in microsft
6 # op dest, source
7 # format.
8 #
9
10 # WILL NOT WORK ANYMORE WITH desboth.pl
11 require "desboth.pl";
12
13 if ( ($ARGV[0] eq "elf"))
14 { require "x86unix.pl"; }
15 elsif ( ($ARGV[0] eq "a.out"))
16 { $aout=1; require "x86unix.pl"; }
17 elsif ( ($ARGV[0] eq "sol"))
18 { $sol=1; require "x86unix.pl"; }
19 elsif ( ($ARGV[0] eq "cpp"))
20 { $cpp=1; require "x86unix.pl"; }
21 elsif ( ($ARGV[0] eq "win32"))
22 { require "x86ms.pl"; }
23 else
24 {
25 print STDERR <<"EOF";
26 Pick one target type from
27 elf - linux, FreeBSD etc
28 a.out - old linux
29 sol - x86 solaris
30 cpp - format so x86unix.cpp can be used
31 win32 - Windows 95/Windows NT
32 EOF
33 exit(1);
34 }
35
36 &comment("Don't even think of reading this code");
37 &comment("It was automatically generated by $prog");
38 &comment("Which is a perl program used to generate the x86 assember for");
39 &comment("any of elf, a.out, Win32, or Solaris");
40 &comment("It can be found in SSLeay 0.6.5+ or in libdes 3.26+");
41 &comment("eric <eay\@cryptsoft.com>");
42 &comment("");
43
44 &file("dx86xxxx");
45
46 $L="edi";
47 $R="esi";
48
49 &des_encrypt("des_encrypt",1);
50 &des_encrypt("des_encrypt2",0);
51
52 &des_encrypt3("des_encrypt3",1);
53 &des_encrypt3("des_decrypt3",0);
54
55 &file_end();
56
57 sub des_encrypt
58 {
59 local($name,$do_ip)=@_;
60
61 &function_begin($name,3,"EXTRN _des_SPtrans:DWORD");
62
63 &comment("");
64 &comment("Load the 2 words");
65 &mov("eax",&wparam(0));
66 &mov($R,&DWP(0,"eax","",0));
67 &mov($L,&DWP(4,"eax","",0));
68
69 if ($do_ip)
70 {
71 &comment("");
72 &comment("IP");
73 &IP($R,$L,"eax");
74 }
75
76 &comment("");
77 &comment("fixup rotate");
78 &rotl($R,3);
79 &rotl($L,3);
80
81 &comment("");
82 &comment("load counter, key_schedule and enc flag");
83
84 # encrypting part
85
86 $ks="ebp";
87 &xor( "ebx", "ebx" );
88 &mov("eax",&wparam(2)); # get encrypt flag
89 &xor( "ecx", "ecx" );
90 &cmp("eax","0");
91 &mov( $ks, &wparam(1) );
92 &je(&label("start_decrypt"));
93
94 for ($i=0; $i<16; $i+=2)
95 {
96 &comment("");
97 &comment("Round $i");
98 &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
99
100 &comment("");
101 &comment("Round ".sprintf("%d",$i+1));
102 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
103 }
104 &jmp(&label("end"));
105
106 &set_label("start_decrypt");
107
108 for ($i=15; $i>0; $i-=2)
109 {
110 &comment("");
111 &comment("Round $i");
112 &D_ENCRYPT(15-$i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
113 &comment("");
114 &comment("Round ".sprintf("%d",$i-1));
115 &D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
116 }
117
118 &set_label("end");
119
120 &comment("");
121 &comment("Fixup");
122 &rotr($L,3); # r
123 &rotr($R,3); # l
124
125 if ($do_ip)
126 {
127 &comment("");
128 &comment("FP");
129 &FP($R,$L,"eax");
130 }
131
132 &mov("eax",&wparam(0));
133 &mov(&DWP(0,"eax","",0),$L);
134 &mov(&DWP(4,"eax","",0),$R);
135
136 &function_end($name);
137 }
138
139 sub D_ENCRYPT
140 {
141 local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_;
142
143 &mov( $t, &DWP(&n2a(($S+1)*4),$ks,"",0));
144 &mov( $u, &DWP(&n2a($S*4),$ks,"",0));
145 &xor( $t, $R);
146 &xor( $u, $R);
147 &rotr( $t, 4 );
148 &and( $u, "0xfcfcfcfc" );
149 &and( $t, "0xfcfcfcfc" );
150 &movb( &LB($tmp1), &LB($u) );
151 &movb( &LB($tmp2), &HB($u) );
152 &xor( $L, &DWP(" $desSP",$tmp1,"",0));
153 &shr( $u, 16);
154 &xor( $L, &DWP("0x200+$desSP",$tmp2,"",0));
155 &movb( &LB($tmp1), &LB($u) );
156 &movb( &LB($tmp2), &HB($u) );
157 &xor( $L, &DWP("0x400+$desSP",$tmp1,"",0));
158 &mov( $u, &DWP("0x600+$desSP",$tmp2,"",0));
159
160 &movb( &LB($tmp1), &LB($t) );
161 &movb( &LB($tmp2), &HB($t) );
162 &xor( $L, &DWP("0x100+$desSP",$tmp1,"",0));
163 &shr( $t, 16);
164 &xor( $u, &DWP("0x300+$desSP",$tmp2,"",0));
165 &movb( &LB($tmp1), &LB($t) );
166 &movb( &LB($tmp2), &HB($t) );
167 &xor( $L, &DWP("0x500+$desSP",$tmp1,"",0));
168 &xor( $u, &DWP("0x700+$desSP",$tmp2,"",0));
169 &xor( $L, $u);
170 }
171
172 sub PERM_OP
173 {
174 local($a,$b,$tt,$shift,$mask)=@_;
175
176 &mov( $tt, $a );
177 &shr( $tt, $shift );
178 &xor( $tt, $b );
179 &and( $tt, $mask );
180 &xor( $b, $tt );
181 &shl( $tt, $shift );
182 &xor( $a, $tt );
183 }
184
185 sub IP
186 {
187 local($l,$r,$tt)=@_;
188
189 &PERM_OP($r,$l,$tt, 4,"0x0f0f0f0f");
190 &PERM_OP($l,$r,$tt,16,"0x0000ffff");
191 &PERM_OP($r,$l,$tt, 2,"0x33333333");
192 &PERM_OP($l,$r,$tt, 8,"0x00ff00ff");
193 &PERM_OP($r,$l,$tt, 1,"0x55555555");
194 }
195
196 sub FP
197 {
198 local($l,$r,$tt)=@_;
199
200 &PERM_OP($l,$r,$tt, 1,"0x55555555");
201 &PERM_OP($r,$l,$tt, 8,"0x00ff00ff");
202 &PERM_OP($l,$r,$tt, 2,"0x33333333");
203 &PERM_OP($r,$l,$tt,16,"0x0000ffff");
204 &PERM_OP($l,$r,$tt, 4,"0x0f0f0f0f");
205 }
206
207 sub n2a
208 {
209 sprintf("%d",$_[0]);
210 }