trezor-agent/trezor_agent/util.py

95 lines
2.1 KiB
Python
Raw Normal View History

2016-02-19 09:34:20 +00:00
"""Various I/O and serialization utilities."""
2015-06-15 15:13:10 +00:00
import io
2016-01-09 14:06:47 +00:00
import struct
2015-06-15 15:13:10 +00:00
2015-06-16 07:03:48 +00:00
2016-02-19 09:34:20 +00:00
def send(conn, data):
"""Send data blob to connection socket."""
2015-06-15 15:13:10 +00:00
conn.sendall(data)
2015-06-16 07:03:48 +00:00
2015-06-15 15:13:10 +00:00
def recv(conn, size):
2016-02-19 09:34:20 +00:00
"""
Receive bytes from connection socket or stream.
If size is struct.calcsize()-compatible format, use it to unpack the data.
Otherwise, return the plain blob as bytes.
"""
2015-06-15 15:13:10 +00:00
try:
fmt = size
size = struct.calcsize(fmt)
except TypeError:
fmt = None
try:
_read = conn.recv
except AttributeError:
_read = conn.read
res = io.BytesIO()
while size > 0:
buf = _read(size)
if not buf:
raise EOFError
size = size - len(buf)
res.write(buf)
res = res.getvalue()
if fmt:
return struct.unpack(fmt, res)
else:
return res
def read_frame(conn):
2016-02-19 09:34:20 +00:00
"""Read size-prefixed frame from connection."""
2015-06-15 15:13:10 +00:00
size, = recv(conn, '>L')
return recv(conn, size)
2015-06-16 07:03:48 +00:00
2015-06-15 15:13:10 +00:00
def bytes2num(s):
2016-02-19 09:34:20 +00:00
"""Convert MSB-first bytes to an unsigned integer."""
2015-06-15 15:13:10 +00:00
res = 0
for i, c in enumerate(reversed(bytearray(s))):
res += c << (i * 8)
return res
2015-06-16 07:03:48 +00:00
2015-06-15 15:13:10 +00:00
def num2bytes(value, size):
2016-02-19 09:34:20 +00:00
"""Convert an unsigned integer to MSB-first bytes with specified size."""
2015-06-15 15:13:10 +00:00
res = []
2015-06-16 07:33:48 +00:00
for _ in range(size):
2015-06-15 15:13:10 +00:00
res.append(value & 0xFF)
value = value >> 8
assert value == 0
2016-04-22 18:43:54 +00:00
return bytes(bytearray(list(reversed(res))))
2015-06-15 15:13:10 +00:00
2015-06-16 07:03:48 +00:00
2015-06-15 15:13:10 +00:00
def pack(fmt, *args):
2016-02-19 09:34:20 +00:00
"""Serialize MSB-first message."""
2015-06-15 15:13:10 +00:00
return struct.pack('>' + fmt, *args)
2015-06-16 07:03:48 +00:00
2015-06-15 15:13:10 +00:00
def frame(*msgs):
2016-02-19 09:34:20 +00:00
"""Serialize MSB-first length-prefixed frame."""
2015-06-15 15:13:10 +00:00
res = io.BytesIO()
for msg in msgs:
res.write(msg)
msg = res.getvalue()
return pack('L', len(msg)) + msg
2016-04-23 19:08:18 +00:00
def crc24(blob):
CRC24_INIT = 0xB704CEL
CRC24_POLY = 0x1864CFBL
crc = CRC24_INIT
for octet in bytearray(blob):
crc ^= (octet << 16)
for _ in range(8):
crc <<= 1
if crc & 0x1000000:
crc ^= CRC24_POLY
assert 0 <= crc < 0x1000000
crc_bytes = struct.pack('>L', crc)
assert crc_bytes[0] == b'\x00'
return crc_bytes[1:]