1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-15 20:19:23 +00:00

feat(core/sdbackup): do not pass strings around.

This commit is contained in:
obrusvit 2023-12-08 18:16:24 +01:00
parent b2d820c273
commit 031df25956
6 changed files with 49 additions and 32 deletions

View File

@ -3,6 +3,7 @@ from typing import TYPE_CHECKING
import storage.device as storage_device
import storage.recovery as storage_recovery
import storage.recovery_shares as storage_recovery_shares
from storage.sd_seed_backup import BackupMedium
from trezor import wire
from trezor.messages import Success
@ -41,17 +42,16 @@ async def recovery_process() -> Success:
raise wire.ActionCancelled
async def _choose_backup_medium() -> str:
async def _choose_backup_medium() -> BackupMedium:
from trezor import utils
if utils.USE_SD_CARD:
from apps.management.sd_backup import bip39_choose_backup_medium
# ask the user for backup type (words/SD card)
backup_medium: str = await bip39_choose_backup_medium(recovery=True)
return await bip39_choose_backup_medium(recovery=True)
else:
backup_medium: str = "words"
return backup_medium
return BackupMedium.Words
async def _continue_recovery_process() -> Success:
@ -78,12 +78,11 @@ async def _continue_recovery_process() -> Success:
secret = None
words = None
recovered_from_sd = None
backup_medium = "words"
backup_medium = BackupMedium.Words
while secret is None:
if is_first_step:
backup_medium: str = await _choose_backup_medium()
if utils.USE_SD_CARD and backup_medium == "sdcard":
backup_medium = await _choose_backup_medium()
if utils.USE_SD_CARD and backup_medium == BackupMedium.SDCard:
# attempt to recover words from sd card
words, backup_type = await sdcard_recover_seed()
if words is None:
@ -108,7 +107,7 @@ async def _continue_recovery_process() -> Success:
await _request_share_first_screen(word_count)
assert word_count is not None
if backup_medium == "words":
if backup_medium == BackupMedium.Words:
# ask for mnemonic words one by one
words = await layout.request_mnemonic(word_count, backup_type)

View File

@ -213,15 +213,16 @@ def _compute_secret_from_entropy(
return secret
async def _backup_bip39_sdcard(mnemonic: bytes, bak_t: BackupType) -> None:
async def _backup_bip39_sdcard(mnemonic: bytes, backup_type: BackupType) -> None:
from apps.management.sd_backup import sdcard_backup_seed
backup_success: bool = await sdcard_backup_seed(mnemonic, bak_t)
backup_success: bool = await sdcard_backup_seed(mnemonic, backup_type)
if not backup_success:
raise ProcessError("SD Card backup could not be verified.")
async def backup_seed(backup_type: BackupType, mnemonic_secret: bytes) -> None:
from storage.sd_seed_backup import BackupMedium
from trezor import utils
if backup_type == BAK_T_SLIP39_BASIC:
@ -232,12 +233,13 @@ async def backup_seed(backup_type: BackupType, mnemonic_secret: bytes) -> None:
if utils.USE_SD_CARD:
from apps.management.sd_backup import bip39_choose_backup_medium
backup_medium: str = await bip39_choose_backup_medium()
backup_medium = await bip39_choose_backup_medium()
else:
backup_medium: str = "words"
if backup_medium == "sdcard":
backup_medium = BackupMedium.Words
if backup_medium == BackupMedium.SDCard:
await _backup_bip39_sdcard(mnemonic_secret, backup_type)
elif backup_medium == "words":
elif backup_medium == BackupMedium.Words:
await layout.bip39_show_and_confirm_mnemonic(mnemonic_secret.decode())
else:
raise ProcessError("Invalid backup medium.")

View File

@ -1,12 +1,12 @@
from storage.sd_seed_backup import recover_seed_from_sdcard, store_seed_on_sdcard
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from storage.sd_seed_backup import BackupMedium
from trezor.enums import BackupType
async def bip39_choose_backup_medium(recovery: bool = False) -> str:
async def bip39_choose_backup_medium(recovery: bool = False) -> BackupMedium:
# TODO this will be general, not only for BIP39
from trezor.ui.layouts import choose_backup_medium
@ -15,6 +15,7 @@ async def bip39_choose_backup_medium(recovery: bool = False) -> str:
async def sdcard_backup_seed(mnemonic_secret: bytes, bak_t: BackupType) -> bool:
from apps.common.sdcard import ensure_sdcard
from storage.sd_seed_backup import store_seed_on_sdcard
await ensure_sdcard(ensure_filesystem=True, for_sd_backup=True)
return store_seed_on_sdcard(mnemonic_secret, bak_t)
@ -22,6 +23,7 @@ async def sdcard_backup_seed(mnemonic_secret: bytes, bak_t: BackupType) -> bool:
async def sdcard_recover_seed() -> tuple[str | None, BackupType | None]:
from apps.common.sdcard import ensure_sdcard
from storage.sd_seed_backup import recover_seed_from_sdcard
await ensure_sdcard(ensure_filesystem=False)
mnemonic_bytes, backup_type = recover_seed_from_sdcard()

View File

@ -3,13 +3,16 @@ from trezorcrypto import sha256
from typing import TYPE_CHECKING
from trezor import io, utils
from trezor.enums import BackupType
from trezor.sdcard import with_filesystem, with_sdcard
from trezor.wire import ProcessError
from trezor.wire import DataError, ProcessError
if TYPE_CHECKING:
from enum import IntEnum
from typing import Generator
from trezor.enums import BackupType
from trezor.utils import BufferReader
else:
IntEnum = object
if utils.USE_SD_CARD:
fatfs = io.fatfs # global_import_cache
@ -22,6 +25,11 @@ if utils.USE_SD_CARD:
SDBACKUP_VERSION = b"0"
class BackupMedium(IntEnum):
Words = 0
SDCard = 1
@with_filesystem
def store_seed_on_sdcard(mnemonic_secret: bytes, backup_type: BackupType) -> bool:
_write_seed_unalloc(mnemonic_secret, backup_type)
@ -64,7 +72,6 @@ def _read_seed_unalloc() -> tuple[bytes | None, BackupType | None]:
if restored_block is None:
return (None, None)
decoded_mnemonic, decoded_backup_type = restored_block
# decoded_mnemonic_str = decoded_mnemonic.decode("utf-8").rstrip("\x00")
return (decoded_mnemonic, decoded_backup_type)
@ -120,9 +127,7 @@ HASH_LEN = const(32)
def _encode_backup_block(mnemonic: bytes, backup_type: BackupType) -> bytes:
from trezor.utils import empty_bytearray
ret = empty_bytearray(SDCARD_BLOCK_SIZE_B)
ret = utils.empty_bytearray(SDCARD_BLOCK_SIZE_B)
ret.extend(SDBACKUP_MAGIC)
ret.extend(SDBACKUP_VERSION)
ret.extend(backup_type.to_bytes(BACKUPTYPE_LEN, "big"))
@ -154,8 +159,14 @@ def _decode_backup_block(block: bytes) -> tuple[bytes, BackupType] | None:
MAGIC_LEN + VERSION_LEN + BACKUPTYPE_LEN + SEEDLEN_LEN + seed_len
)
).digest()
if blockhash_read == blockhash_expected:
return (mnemonic, backup_type)
if blockhash_read == blockhash_expected and backup_type in (0, 1, 2):
if backup_type == 0:
res_bt = BackupType.Bip39
elif backup_type == 1:
res_bt = BackupType.Slip39_Basic
else:
res_bt = BackupType.Slip39_Advanced
return (mnemonic, res_bt)
else:
return None

View File

@ -27,8 +27,7 @@ if TYPE_CHECKING:
def is_trz_card() -> bool:
sdcard.capacity()
pass
return True
class FilesystemWrapper:

View File

@ -11,6 +11,8 @@ from ..common import button_request, interact
if TYPE_CHECKING:
from typing import Any, Awaitable, Iterable, NoReturn, Sequence, TypeVar
from storage.sd_seed_backup import BackupMedium
from ..common import ExceptionType, PropertyType
T = TypeVar("T")
@ -575,8 +577,10 @@ async def show_success(
)
async def choose_backup_medium(recovery: bool = False) -> str:
async def choose_backup_medium(recovery: bool = False) -> BackupMedium:
# TODO what br type
from storage.sd_seed_backup import BackupMedium
br_type = "br_type"
if recovery:
br_code: ButtonRequestType = ButtonRequestType.RecoveryHomepage
@ -599,9 +603,9 @@ async def choose_backup_medium(recovery: bool = False) -> str:
br_code,
)
if result is CONFIRMED:
return "sdcard"
return BackupMedium.SDCard
else:
return "words"
return BackupMedium.Words
async def confirm_output(