From 4d3ff0e175da6db909794ab66d2d8fa6ed482dad Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 20 Mar 2021 17:20:33 +0100 Subject: [PATCH] Revert "Use tgcrypto if available (#1715)" This reverts commit 42cc9e61fbc55cff8dc7eeacecddb579d8adb85f. tgcrypto was made for Pyrogram, and seeing it used elsewhere without much credit "hurts" the author. I personally do not endorse its use, hence the lack of attention or notes in the documentation. People who still want to benefit from the speed boost should go out of their way to discover, install and patch Telethon's aes.py module instead, all while complying with the respective license (another reason to avoid said code in Telethon, which is under the much more permissive MIT license). People using tgcrypto for anything other than Pyrogram will do so knowing full-well that this was not the library's intended usage. --- telethon/crypto/aes.py | 28 +++++++++------------------- telethon/crypto/aesctr.py | 30 +++++------------------------- 2 files changed, 14 insertions(+), 44 deletions(-) diff --git a/telethon/crypto/aes.py b/telethon/crypto/aes.py index 7bd2fe1a..3cfcc1af 100644 --- a/telethon/crypto/aes.py +++ b/telethon/crypto/aes.py @@ -1,8 +1,7 @@ """ AES IGE implementation in Python. -If available, tgcrypto will be used instead, otherwise -if available, cryptg will be used instead, otherwise +If available, cryptg will be used instead, otherwise if available, libssl will be used instead, otherwise the Python implementation will be used. """ @@ -16,20 +15,15 @@ __log__ = logging.getLogger(__name__) try: - import tgcrypto - __log__.debug('tgcrypto detected, it will be used for encryption') + import cryptg + __log__.info('cryptg detected, it will be used for encryption') except ImportError: - tgcrypto = None - try: - import cryptg - __log__.debug('cryptg detected, it will be used for encryption') - except ImportError: - cryptg = None - if libssl.encrypt_ige and libssl.decrypt_ige: - __log__.debug('libssl detected, it will be used for encryption') - else: - __log__.debug('tgcrypto or cryptg modules not installed and libssl not found, ' - 'falling back to (slower) Python encryption') + cryptg = None + if libssl.encrypt_ige and libssl.decrypt_ige: + __log__.info('libssl detected, it will be used for encryption') + else: + __log__.info('cryptg module not installed and libssl not found, ' + 'falling back to (slower) Python encryption') class AES: @@ -43,8 +37,6 @@ class AES: Decrypts the given text in 16-bytes blocks by using the given key and 32-bytes initialization vector. """ - if tgcrypto: - return tgcrypto.ige256_decrypt(cipher_text, key, iv) if cryptg: return cryptg.decrypt_ige(cipher_text, key, iv) if libssl.decrypt_ige: @@ -86,8 +78,6 @@ class AES: if padding: plain_text += os.urandom(16 - padding) - if tgcrypto: - return tgcrypto.ige256_encrypt(plain_text, key, iv) if cryptg: return cryptg.encrypt_ige(plain_text, key, iv) if libssl.encrypt_ige: diff --git a/telethon/crypto/aesctr.py b/telethon/crypto/aesctr.py index d1f5947f..34422904 100644 --- a/telethon/crypto/aesctr.py +++ b/telethon/crypto/aesctr.py @@ -2,19 +2,6 @@ This module holds the AESModeCTR wrapper class. """ import pyaes -import logging - - -__log__ = logging.getLogger(__name__) - - -try: - import tgcrypto - __log__.debug('tgcrypto detected, it will be used for ctr encryption') -except ImportError: - tgcrypto = None - __log__.debug('tgcrypto module not installed, ' - 'falling back to (slower) Python encryption') class AESModeCTR: @@ -29,15 +16,12 @@ class AESModeCTR: :param iv: the bytes initialization vector. Must have a length of 16. """ # TODO Use libssl if available - if tgcrypto: - self._aes = (key, iv, bytearray(1)) - else: - assert isinstance(key, bytes) - self._aes = pyaes.AESModeOfOperationCTR(key) + assert isinstance(key, bytes) + self._aes = pyaes.AESModeOfOperationCTR(key) - assert isinstance(iv, bytes) - assert len(iv) == 16 - self._aes._counter._counter = list(iv) + assert isinstance(iv, bytes) + assert len(iv) == 16 + self._aes._counter._counter = list(iv) def encrypt(self, data): """ @@ -46,8 +30,6 @@ class AESModeCTR: :param data: the plain text to be encrypted. :return: the encrypted cipher text. """ - if tgcrypto: - return tgcrypto.ctr256_encrypt(data, *self._aes) return self._aes.encrypt(data) def decrypt(self, data): @@ -57,6 +39,4 @@ class AESModeCTR: :param data: the cipher text to be decrypted. :return: the decrypted plain text. """ - if tgcrypto: - return tgcrypto.ctr256_decrypt(data, *self._aes) return self._aes.decrypt(data)