1*0Sstevel@tonic-gate#!/usr/local/bin/perl 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gate# Normal is the 4*0Sstevel@tonic-gate# md5_block_x86(MD5_CTX *c, ULONG *X); 5*0Sstevel@tonic-gate# version, non-normal is the 6*0Sstevel@tonic-gate# md5_block_x86(MD5_CTX *c, ULONG *X,int blocks); 7*0Sstevel@tonic-gate 8*0Sstevel@tonic-gate$normal=0; 9*0Sstevel@tonic-gate 10*0Sstevel@tonic-gatepush(@INC,"perlasm","../../perlasm"); 11*0Sstevel@tonic-gaterequire "x86asm.pl"; 12*0Sstevel@tonic-gate 13*0Sstevel@tonic-gate&asm_init($ARGV[0],$0); 14*0Sstevel@tonic-gate 15*0Sstevel@tonic-gate$A="eax"; 16*0Sstevel@tonic-gate$B="ebx"; 17*0Sstevel@tonic-gate$C="ecx"; 18*0Sstevel@tonic-gate$D="edx"; 19*0Sstevel@tonic-gate$tmp1="edi"; 20*0Sstevel@tonic-gate$tmp2="ebp"; 21*0Sstevel@tonic-gate$X="esi"; 22*0Sstevel@tonic-gate 23*0Sstevel@tonic-gate# What we need to load into $tmp for the next round 24*0Sstevel@tonic-gate%Ltmp1=("R0",&Np($C), "R1",&Np($C), "R2",&Np($C), "R3",&Np($D)); 25*0Sstevel@tonic-gate@xo=( 26*0Sstevel@tonic-gate 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, # R0 27*0Sstevel@tonic-gate 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, # R1 28*0Sstevel@tonic-gate 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, # R2 29*0Sstevel@tonic-gate 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9, # R3 30*0Sstevel@tonic-gate ); 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate&md5_block("md5_block_asm_host_order"); 33*0Sstevel@tonic-gate&asm_finish(); 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gatesub Np 36*0Sstevel@tonic-gate { 37*0Sstevel@tonic-gate local($p)=@_; 38*0Sstevel@tonic-gate local(%n)=($A,$D,$B,$A,$C,$B,$D,$C); 39*0Sstevel@tonic-gate return($n{$p}); 40*0Sstevel@tonic-gate } 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gatesub R0 43*0Sstevel@tonic-gate { 44*0Sstevel@tonic-gate local($pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_; 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate &mov($tmp1,$C) if $pos < 0; 47*0Sstevel@tonic-gate &mov($tmp2,&DWP($xo[$ki]*4,$K,"",0)) if $pos < 0; # very first one 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate # body proper 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate &comment("R0 $ki"); 52*0Sstevel@tonic-gate &xor($tmp1,$d); # F function - part 2 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate &and($tmp1,$b); # F function - part 3 55*0Sstevel@tonic-gate &lea($a,&DWP($t,$a,$tmp2,1)); 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate &xor($tmp1,$d); # F function - part 4 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate &add($a,$tmp1); 60*0Sstevel@tonic-gate &mov($tmp1,&Np($c)) if $pos < 1; # next tmp1 for R0 61*0Sstevel@tonic-gate &mov($tmp1,&Np($c)) if $pos == 1; # next tmp1 for R1 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate &rotl($a,$s); 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if ($pos != 2); 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate &add($a,$b); 68*0Sstevel@tonic-gate } 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gatesub R1 71*0Sstevel@tonic-gate { 72*0Sstevel@tonic-gate local($pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_; 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate &comment("R1 $ki"); 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate &lea($a,&DWP($t,$a,$tmp2,1)); 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate &xor($tmp1,$b); # G function - part 2 79*0Sstevel@tonic-gate &and($tmp1,$d); # G function - part 3 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if ($pos != 2); 82*0Sstevel@tonic-gate &xor($tmp1,$c); # G function - part 4 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate &add($a,$tmp1); 85*0Sstevel@tonic-gate &mov($tmp1,&Np($c)) if $pos < 1; # G function - part 1 86*0Sstevel@tonic-gate &mov($tmp1,&Np($c)) if $pos == 1; # G function - part 1 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate &rotl($a,$s); 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate &add($a,$b); 91*0Sstevel@tonic-gate } 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gatesub R2 94*0Sstevel@tonic-gate { 95*0Sstevel@tonic-gate local($n,$pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_; 96*0Sstevel@tonic-gate # This one is different, only 3 logical operations 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gateif (($n & 1) == 0) 99*0Sstevel@tonic-gate { 100*0Sstevel@tonic-gate &comment("R2 $ki"); 101*0Sstevel@tonic-gate # make sure to do 'D' first, not 'B', else we clash with 102*0Sstevel@tonic-gate # the last add from the previous round. 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate &xor($tmp1,$d); # H function - part 2 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate &xor($tmp1,$b); # H function - part 3 107*0Sstevel@tonic-gate &lea($a,&DWP($t,$a,$tmp2,1)); 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate &add($a,$tmp1); 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate &rotl($a,$s); 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gate &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)); 114*0Sstevel@tonic-gate &mov($tmp1,&Np($c)); 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gateelse 117*0Sstevel@tonic-gate { 118*0Sstevel@tonic-gate &comment("R2 $ki"); 119*0Sstevel@tonic-gate # make sure to do 'D' first, not 'B', else we clash with 120*0Sstevel@tonic-gate # the last add from the previous round. 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate &lea($a,&DWP($t,$a,$tmp2,1)); 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate &add($b,$c); # MOVED FORWARD 125*0Sstevel@tonic-gate &xor($tmp1,$d); # H function - part 2 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gate &xor($tmp1,$b); # H function - part 3 128*0Sstevel@tonic-gate &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if ($pos != 2); 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate &add($a,$tmp1); 131*0Sstevel@tonic-gate &mov($tmp1,&Np($c)) if $pos < 1; # H function - part 1 132*0Sstevel@tonic-gate &mov($tmp1,-1) if $pos == 1; # I function - part 1 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate &rotl($a,$s); 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate &add($a,$b); 137*0Sstevel@tonic-gate } 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gatesub R3 141*0Sstevel@tonic-gate { 142*0Sstevel@tonic-gate local($pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_; 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate &comment("R3 $ki"); 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate # ¬($tmp1) 147*0Sstevel@tonic-gate &xor($tmp1,$d) if $pos < 0; # I function - part 2 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate &or($tmp1,$b); # I function - part 3 150*0Sstevel@tonic-gate &lea($a,&DWP($t,$a,$tmp2,1)); 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate &xor($tmp1,$c); # I function - part 4 153*0Sstevel@tonic-gate &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if $pos != 2; # load X/k value 154*0Sstevel@tonic-gate &mov($tmp2,&wparam(0)) if $pos == 2; 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate &add($a,$tmp1); 157*0Sstevel@tonic-gate &mov($tmp1,-1) if $pos < 1; # H function - part 1 158*0Sstevel@tonic-gate &add($K,64) if $pos >=1 && !$normal; 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate &rotl($a,$s); 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gate &xor($tmp1,&Np($d)) if $pos <= 0; # I function - part = first time 163*0Sstevel@tonic-gate &mov($tmp1,&DWP( 0,$tmp2,"",0)) if $pos > 0; 164*0Sstevel@tonic-gate &add($a,$b); 165*0Sstevel@tonic-gate } 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gatesub md5_block 169*0Sstevel@tonic-gate { 170*0Sstevel@tonic-gate local($name)=@_; 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate &function_begin_B($name,"",3); 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate # parameter 1 is the MD5_CTX structure. 175*0Sstevel@tonic-gate # A 0 176*0Sstevel@tonic-gate # B 4 177*0Sstevel@tonic-gate # C 8 178*0Sstevel@tonic-gate # D 12 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate &push("esi"); 181*0Sstevel@tonic-gate &push("edi"); 182*0Sstevel@tonic-gate &mov($tmp1, &wparam(0)); # edi 183*0Sstevel@tonic-gate &mov($X, &wparam(1)); # esi 184*0Sstevel@tonic-gate &mov($C, &wparam(2)); 185*0Sstevel@tonic-gate &push("ebp"); 186*0Sstevel@tonic-gate &shl($C, 6); 187*0Sstevel@tonic-gate &push("ebx"); 188*0Sstevel@tonic-gate &add($C, $X); # offset we end at 189*0Sstevel@tonic-gate &sub($C, 64); 190*0Sstevel@tonic-gate &mov($A, &DWP( 0,$tmp1,"",0)); 191*0Sstevel@tonic-gate &push($C); # Put on the TOS 192*0Sstevel@tonic-gate &mov($B, &DWP( 4,$tmp1,"",0)); 193*0Sstevel@tonic-gate &mov($C, &DWP( 8,$tmp1,"",0)); 194*0Sstevel@tonic-gate &mov($D, &DWP(12,$tmp1,"",0)); 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate &set_label("start") unless $normal; 197*0Sstevel@tonic-gate &comment(""); 198*0Sstevel@tonic-gate &comment("R0 section"); 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gate &R0(-2,$A,$B,$C,$D,$X, 0, 7,0xd76aa478); 201*0Sstevel@tonic-gate &R0( 0,$D,$A,$B,$C,$X, 1,12,0xe8c7b756); 202*0Sstevel@tonic-gate &R0( 0,$C,$D,$A,$B,$X, 2,17,0x242070db); 203*0Sstevel@tonic-gate &R0( 0,$B,$C,$D,$A,$X, 3,22,0xc1bdceee); 204*0Sstevel@tonic-gate &R0( 0,$A,$B,$C,$D,$X, 4, 7,0xf57c0faf); 205*0Sstevel@tonic-gate &R0( 0,$D,$A,$B,$C,$X, 5,12,0x4787c62a); 206*0Sstevel@tonic-gate &R0( 0,$C,$D,$A,$B,$X, 6,17,0xa8304613); 207*0Sstevel@tonic-gate &R0( 0,$B,$C,$D,$A,$X, 7,22,0xfd469501); 208*0Sstevel@tonic-gate &R0( 0,$A,$B,$C,$D,$X, 8, 7,0x698098d8); 209*0Sstevel@tonic-gate &R0( 0,$D,$A,$B,$C,$X, 9,12,0x8b44f7af); 210*0Sstevel@tonic-gate &R0( 0,$C,$D,$A,$B,$X,10,17,0xffff5bb1); 211*0Sstevel@tonic-gate &R0( 0,$B,$C,$D,$A,$X,11,22,0x895cd7be); 212*0Sstevel@tonic-gate &R0( 0,$A,$B,$C,$D,$X,12, 7,0x6b901122); 213*0Sstevel@tonic-gate &R0( 0,$D,$A,$B,$C,$X,13,12,0xfd987193); 214*0Sstevel@tonic-gate &R0( 0,$C,$D,$A,$B,$X,14,17,0xa679438e); 215*0Sstevel@tonic-gate &R0( 1,$B,$C,$D,$A,$X,15,22,0x49b40821); 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate &comment(""); 218*0Sstevel@tonic-gate &comment("R1 section"); 219*0Sstevel@tonic-gate &R1(-1,$A,$B,$C,$D,$X,16, 5,0xf61e2562); 220*0Sstevel@tonic-gate &R1( 0,$D,$A,$B,$C,$X,17, 9,0xc040b340); 221*0Sstevel@tonic-gate &R1( 0,$C,$D,$A,$B,$X,18,14,0x265e5a51); 222*0Sstevel@tonic-gate &R1( 0,$B,$C,$D,$A,$X,19,20,0xe9b6c7aa); 223*0Sstevel@tonic-gate &R1( 0,$A,$B,$C,$D,$X,20, 5,0xd62f105d); 224*0Sstevel@tonic-gate &R1( 0,$D,$A,$B,$C,$X,21, 9,0x02441453); 225*0Sstevel@tonic-gate &R1( 0,$C,$D,$A,$B,$X,22,14,0xd8a1e681); 226*0Sstevel@tonic-gate &R1( 0,$B,$C,$D,$A,$X,23,20,0xe7d3fbc8); 227*0Sstevel@tonic-gate &R1( 0,$A,$B,$C,$D,$X,24, 5,0x21e1cde6); 228*0Sstevel@tonic-gate &R1( 0,$D,$A,$B,$C,$X,25, 9,0xc33707d6); 229*0Sstevel@tonic-gate &R1( 0,$C,$D,$A,$B,$X,26,14,0xf4d50d87); 230*0Sstevel@tonic-gate &R1( 0,$B,$C,$D,$A,$X,27,20,0x455a14ed); 231*0Sstevel@tonic-gate &R1( 0,$A,$B,$C,$D,$X,28, 5,0xa9e3e905); 232*0Sstevel@tonic-gate &R1( 0,$D,$A,$B,$C,$X,29, 9,0xfcefa3f8); 233*0Sstevel@tonic-gate &R1( 0,$C,$D,$A,$B,$X,30,14,0x676f02d9); 234*0Sstevel@tonic-gate &R1( 1,$B,$C,$D,$A,$X,31,20,0x8d2a4c8a); 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate &comment(""); 237*0Sstevel@tonic-gate &comment("R2 section"); 238*0Sstevel@tonic-gate &R2( 0,-1,$A,$B,$C,$D,$X,32, 4,0xfffa3942); 239*0Sstevel@tonic-gate &R2( 1, 0,$D,$A,$B,$C,$X,33,11,0x8771f681); 240*0Sstevel@tonic-gate &R2( 2, 0,$C,$D,$A,$B,$X,34,16,0x6d9d6122); 241*0Sstevel@tonic-gate &R2( 3, 0,$B,$C,$D,$A,$X,35,23,0xfde5380c); 242*0Sstevel@tonic-gate &R2( 4, 0,$A,$B,$C,$D,$X,36, 4,0xa4beea44); 243*0Sstevel@tonic-gate &R2( 5, 0,$D,$A,$B,$C,$X,37,11,0x4bdecfa9); 244*0Sstevel@tonic-gate &R2( 6, 0,$C,$D,$A,$B,$X,38,16,0xf6bb4b60); 245*0Sstevel@tonic-gate &R2( 7, 0,$B,$C,$D,$A,$X,39,23,0xbebfbc70); 246*0Sstevel@tonic-gate &R2( 8, 0,$A,$B,$C,$D,$X,40, 4,0x289b7ec6); 247*0Sstevel@tonic-gate &R2( 9, 0,$D,$A,$B,$C,$X,41,11,0xeaa127fa); 248*0Sstevel@tonic-gate &R2(10, 0,$C,$D,$A,$B,$X,42,16,0xd4ef3085); 249*0Sstevel@tonic-gate &R2(11, 0,$B,$C,$D,$A,$X,43,23,0x04881d05); 250*0Sstevel@tonic-gate &R2(12, 0,$A,$B,$C,$D,$X,44, 4,0xd9d4d039); 251*0Sstevel@tonic-gate &R2(13, 0,$D,$A,$B,$C,$X,45,11,0xe6db99e5); 252*0Sstevel@tonic-gate &R2(14, 0,$C,$D,$A,$B,$X,46,16,0x1fa27cf8); 253*0Sstevel@tonic-gate &R2(15, 1,$B,$C,$D,$A,$X,47,23,0xc4ac5665); 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate &comment(""); 256*0Sstevel@tonic-gate &comment("R3 section"); 257*0Sstevel@tonic-gate &R3(-1,$A,$B,$C,$D,$X,48, 6,0xf4292244); 258*0Sstevel@tonic-gate &R3( 0,$D,$A,$B,$C,$X,49,10,0x432aff97); 259*0Sstevel@tonic-gate &R3( 0,$C,$D,$A,$B,$X,50,15,0xab9423a7); 260*0Sstevel@tonic-gate &R3( 0,$B,$C,$D,$A,$X,51,21,0xfc93a039); 261*0Sstevel@tonic-gate &R3( 0,$A,$B,$C,$D,$X,52, 6,0x655b59c3); 262*0Sstevel@tonic-gate &R3( 0,$D,$A,$B,$C,$X,53,10,0x8f0ccc92); 263*0Sstevel@tonic-gate &R3( 0,$C,$D,$A,$B,$X,54,15,0xffeff47d); 264*0Sstevel@tonic-gate &R3( 0,$B,$C,$D,$A,$X,55,21,0x85845dd1); 265*0Sstevel@tonic-gate &R3( 0,$A,$B,$C,$D,$X,56, 6,0x6fa87e4f); 266*0Sstevel@tonic-gate &R3( 0,$D,$A,$B,$C,$X,57,10,0xfe2ce6e0); 267*0Sstevel@tonic-gate &R3( 0,$C,$D,$A,$B,$X,58,15,0xa3014314); 268*0Sstevel@tonic-gate &R3( 0,$B,$C,$D,$A,$X,59,21,0x4e0811a1); 269*0Sstevel@tonic-gate &R3( 0,$A,$B,$C,$D,$X,60, 6,0xf7537e82); 270*0Sstevel@tonic-gate &R3( 0,$D,$A,$B,$C,$X,61,10,0xbd3af235); 271*0Sstevel@tonic-gate &R3( 0,$C,$D,$A,$B,$X,62,15,0x2ad7d2bb); 272*0Sstevel@tonic-gate &R3( 2,$B,$C,$D,$A,$X,63,21,0xeb86d391); 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate # &mov($tmp2,&wparam(0)); # done in the last R3 275*0Sstevel@tonic-gate # &mov($tmp1, &DWP( 0,$tmp2,"",0)); # done is the last R3 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate &add($A,$tmp1); 278*0Sstevel@tonic-gate &mov($tmp1, &DWP( 4,$tmp2,"",0)); 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate &add($B,$tmp1); 281*0Sstevel@tonic-gate &mov($tmp1, &DWP( 8,$tmp2,"",0)); 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gate &add($C,$tmp1); 284*0Sstevel@tonic-gate &mov($tmp1, &DWP(12,$tmp2,"",0)); 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate &add($D,$tmp1); 287*0Sstevel@tonic-gate &mov(&DWP( 0,$tmp2,"",0),$A); 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate &mov(&DWP( 4,$tmp2,"",0),$B); 290*0Sstevel@tonic-gate &mov($tmp1,&swtmp(0)) unless $normal; 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gate &mov(&DWP( 8,$tmp2,"",0),$C); 293*0Sstevel@tonic-gate &mov(&DWP(12,$tmp2,"",0),$D); 294*0Sstevel@tonic-gate 295*0Sstevel@tonic-gate &cmp($tmp1,$X) unless $normal; # check count 296*0Sstevel@tonic-gate &jae(&label("start")) unless $normal; 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate &pop("eax"); # pop the temp variable off the stack 299*0Sstevel@tonic-gate &pop("ebx"); 300*0Sstevel@tonic-gate &pop("ebp"); 301*0Sstevel@tonic-gate &pop("edi"); 302*0Sstevel@tonic-gate &pop("esi"); 303*0Sstevel@tonic-gate &ret(); 304*0Sstevel@tonic-gate &function_end_B($name); 305*0Sstevel@tonic-gate } 306*0Sstevel@tonic-gate 307