next up previous contents
Next: VHDL code for brute-force Up: Tiger code Previous: The TEA algorithm

The RC5 algorithm

 
let
  /* RC5 algorithm "magic numbers" */
  var P:= 0xb7e15163     /* base of ln   - 2 */
  var Q:= 0x9e3779b9     /* golden ratio - 1 */

  var L0:=k0()            		/* Keys to be tested */
  var L1:=k1()

  var P0:= 0x20656854 ^ 0xC93C8C23        /* plain text */
  var P1:= 0x6e6b6e75 ^ 0x9e9ffdb0  

  var C0:= 0xD28688BF            	/* Cipher text */
  var C1:= 0x1C8450A9

  var S00:=P            		/* initialize constant array */
  var S01:=S00+Q
  var S02:=S01+Q
  var S03:=S02+Q
  var S04:=S03+Q
  var S05:=S04+Q
  var S06:=S05+Q
  var S07:=S06+Q
  var S08:=S07+Q
  var S09:=S08+Q
  var S10:=S09+Q
  var S11:=S10+Q
  var S12:=S11+Q
  var S13:=S12+Q
  var S14:=S13+Q
  var S15:=S14+Q
  var S16:=S15+Q
  var S17:=S16+Q
  var S18:=S17+Q
  var S19:=S18+Q
  var S20:=S19+Q
  var S21:=S20+Q
  var S22:=S21+Q
  var S23:=S22+Q
  var S24:=S23+Q
  var S25:=S24+Q
            			/* calculate key expansion */
  var A:=0
  var B:=0
in
  ( for i:=1 to 3 do (
     S00 := (S00+(A+B)) <<< 3;            A := S00;
     L0  := (L0 +(A+B)) <<< (A+B);        B := L0 ;

     S01 := (S01+(A+B)) <<< 3;            A := S01;
     L1  := (L1 +(A+B)) <<< (A+B);        B := L1 ;

/**/
     S02 := (S02+(A+B)) <<< 3;            A := S02;
     L0  := (L0 +(A+B)) <<< (A+B);        B := L0 ;

     S03 := (S03+(A+B)) <<< 3;            A := S03;
     L1  := (L1 +(A+B)) <<< (A+B);        B := L1 ;

/**/
     S04 := (S04+(A+B)) <<< 3;            A := S04;
     L0  := (L0 +(A+B)) <<< (A+B);        B := L0 ;

     S05 := (S05+(A+B)) <<< 3;            A := S05;
     L1  := (L1 +(A+B)) <<< (A+B);        B := L1 ;

/**/
     S06 := (S06+(A+B)) <<< 3;            A := S06;
     L0  := (L0 +(A+B)) <<< (A+B);        B := L0 ;

     S07 := (S07+(A+B)) <<< 3;            A := S07;
     L1  := (L1 +(A+B)) <<< (A+B);        B := L1 ;

/**/
     S08 := (S08+(A+B)) <<< 3;            A := S08;
     L0  := (L0 +(A+B)) <<< (A+B);        B := L0 ;

     S09 := (S09+(A+B)) <<< 3;            A := S09;
     L1  := (L1 +(A+B)) <<< (A+B);        B := L1 ;

/**/
     S10 := (S10+(A+B)) <<< 3;            A := S10;
     L0  := (L0 +(A+B)) <<< (A+B);        B := L0 ;

     S11 := (S11+(A+B)) <<< 3;            A := S11;
     L1  := (L1 +(A+B)) <<< (A+B);        B := L1 ;

/**/
     S12 := (S12+(A+B)) <<< 3;            A := S12;
     L0  := (L0 +(A+B)) <<< (A+B);        B := L0 ;

     S13 := (S13+(A+B)) <<< 3;            A := S13;
     L1  := (L1 +(A+B)) <<< (A+B);        B := L1 ;

/**/
     S14 := (S14+(A+B)) <<< 3;            A := S14;
     L0  := (L0 +(A+B)) <<< (A+B);        B := L0 ;

     S15 := (S15+(A+B)) <<< 3;            A := S15;
     L1  := (L1 +(A+B)) <<< (A+B);        B := L1 ;

/**/
     S16 := (S16+(A+B)) <<< 3;            A := S16;
     L0  := (L0 +(A+B)) <<< (A+B);        B := L0 ;

     S17 := (S17+(A+B)) <<< 3;            A := S17;
     L1  := (L1 +(A+B)) <<< (A+B);        B := L1 ;

/**/
     S18 := (S18+(A+B)) <<< 3;            A := S18;
     L0  := (L0 +(A+B)) <<< (A+B);        B := L0 ;

     S19 := (S19+(A+B)) <<< 3;            A := S19;
     L1  := (L1 +(A+B)) <<< (A+B);        B := L1 ;

/**/
     S20 := (S20+(A+B)) <<< 3;            A := S20;
     L0  := (L0 +(A+B)) <<< (A+B);        B := L0 ;

     S21 := (S21+(A+B)) <<< 3;            A := S21;
     L1  := (L1 +(A+B)) <<< (A+B);        B := L1 ;

/**/
     S22 := (S22+(A+B)) <<< 3;            A := S22;
     L0  := (L0 +(A+B)) <<< (A+B);        B := L0 ;

     S23 := (S23+(A+B)) <<< 3;            A := S23;
     L1  := (L1 +(A+B)) <<< (A+B);        B := L1 ;

/**/
     S24 := (S24+(A+B)) <<< 3;            A := S24;
     L0  := (L0 +(A+B)) <<< (A+B);        B := L0 ;

     S25 := (S25+(A+B)) <<< 3;            A := S25;
     L1  := (L1 +(A+B)) <<< (A+B);        B := L1
  );

  B:=C1; A:=C0;            	/* now decrypt */

  B:= ((B - S25) >>> A) ^ A;
  A:= ((A - S24) >>> B) ^ B;

  B:= ((B - S23) >>> A) ^ A;
  A:= ((A - S22) >>> B) ^ B;

  B:= ((B - S21) >>> A) ^ A;
  A:= ((A - S20) >>> B) ^ B;

  B:= ((B - S19) >>> A) ^ A;
  A:= ((A - S18) >>> B) ^ B;

  B:= ((B - S17) >>> A) ^ A;
  A:= ((A - S16) >>> B) ^ B;

  B:= ((B - S15) >>> A) ^ A;
  A:= ((A - S14) >>> B) ^ B;

  B:= ((B - S13) >>> A) ^ A;
  A:= ((A - S12) >>> B) ^ B;

  B:= ((B - S11) >>> A) ^ A;
  A:= ((A - S10) >>> B) ^ B;

  B:= ((B - S09) >>> A) ^ A;
  A:= ((A - S08) >>> B) ^ B;

  B:= ((B - S07) >>> A) ^ A;
  A:= ((A - S06) >>> B) ^ B;

  B:= ((B - S05) >>> A) ^ A;
  A:= ((A - S04) >>> B) ^ B;

  B:= ((B - S03) >>> A) ^ A;
  A:= ((A - S02) >>> B) ^ B;

  (P1 = B-S01) && (P0 = A-S00)        /* test for result equality */
)
end



C. Scott Ananian
10/11/1997