1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013, Google Inc.
13 #include <asm/global_data.h>
14 DECLARE_GLOBAL_DATA_PTR;
15 #endif /* !USE_HOSTCC*/
16 #include <fdt_region.h>
18 #include <u-boot/rsa.h>
19 #include <u-boot/hash-checksum.h>
21 #define IMAGE_MAX_HASHED_NODES 100
24 * fit_region_make_list() - Make a list of image regions
26 * Given a list of fdt_regions, create a list of image_regions. This is a
27 * simple conversion routine since the FDT and image code use different
31 * @fdt_regions: Pointer to FDT regions
32 * @count: Number of FDT regions
33 * @region: Pointer to image regions, which must hold @count records. If
34 * region is NULL, then (except for an SPL build) the array will be
36 * @return: Pointer to image regions
38 struct image_region *fit_region_make_list(const void *fit,
39 struct fdt_region *fdt_regions,
41 struct image_region *region)
45 debug("Hash regions:\n");
46 debug("%10s %10s\n", "Offset", "Size");
49 * Use malloc() except in SPL (to save code size). In SPL the caller
50 * must allocate the array.
52 if (!IS_ENABLED(CONFIG_SPL_BUILD) && !region)
53 region = calloc(sizeof(*region), count);
56 for (i = 0; i < count; i++) {
57 debug("%10x %10x\n", fdt_regions[i].offset,
59 region[i].data = fit + fdt_regions[i].offset;
60 region[i].size = fdt_regions[i].size;
66 static int fit_image_setup_verify(struct image_sign_info *info,
67 const void *fit, int noffset,
68 const void *key_blob, int required_keynode,
71 const char *algo_name;
72 const char *padding_name;
74 if (fdt_totalsize(fit) > CONFIG_VAL(FIT_SIGNATURE_MAX_SIZE)) {
75 *err_msgp = "Total size too large";
78 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
79 *err_msgp = "Can't get hash algo property";
83 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
85 padding_name = RSA_DEFAULT_PADDING_NAME;
87 memset(info, '\0', sizeof(*info));
88 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
90 info->node_offset = noffset;
91 info->name = algo_name;
92 info->checksum = image_get_checksum_algo(algo_name);
93 info->crypto = image_get_crypto_algo(algo_name);
94 info->padding = image_get_padding_algo(padding_name);
95 info->fdt_blob = key_blob;
96 info->required_keynode = required_keynode;
97 printf("%s:%s", algo_name, info->keyname);
99 if (!info->checksum || !info->crypto || !info->padding) {
100 *err_msgp = "Unknown signature algorithm";
107 int fit_image_check_sig(const void *fit, int noffset, const void *data,
108 size_t size, const void *key_blob, int required_keynode,
111 struct image_sign_info info;
112 struct image_region region;
117 if (fit_image_setup_verify(&info, fit, noffset, key_blob,
118 required_keynode, err_msgp))
121 if (fit_image_hash_get_value(fit, noffset, &fit_value,
123 *err_msgp = "Can't get hash value property";
130 if (info.crypto->verify(&info, ®ion, 1, fit_value, fit_value_len)) {
131 *err_msgp = "Verification failed";
138 static int fit_image_verify_sig(const void *fit, int image_noffset,
139 const char *data, size_t size,
140 const void *key_blob, int key_offset)
147 /* Process all hash subnodes of the component image node */
148 fdt_for_each_subnode(noffset, fit, image_noffset) {
149 const char *name = fit_get_name(fit, noffset, NULL);
152 * We don't support this since libfdt considers names with the
153 * name root but different @ suffix to be equal
155 if (strchr(name, '@')) {
156 err_msg = "Node name contains @";
159 if (!strncmp(name, FIT_SIG_NODENAME,
160 strlen(FIT_SIG_NODENAME))) {
161 ret = fit_image_check_sig(fit, noffset, data, size,
162 key_blob, -1, &err_msg);
173 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
174 err_msg = "Corrupted or truncated tree";
178 return verified ? 0 : -EPERM;
181 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
182 err_msg, fit_get_name(fit, noffset, NULL),
183 fit_get_name(fit, image_noffset, NULL));
187 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
188 const char *data, size_t size,
189 const void *key_blob, int *no_sigsp)
191 int verify_count = 0;
195 /* Work out what we need to verify */
197 key_node = fdt_subnode_offset(key_blob, 0, FIT_SIG_NODENAME);
199 debug("%s: No signature node found: %s\n", __func__,
200 fdt_strerror(key_node));
204 fdt_for_each_subnode(noffset, key_blob, key_node) {
205 const char *required;
208 required = fdt_getprop(key_blob, noffset, FIT_KEY_REQUIRED,
210 if (!required || strcmp(required, "image"))
212 ret = fit_image_verify_sig(fit, image_noffset, data, size,
215 printf("Failed to verify required signature '%s'\n",
216 fit_get_name(key_blob, noffset, NULL));
229 * fit_config_check_sig() - Check the signature of a config
231 * Here we are looking at a particular signature that needs verification (here
235 * default = "conf-1";
237 * kernel = "kernel-1";
240 * algo = "sha1,rsa2048";
241 * value = <...conf 1 signature...>;
246 * @noffset: Offset of the signature node being checked (e.g.
247 * /configurations/conf-1/signature-1)
248 * @conf_noffset: Offset of configuration node (e.g. /configurations/conf-1)
249 * @key_blob: Blob containing the keys to check against
250 * @required_keynode: Offset in @key_blob of the required key node,
251 * if any. If this is given, then the configuration wil not
252 * pass verification unless that key is used. If this is
253 * -1 then any signature will do.
254 * @err_msgp: In the event of an error, this will be pointed to a
255 * help error string to display to the user.
256 * Return: 0 if all verified ok, <0 on error
258 static int fit_config_check_sig(const void *fit, int noffset, int conf_noffset,
259 const void *key_blob, int required_keynode,
262 static char * const exc_prop[] = {
265 FIT_DATA_POSITION_PROP,
266 FIT_DATA_OFFSET_PROP,
269 const char *prop, *end, *name;
270 struct image_sign_info info;
271 const uint32_t *strings;
272 const char *config_name;
281 config_name = fit_get_name(fit, conf_noffset, NULL);
282 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, key_blob,
283 fit_get_name(fit, noffset, NULL),
284 fit_get_name(key_blob, required_keynode, NULL));
286 if (fit_image_setup_verify(&info, fit, noffset, key_blob,
287 required_keynode, err_msgp))
290 if (fit_image_hash_get_value(fit, noffset, &fit_value,
292 *err_msgp = "Can't get hash value property";
296 /* Count the number of strings in the property */
297 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
298 end = prop ? prop + prop_len : prop;
299 for (name = prop, count = 0; name < end; name++)
303 *err_msgp = "Can't get hashed-nodes property";
307 if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
308 *err_msgp = "hashed-nodes property must be null-terminated";
312 /* Add a sanity check here since we are using the stack */
313 if (count > IMAGE_MAX_HASHED_NODES) {
314 *err_msgp = "Number of hashed nodes exceeds maximum";
318 /* Create a list of node names from those strings */
319 char *node_inc[count];
321 debug("Hash nodes (%d):\n", count);
322 found_config = false;
323 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
324 debug(" '%s'\n", name);
325 node_inc[i] = (char *)name;
326 if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) &&
327 name[sizeof(FIT_CONFS_PATH) - 1] == '/' &&
328 !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) {
329 debug(" (found config node %s)", config_name);
334 *err_msgp = "Selected config not in hashed nodes";
339 * Each node can generate one region for each sub-node. Allow for
340 * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
342 max_regions = 20 + count * 7;
343 struct fdt_region fdt_regions[max_regions];
345 /* Get a list of regions to hash */
346 count = fdt_find_regions(fit, node_inc, count,
347 exc_prop, ARRAY_SIZE(exc_prop),
348 fdt_regions, max_regions - 1,
349 path, sizeof(path), 0);
351 *err_msgp = "Failed to hash configuration";
355 *err_msgp = "No data to hash";
358 if (count >= max_regions - 1) {
359 *err_msgp = "Too many hash regions";
363 /* Add the strings */
364 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
367 * The strings region offset must be a static 0x0.
368 * This is set in tool/image-host.c
370 fdt_regions[count].offset = fdt_off_dt_strings(fit);
371 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
375 /* Allocate the region list on the stack */
376 struct image_region region[count];
378 fit_region_make_list(fit, fdt_regions, count, region);
379 if (info.crypto->verify(&info, region, count, fit_value,
381 *err_msgp = "Verification failed";
389 * fit_config_verify_key() - Verify that a configuration is signed with a key
391 * Here we are looking at a particular configuration that needs verification:
394 * default = "conf-1";
396 * kernel = "kernel-1";
399 * algo = "sha1,rsa2048";
400 * value = <...conf 1 signature...>;
404 * We must check each of the signature subnodes of conf-1. Hopefully one of them
405 * will match the key at key_offset.
408 * @conf_noffset: Offset of the configuration node to check (e.g.
409 * /configurations/conf-1)
410 * @key_blob: Blob containing the keys to check against
411 * @key_offset: Offset of the key to check within @key_blob
412 * @return 0 if OK, -EPERM if any signatures did not verify, or the
413 * configuration node has an invalid name
415 static int fit_config_verify_key(const void *fit, int conf_noffset,
416 const void *key_blob, int key_offset)
419 char *err_msg = "No 'signature' subnode found";
423 /* Process all hash subnodes of the component conf node */
424 fdt_for_each_subnode(noffset, fit, conf_noffset) {
425 const char *name = fit_get_name(fit, noffset, NULL);
427 if (!strncmp(name, FIT_SIG_NODENAME,
428 strlen(FIT_SIG_NODENAME))) {
429 ret = fit_config_check_sig(fit, noffset, conf_noffset,
430 key_blob, key_offset,
442 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
443 err_msg = "Corrupted or truncated tree";
451 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
452 err_msg, fit_get_name(fit, noffset, NULL),
453 fit_get_name(fit, conf_noffset, NULL));
458 * fit_config_verify_required_keys() - verify any required signatures for config
460 * This looks through all the signatures we expect and verifies that at least
461 * all the required ones are valid signatures for the configuration
464 * @conf_noffset: Offset of the configuration node to check (e.g.
465 * /configurations/conf-1)
466 * @key_blob: Blob containing the keys to check against
467 * @return 0 if OK, -EPERM if any signatures did not verify, or the
468 * configuration node has an invalid name
470 static int fit_config_verify_required_keys(const void *fit, int conf_noffset,
471 const void *key_blob)
473 const char *name = fit_get_name(fit, conf_noffset, NULL);
478 bool reqd_policy_all = true;
479 const char *reqd_mode;
482 * We don't support this since libfdt considers names with the
483 * name root but different @ suffix to be equal
485 if (strchr(name, '@')) {
486 printf("Configuration node '%s' contains '@'\n", name);
490 /* Work out what we need to verify */
491 key_node = fdt_subnode_offset(key_blob, 0, FIT_SIG_NODENAME);
493 debug("%s: No signature node found: %s\n", __func__,
494 fdt_strerror(key_node));
498 /* Get required-mode policy property from DTB */
499 reqd_mode = fdt_getprop(key_blob, key_node, "required-mode", NULL);
500 if (reqd_mode && !strcmp(reqd_mode, "any"))
501 reqd_policy_all = false;
503 debug("%s: required-mode policy set to '%s'\n", __func__,
504 reqd_policy_all ? "all" : "any");
507 * The algorithm here is a little convoluted due to how we want it to
508 * work. Here we work through each of the signature nodes in the
509 * public-key area. These are in the U-Boot control devicetree. Each
510 * node was created by signing a configuration, so we check if it is
511 * 'required' and if so, request that it be verified.
513 fdt_for_each_subnode(noffset, key_blob, key_node) {
514 const char *required;
517 required = fdt_getprop(key_blob, noffset, FIT_KEY_REQUIRED,
519 if (!required || strcmp(required, "conf"))
524 ret = fit_config_verify_key(fit, conf_noffset, key_blob,
527 if (reqd_policy_all) {
528 printf("Failed to verify required signature '%s'\n",
529 fit_get_name(key_blob, noffset, NULL));
534 if (!reqd_policy_all)
539 if (reqd_sigs && !verified) {
540 printf("Failed to verify 'any' of the required signature(s)\n");
547 int fit_config_verify(const void *fit, int conf_noffset)
549 return fit_config_verify_required_keys(fit, conf_noffset,