2 * Copyright (c) 2013, Google Inc.
4 * SPDX-License-Identifier: GPL-2.0+
13 DECLARE_GLOBAL_DATA_PTR;
14 #endif /* !USE_HOSTCC*/
17 #include <rsa-checksum.h>
19 #define IMAGE_MAX_HASHED_NODES 100
22 __attribute__((weak)) void *get_blob(void)
28 struct checksum_algo checksum_algos[] = {
48 padding_sha256_rsa2048,
59 padding_sha256_rsa4096,
65 struct image_sig_algo image_sig_algos[] = {
90 struct image_sig_algo *image_get_sig_algo(const char *name)
94 for (i = 0; i < ARRAY_SIZE(image_sig_algos); i++) {
95 if (!strcmp(image_sig_algos[i].name, name))
96 return &image_sig_algos[i];
103 * fit_region_make_list() - Make a list of image regions
105 * Given a list of fdt_regions, create a list of image_regions. This is a
106 * simple conversion routine since the FDT and image code use different
110 * @fdt_regions: Pointer to FDT regions
111 * @count: Number of FDT regions
112 * @region: Pointer to image regions, which must hold @count records. If
113 * region is NULL, then (except for an SPL build) the array will be
115 * @return: Pointer to image regions
117 struct image_region *fit_region_make_list(const void *fit,
118 struct fdt_region *fdt_regions, int count,
119 struct image_region *region)
123 debug("Hash regions:\n");
124 debug("%10s %10s\n", "Offset", "Size");
127 * Use malloc() except in SPL (to save code size). In SPL the caller
128 * must allocate the array.
130 #ifndef CONFIG_SPL_BUILD
132 region = calloc(sizeof(*region), count);
136 for (i = 0; i < count; i++) {
137 debug("%10x %10x\n", fdt_regions[i].offset,
138 fdt_regions[i].size);
139 region[i].data = fit + fdt_regions[i].offset;
140 region[i].size = fdt_regions[i].size;
146 static int fit_image_setup_verify(struct image_sign_info *info,
147 const void *fit, int noffset, int required_keynode,
152 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
153 *err_msgp = "Can't get hash algo property";
156 memset(info, '\0', sizeof(*info));
157 info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
158 info->fit = (void *)fit;
159 info->node_offset = noffset;
160 info->algo = image_get_sig_algo(algo_name);
161 info->fdt_blob = gd_fdt_blob();
162 info->required_keynode = required_keynode;
163 printf("%s:%s", algo_name, info->keyname);
166 *err_msgp = "Unknown signature algorithm";
173 int fit_image_check_sig(const void *fit, int noffset, const void *data,
174 size_t size, int required_keynode, char **err_msgp)
176 struct image_sign_info info;
177 struct image_region region;
182 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
186 if (fit_image_hash_get_value(fit, noffset, &fit_value,
188 *err_msgp = "Can't get hash value property";
195 if (info.algo->verify(&info, ®ion, 1, fit_value, fit_value_len)) {
196 *err_msgp = "Verification failed";
203 static int fit_image_verify_sig(const void *fit, int image_noffset,
204 const char *data, size_t size, const void *sig_blob,
212 /* Process all hash subnodes of the component image node */
213 for (noffset = fdt_first_subnode(fit, image_noffset);
215 noffset = fdt_next_subnode(fit, noffset)) {
216 const char *name = fit_get_name(fit, noffset, NULL);
218 if (!strncmp(name, FIT_SIG_NODENAME,
219 strlen(FIT_SIG_NODENAME))) {
220 ret = fit_image_check_sig(fit, noffset, data,
232 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
233 err_msg = "Corrupted or truncated tree";
237 return verified ? 0 : -EPERM;
240 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
241 err_msg, fit_get_name(fit, noffset, NULL),
242 fit_get_name(fit, image_noffset, NULL));
246 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
247 const char *data, size_t size, const void *sig_blob,
250 int verify_count = 0;
254 /* Work out what we need to verify */
256 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
258 debug("%s: No signature node found: %s\n", __func__,
259 fdt_strerror(sig_node));
263 for (noffset = fdt_first_subnode(sig_blob, sig_node);
265 noffset = fdt_next_subnode(sig_blob, noffset)) {
266 const char *required;
269 required = fdt_getprop(sig_blob, noffset, "required", NULL);
270 if (!required || strcmp(required, "image"))
272 ret = fit_image_verify_sig(fit, image_noffset, data, size,
275 printf("Failed to verify required signature '%s'\n",
276 fit_get_name(sig_blob, noffset, NULL));
288 int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
291 char * const exc_prop[] = {"data"};
292 const char *prop, *end, *name;
293 struct image_sign_info info;
294 const uint32_t *strings;
302 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
303 fit_get_name(fit, noffset, NULL),
304 fit_get_name(gd_fdt_blob(), required_keynode, NULL));
306 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
310 if (fit_image_hash_get_value(fit, noffset, &fit_value,
312 *err_msgp = "Can't get hash value property";
316 /* Count the number of strings in the property */
317 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
318 end = prop ? prop + prop_len : prop;
319 for (name = prop, count = 0; name < end; name++)
323 *err_msgp = "Can't get hashed-nodes property";
327 /* Add a sanity check here since we are using the stack */
328 if (count > IMAGE_MAX_HASHED_NODES) {
329 *err_msgp = "Number of hashed nodes exceeds maximum";
333 /* Create a list of node names from those strings */
334 char *node_inc[count];
336 debug("Hash nodes (%d):\n", count);
337 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
338 debug(" '%s'\n", name);
339 node_inc[i] = (char *)name;
343 * Each node can generate one region for each sub-node. Allow for
344 * 7 sub-nodes (hash@1, signature@1, etc.) and some extra.
346 max_regions = 20 + count * 7;
347 struct fdt_region fdt_regions[max_regions];
349 /* Get a list of regions to hash */
350 count = fdt_find_regions(fit, node_inc, count,
351 exc_prop, ARRAY_SIZE(exc_prop),
352 fdt_regions, max_regions - 1,
353 path, sizeof(path), 0);
355 *err_msgp = "Failed to hash configuration";
359 *err_msgp = "No data to hash";
362 if (count >= max_regions - 1) {
363 *err_msgp = "Too many hash regions";
367 /* Add the strings */
368 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
370 fdt_regions[count].offset = fdt_off_dt_strings(fit) +
371 fdt32_to_cpu(strings[0]);
372 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
376 /* Allocate the region list on the stack */
377 struct image_region region[count];
379 fit_region_make_list(fit, fdt_regions, count, region);
380 if (info.algo->verify(&info, region, count, fit_value,
382 *err_msgp = "Verification failed";
389 static int fit_config_verify_sig(const void *fit, int conf_noffset,
390 const void *sig_blob, int sig_offset)
397 /* Process all hash subnodes of the component conf node */
398 for (noffset = fdt_first_subnode(fit, conf_noffset);
400 noffset = fdt_next_subnode(fit, noffset)) {
401 const char *name = fit_get_name(fit, noffset, NULL);
403 if (!strncmp(name, FIT_SIG_NODENAME,
404 strlen(FIT_SIG_NODENAME))) {
405 ret = fit_config_check_sig(fit, noffset, sig_offset,
417 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
418 err_msg = "Corrupted or truncated tree";
422 return verified ? 0 : -EPERM;
425 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
426 err_msg, fit_get_name(fit, noffset, NULL),
427 fit_get_name(fit, conf_noffset, NULL));
431 int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
432 const void *sig_blob)
437 /* Work out what we need to verify */
438 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
440 debug("%s: No signature node found: %s\n", __func__,
441 fdt_strerror(sig_node));
445 for (noffset = fdt_first_subnode(sig_blob, sig_node);
447 noffset = fdt_next_subnode(sig_blob, noffset)) {
448 const char *required;
451 required = fdt_getprop(sig_blob, noffset, "required", NULL);
452 if (!required || strcmp(required, "conf"))
454 ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
457 printf("Failed to verify required signature '%s'\n",
458 fit_get_name(sig_blob, noffset, NULL));
466 int fit_config_verify(const void *fit, int conf_noffset)
468 return !fit_config_verify_required_sigs(fit, conf_noffset,