2 * Copyright (c) 2013, Google Inc.
4 * SPDX-License-Identifier: GPL-2.0+
13 DECLARE_GLOBAL_DATA_PTR;
14 #endif /* !USE_HOSTCC*/
16 #include <u-boot/rsa.h>
17 #include <u-boot/rsa-checksum.h>
19 #define IMAGE_MAX_HASHED_NODES 100
23 void image_set_host_blob(void *blob)
27 void *image_get_host_blob(void)
33 struct checksum_algo checksum_algos[] = {
70 struct image_sig_algo image_sig_algos[] = {
95 struct image_sig_algo *image_get_sig_algo(const char *name)
99 for (i = 0; i < ARRAY_SIZE(image_sig_algos); i++) {
100 if (!strcmp(image_sig_algos[i].name, name))
101 return &image_sig_algos[i];
108 * fit_region_make_list() - Make a list of image regions
110 * Given a list of fdt_regions, create a list of image_regions. This is a
111 * simple conversion routine since the FDT and image code use different
115 * @fdt_regions: Pointer to FDT regions
116 * @count: Number of FDT regions
117 * @region: Pointer to image regions, which must hold @count records. If
118 * region is NULL, then (except for an SPL build) the array will be
120 * @return: Pointer to image regions
122 struct image_region *fit_region_make_list(const void *fit,
123 struct fdt_region *fdt_regions, int count,
124 struct image_region *region)
128 debug("Hash regions:\n");
129 debug("%10s %10s\n", "Offset", "Size");
132 * Use malloc() except in SPL (to save code size). In SPL the caller
133 * must allocate the array.
135 #ifndef CONFIG_SPL_BUILD
137 region = calloc(sizeof(*region), count);
141 for (i = 0; i < count; i++) {
142 debug("%10x %10x\n", fdt_regions[i].offset,
143 fdt_regions[i].size);
144 region[i].data = fit + fdt_regions[i].offset;
145 region[i].size = fdt_regions[i].size;
151 static int fit_image_setup_verify(struct image_sign_info *info,
152 const void *fit, int noffset, int required_keynode,
157 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
158 *err_msgp = "Can't get hash algo property";
161 memset(info, '\0', sizeof(*info));
162 info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
163 info->fit = (void *)fit;
164 info->node_offset = noffset;
165 info->algo = image_get_sig_algo(algo_name);
166 info->fdt_blob = gd_fdt_blob();
167 info->required_keynode = required_keynode;
168 printf("%s:%s", algo_name, info->keyname);
171 *err_msgp = "Unknown signature algorithm";
178 int fit_image_check_sig(const void *fit, int noffset, const void *data,
179 size_t size, int required_keynode, char **err_msgp)
181 struct image_sign_info info;
182 struct image_region region;
187 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
191 if (fit_image_hash_get_value(fit, noffset, &fit_value,
193 *err_msgp = "Can't get hash value property";
200 if (info.algo->verify(&info, ®ion, 1, fit_value, fit_value_len)) {
201 *err_msgp = "Verification failed";
208 static int fit_image_verify_sig(const void *fit, int image_noffset,
209 const char *data, size_t size, const void *sig_blob,
217 /* Process all hash subnodes of the component image node */
218 fdt_for_each_subnode(noffset, fit, image_noffset) {
219 const char *name = fit_get_name(fit, noffset, NULL);
221 if (!strncmp(name, FIT_SIG_NODENAME,
222 strlen(FIT_SIG_NODENAME))) {
223 ret = fit_image_check_sig(fit, noffset, data,
235 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
236 err_msg = "Corrupted or truncated tree";
240 return verified ? 0 : -EPERM;
243 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
244 err_msg, fit_get_name(fit, noffset, NULL),
245 fit_get_name(fit, image_noffset, NULL));
249 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
250 const char *data, size_t size, const void *sig_blob,
253 int verify_count = 0;
257 /* Work out what we need to verify */
259 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
261 debug("%s: No signature node found: %s\n", __func__,
262 fdt_strerror(sig_node));
266 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
267 const char *required;
270 required = fdt_getprop(sig_blob, noffset, "required", NULL);
271 if (!required || strcmp(required, "image"))
273 ret = fit_image_verify_sig(fit, image_noffset, data, size,
276 printf("Failed to verify required signature '%s'\n",
277 fit_get_name(sig_blob, noffset, NULL));
289 int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
292 char * const exc_prop[] = {"data"};
293 const char *prop, *end, *name;
294 struct image_sign_info info;
295 const uint32_t *strings;
303 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
304 fit_get_name(fit, noffset, NULL),
305 fit_get_name(gd_fdt_blob(), required_keynode, NULL));
307 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
311 if (fit_image_hash_get_value(fit, noffset, &fit_value,
313 *err_msgp = "Can't get hash value property";
317 /* Count the number of strings in the property */
318 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
319 end = prop ? prop + prop_len : prop;
320 for (name = prop, count = 0; name < end; name++)
324 *err_msgp = "Can't get hashed-nodes property";
328 /* Add a sanity check here since we are using the stack */
329 if (count > IMAGE_MAX_HASHED_NODES) {
330 *err_msgp = "Number of hashed nodes exceeds maximum";
334 /* Create a list of node names from those strings */
335 char *node_inc[count];
337 debug("Hash nodes (%d):\n", count);
338 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
339 debug(" '%s'\n", name);
340 node_inc[i] = (char *)name;
344 * Each node can generate one region for each sub-node. Allow for
345 * 7 sub-nodes (hash@1, signature@1, etc.) and some extra.
347 max_regions = 20 + count * 7;
348 struct fdt_region fdt_regions[max_regions];
350 /* Get a list of regions to hash */
351 count = fdt_find_regions(fit, node_inc, count,
352 exc_prop, ARRAY_SIZE(exc_prop),
353 fdt_regions, max_regions - 1,
354 path, sizeof(path), 0);
356 *err_msgp = "Failed to hash configuration";
360 *err_msgp = "No data to hash";
363 if (count >= max_regions - 1) {
364 *err_msgp = "Too many hash regions";
368 /* Add the strings */
369 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
371 fdt_regions[count].offset = fdt_off_dt_strings(fit) +
372 fdt32_to_cpu(strings[0]);
373 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
377 /* Allocate the region list on the stack */
378 struct image_region region[count];
380 fit_region_make_list(fit, fdt_regions, count, region);
381 if (info.algo->verify(&info, region, count, fit_value,
383 *err_msgp = "Verification failed";
390 static int fit_config_verify_sig(const void *fit, int conf_noffset,
391 const void *sig_blob, int sig_offset)
398 /* Process all hash subnodes of the component conf node */
399 fdt_for_each_subnode(noffset, fit, conf_noffset) {
400 const char *name = fit_get_name(fit, noffset, NULL);
402 if (!strncmp(name, FIT_SIG_NODENAME,
403 strlen(FIT_SIG_NODENAME))) {
404 ret = fit_config_check_sig(fit, noffset, sig_offset,
416 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
417 err_msg = "Corrupted or truncated tree";
421 return verified ? 0 : -EPERM;
424 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
425 err_msg, fit_get_name(fit, noffset, NULL),
426 fit_get_name(fit, conf_noffset, NULL));
430 int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
431 const void *sig_blob)
436 /* Work out what we need to verify */
437 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
439 debug("%s: No signature node found: %s\n", __func__,
440 fdt_strerror(sig_node));
444 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
445 const char *required;
448 required = fdt_getprop(sig_blob, noffset, "required", NULL);
449 if (!required || strcmp(required, "conf"))
451 ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
454 printf("Failed to verify required signature '%s'\n",
455 fit_get_name(sig_blob, noffset, NULL));
463 int fit_config_verify(const void *fit, int conf_noffset)
465 return fit_config_verify_required_sigs(fit, conf_noffset,