2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2018-03-20 15:47:05 +00:00
|
|
|
#
|
2019-05-29 16:44:09 +00:00
|
|
|
# Copyright (C) 2012-2019 SatoshiLabs and contributors
|
2018-03-20 15:47:05 +00:00
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
2018-06-21 14:28:34 +00:00
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
2018-03-20 15:47:05 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# 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>.
|
2018-03-20 15:47:05 +00:00
|
|
|
|
2018-10-02 15:35:51 +00:00
|
|
|
from unittest import mock
|
2018-08-13 16:21:24 +00:00
|
|
|
|
2018-05-11 12:53:51 +00:00
|
|
|
import pytest
|
2018-08-13 16:21:24 +00:00
|
|
|
from mnemonic import Mnemonic
|
2018-03-20 15:47:05 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
from trezorlib import device, messages as proto
|
2019-09-23 12:49:54 +00:00
|
|
|
from trezorlib.exceptions import TrezorFailure
|
2018-10-02 15:35:51 +00:00
|
|
|
from trezorlib.messages import ButtonRequestType as B
|
2018-03-20 15:47:05 +00:00
|
|
|
|
2019-09-11 12:43:32 +00:00
|
|
|
from ..common import (
|
2019-09-10 09:24:57 +00:00
|
|
|
MNEMONIC12,
|
|
|
|
click_through,
|
|
|
|
generate_entropy,
|
|
|
|
read_and_confirm_mnemonic,
|
|
|
|
)
|
2018-03-20 15:47:05 +00:00
|
|
|
|
2018-10-02 15:35:51 +00:00
|
|
|
EXTERNAL_ENTROPY = b"zlutoucky kun upel divoke ody" * 2
|
2020-03-13 09:50:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def reset_device(client, strength):
|
|
|
|
mnemonic = None
|
|
|
|
|
|
|
|
def input_flow():
|
|
|
|
nonlocal mnemonic
|
|
|
|
# 1. Confirm Reset
|
|
|
|
# 2. Backup your seed
|
|
|
|
# 3. Confirm warning
|
|
|
|
yield from click_through(client.debug, screens=3, code=B.ResetDevice)
|
|
|
|
|
|
|
|
# mnemonic phrases
|
2021-06-17 14:31:26 +00:00
|
|
|
mnemonic = yield from read_and_confirm_mnemonic(client.debug)
|
2020-03-13 09:50:20 +00:00
|
|
|
|
|
|
|
# confirm recovery seed check
|
2021-06-17 14:31:26 +00:00
|
|
|
br = yield
|
|
|
|
assert br.code == B.Success
|
2020-03-13 09:50:20 +00:00
|
|
|
client.debug.press_yes()
|
|
|
|
|
|
|
|
# confirm success
|
2021-06-17 14:31:26 +00:00
|
|
|
br = yield
|
|
|
|
assert br.code == B.Success
|
2020-03-13 09:50:20 +00:00
|
|
|
client.debug.press_yes()
|
|
|
|
|
|
|
|
os_urandom = mock.Mock(return_value=EXTERNAL_ENTROPY)
|
|
|
|
with mock.patch("os.urandom", os_urandom), client:
|
|
|
|
client.set_expected_responses(
|
|
|
|
[
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.EntropyRequest(),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
2021-07-09 12:48:40 +00:00
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
2020-03-13 09:50:20 +00:00
|
|
|
proto.ButtonRequest(code=B.Success),
|
|
|
|
proto.ButtonRequest(code=B.Success),
|
2021-01-14 14:26:01 +00:00
|
|
|
proto.Success,
|
|
|
|
proto.Features,
|
2020-03-13 09:50:20 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
client.set_input_flow(input_flow)
|
|
|
|
|
|
|
|
# No PIN, no passphrase, don't display random
|
|
|
|
device.reset(
|
|
|
|
client,
|
|
|
|
display_random=False,
|
|
|
|
strength=strength,
|
|
|
|
passphrase_protection=False,
|
|
|
|
pin_protection=False,
|
|
|
|
label="test",
|
|
|
|
language="en-US",
|
|
|
|
)
|
|
|
|
|
|
|
|
# generate mnemonic locally
|
|
|
|
internal_entropy = client.debug.state().reset_entropy
|
|
|
|
entropy = generate_entropy(strength, internal_entropy, EXTERNAL_ENTROPY)
|
|
|
|
expected_mnemonic = Mnemonic("english").to_mnemonic(entropy)
|
|
|
|
|
|
|
|
# Compare that device generated proper mnemonic for given entropies
|
|
|
|
assert mnemonic == expected_mnemonic
|
|
|
|
|
|
|
|
# Check if device is properly initialized
|
|
|
|
resp = client.call_raw(proto.Initialize())
|
|
|
|
assert resp.initialized is True
|
|
|
|
assert resp.needs_backup is False
|
|
|
|
assert resp.pin_protection is False
|
|
|
|
assert resp.passphrase_protection is False
|
|
|
|
assert resp.backup_type is proto.BackupType.Bip39
|
|
|
|
|
|
|
|
# backup attempt fails because backup was done in reset
|
|
|
|
with pytest.raises(TrezorFailure, match="ProcessError: Seed already backed up"):
|
|
|
|
device.backup(client)
|
2018-10-02 15:35:51 +00:00
|
|
|
|
2018-03-20 15:47:05 +00:00
|
|
|
|
|
|
|
@pytest.mark.skip_t1
|
2019-09-10 09:24:57 +00:00
|
|
|
class TestMsgResetDeviceT2:
|
2019-08-27 14:58:59 +00:00
|
|
|
@pytest.mark.setup_client(uninitialized=True)
|
|
|
|
def test_reset_device(self, client):
|
2020-03-13 09:50:20 +00:00
|
|
|
reset_device(client, 128) # 12 words
|
2018-10-02 15:35:51 +00:00
|
|
|
|
2020-03-13 09:50:20 +00:00
|
|
|
@pytest.mark.setup_client(uninitialized=True)
|
|
|
|
def test_reset_device_192(self, client):
|
|
|
|
reset_device(client, 192) # 18 words
|
2019-09-23 12:49:54 +00:00
|
|
|
|
2019-08-27 14:58:59 +00:00
|
|
|
@pytest.mark.setup_client(uninitialized=True)
|
|
|
|
def test_reset_device_pin(self, client):
|
2019-09-10 09:24:57 +00:00
|
|
|
mnemonic = None
|
2020-03-13 09:50:20 +00:00
|
|
|
strength = 256 # 24 words
|
2018-10-02 15:35:51 +00:00
|
|
|
|
|
|
|
def input_flow():
|
2019-09-10 09:24:57 +00:00
|
|
|
nonlocal mnemonic
|
|
|
|
|
2018-10-22 12:44:36 +00:00
|
|
|
# Confirm Reset
|
2021-06-17 14:31:26 +00:00
|
|
|
br = yield
|
|
|
|
assert br.code == B.ResetDevice
|
2019-08-27 14:58:59 +00:00
|
|
|
client.debug.press_yes()
|
2018-10-22 12:44:36 +00:00
|
|
|
|
2018-10-02 15:35:51 +00:00
|
|
|
# Enter new PIN
|
|
|
|
yield
|
2019-08-27 14:58:59 +00:00
|
|
|
client.debug.input("654")
|
2018-10-02 15:35:51 +00:00
|
|
|
|
|
|
|
# Confirm PIN
|
|
|
|
yield
|
2019-08-27 14:58:59 +00:00
|
|
|
client.debug.input("654")
|
2018-10-02 15:35:51 +00:00
|
|
|
|
|
|
|
# Confirm entropy
|
2021-06-17 14:31:26 +00:00
|
|
|
br = yield
|
|
|
|
assert br.code == B.ResetDevice
|
2019-08-27 14:58:59 +00:00
|
|
|
client.debug.press_yes()
|
2018-10-02 15:35:51 +00:00
|
|
|
|
|
|
|
# Backup your seed
|
2021-06-17 14:31:26 +00:00
|
|
|
br = yield
|
|
|
|
assert br.code == B.ResetDevice
|
2019-08-27 14:58:59 +00:00
|
|
|
client.debug.press_yes()
|
2018-10-02 15:35:51 +00:00
|
|
|
|
2019-07-15 09:09:13 +00:00
|
|
|
# Confirm warning
|
2021-06-17 14:31:26 +00:00
|
|
|
br = yield
|
|
|
|
assert br.code == B.ResetDevice
|
2019-08-27 14:58:59 +00:00
|
|
|
client.debug.press_yes()
|
2019-07-15 09:09:13 +00:00
|
|
|
|
2018-10-02 15:35:51 +00:00
|
|
|
# mnemonic phrases
|
2021-06-17 14:31:26 +00:00
|
|
|
mnemonic = yield from read_and_confirm_mnemonic(client.debug)
|
2018-10-02 15:35:51 +00:00
|
|
|
|
2019-06-23 10:20:36 +00:00
|
|
|
# confirm recovery seed check
|
2021-06-17 14:31:26 +00:00
|
|
|
br = yield
|
|
|
|
assert br.code == B.Success
|
2019-08-27 14:58:59 +00:00
|
|
|
client.debug.press_yes()
|
2019-06-23 10:20:36 +00:00
|
|
|
|
|
|
|
# confirm success
|
2021-06-17 14:31:26 +00:00
|
|
|
br = yield
|
|
|
|
assert br.code == B.Success
|
2019-08-27 14:58:59 +00:00
|
|
|
client.debug.press_yes()
|
2018-10-02 15:35:51 +00:00
|
|
|
|
|
|
|
os_urandom = mock.Mock(return_value=EXTERNAL_ENTROPY)
|
2019-08-27 14:58:59 +00:00
|
|
|
with mock.patch("os.urandom", os_urandom), client:
|
|
|
|
client.set_expected_responses(
|
2018-10-02 15:35:51 +00:00
|
|
|
[
|
2018-10-22 12:44:36 +00:00
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
2020-05-25 13:44:41 +00:00
|
|
|
proto.ButtonRequest(code=B.PinEntry),
|
|
|
|
proto.ButtonRequest(code=B.PinEntry),
|
2018-10-02 15:35:51 +00:00
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.EntropyRequest(),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
2021-07-09 12:48:40 +00:00
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
2019-07-26 09:41:47 +00:00
|
|
|
proto.ButtonRequest(code=B.Success),
|
|
|
|
proto.ButtonRequest(code=B.Success),
|
2021-01-14 14:26:01 +00:00
|
|
|
proto.Success,
|
|
|
|
proto.Features,
|
2018-10-02 15:35:51 +00:00
|
|
|
]
|
|
|
|
)
|
2019-08-27 14:58:59 +00:00
|
|
|
client.set_input_flow(input_flow)
|
2018-10-02 15:35:51 +00:00
|
|
|
|
|
|
|
# PIN, passphrase, display random
|
|
|
|
device.reset(
|
2019-08-27 14:58:59 +00:00
|
|
|
client,
|
2018-08-13 16:21:24 +00:00
|
|
|
display_random=True,
|
|
|
|
strength=strength,
|
|
|
|
passphrase_protection=True,
|
|
|
|
pin_protection=True,
|
|
|
|
label="test",
|
2019-12-07 11:11:51 +00:00
|
|
|
language="en-US",
|
2018-08-13 16:21:24 +00:00
|
|
|
)
|
2018-03-20 15:47:05 +00:00
|
|
|
|
2018-10-02 15:35:51 +00:00
|
|
|
# generate mnemonic locally
|
2019-08-27 14:58:59 +00:00
|
|
|
internal_entropy = client.debug.state().reset_entropy
|
2018-10-02 15:35:51 +00:00
|
|
|
entropy = generate_entropy(strength, internal_entropy, EXTERNAL_ENTROPY)
|
|
|
|
expected_mnemonic = Mnemonic("english").to_mnemonic(entropy)
|
2018-03-20 15:47:05 +00:00
|
|
|
|
|
|
|
# Compare that device generated proper mnemonic for given entropies
|
2019-09-10 09:24:57 +00:00
|
|
|
assert mnemonic == expected_mnemonic
|
2018-03-20 15:47:05 +00:00
|
|
|
|
|
|
|
# Check if device is properly initialized
|
2019-08-27 14:58:59 +00:00
|
|
|
resp = client.call_raw(proto.Initialize())
|
2018-03-20 15:47:05 +00:00
|
|
|
assert resp.initialized is True
|
|
|
|
assert resp.needs_backup is False
|
|
|
|
assert resp.pin_protection is True
|
|
|
|
assert resp.passphrase_protection is True
|
|
|
|
|
2019-08-27 14:58:59 +00:00
|
|
|
@pytest.mark.setup_client(uninitialized=True)
|
2021-02-10 23:56:39 +00:00
|
|
|
def test_reset_failed_check(self, client):
|
|
|
|
mnemonic = None
|
|
|
|
strength = 256 # 24 words
|
|
|
|
|
|
|
|
def input_flow():
|
|
|
|
nonlocal mnemonic
|
|
|
|
# 1. Confirm Reset
|
|
|
|
# 2. Backup your seed
|
|
|
|
# 3. Confirm warning
|
|
|
|
yield from click_through(client.debug, screens=3, code=B.ResetDevice)
|
|
|
|
|
|
|
|
# mnemonic phrases, wrong answer
|
2021-06-17 14:31:26 +00:00
|
|
|
mnemonic = yield from read_and_confirm_mnemonic(
|
|
|
|
client.debug, choose_wrong=True
|
2021-02-10 23:56:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# warning screen
|
2021-06-17 14:31:26 +00:00
|
|
|
br = yield
|
|
|
|
assert br.code == B.ResetDevice
|
2021-02-10 23:56:39 +00:00
|
|
|
client.debug.press_yes()
|
|
|
|
|
|
|
|
# mnemonic phrases
|
2021-06-17 14:31:26 +00:00
|
|
|
mnemonic = yield from read_and_confirm_mnemonic(client.debug)
|
2021-02-10 23:56:39 +00:00
|
|
|
|
|
|
|
# confirm recovery seed check
|
2021-06-17 14:31:26 +00:00
|
|
|
br = yield
|
|
|
|
assert br.code == B.Success
|
2021-02-10 23:56:39 +00:00
|
|
|
client.debug.press_yes()
|
|
|
|
|
|
|
|
# confirm success
|
2021-06-17 14:31:26 +00:00
|
|
|
br = yield
|
|
|
|
assert br.code == B.Success
|
2021-02-10 23:56:39 +00:00
|
|
|
client.debug.press_yes()
|
|
|
|
|
|
|
|
os_urandom = mock.Mock(return_value=EXTERNAL_ENTROPY)
|
|
|
|
with mock.patch("os.urandom", os_urandom), client:
|
|
|
|
client.set_expected_responses(
|
|
|
|
[
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.EntropyRequest(),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
2021-07-09 12:48:40 +00:00
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
2021-02-10 23:56:39 +00:00
|
|
|
proto.ButtonRequest(code=B.Success),
|
|
|
|
proto.ButtonRequest(code=B.Success),
|
|
|
|
proto.Success,
|
|
|
|
proto.Features,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
client.set_input_flow(input_flow)
|
|
|
|
|
|
|
|
# PIN, passphrase, display random
|
|
|
|
device.reset(
|
|
|
|
client,
|
|
|
|
display_random=False,
|
|
|
|
strength=strength,
|
|
|
|
passphrase_protection=False,
|
|
|
|
pin_protection=False,
|
|
|
|
label="test",
|
|
|
|
language="en-US",
|
|
|
|
)
|
|
|
|
|
|
|
|
# generate mnemonic locally
|
|
|
|
internal_entropy = client.debug.state().reset_entropy
|
|
|
|
entropy = generate_entropy(strength, internal_entropy, EXTERNAL_ENTROPY)
|
|
|
|
expected_mnemonic = Mnemonic("english").to_mnemonic(entropy)
|
|
|
|
|
|
|
|
# Compare that device generated proper mnemonic for given entropies
|
|
|
|
assert mnemonic == expected_mnemonic
|
|
|
|
|
|
|
|
# Check if device is properly initialized
|
|
|
|
resp = client.call_raw(proto.Initialize())
|
|
|
|
assert resp.initialized is True
|
|
|
|
assert resp.needs_backup is False
|
|
|
|
assert resp.pin_protection is False
|
|
|
|
assert resp.passphrase_protection is False
|
|
|
|
assert resp.backup_type is proto.BackupType.Bip39
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(uninitialized=True)
|
2019-08-27 14:58:59 +00:00
|
|
|
def test_failed_pin(self, client):
|
2018-03-20 15:47:05 +00:00
|
|
|
# external_entropy = b'zlutoucky kun upel divoke ody' * 2
|
|
|
|
strength = 128
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = client.call_raw(
|
2018-08-13 16:21:24 +00:00
|
|
|
proto.ResetDevice(strength=strength, pin_protection=True, label="test")
|
|
|
|
)
|
2018-03-20 15:47:05 +00:00
|
|
|
|
2018-10-22 12:44:36 +00:00
|
|
|
# Confirm Reset
|
|
|
|
assert isinstance(ret, proto.ButtonRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
client.debug.press_yes()
|
|
|
|
ret = client.call_raw(proto.ButtonAck())
|
2018-10-22 12:44:36 +00:00
|
|
|
|
2018-03-20 15:47:05 +00:00
|
|
|
# Enter PIN for first time
|
|
|
|
assert isinstance(ret, proto.ButtonRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
client.debug.input("654")
|
|
|
|
ret = client.call_raw(proto.ButtonAck())
|
2018-03-20 15:47:05 +00:00
|
|
|
|
|
|
|
# Enter PIN for second time
|
|
|
|
assert isinstance(ret, proto.ButtonRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
client.debug.input("456")
|
|
|
|
ret = client.call_raw(proto.ButtonAck())
|
2018-03-20 15:47:05 +00:00
|
|
|
|
|
|
|
assert isinstance(ret, proto.ButtonRequest)
|
|
|
|
|
2019-08-27 14:58:59 +00:00
|
|
|
@pytest.mark.setup_client(mnemonic=MNEMONIC12)
|
|
|
|
def test_already_initialized(self, client):
|
2018-03-20 15:47:05 +00:00
|
|
|
with pytest.raises(Exception):
|
2019-12-07 11:11:51 +00:00
|
|
|
device.reset(client, False, 128, True, True, "label", "en-US")
|