2 # Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
10 # void des_ncbc_encrypt(input, output, length, schedule, ivec, enc)
11 # des_cblock (*input);
12 # des_cblock (*output);
14 # des_key_schedule schedule;
19 # des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
22 #&cbc("des_ncbc_encrypt","des_encrypt",0);
23 #&cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt",
25 #&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",
27 #&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",
30 # When doing a cipher that needs bigendian order,
31 # for encrypt, the iv is kept in bigendian form,
32 # while for decrypt, it is kept in little endian.
35 local($name,$enc_func,$dec_func,$swap,$iv_off,$enc_off,$p1,$p2,$p3)=@_;
36 # name is the function name
37 # enc_func and dec_func and the functions to call for encrypt/decrypt
38 # swap is true if byte order needs to be reversed
39 # iv_off is parameter number for the iv
40 # enc_off is parameter number for the encrypt/decrypt flag
41 # p1,p2,p3 are the offsets for parameters to be passed to the
44 &function_begin_B($name,"");
57 $data_off+=4 if ($p1 > 0);
58 $data_off+=4 if ($p2 > 0);
59 $data_off+=4 if ($p3 > 0);
61 &mov($count, &wparam(2)); # length
63 &comment("getting iv ptr from parameter $iv_off");
64 &mov("ebx", &wparam($iv_off)); # Get iv ptr
66 &mov($in, &DWP(0,"ebx","",0));# iv[0]
67 &mov($out, &DWP(4,"ebx","",0));# iv[1]
71 &push($out); # used in decrypt for iv[1]
72 &push($in); # used in decrypt for iv[0]
74 &mov("ebx", "esp"); # This is the address of tin[2]
76 &mov($in, &wparam(0)); # in
77 &mov($out, &wparam(1)); # out
79 # We have loaded them all, how lets push things
80 &comment("getting encrypt flag from parameter $enc_off");
81 &mov("ecx", &wparam($enc_off)); # Get enc flag
84 &comment("get and push parameter $p3");
86 { &mov("eax", &wparam($p3)); &push("eax"); }
87 else { &push("ecx"); }
91 &comment("get and push parameter $p2");
93 { &mov("eax", &wparam($p2)); &push("eax"); }
94 else { &push("ecx"); }
98 &comment("get and push parameter $p1");
100 { &mov("eax", &wparam($p1)); &push("eax"); }
101 else { &push("ecx"); }
103 &push("ebx"); # push data/iv
106 &jz(&label("decrypt"));
108 &and($count,0xfffffff8);
109 &mov("eax", &DWP($data_off,"esp","",0)); # load iv[0]
110 &mov("ebx", &DWP($data_off+4,"esp","",0)); # load iv[1]
112 &jz(&label("encrypt_finish"));
114 #############################################################
116 &set_label("encrypt_loop");
118 # "eax" and "ebx" hold iv (or the last cipher text)
120 &mov("ecx", &DWP(0,$in,"",0)); # load first 4 bytes
121 &mov("edx", &DWP(4,$in,"",0)); # second 4 bytes
126 &bswap("eax") if $swap;
127 &bswap("ebx") if $swap;
129 &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call
130 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
134 &mov("eax", &DWP($data_off,"esp","",0));
135 &mov("ebx", &DWP($data_off+4,"esp","",0));
137 &bswap("eax") if $swap;
138 &bswap("ebx") if $swap;
140 &mov(&DWP(0,$out,"",0),"eax");
141 &mov(&DWP(4,$out,"",0),"ebx");
143 # eax and ebx are the next iv.
149 &jnz(&label("encrypt_loop"));
151 ###################################################################3
152 &set_label("encrypt_finish");
153 &mov($count, &wparam(2)); # length
155 &jz(&label("finish"));
156 &call(&label("PIC_point"));
157 &set_label("PIC_point");
159 &lea("ecx",&DWP(&label("cbc_enc_jmp_table")."-".&label("PIC_point"),"edx"));
160 &mov($count,&DWP(0,"ecx",$count,4));
164 #&mov($count,&DWP(&label("cbc_enc_jmp_table"),"",$count,4));
169 &movb(&HB("edx"), &BP(6,$in,"",0));
173 &movb(&HB("edx"), &BP(5,$in,"",0));
176 &movb(&LB("edx"), &BP(4,$in,"",0));
179 &mov("ecx", &DWP(0,$in,"",0));
180 &jmp(&label("ejend"));
183 &movb(&HB("ecx"), &BP(2,$in,"",0));
187 &movb(&HB("ecx"), &BP(1,$in,"",0));
190 &movb(&LB("ecx"), &BP(0,$in,"",0));
196 &bswap("eax") if $swap;
197 &bswap("ebx") if $swap;
199 &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call
200 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
204 &mov("eax", &DWP($data_off,"esp","",0));
205 &mov("ebx", &DWP($data_off+4,"esp","",0));
207 &bswap("eax") if $swap;
208 &bswap("ebx") if $swap;
210 &mov(&DWP(0,$out,"",0),"eax");
211 &mov(&DWP(4,$out,"",0),"ebx");
213 &jmp(&label("finish"));
215 #############################################################
216 #############################################################
217 &set_label("decrypt",1);
219 &and($count,0xfffffff8);
220 # The next 2 instructions are only for if the jz is taken
221 &mov("eax", &DWP($data_off+8,"esp","",0)); # get iv[0]
222 &mov("ebx", &DWP($data_off+12,"esp","",0)); # get iv[1]
223 &jz(&label("decrypt_finish"));
225 &set_label("decrypt_loop");
226 &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes
227 &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes
229 &bswap("eax") if $swap;
230 &bswap("ebx") if $swap;
232 &mov(&DWP($data_off,"esp","",0), "eax"); # put back
233 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
237 &mov("eax", &DWP($data_off,"esp","",0)); # get return
238 &mov("ebx", &DWP($data_off+4,"esp","",0)); #
240 &bswap("eax") if $swap;
241 &bswap("ebx") if $swap;
243 &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0]
244 &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1]
249 &mov("eax", &DWP(0,$in,"",0)); # get old cipher text,
250 &mov("ebx", &DWP(4,$in,"",0)); # next iv actually
252 &mov(&DWP(0,$out,"",0),"ecx");
253 &mov(&DWP(4,$out,"",0),"edx");
255 &mov(&DWP($data_off+8,"esp","",0), "eax"); # save iv
256 &mov(&DWP($data_off+12,"esp","",0), "ebx"); #
262 &jnz(&label("decrypt_loop"));
263 ############################ ENDIT #######################3
264 &set_label("decrypt_finish");
265 &mov($count, &wparam(2)); # length
267 &jz(&label("finish"));
269 &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes
270 &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes
272 &bswap("eax") if $swap;
273 &bswap("ebx") if $swap;
275 &mov(&DWP($data_off,"esp","",0), "eax"); # put back
276 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
280 &mov("eax", &DWP($data_off,"esp","",0)); # get return
281 &mov("ebx", &DWP($data_off+4,"esp","",0)); #
283 &bswap("eax") if $swap;
284 &bswap("ebx") if $swap;
286 &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0]
287 &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1]
292 # this is for when we exit
293 &mov("eax", &DWP(0,$in,"",0)); # get old cipher text,
294 &mov("ebx", &DWP(4,$in,"",0)); # next iv actually
298 &movb(&BP(6,$out,"",0), &LB("edx"));
301 &movb(&BP(5,$out,"",0), &HB("edx"));
303 &movb(&BP(4,$out,"",0), &LB("edx"));
305 &mov(&DWP(0,$out,"",0), "ecx");
306 &jmp(&label("djend"));
309 &movb(&BP(2,$out,"",0), &LB("ecx"));
312 &movb(&BP(1,$in,"",0), &HB("ecx"));
314 &movb(&BP(0,$in,"",0), &LB("ecx"));
317 # final iv is still in eax:ebx
318 &jmp(&label("finish"));
321 ############################ FINISH #######################3
322 &set_label("finish",1);
323 &mov("ecx", &wparam($iv_off)); # Get iv ptr
325 #################################################
327 $total+=4 if ($p1 > 0);
328 $total+=4 if ($p2 > 0);
329 $total+=4 if ($p3 > 0);
332 &mov(&DWP(0,"ecx","",0), "eax"); # save iv
333 &mov(&DWP(4,"ecx","",0), "ebx"); # save iv
335 &function_end_A($name);
338 &set_label("cbc_enc_jmp_table");
340 &data_word(&label("ej1")."-".&label("PIC_point"));
341 &data_word(&label("ej2")."-".&label("PIC_point"));
342 &data_word(&label("ej3")."-".&label("PIC_point"));
343 &data_word(&label("ej4")."-".&label("PIC_point"));
344 &data_word(&label("ej5")."-".&label("PIC_point"));
345 &data_word(&label("ej6")."-".&label("PIC_point"));
346 &data_word(&label("ej7")."-".&label("PIC_point"));
348 #&set_label("cbc_dec_jmp_table",1);
350 #&data_word(&label("dj1")."-".&label("PIC_point"));
351 #&data_word(&label("dj2")."-".&label("PIC_point"));
352 #&data_word(&label("dj3")."-".&label("PIC_point"));
353 #&data_word(&label("dj4")."-".&label("PIC_point"));
354 #&data_word(&label("dj5")."-".&label("PIC_point"));
355 #&data_word(&label("dj6")."-".&label("PIC_point"));
356 #&data_word(&label("dj7")."-".&label("PIC_point"));
359 &function_end_B($name);