2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# Copyright (C) 2012-2018 SatoshiLabs and contributors
|
2017-01-03 18:40: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.
|
2017-01-03 18:40: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>.
|
2017-01-03 18:40:05 +00:00
|
|
|
|
2017-12-19 09:11:35 +00:00
|
|
|
import os
|
2017-06-28 15:56:58 +00:00
|
|
|
|
2018-06-04 16:34:32 +00:00
|
|
|
from . import conftest
|
|
|
|
|
2018-04-10 09:42:21 +00:00
|
|
|
from trezorlib import coins
|
|
|
|
from trezorlib import tx_api
|
2018-05-09 16:12:31 +00:00
|
|
|
from trezorlib.client import TrezorClientDebugLink
|
2018-08-10 12:04:58 +00:00
|
|
|
from trezorlib import debuglink
|
|
|
|
from trezorlib import device
|
2013-01-14 13:44:31 +00:00
|
|
|
|
2017-12-19 09:11:35 +00:00
|
|
|
tests_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
tx_api.cache_dir = os.path.join(tests_dir, '../txcache')
|
2016-11-28 15:01:45 +00:00
|
|
|
|
|
|
|
|
2018-03-02 17:25:39 +00:00
|
|
|
class TrezorTest:
|
2018-08-10 12:04:58 +00:00
|
|
|
# 1 2 3 4 5 6 7 8 9 10 11 12
|
|
|
|
mnemonic12 = 'alcohol woman abuse must during monitor noble actual mixed trade anger aisle'
|
|
|
|
mnemonic18 = 'owner little vague addict embark decide pink prosper true fork panda embody mixture exchange choose canoe electric jewel'
|
|
|
|
mnemonic24 = 'dignity pass list indicate nasty swamp pool script soccer toe leaf photo multiply desk host tomato cradle drill spread actor shine dismiss champion exotic'
|
|
|
|
mnemonic_all = ' '.join(['all'] * 12)
|
|
|
|
|
|
|
|
pin4 = '1234'
|
|
|
|
pin6 = '789456'
|
|
|
|
pin8 = '45678978'
|
2017-06-28 15:56:58 +00:00
|
|
|
|
2017-12-23 20:20:49 +00:00
|
|
|
def setup_method(self, method):
|
2018-06-04 16:34:32 +00:00
|
|
|
wirelink = conftest.get_device()
|
2018-03-02 17:25:39 +00:00
|
|
|
debuglink = wirelink.find_debug()
|
2017-09-04 11:36:08 +00:00
|
|
|
self.client = TrezorClientDebugLink(wirelink)
|
|
|
|
self.client.set_debuglink(debuglink)
|
2018-04-10 09:42:21 +00:00
|
|
|
self.client.set_tx_api(coins.tx_api['Bitcoin'])
|
2014-12-10 14:26:18 +00:00
|
|
|
# self.client.set_buttonwait(3)
|
2013-09-13 03:28:56 +00:00
|
|
|
|
2018-08-10 12:04:58 +00:00
|
|
|
device.wipe(self.client)
|
2017-10-31 16:04:27 +00:00
|
|
|
self.client.transport.session_begin()
|
2013-09-13 03:28:56 +00:00
|
|
|
|
2017-12-23 20:20:49 +00:00
|
|
|
def teardown_method(self, method):
|
|
|
|
self.client.transport.session_end()
|
|
|
|
self.client.close()
|
2014-02-17 00:54:54 +00:00
|
|
|
|
2018-08-10 12:04:58 +00:00
|
|
|
def _setup_mnemonic(self, mnemonic=None, pin='', passphrase=False):
|
|
|
|
if mnemonic is None:
|
|
|
|
mnemonic = TrezorTest.mnemonic12
|
|
|
|
debuglink.load_device_by_mnemonic(
|
|
|
|
self.client,
|
|
|
|
mnemonic=mnemonic,
|
|
|
|
pin=pin,
|
|
|
|
passphrase_protection=passphrase,
|
|
|
|
label="test",
|
|
|
|
language="english",
|
|
|
|
)
|
|
|
|
|
2016-10-21 13:24:30 +00:00
|
|
|
def setup_mnemonic_allallall(self):
|
2018-08-10 12:04:58 +00:00
|
|
|
self._setup_mnemonic(mnemonic=TrezorTest.mnemonic_all)
|
2016-10-21 13:24:30 +00:00
|
|
|
|
2014-02-17 00:54:54 +00:00
|
|
|
def setup_mnemonic_nopin_nopassphrase(self):
|
2018-08-10 12:04:58 +00:00
|
|
|
self._setup_mnemonic()
|
2014-02-17 00:54:54 +00:00
|
|
|
|
2018-02-26 14:18:05 +00:00
|
|
|
def setup_mnemonic_nopin_passphrase(self):
|
2018-08-10 12:04:58 +00:00
|
|
|
self._setup_mnemonic(passphrase=True)
|
2018-02-26 14:18:05 +00:00
|
|
|
|
2014-12-13 18:07:30 +00:00
|
|
|
def setup_mnemonic_pin_nopassphrase(self):
|
2018-08-10 12:04:58 +00:00
|
|
|
self._setup_mnemonic(pin=TrezorTest.pin4)
|
2014-12-13 18:07:30 +00:00
|
|
|
|
2014-02-17 00:54:54 +00:00
|
|
|
def setup_mnemonic_pin_passphrase(self):
|
2018-08-10 12:04:58 +00:00
|
|
|
self._setup_mnemonic(pin=TrezorTest.pin4, passphrase=True)
|
2014-02-17 00:54:54 +00:00
|
|
|
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
def generate_entropy(strength, internal_entropy, external_entropy):
|
|
|
|
'''
|
|
|
|
strength - length of produced seed. One of 128, 192, 256
|
|
|
|
random - binary stream of random data from external HRNG
|
|
|
|
'''
|
2017-12-23 20:20:49 +00:00
|
|
|
import hashlib
|
|
|
|
|
2017-06-28 15:56:58 +00:00
|
|
|
if strength not in (128, 192, 256):
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError("Invalid strength")
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
if not internal_entropy:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError("Internal entropy is not provided")
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
if len(internal_entropy) < 32:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError("Internal entropy too short")
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
if not external_entropy:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError("External entropy is not provided")
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
if len(external_entropy) < 32:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError("External entropy too short")
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
entropy = hashlib.sha256(internal_entropy + external_entropy).digest()
|
|
|
|
entropy_stripped = entropy[:strength // 8]
|
|
|
|
|
|
|
|
if len(entropy_stripped) * 8 != strength:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError("Entropy length mismatch")
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
return entropy_stripped
|