You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.1 KiB
76 lines
2.1 KiB
![]()
7 years ago
|
#!/usr/bin/env python3
|
||
|
import binascii
|
||
|
import struct
|
||
|
import click
|
||
|
import pyblake2
|
||
|
import Pyro4
|
||
|
import serpent
|
||
![]()
6 years ago
|
from trezorlib import cosi
|
||
![]()
7 years ago
|
|
||
|
PORT = 5001
|
||
![]()
6 years ago
|
indexmap = {"bootloader": 0, "vendorheader": 1, "firmware": 2}
|
||
![]()
7 years ago
|
|
||
|
|
||
|
def header_digest(index, filename):
|
||
![]()
6 years ago
|
data = open(filename, "rb").read()
|
||
![]()
7 years ago
|
z = bytes(65 * [0x00])
|
||
![]()
6 years ago
|
if index == "bootloader":
|
||
![]()
7 years ago
|
header = data[:0x03BF] + z
|
||
![]()
6 years ago
|
elif index == "vendorheader":
|
||
![]()
7 years ago
|
header = data[:-65] + z
|
||
![]()
6 years ago
|
elif index == "firmware":
|
||
|
vhdrlen = struct.unpack("<I", data[4:8])[0]
|
||
|
header = data[vhdrlen : vhdrlen + 0x03BF] + z
|
||
![]()
7 years ago
|
else:
|
||
|
raise ValueError('Unknown index "%s"' % index)
|
||
|
return pyblake2.blake2s(header).digest()
|
||
|
|
||
|
|
||
|
@click.group()
|
||
|
def cli():
|
||
|
pass
|
||
|
|
||
|
|
||
![]()
6 years ago
|
@cli.command(help="")
|
||
|
@click.argument("index", type=click.Choice(indexmap.keys()))
|
||
|
@click.argument("filename")
|
||
|
@click.argument("participants", nargs=-1)
|
||
![]()
7 years ago
|
def sign(index, filename, participants):
|
||
|
# compute header digest
|
||
|
digest = header_digest(index, filename)
|
||
|
# create participant proxies
|
||
|
if len(participants) < 1:
|
||
![]()
6 years ago
|
raise ValueError("Not enough participants")
|
||
|
print("connecting to %d participants:" % len(participants))
|
||
![]()
7 years ago
|
proxy = []
|
||
|
for p in participants:
|
||
![]()
6 years ago
|
uri = "PYRO:keyctl@%s:%d" % (p, PORT)
|
||
![]()
7 years ago
|
proxy.append(Pyro4.Proxy(uri))
|
||
|
# collect commits
|
||
|
pks, Rs = [], []
|
||
![]()
6 years ago
|
for i, p in enumerate(proxy):
|
||
![]()
7 years ago
|
pk, R = p.get_commit(index, digest)
|
||
|
pk, R = serpent.tobytes(pk), serpent.tobytes(R)
|
||
|
pks.append(pk)
|
||
|
Rs.append(R)
|
||
![]()
6 years ago
|
print("collected commit #%d from %s" % (i, p._pyroUri.host))
|
||
![]()
7 years ago
|
# compute global commit
|
||
![]()
6 years ago
|
global_pk = cosi.combine_keys(pks)
|
||
|
global_R = cosi.combine_keys(Rs)
|
||
![]()
7 years ago
|
# collect signatures
|
||
|
sigs = []
|
||
![]()
6 years ago
|
for i, p in enumerate(proxy):
|
||
![]()
7 years ago
|
sig = p.get_signature(index, digest, global_R, global_pk)
|
||
|
sig = serpent.tobytes(sig)
|
||
|
sigs.append(sig)
|
||
![]()
6 years ago
|
print("collected signature #%d from %s" % (i, p._pyroUri.host))
|
||
![]()
7 years ago
|
# compute global signature
|
||
![]()
6 years ago
|
sig = cosi.combine_sig(global_R, sigs)
|
||
|
cosi.verify(sig, digest, global_pk)
|
||
![]()
6 years ago
|
print("global signature:")
|
||
![]()
7 years ago
|
print(binascii.hexlify(sig).decode())
|
||
|
|
||
|
|
||
![]()
6 years ago
|
if __name__ == "__main__":
|
||
![]()
7 years ago
|
cli()
|