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),
41 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
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;
69 node_name = fit_get_name(fit, noffset, NULL);
71 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
72 printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
73 node_name, image_name);
77 if (calculate_hash(data, size, algo, value, &value_len)) {
78 printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
79 algo, node_name, image_name);
80 return -EPROTONOSUPPORT;
83 ret = fit_set_hash_value(fit, noffset, value, value_len);
85 printf("Can't set hash value for '%s' hash node in '%s' image node\n",
86 node_name, image_name);
94 * fit_image_write_sig() - write the signature to a FIT
96 * This writes the signature and signer data to the FIT.
98 * @fit: pointer to the FIT format image header
99 * @noffset: hash node offset
100 * @value: signature value to be set
101 * @value_len: signature value length
102 * @comment: Text comment to write (NULL for none)
106 * -FDT_ERR_..., on failure
108 static int fit_image_write_sig(void *fit, int noffset, uint8_t *value,
109 int value_len, const char *comment, const char *region_prop,
116 * Get the current string size, before we update the FIT and add
119 string_size = fdt_size_dt_strings(fit);
121 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
123 ret = fdt_setprop_string(fit, noffset, "signer-name",
127 ret = fdt_setprop_string(fit, noffset, "signer-version",
131 ret = fdt_setprop_string(fit, noffset, "comment", comment);
133 ret = fit_set_timestamp(fit, noffset, time(NULL));
134 if (region_prop && !ret) {
137 ret = fdt_setprop(fit, noffset, "hashed-nodes",
138 region_prop, region_proplen);
140 strdata[1] = cpu_to_fdt32(string_size);
142 ret = fdt_setprop(fit, noffset, "hashed-strings",
143 strdata, sizeof(strdata));
150 static int fit_image_setup_sig(struct image_sign_info *info,
151 const char *keydir, void *fit, const char *image_name,
152 int noffset, const char *require_keys, const char *engine_id)
154 const char *node_name;
157 node_name = fit_get_name(fit, noffset, NULL);
158 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
159 printf("Can't get algo property for '%s' signature node in '%s' image node\n",
160 node_name, image_name);
164 memset(info, '\0', sizeof(*info));
165 info->keydir = keydir;
166 info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
168 info->node_offset = noffset;
169 info->name = strdup(algo_name);
170 info->checksum = image_get_checksum_algo(algo_name);
171 info->crypto = image_get_crypto_algo(algo_name);
172 info->require_keys = require_keys;
173 info->engine_id = engine_id;
174 if (!info->checksum || !info->crypto) {
175 printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n",
176 algo_name, node_name, image_name);
184 * fit_image_process_sig- Process a single subnode of the images/ node
186 * Check each subnode and process accordingly. For signature nodes we
187 * generate a signed hash of the supplised data and store it in the node.
189 * @keydir: Directory containing keys to use for signing
190 * @keydest: Destination FDT blob to write public keys into
191 * @fit: pointer to the FIT format image header
192 * @image_name: name of image being processes (used to display errors)
193 * @noffset: subnode offset
194 * @data: data to process
195 * @size: size of data in bytes
196 * @comment: Comment to add to signature nodes
197 * @require_keys: Mark all keys as 'required'
198 * @engine_id: Engine to use for signing
199 * @return 0 if ok, -1 on error
201 static int fit_image_process_sig(const char *keydir, void *keydest,
202 void *fit, const char *image_name,
203 int noffset, const void *data, size_t size,
204 const char *comment, int require_keys, const char *engine_id)
206 struct image_sign_info info;
207 struct image_region region;
208 const char *node_name;
213 if (fit_image_setup_sig(&info, keydir, fit, image_name, noffset,
214 require_keys ? "image" : NULL, engine_id))
217 node_name = fit_get_name(fit, noffset, NULL);
220 ret = info.crypto->sign(&info, ®ion, 1, &value, &value_len);
222 printf("Failed to sign '%s' signature node in '%s' image node: %d\n",
223 node_name, image_name, ret);
225 /* We allow keys to be missing */
231 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
234 if (ret == -FDT_ERR_NOSPACE)
236 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
237 node_name, image_name, fdt_strerror(ret));
242 /* Get keyname again, as FDT has changed and invalidated our pointer */
243 info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
246 * Write the public key into the supplied FDT file; this might fail
247 * several times, since we try signing with successively increasing
251 ret = info.crypto->add_verify_data(&info, keydest);
253 printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
254 node_name, image_name);
263 * fit_image_add_verification_data() - calculate/set verig. data for image node
265 * This adds hash and signature values for an component image node.
267 * All existing hash subnodes are checked, if algorithm property is set to
268 * one of the supported hash algorithms, hash value is computed and
269 * corresponding hash node property is set, for example:
271 * Input component image node structure:
273 * o image@1 (at image_noffset)
274 * | - data = [binary data]
278 * Output component image node structure:
280 * o image@1 (at image_noffset)
281 * | - data = [binary data]
284 * |- value = sha1(data)
286 * For signature details, please see doc/uImage.FIT/signature.txt
288 * @keydir Directory containing *.key and *.crt files (or NULL)
289 * @keydest FDT Blob to write public keys into (NULL if none)
290 * @fit: Pointer to the FIT format image header
291 * @image_noffset: Requested component image node
292 * @comment: Comment to add to signature nodes
293 * @require_keys: Mark all keys as 'required'
294 * @engine_id: Engine to use for signing
295 * @return: 0 on success, <0 on failure
297 int fit_image_add_verification_data(const char *keydir, void *keydest,
298 void *fit, int image_noffset, const char *comment,
299 int require_keys, const char *engine_id)
301 const char *image_name;
306 /* Get image data and data length */
307 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
308 printf("Can't get image data/size\n");
312 image_name = fit_get_name(fit, image_noffset, NULL);
314 /* Process all hash subnodes of the component image node */
315 for (noffset = fdt_first_subnode(fit, image_noffset);
317 noffset = fdt_next_subnode(fit, noffset)) {
318 const char *node_name;
322 * Check subnode name, must be equal to "hash" or "signature".
323 * Multiple hash nodes require unique unit node
324 * names, e.g. hash@1, hash@2, signature@1, etc.
326 node_name = fit_get_name(fit, noffset, NULL);
327 if (!strncmp(node_name, FIT_HASH_NODENAME,
328 strlen(FIT_HASH_NODENAME))) {
329 ret = fit_image_process_hash(fit, image_name, noffset,
331 } else if (IMAGE_ENABLE_SIGN && keydir &&
332 !strncmp(node_name, FIT_SIG_NODENAME,
333 strlen(FIT_SIG_NODENAME))) {
334 ret = fit_image_process_sig(keydir, keydest,
335 fit, image_name, noffset, data, size,
336 comment, require_keys, engine_id);
350 static void strlist_init(struct strlist *list)
352 memset(list, '\0', sizeof(*list));
355 static void strlist_free(struct strlist *list)
359 for (i = 0; i < list->count; i++)
360 free(list->strings[i]);
364 static int strlist_add(struct strlist *list, const char *str)
369 list->strings = realloc(list->strings,
370 (list->count + 1) * sizeof(char *));
373 list->strings[list->count++] = dup;
378 static const char *fit_config_get_image_list(void *fit, int noffset,
379 int *lenp, int *allow_missingp)
381 static const char default_list[] = FIT_KERNEL_PROP "\0"
385 /* If there is an "image" property, use that */
386 prop = fdt_getprop(fit, noffset, "sign-images", lenp);
389 return *lenp ? prop : NULL;
392 /* Default image list */
394 *lenp = sizeof(default_list);
399 static int fit_config_get_hash_list(void *fit, int conf_noffset,
400 int sig_offset, struct strlist *node_inc)
403 const char *prop, *iname, *end;
404 const char *conf_name, *sig_name;
405 char name[200], path[200];
409 conf_name = fit_get_name(fit, conf_noffset, NULL);
410 sig_name = fit_get_name(fit, sig_offset, NULL);
413 * Build a list of nodes we need to hash. We always need the root
414 * node and the configuration.
416 strlist_init(node_inc);
417 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name);
418 if (strlist_add(node_inc, "/") ||
419 strlist_add(node_inc, name))
422 /* Get a list of images that we intend to sign */
423 prop = fit_config_get_image_list(fit, sig_offset, &len,
428 /* Locate the images */
431 for (iname = prop; iname < end; iname += strlen(iname) + 1) {
436 image_noffset = fit_conf_get_prop_node(fit, conf_noffset,
438 if (image_noffset < 0) {
439 printf("Failed to find image '%s' in configuration '%s/%s'\n",
440 iname, conf_name, sig_name);
447 ret = fdt_get_path(fit, image_noffset, path, sizeof(path));
450 if (strlist_add(node_inc, path))
453 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH,
456 /* Add all this image's hashes */
458 for (noffset = fdt_first_subnode(fit, image_noffset);
460 noffset = fdt_next_subnode(fit, noffset)) {
461 const char *name = fit_get_name(fit, noffset, NULL);
463 if (strncmp(name, FIT_HASH_NODENAME,
464 strlen(FIT_HASH_NODENAME)))
466 ret = fdt_get_path(fit, noffset, path, sizeof(path));
469 if (strlist_add(node_inc, path))
475 printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n",
476 conf_name, sig_name, iname);
484 printf("Failed to find any images for configuration '%s/%s'\n",
485 conf_name, sig_name);
492 printf("Out of memory processing configuration '%s/%s'\n", conf_name,
497 printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n",
498 iname, conf_name, sig_name, fdt_strerror(ret));
502 static int fit_config_get_data(void *fit, int conf_noffset, int noffset,
503 struct image_region **regionp, int *region_countp,
504 char **region_propp, int *region_proplen)
506 char * const exc_prop[] = {"data"};
507 struct strlist node_inc;
508 struct image_region *region;
509 struct fdt_region fdt_regions[100];
510 const char *conf_name, *sig_name;
516 conf_name = fit_get_name(fit, conf_noffset, NULL);
517 sig_name = fit_get_name(fit, noffset, NULL);
518 debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
520 /* Get a list of nodes we want to hash */
521 ret = fit_config_get_hash_list(fit, conf_noffset, noffset, &node_inc);
525 /* Get a list of regions to hash */
526 count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
527 exc_prop, ARRAY_SIZE(exc_prop),
528 fdt_regions, ARRAY_SIZE(fdt_regions),
529 path, sizeof(path), 1);
531 printf("Failed to hash configuration '%s/%s': %s\n", conf_name,
532 sig_name, fdt_strerror(ret));
536 printf("No data to hash for configuration '%s/%s': %s\n",
537 conf_name, sig_name, fdt_strerror(ret));
541 /* Build our list of data blocks */
542 region = fit_region_make_list(fit, fdt_regions, count, NULL);
544 printf("Out of memory hashing configuration '%s/%s'\n",
545 conf_name, sig_name);
549 /* Create a list of all hashed properties */
550 debug("Hash nodes:\n");
551 for (i = len = 0; i < node_inc.count; i++) {
552 debug(" %s\n", node_inc.strings[i]);
553 len += strlen(node_inc.strings[i]) + 1;
555 region_prop = malloc(len);
557 printf("Out of memory setting up regions for configuration '%s/%s'\n",
558 conf_name, sig_name);
561 for (i = len = 0; i < node_inc.count;
562 len += strlen(node_inc.strings[i]) + 1, i++)
563 strcpy(region_prop + len, node_inc.strings[i]);
564 strlist_free(&node_inc);
566 *region_countp = count;
568 *region_propp = region_prop;
569 *region_proplen = len;
574 static int fit_config_process_sig(const char *keydir, void *keydest,
575 void *fit, const char *conf_name, int conf_noffset,
576 int noffset, const char *comment, int require_keys,
577 const char *engine_id)
579 struct image_sign_info info;
580 const char *node_name;
581 struct image_region *region;
589 node_name = fit_get_name(fit, noffset, NULL);
590 if (fit_config_get_data(fit, conf_noffset, noffset, ®ion,
591 ®ion_count, ®ion_prop, ®ion_proplen))
594 if (fit_image_setup_sig(&info, keydir, fit, conf_name, noffset,
595 require_keys ? "conf" : NULL, engine_id))
598 ret = info.crypto->sign(&info, region, region_count, &value,
602 printf("Failed to sign '%s' signature node in '%s' conf node\n",
603 node_name, conf_name);
605 /* We allow keys to be missing */
611 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
612 region_prop, region_proplen);
614 if (ret == -FDT_ERR_NOSPACE)
616 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
617 node_name, conf_name, fdt_strerror(ret));
623 /* Get keyname again, as FDT has changed and invalidated our pointer */
624 info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
626 /* Write the public key into the supplied FDT file */
628 ret = info.crypto->add_verify_data(&info, keydest);
630 printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n",
631 node_name, conf_name);
639 static int fit_config_add_verification_data(const char *keydir, void *keydest,
640 void *fit, int conf_noffset, const char *comment,
641 int require_keys, const char *engine_id)
643 const char *conf_name;
646 conf_name = fit_get_name(fit, conf_noffset, NULL);
648 /* Process all hash subnodes of the configuration node */
649 for (noffset = fdt_first_subnode(fit, conf_noffset);
651 noffset = fdt_next_subnode(fit, noffset)) {
652 const char *node_name;
655 node_name = fit_get_name(fit, noffset, NULL);
656 if (!strncmp(node_name, FIT_SIG_NODENAME,
657 strlen(FIT_SIG_NODENAME))) {
658 ret = fit_config_process_sig(keydir, keydest,
659 fit, conf_name, conf_noffset, noffset, comment,
660 require_keys, engine_id);
669 int fit_add_verification_data(const char *keydir, void *keydest, void *fit,
670 const char *comment, int require_keys,
671 const char *engine_id)
673 int images_noffset, confs_noffset;
677 /* Find images parent node offset */
678 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
679 if (images_noffset < 0) {
680 printf("Can't find images parent node '%s' (%s)\n",
681 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
682 return images_noffset;
685 /* Process its subnodes, print out component images details */
686 for (noffset = fdt_first_subnode(fit, images_noffset);
688 noffset = fdt_next_subnode(fit, noffset)) {
690 * Direct child node of the images parent node,
691 * i.e. component image node.
693 ret = fit_image_add_verification_data(keydir, keydest,
694 fit, noffset, comment, require_keys, engine_id);
699 /* If there are no keys, we can't sign configurations */
700 if (!IMAGE_ENABLE_SIGN || !keydir)
703 /* Find configurations parent node offset */
704 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
705 if (confs_noffset < 0) {
706 printf("Can't find images parent node '%s' (%s)\n",
707 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
711 /* Process its subnodes, print out component images details */
712 for (noffset = fdt_first_subnode(fit, confs_noffset);
714 noffset = fdt_next_subnode(fit, noffset)) {
715 ret = fit_config_add_verification_data(keydir, keydest,
716 fit, noffset, comment,
726 #ifdef CONFIG_FIT_SIGNATURE
727 int fit_check_sign(const void *fit, const void *key)
732 cfg_noffset = fit_conf_get_node(fit, NULL);
736 printf("Verifying Hash Integrity ... ");
737 ret = fit_config_verify(fit, cfg_noffset);
740 ret = bootm_host_load_images(fit, cfg_noffset);