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 int required_keynode, char **err_msgp)
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 = gd_fdt_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, int required_keynode, char **err_msgp)
109 struct image_sign_info info;
110 struct image_region region;
115 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
119 if (fit_image_hash_get_value(fit, noffset, &fit_value,
121 *err_msgp = "Can't get hash value property";
128 if (info.crypto->verify(&info, ®ion, 1, fit_value, fit_value_len)) {
129 *err_msgp = "Verification failed";
136 static int fit_image_verify_sig(const void *fit, int image_noffset,
137 const char *data, size_t size,
138 const void *key_blob, int key_offset)
145 /* Process all hash subnodes of the component image node */
146 fdt_for_each_subnode(noffset, fit, image_noffset) {
147 const char *name = fit_get_name(fit, noffset, NULL);
150 * We don't support this since libfdt considers names with the
151 * name root but different @ suffix to be equal
153 if (strchr(name, '@')) {
154 err_msg = "Node name contains @";
157 if (!strncmp(name, FIT_SIG_NODENAME,
158 strlen(FIT_SIG_NODENAME))) {
159 ret = fit_image_check_sig(fit, noffset, data,
171 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
172 err_msg = "Corrupted or truncated tree";
176 return verified ? 0 : -EPERM;
179 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
180 err_msg, fit_get_name(fit, noffset, NULL),
181 fit_get_name(fit, image_noffset, NULL));
185 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
186 const char *data, size_t size,
187 const void *key_blob, int *no_sigsp)
189 int verify_count = 0;
193 /* Work out what we need to verify */
195 key_node = fdt_subnode_offset(key_blob, 0, FIT_SIG_NODENAME);
197 debug("%s: No signature node found: %s\n", __func__,
198 fdt_strerror(key_node));
202 fdt_for_each_subnode(noffset, key_blob, key_node) {
203 const char *required;
206 required = fdt_getprop(key_blob, noffset, FIT_KEY_REQUIRED,
208 if (!required || strcmp(required, "image"))
210 ret = fit_image_verify_sig(fit, image_noffset, data, size,
213 printf("Failed to verify required signature '%s'\n",
214 fit_get_name(key_blob, noffset, NULL));
227 * fit_config_check_sig() - Check the signature of a config
229 * Here we are looking at a particular signature that needs verification (here
233 * default = "conf-1";
235 * kernel = "kernel-1";
238 * algo = "sha1,rsa2048";
239 * value = <...conf 1 signature...>;
244 * @noffset: Offset of the signature node being checked (e.g.
245 * /configurations/conf-1/signature-1)
246 * @conf_noffset: Offset of configuration node (e.g. /configurations/conf-1)
247 * @required_keynode: Offset in @key_blob of the required key node,
248 * if any. If this is given, then the configuration wil not
249 * pass verification unless that key is used. If this is
250 * -1 then any signature will do.
251 * @err_msgp: In the event of an error, this will be pointed to a
252 * help error string to display to the user.
253 * Return: 0 if all verified ok, <0 on error
255 static int fit_config_check_sig(const void *fit, int noffset, int conf_noffset,
256 int required_keynode, char **err_msgp)
258 static char * const exc_prop[] = {
265 const char *prop, *end, *name;
266 struct image_sign_info info;
267 const uint32_t *strings;
268 const char *config_name;
277 config_name = fit_get_name(fit, conf_noffset, NULL);
278 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
279 fit_get_name(fit, noffset, NULL),
280 fit_get_name(gd_fdt_blob(), required_keynode, NULL));
282 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
286 if (fit_image_hash_get_value(fit, noffset, &fit_value,
288 *err_msgp = "Can't get hash value property";
292 /* Count the number of strings in the property */
293 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
294 end = prop ? prop + prop_len : prop;
295 for (name = prop, count = 0; name < end; name++)
299 *err_msgp = "Can't get hashed-nodes property";
303 if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
304 *err_msgp = "hashed-nodes property must be null-terminated";
308 /* Add a sanity check here since we are using the stack */
309 if (count > IMAGE_MAX_HASHED_NODES) {
310 *err_msgp = "Number of hashed nodes exceeds maximum";
314 /* Create a list of node names from those strings */
315 char *node_inc[count];
317 debug("Hash nodes (%d):\n", count);
318 found_config = false;
319 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
320 debug(" '%s'\n", name);
321 node_inc[i] = (char *)name;
322 if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) &&
323 name[sizeof(FIT_CONFS_PATH) - 1] == '/' &&
324 !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) {
325 debug(" (found config node %s)", config_name);
330 *err_msgp = "Selected config not in hashed nodes";
335 * Each node can generate one region for each sub-node. Allow for
336 * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
338 max_regions = 20 + count * 7;
339 struct fdt_region fdt_regions[max_regions];
341 /* Get a list of regions to hash */
342 count = fdt_find_regions(fit, node_inc, count,
343 exc_prop, ARRAY_SIZE(exc_prop),
344 fdt_regions, max_regions - 1,
345 path, sizeof(path), 0);
347 *err_msgp = "Failed to hash configuration";
351 *err_msgp = "No data to hash";
354 if (count >= max_regions - 1) {
355 *err_msgp = "Too many hash regions";
359 /* Add the strings */
360 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
363 * The strings region offset must be a static 0x0.
364 * This is set in tool/image-host.c
366 fdt_regions[count].offset = fdt_off_dt_strings(fit);
367 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
371 /* Allocate the region list on the stack */
372 struct image_region region[count];
374 fit_region_make_list(fit, fdt_regions, count, region);
375 if (info.crypto->verify(&info, region, count, fit_value,
377 *err_msgp = "Verification failed";
385 * fit_config_verify_key() - Verify that a configuration is signed with a key
387 * Here we are looking at a particular configuration that needs verification:
390 * default = "conf-1";
392 * kernel = "kernel-1";
395 * algo = "sha1,rsa2048";
396 * value = <...conf 1 signature...>;
400 * We must check each of the signature subnodes of conf-1. Hopefully one of them
401 * will match the key at key_offset.
404 * @conf_noffset: Offset of the configuration node to check (e.g.
405 * /configurations/conf-1)
406 * @key_blob: Blob containing the keys to check against
407 * @key_offset: Offset of the key to check within @key_blob
408 * @return 0 if OK, -EPERM if any signatures did not verify, or the
409 * configuration node has an invalid name
411 static int fit_config_verify_key(const void *fit, int conf_noffset,
412 const void *key_blob, int key_offset)
415 char *err_msg = "No 'signature' subnode found";
419 /* Process all hash subnodes of the component conf node */
420 fdt_for_each_subnode(noffset, fit, conf_noffset) {
421 const char *name = fit_get_name(fit, noffset, NULL);
423 if (!strncmp(name, FIT_SIG_NODENAME,
424 strlen(FIT_SIG_NODENAME))) {
425 ret = fit_config_check_sig(fit, noffset, conf_noffset,
426 key_offset, &err_msg);
437 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
438 err_msg = "Corrupted or truncated tree";
446 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
447 err_msg, fit_get_name(fit, noffset, NULL),
448 fit_get_name(fit, conf_noffset, NULL));
453 * fit_config_verify_required_keys() - verify any required signatures for config
455 * This looks through all the signatures we expect and verifies that at least
456 * all the required ones are valid signatures for the configuration
459 * @conf_noffset: Offset of the configuration node to check (e.g.
460 * /configurations/conf-1)
461 * @key_blob: Blob containing the keys to check against
462 * @return 0 if OK, -EPERM if any signatures did not verify, or the
463 * configuration node has an invalid name
465 static int fit_config_verify_required_keys(const void *fit, int conf_noffset,
466 const void *key_blob)
468 const char *name = fit_get_name(fit, conf_noffset, NULL);
473 bool reqd_policy_all = true;
474 const char *reqd_mode;
477 * We don't support this since libfdt considers names with the
478 * name root but different @ suffix to be equal
480 if (strchr(name, '@')) {
481 printf("Configuration node '%s' contains '@'\n", name);
485 /* Work out what we need to verify */
486 key_node = fdt_subnode_offset(key_blob, 0, FIT_SIG_NODENAME);
488 debug("%s: No signature node found: %s\n", __func__,
489 fdt_strerror(key_node));
493 /* Get required-mode policy property from DTB */
494 reqd_mode = fdt_getprop(key_blob, key_node, "required-mode", NULL);
495 if (reqd_mode && !strcmp(reqd_mode, "any"))
496 reqd_policy_all = false;
498 debug("%s: required-mode policy set to '%s'\n", __func__,
499 reqd_policy_all ? "all" : "any");
502 * The algorithm here is a little convoluted due to how we want it to
503 * work. Here we work through each of the signature nodes in the
504 * public-key area. These are in the U-Boot control devicetree. Each
505 * node was created by signing a configuration, so we check if it is
506 * 'required' and if so, request that it be verified.
508 fdt_for_each_subnode(noffset, key_blob, key_node) {
509 const char *required;
512 required = fdt_getprop(key_blob, noffset, FIT_KEY_REQUIRED,
514 if (!required || strcmp(required, "conf"))
519 ret = fit_config_verify_key(fit, conf_noffset, key_blob,
522 if (reqd_policy_all) {
523 printf("Failed to verify required signature '%s'\n",
524 fit_get_name(key_blob, noffset, NULL));
529 if (!reqd_policy_all)
534 if (reqd_sigs && !verified) {
535 printf("Failed to verify 'any' of the required signature(s)\n");
542 int fit_config_verify(const void *fit, int conf_noffset)
544 return fit_config_verify_required_keys(fit, conf_noffset,