Add pyaesni support when found

This commit is contained in:
painor 2021-04-01 14:32:08 +01:00
parent 4b16183d2b
commit b4737e52f3
2 changed files with 22 additions and 11 deletions

View File

@ -1,3 +1,4 @@
pyaesni
cryptg cryptg
pysocks pysocks
python-socks[asyncio] python-socks[asyncio]

View File

@ -1,7 +1,8 @@
""" """
AES IGE implementation in Python. AES IGE implementation in Python.
If available, cryptg will be used instead, otherwise If available, pyaesni 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.
""" """
@ -10,20 +11,25 @@ import pyaes
import logging import logging
from . import libssl from . import libssl
__log__ = logging.getLogger(__name__) __log__ = logging.getLogger(__name__)
try: try:
import cryptg import pyaesni
__log__.info('cryptg detected, it will be used for encryption')
__log__.info('pyaesni detected, it will be used for encryption')
except ImportError: except ImportError:
cryptg = None pyaesni = None
if libssl.encrypt_ige and libssl.decrypt_ige: try:
__log__.info('libssl detected, it will be used for encryption') import cryptg
else:
__log__.info('cryptg module not installed and libssl not found, ' __log__.info('cryptg detected, it will be used for encryption')
'falling back to (slower) Python encryption') except ImportError:
cryptg = None
if libssl.encrypt_ige and libssl.decrypt_ige:
__log__.info('libssl detected, it will be used for encryption')
else:
__log__.info('pyaesni 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 pyaesni:
return pyaesni.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 pyaesni:
return pyaesni.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: