3 # void des_ncbc_encrypt(input, output, length, schedule, ivec, enc)
5 # des_cblock (*output);
7 # des_key_schedule schedule;
12 # des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
15 #&cbc("des_ncbc_encrypt","des_encrypt",0);
16 #&cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt",
18 #&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",
20 #&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",
23 # When doing a cipher that needs bigendian order,
24 # for encrypt, the iv is kept in bigendian form,
25 # while for decrypt, it is kept in little endian.
28 local($name,$enc_func,$dec_func,$swap,$iv_off,$enc_off,$p1,$p2,$p3)=@_;
29 # name is the function name
30 # enc_func and dec_func and the functions to call for encrypt/decrypt
31 # swap is true if byte order needs to be reversed
32 # iv_off is parameter number for the iv
33 # enc_off is parameter number for the encrypt/decrypt flag
34 # p1,p2,p3 are the offsets for parameters to be passed to the
37 &function_begin_B($name,"");
50 $data_off+=4 if ($p1 > 0);
51 $data_off+=4 if ($p2 > 0);
52 $data_off+=4 if ($p3 > 0);
54 &mov($count, &wparam(2)); # length
56 &comment("getting iv ptr from parameter $iv_off");
57 &mov("ebx", &wparam($iv_off)); # Get iv ptr
59 &mov($in, &DWP(0,"ebx","",0));# iv[0]
60 &mov($out, &DWP(4,"ebx","",0));# iv[1]
64 &push($out); # used in decrypt for iv[1]
65 &push($in); # used in decrypt for iv[0]
67 &mov("ebx", "esp"); # This is the address of tin[2]
69 &mov($in, &wparam(0)); # in
70 &mov($out, &wparam(1)); # out
72 # We have loaded them all, how lets push things
73 &comment("getting encrypt flag from parameter $enc_off");
74 &mov("ecx", &wparam($enc_off)); # Get enc flag
77 &comment("get and push parameter $p3");
79 { &mov("eax", &wparam($p3)); &push("eax"); }
80 else { &push("ecx"); }
84 &comment("get and push parameter $p2");
86 { &mov("eax", &wparam($p2)); &push("eax"); }
87 else { &push("ecx"); }
91 &comment("get and push parameter $p1");
93 { &mov("eax", &wparam($p1)); &push("eax"); }
94 else { &push("ecx"); }
96 &push("ebx"); # push data/iv
99 &jz(&label("decrypt"));
101 &and($count,0xfffffff8);
102 &mov("eax", &DWP($data_off,"esp","",0)); # load iv[0]
103 &mov("ebx", &DWP($data_off+4,"esp","",0)); # load iv[1]
105 &jz(&label("encrypt_finish"));
107 #############################################################
109 &set_label("encrypt_loop");
111 # "eax" and "ebx" hold iv (or the last cipher text)
113 &mov("ecx", &DWP(0,$in,"",0)); # load first 4 bytes
114 &mov("edx", &DWP(4,$in,"",0)); # second 4 bytes
119 &bswap("eax") if $swap;
120 &bswap("ebx") if $swap;
122 &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call
123 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
127 &mov("eax", &DWP($data_off,"esp","",0));
128 &mov("ebx", &DWP($data_off+4,"esp","",0));
130 &bswap("eax") if $swap;
131 &bswap("ebx") if $swap;
133 &mov(&DWP(0,$out,"",0),"eax");
134 &mov(&DWP(4,$out,"",0),"ebx");
136 # eax and ebx are the next iv.
142 &jnz(&label("encrypt_loop"));
144 ###################################################################3
145 &set_label("encrypt_finish");
146 &mov($count, &wparam(2)); # length
148 &jz(&label("finish"));
151 &mov($count,&DWP(&label("cbc_enc_jmp_table"),"",$count,4));
155 &xor("edx", "edx") if $ppro; # ppro friendly
156 &movb(&HB("edx"), &BP(6,$in,"",0));
159 &movb(&HB("edx"), &BP(5,$in,"",0));
161 &movb(&LB("edx"), &BP(4,$in,"",0));
163 &mov("ecx", &DWP(0,$in,"",0));
164 &jmp(&label("ejend"));
166 &movb(&HB("ecx"), &BP(2,$in,"",0));
167 &xor("ecx", "ecx") if $ppro; # ppro friendly
170 &movb(&HB("ecx"), &BP(1,$in,"",0));
172 &movb(&LB("ecx"), &BP(0,$in,"",0));
178 &bswap("eax") if $swap;
179 &bswap("ebx") if $swap;
181 &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call
182 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
186 &mov("eax", &DWP($data_off,"esp","",0));
187 &mov("ebx", &DWP($data_off+4,"esp","",0));
189 &bswap("eax") if $swap;
190 &bswap("ebx") if $swap;
192 &mov(&DWP(0,$out,"",0),"eax");
193 &mov(&DWP(4,$out,"",0),"ebx");
195 &jmp(&label("finish"));
197 #############################################################
198 #############################################################
199 &set_label("decrypt",1);
201 &and($count,0xfffffff8);
202 # The next 2 instructions are only for if the jz is taken
203 &mov("eax", &DWP($data_off+8,"esp","",0)); # get iv[0]
204 &mov("ebx", &DWP($data_off+12,"esp","",0)); # get iv[1]
205 &jz(&label("decrypt_finish"));
207 &set_label("decrypt_loop");
208 &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes
209 &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes
211 &bswap("eax") if $swap;
212 &bswap("ebx") if $swap;
214 &mov(&DWP($data_off,"esp","",0), "eax"); # put back
215 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
219 &mov("eax", &DWP($data_off,"esp","",0)); # get return
220 &mov("ebx", &DWP($data_off+4,"esp","",0)); #
222 &bswap("eax") if $swap;
223 &bswap("ebx") if $swap;
225 &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0]
226 &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1]
231 &mov("eax", &DWP(0,$in,"",0)); # get old cipher text,
232 &mov("ebx", &DWP(4,$in,"",0)); # next iv actually
234 &mov(&DWP(0,$out,"",0),"ecx");
235 &mov(&DWP(4,$out,"",0),"edx");
237 &mov(&DWP($data_off+8,"esp","",0), "eax"); # save iv
238 &mov(&DWP($data_off+12,"esp","",0), "ebx"); #
244 &jnz(&label("decrypt_loop"));
245 ############################ ENDIT #######################3
246 &set_label("decrypt_finish");
247 &mov($count, &wparam(2)); # length
249 &jz(&label("finish"));
251 &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes
252 &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes
254 &bswap("eax") if $swap;
255 &bswap("ebx") if $swap;
257 &mov(&DWP($data_off,"esp","",0), "eax"); # put back
258 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
262 &mov("eax", &DWP($data_off,"esp","",0)); # get return
263 &mov("ebx", &DWP($data_off+4,"esp","",0)); #
265 &bswap("eax") if $swap;
266 &bswap("ebx") if $swap;
268 &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0]
269 &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1]
274 # this is for when we exit
275 &mov("eax", &DWP(0,$in,"",0)); # get old cipher text,
276 &mov("ebx", &DWP(4,$in,"",0)); # next iv actually
280 &movb(&BP(6,$out,"",0), &LB("edx"));
283 &movb(&BP(5,$out,"",0), &HB("edx"));
285 &movb(&BP(4,$out,"",0), &LB("edx"));
287 &mov(&DWP(0,$out,"",0), "ecx");
288 &jmp(&label("djend"));
291 &movb(&BP(2,$out,"",0), &LB("ecx"));
294 &movb(&BP(1,$in,"",0), &HB("ecx"));
296 &movb(&BP(0,$in,"",0), &LB("ecx"));
299 # final iv is still in eax:ebx
300 &jmp(&label("finish"));
303 ############################ FINISH #######################3
304 &set_label("finish",1);
305 &mov("ecx", &wparam($iv_off)); # Get iv ptr
307 #################################################
309 $total+=4 if ($p1 > 0);
310 $total+=4 if ($p2 > 0);
311 $total+=4 if ($p3 > 0);
314 &mov(&DWP(0,"ecx","",0), "eax"); # save iv
315 &mov(&DWP(4,"ecx","",0), "ebx"); # save iv
317 &function_end_A($name);
319 &set_label("cbc_enc_jmp_table",1);
321 &data_word(&label("ej1"));
322 &data_word(&label("ej2"));
323 &data_word(&label("ej3"));
324 &data_word(&label("ej4"));
325 &data_word(&label("ej5"));
326 &data_word(&label("ej6"));
327 &data_word(&label("ej7"));
328 &set_label("cbc_dec_jmp_table",1);
330 &data_word(&label("dj1"));
331 &data_word(&label("dj2"));
332 &data_word(&label("dj3"));
333 &data_word(&label("dj4"));
334 &data_word(&label("dj5"));
335 &data_word(&label("dj6"));
336 &data_word(&label("dj7"));
338 &function_end_B($name);