2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2016-11-25 21:53:55 +00:00
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# Copyright (C) 2012-2018 SatoshiLabs and contributors
|
2016-11-25 21:53:55 +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.
|
2016-11-25 21:53:55 +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>.
|
2016-11-25 21:53:55 +00:00
|
|
|
|
2018-08-10 11:33:14 +00:00
|
|
|
import binascii
|
|
|
|
|
|
|
|
from mnemonic import Mnemonic
|
2016-05-20 20:27:20 +00:00
|
|
|
|
2017-12-12 15:40:11 +00:00
|
|
|
from . import messages as proto
|
2018-08-10 11:33:14 +00:00
|
|
|
from . import tools
|
|
|
|
from .tools import expect
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2012-12-13 19:05:04 +00:00
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
def pin_info(pin):
|
2016-05-05 01:16:17 +00:00
|
|
|
print("Device asks for PIN %s" % pin)
|
2012-12-03 15:36:03 +00:00
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
def button_press(yes_no):
|
2016-05-05 01:16:17 +00:00
|
|
|
print("User pressed", '"y"' if yes_no else '"n"')
|
2014-02-06 22:34:13 +00:00
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
class DebugLink(object):
|
2013-09-01 00:46:53 +00:00
|
|
|
def __init__(self, transport, pin_func=pin_info, button_func=button_press):
|
2012-12-03 15:36:03 +00:00
|
|
|
self.transport = transport
|
2016-11-15 12:46:00 +00:00
|
|
|
self.transport.session_begin()
|
2012-12-13 19:05:04 +00:00
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
self.pin_func = pin_func
|
|
|
|
self.button_func = button_func
|
2014-02-06 22:34:13 +00:00
|
|
|
|
2014-02-13 15:46:21 +00:00
|
|
|
def close(self):
|
2016-11-15 12:46:00 +00:00
|
|
|
self.transport.session_end()
|
2016-01-12 23:17:38 +00:00
|
|
|
|
2014-02-25 18:31:31 +00:00
|
|
|
def _call(self, msg, nowait=False):
|
|
|
|
self.transport.write(msg)
|
|
|
|
if nowait:
|
2018-05-09 16:11:38 +00:00
|
|
|
return None
|
2017-08-24 12:29:27 +00:00
|
|
|
ret = self.transport.read()
|
2014-02-25 18:31:31 +00:00
|
|
|
return ret
|
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
def read_pin(self):
|
2014-02-25 18:31:31 +00:00
|
|
|
obj = self._call(proto.DebugLinkGetState())
|
2016-05-05 01:16:17 +00:00
|
|
|
print("Read PIN:", obj.pin)
|
|
|
|
print("Read matrix:", obj.matrix)
|
2014-02-06 22:34:13 +00:00
|
|
|
|
2013-09-01 01:34:36 +00:00
|
|
|
return (obj.pin, obj.matrix)
|
2014-02-06 22:34:13 +00:00
|
|
|
|
2013-09-01 01:34:36 +00:00
|
|
|
def read_pin_encoded(self):
|
2014-02-20 18:15:43 +00:00
|
|
|
pin, _ = self.read_pin()
|
|
|
|
pin_encoded = self.encode_pin(pin)
|
|
|
|
self.pin_func(pin_encoded)
|
|
|
|
return pin_encoded
|
|
|
|
|
|
|
|
def encode_pin(self, pin):
|
|
|
|
_, matrix = self.read_pin()
|
2014-02-06 22:34:13 +00:00
|
|
|
|
2013-09-01 00:46:53 +00:00
|
|
|
# Now we have real PIN and PIN matrix.
|
|
|
|
# We have to encode that into encoded pin,
|
|
|
|
# because application must send back positions
|
|
|
|
# on keypad, not a real PIN.
|
2016-05-26 15:20:44 +00:00
|
|
|
pin_encoded = ''.join([str(matrix.index(p) + 1) for p in pin])
|
2014-02-06 22:34:13 +00:00
|
|
|
|
2016-05-05 01:16:17 +00:00
|
|
|
print("Encoded PIN:", pin_encoded)
|
2013-09-01 00:46:53 +00:00
|
|
|
return pin_encoded
|
2016-01-12 23:17:38 +00:00
|
|
|
|
2014-02-06 22:34:13 +00:00
|
|
|
def read_layout(self):
|
2014-02-25 18:31:31 +00:00
|
|
|
obj = self._call(proto.DebugLinkGetState())
|
2014-02-06 22:34:13 +00:00
|
|
|
return obj.layout
|
|
|
|
|
|
|
|
def read_mnemonic(self):
|
2014-02-25 18:31:31 +00:00
|
|
|
obj = self._call(proto.DebugLinkGetState())
|
2014-02-06 22:34:13 +00:00
|
|
|
return obj.mnemonic
|
|
|
|
|
|
|
|
def read_node(self):
|
2014-02-25 18:31:31 +00:00
|
|
|
obj = self._call(proto.DebugLinkGetState())
|
2014-02-06 22:34:13 +00:00
|
|
|
return obj.node
|
|
|
|
|
2014-03-07 16:25:55 +00:00
|
|
|
def read_recovery_word(self):
|
2014-02-25 18:31:31 +00:00
|
|
|
obj = self._call(proto.DebugLinkGetState())
|
2014-03-07 16:25:55 +00:00
|
|
|
return (obj.recovery_fake_word, obj.recovery_word_pos)
|
2014-02-20 18:15:43 +00:00
|
|
|
|
2014-03-07 16:25:55 +00:00
|
|
|
def read_reset_word(self):
|
2014-02-25 18:31:31 +00:00
|
|
|
obj = self._call(proto.DebugLinkGetState())
|
2014-03-07 16:25:55 +00:00
|
|
|
return obj.reset_word
|
|
|
|
|
2018-03-20 15:46:53 +00:00
|
|
|
def read_reset_word_pos(self):
|
|
|
|
obj = self._call(proto.DebugLinkGetState())
|
|
|
|
return obj.reset_word_pos
|
|
|
|
|
2014-03-07 16:25:55 +00:00
|
|
|
def read_reset_entropy(self):
|
|
|
|
obj = self._call(proto.DebugLinkGetState())
|
|
|
|
return obj.reset_entropy
|
2014-02-20 18:15:43 +00:00
|
|
|
|
2014-02-17 01:16:43 +00:00
|
|
|
def read_passphrase_protection(self):
|
2014-02-25 18:31:31 +00:00
|
|
|
obj = self._call(proto.DebugLinkGetState())
|
2014-02-17 01:16:43 +00:00
|
|
|
return obj.passphrase_protection
|
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
def press_button(self, yes_no):
|
2016-05-05 01:16:17 +00:00
|
|
|
print("Pressing", yes_no)
|
2012-12-03 15:36:03 +00:00
|
|
|
self.button_func(yes_no)
|
2014-02-25 18:31:31 +00:00
|
|
|
self._call(proto.DebugLinkDecision(yes_no=yes_no), nowait=True)
|
2012-12-13 19:05:04 +00:00
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
def press_yes(self):
|
|
|
|
self.press_button(True)
|
2014-02-06 22:34:13 +00:00
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
def press_no(self):
|
2013-10-11 01:51:45 +00:00
|
|
|
self.press_button(False)
|
|
|
|
|
2018-03-20 15:46:53 +00:00
|
|
|
def swipe(self, up_down):
|
|
|
|
print("Swiping", up_down)
|
|
|
|
self._call(proto.DebugLinkDecision(up_down=up_down), nowait=True)
|
|
|
|
|
|
|
|
def swipe_up(self):
|
|
|
|
self.swipe(True)
|
|
|
|
|
|
|
|
def swipe_down(self):
|
|
|
|
self.swipe(False)
|
|
|
|
|
|
|
|
def input(self, text):
|
|
|
|
self._call(proto.DebugLinkDecision(input=text), nowait=True)
|
|
|
|
|
2013-10-11 01:51:45 +00:00
|
|
|
def stop(self):
|
2014-02-25 18:31:31 +00:00
|
|
|
self._call(proto.DebugLinkStop(), nowait=True)
|
2016-05-26 18:46:40 +00:00
|
|
|
|
|
|
|
def memory_read(self, address, length):
|
|
|
|
obj = self._call(proto.DebugLinkMemoryRead(address=address, length=length))
|
|
|
|
return obj.memory
|
|
|
|
|
|
|
|
def memory_write(self, address, memory, flash=False):
|
|
|
|
self._call(proto.DebugLinkMemoryWrite(address=address, memory=memory, flash=flash), nowait=True)
|
|
|
|
|
|
|
|
def flash_erase(self, sector):
|
2017-06-23 19:31:42 +00:00
|
|
|
self._call(proto.DebugLinkFlashErase(sector=sector), nowait=True)
|
2018-08-10 11:33:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
@expect(proto.Success, field="message")
|
|
|
|
def load_device_by_mnemonic(client, mnemonic, pin, passphrase_protection, label, language='english', skip_checksum=False, expand=False):
|
|
|
|
# Convert mnemonic to UTF8 NKFD
|
|
|
|
mnemonic = Mnemonic.normalize_string(mnemonic)
|
|
|
|
|
|
|
|
# Convert mnemonic to ASCII stream
|
|
|
|
mnemonic = mnemonic.encode('utf-8')
|
|
|
|
|
|
|
|
m = Mnemonic('english')
|
|
|
|
|
|
|
|
if expand:
|
|
|
|
mnemonic = m.expand(mnemonic)
|
|
|
|
|
|
|
|
if not skip_checksum and not m.check(mnemonic):
|
|
|
|
raise ValueError("Invalid mnemonic checksum")
|
|
|
|
|
|
|
|
if client.features.initialized:
|
|
|
|
raise RuntimeError("Device is initialized already. Call wipe_device() and try again.")
|
|
|
|
|
|
|
|
resp = client.call(proto.LoadDevice(mnemonic=mnemonic, pin=pin,
|
|
|
|
passphrase_protection=passphrase_protection,
|
|
|
|
language=language,
|
|
|
|
label=label,
|
|
|
|
skip_checksum=skip_checksum))
|
|
|
|
client.init_device()
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
|
|
@expect(proto.Success, field="message")
|
|
|
|
def load_device_by_xprv(client, xprv, pin, passphrase_protection, label, language):
|
|
|
|
if client.features.initialized:
|
|
|
|
raise RuntimeError("Device is initialized already. Call wipe_device() and try again.")
|
|
|
|
|
|
|
|
if xprv[0:4] not in ('xprv', 'tprv'):
|
|
|
|
raise ValueError("Unknown type of xprv")
|
|
|
|
|
|
|
|
if not 100 < len(xprv) < 112: # yes this is correct in Python
|
|
|
|
raise ValueError("Invalid length of xprv")
|
|
|
|
|
|
|
|
node = proto.HDNodeType()
|
|
|
|
data = binascii.hexlify(tools.b58decode(xprv, None))
|
|
|
|
|
|
|
|
if data[90:92] != b'00':
|
|
|
|
raise ValueError("Contain invalid private key")
|
|
|
|
|
|
|
|
checksum = binascii.hexlify(tools.btc_hash(binascii.unhexlify(data[:156]))[:4])
|
|
|
|
if checksum != data[156:]:
|
|
|
|
raise ValueError("Checksum doesn't match")
|
|
|
|
|
|
|
|
# version 0488ade4
|
|
|
|
# depth 00
|
|
|
|
# fingerprint 00000000
|
|
|
|
# child_num 00000000
|
|
|
|
# chaincode 873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508
|
|
|
|
# privkey 00e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35
|
|
|
|
# checksum e77e9d71
|
|
|
|
|
|
|
|
node.depth = int(data[8:10], 16)
|
|
|
|
node.fingerprint = int(data[10:18], 16)
|
|
|
|
node.child_num = int(data[18:26], 16)
|
|
|
|
node.chain_code = binascii.unhexlify(data[26:90])
|
|
|
|
node.private_key = binascii.unhexlify(data[92:156]) # skip 0x00 indicating privkey
|
|
|
|
|
|
|
|
resp = client.call(proto.LoadDevice(node=node,
|
|
|
|
pin=pin,
|
|
|
|
passphrase_protection=passphrase_protection,
|
|
|
|
language=language,
|
|
|
|
label=label))
|
|
|
|
client.init_device()
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
|
|
@expect(proto.Success, field="message")
|
|
|
|
def self_test(client):
|
2018-08-10 13:18:34 +00:00
|
|
|
if client.features.bootloader_mode is not True:
|
2018-08-10 11:33:14 +00:00
|
|
|
raise RuntimeError("Device must be in bootloader mode")
|
|
|
|
|
|
|
|
return client.call(proto.SelfTest(payload=b'\x00\xFF\x55\xAA\x66\x99\x33\xCCABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\x00\xFF\x55\xAA\x66\x99\x33\xCC'))
|