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 #ifndef CONFIG_SPL_BUILD
54 region = calloc(sizeof(*region), count);
58 for (i = 0; i < count; i++) {
59 debug("%10x %10x\n", fdt_regions[i].offset,
61 region[i].data = fit + fdt_regions[i].offset;
62 region[i].size = fdt_regions[i].size;
68 static int fit_image_setup_verify(struct image_sign_info *info,
69 const void *fit, int noffset,
70 int required_keynode, char **err_msgp)
73 const char *padding_name;
75 if (fdt_totalsize(fit) > CONFIG_FIT_SIGNATURE_MAX_SIZE) {
76 *err_msgp = "Total size too large";
80 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
81 *err_msgp = "Can't get hash algo property";
85 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
87 padding_name = RSA_DEFAULT_PADDING_NAME;
89 memset(info, '\0', sizeof(*info));
90 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
91 info->fit = (void *)fit;
92 info->node_offset = noffset;
93 info->name = algo_name;
94 info->checksum = image_get_checksum_algo(algo_name);
95 info->crypto = image_get_crypto_algo(algo_name);
96 info->padding = image_get_padding_algo(padding_name);
97 info->fdt_blob = gd_fdt_blob();
98 info->required_keynode = required_keynode;
99 printf("%s:%s", algo_name, info->keyname);
101 if (!info->checksum || !info->crypto || !info->padding) {
102 *err_msgp = "Unknown signature algorithm";
109 int fit_image_check_sig(const void *fit, int noffset, const void *data,
110 size_t size, int required_keynode, char **err_msgp)
112 struct image_sign_info info;
113 struct image_region region;
118 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
122 if (fit_image_hash_get_value(fit, noffset, &fit_value,
124 *err_msgp = "Can't get hash value property";
131 if (info.crypto->verify(&info, ®ion, 1, fit_value, fit_value_len)) {
132 *err_msgp = "Verification failed";
139 static int fit_image_verify_sig(const void *fit, int image_noffset,
140 const char *data, size_t size,
141 const void *sig_blob, int sig_offset)
148 /* Process all hash subnodes of the component image node */
149 fdt_for_each_subnode(noffset, fit, image_noffset) {
150 const char *name = fit_get_name(fit, noffset, NULL);
153 * We don't support this since libfdt considers names with the
154 * name root but different @ suffix to be equal
156 if (strchr(name, '@')) {
157 err_msg = "Node name contains @";
160 if (!strncmp(name, FIT_SIG_NODENAME,
161 strlen(FIT_SIG_NODENAME))) {
162 ret = fit_image_check_sig(fit, noffset, data,
174 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
175 err_msg = "Corrupted or truncated tree";
179 return verified ? 0 : -EPERM;
182 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
183 err_msg, fit_get_name(fit, noffset, NULL),
184 fit_get_name(fit, image_noffset, NULL));
188 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
189 const char *data, size_t size,
190 const void *sig_blob, int *no_sigsp)
192 int verify_count = 0;
196 /* Work out what we need to verify */
198 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
200 debug("%s: No signature node found: %s\n", __func__,
201 fdt_strerror(sig_node));
205 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
206 const char *required;
209 required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED,
211 if (!required || strcmp(required, "image"))
213 ret = fit_image_verify_sig(fit, image_noffset, data, size,
216 printf("Failed to verify required signature '%s'\n",
217 fit_get_name(sig_blob, noffset, NULL));
230 * fit_config_check_sig() - Check the signature of a config
233 * @noffset: Offset of configuration node (e.g. /configurations/conf-1)
234 * @required_keynode: Offset in the control FDT of the required key node,
235 * if any. If this is given, then the configuration wil not
236 * pass verification unless that key is used. If this is
237 * -1 then any signature will do.
238 * @conf_noffset: Offset of the configuration subnode being checked (e.g.
239 * /configurations/conf-1/kernel)
240 * @err_msgp: In the event of an error, this will be pointed to a
241 * help error string to display to the user.
242 * @return 0 if all verified ok, <0 on error
244 static int fit_config_check_sig(const void *fit, int noffset,
245 int required_keynode, int conf_noffset,
248 static char * const exc_prop[] = {
255 const char *prop, *end, *name;
256 struct image_sign_info info;
257 const uint32_t *strings;
258 const char *config_name;
267 config_name = fit_get_name(fit, conf_noffset, NULL);
268 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
269 fit_get_name(fit, noffset, NULL),
270 fit_get_name(gd_fdt_blob(), required_keynode, NULL));
272 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
276 if (fit_image_hash_get_value(fit, noffset, &fit_value,
278 *err_msgp = "Can't get hash value property";
282 /* Count the number of strings in the property */
283 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
284 end = prop ? prop + prop_len : prop;
285 for (name = prop, count = 0; name < end; name++)
289 *err_msgp = "Can't get hashed-nodes property";
293 if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
294 *err_msgp = "hashed-nodes property must be null-terminated";
298 /* Add a sanity check here since we are using the stack */
299 if (count > IMAGE_MAX_HASHED_NODES) {
300 *err_msgp = "Number of hashed nodes exceeds maximum";
304 /* Create a list of node names from those strings */
305 char *node_inc[count];
307 debug("Hash nodes (%d):\n", count);
308 found_config = false;
309 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
310 debug(" '%s'\n", name);
311 node_inc[i] = (char *)name;
312 if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) &&
313 name[sizeof(FIT_CONFS_PATH) - 1] == '/' &&
314 !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) {
315 debug(" (found config node %s)", config_name);
320 *err_msgp = "Selected config not in hashed nodes";
325 * Each node can generate one region for each sub-node. Allow for
326 * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
328 max_regions = 20 + count * 7;
329 struct fdt_region fdt_regions[max_regions];
331 /* Get a list of regions to hash */
332 count = fdt_find_regions(fit, node_inc, count,
333 exc_prop, ARRAY_SIZE(exc_prop),
334 fdt_regions, max_regions - 1,
335 path, sizeof(path), 0);
337 *err_msgp = "Failed to hash configuration";
341 *err_msgp = "No data to hash";
344 if (count >= max_regions - 1) {
345 *err_msgp = "Too many hash regions";
349 /* Add the strings */
350 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
353 * The strings region offset must be a static 0x0.
354 * This is set in tool/image-host.c
356 fdt_regions[count].offset = fdt_off_dt_strings(fit);
357 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
361 /* Allocate the region list on the stack */
362 struct image_region region[count];
364 fit_region_make_list(fit, fdt_regions, count, region);
365 if (info.crypto->verify(&info, region, count, fit_value,
367 *err_msgp = "Verification failed";
374 static int fit_config_verify_sig(const void *fit, int conf_noffset,
375 const void *sig_blob, int sig_offset)
378 char *err_msg = "No 'signature' subnode found";
382 /* Process all hash subnodes of the component conf node */
383 fdt_for_each_subnode(noffset, fit, conf_noffset) {
384 const char *name = fit_get_name(fit, noffset, NULL);
386 if (!strncmp(name, FIT_SIG_NODENAME,
387 strlen(FIT_SIG_NODENAME))) {
388 ret = fit_config_check_sig(fit, noffset, sig_offset,
389 conf_noffset, &err_msg);
400 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
401 err_msg = "Corrupted or truncated tree";
409 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
410 err_msg, fit_get_name(fit, noffset, NULL),
411 fit_get_name(fit, conf_noffset, NULL));
415 static int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
416 const void *sig_blob)
418 const char *name = fit_get_name(fit, conf_noffset, NULL);
423 bool reqd_policy_all = true;
424 const char *reqd_mode;
427 * We don't support this since libfdt considers names with the
428 * name root but different @ suffix to be equal
430 if (strchr(name, '@')) {
431 printf("Configuration node '%s' contains '@'\n", name);
435 /* Work out what we need to verify */
436 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
438 debug("%s: No signature node found: %s\n", __func__,
439 fdt_strerror(sig_node));
443 /* Get required-mode policy property from DTB */
444 reqd_mode = fdt_getprop(sig_blob, sig_node, "required-mode", NULL);
445 if (reqd_mode && !strcmp(reqd_mode, "any"))
446 reqd_policy_all = false;
448 debug("%s: required-mode policy set to '%s'\n", __func__,
449 reqd_policy_all ? "all" : "any");
451 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
452 const char *required;
455 required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED,
457 if (!required || strcmp(required, "conf"))
462 ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
465 if (reqd_policy_all) {
466 printf("Failed to verify required signature '%s'\n",
467 fit_get_name(sig_blob, noffset, NULL));
472 if (!reqd_policy_all)
477 if (reqd_sigs && !verified) {
478 printf("Failed to verify 'any' of the required signature(s)\n");
485 int fit_config_verify(const void *fit, int conf_noffset)
487 return fit_config_verify_required_sigs(fit, conf_noffset,