aboutsummaryrefslogtreecommitdiff
path: root/platform/ext/target/cypress/psoc64/security/sign.py
blob: f5315101c7bad31f368067dd1d5cacf8a11f2b2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python3
"""
Copyright (c) 2019 Cypress Semiconductor Corporation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from cysecuretools import CySecureTools
import sys, argparse
import os
from shutil import copyfile, move


def myargs(argv):
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument('-h', '--help',
                        dest='show_help',
                        action='help',
                        help='Print this help message and exit')

    parser.add_argument('-p', '--policy',
                        dest='policy_file',
                        action='store',
                        type=str,
                        help="Device policy file",
                        required=True)

    parser.add_argument('-d', '--device',
                        dest='device',
                        action='store',
                        type=str,
                        help="device target",
                        required=True)

    parser.add_argument('-s', '--s_hex',
                        dest='s_hex_file',
                        action='store',
                        default='',
                        type=str,
                        help="TFM SPE image to sign in hex format")

    parser.add_argument('-ns', '--ns_hex',
                        dest='ns_hex_file',
                        action='store',
                        default='',
                        type=str,
                        help="TFM NSPE image to sign in hex format")

    options = parser.parse_args(argv)
    return options


def main(argv):

    options = myargs(argv)
    print("options={}".format(options))

    if not options.s_hex_file and not options.ns_hex_file:
        print('Error: no files to sign')
        exit(1)

    tools = CySecureTools(options.device, options.policy_file)

    if options.s_hex_file:
        print('signing tfm_s image:', options.s_hex_file)

        # sign_image overwrites original image, make a copy of it
        name, ext = os.path.splitext(options.s_hex_file)
        s_hex_signed_file = name + '_signed' + ext
        try:
            copyfile(options.s_hex_file, s_hex_signed_file)
        except IOError as e:
            print("Failed to copy file '{}' to '{}' ({})"
                   .format(options.s_hex_file, s_hex_signed_file, e.message))
            raise

        tools.sign_image(s_hex_signed_file, 1)

    if options.ns_hex_file:
        print('signing tfm_ns image:', options.ns_hex_file)

        name, ext = os.path.splitext(options.ns_hex_file)
        ns_hex_signed_file = name + '_signed' + ext
        try:
            copyfile(options.ns_hex_file, ns_hex_signed_file)
        except IOError as e:
            print("Failed to copy file '{}' to '{}' ({})"
                   .format(options.ns_hex_file, ns_hex_signed_file, e.message))
            raise

        tools.sign_image(ns_hex_signed_file, 16)

        # for CM4, sign_image creates an unsigned copy of the image
        # named <image to sign>_cm4.hex. Delete it to avoid confusion.
        file_name = name + '_signed_cm4' + ext
        if os.path.isfile(file_name):
            try:
                os.remove(file_name)
            except IOError:
                print("Could not erase '{}'"
                          .format(file_name))

    print('Done.')

if __name__ == "__main__":
   main(sys.argv[1:])