Ejemplo n.º 1
0
 /**
  * Expand a 32 byte key, the size it is expanded to varies
  * based on the block size of the Rijndael implementation chosen
  *
  * @return void
  */
 private function expandKey256()
 {
     // clear the xkey, we're creating a new one
     $this->xkey = "";
     $max = 0;
     // the number of rounds we make depends on the block size of the text
     // used during encryption/decryption
     if ($this->blockSize() == 16) {
         $max = 60;
     }
     if ($this->blockSize() == 24) {
         $max = 90;
     }
     if ($this->blockSize() == 32) {
         $max = 120;
     }
     // 32 byte key expands to 240 bytes
     for ($i = 0; $i < $max; ++$i) {
         if ($i >= 0 && $i <= 7) {
             $this->xkey .= $this->k($i * 4);
         } else {
             if ($i % 8 == 0) {
                 // rotate the 4 bytes
                 $subword = $this->rotWord($this->ek(($i - 1) * 4));
                 // apply the sbox
                 $this->subWord($subword);
                 // return 4 byte value based on self::$_rcon table
                 $rcon = $this->rcon($i / 8);
                 // grab 4 bytes from $this->extended_key
                 $ek = $this->ek(($i - 8) * 4);
                 $h1 = parent::str2Hex($subword);
                 $h2 = parent::dec2Hex($rcon);
                 $h3 = parent::str2Hex($ek);
                 $res = parent::xorHex($h1, $h2, $h3);
                 $this->xkey .= parent::hex2Str($res);
             } else {
                 if ($i % 4 == 0) {
                     // get the subsitution from the s-box
                     $subword = $this->ek(($i - 1) * 4);
                     $this->subWord($subword);
                     // get the extended key part
                     $ek = $this->ek(($i - 8) * 4);
                     // xor the two parts
                     $h1 = parent::str2Hex($subword);
                     $h2 = parent::str2Hex($ek);
                     $res = parent::xorHex($h1, $h2);
                     $this->xkey .= parent::hex2Str($res);
                 } else {
                     $h1 = parent::str2Hex($this->ek(($i - 1) * 4));
                     $h2 = parent::str2Hex($this->ek(($i - 8) * 4));
                     $res = parent::xorHex($h1, $h2);
                     $this->xkey .= parent::hex2Str($res);
                 }
             }
         }
     }
     return true;
 }