1111from __future__ import annotations
1212import hmac
1313import hashlib
14- from cryptography .hazmat .backends import default_backend
1514from cryptography .hazmat .primitives .ciphers import Cipher
1615from cryptography .hazmat .primitives .ciphers .algorithms import AES
1716from cryptography .hazmat .primitives .ciphers .modes import GCM , CBC
@@ -54,7 +53,7 @@ def encrypt(self, plaintext: bytes, cek: bytes, iv: bytes, aad: bytes) -> tuple[
5453 pad = PKCS7 (AES .block_size ).padder ()
5554 padded_data = pad .update (plaintext ) + pad .finalize ()
5655
57- cipher = Cipher (AES (ekey ), CBC (iv ), backend = default_backend () )
56+ cipher = Cipher (AES (ekey ), CBC (iv ))
5857 enc = cipher .encryptor ()
5958 ciphertext = enc .update (padded_data ) + enc .finalize ()
6059 tag = self ._hmac (ciphertext , aad , iv , hkey )
@@ -69,7 +68,7 @@ def decrypt(self, ciphertext: bytes, tag: bytes, cek: bytes, iv: bytes, aad: byt
6968 if not hmac .compare_digest (ctag , tag ):
7069 raise DecodeError ("tag does not match" )
7170
72- cipher = Cipher (AES (dkey ), CBC (iv ), backend = default_backend () )
71+ cipher = Cipher (AES (dkey ), CBC (iv ))
7372 d = cipher .decryptor ()
7473 data = d .update (ciphertext ) + d .finalize ()
7574 unpad = PKCS7 (AES .block_size ).unpadder ()
@@ -90,15 +89,15 @@ def __init__(self, key_size: int):
9089
9190 def encrypt (self , plaintext : bytes , cek : bytes , iv : bytes , aad : bytes ) -> tuple [bytes , bytes ]:
9291 """Key Encryption with AES GCM"""
93- cipher = Cipher (AES (cek ), GCM (iv ), backend = default_backend () )
92+ cipher = Cipher (AES (cek ), GCM (iv ))
9493 enc = cipher .encryptor ()
9594 enc .authenticate_additional_data (aad )
9695 ciphertext = enc .update (plaintext ) + enc .finalize ()
9796 return ciphertext , enc .tag
9897
9998 def decrypt (self , ciphertext : bytes , tag : bytes , cek : bytes , iv : bytes , aad : bytes ) -> bytes :
10099 """Key Decryption with AES GCM"""
101- cipher = Cipher (AES (cek ), GCM (iv , tag ), backend = default_backend () )
100+ cipher = Cipher (AES (cek ), GCM (iv , tag ))
102101 d = cipher .decryptor ()
103102 d .authenticate_additional_data (aad )
104103 try :
0 commit comments