2 * Copyright (c) 2013, Google Inc.
4 * SPDX-License-Identifier: GPL-2.0+
13 DECLARE_GLOBAL_DATA_PTR;
14 #endif /* !USE_HOSTCC*/
18 #define IMAGE_MAX_HASHED_NODES 100
20 struct image_sig_algo image_sig_algos[] = {
29 struct image_sig_algo *image_get_sig_algo(const char *name)
33 for (i = 0; i < ARRAY_SIZE(image_sig_algos); i++) {
34 if (!strcmp(image_sig_algos[i].name, name))
35 return &image_sig_algos[i];
42 * fit_region_make_list() - Make a list of image regions
44 * Given a list of fdt_regions, create a list of image_regions. This is a
45 * simple conversion routine since the FDT and image code use different
49 * @fdt_regions: Pointer to FDT regions
50 * @count: Number of FDT regions
51 * @region: Pointer to image regions, which must hold @count records. If
52 * region is NULL, then (except for an SPL build) the array will be
54 * @return: Pointer to image regions
56 struct image_region *fit_region_make_list(const void *fit,
57 struct fdt_region *fdt_regions, int count,
58 struct image_region *region)
62 debug("Hash regions:\n");
63 debug("%10s %10s\n", "Offset", "Size");
66 * Use malloc() except in SPL (to save code size). In SPL the caller
67 * must allocate the array.
69 #ifndef CONFIG_SPL_BUILD
71 region = calloc(sizeof(*region), count);
75 for (i = 0; i < count; i++) {
76 debug("%10x %10x\n", fdt_regions[i].offset,
78 region[i].data = fit + fdt_regions[i].offset;
79 region[i].size = fdt_regions[i].size;
85 static int fit_image_setup_verify(struct image_sign_info *info,
86 const void *fit, int noffset, int required_keynode,
91 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
92 *err_msgp = "Can't get hash algo property";
95 memset(info, '\0', sizeof(*info));
96 info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
97 info->fit = (void *)fit;
98 info->node_offset = noffset;
99 info->algo = image_get_sig_algo(algo_name);
100 info->fdt_blob = gd_fdt_blob();
101 info->required_keynode = required_keynode;
102 printf("%s:%s", algo_name, info->keyname);
105 *err_msgp = "Unknown signature algorithm";
112 int fit_image_check_sig(const void *fit, int noffset, const void *data,
113 size_t size, int required_keynode, char **err_msgp)
115 struct image_sign_info info;
116 struct image_region region;
121 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
125 if (fit_image_hash_get_value(fit, noffset, &fit_value,
127 *err_msgp = "Can't get hash value property";
134 if (info.algo->verify(&info, ®ion, 1, fit_value, fit_value_len)) {
135 *err_msgp = "Verification failed";
142 static int fit_image_verify_sig(const void *fit, int image_noffset,
143 const char *data, size_t size, const void *sig_blob,
151 /* Process all hash subnodes of the component image node */
152 for (noffset = fdt_first_subnode(fit, image_noffset);
154 noffset = fdt_next_subnode(fit, noffset)) {
155 const char *name = fit_get_name(fit, noffset, NULL);
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, const void *sig_blob,
189 int verify_count = 0;
193 /* Work out what we need to verify */
195 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
197 debug("%s: No signature node found: %s\n", __func__,
198 fdt_strerror(sig_node));
202 for (noffset = fdt_first_subnode(sig_blob, sig_node);
204 noffset = fdt_next_subnode(sig_blob, noffset)) {
205 const char *required;
208 required = fdt_getprop(sig_blob, noffset, "required", NULL);
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(sig_blob, noffset, NULL));
227 int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
230 char * const exc_prop[] = {"data"};
231 const char *prop, *end, *name;
232 struct image_sign_info info;
233 const uint32_t *strings;
241 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
242 fit_get_name(fit, noffset, NULL),
243 fit_get_name(gd_fdt_blob(), required_keynode, NULL));
245 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
249 if (fit_image_hash_get_value(fit, noffset, &fit_value,
251 *err_msgp = "Can't get hash value property";
255 /* Count the number of strings in the property */
256 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
257 end = prop ? prop + prop_len : prop;
258 for (name = prop, count = 0; name < end; name++)
262 *err_msgp = "Can't get hashed-nodes property";
266 /* Add a sanity check here since we are using the stack */
267 if (count > IMAGE_MAX_HASHED_NODES) {
268 *err_msgp = "Number of hashed nodes exceeds maximum";
272 /* Create a list of node names from those strings */
273 char *node_inc[count];
275 debug("Hash nodes (%d):\n", count);
276 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
277 debug(" '%s'\n", name);
278 node_inc[i] = (char *)name;
282 * Each node can generate one region for each sub-node. Allow for
283 * 7 sub-nodes (hash@1, signature@1, etc.) and some extra.
285 max_regions = 20 + count * 7;
286 struct fdt_region fdt_regions[max_regions];
288 /* Get a list of regions to hash */
289 count = fdt_find_regions(fit, node_inc, count,
290 exc_prop, ARRAY_SIZE(exc_prop),
291 fdt_regions, max_regions - 1,
292 path, sizeof(path), 0);
294 *err_msgp = "Failed to hash configuration";
298 *err_msgp = "No data to hash";
301 if (count >= max_regions - 1) {
302 *err_msgp = "Too many hash regions";
306 /* Add the strings */
307 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
309 fdt_regions[count].offset = fdt_off_dt_strings(fit) +
310 fdt32_to_cpu(strings[0]);
311 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
315 /* Allocate the region list on the stack */
316 struct image_region region[count];
318 fit_region_make_list(fit, fdt_regions, count, region);
319 if (info.algo->verify(&info, region, count, fit_value,
321 *err_msgp = "Verification failed";
328 static int fit_config_verify_sig(const void *fit, int conf_noffset,
329 const void *sig_blob, int sig_offset)
336 /* Process all hash subnodes of the component conf node */
337 for (noffset = fdt_first_subnode(fit, conf_noffset);
339 noffset = fdt_next_subnode(fit, noffset)) {
340 const char *name = fit_get_name(fit, noffset, NULL);
342 if (!strncmp(name, FIT_SIG_NODENAME,
343 strlen(FIT_SIG_NODENAME))) {
344 ret = fit_config_check_sig(fit, noffset, sig_offset,
356 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
357 err_msg = "Corrupted or truncated tree";
361 return verified ? 0 : -EPERM;
364 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
365 err_msg, fit_get_name(fit, noffset, NULL),
366 fit_get_name(fit, conf_noffset, NULL));
370 int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
371 const void *sig_blob)
376 /* Work out what we need to verify */
377 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
379 debug("%s: No signature node found: %s\n", __func__,
380 fdt_strerror(sig_node));
384 for (noffset = fdt_first_subnode(sig_blob, sig_node);
386 noffset = fdt_next_subnode(sig_blob, noffset)) {
387 const char *required;
390 required = fdt_getprop(sig_blob, noffset, "required", NULL);
391 if (!required || strcmp(required, "conf"))
393 ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
396 printf("Failed to verify required signature '%s'\n",
397 fit_get_name(sig_blob, noffset, NULL));
405 int fit_config_verify(const void *fit, int conf_noffset)
407 return !fit_config_verify_required_sigs(fit, conf_noffset,