Ejemplo n.º 1
0
 /**
  * Construct the instance for the supplied cipher name
  *
  * @param string $cipher The cipher to implement
  *
  * @return void
  * @throws InvalidArgumentException if the cipher is not supported
  */
 public function __construct($cipher)
 {
     parent::__construct($cipher);
     list(, $bits) = explode('-', $cipher, 2);
     $this->setBlockSize(128);
     $this->setKeySize($bits);
 }
Ejemplo n.º 2
0
 /**
  * Sets the key.
  *
  * Rijndael supports five different key lengths, AES only supports three.
  *
  * @see \phpseclib\Crypt\Rijndael:setKey()
  * @see setKeyLength()
  * @access public
  * @param string $key
  */
 function setKey($key)
 {
     parent::setKey($key);
     if (!$this->explicit_key_length) {
         $length = strlen($key);
         switch (true) {
             case $length <= 16:
                 $this->key_size = 16;
                 break;
             case $length <= 24:
                 $this->key_size = 24;
                 break;
             default:
                 $this->key_size = 32;
         }
         $this->_setEngine();
     }
 }
Ejemplo n.º 3
0
 /**
  * Treat consecutive packets as if they are a discontinuous buffer.
  *
  * The default behavior.
  *
  * @see Crypt_Rijndael::enableContinuousBuffer()
  * @access public
  */
 function disableContinuousBuffer()
 {
     parent::disableContinuousBuffer();
     if (CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT) {
         mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
         mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
     }
 }
Ejemplo n.º 4
0
 /**
  * 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 explictly set, CRYPT_AES_MODE_CBC will be used.
  *
  * @see Rijndael::Rijndael()
  * @see Base::Base()
  * @param optional Integer $mode
  * @access public
  */
 function __construct($mode = CRYPT_AES_MODE_CBC)
 {
     parent::__construct($mode);
 }
Ejemplo n.º 5
0
    /**
     * Creates performance-optimized function for de/encrypt(), storing it in $this->inline_crypt
     *
     * @see Crypt_Rijndael::encrypt()
     * @see Crypt_Rijndael::decrypt()
     * @access private
     */
    function inline_crypt_setup()
    {
        // Note: inline_crypt_setup() will be called only if $this->changed === true
        // So here we are'nt 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->inline_crypt, must work as fast as even possible.
        $lambda_functions =& Rijndael::get_lambda_functions();
        $block_size = $this->block_size;
        $mode = $this->mode;
        // The first 5 generated $lambda_functions will use the key-words hardcoded for better performance.
        // For memory reason we limit those ultra-optimized function code to 5.
        // After that, we use pure (extracted) integer vars for the key-words which is faster than accessing them via array.
        if (count($lambda_functions) < 5) {
            $w = $this->w;
            $dw = $this->dw;
            $init_encryptBlock = '';
            $init_decryptBlock = '';
        } else {
            for ($i = 0, $cw = count($this->w); $i < $cw; ++$i) {
                $w[] = '$w_' . $i;
                $dw[] = '$dw_' . $i;
            }
            $init_encryptBlock = 'extract($self->w,  EXTR_PREFIX_ALL, "w");';
            $init_decryptBlock = 'extract($self->dw, EXTR_PREFIX_ALL, "dw");';
        }
        $code_hash = md5("{$mode}, {$block_size}, " . implode(',', $w));
        if (!isset($lambda_functions[$code_hash])) {
            $Nr = $this->Nr;
            $Nb = $this->Nb;
            $c = $this->c;
            // Generating encrypt code:
            $init_encryptBlock .= '
                $t0 = $self->t0;
                $t1 = $self->t1;
                $t2 = $self->t2;
                $t3 = $self->t3;
                $sbox = $self->sbox;';
            $s = 'e';
            $e = 's';
            $wc = $Nb - 1;
            // Preround: addRoundKey
            $_encryptBlock = '$in = unpack("N*", $in);' . "\n";
            for ($i = 0; $i < $Nb; ++$i) {
                $_encryptBlock .= '$s' . $i . ' = $in[' . ($i + 1) . '] ^ ' . $w[++$wc] . ";\n";
            }
            // Mainrounds: shiftRows + subWord + mixColumns + addRoundKey
            for ($round = 1; $round < $Nr; ++$round) {
                list($s, $e) = array($e, $s);
                for ($i = 0; $i < $Nb; ++$i) {
                    $_encryptBlock .= '$' . $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";
                }
            }
            // Finalround: subWord + shiftRows + addRoundKey
            for ($i = 0; $i < $Nb; ++$i) {
                $_encryptBlock .= '$' . $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";
            }
            $_encryptBlock .= '$in = pack("N*"' . "\n";
            for ($i = 0; $i < $Nb; ++$i) {
                $_encryptBlock .= ',
                    ($' . $e . $i . ' & 0xFF000000) ^
                    ($' . $e . ($i + $c[1]) % $Nb . ' & 0x00FF0000) ^
                    ($' . $e . ($i + $c[2]) % $Nb . ' & 0x0000FF00) ^
                    ($' . $e . ($i + $c[3]) % $Nb . ' & 0x000000FF) ^
                    ' . $w[$i] . "\n";
            }
            $_encryptBlock .= ');';
            // Generating decrypt code:
            $init_decryptBlock .= '
                $dt0 = $self->dt0;
                $dt1 = $self->dt1;
                $dt2 = $self->dt2;
                $dt3 = $self->dt3;
                $isbox = $self->isbox;';
            $s = 'e';
            $e = 's';
            $wc = $Nb - 1;
            // Preround: addRoundKey
            $_decryptBlock = '$in = unpack("N*", $in);' . "\n";
            for ($i = 0; $i < $Nb; ++$i) {
                $_decryptBlock .= '$s' . $i . ' = $in[' . ($i + 1) . '] ^ ' . $dw[++$wc] . ';' . "\n";
            }
            // Mainrounds: shiftRows + subWord + mixColumns + addRoundKey
            for ($round = 1; $round < $Nr; ++$round) {
                list($s, $e) = array($e, $s);
                for ($i = 0; $i < $Nb; ++$i) {
                    $_decryptBlock .= '$' . $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) {
                $_decryptBlock .= '$' . $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";
            }
            $_decryptBlock .= '$in = pack("N*"' . "\n";
            for ($i = 0; $i < $Nb; ++$i) {
                $_decryptBlock .= ',
                    ($' . $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";
            }
            $_decryptBlock .= ');';
            // Generating mode of operation code:
            switch ($mode) {
                case CRYPT_RIJNDAEL_MODE_ECB:
                    $encrypt = $init_encryptBlock . '
                        $ciphertext = "";
                        $text = $self->_pad($text);
                        $plaintext_len = strlen($text);

                        for ($i = 0; $i < $plaintext_len; $i+= ' . $block_size . ') {
                            $in = substr($text, $i, ' . $block_size . ');
                            ' . $_encryptBlock . '
                            $ciphertext.= $in;
                        }
                       
                        return $ciphertext;
                        ';
                    $decrypt = $init_decryptBlock . '
                        $plaintext = "";
                        $text = str_pad($text, strlen($text) + (' . $block_size . ' - strlen($text) % ' . $block_size . ') % ' . $block_size . ', chr(0));
                        $ciphertext_len = strlen($text);

                        for ($i = 0; $i < $ciphertext_len; $i+= ' . $block_size . ') {
                            $in = substr($text, $i, ' . $block_size . ');
                            ' . $_decryptBlock . '
                            $plaintext.= $in;
                        }

                        return $self->_unpad($plaintext);
                        ';
                    break;
                case CRYPT_RIJNDAEL_MODE_CBC:
                    $encrypt = $init_encryptBlock . '
                        $ciphertext = "";
                        $text = $self->_pad($text);
                        $plaintext_len = strlen($text);

                        $in = $self->encryptIV;

                        for ($i = 0; $i < $plaintext_len; $i+= ' . $block_size . ') {
                            $in = substr($text, $i, ' . $block_size . ') ^ $in;
                            ' . $_encryptBlock . '
                            $ciphertext.= $in;
                        }

                        if ($self->continuousBuffer) {
                            $self->encryptIV = $in;
                        }

                        return $ciphertext;
                        ';
                    $decrypt = $init_decryptBlock . '
                        $plaintext = "";
                        $text = str_pad($text, strlen($text) + (' . $block_size . ' - strlen($text) % ' . $block_size . ') % ' . $block_size . ', chr(0));
                        $ciphertext_len = strlen($text);

                        $iv = $self->decryptIV;

                        for ($i = 0; $i < $ciphertext_len; $i+= ' . $block_size . ') {
                            $in = $block = substr($text, $i, ' . $block_size . ');
                            ' . $_decryptBlock . '
                            $plaintext.= $in ^ $iv;
                            $iv = $block;
                        }

                        if ($self->continuousBuffer) {
                            $self->decryptIV = $iv;
                        }

                        return $self->_unpad($plaintext);
                        ';
                    break;
                case CRYPT_RIJNDAEL_MODE_CTR:
                    $encrypt = $init_encryptBlock . '
                        $ciphertext = "";
                        $plaintext_len = strlen($text);
                        $xor = $self->encryptIV;
                        $buffer = &$self->enbuffer;

                        if (strlen($buffer["encrypted"])) {
                            for ($i = 0; $i < $plaintext_len; $i+= ' . $block_size . ') {
                                $block = substr($text, $i, ' . $block_size . ');
                                if (strlen($block) > strlen($buffer["encrypted"])) {
                                    $in = $self->_generate_xor(' . $block_size . ', $xor);
                                    ' . $_encryptBlock . '
                                    $buffer["encrypted"].= $in;
                                }
                                $key = $self->_string_shift($buffer["encrypted"], ' . $block_size . ');
                                $ciphertext.= $block ^ $key;
                            }
                        } else {
                            for ($i = 0; $i < $plaintext_len; $i+= ' . $block_size . ') {
                                $block = substr($text, $i, ' . $block_size . ');
                                $in = $self->_generate_xor(' . $block_size . ', $xor);
                                ' . $_encryptBlock . '
                                $key = $in;
                                $ciphertext.= $block ^ $key;
                            }
                        }
                        if ($self->continuousBuffer) {
                            $self->encryptIV = $xor;
                            if ($start = $plaintext_len % ' . $block_size . ') {
                                $buffer["encrypted"] = substr($key, $start) . $buffer["encrypted"];
                            }
                        }

                        return $ciphertext;
                    ';
                    $decrypt = $init_encryptBlock . '
                        $plaintext = "";
                        $ciphertext_len = strlen($text);
                        $xor = $self->decryptIV;
                        $buffer = &$self->debuffer;

                        if (strlen($buffer["ciphertext"])) {
                            for ($i = 0; $i < $ciphertext_len; $i+= ' . $block_size . ') {
                                $block = substr($text, $i, ' . $block_size . ');
                                if (strlen($block) > strlen($buffer["ciphertext"])) {
                                    $in = $self->_generate_xor(' . $block_size . ', $xor);
                                    ' . $_encryptBlock . '
                                    $buffer["ciphertext"].= $in;
                                }
                                $key = $self->_string_shift($buffer["ciphertext"], ' . $block_size . ');
                                $plaintext.= $block ^ $key;
                            }
                        } else {
                            for ($i = 0; $i < $ciphertext_len; $i+= ' . $block_size . ') {
                                $block = substr($text, $i, ' . $block_size . ');
                                $in = $self->_generate_xor(' . $block_size . ', $xor);
                                ' . $_encryptBlock . '
                                $key = $in;
                                $plaintext.= $block ^ $key;
                            }
                        }
                        if ($self->continuousBuffer) {
                            $self->decryptIV = $xor;
                            if ($start = $ciphertext_len % ' . $block_size . ') {
                                $buffer["ciphertext"] = substr($key, $start) . $buffer["ciphertext"];
                            }
                        }
                       
                        return $plaintext;
                        ';
                    break;
                case CRYPT_RIJNDAEL_MODE_CFB:
                    $encrypt = $init_encryptBlock . '
                        $ciphertext = "";
                        $buffer = &$self->enbuffer;

                        if ($self->continuousBuffer) {
                            $iv = &$self->encryptIV;
                            $pos = &$buffer["pos"];
                        } else {
                            $iv = $self->encryptIV;
                            $pos = 0;
                        }
                        $len = strlen($text);
                        $i = 0;
                        if ($pos) {
                            $orig_pos = $pos;
                            $max = ' . $block_size . ' - $pos;
                            if ($len >= $max) {
                                $i = $max;
                                $len-= $max;
                                $pos = 0;
                            } else {
                                $i = $len;
                                $pos+= $len;
                                $len = 0;
                            }
                            $ciphertext = substr($iv, $orig_pos) ^ $text;
                            $iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
                        }
                        while ($len >= ' . $block_size . ') {
                            $in = $iv;
                            ' . $_encryptBlock . ';
                            $iv = $in ^ substr($text, $i, ' . $block_size . ');
                            $ciphertext.= $iv;
                            $len-= ' . $block_size . ';
                            $i+= ' . $block_size . ';
                        }
                        if ($len) {
                            $in = $iv;
                            ' . $_encryptBlock . '
                            $iv = $in;
                            $block = $iv ^ substr($text, $i);
                            $iv = substr_replace($iv, $block, 0, $len);
                            $ciphertext.= $block;
                            $pos = $len;
                        }
                        return $ciphertext;
                    ';
                    $decrypt = $init_encryptBlock . '
                        $plaintext = "";
                        $buffer = &$self->debuffer;

                        if ($self->continuousBuffer) {
                            $iv = &$self->decryptIV;
                            $pos = &$buffer["pos"];
                        } else {
                            $iv = $self->decryptIV;
                            $pos = 0;
                        }
                        $len = strlen($text);
                        $i = 0;
                        if ($pos) {
                            $orig_pos = $pos;
                            $max = ' . $block_size . ' - $pos;
                            if ($len >= $max) {
                                $i = $max;
                                $len-= $max;
                                $pos = 0;
                            } else {
                                $i = $len;
                                $pos+= $len;
                                $len = 0;
                            }
                            $plaintext = substr($iv, $orig_pos) ^ $text;
                            $iv = substr_replace($iv, substr($text, 0, $i), $orig_pos, $i);
                        }
                        while ($len >= ' . $block_size . ') {
                            $in = $iv;
                            ' . $_encryptBlock . '
                            $iv = $in;
                            $cb = substr($text, $i, ' . $block_size . ');
                            $plaintext.= $iv ^ $cb;
                            $iv = $cb;
                            $len-= ' . $block_size . ';
                            $i+= ' . $block_size . ';
                        }
                        if ($len) {
                            $in = $iv;
                            ' . $_encryptBlock . '
                            $iv = $in;
                            $plaintext.= $iv ^ substr($text, $i);
                            $iv = substr_replace($iv, substr($text, $i), 0, $len);
                            $pos = $len;
                        }

                        return $plaintext;
                        ';
                    break;
                case CRYPT_RIJNDAEL_MODE_OFB:
                    $encrypt = $init_encryptBlock . '
                        $ciphertext = "";
                        $plaintext_len = strlen($text);
                        $xor = $self->encryptIV;
                        $buffer = &$self->enbuffer;

                        if (strlen($buffer["xor"])) {
                            for ($i = 0; $i < $plaintext_len; $i+= ' . $block_size . ') {
                                $block = substr($text, $i, ' . $block_size . ');
                                if (strlen($block) > strlen($buffer["xor"])) {
                                    $in = $xor;
                                    ' . $_encryptBlock . '
                                    $xor = $in;
                                    $buffer["xor"].= $xor;
                                }
                                $key = $self->_string_shift($buffer["xor"], ' . $block_size . ');
                                $ciphertext.= $block ^ $key;
                            }
                        } else {
                            for ($i = 0; $i < $plaintext_len; $i+= ' . $block_size . ') {
                                $in = $xor;
                                ' . $_encryptBlock . '
                                $xor = $in;
                                $ciphertext.= substr($text, $i, ' . $block_size . ') ^ $xor;
                            }
                            $key = $xor;
                        }
                        if ($self->continuousBuffer) {
                            $self->encryptIV = $xor;
                            if ($start = $plaintext_len % ' . $block_size . ') {
                                 $buffer["xor"] = substr($key, $start) . $buffer["xor"];
                            }
                        }
                        return $ciphertext;
                        ';
                    $decrypt = $init_encryptBlock . '
                        $plaintext = "";
                        $ciphertext_len = strlen($text);
                        $xor = $self->decryptIV;
                        $buffer = &$self->debuffer;

                        if (strlen($buffer["xor"])) {
                            for ($i = 0; $i < $ciphertext_len; $i+= ' . $block_size . ') {
                                $block = substr($text, $i, ' . $block_size . ');
                                if (strlen($block) > strlen($buffer["xor"])) {
                                    $in = $xor;
                                    ' . $_encryptBlock . '
                                    $xor = $in;
                                    $buffer["xor"].= $xor;
                                }
                                $key = $self->_string_shift($buffer["xor"], ' . $block_size . ');
                                $plaintext.= $block ^ $key;
                            }
                        } else {
                            for ($i = 0; $i < $ciphertext_len; $i+= ' . $block_size . ') {
                                $in = $xor;
                                ' . $_encryptBlock . '
                                $xor = $in;
                                $plaintext.= substr($text, $i, ' . $block_size . ') ^ $xor;
                            }
                            $key = $xor;
                        }
                        if ($self->continuousBuffer) {
                            $self->decryptIV = $xor;
                            if ($start = $ciphertext_len % ' . $block_size . ') {
                                 $buffer["xor"] = substr($key, $start) . $buffer["xor"];
                            }
                        }
                        return $plaintext;
                        ';
                    break;
            }
            $lambda_functions[$code_hash] = create_function('$action, &$self, $text', 'if ($action == "encrypt") { ' . $encrypt . ' } else { ' . $decrypt . ' }');
        }
        $this->inline_crypt = $lambda_functions[$code_hash];
    }
Ejemplo n.º 6
0
 /**
  * Setup the performance-optimized function for de/encrypt()
  *
  * @see Base::_setupInlineCrypt()
  * @access private
  */
 function _setupInlineCrypt()
 {
     // Note: _setupInlineCrypt() will be called only if $this->changed === true
     // So here we are'nt 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->inline_crypt, must work as fast as even possible.
     $lambda_functions =& Rijndael::_getLambdaFunctions();
     // 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("Rijndael, {$this->mode}, {$this->block_size}, ", 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;
         // Preround: addRoundKey
         $encrypt_block = '$in = unpack("N*", $in);' . "\n";
         for ($i = 0; $i < $Nb; ++$i) {
             $encrypt_block .= '$s' . $i . ' = $in[' . ($i + 1) . '] ^ ' . $w[++$wc] . ";\n";
         }
         // Mainrounds: 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";
             }
         }
         // Finalround: 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;
         // Preround: addRoundKey
         $decrypt_block = '$in = unpack("N*", $in);' . "\n";
         for ($i = 0; $i < $Nb; ++$i) {
             $decrypt_block .= '$s' . $i . ' = $in[' . ($i + 1) . '] ^ ' . $dw[++$wc] . ';' . "\n";
         }
         // Mainrounds: 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->inline_crypt = $lambda_functions[$code_hash];
 }
Ejemplo n.º 7
0
 private static function installLocalWidget($categoryId, $userId, $uploadedFilePath)
 {
     try {
         $unziper = new fileUnzip($uploadedFilePath);
         $archiveContent = $unziper->getFilesList();
         $tempFolder = './widgets/' . mktime();
         $alreadyInstalled = false;
         if (!self::isFileInArchive($archiveContent, 'config.xml')) {
             throw new FileException(MwwException::MODEL, 'Unable to find config.xml in the provided archive');
         } else {
             if (!self::isFileInArchive($archiveContent, 'index.html') && !self::isFileInArchive($archiveContent, 'index.xul')) {
                 throw new FileException(MwwException::MODEL, 'Unable to find index.html in the provided archive');
             } else {
                 // The mandatory files are in the archive.
                 // We unzip the document in the widget repository under an arbitrary folder.
                 $unziper->unzipAll($tempFolder);
                 $unziper->close();
                 // parse the manifest.
                 $manifestPath = $tempFolder . '/config.xml';
                 $doc = new DOMDocument();
                 $doc->preserveWhiteSpace = false;
                 if (!@$doc->load($manifestPath)) {
                     throw new XMLParsingException(MwwException::MODEL, 'Unable to parse XML file. Document not well formed.');
                 }
                 // validate the manifest.
                 if (!@$doc->schemaValidate('./schema/manifest.xsd')) {
                     throw new XMLValidationException(MwwException::MODEL, "The XML File isn't valid according to the XML Schema.");
                 }
                 // test if the id is unique (test if the directory exists).
                 $id = $doc->getElementsByTagName('widget')->item(0)->getAttribute('id');
                 // getting name/title of the widget.
                 if ($doc->getElementsByTagName('name')->length) {
                     $name = $doc->getElementsByTagName('name')->item(0)->nodeValue;
                 } else {
                     if ($doc->getElementsByTagName('title')->length) {
                         $name = $doc->getElementsByTagName('title')->item(0)->nodeValue;
                     } else {
                         throw new XMLValidationException(MwwException::MODEL, "The XML File is not valid. The mandatory title or name element is missing.");
                     }
                 }
                 // getting widget description.
                 $description = null;
                 if ($doc->getElementsByTagName('description')->length) {
                     $description = $doc->getElementsByTagName('description')->item(0)->nodeValue;
                 }
                 // is widget authentication enabled or disabled ?
                 $widgetAuth = false;
                 $authKey = null;
                 if ($doc->getElementsByTagName('widget_authentication')->length) {
                     $authValue = $doc->getElementsByTagName('widget_authentication')->item(0)->nodeValue;
                     if ($authValue == 'enabled') {
                         $widgetAuth = true;
                         // create the key.
                         $crypto = new Rijndael();
                         $authKey = $crypto->generateKey();
                     }
                 }
                 $targetFolder = './widgets/' . $id;
                 if (file_exists($targetFolder)) {
                     $alreadyInstalled = true;
                     throw new FileException(MwwException::MODEL, 'A widget with the id \'' . $id . '\' already exists.');
                 }
                 // create the widget directory (actually we simply rename the temporary directory).
                 if (!@rename($tempFolder, $targetFolder)) {
                     throw new FileException(MwwException::MODEL, 'The widget directory could not be created');
                 }
                 $nameToReturn = $name;
                 // -- Persistant data access section.
                 $db = DbUtil::accessFactory();
                 $categoryId = $categoryId != 0 ? $db->escape($categoryId) : 'NULL';
                 $userId = $db->escape($userId);
                 $name = $db->escape($name);
                 $description = $db->escape($description);
                 $generatedKey = $widgetAuth ? "'{$authKey}'" : 'NULL';
                 // insert the widget into the database.
                 if (!$db->execute("INSERT INTO `widgets` (`widgetid`, `widgetname`, `visible`, `copname`, `category`, `description`, `authkey`) VALUES ('{$id}', '{$name}', 0, (SELECT `copname` FROM `users` WHERE `id`= {$userId}), {$categoryId}, '{$description}', {$generatedKey})")) {
                     throw new DBException(MwwException::MODEL, "Unable to insert new widget '{$id}' information in persistant data");
                 }
                 // -- End of Persistant data access section.
                 // End of the installation. Everything was fine.
                 return $nameToReturn;
             }
         }
     } catch (Exception $ex) {
         if (isset($tempFolder) && is_dir($tempFolder)) {
             Util::full_rmdir($tempFolder);
         }
         if (isset($targetFolder) && is_dir($targetFolder) && !$alreadyInstalled) {
             Util::full_rmdir($targetFolder);
         }
         throw $ex;
     }
 }
Ejemplo n.º 8
0
 /**
  * Sets the key.
  *
  * Rijndael supports five different key lengths, AES only supports three.
  *
  * @see \phpseclib\Crypt\Rijndael:setKey()
  * @see setKeyLength()
  * @access public
  * @param string $key
  * @throws \LengthException if the key length isn't supported
  */
 function setKey($key)
 {
     switch (strlen($key)) {
         case 16:
         case 24:
         case 32:
             break;
         default:
             throw new \LengthException('Key of size ' . strlen($key) . ' not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported');
     }
     parent::setKey($key);
 }