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