2017-02-09 15:26:15 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
import struct
|
|
|
|
import binascii
|
|
|
|
import ed25519
|
2017-03-22 00:53:25 +00:00
|
|
|
import pyblake2
|
2017-02-09 15:26:15 +00:00
|
|
|
|
2017-02-09 17:14:10 +00:00
|
|
|
|
2017-02-09 15:26:15 +00:00
|
|
|
def get_sig(data):
|
|
|
|
print('Enter privkey: ', end='')
|
|
|
|
seckey = binascii.unhexlify(input())
|
|
|
|
signkey = ed25519.SigningKey(seckey)
|
2017-03-22 00:53:25 +00:00
|
|
|
digest = pyblake2.blake2s(data).digest()
|
2017-04-01 00:32:05 +00:00
|
|
|
sigmask = 0x01 # (1 _ _ _ _)
|
2017-02-09 15:26:15 +00:00
|
|
|
sig = signkey.sign(digest)
|
2017-04-01 00:32:05 +00:00
|
|
|
return sigmask, sig
|
2017-02-09 15:26:15 +00:00
|
|
|
|
|
|
|
|
2017-04-01 00:32:05 +00:00
|
|
|
# loader/firmware headers specification: https://github.com/trezor/trezor-core/blob/master/docs/bootloader.md
|
2017-02-09 15:26:15 +00:00
|
|
|
|
2017-04-01 00:32:05 +00:00
|
|
|
|
|
|
|
class BinImage:
|
|
|
|
|
|
|
|
def __init__(self, data, magic, max_size):
|
2017-03-31 21:54:59 +00:00
|
|
|
header = struct.unpack('<4sIIIBBBB427sB64s', data[:512])
|
2017-03-30 20:58:00 +00:00
|
|
|
self.magic, \
|
|
|
|
self.hdrlen, \
|
|
|
|
self.expiry, \
|
|
|
|
self.codelen, \
|
|
|
|
self.vmajor, \
|
|
|
|
self.vminor, \
|
|
|
|
self.vpatch, \
|
|
|
|
self.vbuild, \
|
|
|
|
self.reserved, \
|
2017-04-01 00:32:05 +00:00
|
|
|
self.sigmask, \
|
2017-03-30 20:58:00 +00:00
|
|
|
self.sig = header
|
2017-04-01 00:32:05 +00:00
|
|
|
assert self.magic == magic
|
2017-03-31 21:54:59 +00:00
|
|
|
assert self.hdrlen == 512
|
2017-04-01 00:32:05 +00:00
|
|
|
total_len = self.hdrlen + self.codelen
|
|
|
|
assert total_len % 512 == 0
|
|
|
|
assert total_len >= 4 * 1024
|
|
|
|
assert total_len <= max_size
|
2017-03-31 21:54:59 +00:00
|
|
|
assert self.reserved == 427 * b'\x00'
|
2017-02-17 16:11:34 +00:00
|
|
|
self.code = data[self.hdrlen:]
|
2017-02-09 15:26:15 +00:00
|
|
|
assert len(self.code) == self.codelen
|
|
|
|
|
|
|
|
def print(self):
|
2017-04-01 00:32:05 +00:00
|
|
|
if self.magic == b'TRZF':
|
|
|
|
print('TREZOR Firmware Image')
|
|
|
|
elif self.magic == b'TRZL':
|
|
|
|
print('TREZOR Loader Image')
|
|
|
|
else:
|
|
|
|
print('TREZOR Unknown Image')
|
2017-02-09 15:26:15 +00:00
|
|
|
print(' * magic :', self.magic.decode('ascii'))
|
|
|
|
print(' * hdrlen :', self.hdrlen)
|
|
|
|
print(' * expiry :', self.expiry)
|
|
|
|
print(' * codelen :', self.codelen)
|
|
|
|
print(' * version : %d.%d.%d.%d' % (self.vmajor, self.vminor, self.vpatch, self.vbuild))
|
2017-04-01 00:32:05 +00:00
|
|
|
print(' * sigmask :', self.sigmask)
|
2017-02-09 15:26:15 +00:00
|
|
|
print(' * sig :', binascii.hexlify(self.sig).decode('ascii'))
|
|
|
|
|
2017-03-30 20:58:00 +00:00
|
|
|
def serialize_header(self, sig=True):
|
2017-03-31 21:54:59 +00:00
|
|
|
header = struct.pack('<4sIIIBBBB427s', \
|
2017-02-09 15:26:15 +00:00
|
|
|
self.magic, self.hdrlen, self.expiry, self.codelen, \
|
|
|
|
self.vmajor, self.vminor, self.vpatch, self.vbuild, \
|
|
|
|
self.reserved)
|
|
|
|
if sig:
|
2017-04-01 00:32:05 +00:00
|
|
|
header += struct.pack('<B64s', self.sigmask, self.sig)
|
2017-02-09 15:26:15 +00:00
|
|
|
else:
|
|
|
|
header += 65 * b'\x00'
|
2017-02-17 16:11:34 +00:00
|
|
|
assert len(header) == self.hdrlen
|
2017-02-09 15:26:15 +00:00
|
|
|
return header
|
|
|
|
|
|
|
|
def sign(self):
|
2017-03-30 20:58:00 +00:00
|
|
|
header = self.serialize_header(sig=False)
|
2017-02-09 15:26:15 +00:00
|
|
|
data = header + self.code
|
2017-02-17 16:11:34 +00:00
|
|
|
assert len(data) == self.hdrlen + self.codelen
|
2017-04-01 00:32:05 +00:00
|
|
|
self.sigmask, self.sig = get_sig(data)
|
2017-02-09 15:26:15 +00:00
|
|
|
|
|
|
|
def write(self, filename):
|
|
|
|
with open(filename, 'wb') as f:
|
2017-03-30 20:58:00 +00:00
|
|
|
f.write(self.serialize_header())
|
2017-02-09 15:26:15 +00:00
|
|
|
f.write(self.code)
|
|
|
|
|
|
|
|
|
2017-04-01 00:32:05 +00:00
|
|
|
class FirmwareImage(BinImage):
|
|
|
|
|
|
|
|
def __init__(self, data):
|
|
|
|
super().__init__(data, magic=b'TRZF', max_size=7*128*1024)
|
|
|
|
|
|
|
|
|
|
|
|
class LoaderImage(BinImage):
|
|
|
|
|
|
|
|
def __init__(self, data):
|
|
|
|
super().__init__(data, magic=b'TRZL', max_size=64*1024 + 7*128*1024)
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
2017-02-09 15:26:15 +00:00
|
|
|
class VendorHeader:
|
|
|
|
|
|
|
|
def __init__(self, data):
|
2017-02-09 17:14:10 +00:00
|
|
|
header = struct.unpack('<4sIIBBBB', data[:16])
|
2017-03-30 20:58:00 +00:00
|
|
|
self.magic, \
|
|
|
|
self.hdrlen, \
|
|
|
|
self.expiry, \
|
|
|
|
self.vmajor, \
|
|
|
|
self.vminor, \
|
|
|
|
self.vsig_m, \
|
|
|
|
self.vsig_n = header
|
2017-02-09 17:14:10 +00:00
|
|
|
assert self.magic == b'TRZF'
|
|
|
|
assert self.vsig_m > 0 and self.vsig_m <= self.vsig_n
|
|
|
|
assert self.vsig_n > 0 and self.vsig_n <= 8
|
|
|
|
p = 16
|
|
|
|
self.vpub = []
|
|
|
|
for _ in range(self.vsig_n):
|
|
|
|
self.vpub.append(data[p:p + 32])
|
|
|
|
p += 32
|
|
|
|
self.vstr_len = data[p]
|
|
|
|
p += 1
|
|
|
|
self.vstr = data[p:p + self.vstr_len]
|
|
|
|
p += self.vstr_len
|
|
|
|
self.vimg_len, _ = struct.unpack('<H', data[p: p + 2])
|
|
|
|
p += 2
|
|
|
|
self.vimg = data[p:p + self.vimg_len]
|
|
|
|
p += self.vimg_len
|
2017-04-01 00:32:05 +00:00
|
|
|
self.sigmask = data[p]
|
2017-02-09 17:14:10 +00:00
|
|
|
p += 1
|
|
|
|
self.sig = data[p:p+64]
|
|
|
|
assert len(data) == 4 + 4 + 4 + 1 + 1 + 1 + 1 + \
|
|
|
|
32 * len(self.vpub) + \
|
|
|
|
1 + self.vstr_len + \
|
|
|
|
2 + self.vimg_len + \
|
|
|
|
1 + 64
|
2017-02-09 15:26:15 +00:00
|
|
|
|
|
|
|
def print(self):
|
2017-02-09 17:14:10 +00:00
|
|
|
print('TREZOR Vendor Header')
|
|
|
|
print(' * magic :', self.magic.decode('ascii'))
|
|
|
|
print(' * hdrlen :', self.hdrlen)
|
|
|
|
print(' * expiry :', self.expiry)
|
|
|
|
print(' * version : %d.%d' % (self.vmajor, self.vminor))
|
|
|
|
print(' * scheme : %d out of %d' % (self.vsig_m, self.vsig_n))
|
|
|
|
for i in range(self.vsig_n):
|
|
|
|
print(' * vpub #%d :' % (i + 1), binascii.hexlify(self.vpub[i]).decode('ascii'))
|
|
|
|
print(' * vstr :', self.vstr.decode('ascii'))
|
|
|
|
print(' * vimg : (%d bytes)', len(self.vimg))
|
2017-04-01 00:32:05 +00:00
|
|
|
print(' * sigmask :', self.sigmask)
|
|
|
|
print(' * sig :', binascii.hexlify(self.sig).decode('ascii'))
|
2017-02-09 17:14:10 +00:00
|
|
|
|
2017-03-30 20:58:00 +00:00
|
|
|
def serialize_header(self, sig=True):
|
2017-02-09 17:14:10 +00:00
|
|
|
header = struct.pack('<4sIIBBBB', \
|
|
|
|
self.magic, self.hdrlen, self.expiry, \
|
|
|
|
self.vmajor, self.vminor, \
|
|
|
|
self.vsig_m, self.vsig_n)
|
|
|
|
for i in range(self.vsig_n):
|
|
|
|
header += self.vpub[i]
|
|
|
|
header += struct.pack('<B', self.vstr_len) + self.vstr
|
|
|
|
header += struct.pack('<H', self.vimg_len) + self.vimg
|
|
|
|
if sig:
|
2017-04-01 00:32:05 +00:00
|
|
|
header += struct.pack('<B64s', self.sigmask, self.sig)
|
2017-02-09 17:14:10 +00:00
|
|
|
else:
|
|
|
|
header += 65 * b'\x00'
|
2017-02-17 16:11:34 +00:00
|
|
|
assert len(header) == self.hdrlen
|
2017-02-09 17:14:10 +00:00
|
|
|
return header
|
2017-02-09 15:26:15 +00:00
|
|
|
|
|
|
|
def sign(self):
|
2017-03-30 20:58:00 +00:00
|
|
|
header = self.serialize_header(sig=False)
|
2017-04-01 00:32:05 +00:00
|
|
|
self.sigmask, self.sig = get_sig(header)
|
2017-02-09 17:14:10 +00:00
|
|
|
|
|
|
|
def write(self, filename):
|
|
|
|
with open(filename, 'wb') as f:
|
2017-03-30 20:58:00 +00:00
|
|
|
f.write(self.serialize_header())
|
2017-04-01 00:32:05 +00:00
|
|
|
"""
|
2017-02-09 15:26:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def binopen(filename):
|
|
|
|
print()
|
|
|
|
print('Opening file', filename)
|
|
|
|
print()
|
|
|
|
data = open(filename, 'rb').read()
|
|
|
|
magic = data[:4]
|
2017-03-20 15:03:02 +00:00
|
|
|
if magic == b'TRZL':
|
|
|
|
return LoaderImage(data)
|
2017-02-09 15:26:15 +00:00
|
|
|
if magic == b'TRZF':
|
|
|
|
return FirmwareImage(data)
|
2017-04-01 00:32:05 +00:00
|
|
|
# if magic == b'TRZV':
|
|
|
|
# return VendorHeader(data)
|
2017-02-09 15:26:15 +00:00
|
|
|
raise Exception('Unknown file format')
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
if len(sys.argv) < 2:
|
2017-03-31 21:54:59 +00:00
|
|
|
print('Usage: binctl file.bin [-s]')
|
2017-02-09 15:26:15 +00:00
|
|
|
return 1
|
|
|
|
fn = sys.argv[1]
|
|
|
|
sign = len(sys.argv) > 2 and sys.argv[2] == '-s'
|
|
|
|
b = binopen(fn)
|
|
|
|
b.print()
|
|
|
|
if sign:
|
|
|
|
print()
|
|
|
|
b.sign()
|
|
|
|
print()
|
|
|
|
b.print()
|
|
|
|
b.write(fn)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|