2016-08-30 14:11:19 +03:00
|
|
|
import os
|
2016-09-04 13:42:11 +03:00
|
|
|
from utils import BinaryWriter
|
2016-08-30 18:40:49 +03:00
|
|
|
import hashlib
|
|
|
|
|
2016-09-08 17:11:37 +03:00
|
|
|
# region Multiple utilities
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
def generate_random_long(signed=True):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Generates a random long integer (8 bytes), which is optionally signed"""
|
2016-09-03 11:54:58 +03:00
|
|
|
return int.from_bytes(os.urandom(8), signed=signed, byteorder='little')
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
def generate_random_bytes(count):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Generates a random bytes array"""
|
2016-08-30 14:11:19 +03:00
|
|
|
return os.urandom(count)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
|
2016-09-08 17:11:37 +03:00
|
|
|
def load_settings(path='api/settings'):
|
|
|
|
"""Loads the user settings located under `api/`"""
|
|
|
|
settings = {}
|
|
|
|
with open(path, 'r', encoding='utf-8') as file:
|
|
|
|
for line in file:
|
|
|
|
value_pair = line.split('=')
|
|
|
|
left = value_pair[0].strip()
|
|
|
|
right = value_pair[1].strip()
|
|
|
|
if right.isnumeric():
|
|
|
|
settings[left] = int(right)
|
|
|
|
else:
|
|
|
|
settings[left] = right
|
|
|
|
|
|
|
|
return settings
|
|
|
|
|
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Cryptographic related utils
|
2016-08-30 18:40:49 +03:00
|
|
|
|
|
|
|
|
2016-08-26 13:58:53 +03:00
|
|
|
def calc_key(shared_key, msg_key, client):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Calculate the key based on Telegram guidelines, specifying whether it's the client or not"""
|
2016-08-26 13:58:53 +03:00
|
|
|
x = 0 if client else 8
|
|
|
|
|
2016-08-30 18:40:49 +03:00
|
|
|
sha1a = sha1(msg_key + shared_key[x:x + 32])
|
|
|
|
sha1b = sha1(shared_key[x + 32:x + 48] + msg_key + shared_key[x + 48:x + 64])
|
|
|
|
sha1c = sha1(shared_key[x + 64:x + 96] + msg_key)
|
|
|
|
sha1d = sha1(msg_key + shared_key[x + 96:x + 128])
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
key = sha1a[0:8] + sha1b[8:20] + sha1c[4:16]
|
|
|
|
iv = sha1a[8:20] + sha1b[0:8] + sha1c[16:20] + sha1d[0:8]
|
|
|
|
|
|
|
|
return key, iv
|
|
|
|
|
|
|
|
|
|
|
|
def calc_msg_key(data):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Calculates the message key from the given data"""
|
2016-08-26 13:58:53 +03:00
|
|
|
return sha1(data)[4:20]
|
|
|
|
|
|
|
|
|
2016-08-30 18:40:49 +03:00
|
|
|
def generate_key_data_from_nonces(server_nonce, new_nonce):
|
2016-09-04 12:07:18 +03:00
|
|
|
"""Generates the key data corresponding to the given nonces"""
|
2016-08-30 18:40:49 +03:00
|
|
|
hash1 = sha1(bytes(new_nonce + server_nonce))
|
|
|
|
hash2 = sha1(bytes(server_nonce + new_nonce))
|
|
|
|
hash3 = sha1(bytes(new_nonce + new_nonce))
|
|
|
|
|
|
|
|
with BinaryWriter() as key_buffer:
|
|
|
|
with BinaryWriter() as iv_buffer:
|
|
|
|
key_buffer.write(hash1)
|
|
|
|
key_buffer.write(hash2[:12])
|
|
|
|
|
|
|
|
iv_buffer.write(hash2[12:20])
|
|
|
|
iv_buffer.write(hash3)
|
2016-09-03 17:46:40 +03:00
|
|
|
iv_buffer.write(new_nonce[:4])
|
2016-08-30 18:40:49 +03:00
|
|
|
|
|
|
|
return key_buffer.get_bytes(), iv_buffer.get_bytes()
|
|
|
|
|
|
|
|
|
|
|
|
def sha1(data):
|
2016-09-04 12:07:18 +03:00
|
|
|
"""Calculates the SHA1 digest for the given data"""
|
2016-09-03 11:54:58 +03:00
|
|
|
sha = hashlib.sha1()
|
2016-08-30 18:40:49 +03:00
|
|
|
sha.update(data)
|
|
|
|
return sha.digest()
|
2016-09-04 12:07:18 +03:00
|
|
|
|
2016-09-08 17:11:37 +03:00
|
|
|
# endregion
|