Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verify the mac even if the padding is 1 byte long #234

Merged
merged 1 commit into from
Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tlslite/utils/constanttime.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def ct_check_cbc_mac_and_pad(data, mac, seqnumBytes, contentType, version):
data_mac.update(compatHMAC(data[:start_pos]))

# don't check past the array end (already checked to be >= zero)
end_pos = data_len - 1 - mac.digest_size
end_pos = data_len - mac.digest_size

# calculate all possible
for i in range(start_pos, end_pos): # constant for given overall length
Expand Down
21 changes: 21 additions & 0 deletions unit_tests/test_tlslite_utils_constanttime.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from hypothesis import given, example
import hypothesis.strategies as st
from tlslite.utils.compat import compatHMAC
from tlslite.utils.cryptomath import getRandomBytes
from tlslite.recordlayer import RecordLayer
import tlslite.utils.tlshashlib as hashlib
import hmac
Expand Down Expand Up @@ -266,6 +267,26 @@ def test_with_invalid_hash(self):
self.assertFalse(ct_check_cbc_mac_and_pad(data, h, seqnum_bytes,
content_type, version))

@given(i=st.integers(1, 20))
def test_with_invalid_random_hash(self, i):
key = compatHMAC(getRandomBytes(20))
seqnum_bytes = bytearray(16)
content_type = 0x15
version = (3, 3)
application_data = getRandomBytes(63)
mac = hashlib.sha1

data = self.data_prepare(application_data, seqnum_bytes, content_type,
version, mac, key)
data[-i] ^= 0xff
padding = bytearray(b'\x00')
data += padding

h = hmac.new(key, digestmod=mac)
h.block_size = mac().block_size
self.assertFalse(ct_check_cbc_mac_and_pad(data, h, seqnum_bytes,
content_type, version))

def test_with_invalid_pad(self):
key = compatHMAC(bytearray(20))
seqnum_bytes = bytearray(16)
Expand Down