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
|
|
|
|
2019-09-11 12:43:32 +00:00
|
|
|
from ..common import generate_entropy
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
|
2017-12-19 18:24:18 +00:00
|
|
|
@pytest.mark.skip_t2
|
2019-09-11 12:29:39 +00:00
|
|
|
class TestMsgResetDeviceSkipbackup:
|
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
|
|
|
|
|
2019-08-27 14:58:59 +00:00
|
|
|
@pytest.mark.setup_client(uninitialized=True)
|
|
|
|
def test_reset_device_skip_backup(self, client):
|
|
|
|
ret = client.call_raw(
|
2018-08-13 16:21:24 +00:00
|
|
|
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,
|
2019-12-07 11:11:51 +00:00
|
|
|
language="en-US",
|
2018-08-13 16:21:24 +00:00
|
|
|
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)
|
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
|
|
|
|
2017-06-28 15:56:58 +00:00
|
|
|
# Provide entropy
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.EntropyRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
internal_entropy = client.debug.read_reset_entropy()
|
|
|
|
ret = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = client.call_raw(proto.Initialize())
|
2018-10-04 15:37:49 +00:00
|
|
|
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
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = client.call_raw(proto.BackupDevice())
|
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)
|
2019-08-27 14:58:59 +00:00
|
|
|
mnemonic.append(client.debug.read_reset_word())
|
|
|
|
client.debug.press_yes()
|
|
|
|
client.call_raw(proto.ButtonAck())
|
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 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)
|
2019-08-27 14:58:59 +00:00
|
|
|
mnemonic.append(client.debug.read_reset_word())
|
|
|
|
client.debug.press_yes()
|
|
|
|
ret = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = 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
|
|
|
|
2019-08-27 14:58:59 +00:00
|
|
|
@pytest.mark.setup_client(uninitialized=True)
|
|
|
|
def test_reset_device_skip_backup_break(self, client):
|
|
|
|
ret = client.call_raw(
|
2018-08-13 16:21:24 +00:00
|
|
|
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,
|
2019-12-07 11:11:51 +00:00
|
|
|
language="en-US",
|
2018-08-13 16:21:24 +00:00
|
|
|
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)
|
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
|
|
|
|
2017-07-05 11:03:06 +00:00
|
|
|
# Provide entropy
|
2017-12-23 20:20:49 +00:00
|
|
|
assert isinstance(ret, proto.EntropyRequest)
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = client.call_raw(proto.Initialize())
|
2018-10-04 15:37:49 +00:00
|
|
|
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
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = client.call_raw(proto.BackupDevice())
|
2017-07-05 11:03:06 +00:00
|
|
|
|
|
|
|
# send Initialize -> break workflow
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = 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
|
2019-08-27 14:58:59 +00:00
|
|
|
ret = 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
|
|
|
|
|
2019-08-27 14:58:59 +00:00
|
|
|
def test_initialized_device_backup_fail(self, client):
|
|
|
|
ret = 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
|
|
|
|
2019-08-27 14:58:59 +00:00
|
|
|
@pytest.mark.setup_client(uninitialized=True)
|
|
|
|
def test_reset_device_skip_backup_show_entropy_fail(self, client):
|
|
|
|
ret = client.call_raw(
|
2018-10-11 13:29:30 +00:00
|
|
|
proto.ResetDevice(
|
|
|
|
display_random=True,
|
|
|
|
strength=self.strength,
|
|
|
|
passphrase_protection=False,
|
|
|
|
pin_protection=False,
|
2019-12-07 11:11:51 +00:00
|
|
|
language="en-US",
|
2018-10-11 13:29:30 +00:00
|
|
|
label="test",
|
|
|
|
skip_backup=True,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
assert isinstance(ret, proto.Failure)
|