Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.key = key
self.ota = ota
self.iv = None
def setup_iv(self, iv=None):
self.iv = os.urandom(self.IV_LENGTH) if iv is None else iv
self.setup()
return self
def decrypt(self, s):
return self.cipher.decrypt(s)
def encrypt(self, s):
return self.cipher.encrypt(s)
@classmethod
def name(cls):
return cls.__name__.replace('_Cipher', '').replace('_', '-').lower()
class AEADCipher(BaseCipher):
PACKET_LIMIT = 16*1024-1
def setup_iv(self, iv=None):
self.iv = os.urandom(self.IV_LENGTH) if iv is None else iv
randkey = hmac.new(self.iv, self.key, hashlib.sha1).digest()
blocks_needed = (self.KEY_LENGTH + len(randkey) - 1) // len(randkey)
okm = bytearray()
output_block = b''
for counter in range(blocks_needed):
output_block = hmac.new(randkey, output_block + b'ss-subkey' + bytes([counter+1]), hashlib.sha1).digest()
okm.extend(output_block)
self.key = bytes(okm[:self.KEY_LENGTH])
self._nonce = 0
self._buffer = bytearray()
self._declen = None
self.setup()
@property
KEY_LENGTH = IV_LENGTH = 16
class ChaCha20_IETF_POLY1305_Cipher(AEADCipher):
KEY_LENGTH = 32
IV_LENGTH = 32
NONCE_LENGTH = 12
TAG_LENGTH = 16
def decrypt_and_verify(self, buffer, tag):
return self.cipher_new(self.nonce).decrypt_and_verify(buffer, tag)
def encrypt_and_digest(self, buffer):
return self.cipher_new(self.nonce).encrypt_and_digest(buffer)
def setup(self):
from Crypto.Cipher import ChaCha20_Poly1305
self.cipher_new = lambda nonce: ChaCha20_Poly1305.new(key=self.key, nonce=nonce)
class BF_CFB_Cipher(BaseCipher):
KEY_LENGTH = 16
IV_LENGTH = 8
def setup(self):
from Crypto.Cipher import Blowfish
self.cipher = Blowfish.new(self.key, Blowfish.MODE_CFB, iv=self.iv, segment_size=64)
class CAST5_CFB_Cipher(BaseCipher):
KEY_LENGTH = 16
IV_LENGTH = 8
def setup(self):
from Crypto.Cipher import CAST
self.cipher = CAST.new(self.key, CAST.MODE_CFB, iv=self.iv, segment_size=64)
class DES_CFB_Cipher(BaseCipher):
KEY_LENGTH = 8
IV_LENGTH = 8
def decrypt_and_verify(self, buffer, tag):
return self.cipher_new(self.nonce).decrypt_and_verify(buffer, tag)
def encrypt_and_digest(self, buffer):
return self.cipher_new(self.nonce).encrypt_and_digest(buffer)
def setup(self):
from Crypto.Cipher import ChaCha20_Poly1305
self.cipher_new = lambda nonce: ChaCha20_Poly1305.new(key=self.key, nonce=nonce)
class BF_CFB_Cipher(BaseCipher):
KEY_LENGTH = 16
IV_LENGTH = 8
def setup(self):
from Crypto.Cipher import Blowfish
self.cipher = Blowfish.new(self.key, Blowfish.MODE_CFB, iv=self.iv, segment_size=64)
class CAST5_CFB_Cipher(BaseCipher):
KEY_LENGTH = 16
IV_LENGTH = 8
def setup(self):
from Crypto.Cipher import CAST
self.cipher = CAST.new(self.key, CAST.MODE_CFB, iv=self.iv, segment_size=64)
class DES_CFB_Cipher(BaseCipher):
KEY_LENGTH = 8
IV_LENGTH = 8
def setup(self):
from Crypto.Cipher import DES
self.cipher = DES.new(self.key, DES.MODE_CFB, iv=self.iv, segment_size=64)
class PacketCipher:
def __init__(self, cipher, key, name):
self.cipher = lambda iv=None: cipher(key).setup_iv(iv)
KEY_LENGTH = 32
IV_LENGTH = 8
def setup(self):
from Crypto.Cipher import ChaCha20
self.cipher = ChaCha20.new(key=self.key, nonce=self.iv)
class ChaCha20_IETF_Cipher(ChaCha20_Cipher):
IV_LENGTH = 12
class Salsa20_Cipher(BaseCipher):
KEY_LENGTH = 32
IV_LENGTH = 8
def setup(self):
from Crypto.Cipher import Salsa20
self.cipher = Salsa20.new(key=self.key, nonce=self.iv)
class AES_256_CFB_Cipher(BaseCipher):
KEY_LENGTH = 32
IV_LENGTH = 16
SEGMENT_SIZE = 128
def setup(self):
from Crypto.Cipher import AES
self.cipher = AES.new(self.key, AES.MODE_CFB, iv=self.iv, segment_size=self.SEGMENT_SIZE)
class AES_128_CFB_Cipher(AES_256_CFB_Cipher):
KEY_LENGTH = 16
class AES_192_CFB_Cipher(AES_256_CFB_Cipher):
KEY_LENGTH = 24
class AES_256_CFB8_Cipher(AES_256_CFB_Cipher):
SEGMENT_SIZE = 8
class AES_192_CFB8_Cipher(AES_256_CFB8_Cipher):
KEY_LENGTH = 24
class AES_128_CFB8_Cipher(AES_256_CFB8_Cipher):
import hashlib, struct, base64
from .cipher import BaseCipher, AEADCipher
# Pure Python Ciphers
class Table_Cipher(BaseCipher):
PYTHON = True
KEY_LENGTH = 0
IV_LENGTH = 0
def setup(self):
if self.key in self.CACHE:
self.encrypt_table, self.decrypt_table = self.CACHE[self.key]
else:
a, _ = struct.unpack('
if self.key in self.CACHE:
self.encrypt_table, self.decrypt_table = self.CACHE[self.key]
else:
a, _ = struct.unpack('
class BF_CFB_Cipher(BaseCipher):
KEY_LENGTH = 16
IV_LENGTH = 8
def setup(self):
from Crypto.Cipher import Blowfish
self.cipher = Blowfish.new(self.key, Blowfish.MODE_CFB, iv=self.iv, segment_size=64)
class CAST5_CFB_Cipher(BaseCipher):
KEY_LENGTH = 16
IV_LENGTH = 8
def setup(self):
from Crypto.Cipher import CAST
self.cipher = CAST.new(self.key, CAST.MODE_CFB, iv=self.iv, segment_size=64)
class DES_CFB_Cipher(BaseCipher):
KEY_LENGTH = 8
IV_LENGTH = 8
def setup(self):
from Crypto.Cipher import DES
self.cipher = DES.new(self.key, DES.MODE_CFB, iv=self.iv, segment_size=64)
class PacketCipher:
def __init__(self, cipher, key, name):
self.cipher = lambda iv=None: cipher(key).setup_iv(iv)
self.ivlen = cipher.IV_LENGTH
self.name = name
def decrypt(self, data):
return self.cipher(data[:self.ivlen]).decrypt(data[self.ivlen:])
def encrypt(self, data):
cipher = self.cipher()
return cipher.iv+cipher.encrypt(data)
KEY_LENGTH = 24
class AES_128_CFB8_Cipher(AES_256_CFB8_Cipher):
KEY_LENGTH = 16
class AES_256_OFB_Cipher(BaseCipher):
KEY_LENGTH = 32
IV_LENGTH = 16
def setup(self):
from Crypto.Cipher import AES
self.cipher = AES.new(self.key, AES.MODE_OFB, iv=self.iv)
class AES_192_OFB_Cipher(AES_256_OFB_Cipher):
KEY_LENGTH = 24
class AES_128_OFB_Cipher(AES_256_OFB_Cipher):
KEY_LENGTH = 16
class AES_256_CTR_Cipher(BaseCipher):
KEY_LENGTH = 32
IV_LENGTH = 16
def setup(self):
from Crypto.Cipher import AES
self.cipher = AES.new(self.key, AES.MODE_CTR, nonce=b'', initial_value=self.iv)
class AES_192_CTR_Cipher(AES_256_CTR_Cipher):
KEY_LENGTH = 24
class AES_128_CTR_Cipher(AES_256_CTR_Cipher):
KEY_LENGTH = 16
class AES_256_GCM_Cipher(AEADCipher):
KEY_LENGTH = 32
IV_LENGTH = 32
NONCE_LENGTH = 12
TAG_LENGTH = 16
def decrypt_and_verify(self, buffer, tag):
ret.extend(self.decrypt_and_verify(self._buffer[:self._declen], self._buffer[self._declen:self._declen+self.TAG_LENGTH]))
del self._buffer[:self._declen+self.TAG_LENGTH]
self._declen = None
except Exception:
return bytes([0])
return bytes(ret)
def encrypt(self, s):
ret = bytearray()
for i in range(0, len(s), self.PACKET_LIMIT):
buf = s[i:i+self.PACKET_LIMIT]
len_chunk, len_tag = self.encrypt_and_digest(len(buf).to_bytes(2, 'big'))
body_chunk, body_tag = self.encrypt_and_digest(buf)
ret.extend(len_chunk+len_tag+body_chunk+body_tag)
return bytes(ret)
class RC4_Cipher(BaseCipher):
KEY_LENGTH = 16
IV_LENGTH = 0
def setup(self):
from Crypto.Cipher import ARC4
self.cipher = ARC4.new(self.key)
class RC4_MD5_Cipher(RC4_Cipher):
IV_LENGTH = 16
def setup(self):
self.key = hashlib.md5(self.key + self.iv).digest()
RC4_Cipher.setup(self)
class ChaCha20_Cipher(BaseCipher):
KEY_LENGTH = 32
IV_LENGTH = 8
def setup(self):
return bytes(ret)
class RC4_Cipher(BaseCipher):
KEY_LENGTH = 16
IV_LENGTH = 0
def setup(self):
from Crypto.Cipher import ARC4
self.cipher = ARC4.new(self.key)
class RC4_MD5_Cipher(RC4_Cipher):
IV_LENGTH = 16
def setup(self):
self.key = hashlib.md5(self.key + self.iv).digest()
RC4_Cipher.setup(self)
class ChaCha20_Cipher(BaseCipher):
KEY_LENGTH = 32
IV_LENGTH = 8
def setup(self):
from Crypto.Cipher import ChaCha20
self.cipher = ChaCha20.new(key=self.key, nonce=self.iv)
class ChaCha20_IETF_Cipher(ChaCha20_Cipher):
IV_LENGTH = 12
class Salsa20_Cipher(BaseCipher):
KEY_LENGTH = 32
IV_LENGTH = 8
def setup(self):
from Crypto.Cipher import Salsa20
self.cipher = Salsa20.new(key=self.key, nonce=self.iv)
class AES_256_CFB_Cipher(BaseCipher):