2017-11-26 18:57:40 +03:00
|
|
|
"""
|
2018-07-25 13:11:58 +03:00
|
|
|
AES IGE implementation in Python.
|
|
|
|
|
2021-03-20 19:20:33 +03:00
|
|
|
If available, cryptg will be used instead, otherwise
|
2018-07-25 13:11:58 +03:00
|
|
|
if available, libssl will be used instead, otherwise
|
|
|
|
the Python implementation will be used.
|
2017-11-26 18:57:40 +03:00
|
|
|
"""
|
2016-09-16 15:04:46 +03:00
|
|
|
import os
|
2016-09-03 21:34:24 +03:00
|
|
|
import pyaes
|
2018-07-25 13:11:58 +03:00
|
|
|
import logging
|
|
|
|
from . import libssl
|
|
|
|
|
|
|
|
|
|
|
|
__log__ = logging.getLogger(__name__)
|
|
|
|
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
try:
|
2021-03-20 19:20:33 +03:00
|
|
|
import cryptg
|
|
|
|
__log__.info('cryptg detected, it will be used for encryption')
|
2018-02-16 20:24:44 +03:00
|
|
|
except ImportError:
|
2021-03-20 19:20:33 +03:00
|
|
|
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')
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
|
|
|
|
class AES:
|
|
|
|
"""
|
|
|
|
Class that servers as an interface to encrypt and decrypt
|
|
|
|
text through the AES IGE mode.
|
|
|
|
"""
|
|
|
|
@staticmethod
|
|
|
|
def decrypt_ige(cipher_text, key, iv):
|
2017-11-26 18:57:40 +03:00
|
|
|
"""
|
2018-02-16 20:24:44 +03:00
|
|
|
Decrypts the given text in 16-bytes blocks by using the
|
|
|
|
given key and 32-bytes initialization vector.
|
2017-11-26 18:57:40 +03:00
|
|
|
"""
|
2018-02-16 20:24:44 +03:00
|
|
|
if cryptg:
|
|
|
|
return cryptg.decrypt_ige(cipher_text, key, iv)
|
2018-07-25 13:11:58 +03:00
|
|
|
if libssl.decrypt_ige:
|
|
|
|
return libssl.decrypt_ige(cipher_text, key, iv)
|
2018-02-16 20:24:44 +03:00
|
|
|
|
|
|
|
iv1 = iv[:len(iv) // 2]
|
|
|
|
iv2 = iv[len(iv) // 2:]
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
aes = pyaes.AES(key)
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
plain_text = []
|
|
|
|
blocks_count = len(cipher_text) // 16
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
cipher_text_block = [0] * 16
|
|
|
|
for block_index in range(blocks_count):
|
|
|
|
for i in range(16):
|
|
|
|
cipher_text_block[i] = \
|
|
|
|
cipher_text[block_index * 16 + i] ^ iv2[i]
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
plain_text_block = aes.decrypt(cipher_text_block)
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
for i in range(16):
|
|
|
|
plain_text_block[i] ^= iv1[i]
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
iv1 = cipher_text[block_index * 16:block_index * 16 + 16]
|
|
|
|
iv2 = plain_text_block
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
plain_text.extend(plain_text_block)
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
return bytes(plain_text)
|
2016-09-16 15:04:46 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
@staticmethod
|
|
|
|
def encrypt_ige(plain_text, key, iv):
|
|
|
|
"""
|
|
|
|
Encrypts the given text in 16-bytes blocks by using the
|
|
|
|
given key and 32-bytes initialization vector.
|
|
|
|
"""
|
2018-07-25 13:11:58 +03:00
|
|
|
padding = len(plain_text) % 16
|
|
|
|
if padding:
|
|
|
|
plain_text += os.urandom(16 - padding)
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
if cryptg:
|
|
|
|
return cryptg.encrypt_ige(plain_text, key, iv)
|
2018-07-25 13:11:58 +03:00
|
|
|
if libssl.encrypt_ige:
|
|
|
|
return libssl.encrypt_ige(plain_text, key, iv)
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
iv1 = iv[:len(iv) // 2]
|
|
|
|
iv2 = iv[len(iv) // 2:]
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
aes = pyaes.AES(key)
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
cipher_text = []
|
|
|
|
blocks_count = len(plain_text) // 16
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
for block_index in range(blocks_count):
|
|
|
|
plain_text_block = list(
|
|
|
|
plain_text[block_index * 16:block_index * 16 + 16]
|
|
|
|
)
|
|
|
|
for i in range(16):
|
|
|
|
plain_text_block[i] ^= iv1[i]
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
cipher_text_block = aes.encrypt(plain_text_block)
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
for i in range(16):
|
|
|
|
cipher_text_block[i] ^= iv2[i]
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
iv1 = cipher_text_block
|
|
|
|
iv2 = plain_text[block_index * 16:block_index * 16 + 16]
|
2016-09-03 21:34:24 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
cipher_text.extend(cipher_text_block)
|
2017-08-20 02:21:11 +03:00
|
|
|
|
2018-02-16 20:24:44 +03:00
|
|
|
return bytes(cipher_text)
|