blob: 98ba831138000bcaf25a6cd3da1612081112c9c2 [file] [log] [blame] [view]
David Brownd2fcc212017-09-11 14:47:48 -06001# Image tool
David Browne369fec2017-06-07 09:35:48 -06002
3The Python program `scripts/imgtool.py` can be used to perform the
4operations that are necessary to manage keys and sign images. Using
5this script should be preferred to the manual steps described in
6`doc/signed_images.md`.
7
8This program is written for Python3, and has several dependencies on
9Python libraries. These can be installed using 'pip3' manually:
10
David Brown31d29c82017-11-21 15:38:56 -070011 pip3 install --user cryptography
David Browne369fec2017-06-07 09:35:48 -060012
13or, on Ubuntu, using the package manager:
14
David Brown31d29c82017-11-21 15:38:56 -070015 sudo apt-get install python3-cryptography
David Browne369fec2017-06-07 09:35:48 -060016
17## Managing keys
18
19This tool currently supports rsa-2048 and ecdsa-p256 keys. You can
20generate a keypair for one of these types using the 'keygen' command:
21
22 ./scripts/imgtool.py keygen -k filename.pem -t rsa-2048
23
24or use ecdsa-p256 for the type. The key type used should match what
25mcuboot is configured to verify.
26
27This key file is what is used to sign images, this file should be
28protected, and not widely distributed.
29
David Brown31d29c82017-11-21 15:38:56 -070030You can add the `-p` argument to `keygen`, which will cause it to
31prompt for a password. You will need to enter this password in every
32time you use the private key.
33
David Browne369fec2017-06-07 09:35:48 -060034## Incorporating the public key into the code
35
36There is a development key distributed with mcuboot that can be used
37for testing. Since this private key is widely distributed, it should
38never be used for production. Once you have generated a production
39key, as described above, you should replace the public key in the
40bootloader with the generated one.
41
42For Zephyr, the keys live in the file `boot/zephyr/keys.c`. For
43mynewt, follow the instructions in `doc/signed_images.md` to generate
44the key file.
45
46 ./scripts/imgtool.py getpub -k filename.pem
47
48will extract the public key from the given private key file, and
49output it as a C data structure. You can replace or insert this code
50into the key file.
51
52## Signing images
53
54Image signing takes a binary image intended for Slot 0 and adds a
55header and trailer that the bootloader is expecting:
56
57 usage: imgtool.py sign [-h] -k filename --align ALIGN -v VERSION -H
58 HEADER_SIZE [--pad PAD] [--rsa-pkcs1-15]
59 infile outfile
60
61 positional arguments:
62 infile
63 outfile
64
65 optional arguments:
66 -h, --help show this help message and exit
67 -k filename, --key filename
68 --align ALIGN
69 -v VERSION, --version VERSION
70 -H HEADER_SIZE, --header-size HEADER_SIZE
David Brown2c21f712017-06-08 10:03:42 -060071 --included-header Image has gap for header
David Browne369fec2017-06-07 09:35:48 -060072 --pad PAD Pad image to this many bytes, adding trailer magic
73 --rsa-pkcs1-15 Use old PKCS#1 v1.5 signature algorithm
74
75The main arguments given are the key file generated above, a version
76field to place in the header (1.2.3 for example), the alignment of the
77flash device in question, and the header size.
78
79The header size depends on the operating system and the particular
80flash device. For Zephyr, it will be configured as part of the build,
David Brown2c21f712017-06-08 10:03:42 -060081and will be a small power of two. By default, the header will be
82prepended to the image. If `--included-header` is given, the image
83must start with header-size bytes of zeros, and the header will be
84overwritten over these bytes.
David Browne369fec2017-06-07 09:35:48 -060085
86The optional --pad argument will place a trailer on the image that
87indicates that the image should be considered an upgrade. Writing
88this image in slot 1 will then cause the bootloader to upgrade to it.
89
90Lastly, the --rsa-pkcs1-15 will cause the tool to use the older,
91deprecated pkcs#1 v1.5 signing algorithm when using RSA. This can be
92enabled in the bootloader as wel, and may be needed if you are using
93an older version of the bootloader.