Skip to content

Commit afcd19a

Browse files
committed
Add crypto package
1 parent 981d644 commit afcd19a

File tree

5 files changed

+255
-0
lines changed

5 files changed

+255
-0
lines changed

pyrogram/crypto/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from .ige import IGE
20+
from .kdf import KDF
21+
from .prime import Prime
22+
from .rsa import RSA

pyrogram/crypto/ige.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from pyaes import AES
20+
21+
BLOCK_SIZE = 16
22+
23+
24+
# TODO: Performance optimization
25+
26+
class IGE:
27+
@classmethod
28+
def encrypt(cls, data: bytes, key: bytes, iv: bytes) -> bytes:
29+
return cls.ige(data, key, iv, True)
30+
31+
@classmethod
32+
def decrypt(cls, data: bytes, key: bytes, iv: bytes) -> bytes:
33+
return cls.ige(data, key, iv, False)
34+
35+
@staticmethod
36+
def xor(a: bytes, b: bytes) -> bytes:
37+
return int.to_bytes(
38+
int.from_bytes(a, "big") ^ int.from_bytes(b, "big"),
39+
len(a),
40+
"big",
41+
)
42+
43+
@classmethod
44+
def ige(cls, data: bytes, key: bytes, iv: bytes, encrypt: bool) -> bytes:
45+
cipher = AES(key)
46+
47+
iv_1 = iv[:BLOCK_SIZE]
48+
iv_2 = iv[BLOCK_SIZE:]
49+
50+
data = [data[i: i + BLOCK_SIZE] for i in range(0, len(data), BLOCK_SIZE)]
51+
52+
if encrypt:
53+
for i, chunk in enumerate(data):
54+
iv_1 = data[i] = cls.xor(cipher.encrypt(cls.xor(chunk, iv_1)), iv_2)
55+
iv_2 = chunk
56+
else:
57+
for i, chunk in enumerate(data):
58+
iv_2 = data[i] = cls.xor(cipher.decrypt(cls.xor(chunk, iv_2)), iv_1)
59+
iv_1 = chunk
60+
61+
return b"".join(data)

pyrogram/crypto/kdf.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from hashlib import sha1
20+
21+
22+
class KDF:
23+
def __new__(cls, auth_key: bytes, msg_key: bytes, outgoing: bool) -> tuple:
24+
# https://core.telegram.org/mtproto/description#defining-aes-key-and-initialization-vector
25+
x = 0 if outgoing else 8
26+
27+
sha1_a = sha1(msg_key + auth_key[x:x + 32]).digest()
28+
sha1_b = sha1(auth_key[x + 32:x + 48] + msg_key + auth_key[x + 48:x + 64]).digest()
29+
sha1_c = sha1(auth_key[x + 64:x + 96] + msg_key).digest()
30+
sha1_d = sha1(msg_key + auth_key[x + 96:x + 128]).digest()
31+
32+
aes_key = sha1_a[:8] + sha1_b[8:20] + sha1_c[4:16]
33+
aes_iv = sha1_a[8:20] + sha1_b[:8] + sha1_c[16:20] + sha1_d[:8]
34+
35+
return aes_key, aes_iv

pyrogram/crypto/prime.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from random import randint
20+
21+
22+
class Prime:
23+
# Recursive variant
24+
# @classmethod
25+
# def gcd(cls, a: int, b: int) -> int:
26+
# return cls.gcd(b, a % b) if b else a
27+
28+
@staticmethod
29+
def gcd(a: int, b: int) -> int:
30+
while b:
31+
a, b = b, a % b
32+
33+
return a
34+
35+
@classmethod
36+
def decompose(cls, pq: int) -> int:
37+
# https://comeoncodeon.wordpress.com/2010/09/18/pollard-rho-brent-integer-factorization/
38+
if pq % 2 == 0:
39+
return 2
40+
41+
y, c, m = randint(1, pq - 1), randint(1, pq - 1), randint(1, pq - 1)
42+
g = r = q = 1
43+
x = ys = 0
44+
45+
while g == 1:
46+
x = y
47+
48+
for i in range(r):
49+
y = (pow(y, 2, pq) + c) % pq
50+
51+
k = 0
52+
53+
while k < r and g == 1:
54+
ys = y
55+
56+
for i in range(min(m, r - k)):
57+
y = (pow(y, 2, pq) + c) % pq
58+
q = q * (abs(x - y)) % pq
59+
60+
g = cls.gcd(q, pq)
61+
k += m
62+
63+
r *= 2
64+
65+
if g == pq:
66+
while True:
67+
ys = (pow(ys, 2, pq) + c) % pq
68+
g = cls.gcd(abs(x - ys), pq)
69+
70+
if g > 1:
71+
break
72+
73+
return g

pyrogram/crypto/rsa.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from collections import namedtuple
20+
21+
PublicKey = namedtuple("PublicKey", ["m", "e"])
22+
23+
24+
class RSA:
25+
# To get modulus and exponent:
26+
# grep -v -- - public.key | tr -d \\n | base64 -d | openssl asn1parse -inform DER -i
27+
28+
# TODO Add CDNs keys
29+
server_public_keys = {
30+
0xc3b42b026ce86b21 - (1 << 64): PublicKey(
31+
# -----BEGIN RSA PUBLIC KEY-----
32+
# MIIBCgKCAQEAwVACPi9w23mF3tBkdZz+zwrzKOaaQdr01vAbU4E1pvkfj4sqDsm6
33+
# lyDONS789sVoD/xCS9Y0hkkC3gtL1tSfTlgCMOOul9lcixlEKzwKENj1Yz/s7daS
34+
# an9tqw3bfUV/nqgbhGX81v/+7RFAEd+RwFnK7a+XYl9sluzHRyVVaTTveB2GazTw
35+
# Efzk2DWgkBluml8OREmvfraX3bkHZJTKX4EQSjBbbdJ2ZXIsRrYOXfaA+xayEGB+
36+
# 8hdlLmAjbCVfaigxX0CDqWeR1yFL9kwd9P0NsZRPsmoqVwMbMu7mStFai6aIhc3n
37+
# Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
38+
# -----END RSA PUBLIC KEY-----
39+
int(
40+
"C150023E2F70DB7985DED064759CFECF0AF328E69A41DAF4D6F01B538135A6F9"
41+
"1F8F8B2A0EC9BA9720CE352EFCF6C5680FFC424BD634864902DE0B4BD6D49F4E"
42+
"580230E3AE97D95C8B19442B3C0A10D8F5633FECEDD6926A7F6DAB0DDB7D457F"
43+
"9EA81B8465FCD6FFFEED114011DF91C059CAEDAF97625F6C96ECC74725556934"
44+
"EF781D866B34F011FCE4D835A090196E9A5F0E4449AF7EB697DDB9076494CA5F"
45+
"81104A305B6DD27665722C46B60E5DF680FB16B210607EF217652E60236C255F"
46+
"6A28315F4083A96791D7214BF64C1DF4FD0DB1944FB26A2A57031B32EEE64AD1"
47+
"5A8BA68885CDE74A5BFC920F6ABF59BA5C75506373E7130F9042DA922179251F",
48+
16
49+
), # Modulus
50+
int("010001", 16) # Exponent
51+
)
52+
}
53+
54+
@classmethod
55+
def encrypt(cls, data: bytes, fingerprint: int) -> bytes:
56+
return int.to_bytes(
57+
pow(
58+
int.from_bytes(data, "big"),
59+
cls.server_public_keys[fingerprint].e,
60+
cls.server_public_keys[fingerprint].m
61+
),
62+
256,
63+
"big"
64+
)

0 commit comments

Comments
 (0)