diff options
-rw-r--r-- | cryptography/hazmat/backends/commoncrypto/hashes.py | 4 | ||||
-rw-r--r-- | cryptography/hazmat/backends/commoncrypto/hmac.py | 4 | ||||
-rw-r--r-- | cryptography/hazmat/backends/openssl/cmac.py | 15 | ||||
-rw-r--r-- | cryptography/hazmat/backends/openssl/hashes.py | 4 | ||||
-rw-r--r-- | cryptography/hazmat/backends/openssl/hmac.py | 4 | ||||
-rw-r--r-- | cryptography/hazmat/primitives/ciphers/modes.py | 29 | ||||
-rw-r--r-- | cryptography/hazmat/primitives/cmac.py | 10 | ||||
-rw-r--r-- | cryptography/hazmat/primitives/hashes.py | 4 | ||||
-rw-r--r-- | cryptography/hazmat/primitives/hmac.py | 4 | ||||
-rw-r--r-- | cryptography/utils.py | 27 | ||||
-rw-r--r-- | tests/hazmat/backends/test_commoncrypto.py | 1 | ||||
-rw-r--r-- | tests/hazmat/backends/test_openssl.py | 3 | ||||
-rw-r--r-- | tests/hazmat/primitives/test_block.py | 1 | ||||
-rw-r--r-- | tests/hazmat/primitives/test_ec.py | 11 | ||||
-rw-r--r-- | tests/hazmat/primitives/test_hashes.py | 2 | ||||
-rw-r--r-- | tests/hazmat/primitives/test_hmac.py | 2 | ||||
-rw-r--r-- | tests/hazmat/primitives/test_pbkdf2hmac.py | 2 | ||||
-rw-r--r-- | tests/test_interfaces.py | 68 |
18 files changed, 169 insertions, 26 deletions
diff --git a/cryptography/hazmat/backends/commoncrypto/hashes.py b/cryptography/hazmat/backends/commoncrypto/hashes.py index ebad7201..217f4e8c 100644 --- a/cryptography/hazmat/backends/commoncrypto/hashes.py +++ b/cryptography/hazmat/backends/commoncrypto/hashes.py @@ -21,7 +21,7 @@ from cryptography.hazmat.primitives import interfaces @utils.register_interface(interfaces.HashContext) class _HashContext(object): def __init__(self, backend, algorithm, ctx=None): - self.algorithm = algorithm + self._algorithm = algorithm self._backend = backend if ctx is None: @@ -39,6 +39,8 @@ class _HashContext(object): self._ctx = ctx + algorithm = utils.read_only_property("_algorithm") + def copy(self): methods = self._backend._hash_mapping[self.algorithm.name] new_ctx = self._backend._ffi.new(methods.ctx) diff --git a/cryptography/hazmat/backends/commoncrypto/hmac.py b/cryptography/hazmat/backends/commoncrypto/hmac.py index c6e3f276..c2b6c379 100644 --- a/cryptography/hazmat/backends/commoncrypto/hmac.py +++ b/cryptography/hazmat/backends/commoncrypto/hmac.py @@ -22,7 +22,7 @@ from cryptography.hazmat.primitives import interfaces @utils.register_interface(interfaces.HashContext) class _HMACContext(object): def __init__(self, backend, key, algorithm, ctx=None): - self.algorithm = algorithm + self._algorithm = algorithm self._backend = backend if ctx is None: ctx = self._backend._ffi.new("CCHmacContext *") @@ -40,6 +40,8 @@ class _HMACContext(object): self._ctx = ctx self._key = key + algorithm = utils.read_only_property("_algorithm") + def copy(self): copied_ctx = self._backend._ffi.new("CCHmacContext *") # CommonCrypto has no APIs for copying HMACs, so we have to copy the diff --git a/cryptography/hazmat/backends/openssl/cmac.py b/cryptography/hazmat/backends/openssl/cmac.py index da7b7484..113188ca 100644 --- a/cryptography/hazmat/backends/openssl/cmac.py +++ b/cryptography/hazmat/backends/openssl/cmac.py @@ -15,8 +15,10 @@ from __future__ import absolute_import, division, print_function from cryptography import utils -from cryptography.exceptions import UnsupportedAlgorithm, _Reasons -from cryptography.hazmat.primitives import interfaces +from cryptography.exceptions import ( + InvalidSignature, UnsupportedAlgorithm, _Reasons +) +from cryptography.hazmat.primitives import constant_time, interfaces from cryptography.hazmat.primitives.ciphers.modes import CBC @@ -50,6 +52,8 @@ class _CMACContext(object): self._ctx = ctx + algorithm = utils.read_only_property("_algorithm") + def update(self, data): res = self._backend._lib.CMAC_Update(self._ctx, data, len(data)) assert res == 1 @@ -78,3 +82,10 @@ class _CMACContext(object): return _CMACContext( self._backend, self._algorithm, ctx=copied_ctx ) + + def verify(self, signature): + if not isinstance(signature, bytes): + raise TypeError("signature must be bytes.") + digest = self.finalize() + if not constant_time.bytes_eq(digest, signature): + raise InvalidSignature("Signature did not match digest.") diff --git a/cryptography/hazmat/backends/openssl/hashes.py b/cryptography/hazmat/backends/openssl/hashes.py index da91eef6..591c014a 100644 --- a/cryptography/hazmat/backends/openssl/hashes.py +++ b/cryptography/hazmat/backends/openssl/hashes.py @@ -22,7 +22,7 @@ from cryptography.hazmat.primitives import interfaces @utils.register_interface(interfaces.HashContext) class _HashContext(object): def __init__(self, backend, algorithm, ctx=None): - self.algorithm = algorithm + self._algorithm = algorithm self._backend = backend @@ -44,6 +44,8 @@ class _HashContext(object): self._ctx = ctx + algorithm = utils.read_only_property("_algorithm") + def copy(self): copied_ctx = self._backend._lib.EVP_MD_CTX_create() copied_ctx = self._backend._ffi.gc( diff --git a/cryptography/hazmat/backends/openssl/hmac.py b/cryptography/hazmat/backends/openssl/hmac.py index ca62184b..d5300ea0 100644 --- a/cryptography/hazmat/backends/openssl/hmac.py +++ b/cryptography/hazmat/backends/openssl/hmac.py @@ -23,7 +23,7 @@ from cryptography.hazmat.primitives import interfaces @utils.register_interface(interfaces.HashContext) class _HMACContext(object): def __init__(self, backend, key, algorithm, ctx=None): - self.algorithm = algorithm + self._algorithm = algorithm self._backend = backend if ctx is None: @@ -48,6 +48,8 @@ class _HMACContext(object): self._ctx = ctx self._key = key + algorithm = utils.read_only_property("_algorithm") + def copy(self): copied_ctx = self._backend._ffi.new("HMAC_CTX *") self._backend._lib.HMAC_CTX_init(copied_ctx) diff --git a/cryptography/hazmat/primitives/ciphers/modes.py b/cryptography/hazmat/primitives/ciphers/modes.py index 509b4de2..d995b876 100644 --- a/cryptography/hazmat/primitives/ciphers/modes.py +++ b/cryptography/hazmat/primitives/ciphers/modes.py @@ -17,10 +17,10 @@ from cryptography import utils from cryptography.hazmat.primitives import interfaces -def _check_iv_length(mode, algorithm): - if len(mode.initialization_vector) * 8 != algorithm.block_size: +def _check_iv_length(self, algorithm): + if len(self.initialization_vector) * 8 != algorithm.block_size: raise ValueError("Invalid IV size ({0}) for {1}.".format( - len(mode.initialization_vector), mode.name + len(self.initialization_vector), self.name )) @@ -30,8 +30,9 @@ class CBC(object): name = "CBC" def __init__(self, initialization_vector): - self.initialization_vector = initialization_vector + self._initialization_vector = initialization_vector + initialization_vector = utils.read_only_property("_initialization_vector") validate_for_algorithm = _check_iv_length @@ -49,8 +50,9 @@ class OFB(object): name = "OFB" def __init__(self, initialization_vector): - self.initialization_vector = initialization_vector + self._initialization_vector = initialization_vector + initialization_vector = utils.read_only_property("_initialization_vector") validate_for_algorithm = _check_iv_length @@ -60,8 +62,9 @@ class CFB(object): name = "CFB" def __init__(self, initialization_vector): - self.initialization_vector = initialization_vector + self._initialization_vector = initialization_vector + initialization_vector = utils.read_only_property("_initialization_vector") validate_for_algorithm = _check_iv_length @@ -71,8 +74,9 @@ class CFB8(object): name = "CFB8" def __init__(self, initialization_vector): - self.initialization_vector = initialization_vector + self._initialization_vector = initialization_vector + initialization_vector = utils.read_only_property("_initialization_vector") validate_for_algorithm = _check_iv_length @@ -82,7 +86,9 @@ class CTR(object): name = "CTR" def __init__(self, nonce): - self.nonce = nonce + self._nonce = nonce + + nonce = utils.read_only_property("_nonce") def validate_for_algorithm(self, algorithm): if len(self.nonce) * 8 != algorithm.block_size: @@ -109,8 +115,11 @@ class GCM(object): min_tag_length) ) - self.initialization_vector = initialization_vector - self.tag = tag + self._initialization_vector = initialization_vector + self._tag = tag + + tag = utils.read_only_property("_tag") + initialization_vector = utils.read_only_property("_initialization_vector") def validate_for_algorithm(self, algorithm): pass diff --git a/cryptography/hazmat/primitives/cmac.py b/cryptography/hazmat/primitives/cmac.py index 7ae5c118..a70a9a42 100644 --- a/cryptography/hazmat/primitives/cmac.py +++ b/cryptography/hazmat/primitives/cmac.py @@ -15,10 +15,10 @@ from __future__ import absolute_import, division, print_function from cryptography import utils from cryptography.exceptions import ( - AlreadyFinalized, InvalidSignature, UnsupportedAlgorithm, _Reasons + AlreadyFinalized, UnsupportedAlgorithm, _Reasons ) from cryptography.hazmat.backends.interfaces import CMACBackend -from cryptography.hazmat.primitives import constant_time, interfaces +from cryptography.hazmat.primitives import interfaces @utils.register_interface(interfaces.MACContext) @@ -57,11 +57,7 @@ class CMAC(object): return digest def verify(self, signature): - if not isinstance(signature, bytes): - raise TypeError("signature must be bytes.") - digest = self.finalize() - if not constant_time.bytes_eq(digest, signature): - raise InvalidSignature("Signature did not match digest.") + self._ctx.verify(signature) def copy(self): if self._ctx is None: diff --git a/cryptography/hazmat/primitives/hashes.py b/cryptography/hazmat/primitives/hashes.py index 04f7620a..8c2284e3 100644 --- a/cryptography/hazmat/primitives/hashes.py +++ b/cryptography/hazmat/primitives/hashes.py @@ -32,7 +32,7 @@ class Hash(object): if not isinstance(algorithm, interfaces.HashAlgorithm): raise TypeError("Expected instance of interfaces.HashAlgorithm.") - self.algorithm = algorithm + self._algorithm = algorithm self._backend = backend @@ -41,6 +41,8 @@ class Hash(object): else: self._ctx = ctx + algorithm = utils.read_only_property("_algorithm") + def update(self, data): if self._ctx is None: raise AlreadyFinalized("Context was already finalized.") diff --git a/cryptography/hazmat/primitives/hmac.py b/cryptography/hazmat/primitives/hmac.py index b85fb2aa..22a31391 100644 --- a/cryptography/hazmat/primitives/hmac.py +++ b/cryptography/hazmat/primitives/hmac.py @@ -33,7 +33,7 @@ class HMAC(object): if not isinstance(algorithm, interfaces.HashAlgorithm): raise TypeError("Expected instance of interfaces.HashAlgorithm.") - self.algorithm = algorithm + self._algorithm = algorithm self._backend = backend self._key = key @@ -42,6 +42,8 @@ class HMAC(object): else: self._ctx = ctx + algorithm = utils.read_only_property("_algorithm") + def update(self, data): if self._ctx is None: raise AlreadyFinalized("Context was already finalized.") diff --git a/cryptography/utils.py b/cryptography/utils.py index 1deb3d1d..03c8c0e8 100644 --- a/cryptography/utils.py +++ b/cryptography/utils.py @@ -13,6 +13,8 @@ from __future__ import absolute_import, division, print_function +import abc +import inspect import sys @@ -21,6 +23,7 @@ DeprecatedIn06 = DeprecationWarning def register_interface(iface): def register_decorator(klass): + verify_interface(iface, klass) iface.register(klass) return klass return register_decorator @@ -30,6 +33,30 @@ def read_only_property(name): return property(lambda self: getattr(self, name)) +class InterfaceNotImplemented(Exception): + pass + + +def verify_interface(iface, klass): + for method in iface.__abstractmethods__: + if not hasattr(klass, method): + raise InterfaceNotImplemented( + "{0} is missing a {1!r} method".format(klass, method) + ) + if isinstance(getattr(iface, method), abc.abstractproperty): + # Can't properly verify these yet. + continue + spec = inspect.getargspec(getattr(iface, method)) + actual = inspect.getargspec(getattr(klass, method)) + if spec != actual: + raise InterfaceNotImplemented( + "{0}.{1}'s signature differs from the expected. Expected: " + "{2!r}. Received: {3!r}".format( + klass, method, spec, actual + ) + ) + + def bit_length(x): if sys.version_info >= (2, 7): return x.bit_length() diff --git a/tests/hazmat/backends/test_commoncrypto.py b/tests/hazmat/backends/test_commoncrypto.py index 28d1a6ca..b79c02e0 100644 --- a/tests/hazmat/backends/test_commoncrypto.py +++ b/tests/hazmat/backends/test_commoncrypto.py @@ -30,6 +30,7 @@ from ...utils import raises_unsupported_algorithm class DummyCipher(object): name = "dummy-cipher" block_size = 128 + key_size = 128 @pytest.mark.skipif("commoncrypto" not in diff --git a/tests/hazmat/backends/test_openssl.py b/tests/hazmat/backends/test_openssl.py index 39708f82..65113f1a 100644 --- a/tests/hazmat/backends/test_openssl.py +++ b/tests/hazmat/backends/test_openssl.py @@ -52,6 +52,7 @@ class DummyMode(object): @utils.register_interface(interfaces.CipherAlgorithm) class DummyCipher(object): name = "dummy-cipher" + key_size = 128 @utils.register_interface(interfaces.AsymmetricPadding) @@ -62,6 +63,8 @@ class DummyPadding(object): @utils.register_interface(interfaces.HashAlgorithm) class DummyHash(object): name = "dummy-hash" + block_size = 128 + digest_size = 128 class DummyMGF(object): diff --git a/tests/hazmat/primitives/test_block.py b/tests/hazmat/primitives/test_block.py index 6ee230a7..53b87341 100644 --- a/tests/hazmat/primitives/test_block.py +++ b/tests/hazmat/primitives/test_block.py @@ -44,6 +44,7 @@ class DummyMode(object): @utils.register_interface(interfaces.CipherAlgorithm) class DummyCipher(object): name = "dummy-cipher" + key_size = 128 @pytest.mark.requires_backend_interface(interface=CipherBackend) diff --git a/tests/hazmat/primitives/test_ec.py b/tests/hazmat/primitives/test_ec.py index decb3716..683df046 100644 --- a/tests/hazmat/primitives/test_ec.py +++ b/tests/hazmat/primitives/test_ec.py @@ -68,11 +68,20 @@ class DummyCurve(object): @utils.register_interface(interfaces.EllipticCurveSignatureAlgorithm) class DummySignatureAlgorithm(object): - pass + algorithm = None @utils.register_interface(EllipticCurveBackend) class DeprecatedDummyECBackend(object): + def _unimplemented(self): + raise NotImplementedError + + elliptic_curve_signature_algorithm_supported = _unimplemented + load_elliptic_curve_private_numbers = _unimplemented + load_elliptic_curve_public_numbers = _unimplemented + elliptic_curve_supported = _unimplemented + generate_elliptic_curve_private_key = _unimplemented + def elliptic_curve_private_key_from_numbers(self, numbers): return b"private_key" diff --git a/tests/hazmat/primitives/test_hashes.py b/tests/hazmat/primitives/test_hashes.py index ba4f53af..79fc25c3 100644 --- a/tests/hazmat/primitives/test_hashes.py +++ b/tests/hazmat/primitives/test_hashes.py @@ -33,6 +33,8 @@ from ...utils import raises_unsupported_algorithm @utils.register_interface(interfaces.HashAlgorithm) class UnsupportedDummyHash(object): name = "unsupported-dummy-hash" + block_size = 128 + digest_size = 128 @pytest.mark.requires_backend_interface(interface=HashBackend) diff --git a/tests/hazmat/primitives/test_hmac.py b/tests/hazmat/primitives/test_hmac.py index baf8a299..01b1cdcb 100644 --- a/tests/hazmat/primitives/test_hmac.py +++ b/tests/hazmat/primitives/test_hmac.py @@ -33,6 +33,8 @@ from ...utils import raises_unsupported_algorithm @utils.register_interface(interfaces.HashAlgorithm) class UnsupportedDummyHash(object): name = "unsupported-dummy-hash" + block_size = 128 + digest_size = 128 @pytest.mark.supported( diff --git a/tests/hazmat/primitives/test_pbkdf2hmac.py b/tests/hazmat/primitives/test_pbkdf2hmac.py index e928fc6a..fa925877 100644 --- a/tests/hazmat/primitives/test_pbkdf2hmac.py +++ b/tests/hazmat/primitives/test_pbkdf2hmac.py @@ -31,6 +31,8 @@ from ...utils import raises_unsupported_algorithm @utils.register_interface(interfaces.HashAlgorithm) class DummyHash(object): name = "dummy-hash" + block_size = 128 + digest_size = 128 class TestPBKDF2HMAC(object): diff --git a/tests/test_interfaces.py b/tests/test_interfaces.py new file mode 100644 index 00000000..0c72ad33 --- /dev/null +++ b/tests/test_interfaces.py @@ -0,0 +1,68 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import abc + +import pytest + +import six + +from cryptography.utils import ( + InterfaceNotImplemented, register_interface, verify_interface +) + + +class TestVerifyInterface(object): + def test_verify_missing_method(self): + @six.add_metaclass(abc.ABCMeta) + class SimpleInterface(object): + @abc.abstractmethod + def method(self): + """A simple method""" + + @register_interface(SimpleInterface) + class NonImplementer(object): + pass + + with pytest.raises(InterfaceNotImplemented): + verify_interface(SimpleInterface, NonImplementer) + + def test_different_arguments(self): + @six.add_metaclass(abc.ABCMeta) + class SimpleInterface(object): + @abc.abstractmethod + def method(self, a): + """Method with one argument""" + + @register_interface(SimpleInterface) + class NonImplementer(object): + def method(self): + """Method with no arguments""" + + with pytest.raises(InterfaceNotImplemented): + verify_interface(SimpleInterface, NonImplementer) + + def test_handles_abstract_property(self): + @six.add_metaclass(abc.ABCMeta) + class SimpleInterface(object): + @abc.abstractproperty + def property(self): + """An abstract property""" + + @register_interface(SimpleInterface) + class NonImplementer(object): + @property + def property(self): + """A concrete property""" + + verify_interface(SimpleInterface, NonImplementer) |