From f56c54e36ce06e8e29691c3c4dfd000e8477a41d Mon Sep 17 00:00:00 2001 From: Ayrx Date: Sun, 16 Mar 2014 14:36:17 +0800 Subject: Added backend check to cipher primitives --- cryptography/hazmat/primitives/ciphers/base.py | 8 +++++++- docs/hazmat/primitives/symmetric-encryption.rst | 4 ++++ tests/hazmat/primitives/test_ciphers.py | 11 +++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/cryptography/hazmat/primitives/ciphers/base.py b/cryptography/hazmat/primitives/ciphers/base.py index d366e4cf..2c804cac 100644 --- a/cryptography/hazmat/primitives/ciphers/base.py +++ b/cryptography/hazmat/primitives/ciphers/base.py @@ -16,12 +16,18 @@ from __future__ import absolute_import, division, print_function from cryptography import utils from cryptography.exceptions import ( AlreadyFinalized, NotYetFinalized, AlreadyUpdated, -) + UnsupportedInterface) + +from cryptography.hazmat.backends.interfaces import CipherBackend from cryptography.hazmat.primitives import interfaces class Cipher(object): def __init__(self, algorithm, mode, backend): + if not isinstance(backend, CipherBackend): + raise UnsupportedInterface( + "Backend object does not implement CipherBackend") + if not isinstance(algorithm, interfaces.CipherAlgorithm): raise TypeError("Expected interface of interfaces.CipherAlgorithm") diff --git a/docs/hazmat/primitives/symmetric-encryption.rst b/docs/hazmat/primitives/symmetric-encryption.rst index 741091b2..71a1064e 100644 --- a/docs/hazmat/primitives/symmetric-encryption.rst +++ b/docs/hazmat/primitives/symmetric-encryption.rst @@ -56,6 +56,10 @@ an "encrypt-then-MAC" formulation as `described by Colin Percival`_. :class:`~cryptography.hazmat.backends.interfaces.CipherBackend` provider. + :raises cryptography.exceptions.UnsupportedInterface: This is raised if the + provided ``backend`` does not implement + :class:`~cryptography.hazmat.backends.interfaces.CipherBackend` + .. method:: encryptor() :return: An encrypting diff --git a/tests/hazmat/primitives/test_ciphers.py b/tests/hazmat/primitives/test_ciphers.py index d9f83535..dfed63a8 100644 --- a/tests/hazmat/primitives/test_ciphers.py +++ b/tests/hazmat/primitives/test_ciphers.py @@ -14,9 +14,13 @@ from __future__ import absolute_import, division, print_function import binascii +from cryptography.hazmat.primitives.ciphers.modes import ECB import pytest +from cryptography.exceptions import UnsupportedInterface + +from cryptography.hazmat.primitives import ciphers from cryptography.hazmat.primitives.ciphers.algorithms import ( AES, Camellia, TripleDES, Blowfish, ARC4, CAST5, IDEA ) @@ -120,3 +124,10 @@ class TestIDEA(object): def test_invalid_key_size(self): with pytest.raises(ValueError): IDEA(b"\x00" * 17) + + +def test_invalid_backend(): + pretend_backend = object() + + with pytest.raises(UnsupportedInterface): + ciphers.Cipher(AES(b"AAAAAAAAAAAAAAAA"), ECB, pretend_backend) -- cgit v1.2.3 From 471c1184eb8a7c24111abdc0bcb418c6c6839757 Mon Sep 17 00:00:00 2001 From: Ayrx Date: Sun, 16 Mar 2014 19:53:20 +0800 Subject: Style nitpick --- tests/hazmat/primitives/test_ciphers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/hazmat/primitives/test_ciphers.py b/tests/hazmat/primitives/test_ciphers.py index dfed63a8..e1f5dbc3 100644 --- a/tests/hazmat/primitives/test_ciphers.py +++ b/tests/hazmat/primitives/test_ciphers.py @@ -14,7 +14,6 @@ from __future__ import absolute_import, division, print_function import binascii -from cryptography.hazmat.primitives.ciphers.modes import ECB import pytest @@ -25,6 +24,8 @@ from cryptography.hazmat.primitives.ciphers.algorithms import ( AES, Camellia, TripleDES, Blowfish, ARC4, CAST5, IDEA ) +from cryptography.hazmat.primitives.ciphers.modes import ECB + class TestAES(object): @pytest.mark.parametrize(("key", "keysize"), [ -- cgit v1.2.3