Skip to content

Commit e3b05cb

Browse files
committed
Add MTProto 2.0 key derivation function
1 parent fe9b968 commit e3b05cb

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

pyrogram/crypto/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from .ige import IGE
20-
from .kdf import KDF
20+
from .kdf import KDF, KDF2
2121
from .prime import Prime
2222
from .rsa import RSA

pyrogram/crypto/kdf.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19-
from hashlib import sha1
19+
from hashlib import sha1, sha256
2020

2121

2222
class KDF:
@@ -33,3 +33,17 @@ def __new__(cls, auth_key: bytes, msg_key: bytes, outgoing: bool) -> tuple:
3333
aes_iv = sha1_a[8:20] + sha1_b[:8] + sha1_c[16:20] + sha1_d[:8]
3434

3535
return aes_key, aes_iv
36+
37+
38+
class KDF2:
39+
def __new__(cls, auth_key: bytes, msg_key: bytes, outgoing: bool) -> tuple:
40+
# https://core.telegram.org/mtproto/description#defining-aes-key-and-initialization-vector
41+
x = 0 if outgoing else 8
42+
43+
sha256_a = sha256(msg_key + auth_key[x: x + 36]).digest()
44+
sha256_b = sha256(auth_key[x + 40:x + 76] + msg_key).digest() # 76 = 40 + 36
45+
46+
aes_key = sha256_a[:8] + sha256_b[8:24] + sha256_a[24:32]
47+
aes_iv = sha256_b[:8] + sha256_a[8:24] + sha256_b[24:32]
48+
49+
return aes_key, aes_iv

0 commit comments

Comments
 (0)