Comment out MTProto 1.0 implementation

This commit is contained in:
Dan 2017-12-09 02:25:14 +01:00
parent a3e5ba8862
commit 87b2c4b1e7
3 changed files with 47 additions and 47 deletions

View File

@ -17,6 +17,6 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from .ige import IGE from .ige import IGE
from .kdf import KDF, KDF2 from .kdf import KDF2
from .prime import Prime from .prime import Prime
from .rsa import RSA from .rsa import RSA

View File

@ -16,23 +16,23 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from hashlib import sha1, sha256 from hashlib import sha256
class KDF: # class KDF:
def __new__(cls, auth_key: bytes, msg_key: bytes, outgoing: bool) -> tuple: # def __new__(cls, auth_key: bytes, msg_key: bytes, outgoing: bool) -> tuple:
# https://core.telegram.org/mtproto/description#defining-aes-key-and-initialization-vector # # https://core.telegram.org/mtproto/description#defining-aes-key-and-initialization-vector
x = 0 if outgoing else 8 # x = 0 if outgoing else 8
#
sha1_a = sha1(msg_key + auth_key[x:x + 32]).digest() # sha1_a = sha1(msg_key + auth_key[x:x + 32]).digest()
sha1_b = sha1(auth_key[x + 32:x + 48] + msg_key + auth_key[x + 48:x + 64]).digest() # sha1_b = sha1(auth_key[x + 32:x + 48] + msg_key + auth_key[x + 48:x + 64]).digest()
sha1_c = sha1(auth_key[x + 64:x + 96] + msg_key).digest() # sha1_c = sha1(auth_key[x + 64:x + 96] + msg_key).digest()
sha1_d = sha1(msg_key + auth_key[x + 96:x + 128]).digest() # sha1_d = sha1(msg_key + auth_key[x + 96:x + 128]).digest()
#
aes_key = sha1_a[:8] + sha1_b[8:20] + sha1_c[4:16] # aes_key = sha1_a[:8] + sha1_b[8:20] + sha1_c[4:16]
aes_iv = sha1_a[8:20] + sha1_b[:8] + sha1_c[16:20] + sha1_d[:8] # aes_iv = sha1_a[8:20] + sha1_b[:8] + sha1_c[16:20] + sha1_d[:8]
#
return aes_key, aes_iv # return aes_key, aes_iv
class KDF2: class KDF2:

View File

@ -32,7 +32,7 @@ from pyrogram.api.all import layer
from pyrogram.api.core import Message, Object, MsgContainer, Long, FutureSalt from pyrogram.api.core import Message, Object, MsgContainer, Long, FutureSalt
from pyrogram.api.errors import Error from pyrogram.api.errors import Error
from pyrogram.connection import Connection from pyrogram.connection import Connection
from pyrogram.crypto import IGE, KDF, KDF2 from pyrogram.crypto import IGE, KDF2
from .internals import MsgId, MsgFactory, DataCenter from .internals import MsgId, MsgFactory, DataCenter
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -174,13 +174,13 @@ class Session:
self.stop() self.stop()
self.start() self.start()
def pack(self, message: Message) -> bytes: # def pack(self, message: Message) -> bytes:
data = Long(self.current_salt.salt) + self.session_id + message.write() # data = Long(self.current_salt.salt) + self.session_id + message.write()
msg_key = sha1(data).digest()[-16:] # msg_key = sha1(data).digest()[-16:]
aes_key, aes_iv = KDF(self.auth_key, msg_key, True) # aes_key, aes_iv = KDF(self.auth_key, msg_key, True)
padding = urandom(-len(data) % 16) # padding = urandom(-len(data) % 16)
#
return self.auth_key_id + msg_key + IGE.encrypt(data + padding, aes_key, aes_iv) # return self.auth_key_id + msg_key + IGE.encrypt(data + padding, aes_key, aes_iv)
def pack2(self, message: Message): def pack2(self, message: Message):
data = Long(self.current_salt.salt) + self.session_id + message.write() data = Long(self.current_salt.salt) + self.session_id + message.write()
@ -197,29 +197,29 @@ class Session:
return self.auth_key_id + msg_key + IGE.encrypt(data + padding, aes_key, aes_iv) return self.auth_key_id + msg_key + IGE.encrypt(data + padding, aes_key, aes_iv)
def unpack(self, b: BytesIO) -> Message: # def unpack(self, b: BytesIO) -> Message:
assert b.read(8) == self.auth_key_id, b.getvalue() # assert b.read(8) == self.auth_key_id, b.getvalue()
#
msg_key = b.read(16) # msg_key = b.read(16)
aes_key, aes_iv = KDF(self.auth_key, msg_key, False) # aes_key, aes_iv = KDF(self.auth_key, msg_key, False)
data = BytesIO(IGE.decrypt(b.read(), aes_key, aes_iv)) # data = BytesIO(IGE.decrypt(b.read(), aes_key, aes_iv))
data.read(8) # Server salt # data.read(8) # Server salt
#
# https://core.telegram.org/mtproto/security_guidelines#checking-session-id # # https://core.telegram.org/mtproto/security_guidelines#checking-session-id
assert data.read(8) == self.session_id # assert data.read(8) == self.session_id
#
message = Message.read(data) # message = Message.read(data)
#
# https://core.telegram.org/mtproto/security_guidelines#checking-sha1-hash-value-of-msg-key # # https://core.telegram.org/mtproto/security_guidelines#checking-sha1-hash-value-of-msg-key
# https://core.telegram.org/mtproto/security_guidelines#checking-message-length # # https://core.telegram.org/mtproto/security_guidelines#checking-message-length
# 32 = salt (8) + session_id (8) + msg_id (8) + seq_no (4) + length (4) # # 32 = salt (8) + session_id (8) + msg_id (8) + seq_no (4) + length (4)
assert msg_key == sha1(data.getvalue()[:32 + message.length]).digest()[-16:] # assert msg_key == sha1(data.getvalue()[:32 + message.length]).digest()[-16:]
#
# https://core.telegram.org/mtproto/security_guidelines#checking-msg-id # # https://core.telegram.org/mtproto/security_guidelines#checking-msg-id
# TODO: check for lower msg_ids # # TODO: check for lower msg_ids
assert message.msg_id % 2 != 0 # assert message.msg_id % 2 != 0
#
return message # return message
def unpack2(self, b: BytesIO) -> Message: def unpack2(self, b: BytesIO) -> Message:
assert b.read(8) == self.auth_key_id, b.getvalue() assert b.read(8) == self.auth_key_id, b.getvalue()