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:
parent
a16fbe6237
commit
a4f378e565
@ -8,7 +8,7 @@ import click
|
|||||||
import ecdsa
|
import ecdsa
|
||||||
|
|
||||||
from trezorlib.firmware.legacy import LegacyV2Firmware
|
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 = [
|
SECRET_KEYS = [
|
||||||
ecdsa.SigningKey.from_string(bytes.fromhex(sk), curve=ecdsa.SECP256k1)
|
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]
|
PUBLIC_KEYS: list[ecdsa.VerifyingKey] = [sk.get_verifying_key() for sk in SECRET_KEYS]
|
||||||
|
|
||||||
# Should be these public keys
|
# Should be these public keys
|
||||||
assert [
|
assert [pk.to_string("compressed") for pk in PUBLIC_KEYS] == LEGACY_V3_DEV.firmware_keys
|
||||||
pk.to_string("compressed") for pk in PUBLIC_KEYS
|
|
||||||
] == TREZOR_ONE_V3_DEV.firmware_keys
|
|
||||||
|
|
||||||
|
|
||||||
def signmessage(digest: bytes, key: ecdsa.SigningKey) -> bytes:
|
def signmessage(digest: bytes, key: ecdsa.SigningKey) -> bytes:
|
||||||
|
1
python/.changelog.d/+13d8ef2d.changed
Normal file
1
python/.changelog.d/+13d8ef2d.changed
Normal file
@ -0,0 +1 @@
|
|||||||
|
Internal names are used consistently in constants and names. Original model-based names are kept as aliases for backwards compatibility.
|
@ -405,7 +405,7 @@ class LegacyFirmware(firmware.LegacyFirmware):
|
|||||||
def insert_signature(self, slot: int, key_index: int, signature: bytes) -> None:
|
def insert_signature(self, slot: int, key_index: int, signature: bytes) -> None:
|
||||||
if not 0 <= slot < firmware.V1_SIGNATURE_SLOTS:
|
if not 0 <= slot < firmware.V1_SIGNATURE_SLOTS:
|
||||||
raise ValueError("Invalid slot number")
|
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")
|
raise ValueError("Invalid key index")
|
||||||
self.key_indexes[slot] = key_index
|
self.key_indexes[slot] = key_index
|
||||||
self.signatures[slot] = signature
|
self.signatures[slot] = signature
|
||||||
@ -427,9 +427,9 @@ class LegacyFirmware(firmware.LegacyFirmware):
|
|||||||
self, dev_keys: bool = False, signature_version: int = 2
|
self, dev_keys: bool = False, signature_version: int = 2
|
||||||
) -> t.Sequence[bytes]:
|
) -> t.Sequence[bytes]:
|
||||||
if dev_keys:
|
if dev_keys:
|
||||||
return fw_models.TREZOR_ONE_V1V2_DEV.firmware_keys
|
return fw_models.LEGACY_V1V2_DEV.firmware_keys
|
||||||
else:
|
else:
|
||||||
return fw_models.TREZOR_ONE_V1V2.firmware_keys
|
return fw_models.LEGACY_V1V2.firmware_keys
|
||||||
|
|
||||||
def slots(self) -> t.Iterable[int]:
|
def slots(self) -> t.Iterable[int]:
|
||||||
return self.key_indexes
|
return self.key_indexes
|
||||||
@ -467,10 +467,10 @@ class LegacyV2Firmware(firmware.LegacyV2Firmware):
|
|||||||
self, dev_keys: bool = False, signature_version: int = 3
|
self, dev_keys: bool = False, signature_version: int = 3
|
||||||
) -> t.Sequence[bytes]:
|
) -> t.Sequence[bytes]:
|
||||||
keymap: t.Dict[t.Tuple[int, bool], fw_models.ModelKeys] = {
|
keymap: t.Dict[t.Tuple[int, bool], fw_models.ModelKeys] = {
|
||||||
(3, False): fw_models.TREZOR_ONE_V3,
|
(3, False): fw_models.LEGACY_V3,
|
||||||
(3, True): fw_models.TREZOR_ONE_V3_DEV,
|
(3, True): fw_models.LEGACY_V3_DEV,
|
||||||
(2, False): fw_models.TREZOR_ONE_V1V2,
|
(2, False): fw_models.LEGACY_V1V2,
|
||||||
(2, True): fw_models.TREZOR_ONE_V1V2_DEV,
|
(2, True): fw_models.LEGACY_V1V2_DEV,
|
||||||
}
|
}
|
||||||
if not (signature_version, dev_keys) in keymap:
|
if not (signature_version, dev_keys) in keymap:
|
||||||
raise ValueError("Unsupported signature version")
|
raise ValueError("Unsupported signature version")
|
||||||
|
@ -25,10 +25,10 @@ V2_CHUNK_SIZE = 1024 * 128
|
|||||||
# === KEYS KEPT FOR COMPATIBILITY ===
|
# === KEYS KEPT FOR COMPATIBILITY ===
|
||||||
# use `trezorlib.firmware.models` directly
|
# use `trezorlib.firmware.models` directly
|
||||||
|
|
||||||
V1_BOOTLOADER_KEYS = models.TREZOR_ONE_V1V2.firmware_keys
|
V1_BOOTLOADER_KEYS = models.LEGACY_V1V2.firmware_keys
|
||||||
V2_BOARDLOADER_KEYS = models.TREZOR_T.boardloader_keys
|
V2_BOARDLOADER_KEYS = models.T2T1.boardloader_keys
|
||||||
V2_BOARDLOADER_DEV_KEYS = models.TREZOR_T_DEV.boardloader_keys
|
V2_BOARDLOADER_DEV_KEYS = models.TREZOR_CORE_DEV.boardloader_keys
|
||||||
V2_BOOTLOADER_KEYS = models.TREZOR_T.bootloader_keys
|
V2_BOOTLOADER_KEYS = models.T2T1.bootloader_keys
|
||||||
V2_BOOTLOADER_DEV_KEYS = models.TREZOR_T_DEV.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
|
||||||
|
@ -108,24 +108,24 @@ class LegacyV2Firmware(FirmwareImage):
|
|||||||
|
|
||||||
def verify_v2(self, dev_keys: bool) -> None:
|
def verify_v2(self, dev_keys: bool) -> None:
|
||||||
if not dev_keys:
|
if not dev_keys:
|
||||||
public_keys = models.TREZOR_ONE_V1V2.firmware_keys
|
public_keys = models.LEGACY_V1V2.firmware_keys
|
||||||
else:
|
else:
|
||||||
public_keys = models.TREZOR_ONE_V1V2_DEV.firmware_keys
|
public_keys = models.LEGACY_V1V2_DEV.firmware_keys
|
||||||
|
|
||||||
self.validate_code_hashes()
|
self.validate_code_hashes()
|
||||||
check_sig_v1(
|
check_sig_v1(
|
||||||
self.digest(),
|
self.digest(),
|
||||||
self.header.v1_key_indexes,
|
self.header.v1_key_indexes,
|
||||||
self.header.v1_signatures,
|
self.header.v1_signatures,
|
||||||
models.TREZOR_ONE_V1V2.firmware_sigs_needed,
|
models.LEGACY_V1V2.firmware_sigs_needed,
|
||||||
public_keys,
|
public_keys,
|
||||||
)
|
)
|
||||||
|
|
||||||
def verify_v3(self, dev_keys: bool) -> None:
|
def verify_v3(self, dev_keys: bool) -> None:
|
||||||
if not dev_keys:
|
if not dev_keys:
|
||||||
model_keys = models.TREZOR_ONE_V3
|
model_keys = models.LEGACY_V3
|
||||||
else:
|
else:
|
||||||
model_keys = models.TREZOR_ONE_V3_DEV
|
model_keys = models.LEGACY_V3_DEV
|
||||||
|
|
||||||
self.validate_code_hashes()
|
self.validate_code_hashes()
|
||||||
check_sig_signmessage(
|
check_sig_signmessage(
|
||||||
@ -191,9 +191,9 @@ class LegacyFirmware(Struct):
|
|||||||
|
|
||||||
def verify(self, dev_keys: bool = False) -> None:
|
def verify(self, dev_keys: bool = False) -> None:
|
||||||
if not dev_keys:
|
if not dev_keys:
|
||||||
model_keys = models.TREZOR_ONE_V1V2
|
model_keys = models.LEGACY_V1V2
|
||||||
else:
|
else:
|
||||||
model_keys = models.TREZOR_ONE_V1V2_DEV
|
model_keys = models.LEGACY_V1V2_DEV
|
||||||
check_sig_v1(
|
check_sig_v1(
|
||||||
self.digest(),
|
self.digest(),
|
||||||
self.key_indexes,
|
self.key_indexes,
|
||||||
|
@ -23,17 +23,22 @@ if t.TYPE_CHECKING:
|
|||||||
|
|
||||||
|
|
||||||
class Model(Enum):
|
class Model(Enum):
|
||||||
ONE = b"T1B1"
|
T1B1 = b"T1B1"
|
||||||
T = b"T2T1"
|
T2T1 = b"T2T1"
|
||||||
R = b"T2B1"
|
T2B1 = b"T2B1"
|
||||||
DISC1 = b"D001"
|
DISC1 = b"D001"
|
||||||
|
|
||||||
|
# legacy aliases
|
||||||
|
ONE = T1B1
|
||||||
|
T = T2T1
|
||||||
|
R = T2B1
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_hw_model(cls, hw_model: t.Union["Self", bytes]) -> "Self":
|
def from_hw_model(cls, hw_model: t.Union["Self", bytes]) -> "Self":
|
||||||
if isinstance(hw_model, cls):
|
if isinstance(hw_model, cls):
|
||||||
return hw_model
|
return hw_model
|
||||||
if hw_model == b"\x00\x00\x00\x00":
|
if hw_model == b"\x00\x00\x00\x00":
|
||||||
return cls.T
|
return cls.T2T1
|
||||||
raise ValueError(f"Unknown hardware model: {hw_model}")
|
raise ValueError(f"Unknown hardware model: {hw_model}")
|
||||||
|
|
||||||
def model_keys(self, dev_keys: bool = False) -> "ModelKeys":
|
def model_keys(self, dev_keys: bool = False) -> "ModelKeys":
|
||||||
@ -57,7 +62,7 @@ class ModelKeys:
|
|||||||
firmware_sigs_needed: int
|
firmware_sigs_needed: int
|
||||||
|
|
||||||
|
|
||||||
TREZOR_ONE_V1V2 = ModelKeys(
|
LEGACY_V1V2 = ModelKeys(
|
||||||
production=True,
|
production=True,
|
||||||
boardloader_keys=(),
|
boardloader_keys=(),
|
||||||
boardloader_sigs_needed=-1,
|
boardloader_sigs_needed=-1,
|
||||||
@ -76,7 +81,7 @@ TREZOR_ONE_V1V2 = ModelKeys(
|
|||||||
firmware_sigs_needed=3,
|
firmware_sigs_needed=3,
|
||||||
)
|
)
|
||||||
|
|
||||||
TREZOR_ONE_V1V2_DEV = ModelKeys(
|
LEGACY_V1V2_DEV = ModelKeys(
|
||||||
production=False,
|
production=False,
|
||||||
boardloader_keys=(),
|
boardloader_keys=(),
|
||||||
boardloader_sigs_needed=-1,
|
boardloader_sigs_needed=-1,
|
||||||
@ -95,7 +100,7 @@ TREZOR_ONE_V1V2_DEV = ModelKeys(
|
|||||||
firmware_sigs_needed=3,
|
firmware_sigs_needed=3,
|
||||||
)
|
)
|
||||||
|
|
||||||
TREZOR_ONE_V3 = ModelKeys(
|
LEGACY_V3 = ModelKeys(
|
||||||
production=True,
|
production=True,
|
||||||
boardloader_keys=(),
|
boardloader_keys=(),
|
||||||
boardloader_sigs_needed=-1,
|
boardloader_sigs_needed=-1,
|
||||||
@ -112,7 +117,7 @@ TREZOR_ONE_V3 = ModelKeys(
|
|||||||
firmware_sigs_needed=2,
|
firmware_sigs_needed=2,
|
||||||
)
|
)
|
||||||
|
|
||||||
TREZOR_ONE_V3_DEV = ModelKeys(
|
LEGACY_V3_DEV = ModelKeys(
|
||||||
production=False,
|
production=False,
|
||||||
boardloader_keys=(),
|
boardloader_keys=(),
|
||||||
boardloader_sigs_needed=-1,
|
boardloader_sigs_needed=-1,
|
||||||
@ -129,7 +134,7 @@ TREZOR_ONE_V3_DEV = ModelKeys(
|
|||||||
firmware_sigs_needed=2,
|
firmware_sigs_needed=2,
|
||||||
)
|
)
|
||||||
|
|
||||||
TREZOR_T = ModelKeys(
|
T2T1 = ModelKeys(
|
||||||
production=True,
|
production=True,
|
||||||
boardloader_keys=[
|
boardloader_keys=[
|
||||||
bytes.fromhex(key)
|
bytes.fromhex(key)
|
||||||
@ -153,7 +158,7 @@ TREZOR_T = ModelKeys(
|
|||||||
firmware_sigs_needed=-1,
|
firmware_sigs_needed=-1,
|
||||||
)
|
)
|
||||||
|
|
||||||
TREZOR_T_DEV = ModelKeys(
|
TREZOR_CORE_DEV = ModelKeys(
|
||||||
production=False,
|
production=False,
|
||||||
boardloader_keys=[
|
boardloader_keys=[
|
||||||
bytes.fromhex(key)
|
bytes.fromhex(key)
|
||||||
@ -177,7 +182,7 @@ TREZOR_T_DEV = ModelKeys(
|
|||||||
firmware_sigs_needed=-1,
|
firmware_sigs_needed=-1,
|
||||||
)
|
)
|
||||||
|
|
||||||
TREZOR_R = ModelKeys(
|
T2B1 = ModelKeys(
|
||||||
production=True,
|
production=True,
|
||||||
boardloader_keys=[
|
boardloader_keys=[
|
||||||
bytes.fromhex(key)
|
bytes.fromhex(key)
|
||||||
@ -201,20 +206,31 @@ TREZOR_R = ModelKeys(
|
|||||||
firmware_sigs_needed=-1,
|
firmware_sigs_needed=-1,
|
||||||
)
|
)
|
||||||
|
|
||||||
TREZOR_R_DEV = TREZOR_T_DEV
|
|
||||||
DISC1 = TREZOR_T_DEV
|
|
||||||
DISC1_DEV = TREZOR_T_DEV
|
|
||||||
|
|
||||||
MODEL_MAP = {
|
MODEL_MAP = {
|
||||||
Model.ONE: TREZOR_ONE_V3,
|
Model.T1B1: LEGACY_V3,
|
||||||
Model.T: TREZOR_T,
|
Model.T2T1: T2T1,
|
||||||
Model.R: TREZOR_R,
|
Model.T2B1: T2B1,
|
||||||
Model.DISC1: DISC1,
|
Model.DISC1: TREZOR_CORE_DEV,
|
||||||
}
|
}
|
||||||
|
|
||||||
MODEL_MAP_DEV = {
|
MODEL_MAP_DEV = {
|
||||||
Model.ONE: TREZOR_ONE_V3_DEV,
|
Model.T1B1: LEGACY_V3_DEV,
|
||||||
Model.T: TREZOR_T_DEV,
|
Model.T2T1: TREZOR_CORE_DEV,
|
||||||
Model.R: TREZOR_R_DEV,
|
Model.T2B1: TREZOR_CORE_DEV,
|
||||||
Model.DISC1: DISC1_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
|
||||||
|
@ -34,7 +34,9 @@ class TrezorModel:
|
|||||||
default_mapping: mapping.ProtobufMapping
|
default_mapping: mapping.ProtobufMapping
|
||||||
|
|
||||||
|
|
||||||
TREZOR_ONE = TrezorModel(
|
# ==== internal names ====
|
||||||
|
|
||||||
|
T1B1 = TrezorModel(
|
||||||
name="1",
|
name="1",
|
||||||
internal_name="T1B1",
|
internal_name="T1B1",
|
||||||
minimum_version=(1, 8, 0),
|
minimum_version=(1, 8, 0),
|
||||||
@ -43,7 +45,7 @@ TREZOR_ONE = TrezorModel(
|
|||||||
default_mapping=mapping.DEFAULT_MAPPING,
|
default_mapping=mapping.DEFAULT_MAPPING,
|
||||||
)
|
)
|
||||||
|
|
||||||
TREZOR_T = TrezorModel(
|
T2T1 = TrezorModel(
|
||||||
name="T",
|
name="T",
|
||||||
internal_name="T2T1",
|
internal_name="T2T1",
|
||||||
minimum_version=(2, 1, 0),
|
minimum_version=(2, 1, 0),
|
||||||
@ -52,7 +54,7 @@ TREZOR_T = TrezorModel(
|
|||||||
default_mapping=mapping.DEFAULT_MAPPING,
|
default_mapping=mapping.DEFAULT_MAPPING,
|
||||||
)
|
)
|
||||||
|
|
||||||
TREZOR_R = TrezorModel(
|
T2B1 = TrezorModel(
|
||||||
name="Safe 3",
|
name="Safe 3",
|
||||||
internal_name="T2B1",
|
internal_name="T2B1",
|
||||||
minimum_version=(2, 1, 0),
|
minimum_version=(2, 1, 0),
|
||||||
@ -61,7 +63,7 @@ TREZOR_R = TrezorModel(
|
|||||||
default_mapping=mapping.DEFAULT_MAPPING,
|
default_mapping=mapping.DEFAULT_MAPPING,
|
||||||
)
|
)
|
||||||
|
|
||||||
TREZOR_DISC1 = TrezorModel(
|
DISC1 = TrezorModel(
|
||||||
name="DISC1",
|
name="DISC1",
|
||||||
internal_name="D001",
|
internal_name="D001",
|
||||||
minimum_version=(2, 1, 0),
|
minimum_version=(2, 1, 0),
|
||||||
@ -70,12 +72,20 @@ TREZOR_DISC1 = TrezorModel(
|
|||||||
default_mapping=mapping.DEFAULT_MAPPING,
|
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]:
|
def by_name(name: Optional[str]) -> Optional[TrezorModel]:
|
||||||
if name is None:
|
if name is None:
|
||||||
return TREZOR_ONE
|
return T1B1
|
||||||
for model in TREZORS:
|
for model in TREZORS:
|
||||||
if model.name == name:
|
if model.name == name:
|
||||||
return model
|
return model
|
||||||
|
Loading…
Reference in New Issue
Block a user