mode = strtoupper($mode); $this->iv = $iv; $this->Nb = 4; $this->Nk = strlen($z)/4; $this->Nr = $this->Nk + $this->Nb + 2; if ($this->mode != "ECB" && strlen($this->iv) != 16) die("The initialization vector must be 128 bits (or 16 characters) long."); if ($this->Nk != 4 && $this->Nk != 6 && $this->Nk != 8) die("Key is " . ($this->Nk*32) . " bits long. *not* 128, 192, or 256."); $this->Nr = $this->Nk+$this->Nb+2; $this->w = array(); // Nb*(Nr+1) 32-bit words $this->s = array(array()); // 2-D array of Nb colums and 4 rows $this->KeyExpansion($z); // places expanded key in w } /** Encrypts an aribtrary length String. * @params plaintext string * @returns ciphertext string **/ function encrypt($x) { $t = ""; // 16-byte block to hold the temporary input of the cipher $y = ""; // returned cipher text; $y_block = $this->iv; // 16-byte block to hold the temporary output of the cipher $xsize = strlen($x); switch($this->mode) { case "ECB": // put a 16-byte block into t, ecnrypt it and add it to the result for ($i=0; $i<$xsize; $i+=16) { for ($j=0; $j<16; $j++) { if (($i+$j)<$xsize) { $t[$j] = $x[$i+$j]; } else $t[$j] = chr(0); } $y_block = $this->encryptBlock($t); $y .= $y_block; } break; case "CBC": // put a 16-byte block into t, ecnrypt it and add it to the result for ($i=0; $i<$xsize; $i+=16) { for ($j=0; $j<16; $j++) { // XOR this block of plaintext with the initialization vector $t[$j] = chr(ord(($i+$j)<$xsize ? $x[$i+$j] : chr(0)) ^ ord($y_block[$j])); } $y_block = $this->encryptBlock($t); $y .= $y_block; } break; case "CFB": for ($i=0; $i<$xsize; $i+=16) { // Encrypt the initialization vector/cipher output then XOR with the plaintext $y_block = $this->encryptBlock($y_block); for ($j=0; $j<16; $j++) { // XOR the cipher output with the plaintext. $y_block[$j] = chr(ord(($i+$j)<$xsize ? $x[$i+$j] : chr(0)) ^ ord($y_block[$j])); } $y .= $y_block; } break; case "OFB": for ($i=0; $i<$xsize; $i+=16) { // Encrypt the initialization vector/cipher output then XOR with the plaintext $t = $this->encryptBlock($y_block); for ($j=0; $j<16; $j++) { // XOR the cipher output with the plaintext. $y_block[$j] = chr(ord(($i+$j)<$xsize ? $x[$i+$j] : chr(0)) ^ ord($t[$j])); } $y .= $y_block; $y_block = $t; } break; } return $y; } /** Decrypts an aribtrary length String. * @params ciphertext string * @returns plaintext string **/ function decrypt($y) { $t = ""; // 16-byte block $x = ""; // returned plain text; $y_block = $this->iv; $x_block = ""; // put a 16-byte block into t $ysize = strlen($y); switch($this->mode) { case "ECB": for ($i=0; $i<$ysize; $i+=16) { for ($j=0; $j<16; $j++) { if (($i+$j)<$ysize) $t[$j] = $y[$i+$j]; else $t[$j] = chr(0); } $x_block = $this->decryptBlock($t); $x .= $x_block; } break; case "CBC": for ($i=0; $i<$ysize; $i+=16) { for ($j=0; $j<16; $j++) { if (($i+$j)<$ysize) $t[$j] = $y[$i+$j]; else $t[$j] = chr(0); } $x_block = $this->decryptBlock($t); // XOR the iv/previous cipher block with this decrypted cipher block for ($j=0; $j<16; $j++) $x_block[$j] = chr(ord($x_block[$j]) ^ ord($y_block[$j])); $y_block = $t; $x .= $x_block; } break; case "CFB": for ($i=0; $i<$ysize; $i+=16) { // Encrypt the initialization vector/cipher output then XOR with the ciphertext $x_block = $this->encryptBlock($y_block); for ($j=0; $j<16; $j++) { // XOR the cipher output with the ciphertext. $x_block[$j] = chr(ord(($i+$j)<$ysize ? $y[$i+$j] : chr(0)) ^ ord($x_block[$j])); $y_block[$j] = $y[$i+$j]; } $x .= $x_block; } break; case "OFB": $x = $this->encrypt($y); break; } return rtrim($x, chr(0)); // Remove any buffer residue on return. } /** Encrypts the 16-byte plain text. * @params 16-byte plaintext string * @returns 16-byte ciphertext string **/ function encryptBlock($x) { $y = ""; // 16-byte string // place input x into the initial state matrix in column order for ($i=0; $i<4*$this->Nb; $i++) { // we want integerger division for the second index $this->s[$i%4][($i-$i%$this->Nb)/$this->Nb] = ord($x[$i]); } // add round key $this->addRoundKey(0); for ($i=1; $i<$this->Nr; $i++) { // substitute bytes $this->subBytes(); // shift rows $this->shiftRows(); // mix columns $this->mixColumns(); // add round key $this->addRoundKey($i); } // substitute bytes $this->subBytes(); // shift rows $this->shiftRows(); // add round key $this->addRoundKey($i); // place state matrix s into y in column order for ($i=0; $i<4*$this->Nb; $i++) $y .= chr($this->s[$i%4][($i-$i%$this->Nb)/$this->Nb]); return $y; } /** Decrypts the 16-byte cipher text. * @params 16-byte ciphertext string * @returns 16-byte plaintext string **/ function decryptBlock($y) { $x = ""; // 16-byte string // place input y into the initial state matrix in column order for ($i=0; $i<4*$this->Nb; $i++) $this->s[$i%4][($i-$i%$this->Nb)/$this->Nb] = ord($y[$i]); // add round key $this->addRoundKey($this->Nr); for ($i=$this->Nr-1; $i>0; $i--) { // inverse shift rows $this->invShiftRows(); // inverse sub bytes $this->invSubBytes(); // add round key $this->addRoundKey($i); // inverse mix columns $this->invMixColumns(); } // inverse shift rows $this->invShiftRows(); // inverse sub bytes $this->invSubBytes(); // add round key $this->addRoundKey($i); // place state matrix s into x in column order for ($i=0; $i<4*$this->Nb; $i++) { // Used to remove filled null characters. //$x .= ($this->s[$i%4][($i-$i%$this->Nb)/$this->Nb] == chr(0) ? "" : chr($this->s[$i%4][($i-$i%$this->Nb)/$this->Nb])); $x .= chr($this->s[$i%4][($i-$i%$this->Nb)/$this->Nb]); } return $x; } function __destruct() { unset($this->w); unset($this->s); } /** makes a big key out of a small one * @returns void **/ function KeyExpansion($z) { // Rcon is the round constant static $Rcon = array( 0x00000000, 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, 0x1b000000, 0x36000000, 0x6c000000, 0xd8000000, 0xab000000, 0x4d000000, 0x9a000000, 0x2f000000 ); $temp = 0; // temporary 32-bit word // the first Nk words of w are the cipher key z for ($i=0; $i<$this->Nk; $i++) { $this->w[$i] = 0; // fill an entire word of expanded key w // by pushing 4 bytes into the w[i] word $this->w[$i] = ord($z[4*$i]); // add a byte in $this->w[$i] <<= 8; // make room for the next byte $this->w[$i] += ord($z[4*$i+1]); $this->w[$i] <<= 8; $this->w[$i] += ord($z[4*$i+2]); $this->w[$i] <<= 8; $this->w[$i] += ord($z[4*$i+3]); } for (; $i<$this->Nb*($this->Nr+1); $i++) { $temp = $this->w[$i-1]; if ($i%$this->Nk == 0) $temp = $this->subWord($this->rotWord($temp)) ^ $Rcon[$i/$this->Nk]; else if ($this->Nk > 6 && $i%$this->Nk == 4) $temp = $this->subWord($temp); $this->w[$i] = $this->w[$i-$this->Nk] ^ $temp; $this->make32BitWord($this->w[$i]); } } /** adds the key schedule for a round to a state matrix. * @returns void **/ function addRoundKey($round) { $temp = ""; for ($i=0; $i<4; $i++) { for ($j=0; $j<$this->Nb; $j++) { // place the i-th byte of the j-th word from expanded key w into temp $temp = $this->w[$round*$this->Nb+$j] >> (3-$i)*8; // Cast temp from a 32-bit word into an 8-bit byte. $temp %= 256; // Can't do unsigned shifts, so we need to make this temp positive $temp = ($temp < 0 ? (256 + $temp) : $temp); $this->s[$i][$j] ^= $temp; // xor temp with the byte at location (i,j) of the state } } } /** unmixes each column of a state matrix. * @returns void **/ function invMixColumns() { $s0 = $s1 = $s2 = $s3= ''; // There are Nb columns for ($i=0; $i<$this->Nb; $i++) { $s0 = $this->s[0][$i]; $s1 = $this->s[1][$i]; $s2 = $this->s[2][$i]; $s3 = $this->s[3][$i]; $this->s[0][$i] = $this->mult(0x0e, $s0) ^ $this->mult(0x0b, $s1) ^ $this->mult(0x0d, $s2) ^ $this->mult(0x09, $s3); $this->s[1][$i] = $this->mult(0x09, $s0) ^ $this->mult(0x0e, $s1) ^ $this->mult(0x0b, $s2) ^ $this->mult(0x0d, $s3); $this->s[2][$i] = $this->mult(0x0d, $s0) ^ $this->mult(0x09, $s1) ^ $this->mult(0x0e, $s2) ^ $this->mult(0x0b, $s3); $this->s[3][$i] = $this->mult(0x0b, $s0) ^ $this->mult(0x0d, $s1) ^ $this->mult(0x09, $s2) ^ $this->mult(0x0e, $s3); } } /** applies an inverse cyclic shift to the last 3 rows of a state matrix. * @returns void **/ function invShiftRows() { $temp = ""; for ($i=1; $i<4; $i++) { for ($j=0; $j<$this->Nb; $j++) $temp[($i+$j)%$this->Nb] = $this->s[$i][$j]; for ($j=0; $j<$this->Nb; $j++) $this->s[$i][$j] = $temp[$j]; } } /** applies inverse S-Box substitution to each byte of a state matrix. * @returns void **/ function invSubBytes() { for ($i=0; $i<4; $i++) for ($j=0; $j<$this->Nb; $j++) $this->s[$i][$j] = $this->invSBox[$this->s[$i][$j]]; } /** mixes each column of a state matrix. * @returns void **/ function mixColumns() { $s0 = $s1 = $s2 = $s3= ''; // There are Nb columns for ($i=0; $i<$this->Nb; $i++) { $s0 = $this->s[0][$i]; $s1 = $this->s[1][$i]; $s2 = $this->s[2][$i]; $s3 = $this->s[3][$i]; $this->s[0][$i] = $this->mult(0x02, $s0) ^ $this->mult(0x03, $s1) ^ $this->mult(0x01, $s2) ^ $this->mult(0x01, $s3); $this->s[1][$i] = $this->mult(0x01, $s0) ^ $this->mult(0x02, $s1) ^ $this->mult(0x03, $s2) ^ $this->mult(0x01, $s3); $this->s[2][$i] = $this->mult(0x01, $s0) ^ $this->mult(0x01, $s1) ^ $this->mult(0x02, $s2) ^ $this->mult(0x03, $s3); $this->s[3][$i] = $this->mult(0x03, $s0) ^ $this->mult(0x01, $s1) ^ $this->mult(0x01, $s2) ^ $this->mult(0x02, $s3); } } /** applies a cyclic shift to the last 3 rows of a state matrix. * @returns void **/ function shiftRows() { $temp = ""; for ($i=1; $i<4; $i++) { for ($j=0; $j<$this->Nb; $j++) $temp[$j] = $this->s[$i][($j+$i)%$this->Nb]; for ($j=0; $j<$this->Nb; $j++) $this->s[$i][$j] = $temp[$j]; } } /** applies S-Box substitution to each byte of a state matrix. * @returns void **/ function subBytes() { for ($i=0; $i<4; $i++) { for ($j=0; $j<$this->Nb; $j++) $this->s[$i][$j] = $this->sBox[$this->s[$i][$j]]; } } /** multiplies two polynomials a(x), b(x) in GF(2^8) modulo the irreducible polynomial m(x) = x^8+x^4+x^3+x+1 * @returns 8-bit value **/ function mult($a, $b) { $sum = $this->ltable[$a] + $this->ltable[$b]; $sum %= 255; // Get the antilog $sum = $this->atable[$sum]; return ($a == 0 ? 0 : ($b == 0 ? 0 : $sum)); } /** applies a cyclic permutation to a 4-byte word. * @returns 32-bit int **/ function rotWord($w) { $temp = $w >> 24; // put the first 8-bits into temp $w <<= 8; // make room for temp to fill the lower end of the word $this->make32BitWord($w); // Can't do unsigned shifts, so we need to make this temp positive $temp = ($temp < 0 ? (256 + $temp) : $temp); $w += $temp; return $w; } /** applies S-box substitution to each byte of a 4-byte word. * @returns 32-bit int **/ function subWord($w) { $temp = 0; // loop through 4 bytes of a word for ($i=0; $i<4; $i++) { $temp = $w >> 24; // put the first 8-bits into temp // Can't do unsigned shifts, so we need to make this temp positive $temp = ($temp < 0 ? (256 + $temp) : $temp); $w <<= 8; // make room for the substituted byte in w; $this->make32BitWord($w); $w += $this->sBox[$temp]; // add the substituted byte back } $this->make32BitWord($w); return $w; } /** reduces a 64-bit word to a 32-bit word * @returns void **/ function make32BitWord(&$w) { // Reduce this 64-bit word to 32-bits on 64-bit machines $w &= 0x00000000FFFFFFFF; } } ?>