1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-21 05:48:23 +00:00
trezor-firmware/tools/firmware_hash.py

48 lines
1.2 KiB
Python
Raw Normal View History

2022-04-26 17:53:12 +00:00
#!/usr/bin/env python3
import sys
from hashlib import blake2s
FILE_T1 = "legacy/firmware/trezor.bin"
FILE_T2 = "core/build/firmware/firmware.bin"
T1_HEADER_MAGIC_OLD = b"TRZR"
T1_HEADER_OLD_SIZE = 256
T2_HEADER_MAGIC_VENDOR = b"TRZV"
2022-04-26 17:53:12 +00:00
SIZE_T1 = (7 * 128 + 64) * 1024
SIZE_T2 = 13 * 128 * 1024
if len(sys.argv) > 2:
filenames = sys.argv[2:]
elif len(sys.argv) == 2:
filenames = (FILE_T1, FILE_T2)
else:
print(f"Usage: {sys.argv[0]} HEX_CHALLENGE [FILE]...")
print(f" HEX_CHALLENGE: a 0-32 byte challenge in hexadecimal")
exit(1)
for filename in filenames:
2022-04-26 17:53:12 +00:00
try:
data = open(filename, "rb").read()
except FileNotFoundError:
print(f"{filename} not found")
continue
offset = 0
if data[:4] == T2_HEADER_MAGIC_VENDOR:
size = SIZE_T2
2022-04-26 17:53:12 +00:00
else:
size = SIZE_T1
if data[:4] == T1_HEADER_MAGIC_OLD:
offset = T1_HEADER_OLD_SIZE
if len(data) - offset > size:
raise ValueError(filename, "too big")
data = data[offset:] + b"\xff" * (size - len(data) + offset)
2022-04-26 17:53:12 +00:00
challenge = bytes.fromhex(sys.argv[1])
firmware_hash = blake2s(data, key=challenge).hexdigest()
2022-04-26 17:53:12 +00:00
print(f"{filename}: {firmware_hash}")