blob: 896afdd3ef49c662adef97043648b3c5381992b8 [file] [log] [blame]
Fabio Utzige89841d2018-12-21 11:19:06 -02001#! /usr/bin/env python3
2#
David Vincze71b8f982020-03-17 19:08:12 +01003# Copyright 2017-2020 Linaro Limited
Roland Mikhel3d92a6c2023-02-23 15:35:44 +01004# Copyright 2019-2023 Arm Limited
Fabio Utzige89841d2018-12-21 11:19:06 -02005#
David Brown79c4fcf2021-01-26 15:04:05 -07006# SPDX-License-Identifier: Apache-2.0
7#
Fabio Utzige89841d2018-12-21 11:19:06 -02008# Licensed under the Apache License, Version 2.0 (the "License");
9# you may not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS,
16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19
David Vinczeda8c9192019-03-26 17:17:41 +010020import re
Fabio Utzige89841d2018-12-21 11:19:06 -020021import click
22import getpass
23import imgtool.keys as keys
Fabio Utzig4a5477a2019-05-27 15:45:08 -030024import sys
Andrzej Puzdrowski160303c2022-03-15 15:41:14 +010025import base64
Fabio Utzig25c6a152019-09-10 12:52:26 -030026from imgtool import image, imgtool_version
Fabio Utzige89841d2018-12-21 11:19:06 -020027from imgtool.version import decode_version
David Vinczeca561352023-01-27 15:39:12 +010028from imgtool.dumpinfo import dump_imginfo
Fabio Utzig4facd1b2020-04-02 13:17:38 -030029from .keys import (
30 RSAUsageError, ECDSAUsageError, Ed25519UsageError, X25519UsageError)
Fabio Utzige89841d2018-12-21 11:19:06 -020031
David Vincze71b8f982020-03-17 19:08:12 +010032MIN_PYTHON_VERSION = (3, 6)
33if sys.version_info < MIN_PYTHON_VERSION:
34 sys.exit("Python %s.%s or newer is required by imgtool."
35 % MIN_PYTHON_VERSION)
36
Fabio Utzige89841d2018-12-21 11:19:06 -020037
38def gen_rsa2048(keyfile, passwd):
Fabio Utzig19fd79a2019-05-08 18:20:39 -030039 keys.RSA.generate().export_private(path=keyfile, passwd=passwd)
40
41
42def gen_rsa3072(keyfile, passwd):
43 keys.RSA.generate(key_size=3072).export_private(path=keyfile,
44 passwd=passwd)
Fabio Utzige89841d2018-12-21 11:19:06 -020045
46
47def gen_ecdsa_p256(keyfile, passwd):
48 keys.ECDSA256P1.generate().export_private(keyfile, passwd=passwd)
49
50
Roland Mikhel57041742023-02-03 14:43:13 +010051def gen_ecdsa_p384(keyfile, passwd):
52 keys.ECDSA384P1.generate().export_private(keyfile, passwd=passwd)
53
54
Fabio Utzig8101d1f2019-05-09 15:03:22 -030055def gen_ed25519(keyfile, passwd):
Fabio Utzig4bd4c7c2019-06-27 08:23:21 -030056 keys.Ed25519.generate().export_private(path=keyfile, passwd=passwd)
Fabio Utzig8101d1f2019-05-09 15:03:22 -030057
58
Fabio Utzig4facd1b2020-04-02 13:17:38 -030059def gen_x25519(keyfile, passwd):
60 keys.X25519.generate().export_private(path=keyfile, passwd=passwd)
61
62
Fabio Utzig4e2cdfe2022-09-28 17:44:01 -030063valid_langs = ['c', 'rust']
Bence Baloghed8d68a2023-07-18 15:57:52 +020064valid_encodings = ['lang-c', 'lang-rust', 'pem', 'raw']
Fabio Utzige89841d2018-12-21 11:19:06 -020065keygens = {
66 'rsa-2048': gen_rsa2048,
Fabio Utzig19fd79a2019-05-08 18:20:39 -030067 'rsa-3072': gen_rsa3072,
Fabio Utzige89841d2018-12-21 11:19:06 -020068 'ecdsa-p256': gen_ecdsa_p256,
Roland Mikhel57041742023-02-03 14:43:13 +010069 'ecdsa-p384': gen_ecdsa_p384,
Fabio Utzig4facd1b2020-04-02 13:17:38 -030070 'ed25519': gen_ed25519,
71 'x25519': gen_x25519,
Fabio Utzige89841d2018-12-21 11:19:06 -020072}
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +000073valid_formats = ['openssl', 'pkcs8']
Fabio Utzige89841d2018-12-21 11:19:06 -020074
Antonio de Angelis7ba01c02022-11-15 15:10:41 +000075
Andrzej Puzdrowski160303c2022-03-15 15:41:14 +010076def load_signature(sigfile):
77 with open(sigfile, 'rb') as f:
78 signature = base64.b64decode(f.read())
79 return signature
Fabio Utzige89841d2018-12-21 11:19:06 -020080
Antonio de Angelis7ba01c02022-11-15 15:10:41 +000081
Andrzej Puzdrowskif72e3742022-03-17 11:34:38 +010082def save_signature(sigfile, sig):
83 with open(sigfile, 'wb') as f:
84 signature = base64.b64encode(sig)
85 f.write(signature)
86
Antonio de Angelis7ba01c02022-11-15 15:10:41 +000087
Fabio Utzige89841d2018-12-21 11:19:06 -020088def load_key(keyfile):
89 # TODO: better handling of invalid pass-phrase
90 key = keys.load(keyfile)
91 if key is not None:
92 return key
93 passwd = getpass.getpass("Enter key passphrase: ").encode('utf-8')
94 return keys.load(keyfile, passwd)
95
96
97def get_password():
98 while True:
99 passwd = getpass.getpass("Enter key passphrase: ")
100 passwd2 = getpass.getpass("Reenter passphrase: ")
101 if passwd == passwd2:
102 break
103 print("Passwords do not match, try again")
104
105 # Password must be bytes, always use UTF-8 for consistent
106 # encoding.
107 return passwd.encode('utf-8')
108
109
110@click.option('-p', '--password', is_flag=True,
111 help='Prompt for password to protect key')
112@click.option('-t', '--type', metavar='type', required=True,
Fabio Utzig7ca28552019-12-13 11:24:20 -0300113 type=click.Choice(keygens.keys()), prompt=True,
114 help='{}'.format('One of: {}'.format(', '.join(keygens.keys()))))
Fabio Utzige89841d2018-12-21 11:19:06 -0200115@click.option('-k', '--key', metavar='filename', required=True)
116@click.command(help='Generate pub/private keypair')
117def keygen(type, key, password):
118 password = get_password() if password else None
119 keygens[type](key, password)
120
121
Fabio Utzig4e2cdfe2022-09-28 17:44:01 -0300122@click.option('-l', '--lang', metavar='lang',
123 type=click.Choice(valid_langs),
124 help='This option is deprecated. Please use the '
125 '`--encoding` option. '
126 'Valid langs: {}'.format(', '.join(valid_langs)))
127@click.option('-e', '--encoding', metavar='encoding',
128 type=click.Choice(valid_encodings),
129 help='Valid encodings: {}'.format(', '.join(valid_encodings)))
Fabio Utzige89841d2018-12-21 11:19:06 -0200130@click.option('-k', '--key', metavar='filename', required=True)
Bence Balogh367aefb2023-07-18 15:51:54 +0200131@click.option('-o', '--output', metavar='output', required=False,
132 help='Specify the output file\'s name. \
133 The stdout is used if it is not provided.')
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200134@click.command(help='Dump public key from keypair')
Bence Balogh367aefb2023-07-18 15:51:54 +0200135def getpub(key, encoding, lang, output):
Fabio Utzig4e2cdfe2022-09-28 17:44:01 -0300136 if encoding and lang:
Antonio de Angelis7ba01c02022-11-15 15:10:41 +0000137 raise click.UsageError('Please use only one of `--encoding/-e` '
138 'or `--lang/-l`')
Fabio Utzig4e2cdfe2022-09-28 17:44:01 -0300139 elif not encoding and not lang:
140 # Preserve old behavior defaulting to `c`. If `lang` is removed,
141 # `default=valid_encodings[0]` should be added to `-e` param.
142 lang = valid_langs[0]
Fabio Utzige89841d2018-12-21 11:19:06 -0200143 key = load_key(key)
Bence Balogh367aefb2023-07-18 15:51:54 +0200144
145 if not output:
146 output = sys.stdout
Fabio Utzige89841d2018-12-21 11:19:06 -0200147 if key is None:
148 print("Invalid passphrase")
Fabio Utzig4e2cdfe2022-09-28 17:44:01 -0300149 elif lang == 'c' or encoding == 'lang-c':
Bence Balogh367aefb2023-07-18 15:51:54 +0200150 key.emit_c_public(file=output)
Fabio Utzig4e2cdfe2022-09-28 17:44:01 -0300151 elif lang == 'rust' or encoding == 'lang-rust':
Bence Balogh367aefb2023-07-18 15:51:54 +0200152 key.emit_rust_public(file=output)
Fabio Utzig4e2cdfe2022-09-28 17:44:01 -0300153 elif encoding == 'pem':
Bence Balogh367aefb2023-07-18 15:51:54 +0200154 key.emit_public_pem(file=output)
Bence Baloghed8d68a2023-07-18 15:57:52 +0200155 elif encoding == 'raw':
156 key.emit_raw_public(file=output)
Fabio Utzige89841d2018-12-21 11:19:06 -0200157 else:
Fabio Utzig4e2cdfe2022-09-28 17:44:01 -0300158 raise click.UsageError()
Fabio Utzige89841d2018-12-21 11:19:06 -0200159
160
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200161@click.option('--minimal', default=False, is_flag=True,
162 help='Reduce the size of the dumped private key to include only '
163 'the minimum amount of data required to decrypt. This '
164 'might require changes to the build config. Check the docs!'
165 )
166@click.option('-k', '--key', metavar='filename', required=True)
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +0000167@click.option('-f', '--format',
168 type=click.Choice(valid_formats),
Fabio Utzig8f289ba2023-01-09 21:01:55 -0300169 help='Valid formats: {}'.format(', '.join(valid_formats))
170 )
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200171@click.command(help='Dump private key from keypair')
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +0000172def getpriv(key, minimal, format):
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200173 key = load_key(key)
174 if key is None:
175 print("Invalid passphrase")
Fabio Utzig1f508922020-01-15 11:37:51 -0300176 try:
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +0000177 key.emit_private(minimal, format)
Fabio Utzig4facd1b2020-04-02 13:17:38 -0300178 except (RSAUsageError, ECDSAUsageError, Ed25519UsageError,
179 X25519UsageError) as e:
Fabio Utzig1f508922020-01-15 11:37:51 -0300180 raise click.UsageError(e)
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200181
182
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300183@click.argument('imgfile')
184@click.option('-k', '--key', metavar='filename')
185@click.command(help="Check that signed image can be verified by given key")
186def verify(key, imgfile):
187 key = load_key(key) if key else None
Casper Meijn2a01f3f2020-08-22 13:51:40 +0200188 ret, version, digest = image.Image.verify(imgfile, key)
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300189 if ret == image.VerifyResult.OK:
190 print("Image was correctly validated")
Marek Pietae9555102019-08-08 16:08:16 +0200191 print("Image version: {}.{}.{}+{}".format(*version))
Casper Meijn2a01f3f2020-08-22 13:51:40 +0200192 print("Image digest: {}".format(digest.hex()))
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300193 return
194 elif ret == image.VerifyResult.INVALID_MAGIC:
195 print("Invalid image magic; is this an MCUboot image?")
Christian Skubichf13db122019-07-31 11:34:15 +0200196 elif ret == image.VerifyResult.INVALID_TLV_INFO_MAGIC:
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300197 print("Invalid TLV info magic; is this an MCUboot image?")
198 elif ret == image.VerifyResult.INVALID_HASH:
Roland Mikhel57041742023-02-03 14:43:13 +0100199 print("Image has an invalid hash")
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300200 elif ret == image.VerifyResult.INVALID_SIGNATURE:
201 print("No signature found for the given key")
Christian Skubichf13db122019-07-31 11:34:15 +0200202 else:
203 print("Unknown return code: {}".format(ret))
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300204 sys.exit(1)
205
206
David Vinczeca561352023-01-27 15:39:12 +0100207@click.argument('imgfile')
208@click.option('-o', '--outfile', metavar='filename', required=False,
209 help='Save image information to outfile in YAML format')
210@click.option('-s', '--silent', default=False, is_flag=True,
211 help='Do not print image information to output')
212@click.command(help='Print header, TLV area and trailer information '
213 'of a signed image')
214def dumpinfo(imgfile, outfile, silent):
215 dump_imginfo(imgfile, outfile, silent)
216 print("dumpinfo has run successfully")
217
218
Fabio Utzige89841d2018-12-21 11:19:06 -0200219def validate_version(ctx, param, value):
220 try:
221 decode_version(value)
222 return value
223 except ValueError as e:
224 raise click.BadParameter("{}".format(e))
225
226
David Vincze1a7a6902020-02-18 15:05:16 +0100227def validate_security_counter(ctx, param, value):
228 if value is not None:
229 if value.lower() == 'auto':
230 return 'auto'
231 else:
232 try:
233 return int(value, 0)
234 except ValueError:
235 raise click.BadParameter(
236 "{} is not a valid integer. Please use code literals "
237 "prefixed with 0b/0B, 0o/0O, or 0x/0X as necessary."
238 .format(value))
239
240
Fabio Utzige89841d2018-12-21 11:19:06 -0200241def validate_header_size(ctx, param, value):
242 min_hdr_size = image.IMAGE_HEADER_SIZE
243 if value < min_hdr_size:
244 raise click.BadParameter(
245 "Minimum value for -H/--header-size is {}".format(min_hdr_size))
246 return value
247
248
David Vinczeda8c9192019-03-26 17:17:41 +0100249def get_dependencies(ctx, param, value):
250 if value is not None:
251 versions = []
252 images = re.findall(r"\((\d+)", value)
253 if len(images) == 0:
254 raise click.BadParameter(
255 "Image dependency format is invalid: {}".format(value))
256 raw_versions = re.findall(r",\s*([0-9.+]+)\)", value)
257 if len(images) != len(raw_versions):
258 raise click.BadParameter(
259 '''There's a mismatch between the number of dependency images
260 and versions in: {}'''.format(value))
261 for raw_version in raw_versions:
262 try:
263 versions.append(decode_version(raw_version))
264 except ValueError as e:
265 raise click.BadParameter("{}".format(e))
266 dependencies = dict()
267 dependencies[image.DEP_IMAGES_KEY] = images
268 dependencies[image.DEP_VERSIONS_KEY] = versions
269 return dependencies
270
271
Fabio Utzige89841d2018-12-21 11:19:06 -0200272class BasedIntParamType(click.ParamType):
273 name = 'integer'
274
275 def convert(self, value, param, ctx):
276 try:
David Vincze1a7a6902020-02-18 15:05:16 +0100277 return int(value, 0)
Fabio Utzige89841d2018-12-21 11:19:06 -0200278 except ValueError:
David Vincze1a7a6902020-02-18 15:05:16 +0100279 self.fail('%s is not a valid integer. Please use code literals '
280 'prefixed with 0b/0B, 0o/0O, or 0x/0X as necessary.'
281 % value, param, ctx)
Fabio Utzige89841d2018-12-21 11:19:06 -0200282
283
284@click.argument('outfile')
285@click.argument('infile')
Ihor Slabkyy24d93732020-03-10 15:33:57 +0200286@click.option('--custom-tlv', required=False, nargs=2, default=[],
287 multiple=True, metavar='[tag] [value]',
288 help='Custom TLV that will be placed into protected area. '
289 'Add "0x" prefix if the value should be interpreted as an '
290 'integer, otherwise it will be interpreted as a string. '
291 'Specify the option multiple times to add multiple TLVs.')
Fabio Utzig9117fde2019-10-17 11:11:46 -0300292@click.option('-R', '--erased-val', type=click.Choice(['0', '0xff']),
293 required=False,
294 help='The value that is read back from erased flash.')
Fabio Utzigedbabcf2019-10-11 13:03:37 -0300295@click.option('-x', '--hex-addr', type=BasedIntParamType(), required=False,
296 help='Adjust address in hex output file.')
Håkon Øye Amundsendf8c8912019-08-26 12:15:28 +0000297@click.option('-L', '--load-addr', type=BasedIntParamType(), required=False,
David Vincze1e0c5442020-04-07 14:12:33 +0200298 help='Load address for image when it should run from RAM.')
Dominik Ermel50820b12020-12-14 13:16:46 +0000299@click.option('-F', '--rom-fixed', type=BasedIntParamType(), required=False,
300 help='Set flash address the image is built for.')
Fabio Utzig9a492d52020-01-15 11:31:52 -0300301@click.option('--save-enctlv', default=False, is_flag=True,
302 help='When upgrading, save encrypted key TLVs instead of plain '
303 'keys. Enable when BOOT_SWAP_SAVE_ENCTLV config option '
304 'was set.')
Fabio Utzige89841d2018-12-21 11:19:06 -0200305@click.option('-E', '--encrypt', metavar='filename',
David Vinczee574f2d2020-07-10 11:42:03 +0200306 help='Encrypt image using the provided public key. '
Tamas Banfe031092020-09-10 17:32:39 +0200307 '(Not supported in direct-xip or ram-load mode.)')
Salome Thirot0f641972021-05-14 11:19:55 +0100308@click.option('--encrypt-keylen', default='128',
Antonio de Angelis7ba01c02022-11-15 15:10:41 +0000309 type=click.Choice(['128', '256']),
Salome Thirot0f641972021-05-14 11:19:55 +0100310 help='When encrypting the image using AES, select a 128 bit or '
311 '256 bit key len.')
Michel Jaouend09aa6b2022-01-07 16:48:58 +0100312@click.option('-c', '--clear', required=False, is_flag=True, default=False,
313 help='Output a non-encrypted image with encryption capabilities,'
314 'so it can be installed in the primary slot, and encrypted '
315 'when swapped to the secondary.')
Fabio Utzige89841d2018-12-21 11:19:06 -0200316@click.option('-e', '--endian', type=click.Choice(['little', 'big']),
317 default='little', help="Select little or big endian")
318@click.option('--overwrite-only', default=False, is_flag=True,
319 help='Use overwrite-only instead of swap upgrades')
David Vincze71b8f982020-03-17 19:08:12 +0100320@click.option('--boot-record', metavar='sw_type', help='Create CBOR encoded '
321 'boot record TLV. The sw_type represents the role of the '
322 'software component (e.g. CoFM for coprocessor firmware). '
323 '[max. 12 characters]')
Fabio Utzige89841d2018-12-21 11:19:06 -0200324@click.option('-M', '--max-sectors', type=int,
Fabio Utzig9a492d52020-01-15 11:31:52 -0300325 help='When padding allow for this amount of sectors (defaults '
326 'to 128)')
Henrik Brix Andersen0ce958e2020-03-11 14:04:11 +0100327@click.option('--confirm', default=False, is_flag=True,
Martí Bolívar009a1502020-09-04 14:23:39 -0700328 help='When padding the image, mark it as confirmed (implies '
329 '--pad)')
Fabio Utzige89841d2018-12-21 11:19:06 -0200330@click.option('--pad', default=False, is_flag=True,
331 help='Pad image to --slot-size bytes, adding trailer magic')
332@click.option('-S', '--slot-size', type=BasedIntParamType(), required=True,
Fabio Utzig826abf42020-07-13 20:56:35 -0300333 help='Size of the slot. If the slots have different sizes, use '
334 'the size of the secondary slot.')
Fabio Utzige89841d2018-12-21 11:19:06 -0200335@click.option('--pad-header', default=False, is_flag=True,
Fabio Utzig9a492d52020-01-15 11:31:52 -0300336 help='Add --header-size zeroed bytes at the beginning of the '
337 'image')
Fabio Utzige89841d2018-12-21 11:19:06 -0200338@click.option('-H', '--header-size', callback=validate_header_size,
339 type=BasedIntParamType(), required=True)
David Brown4878c272020-03-10 16:23:56 -0600340@click.option('--pad-sig', default=False, is_flag=True,
341 help='Add 0-2 bytes of padding to ECDSA signature '
342 '(for mcuboot <1.5)')
David Vinczeda8c9192019-03-26 17:17:41 +0100343@click.option('-d', '--dependencies', callback=get_dependencies,
344 required=False, help='''Add dependence on another image, format:
345 "(<image_ID>,<image_version>), ... "''')
David Vincze1a7a6902020-02-18 15:05:16 +0100346@click.option('-s', '--security-counter', callback=validate_security_counter,
347 help='Specify the value of security counter. Use the `auto` '
348 'keyword to automatically generate it from the image version.')
Fabio Utzige89841d2018-12-21 11:19:06 -0200349@click.option('-v', '--version', callback=validate_version, required=True)
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000350@click.option('--align', type=click.Choice(['1', '2', '4', '8', '16', '32']),
Fabio Utzige89841d2018-12-21 11:19:06 -0200351 required=True)
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000352@click.option('--max-align', type=click.Choice(['8', '16', '32']),
Piotr Mienkowskib6d5cf32022-01-31 01:01:11 +0100353 required=False,
354 help='Maximum flash alignment. Set if flash alignment of the '
355 'primary and secondary slot differ and any of them is larger '
356 'than 8.')
David Vinczedde178d2020-03-26 20:06:01 +0100357@click.option('--public-key-format', type=click.Choice(['hash', 'full']),
358 default='hash', help='In what format to add the public key to '
359 'the image manifest: full key or hash of the key.')
Fabio Utzige89841d2018-12-21 11:19:06 -0200360@click.option('-k', '--key', metavar='filename')
Andrzej Puzdrowski160303c2022-03-15 15:41:14 +0100361@click.option('--fix-sig', metavar='filename',
iysheng6093cbb2022-05-28 17:00:40 +0800362 help='fixed signature for the image. It will be used instead of '
Andrzej Puzdrowski160303c2022-03-15 15:41:14 +0100363 'the signature calculated using the public key')
364@click.option('--fix-sig-pubkey', metavar='filename',
365 help='public key relevant to fixed signature')
Andrzej Puzdrowskif72e3742022-03-17 11:34:38 +0100366@click.option('--sig-out', metavar='filename',
iysheng6093cbb2022-05-28 17:00:40 +0800367 help='Path to the file to which signature will be written. '
Andrzej Puzdrowskif72e3742022-03-17 11:34:38 +0100368 'The image signature will be encoded as base64 formatted string')
Andrzej Puzdrowskidfce0be2022-03-28 09:34:15 +0200369@click.option('--vector-to-sign', type=click.Choice(['payload', 'digest']),
Antonio de Angelis7ba01c02022-11-15 15:10:41 +0000370 help='send to OUTFILE the payload or payload''s digest instead '
371 'of complied image. These data can be used for external image '
Andrzej Puzdrowskidfce0be2022-03-28 09:34:15 +0200372 'signing')
Fabio Utzig7c00acd2019-01-07 09:54:20 -0200373@click.command(help='''Create a signed or unsigned image\n
374 INFILE and OUTFILE are parsed as Intel HEX if the params have
Håkon Øye Amundsendf8c8912019-08-26 12:15:28 +0000375 .hex extension, otherwise binary format is used''')
David Vinczedde178d2020-03-26 20:06:01 +0100376def sign(key, public_key_format, align, version, pad_sig, header_size,
377 pad_header, slot_size, pad, confirm, max_sectors, overwrite_only,
Salome Thirot0f641972021-05-14 11:19:55 +0100378 endian, encrypt_keylen, encrypt, infile, outfile, dependencies,
379 load_addr, hex_addr, erased_val, save_enctlv, security_counter,
Andrzej Puzdrowski160303c2022-03-15 15:41:14 +0100380 boot_record, custom_tlv, rom_fixed, max_align, clear, fix_sig,
David Vincze7f982b02023-04-27 16:12:17 +0200381 fix_sig_pubkey, sig_out, vector_to_sign):
Martí Bolívar009a1502020-09-04 14:23:39 -0700382
383 if confirm:
384 # Confirmed but non-padded images don't make much sense, because
385 # otherwise there's no trailer area for writing the confirmed status.
386 pad = True
Fabio Utzig7c00acd2019-01-07 09:54:20 -0200387 img = image.Image(version=decode_version(version), header_size=header_size,
Henrik Brix Andersen0ce958e2020-03-11 14:04:11 +0100388 pad_header=pad_header, pad=pad, confirm=confirm,
389 align=int(align), slot_size=slot_size,
390 max_sectors=max_sectors, overwrite_only=overwrite_only,
Dominik Ermel50820b12020-12-14 13:16:46 +0000391 endian=endian, load_addr=load_addr, rom_fixed=rom_fixed,
392 erased_val=erased_val, save_enctlv=save_enctlv,
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000393 security_counter=security_counter, max_align=max_align)
Fabio Utzig7c00acd2019-01-07 09:54:20 -0200394 img.load(infile)
Fabio Utzige89841d2018-12-21 11:19:06 -0200395 key = load_key(key) if key else None
396 enckey = load_key(encrypt) if encrypt else None
Fabio Utzig7a3b2602019-10-22 09:56:44 -0300397 if enckey and key:
398 if ((isinstance(key, keys.ECDSA256P1) and
399 not isinstance(enckey, keys.ECDSA256P1Public))
Roland Mikhel57041742023-02-03 14:43:13 +0100400 or (isinstance(key, keys.ECDSA384P1) and
401 not isinstance(enckey, keys.ECDSA384P1Public))
Fabio Utzig7a3b2602019-10-22 09:56:44 -0300402 or (isinstance(key, keys.RSA) and
403 not isinstance(enckey, keys.RSAPublic))):
404 # FIXME
Fabio Utzig1f508922020-01-15 11:37:51 -0300405 raise click.UsageError("Signing and encryption must use the same "
406 "type of key")
David Brown4878c272020-03-10 16:23:56 -0600407
408 if pad_sig and hasattr(key, 'pad_sig'):
409 key.pad_sig = True
410
Ihor Slabkyy24d93732020-03-10 15:33:57 +0200411 # Get list of custom protected TLVs from the command-line
412 custom_tlvs = {}
413 for tlv in custom_tlv:
414 tag = int(tlv[0], 0)
415 if tag in custom_tlvs:
416 raise click.UsageError('Custom TLV %s already exists.' % hex(tag))
417 if tag in image.TLV_VALUES.values():
418 raise click.UsageError(
419 'Custom TLV %s conflicts with predefined TLV.' % hex(tag))
420
421 value = tlv[1]
422 if value.startswith('0x'):
423 if len(value[2:]) % 2:
424 raise click.UsageError('Custom TLV length is odd.')
425 custom_tlvs[tag] = bytes.fromhex(value[2:])
426 else:
427 custom_tlvs[tag] = value.encode('utf-8')
428
Andrzej Puzdrowski160303c2022-03-15 15:41:14 +0100429 # Allow signature calculated externally.
430 raw_signature = load_signature(fix_sig) if fix_sig else None
431
432 baked_signature = None
433 pub_key = None
434
435 if raw_signature is not None:
436 if fix_sig_pubkey is None:
Antonio de Angelis7ba01c02022-11-15 15:10:41 +0000437 raise click.UsageError(
Andrzej Puzdrowski160303c2022-03-15 15:41:14 +0100438 'public key of the fixed signature is not specified')
439
440 pub_key = load_key(fix_sig_pubkey)
441
442 baked_signature = {
Antonio de Angelis7ba01c02022-11-15 15:10:41 +0000443 'value': raw_signature
Andrzej Puzdrowski160303c2022-03-15 15:41:14 +0100444 }
445
Ihor Slabkyy24d93732020-03-10 15:33:57 +0200446 img.create(key, public_key_format, enckey, dependencies, boot_record,
Antonio de Angelis7ba01c02022-11-15 15:10:41 +0000447 custom_tlvs, int(encrypt_keylen), clear, baked_signature,
David Vincze7f982b02023-04-27 16:12:17 +0200448 pub_key, vector_to_sign)
Fabio Utzigedbabcf2019-10-11 13:03:37 -0300449 img.save(outfile, hex_addr)
Fabio Utzige89841d2018-12-21 11:19:06 -0200450
Andrzej Puzdrowskif72e3742022-03-17 11:34:38 +0100451 if sig_out is not None:
452 new_signature = img.get_signature()
453 save_signature(sig_out, new_signature)
454
Fabio Utzige89841d2018-12-21 11:19:06 -0200455
456class AliasesGroup(click.Group):
457
458 _aliases = {
459 "create": "sign",
460 }
461
462 def list_commands(self, ctx):
463 cmds = [k for k in self.commands]
464 aliases = [k for k in self._aliases]
465 return sorted(cmds + aliases)
466
467 def get_command(self, ctx, cmd_name):
468 rv = click.Group.get_command(self, ctx, cmd_name)
469 if rv is not None:
470 return rv
471 if cmd_name in self._aliases:
472 return click.Group.get_command(self, ctx, self._aliases[cmd_name])
473 return None
474
475
Fabio Utzig25c6a152019-09-10 12:52:26 -0300476@click.command(help='Print imgtool version information')
477def version():
478 print(imgtool_version)
479
480
Fabio Utzige89841d2018-12-21 11:19:06 -0200481@click.command(cls=AliasesGroup,
482 context_settings=dict(help_option_names=['-h', '--help']))
483def imgtool():
484 pass
485
486
487imgtool.add_command(keygen)
488imgtool.add_command(getpub)
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200489imgtool.add_command(getpriv)
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300490imgtool.add_command(verify)
Fabio Utzige89841d2018-12-21 11:19:06 -0200491imgtool.add_command(sign)
Fabio Utzig25c6a152019-09-10 12:52:26 -0300492imgtool.add_command(version)
David Vinczeca561352023-01-27 15:39:12 +0100493imgtool.add_command(dumpinfo)
Fabio Utzige89841d2018-12-21 11:19:06 -0200494
495
496if __name__ == '__main__':
497 imgtool()