1#!/usr/local/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# I've added the stuff needed for crypt() but I've not worried about making 6# things perfect. 7# 8 9push(@INC,"perlasm","../../perlasm"); 10require "x86asm.pl"; 11 12&asm_init($ARGV[0],"crypt586.pl"); 13 14$L="edi"; 15$R="esi"; 16 17&external_label("des_SPtrans"); 18&fcrypt_body("fcrypt_body"); 19&asm_finish(); 20 21sub fcrypt_body 22 { 23 local($name,$do_ip)=@_; 24 25 &function_begin($name,"EXTRN _des_SPtrans:DWORD"); 26 27 &comment(""); 28 &comment("Load the 2 words"); 29 $ks="ebp"; 30 31 &xor( $L, $L); 32 &xor( $R, $R); 33 &mov($ks,&wparam(1)); 34 35 &push(&DWC(25)); # add a variable 36 37 &set_label("start"); 38 for ($i=0; $i<16; $i+=2) 39 { 40 &comment(""); 41 &comment("Round $i"); 42 &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx"); 43 44 &comment(""); 45 &comment("Round ".sprintf("%d",$i+1)); 46 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx"); 47 } 48 &mov("ebx", &swtmp(0)); 49 &mov("eax", $L); 50 &dec("ebx"); 51 &mov($L, $R); 52 &mov($R, "eax"); 53 &mov(&swtmp(0), "ebx"); 54 &jnz(&label("start")); 55 56 &comment(""); 57 &comment("FP"); 58 &mov("edx",&wparam(0)); 59 60 &FP_new($R,$L,"eax",3); 61 &mov(&DWP(0,"edx","",0),"eax"); 62 &mov(&DWP(4,"edx","",0),$L); 63 64 &pop("ecx"); # remove variable 65 66 &function_end($name); 67 } 68 69sub D_ENCRYPT 70 { 71 local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_; 72 73 &mov( $u, &wparam(2)); # 2 74 &mov( $t, $R); 75 &shr( $t, 16); # 1 76 &mov( $tmp2, &wparam(3)); # 2 77 &xor( $t, $R); # 1 78 79 &and( $u, $t); # 2 80 &and( $t, $tmp2); # 2 81 82 &mov( $tmp1, $u); 83 &shl( $tmp1, 16); # 1 84 &mov( $tmp2, $t); 85 &shl( $tmp2, 16); # 1 86 &xor( $u, $tmp1); # 2 87 &xor( $t, $tmp2); # 2 88 &mov( $tmp1, &DWP(&n2a($S*4),$ks,"",0)); # 2 89 &xor( $u, $tmp1); 90 &mov( $tmp2, &DWP(&n2a(($S+1)*4),$ks,"",0)); # 2 91 &xor( $u, $R); 92 &xor( $t, $R); 93 &xor( $t, $tmp2); 94 95 &and( $u, "0xfcfcfcfc" ); # 2 96 &xor( $tmp1, $tmp1); # 1 97 &and( $t, "0xcfcfcfcf" ); # 2 98 &xor( $tmp2, $tmp2); 99 &movb( &LB($tmp1), &LB($u) ); 100 &movb( &LB($tmp2), &HB($u) ); 101 &rotr( $t, 4 ); 102 &mov( $ks, &DWP(" $desSP",$tmp1,"",0)); 103 &movb( &LB($tmp1), &LB($t) ); 104 &xor( $L, $ks); 105 &mov( $ks, &DWP("0x200+$desSP",$tmp2,"",0)); 106 &xor( $L, $ks); 107 &movb( &LB($tmp2), &HB($t) ); 108 &shr( $u, 16); 109 &mov( $ks, &DWP("0x100+$desSP",$tmp1,"",0)); 110 &xor( $L, $ks); 111 &movb( &LB($tmp1), &HB($u) ); 112 &shr( $t, 16); 113 &mov( $ks, &DWP("0x300+$desSP",$tmp2,"",0)); 114 &xor( $L, $ks); 115 &mov( $ks, &wparam(1)); 116 &movb( &LB($tmp2), &HB($t) ); 117 &and( $u, "0xff" ); 118 &and( $t, "0xff" ); 119 &mov( $tmp1, &DWP("0x600+$desSP",$tmp1,"",0)); 120 &xor( $L, $tmp1); 121 &mov( $tmp1, &DWP("0x700+$desSP",$tmp2,"",0)); 122 &xor( $L, $tmp1); 123 &mov( $tmp1, &DWP("0x400+$desSP",$u,"",0)); 124 &xor( $L, $tmp1); 125 &mov( $tmp1, &DWP("0x500+$desSP",$t,"",0)); 126 &xor( $L, $tmp1); 127 } 128 129sub n2a 130 { 131 sprintf("%d",$_[0]); 132 } 133 134# now has a side affect of rotating $a by $shift 135sub R_PERM_OP 136 { 137 local($a,$b,$tt,$shift,$mask,$last)=@_; 138 139 &rotl( $a, $shift ) if ($shift != 0); 140 &mov( $tt, $a ); 141 &xor( $a, $b ); 142 &and( $a, $mask ); 143 if ($notlast eq $b) 144 { 145 &xor( $b, $a ); 146 &xor( $tt, $a ); 147 } 148 else 149 { 150 &xor( $tt, $a ); 151 &xor( $b, $a ); 152 } 153 &comment(""); 154 } 155 156sub IP_new 157 { 158 local($l,$r,$tt,$lr)=@_; 159 160 &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l); 161 &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l); 162 &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r); 163 &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r); 164 &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r); 165 166 if ($lr != 3) 167 { 168 if (($lr-3) < 0) 169 { &rotr($tt, 3-$lr); } 170 else { &rotl($tt, $lr-3); } 171 } 172 if ($lr != 2) 173 { 174 if (($lr-2) < 0) 175 { &rotr($r, 2-$lr); } 176 else { &rotl($r, $lr-2); } 177 } 178 } 179 180sub FP_new 181 { 182 local($l,$r,$tt,$lr)=@_; 183 184 if ($lr != 2) 185 { 186 if (($lr-2) < 0) 187 { &rotl($r, 2-$lr); } 188 else { &rotr($r, $lr-2); } 189 } 190 if ($lr != 3) 191 { 192 if (($lr-3) < 0) 193 { &rotl($l, 3-$lr); } 194 else { &rotr($l, $lr-3); } 195 } 196 197 &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r); 198 &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r); 199 &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l); 200 &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l); 201 &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r); 202 &rotr($tt , 4); 203 } 204 205