1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013, Google Inc.
12 #include <asm/global_data.h>
13 DECLARE_GLOBAL_DATA_PTR;
14 #endif /* !USE_HOSTCC*/
15 #include <fdt_region.h>
17 #include <u-boot/rsa.h>
18 #include <u-boot/hash-checksum.h>
20 #define IMAGE_MAX_HASHED_NODES 100
23 * fit_region_make_list() - Make a list of image regions
25 * Given a list of fdt_regions, create a list of image_regions. This is a
26 * simple conversion routine since the FDT and image code use different
30 * @fdt_regions: Pointer to FDT regions
31 * @count: Number of FDT regions
32 * @region: Pointer to image regions, which must hold @count records. If
33 * region is NULL, then (except for an SPL build) the array will be
35 * @return: Pointer to image regions
37 struct image_region *fit_region_make_list(const void *fit,
38 struct fdt_region *fdt_regions,
40 struct image_region *region)
44 debug("Hash regions:\n");
45 debug("%10s %10s\n", "Offset", "Size");
48 * Use malloc() except in SPL (to save code size). In SPL the caller
49 * must allocate the array.
51 if (!IS_ENABLED(CONFIG_SPL_BUILD) && !region)
52 region = calloc(sizeof(*region), count);
55 for (i = 0; i < count; i++) {
56 debug("%10x %10x\n", fdt_regions[i].offset,
58 region[i].data = fit + fdt_regions[i].offset;
59 region[i].size = fdt_regions[i].size;
65 static int fit_image_setup_verify(struct image_sign_info *info,
66 const void *fit, int noffset,
67 const void *key_blob, int required_keynode,
70 const char *algo_name;
71 const char *padding_name;
73 if (fdt_totalsize(fit) > CONFIG_VAL(FIT_SIGNATURE_MAX_SIZE)) {
74 *err_msgp = "Total size too large";
77 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
78 *err_msgp = "Can't get hash algo property";
82 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
84 padding_name = RSA_DEFAULT_PADDING_NAME;
86 memset(info, '\0', sizeof(*info));
87 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
89 info->node_offset = noffset;
90 info->name = algo_name;
91 info->checksum = image_get_checksum_algo(algo_name);
92 info->crypto = image_get_crypto_algo(algo_name);
93 info->padding = image_get_padding_algo(padding_name);
94 info->fdt_blob = key_blob;
95 info->required_keynode = required_keynode;
96 printf("%s:%s", algo_name, info->keyname);
98 if (!info->checksum || !info->crypto || !info->padding) {
99 *err_msgp = "Unknown signature algorithm";
106 int fit_image_check_sig(const void *fit, int noffset, const void *data,
107 size_t size, const void *key_blob, int required_keynode,
110 struct image_sign_info info;
111 struct image_region region;
116 if (fit_image_setup_verify(&info, fit, noffset, key_blob,
117 required_keynode, err_msgp))
120 if (fit_image_hash_get_value(fit, noffset, &fit_value,
122 *err_msgp = "Can't get hash value property";
129 if (info.crypto->verify(&info, ®ion, 1, fit_value, fit_value_len)) {
130 *err_msgp = "Verification failed";
137 static int fit_image_verify_sig(const void *fit, int image_noffset,
138 const char *data, size_t size,
139 const void *key_blob, int key_offset)
146 /* Process all hash subnodes of the component image node */
147 fdt_for_each_subnode(noffset, fit, image_noffset) {
148 const char *name = fit_get_name(fit, noffset, NULL);
151 * We don't support this since libfdt considers names with the
152 * name root but different @ suffix to be equal
154 if (strchr(name, '@')) {
155 err_msg = "Node name contains @";
158 if (!strncmp(name, FIT_SIG_NODENAME,
159 strlen(FIT_SIG_NODENAME))) {
160 ret = fit_image_check_sig(fit, noffset, data, size,
161 key_blob, -1, &err_msg);
172 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
173 err_msg = "Corrupted or truncated tree";
177 return verified ? 0 : -EPERM;
180 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
181 err_msg, fit_get_name(fit, noffset, NULL),
182 fit_get_name(fit, image_noffset, NULL));
186 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
187 const char *data, size_t size,
188 const void *key_blob, int *no_sigsp)
190 int verify_count = 0;
194 /* Work out what we need to verify */
196 key_node = fdt_subnode_offset(key_blob, 0, FIT_SIG_NODENAME);
198 debug("%s: No signature node found: %s\n", __func__,
199 fdt_strerror(key_node));
203 fdt_for_each_subnode(noffset, key_blob, key_node) {
204 const char *required;
207 required = fdt_getprop(key_blob, noffset, FIT_KEY_REQUIRED,
209 if (!required || strcmp(required, "image"))
211 ret = fit_image_verify_sig(fit, image_noffset, data, size,
214 printf("Failed to verify required signature '%s'\n",
215 fit_get_name(key_blob, noffset, NULL));
228 * fit_config_check_sig() - Check the signature of a config
230 * Here we are looking at a particular signature that needs verification (here
234 * default = "conf-1";
236 * kernel = "kernel-1";
239 * algo = "sha1,rsa2048";
240 * value = <...conf 1 signature...>;
245 * @noffset: Offset of the signature node being checked (e.g.
246 * /configurations/conf-1/signature-1)
247 * @conf_noffset: Offset of configuration node (e.g. /configurations/conf-1)
248 * @key_blob: Blob containing the keys to check against
249 * @required_keynode: Offset in @key_blob of the required key node,
250 * if any. If this is given, then the configuration wil not
251 * pass verification unless that key is used. If this is
252 * -1 then any signature will do.
253 * @err_msgp: In the event of an error, this will be pointed to a
254 * help error string to display to the user.
255 * Return: 0 if all verified ok, <0 on error
257 static int fit_config_check_sig(const void *fit, int noffset, int conf_noffset,
258 const void *key_blob, int required_keynode,
261 static char * const exc_prop[] = {
264 FIT_DATA_POSITION_PROP,
265 FIT_DATA_OFFSET_PROP,
268 const char *prop, *end, *name;
269 struct image_sign_info info;
270 const uint32_t *strings;
271 const char *config_name;
280 config_name = fit_get_name(fit, conf_noffset, NULL);
281 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, key_blob,
282 fit_get_name(fit, noffset, NULL),
283 fit_get_name(key_blob, required_keynode, NULL));
285 if (fit_image_setup_verify(&info, fit, noffset, key_blob,
286 required_keynode, err_msgp))
289 if (fit_image_hash_get_value(fit, noffset, &fit_value,
291 *err_msgp = "Can't get hash value property";
295 /* Count the number of strings in the property */
296 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
297 end = prop ? prop + prop_len : prop;
298 for (name = prop, count = 0; name < end; name++)
302 *err_msgp = "Can't get hashed-nodes property";
306 if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
307 *err_msgp = "hashed-nodes property must be null-terminated";
311 /* Add a sanity check here since we are using the stack */
312 if (count > IMAGE_MAX_HASHED_NODES) {
313 *err_msgp = "Number of hashed nodes exceeds maximum";
317 /* Create a list of node names from those strings */
318 char *node_inc[count];
320 debug("Hash nodes (%d):\n", count);
321 found_config = false;
322 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
323 debug(" '%s'\n", name);
324 node_inc[i] = (char *)name;
325 if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) &&
326 name[sizeof(FIT_CONFS_PATH) - 1] == '/' &&
327 !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) {
328 debug(" (found config node %s)", config_name);
333 *err_msgp = "Selected config not in hashed nodes";
338 * Each node can generate one region for each sub-node. Allow for
339 * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
341 max_regions = 20 + count * 7;
342 struct fdt_region fdt_regions[max_regions];
344 /* Get a list of regions to hash */
345 count = fdt_find_regions(fit, node_inc, count,
346 exc_prop, ARRAY_SIZE(exc_prop),
347 fdt_regions, max_regions - 1,
348 path, sizeof(path), 0);
350 *err_msgp = "Failed to hash configuration";
354 *err_msgp = "No data to hash";
357 if (count >= max_regions - 1) {
358 *err_msgp = "Too many hash regions";
362 /* Add the strings */
363 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
366 * The strings region offset must be a static 0x0.
367 * This is set in tool/image-host.c
369 fdt_regions[count].offset = fdt_off_dt_strings(fit);
370 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
374 /* Allocate the region list on the stack */
375 struct image_region region[count];
377 fit_region_make_list(fit, fdt_regions, count, region);
378 if (info.crypto->verify(&info, region, count, fit_value,
380 *err_msgp = "Verification failed";
388 * fit_config_verify_key() - Verify that a configuration is signed with a key
390 * Here we are looking at a particular configuration that needs verification:
393 * default = "conf-1";
395 * kernel = "kernel-1";
398 * algo = "sha1,rsa2048";
399 * value = <...conf 1 signature...>;
403 * We must check each of the signature subnodes of conf-1. Hopefully one of them
404 * will match the key at key_offset.
407 * @conf_noffset: Offset of the configuration node to check (e.g.
408 * /configurations/conf-1)
409 * @key_blob: Blob containing the keys to check against
410 * @key_offset: Offset of the key to check within @key_blob
411 * @return 0 if OK, -EPERM if any signatures did not verify, or the
412 * configuration node has an invalid name
414 static int fit_config_verify_key(const void *fit, int conf_noffset,
415 const void *key_blob, int key_offset)
418 char *err_msg = "No 'signature' subnode found";
422 /* Process all hash subnodes of the component conf node */
423 fdt_for_each_subnode(noffset, fit, conf_noffset) {
424 const char *name = fit_get_name(fit, noffset, NULL);
426 if (!strncmp(name, FIT_SIG_NODENAME,
427 strlen(FIT_SIG_NODENAME))) {
428 ret = fit_config_check_sig(fit, noffset, conf_noffset,
429 key_blob, key_offset,
441 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
442 err_msg = "Corrupted or truncated tree";
450 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
451 err_msg, fit_get_name(fit, noffset, NULL),
452 fit_get_name(fit, conf_noffset, NULL));
457 * fit_config_verify_required_keys() - verify any required signatures for config
459 * This looks through all the signatures we expect and verifies that at least
460 * all the required ones are valid signatures for the configuration
463 * @conf_noffset: Offset of the configuration node to check (e.g.
464 * /configurations/conf-1)
465 * @key_blob: Blob containing the keys to check against
466 * @return 0 if OK, -EPERM if any signatures did not verify, or the
467 * configuration node has an invalid name
469 static int fit_config_verify_required_keys(const void *fit, int conf_noffset,
470 const void *key_blob)
472 const char *name = fit_get_name(fit, conf_noffset, NULL);
477 bool reqd_policy_all = true;
478 const char *reqd_mode;
481 * We don't support this since libfdt considers names with the
482 * name root but different @ suffix to be equal
484 if (strchr(name, '@')) {
485 printf("Configuration node '%s' contains '@'\n", name);
489 /* Work out what we need to verify */
490 key_node = fdt_subnode_offset(key_blob, 0, FIT_SIG_NODENAME);
492 debug("%s: No signature node found: %s\n", __func__,
493 fdt_strerror(key_node));
497 /* Get required-mode policy property from DTB */
498 reqd_mode = fdt_getprop(key_blob, key_node, "required-mode", NULL);
499 if (reqd_mode && !strcmp(reqd_mode, "any"))
500 reqd_policy_all = false;
502 debug("%s: required-mode policy set to '%s'\n", __func__,
503 reqd_policy_all ? "all" : "any");
506 * The algorithm here is a little convoluted due to how we want it to
507 * work. Here we work through each of the signature nodes in the
508 * public-key area. These are in the U-Boot control devicetree. Each
509 * node was created by signing a configuration, so we check if it is
510 * 'required' and if so, request that it be verified.
512 fdt_for_each_subnode(noffset, key_blob, key_node) {
513 const char *required;
516 required = fdt_getprop(key_blob, noffset, FIT_KEY_REQUIRED,
518 if (!required || strcmp(required, "conf"))
523 ret = fit_config_verify_key(fit, conf_noffset, key_blob,
526 if (reqd_policy_all) {
527 printf("Failed to verify required signature '%s'\n",
528 fit_get_name(key_blob, noffset, NULL));
533 if (!reqd_policy_all)
538 if (reqd_sigs && !verified) {
539 printf("Failed to verify 'any' of the required signature(s)\n");
546 int fit_config_verify(const void *fit, int conf_noffset)
548 return fit_config_verify_required_keys(fit, conf_noffset,