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
9 # The inner loop instruction sequence and the IP/FP modifications are from
10 # Svend Olaf Mikkelsen.
12 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
13 push(@INC,"${dir}","${dir}../../perlasm");
18 # base code is in Microsoft
23 $output=pop and open STDOUT,">$output";
30 $small_footprint=1 if (grep(/\-DOPENSSL_SMALL_FOOTPRINT/,@ARGV));
31 # one can discuss setting this variable to 1 unconditionally, as
32 # the folded loop is only 3% slower than unrolled, but >7 times smaller
34 &public_label("DES_SPtrans");
35 &static_label("des_sptrans");
37 &DES_encrypt_internal();
38 &DES_decrypt_internal();
39 &DES_encrypt("DES_encrypt1",1);
40 &DES_encrypt("DES_encrypt2",0);
41 &DES_encrypt3("DES_encrypt3",1);
42 &DES_encrypt3("DES_decrypt3",0);
43 &cbc("DES_ncbc_encrypt","DES_encrypt1","DES_encrypt1",0,4,5,3,5,-1);
44 &cbc("DES_ede3_cbc_encrypt","DES_encrypt3","DES_decrypt3",0,6,7,3,4,5);
49 close STDOUT or die "error closing STDOUT: $!";
51 sub DES_encrypt_internal()
53 &function_begin_B("_x86_DES_encrypt");
57 &lea("edx",&DWP(128,"ecx"));
61 &D_ENCRYPT(0,$L,$R,0,$trans,"eax","ebx","ecx","edx",&swtmp(0));
63 &D_ENCRYPT(1,$R,$L,2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
66 &cmp("ecx",&swtmp(1));
67 &mov(&swtmp(0),"ecx");
74 for ($i=0; $i<16; $i+=2)
77 &D_ENCRYPT($i,$L,$R,$i*2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
78 &comment("Round ".sprintf("%d",$i+1));
79 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
85 &function_end_B("_x86_DES_encrypt");
88 sub DES_decrypt_internal()
90 &function_begin_B("_x86_DES_decrypt");
95 &lea("ecx",&DWP(128,"ecx"));
98 &D_ENCRYPT(0,$L,$R,-2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
100 &D_ENCRYPT(1,$R,$L,-4,$trans,"eax","ebx","ecx","edx",&swtmp(0));
103 &cmp("ecx",&swtmp(1));
104 &mov(&swtmp(0),"ecx");
105 &ja(&label("dloop"));
111 for ($i=15; $i>0; $i-=2)
113 &comment("Round $i");
114 &D_ENCRYPT(15-$i,$L,$R,$i*2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
115 &comment("Round ".sprintf("%d",$i-1));
116 &D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$trans,"eax","ebx","ecx","edx",&swtmp(0));
122 &function_end_B("_x86_DES_decrypt");
127 local($name,$do_ip)=@_;
129 &function_begin_B($name);
135 &comment("Load the 2 words");
140 &xor( "ecx", "ecx" );
145 &mov("eax",&DWP(0,$R,"",0));
146 &mov("ebx",&wparam(2)); # get encrypt flag
147 &mov($L,&DWP(4,$R,"",0));
150 &IP_new("eax",$L,$R,3);
154 &mov("eax",&wparam(0));
155 &xor( "ecx", "ecx" );
160 &mov($R,&DWP(0,"eax","",0));
161 &mov("ebx",&wparam(2)); # get encrypt flag
163 &mov($L,&DWP(4,"eax","",0));
168 &call (&label("pic_point"));
169 &set_label("pic_point");
171 &lea ($trans,&DWP(&label("des_sptrans")."-".&label("pic_point"),$trans));
173 &mov( "ecx", &wparam(1) );
176 &je(&label("decrypt"));
177 &call("_x86_DES_encrypt");
178 &jmp(&label("done"));
179 &set_label("decrypt");
180 &call("_x86_DES_decrypt");
187 &mov("edx",&wparam(0));
188 &FP_new($L,$R,"eax",3);
190 &mov(&DWP(0,"edx","",0),"eax");
191 &mov(&DWP(4,"edx","",0),$R);
198 &mov("eax",&wparam(0));
200 &mov(&DWP(0,"eax","",0),$L);
201 &mov(&DWP(4,"eax","",0),$R);
210 &function_end_B($name);
215 local($r,$L,$R,$S,$trans,$u,$tmp1,$tmp2,$t,$wp1)=@_;
217 &mov( $u, &DWP(&n2a($S*4),$tmp2,"",0));
219 &mov( $t, &DWP(&n2a(($S+1)*4),$tmp2,"",0));
223 &and( $u, "0xfcfcfcfc" );
224 &and( $t, "0xcfcfcfcf" );
225 &movb( &LB($tmp1), &LB($u) );
226 &movb( &LB($tmp2), &HB($u) );
228 &xor( $L, &DWP(" ",$trans,$tmp1,0));
229 &movb( &LB($tmp1), &LB($t) );
230 &xor( $L, &DWP("0x200",$trans,$tmp2,0));
231 &movb( &LB($tmp2), &HB($t) );
233 &xor( $L, &DWP("0x100",$trans,$tmp1,0));
234 &movb( &LB($tmp1), &HB($u) );
236 &xor( $L, &DWP("0x300",$trans,$tmp2,0));
237 &movb( &LB($tmp2), &HB($t) );
240 &xor( $L, &DWP("0x600",$trans,$tmp1,0));
241 &xor( $L, &DWP("0x700",$trans,$tmp2,0));
243 &xor( $L, &DWP("0x400",$trans,$u,0));
244 &xor( $L, &DWP("0x500",$trans,$t,0));
252 # now has a side affect of rotating $a by $shift
255 local($a,$b,$tt,$shift,$mask,$last)=@_;
257 &rotl( $a, $shift ) if ($shift != 0);
261 # This can never succeed, and besides it is difficult to see what the
262 # idea was - Ben 13 Feb 99
278 local($l,$r,$tt,$lr)=@_;
280 &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);
281 &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l);
282 &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r);
283 &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r);
284 &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);
289 { &rotr($tt, 3-$lr); }
290 else { &rotl($tt, $lr-3); }
295 { &rotr($r, 2-$lr); }
296 else { &rotl($r, $lr-2); }
302 local($l,$r,$tt,$lr)=@_;
307 { &rotl($r, 2-$lr); }
308 else { &rotr($r, $lr-2); }
313 { &rotl($l, 3-$lr); }
314 else { &rotr($l, $lr-3); }
317 &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);
318 &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r);
319 &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);
320 &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l);
321 &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r);
327 &set_label("DES_SPtrans",64);
328 &set_label("des_sptrans");
329 &data_word(0x02080800, 0x00080000, 0x02000002, 0x02080802);
330 &data_word(0x02000000, 0x00080802, 0x00080002, 0x02000002);
331 &data_word(0x00080802, 0x02080800, 0x02080000, 0x00000802);
332 &data_word(0x02000802, 0x02000000, 0x00000000, 0x00080002);
333 &data_word(0x00080000, 0x00000002, 0x02000800, 0x00080800);
334 &data_word(0x02080802, 0x02080000, 0x00000802, 0x02000800);
335 &data_word(0x00000002, 0x00000800, 0x00080800, 0x02080002);
336 &data_word(0x00000800, 0x02000802, 0x02080002, 0x00000000);
337 &data_word(0x00000000, 0x02080802, 0x02000800, 0x00080002);
338 &data_word(0x02080800, 0x00080000, 0x00000802, 0x02000800);
339 &data_word(0x02080002, 0x00000800, 0x00080800, 0x02000002);
340 &data_word(0x00080802, 0x00000002, 0x02000002, 0x02080000);
341 &data_word(0x02080802, 0x00080800, 0x02080000, 0x02000802);
342 &data_word(0x02000000, 0x00000802, 0x00080002, 0x00000000);
343 &data_word(0x00080000, 0x02000000, 0x02000802, 0x02080800);
344 &data_word(0x00000002, 0x02080002, 0x00000800, 0x00080802);
346 &data_word(0x40108010, 0x00000000, 0x00108000, 0x40100000);
347 &data_word(0x40000010, 0x00008010, 0x40008000, 0x00108000);
348 &data_word(0x00008000, 0x40100010, 0x00000010, 0x40008000);
349 &data_word(0x00100010, 0x40108000, 0x40100000, 0x00000010);
350 &data_word(0x00100000, 0x40008010, 0x40100010, 0x00008000);
351 &data_word(0x00108010, 0x40000000, 0x00000000, 0x00100010);
352 &data_word(0x40008010, 0x00108010, 0x40108000, 0x40000010);
353 &data_word(0x40000000, 0x00100000, 0x00008010, 0x40108010);
354 &data_word(0x00100010, 0x40108000, 0x40008000, 0x00108010);
355 &data_word(0x40108010, 0x00100010, 0x40000010, 0x00000000);
356 &data_word(0x40000000, 0x00008010, 0x00100000, 0x40100010);
357 &data_word(0x00008000, 0x40000000, 0x00108010, 0x40008010);
358 &data_word(0x40108000, 0x00008000, 0x00000000, 0x40000010);
359 &data_word(0x00000010, 0x40108010, 0x00108000, 0x40100000);
360 &data_word(0x40100010, 0x00100000, 0x00008010, 0x40008000);
361 &data_word(0x40008010, 0x00000010, 0x40100000, 0x00108000);
363 &data_word(0x04000001, 0x04040100, 0x00000100, 0x04000101);
364 &data_word(0x00040001, 0x04000000, 0x04000101, 0x00040100);
365 &data_word(0x04000100, 0x00040000, 0x04040000, 0x00000001);
366 &data_word(0x04040101, 0x00000101, 0x00000001, 0x04040001);
367 &data_word(0x00000000, 0x00040001, 0x04040100, 0x00000100);
368 &data_word(0x00000101, 0x04040101, 0x00040000, 0x04000001);
369 &data_word(0x04040001, 0x04000100, 0x00040101, 0x04040000);
370 &data_word(0x00040100, 0x00000000, 0x04000000, 0x00040101);
371 &data_word(0x04040100, 0x00000100, 0x00000001, 0x00040000);
372 &data_word(0x00000101, 0x00040001, 0x04040000, 0x04000101);
373 &data_word(0x00000000, 0x04040100, 0x00040100, 0x04040001);
374 &data_word(0x00040001, 0x04000000, 0x04040101, 0x00000001);
375 &data_word(0x00040101, 0x04000001, 0x04000000, 0x04040101);
376 &data_word(0x00040000, 0x04000100, 0x04000101, 0x00040100);
377 &data_word(0x04000100, 0x00000000, 0x04040001, 0x00000101);
378 &data_word(0x04000001, 0x00040101, 0x00000100, 0x04040000);
380 &data_word(0x00401008, 0x10001000, 0x00000008, 0x10401008);
381 &data_word(0x00000000, 0x10400000, 0x10001008, 0x00400008);
382 &data_word(0x10401000, 0x10000008, 0x10000000, 0x00001008);
383 &data_word(0x10000008, 0x00401008, 0x00400000, 0x10000000);
384 &data_word(0x10400008, 0x00401000, 0x00001000, 0x00000008);
385 &data_word(0x00401000, 0x10001008, 0x10400000, 0x00001000);
386 &data_word(0x00001008, 0x00000000, 0x00400008, 0x10401000);
387 &data_word(0x10001000, 0x10400008, 0x10401008, 0x00400000);
388 &data_word(0x10400008, 0x00001008, 0x00400000, 0x10000008);
389 &data_word(0x00401000, 0x10001000, 0x00000008, 0x10400000);
390 &data_word(0x10001008, 0x00000000, 0x00001000, 0x00400008);
391 &data_word(0x00000000, 0x10400008, 0x10401000, 0x00001000);
392 &data_word(0x10000000, 0x10401008, 0x00401008, 0x00400000);
393 &data_word(0x10401008, 0x00000008, 0x10001000, 0x00401008);
394 &data_word(0x00400008, 0x00401000, 0x10400000, 0x10001008);
395 &data_word(0x00001008, 0x10000000, 0x10000008, 0x10401000);
397 &data_word(0x08000000, 0x00010000, 0x00000400, 0x08010420);
398 &data_word(0x08010020, 0x08000400, 0x00010420, 0x08010000);
399 &data_word(0x00010000, 0x00000020, 0x08000020, 0x00010400);
400 &data_word(0x08000420, 0x08010020, 0x08010400, 0x00000000);
401 &data_word(0x00010400, 0x08000000, 0x00010020, 0x00000420);
402 &data_word(0x08000400, 0x00010420, 0x00000000, 0x08000020);
403 &data_word(0x00000020, 0x08000420, 0x08010420, 0x00010020);
404 &data_word(0x08010000, 0x00000400, 0x00000420, 0x08010400);
405 &data_word(0x08010400, 0x08000420, 0x00010020, 0x08010000);
406 &data_word(0x00010000, 0x00000020, 0x08000020, 0x08000400);
407 &data_word(0x08000000, 0x00010400, 0x08010420, 0x00000000);
408 &data_word(0x00010420, 0x08000000, 0x00000400, 0x00010020);
409 &data_word(0x08000420, 0x00000400, 0x00000000, 0x08010420);
410 &data_word(0x08010020, 0x08010400, 0x00000420, 0x00010000);
411 &data_word(0x00010400, 0x08010020, 0x08000400, 0x00000420);
412 &data_word(0x00000020, 0x00010420, 0x08010000, 0x08000020);
414 &data_word(0x80000040, 0x00200040, 0x00000000, 0x80202000);
415 &data_word(0x00200040, 0x00002000, 0x80002040, 0x00200000);
416 &data_word(0x00002040, 0x80202040, 0x00202000, 0x80000000);
417 &data_word(0x80002000, 0x80000040, 0x80200000, 0x00202040);
418 &data_word(0x00200000, 0x80002040, 0x80200040, 0x00000000);
419 &data_word(0x00002000, 0x00000040, 0x80202000, 0x80200040);
420 &data_word(0x80202040, 0x80200000, 0x80000000, 0x00002040);
421 &data_word(0x00000040, 0x00202000, 0x00202040, 0x80002000);
422 &data_word(0x00002040, 0x80000000, 0x80002000, 0x00202040);
423 &data_word(0x80202000, 0x00200040, 0x00000000, 0x80002000);
424 &data_word(0x80000000, 0x00002000, 0x80200040, 0x00200000);
425 &data_word(0x00200040, 0x80202040, 0x00202000, 0x00000040);
426 &data_word(0x80202040, 0x00202000, 0x00200000, 0x80002040);
427 &data_word(0x80000040, 0x80200000, 0x00202040, 0x00000000);
428 &data_word(0x00002000, 0x80000040, 0x80002040, 0x80202000);
429 &data_word(0x80200000, 0x00002040, 0x00000040, 0x80200040);
431 &data_word(0x00004000, 0x00000200, 0x01000200, 0x01000004);
432 &data_word(0x01004204, 0x00004004, 0x00004200, 0x00000000);
433 &data_word(0x01000000, 0x01000204, 0x00000204, 0x01004000);
434 &data_word(0x00000004, 0x01004200, 0x01004000, 0x00000204);
435 &data_word(0x01000204, 0x00004000, 0x00004004, 0x01004204);
436 &data_word(0x00000000, 0x01000200, 0x01000004, 0x00004200);
437 &data_word(0x01004004, 0x00004204, 0x01004200, 0x00000004);
438 &data_word(0x00004204, 0x01004004, 0x00000200, 0x01000000);
439 &data_word(0x00004204, 0x01004000, 0x01004004, 0x00000204);
440 &data_word(0x00004000, 0x00000200, 0x01000000, 0x01004004);
441 &data_word(0x01000204, 0x00004204, 0x00004200, 0x00000000);
442 &data_word(0x00000200, 0x01000004, 0x00000004, 0x01000200);
443 &data_word(0x00000000, 0x01000204, 0x01000200, 0x00004200);
444 &data_word(0x00000204, 0x00004000, 0x01004204, 0x01000000);
445 &data_word(0x01004200, 0x00000004, 0x00004004, 0x01004204);
446 &data_word(0x01000004, 0x01004200, 0x01004000, 0x00004004);
448 &data_word(0x20800080, 0x20820000, 0x00020080, 0x00000000);
449 &data_word(0x20020000, 0x00800080, 0x20800000, 0x20820080);
450 &data_word(0x00000080, 0x20000000, 0x00820000, 0x00020080);
451 &data_word(0x00820080, 0x20020080, 0x20000080, 0x20800000);
452 &data_word(0x00020000, 0x00820080, 0x00800080, 0x20020000);
453 &data_word(0x20820080, 0x20000080, 0x00000000, 0x00820000);
454 &data_word(0x20000000, 0x00800000, 0x20020080, 0x20800080);
455 &data_word(0x00800000, 0x00020000, 0x20820000, 0x00000080);
456 &data_word(0x00800000, 0x00020000, 0x20000080, 0x20820080);
457 &data_word(0x00020080, 0x20000000, 0x00000000, 0x00820000);
458 &data_word(0x20800080, 0x20020080, 0x20020000, 0x00800080);
459 &data_word(0x20820000, 0x00000080, 0x00800080, 0x20020000);
460 &data_word(0x20820080, 0x00800000, 0x20800000, 0x20000080);
461 &data_word(0x00820000, 0x00020080, 0x20020080, 0x20800000);
462 &data_word(0x00000080, 0x20820000, 0x00820080, 0x00000000);
463 &data_word(0x20000000, 0x20800080, 0x00020000, 0x00820080);