]>
Commit | Line | Data |
---|---|---|
997358a6 MW |
1 | #!/usr/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 | push(@INC,"perlasm","../../perlasm"); | |
8 | require "x86asm.pl"; | |
9 | require "cbc.pl"; | |
10 | require "desboth.pl"; | |
11 | ||
12 | # base code is in microsft | |
13 | # op dest, source | |
14 | # format. | |
15 | # | |
16 | ||
17 | &asm_init($ARGV[0],"des-586.pl"); | |
18 | ||
19 | $L="edi"; | |
20 | $R="esi"; | |
21 | ||
22 | &external_label("des_SPtrans"); | |
23 | &des_encrypt("des_encrypt",1); | |
24 | &des_encrypt("des_encrypt2",0); | |
25 | &des_encrypt3("des_encrypt3",1); | |
26 | &des_encrypt3("des_decrypt3",0); | |
27 | &cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",0,4,5,3,5,-1); | |
28 | &cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",0,6,7,3,4,5); | |
29 | ||
30 | &asm_finish(); | |
31 | ||
32 | sub des_encrypt | |
33 | { | |
34 | local($name,$do_ip)=@_; | |
35 | ||
36 | &function_begin_B($name,"EXTRN _des_SPtrans:DWORD"); | |
37 | ||
38 | &push("esi"); | |
39 | &push("edi"); | |
40 | ||
41 | &comment(""); | |
42 | &comment("Load the 2 words"); | |
43 | $ks="ebp"; | |
44 | ||
45 | if ($do_ip) | |
46 | { | |
47 | &mov($R,&wparam(0)); | |
48 | &xor( "ecx", "ecx" ); | |
49 | ||
50 | &push("ebx"); | |
51 | &push("ebp"); | |
52 | ||
53 | &mov("eax",&DWP(0,$R,"",0)); | |
54 | &mov("ebx",&wparam(2)); # get encrypt flag | |
55 | &mov($L,&DWP(4,$R,"",0)); | |
56 | &comment(""); | |
57 | &comment("IP"); | |
58 | &IP_new("eax",$L,$R,3); | |
59 | } | |
60 | else | |
61 | { | |
62 | &mov("eax",&wparam(0)); | |
63 | &xor( "ecx", "ecx" ); | |
64 | ||
65 | &push("ebx"); | |
66 | &push("ebp"); | |
67 | ||
68 | &mov($R,&DWP(0,"eax","",0)); | |
69 | &mov("ebx",&wparam(2)); # get encrypt flag | |
70 | &rotl($R,3); | |
71 | &mov($L,&DWP(4,"eax","",0)); | |
72 | &rotl($L,3); | |
73 | } | |
74 | ||
75 | &mov( $ks, &wparam(1) ); | |
76 | &cmp("ebx","0"); | |
77 | &je(&label("start_decrypt")); | |
78 | ||
79 | for ($i=0; $i<16; $i+=2) | |
80 | { | |
81 | &comment(""); | |
82 | &comment("Round $i"); | |
83 | &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx"); | |
84 | ||
85 | &comment(""); | |
86 | &comment("Round ".sprintf("%d",$i+1)); | |
87 | &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx"); | |
88 | } | |
89 | &jmp(&label("end")); | |
90 | ||
91 | &set_label("start_decrypt"); | |
92 | ||
93 | for ($i=15; $i>0; $i-=2) | |
94 | { | |
95 | &comment(""); | |
96 | &comment("Round $i"); | |
97 | &D_ENCRYPT(15-$i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx"); | |
98 | &comment(""); | |
99 | &comment("Round ".sprintf("%d",$i-1)); | |
100 | &D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx"); | |
101 | } | |
102 | ||
103 | &set_label("end"); | |
104 | ||
105 | if ($do_ip) | |
106 | { | |
107 | &comment(""); | |
108 | &comment("FP"); | |
109 | &mov("edx",&wparam(0)); | |
110 | &FP_new($L,$R,"eax",3); | |
111 | ||
112 | &mov(&DWP(0,"edx","",0),"eax"); | |
113 | &mov(&DWP(4,"edx","",0),$R); | |
114 | } | |
115 | else | |
116 | { | |
117 | &comment(""); | |
118 | &comment("Fixup"); | |
119 | &rotr($L,3); # r | |
120 | &mov("eax",&wparam(0)); | |
121 | &rotr($R,3); # l | |
122 | &mov(&DWP(0,"eax","",0),$L); | |
123 | &mov(&DWP(4,"eax","",0),$R); | |
124 | } | |
125 | ||
126 | &pop("ebp"); | |
127 | &pop("ebx"); | |
128 | &pop("edi"); | |
129 | &pop("esi"); | |
130 | &ret(); | |
131 | ||
132 | &function_end_B($name); | |
133 | } | |
134 | ||
135 | sub D_ENCRYPT | |
136 | { | |
137 | local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_; | |
138 | ||
139 | &mov( $u, &DWP(&n2a($S*4),$ks,"",0)); | |
140 | &xor( $tmp1, $tmp1); | |
141 | &mov( $t, &DWP(&n2a(($S+1)*4),$ks,"",0)); | |
142 | &xor( $u, $R); | |
143 | &xor( $t, $R); | |
144 | &and( $u, "0xfcfcfcfc" ); | |
145 | &and( $t, "0xcfcfcfcf" ); | |
146 | &movb( &LB($tmp1), &LB($u) ); | |
147 | &movb( &LB($tmp2), &HB($u) ); | |
148 | &rotr( $t, 4 ); | |
149 | &mov( $ks, &DWP(" $desSP",$tmp1,"",0)); | |
150 | &movb( &LB($tmp1), &LB($t) ); | |
151 | &xor( $L, $ks); | |
152 | &mov( $ks, &DWP("0x200+$desSP",$tmp2,"",0)); | |
153 | &xor( $L, $ks); ###### | |
154 | &movb( &LB($tmp2), &HB($t) ); | |
155 | &shr( $u, 16); | |
156 | &mov( $ks, &DWP("0x100+$desSP",$tmp1,"",0)); | |
157 | &xor( $L, $ks); ###### | |
158 | &movb( &LB($tmp1), &HB($u) ); | |
159 | &shr( $t, 16); | |
160 | &mov( $ks, &DWP("0x300+$desSP",$tmp2,"",0)); | |
161 | &xor( $L, $ks); | |
162 | &mov( $ks, &wparam(1) ); | |
163 | &movb( &LB($tmp2), &HB($t) ); | |
164 | &and( $u, "0xff" ); | |
165 | &and( $t, "0xff" ); | |
166 | &mov( $tmp1, &DWP("0x600+$desSP",$tmp1,"",0)); | |
167 | &xor( $L, $tmp1); | |
168 | &mov( $tmp1, &DWP("0x700+$desSP",$tmp2,"",0)); | |
169 | &xor( $L, $tmp1); | |
170 | &mov( $tmp1, &DWP("0x400+$desSP",$u,"",0)); | |
171 | &xor( $L, $tmp1); | |
172 | &mov( $tmp1, &DWP("0x500+$desSP",$t,"",0)); | |
173 | &xor( $L, $tmp1); | |
174 | } | |
175 | ||
176 | sub n2a | |
177 | { | |
178 | sprintf("%d",$_[0]); | |
179 | } | |
180 | ||
181 | # now has a side affect of rotating $a by $shift | |
182 | sub R_PERM_OP | |
183 | { | |
184 | local($a,$b,$tt,$shift,$mask,$last)=@_; | |
185 | ||
186 | &rotl( $a, $shift ) if ($shift != 0); | |
187 | &mov( $tt, $a ); | |
188 | &xor( $a, $b ); | |
189 | &and( $a, $mask ); | |
190 | if (!$last eq $b) | |
191 | { | |
192 | &xor( $b, $a ); | |
193 | &xor( $tt, $a ); | |
194 | } | |
195 | else | |
196 | { | |
197 | &xor( $tt, $a ); | |
198 | &xor( $b, $a ); | |
199 | } | |
200 | &comment(""); | |
201 | } | |
202 | ||
203 | sub IP_new | |
204 | { | |
205 | local($l,$r,$tt,$lr)=@_; | |
206 | ||
207 | &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l); | |
208 | &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l); | |
209 | &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r); | |
210 | &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r); | |
211 | &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r); | |
212 | ||
213 | if ($lr != 3) | |
214 | { | |
215 | if (($lr-3) < 0) | |
216 | { &rotr($tt, 3-$lr); } | |
217 | else { &rotl($tt, $lr-3); } | |
218 | } | |
219 | if ($lr != 2) | |
220 | { | |
221 | if (($lr-2) < 0) | |
222 | { &rotr($r, 2-$lr); } | |
223 | else { &rotl($r, $lr-2); } | |
224 | } | |
225 | } | |
226 | ||
227 | sub FP_new | |
228 | { | |
229 | local($l,$r,$tt,$lr)=@_; | |
230 | ||
231 | if ($lr != 2) | |
232 | { | |
233 | if (($lr-2) < 0) | |
234 | { &rotl($r, 2-$lr); } | |
235 | else { &rotr($r, $lr-2); } | |
236 | } | |
237 | if ($lr != 3) | |
238 | { | |
239 | if (($lr-3) < 0) | |
240 | { &rotl($l, 3-$lr); } | |
241 | else { &rotr($l, $lr-3); } | |
242 | } | |
243 | ||
244 | &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r); | |
245 | &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r); | |
246 | &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l); | |
247 | &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l); | |
248 | &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r); | |
249 | &rotr($tt , 4); | |
250 | } | |
251 |