blob: 9413d31d44a6f84a57e723b7ad6e46bace054755 [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 Utzig19356bf2017-05-11 16:19:36 -030031#ifdef MCUBOOT_SIGN_RSA
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include "mbedtls/rsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060033#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -030034#if defined(MCUBOOT_SIGN_EC) || defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include "mbedtls/ecdsa.h"
David Brownd7e350d2017-04-24 13:29:28 -060036#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080037#include "mbedtls/asn1.h"
38
39#include "bootutil_priv.h"
40
41/*
42 * Compute SHA256 over the image.
43 */
44static int
45bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap,
46 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
47 uint8_t *hash_result, uint8_t *seed, int seed_len)
48{
David Browne629bf32017-04-24 13:37:33 -060049 bootutil_sha256_context sha256_ctx;
Christopher Collins92ea77f2016-12-12 15:59:26 -080050 uint32_t blk_sz;
51 uint32_t size;
52 uint32_t off;
53 int rc;
54
David Browne629bf32017-04-24 13:37:33 -060055 bootutil_sha256_init(&sha256_ctx);
Christopher Collins92ea77f2016-12-12 15:59:26 -080056
57 /* in some cases (split image) the hash is seeded with data from
58 * the loader image */
59 if(seed && (seed_len > 0)) {
David Browne629bf32017-04-24 13:37:33 -060060 bootutil_sha256_update(&sha256_ctx, seed, seed_len);
Christopher Collins92ea77f2016-12-12 15:59:26 -080061 }
62
63 size = hdr->ih_img_size + hdr->ih_hdr_size;
64
65 /*
66 * Hash is computed over image header and image itself. No TLV is
67 * included ATM.
68 */
69 size = hdr->ih_img_size + hdr->ih_hdr_size;
70 for (off = 0; off < size; off += blk_sz) {
71 blk_sz = size - off;
72 if (blk_sz > tmp_buf_sz) {
73 blk_sz = tmp_buf_sz;
74 }
75 rc = flash_area_read(fap, off, tmp_buf, blk_sz);
76 if (rc) {
77 return rc;
78 }
David Browne629bf32017-04-24 13:37:33 -060079 bootutil_sha256_update(&sha256_ctx, tmp_buf, blk_sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -080080 }
David Browne629bf32017-04-24 13:37:33 -060081 bootutil_sha256_finish(&sha256_ctx, hash_result);
Christopher Collins92ea77f2016-12-12 15:59:26 -080082
83 return 0;
84}
85
86/*
87 * Verify the integrity of the image.
88 * Return non-zero if image could not be validated/does not validate.
89 */
90int
91bootutil_img_validate(struct image_header *hdr, const struct flash_area *fap,
92 uint8_t *tmp_buf, uint32_t tmp_buf_sz,
93 uint8_t *seed, int seed_len, uint8_t *out_hash)
94{
95 uint32_t off;
96 uint32_t size;
97 uint32_t sha_off = 0;
Fabio Utzig19356bf2017-05-11 16:19:36 -030098#if defined(MCUBOOT_SIGN_RSA) || defined(MCUBOOT_SIGN_EC) || \
99 defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800100 uint32_t sig_off = 0;
101 uint32_t sig_len = 0;
102#endif
103 struct image_tlv tlv;
104 uint8_t buf[256];
105 uint8_t hash[32];
106 int rc;
107
Fabio Utzig19356bf2017-05-11 16:19:36 -0300108#ifdef MCUBOOT_SIGN_RSA
Christopher Collins92ea77f2016-12-12 15:59:26 -0800109 if ((hdr->ih_flags & IMAGE_F_PKCS15_RSA2048_SHA256) == 0) {
110 return -1;
111 }
112#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -0300113#ifdef MCUBOOT_SIGN_EC
Christopher Collins92ea77f2016-12-12 15:59:26 -0800114 if ((hdr->ih_flags & IMAGE_F_ECDSA224_SHA256) == 0) {
115 return -1;
116 }
117#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -0300118#ifdef MCUBOOT_SIGN_EC256
Marko Kiiskilabf943392016-12-29 17:29:48 -0800119 if ((hdr->ih_flags & IMAGE_F_ECDSA256_SHA256) == 0) {
120 return -1;
121 }
122#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800123 if ((hdr->ih_flags & IMAGE_F_SHA256) == 0) {
124 return -1;
125 }
126
127 rc = bootutil_img_hash(hdr, fap, tmp_buf, tmp_buf_sz, hash,
128 seed, seed_len);
129 if (rc) {
130 return rc;
131 }
132
133 if (out_hash) {
134 memcpy(out_hash, hash, 32);
135 }
136
137 /* After image there are TLVs. */
138 off = hdr->ih_img_size + hdr->ih_hdr_size;
139 size = off + hdr->ih_tlv_size;
140
141 for (; off < size; off += sizeof(tlv) + tlv.it_len) {
142 rc = flash_area_read(fap, off, &tlv, sizeof tlv);
143 if (rc) {
144 return rc;
145 }
146 if (tlv.it_type == IMAGE_TLV_SHA256) {
147 if (tlv.it_len != sizeof(hash)) {
148 return -1;
149 }
150 sha_off = off + sizeof(tlv);
151 }
Fabio Utzig19356bf2017-05-11 16:19:36 -0300152#ifdef MCUBOOT_SIGN_RSA
Christopher Collins92ea77f2016-12-12 15:59:26 -0800153 if (tlv.it_type == IMAGE_TLV_RSA2048) {
154 if (tlv.it_len != 256) { /* 2048 bits */
155 return -1;
156 }
157 sig_off = off + sizeof(tlv);
158 sig_len = tlv.it_len;
159 }
160#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -0300161#ifdef MCUBOOT_SIGN_EC
Christopher Collins92ea77f2016-12-12 15:59:26 -0800162 if (tlv.it_type == IMAGE_TLV_ECDSA224) {
163 if (tlv.it_len < 64) { /* oids + 2 * 28 bytes */
164 return -1;
165 }
166 sig_off = off + sizeof(tlv);
167 sig_len = tlv.it_len;
168 }
169#endif
Fabio Utzig19356bf2017-05-11 16:19:36 -0300170#ifdef MCUBOOT_SIGN_EC256
Marko Kiiskilabf943392016-12-29 17:29:48 -0800171 if (tlv.it_type == IMAGE_TLV_ECDSA256) {
172 if (tlv.it_len < 72) { /* oids + 2 * 32 bytes */
173 return -1;
174 }
175 sig_off = off + sizeof(tlv);
176 sig_len = tlv.it_len;
177 }
178#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800179 }
180 if (hdr->ih_flags & IMAGE_F_SHA256) {
181 if (!sha_off) {
182 /*
183 * Header said there should be hash TLV, no TLV found.
184 */
185 return -1;
186 }
187 rc = flash_area_read(fap, sha_off, buf, sizeof hash);
188 if (rc) {
189 return rc;
190 }
191 if (memcmp(hash, buf, sizeof(hash))) {
192 return -1;
193 }
194 }
Fabio Utzig19356bf2017-05-11 16:19:36 -0300195#if defined(MCUBOOT_SIGN_RSA) || defined(MCUBOOT_SIGN_EC) || \
196 defined(MCUBOOT_SIGN_EC256)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800197 if (!sig_off) {
198 /*
199 * Header said there should be PKCS1.v5 signature, no TLV
200 * found.
201 */
202 return -1;
203 }
204 rc = flash_area_read(fap, sig_off, buf, sig_len);
205 if (rc) {
206 return -1;
207 }
208
209 if (hdr->ih_key_id >= bootutil_key_cnt) {
210 return -1;
211 }
212 rc = bootutil_verify_sig(hash, sizeof(hash), buf, sig_len, hdr->ih_key_id);
213 if (rc) {
214 return -1;
215 }
216#endif
217 return 0;
218}