forked from pyca/cryptography
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add aes cbc pkcs5 wycheproof tests (pyca#4347)
- Loading branch information
1 parent
dedb415
commit 52ff07f
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |