1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-13 19:18:56 +00:00

tools: don't convert sigidx back and forth in firmwarectl

This commit is contained in:
Pavol Rusnak 2017-03-30 15:11:12 +02:00
parent 42b18f55ec
commit 7da63a36e2
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

View File

@ -9,26 +9,12 @@ import pyblake2
# loader/firmware headers specification: https://github.com/trezor/trezor-core/blob/master/docs/bootloader.md
# converts 8-bit bitmap to tuple of values
def bitmap_to_tuple(b):
return tuple([ i + 1 for i in range(8) if b & (1 << i) ])
# converts tuple of values to 8-bit bitmap
def tuple_to_bitmap(t):
b = 0
for i in t:
if i >= 1 and i <= 8:
b |= (1 << (i - 1))
return b
def get_sig(data):
print('Enter privkey: ', end='')
seckey = binascii.unhexlify(input())
signkey = ed25519.SigningKey(seckey)
digest = pyblake2.blake2s(data).digest()
sigidx = (1, )
sigidx = 0x01 # (1 _ _ _ _)
sig = signkey.sign(digest)
return sigidx, sig
@ -46,7 +32,6 @@ class LoaderImage:
assert self.codelen + self.hdrlen <= 64 * 1024 + 7 * 128 * 1024
assert (self.codelen + self.hdrlen) % 512 == 0
assert self.reserved == 171 * b'\x00'
self.sigidx = bitmap_to_tuple(self.sigidx)
self.code = data[self.hdrlen:]
assert len(self.code) == self.codelen
@ -66,8 +51,7 @@ class LoaderImage:
self.vmajor, self.vminor, self.vpatch, self.vbuild, \
self.reserved)
if sig:
sigidx = tuple_to_bitmap(self.sigidx)
header += struct.pack('<B64s', sigidx, self.sig)
header += struct.pack('<B64s', self.sigidx, self.sig)
else:
header += 65 * b'\x00'
assert len(header) == self.hdrlen
@ -108,7 +92,7 @@ class VendorHeader:
p += 2
self.vimg = data[p:p + self.vimg_len]
p += self.vimg_len
self.sigidx = bitmap_to_tuple(data[p])
self.sigidx = data[p]
p += 1
self.sig = data[p:p+64]
assert len(data) == 4 + 4 + 4 + 1 + 1 + 1 + 1 + \
@ -141,8 +125,7 @@ class VendorHeader:
header += struct.pack('<B', self.vstr_len) + self.vstr
header += struct.pack('<H', self.vimg_len) + self.vimg
if sig:
sigidx = tuple_to_bitmap(self.sigidx)
header += struct.pack('<B64s', sigidx, self.sig)
header += struct.pack('<B64s', self.sigidx, self.sig)
else:
header += 65 * b'\x00'
assert len(header) == self.hdrlen
@ -168,7 +151,6 @@ class FirmwareImage:
assert self.hdrlen == 256
assert self.codelen % 4 == 0
assert self.reserved == 171 * b'\x00'
self.sigidx = bitmap_to_tuple(self.sigidx)
self.code = data[self.hdrlen:]
assert len(self.code) == self.codelen
@ -188,8 +170,7 @@ class FirmwareImage:
self.vmajor, self.vminor, self.vpatch, self.vbuild, \
self.reserved)
if sig:
sigidx = tuple_to_bitmap(self.sigidx)
header += struct.pack('<B64s', sigidx, self.sig)
header += struct.pack('<B64s', self.sigidx, self.sig)
else:
header += 65 * b'\x00'
assert len(header) == self.hdrlen