2016-05-20 20:27:20 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-05-05 01:16:17 +00:00
|
|
|
from . import messages_pb2 as proto
|
|
|
|
from .transport import NotImplementedException
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2014-02-25 18:31:31 +00:00
|
|
|
def pprint(msg):
|
2016-05-20 11:55:43 +00:00
|
|
|
return "<%s> (%d bytes):\n%s" % (msg.__class__.__name__, msg.ByteSize(), msg)
|
2014-02-25 18:31:31 +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
|
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):
|
|
|
|
self.transport.close()
|
2016-01-12 23:17:38 +00:00
|
|
|
|
2014-02-25 18:31:31 +00:00
|
|
|
def _call(self, msg, nowait=False):
|
2016-05-05 01:16:17 +00:00
|
|
|
print("DEBUGLINK SEND", pprint(msg))
|
2014-02-25 18:31:31 +00:00
|
|
|
self.transport.write(msg)
|
|
|
|
if nowait:
|
|
|
|
return
|
|
|
|
ret = self.transport.read_blocking()
|
2016-05-05 01:16:17 +00:00
|
|
|
print("DEBUGLINK RECV", pprint(ret))
|
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.
|
2013-09-01 01:34:36 +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
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
def stop(self):
|
2014-02-25 18:31:31 +00:00
|
|
|
self._call(proto.DebugLinkStop(), nowait=True)
|