blob: 127705cb6f97c2559f80bd82b9a7e7ba536dddb6 [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
Carles Cufif2429012018-01-30 16:45:50 +01009Python libraries. These can be installed using 'pip3':
David Browne369fec2017-06-07 09:35:48 -060010
Carles Cufif2429012018-01-30 16:45:50 +010011 pip3 install --user -r scripts/requirements.txt
David Browne369fec2017-06-07 09:35:48 -060012
13## Managing keys
14
15This tool currently supports rsa-2048 and ecdsa-p256 keys. You can
16generate a keypair for one of these types using the 'keygen' command:
17
18 ./scripts/imgtool.py keygen -k filename.pem -t rsa-2048
19
20or use ecdsa-p256 for the type. The key type used should match what
21mcuboot is configured to verify.
22
23This key file is what is used to sign images, this file should be
24protected, and not widely distributed.
25
David Brown31d29c82017-11-21 15:38:56 -070026You can add the `-p` argument to `keygen`, which will cause it to
27prompt for a password. You will need to enter this password in every
28time you use the private key.
29
David Browne369fec2017-06-07 09:35:48 -060030## Incorporating the public key into the code
31
32There is a development key distributed with mcuboot that can be used
33for testing. Since this private key is widely distributed, it should
34never be used for production. Once you have generated a production
35key, as described above, you should replace the public key in the
36bootloader with the generated one.
37
38For Zephyr, the keys live in the file `boot/zephyr/keys.c`. For
39mynewt, follow the instructions in `doc/signed_images.md` to generate
40the key file.
41
42 ./scripts/imgtool.py getpub -k filename.pem
43
44will extract the public key from the given private key file, and
45output it as a C data structure. You can replace or insert this code
46into the key file.
47
48## Signing images
49
50Image signing takes a binary image intended for Slot 0 and adds a
51header and trailer that the bootloader is expecting:
52
53 usage: imgtool.py sign [-h] -k filename --align ALIGN -v VERSION -H
54 HEADER_SIZE [--pad PAD] [--rsa-pkcs1-15]
55 infile outfile
56
57 positional arguments:
58 infile
59 outfile
60
61 optional arguments:
62 -h, --help show this help message and exit
63 -k filename, --key filename
64 --align ALIGN
65 -v VERSION, --version VERSION
66 -H HEADER_SIZE, --header-size HEADER_SIZE
David Brown2c21f712017-06-08 10:03:42 -060067 --included-header Image has gap for header
David Browne369fec2017-06-07 09:35:48 -060068 --pad PAD Pad image to this many bytes, adding trailer magic
69 --rsa-pkcs1-15 Use old PKCS#1 v1.5 signature algorithm
70
71The main arguments given are the key file generated above, a version
72field to place in the header (1.2.3 for example), the alignment of the
73flash device in question, and the header size.
74
75The header size depends on the operating system and the particular
76flash device. For Zephyr, it will be configured as part of the build,
David Brown2c21f712017-06-08 10:03:42 -060077and will be a small power of two. By default, the header will be
78prepended to the image. If `--included-header` is given, the image
79must start with header-size bytes of zeros, and the header will be
80overwritten over these bytes.
David Browne369fec2017-06-07 09:35:48 -060081
82The optional --pad argument will place a trailer on the image that
83indicates that the image should be considered an upgrade. Writing
84this image in slot 1 will then cause the bootloader to upgrade to it.
85
86Lastly, the --rsa-pkcs1-15 will cause the tool to use the older,
87deprecated pkcs#1 v1.5 signing algorithm when using RSA. This can be
88enabled in the bootloader as wel, and may be needed if you are using
89an older version of the bootloader.