Dump libssl bindings in favour of the new optional cryptg module

This commit is contained in:
Lonami Exo
2018-02-16 18:24:44 +01:00
parent c11aefa95b
commit 030f292203
2 changed files with 64 additions and 167 deletions

View File

@@ -3,86 +3,90 @@ AES IGE implementation in Python. This module may use libssl if available.
"""
import os
import pyaes
from . import libssl
try:
import cryptg
except ImportError:
cryptg = None
if libssl.AES is not None:
# Use libssl if available, since it will be faster
AES = libssl.AES
else:
# Fallback to a pure Python implementation
class AES:
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):
"""
Class that servers as an interface to encrypt and decrypt
text through the AES IGE mode.
Decrypts the given text in 16-bytes blocks by using the
given key and 32-bytes initialization vector.
"""
@staticmethod
def decrypt_ige(cipher_text, key, iv):
"""
Decrypts the given text in 16-bytes blocks by using the
given key and 32-bytes initialization vector.
"""
iv1 = iv[:len(iv) // 2]
iv2 = iv[len(iv) // 2:]
if cryptg:
return cryptg.decrypt_ige(cipher_text, key, iv)
aes = pyaes.AES(key)
iv1 = iv[:len(iv) // 2]
iv2 = iv[len(iv) // 2:]
plain_text = []
blocks_count = len(cipher_text) // 16
aes = pyaes.AES(key)
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]
plain_text = []
blocks_count = len(cipher_text) // 16
plain_text_block = aes.decrypt(cipher_text_block)
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]
for i in range(16):
plain_text_block[i] ^= iv1[i]
plain_text_block = aes.decrypt(cipher_text_block)
iv1 = cipher_text[block_index * 16:block_index * 16 + 16]
iv2 = plain_text_block
for i in range(16):
plain_text_block[i] ^= iv1[i]
plain_text.extend(plain_text_block)
iv1 = cipher_text[block_index * 16:block_index * 16 + 16]
iv2 = plain_text_block
return bytes(plain_text)
plain_text.extend(plain_text_block)
@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.
"""
return bytes(plain_text)
# Add random padding iff it's not evenly divisible by 16 already
if len(plain_text) % 16 != 0:
padding_count = 16 - len(plain_text) % 16
plain_text += os.urandom(padding_count)
@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.
"""
# Add random padding iff it's not evenly divisible by 16 already
if len(plain_text) % 16 != 0:
padding_count = 16 - len(plain_text) % 16
plain_text += os.urandom(padding_count)
iv1 = iv[:len(iv) // 2]
iv2 = iv[len(iv) // 2:]
if cryptg:
return cryptg.encrypt_ige(plain_text, key, iv)
aes = pyaes.AES(key)
iv1 = iv[:len(iv) // 2]
iv2 = iv[len(iv) // 2:]
cipher_text = []
blocks_count = len(plain_text) // 16
aes = pyaes.AES(key)
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]
cipher_text = []
blocks_count = len(plain_text) // 16
cipher_text_block = aes.encrypt(plain_text_block)
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]
for i in range(16):
cipher_text_block[i] ^= iv2[i]
cipher_text_block = aes.encrypt(plain_text_block)
iv1 = cipher_text_block
iv2 = plain_text[block_index * 16:block_index * 16 + 16]
for i in range(16):
cipher_text_block[i] ^= iv2[i]
cipher_text.extend(cipher_text_block)
iv1 = cipher_text_block
iv2 = plain_text[block_index * 16:block_index * 16 + 16]
return bytes(cipher_text)
cipher_text.extend(cipher_text_block)
return bytes(cipher_text)