David Brown | d2fcc21 | 2017-09-11 14:47:48 -0600 | [diff] [blame] | 1 | # Image tool |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 2 | |
| 3 | The Python program `scripts/imgtool.py` can be used to perform the |
| 4 | operations that are necessary to manage keys and sign images. Using |
| 5 | this script should be preferred to the manual steps described in |
| 6 | `doc/signed_images.md`. |
| 7 | |
| 8 | This program is written for Python3, and has several dependencies on |
Carles Cufi | f242901 | 2018-01-30 16:45:50 +0100 | [diff] [blame^] | 9 | Python libraries. These can be installed using 'pip3': |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 10 | |
Carles Cufi | f242901 | 2018-01-30 16:45:50 +0100 | [diff] [blame^] | 11 | pip3 install --user -r scripts/requirements.txt |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 12 | |
| 13 | ## Managing keys |
| 14 | |
| 15 | This tool currently supports rsa-2048 and ecdsa-p256 keys. You can |
| 16 | generate a keypair for one of these types using the 'keygen' command: |
| 17 | |
| 18 | ./scripts/imgtool.py keygen -k filename.pem -t rsa-2048 |
| 19 | |
| 20 | or use ecdsa-p256 for the type. The key type used should match what |
| 21 | mcuboot is configured to verify. |
| 22 | |
| 23 | This key file is what is used to sign images, this file should be |
| 24 | protected, and not widely distributed. |
| 25 | |
David Brown | 31d29c8 | 2017-11-21 15:38:56 -0700 | [diff] [blame] | 26 | You can add the `-p` argument to `keygen`, which will cause it to |
| 27 | prompt for a password. You will need to enter this password in every |
| 28 | time you use the private key. |
| 29 | |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 30 | ## Incorporating the public key into the code |
| 31 | |
| 32 | There is a development key distributed with mcuboot that can be used |
| 33 | for testing. Since this private key is widely distributed, it should |
| 34 | never be used for production. Once you have generated a production |
| 35 | key, as described above, you should replace the public key in the |
| 36 | bootloader with the generated one. |
| 37 | |
| 38 | For Zephyr, the keys live in the file `boot/zephyr/keys.c`. For |
| 39 | mynewt, follow the instructions in `doc/signed_images.md` to generate |
| 40 | the key file. |
| 41 | |
| 42 | ./scripts/imgtool.py getpub -k filename.pem |
| 43 | |
| 44 | will extract the public key from the given private key file, and |
| 45 | output it as a C data structure. You can replace or insert this code |
| 46 | into the key file. |
| 47 | |
| 48 | ## Signing images |
| 49 | |
| 50 | Image signing takes a binary image intended for Slot 0 and adds a |
| 51 | header 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 Brown | 2c21f71 | 2017-06-08 10:03:42 -0600 | [diff] [blame] | 67 | --included-header Image has gap for header |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 68 | --pad PAD Pad image to this many bytes, adding trailer magic |
| 69 | --rsa-pkcs1-15 Use old PKCS#1 v1.5 signature algorithm |
| 70 | |
| 71 | The main arguments given are the key file generated above, a version |
| 72 | field to place in the header (1.2.3 for example), the alignment of the |
| 73 | flash device in question, and the header size. |
| 74 | |
| 75 | The header size depends on the operating system and the particular |
| 76 | flash device. For Zephyr, it will be configured as part of the build, |
David Brown | 2c21f71 | 2017-06-08 10:03:42 -0600 | [diff] [blame] | 77 | and will be a small power of two. By default, the header will be |
| 78 | prepended to the image. If `--included-header` is given, the image |
| 79 | must start with header-size bytes of zeros, and the header will be |
| 80 | overwritten over these bytes. |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 81 | |
| 82 | The optional --pad argument will place a trailer on the image that |
| 83 | indicates that the image should be considered an upgrade. Writing |
| 84 | this image in slot 1 will then cause the bootloader to upgrade to it. |
| 85 | |
| 86 | Lastly, the --rsa-pkcs1-15 will cause the tool to use the older, |
| 87 | deprecated pkcs#1 v1.5 signing algorithm when using RSA. This can be |
| 88 | enabled in the bootloader as wel, and may be needed if you are using |
| 89 | an older version of the bootloader. |