diff options
author | Alex Gaynor <alex.gaynor@gmail.com> | 2013-08-09 12:12:30 -0700 |
---|---|---|
committer | Alex Gaynor <alex.gaynor@gmail.com> | 2013-08-09 12:12:30 -0700 |
commit | 250903aa4c1776a1859bbb41b9e19953e0733206 (patch) | |
tree | 03f5af319475080420822f8bc1d4f70ace4ba8e4 | |
parent | 0b26f787601acedf80d8fad5c71bf60c907b5201 (diff) | |
download | cryptography-250903aa4c1776a1859bbb41b9e19953e0733206.tar.gz cryptography-250903aa4c1776a1859bbb41b9e19953e0733206.tar.bz2 cryptography-250903aa4c1776a1859bbb41b9e19953e0733206.zip |
Removed duplicate tests, added tests + fix for use after finalize
-rw-r--r-- | cryptography/primitives/block/base.py | 4 | ||||
-rw-r--r-- | tests/primitives/test_block.py | 24 |
2 files changed, 13 insertions, 15 deletions
diff --git a/cryptography/primitives/block/base.py b/cryptography/primitives/block/base.py index 6e3565a3..d52fa73e 100644 --- a/cryptography/primitives/block/base.py +++ b/cryptography/primitives/block/base.py @@ -10,9 +10,13 @@ class BlockCipher(object): self._ctx = api.create_block_cipher_context(cipher, mode) def encrypt(self, plaintext): + if self._ctx is None: + raise ValueError("BlockCipher was already finalized") return api.update_encrypt_context(self._ctx, plaintext) def finalize(self): + if self._ctx is None: + raise ValueError("BlockCipher was already finalized") # TODO: this might be a decrypt context result = api.finalize_encrypt_context(self._ctx) self._ctx = None diff --git a/tests/primitives/test_block.py b/tests/primitives/test_block.py index 5e342d2f..79d65a0f 100644 --- a/tests/primitives/test_block.py +++ b/tests/primitives/test_block.py @@ -4,22 +4,16 @@ import pytest from cryptography.primitives.block import BlockCipher, ciphers, modes, padding -from ..utils import load_nist_vectors_from_file - class TestBlockCipher(object): - @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), - load_nist_vectors_from_file( - "AES/KAT/CBCGFSbox256.rsp", - "ENCRYPT", - ["key", "iv", "plaintext", "ciphertext"] - ) - ) - def test_aes_cbc_nopadding(self, key, iv, plaintext, ciphertext): + def test_use_after_finalize(self): cipher = BlockCipher( - ciphers.AES(binascii.unhexlify(key)), - modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) + ciphers.AES(binascii.unhexlify(b"0" * 32)), + modes.CBC(binascii.unhexlify(b"0" * 32), padding.NoPadding()) ) - actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) - actual_ciphertext += cipher.finalize() - assert binascii.hexlify(actual_ciphertext) + cipher.encrypt(b"a" * 16) + cipher.finalize() + with pytest.raises(ValueError): + cipher.encrypt(b"b" * 16) + with pytest.raises(ValueError): + cipher.finalize() |