From c024255dda87decd65bd05bd767feda0b43f80c7 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Tue, 10 Sep 2013 18:54:13 -0500 Subject: move abc, inline introspect method, use six for abcs * abc moved to cryptography.primitive.interfaces * six added to dependencies * six used to have py2x/py3x compatible abc * nonce abc removed for now --- cryptography/bindings/openssl/api.py | 18 ++++++------------ cryptography/primitives/abc/__init__.py | 0 cryptography/primitives/abc/block/__init__.py | 0 cryptography/primitives/abc/block/modes.py | 21 --------------------- cryptography/primitives/block/modes.py | 4 ++-- cryptography/primitives/interfaces.py | 21 +++++++++++++++++++++ setup.py | 2 ++ tox.ini | 1 + 8 files changed, 32 insertions(+), 35 deletions(-) delete mode 100644 cryptography/primitives/abc/__init__.py delete mode 100644 cryptography/primitives/abc/block/__init__.py delete mode 100644 cryptography/primitives/abc/block/modes.py create mode 100644 cryptography/primitives/interfaces.py diff --git a/cryptography/bindings/openssl/api.py b/cryptography/bindings/openssl/api.py index f95e4d62..917c1846 100644 --- a/cryptography/bindings/openssl/api.py +++ b/cryptography/bindings/openssl/api.py @@ -13,7 +13,7 @@ from __future__ import absolute_import, division, print_function -from cryptography.primitives.abc.block import modes +from cryptography.primitives import interfaces import cffi @@ -74,12 +74,14 @@ class API(object): ) evp_cipher = self._lib.EVP_get_cipherbyname(ciphername.encode("ascii")) assert evp_cipher != self._ffi.NULL - iv_nonce = self._introspect(mode) + if isinstance(mode, interfaces.ModeWithInitializationVector): + iv_nonce = mode.initialization_vector + else: + iv_nonce = self._ffi.NULL # TODO: Sometimes this needs to be a DecryptInit, when? res = self._lib.EVP_EncryptInit_ex( - ctx, evp_cipher, self._ffi.NULL, cipher.key, - iv_nonce + ctx, evp_cipher, self._ffi.NULL, cipher.key, iv_nonce ) assert res != 0 @@ -88,14 +90,6 @@ class API(object): self._lib.EVP_CIPHER_CTX_set_padding(ctx, 0) return ctx - def _introspect(self, mode): - if isinstance(mode, modes.ModeWithInitializationVector): - return mode.initialization_vector - elif isinstance(mode, modes.ModeWithNonce): - return mode.nonce - else: - return self._ffi.NULL - def update_encrypt_context(self, ctx, plaintext): buf = self._ffi.new("unsigned char[]", len(plaintext)) outlen = self._ffi.new("int *") diff --git a/cryptography/primitives/abc/__init__.py b/cryptography/primitives/abc/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/cryptography/primitives/abc/block/__init__.py b/cryptography/primitives/abc/block/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/cryptography/primitives/abc/block/modes.py b/cryptography/primitives/abc/block/modes.py deleted file mode 100644 index 609a2ae3..00000000 --- a/cryptography/primitives/abc/block/modes.py +++ /dev/null @@ -1,21 +0,0 @@ -# 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. - -from __future__ import absolute_import, division, print_function - -import abc - - -ModeWithInitializationVector = abc.ABCMeta('ModeWithInitializationVector', - (object, ), {}) -ModeWithNonce = abc.ABCMeta('ModeWithNonce', (object, ), {}) diff --git a/cryptography/primitives/block/modes.py b/cryptography/primitives/block/modes.py index 1e9b14b7..c722e739 100644 --- a/cryptography/primitives/block/modes.py +++ b/cryptography/primitives/block/modes.py @@ -13,7 +13,7 @@ from __future__ import absolute_import, division, print_function -from cryptography.primitives.abc.block import modes +from cryptography.primitives import interfaces class CBC(object): @@ -28,4 +28,4 @@ class ECB(object): name = "ECB" -modes.ModeWithInitializationVector.register(CBC) +interfaces.ModeWithInitializationVector.register(CBC) diff --git a/cryptography/primitives/interfaces.py b/cryptography/primitives/interfaces.py new file mode 100644 index 00000000..a2b091df --- /dev/null +++ b/cryptography/primitives/interfaces.py @@ -0,0 +1,21 @@ +# 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. + +from __future__ import absolute_import, division, print_function + +import abc +import six + + +class ModeWithInitializationVector(six.with_metaclass(abc.ABCMeta)): + pass diff --git a/setup.py b/setup.py index 1044c979..cbbf100a 100644 --- a/setup.py +++ b/setup.py @@ -21,9 +21,11 @@ with open("cryptography/__about__.py") as fp: CFFI_DEPENDENCY = "cffi>=0.6" +SIX_DEPENDENCY = "six>=1.4.1" install_requires = [ CFFI_DEPENDENCY, + SIX_DEPENDENCY ] setup_requires = [ diff --git a/tox.ini b/tox.ini index 4d17ebe8..02d521b1 100644 --- a/tox.ini +++ b/tox.ini @@ -5,6 +5,7 @@ envlist = py26,py27,pypy,py32,py33,docs,pep8 deps = pytest-cov pretend + six commands = py.test --cov=cryptography/ --cov=tests/ [testenv:docs] -- cgit v1.2.3