1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-23 16:08:17 +00:00

fixes for latest V2 format, add checksum

This commit is contained in:
Jan Pochyla 2016-08-05 13:11:24 +02:00
parent 8bb7f550ad
commit e9427b04f3

View File

@ -162,21 +162,27 @@ class TransportV2(Transport):
def write(self, msg): def write(self, msg):
data = bytearray(msg.SerializeToString()) data = bytearray(msg.SerializeToString())
# Convert to unsigned in python2
checksum = 0 # binascii.crc32(data) & 0xffffffff
header1 = struct.pack(">L", self.session_id) header1 = struct.pack(">L", self.session_id)
header2 = struct.pack(">LL", mapping.get_type(msg), len(data)) header2 = struct.pack(">LL", mapping.get_type(msg), len(data))
footer = struct.pack(">L", checksum)
data = header2 + data data = header2 + data + footer
first = True first = True
while len(data): while len(data):
if first: if first:
# Magic characters, header1, header2, data padded to 64 bytes # Magic character, header1, header2, data padded to 64 bytes
datalen = 62 - len(header1)
chunk = b'?!' + header1 + data[:datalen] + b'\0' * (datalen - len(data[:datalen]))
else:
# Magic characters, header1, data padded to 64 bytes
datalen = 63 - len(header1) datalen = 63 - len(header1)
chunk = b'?' + header1 + data[:datalen] + b'\0' * (datalen - len(data[:datalen])) chunk = b'!' + header1 + \
data[:datalen] + b'\0' * (datalen - len(data[:datalen]))
else:
# Magic character, header1, data padded to 64 bytes
datalen = 63 - len(header1)
chunk = b'#' + header1 + \
data[:datalen] + b'\0' * (datalen - len(data[:datalen]))
self._write_chunk(chunk) self._write_chunk(chunk)
data = data[datalen:] data = data[datalen:]
@ -185,8 +191,9 @@ class TransportV2(Transport):
def _read(self): def _read(self):
chunk = self._read_chunk() chunk = self._read_chunk()
(session_id, msg_type, datalen, data) = self.parse_first(chunk) (session_id, msg_type, datalen, data) = self.parse_first(chunk)
payloadlen = datalen + 4 # For the checksum
while len(data) < datalen: while len(data) < payloadlen:
chunk = self._read_chunk() chunk = self._read_chunk()
(next_session_id, next_data) = self.parse_next(chunk) (next_session_id, next_data) = self.parse_next(chunk)
@ -195,25 +202,30 @@ class TransportV2(Transport):
data.extend(next_data) data.extend(next_data)
# Strip padding zeros data = data[:payloadlen] # Strip padding zeros
data = data[:datalen] footer = data[-4:]
data = data[:-4]
# csum = struct.unpack('>L', footer)
# assert csum == binascii.crc32(data), "Checksum mismatch"
return (session_id, msg_type, data) return (session_id, msg_type, data)
def parse_first(self, chunk): def parse_first(self, chunk):
if chunk[:2] != b"?!": if chunk[0:1] != b"!":
raise Exception("Unexpected magic characters") raise Exception("Unexpected magic character")
try: try:
headerlen = struct.calcsize(">LLL") headerlen = struct.calcsize(">LLL")
(session_id, msg_type, datalen) = struct.unpack(">LLL", bytes(chunk[2:2 + headerlen])) (session_id, msg_type, datalen) = struct.unpack(">LLL", bytes(chunk[1:1 + headerlen]))
except: except:
raise Exception("Cannot parse header") raise Exception("Cannot parse header")
data = chunk[2 + headerlen:] data = chunk[1 + headerlen:]
return (session_id, msg_type, datalen, data) return (session_id, msg_type, datalen, data)
def parse_next(self, chunk): def parse_next(self, chunk):
if chunk[0:1] != b"?": if chunk[0:1] != b"#":
raise Exception("Unexpected magic characters") raise Exception("Unexpected magic characters")
try: try: