blob: 3d8281f2faa6d6de24d7f84dc42910595e245be4 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass3e569a62013-06-13 15:10:00 -07002/*
3 * Copyright (c) 2013, Google Inc.
Simon Glass3e569a62013-06-13 15:10:00 -07004 */
5
6#ifdef USE_HOSTCC
7#include "mkimage.h"
8#include <time.h>
9#else
10#include <common.h>
Simon Glass56518e72013-06-13 15:10:01 -070011#include <malloc.h>
12DECLARE_GLOBAL_DATA_PTR;
Simon Glass3e569a62013-06-13 15:10:00 -070013#endif /* !USE_HOSTCC*/
Simon Glass3e569a62013-06-13 15:10:00 -070014#include <image.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020015#include <u-boot/rsa.h>
16#include <u-boot/rsa-checksum.h>
Simon Glass3e569a62013-06-13 15:10:00 -070017
Simon Glass4d098522013-06-13 15:10:09 -070018#define IMAGE_MAX_HASHED_NODES 100
19
Heiko Schocher646257d2014-03-03 12:19:26 +010020#ifdef USE_HOSTCC
Heiko Schocher29a23f92014-03-03 12:19:30 +010021void *host_blob;
22void image_set_host_blob(void *blob)
Heiko Schocher646257d2014-03-03 12:19:26 +010023{
Heiko Schocher29a23f92014-03-03 12:19:30 +010024 host_blob = blob;
25}
26void *image_get_host_blob(void)
27{
28 return host_blob;
Heiko Schocher646257d2014-03-03 12:19:26 +010029}
30#endif
31
32struct checksum_algo checksum_algos[] = {
33 {
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090034 .name = "sha1",
35 .checksum_len = SHA1_SUM_LEN,
36 .der_len = SHA1_DER_LEN,
37 .der_prefix = sha1_der_prefix,
Heiko Schocher646257d2014-03-03 12:19:26 +010038#if IMAGE_ENABLE_SIGN
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090039 .calculate_sign = EVP_sha1,
Heiko Schocher29a23f92014-03-03 12:19:30 +010040#endif
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090041 .calculate = hash_calculate,
Heiko Schocher646257d2014-03-03 12:19:26 +010042 },
43 {
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090044 .name = "sha256",
45 .checksum_len = SHA256_SUM_LEN,
46 .der_len = SHA256_DER_LEN,
47 .der_prefix = sha256_der_prefix,
Heiko Schocherdb1b5f32014-03-03 12:19:27 +010048#if IMAGE_ENABLE_SIGN
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090049 .calculate_sign = EVP_sha256,
Heiko Schocher29a23f92014-03-03 12:19:30 +010050#endif
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090051 .calculate = hash_calculate,
Heiko Schocher646257d2014-03-03 12:19:26 +010052 }
Heiko Schocherdb1b5f32014-03-03 12:19:27 +010053
Heiko Schocher646257d2014-03-03 12:19:26 +010054};
Heiko Schocherdb1b5f32014-03-03 12:19:27 +010055
Andrew Duda0c1d74f2016-11-08 18:53:41 +000056struct crypto_algo crypto_algos[] = {
Simon Glass19c402a2013-06-13 15:10:02 -070057 {
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090058 .name = "rsa2048",
59 .key_len = RSA2048_BYTES,
60 .sign = rsa_sign,
61 .add_verify_data = rsa_add_verify_data,
62 .verify = rsa_verify,
Andrew Duda0c1d74f2016-11-08 18:53:41 +000063 },
64 {
Masahiro Yamada8ec87df2017-10-23 10:03:40 +090065 .name = "rsa4096",
66 .key_len = RSA4096_BYTES,
67 .sign = rsa_sign,
68 .add_verify_data = rsa_add_verify_data,
69 .verify = rsa_verify,
Andrew Duda0c1d74f2016-11-08 18:53:41 +000070 }
71
72};
73
Philippe Reynes20031562018-11-14 13:51:00 +010074struct padding_algo padding_algos[] = {
75 {
76 .name = "pkcs-1.5",
77 .verify = padding_pkcs_15_verify,
78 },
79};
80
Andrew Duda83dd98e2016-11-08 18:53:41 +000081struct checksum_algo *image_get_checksum_algo(const char *full_name)
Simon Glass3e569a62013-06-13 15:10:00 -070082{
83 int i;
Andrew Duda83dd98e2016-11-08 18:53:41 +000084 const char *name;
Simon Glass3e569a62013-06-13 15:10:00 -070085
Andrew Duda83dd98e2016-11-08 18:53:41 +000086 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
87 name = checksum_algos[i].name;
88 /* Make sure names match and next char is a comma */
89 if (!strncmp(name, full_name, strlen(name)) &&
90 full_name[strlen(name)] == ',')
91 return &checksum_algos[i];
92 }
93
94 return NULL;
95}
96
97struct crypto_algo *image_get_crypto_algo(const char *full_name)
98{
99 int i;
100 const char *name;
101
102 /* Move name to after the comma */
103 name = strchr(full_name, ',');
104 if (!name)
105 return NULL;
106 name += 1;
107
108 for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
109 if (!strcmp(crypto_algos[i].name, name))
110 return &crypto_algos[i];
Simon Glass3e569a62013-06-13 15:10:00 -0700111 }
112
113 return NULL;
114}
Simon Glass56518e72013-06-13 15:10:01 -0700115
Philippe Reynes20031562018-11-14 13:51:00 +0100116struct padding_algo *image_get_padding_algo(const char *name)
117{
118 int i;
119
120 if (!name)
121 return NULL;
122
123 for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
124 if (!strcmp(padding_algos[i].name, name))
125 return &padding_algos[i];
126 }
127
128 return NULL;
129}
130
Simon Glass4d098522013-06-13 15:10:09 -0700131/**
132 * fit_region_make_list() - Make a list of image regions
133 *
134 * Given a list of fdt_regions, create a list of image_regions. This is a
135 * simple conversion routine since the FDT and image code use different
136 * structures.
137 *
138 * @fit: FIT image
139 * @fdt_regions: Pointer to FDT regions
140 * @count: Number of FDT regions
141 * @region: Pointer to image regions, which must hold @count records. If
142 * region is NULL, then (except for an SPL build) the array will be
143 * allocated.
144 * @return: Pointer to image regions
145 */
146struct image_region *fit_region_make_list(const void *fit,
147 struct fdt_region *fdt_regions, int count,
148 struct image_region *region)
149{
150 int i;
151
152 debug("Hash regions:\n");
153 debug("%10s %10s\n", "Offset", "Size");
154
155 /*
156 * Use malloc() except in SPL (to save code size). In SPL the caller
157 * must allocate the array.
158 */
159#ifndef CONFIG_SPL_BUILD
160 if (!region)
161 region = calloc(sizeof(*region), count);
162#endif
163 if (!region)
164 return NULL;
165 for (i = 0; i < count; i++) {
166 debug("%10x %10x\n", fdt_regions[i].offset,
167 fdt_regions[i].size);
168 region[i].data = fit + fdt_regions[i].offset;
169 region[i].size = fdt_regions[i].size;
170 }
171
172 return region;
173}
174
Simon Glass56518e72013-06-13 15:10:01 -0700175static int fit_image_setup_verify(struct image_sign_info *info,
176 const void *fit, int noffset, int required_keynode,
177 char **err_msgp)
178{
179 char *algo_name;
Philippe Reynes20031562018-11-14 13:51:00 +0100180 const char *padding_name;
Simon Glass56518e72013-06-13 15:10:01 -0700181
Teddy Reed72239fc2018-06-09 11:38:05 -0400182 if (fdt_totalsize(fit) > CONFIG_FIT_SIGNATURE_MAX_SIZE) {
183 *err_msgp = "Total size too large";
184 return 1;
185 }
186
Simon Glass56518e72013-06-13 15:10:01 -0700187 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
188 *err_msgp = "Can't get hash algo property";
189 return -1;
190 }
Philippe Reynes20031562018-11-14 13:51:00 +0100191
192 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
193 if (!padding_name)
194 padding_name = RSA_DEFAULT_PADDING_NAME;
195
Simon Glass56518e72013-06-13 15:10:01 -0700196 memset(info, '\0', sizeof(*info));
197 info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
198 info->fit = (void *)fit;
199 info->node_offset = noffset;
Andrew Duda83dd98e2016-11-08 18:53:41 +0000200 info->name = algo_name;
201 info->checksum = image_get_checksum_algo(algo_name);
202 info->crypto = image_get_crypto_algo(algo_name);
Philippe Reynes20031562018-11-14 13:51:00 +0100203 info->padding = image_get_padding_algo(padding_name);
Simon Glass56518e72013-06-13 15:10:01 -0700204 info->fdt_blob = gd_fdt_blob();
205 info->required_keynode = required_keynode;
206 printf("%s:%s", algo_name, info->keyname);
207
Andrew Duda83dd98e2016-11-08 18:53:41 +0000208 if (!info->checksum || !info->crypto) {
Simon Glass56518e72013-06-13 15:10:01 -0700209 *err_msgp = "Unknown signature algorithm";
210 return -1;
211 }
212
213 return 0;
214}
215
216int fit_image_check_sig(const void *fit, int noffset, const void *data,
217 size_t size, int required_keynode, char **err_msgp)
218{
219 struct image_sign_info info;
220 struct image_region region;
221 uint8_t *fit_value;
222 int fit_value_len;
223
224 *err_msgp = NULL;
225 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
226 err_msgp))
227 return -1;
228
229 if (fit_image_hash_get_value(fit, noffset, &fit_value,
230 &fit_value_len)) {
231 *err_msgp = "Can't get hash value property";
232 return -1;
233 }
234
235 region.data = data;
236 region.size = size;
237
Andrew Duda83dd98e2016-11-08 18:53:41 +0000238 if (info.crypto->verify(&info, &region, 1, fit_value, fit_value_len)) {
Simon Glass56518e72013-06-13 15:10:01 -0700239 *err_msgp = "Verification failed";
240 return -1;
241 }
242
243 return 0;
244}
245
246static int fit_image_verify_sig(const void *fit, int image_noffset,
247 const char *data, size_t size, const void *sig_blob,
248 int sig_offset)
249{
250 int noffset;
251 char *err_msg = "";
252 int verified = 0;
253 int ret;
254
255 /* Process all hash subnodes of the component image node */
Simon Glassdf87e6b2016-10-02 17:59:29 -0600256 fdt_for_each_subnode(noffset, fit, image_noffset) {
Simon Glass56518e72013-06-13 15:10:01 -0700257 const char *name = fit_get_name(fit, noffset, NULL);
258
259 if (!strncmp(name, FIT_SIG_NODENAME,
260 strlen(FIT_SIG_NODENAME))) {
261 ret = fit_image_check_sig(fit, noffset, data,
262 size, -1, &err_msg);
263 if (ret) {
264 puts("- ");
265 } else {
266 puts("+ ");
267 verified = 1;
268 break;
269 }
270 }
271 }
272
273 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
274 err_msg = "Corrupted or truncated tree";
275 goto error;
276 }
277
278 return verified ? 0 : -EPERM;
279
280error:
281 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
282 err_msg, fit_get_name(fit, noffset, NULL),
283 fit_get_name(fit, image_noffset, NULL));
284 return -1;
285}
286
287int fit_image_verify_required_sigs(const void *fit, int image_noffset,
288 const char *data, size_t size, const void *sig_blob,
289 int *no_sigsp)
290{
291 int verify_count = 0;
292 int noffset;
293 int sig_node;
294
295 /* Work out what we need to verify */
296 *no_sigsp = 1;
297 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
298 if (sig_node < 0) {
299 debug("%s: No signature node found: %s\n", __func__,
300 fdt_strerror(sig_node));
301 return 0;
302 }
303
Simon Glassdf87e6b2016-10-02 17:59:29 -0600304 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
Simon Glass56518e72013-06-13 15:10:01 -0700305 const char *required;
306 int ret;
307
308 required = fdt_getprop(sig_blob, noffset, "required", NULL);
309 if (!required || strcmp(required, "image"))
310 continue;
311 ret = fit_image_verify_sig(fit, image_noffset, data, size,
312 sig_blob, noffset);
313 if (ret) {
314 printf("Failed to verify required signature '%s'\n",
315 fit_get_name(sig_blob, noffset, NULL));
316 return ret;
317 }
318 verify_count++;
319 }
320
321 if (verify_count)
322 *no_sigsp = 0;
323
324 return 0;
325}
Simon Glass4d098522013-06-13 15:10:09 -0700326
327int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
328 char **err_msgp)
329{
330 char * const exc_prop[] = {"data"};
331 const char *prop, *end, *name;
332 struct image_sign_info info;
333 const uint32_t *strings;
334 uint8_t *fit_value;
335 int fit_value_len;
336 int max_regions;
337 int i, prop_len;
338 char path[200];
339 int count;
340
341 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
342 fit_get_name(fit, noffset, NULL),
343 fit_get_name(gd_fdt_blob(), required_keynode, NULL));
344 *err_msgp = NULL;
345 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
346 err_msgp))
347 return -1;
348
349 if (fit_image_hash_get_value(fit, noffset, &fit_value,
350 &fit_value_len)) {
351 *err_msgp = "Can't get hash value property";
352 return -1;
353 }
354
355 /* Count the number of strings in the property */
356 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
357 end = prop ? prop + prop_len : prop;
358 for (name = prop, count = 0; name < end; name++)
359 if (!*name)
360 count++;
361 if (!count) {
362 *err_msgp = "Can't get hashed-nodes property";
363 return -1;
364 }
365
Konrad Beckmannf1c85682018-11-07 14:51:45 -0500366 if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
367 *err_msgp = "hashed-nodes property must be null-terminated";
368 return -1;
369 }
370
Simon Glass4d098522013-06-13 15:10:09 -0700371 /* Add a sanity check here since we are using the stack */
372 if (count > IMAGE_MAX_HASHED_NODES) {
373 *err_msgp = "Number of hashed nodes exceeds maximum";
374 return -1;
375 }
376
377 /* Create a list of node names from those strings */
378 char *node_inc[count];
379
380 debug("Hash nodes (%d):\n", count);
381 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
382 debug(" '%s'\n", name);
383 node_inc[i] = (char *)name;
384 }
385
386 /*
387 * Each node can generate one region for each sub-node. Allow for
Andre Przywarab2267e82017-12-04 02:05:10 +0000388 * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
Simon Glass4d098522013-06-13 15:10:09 -0700389 */
390 max_regions = 20 + count * 7;
391 struct fdt_region fdt_regions[max_regions];
392
393 /* Get a list of regions to hash */
394 count = fdt_find_regions(fit, node_inc, count,
395 exc_prop, ARRAY_SIZE(exc_prop),
396 fdt_regions, max_regions - 1,
397 path, sizeof(path), 0);
398 if (count < 0) {
399 *err_msgp = "Failed to hash configuration";
400 return -1;
401 }
402 if (count == 0) {
403 *err_msgp = "No data to hash";
404 return -1;
405 }
406 if (count >= max_regions - 1) {
407 *err_msgp = "Too many hash regions";
408 return -1;
409 }
410
411 /* Add the strings */
412 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
413 if (strings) {
Teddy Reed7346c1e2018-06-09 11:45:20 -0400414 /*
415 * The strings region offset must be a static 0x0.
416 * This is set in tool/image-host.c
417 */
418 fdt_regions[count].offset = fdt_off_dt_strings(fit);
Simon Glass4d098522013-06-13 15:10:09 -0700419 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
420 count++;
421 }
422
423 /* Allocate the region list on the stack */
424 struct image_region region[count];
425
426 fit_region_make_list(fit, fdt_regions, count, region);
Andrew Duda83dd98e2016-11-08 18:53:41 +0000427 if (info.crypto->verify(&info, region, count, fit_value,
428 fit_value_len)) {
Simon Glass4d098522013-06-13 15:10:09 -0700429 *err_msgp = "Verification failed";
430 return -1;
431 }
432
433 return 0;
434}
435
436static int fit_config_verify_sig(const void *fit, int conf_noffset,
437 const void *sig_blob, int sig_offset)
438{
439 int noffset;
440 char *err_msg = "";
441 int verified = 0;
442 int ret;
443
444 /* Process all hash subnodes of the component conf node */
Simon Glassdf87e6b2016-10-02 17:59:29 -0600445 fdt_for_each_subnode(noffset, fit, conf_noffset) {
Simon Glass4d098522013-06-13 15:10:09 -0700446 const char *name = fit_get_name(fit, noffset, NULL);
447
448 if (!strncmp(name, FIT_SIG_NODENAME,
449 strlen(FIT_SIG_NODENAME))) {
450 ret = fit_config_check_sig(fit, noffset, sig_offset,
451 &err_msg);
452 if (ret) {
453 puts("- ");
454 } else {
455 puts("+ ");
456 verified = 1;
457 break;
458 }
459 }
460 }
461
462 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
463 err_msg = "Corrupted or truncated tree";
464 goto error;
465 }
466
467 return verified ? 0 : -EPERM;
468
469error:
470 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
471 err_msg, fit_get_name(fit, noffset, NULL),
472 fit_get_name(fit, conf_noffset, NULL));
473 return -1;
474}
475
476int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
477 const void *sig_blob)
478{
479 int noffset;
480 int sig_node;
481
482 /* Work out what we need to verify */
483 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
484 if (sig_node < 0) {
485 debug("%s: No signature node found: %s\n", __func__,
486 fdt_strerror(sig_node));
487 return 0;
488 }
489
Simon Glassdf87e6b2016-10-02 17:59:29 -0600490 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
Simon Glass4d098522013-06-13 15:10:09 -0700491 const char *required;
492 int ret;
493
494 required = fdt_getprop(sig_blob, noffset, "required", NULL);
495 if (!required || strcmp(required, "conf"))
496 continue;
497 ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
498 noffset);
499 if (ret) {
500 printf("Failed to verify required signature '%s'\n",
501 fit_get_name(sig_blob, noffset, NULL));
502 return ret;
503 }
504 }
505
506 return 0;
507}
508
509int fit_config_verify(const void *fit, int conf_noffset)
510{
Simon Glass12df2ab2014-06-12 07:24:45 -0600511 return fit_config_verify_required_sigs(fit, conf_noffset,
512 gd_fdt_blob());
Simon Glass4d098522013-06-13 15:10:09 -0700513}