2019-09-26 10:42:20 +00:00
|
|
|
# This file is part of the Trezor project.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012-2019 SatoshiLabs and contributors
|
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the License along with this library.
|
|
|
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
|
|
|
|
|
|
|
|
|
|
|
import pytest
|
2021-02-03 12:39:26 +00:00
|
|
|
from shamir_mnemonic import shamir
|
2019-09-26 10:42:20 +00:00
|
|
|
|
2023-05-04 12:18:55 +00:00
|
|
|
from trezorlib import device
|
2022-01-31 12:25:30 +00:00
|
|
|
from trezorlib.debuglink import TrezorClientDebugLink as Client
|
2024-05-23 14:09:16 +00:00
|
|
|
from trezorlib.messages import BackupAvailability, BackupType
|
2019-09-26 10:42:20 +00:00
|
|
|
|
2023-05-04 12:18:55 +00:00
|
|
|
from ...common import WITH_MOCK_URANDOM
|
|
|
|
from ...input_flows import (
|
|
|
|
InputFlowBip39Backup,
|
|
|
|
InputFlowResetSkipBackup,
|
|
|
|
InputFlowSlip39AdvancedBackup,
|
|
|
|
InputFlowSlip39BasicBackup,
|
|
|
|
)
|
2019-09-26 10:42:20 +00:00
|
|
|
|
|
|
|
|
2023-05-04 12:18:55 +00:00
|
|
|
def backup_flow_bip39(client: Client) -> bytes:
|
2019-09-26 10:42:20 +00:00
|
|
|
with client:
|
2023-05-04 12:18:55 +00:00
|
|
|
IF = InputFlowBip39Backup(client)
|
|
|
|
client.set_input_flow(IF.get())
|
2019-09-26 10:42:20 +00:00
|
|
|
device.backup(client)
|
|
|
|
|
2023-05-04 12:18:55 +00:00
|
|
|
assert IF.mnemonic is not None
|
|
|
|
return IF.mnemonic.encode()
|
2019-09-26 10:42:20 +00:00
|
|
|
|
|
|
|
|
2022-01-31 12:25:30 +00:00
|
|
|
def backup_flow_slip39_basic(client: Client):
|
2019-09-26 10:42:20 +00:00
|
|
|
with client:
|
2023-05-04 12:18:55 +00:00
|
|
|
IF = InputFlowSlip39BasicBackup(client, False)
|
|
|
|
client.set_input_flow(IF.get())
|
2019-09-26 10:42:20 +00:00
|
|
|
device.backup(client)
|
|
|
|
|
2023-05-04 12:18:55 +00:00
|
|
|
groups = shamir.decode_mnemonics(IF.mnemonics[:3])
|
2021-02-03 12:39:26 +00:00
|
|
|
ems = shamir.recover_ems(groups)
|
|
|
|
return ems.ciphertext
|
2019-09-26 10:42:20 +00:00
|
|
|
|
|
|
|
|
2022-01-31 12:25:30 +00:00
|
|
|
def backup_flow_slip39_advanced(client: Client):
|
2019-09-26 10:42:20 +00:00
|
|
|
with client:
|
2023-05-04 12:18:55 +00:00
|
|
|
IF = InputFlowSlip39AdvancedBackup(client, False)
|
|
|
|
client.set_input_flow(IF.get())
|
2019-09-26 10:42:20 +00:00
|
|
|
device.backup(client)
|
|
|
|
|
2023-05-04 12:18:55 +00:00
|
|
|
mnemonics = IF.mnemonics[0:3] + IF.mnemonics[5:8] + IF.mnemonics[10:13]
|
2021-02-03 12:39:26 +00:00
|
|
|
groups = shamir.decode_mnemonics(mnemonics)
|
|
|
|
ems = shamir.recover_ems(groups)
|
|
|
|
return ems.ciphertext
|
2019-09-26 10:42:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
VECTORS = [
|
|
|
|
(BackupType.Bip39, backup_flow_bip39),
|
2024-05-17 21:08:23 +00:00
|
|
|
(BackupType.Slip39_Basic_Extendable, backup_flow_slip39_basic),
|
|
|
|
(BackupType.Slip39_Advanced_Extendable, backup_flow_slip39_advanced),
|
2019-09-26 10:42:20 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
@pytest.mark.models("core")
|
2019-09-26 10:42:20 +00:00
|
|
|
@pytest.mark.parametrize("backup_type, backup_flow", VECTORS)
|
2020-01-07 09:16:08 +00:00
|
|
|
@pytest.mark.setup_client(uninitialized=True)
|
2022-01-31 12:25:30 +00:00
|
|
|
def test_skip_backup_msg(client: Client, backup_type, backup_flow):
|
2023-05-04 12:18:55 +00:00
|
|
|
with WITH_MOCK_URANDOM, client:
|
2019-12-09 16:01:04 +00:00
|
|
|
device.reset(
|
|
|
|
client,
|
|
|
|
skip_backup=True,
|
|
|
|
passphrase_protection=False,
|
|
|
|
pin_protection=False,
|
|
|
|
backup_type=backup_type,
|
|
|
|
)
|
2019-09-26 10:42:20 +00:00
|
|
|
|
|
|
|
assert client.features.initialized is True
|
2024-05-23 14:09:16 +00:00
|
|
|
assert client.features.backup_availability == BackupAvailability.Required
|
2019-09-26 10:42:20 +00:00
|
|
|
assert client.features.unfinished_backup is False
|
|
|
|
assert client.features.no_backup is False
|
|
|
|
assert client.features.backup_type is backup_type
|
|
|
|
|
|
|
|
secret = backup_flow(client)
|
|
|
|
|
|
|
|
client.init_device()
|
|
|
|
assert client.features.initialized is True
|
2024-05-23 14:09:16 +00:00
|
|
|
assert client.features.backup_availability == BackupAvailability.NotAvailable
|
2019-09-26 10:42:20 +00:00
|
|
|
assert client.features.unfinished_backup is False
|
|
|
|
assert client.features.backup_type is backup_type
|
|
|
|
|
|
|
|
assert secret is not None
|
|
|
|
state = client.debug.state()
|
|
|
|
assert state.mnemonic_type is backup_type
|
|
|
|
assert state.mnemonic_secret == secret
|
|
|
|
|
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
@pytest.mark.models("core")
|
2019-09-26 10:42:20 +00:00
|
|
|
@pytest.mark.parametrize("backup_type, backup_flow", VECTORS)
|
2020-01-07 09:16:08 +00:00
|
|
|
@pytest.mark.setup_client(uninitialized=True)
|
2023-06-26 08:50:28 +00:00
|
|
|
def test_skip_backup_manual(client: Client, backup_type: BackupType, backup_flow):
|
2023-05-04 12:18:55 +00:00
|
|
|
with WITH_MOCK_URANDOM, client:
|
|
|
|
IF = InputFlowResetSkipBackup(client)
|
|
|
|
client.set_input_flow(IF.get())
|
2019-09-26 10:42:20 +00:00
|
|
|
device.reset(
|
|
|
|
client,
|
|
|
|
pin_protection=False,
|
|
|
|
passphrase_protection=False,
|
|
|
|
backup_type=backup_type,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert client.features.initialized is True
|
2024-05-23 14:09:16 +00:00
|
|
|
assert client.features.backup_availability == BackupAvailability.Required
|
2019-09-26 10:42:20 +00:00
|
|
|
assert client.features.unfinished_backup is False
|
|
|
|
assert client.features.no_backup is False
|
|
|
|
assert client.features.backup_type is backup_type
|
|
|
|
|
|
|
|
secret = backup_flow(client)
|
|
|
|
|
|
|
|
client.init_device()
|
|
|
|
assert client.features.initialized is True
|
2024-05-23 14:09:16 +00:00
|
|
|
assert client.features.backup_availability == BackupAvailability.NotAvailable
|
2019-09-26 10:42:20 +00:00
|
|
|
assert client.features.unfinished_backup is False
|
|
|
|
assert client.features.backup_type is backup_type
|
|
|
|
|
|
|
|
assert secret is not None
|
|
|
|
state = client.debug.state()
|
|
|
|
assert state.mnemonic_type is backup_type
|
|
|
|
assert state.mnemonic_secret == secret
|