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
|
2018-10-02 15:35:51 +00:00
|
|
|
from trezorlib.messages import ButtonRequestType as B
|
2018-03-20 15:47:05 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
from .common import TrezorTest, generate_entropy
|
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
|
|
|
|
|
2018-03-20 15:47:05 +00:00
|
|
|
|
|
|
|
@pytest.mark.skip_t1
|
|
|
|
class TestMsgResetDeviceT2(TrezorTest):
|
|
|
|
def test_reset_device(self):
|
2018-10-02 15:35:51 +00:00
|
|
|
words = []
|
2018-03-20 15:47:05 +00:00
|
|
|
strength = 128
|
2018-10-02 15:35:51 +00:00
|
|
|
|
|
|
|
def input_flow():
|
2018-10-22 12:44:36 +00:00
|
|
|
# Confirm Reset
|
|
|
|
btn_code = yield
|
|
|
|
assert btn_code == B.ResetDevice
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
2018-10-02 15:35:51 +00:00
|
|
|
# Backup your seed
|
|
|
|
btn_code = yield
|
|
|
|
assert btn_code == B.ResetDevice
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
2019-07-15 09:09:13 +00:00
|
|
|
# Confirm warning
|
|
|
|
btn_code = yield
|
|
|
|
assert btn_code == B.ResetDevice
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
2018-10-02 15:35:51 +00:00
|
|
|
# mnemonic phrases
|
|
|
|
btn_code = yield
|
|
|
|
assert btn_code == B.ResetDevice
|
|
|
|
# 12 words, 3 pages
|
|
|
|
for i in range(3):
|
2019-07-26 15:37:26 +00:00
|
|
|
words.extend(self.client.debug.read_reset_word().split())
|
2018-10-02 15:35:51 +00:00
|
|
|
if i < 2:
|
|
|
|
self.client.debug.swipe_down()
|
|
|
|
else:
|
|
|
|
# last page is confirmation
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
|
|
|
# check backup words
|
2019-07-08 07:37:36 +00:00
|
|
|
for _ in range(3):
|
2019-07-26 15:37:26 +00:00
|
|
|
index = self.client.debug.read_reset_word_pos()
|
2018-10-02 15:35:51 +00:00
|
|
|
self.client.debug.input(words[index])
|
|
|
|
|
2019-06-23 10:20:36 +00:00
|
|
|
# confirm recovery seed check
|
|
|
|
btn_code = yield
|
2019-07-26 09:41:47 +00:00
|
|
|
assert btn_code == B.Success
|
2019-06-23 10:20:36 +00:00
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
|
|
|
# confirm success
|
2018-10-02 15:35:51 +00:00
|
|
|
btn_code = yield
|
2019-07-26 09:41:47 +00:00
|
|
|
assert btn_code == B.Success
|
2018-10-02 15:35:51 +00:00
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
|
|
|
os_urandom = mock.Mock(return_value=EXTERNAL_ENTROPY)
|
|
|
|
with mock.patch("os.urandom", os_urandom), self.client:
|
|
|
|
self.client.set_expected_responses(
|
|
|
|
[
|
2018-10-22 12:44:36 +00:00
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
2018-10-02 15:35:51 +00:00
|
|
|
proto.EntropyRequest(),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
2019-07-26 09:41:47 +00:00
|
|
|
proto.ButtonRequest(code=B.Success),
|
|
|
|
proto.ButtonRequest(code=B.Success),
|
2018-10-02 15:35:51 +00:00
|
|
|
proto.Success(),
|
|
|
|
proto.Features(),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
self.client.set_input_flow(input_flow)
|
|
|
|
|
|
|
|
# No PIN, no passphrase, don't display random
|
|
|
|
device.reset(
|
|
|
|
self.client,
|
2018-08-13 16:21:24 +00:00
|
|
|
display_random=False,
|
|
|
|
strength=strength,
|
|
|
|
passphrase_protection=False,
|
|
|
|
pin_protection=False,
|
|
|
|
label="test",
|
2018-10-02 15:35:51 +00:00
|
|
|
language="english",
|
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
|
|
|
|
internal_entropy = self.client.debug.state().reset_entropy
|
|
|
|
entropy = generate_entropy(strength, internal_entropy, EXTERNAL_ENTROPY)
|
2018-08-13 16:21:24 +00:00
|
|
|
expected_mnemonic = Mnemonic("english").to_mnemonic(entropy)
|
2018-03-20 15:47:05 +00:00
|
|
|
|
|
|
|
# Compare that device generated proper mnemonic for given entropies
|
2018-08-13 16:21:24 +00:00
|
|
|
assert " ".join(words) == expected_mnemonic
|
2018-03-20 15:47:05 +00:00
|
|
|
|
|
|
|
# Check if device is properly initialized
|
|
|
|
resp = self.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
|
|
|
|
|
|
|
|
def test_reset_device_pin(self):
|
2018-10-02 15:35:51 +00:00
|
|
|
words = []
|
2018-03-20 15:47:05 +00:00
|
|
|
strength = 128
|
2018-10-02 15:35:51 +00:00
|
|
|
|
|
|
|
def input_flow():
|
2018-10-22 12:44:36 +00:00
|
|
|
# Confirm Reset
|
|
|
|
btn_code = yield
|
|
|
|
assert btn_code == B.ResetDevice
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
2018-10-02 15:35:51 +00:00
|
|
|
# Enter new PIN
|
|
|
|
yield
|
|
|
|
self.client.debug.input("654")
|
|
|
|
|
|
|
|
# Confirm PIN
|
|
|
|
yield
|
|
|
|
self.client.debug.input("654")
|
|
|
|
|
|
|
|
# Confirm entropy
|
|
|
|
btn_code = yield
|
|
|
|
assert btn_code == B.ResetDevice
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
|
|
|
# Backup your seed
|
|
|
|
btn_code = yield
|
|
|
|
assert btn_code == B.ResetDevice
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
2019-07-15 09:09:13 +00:00
|
|
|
# Confirm warning
|
|
|
|
btn_code = yield
|
|
|
|
assert btn_code == B.ResetDevice
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
2018-10-02 15:35:51 +00:00
|
|
|
# mnemonic phrases
|
|
|
|
btn_code = yield
|
|
|
|
assert btn_code == B.ResetDevice
|
|
|
|
# 12 words, 3 pages
|
|
|
|
for i in range(3):
|
2019-07-26 15:37:26 +00:00
|
|
|
words.extend(self.client.debug.read_reset_word().split())
|
2018-10-02 15:35:51 +00:00
|
|
|
if i < 2:
|
|
|
|
self.client.debug.swipe_down()
|
|
|
|
else:
|
|
|
|
# last page is confirmation
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
|
|
|
# check backup words
|
2019-07-08 07:37:36 +00:00
|
|
|
for _ in range(3):
|
2019-07-26 15:37:26 +00:00
|
|
|
index = self.client.debug.read_reset_word_pos()
|
2018-10-02 15:35:51 +00:00
|
|
|
self.client.debug.input(words[index])
|
|
|
|
|
2019-06-23 10:20:36 +00:00
|
|
|
# confirm recovery seed check
|
|
|
|
btn_code = yield
|
2019-07-26 09:41:47 +00:00
|
|
|
assert btn_code == B.Success
|
2019-06-23 10:20:36 +00:00
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
|
|
|
# confirm success
|
2018-10-02 15:35:51 +00:00
|
|
|
btn_code = yield
|
2019-07-26 09:41:47 +00:00
|
|
|
assert btn_code == B.Success
|
2018-10-02 15:35:51 +00:00
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
|
|
|
os_urandom = mock.Mock(return_value=EXTERNAL_ENTROPY)
|
|
|
|
with mock.patch("os.urandom", os_urandom), self.client:
|
|
|
|
self.client.set_expected_responses(
|
|
|
|
[
|
2018-10-22 12:44:36 +00:00
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
2018-10-02 15:35:51 +00:00
|
|
|
proto.ButtonRequest(code=B.Other),
|
|
|
|
proto.ButtonRequest(code=B.Other),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.EntropyRequest(),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
2019-07-26 09:41:47 +00:00
|
|
|
proto.ButtonRequest(code=B.Success),
|
|
|
|
proto.ButtonRequest(code=B.Success),
|
2018-10-02 15:35:51 +00:00
|
|
|
proto.Success(),
|
|
|
|
proto.Features(),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
self.client.set_input_flow(input_flow)
|
|
|
|
|
|
|
|
# PIN, passphrase, display random
|
|
|
|
device.reset(
|
|
|
|
self.client,
|
2018-08-13 16:21:24 +00:00
|
|
|
display_random=True,
|
|
|
|
strength=strength,
|
|
|
|
passphrase_protection=True,
|
|
|
|
pin_protection=True,
|
|
|
|
label="test",
|
2018-10-02 15:35:51 +00:00
|
|
|
language="english",
|
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
|
|
|
|
internal_entropy = self.client.debug.state().reset_entropy
|
|
|
|
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
|
2018-08-13 16:21:24 +00:00
|
|
|
assert " ".join(words) == expected_mnemonic
|
2018-03-20 15:47:05 +00:00
|
|
|
|
|
|
|
# Check if device is properly initialized
|
|
|
|
resp = self.client.call_raw(proto.Initialize())
|
|
|
|
assert resp.initialized is True
|
|
|
|
assert resp.needs_backup is False
|
|
|
|
assert resp.pin_protection is True
|
|
|
|
assert resp.passphrase_protection is True
|
|
|
|
|
|
|
|
def test_failed_pin(self):
|
|
|
|
# external_entropy = b'zlutoucky kun upel divoke ody' * 2
|
|
|
|
strength = 128
|
2018-08-13 16:21:24 +00:00
|
|
|
ret = self.client.call_raw(
|
|
|
|
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)
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
ret = self.client.call_raw(proto.ButtonAck())
|
|
|
|
|
2018-03-20 15:47:05 +00:00
|
|
|
# Enter PIN for first time
|
|
|
|
assert isinstance(ret, proto.ButtonRequest)
|
2018-08-13 16:21:24 +00:00
|
|
|
self.client.debug.input("654")
|
2018-03-20 15:47:05 +00:00
|
|
|
ret = self.client.call_raw(proto.ButtonAck())
|
|
|
|
|
|
|
|
# Enter PIN for second time
|
|
|
|
assert isinstance(ret, proto.ButtonRequest)
|
2018-08-13 16:21:24 +00:00
|
|
|
self.client.debug.input("456")
|
2018-03-20 15:47:05 +00:00
|
|
|
ret = self.client.call_raw(proto.ButtonAck())
|
|
|
|
|
|
|
|
assert isinstance(ret, proto.ButtonRequest)
|
|
|
|
|
|
|
|
def test_already_initialized(self):
|
|
|
|
self.setup_mnemonic_nopin_nopassphrase()
|
|
|
|
with pytest.raises(Exception):
|
2018-08-13 16:21:24 +00:00
|
|
|
device.reset(self.client, False, 128, True, True, "label", "english")
|