Skip to content

Commit 3674815

Browse files
committed
verify the mac even if the padding is 1 byte long
off-by-one error on mac checking, if the padding is of minimal length (a single 0x00 byte), the mac is not checked and thus the return value is never falsified this fixes the issue
1 parent 5b3c7b6 commit 3674815

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

tlslite/utils/constanttime.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def ct_check_cbc_mac_and_pad(data, mac, seqnumBytes, contentType, version):
170170
data_mac.update(compatHMAC(data[:start_pos]))
171171

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

175175
# calculate all possible
176176
for i in range(start_pos, end_pos): # constant for given overall length

unit_tests/test_tlslite_utils_constanttime.py

+21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from hypothesis import given, example
1717
import hypothesis.strategies as st
1818
from tlslite.utils.compat import compatHMAC
19+
from tlslite.utils.cryptomath import getRandomBytes
1920
from tlslite.recordlayer import RecordLayer
2021
import tlslite.utils.tlshashlib as hashlib
2122
import hmac
@@ -266,6 +267,26 @@ def test_with_invalid_hash(self):
266267
self.assertFalse(ct_check_cbc_mac_and_pad(data, h, seqnum_bytes,
267268
content_type, version))
268269

270+
@given(i=st.integers(1, 20))
271+
def test_with_invalid_random_hash(self, i):
272+
key = compatHMAC(getRandomBytes(20))
273+
seqnum_bytes = bytearray(16)
274+
content_type = 0x15
275+
version = (3, 3)
276+
application_data = getRandomBytes(63)
277+
mac = hashlib.sha1
278+
279+
data = self.data_prepare(application_data, seqnum_bytes, content_type,
280+
version, mac, key)
281+
data[-i] ^= 0xff
282+
padding = bytearray(b'\x00')
283+
data += padding
284+
285+
h = hmac.new(key, digestmod=mac)
286+
h.block_size = mac().block_size
287+
self.assertFalse(ct_check_cbc_mac_and_pad(data, h, seqnum_bytes,
288+
content_type, version))
289+
269290
def test_with_invalid_pad(self):
270291
key = compatHMAC(bytearray(20))
271292
seqnum_bytes = bytearray(16)

0 commit comments

Comments
 (0)