mirror of
https://github.com/TeamPGM/pyrogram.git
synced 2024-11-28 00:56:19 +00:00
Comment out MTProto 1.0 implementation
This commit is contained in:
parent
a3e5ba8862
commit
87b2c4b1e7
@ -17,6 +17,6 @@
|
||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from .ige import IGE
|
||||
from .kdf import KDF, KDF2
|
||||
from .kdf import KDF2
|
||||
from .prime import Prime
|
||||
from .rsa import RSA
|
||||
|
@ -16,23 +16,23 @@
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from hashlib import sha1, sha256
|
||||
from hashlib import sha256
|
||||
|
||||
|
||||
class KDF:
|
||||
def __new__(cls, auth_key: bytes, msg_key: bytes, outgoing: bool) -> tuple:
|
||||
# https://core.telegram.org/mtproto/description#defining-aes-key-and-initialization-vector
|
||||
x = 0 if outgoing else 8
|
||||
|
||||
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_c = sha1(auth_key[x + 64:x + 96] + msg_key).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_iv = sha1_a[8:20] + sha1_b[:8] + sha1_c[16:20] + sha1_d[:8]
|
||||
|
||||
return aes_key, aes_iv
|
||||
# class KDF:
|
||||
# def __new__(cls, auth_key: bytes, msg_key: bytes, outgoing: bool) -> tuple:
|
||||
# # https://core.telegram.org/mtproto/description#defining-aes-key-and-initialization-vector
|
||||
# x = 0 if outgoing else 8
|
||||
#
|
||||
# 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_c = sha1(auth_key[x + 64:x + 96] + msg_key).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_iv = sha1_a[8:20] + sha1_b[:8] + sha1_c[16:20] + sha1_d[:8]
|
||||
#
|
||||
# return aes_key, aes_iv
|
||||
|
||||
|
||||
class KDF2:
|
||||
|
@ -32,7 +32,7 @@ from pyrogram.api.all import layer
|
||||
from pyrogram.api.core import Message, Object, MsgContainer, Long, FutureSalt
|
||||
from pyrogram.api.errors import Error
|
||||
from pyrogram.connection import Connection
|
||||
from pyrogram.crypto import IGE, KDF, KDF2
|
||||
from pyrogram.crypto import IGE, KDF2
|
||||
from .internals import MsgId, MsgFactory, DataCenter
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
@ -174,13 +174,13 @@ class Session:
|
||||
self.stop()
|
||||
self.start()
|
||||
|
||||
def pack(self, message: Message) -> bytes:
|
||||
data = Long(self.current_salt.salt) + self.session_id + message.write()
|
||||
msg_key = sha1(data).digest()[-16:]
|
||||
aes_key, aes_iv = KDF(self.auth_key, msg_key, True)
|
||||
padding = urandom(-len(data) % 16)
|
||||
|
||||
return self.auth_key_id + msg_key + IGE.encrypt(data + padding, aes_key, aes_iv)
|
||||
# def pack(self, message: Message) -> bytes:
|
||||
# data = Long(self.current_salt.salt) + self.session_id + message.write()
|
||||
# msg_key = sha1(data).digest()[-16:]
|
||||
# aes_key, aes_iv = KDF(self.auth_key, msg_key, True)
|
||||
# padding = urandom(-len(data) % 16)
|
||||
#
|
||||
# return self.auth_key_id + msg_key + IGE.encrypt(data + padding, aes_key, aes_iv)
|
||||
|
||||
def pack2(self, message: Message):
|
||||
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)
|
||||
|
||||
def unpack(self, b: BytesIO) -> Message:
|
||||
assert b.read(8) == self.auth_key_id, b.getvalue()
|
||||
|
||||
msg_key = b.read(16)
|
||||
aes_key, aes_iv = KDF(self.auth_key, msg_key, False)
|
||||
data = BytesIO(IGE.decrypt(b.read(), aes_key, aes_iv))
|
||||
data.read(8) # Server salt
|
||||
|
||||
# https://core.telegram.org/mtproto/security_guidelines#checking-session-id
|
||||
assert data.read(8) == self.session_id
|
||||
|
||||
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-message-length
|
||||
# 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:]
|
||||
|
||||
# https://core.telegram.org/mtproto/security_guidelines#checking-msg-id
|
||||
# TODO: check for lower msg_ids
|
||||
assert message.msg_id % 2 != 0
|
||||
|
||||
return message
|
||||
# def unpack(self, b: BytesIO) -> Message:
|
||||
# assert b.read(8) == self.auth_key_id, b.getvalue()
|
||||
#
|
||||
# msg_key = b.read(16)
|
||||
# aes_key, aes_iv = KDF(self.auth_key, msg_key, False)
|
||||
# data = BytesIO(IGE.decrypt(b.read(), aes_key, aes_iv))
|
||||
# data.read(8) # Server salt
|
||||
#
|
||||
# # https://core.telegram.org/mtproto/security_guidelines#checking-session-id
|
||||
# assert data.read(8) == self.session_id
|
||||
#
|
||||
# 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-message-length
|
||||
# # 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:]
|
||||
#
|
||||
# # https://core.telegram.org/mtproto/security_guidelines#checking-msg-id
|
||||
# # TODO: check for lower msg_ids
|
||||
# assert message.msg_id % 2 != 0
|
||||
#
|
||||
# return message
|
||||
|
||||
def unpack2(self, b: BytesIO) -> Message:
|
||||
assert b.read(8) == self.auth_key_id, b.getvalue()
|
||||
|
Loading…
Reference in New Issue
Block a user