mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-11 16:00:57 +00:00
tools: fix flake8 warnings
This commit is contained in:
parent
d7a563a03d
commit
5b84db0f7f
29
tools/binctl
29
tools/binctl
@ -14,8 +14,9 @@ def sign_data(seckey, data):
|
||||
digest = pyblake2.blake2s(data).digest()
|
||||
return signkey.sign(digest)
|
||||
|
||||
|
||||
def format_sigmask(sigmask):
|
||||
bits = [ str(b + 1) if sigmask & (1<<b) else '.' for b in range(8) ]
|
||||
bits = [str(b + 1) if sigmask & (1 << b) else '.' for b in range(8)]
|
||||
return '0x%02x = [%s]' % (sigmask, ' '.join(bits))
|
||||
|
||||
|
||||
@ -67,10 +68,10 @@ class BinImage(object):
|
||||
print()
|
||||
|
||||
def serialize_header(self, sig=True):
|
||||
header = struct.pack('<4sIIIBBBB427s', \
|
||||
self.magic, self.hdrlen, self.expiry, self.codelen, \
|
||||
self.vmajor, self.vminor, self.vpatch, self.vbuild, \
|
||||
self.reserved)
|
||||
header = struct.pack('<4sIIIBBBB427s',
|
||||
self.magic, self.hdrlen, self.expiry, self.codelen,
|
||||
self.vmajor, self.vminor, self.vpatch, self.vbuild,
|
||||
self.reserved)
|
||||
if sig:
|
||||
header += struct.pack('<B64s', self.sigmask, self.sig)
|
||||
else:
|
||||
@ -94,7 +95,7 @@ class BinImage(object):
|
||||
class FirmwareImage(BinImage):
|
||||
|
||||
def __init__(self, data, vhdrlen):
|
||||
super(FirmwareImage, self).__init__(data[vhdrlen:], magic=b'TRZF', max_size=7*128*1024)
|
||||
super(FirmwareImage, self).__init__(data[vhdrlen:], magic=b'TRZF', max_size=7 * 128 * 1024)
|
||||
self.vhdrlen = vhdrlen
|
||||
self.vheader = data[:vhdrlen]
|
||||
|
||||
@ -104,10 +105,11 @@ class FirmwareImage(BinImage):
|
||||
f.write(self.serialize_header())
|
||||
f.write(self.code)
|
||||
|
||||
|
||||
class BootloaderImage(BinImage):
|
||||
|
||||
def __init__(self, data):
|
||||
super(BootloaderImage, self).__init__(data, magic=b'TRZB', max_size=64*1024 + 7*128*1024)
|
||||
super(BootloaderImage, self).__init__(data, magic=b'TRZB', max_size=64 * 1024 + 7 * 128 * 1024)
|
||||
|
||||
|
||||
class VendorHeader(object):
|
||||
@ -140,7 +142,7 @@ class VendorHeader(object):
|
||||
p += self.vimg_len
|
||||
self.sigmask = data[p]
|
||||
p += 1
|
||||
self.sig = data[p:p+64]
|
||||
self.sig = data[p:p + 64]
|
||||
assert len(data) == 4 + 4 + 4 + 1 + 1 + 1 + 1 + \
|
||||
32 * len(self.vpub) + \
|
||||
1 + self.vstr_len + vstr_pad + \
|
||||
@ -162,14 +164,14 @@ class VendorHeader(object):
|
||||
print(' * sig :', binascii.hexlify(self.sig).decode('ascii'))
|
||||
|
||||
def serialize_header(self, sig=True):
|
||||
header = struct.pack('<4sIIBBBB', \
|
||||
self.magic, self.hdrlen, self.expiry, \
|
||||
self.vmajor, self.vminor, \
|
||||
self.vsig_m, self.vsig_n)
|
||||
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 += (-len(header) & 3) * b'\x00' # vstr_pad
|
||||
header += (-len(header) & 3) * b'\x00' # vstr_pad
|
||||
header += self.vimg
|
||||
if sig:
|
||||
header += struct.pack('<B64s', self.sigmask, self.sig)
|
||||
@ -205,6 +207,7 @@ def binopen(filename):
|
||||
return FirmwareImage(data, 0)
|
||||
raise Exception('Unknown file format')
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print('Usage: binctl file.bin [-s index seckey]')
|
||||
|
@ -107,6 +107,7 @@ def clear_directory(top_dir):
|
||||
|
||||
os.rmdir(root)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
clear_directory('../mocks/generated')
|
||||
build_directory('../embed/extmod', '../mocks/generated')
|
||||
|
@ -49,4 +49,5 @@ def main():
|
||||
with open(ofn, 'wb') as f:
|
||||
f.write(vheader)
|
||||
|
||||
|
||||
main()
|
||||
|
@ -6,6 +6,7 @@ def pairwise(iterable):
|
||||
a = iter(iterable)
|
||||
return zip(a, a)
|
||||
|
||||
|
||||
files = sys.argv[1:]
|
||||
files = list(pairwise(files))
|
||||
|
||||
|
@ -26,6 +26,7 @@ def expmod(b, e, m):
|
||||
def inv(x):
|
||||
return expmod(x, q - 2, q)
|
||||
|
||||
|
||||
d = -121665 * inv(121666)
|
||||
I = expmod(2, (q - 1) >> 2, q)
|
||||
|
||||
@ -39,6 +40,7 @@ def xrecover(y):
|
||||
x = q - x
|
||||
return x
|
||||
|
||||
|
||||
By = 4 * inv(5)
|
||||
Bx = xrecover(By)
|
||||
B = [Bx % q, By % q]
|
||||
|
@ -77,7 +77,7 @@ def combinePubkeys(m, n):
|
||||
sum = None
|
||||
for j in range(0, n):
|
||||
if mask & (1 << j) != 0:
|
||||
if sum == None:
|
||||
if sum is None:
|
||||
sum = pks[j]
|
||||
else:
|
||||
sum = ed25519raw.edwards(sum, pks[j])
|
||||
@ -88,9 +88,8 @@ def combinePubkeys(m, n):
|
||||
def get_nonce(sk, data, ctr):
|
||||
h = ed25519raw.H(sk)
|
||||
b = ed25519raw.b
|
||||
a = 2**(b - 2) + sum(2**i * ed25519raw.bit(h, i) for i in range(3, b - 2))
|
||||
r = ed25519raw.Hint(bytes([h[i] for i in range(b >> 3, b >> 2)]) + data +
|
||||
binascii.unhexlify("%08x" % ctr))
|
||||
a = 2 ** (b - 2) + sum(2 ** i * ed25519raw.bit(h, i) for i in range(3, b - 2))
|
||||
r = ed25519raw.Hint(bytes([h[i] for i in range(b >> 3, b >> 2)]) + data + binascii.unhexlify('%08x' % ctr))
|
||||
R = ed25519raw.scalarmult(ed25519raw.B, r)
|
||||
return (r, ed25519raw.encodepoint(R))
|
||||
|
||||
@ -256,5 +255,6 @@ def test():
|
||||
ed25519raw.checkvalid(sig, digest, globalPk)
|
||||
print('Valid Signature!')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test()
|
||||
|
@ -165,6 +165,7 @@ def write_to_file(genpath, t, out):
|
||||
f.write(data)
|
||||
f.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('modulename', type=str, help="Name of module to generate")
|
||||
|
@ -56,7 +56,7 @@ def process_image(ifn, savefiles=False):
|
||||
pixeldata = process_grayscale(w, h, pix)
|
||||
z = zlib.compressobj(level=9, wbits=10)
|
||||
zdata = z.compress(pixeldata) + z.flush()
|
||||
zdata = zdata[2:-4] # strip header and checksum
|
||||
zdata = zdata[2:-4] # strip header and checksum
|
||||
|
||||
data = b''
|
||||
if im.mode == 'RGB':
|
||||
|
@ -46,6 +46,7 @@ def process_face(name, style, size):
|
||||
f.write(' Font_%s_%s_%d_glyph_%d,\n' % (name, style, size, i))
|
||||
f.write('};\n')
|
||||
|
||||
|
||||
process_face('Roboto', 'Regular', 20)
|
||||
process_face('Roboto', 'Bold', 20)
|
||||
process_face('RobotoMono', 'Regular', 20)
|
||||
|
Loading…
Reference in New Issue
Block a user