diff options
author | Alex Gaynor <alex.gaynor@gmail.com> | 2013-11-19 10:44:51 -0800 |
---|---|---|
committer | Alex Gaynor <alex.gaynor@gmail.com> | 2013-11-19 10:44:51 -0800 |
commit | 2c03c89cce729f08496756bcac5f8564b5599dca (patch) | |
tree | 8eff66953a82bf6f795706e280616cc8cd1b65b7 | |
parent | 04b8330d0a719b7f312207e7098c44f55a25fe39 (diff) | |
download | cryptography-2c03c89cce729f08496756bcac5f8564b5599dca.tar.gz cryptography-2c03c89cce729f08496756bcac5f8564b5599dca.tar.bz2 cryptography-2c03c89cce729f08496756bcac5f8564b5599dca.zip |
Even fewer secret branches before the data is valid
-rw-r--r-- | cryptography/hazmat/primitives/padding.py | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/cryptography/hazmat/primitives/padding.py b/cryptography/hazmat/primitives/padding.py index bc7a768d..d185fb6f 100644 --- a/cryptography/hazmat/primitives/padding.py +++ b/cryptography/hazmat/primitives/padding.py @@ -48,6 +48,9 @@ bool Cryptography_check_padding(const uint8_t *data, uint8_t block_len) { mismatch |= (mask & (pad_size ^ b)); } + /* Check to make sure the pad_size was within the valid range. */ + mismatch |= ~(0 < pad_size <= block_len); + /* Make sure any bits set are copied to the lowest bit */ mismatch |= mismatch >> 4; mismatch |= mismatch >> 2; @@ -146,15 +149,15 @@ class _PKCS7UnpaddingContext(object): if len(self._buffer) != self.block_size // 8: raise ValueError("Invalid padding bytes") - pad_size = six.indexbytes(self._buffer, -1) valid = _lib.Cryptography_check_padding( self._buffer, self.block_size // 8 ) - if not valid or not (0 < pad_size <= self.block_size // 8): + if not valid: raise ValueError("Invalid padding bytes") + pad_size = six.indexbytes(self._buffer, -1) res = self._buffer[:-pad_size] self._buffer = None return res |