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
|
|
|
|
|
|
|
from trezorlib import device, messages
|
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
|
|
|
from trezorlib.messages import ButtonRequestType as B
|
|
|
|
|
2019-09-20 15:39:25 +00:00
|
|
|
from ..common import (
|
|
|
|
MNEMONIC12,
|
2019-09-23 12:49:54 +00:00
|
|
|
MNEMONIC_SLIP39_ADVANCED_20,
|
2019-09-20 15:39:25 +00:00
|
|
|
MNEMONIC_SLIP39_BASIC_20_3of6,
|
|
|
|
read_and_confirm_mnemonic,
|
|
|
|
)
|
2019-09-20 15:11:24 +00:00
|
|
|
|
|
|
|
|
2022-01-11 14:49:33 +00:00
|
|
|
def click_info_button(debug):
|
|
|
|
"""Click Shamir backup info button and return back."""
|
2022-01-27 10:48:14 +00:00
|
|
|
debug.press_info()
|
2022-01-11 14:49:33 +00:00
|
|
|
yield # Info screen with text
|
2022-01-27 10:48:14 +00:00
|
|
|
debug.press_yes()
|
2022-01-11 14:49:33 +00:00
|
|
|
|
|
|
|
|
2019-09-20 15:11:24 +00:00
|
|
|
@pytest.mark.skip_t1 # 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):
|
2019-09-20 15:11:24 +00:00
|
|
|
assert client.features.needs_backup is True
|
|
|
|
mnemonic = None
|
|
|
|
|
|
|
|
def input_flow():
|
|
|
|
nonlocal mnemonic
|
|
|
|
yield # Confirm Backup
|
|
|
|
client.debug.press_yes()
|
2021-06-17 14:31:26 +00:00
|
|
|
# Mnemonic phrases
|
|
|
|
mnemonic = yield from read_and_confirm_mnemonic(client.debug)
|
2019-09-20 15:11:24 +00:00
|
|
|
yield # Confirm success
|
|
|
|
client.debug.press_yes()
|
|
|
|
yield # Backup is done
|
|
|
|
client.debug.press_yes()
|
|
|
|
|
|
|
|
with client:
|
|
|
|
client.set_input_flow(input_flow)
|
|
|
|
client.set_expected_responses(
|
|
|
|
[
|
|
|
|
messages.ButtonRequest(code=B.ResetDevice),
|
2021-07-09 12:48:40 +00:00
|
|
|
messages.ButtonRequest(code=B.ResetDevice),
|
2019-09-20 15:11:24 +00:00
|
|
|
messages.ButtonRequest(code=B.Success),
|
|
|
|
messages.ButtonRequest(code=B.Success),
|
2021-01-14 14:26:01 +00:00
|
|
|
messages.Success,
|
|
|
|
messages.Features,
|
2019-09-20 15:11:24 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
device.backup(client)
|
|
|
|
|
|
|
|
assert 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
|
2019-09-20 16:06:02 +00:00
|
|
|
assert client.features.needs_backup is False
|
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
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skip_t1
|
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):
|
2019-09-20 15:39:25 +00:00
|
|
|
assert client.features.needs_backup is True
|
|
|
|
mnemonics = []
|
|
|
|
|
|
|
|
def input_flow():
|
2022-01-11 14:49:33 +00:00
|
|
|
yield # Checklist
|
|
|
|
client.debug.press_yes()
|
|
|
|
if click_info:
|
|
|
|
yield from click_info_button(client.debug)
|
|
|
|
yield # Number of shares (5)
|
|
|
|
client.debug.press_yes()
|
|
|
|
yield # Checklist
|
|
|
|
client.debug.press_yes()
|
|
|
|
if click_info:
|
|
|
|
yield from click_info_button(client.debug)
|
|
|
|
yield # Threshold (3)
|
|
|
|
client.debug.press_yes()
|
|
|
|
yield # Checklist
|
|
|
|
client.debug.press_yes()
|
|
|
|
yield # Confirm show seeds
|
|
|
|
client.debug.press_yes()
|
2019-09-20 15:39:25 +00:00
|
|
|
|
|
|
|
# Mnemonic phrases
|
|
|
|
for _ in range(5):
|
2021-06-17 14:31:26 +00:00
|
|
|
# Phrase screen
|
|
|
|
mnemonic = yield from read_and_confirm_mnemonic(client.debug)
|
2019-09-20 15:39:25 +00:00
|
|
|
mnemonics.append(mnemonic)
|
|
|
|
yield # Confirm continue to next
|
|
|
|
client.debug.press_yes()
|
|
|
|
|
2022-01-11 14:49:33 +00:00
|
|
|
yield # Confirm backup
|
2019-09-20 15:39:25 +00:00
|
|
|
client.debug.press_yes()
|
|
|
|
|
|
|
|
with client:
|
|
|
|
client.set_input_flow(input_flow)
|
|
|
|
client.set_expected_responses(
|
2022-01-11 14:49:33 +00:00
|
|
|
[messages.ButtonRequest(code=B.ResetDevice)]
|
|
|
|
* (8 if click_info else 6) # intro screens (and optional info)
|
2021-06-17 14:31:26 +00:00
|
|
|
+ [
|
2021-07-09 12:48:40 +00:00
|
|
|
messages.ButtonRequest(code=B.ResetDevice),
|
2019-09-20 15:39:25 +00:00
|
|
|
messages.ButtonRequest(code=B.Success),
|
2021-06-17 14:31:26 +00:00
|
|
|
]
|
|
|
|
* 5 # individual shares
|
|
|
|
+ [
|
2019-09-20 15:39:25 +00:00
|
|
|
messages.ButtonRequest(code=B.Success),
|
2021-01-14 14:26:01 +00:00
|
|
|
messages.Success,
|
|
|
|
messages.Features,
|
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
|
2019-09-20 16:06:02 +00:00
|
|
|
assert client.features.needs_backup is False
|
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)
|
|
|
|
actual_ms = shamir.combine_mnemonics(mnemonics[:3])
|
|
|
|
assert expected_ms == actual_ms
|
2019-09-23 12:49:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skip_t1
|
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):
|
2019-09-23 12:49:54 +00:00
|
|
|
assert client.features.needs_backup is True
|
|
|
|
mnemonics = []
|
|
|
|
|
|
|
|
def input_flow():
|
2022-01-11 14:49:33 +00:00
|
|
|
yield # Checklist
|
|
|
|
client.debug.press_yes()
|
|
|
|
if click_info:
|
|
|
|
yield from click_info_button(client.debug)
|
|
|
|
yield # Set and confirm group count
|
|
|
|
client.debug.press_yes()
|
|
|
|
yield # Checklist
|
|
|
|
client.debug.press_yes()
|
|
|
|
if click_info:
|
|
|
|
yield from click_info_button(client.debug)
|
|
|
|
yield # Set and confirm group threshold
|
|
|
|
client.debug.press_yes()
|
|
|
|
yield # Checklist
|
|
|
|
client.debug.press_yes()
|
|
|
|
for _ in range(5): # for each of 5 groups
|
|
|
|
if click_info:
|
|
|
|
yield from click_info_button(client.debug)
|
|
|
|
yield # Set & Confirm number of shares
|
|
|
|
client.debug.press_yes()
|
|
|
|
if click_info:
|
|
|
|
yield from click_info_button(client.debug)
|
|
|
|
yield # Set & confirm share threshold value
|
|
|
|
client.debug.press_yes()
|
|
|
|
yield # Confirm show seeds
|
|
|
|
client.debug.press_yes()
|
2019-09-23 12:49:54 +00:00
|
|
|
|
|
|
|
# Mnemonic phrases
|
|
|
|
for _ in range(5):
|
|
|
|
for _ in range(5):
|
2021-06-17 14:31:26 +00:00
|
|
|
# Phrase screen
|
|
|
|
mnemonic = yield from read_and_confirm_mnemonic(client.debug)
|
2019-09-23 12:49:54 +00:00
|
|
|
mnemonics.append(mnemonic)
|
|
|
|
yield # Confirm continue to next
|
|
|
|
client.debug.press_yes()
|
|
|
|
|
2022-01-11 14:49:33 +00:00
|
|
|
yield # Confirm backup
|
2019-09-23 12:49:54 +00:00
|
|
|
client.debug.press_yes()
|
|
|
|
|
|
|
|
with client:
|
|
|
|
client.set_input_flow(input_flow)
|
|
|
|
client.set_expected_responses(
|
2022-01-11 14:49:33 +00:00
|
|
|
[messages.ButtonRequest(code=B.ResetDevice)]
|
|
|
|
* (8 if click_info else 6) # intro screens (and optional info)
|
2021-06-17 14:31:26 +00:00
|
|
|
+ [
|
2022-01-11 14:49:33 +00:00
|
|
|
(click_info, messages.ButtonRequest(code=B.ResetDevice)),
|
2019-09-23 12:49:54 +00:00
|
|
|
messages.ButtonRequest(code=B.ResetDevice),
|
2022-01-11 14:49:33 +00:00
|
|
|
(click_info, messages.ButtonRequest(code=B.ResetDevice)),
|
2019-09-23 12:49:54 +00:00
|
|
|
messages.ButtonRequest(code=B.ResetDevice),
|
2021-06-17 14:31:26 +00:00
|
|
|
]
|
2022-01-11 14:49:33 +00:00
|
|
|
* 5 # group thresholds (and optional info)
|
2021-06-17 14:31:26 +00:00
|
|
|
+ [
|
2021-07-09 12:48:40 +00:00
|
|
|
messages.ButtonRequest(code=B.ResetDevice),
|
2019-09-23 12:49:54 +00:00
|
|
|
messages.ButtonRequest(code=B.Success),
|
2021-06-17 14:31:26 +00:00
|
|
|
]
|
|
|
|
* 25 # individual shares
|
|
|
|
+ [
|
2019-09-23 12:49:54 +00:00
|
|
|
messages.ButtonRequest(code=B.Success),
|
2021-01-14 14:26:01 +00:00
|
|
|
messages.Success,
|
|
|
|
messages.Features,
|
2019-09-23 12:49:54 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
device.backup(client)
|
|
|
|
|
|
|
|
client.init_device()
|
|
|
|
assert client.features.initialized is True
|
|
|
|
assert client.features.needs_backup is False
|
|
|
|
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(
|
|
|
|
mnemonics[:3] + mnemonics[5:8] + mnemonics[10:13]
|
|
|
|
)
|
|
|
|
assert expected_ms == actual_ms
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
assert client.features.needs_backup is False
|
|
|
|
|
|
|
|
# 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
|
|
|
|
assert client.features.needs_backup is True
|
|
|
|
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
|
|
|
|
assert client.features.needs_backup is False
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
# we only test this with bip39 because the code path is always the same
|
|
|
|
@pytest.mark.setup_client(uninitialized=True)
|
2022-01-31 12:25:30 +00:00
|
|
|
def test_no_backup_show_entropy_fails(client: Client):
|
2019-09-23 12:49:54 +00:00
|
|
|
with pytest.raises(
|
2019-10-01 10:04:55 +00:00
|
|
|
TrezorFailure, match=r".*Can't show internal entropy when backup is skipped"
|
2019-09-23 12:49:54 +00:00
|
|
|
):
|
|
|
|
device.reset(
|
|
|
|
client,
|
|
|
|
display_random=True,
|
|
|
|
strength=128,
|
|
|
|
passphrase_protection=False,
|
|
|
|
pin_protection=False,
|
|
|
|
label="test",
|
2019-12-07 11:11:51 +00:00
|
|
|
language="en-US",
|
2019-09-23 12:49:54 +00:00
|
|
|
no_backup=True,
|
|
|
|
)
|