From 3d979e2d6523a86ecc73e8296b83b25ed0d08c10 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Fri, 8 Jul 2022 00:22:30 +0200 Subject: [PATCH] Added Android compatibility to AES proxy class --- RNS/Cryptography/AES.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/RNS/Cryptography/AES.py b/RNS/Cryptography/AES.py index 931fd7c..caca7d6 100644 --- a/RNS/Cryptography/AES.py +++ b/RNS/Cryptography/AES.py @@ -21,6 +21,7 @@ # SOFTWARE. import RNS.Cryptography.Provider as cp +import RNS.vendor.platformutils as pu if cp.PROVIDER == cp.PROVIDER_INTERNAL: from .aes import AES @@ -28,6 +29,9 @@ if cp.PROVIDER == cp.PROVIDER_INTERNAL: elif cp.PROVIDER == cp.PROVIDER_PYCA: from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes + if pu.cryptography_old_api(): + from cryptography.hazmat.backends import default_backend + class AES_128_CBC: @@ -38,7 +42,11 @@ class AES_128_CBC: return cipher.encrypt(plaintext, iv) elif cp.PROVIDER == cp.PROVIDER_PYCA: - cipher = Cipher(algorithms.AES(key), modes.CBC(iv)) + if not pu.cryptography_old_api(): + cipher = Cipher(algorithms.AES(key), modes.CBC(iv)) + else: + cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend()) + encryptor = cipher.encryptor() ciphertext = encryptor.update(plaintext) + encryptor.finalize() return ciphertext @@ -50,7 +58,11 @@ class AES_128_CBC: return cipher.decrypt(ciphertext, iv) elif cp.PROVIDER == cp.PROVIDER_PYCA: - cipher = Cipher(algorithms.AES(key), modes.CBC(iv)) + if not pu.cryptography_old_api(): + cipher = Cipher(algorithms.AES(key), modes.CBC(iv)) + else: + cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend()) + decryptor = cipher.decryptor() plaintext = decryptor.update(ciphertext) + decryptor.finalize() return plaintext