blob: 1cdb792a54cbaaba13c183119ae514c5cec3d10e [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
Mateusz Michalek63fa7e42024-08-09 12:16:40 +020025import struct
26import os
27import lzma
28import hashlib
Andrzej Puzdrowski160303c2022-03-15 15:41:14 +010029import base64
Fabio Utzig25c6a152019-09-10 12:52:26 -030030from imgtool import image, imgtool_version
Fabio Utzige89841d2018-12-21 11:19:06 -020031from imgtool.version import decode_version
David Vinczeca561352023-01-27 15:39:12 +010032from imgtool.dumpinfo import dump_imginfo
Fabio Utzig4facd1b2020-04-02 13:17:38 -030033from .keys import (
34 RSAUsageError, ECDSAUsageError, Ed25519UsageError, X25519UsageError)
Fabio Utzige89841d2018-12-21 11:19:06 -020035
Mateusz Michalek63fa7e42024-08-09 12:16:40 +020036comp_default_dictsize=131072
37comp_default_pb=2
38comp_default_lc=3
39comp_default_lp=1
40comp_default_preset=9
41
42
David Vincze71b8f982020-03-17 19:08:12 +010043MIN_PYTHON_VERSION = (3, 6)
44if sys.version_info < MIN_PYTHON_VERSION:
45 sys.exit("Python %s.%s or newer is required by imgtool."
46 % MIN_PYTHON_VERSION)
47
Fabio Utzige89841d2018-12-21 11:19:06 -020048
49def gen_rsa2048(keyfile, passwd):
Fabio Utzig19fd79a2019-05-08 18:20:39 -030050 keys.RSA.generate().export_private(path=keyfile, passwd=passwd)
51
52
53def gen_rsa3072(keyfile, passwd):
54 keys.RSA.generate(key_size=3072).export_private(path=keyfile,
55 passwd=passwd)
Fabio Utzige89841d2018-12-21 11:19:06 -020056
57
58def gen_ecdsa_p256(keyfile, passwd):
59 keys.ECDSA256P1.generate().export_private(keyfile, passwd=passwd)
60
61
Roland Mikhel57041742023-02-03 14:43:13 +010062def gen_ecdsa_p384(keyfile, passwd):
63 keys.ECDSA384P1.generate().export_private(keyfile, passwd=passwd)
64
65
Fabio Utzig8101d1f2019-05-09 15:03:22 -030066def gen_ed25519(keyfile, passwd):
Fabio Utzig4bd4c7c2019-06-27 08:23:21 -030067 keys.Ed25519.generate().export_private(path=keyfile, passwd=passwd)
Fabio Utzig8101d1f2019-05-09 15:03:22 -030068
69
Fabio Utzig4facd1b2020-04-02 13:17:38 -030070def gen_x25519(keyfile, passwd):
71 keys.X25519.generate().export_private(path=keyfile, passwd=passwd)
72
73
Fabio Utzig4e2cdfe2022-09-28 17:44:01 -030074valid_langs = ['c', 'rust']
Bence Balogh97a20f12023-07-18 15:59:33 +020075valid_hash_encodings = ['lang-c', 'raw']
Bence Baloghed8d68a2023-07-18 15:57:52 +020076valid_encodings = ['lang-c', 'lang-rust', 'pem', 'raw']
Fabio Utzige89841d2018-12-21 11:19:06 -020077keygens = {
78 'rsa-2048': gen_rsa2048,
Fabio Utzig19fd79a2019-05-08 18:20:39 -030079 'rsa-3072': gen_rsa3072,
Fabio Utzige89841d2018-12-21 11:19:06 -020080 'ecdsa-p256': gen_ecdsa_p256,
Roland Mikhel57041742023-02-03 14:43:13 +010081 'ecdsa-p384': gen_ecdsa_p384,
Fabio Utzig4facd1b2020-04-02 13:17:38 -030082 'ed25519': gen_ed25519,
83 'x25519': gen_x25519,
Fabio Utzige89841d2018-12-21 11:19:06 -020084}
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +000085valid_formats = ['openssl', 'pkcs8']
Dominik Ermelc894d042024-08-22 14:58:15 +000086valid_sha = [ 'auto', '256', '384', '512' ]
Fabio Utzige89841d2018-12-21 11:19:06 -020087
Antonio de Angelis7ba01c02022-11-15 15:10:41 +000088
Andrzej Puzdrowski160303c2022-03-15 15:41:14 +010089def load_signature(sigfile):
90 with open(sigfile, 'rb') as f:
91 signature = base64.b64decode(f.read())
92 return signature
Fabio Utzige89841d2018-12-21 11:19:06 -020093
Antonio de Angelis7ba01c02022-11-15 15:10:41 +000094
Andrzej Puzdrowskif72e3742022-03-17 11:34:38 +010095def save_signature(sigfile, sig):
96 with open(sigfile, 'wb') as f:
97 signature = base64.b64encode(sig)
98 f.write(signature)
99
Antonio de Angelis7ba01c02022-11-15 15:10:41 +0000100
Fabio Utzige89841d2018-12-21 11:19:06 -0200101def load_key(keyfile):
102 # TODO: better handling of invalid pass-phrase
103 key = keys.load(keyfile)
104 if key is not None:
105 return key
106 passwd = getpass.getpass("Enter key passphrase: ").encode('utf-8')
107 return keys.load(keyfile, passwd)
108
109
110def get_password():
111 while True:
112 passwd = getpass.getpass("Enter key passphrase: ")
113 passwd2 = getpass.getpass("Reenter passphrase: ")
114 if passwd == passwd2:
115 break
116 print("Passwords do not match, try again")
117
118 # Password must be bytes, always use UTF-8 for consistent
119 # encoding.
120 return passwd.encode('utf-8')
121
122
123@click.option('-p', '--password', is_flag=True,
124 help='Prompt for password to protect key')
125@click.option('-t', '--type', metavar='type', required=True,
Fabio Utzig7ca28552019-12-13 11:24:20 -0300126 type=click.Choice(keygens.keys()), prompt=True,
127 help='{}'.format('One of: {}'.format(', '.join(keygens.keys()))))
Fabio Utzige89841d2018-12-21 11:19:06 -0200128@click.option('-k', '--key', metavar='filename', required=True)
129@click.command(help='Generate pub/private keypair')
130def keygen(type, key, password):
131 password = get_password() if password else None
132 keygens[type](key, password)
133
134
Fabio Utzig4e2cdfe2022-09-28 17:44:01 -0300135@click.option('-l', '--lang', metavar='lang',
136 type=click.Choice(valid_langs),
137 help='This option is deprecated. Please use the '
138 '`--encoding` option. '
139 'Valid langs: {}'.format(', '.join(valid_langs)))
140@click.option('-e', '--encoding', metavar='encoding',
141 type=click.Choice(valid_encodings),
142 help='Valid encodings: {}'.format(', '.join(valid_encodings)))
Fabio Utzige89841d2018-12-21 11:19:06 -0200143@click.option('-k', '--key', metavar='filename', required=True)
Bence Balogh367aefb2023-07-18 15:51:54 +0200144@click.option('-o', '--output', metavar='output', required=False,
145 help='Specify the output file\'s name. \
146 The stdout is used if it is not provided.')
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200147@click.command(help='Dump public key from keypair')
Bence Balogh367aefb2023-07-18 15:51:54 +0200148def getpub(key, encoding, lang, output):
Fabio Utzig4e2cdfe2022-09-28 17:44:01 -0300149 if encoding and lang:
Antonio de Angelis7ba01c02022-11-15 15:10:41 +0000150 raise click.UsageError('Please use only one of `--encoding/-e` '
151 'or `--lang/-l`')
Fabio Utzig4e2cdfe2022-09-28 17:44:01 -0300152 elif not encoding and not lang:
153 # Preserve old behavior defaulting to `c`. If `lang` is removed,
154 # `default=valid_encodings[0]` should be added to `-e` param.
155 lang = valid_langs[0]
Fabio Utzige89841d2018-12-21 11:19:06 -0200156 key = load_key(key)
Bence Balogh367aefb2023-07-18 15:51:54 +0200157
158 if not output:
159 output = sys.stdout
Fabio Utzige89841d2018-12-21 11:19:06 -0200160 if key is None:
161 print("Invalid passphrase")
Fabio Utzig4e2cdfe2022-09-28 17:44:01 -0300162 elif lang == 'c' or encoding == 'lang-c':
Bence Balogh367aefb2023-07-18 15:51:54 +0200163 key.emit_c_public(file=output)
Fabio Utzig4e2cdfe2022-09-28 17:44:01 -0300164 elif lang == 'rust' or encoding == 'lang-rust':
Bence Balogh367aefb2023-07-18 15:51:54 +0200165 key.emit_rust_public(file=output)
Fabio Utzig4e2cdfe2022-09-28 17:44:01 -0300166 elif encoding == 'pem':
Bence Balogh367aefb2023-07-18 15:51:54 +0200167 key.emit_public_pem(file=output)
Bence Baloghed8d68a2023-07-18 15:57:52 +0200168 elif encoding == 'raw':
169 key.emit_raw_public(file=output)
Fabio Utzige89841d2018-12-21 11:19:06 -0200170 else:
Fabio Utzig4e2cdfe2022-09-28 17:44:01 -0300171 raise click.UsageError()
Fabio Utzige89841d2018-12-21 11:19:06 -0200172
173
Bence Balogh97a20f12023-07-18 15:59:33 +0200174@click.option('-e', '--encoding', metavar='encoding',
175 type=click.Choice(valid_hash_encodings),
176 help='Valid encodings: {}. '
177 'Default value is {}.'
178 .format(', '.join(valid_hash_encodings),
179 valid_hash_encodings[0]))
180@click.option('-k', '--key', metavar='filename', required=True)
181@click.option('-o', '--output', metavar='output', required=False,
182 help='Specify the output file\'s name. \
183 The stdout is used if it is not provided.')
184@click.command(help='Dump the SHA256 hash of the public key')
185def getpubhash(key, output, encoding):
186 if not encoding:
187 encoding = valid_hash_encodings[0]
188 key = load_key(key)
189
190 if not output:
191 output = sys.stdout
192 if key is None:
193 print("Invalid passphrase")
194 elif encoding == 'lang-c':
195 key.emit_c_public_hash(file=output)
196 elif encoding == 'raw':
197 key.emit_raw_public_hash(file=output)
198 else:
199 raise click.UsageError()
200
201
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200202@click.option('--minimal', default=False, is_flag=True,
203 help='Reduce the size of the dumped private key to include only '
204 'the minimum amount of data required to decrypt. This '
205 'might require changes to the build config. Check the docs!'
206 )
207@click.option('-k', '--key', metavar='filename', required=True)
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +0000208@click.option('-f', '--format',
209 type=click.Choice(valid_formats),
Fabio Utzig8f289ba2023-01-09 21:01:55 -0300210 help='Valid formats: {}'.format(', '.join(valid_formats))
211 )
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200212@click.command(help='Dump private key from keypair')
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +0000213def getpriv(key, minimal, format):
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200214 key = load_key(key)
215 if key is None:
216 print("Invalid passphrase")
Fabio Utzig1f508922020-01-15 11:37:51 -0300217 try:
Antonio de Angelisc6e7e9b2022-11-15 15:06:40 +0000218 key.emit_private(minimal, format)
Fabio Utzig4facd1b2020-04-02 13:17:38 -0300219 except (RSAUsageError, ECDSAUsageError, Ed25519UsageError,
220 X25519UsageError) as e:
Fabio Utzig1f508922020-01-15 11:37:51 -0300221 raise click.UsageError(e)
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200222
223
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300224@click.argument('imgfile')
225@click.option('-k', '--key', metavar='filename')
226@click.command(help="Check that signed image can be verified by given key")
227def verify(key, imgfile):
228 key = load_key(key) if key else None
Dominik Ermel1c04eac2024-09-12 19:37:40 +0000229 ret, version, digest, signature = image.Image.verify(imgfile, key)
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300230 if ret == image.VerifyResult.OK:
231 print("Image was correctly validated")
Marek Pietae9555102019-08-08 16:08:16 +0200232 print("Image version: {}.{}.{}+{}".format(*version))
Dominik Ermel1c04eac2024-09-12 19:37:40 +0000233 if digest:
234 print("Image digest: {}".format(digest.hex()))
235 if signature and digest is None:
236 print("Image signature over image: {}".format(signature.hex()))
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300237 return
238 elif ret == image.VerifyResult.INVALID_MAGIC:
239 print("Invalid image magic; is this an MCUboot image?")
Christian Skubichf13db122019-07-31 11:34:15 +0200240 elif ret == image.VerifyResult.INVALID_TLV_INFO_MAGIC:
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300241 print("Invalid TLV info magic; is this an MCUboot image?")
242 elif ret == image.VerifyResult.INVALID_HASH:
Roland Mikhel57041742023-02-03 14:43:13 +0100243 print("Image has an invalid hash")
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300244 elif ret == image.VerifyResult.INVALID_SIGNATURE:
245 print("No signature found for the given key")
Rustam Ismayilov36f8bf32023-11-27 21:44:54 +0100246 elif ret == image.VerifyResult.KEY_MISMATCH:
247 print("Key type does not match TLV record")
Christian Skubichf13db122019-07-31 11:34:15 +0200248 else:
249 print("Unknown return code: {}".format(ret))
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300250 sys.exit(1)
251
252
David Vinczeca561352023-01-27 15:39:12 +0100253@click.argument('imgfile')
254@click.option('-o', '--outfile', metavar='filename', required=False,
255 help='Save image information to outfile in YAML format')
256@click.option('-s', '--silent', default=False, is_flag=True,
257 help='Do not print image information to output')
258@click.command(help='Print header, TLV area and trailer information '
259 'of a signed image')
260def dumpinfo(imgfile, outfile, silent):
261 dump_imginfo(imgfile, outfile, silent)
Guillaume G.6678c372025-03-17 13:28:22 +0100262 if not silent:
263 print("dumpinfo has run successfully")
David Vinczeca561352023-01-27 15:39:12 +0100264
265
Fabio Utzige89841d2018-12-21 11:19:06 -0200266def validate_version(ctx, param, value):
267 try:
268 decode_version(value)
269 return value
270 except ValueError as e:
271 raise click.BadParameter("{}".format(e))
272
273
David Vincze1a7a6902020-02-18 15:05:16 +0100274def validate_security_counter(ctx, param, value):
275 if value is not None:
276 if value.lower() == 'auto':
277 return 'auto'
278 else:
279 try:
280 return int(value, 0)
281 except ValueError:
282 raise click.BadParameter(
283 "{} is not a valid integer. Please use code literals "
284 "prefixed with 0b/0B, 0o/0O, or 0x/0X as necessary."
285 .format(value))
286
287
Fabio Utzige89841d2018-12-21 11:19:06 -0200288def validate_header_size(ctx, param, value):
289 min_hdr_size = image.IMAGE_HEADER_SIZE
290 if value < min_hdr_size:
291 raise click.BadParameter(
292 "Minimum value for -H/--header-size is {}".format(min_hdr_size))
293 return value
294
295
David Vinczeda8c9192019-03-26 17:17:41 +0100296def get_dependencies(ctx, param, value):
297 if value is not None:
298 versions = []
299 images = re.findall(r"\((\d+)", value)
300 if len(images) == 0:
301 raise click.BadParameter(
302 "Image dependency format is invalid: {}".format(value))
303 raw_versions = re.findall(r",\s*([0-9.+]+)\)", value)
304 if len(images) != len(raw_versions):
305 raise click.BadParameter(
306 '''There's a mismatch between the number of dependency images
307 and versions in: {}'''.format(value))
308 for raw_version in raw_versions:
309 try:
310 versions.append(decode_version(raw_version))
311 except ValueError as e:
312 raise click.BadParameter("{}".format(e))
313 dependencies = dict()
314 dependencies[image.DEP_IMAGES_KEY] = images
315 dependencies[image.DEP_VERSIONS_KEY] = versions
316 return dependencies
317
Mateusz Michalek63fa7e42024-08-09 12:16:40 +0200318def create_lzma2_header(dictsize, pb, lc, lp):
319 header = bytearray()
320 for i in range(0, 40):
321 if dictsize <= ((2 | ((i) & 1)) << int((i) / 2 + 11)):
322 header.append(i)
323 break
324 header.append( ( pb * 5 + lp) * 9 + lc)
325 return header
David Vinczeda8c9192019-03-26 17:17:41 +0100326
Fabio Utzige89841d2018-12-21 11:19:06 -0200327class BasedIntParamType(click.ParamType):
328 name = 'integer'
329
330 def convert(self, value, param, ctx):
331 try:
David Vincze1a7a6902020-02-18 15:05:16 +0100332 return int(value, 0)
Fabio Utzige89841d2018-12-21 11:19:06 -0200333 except ValueError:
David Vincze1a7a6902020-02-18 15:05:16 +0100334 self.fail('%s is not a valid integer. Please use code literals '
335 'prefixed with 0b/0B, 0o/0O, or 0x/0X as necessary.'
336 % value, param, ctx)
Fabio Utzige89841d2018-12-21 11:19:06 -0200337
338
339@click.argument('outfile')
340@click.argument('infile')
Mateusz Wielgosdc030552024-02-26 15:02:45 -0600341@click.option('--non-bootable', default=False, is_flag=True,
342 help='Mark the image as non-bootable.')
Ihor Slabkyy24d93732020-03-10 15:33:57 +0200343@click.option('--custom-tlv', required=False, nargs=2, default=[],
344 multiple=True, metavar='[tag] [value]',
345 help='Custom TLV that will be placed into protected area. '
346 'Add "0x" prefix if the value should be interpreted as an '
347 'integer, otherwise it will be interpreted as a string. '
348 'Specify the option multiple times to add multiple TLVs.')
Fabio Utzig9117fde2019-10-17 11:11:46 -0300349@click.option('-R', '--erased-val', type=click.Choice(['0', '0xff']),
350 required=False,
351 help='The value that is read back from erased flash.')
Fabio Utzigedbabcf2019-10-11 13:03:37 -0300352@click.option('-x', '--hex-addr', type=BasedIntParamType(), required=False,
353 help='Adjust address in hex output file.')
Håkon Øye Amundsendf8c8912019-08-26 12:15:28 +0000354@click.option('-L', '--load-addr', type=BasedIntParamType(), required=False,
David Vincze1e0c5442020-04-07 14:12:33 +0200355 help='Load address for image when it should run from RAM.')
Dominik Ermel50820b12020-12-14 13:16:46 +0000356@click.option('-F', '--rom-fixed', type=BasedIntParamType(), required=False,
357 help='Set flash address the image is built for.')
Fabio Utzig9a492d52020-01-15 11:31:52 -0300358@click.option('--save-enctlv', default=False, is_flag=True,
359 help='When upgrading, save encrypted key TLVs instead of plain '
360 'keys. Enable when BOOT_SWAP_SAVE_ENCTLV config option '
361 'was set.')
Fabio Utzige89841d2018-12-21 11:19:06 -0200362@click.option('-E', '--encrypt', metavar='filename',
David Vinczee574f2d2020-07-10 11:42:03 +0200363 help='Encrypt image using the provided public key. '
Tamas Banfe031092020-09-10 17:32:39 +0200364 '(Not supported in direct-xip or ram-load mode.)')
Salome Thirot0f641972021-05-14 11:19:55 +0100365@click.option('--encrypt-keylen', default='128',
Antonio de Angelis7ba01c02022-11-15 15:10:41 +0000366 type=click.Choice(['128', '256']),
Salome Thirot0f641972021-05-14 11:19:55 +0100367 help='When encrypting the image using AES, select a 128 bit or '
368 '256 bit key len.')
Mateusz Michalek63fa7e42024-08-09 12:16:40 +0200369@click.option('--compression', default='disabled',
Mateusz Michalekd69933c2024-10-04 13:36:52 +0200370 type=click.Choice(['disabled', 'lzma2', 'lzma2armthumb']),
Mateusz Michalek63fa7e42024-08-09 12:16:40 +0200371 help='Enable image compression using specified type. '
372 'Will fall back without image compression automatically '
373 'if the compression increases the image size.')
Michel Jaouend09aa6b2022-01-07 16:48:58 +0100374@click.option('-c', '--clear', required=False, is_flag=True, default=False,
375 help='Output a non-encrypted image with encryption capabilities,'
376 'so it can be installed in the primary slot, and encrypted '
377 'when swapped to the secondary.')
Fabio Utzige89841d2018-12-21 11:19:06 -0200378@click.option('-e', '--endian', type=click.Choice(['little', 'big']),
379 default='little', help="Select little or big endian")
380@click.option('--overwrite-only', default=False, is_flag=True,
381 help='Use overwrite-only instead of swap upgrades')
David Vincze71b8f982020-03-17 19:08:12 +0100382@click.option('--boot-record', metavar='sw_type', help='Create CBOR encoded '
383 'boot record TLV. The sw_type represents the role of the '
384 'software component (e.g. CoFM for coprocessor firmware). '
385 '[max. 12 characters]')
Fabio Utzige89841d2018-12-21 11:19:06 -0200386@click.option('-M', '--max-sectors', type=int,
Fabio Utzig9a492d52020-01-15 11:31:52 -0300387 help='When padding allow for this amount of sectors (defaults '
388 'to 128)')
Henrik Brix Andersen0ce958e2020-03-11 14:04:11 +0100389@click.option('--confirm', default=False, is_flag=True,
Martí Bolívar009a1502020-09-04 14:23:39 -0700390 help='When padding the image, mark it as confirmed (implies '
391 '--pad)')
Fabio Utzige89841d2018-12-21 11:19:06 -0200392@click.option('--pad', default=False, is_flag=True,
393 help='Pad image to --slot-size bytes, adding trailer magic')
394@click.option('-S', '--slot-size', type=BasedIntParamType(), required=True,
Fabio Utzig826abf42020-07-13 20:56:35 -0300395 help='Size of the slot. If the slots have different sizes, use '
396 'the size of the secondary slot.')
Fabio Utzige89841d2018-12-21 11:19:06 -0200397@click.option('--pad-header', default=False, is_flag=True,
Fabio Utzig9a492d52020-01-15 11:31:52 -0300398 help='Add --header-size zeroed bytes at the beginning of the '
399 'image')
Fabio Utzige89841d2018-12-21 11:19:06 -0200400@click.option('-H', '--header-size', callback=validate_header_size,
401 type=BasedIntParamType(), required=True)
David Brown4878c272020-03-10 16:23:56 -0600402@click.option('--pad-sig', default=False, is_flag=True,
403 help='Add 0-2 bytes of padding to ECDSA signature '
404 '(for mcuboot <1.5)')
David Vinczeda8c9192019-03-26 17:17:41 +0100405@click.option('-d', '--dependencies', callback=get_dependencies,
406 required=False, help='''Add dependence on another image, format:
407 "(<image_ID>,<image_version>), ... "''')
David Vincze1a7a6902020-02-18 15:05:16 +0100408@click.option('-s', '--security-counter', callback=validate_security_counter,
409 help='Specify the value of security counter. Use the `auto` '
410 'keyword to automatically generate it from the image version.')
Fabio Utzige89841d2018-12-21 11:19:06 -0200411@click.option('-v', '--version', callback=validate_version, required=True)
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000412@click.option('--align', type=click.Choice(['1', '2', '4', '8', '16', '32']),
Andrej Butok06bc5482023-12-19 10:36:08 +0100413 default='1',
Andrej Butok6c4f7b42023-11-06 15:01:19 +0100414 required=False,
415 help='Alignment used by swap update modes.')
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000416@click.option('--max-align', type=click.Choice(['8', '16', '32']),
Piotr Mienkowskib6d5cf32022-01-31 01:01:11 +0100417 required=False,
418 help='Maximum flash alignment. Set if flash alignment of the '
419 'primary and secondary slot differ and any of them is larger '
420 'than 8.')
David Vinczedde178d2020-03-26 20:06:01 +0100421@click.option('--public-key-format', type=click.Choice(['hash', 'full']),
422 default='hash', help='In what format to add the public key to '
423 'the image manifest: full key or hash of the key.')
Fabio Utzige89841d2018-12-21 11:19:06 -0200424@click.option('-k', '--key', metavar='filename')
Andrzej Puzdrowski160303c2022-03-15 15:41:14 +0100425@click.option('--fix-sig', metavar='filename',
iysheng6093cbb2022-05-28 17:00:40 +0800426 help='fixed signature for the image. It will be used instead of '
Andrzej Puzdrowski160303c2022-03-15 15:41:14 +0100427 'the signature calculated using the public key')
428@click.option('--fix-sig-pubkey', metavar='filename',
429 help='public key relevant to fixed signature')
Dominik Ermel1c04eac2024-09-12 19:37:40 +0000430@click.option('--pure', 'is_pure', is_flag=True, default=False, show_default=True,
431 help='Expected Pure variant of signature; the Pure variant is '
432 'expected to be signature done over an image rather than hash of '
433 'that image.')
Andrzej Puzdrowskif72e3742022-03-17 11:34:38 +0100434@click.option('--sig-out', metavar='filename',
iysheng6093cbb2022-05-28 17:00:40 +0800435 help='Path to the file to which signature will be written. '
Andrzej Puzdrowskif72e3742022-03-17 11:34:38 +0100436 'The image signature will be encoded as base64 formatted string')
Dominik Ermelc894d042024-08-22 14:58:15 +0000437@click.option('--sha', 'user_sha', type=click.Choice(valid_sha), default='auto',
438 help='selected sha algorithm to use; defaults to "auto" which is 256 if '
439 'no cryptographic signature is used, or default for signature type')
Andrzej Puzdrowskidfce0be2022-03-28 09:34:15 +0200440@click.option('--vector-to-sign', type=click.Choice(['payload', 'digest']),
Antonio de Angelis7ba01c02022-11-15 15:10:41 +0000441 help='send to OUTFILE the payload or payload''s digest instead '
442 'of complied image. These data can be used for external image '
Andrzej Puzdrowskidfce0be2022-03-28 09:34:15 +0200443 'signing')
Fabio Utzig7c00acd2019-01-07 09:54:20 -0200444@click.command(help='''Create a signed or unsigned image\n
445 INFILE and OUTFILE are parsed as Intel HEX if the params have
Håkon Øye Amundsendf8c8912019-08-26 12:15:28 +0000446 .hex extension, otherwise binary format is used''')
David Vinczedde178d2020-03-26 20:06:01 +0100447def sign(key, public_key_format, align, version, pad_sig, header_size,
448 pad_header, slot_size, pad, confirm, max_sectors, overwrite_only,
Mateusz Michalek63fa7e42024-08-09 12:16:40 +0200449 endian, encrypt_keylen, encrypt, compression, infile, outfile,
450 dependencies, load_addr, hex_addr, erased_val, save_enctlv,
451 security_counter, boot_record, custom_tlv, rom_fixed, max_align,
Dominik Ermel1c04eac2024-09-12 19:37:40 +0000452 clear, fix_sig, fix_sig_pubkey, sig_out, user_sha, is_pure,
453 vector_to_sign, non_bootable):
Martí Bolívar009a1502020-09-04 14:23:39 -0700454
455 if confirm:
456 # Confirmed but non-padded images don't make much sense, because
457 # otherwise there's no trailer area for writing the confirmed status.
458 pad = True
Fabio Utzig7c00acd2019-01-07 09:54:20 -0200459 img = image.Image(version=decode_version(version), header_size=header_size,
Henrik Brix Andersen0ce958e2020-03-11 14:04:11 +0100460 pad_header=pad_header, pad=pad, confirm=confirm,
461 align=int(align), slot_size=slot_size,
462 max_sectors=max_sectors, overwrite_only=overwrite_only,
Dominik Ermel50820b12020-12-14 13:16:46 +0000463 endian=endian, load_addr=load_addr, rom_fixed=rom_fixed,
464 erased_val=erased_val, save_enctlv=save_enctlv,
Mateusz Wielgosdc030552024-02-26 15:02:45 -0600465 security_counter=security_counter, max_align=max_align,
466 non_bootable=non_bootable)
Mateusz Michalek63fa7e42024-08-09 12:16:40 +0200467 compression_tlvs = {}
Fabio Utzig7c00acd2019-01-07 09:54:20 -0200468 img.load(infile)
Fabio Utzige89841d2018-12-21 11:19:06 -0200469 key = load_key(key) if key else None
470 enckey = load_key(encrypt) if encrypt else None
Fabio Utzig7a3b2602019-10-22 09:56:44 -0300471 if enckey and key:
472 if ((isinstance(key, keys.ECDSA256P1) and
473 not isinstance(enckey, keys.ECDSA256P1Public))
Roland Mikhel57041742023-02-03 14:43:13 +0100474 or (isinstance(key, keys.ECDSA384P1) and
475 not isinstance(enckey, keys.ECDSA384P1Public))
Fabio Utzig7a3b2602019-10-22 09:56:44 -0300476 or (isinstance(key, keys.RSA) and
477 not isinstance(enckey, keys.RSAPublic))):
478 # FIXME
Fabio Utzig1f508922020-01-15 11:37:51 -0300479 raise click.UsageError("Signing and encryption must use the same "
480 "type of key")
David Brown4878c272020-03-10 16:23:56 -0600481
482 if pad_sig and hasattr(key, 'pad_sig'):
483 key.pad_sig = True
484
Ihor Slabkyy24d93732020-03-10 15:33:57 +0200485 # Get list of custom protected TLVs from the command-line
486 custom_tlvs = {}
487 for tlv in custom_tlv:
488 tag = int(tlv[0], 0)
489 if tag in custom_tlvs:
490 raise click.UsageError('Custom TLV %s already exists.' % hex(tag))
491 if tag in image.TLV_VALUES.values():
492 raise click.UsageError(
493 'Custom TLV %s conflicts with predefined TLV.' % hex(tag))
494
495 value = tlv[1]
496 if value.startswith('0x'):
497 if len(value[2:]) % 2:
498 raise click.UsageError('Custom TLV length is odd.')
499 custom_tlvs[tag] = bytes.fromhex(value[2:])
500 else:
501 custom_tlvs[tag] = value.encode('utf-8')
502
Andrzej Puzdrowski160303c2022-03-15 15:41:14 +0100503 # Allow signature calculated externally.
504 raw_signature = load_signature(fix_sig) if fix_sig else None
505
506 baked_signature = None
507 pub_key = None
508
509 if raw_signature is not None:
510 if fix_sig_pubkey is None:
Antonio de Angelis7ba01c02022-11-15 15:10:41 +0000511 raise click.UsageError(
Andrzej Puzdrowski160303c2022-03-15 15:41:14 +0100512 'public key of the fixed signature is not specified')
513
514 pub_key = load_key(fix_sig_pubkey)
515
516 baked_signature = {
Antonio de Angelis7ba01c02022-11-15 15:10:41 +0000517 'value': raw_signature
Andrzej Puzdrowski160303c2022-03-15 15:41:14 +0100518 }
519
Dominik Ermel1c04eac2024-09-12 19:37:40 +0000520 if is_pure and user_sha != 'auto':
521 raise click.UsageError(
522 'Pure signatures, currently, enforces preferred hash algorithm, '
523 'and forbids sha selection by user.')
524
Mateusz Michalek1da18e92025-01-02 14:48:28 +0100525 if compression in ["lzma2", "lzma2armthumb"]:
526 img.create(key, public_key_format, enckey, dependencies, boot_record,
Mateusz Michalek33de65c2024-10-15 16:01:18 +0200527 custom_tlvs, compression_tlvs, None, int(encrypt_keylen), clear,
Dominik Ermel1c04eac2024-09-12 19:37:40 +0000528 baked_signature, pub_key, vector_to_sign, user_sha=user_sha,
Mateusz Michalek1da18e92025-01-02 14:48:28 +0100529 is_pure=is_pure, keep_comp_size=False, dont_encrypt=True)
Mateusz Michalek63fa7e42024-08-09 12:16:40 +0200530 compressed_img = image.Image(version=decode_version(version),
531 header_size=header_size, pad_header=pad_header,
532 pad=pad, confirm=confirm, align=int(align),
533 slot_size=slot_size, max_sectors=max_sectors,
534 overwrite_only=overwrite_only, endian=endian,
535 load_addr=load_addr, rom_fixed=rom_fixed,
536 erased_val=erased_val, save_enctlv=save_enctlv,
537 security_counter=security_counter, max_align=max_align)
538 compression_filters = [
539 {"id": lzma.FILTER_LZMA2, "preset": comp_default_preset,
540 "dict_size": comp_default_dictsize, "lp": comp_default_lp,
541 "lc": comp_default_lc}
542 ]
Mateusz Michalekd69933c2024-10-04 13:36:52 +0200543 if compression == "lzma2armthumb":
544 compression_filters.insert(0, {"id":lzma.FILTER_ARMTHUMB})
Mateusz Michalek63fa7e42024-08-09 12:16:40 +0200545 compressed_data = lzma.compress(img.get_infile_data(),filters=compression_filters,
546 format=lzma.FORMAT_RAW)
547 uncompressed_size = len(img.get_infile_data())
548 compressed_size = len(compressed_data)
549 print(f"compressed image size: {compressed_size} bytes")
550 print(f"original image size: {uncompressed_size} bytes")
551 compression_tlvs["DECOMP_SIZE"] = struct.pack(
552 img.get_struct_endian() + 'L', img.image_size)
553 compression_tlvs["DECOMP_SHA"] = img.image_hash
554 compression_tlvs_size = len(compression_tlvs["DECOMP_SIZE"])
555 compression_tlvs_size += len(compression_tlvs["DECOMP_SHA"])
556 if img.get_signature():
557 compression_tlvs["DECOMP_SIGNATURE"] = img.get_signature()
558 compression_tlvs_size += len(compression_tlvs["DECOMP_SIGNATURE"])
559 if (compressed_size + compression_tlvs_size) < uncompressed_size:
560 compression_header = create_lzma2_header(
561 dictsize = comp_default_dictsize, pb = comp_default_pb,
562 lc = comp_default_lc, lp = comp_default_lp)
563 compressed_img.load_compressed(compressed_data, compression_header)
564 compressed_img.base_addr = img.base_addr
Mateusz Michalek1da18e92025-01-02 14:48:28 +0100565 keep_comp_size = False;
566 if enckey:
567 keep_comp_size = True
Mateusz Michalek63fa7e42024-08-09 12:16:40 +0200568 compressed_img.create(key, public_key_format, enckey,
569 dependencies, boot_record, custom_tlvs, compression_tlvs,
570 compression, int(encrypt_keylen), clear, baked_signature,
Dominik Ermel1c04eac2024-09-12 19:37:40 +0000571 pub_key, vector_to_sign, user_sha=user_sha,
Mateusz Michalek1da18e92025-01-02 14:48:28 +0100572 is_pure=is_pure, keep_comp_size=keep_comp_size)
Mateusz Michalek63fa7e42024-08-09 12:16:40 +0200573 img = compressed_img
Mateusz Michalek1da18e92025-01-02 14:48:28 +0100574 else:
575 img.create(key, public_key_format, enckey, dependencies, boot_record,
576 custom_tlvs, compression_tlvs, None, int(encrypt_keylen), clear,
577 baked_signature, pub_key, vector_to_sign, user_sha=user_sha,
578 is_pure=is_pure)
Mateusz Michalek63fa7e42024-08-09 12:16:40 +0200579 img.save(outfile, hex_addr)
Andrzej Puzdrowskif72e3742022-03-17 11:34:38 +0100580 if sig_out is not None:
581 new_signature = img.get_signature()
582 save_signature(sig_out, new_signature)
583
Fabio Utzige89841d2018-12-21 11:19:06 -0200584
585class AliasesGroup(click.Group):
586
587 _aliases = {
588 "create": "sign",
589 }
590
591 def list_commands(self, ctx):
592 cmds = [k for k in self.commands]
593 aliases = [k for k in self._aliases]
594 return sorted(cmds + aliases)
595
596 def get_command(self, ctx, cmd_name):
597 rv = click.Group.get_command(self, ctx, cmd_name)
598 if rv is not None:
599 return rv
600 if cmd_name in self._aliases:
601 return click.Group.get_command(self, ctx, self._aliases[cmd_name])
602 return None
603
604
Fabio Utzig25c6a152019-09-10 12:52:26 -0300605@click.command(help='Print imgtool version information')
606def version():
607 print(imgtool_version)
608
609
Fabio Utzige89841d2018-12-21 11:19:06 -0200610@click.command(cls=AliasesGroup,
611 context_settings=dict(help_option_names=['-h', '--help']))
612def imgtool():
613 pass
614
615
616imgtool.add_command(keygen)
617imgtool.add_command(getpub)
Bence Balogh97a20f12023-07-18 15:59:33 +0200618imgtool.add_command(getpubhash)
Ioannis Konstantelias78e57c72019-11-28 16:06:12 +0200619imgtool.add_command(getpriv)
Fabio Utzig4a5477a2019-05-27 15:45:08 -0300620imgtool.add_command(verify)
Fabio Utzige89841d2018-12-21 11:19:06 -0200621imgtool.add_command(sign)
Fabio Utzig25c6a152019-09-10 12:52:26 -0300622imgtool.add_command(version)
David Vinczeca561352023-01-27 15:39:12 +0100623imgtool.add_command(dumpinfo)
Fabio Utzige89841d2018-12-21 11:19:06 -0200624
625
626if __name__ == '__main__':
627 imgtool()