From 81ff60c3e1a2cd512bdc5956c944692a26933331 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Mon, 29 Jan 2018 15:02:32 +0100 Subject: [PATCH] tools: drop combine_sign; rework keyctl into 3 tools --- SConscript.bootloader | 4 +- SConscript.firmware | 4 +- SConscript.prodtest | 4 +- SConscript.reflash | 4 +- tools/combine_sign | 27 --------- tools/keyctl | 121 ++++++++------------------------------- tools/keyctl-coordinator | 76 ++++++++++++++++++++++++ tools/keyctl-proxy | 67 ++++++++++++++++++++++ 8 files changed, 176 insertions(+), 131 deletions(-) delete mode 100755 tools/combine_sign create mode 100755 tools/keyctl-coordinator create mode 100755 tools/keyctl-proxy diff --git a/SConscript.bootloader b/SConscript.bootloader index a84f9dee6..bae9718e3 100644 --- a/SConscript.bootloader +++ b/SConscript.bootloader @@ -156,7 +156,7 @@ env.Replace( env.Replace( BINCTL='tools/binctl', - COMBINE_SIGN='tools/combine_sign', + KEYCTL='tools/keyctl', ) # @@ -182,5 +182,5 @@ program_bin = env.Command( action=[ '$OBJCOPY -O binary -j .header -j .flash -j .data $SOURCE $TARGET', '$BINCTL $TARGET -h', - '$BINCTL $TARGET -s 1:2 `$COMBINE_SIGN bootloader $TARGET 4141414141414141414141414141414141414141414141414141414141414141 4242424242424242424242424242424242424242424242424242424242424242`', + '$BINCTL $TARGET -s 1:2 `$KEYCTL sign bootloader $TARGET 4141414141414141414141414141414141414141414141414141414141414141 4242424242424242424242424242424242424242424242424242424242424242`', ], ) diff --git a/SConscript.firmware b/SConscript.firmware index 039bf9860..da1ddb1ef 100644 --- a/SConscript.firmware +++ b/SConscript.firmware @@ -336,7 +336,7 @@ env.Replace( env.Replace( BINCTL='tools/binctl', - COMBINE_SIGN='tools/combine_sign', + KEYCTL='tools/keyctl', PYTHON='python', MAKEQSTRDATA='$PYTHON vendor/micropython/py/makeqstrdata.py', MAKEVERSIONHDR='$PYTHON vendor/micropython/py/makeversionhdr.py', @@ -418,5 +418,5 @@ program_bin = env.Command( action=[ '$OBJCOPY -O binary -j .vendorheader -j .header -j .flash -j .data $SOURCE $TARGET', '$BINCTL $TARGET -h', - '$BINCTL $TARGET -s 1:2 `$COMBINE_SIGN firmware $TARGET 4747474747474747474747474747474747474747474747474747474747474747 4848484848484848484848484848484848484848484848484848484848484848`', + '$BINCTL $TARGET -s 1:2 `$KEYCTL sign firmware $TARGET 4747474747474747474747474747474747474747474747474747474747474747 4848484848484848484848484848484848484848484848484848484848484848`', ], ) diff --git a/SConscript.prodtest b/SConscript.prodtest index eea4b4e91..a5c719e28 100644 --- a/SConscript.prodtest +++ b/SConscript.prodtest @@ -120,7 +120,7 @@ env.Replace( env.Replace( BINCTL='tools/binctl', - COMBINE_SIGN='tools/combine_sign', + KEYCTL='tools/keyctl', ) # @@ -154,5 +154,5 @@ program_bin = env.Command( action=[ '$OBJCOPY -O binary -j .vendorheader -j .header -j .flash -j .data $SOURCE $TARGET', '$BINCTL $TARGET -h', - '$BINCTL $TARGET -s 1:2 `$COMBINE_SIGN firmware $TARGET 4747474747474747474747474747474747474747474747474747474747474747 4848484848484848484848484848484848484848484848484848484848484848`', + '$BINCTL $TARGET -s 1:2 `$KEYCTL sign firmware $TARGET 4747474747474747474747474747474747474747474747474747474747474747 4848484848484848484848484848484848484848484848484848484848484848`', ], ) diff --git a/SConscript.reflash b/SConscript.reflash index baa81d2a3..0a0558c4f 100644 --- a/SConscript.reflash +++ b/SConscript.reflash @@ -120,7 +120,7 @@ env.Replace( env.Replace( BINCTL='tools/binctl', - COMBINE_SIGN='tools/combine_sign', + KEYCTL='tools/keyctl', ) # @@ -154,5 +154,5 @@ program_bin = env.Command( action=[ '$OBJCOPY -O binary -j .vendorheader -j .header -j .flash -j .data $SOURCE $TARGET', '$BINCTL $TARGET -h', - '$BINCTL $TARGET -s 1:2 `$COMBINE_SIGN firmware $TARGET 4747474747474747474747474747474747474747474747474747474747474747 4848484848484848484848484848484848484848484848484848484848484848`', + '$BINCTL $TARGET -s 1:2 `$KEYCTL sign firmware $TARGET 4747474747474747474747474747474747474747474747474747474747474747 4848484848484848484848484848484848484848484848484848484848484848`', ], ) diff --git a/tools/combine_sign b/tools/combine_sign deleted file mode 100755 index e71fc717a..000000000 --- a/tools/combine_sign +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash - -TOOLDIR=$(dirname $0) - -TYPE=$1 -FILE=$2 -shift -shift -SECKEYS=$* - -COMMITS="" - -for seckey in $SECKEYS; do - commit=$( $TOOLDIR/keyctl commit $TYPE $FILE $seckey ) - COMMITS="$COMMITS $commit" -done - -global_commit=$( $TOOLDIR/keyctl global_commit $COMMITS ) - -SIGS="" - -for seckey in $SECKEYS; do - sig=$( $TOOLDIR/keyctl sign $TYPE $FILE $global_commit $seckey ) - SIGS="$SIGS $sig" -done - -$TOOLDIR/keyctl global_sign $TYPE $FILE $global_commit $SIGS diff --git a/tools/keyctl b/tools/keyctl index c9155f096..b2d0130be 100755 --- a/tools/keyctl +++ b/tools/keyctl @@ -1,13 +1,10 @@ #!/usr/bin/env python3 - import binascii +import struct import click import pyblake2 -import struct - from trezorlib import ed25519raw, ed25519cosi - indexmap = { 'bootloader': 0, 'vendorheader': 1, @@ -15,31 +12,19 @@ indexmap = { } -def get_trezor(): - from trezorlib.client import TrezorClient - from trezorlib.transport_hid import HidTransport - devices = HidTransport.enumerate() - if len(devices) > 0: - return TrezorClient(devices[0]) - else: - raise Exception('No TREZOR found') - - -def header_to_sign(index, data): +def header_digest(index, filename): + data = open(filename, 'rb').read() z = bytes(65 * [0x00]) if index == 'bootloader': - return data[:0x03BF] + z + header = data[:0x03BF] + z elif index == 'vendorheader': - return data[:-65] + z + header = data[:-65] + z elif index == 'firmware': vhdrlen = struct.unpack(' 0: + return TrezorClient(devices[0]) + else: + raise Exception('No TREZOR found') + + +def get_path(index): + return "10018'/%d'" % indexmap[index] + + +@Pyro4.expose +class KeyctlProxy(object): + + def __init__(self): + super(KeyctlProxy, self).__init__() + + def get_commit(self, index, digest): + digest = serpent.tobytes(digest) + t = get_trezor() + path = get_path(index) + print('commiting to hash %s with path %s' % (binascii.hexlify(digest).decode(), path)) + commit = t.cosi_commit(t.expand_path(path), digest) + pk = commit.pubkey + R = commit.commitment + return (pk, R) + + def get_signature(self, index, digest, global_R, global_pk): + digest, global_R, global_pk = serpent.tobytes(digest), serpent.tobytes(global_R), serpent.tobytes(global_pk) + t = get_trezor() + path = get_path(index) + print('signing hash %s with path %s' % (binascii.hexlify(digest).decode(), path)) + signature = t.cosi_sign(t.expand_path(path), digest, global_R, global_pk) + sig = signature.signature + return sig + + +if __name__ == '__main__': + if len(sys.argv) > 1: + iface = sys.argv[1] + else: + print('Usage: keyctl-proxy interface') + sys.exit(1) + host = netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] + daemon = Pyro4.Daemon(host=host, port=PORT) + proxy = KeyctlProxy() + uri = daemon.register(proxy, 'keyctl') + print('keyctl-proxy running at URI: "%s"' % uri) + daemon.requestLoop()