Jens Wiklander | bc42074 | 2015-05-05 14:59:15 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
Jens Wiklander | cd5cf43 | 2017-11-28 16:59:15 +0100 | [diff] [blame^] | 3 | # Copyright (c) 2015, 2017, Linaro Limited |
Jens Wiklander | bc42074 | 2015-05-05 14:59:15 +0200 | [diff] [blame] | 4 | # All rights reserved. |
| 5 | # |
Jens Wiklander | cd5cf43 | 2017-11-28 16:59:15 +0100 | [diff] [blame^] | 6 | # SPDX-License-Identifier: BSD-2-Clause |
| 7 | # |
Jens Wiklander | bc42074 | 2015-05-05 14:59:15 +0200 | [diff] [blame] | 8 | # Redistribution and use in source and binary forms, with or without |
| 9 | # modification, are permitted provided that the following conditions are met: |
| 10 | # |
| 11 | # 1. Redistributions of source code must retain the above copyright notice, |
| 12 | # this list of conditions and the following disclaimer. |
| 13 | # |
| 14 | # 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | # this list of conditions and the following disclaimer in the documentation |
| 16 | # and/or other materials provided with the distribution. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | # POSSIBILITY OF SUCH DAMAGE. |
Jens Wiklander | cd5cf43 | 2017-11-28 16:59:15 +0100 | [diff] [blame^] | 29 | |
| 30 | def uuid_parse(s): |
| 31 | from uuid import UUID |
| 32 | return UUID(s) |
| 33 | |
| 34 | |
| 35 | def int_parse(str): |
| 36 | return int(str, 0) |
| 37 | |
Jens Wiklander | bc42074 | 2015-05-05 14:59:15 +0200 | [diff] [blame] | 38 | |
| 39 | def get_args(): |
Jens Wiklander | cd5cf43 | 2017-11-28 16:59:15 +0100 | [diff] [blame^] | 40 | from argparse import ArgumentParser |
Jens Wiklander | bc42074 | 2015-05-05 14:59:15 +0200 | [diff] [blame] | 41 | |
Jens Wiklander | cd5cf43 | 2017-11-28 16:59:15 +0100 | [diff] [blame^] | 42 | parser = ArgumentParser() |
| 43 | parser.add_argument('--uuid', required=True, |
| 44 | type=uuid_parse, help='UUID of TA') |
| 45 | parser.add_argument('--version', type=int_parse, default=0, help='Version') |
| 46 | parser.add_argument('--key', required=True, help='Name of key file') |
| 47 | parser.add_argument('--in', required=True, dest='inf', |
| 48 | help='Name of in file') |
| 49 | parser.add_argument('--out', required=True, help='Name of out file') |
| 50 | return parser.parse_args() |
| 51 | |
Jens Wiklander | bc42074 | 2015-05-05 14:59:15 +0200 | [diff] [blame] | 52 | |
| 53 | def main(): |
Jens Wiklander | cd5cf43 | 2017-11-28 16:59:15 +0100 | [diff] [blame^] | 54 | from Crypto.Signature import PKCS1_v1_5 |
| 55 | from Crypto.Hash import SHA256 |
| 56 | from Crypto.PublicKey import RSA |
| 57 | import struct |
Jens Wiklander | bc42074 | 2015-05-05 14:59:15 +0200 | [diff] [blame] | 58 | |
Jens Wiklander | cd5cf43 | 2017-11-28 16:59:15 +0100 | [diff] [blame^] | 59 | args = get_args() |
Jens Wiklander | bc42074 | 2015-05-05 14:59:15 +0200 | [diff] [blame] | 60 | |
Jens Wiklander | cd5cf43 | 2017-11-28 16:59:15 +0100 | [diff] [blame^] | 61 | f = open(args.key, 'rb') |
| 62 | key = RSA.importKey(f.read()) |
| 63 | f.close() |
Jens Wiklander | bc42074 | 2015-05-05 14:59:15 +0200 | [diff] [blame] | 64 | |
Jens Wiklander | cd5cf43 | 2017-11-28 16:59:15 +0100 | [diff] [blame^] | 65 | f = open(args.inf, 'rb') |
| 66 | img = f.read() |
| 67 | f.close() |
Jens Wiklander | bc42074 | 2015-05-05 14:59:15 +0200 | [diff] [blame] | 68 | |
Jens Wiklander | cd5cf43 | 2017-11-28 16:59:15 +0100 | [diff] [blame^] | 69 | signer = PKCS1_v1_5.new(key) |
| 70 | h = SHA256.new() |
Jens Wiklander | bc42074 | 2015-05-05 14:59:15 +0200 | [diff] [blame] | 71 | |
Jens Wiklander | cd5cf43 | 2017-11-28 16:59:15 +0100 | [diff] [blame^] | 72 | digest_len = h.digest_size |
| 73 | sig_len = len(signer.sign(h)) |
| 74 | img_size = len(img) |
Jens Wiklander | bc42074 | 2015-05-05 14:59:15 +0200 | [diff] [blame] | 75 | |
Jens Wiklander | cd5cf43 | 2017-11-28 16:59:15 +0100 | [diff] [blame^] | 76 | magic = 0x4f545348 # SHDR_MAGIC |
| 77 | img_type = 1 # SHDR_BOOTSTRAP_TA |
| 78 | algo = 0x70004830 # TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 |
| 79 | shdr = struct.pack('<IIIIHH', |
| 80 | magic, img_type, img_size, algo, digest_len, sig_len) |
| 81 | shdr_uuid = args.uuid.bytes |
| 82 | shdr_version = struct.pack('<I', args.version) |
Jens Wiklander | bc42074 | 2015-05-05 14:59:15 +0200 | [diff] [blame] | 83 | |
Jens Wiklander | cd5cf43 | 2017-11-28 16:59:15 +0100 | [diff] [blame^] | 84 | h.update(shdr) |
| 85 | h.update(shdr_uuid) |
| 86 | h.update(shdr_version) |
| 87 | h.update(img) |
| 88 | sig = signer.sign(h) |
Jens Wiklander | bc42074 | 2015-05-05 14:59:15 +0200 | [diff] [blame] | 89 | |
Jens Wiklander | cd5cf43 | 2017-11-28 16:59:15 +0100 | [diff] [blame^] | 90 | f = open(args.out, 'wb') |
| 91 | f.write(shdr) |
| 92 | f.write(h.digest()) |
| 93 | f.write(sig) |
| 94 | f.write(shdr_uuid) |
| 95 | f.write(shdr_version) |
| 96 | f.write(img) |
| 97 | f.close() |
| 98 | |
Jens Wiklander | bc42074 | 2015-05-05 14:59:15 +0200 | [diff] [blame] | 99 | |
| 100 | if __name__ == "__main__": |
Jens Wiklander | cd5cf43 | 2017-11-28 16:59:15 +0100 | [diff] [blame^] | 101 | main() |