ECDH improvements

This commit is contained in:
Daniil Gentili 2019-12-15 13:21:57 +01:00
parent 1edcecb26b
commit 58918b6fad
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
9 changed files with 209 additions and 68 deletions

View File

@ -10,7 +10,7 @@
"krakjoe/pthreads-polyfill": "*"
},
"require": {
"php": ">=7.1.0",
"php": ">=7.4.0",
"danog/primemodule": "^1",
"danog/magicalserializer": "^1.0",
"phpseclib/phpseclib": "dev-master#f715b2928976aaef389839a056c947aa8023277b as 2.0.15",

View File

@ -21,7 +21,7 @@ namespace danog\MadelineProto\MTProtoTools;
trait Crypt
{
public function aesCalculate($msg_key, $auth_key, $to_server = true)
public static function aesCalculate($msg_key, $auth_key, $to_server = true)
{
$x = $to_server ? 0 : 8;
$sha256_a = \hash('sha256', $msg_key.\substr($auth_key, $x, 36), true);
@ -32,7 +32,7 @@ trait Crypt
return [$aes_key, $aes_iv];
}
public function oldAesCalculate($msg_key, $auth_key, $to_server = true)
public static function oldAesCalculate($msg_key, $auth_key, $to_server = true)
{
$x = $to_server ? 0 : 8;
$sha1_a = \sha1($msg_key.\substr($auth_key, $x, 32), true);
@ -45,7 +45,7 @@ trait Crypt
return [$aes_key, $aes_iv];
}
public function ctrEncrypt($message, $key, $iv)
public static function ctrEncrypt($message, $key, $iv)
{
$cipher = new \phpseclib3\Crypt\AES('ctr');
$cipher->setKey($key);
@ -54,7 +54,7 @@ trait Crypt
return @$cipher->encrypt($message);
}
public function igeEncrypt($message, $key, $iv)
public static function igeEncrypt($message, $key, $iv)
{
$cipher = new \phpseclib3\Crypt\AES('ige');
$cipher->setKey($key);
@ -62,7 +62,7 @@ trait Crypt
return @$cipher->encrypt($message);
}
public function igeDecrypt($message, $key, $iv)
public static function igeDecrypt($message, $key, $iv)
{
$cipher = new \phpseclib3\Crypt\AES('ige');
$cipher->setKey($key);

File diff suppressed because one or more lines are too long

View File

@ -262,11 +262,6 @@ class TL
$TL_dict[$type][$key]['params'][] = ['name' => $explode[0], 'type' => $explode[1]];
}
}
/*
if (!$TL_dict[$type][$key][$type === 'constructors' ? 'predicate' : 'method']) {
var_dump($line);
\var_dump($TL_dict[$type][$key]);
}*/
$key++;
}
@ -584,17 +579,14 @@ class TL
if ($bare = $type['type'] != '' && $type['type'][0] === '%') {
$type['type'] = \substr($type['type'], 1);
}
if ($predicate === $type['type'] && !$auto) {
if ($predicate === $type['type']) {//} && !$auto) {
$bare = true;
}
if ($predicate === 'messageEntityMentionName') {
$constructorData = $this->constructors->findByPredicate('inputMessageEntityMentionName');
}
$concat = '';
if (!$bare) {
$concat = $constructorData['id'];
}
$concat = $bare ? '' : $constructorData['id'];
return $concat.yield $this->serializeParams($constructorData, $object, '', $layer);
}
@ -876,8 +868,6 @@ class TL
*/
public function deserialize($stream, $type = ['type' => ''])
{
//var_dump($type);
if (\is_string($stream)) {
$res = \fopen('php://memory', 'rw+b');
\fwrite($res, $stream);

View File

@ -27,7 +27,7 @@ use danog\MadelineProto\Magic;
class API extends InternalDoc
{
/**
* Construct API
* Construct API.
*
* @param array $settings Settings
*/
@ -37,7 +37,7 @@ class API extends InternalDoc
$this->API = new Lite($settings);
foreach (\get_class_methods($this->API) as $method) {
$this->methods[$method] = [$this->API, strtolower($method)];
$this->methods[$method] = [$this->API, \strtolower($method)];
}
}
}

View File

@ -79,7 +79,7 @@ class APIFactory extends AbstractAPIFactory
public $liteServer;
/**
* Just proxy async requests to API
* Just proxy async requests to API.
*
* @param string $name Method name
* @param array $arguments Arguments

View File

@ -21,6 +21,7 @@ namespace danog\MadelineProto\TON;
use danog\MadelineProto\Logger;
use danog\MadelineProto\TL\TL;
use danog\MadelineProto\Tools;
use function Amp\File\get;
@ -53,6 +54,12 @@ class Lite
* @var Logger
*/
public $logger;
/**
* Liteserver connections.
*
* @var ADNLConnection[]
*/
private $connections = [];
/**
* Construct settings.
*
@ -91,7 +98,17 @@ class Lite
'cleanup'
)
);
var_dump($this->config);
foreach ($this->config['liteservers'] as $lite) {
$this->connections[] = $connection = new ADNLConnection($this->TL);
yield $connection->connect($lite);
yield $connection->send(
[
'_' => 'liteServer.getTime'
]
);
}
yield Tools::sleep(10);
}
/**

View File

@ -1,43 +0,0 @@
<?php
/**
* TON tools module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2019 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
*
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\TON;
class Tools
{
/**
* Sanify TL obtained from JSON for TL serialization.
*
* @param array $input Data to sanitize
* @return array
*/
public static function convertJsonTL(array $input): array
{
$cb = static function (&$val) use (&$cb) {
if (isset($val['@type'])) {
$val['_'] = $val['@type'];
} elseif (\is_array($val)) {
\array_walk($val, $cb);
}
};
\array_walk($input, $cb);
return $input;
}
}

View File

@ -42,6 +42,26 @@ use function Amp\Promise\wait;
*/
trait Tools
{
/**
* Sanify TL obtained from JSON for TL serialization.
*
* @param array $input Data to sanitize
* @return array
*/
public static function convertJsonTL(array $input): array
{
$cb = static function (&$val) use (&$cb) {
if (isset($val['@type'])) {
$val['_'] = $val['@type'];
} elseif (\is_array($val)) {
\array_walk($val, $cb);
}
};
\array_walk($input, $cb);
return $input;
}
/**
* Generate MTProto vector hash.
*