1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-05 22:18:16 +00:00
trezor-firmware/tests/device_tests/test_firmware_hash.py

50 lines
1.5 KiB
Python
Raw Normal View History

from hashlib import blake2s
import pytest
from trezorlib import firmware, models
2024-11-15 16:31:22 +00:00
from trezorlib.debuglink import SessionDebugWrapper as Session
# size of FIRMWARE_AREA, see core/embed/models/model_*_layout.c
FIRMWARE_LENGTHS = {
models.T1B1: 7 * 128 * 1024 + 64 * 1024,
models.T2T1: 13 * 128 * 1024,
models.T2B1: 13 * 128 * 1024,
models.T3T1: 208 * 8 * 1024,
models.T3B1: 208 * 8 * 1024,
}
2024-11-15 16:31:22 +00:00
def test_firmware_hash_emu(session: Session) -> None:
if session.features.fw_vendor != "EMULATOR":
pytest.skip("Only for emulator")
2024-11-15 16:31:22 +00:00
data = b"\xff" * FIRMWARE_LENGTHS[session.model]
expected_hash = blake2s(data).digest()
2024-11-15 16:31:22 +00:00
hash = firmware.get_hash(session, None)
assert hash == expected_hash
challenge = b"Hello Trezor"
expected_hash = blake2s(data, key=challenge).digest()
2024-11-15 16:31:22 +00:00
hash = firmware.get_hash(session, challenge)
assert hash == expected_hash
2024-11-15 16:31:22 +00:00
def test_firmware_hash_hw(session: Session) -> None:
if session.features.fw_vendor == "EMULATOR":
pytest.skip("Only for hardware")
# TODO get firmware image from outside the environment, check for actual result
challenge = b"Hello Trezor"
2024-11-15 16:31:22 +00:00
empty_data = b"\xff" * FIRMWARE_LENGTHS[session.model]
empty_hash = blake2s(empty_data).digest()
empty_hash_challenge = blake2s(empty_data, key=challenge).digest()
2024-11-15 16:31:22 +00:00
hash = firmware.get_hash(session, None)
assert hash != empty_hash
2024-11-15 16:31:22 +00:00
hash2 = firmware.get_hash(session, challenge)
assert hash != hash2
assert hash2 != empty_hash_challenge