2022-08-02 14:11:55 +00:00
|
|
|
from hashlib import blake2s
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2024-03-09 21:05:05 +00:00
|
|
|
from trezorlib import firmware, models
|
2022-08-02 14:11:55 +00:00
|
|
|
from trezorlib.debuglink import TrezorClientDebugLink as Client
|
|
|
|
|
2024-03-09 21:05:05 +00:00
|
|
|
# size of FIRMWARE_AREA, see core/embed/models/model_*_layout.c
|
2022-08-02 14:11:55 +00:00
|
|
|
FIRMWARE_LENGTHS = {
|
2024-03-09 21:05:05 +00:00
|
|
|
models.T1B1: 7 * 128 * 1024 + 64 * 1024,
|
|
|
|
models.T2T1: 13 * 128 * 1024,
|
|
|
|
models.T2B1: 13 * 128 * 1024,
|
|
|
|
models.T3T1: 208 * 8 * 1024,
|
2024-09-02 11:01:16 +00:00
|
|
|
models.T3B1: 208 * 8 * 1024,
|
2022-08-02 14:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def test_firmware_hash_emu(client: Client) -> None:
|
|
|
|
if client.features.fw_vendor != "EMULATOR":
|
|
|
|
pytest.skip("Only for emulator")
|
|
|
|
|
2024-03-09 21:05:05 +00:00
|
|
|
data = b"\xff" * FIRMWARE_LENGTHS[client.model]
|
2022-08-02 14:11:55 +00:00
|
|
|
|
|
|
|
expected_hash = blake2s(data).digest()
|
|
|
|
hash = firmware.get_hash(client, None)
|
|
|
|
assert hash == expected_hash
|
|
|
|
|
|
|
|
challenge = b"Hello Trezor"
|
|
|
|
expected_hash = blake2s(data, key=challenge).digest()
|
|
|
|
hash = firmware.get_hash(client, challenge)
|
|
|
|
assert hash == expected_hash
|
|
|
|
|
|
|
|
|
|
|
|
def test_firmware_hash_hw(client: Client) -> None:
|
|
|
|
if client.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-03-09 21:05:05 +00:00
|
|
|
empty_data = b"\xff" * FIRMWARE_LENGTHS[client.model]
|
2022-08-02 14:11:55 +00:00
|
|
|
empty_hash = blake2s(empty_data).digest()
|
|
|
|
empty_hash_challenge = blake2s(empty_data, key=challenge).digest()
|
|
|
|
|
|
|
|
hash = firmware.get_hash(client, None)
|
|
|
|
assert hash != empty_hash
|
|
|
|
|
|
|
|
hash2 = firmware.get_hash(client, challenge)
|
|
|
|
assert hash != hash2
|
|
|
|
assert hash2 != empty_hash_challenge
|