blob: bcc201b85514d4c42e50ae8ea2efafb58ecb64d0 [file] [log] [blame] [view]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001<!--
David Vincze4e3c47b2020-04-21 17:11:33 +02002 -
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 Collins92ea77f2016-12-12 15:59:26 -080020-->
21
22## Image signing
23
24This signs the image by computing hash over the image, and then
25signing that hash. Signature is computed by newt tool when it's
26creating the image. This signature is placed in the image trailer.
27
28The public key of this keypair must be included in the bootloader,
29as it verifies it before allowing the image to run.
30
31This facility allows you to use multiple signing keys. This would
32be useful when you want to prevent production units from booting
33development images, but want development units to be able to boot
34both production images and development images.
35
David Vincze25459bf2020-04-21 17:11:20 +020036For an alternative solution when the public key(s) doesn't need to be
37included in the bootloader, see the [design](design.md) document.
38
Christopher Collins92ea77f2016-12-12 15:59:26 -080039## Creating signing keys
40First you need a keypair to use for signing. You can create
41one with openssl command line tool.
42
43openssl genrsa -out image_sign.pem 2048
44
45This created a file which contains both the private and public key,
46and will be used when signing images.
47
48Then you need to extract the public key from this to include it
49in the bootloader. Bootloader need to keep key parsing minimal,
50so it expects simple key format.
51
52openssl rsa -in image_sign.pem -pubout -out image_sign_pub.der -outform DER -RSAPublicKey_out
53
54Now the public key is in file called image_sign_pub.der.
55
Roland Mikhel206b9142023-02-23 15:28:52 +010056For ECDSA256 these commands are similar.
Marko Kiiskila8eeba122016-12-29 17:38:54 -080057openssl ecparam -name prime256v1 -genkey -noout -out image_sign.pem
58openssl ec -in image_sign.pem -pubout -outform DER -out image_sign_pub.der
59
Christopher Collins92ea77f2016-12-12 15:59:26 -080060## Creating a key package
61
62xxd -i image_sign_pub.der image_sign_pub.c.import
63
64Then you need to create a package containing this key, or keys.
Christopher Collins92ea77f2016-12-12 15:59:26 -080065
66## Sample pkg.yml
67This gets bootutil to turn on image signature validation.
68
69 pkg.name: libs/mykeys
70 pkg.deps:
Marko Kiiskilabf986da2016-12-13 17:15:24 -080071 - "@apache-mynewt-core/boot/bootutil"
Christopher Collins92ea77f2016-12-12 15:59:26 -080072
73## Sample source file
74This exports the keys.
75
76 #include <bootutil/sign_key.h>
77
78 #include "image_sign_pub.c.import"
79
80 const struct bootutil_key bootutil_keys[] = {
81 [0] = {
82 .key = image_sign_pub_der,
83 .len = &image_sign_pub_der_len,
84 }
85 };
86
87 const int bootutil_key_cnt = sizeof(bootutil_keys) / sizeof(bootutil_keys[0]);
88
Francesco Servidio5bc98322021-11-03 13:19:22 +010089## Building the bootloader
Christopher Collins92ea77f2016-12-12 15:59:26 -080090
Fabio Utzigea422c22017-09-11 11:02:47 -030091Enable the BOOTUTIL_SIGN_RSA syscfg setting in your app or target syscfg.yml
92file
Christopher Collins92ea77f2016-12-12 15:59:26 -080093
94 syscfg.vals:
Fabio Utzig32d68f02017-07-25 22:05:38 -030095 BOOTUTIL_SIGN_RSA: 1
Christopher Collins92ea77f2016-12-12 15:59:26 -080096
97After you've created the key package, you must include it in the build
98for bootloader. So modify the pkg.yml for apps/boot to include it.
Marko Kiiskila919eaf42016-12-28 17:39:45 -080099
Roland Mikhel206b9142023-02-23 15:28:52 +0100100The syscfg variable to enable ECDSA256 is BOOTUTIL_SIGN_EC256.