2017-01-03 18:40:05 +00:00
|
|
|
# This file is part of the TREZOR project.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012-2016 Marek Palatinus <slush@satoshilabs.com>
|
|
|
|
# Copyright (C) 2012-2016 Pavol Rusnak <stick@satoshilabs.com>
|
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-05-20 20:27:20 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2013-01-14 13:44:31 +00:00
|
|
|
import unittest
|
2017-06-28 15:56:58 +00:00
|
|
|
import hashlib
|
|
|
|
|
2017-07-01 15:59:11 +00:00
|
|
|
from trezorlib.client import TrezorClient, TrezorClientDebugLink
|
2016-11-28 15:01:45 +00:00
|
|
|
from trezorlib import tx_api
|
2016-05-26 15:20:44 +00:00
|
|
|
import config
|
2013-01-14 13:44:31 +00:00
|
|
|
|
2016-11-28 15:01:45 +00:00
|
|
|
|
|
|
|
tx_api.cache_dir = '../txcache'
|
|
|
|
|
|
|
|
|
2013-09-13 03:28:56 +00:00
|
|
|
class TrezorTest(unittest.TestCase):
|
2017-06-28 15:56:58 +00:00
|
|
|
|
2013-01-14 13:44:31 +00:00
|
|
|
def setUp(self):
|
2016-11-12 14:06:32 +00:00
|
|
|
transport = config.TRANSPORT(*config.TRANSPORT_ARGS, **config.TRANSPORT_KWARGS)
|
|
|
|
if hasattr(config, 'DEBUG_TRANSPORT'):
|
|
|
|
debug_transport = config.DEBUG_TRANSPORT(*config.DEBUG_TRANSPORT_ARGS, **config.DEBUG_TRANSPORT_KWARGS)
|
2017-07-01 15:59:11 +00:00
|
|
|
self.client = TrezorClientDebugLink(transport)
|
2016-11-12 14:06:32 +00:00
|
|
|
self.client.set_debuglink(debug_transport)
|
|
|
|
else:
|
|
|
|
self.client = TrezorClient(transport)
|
2016-11-28 15:01:45 +00:00
|
|
|
self.client.set_tx_api(tx_api.TxApiBitcoin)
|
2014-12-10 14:26:18 +00:00
|
|
|
# self.client.set_buttonwait(3)
|
2013-09-13 03:28:56 +00:00
|
|
|
|
2014-03-07 20:44:27 +00:00
|
|
|
# 1 2 3 4 5 6 7 8 9 10 11 12
|
2014-02-17 00:54:54 +00:00
|
|
|
self.mnemonic12 = 'alcohol woman abuse must during monitor noble actual mixed trade anger aisle'
|
|
|
|
self.mnemonic18 = 'owner little vague addict embark decide pink prosper true fork panda embody mixture exchange choose canoe electric jewel'
|
|
|
|
self.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'
|
2016-10-21 13:24:30 +00:00
|
|
|
self.mnemonic_all = ' '.join(['all'] * 12)
|
2013-09-13 03:28:56 +00:00
|
|
|
|
2014-02-17 00:54:54 +00:00
|
|
|
self.pin4 = '1234'
|
|
|
|
self.pin6 = '789456'
|
|
|
|
self.pin8 = '45678978'
|
2013-09-13 03:28:56 +00:00
|
|
|
|
2014-02-17 00:54:54 +00:00
|
|
|
self.client.wipe_device()
|
2013-09-13 03:28:56 +00:00
|
|
|
|
2016-05-05 01:16:17 +00:00
|
|
|
print("Setup finished")
|
|
|
|
print("--------------")
|
2014-02-17 00:54:54 +00:00
|
|
|
|
2016-10-21 13:24:30 +00:00
|
|
|
def setup_mnemonic_allallall(self):
|
|
|
|
self.client.load_device_by_mnemonic(mnemonic=self.mnemonic_all, pin='', passphrase_protection=False, label='test', language='english')
|
|
|
|
|
2014-02-17 00:54:54 +00:00
|
|
|
def setup_mnemonic_nopin_nopassphrase(self):
|
|
|
|
self.client.load_device_by_mnemonic(mnemonic=self.mnemonic12, pin='', passphrase_protection=False, label='test', language='english')
|
|
|
|
|
2014-12-13 18:07:30 +00:00
|
|
|
def setup_mnemonic_pin_nopassphrase(self):
|
|
|
|
self.client.load_device_by_mnemonic(mnemonic=self.mnemonic12, pin=self.pin4, passphrase_protection=False, label='test', language='english')
|
|
|
|
|
2014-02-17 00:54:54 +00:00
|
|
|
def setup_mnemonic_pin_passphrase(self):
|
|
|
|
self.client.load_device_by_mnemonic(mnemonic=self.mnemonic12, pin=self.pin4, passphrase_protection=True, label='test', language='english')
|
|
|
|
|
2013-01-14 13:44:31 +00:00
|
|
|
def tearDown(self):
|
2014-02-13 16:20:40 +00:00
|
|
|
self.client.close()
|
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
|
|
|
|
'''
|
|
|
|
if strength not in (128, 192, 256):
|
|
|
|
raise Exception("Invalid strength")
|
|
|
|
|
|
|
|
if not internal_entropy:
|
|
|
|
raise Exception("Internal entropy is not provided")
|
|
|
|
|
|
|
|
if len(internal_entropy) < 32:
|
|
|
|
raise Exception("Internal entropy too short")
|
|
|
|
|
|
|
|
if not external_entropy:
|
|
|
|
raise Exception("External entropy is not provided")
|
|
|
|
|
|
|
|
if len(external_entropy) < 32:
|
|
|
|
raise Exception("External entropy too short")
|
|
|
|
|
|
|
|
entropy = hashlib.sha256(internal_entropy + external_entropy).digest()
|
|
|
|
entropy_stripped = entropy[:strength // 8]
|
|
|
|
|
|
|
|
if len(entropy_stripped) * 8 != strength:
|
|
|
|
raise Exception("Entropy length mismatch")
|
|
|
|
|
|
|
|
return entropy_stripped
|