aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorCédric Krier <ced@b2ck.com>2016-03-14 00:54:58 +0100
committerCédric Krier <ced@b2ck.com>2016-03-14 00:54:58 +0100
commit288027ee43494eff37ee2c558b3683ea94c769ff (patch)
treea18c1074e36f74729d89e4228de3326a57b4bf53 /src
parent89d47907f112be41a9ea4e684af24d5a74c14336 (diff)
downloadcryptography-288027ee43494eff37ee2c558b3683ea94c769ff.tar.gz
cryptography-288027ee43494eff37ee2c558b3683ea94c769ff.tar.bz2
cryptography-288027ee43494eff37ee2c558b3683ea94c769ff.zip
Don't use subclass
Diffstat (limited to 'src')
-rw-r--r--src/cryptography/hazmat/primitives/padding.py200
1 files changed, 112 insertions, 88 deletions
diff --git a/src/cryptography/hazmat/primitives/padding.py b/src/cryptography/hazmat/primitives/padding.py
index c6fe656e..77fb8f83 100644
--- a/src/cryptography/hazmat/primitives/padding.py
+++ b/src/cryptography/hazmat/primitives/padding.py
@@ -28,107 +28,76 @@ class PaddingContext(object):
"""
-class _BytePadding(object):
- def __init__(self, block_size):
- if not (0 <= block_size < 256):
- raise ValueError("block_size must be in range(0, 256).")
+def _byte_padding_check(block_size):
+ if not (0 <= block_size < 256):
+ raise ValueError("block_size must be in range(0, 256).")
- if block_size % 8 != 0:
- raise ValueError("block_size must be a multiple of 8.")
+ if block_size % 8 != 0:
+ raise ValueError("block_size must be a multiple of 8.")
- self.block_size = block_size
+def _byte_padding_update(buffer_, data, block_size):
+ if buffer_ is None:
+ raise AlreadyFinalized("Context was already finalized.")
-@six.add_metaclass(abc.ABCMeta)
-class _BytePaddingContext(object):
- def __init__(self, block_size):
- self.block_size = block_size
- # TODO: more copies than necessary, we should use zero-buffer (#193)
- self._buffer = b""
+ if not isinstance(data, bytes):
+ raise TypeError("data must be bytes.")
- def update(self, data):
- if self._buffer is None:
- raise AlreadyFinalized("Context was already finalized.")
+ buffer_ += data
- if not isinstance(data, bytes):
- raise TypeError("data must be bytes.")
+ finished_blocks = len(buffer_) // (block_size // 8)
- self._buffer += data
+ result = buffer_[:finished_blocks * (block_size // 8)]
+ buffer_ = buffer_[finished_blocks * (block_size // 8):]
- finished_blocks = len(self._buffer) // (self.block_size // 8)
+ return buffer_, result
- result = self._buffer[:finished_blocks * (self.block_size // 8)]
- self._buffer = self._buffer[finished_blocks * (self.block_size // 8):]
- return result
+def _byte_padding_pad(buffer_, block_size, paddingfn):
+ if buffer_ is None:
+ raise AlreadyFinalized("Context was already finalized.")
- @abc.abstractmethod
- def _padding(self, size):
- """
- Returns the padding for the size.
- """
+ pad_size = block_size // 8 - len(buffer_)
+ return buffer_ + paddingfn(pad_size)
- def finalize(self):
- if self._buffer is None:
- raise AlreadyFinalized("Context was already finalized.")
- pad_size = self.block_size // 8 - len(self._buffer)
- result = self._buffer + self._padding(pad_size)
- self._buffer = None
- return result
+def _byte_unpadding_update(buffer_, data, block_size):
+ if buffer_ is None:
+ raise AlreadyFinalized("Context was already finalized.")
+ if not isinstance(data, bytes):
+ raise TypeError("data must be bytes.")
-@six.add_metaclass(abc.ABCMeta)
-class _ByteUnpaddingContext(object):
- def __init__(self, block_size):
- self.block_size = block_size
- # TODO: more copies than necessary, we should use zero-buffer (#193)
- self._buffer = b""
-
- def update(self, data):
- if self._buffer is None:
- raise AlreadyFinalized("Context was already finalized.")
+ buffer_ += data
- if not isinstance(data, bytes):
- raise TypeError("data must be bytes.")
+ finished_blocks = max(len(buffer_) // (block_size // 8) - 1, 0)
- self._buffer += data
+ result = buffer_[:finished_blocks * (block_size // 8)]
+ buffer_ = buffer_[finished_blocks * (block_size // 8):]
- finished_blocks = max(
- len(self._buffer) // (self.block_size // 8) - 1,
- 0
- )
+ return buffer_, result
- result = self._buffer[:finished_blocks * (self.block_size // 8)]
- self._buffer = self._buffer[finished_blocks * (self.block_size // 8):]
-
- return result
- @abc.abstractmethod
- def _check_padding(self):
- """
- Returns if the padding is valid.
- """
+def _byte_unpadding_check(buffer_, block_size, checkfn):
+ if buffer_ is None:
+ raise AlreadyFinalized("Context was already finalized.")
- def finalize(self):
- if self._buffer is None:
- raise AlreadyFinalized("Context was already finalized.")
+ if len(buffer_) != block_size // 8:
+ raise ValueError("Invalid padding bytes.")
- if len(self._buffer) != self.block_size // 8:
- raise ValueError("Invalid padding bytes.")
+ valid = checkfn(buffer_, block_size // 8)
- valid = self._check_padding()
+ if not valid:
+ raise ValueError("Invalid padding bytes.")
- if not valid:
- raise ValueError("Invalid padding bytes.")
+ pad_size = six.indexbytes(buffer_, -1)
+ return buffer_[:-pad_size]
- pad_size = six.indexbytes(self._buffer, -1)
- res = self._buffer[:-pad_size]
- self._buffer = None
- return res
-
-class PKCS7(_BytePadding):
+class PKCS7(object):
+ def __init__(self, block_size):
+ _byte_padding_check(block_size)
+ self.block_size = block_size
def padder(self):
return _PKCS7PaddingContext(self.block_size)
@@ -138,22 +107,51 @@ class PKCS7(_BytePadding):
@utils.register_interface(PaddingContext)
-class _PKCS7PaddingContext(_BytePaddingContext):
+class _PKCS7PaddingContext(object):
+ def __init__(self, block_size):
+ self.block_size = block_size
+ # TODO: more copies than necessary, we should use zero-buffer (#193)
+ self._buffer = b""
+
+ def update(self, data):
+ self._buffer, result = _byte_padding_update(
+ self._buffer, data, self.block_size)
+ return result
def _padding(self, size):
return six.int2byte(size) * size
+ def finalize(self):
+ result = _byte_padding_pad(
+ self._buffer, self.block_size, self._padding)
+ self._buffer = None
+ return result
+
@utils.register_interface(PaddingContext)
-class _PKCS7UnpaddingContext(_ByteUnpaddingContext):
+class _PKCS7UnpaddingContext(object):
+ def __init__(self, block_size):
+ self.block_size = block_size
+ # TODO: more copies than necessary, we should use zero-buffer (#193)
+ self._buffer = b""
- def _check_padding(self):
- return lib.Cryptography_check_pkcs7_padding(
- self._buffer, self.block_size // 8
- )
+ def update(self, data):
+ self._buffer, result = _byte_unpadding_update(
+ self._buffer, data, self.block_size)
+ return result
+ def finalize(self):
+ result = _byte_unpadding_check(
+ self._buffer, self.block_size,
+ lib.Cryptography_check_pkcs7_padding)
+ self._buffer = None
+ return result
-class ANSIX923(_BytePadding):
+
+class ANSIX923(object):
+ def __init__(self, block_size):
+ _byte_padding_check(block_size)
+ self.block_size = block_size
def padder(self):
return _ANSIX923PaddingContext(self.block_size)
@@ -163,16 +161,42 @@ class ANSIX923(_BytePadding):
@utils.register_interface(PaddingContext)
-class _ANSIX923PaddingContext(_BytePaddingContext):
+class _ANSIX923PaddingContext(object):
+ def __init__(self, block_size):
+ self.block_size = block_size
+ # TODO: more copies than necessary, we should use zero-buffer (#193)
+ self._buffer = b""
+
+ def update(self, data):
+ self._buffer, result = _byte_padding_update(
+ self._buffer, data, self.block_size)
+ return result
def _padding(self, size):
return six.int2byte(0) * (size - 1) + six.int2byte(size)
+ def finalize(self):
+ result = _byte_padding_pad(
+ self._buffer, self.block_size, self._padding)
+ self._buffer = None
+ return result
+
@utils.register_interface(PaddingContext)
-class _ANSIX923UnpaddingContext(_ByteUnpaddingContext):
+class _ANSIX923UnpaddingContext(object):
+ def __init__(self, block_size):
+ self.block_size = block_size
+ # TODO: more copies than necessary, we should use zero-buffer (#193)
+ self._buffer = b""
- def _check_padding(self):
- return lib.Cryptography_check_ansix923_padding(
- self._buffer, self.block_size // 8
- )
+ def update(self, data):
+ self._buffer, result = _byte_unpadding_update(
+ self._buffer, data, self.block_size)
+ return result
+
+ def finalize(self):
+ result = _byte_unpadding_check(
+ self._buffer, self.block_size,
+ lib.Cryptography_check_ansix923_padding)
+ self._buffer = None
+ return result