1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-26 17:38:39 +00:00

protocol: 2/3 compat fixes

This commit is contained in:
Jan Pochyla 2017-09-04 11:44:33 +02:00
parent 3d3c2a29d0
commit 051f8e961b
2 changed files with 9 additions and 8 deletions

View File

@ -67,7 +67,7 @@ class ProtocolV1(object):
raise Exception('Unexpected magic characters') raise Exception('Unexpected magic characters')
try: try:
headerlen = struct.calcsize('>HL') headerlen = struct.calcsize('>HL')
(msg_type, datalen) = struct.unpack('>HL', bytes(chunk[3:3 + headerlen])) (msg_type, datalen) = struct.unpack('>HL', chunk[3:3 + headerlen])
except: except:
raise Exception('Cannot parse header') raise Exception('Cannot parse header')

View File

@ -31,7 +31,7 @@ class ProtocolV2(object):
def session_begin(self, transport): def session_begin(self, transport):
chunk = struct.pack('>B', 0x03) chunk = struct.pack('>B', 0x03)
chunk = chunk.ljust(REPLEN, bytes([0x00])) chunk = chunk.ljust(REPLEN, b'\x00')
transport.write_chunk(chunk) transport.write_chunk(chunk)
resp = transport.read_chunk() resp = transport.read_chunk()
self.session = self.parse_session_open(resp) self.session = self.parse_session_open(resp)
@ -40,10 +40,11 @@ class ProtocolV2(object):
if not self.session: if not self.session:
return return
chunk = struct.pack('>BL', 0x04, self.session) chunk = struct.pack('>BL', 0x04, self.session)
chunk = chunk.ljust(REPLEN, bytes([0x00])) chunk = chunk.ljust(REPLEN, b'\x00')
transport.write_chunk(chunk) transport.write_chunk(chunk)
resp = transport.read_chunk() resp = transport.read_chunk()
if resp[0] != 0x04: (magic, ) = struct.unpack('>B', resp[:1])
if magic != 0x04:
raise Exception('Expected session close') raise Exception('Expected session close')
self.session = None self.session = None
@ -65,7 +66,7 @@ class ProtocolV2(object):
repheader = struct.pack('>BLL', 0x02, self.session, seq) repheader = struct.pack('>BLL', 0x02, self.session, seq)
datalen = REPLEN - len(repheader) datalen = REPLEN - len(repheader)
chunk = repheader + data[:datalen] chunk = repheader + data[:datalen]
chunk = chunk.ljust(REPLEN, bytes([0x00])) chunk = chunk.ljust(REPLEN, b'\x00')
transport.write_chunk(chunk) transport.write_chunk(chunk)
data = data[datalen:] data = data[datalen:]
seq += 1 seq += 1
@ -95,7 +96,7 @@ class ProtocolV2(object):
def parse_first(self, chunk): def parse_first(self, chunk):
try: try:
headerlen = struct.calcsize('>BLLL') headerlen = struct.calcsize('>BLLL')
(magic, session, msg_type, datalen) = struct.unpack('>BLLL', bytes(chunk[:headerlen])) (magic, session, msg_type, datalen) = struct.unpack('>BLLL', chunk[:headerlen])
except: except:
raise Exception('Cannot parse header') raise Exception('Cannot parse header')
if magic != 0x01: if magic != 0x01:
@ -107,7 +108,7 @@ class ProtocolV2(object):
def parse_next(self, chunk): def parse_next(self, chunk):
try: try:
headerlen = struct.calcsize('>BLL') headerlen = struct.calcsize('>BLL')
(magic, session, sequence) = struct.unpack('>BLL', bytes(chunk[:headerlen])) (magic, session, sequence) = struct.unpack('>BLL', chunk[:headerlen])
except: except:
raise Exception('Cannot parse header') raise Exception('Cannot parse header')
if magic != 0x02: if magic != 0x02:
@ -119,7 +120,7 @@ class ProtocolV2(object):
def parse_session_open(self, chunk): def parse_session_open(self, chunk):
try: try:
headerlen = struct.calcsize('>BL') headerlen = struct.calcsize('>BL')
(magic, session) = struct.unpack('>BL', bytes(chunk[:headerlen])) (magic, session) = struct.unpack('>BL', chunk[:headerlen])
except: except:
raise Exception('Cannot parse header') raise Exception('Cannot parse header')
if magic != 0x03: if magic != 0x03: