1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013, Google Inc.
13 DECLARE_GLOBAL_DATA_PTR;
14 #endif /* !USE_HOSTCC*/
15 #include <fdt_region.h>
17 #include <u-boot/rsa.h>
18 #include <u-boot/rsa-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 #ifndef CONFIG_SPL_BUILD
53 region = calloc(sizeof(*region), count);
57 for (i = 0; i < count; i++) {
58 debug("%10x %10x\n", fdt_regions[i].offset,
60 region[i].data = fit + fdt_regions[i].offset;
61 region[i].size = fdt_regions[i].size;
67 static int fit_image_setup_verify(struct image_sign_info *info,
68 const void *fit, int noffset,
69 int required_keynode, char **err_msgp)
72 const char *padding_name;
74 if (fdt_totalsize(fit) > CONFIG_FIT_SIGNATURE_MAX_SIZE) {
75 *err_msgp = "Total size too large";
79 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
80 *err_msgp = "Can't get hash algo property";
84 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
86 padding_name = RSA_DEFAULT_PADDING_NAME;
88 memset(info, '\0', sizeof(*info));
89 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
90 info->fit = (void *)fit;
91 info->node_offset = noffset;
92 info->name = algo_name;
93 info->checksum = image_get_checksum_algo(algo_name);
94 info->crypto = image_get_crypto_algo(algo_name);
95 info->padding = image_get_padding_algo(padding_name);
96 info->fdt_blob = gd_fdt_blob();
97 info->required_keynode = required_keynode;
98 printf("%s:%s", algo_name, info->keyname);
100 if (!info->checksum || !info->crypto || !info->padding) {
101 *err_msgp = "Unknown signature algorithm";
108 int fit_image_check_sig(const void *fit, int noffset, const void *data,
109 size_t size, int required_keynode, char **err_msgp)
111 struct image_sign_info info;
112 struct image_region region;
117 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
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 *sig_blob, int sig_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);
151 if (!strncmp(name, FIT_SIG_NODENAME,
152 strlen(FIT_SIG_NODENAME))) {
153 ret = fit_image_check_sig(fit, noffset, data,
165 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
166 err_msg = "Corrupted or truncated tree";
170 return verified ? 0 : -EPERM;
173 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
174 err_msg, fit_get_name(fit, noffset, NULL),
175 fit_get_name(fit, image_noffset, NULL));
179 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
180 const char *data, size_t size,
181 const void *sig_blob, int *no_sigsp)
183 int verify_count = 0;
187 /* Work out what we need to verify */
189 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
191 debug("%s: No signature node found: %s\n", __func__,
192 fdt_strerror(sig_node));
196 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
197 const char *required;
200 required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED,
202 if (!required || strcmp(required, "image"))
204 ret = fit_image_verify_sig(fit, image_noffset, data, size,
207 printf("Failed to verify required signature '%s'\n",
208 fit_get_name(sig_blob, noffset, NULL));
221 * fit_config_check_sig() - Check the signature of a config
224 * @noffset: Offset of configuration node (e.g. /configurations/conf-1)
225 * @required_keynode: Offset in the control FDT of the required key node,
226 * if any. If this is given, then the configuration wil not
227 * pass verification unless that key is used. If this is
228 * -1 then any signature will do.
229 * @conf_noffset: Offset of the configuration subnode being checked (e.g.
230 * /configurations/conf-1/kernel)
231 * @err_msgp: In the event of an error, this will be pointed to a
232 * help error string to display to the user.
233 * @return 0 if all verified ok, <0 on error
235 static int fit_config_check_sig(const void *fit, int noffset,
236 int required_keynode, int conf_noffset,
239 char * const exc_prop[] = {"data", "data-size", "data-position"};
240 const char *prop, *end, *name;
241 struct image_sign_info info;
242 const uint32_t *strings;
243 const char *config_name;
252 config_name = fit_get_name(fit, conf_noffset, NULL);
253 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
254 fit_get_name(fit, noffset, NULL),
255 fit_get_name(gd_fdt_blob(), required_keynode, NULL));
257 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
261 if (fit_image_hash_get_value(fit, noffset, &fit_value,
263 *err_msgp = "Can't get hash value property";
267 /* Count the number of strings in the property */
268 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
269 end = prop ? prop + prop_len : prop;
270 for (name = prop, count = 0; name < end; name++)
274 *err_msgp = "Can't get hashed-nodes property";
278 if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
279 *err_msgp = "hashed-nodes property must be null-terminated";
283 /* Add a sanity check here since we are using the stack */
284 if (count > IMAGE_MAX_HASHED_NODES) {
285 *err_msgp = "Number of hashed nodes exceeds maximum";
289 /* Create a list of node names from those strings */
290 char *node_inc[count];
292 debug("Hash nodes (%d):\n", count);
293 found_config = false;
294 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
295 debug(" '%s'\n", name);
296 node_inc[i] = (char *)name;
297 if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) &&
298 name[sizeof(FIT_CONFS_PATH) - 1] == '/' &&
299 !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) {
300 debug(" (found config node %s)", config_name);
305 *err_msgp = "Selected config not in hashed nodes";
310 * Each node can generate one region for each sub-node. Allow for
311 * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
313 max_regions = 20 + count * 7;
314 struct fdt_region fdt_regions[max_regions];
316 /* Get a list of regions to hash */
317 count = fdt_find_regions(fit, node_inc, count,
318 exc_prop, ARRAY_SIZE(exc_prop),
319 fdt_regions, max_regions - 1,
320 path, sizeof(path), 0);
322 *err_msgp = "Failed to hash configuration";
326 *err_msgp = "No data to hash";
329 if (count >= max_regions - 1) {
330 *err_msgp = "Too many hash regions";
334 /* Add the strings */
335 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
338 * The strings region offset must be a static 0x0.
339 * This is set in tool/image-host.c
341 fdt_regions[count].offset = fdt_off_dt_strings(fit);
342 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
346 /* Allocate the region list on the stack */
347 struct image_region region[count];
349 fit_region_make_list(fit, fdt_regions, count, region);
350 if (info.crypto->verify(&info, region, count, fit_value,
352 *err_msgp = "Verification failed";
359 static int fit_config_verify_sig(const void *fit, int conf_noffset,
360 const void *sig_blob, int sig_offset)
363 char *err_msg = "No 'signature' subnode found";
367 /* Process all hash subnodes of the component conf node */
368 fdt_for_each_subnode(noffset, fit, conf_noffset) {
369 const char *name = fit_get_name(fit, noffset, NULL);
371 if (!strncmp(name, FIT_SIG_NODENAME,
372 strlen(FIT_SIG_NODENAME))) {
373 ret = fit_config_check_sig(fit, noffset, sig_offset,
374 conf_noffset, &err_msg);
385 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
386 err_msg = "Corrupted or truncated tree";
394 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
395 err_msg, fit_get_name(fit, noffset, NULL),
396 fit_get_name(fit, conf_noffset, NULL));
400 int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
401 const void *sig_blob)
407 bool reqd_policy_all = true;
408 const char *reqd_mode;
410 /* Work out what we need to verify */
411 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
413 debug("%s: No signature node found: %s\n", __func__,
414 fdt_strerror(sig_node));
418 /* Get required-mode policy property from DTB */
419 reqd_mode = fdt_getprop(sig_blob, sig_node, "required-mode", NULL);
420 if (reqd_mode && !strcmp(reqd_mode, "any"))
421 reqd_policy_all = false;
423 debug("%s: required-mode policy set to '%s'\n", __func__,
424 reqd_policy_all ? "all" : "any");
426 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
427 const char *required;
430 required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED,
432 if (!required || strcmp(required, "conf"))
437 ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
440 if (reqd_policy_all) {
441 printf("Failed to verify required signature '%s'\n",
442 fit_get_name(sig_blob, noffset, NULL));
447 if (!reqd_policy_all)
452 if (reqd_sigs && !verified) {
453 printf("Failed to verify 'any' of the required signature(s)\n");
460 int fit_config_verify(const void *fit, int conf_noffset)
462 return fit_config_verify_required_sigs(fit, conf_noffset,