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