blob: be856fdd61643bc9c137f79d4410050e4e749f1f [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include <assert.h>
21#include <stddef.h>
22#include <inttypes.h>
23#include <string.h>
24
Christopher Collins92ea77f2016-12-12 15:59:26 -080025#include "hal/hal_flash.h"
26#include "flash_map/flash_map.h"
27#include "bootutil/image.h"
David Browne629bf32017-04-24 13:37:33 -060028#include "bootutil/sha256.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080029#include "bootutil/sign_key.h"
30
Fabio Utzigba1fbe62017-07-21 14:01:20 -030031#ifdef MCUBOOT_MYNEWT
32#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030033#endif
34
Fabio Utzig19356bf2017-05-11 16:19:36 -030035#ifdef MCUBOOT_SIGN_RSA
Christopher Collins92ea77f2016-12-12 15:59:26 -080036#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060037#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -030038#if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080039#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060040#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080041#include "mbedtls/asn1.h"
42
43#include "bootutil_priv.h"
44
45/*
46 * Compute SHA256 over the image.
47 */
48static int
49bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap,
50 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
51 uint8_t *hash_result, uint8_t *seed, int seed_len)
52{
David Browne629bf32017-04-24 13:37:33 -060053 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080054 uint32_t blk_sz;
55 uint32_t size;
56 uint32_t off;
57 int rc;
58
David Browne629bf32017-04-24 13:37:33 -060059 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080060
61 /* in some cases (split image) the hash is seeded with data from
62 * the loader image */
63 if(seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -060064 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -080065 }
66
67 size = hdr->ih_img_size + hdr->ih_hdr_size;
68
69 /*
70 * Hash is computed over image header and image itself. No TLV is
71 * included ATM.
72 */
73 size = hdr->ih_img_size + hdr->ih_hdr_size;
74 for (off = 0; off < size; off += blk_sz) {
75 blk_sz = size - off;
76 if (blk_sz > tmp_buf_sz) {
77 blk_sz = tmp_buf_sz;
78 }
79 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
80 if (rc) {
81 return rc;
82 }
David Browne629bf32017-04-24 13:37:33 -060083 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -080084 }
David Browne629bf32017-04-24 13:37:33 -060085 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -080086
87 return 0;
88}
89
90/*
91 * Verify the integrity of the image.
92 * Return non-zero if image could not be validated/does not validate.
93 */
94int
95bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
96 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
97 uint8_t *seed, int seed_len, uint8_t *out_hash)
98{
99 uint32_t off;
100 uint32_t size;
101 uint32_t sha_off = 0;
Fabio Utzig19356bf2017-05-11 16:19:36 -0300102#if defined(MCUBOOT_SIGN_RSA) || defined(MCUBOOT_SIGN_EC) || \
103 defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800104 uint32_t sig_off = 0;
105 uint32_t sig_len = 0;
106#endif
107 struct image_tlv tlv;
108 uint8_t buf[256];
109 uint8_t hash[32];
110 int rc;
111
Fabio Utzig19356bf2017-05-11 16:19:36 -0300112#ifdef MCUBOOT_SIGN_RSA
David Brownf4e904d2017-05-31 13:21:39 -0600113#ifdef MCUBOOT_RSA_PKCS1_15
Christopher Collins92ea77f2016-12-12 15:59:26 -0800114 if ((hdr->ih_flags & IMAGE_F_PKCS15_RSA2048_SHA256) == 0) {
115 return -1;
116 }
David Brownf4e904d2017-05-31 13:21:39 -0600117#else
118 if ((hdr->ih_flags & IMAGE_F_PKCS1_PSS_RSA2048_SHA256) == 0) {
119 return -1;
120 }
121#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800122#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -0300123#ifdef MCUBOOT_SIGN_EC
Christopher Collins92ea77f2016-12-12 15:59:26 -0800124 if ((hdr->ih_flags & IMAGE_F_ECDSA224_SHA256) == 0) {
125 return -1;
126 }
127#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -0300128#ifdef MCUBOOT_SIGN_EC256
Marko Kiiskilabf943392016-12-29 17:29:48 -0800129 if ((hdr->ih_flags & IMAGE_F_ECDSA256_SHA256) == 0) {
130 return -1;
131 }
132#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800133 if ((hdr->ih_flags & IMAGE_F_SHA256) == 0) {
134 return -1;
135 }
136
137 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash,
138 seed, seed_len);
139 if (rc) {
140 return rc;
141 }
142
143 if (out_hash) {
144 memcpy(out_hash, hash, 32);
145 }
146
147 /* After image there are TLVs. */
148 off = hdr->ih_img_size + hdr->ih_hdr_size;
149 size = off + hdr->ih_tlv_size;
150
151 for (; off < size; off += sizeof(tlv) + tlv.it_len) {
152 rc = flash_area_read(fap, off, &tlv, sizeof tlv);
153 if (rc) {
154 return rc;
155 }
156 if (tlv.it_type == IMAGE_TLV_SHA256) {
157 if (tlv.it_len != sizeof(hash)) {
158 return -1;
159 }
160 sha_off = off + sizeof(tlv);
161 }
Fabio Utzig19356bf2017-05-11 16:19:36 -0300162#ifdef MCUBOOT_SIGN_RSA
Christopher Collins92ea77f2016-12-12 15:59:26 -0800163 if (tlv.it_type == IMAGE_TLV_RSA2048) {
164 if (tlv.it_len != 256) { /* 2048 bits */
165 return -1;
166 }
167 sig_off = off + sizeof(tlv);
168 sig_len = tlv.it_len;
169 }
170#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -0300171#ifdef MCUBOOT_SIGN_EC
Christopher Collins92ea77f2016-12-12 15:59:26 -0800172 if (tlv.it_type == IMAGE_TLV_ECDSA224) {
173 if (tlv.it_len < 64) { /* oids + 2 * 28 bytes */
174 return -1;
175 }
176 sig_off = off + sizeof(tlv);
177 sig_len = tlv.it_len;
178 }
179#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -0300180#ifdef MCUBOOT_SIGN_EC256
Marko Kiiskilabf943392016-12-29 17:29:48 -0800181 if (tlv.it_type == IMAGE_TLV_ECDSA256) {
182 if (tlv.it_len < 72) { /* oids + 2 * 32 bytes */
183 return -1;
184 }
185 sig_off = off + sizeof(tlv);
186 sig_len = tlv.it_len;
187 }
188#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800189 }
190 if (hdr->ih_flags & IMAGE_F_SHA256) {
191 if (!sha_off) {
192 /*
193 * Header said there should be hash TLV, no TLV found.
194 */
195 return -1;
196 }
197 rc = flash_area_read(fap, sha_off, buf, sizeof hash);
198 if (rc) {
199 return rc;
200 }
201 if (memcmp(hash, buf, sizeof(hash))) {
202 return -1;
203 }
204 }
Fabio Utzig19356bf2017-05-11 16:19:36 -0300205#if defined(MCUBOOT_SIGN_RSA) || defined(MCUBOOT_SIGN_EC) || \
206 defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800207 if (!sig_off) {
208 /*
209 * Header said there should be PKCS1.v5 signature, no TLV
210 * found.
211 */
212 return -1;
213 }
214 rc = flash_area_read(fap, sig_off, buf, sig_len);
215 if (rc) {
216 return -1;
217 }
218
219 if (hdr->ih_key_id >= bootutil_key_cnt) {
220 return -1;
221 }
222 rc = bootutil_verify_sig(hash, sizeof(hash), buf, sig_len, hdr->ih_key_id);
223 if (rc) {
224 return -1;
225 }
226#endif
227 return 0;
228}