private int[] makeWorkingKey(byte[] key) throws IllegalArgumentException {
  int[] kPad=new int[16];
  int off=0;
  int length=0;
  for (off=key.length - 4; off > 0; off-=4) {
    kPad[length++]=bytesToWord(key,off);
  }
  if (off == 0) {
    kPad[length++]=bytesToWord(key,0);
    if (length < 8) {
      kPad[length]=1;
    }
  }
 else {
    throw new IllegalArgumentException("key must be a multiple of 4 bytes");
  }
  int amount=(ROUNDS + 1) * 4;
  int[] w=new int[amount];
  for (int i=8; i < 16; i++) {
    kPad[i]=rotateLeft(kPad[i - 8] ^ kPad[i - 5] ^ kPad[i - 3]^ kPad[i - 1]^ PHI^ (i - 8),11);
  }
  System.arraycopy(kPad,8,w,0,8);
  for (int i=8; i < amount; i++) {
    w[i]=rotateLeft(w[i - 8] ^ w[i - 5] ^ w[i - 3]^ w[i - 1]^ PHI^ i,11);
  }
  sb3(w[0],w[1],w[2],w[3]);
  w[0]=X0;
  w[1]=X1;
  w[2]=X2;
  w[3]=X3;
  sb2(w[4],w[5],w[6],w[7]);
  w[4]=X0;
  w[5]=X1;
  w[6]=X2;
  w[7]=X3;
  sb1(w[8],w[9],w[10],w[11]);
  w[8]=X0;
  w[9]=X1;
  w[10]=X2;
  w[11]=X3;
  sb0(w[12],w[13],w[14],w[15]);
  w[12]=X0;
  w[13]=X1;
  w[14]=X2;
  w[15]=X3;
  sb7(w[16],w[17],w[18],w[19]);
  w[16]=X0;
  w[17]=X1;
  w[18]=X2;
  w[19]=X3;
  sb6(w[20],w[21],w[22],w[23]);
  w[20]=X0;
  w[21]=X1;
  w[22]=X2;
  w[23]=X3;
  sb5(w[24],w[25],w[26],w[27]);
  w[24]=X0;
  w[25]=X1;
  w[26]=X2;
  w[27]=X3;
  sb4(w[28],w[29],w[30],w[31]);
  w[28]=X0;
  w[29]=X1;
  w[30]=X2;
  w[31]=X3;
  sb3(w[32],w[33],w[34],w[35]);
  w[32]=X0;
  w[33]=X1;
  w[34]=X2;
  w[35]=X3;
  sb2(w[36],w[37],w[38],w[39]);
  w[36]=X0;
  w[37]=X1;
  w[38]=X2;
  w[39]=X3;
  sb1(w[40],w[41],w[42],w[43]);
  w[40]=X0;
  w[41]=X1;
  w[42]=X2;
  w[43]=X3;
  sb0(w[44],w[45],w[46],w[47]);
  w[44]=X0;
  w[45]=X1;
  w[46]=X2;
  w[47]=X3;
  sb7(w[48],w[49],w[50],w[51]);
  w[48]=X0;
  w[49]=X1;
  w[50]=X2;
  w[51]=X3;
  sb6(w[52],w[53],w[54],w[55]);
  w[52]=X0;
  w[53]=X1;
  w[54]=X2;
  w[55]=X3;
  sb5(w[56],w[57],w[58],w[59]);
  w[56]=X0;
  w[57]=X1;
  w[58]=X2;
  w[59]=X3;
  sb4(w[60],w[61],w[62],w[63]);
  w[60]=X0;
  w[61]=X1;
  w[62]=X2;
  w[63]=X3;
  sb3(w[64],w[65],w[66],w[67]);
  w[64]=X0;
  w[65]=X1;
  w[66]=X2;
  w[67]=X3;
  sb2(w[68],w[69],w[70],w[71]);
  w[68]=X0;
  w[69]=X1;
  w[70]=X2;
  w[71]=X3;
  sb1(w[72],w[73],w[74],w[75]);
  w[72]=X0;
  w[73]=X1;
  w[74]=X2;
  w[75]=X3;
  sb0(w[76],w[77],w[78],w[79]);
  w[76]=X0;
  w[77]=X1;
  w[78]=X2;
  w[79]=X3;
  sb7(w[80],w[81],w[82],w[83]);
  w[80]=X0;
  w[81]=X1;
  w[82]=X2;
  w[83]=X3;
  sb6(w[84],w[85],w[86],w[87]);
  w[84]=X0;
  w[85]=X1;
  w[86]=X2;
  w[87]=X3;
  sb5(w[88],w[89],w[90],w[91]);
  w[88]=X0;
  w[89]=X1;
  w[90]=X2;
  w[91]=X3;
  sb4(w[92],w[93],w[94],w[95]);
  w[92]=X0;
  w[93]=X1;
  w[94]=X2;
  w[95]=X3;
  sb3(w[96],w[97],w[98],w[99]);
  w[96]=X0;
  w[97]=X1;
  w[98]=X2;
  w[99]=X3;
  sb2(w[100],w[101],w[102],w[103]);
  w[100]=X0;
  w[101]=X1;
  w[102]=X2;
  w[103]=X3;
  sb1(w[104],w[105],w[106],w[107]);
  w[104]=X0;
  w[105]=X1;
  w[106]=X2;
  w[107]=X3;
  sb0(w[108],w[109],w[110],w[111]);
  w[108]=X0;
  w[109]=X1;
  w[110]=X2;
  w[111]=X3;
  sb7(w[112],w[113],w[114],w[115]);
  w[112]=X0;
  w[113]=X1;
  w[114]=X2;
  w[115]=X3;
  sb6(w[116],w[117],w[118],w[119]);
  w[116]=X0;
  w[117]=X1;
  w[118]=X2;
  w[119]=X3;
  sb5(w[120],w[121],w[122],w[123]);
  w[120]=X0;
  w[121]=X1;
  w[122]=X2;
  w[123]=X3;
  sb4(w[124],w[125],w[126],w[127]);
  w[124]=X0;
  w[125]=X1;
  w[126]=X2;
  w[127]=X3;
  sb3(w[128],w[129],w[130],w[131]);
  w[128]=X0;
  w[129]=X1;
  w[130]=X2;
  w[131]=X3;
  return w;
}

private int [ ] makeWorkingKey ( byte [ ] key ) throws IllegalArgumentException { int [ ] kPad = new int [ 16 ] ; int off = 0 ; int length = 0 ; for ( off = key . length - 4 ; off > 0 ; off -= 4 ) { kPad [ length ++ ] = bytesToWord ( key , off ) ; } if ( off == 0 ) { kPad [ length ++ ] = bytesToWord ( key , 0 ) ; if ( length < 8 ) { kPad [ length ] = 1 ; } } else { throw new IllegalArgumentException ( "key must be a multiple of 4 bytes" ) ; } int amount = ( ROUNDS + 1 ) * 4 ; int [ ] w = new int [ amount ] ; for ( int i = 8 ; i < 16 ; i ++ ) { kPad [ i ] = rotateLeft ( kPad [ i - 8 ] ^ kPad [ i - 5 ] ^ kPad [ i - 3 ] ^ kPad [ i - 1 ] ^ PHI ^ ( i - 8 ) , 11 ) ; } System . arraycopy ( kPad , 8 , w , 0 , 8 ) ; for ( int i = 8 ; i < amount ; i ++ ) { w [ i ] = rotateLeft ( w [ i - 8 ] ^ w [ i - 5 ] ^ w [ i - 3 ] ^ w [ i - 1 ] ^ PHI ^ i , 11 ) ; } sb3 ( w [ 0 ] , w [ 1 ] , w [ 2 ] , w [ 3 ] ) ; w [ 0 ] = X0 ; w [ 1 ] = X1 ; w [ 2 ] = X2 ; w [ 3 ] = X3 ; sb2 ( w [ 4 ] , w [ 5 ] , w [ 6 ] , w [ 7 ] ) ; w [ 4 ] = X0 ; w [ 5 ] = X1 ; w [ 6 ] = X2 ; w [ 7 ] = X3 ; sb1 ( w [ 8 ] , w [ 9 ] , w [ 10 ] , w [ 11 ] ) ; w [ 8 ] = X0 ; w [ 9 ] = X1 ; w [ 10 ] = X2 ; w [ 11 ] = X3 ; sb0 ( w [ 12 ] , w [ 13 ] , w [ 14 ] , w [ 15 ] ) ; w [ 12 ] = X0 ; w [ 13 ] = X1 ; w [ 14 ] = X2 ; w [ 15 ] = X3 ; sb7 ( w [ 16 ] , w [ 17 ] , w [ 18 ] , w [ 19 ] ) ; w [ 16 ] = X0 ; w [ 17 ] = X1 ; w [ 18 ] = X2 ; w [ 19 ] = X3 ; sb6 ( w [ 20 ] , w [ 21 ] , w [ 22 ] , w [ 23 ] ) ; w [ 20 ] = X0 ; w [ 21 ] = X1 ; w [ 22 ] = X2 ; w [ 23 ] = X3 ; sb5 ( w [ 24 ] , w [ 25 ] , w [ 26 ] , w [ 27 ] ) ; w [ 24 ] = X0 ; w [ 25 ] = X1 ; w [ 26 ] = X2 ; w [ 27 ] = X3 ; sb4 ( w [ 28 ] , w [ 29 ] , w [ 30 ] , w [ 31 ] ) ; w [ 28 ] = X0 ; w [ 29 ] = X1 ; w [ 30 ] = X2 ; w [ 31 ] = X3 ; sb3 ( w [ 32 ] , w [ 33 ] , w [ 34 ] , w [ 35 ] ) ; w [ 32 ] = X0 ; w [ 33 ] = X1 ; w [ 34 ] = X2 ; w [ 35 ] = X3 ; sb2 ( w [ 36 ] , w [ 37 ] , w [ 38 ] , w [ 39 ] ) ; w [ 36 ] = X0 ; w [ 37 ] = X1 ; w [ 38 ] = X2 ; w [ 39 ] = X3 ; sb1 ( w [ 40 ] , w [ 41 ] , w [ 42 ] , w [ 43 ] ) ; w [ 40 ] = X0 ; w [ 41 ] = X1 ; w [ 42 ] = X2 ; w [ 43 ] = X3 ; sb0 ( w [ 44 ] , w [ 45 ] , w [ 46 ] , w [ 47 ] ) ; w [ 44 ] = X0 ; w [ 45 ] = X1 ; w [ 46 ] = X2 ; w [ 47 ] = X3 ; sb7 ( w [ 48 ] , w [ 49 ] , w [ 50 ] , w [ 51 ] ) ; w [ 48 ] = X0 ; w [ 49 ] = X1 ; w [ 50 ] = X2 ; w [ 51 ] = X3 ; sb6 ( w [ 52 ] , w [ 53 ] , w [ 54 ] , w [ 55 ] ) ; w [ 52 ] = X0 ; w [ 53 ] = X1 ; w [ 54 ] = X2 ; w [ 55 ] = X3 ; sb5 ( w [ 56 ] , w [ 57 ] , w [ 58 ] , w [ 59 ] ) ; w [ 56 ] = X0 ; w [ 57 ] = X1 ; w [ 58 ] = X2 ; w [ 59 ] = X3 ; sb4 ( w [ 60 ] , w [ 61 ] , w [ 62 ] , w [ 63 ] ) ; w [ 60 ] = X0 ; w [ 61 ] = X1 ; w [ 62 ] = X2 ; w [ 63 ] = X3 ; sb3 ( w [ 64 ] , w [ 65 ] , w [ 66 ] , w [ 67 ] ) ; w [ 64 ] = X0 ; w [ 65 ] = X1 ; w [ 66 ] = X2 ; w [ 67 ] = X3 ; sb2 ( w [ 68 ] , w [ 69 ] , w [ 70 ] , w [ 71 ] ) ; w [ 68 ] = X0 ; w [ 69 ] = X1 ; w [ 70 ] = X2 ; w [ 71 ] = X3 ; sb1 ( w [ 72 ] , w [ 73 ] , w [ 74 ] , w [ 75 ] ) ; w [ 72 ] = X0 ; w [ 73 ] = X1 ; w [ 74 ] = X2 ; w [ 75 ] = X3 ; sb0 ( w [ 76 ] , w [ 77 ] , w [ 78 ] , w [ 79 ] ) ; w [ 76 ] = X0 ; w [ 77 ] = X1 ; w [ 78 ] = X2 ; w [ 79 ] = X3 ; sb7 ( w [ 80 ] , w [ 81 ] , w [ 82 ] , w [ 83 ] ) ; w [ 80 ] = X0 ; w [ 81 ] = X1 ; w [ 82 ] = X2 ; w [ 83 ] = X3 ; sb6 ( w [ 84 ] , w [ 85 ] , w [ 86 ] , w [ 87 ] ) ; w [ 84 ] = X0 ; w [ 85 ] = X1 ; w [ 86 ] = X2 ; w [ 87 ] = X3 ; sb5 ( w [ 88 ] , w [ 89 ] , w [ 90 ] , w [ 91 ] ) ; w [ 88 ] = X0 ; w [ 89 ] = X1 ; w [ 90 ] = X2 ; w [ 91 ] = X3 ; sb4 ( w [ 92 ] , w [ 93 ] , w [ 94 ] , w [ 95 ] ) ; w [ 92 ] = X0 ; w [ 93 ] = X1 ; w [ 94 ] = X2 ; w [ 95 ] = X3 ; sb3 ( w [ 96 ] , w [ 97 ] , w [ 98 ] , w [ 99 ] ) ; w [ 96 ] = X0 ; w [ 97 ] = X1 ; w [ 98 ] = X2 ; w [ 99 ] = X3 ; sb2 ( w [ 100 ] , w [ 101 ] , w [ 102 ] , w [ 103 ] ) ; w [ 100 ] = X0 ; w [ 101 ] = X1 ; w [ 102 ] = X2 ; w [ 103 ] = X3 ; sb1 ( w [ 104 ] , w [ 105 ] , w [ 106 ] , w [ 107 ] ) ; w [ 104 ] = X0 ; w [ 105 ] = X1 ; w [ 106 ] = X2 ; w [ 107 ] = X3 ; sb0 ( w [ 108 ] , w [ 109 ] , w [ 110 ] , w [ 111 ] ) ; w [ 108 ] = X0 ; w [ 109 ] = X1 ; w [ 110 ] = X2 ; w [ 111 ] = X3 ; sb7 ( w [ 112 ] , w [ 113 ] , w [ 114 ] , w [ 115 ] ) ; w [ 112 ] = X0 ; w [ 113 ] = X1 ; w [ 114 ] = X2 ; w [ 115 ] = X3 ; sb6 ( w [ 116 ] , w [ 117 ] , w [ 118 ] , w [ 119 ] ) ; w [ 116 ] = X0 ; w [ 117 ] = X1 ; w [ 118 ] = X2 ; w [ 119 ] = X3 ; sb5 ( w [ 120 ] , w [ 121 ] , w [ 122 ] , w [ 123 ] ) ; w [ 120 ] = X0 ; w [ 121 ] = X1 ; w [ 122 ] = X2 ; w [ 123 ] = X3 ; sb4 ( w [ 124 ] , w [ 125 ] , w [ 126 ] , w [ 127 ] ) ; w [ 124 ] = X0 ; w [ 125 ] = X1 ; w [ 126 ] = X2 ; w [ 127 ] = X3 ; sb3 ( w [ 128 ] , w [ 129 ] , w [ 130 ] , w [ 131 ] ) ; w [ 128 ] = X0 ; w [ 129 ] = X1 ; w [ 130 ] = X2 ; w [ 131 ] = X3 ; return w ; }
expand a user - supplied key material into a session key .
