1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-17 20:08:12 +00:00

chore(python): use model names more consistently

This commit is contained in:
matejcik 2023-10-16 12:38:24 +02:00
parent a16fbe6237
commit a4f378e565
7 changed files with 78 additions and 53 deletions

View File

@ -8,7 +8,7 @@ import click
import ecdsa
from trezorlib.firmware.legacy import LegacyV2Firmware
from trezorlib.firmware.models import TREZOR_ONE_V3_DEV
from trezorlib.firmware.models import LEGACY_V3_DEV
SECRET_KEYS = [
ecdsa.SigningKey.from_string(bytes.fromhex(sk), curve=ecdsa.SECP256k1)
@ -22,9 +22,7 @@ SECRET_KEYS = [
PUBLIC_KEYS: list[ecdsa.VerifyingKey] = [sk.get_verifying_key() for sk in SECRET_KEYS]
# Should be these public keys
assert [
pk.to_string("compressed") for pk in PUBLIC_KEYS
] == TREZOR_ONE_V3_DEV.firmware_keys
assert [pk.to_string("compressed") for pk in PUBLIC_KEYS] == LEGACY_V3_DEV.firmware_keys
def signmessage(digest: bytes, key: ecdsa.SigningKey) -> bytes:

View File

@ -0,0 +1 @@
Internal names are used consistently in constants and names. Original model-based names are kept as aliases for backwards compatibility.

View File

@ -405,7 +405,7 @@ class LegacyFirmware(firmware.LegacyFirmware):
def insert_signature(self, slot: int, key_index: int, signature: bytes) -> None:
if not 0 <= slot < firmware.V1_SIGNATURE_SLOTS:
raise ValueError("Invalid slot number")
if not 0 < key_index <= len(fw_models.TREZOR_ONE_V1V2.firmware_keys):
if not 0 < key_index <= len(fw_models.LEGACY_V1V2.firmware_keys):
raise ValueError("Invalid key index")
self.key_indexes[slot] = key_index
self.signatures[slot] = signature
@ -427,9 +427,9 @@ class LegacyFirmware(firmware.LegacyFirmware):
self, dev_keys: bool = False, signature_version: int = 2
) -> t.Sequence[bytes]:
if dev_keys:
return fw_models.TREZOR_ONE_V1V2_DEV.firmware_keys
return fw_models.LEGACY_V1V2_DEV.firmware_keys
else:
return fw_models.TREZOR_ONE_V1V2.firmware_keys
return fw_models.LEGACY_V1V2.firmware_keys
def slots(self) -> t.Iterable[int]:
return self.key_indexes
@ -467,10 +467,10 @@ class LegacyV2Firmware(firmware.LegacyV2Firmware):
self, dev_keys: bool = False, signature_version: int = 3
) -> t.Sequence[bytes]:
keymap: t.Dict[t.Tuple[int, bool], fw_models.ModelKeys] = {
(3, False): fw_models.TREZOR_ONE_V3,
(3, True): fw_models.TREZOR_ONE_V3_DEV,
(2, False): fw_models.TREZOR_ONE_V1V2,
(2, True): fw_models.TREZOR_ONE_V1V2_DEV,
(3, False): fw_models.LEGACY_V3,
(3, True): fw_models.LEGACY_V3_DEV,
(2, False): fw_models.LEGACY_V1V2,
(2, True): fw_models.LEGACY_V1V2_DEV,
}
if not (signature_version, dev_keys) in keymap:
raise ValueError("Unsupported signature version")

View File

@ -25,10 +25,10 @@ V2_CHUNK_SIZE = 1024 * 128
# === KEYS KEPT FOR COMPATIBILITY ===
# use `trezorlib.firmware.models` directly
V1_BOOTLOADER_KEYS = models.TREZOR_ONE_V1V2.firmware_keys
V2_BOARDLOADER_KEYS = models.TREZOR_T.boardloader_keys
V2_BOARDLOADER_DEV_KEYS = models.TREZOR_T_DEV.boardloader_keys
V2_BOOTLOADER_KEYS = models.TREZOR_T.bootloader_keys
V2_BOOTLOADER_DEV_KEYS = models.TREZOR_T_DEV.bootloader_keys
V1_BOOTLOADER_KEYS = models.LEGACY_V1V2.firmware_keys
V2_BOARDLOADER_KEYS = models.T2T1.boardloader_keys
V2_BOARDLOADER_DEV_KEYS = models.TREZOR_CORE_DEV.boardloader_keys
V2_BOOTLOADER_KEYS = models.T2T1.bootloader_keys
V2_BOOTLOADER_DEV_KEYS = models.TREZOR_CORE_DEV.bootloader_keys
V2_SIGS_REQUIRED = models.TREZOR_T.boardloader_sigs_needed
V2_SIGS_REQUIRED = models.T2T1.boardloader_sigs_needed

View File

@ -108,24 +108,24 @@ class LegacyV2Firmware(FirmwareImage):
def verify_v2(self, dev_keys: bool) -> None:
if not dev_keys:
public_keys = models.TREZOR_ONE_V1V2.firmware_keys
public_keys = models.LEGACY_V1V2.firmware_keys
else:
public_keys = models.TREZOR_ONE_V1V2_DEV.firmware_keys
public_keys = models.LEGACY_V1V2_DEV.firmware_keys
self.validate_code_hashes()
check_sig_v1(
self.digest(),
self.header.v1_key_indexes,
self.header.v1_signatures,
models.TREZOR_ONE_V1V2.firmware_sigs_needed,
models.LEGACY_V1V2.firmware_sigs_needed,
public_keys,
)
def verify_v3(self, dev_keys: bool) -> None:
if not dev_keys:
model_keys = models.TREZOR_ONE_V3
model_keys = models.LEGACY_V3
else:
model_keys = models.TREZOR_ONE_V3_DEV
model_keys = models.LEGACY_V3_DEV
self.validate_code_hashes()
check_sig_signmessage(
@ -191,9 +191,9 @@ class LegacyFirmware(Struct):
def verify(self, dev_keys: bool = False) -> None:
if not dev_keys:
model_keys = models.TREZOR_ONE_V1V2
model_keys = models.LEGACY_V1V2
else:
model_keys = models.TREZOR_ONE_V1V2_DEV
model_keys = models.LEGACY_V1V2_DEV
check_sig_v1(
self.digest(),
self.key_indexes,

View File

@ -23,17 +23,22 @@ if t.TYPE_CHECKING:
class Model(Enum):
ONE = b"T1B1"
T = b"T2T1"
R = b"T2B1"
T1B1 = b"T1B1"
T2T1 = b"T2T1"
T2B1 = b"T2B1"
DISC1 = b"D001"
# legacy aliases
ONE = T1B1
T = T2T1
R = T2B1
@classmethod
def from_hw_model(cls, hw_model: t.Union["Self", bytes]) -> "Self":
if isinstance(hw_model, cls):
return hw_model
if hw_model == b"\x00\x00\x00\x00":
return cls.T
return cls.T2T1
raise ValueError(f"Unknown hardware model: {hw_model}")
def model_keys(self, dev_keys: bool = False) -> "ModelKeys":
@ -57,7 +62,7 @@ class ModelKeys:
firmware_sigs_needed: int
TREZOR_ONE_V1V2 = ModelKeys(
LEGACY_V1V2 = ModelKeys(
production=True,
boardloader_keys=(),
boardloader_sigs_needed=-1,
@ -76,7 +81,7 @@ TREZOR_ONE_V1V2 = ModelKeys(
firmware_sigs_needed=3,
)
TREZOR_ONE_V1V2_DEV = ModelKeys(
LEGACY_V1V2_DEV = ModelKeys(
production=False,
boardloader_keys=(),
boardloader_sigs_needed=-1,
@ -95,7 +100,7 @@ TREZOR_ONE_V1V2_DEV = ModelKeys(
firmware_sigs_needed=3,
)
TREZOR_ONE_V3 = ModelKeys(
LEGACY_V3 = ModelKeys(
production=True,
boardloader_keys=(),
boardloader_sigs_needed=-1,
@ -112,7 +117,7 @@ TREZOR_ONE_V3 = ModelKeys(
firmware_sigs_needed=2,
)
TREZOR_ONE_V3_DEV = ModelKeys(
LEGACY_V3_DEV = ModelKeys(
production=False,
boardloader_keys=(),
boardloader_sigs_needed=-1,
@ -129,7 +134,7 @@ TREZOR_ONE_V3_DEV = ModelKeys(
firmware_sigs_needed=2,
)
TREZOR_T = ModelKeys(
T2T1 = ModelKeys(
production=True,
boardloader_keys=[
bytes.fromhex(key)
@ -153,7 +158,7 @@ TREZOR_T = ModelKeys(
firmware_sigs_needed=-1,
)
TREZOR_T_DEV = ModelKeys(
TREZOR_CORE_DEV = ModelKeys(
production=False,
boardloader_keys=[
bytes.fromhex(key)
@ -177,7 +182,7 @@ TREZOR_T_DEV = ModelKeys(
firmware_sigs_needed=-1,
)
TREZOR_R = ModelKeys(
T2B1 = ModelKeys(
production=True,
boardloader_keys=[
bytes.fromhex(key)
@ -201,20 +206,31 @@ TREZOR_R = ModelKeys(
firmware_sigs_needed=-1,
)
TREZOR_R_DEV = TREZOR_T_DEV
DISC1 = TREZOR_T_DEV
DISC1_DEV = TREZOR_T_DEV
MODEL_MAP = {
Model.ONE: TREZOR_ONE_V3,
Model.T: TREZOR_T,
Model.R: TREZOR_R,
Model.DISC1: DISC1,
Model.T1B1: LEGACY_V3,
Model.T2T1: T2T1,
Model.T2B1: T2B1,
Model.DISC1: TREZOR_CORE_DEV,
}
MODEL_MAP_DEV = {
Model.ONE: TREZOR_ONE_V3_DEV,
Model.T: TREZOR_T_DEV,
Model.R: TREZOR_R_DEV,
Model.DISC1: DISC1_DEV,
Model.T1B1: LEGACY_V3_DEV,
Model.T2T1: TREZOR_CORE_DEV,
Model.T2B1: TREZOR_CORE_DEV,
Model.DISC1: TREZOR_CORE_DEV,
}
# aliases
TREZOR_ONE_V1V2 = LEGACY_V1V2
TREZOR_ONE_V1V2_DEV = LEGACY_V1V2_DEV
TREZOR_ONE_V3 = LEGACY_V3
TREZOR_ONE_V3_DEV = LEGACY_V3_DEV
TREZOR_T = T2T1
TREZOR_R = T2B1
TREZOR_T_DEV = TREZOR_CORE_DEV
TREZOR_R_DEV = TREZOR_CORE_DEV
DISC1 = TREZOR_CORE_DEV
DISC1_DEV = TREZOR_CORE_DEV

View File

@ -34,7 +34,9 @@ class TrezorModel:
default_mapping: mapping.ProtobufMapping
TREZOR_ONE = TrezorModel(
# ==== internal names ====
T1B1 = TrezorModel(
name="1",
internal_name="T1B1",
minimum_version=(1, 8, 0),
@ -43,7 +45,7 @@ TREZOR_ONE = TrezorModel(
default_mapping=mapping.DEFAULT_MAPPING,
)
TREZOR_T = TrezorModel(
T2T1 = TrezorModel(
name="T",
internal_name="T2T1",
minimum_version=(2, 1, 0),
@ -52,7 +54,7 @@ TREZOR_T = TrezorModel(
default_mapping=mapping.DEFAULT_MAPPING,
)
TREZOR_R = TrezorModel(
T2B1 = TrezorModel(
name="Safe 3",
internal_name="T2B1",
minimum_version=(2, 1, 0),
@ -61,7 +63,7 @@ TREZOR_R = TrezorModel(
default_mapping=mapping.DEFAULT_MAPPING,
)
TREZOR_DISC1 = TrezorModel(
DISC1 = TrezorModel(
name="DISC1",
internal_name="D001",
minimum_version=(2, 1, 0),
@ -70,12 +72,20 @@ TREZOR_DISC1 = TrezorModel(
default_mapping=mapping.DEFAULT_MAPPING,
)
TREZORS = {TREZOR_ONE, TREZOR_T, TREZOR_R, TREZOR_DISC1}
# ==== model based names ====
TREZOR_ONE = T1B1
TREZOR_T = T2T1
TREZOR_R = T2B1
TREZOR_SAFE3 = T2B1
TREZOR_DISC1 = DISC1
TREZORS = {T1B1, T2T1, T2B1, DISC1}
def by_name(name: Optional[str]) -> Optional[TrezorModel]:
if name is None:
return TREZOR_ONE
return T1B1
for model in TREZORS:
if model.name == name:
return model