| 17 | |
| 18 | @wycheproof_tests("aes_cbc_pkcs5_test.json") |
| 19 | def test_aes_cbc_pkcs5(backend, wycheproof): |
| 20 | key = binascii.unhexlify(wycheproof.testcase["key"]) |
| 21 | iv = binascii.unhexlify(wycheproof.testcase["iv"]) |
| 22 | msg = binascii.unhexlify(wycheproof.testcase["msg"]) |
| 23 | ct = binascii.unhexlify(wycheproof.testcase["ct"]) |
| 24 | |
| 25 | padder = padding.PKCS7(128).padder() |
| 26 | |
| 27 | cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend) |
| 28 | enc = cipher.encryptor() |
| 29 | computed_ct = ( |
| 30 | enc.update(padder.update(msg) + padder.finalize()) + enc.finalize() |
| 31 | ) |
| 32 | dec = cipher.decryptor() |
| 33 | padded_msg = dec.update(ct) + dec.finalize() |
| 34 | unpadder = padding.PKCS7(128).unpadder() |
| 35 | if wycheproof.valid or wycheproof.acceptable: |
| 36 | assert computed_ct == ct |
| 37 | computed_msg = unpadder.update(padded_msg) + unpadder.finalize() |
| 38 | assert computed_msg == msg |
| 39 | else: |
| 40 | assert computed_ct != ct |
| 41 | with pytest.raises(ValueError): |
| 42 | unpadder.update(padded_msg) + unpadder.finalize() |
| 43 | |
| 44 | |
| 45 | @wycheproof_tests("aes_gcm_test.json") |