aboutsummaryrefslogtreecommitdiffstats
path: root/tests/wycheproof
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2018-07-17 20:27:52 +0800
committerAlex Gaynor <alex.gaynor@gmail.com>2018-07-17 08:27:52 -0400
commit160c9b20d47fe11311ce1d89aaa70d1d093e37e0 (patch)
tree970b88e956330ce5bb09c4b49559904e3d6cf07a /tests/wycheproof
parentd1c73fd7827ea05505b033a0b126c35749430ea9 (diff)
downloadcryptography-160c9b20d47fe11311ce1d89aaa70d1d093e37e0.tar.gz
cryptography-160c9b20d47fe11311ce1d89aaa70d1d093e37e0.tar.bz2
cryptography-160c9b20d47fe11311ce1d89aaa70d1d093e37e0.zip
add aes cbc pkcs5 wycheproof tests (#4347)
Diffstat (limited to 'tests/wycheproof')
-rw-r--r--tests/wycheproof/test_aes.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/wycheproof/test_aes.py b/tests/wycheproof/test_aes.py
new file mode 100644
index 00000000..65db9cdb
--- /dev/null
+++ b/tests/wycheproof/test_aes.py
@@ -0,0 +1,42 @@
+# This file is dual licensed under the terms of the Apache License, Version
+# 2.0, and the BSD License. See the LICENSE file in the root of this repository
+# for complete details.
+
+from __future__ import absolute_import, division, print_function
+
+import binascii
+
+import pytest
+
+from cryptography.hazmat.backends.interfaces import CipherBackend
+from cryptography.hazmat.primitives import padding
+from cryptography.hazmat.primitives.ciphers import (
+ Cipher, algorithms, modes
+)
+
+
+@pytest.mark.requires_backend_interface(interface=CipherBackend)
+@pytest.mark.wycheproof_tests("aes_cbc_pkcs5_test.json")
+def test_aes_cbc_pkcs5(backend, wycheproof):
+ key = binascii.unhexlify(wycheproof.testcase["key"])
+ iv = binascii.unhexlify(wycheproof.testcase["iv"])
+ msg = binascii.unhexlify(wycheproof.testcase["msg"])
+ ct = binascii.unhexlify(wycheproof.testcase["ct"])
+
+ padder = padding.PKCS7(128).padder()
+
+ cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend)
+ enc = cipher.encryptor()
+ computed_ct = enc.update(
+ padder.update(msg) + padder.finalize()) + enc.finalize()
+ dec = cipher.decryptor()
+ padded_msg = dec.update(ct) + dec.finalize()
+ unpadder = padding.PKCS7(128).unpadder()
+ if wycheproof.valid or wycheproof.acceptable:
+ assert computed_ct == ct
+ computed_msg = unpadder.update(padded_msg) + unpadder.finalize()
+ assert computed_msg == msg
+ else:
+ assert computed_ct != ct
+ with pytest.raises(ValueError):
+ unpadder.update(padded_msg) + unpadder.finalize()