/** * Setup the performance-optimized function for de/encrypt() * * @see NostoCryptBase::setupInlineCrypt() */ public function setupInlineCrypt() { // Note: _setupInlineCrypt() will be called only if $this->changed === true // So here we are not under the same heavy timing-stress as we are in _de/encryptBlock() or de/encrypt(). // However...the here generated function- $code, stored as php callback in $this->inlineCrypt, must work as fast // as even possible. $lambda_functions =& NostoCryptRijndael::getLambdaFunctions(); $w = array(); $dw = array(); // The first 10 generated $lambda_functions will use the key-words hardcoded for better performance. // For memory reason we limit those ultra-optimized functions. // After that, we use pure (extracted) integer vars for the key-words which is faster than accessing them via // array. if (count($lambda_functions) < 10) { $w = $this->w; $dw = $this->dw; $init_encrypt = ''; $init_decrypt = ''; } else { for ($i = 0, $cw = count($this->w); $i < $cw; ++$i) { $w[] = '$w[' . $i . ']'; $dw[] = '$dw[' . $i . ']'; } $init_encrypt = '$w = $self->w;'; $init_decrypt = '$dw = $self->dw;'; } $code_hash = md5(str_pad("NostoCryptRijndael, {$this->mode}, {$this->blockSize}, ", 32, "") . implode(',', $w)); if (!isset($lambda_functions[$code_hash])) { $nr = $this->nr; $nb = $this->nb; $c = $this->c; // Generating encrypt code: $init_encrypt .= ' static $t0, $t1, $t2, $t3, $sbox; if (!$t0) { for ($i = 0; $i < 256; ++$i) { $t0[$i] = (int)$self->t0[$i]; $t1[$i] = (int)$self->t1[$i]; $t2[$i] = (int)$self->t2[$i]; $t3[$i] = (int)$self->t3[$i]; $sbox[$i] = (int)$self->sbox[$i]; } } '; $s = 'e'; $e = 's'; $wc = $nb - 1; // Pre-round: addRoundKey $encrypt_block = '$in = unpack("N*", $in);' . "\n"; for ($i = 0; $i < $nb; ++$i) { $encrypt_block .= '$s' . $i . ' = $in[' . ($i + 1) . '] ^ ' . $w[++$wc] . ";\n"; } // Main-rounds: shiftRows + subWord + mixColumns + addRoundKey for ($round = 1; $round < $nr; ++$round) { list($s, $e) = array($e, $s); for ($i = 0; $i < $nb; ++$i) { $encrypt_block .= '$' . $e . $i . ' = $t0[($' . $s . $i . ' >> 24) & 0xff] ^ $t1[($' . $s . ($i + $c[1]) % $nb . ' >> 16) & 0xff] ^ $t2[($' . $s . ($i + $c[2]) % $nb . ' >> 8) & 0xff] ^ $t3[ $' . $s . ($i + $c[3]) % $nb . ' & 0xff] ^ ' . $w[++$wc] . ";\n"; } } // Final-round: subWord + shiftRows + addRoundKey for ($i = 0; $i < $nb; ++$i) { $encrypt_block .= '$' . $e . $i . ' = $sbox[ $' . $e . $i . ' & 0xff] | ($sbox[($' . $e . $i . ' >> 8) & 0xff] << 8) | ($sbox[($' . $e . $i . ' >> 16) & 0xff] << 16) | ($sbox[($' . $e . $i . ' >> 24) & 0xff] << 24);' . "\n"; } $encrypt_block .= '$in = pack("N*"' . "\n"; for ($i = 0; $i < $nb; ++$i) { $encrypt_block .= ', ($' . $e . $i . ' & 0xFF000000) ^ ($' . $e . ($i + $c[1]) % $nb . ' & 0x00FF0000) ^ ($' . $e . ($i + $c[2]) % $nb . ' & 0x0000FF00) ^ ($' . $e . ($i + $c[3]) % $nb . ' & 0x000000FF) ^ ' . $w[$i] . "\n"; } $encrypt_block .= ');'; // Generating decrypt code: $init_decrypt .= ' static $dt0, $dt1, $dt2, $dt3, $isbox; if (!$dt0) { for ($i = 0; $i < 256; ++$i) { $dt0[$i] = (int)$self->dt0[$i]; $dt1[$i] = (int)$self->dt1[$i]; $dt2[$i] = (int)$self->dt2[$i]; $dt3[$i] = (int)$self->dt3[$i]; $isbox[$i] = (int)$self->isbox[$i]; } } '; $s = 'e'; $e = 's'; $wc = $nb - 1; // Pre-round: addRoundKey $decrypt_block = '$in = unpack("N*", $in);' . "\n"; for ($i = 0; $i < $nb; ++$i) { $decrypt_block .= '$s' . $i . ' = $in[' . ($i + 1) . '] ^ ' . $dw[++$wc] . ';' . "\n"; } // Main-rounds: shiftRows + subWord + mixColumns + addRoundKey for ($round = 1; $round < $nr; ++$round) { list($s, $e) = array($e, $s); for ($i = 0; $i < $nb; ++$i) { $decrypt_block .= '$' . $e . $i . ' = $dt0[($' . $s . $i . ' >> 24) & 0xff] ^ $dt1[($' . $s . ($nb + $i - $c[1]) % $nb . ' >> 16) & 0xff] ^ $dt2[($' . $s . ($nb + $i - $c[2]) % $nb . ' >> 8) & 0xff] ^ $dt3[ $' . $s . ($nb + $i - $c[3]) % $nb . ' & 0xff] ^ ' . $dw[++$wc] . ";\n"; } } // Finalround: subWord + shiftRows + addRoundKey for ($i = 0; $i < $nb; ++$i) { $decrypt_block .= '$' . $e . $i . ' = $isbox[ $' . $e . $i . ' & 0xff] | ($isbox[($' . $e . $i . ' >> 8) & 0xff] << 8) | ($isbox[($' . $e . $i . ' >> 16) & 0xff] << 16) | ($isbox[($' . $e . $i . ' >> 24) & 0xff] << 24);' . "\n"; } $decrypt_block .= '$in = pack("N*"' . "\n"; for ($i = 0; $i < $nb; ++$i) { $decrypt_block .= ', ($' . $e . $i . ' & 0xFF000000) ^ ($' . $e . ($nb + $i - $c[1]) % $nb . ' & 0x00FF0000) ^ ($' . $e . ($nb + $i - $c[2]) % $nb . ' & 0x0000FF00) ^ ($' . $e . ($nb + $i - $c[3]) % $nb . ' & 0x000000FF) ^ ' . $dw[$i] . "\n"; } $decrypt_block .= ');'; $lambda_functions[$code_hash] = $this->createInlineCryptFunction(array('init_crypt' => '', 'init_encrypt' => $init_encrypt, 'init_decrypt' => $init_decrypt, 'encrypt_block' => $encrypt_block, 'decrypt_block' => $decrypt_block)); } $this->inlineCrypt = $lambda_functions[$code_hash]; }
/** * Default Constructor. * * Determines whether or not the mcrypt extension should be used. * * $mode could be: * * - CRYPT_AES_MODE_ECB * * - CRYPT_AES_MODE_CBC * * - CRYPT_AES_MODE_CTR * * - CRYPT_AES_MODE_CFB * * - CRYPT_AES_MODE_OFB * * If not explicitly set, CRYPT_AES_MODE_CBC will be used. * * @see NostoCryptRijndael::__construct() * @see NostoCryptBase::__construct() * @param optional Integer $mode */ public function __construct($mode = CRYPT_AES_MODE_CBC) { parent::__construct($mode); }