2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2017-06-28 15:56:58 +00:00
|
|
|
#
|
2019-05-29 16:44:09 +00:00
|
|
|
# Copyright (C) 2012-2019 SatoshiLabs and contributors
|
2017-06-28 15:56:58 +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.
|
2017-06-28 15:56:58 +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-05-11 12:53:51 +00:00
|
|
|
import pytest
|
2018-08-13 16:21:24 +00:00
|
|
|
from mnemonic import Mnemonic
|
2017-06-28 15:56:58 +00:00
|
|
|
|
2017-12-12 15:40:11 +00:00
|
|
|
from trezorlib import messages as proto
|
2018-08-13 16:21:24 +00:00
|
|
|
|
|
|
|
from .common import TrezorTest, generate_entropy
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
|
2017-12-19 18:24:18 +00:00
|
|
|
@pytest.mark.skip_t2
|
2017-12-23 20:20:49 +00:00
|
|
|
class TestMsgResetDeviceSkipbackup(TrezorTest):
|
2017-06-28 15:56:58 +00:00
|
|
|
|
2018-10-11 15:26:20 +00:00
|
|
|
external_entropy = b"zlutoucky kun upel divoke ody" * 2
|
|
|
|
strength = 128
|
|
|
|
|
|
|
|
def test_reset_device_skip_backup(self):
|
2017-06-28 15:56:58 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
ret = self.client.call_raw(
|
|
|
|
proto.ResetDevice(
|
|
|
|
display_random=False,
|
2018-10-11 15:26:20 +00:00
|
|
|
strength=self.strength,
|
2018-08-13 16:21:24 +00:00
|
|
|
passphrase_protection=False,
|
|
|
|
pin_protection=False,
|
|
|
|
language="english",
|
|
|
|
label="test",
|
|
|
|
skip_backup=True,
|
|
|
|
)
|
|
|
|
)
|
2017-06-28 15:56:58 +00:00
|
|
|
|
2018-10-22 12:44:36 +00:00
|
|
|
assert isinstance(ret, proto.ButtonRequest)
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
ret = self.client.call_raw(proto.ButtonAck())
|
|
|
|
|
2017-06-28 15:56:58 +00:00
|
|
|
# Provide entropy
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.EntropyRequest)
|
2017-06-28 15:56:58 +00:00
|
|
|
internal_entropy = self.client.debug.read_reset_entropy()
|
2018-10-11 15:26:20 +00:00
|
|
|
ret = self.client.call_raw(proto.EntropyAck(entropy=self.external_entropy))
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.Success)
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
# Check if device is properly initialized
|
2018-10-04 15:37:49 +00:00
|
|
|
ret = self.client.call_raw(proto.Initialize())
|
|
|
|
assert ret.initialized is True
|
|
|
|
assert ret.needs_backup is True
|
|
|
|
assert ret.unfinished_backup is False
|
|
|
|
assert ret.no_backup is False
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
# Generate mnemonic locally
|
2018-10-11 15:26:20 +00:00
|
|
|
entropy = generate_entropy(
|
|
|
|
self.strength, internal_entropy, self.external_entropy
|
|
|
|
)
|
2018-08-13 16:21:24 +00:00
|
|
|
expected_mnemonic = Mnemonic("english").to_mnemonic(entropy)
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
# start Backup workflow
|
|
|
|
ret = self.client.call_raw(proto.BackupDevice())
|
|
|
|
|
|
|
|
mnemonic = []
|
2018-10-11 15:26:20 +00:00
|
|
|
for _ in range(self.strength // 32 * 3):
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.ButtonRequest)
|
2017-06-28 15:56:58 +00:00
|
|
|
mnemonic.append(self.client.debug.read_reset_word())
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
self.client.call_raw(proto.ButtonAck())
|
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
mnemonic = " ".join(mnemonic)
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
# Compare that device generated proper mnemonic for given entropies
|
2017-12-23 20:20:49 +00:00
|
|
|
assert mnemonic == expected_mnemonic
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
mnemonic = []
|
2018-10-11 15:26:20 +00:00
|
|
|
for _ in range(self.strength // 32 * 3):
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.ButtonRequest)
|
2017-06-28 15:56:58 +00:00
|
|
|
mnemonic.append(self.client.debug.read_reset_word())
|
|
|
|
self.client.debug.press_yes()
|
2018-10-04 15:37:49 +00:00
|
|
|
ret = self.client.call_raw(proto.ButtonAck())
|
2017-06-28 15:56:58 +00:00
|
|
|
|
2018-10-04 15:37:49 +00:00
|
|
|
assert isinstance(ret, proto.Success)
|
2017-06-28 15:56:58 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
mnemonic = " ".join(mnemonic)
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
# Compare that second pass printed out the same mnemonic once again
|
2017-12-23 20:20:49 +00:00
|
|
|
assert mnemonic == expected_mnemonic
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
# start backup again - should fail
|
|
|
|
ret = self.client.call_raw(proto.BackupDevice())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.Failure)
|
2017-06-28 15:56:58 +00:00
|
|
|
|
2017-07-05 11:03:06 +00:00
|
|
|
def test_reset_device_skip_backup_break(self):
|
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
ret = self.client.call_raw(
|
|
|
|
proto.ResetDevice(
|
|
|
|
display_random=False,
|
2018-10-11 15:26:20 +00:00
|
|
|
strength=self.strength,
|
2018-08-13 16:21:24 +00:00
|
|
|
passphrase_protection=False,
|
|
|
|
pin_protection=False,
|
|
|
|
language="english",
|
|
|
|
label="test",
|
|
|
|
skip_backup=True,
|
|
|
|
)
|
|
|
|
)
|
2017-07-05 11:03:06 +00:00
|
|
|
|
2018-10-22 12:44:36 +00:00
|
|
|
assert isinstance(ret, proto.ButtonRequest)
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
ret = self.client.call_raw(proto.ButtonAck())
|
|
|
|
|
2017-07-05 11:03:06 +00:00
|
|
|
# Provide entropy
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.EntropyRequest)
|
2018-10-11 15:26:20 +00:00
|
|
|
ret = self.client.call_raw(proto.EntropyAck(entropy=self.external_entropy))
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.Success)
|
2017-07-05 11:03:06 +00:00
|
|
|
|
|
|
|
# Check if device is properly initialized
|
2018-10-04 15:37:49 +00:00
|
|
|
ret = self.client.call_raw(proto.Initialize())
|
|
|
|
assert ret.initialized is True
|
|
|
|
assert ret.needs_backup is True
|
|
|
|
assert ret.unfinished_backup is False
|
|
|
|
assert ret.no_backup is False
|
2017-07-05 11:03:06 +00:00
|
|
|
|
|
|
|
# start Backup workflow
|
|
|
|
ret = self.client.call_raw(proto.BackupDevice())
|
|
|
|
|
|
|
|
# send Initialize -> break workflow
|
|
|
|
ret = self.client.call_raw(proto.Initialize())
|
2018-10-04 15:37:49 +00:00
|
|
|
assert isinstance(ret, proto.Features)
|
|
|
|
assert ret.initialized is True
|
|
|
|
assert ret.needs_backup is False
|
|
|
|
assert ret.unfinished_backup is True
|
|
|
|
assert ret.no_backup is False
|
2017-07-05 11:03:06 +00:00
|
|
|
|
|
|
|
# start backup again - should fail
|
|
|
|
ret = self.client.call_raw(proto.BackupDevice())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.Failure)
|
2017-07-05 11:03:06 +00:00
|
|
|
|
2018-10-04 15:37:49 +00:00
|
|
|
# read Features again
|
|
|
|
ret = self.client.call_raw(proto.Initialize())
|
|
|
|
assert isinstance(ret, proto.Features)
|
|
|
|
assert ret.initialized is True
|
|
|
|
assert ret.needs_backup is False
|
|
|
|
assert ret.unfinished_backup is True
|
|
|
|
assert ret.no_backup is False
|
|
|
|
|
2017-06-28 15:56:58 +00:00
|
|
|
def test_initialized_device_backup_fail(self):
|
|
|
|
self.setup_mnemonic_nopin_nopassphrase()
|
|
|
|
ret = self.client.call_raw(proto.BackupDevice())
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.Failure)
|
2018-10-11 13:29:30 +00:00
|
|
|
|
|
|
|
def test_reset_device_skip_backup_show_entropy_fail(self):
|
|
|
|
ret = self.client.call_raw(
|
|
|
|
proto.ResetDevice(
|
|
|
|
display_random=True,
|
|
|
|
strength=self.strength,
|
|
|
|
passphrase_protection=False,
|
|
|
|
pin_protection=False,
|
|
|
|
language="english",
|
|
|
|
label="test",
|
|
|
|
skip_backup=True,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
assert isinstance(ret, proto.Failure)
|