aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2014-01-30 15:23:17 -0800
committerAlex Gaynor <alex.gaynor@gmail.com>2014-01-30 15:23:17 -0800
commit0929f8ff07e71410bd2ce89d407805bd476c1761 (patch)
treeab117c3c0ce3d27d08424dd5976edae4af8f3eb1 /tests
parent723bb9675b7f78e8515be0a80305f6fd009a80fa (diff)
downloadcryptography-0929f8ff07e71410bd2ce89d407805bd476c1761.tar.gz
cryptography-0929f8ff07e71410bd2ce89d407805bd476c1761.tar.bz2
cryptography-0929f8ff07e71410bd2ce89d407805bd476c1761.zip
More direct tests
Diffstat (limited to 'tests')
-rw-r--r--tests/hazmat/backends/test_multibackend.py50
1 files changed, 49 insertions, 1 deletions
diff --git a/tests/hazmat/backends/test_multibackend.py b/tests/hazmat/backends/test_multibackend.py
index f300b6c8..03b3187b 100644
--- a/tests/hazmat/backends/test_multibackend.py
+++ b/tests/hazmat/backends/test_multibackend.py
@@ -15,8 +15,9 @@ import pytest
from cryptography.exceptions import UnsupportedAlgorithm
from cryptography.hazmat.backends.multibackend import PrioritizedMultiBackend
-from cryptography.hazmat.primitives import hashes
+from cryptography.hazmat.primitives import hashes, hmac
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
+from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
class DummyCipherBackend(object):
@@ -47,6 +48,31 @@ class DummyHashBackend(object):
raise UnsupportedAlgorithm
+class DummyHMACBackend(object):
+ def __init__(self, supported_algorithms):
+ self._algorithms = supported_algorithms
+
+ def hmac_supported(self, algorithm):
+ return type(algorithm) in self._algorithms
+
+ def create_hmac_ctx(self, key, algorithm):
+ if not self.hmac_supported(algorithm):
+ raise UnsupportedAlgorithm
+
+
+class DummyPBKDF2HMAC(object):
+ def __init__(self, supported_algorithms):
+ self._algorithms = supported_algorithms
+
+ def pbkdf2_hmac_supported(self, algorithm):
+ return type(algorithm) in self._algorithms
+
+ def derive_pbkdf2_hmac(self, algorithm, length, salt, iterations,
+ key_material):
+ if not self.pbkdf2_hmac_supported(algorithm):
+ raise UnsupportedAlgorithm
+
+
class TestPrioritizedMultiBackend(object):
def test_ciphers(self):
@@ -87,3 +113,25 @@ class TestPrioritizedMultiBackend(object):
with pytest.raises(UnsupportedAlgorithm):
hashes.Hash(hashes.SHA1(), backend=backend)
+
+ def test_hmac(self):
+ backend = PrioritizedMultiBackend([
+ DummyHMACBackend([hashes.MD5])
+ ])
+ assert backend.hmac_supported(hashes.MD5())
+
+ hmac.HMAC(b"", hashes.MD5(), backend=backend)
+
+ with pytest.raises(UnsupportedAlgorithm):
+ hmac.HMAC(b"", hashes.SHA1(), backend=backend)
+
+ def test_pbkdf2(self):
+ backend = PrioritizedMultiBackend([
+ DummyPBKDF2HMAC([hashes.MD5])
+ ])
+ assert backend.pbkdf2_hmac_supported(hashes.MD5())
+
+ backend.derive_pbkdf2_hmac(hashes.MD5(), 10, b"", 10, b"")
+
+ with pytest.raises(UnsupportedAlgorithm):
+ backend.derive_pbkdf2_hmac(hashes.SHA1(), 10, b"", 10, b"")