blob: c1edcfb51fa4485560cfdf291c67d8485a27a8d3 [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
Marko Kiiskila8eeba122016-12-29 17:38:54 -080056For ECDSA224 these commands are similar.
Marko Kiiskila919eaf42016-12-28 17:39:45 -080057
58openssl ecparam -name secp224r1 -genkey -noout -out image_sign.pem
59openssl ec -in image_sign.pem -pubout -outform DER -out image_sign_pub.der
60
Marko Kiiskila8eeba122016-12-29 17:38:54 -080061And then the ECDSA256.
62openssl ecparam -name prime256v1 -genkey -noout -out image_sign.pem
63openssl ec -in image_sign.pem -pubout -outform DER -out image_sign_pub.der
64
Christopher Collins92ea77f2016-12-12 15:59:26 -080065## Creating a key package
66
67xxd -i image_sign_pub.der image_sign_pub.c.import
68
69Then you need to create a package containing this key, or keys.
Christopher Collins92ea77f2016-12-12 15:59:26 -080070
71## Sample pkg.yml
72This gets bootutil to turn on image signature validation.
73
74 pkg.name: libs/mykeys
75 pkg.deps:
Marko Kiiskilabf986da2016-12-13 17:15:24 -080076 - "@apache-mynewt-core/boot/bootutil"
Christopher Collins92ea77f2016-12-12 15:59:26 -080077
78## Sample source file
79This 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
Francesco Servidio5bc98322021-11-03 13:19:22 +010094## Building the bootloader
Christopher Collins92ea77f2016-12-12 15:59:26 -080095
Fabio Utzigea422c22017-09-11 11:02:47 -030096Enable the BOOTUTIL_SIGN_RSA syscfg setting in your app or target syscfg.yml
97file
Christopher Collins92ea77f2016-12-12 15:59:26 -080098
99 syscfg.vals:
Fabio Utzig32d68f02017-07-25 22:05:38 -0300100 BOOTUTIL_SIGN_RSA: 1
Christopher Collins92ea77f2016-12-12 15:59:26 -0800101
102After you've created the key package, you must include it in the build
103for bootloader. So modify the pkg.yml for apps/boot to include it.
Marko Kiiskila919eaf42016-12-28 17:39:45 -0800104
Fabio Utzig32d68f02017-07-25 22:05:38 -0300105The syscfg variable to enable ECDSA224 is BOOTUTIL_SIGN_EC, and
106BOOTUTIL_SIGN_EC256 for ECDS256.