diff options
author | Donald Stufft <donald@stufft.io> | 2013-11-17 10:52:33 -0800 |
---|---|---|
committer | Donald Stufft <donald@stufft.io> | 2013-11-17 10:52:33 -0800 |
commit | 4a28d323b1227860da118a933d5f4940acaef999 (patch) | |
tree | e354636fdb891044548a3bb7042a253bfa97a616 /cryptography | |
parent | 83e0e1f00224c1f995298226c2e0837d555af67c (diff) | |
parent | 0a394df31c4165d0230843ebea2717b3cd3caafa (diff) | |
download | cryptography-4a28d323b1227860da118a933d5f4940acaef999.tar.gz cryptography-4a28d323b1227860da118a933d5f4940acaef999.tar.bz2 cryptography-4a28d323b1227860da118a933d5f4940acaef999.zip |
Merge pull request #262 from dreid/cipher-algorithm-interfaces
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): |