Undo breaking change and speed up key fingerprint matching

This commit is contained in:
Daniil Gentili 2017-01-26 14:29:58 +01:00
parent 7df2526d9c
commit 6e3e9e5d12
3 changed files with 17 additions and 19 deletions

View File

@ -30,7 +30,6 @@ class MTProto extends PrimeModule
use \danog\MadelineProto\MTProtoTools\UpdateHandler;
use \danog\MadelineProto\TL\TL;
use \danog\MadelineProto\Tools;
use \danog\MadelineProto\RSA;
public $settings = [];
public $config = ['expires' => -1];
@ -48,7 +47,7 @@ class MTProto extends PrimeModule
// Load rsa key
\danog\MadelineProto\Logger::log('Loading RSA key...', Logger::ULTRA_VERBOSE);
$this->key = $this->loadKey($this->settings['authorization']['rsa_key']);
$this->key = new RSA($this->settings['authorization']['rsa_key']);
// Istantiate TL class
\danog\MadelineProto\Logger::log('Translating tl schemas...', Logger::ULTRA_VERBOSE);

View File

@ -63,8 +63,7 @@ trait AuthKeyHandler
* Find our key in the server_public_key_fingerprints vector
*/
foreach ($ResPQ['server_public_key_fingerprints'] as $curfp) {
$curfp_biginteger = new \phpseclib\Math\BigInteger($curfp);
if ($this->key['fp']->equals($curfp_biginteger)) {
if ($this->key->keydata['fp'] === $curfp) {
$public_key_fingerprint = $curfp;
break;
}
@ -123,7 +122,7 @@ trait AuthKeyHandler
$sha_digest = sha1($p_q_inner_data, true);
$random_bytes = $this->random(255 - strlen($p_q_inner_data) - strlen($sha_digest));
$to_encrypt = $sha_digest.$p_q_inner_data.$random_bytes;
$encrypted_data = $this->RSA_encrypt($to_encrypt, $this->key);
$encrypted_data = $this->key->encrypt($to_encrypt);
\danog\MadelineProto\Logger::log('Starting Diffie Hellman key exchange', \danog\MadelineProto\Logger::VERBOSE);
/*

View File

@ -12,9 +12,13 @@ If not, see <http://www.gnu.org/licenses/>.
namespace danog\MadelineProto;
trait RSA
class RSA
{
public function loadKey($rsa_key)
use \danog\MadelineProto\TL\TL;
use \danog\MadelineProto\Tools;
public $keydata = [];
public function __construct($rsa_key)
{
\danog\MadelineProto\Logger::log('Istantiating \phpseclib\Crypt\RSA...', LOGGER::ULTRA_VERBOSE);
$key = new \phpseclib\Crypt\RSA();
@ -25,35 +29,31 @@ trait RSA
} else {
$key->loadKey($rsa_key);
}
$keydata = ['n' => $key->modulus, 'e' => $key->exponent];
$this->keydata = ['n' => $key->modulus, 'e' => $key->exponent];
\danog\MadelineProto\Logger::log('Computing fingerprint...', LOGGER::ULTRA_VERBOSE);
$keydata['fp_bytes'] = substr(
$this->keydata['fp'] = \danog\PHP\Struct::unpack('<q', substr(
sha1(
$this->serialize_object(
['type' => 'bytes'],
$keydata['n']->toBytes()
$this->keydata['n']->toBytes()
)
.
$this->serialize_object(
['type' => 'bytes'],
$keydata['e']->toBytes()
$this->keydata['e']->toBytes()
),
true
),
-8
);
\danog\MadelineProto\Logger::log('Generating BigInteger object for fingerprint...', LOGGER::ULTRA_VERBOSE);
$keydata['fp'] = new \phpseclib\Math\BigInteger(strrev($keydata['fp_bytes']), -256);
return $keydata;
))[0];
return $this->keydata;
}
public function RSA_encrypt($data, $keydata)
public function encrypt($data)
{
\danog\MadelineProto\Logger::log('Encrypting with rsa key...', LOGGER::VERBOSE);
return (new \phpseclib\Math\BigInteger($data, 256))->powMod($keydata['e'], $keydata['n'])->toBytes();
return (new \phpseclib\Math\BigInteger($data, 256))->powMod($this->keydata['e'], $this->keydata['n'])->toBytes();
}
}