1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 07:28:10 +00:00

python/firmware: simplify calculate_code_hashes

This commit is contained in:
matejcik 2019-12-20 13:50:24 +01:00 committed by Pavol Rusnak
parent 40477b836e
commit 3fc3231254
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

View File

@ -358,17 +358,14 @@ def calculate_code_hashes(
padding_byte: bytes = None,
) -> None:
hashes = []
for i in range(16):
if i == 0:
# Because first chunk is sent along with headers, there is less code in it.
chunk = code[: chunk_size - code_offset]
else:
# Subsequent chunks are shifted by the "missing header" size.
ptr = i * chunk_size - code_offset
chunk = code[ptr : ptr + chunk_size]
# padding for last chunk
if padding_byte is not None and i > 1 and chunk and len(chunk) < chunk_size:
# End offset for each chunk. Normally this would be (i+1)*chunk_size for i-th chunk,
# but the first chunk is shorter by code_offset, so all end offsets are shifted.
ends = [(i + 1) * chunk_size - code_offset for i in range(16)]
start = 0
for end in ends:
chunk = code[start:end]
# padding for last non-empty chunk
if padding_byte is not None and start < len(code) and end > len(code):
chunk += padding_byte[0:1] * (chunk_size - len(chunk))
if not chunk:
@ -376,6 +373,8 @@ def calculate_code_hashes(
else:
hashes.append(hash_function(chunk).digest())
start = end
return hashes