Alex Gaynor | 95b8620 | 2013-08-08 19:36:44 -0700 | [diff] [blame] | 1 | import binascii |
| 2 | |
| 3 | import pytest |
| 4 | |
| 5 | from cryptography.primitives.block import BlockCipher, ciphers, modes, padding |
| 6 | |
Alex Gaynor | cd95f3c | 2013-08-08 22:08:26 -0700 | [diff] [blame] | 7 | from ..utils import load_nist_vectors_from_file |
| 8 | |
Alex Gaynor | 95b8620 | 2013-08-08 19:36:44 -0700 | [diff] [blame] | 9 | |
| 10 | class TestBlockCipher(object): |
Alex Gaynor | cd95f3c | 2013-08-08 22:08:26 -0700 | [diff] [blame] | 11 | @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
Alex Gaynor | de9a3ee | 2013-08-09 10:42:44 -0700 | [diff] [blame] | 12 | load_nist_vectors_from_file( |
| 13 | "AES/KAT/CBCGFSbox256.rsp", |
| 14 | "ENCRYPT", |
| 15 | ["key", "iv", "plaintext", "ciphertext"] |
| 16 | ) |
Alex Gaynor | cd95f3c | 2013-08-08 22:08:26 -0700 | [diff] [blame] | 17 | ) |
Alex Gaynor | 95b8620 | 2013-08-08 19:36:44 -0700 | [diff] [blame] | 18 | def test_aes_cbc_nopadding(self, key, iv, plaintext, ciphertext): |
| 19 | cipher = BlockCipher( |
Alex Gaynor | de9a3ee | 2013-08-09 10:42:44 -0700 | [diff] [blame] | 20 | ciphers.AES(binascii.unhexlify(key)), |
| 21 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
Alex Gaynor | 95b8620 | 2013-08-08 19:36:44 -0700 | [diff] [blame] | 22 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 23 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 24 | actual_ciphertext += cipher.finalize() |
Alex Gaynor | de9a3ee | 2013-08-09 10:42:44 -0700 | [diff] [blame] | 25 | assert binascii.hexlify(actual_ciphertext) |