diff options
author | Terry Chia <terrycwk1994@gmail.com> | 2014-10-11 22:35:58 +0800 |
---|---|---|
committer | Terry Chia <terrycwk1994@gmail.com> | 2014-10-20 22:58:48 +0800 |
commit | 0d7439d171c0692959de175aa1040d5bba2313a0 (patch) | |
tree | 7a4e94556c0d07f67fd45211e8c31304f5768407 | |
parent | a31e26f0dca915ccbb2e5e87a929b6bbc154c3c3 (diff) | |
download | cryptography-0d7439d171c0692959de175aa1040d5bba2313a0.tar.gz cryptography-0d7439d171c0692959de175aa1040d5bba2313a0.tar.bz2 cryptography-0d7439d171c0692959de175aa1040d5bba2313a0.zip |
Add MACContext and make HMAC and CMAC to use it.
-rw-r--r-- | cryptography/hazmat/backends/interfaces.py | 4 | ||||
-rw-r--r-- | cryptography/hazmat/backends/openssl/cmac.py | 1 | ||||
-rw-r--r-- | cryptography/hazmat/primitives/cmac.py | 1 | ||||
-rw-r--r-- | cryptography/hazmat/primitives/hmac.py | 1 | ||||
-rw-r--r-- | cryptography/hazmat/primitives/interfaces.py | 37 |
5 files changed, 41 insertions, 3 deletions
diff --git a/cryptography/hazmat/backends/interfaces.py b/cryptography/hazmat/backends/interfaces.py index 69d776ff..ecb5bf48 100644 --- a/cryptography/hazmat/backends/interfaces.py +++ b/cryptography/hazmat/backends/interfaces.py @@ -66,7 +66,7 @@ class HMACBackend(object): @abc.abstractmethod def create_hmac_ctx(self, key, algorithm): """ - Create a HashContext for calculating a message authentication code. + Create a MACContext for calculating a message authentication code. """ @@ -81,7 +81,7 @@ class CMACBackend(object): @abc.abstractmethod def create_cmac_ctx(self, algorithm): """ - Create a CMACContext for calculating a message authentication code. + Create a MACContext for calculating a message authentication code. """ diff --git a/cryptography/hazmat/backends/openssl/cmac.py b/cryptography/hazmat/backends/openssl/cmac.py index 7acf4391..f1d068eb 100644 --- a/cryptography/hazmat/backends/openssl/cmac.py +++ b/cryptography/hazmat/backends/openssl/cmac.py @@ -20,6 +20,7 @@ from cryptography.hazmat.primitives import interfaces from cryptography.hazmat.primitives.ciphers.modes import CBC +@utils.register_interface(interfaces.MACContext) @utils.register_interface(interfaces.CMACContext) class _CMACContext(object): def __init__(self, backend, algorithm, ctx=None): diff --git a/cryptography/hazmat/primitives/cmac.py b/cryptography/hazmat/primitives/cmac.py index fa463ae0..968fa463 100644 --- a/cryptography/hazmat/primitives/cmac.py +++ b/cryptography/hazmat/primitives/cmac.py @@ -21,6 +21,7 @@ from cryptography.hazmat.backends.interfaces import CMACBackend from cryptography.hazmat.primitives import constant_time, interfaces +@utils.register_interface(interfaces.MACContext) @utils.register_interface(interfaces.CMACContext) class CMAC(object): def __init__(self, algorithm, backend, ctx=None): diff --git a/cryptography/hazmat/primitives/hmac.py b/cryptography/hazmat/primitives/hmac.py index 026ad3b3..23292432 100644 --- a/cryptography/hazmat/primitives/hmac.py +++ b/cryptography/hazmat/primitives/hmac.py @@ -21,6 +21,7 @@ from cryptography.hazmat.backends.interfaces import HMACBackend from cryptography.hazmat.primitives import constant_time, interfaces +@utils.register_interface(interfaces.MACContext) @utils.register_interface(interfaces.HashContext) class HMAC(object): def __init__(self, key, algorithm, backend, ctx=None): diff --git a/cryptography/hazmat/primitives/interfaces.py b/cryptography/hazmat/primitives/interfaces.py index 6ae0a4c5..dacabb2e 100644 --- a/cryptography/hazmat/primitives/interfaces.py +++ b/cryptography/hazmat/primitives/interfaces.py @@ -393,11 +393,12 @@ class KeyDerivationFunction(object): @six.add_metaclass(abc.ABCMeta) class CMACContext(object): @abc.abstractmethod - def update(self, data): + def update(self): """ Processes the provided bytes. """ + @abc.abstractmethod def finalize(self): """ Returns the message authentication code as bytes. @@ -486,3 +487,37 @@ class EllipticCurvePublicKeyWithNumbers(EllipticCurvePublicKey): """ Returns an EllipticCurvePublicNumbers. """ + + +@six.add_metaclass(abc.ABCMeta) +class MACContext(object): + @abc.abstractproperty + def algorithm(self): + """ + The algorithm that will be used by this context. + """ + + @abc.abstractmethod + def update(self, data): + """ + Processes the provided bytes. + """ + + @abc.abstractmethod + def finalize(self): + """ + Returns the message authentication code as bytes. + """ + + @abc.abstractmethod + def copy(self): + """ + Return a MACContext that is a copy of the current context. + """ + + @abc.abstractmethod + def verify(self): + """ + Checks if the generated message authentication code matches the + signature. + """ |