Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1 | <!-- |
David Vincze | 4e3c47b | 2020-04-21 17:11:33 +0200 | [diff] [blame] | 2 | - |
| 3 | - Licensed to the Apache Software Foundation (ASF) under one |
| 4 | - or more contributor license agreements. See the NOTICE file |
| 5 | - distributed with this work for additional information |
| 6 | - regarding copyright ownership. The ASF licenses this file |
| 7 | - to you under the Apache License, Version 2.0 (the |
| 8 | - "License"); you may not use this file except in compliance |
| 9 | - with the License. You may obtain a copy of the License at |
| 10 | - |
| 11 | - http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | - |
| 13 | - Unless required by applicable law or agreed to in writing, |
| 14 | - software distributed under the License is distributed on an |
| 15 | - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | - KIND, either express or implied. See the License for the |
| 17 | - specific language governing permissions and limitations |
| 18 | - under the License. |
| 19 | - |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 20 | --> |
| 21 | |
| 22 | ## Image signing |
| 23 | |
| 24 | This signs the image by computing hash over the image, and then |
| 25 | signing that hash. Signature is computed by newt tool when it's |
| 26 | creating the image. This signature is placed in the image trailer. |
| 27 | |
| 28 | The public key of this keypair must be included in the bootloader, |
| 29 | as it verifies it before allowing the image to run. |
| 30 | |
| 31 | This facility allows you to use multiple signing keys. This would |
| 32 | be useful when you want to prevent production units from booting |
| 33 | development images, but want development units to be able to boot |
| 34 | both production images and development images. |
| 35 | |
David Vincze | 25459bf | 2020-04-21 17:11:20 +0200 | [diff] [blame] | 36 | For an alternative solution when the public key(s) doesn't need to be |
| 37 | included in the bootloader, see the [design](design.md) document. |
| 38 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 39 | ## Creating signing keys |
| 40 | First you need a keypair to use for signing. You can create |
| 41 | one with openssl command line tool. |
| 42 | |
| 43 | openssl genrsa -out image_sign.pem 2048 |
| 44 | |
| 45 | This created a file which contains both the private and public key, |
| 46 | and will be used when signing images. |
| 47 | |
| 48 | Then you need to extract the public key from this to include it |
| 49 | in the bootloader. Bootloader need to keep key parsing minimal, |
| 50 | so it expects simple key format. |
| 51 | |
| 52 | openssl rsa -in image_sign.pem -pubout -out image_sign_pub.der -outform DER -RSAPublicKey_out |
| 53 | |
| 54 | Now the public key is in file called image_sign_pub.der. |
| 55 | |
Marko Kiiskila | 8eeba12 | 2016-12-29 17:38:54 -0800 | [diff] [blame] | 56 | For ECDSA224 these commands are similar. |
Marko Kiiskila | 919eaf4 | 2016-12-28 17:39:45 -0800 | [diff] [blame] | 57 | |
| 58 | openssl ecparam -name secp224r1 -genkey -noout -out image_sign.pem |
| 59 | openssl ec -in image_sign.pem -pubout -outform DER -out image_sign_pub.der |
| 60 | |
Marko Kiiskila | 8eeba12 | 2016-12-29 17:38:54 -0800 | [diff] [blame] | 61 | And then the ECDSA256. |
| 62 | openssl ecparam -name prime256v1 -genkey -noout -out image_sign.pem |
| 63 | openssl ec -in image_sign.pem -pubout -outform DER -out image_sign_pub.der |
| 64 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 65 | ## Creating a key package |
| 66 | |
| 67 | xxd -i image_sign_pub.der image_sign_pub.c.import |
| 68 | |
| 69 | Then you need to create a package containing this key, or keys. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 70 | |
| 71 | ## Sample pkg.yml |
| 72 | This gets bootutil to turn on image signature validation. |
| 73 | |
| 74 | pkg.name: libs/mykeys |
| 75 | pkg.deps: |
Marko Kiiskila | bf986da | 2016-12-13 17:15:24 -0800 | [diff] [blame] | 76 | - "@apache-mynewt-core/boot/bootutil" |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 77 | |
| 78 | ## Sample source file |
| 79 | This exports the keys. |
| 80 | |
| 81 | #include <bootutil/sign_key.h> |
| 82 | |
| 83 | #include "image_sign_pub.c.import" |
| 84 | |
| 85 | const struct bootutil_key bootutil_keys[] = { |
| 86 | [0] = { |
| 87 | .key = image_sign_pub_der, |
| 88 | .len = &image_sign_pub_der_len, |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | const int bootutil_key_cnt = sizeof(bootutil_keys) / sizeof(bootutil_keys[0]); |
| 93 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 94 | ## Building the bootloader |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 95 | |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame] | 96 | Enable the BOOTUTIL_SIGN_RSA syscfg setting in your app or target syscfg.yml |
| 97 | file |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 98 | |
| 99 | syscfg.vals: |
Fabio Utzig | 32d68f0 | 2017-07-25 22:05:38 -0300 | [diff] [blame] | 100 | BOOTUTIL_SIGN_RSA: 1 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 101 | |
| 102 | After you've created the key package, you must include it in the build |
| 103 | for bootloader. So modify the pkg.yml for apps/boot to include it. |
Marko Kiiskila | 919eaf4 | 2016-12-28 17:39:45 -0800 | [diff] [blame] | 104 | |
Fabio Utzig | 32d68f0 | 2017-07-25 22:05:38 -0300 | [diff] [blame] | 105 | The syscfg variable to enable ECDSA224 is BOOTUTIL_SIGN_EC, and |
| 106 | BOOTUTIL_SIGN_EC256 for ECDS256. |