|
- from Crypto.Cipher import AES as ccAES
- from cryptography.exceptions import InvalidTag
-
- # https://www.pycryptodome.org/src/cipher/modern#gcm-mode
-
- class CipherEncryptor:
- def __init__(self, encor):
- self._encor = encor
-
- self.authenticate_additional_data = encor.update
- self.update = encor.encrypt
-
- @property
- def tag(self):
- return self._encor.digest()
-
- def finalize(self):
- return b''
-
- class CipherDecryptor:
- def __init__(self, decor, tag=None):
- self._decor = decor
- self._tag = tag
-
- self.authenticate_additional_data = decor.update
- self.update = decor.decrypt
-
- def finalize(self):
- try:
- #print(repr(self._decor))
- self._decor.verify(self._tag)
- except ValueError:
- raise InvalidTag('tag mismatch')
-
- return b''
-
- class Cipher:
- def __init__(self, algo, mode, backend=None):
- self._algo = algo
- self._mode = mode
-
- def _getmode(self):
- if isinstance(self._mode, GCM):
- return ccAES.MODE_GCM
-
- def _nonce(self):
- return self._mode._iv
-
- def encryptor(self):
- return CipherEncryptor(ccAES.new(self._algo._key,
- self._getmode(), nonce=self._nonce()))
-
- def decryptor(self):
- return CipherDecryptor(ccAES.new(self._algo._key,
- self._getmode(), nonce=self._nonce()), tag=self._mode._tag)
-
- class AES:
- def __init__(self, key):
- self._key = key
-
- class algorithms:
- AES = AES
-
- class GCM:
- def __init__(self, iv, tag=None):
- self._iv = iv
- self._tag = tag
-
- class modes:
- GCM = GCM
|