mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-09-21 03:08:49 +03:00
Use tgcrypto if available (#1715)
This commit is contained in:
parent
d9691c9342
commit
42cc9e61fb
|
@ -1,7 +1,8 @@
|
||||||
"""
|
"""
|
||||||
AES IGE implementation in Python.
|
AES IGE implementation in Python.
|
||||||
|
|
||||||
If available, cryptg will be used instead, otherwise
|
If available, tgcrypto will be used instead, otherwise
|
||||||
|
if available, cryptg will be used instead, otherwise
|
||||||
if available, libssl will be used instead, otherwise
|
if available, libssl will be used instead, otherwise
|
||||||
the Python implementation will be used.
|
the Python implementation will be used.
|
||||||
"""
|
"""
|
||||||
|
@ -15,15 +16,20 @@ __log__ = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import cryptg
|
import tgcrypto
|
||||||
__log__.info('cryptg detected, it will be used for encryption')
|
__log__.debug('tgcrypto detected, it will be used for encryption')
|
||||||
except ImportError:
|
except ImportError:
|
||||||
cryptg = None
|
tgcrypto = None
|
||||||
if libssl.encrypt_ige and libssl.decrypt_ige:
|
try:
|
||||||
__log__.info('libssl detected, it will be used for encryption')
|
import cryptg
|
||||||
else:
|
__log__.debug('cryptg detected, it will be used for encryption')
|
||||||
__log__.info('cryptg module not installed and libssl not found, '
|
except ImportError:
|
||||||
'falling back to (slower) Python encryption')
|
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')
|
||||||
|
|
||||||
|
|
||||||
class AES:
|
class AES:
|
||||||
|
@ -37,6 +43,8 @@ class AES:
|
||||||
Decrypts the given text in 16-bytes blocks by using the
|
Decrypts the given text in 16-bytes blocks by using the
|
||||||
given key and 32-bytes initialization vector.
|
given key and 32-bytes initialization vector.
|
||||||
"""
|
"""
|
||||||
|
if tgcrypto:
|
||||||
|
return tgcrypto.ige256_decrypt(cipher_text, key, iv)
|
||||||
if cryptg:
|
if cryptg:
|
||||||
return cryptg.decrypt_ige(cipher_text, key, iv)
|
return cryptg.decrypt_ige(cipher_text, key, iv)
|
||||||
if libssl.decrypt_ige:
|
if libssl.decrypt_ige:
|
||||||
|
@ -78,6 +86,8 @@ class AES:
|
||||||
if padding:
|
if padding:
|
||||||
plain_text += os.urandom(16 - padding)
|
plain_text += os.urandom(16 - padding)
|
||||||
|
|
||||||
|
if tgcrypto:
|
||||||
|
return tgcrypto.ige256_encrypt(plain_text, key, iv)
|
||||||
if cryptg:
|
if cryptg:
|
||||||
return cryptg.encrypt_ige(plain_text, key, iv)
|
return cryptg.encrypt_ige(plain_text, key, iv)
|
||||||
if libssl.encrypt_ige:
|
if libssl.encrypt_ige:
|
||||||
|
|
|
@ -2,6 +2,19 @@
|
||||||
This module holds the AESModeCTR wrapper class.
|
This module holds the AESModeCTR wrapper class.
|
||||||
"""
|
"""
|
||||||
import pyaes
|
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:
|
class AESModeCTR:
|
||||||
|
@ -16,12 +29,15 @@ class AESModeCTR:
|
||||||
:param iv: the bytes initialization vector. Must have a length of 16.
|
:param iv: the bytes initialization vector. Must have a length of 16.
|
||||||
"""
|
"""
|
||||||
# TODO Use libssl if available
|
# TODO Use libssl if available
|
||||||
assert isinstance(key, bytes)
|
if tgcrypto:
|
||||||
self._aes = pyaes.AESModeOfOperationCTR(key)
|
self._aes = (key, iv, bytearray(1))
|
||||||
|
else:
|
||||||
|
assert isinstance(key, bytes)
|
||||||
|
self._aes = pyaes.AESModeOfOperationCTR(key)
|
||||||
|
|
||||||
assert isinstance(iv, bytes)
|
assert isinstance(iv, bytes)
|
||||||
assert len(iv) == 16
|
assert len(iv) == 16
|
||||||
self._aes._counter._counter = list(iv)
|
self._aes._counter._counter = list(iv)
|
||||||
|
|
||||||
def encrypt(self, data):
|
def encrypt(self, data):
|
||||||
"""
|
"""
|
||||||
|
@ -30,6 +46,8 @@ class AESModeCTR:
|
||||||
:param data: the plain text to be encrypted.
|
:param data: the plain text to be encrypted.
|
||||||
:return: the encrypted cipher text.
|
:return: the encrypted cipher text.
|
||||||
"""
|
"""
|
||||||
|
if tgcrypto:
|
||||||
|
return tgcrypto.ctr256_encrypt(data, *self._aes)
|
||||||
return self._aes.encrypt(data)
|
return self._aes.encrypt(data)
|
||||||
|
|
||||||
def decrypt(self, data):
|
def decrypt(self, data):
|
||||||
|
@ -39,4 +57,6 @@ class AESModeCTR:
|
||||||
:param data: the cipher text to be decrypted.
|
:param data: the cipher text to be decrypted.
|
||||||
:return: the decrypted plain text.
|
:return: the decrypted plain text.
|
||||||
"""
|
"""
|
||||||
|
if tgcrypto:
|
||||||
|
return tgcrypto.ctr256_decrypt(data, *self._aes)
|
||||||
return self._aes.decrypt(data)
|
return self._aes.decrypt(data)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user