1*0Sstevel@tonic-gate#!/usr/local/bin/perl 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gatepush(@INC,"perlasm","../../perlasm"); 4*0Sstevel@tonic-gaterequire "x86asm.pl"; 5*0Sstevel@tonic-gaterequire "cbc.pl"; 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gate&asm_init($ARGV[0],"rc5-586.pl"); 8*0Sstevel@tonic-gate 9*0Sstevel@tonic-gate$RC5_MAX_ROUNDS=16; 10*0Sstevel@tonic-gate$RC5_32_OFF=($RC5_MAX_ROUNDS+2)*4; 11*0Sstevel@tonic-gate$A="edi"; 12*0Sstevel@tonic-gate$B="esi"; 13*0Sstevel@tonic-gate$S="ebp"; 14*0Sstevel@tonic-gate$tmp1="eax"; 15*0Sstevel@tonic-gate$r="ebx"; 16*0Sstevel@tonic-gate$tmpc="ecx"; 17*0Sstevel@tonic-gate$tmp4="edx"; 18*0Sstevel@tonic-gate 19*0Sstevel@tonic-gate&RC5_32_encrypt("RC5_32_encrypt",1); 20*0Sstevel@tonic-gate&RC5_32_encrypt("RC5_32_decrypt",0); 21*0Sstevel@tonic-gate&cbc("RC5_32_cbc_encrypt","RC5_32_encrypt","RC5_32_decrypt",0,4,5,3,-1,-1); 22*0Sstevel@tonic-gate&asm_finish(); 23*0Sstevel@tonic-gate 24*0Sstevel@tonic-gatesub RC5_32_encrypt 25*0Sstevel@tonic-gate { 26*0Sstevel@tonic-gate local($name,$enc)=@_; 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate &function_begin_B($name,""); 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate &comment(""); 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate &push("ebp"); 33*0Sstevel@tonic-gate &push("esi"); 34*0Sstevel@tonic-gate &push("edi"); 35*0Sstevel@tonic-gate &mov($tmp4,&wparam(0)); 36*0Sstevel@tonic-gate &mov($S,&wparam(1)); 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate &comment("Load the 2 words"); 39*0Sstevel@tonic-gate &mov($A,&DWP(0,$tmp4,"",0)); 40*0Sstevel@tonic-gate &mov($B,&DWP(4,$tmp4,"",0)); 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate &push($r); 43*0Sstevel@tonic-gate &mov($r, &DWP(0,$S,"",0)); 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate # encrypting part 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate if ($enc) 48*0Sstevel@tonic-gate { 49*0Sstevel@tonic-gate &add($A, &DWP(4+0,$S,"",0)); 50*0Sstevel@tonic-gate &add($B, &DWP(4+4,$S,"",0)); 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate for ($i=0; $i<$RC5_MAX_ROUNDS; $i++) 53*0Sstevel@tonic-gate { 54*0Sstevel@tonic-gate &xor($A, $B); 55*0Sstevel@tonic-gate &mov($tmp1, &DWP(12+$i*8,$S,"",0)); 56*0Sstevel@tonic-gate &mov($tmpc, $B); 57*0Sstevel@tonic-gate &rotl($A, &LB("ecx")); 58*0Sstevel@tonic-gate &add($A, $tmp1); 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate &xor($B, $A); 61*0Sstevel@tonic-gate &mov($tmp1, &DWP(16+$i*8,$S,"",0)); 62*0Sstevel@tonic-gate &mov($tmpc, $A); 63*0Sstevel@tonic-gate &rotl($B, &LB("ecx")); 64*0Sstevel@tonic-gate &add($B, $tmp1); 65*0Sstevel@tonic-gate if (($i == 7) || ($i == 11)) 66*0Sstevel@tonic-gate { 67*0Sstevel@tonic-gate &cmp($r, $i+1); 68*0Sstevel@tonic-gate &je(&label("rc5_exit")); 69*0Sstevel@tonic-gate } 70*0Sstevel@tonic-gate } 71*0Sstevel@tonic-gate } 72*0Sstevel@tonic-gate else 73*0Sstevel@tonic-gate { 74*0Sstevel@tonic-gate &cmp($r, 12); 75*0Sstevel@tonic-gate &je(&label("rc5_dec_12")); 76*0Sstevel@tonic-gate &cmp($r, 8); 77*0Sstevel@tonic-gate &je(&label("rc5_dec_8")); 78*0Sstevel@tonic-gate for ($i=$RC5_MAX_ROUNDS; $i > 0; $i--) 79*0Sstevel@tonic-gate { 80*0Sstevel@tonic-gate &set_label("rc5_dec_$i") if ($i == 12) || ($i == 8); 81*0Sstevel@tonic-gate &mov($tmp1, &DWP($i*8+8,$S,"",0)); 82*0Sstevel@tonic-gate &sub($B, $tmp1); 83*0Sstevel@tonic-gate &mov($tmpc, $A); 84*0Sstevel@tonic-gate &rotr($B, &LB("ecx")); 85*0Sstevel@tonic-gate &xor($B, $A); 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate &mov($tmp1, &DWP($i*8+4,$S,"",0)); 88*0Sstevel@tonic-gate &sub($A, $tmp1); 89*0Sstevel@tonic-gate &mov($tmpc, $B); 90*0Sstevel@tonic-gate &rotr($A, &LB("ecx")); 91*0Sstevel@tonic-gate &xor($A, $B); 92*0Sstevel@tonic-gate } 93*0Sstevel@tonic-gate &sub($B, &DWP(4+4,$S,"",0)); 94*0Sstevel@tonic-gate &sub($A, &DWP(4+0,$S,"",0)); 95*0Sstevel@tonic-gate } 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate &set_label("rc5_exit"); 98*0Sstevel@tonic-gate &mov(&DWP(0,$tmp4,"",0),$A); 99*0Sstevel@tonic-gate &mov(&DWP(4,$tmp4,"",0),$B); 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate &pop("ebx"); 102*0Sstevel@tonic-gate &pop("edi"); 103*0Sstevel@tonic-gate &pop("esi"); 104*0Sstevel@tonic-gate &pop("ebp"); 105*0Sstevel@tonic-gate &ret(); 106*0Sstevel@tonic-gate &function_end_B($name); 107*0Sstevel@tonic-gate } 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate 110