2019-09-20 15:11:24 +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
|
2019-09-20 15:39:25 +00:00
|
|
|
import shamir_mnemonic as shamir
|
2019-09-20 15:11:24 +00:00
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
from trezorlib import device, messages
|
|
|
|
from trezorlib.debuglink import LayoutType
|
2022-01-31 12:25:30 +00:00
|
|
|
from trezorlib.debuglink import TrezorClientDebugLink as Client
|
2019-09-23 12:49:54 +00:00
|
|
|
from trezorlib.exceptions import TrezorFailure
|
2019-09-20 15:11:24 +00:00
|
|
|
|
2019-09-20 15:39:25 +00:00
|
|
|
from ..common import (
|
|
|
|
MNEMONIC12,
|
2019-09-23 12:49:54 +00:00
|
|
|
MNEMONIC_SLIP39_ADVANCED_20,
|
2024-04-11 14:21:17 +00:00
|
|
|
MNEMONIC_SLIP39_CUSTOM_SECRET,
|
2024-12-06 09:57:56 +00:00
|
|
|
MNEMONIC_SLIP39_SINGLE_EXT_20,
|
2019-09-20 15:39:25 +00:00
|
|
|
MNEMONIC_SLIP39_BASIC_20_3of6,
|
2024-04-11 14:21:17 +00:00
|
|
|
MNEMONIC_SLIP39_CUSTOM_1of1,
|
2019-09-20 15:39:25 +00:00
|
|
|
)
|
2023-05-04 12:18:55 +00:00
|
|
|
from ..input_flows import (
|
|
|
|
InputFlowBip39Backup,
|
|
|
|
InputFlowSlip39AdvancedBackup,
|
|
|
|
InputFlowSlip39BasicBackup,
|
2024-04-11 14:21:17 +00:00
|
|
|
InputFlowSlip39CustomBackup,
|
2023-05-04 12:18:55 +00:00
|
|
|
)
|
2022-01-11 14:49:33 +00:00
|
|
|
|
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
@pytest.mark.models("core") # TODO we want this for t1 too
|
2019-11-13 11:47:51 +00:00
|
|
|
@pytest.mark.setup_client(needs_backup=True, mnemonic=MNEMONIC12)
|
2022-01-31 12:25:30 +00:00
|
|
|
def test_backup_bip39(client: Client):
|
2024-05-23 14:09:16 +00:00
|
|
|
assert client.features.backup_availability == messages.BackupAvailability.Required
|
2019-09-20 15:11:24 +00:00
|
|
|
|
|
|
|
with client:
|
2023-05-04 12:18:55 +00:00
|
|
|
IF = InputFlowBip39Backup(client)
|
|
|
|
client.set_input_flow(IF.get())
|
2019-09-20 15:11:24 +00:00
|
|
|
device.backup(client)
|
|
|
|
|
2023-05-04 12:18:55 +00:00
|
|
|
assert IF.mnemonic == MNEMONIC12
|
2019-09-20 16:06:02 +00:00
|
|
|
client.init_device()
|
2019-09-23 12:49:54 +00:00
|
|
|
assert client.features.initialized is True
|
2024-05-23 14:09:16 +00:00
|
|
|
assert (
|
|
|
|
client.features.backup_availability == messages.BackupAvailability.NotAvailable
|
|
|
|
)
|
2019-09-23 12:49:54 +00:00
|
|
|
assert client.features.unfinished_backup is False
|
|
|
|
assert client.features.no_backup is False
|
|
|
|
assert client.features.backup_type is messages.BackupType.Bip39
|
2019-09-20 15:39:25 +00:00
|
|
|
|
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
@pytest.mark.models("core")
|
2019-11-13 11:47:51 +00:00
|
|
|
@pytest.mark.setup_client(needs_backup=True, mnemonic=MNEMONIC_SLIP39_BASIC_20_3of6)
|
2022-01-11 14:49:33 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"click_info", [True, False], ids=["click_info", "no_click_info"]
|
|
|
|
)
|
2022-01-31 12:25:30 +00:00
|
|
|
def test_backup_slip39_basic(client: Client, click_info: bool):
|
2024-09-02 11:01:16 +00:00
|
|
|
if click_info and client.layout_type is LayoutType.TR:
|
2024-03-11 15:20:08 +00:00
|
|
|
pytest.skip("click_info not implemented on T2B1")
|
2023-05-12 09:19:35 +00:00
|
|
|
|
2024-05-23 14:09:16 +00:00
|
|
|
assert client.features.backup_availability == messages.BackupAvailability.Required
|
2019-09-20 15:39:25 +00:00
|
|
|
|
|
|
|
with client:
|
2023-05-04 12:18:55 +00:00
|
|
|
IF = InputFlowSlip39BasicBackup(client, click_info)
|
|
|
|
client.set_input_flow(IF.get())
|
2019-09-20 15:39:25 +00:00
|
|
|
device.backup(client)
|
|
|
|
|
2019-09-20 16:06:02 +00:00
|
|
|
client.init_device()
|
2019-09-23 12:49:54 +00:00
|
|
|
assert client.features.initialized is True
|
2024-05-23 14:09:16 +00:00
|
|
|
assert (
|
|
|
|
client.features.backup_availability == messages.BackupAvailability.NotAvailable
|
|
|
|
)
|
2019-09-23 12:49:54 +00:00
|
|
|
assert client.features.unfinished_backup is False
|
|
|
|
assert client.features.no_backup is False
|
|
|
|
assert client.features.backup_type is messages.BackupType.Slip39_Basic
|
2019-09-20 16:06:02 +00:00
|
|
|
|
2019-09-20 15:39:25 +00:00
|
|
|
expected_ms = shamir.combine_mnemonics(MNEMONIC_SLIP39_BASIC_20_3of6)
|
2023-05-04 12:18:55 +00:00
|
|
|
actual_ms = shamir.combine_mnemonics(IF.mnemonics[:3])
|
2019-09-20 15:39:25 +00:00
|
|
|
assert expected_ms == actual_ms
|
2019-09-23 12:49:54 +00:00
|
|
|
|
|
|
|
|
2024-12-06 09:57:56 +00:00
|
|
|
@pytest.mark.models("core")
|
|
|
|
@pytest.mark.setup_client(needs_backup=True, mnemonic=MNEMONIC_SLIP39_SINGLE_EXT_20)
|
|
|
|
def test_backup_slip39_single(client: Client):
|
|
|
|
assert client.features.backup_availability == messages.BackupAvailability.Required
|
|
|
|
|
|
|
|
with client:
|
|
|
|
IF = InputFlowBip39Backup(
|
|
|
|
client, confirm_success=(client.layout_type is not LayoutType.Mercury)
|
|
|
|
)
|
|
|
|
client.set_input_flow(IF.get())
|
|
|
|
device.backup(client)
|
|
|
|
|
|
|
|
client.init_device()
|
|
|
|
assert client.features.initialized is True
|
|
|
|
assert (
|
|
|
|
client.features.backup_availability == messages.BackupAvailability.NotAvailable
|
|
|
|
)
|
|
|
|
|
|
|
|
assert client.features.unfinished_backup is False
|
|
|
|
assert client.features.no_backup is False
|
|
|
|
assert client.features.backup_type is messages.BackupType.Slip39_Single_Extendable
|
|
|
|
assert shamir.combine_mnemonics([IF.mnemonic]) == shamir.combine_mnemonics(
|
|
|
|
MNEMONIC_SLIP39_SINGLE_EXT_20
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
@pytest.mark.models("core")
|
2019-11-13 11:47:51 +00:00
|
|
|
@pytest.mark.setup_client(needs_backup=True, mnemonic=MNEMONIC_SLIP39_ADVANCED_20)
|
2022-01-11 14:49:33 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"click_info", [True, False], ids=["click_info", "no_click_info"]
|
|
|
|
)
|
2022-01-31 12:25:30 +00:00
|
|
|
def test_backup_slip39_advanced(client: Client, click_info: bool):
|
2024-09-02 11:01:16 +00:00
|
|
|
if click_info and client.layout_type is LayoutType.TR:
|
2024-03-11 15:20:08 +00:00
|
|
|
pytest.skip("click_info not implemented on T2B1")
|
2023-05-12 09:19:35 +00:00
|
|
|
|
2024-05-23 14:09:16 +00:00
|
|
|
assert client.features.backup_availability == messages.BackupAvailability.Required
|
2019-09-23 12:49:54 +00:00
|
|
|
|
|
|
|
with client:
|
2023-05-04 12:18:55 +00:00
|
|
|
IF = InputFlowSlip39AdvancedBackup(client, click_info)
|
|
|
|
client.set_input_flow(IF.get())
|
2019-09-23 12:49:54 +00:00
|
|
|
device.backup(client)
|
|
|
|
|
|
|
|
client.init_device()
|
|
|
|
assert client.features.initialized is True
|
2024-05-23 14:09:16 +00:00
|
|
|
assert (
|
|
|
|
client.features.backup_availability == messages.BackupAvailability.NotAvailable
|
|
|
|
)
|
2019-09-23 12:49:54 +00:00
|
|
|
assert client.features.unfinished_backup is False
|
|
|
|
assert client.features.no_backup is False
|
|
|
|
assert client.features.backup_type is messages.BackupType.Slip39_Advanced
|
|
|
|
|
|
|
|
expected_ms = shamir.combine_mnemonics(MNEMONIC_SLIP39_ADVANCED_20)
|
|
|
|
actual_ms = shamir.combine_mnemonics(
|
2023-05-04 12:18:55 +00:00
|
|
|
IF.mnemonics[:3] + IF.mnemonics[5:8] + IF.mnemonics[10:13]
|
2019-09-23 12:49:54 +00:00
|
|
|
)
|
|
|
|
assert expected_ms == actual_ms
|
|
|
|
|
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
@pytest.mark.models("core")
|
2024-04-11 14:21:17 +00:00
|
|
|
@pytest.mark.setup_client(needs_backup=True, mnemonic=MNEMONIC_SLIP39_CUSTOM_1of1[0])
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"share_threshold,share_count",
|
|
|
|
[(1, 1), (2, 2), (3, 5)],
|
|
|
|
ids=["1_of_1", "2_of_2", "3_of_5"],
|
|
|
|
)
|
|
|
|
def test_backup_slip39_custom(client: Client, share_threshold, share_count):
|
2024-05-23 14:09:16 +00:00
|
|
|
assert client.features.backup_availability == messages.BackupAvailability.Required
|
2024-04-11 14:21:17 +00:00
|
|
|
|
|
|
|
with client:
|
|
|
|
IF = InputFlowSlip39CustomBackup(client, share_count)
|
|
|
|
client.set_input_flow(IF.get())
|
|
|
|
device.backup(
|
|
|
|
client, group_threshold=1, groups=[(share_threshold, share_count)]
|
|
|
|
)
|
|
|
|
|
|
|
|
client.init_device()
|
|
|
|
assert client.features.initialized is True
|
2024-05-23 14:09:16 +00:00
|
|
|
assert (
|
|
|
|
client.features.backup_availability == messages.BackupAvailability.NotAvailable
|
|
|
|
)
|
2024-04-11 14:21:17 +00:00
|
|
|
assert client.features.unfinished_backup is False
|
|
|
|
assert client.features.no_backup is False
|
|
|
|
|
|
|
|
assert len(IF.mnemonics) == share_count
|
|
|
|
assert (
|
|
|
|
shamir.combine_mnemonics(IF.mnemonics[-share_threshold:]).hex()
|
|
|
|
== MNEMONIC_SLIP39_CUSTOM_SECRET
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-09-23 12:49:54 +00:00
|
|
|
# we only test this with bip39 because the code path is always the same
|
2019-11-13 11:47:51 +00:00
|
|
|
@pytest.mark.setup_client(no_backup=True)
|
2022-01-31 12:25:30 +00:00
|
|
|
def test_no_backup_fails(client: Client):
|
2020-08-26 10:06:51 +00:00
|
|
|
client.ensure_unlocked()
|
2019-09-23 12:49:54 +00:00
|
|
|
assert client.features.initialized is True
|
|
|
|
assert client.features.no_backup is True
|
2024-05-23 14:09:16 +00:00
|
|
|
assert (
|
|
|
|
client.features.backup_availability == messages.BackupAvailability.NotAvailable
|
|
|
|
)
|
2019-09-23 12:49:54 +00:00
|
|
|
|
|
|
|
# backup attempt should fail because no_backup=True
|
2019-10-01 10:04:55 +00:00
|
|
|
with pytest.raises(TrezorFailure, match=r".*Seed already backed up"):
|
2019-09-23 12:49:54 +00:00
|
|
|
device.backup(client)
|
|
|
|
|
|
|
|
|
|
|
|
# we only test this with bip39 because the code path is always the same
|
2019-11-13 11:47:51 +00:00
|
|
|
@pytest.mark.setup_client(needs_backup=True)
|
2022-01-31 12:25:30 +00:00
|
|
|
def test_interrupt_backup_fails(client: Client):
|
2020-08-26 10:06:51 +00:00
|
|
|
client.ensure_unlocked()
|
2019-09-23 12:49:54 +00:00
|
|
|
assert client.features.initialized is True
|
2024-05-23 14:09:16 +00:00
|
|
|
assert client.features.backup_availability == messages.BackupAvailability.Required
|
2019-09-23 12:49:54 +00:00
|
|
|
assert client.features.unfinished_backup is False
|
|
|
|
assert client.features.no_backup is False
|
|
|
|
|
|
|
|
# start backup
|
|
|
|
client.call_raw(messages.BackupDevice())
|
|
|
|
|
|
|
|
# interupt backup by sending initialize
|
|
|
|
client.init_device()
|
|
|
|
|
|
|
|
# check that device state is as expected
|
|
|
|
assert client.features.initialized is True
|
2024-05-23 14:09:16 +00:00
|
|
|
assert (
|
|
|
|
client.features.backup_availability == messages.BackupAvailability.NotAvailable
|
|
|
|
)
|
2019-09-23 12:49:54 +00:00
|
|
|
assert client.features.unfinished_backup is True
|
|
|
|
assert client.features.no_backup is False
|
|
|
|
|
|
|
|
# Second attempt at backup should fail
|
2019-10-01 10:04:55 +00:00
|
|
|
with pytest.raises(TrezorFailure, match=r".*Seed already backed up"):
|
2019-09-23 12:49:54 +00:00
|
|
|
device.backup(client)
|