Reuse the AESModeCTR class on CdnDecrypter

This commit is contained in:
Lonami Exo 2017-08-28 20:30:33 +02:00
parent 5404670469
commit bc72e52834
2 changed files with 7 additions and 19 deletions

View File

@ -12,18 +12,10 @@ class AESModeCTR:
assert isinstance(iv, bytes) assert isinstance(iv, bytes)
assert len(iv) == 16 assert len(iv) == 16
self.iv = iv self._aes._counter._counter = list(iv)
self._aes._counter._counter = list(self.iv)
def reset(self):
pass
def encrypt(self, data): def encrypt(self, data):
result = self._aes.encrypt(data) return self._aes.encrypt(data)
self.reset()
return result
def decrypt(self, data): def decrypt(self, data):
result = self._aes.decrypt(data) return self._aes.decrypt(data)
self.reset()
return result

View File

@ -1,9 +1,9 @@
from hashlib import sha256 from hashlib import sha256
import pyaes
from ..tl import JsonSession from ..tl import JsonSession
from ..tl.functions.upload import GetCdnFileRequest, ReuploadCdnFileRequest from ..tl.functions.upload import GetCdnFileRequest, ReuploadCdnFileRequest
from ..tl.types.upload import CdnFileReuploadNeeded from ..tl.types.upload import CdnFileReuploadNeeded
from ..crypto import AESModeCTR
from ..errors import CdnFileTamperedError from ..errors import CdnFileTamperedError
@ -29,13 +29,9 @@ class CdnDecrypter:
""" """
# TODO Avoid the need for 'client_cls=TelegramBareClient' # TODO Avoid the need for 'client_cls=TelegramBareClient'
# https://core.telegram.org/cdn # https://core.telegram.org/cdn
# TODO Use libssl if available cdn_aes = AESModeCTR(
cdn_aes = pyaes.AESModeOfOperationCTR(cdn_redirect.encryption_key) key=cdn_redirect.encryption_key,
iv=cdn_redirect.encryption_iv[:12] + (offset >> 4).to_bytes(4, 'big')
# The returned IV is the counter used on CTR
cdn_aes._counter._counter = list(
cdn_redirect.encryption_iv[:12] +
(offset >> 4).to_bytes(4, 'big')
) )
# Create a new client on said CDN # Create a new client on said CDN