aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cryptography/hazmat/primitives/padding.py9
-rw-r--r--docs/hazmat/primitives/padding.rst9
-rw-r--r--tests/hazmat/primitives/test_padding.py9
3 files changed, 19 insertions, 8 deletions
diff --git a/cryptography/hazmat/primitives/padding.py b/cryptography/hazmat/primitives/padding.py
index d78c6a5b..c1a763b5 100644
--- a/cryptography/hazmat/primitives/padding.py
+++ b/cryptography/hazmat/primitives/padding.py
@@ -20,6 +20,7 @@ import cffi
import six
from cryptography import utils
+from cryptography.exceptions import AlreadyFinalized
from cryptography.hazmat.bindings.utils import _create_modulename
from cryptography.hazmat.primitives import interfaces
@@ -101,7 +102,7 @@ class _PKCS7PaddingContext(object):
def update(self, data):
if self._buffer is None:
- raise ValueError("Context was already finalized")
+ raise AlreadyFinalized("Context was already finalized")
if isinstance(data, six.text_type):
raise TypeError("Unicode-objects must be encoded before padding")
@@ -117,7 +118,7 @@ class _PKCS7PaddingContext(object):
def finalize(self):
if self._buffer is None:
- raise ValueError("Context was already finalized")
+ raise AlreadyFinalized("Context was already finalized")
pad_size = self.block_size // 8 - len(self._buffer)
result = self._buffer + six.int2byte(pad_size) * pad_size
@@ -134,7 +135,7 @@ class _PKCS7UnpaddingContext(object):
def update(self, data):
if self._buffer is None:
- raise ValueError("Context was already finalized")
+ raise AlreadyFinalized("Context was already finalized")
if isinstance(data, six.text_type):
raise TypeError("Unicode-objects must be encoded before unpadding")
@@ -153,7 +154,7 @@ class _PKCS7UnpaddingContext(object):
def finalize(self):
if self._buffer is None:
- raise ValueError("Context was already finalized")
+ raise AlreadyFinalized("Context was already finalized")
if len(self._buffer) != self.block_size // 8:
raise ValueError("Invalid padding bytes")
diff --git a/docs/hazmat/primitives/padding.rst b/docs/hazmat/primitives/padding.rst
index c0209446..3056eb92 100644
--- a/docs/hazmat/primitives/padding.rst
+++ b/docs/hazmat/primitives/padding.rst
@@ -66,7 +66,16 @@ multiple of the block size.
:param bytes data: The data you wish to pass into the context.
:return bytes: Returns the data that was padded or unpadded.
+ :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`.
.. method:: finalize()
+ Finalize the current context and return the rest of the data.
+
+ After ``finalize`` has been called this object can no longer be used;
+ :meth:`update` and :meth:`finalize` will raise an
+ :class:`~cryptography.exceptions.AlreadyFinalized` exception.
+
:return bytes: Returns the remainder of the data.
+ :raises ValueError: When trying to remove padding from incorrectly
+ padded data.
diff --git a/tests/hazmat/primitives/test_padding.py b/tests/hazmat/primitives/test_padding.py
index 932cef1e..cac54f27 100644
--- a/tests/hazmat/primitives/test_padding.py
+++ b/tests/hazmat/primitives/test_padding.py
@@ -17,6 +17,7 @@ import pytest
import six
+from cryptography.exceptions import AlreadyFinalized
from cryptography.hazmat.primitives import padding
@@ -97,15 +98,15 @@ class TestPKCS7(object):
def test_use_after_finalize(self):
padder = padding.PKCS7(128).padder()
b = padder.finalize()
- with pytest.raises(ValueError):
+ with pytest.raises(AlreadyFinalized):
padder.update(b"")
- with pytest.raises(ValueError):
+ with pytest.raises(AlreadyFinalized):
padder.finalize()
unpadder = padding.PKCS7(128).unpadder()
unpadder.update(b)
unpadder.finalize()
- with pytest.raises(ValueError):
+ with pytest.raises(AlreadyFinalized):
unpadder.update(b"")
- with pytest.raises(ValueError):
+ with pytest.raises(AlreadyFinalized):
unpadder.finalize()