2 * Copyright (c) 2013, Google Inc.
4 * (C) Copyright 2008 Semihalf
6 * (C) Copyright 2000-2006
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 * SPDX-License-Identifier: GPL-2.0+
18 * fit_set_hash_value - set hash value in requested has node
19 * @fit: pointer to the FIT format image header
20 * @noffset: hash node offset
21 * @value: hash value to be set
22 * @value_len: hash value length
24 * fit_set_hash_value() attempts to set hash value in a node at offset
25 * given and returns operation status to the caller.
31 static int fit_set_hash_value(void *fit, int noffset, uint8_t *value,
36 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
38 printf("Can't set hash '%s' property for '%s' node(%s)\n",
39 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
48 * fit_image_process_hash - Process a single subnode of the images/ node
50 * Check each subnode and process accordingly. For hash nodes we generate
51 * a hash of the supplised data and store it in the node.
53 * @fit: pointer to the FIT format image header
54 * @image_name: name of image being processes (used to display errors)
55 * @noffset: subnode offset
56 * @data: data to process
57 * @size: size of data in bytes
58 * @return 0 if ok, -1 on error
60 static int fit_image_process_hash(void *fit, const char *image_name,
61 int noffset, const void *data, size_t size)
63 uint8_t value[FIT_MAX_HASH_LEN];
64 const char *node_name;
68 node_name = fit_get_name(fit, noffset, NULL);
70 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
71 printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
72 node_name, image_name);
76 if (calculate_hash(data, size, algo, value, &value_len)) {
77 printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
78 algo, node_name, image_name);
82 if (fit_set_hash_value(fit, noffset, value, value_len)) {
83 printf("Can't set hash value for '%s' hash node in '%s' image node\n",
84 node_name, image_name);
92 * fit_image_write_sig() - write the signature to a FIT
94 * This writes the signature and signer data to the FIT.
96 * @fit: pointer to the FIT format image header
97 * @noffset: hash node offset
98 * @value: signature value to be set
99 * @value_len: signature value length
100 * @comment: Text comment to write (NULL for none)
104 * -FDT_ERR_..., on failure
106 static int fit_image_write_sig(void *fit, int noffset, uint8_t *value,
107 int value_len, const char *comment, const char *region_prop,
114 * Get the current string size, before we update the FIT and add
117 string_size = fdt_size_dt_strings(fit);
119 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
121 ret = fdt_setprop_string(fit, noffset, "signer-name",
125 ret = fdt_setprop_string(fit, noffset, "signer-version",
129 ret = fdt_setprop_string(fit, noffset, "comment", comment);
131 ret = fit_set_timestamp(fit, noffset, time(NULL));
132 if (region_prop && !ret) {
135 ret = fdt_setprop(fit, noffset, "hashed-nodes",
136 region_prop, region_proplen);
138 strdata[1] = cpu_to_fdt32(string_size);
140 ret = fdt_setprop(fit, noffset, "hashed-strings",
141 strdata, sizeof(strdata));
148 static int fit_image_setup_sig(struct image_sign_info *info,
149 const char *keydir, void *fit, const char *image_name,
150 int noffset, const char *require_keys)
152 const char *node_name;
155 node_name = fit_get_name(fit, noffset, NULL);
156 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
157 printf("Can't get algo property for '%s' signature node in '%s' image node\n",
158 node_name, image_name);
162 memset(info, '\0', sizeof(*info));
163 info->keydir = keydir;
164 info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
166 info->node_offset = noffset;
167 info->algo = image_get_sig_algo(algo_name);
168 info->require_keys = require_keys;
170 printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n",
171 algo_name, node_name, image_name);
179 * fit_image_process_sig- Process a single subnode of the images/ node
181 * Check each subnode and process accordingly. For signature nodes we
182 * generate a signed hash of the supplised data and store it in the node.
184 * @keydir: Directory containing keys to use for signing
185 * @keydest: Destination FDT blob to write public keys into
186 * @fit: pointer to the FIT format image header
187 * @image_name: name of image being processes (used to display errors)
188 * @noffset: subnode offset
189 * @data: data to process
190 * @size: size of data in bytes
191 * @comment: Comment to add to signature nodes
192 * @require_keys: Mark all keys as 'required'
193 * @return 0 if ok, -1 on error
195 static int fit_image_process_sig(const char *keydir, void *keydest,
196 void *fit, const char *image_name,
197 int noffset, const void *data, size_t size,
198 const char *comment, int require_keys)
200 struct image_sign_info info;
201 struct image_region region;
202 const char *node_name;
207 if (fit_image_setup_sig(&info, keydir, fit, image_name, noffset,
208 require_keys ? "image" : NULL))
211 node_name = fit_get_name(fit, noffset, NULL);
214 ret = info.algo->sign(&info, ®ion, 1, &value, &value_len);
216 printf("Failed to sign '%s' signature node in '%s' image node: %d\n",
217 node_name, image_name, ret);
219 /* We allow keys to be missing */
225 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
228 if (ret == -FDT_ERR_NOSPACE)
230 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
231 node_name, image_name, fdt_strerror(ret));
236 /* Get keyname again, as FDT has changed and invalidated our pointer */
237 info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
239 /* Write the public key into the supplied FDT file */
240 if (keydest && info.algo->add_verify_data(&info, keydest)) {
241 printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
242 node_name, image_name);
250 * fit_image_add_verification_data() - calculate/set verig. data for image node
252 * This adds hash and signature values for an component image node.
254 * All existing hash subnodes are checked, if algorithm property is set to
255 * one of the supported hash algorithms, hash value is computed and
256 * corresponding hash node property is set, for example:
258 * Input component image node structure:
260 * o image@1 (at image_noffset)
261 * | - data = [binary data]
265 * Output component image node structure:
267 * o image@1 (at image_noffset)
268 * | - data = [binary data]
271 * |- value = sha1(data)
273 * For signature details, please see doc/uImage.FIT/signature.txt
275 * @keydir Directory containing *.key and *.crt files (or NULL)
276 * @keydest FDT Blob to write public keys into (NULL if none)
277 * @fit: Pointer to the FIT format image header
278 * @image_noffset: Requested component image node
279 * @comment: Comment to add to signature nodes
280 * @require_keys: Mark all keys as 'required'
281 * @return: 0 on success, <0 on failure
283 int fit_image_add_verification_data(const char *keydir, void *keydest,
284 void *fit, int image_noffset, const char *comment,
287 const char *image_name;
292 /* Get image data and data length */
293 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
294 printf("Can't get image data/size\n");
298 image_name = fit_get_name(fit, image_noffset, NULL);
300 /* Process all hash subnodes of the component image node */
301 for (noffset = fdt_first_subnode(fit, image_noffset);
303 noffset = fdt_next_subnode(fit, noffset)) {
304 const char *node_name;
308 * Check subnode name, must be equal to "hash" or "signature".
309 * Multiple hash nodes require unique unit node
310 * names, e.g. hash@1, hash@2, signature@1, etc.
312 node_name = fit_get_name(fit, noffset, NULL);
313 if (!strncmp(node_name, FIT_HASH_NODENAME,
314 strlen(FIT_HASH_NODENAME))) {
315 ret = fit_image_process_hash(fit, image_name, noffset,
317 } else if (IMAGE_ENABLE_SIGN && keydir &&
318 !strncmp(node_name, FIT_SIG_NODENAME,
319 strlen(FIT_SIG_NODENAME))) {
320 ret = fit_image_process_sig(keydir, keydest,
321 fit, image_name, noffset, data, size,
322 comment, require_keys);
336 static void strlist_init(struct strlist *list)
338 memset(list, '\0', sizeof(*list));
341 static void strlist_free(struct strlist *list)
345 for (i = 0; i < list->count; i++)
346 free(list->strings[i]);
350 static int strlist_add(struct strlist *list, const char *str)
355 list->strings = realloc(list->strings,
356 (list->count + 1) * sizeof(char *));
359 list->strings[list->count++] = dup;
364 static const char *fit_config_get_image_list(void *fit, int noffset,
365 int *lenp, int *allow_missingp)
367 static const char default_list[] = FIT_KERNEL_PROP "\0"
371 /* If there is an "image" property, use that */
372 prop = fdt_getprop(fit, noffset, "sign-images", lenp);
375 return *lenp ? prop : NULL;
378 /* Default image list */
380 *lenp = sizeof(default_list);
385 static int fit_config_get_hash_list(void *fit, int conf_noffset,
386 int sig_offset, struct strlist *node_inc)
389 const char *prop, *iname, *end;
390 const char *conf_name, *sig_name;
391 char name[200], path[200];
395 conf_name = fit_get_name(fit, conf_noffset, NULL);
396 sig_name = fit_get_name(fit, sig_offset, NULL);
399 * Build a list of nodes we need to hash. We always need the root
400 * node and the configuration.
402 strlist_init(node_inc);
403 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name);
404 if (strlist_add(node_inc, "/") ||
405 strlist_add(node_inc, name))
408 /* Get a list of images that we intend to sign */
409 prop = fit_config_get_image_list(fit, sig_offset, &len,
414 /* Locate the images */
417 for (iname = prop; iname < end; iname += strlen(iname) + 1) {
422 image_noffset = fit_conf_get_prop_node(fit, conf_noffset,
424 if (image_noffset < 0) {
425 printf("Failed to find image '%s' in configuration '%s/%s'\n",
426 iname, conf_name, sig_name);
433 ret = fdt_get_path(fit, image_noffset, path, sizeof(path));
436 if (strlist_add(node_inc, path))
439 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH,
442 /* Add all this image's hashes */
444 for (noffset = fdt_first_subnode(fit, image_noffset);
446 noffset = fdt_next_subnode(fit, noffset)) {
447 const char *name = fit_get_name(fit, noffset, NULL);
449 if (strncmp(name, FIT_HASH_NODENAME,
450 strlen(FIT_HASH_NODENAME)))
452 ret = fdt_get_path(fit, noffset, path, sizeof(path));
455 if (strlist_add(node_inc, path))
461 printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n",
462 conf_name, sig_name, iname);
470 printf("Failed to find any images for configuration '%s/%s'\n",
471 conf_name, sig_name);
478 printf("Out of memory processing configuration '%s/%s'\n", conf_name,
483 printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n",
484 iname, conf_name, sig_name, fdt_strerror(ret));
488 static int fit_config_get_data(void *fit, int conf_noffset, int noffset,
489 struct image_region **regionp, int *region_countp,
490 char **region_propp, int *region_proplen)
492 char * const exc_prop[] = {"data"};
493 struct strlist node_inc;
494 struct image_region *region;
495 struct fdt_region fdt_regions[100];
496 const char *conf_name, *sig_name;
502 conf_name = fit_get_name(fit, conf_noffset, NULL);
503 sig_name = fit_get_name(fit, conf_noffset, NULL);
504 debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
506 /* Get a list of nodes we want to hash */
507 ret = fit_config_get_hash_list(fit, conf_noffset, noffset, &node_inc);
511 /* Get a list of regions to hash */
512 count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
513 exc_prop, ARRAY_SIZE(exc_prop),
514 fdt_regions, ARRAY_SIZE(fdt_regions),
515 path, sizeof(path), 1);
517 printf("Failed to hash configuration '%s/%s': %s\n", conf_name,
518 sig_name, fdt_strerror(ret));
522 printf("No data to hash for configuration '%s/%s': %s\n",
523 conf_name, sig_name, fdt_strerror(ret));
527 /* Build our list of data blocks */
528 region = fit_region_make_list(fit, fdt_regions, count, NULL);
530 printf("Out of memory hashing configuration '%s/%s'\n",
531 conf_name, sig_name);
535 /* Create a list of all hashed properties */
536 debug("Hash nodes:\n");
537 for (i = len = 0; i < node_inc.count; i++) {
538 debug(" %s\n", node_inc.strings[i]);
539 len += strlen(node_inc.strings[i]) + 1;
541 region_prop = malloc(len);
543 printf("Out of memory setting up regions for configuration '%s/%s'\n",
544 conf_name, sig_name);
547 for (i = len = 0; i < node_inc.count;
548 len += strlen(node_inc.strings[i]) + 1, i++)
549 strcpy(region_prop + len, node_inc.strings[i]);
550 strlist_free(&node_inc);
552 *region_countp = count;
554 *region_propp = region_prop;
555 *region_proplen = len;
560 static int fit_config_process_sig(const char *keydir, void *keydest,
561 void *fit, const char *conf_name, int conf_noffset,
562 int noffset, const char *comment, int require_keys)
564 struct image_sign_info info;
565 const char *node_name;
566 struct image_region *region;
574 node_name = fit_get_name(fit, noffset, NULL);
575 if (fit_config_get_data(fit, conf_noffset, noffset, ®ion,
576 ®ion_count, ®ion_prop, ®ion_proplen))
579 if (fit_image_setup_sig(&info, keydir, fit, conf_name, noffset,
580 require_keys ? "conf" : NULL))
583 ret = info.algo->sign(&info, region, region_count, &value, &value_len);
586 printf("Failed to sign '%s' signature node in '%s' conf node\n",
587 node_name, conf_name);
589 /* We allow keys to be missing */
595 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
596 region_prop, region_proplen);
598 if (ret == -FDT_ERR_NOSPACE)
600 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
601 node_name, conf_name, fdt_strerror(ret));
607 /* Get keyname again, as FDT has changed and invalidated our pointer */
608 info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
610 /* Write the public key into the supplied FDT file */
612 ret = info.algo->add_verify_data(&info, keydest);
616 printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
617 node_name, conf_name);
625 static int fit_config_add_verification_data(const char *keydir, void *keydest,
626 void *fit, int conf_noffset, const char *comment,
629 const char *conf_name;
632 conf_name = fit_get_name(fit, conf_noffset, NULL);
634 /* Process all hash subnodes of the configuration node */
635 for (noffset = fdt_first_subnode(fit, conf_noffset);
637 noffset = fdt_next_subnode(fit, noffset)) {
638 const char *node_name;
641 node_name = fit_get_name(fit, noffset, NULL);
642 if (!strncmp(node_name, FIT_SIG_NODENAME,
643 strlen(FIT_SIG_NODENAME))) {
644 ret = fit_config_process_sig(keydir, keydest,
645 fit, conf_name, conf_noffset, noffset, comment,
655 int fit_add_verification_data(const char *keydir, void *keydest, void *fit,
656 const char *comment, int require_keys)
658 int images_noffset, confs_noffset;
662 /* Find images parent node offset */
663 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
664 if (images_noffset < 0) {
665 printf("Can't find images parent node '%s' (%s)\n",
666 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
667 return images_noffset;
670 /* Process its subnodes, print out component images details */
671 for (noffset = fdt_first_subnode(fit, images_noffset);
673 noffset = fdt_next_subnode(fit, noffset)) {
675 * Direct child node of the images parent node,
676 * i.e. component image node.
678 ret = fit_image_add_verification_data(keydir, keydest,
679 fit, noffset, comment, require_keys);
684 /* If there are no keys, we can't sign configurations */
685 if (!IMAGE_ENABLE_SIGN || !keydir)
688 /* Find configurations parent node offset */
689 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
690 if (confs_noffset < 0) {
691 printf("Can't find images parent node '%s' (%s)\n",
692 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
696 /* Process its subnodes, print out component images details */
697 for (noffset = fdt_first_subnode(fit, confs_noffset);
699 noffset = fdt_next_subnode(fit, noffset)) {
700 ret = fit_config_add_verification_data(keydir, keydest,
701 fit, noffset, comment,
710 #ifdef CONFIG_FIT_SIGNATURE
711 int fit_check_sign(const void *fit, const void *key)
716 cfg_noffset = fit_conf_get_node(fit, NULL);
720 printf("Verifying Hash Integrity ... ");
721 ret = fit_config_verify(fit, cfg_noffset);
724 ret = bootm_host_load_images(fit, cfg_noffset);