Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
while 1:
yield from struct.pack('<16I', *(a+b&0xffffffff for a, b in zip(ChaCha20_round(data[:]), data)))
data[12:14] = (0, data[13]+1) if data[12]==0xffffffff else (data[12]+1, data[13])
class XChaCha20_IETF_Cipher(XChaCha20_Cipher):
IV_LENGTH = 16+12
def poly1305(cipher_encrypt, nonce, ciphertext):
otk = cipher_encrypt(nonce, bytes(32))
mac_data = ciphertext + bytes((-len(ciphertext))%16 + 8) + len(ciphertext).to_bytes(8, 'little')
acc, r, s = 0, int.from_bytes(otk[:16], 'little') & 0x0ffffffc0ffffffc0ffffffc0fffffff, int.from_bytes(otk[16:], 'little')
for i in range(0, len(mac_data), 16):
acc = (r * (acc+int.from_bytes(mac_data[i:i+16]+b'\x01', 'little'))) % ((1<<130)-5)
return ((acc + s) & ((1<<128)-1)).to_bytes(16, 'little')
class ChaCha20_IETF_POLY1305_Cipher(AEADCipher):
PYTHON = True
KEY_LENGTH = 32
IV_LENGTH = 32
NONCE_LENGTH = 12
TAG_LENGTH = 16
def process(self, s, tag=None):
nonce = self.nonce
if tag is not None:
assert tag == poly1305(self.cipher_encrypt, nonce, s)
data = self.cipher_encrypt(nonce, s, counter=1)
if tag is None:
return data, poly1305(self.cipher_encrypt, nonce, data)
else:
return data
encrypt_and_digest = decrypt_and_verify = process
def setup(self):
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 AES
self.cipher_new = lambda nonce: AES.new(self.key, AES.MODE_GCM, nonce=nonce, mac_len=self.TAG_LENGTH)
class AES_192_GCM_Cipher(AES_256_GCM_Cipher):
KEY_LENGTH = IV_LENGTH = 24
class AES_128_GCM_Cipher(AES_256_GCM_Cipher):
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):
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):
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 AES
self.cipher_new = lambda nonce: AES.new(self.key, AES.MODE_GCM, nonce=nonce, mac_len=self.TAG_LENGTH)
class AES_192_GCM_Cipher(AES_256_GCM_Cipher):
KEY_LENGTH = IV_LENGTH = 24
class AES_128_GCM_Cipher(AES_256_GCM_Cipher):
KEY_LENGTH = IV_LENGTH = 16
self.stream = self.core()
self.cipher = self.CIPHER.new(self.key)
def core(self):
next_iv = int.from_bytes(self.iv, 'big')
while 1:
yield from self.cipher.encrypt(next_iv)
next_iv = 0 if next_iv >= (1<<(self.IV_LENGTH*8))-1 else next_iv+1
class OFBCipher(CTRCipher):
def core(self):
data = self.iv
while 1:
data = self.cipher.encrypt(data)
yield from data
class GCMCipher(AEADCipher):
PYTHON = True
NONCE_LENGTH = 12
TAG_LENGTH = 16
def setup(self):
self.cipher = self.CIPHER.new(self.key)
self.hkey = []
x = int.from_bytes(self.cipher.encrypt(0), 'big')
for i in range(128):
self.hkey.insert(0, x)
x = (x>>1)^(0xe1<<120) if x&1 else x>>1
def process(self, s, tag=None):
def multh(y):
z = 0
for i in range(128):
if y & (1<