blob: 5c6563084211c906db9e8ea438a9be4893ac7d40 [file] [log] [blame]
Jens Wiklanderbc420742015-05-05 14:59:15 +02001#!/usr/bin/env python
2#
Jens Wiklandercd5cf432017-11-28 16:59:15 +01003# Copyright (c) 2015, 2017, Linaro Limited
Jens Wiklanderbc420742015-05-05 14:59:15 +02004# All rights reserved.
5#
Jens Wiklandercd5cf432017-11-28 16:59:15 +01006# SPDX-License-Identifier: BSD-2-Clause
7#
Jens Wiklanderbc420742015-05-05 14:59:15 +02008# 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 Wiklandercd5cf432017-11-28 16:59:15 +010029
30def uuid_parse(s):
31 from uuid import UUID
32 return UUID(s)
33
34
35def int_parse(str):
36 return int(str, 0)
37
Jens Wiklanderbc420742015-05-05 14:59:15 +020038
39def get_args():
Jens Wiklandercd5cf432017-11-28 16:59:15 +010040 from argparse import ArgumentParser
Jens Wiklanderbc420742015-05-05 14:59:15 +020041
Jens Wiklandercd5cf432017-11-28 16:59:15 +010042 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 Wiklanderbc420742015-05-05 14:59:15 +020052
53def main():
Jens Wiklandercd5cf432017-11-28 16:59:15 +010054 from Crypto.Signature import PKCS1_v1_5
55 from Crypto.Hash import SHA256
56 from Crypto.PublicKey import RSA
57 import struct
Jens Wiklanderbc420742015-05-05 14:59:15 +020058
Jens Wiklandercd5cf432017-11-28 16:59:15 +010059 args = get_args()
Jens Wiklanderbc420742015-05-05 14:59:15 +020060
Jens Wiklandercd5cf432017-11-28 16:59:15 +010061 f = open(args.key, 'rb')
62 key = RSA.importKey(f.read())
63 f.close()
Jens Wiklanderbc420742015-05-05 14:59:15 +020064
Jens Wiklandercd5cf432017-11-28 16:59:15 +010065 f = open(args.inf, 'rb')
66 img = f.read()
67 f.close()
Jens Wiklanderbc420742015-05-05 14:59:15 +020068
Jens Wiklandercd5cf432017-11-28 16:59:15 +010069 signer = PKCS1_v1_5.new(key)
70 h = SHA256.new()
Jens Wiklanderbc420742015-05-05 14:59:15 +020071
Jens Wiklandercd5cf432017-11-28 16:59:15 +010072 digest_len = h.digest_size
73 sig_len = len(signer.sign(h))
74 img_size = len(img)
Jens Wiklanderbc420742015-05-05 14:59:15 +020075
Jens Wiklandercd5cf432017-11-28 16:59:15 +010076 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 Wiklanderbc420742015-05-05 14:59:15 +020083
Jens Wiklandercd5cf432017-11-28 16:59:15 +010084 h.update(shdr)
85 h.update(shdr_uuid)
86 h.update(shdr_version)
87 h.update(img)
88 sig = signer.sign(h)
Jens Wiklanderbc420742015-05-05 14:59:15 +020089
Jens Wiklandercd5cf432017-11-28 16:59:15 +010090 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 Wiklanderbc420742015-05-05 14:59:15 +020099
100if __name__ == "__main__":
Jens Wiklandercd5cf432017-11-28 16:59:15 +0100101 main()