2019-07-19 13:50:42 +00:00
|
|
|
from itertools import combinations
|
2019-06-27 13:59:39 +00:00
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
import pytest
|
2019-07-19 13:50:42 +00:00
|
|
|
import shamir_mnemonic as shamir
|
|
|
|
from shamir_mnemonic import MnemonicError
|
2019-06-27 13:59:39 +00:00
|
|
|
|
|
|
|
from trezorlib import device, messages as proto
|
2019-07-22 09:36:29 +00:00
|
|
|
from trezorlib.messages import ButtonRequestType as B, ResetDeviceBackupType
|
2019-06-27 13:59:39 +00:00
|
|
|
|
2019-07-19 13:50:42 +00:00
|
|
|
from .common import TrezorTest, generate_entropy
|
2019-06-27 13:59:39 +00:00
|
|
|
|
|
|
|
EXTERNAL_ENTROPY = b"zlutoucky kun upel divoke ody" * 2
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skip_t1
|
|
|
|
class TestMsgResetDeviceT2(TrezorTest):
|
|
|
|
# TODO: test with different options
|
|
|
|
def test_reset_device_shamir(self):
|
|
|
|
strength = 128
|
2019-07-19 13:50:42 +00:00
|
|
|
member_threshold = 3
|
2019-06-27 13:59:39 +00:00
|
|
|
|
|
|
|
def input_flow():
|
|
|
|
# Confirm Reset
|
|
|
|
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()
|
|
|
|
|
2019-06-27 13:59:39 +00:00
|
|
|
# shares info
|
|
|
|
btn_code = yield
|
|
|
|
assert btn_code == B.ResetDevice
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
|
|
|
# Set & Confirm number of shares
|
|
|
|
btn_code = yield
|
|
|
|
assert btn_code == B.ResetDevice
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
|
|
|
# threshold info
|
|
|
|
btn_code = yield
|
|
|
|
assert btn_code == B.ResetDevice
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
|
|
|
# Set & confirm threshold value
|
|
|
|
btn_code = yield
|
|
|
|
assert btn_code == B.ResetDevice
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
|
|
|
# Confirm show seeds
|
|
|
|
btn_code = yield
|
|
|
|
assert btn_code == B.ResetDevice
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
|
|
|
# show & confirm shares
|
|
|
|
all_mnemonics = []
|
|
|
|
for h in range(5):
|
|
|
|
words = []
|
|
|
|
btn_code = yield
|
|
|
|
assert btn_code == B.Other
|
|
|
|
|
|
|
|
# mnemonic phrases
|
|
|
|
# 20 word over 6 pages for strength 128, 33 words over 9 pages for strength 256
|
|
|
|
for i in range(6):
|
2019-07-26 15:37:26 +00:00
|
|
|
words.extend(self.client.debug.read_reset_word().split())
|
2019-06-27 13:59:39 +00:00
|
|
|
if i < 5:
|
|
|
|
self.client.debug.swipe_down()
|
|
|
|
else:
|
|
|
|
# last page is confirmation
|
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
|
|
|
# check share
|
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()
|
2019-06-27 13:59:39 +00:00
|
|
|
self.client.debug.input(words[index])
|
|
|
|
|
|
|
|
all_mnemonics.extend([" ".join(words)])
|
|
|
|
|
|
|
|
# Confirm continue to next share
|
|
|
|
btn_code = yield
|
2019-07-26 09:41:47 +00:00
|
|
|
assert btn_code == B.Success
|
2019-06-27 13:59:39 +00:00
|
|
|
self.client.debug.press_yes()
|
|
|
|
|
|
|
|
# generate secret locally
|
2019-07-19 13:50:42 +00:00
|
|
|
internal_entropy = self.client.debug.state().reset_entropy
|
|
|
|
secret = generate_entropy(strength, internal_entropy, EXTERNAL_ENTROPY)
|
2019-06-27 13:59:39 +00:00
|
|
|
|
|
|
|
# validate that all combinations will result in the correct master secret
|
2019-07-19 13:50:42 +00:00
|
|
|
validate_mnemonics(all_mnemonics, member_threshold, secret)
|
2019-06-27 13:59:39 +00:00
|
|
|
|
|
|
|
# safety warning
|
|
|
|
btn_code = yield
|
2019-07-26 09:41:47 +00:00
|
|
|
assert btn_code == B.Success
|
2019-06-27 13:59:39 +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(
|
|
|
|
[
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.EntropyRequest(),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
2019-07-15 09:09:13 +00:00
|
|
|
proto.ButtonRequest(code=B.ResetDevice),
|
2019-06-27 13:59:39 +00:00
|
|
|
proto.ButtonRequest(code=B.Other),
|
2019-07-26 09:41:47 +00:00
|
|
|
proto.ButtonRequest(code=B.Success),
|
2019-06-27 13:59:39 +00:00
|
|
|
proto.ButtonRequest(code=B.Other),
|
2019-07-26 09:41:47 +00:00
|
|
|
proto.ButtonRequest(code=B.Success),
|
2019-06-27 13:59:39 +00:00
|
|
|
proto.ButtonRequest(code=B.Other),
|
2019-07-26 09:41:47 +00:00
|
|
|
proto.ButtonRequest(code=B.Success),
|
2019-06-27 13:59:39 +00:00
|
|
|
proto.ButtonRequest(code=B.Other),
|
2019-07-26 09:41:47 +00:00
|
|
|
proto.ButtonRequest(code=B.Success),
|
2019-06-27 13:59:39 +00:00
|
|
|
proto.ButtonRequest(code=B.Other),
|
2019-07-26 09:41:47 +00:00
|
|
|
proto.ButtonRequest(code=B.Success),
|
|
|
|
proto.ButtonRequest(code=B.Success),
|
2019-06-27 13:59:39 +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,
|
|
|
|
display_random=False,
|
|
|
|
strength=strength,
|
|
|
|
passphrase_protection=False,
|
|
|
|
pin_protection=False,
|
|
|
|
label="test",
|
|
|
|
language="english",
|
2019-07-22 09:36:29 +00:00
|
|
|
backup_type=ResetDeviceBackupType.Slip39_Single_Group,
|
2019-06-27 13:59:39 +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
|
|
|
|
|
|
|
|
|
2019-07-19 13:50:42 +00:00
|
|
|
def validate_mnemonics(mnemonics, threshold, expected_secret):
|
|
|
|
# We expect these combinations to recreate the secret properly
|
|
|
|
for test_group in combinations(mnemonics, threshold):
|
2019-08-12 10:27:02 +00:00
|
|
|
# TODO: HOTFIX, we should fix this properly by modifying and unifying the python-shamir-mnemonic API
|
|
|
|
ms = shamir.combine_mnemonics(test_group)
|
|
|
|
identifier, iteration_exponent, _, _, _ = shamir._decode_mnemonics(test_group)
|
|
|
|
ems = shamir._encrypt(ms, b"", iteration_exponent, identifier)
|
|
|
|
assert ems == expected_secret
|
2019-07-19 13:50:42 +00:00
|
|
|
# We expect these combinations to raise MnemonicError
|
|
|
|
for test_group in combinations(mnemonics, threshold - 1):
|
|
|
|
with pytest.raises(
|
|
|
|
MnemonicError, match=r".*Expected {} mnemonics.*".format(threshold)
|
|
|
|
):
|
2019-08-12 10:27:02 +00:00
|
|
|
shamir.combine_mnemonics(test_group)
|