diff options
author | David Reid <dreid@dreid.org> | 2013-11-15 16:19:50 -0800 |
---|---|---|
committer | David Reid <dreid@dreid.org> | 2013-11-15 16:19:50 -0800 |
commit | 0a394df31c4165d0230843ebea2717b3cd3caafa (patch) | |
tree | 5c674a900d0f7465cde2796b9bba21a87d67b56e /cryptography | |
parent | 9489c769dbd7ea7c6830b5fcd70095818452e607 (diff) | |
download | cryptography-0a394df31c4165d0230843ebea2717b3cd3caafa.tar.gz cryptography-0a394df31c4165d0230843ebea2717b3cd3caafa.tar.bz2 cryptography-0a394df31c4165d0230843ebea2717b3cd3caafa.zip |
Implement and document an interface for cipher algorithms
Diffstat (limited to 'cryptography')
-rw-r--r-- | cryptography/hazmat/primitives/ciphers/algorithms.py | 8 | ||||
-rw-r--r-- | cryptography/hazmat/primitives/ciphers/base.py | 3 | ||||
-rw-r--r-- | cryptography/hazmat/primitives/interfaces.py | 14 |
3 files changed, 25 insertions, 0 deletions
diff --git a/cryptography/hazmat/primitives/ciphers/algorithms.py b/cryptography/hazmat/primitives/ciphers/algorithms.py index c135f563..1ed487b6 100644 --- a/cryptography/hazmat/primitives/ciphers/algorithms.py +++ b/cryptography/hazmat/primitives/ciphers/algorithms.py @@ -13,7 +13,10 @@ from __future__ import absolute_import, division, print_function +from cryptography.hazmat.primitives import interfaces + +@interfaces.register(interfaces.CipherAlgorithm) class AES(object): name = "AES" block_size = 128 @@ -33,6 +36,7 @@ class AES(object): return len(self.key) * 8 +@interfaces.register(interfaces.CipherAlgorithm) class Camellia(object): name = "camellia" block_size = 128 @@ -52,6 +56,7 @@ class Camellia(object): return len(self.key) * 8 +@interfaces.register(interfaces.CipherAlgorithm) class TripleDES(object): name = "3DES" block_size = 64 @@ -75,6 +80,7 @@ class TripleDES(object): return len(self.key) * 8 +@interfaces.register(interfaces.CipherAlgorithm) class Blowfish(object): name = "Blowfish" block_size = 64 @@ -94,6 +100,7 @@ class Blowfish(object): return len(self.key) * 8 +@interfaces.register(interfaces.CipherAlgorithm) class CAST5(object): name = "CAST5" block_size = 64 @@ -113,6 +120,7 @@ class CAST5(object): return len(self.key) * 8 +@interfaces.register(interfaces.CipherAlgorithm) class ARC4(object): name = "RC4" block_size = 1 diff --git a/cryptography/hazmat/primitives/ciphers/base.py b/cryptography/hazmat/primitives/ciphers/base.py index 0fe11996..78bf7e0c 100644 --- a/cryptography/hazmat/primitives/ciphers/base.py +++ b/cryptography/hazmat/primitives/ciphers/base.py @@ -24,6 +24,9 @@ class Cipher(object): _default_backend as backend, ) + if not isinstance(algorithm, interfaces.CipherAlgorithm): + raise TypeError("Expected interface of interfaces.CipherAlgorithm") + self.algorithm = algorithm self.mode = mode self._backend = backend diff --git a/cryptography/hazmat/primitives/interfaces.py b/cryptography/hazmat/primitives/interfaces.py index 67dbe6fa..bbbb266c 100644 --- a/cryptography/hazmat/primitives/interfaces.py +++ b/cryptography/hazmat/primitives/interfaces.py @@ -25,6 +25,20 @@ def register(iface): return register_decorator +class CipherAlgorithm(six.with_metaclass(abc.ABCMeta)): + @abc.abstractproperty + def name(self): + """ + A string naming this mode. (e.g. AES, Camellia) + """ + + @abc.abstractproperty + def key_size(self): + """ + The size of the key being used as an integer in bits. (e.g. 128, 256) + """ + + class Mode(six.with_metaclass(abc.ABCMeta)): @abc.abstractproperty def name(self): |