From e30ccb6717af637ba0c2ec0f3ffefb6884431437 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 15 Jul 2016 13:03:36 +0200 Subject: [PATCH] Change aes engine --- .RightPack.class.php.swp | Bin 0 -> 1024 bytes .gitignore | 3 +- .mtproto.php.swp | Bin 0 -> 1024 bytes AES.class.php | 593 --------------------------------------- RightPack.class.php | 26 ++ aes256.php | 112 ++++++++ composer.json | 16 ++ composer.lock | 67 +++++ crypt.php | 2 +- 9 files changed, 224 insertions(+), 595 deletions(-) create mode 100644 .RightPack.class.php.swp create mode 100644 .mtproto.php.swp delete mode 100644 AES.class.php create mode 100644 RightPack.class.php create mode 100644 aes256.php create mode 100644 composer.json create mode 100644 composer.lock diff --git a/.RightPack.class.php.swp b/.RightPack.class.php.swp new file mode 100644 index 0000000000000000000000000000000000000000..4c776be4489b79643f64cf37decd0e1d1b3ead16 GIT binary patch literal 1024 zcmYc?$V<%2S1{5u)iY*50uBKT3@M3unVC6Af;gBEb-Kl=MP;c)IMjfI^)vEwQ}rRL z^@~yq@{9F-6H`)iGV@Xcita%KK(t;#MgbmUMx{nWU^E2C3xQB{Z2*Xj9Mu2- literal 0 HcmV?d00001 diff --git a/AES.class.php b/AES.class.php deleted file mode 100644 index aa09ec557..000000000 --- a/AES.class.php +++ /dev/null @@ -1,593 +0,0 @@ -Nk = strlen($z) / 4; - $this->Nr = $this->Nk + self::$Nb + 2; - - 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 + self::$Nb + 2; - $this->w = []; // Nb*(Nr+1) 32-bit words - $this->s = [[]]; // 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 - * Whenever possible you should stream your plaintext through the - * encryptBlock() function directly, as the amount of time required - * to encrypt is linear to the size of the ciphertext. - **/ - public function encrypt($x) - { - $t = ''; // 16-byte block - $y = ''; // returned cipher text; - - // put a 16-byte block into t - $xsize = strlen($x); - 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 .= $this->encryptBlock($t); - } - - return $y; - } - - /** Decrypts an aribtrary length String. - * @params ciphertext string - * @returns plaintext string - * Whenever possible you should stream your ciphertext through the - * decryptBlock() function directly, as the amount of time required - * to decrypt is linear to the size of the ciphertext. - **/ - public function decrypt($y) - { - $t = ''; // 16-byte block - $x = ''; // returned plain text; - - // put a 16-byte block into t - $ysize = strlen($y); - 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 .= $this->decryptBlock($t); - } - - return $x; - } - - /** Encrypts the 16-byte plain text. - * @params 16-byte plaintext string - * @returns 16-byte ciphertext string - **/ - public function encryptBlock($x) - { - $y = ''; // 16-byte string - - // place input x into the initial state matrix in column order - for ($i = 0; $i < 4 * self::$Nb; $i++) { - // we want integerger division for the second index - $this->s[$i % 4][($i - $i % self::$Nb) / self::$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 * self::$Nb; $i++) { - $y .= chr($this->s[$i % 4][($i - $i % self::$Nb) / self::$Nb]); - } - - return $y; - } - - /** Decrypts the 16-byte cipher text. - * @params 16-byte ciphertext string - * @returns 16-byte plaintext string - **/ - public function decryptBlock($y) - { - $x = ''; // 16-byte string - - // place input y into the initial state matrix in column order - for ($i = 0; $i < 4 * self::$Nb; $i++) { - $this->s[$i % 4][($i - $i % self::$Nb) / self::$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 * self::$Nb; $i++) { - // Used to remove filled null characters. - $x .= ($this->s[$i % 4][($i - $i % self::$Nb) / self::$Nb] == chr(0) ? '' : chr($this->s[$i % 4][($i - $i % self::$Nb) / self::$Nb])); - } - - return $x; - } - - public function __destruct() - { - unset($this->w); - unset($this->s); - } - - /** makes a big key out of a small one - * @returns void - **/ - private function KeyExpansion($z) - { - // Rcon is the round constant - static $Rcon = [ - 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 < self::$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]; - } elseif ($this->Nk > 6 && $i % $this->Nk == 4) { - $temp = $this->subWord($temp); - } - - $this->w[$i] = $this->w[$i - $this->Nk] ^ $temp; - - self::make32BitWord($this->w[$i]); - } - } - - /** adds the key schedule for a round to a state matrix. - * @returns void - **/ - private function addRoundKey($round) - { - $temp = ''; - - for ($i = 0; $i < 4; $i++) { - for ($j = 0; $j < self::$Nb; $j++) { - // place the i-th byte of the j-th word from expanded key w into temp - $temp = $this->w[$round * self::$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 - **/ - private function invMixColumns() - { - $s0 = $s1 = $s2 = $s3 = ''; - - // There are Nb columns - for ($i = 0; $i < self::$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 - **/ - private function invShiftRows() - { - $temp = ''; - for ($i = 1; $i < 4; $i++) { - for ($j = 0; $j < self::$Nb; $j++) { - $temp[($i + $j) % self::$Nb] = $this->s[$i][$j]; - } - for ($j = 0; $j < self::$Nb; $j++) { - $this->s[$i][$j] = $temp[$j]; - } - } - } - - /** applies inverse S-Box substitution to each byte of a state matrix. - * @returns void - **/ - private function invSubBytes() - { - for ($i = 0; $i < 4; $i++) { - for ($j = 0; $j < self::$Nb; $j++) { - $this->s[$i][$j] = self::$invSBox[$this->s[$i][$j]]; - } - } - } - - /** mixes each column of a state matrix. - * @returns void - **/ - private function mixColumns() - { - $s0 = $s1 = $s2 = $s3 = ''; - - // There are Nb columns - for ($i = 0; $i < self::$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 - **/ - private function shiftRows() - { - $temp = ''; - for ($i = 1; $i < 4; $i++) { - for ($j = 0; $j < self::$Nb; $j++) { - $temp[$j] = $this->s[$i][($j + $i) % self::$Nb]; - } - for ($j = 0; $j < self::$Nb; $j++) { - $this->s[$i][$j] = $temp[$j]; - } - } - } - - /** applies S-Box substitution to each byte of a state matrix. - * @returns void - **/ - private function subBytes() - { - for ($i = 0; $i < 4; $i++) { - for ($j = 0; $j < self::$Nb; $j++) { - $this->s[$i][$j] = self::$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 - **/ - private static function mult($a, $b) - { - $sum = self::$ltable[$a] + self::$ltable[$b]; - $sum %= 255; - // Get the antilog - $sum = self::$atable[$sum]; - - return $a == 0 ? 0 : ($b == 0 ? 0 : $sum); - } - - /** applies a cyclic permutation to a 4-byte word. - * @returns 32-bit int - **/ - private static 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 - self::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 - **/ - private static 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; - self::make32BitWord($w); - $w += self::$sBox[$temp]; // add the substituted byte back - } - - self::make32BitWord($w); - - return $w; - } - - /** reduces a 64-bit word to a 32-bit word - * @returns void - **/ - private static function make32BitWord(&$w) - { - // Reduce this 64-bit word to 32-bits on 64-bit machines - $w &= 0x00000000FFFFFFFF; - } -} diff --git a/RightPack.class.php b/RightPack.class.php new file mode 100644 index 000000000..a93c0130d --- /dev/null +++ b/RightPack.class.php @@ -0,0 +1,26 @@ + + * @license MIT license +*/ + +class FileServe { + const $formatinfo = []; + const $modifiers = ["<" => ]; + public function pack($format, ...$data) { + $count = count($data); + $packcommand = []; + $current = 0; + foreach (str_split($format) as $currentformat) { + if(isset($modifiers[$currentformat])) { + $packcommand[$current]["format_info"] = $modifiers[$currentformat]; + } elseif(isset($formatinfo[$currentformat])) { + $packcommand[$current]["format"] + } +} diff --git a/aes256.php b/aes256.php new file mode 100644 index 000000000..b8e04b1cb --- /dev/null +++ b/aes256.php @@ -0,0 +1,112 @@ +key = $key; + $this->iv = $iv; + $this->cipherText = $cipherText; + $this->plainText = $plainText; + $this->debug = $debug;
} +public function IGE256Decrypt() +{ + +$key = $this->key; +$message = $this->cipherText; +$blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); + +$xPrev = substr($this->iv, 0, $blockSize); +$yPrev = substr($this->iv, $blockSize, strlen($this->iv)); + +$decrypted = ''; + +for ($i=0; $i < strlen($message); $i += $blockSize) +{ + $x = substr($message, $i, $blockSize); + $this->debugLog("x: " . _c($x) . "\n"); + + $yXOR = $this->exor($x, $yPrev); + $this->debugLog("yPrev: " . _c($yPrev) . "\n"); + $this->debugLog("yXOR: " . _c($yXOR) . "\n"); + $yFinal = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $yXOR, MCRYPT_MODE_ECB); $yFinal = str_pad($yFinal, strlen($xPrev), "\x00"); +$this->debugLog("yFinal: " . _c($yFinal) . “\n"); + +$y = $this->exor($yFinal, $xPrev); +$this->debugLog("xPrev: " . _c($xPrev) . "\n"); +$this->debugLog("y: " . _c($y) . "\n"); + +$xPrev = $x; +$yPrev = $y; +$decrypted .= $y; + +$this->debugLog("Currently Decrypted: "._c($decrypted)."\n\n"); +} +return $decrypted; +} + +public function IGE256Encrypt() +{ +$key = $this->key; +$message = $this->plainText; +$blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); + +$xPrev = substr($this->iv, $blockSize, strlen($this->iv)); +$yPrev = substr($this->iv, 0, $blockSize); + +$encrypted = ''; + +for ($i=0; $i < strlen($message); $i += $blockSize) +{ + +$x = substr($message, $i, $blockSize); +$this->debugLog("x: " . _c($x) . “\n"); + +$yXOR = $this->exor($x, $yPrev); +$this->debugLog("yPrev: " . _c($yPrev) . "\n"); +$this->debugLog("yXOR: " . _c($yXOR) . "\n"); +$yFinal = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $yXOR, MCRYPT_MODE_ECB); $yFinal = str_pad($yFinal, strlen($xPrev), "\x00"); +$this->debugLog("yFinal: " . _c($yFinal) . “\n"); +$y = $this->exor($yFinal, $xPrev); +$this->debugLog("xPrev: " . _c($xPrev) . “\n"); +$this->debugLog("y: " . _c($y) . “\n"); + +$xPrev = $x; +$yPrev = $y; + +$encrypted .= $y; +$this->debugLog("Currently encrypted: "._c($encrypted)."\n\n"); +} +return $encrypted; +} + +public function debugLog($message) +{ + if ($this->debug) + echo $message; +} + +public function exor($array1, $array2) +{ + $len = (strlen($array1) <= strlen($array2)) ? strlen($array2) : strlen($array1); + +$array1 = str_pad($array1, $len, “\x00"); +$array2 = str_pad($array2, $len, “\x00"); + +$res = ‘'; +for ($i=0; $i < $len; $i++) +{ + $res .= $array1[$i] ^ $array2[$i]; +} +return $res; +} + +function _c($binary) { return sprintf(“[%s]", chunk_split(bin2hex($binary), 4, ' ')); } + +} diff --git a/composer.json b/composer.json new file mode 100644 index 000000000..aabe9a8cb --- /dev/null +++ b/composer.json @@ -0,0 +1,16 @@ +{ + "name": "danog/madelineproto", + "description": "PHP implementation of telegram's MTProto protocol.", + "type": "project", + "require": { + "danog/phpstruct": "^0.4.0" + }, + "license": "MIT", + "authors": [ + { + "name": "Daniil Gentili", + "email": "daniil@daniil.it" + } + ], + "minimum-stability": "stable" +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 000000000..ac71e09f2 --- /dev/null +++ b/composer.lock @@ -0,0 +1,67 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "79f84e2c764ec641d3d2001fe7ce80cf", + "content-hash": "06c2d6c2d0a9a8f1e37a186683029483", + "packages": [ + { + "name": "danog/phpstruct", + "version": "0.4.1", + "source": { + "type": "git", + "url": "https://github.com/danog/PHPStruct.git", + "reference": "c45742bcbc6e1bbf6bdca7b92b65a6b9624853ca" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/danog/PHPStruct/zipball/c45742bcbc6e1bbf6bdca7b92b65a6b9624853ca", + "reference": "c45742bcbc6e1bbf6bdca7b92b65a6b9624853ca", + "shasum": "" + }, + "require": { + "php": ">=5.6.0" + }, + "require-dev": { + "phpunit/phpunit": "5.4.*" + }, + "type": "library", + "autoload": { + "psr-0": { + "danog\\PHP\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "danog", + "email": "daniil@daniil.it" + } + ], + "description": "PHP implementation of python's struct module.", + "homepage": "https://daniil.it/phpstruct", + "keywords": [ + "byte", + "bytes", + "pack", + "python", + "struct", + "unpack" + ], + "time": "2016-07-15 10:56:16" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/crypt.php b/crypt.php index 43d2cdb70..7e81b3b3d 100644 --- a/crypt.php +++ b/crypt.php @@ -2,7 +2,7 @@ set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__).DIRECTORY_SEPARATOR.'libpy2php'); require_once 'libpy2php.php'; -require_once 'AES.class.php'; +require_once 'aes256.php'; class crypt { public function ige_encrypt($message, $key, $iv)