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
23 void image_set_host_blob(void *blob)
27 void *image_get_host_blob(void)
33 struct checksum_algo checksum_algos[] = {
52 padding_sha256_rsa2048,
62 padding_sha256_rsa4096,
67 struct image_sig_algo image_sig_algos[] = {
92 struct image_sig_algo *image_get_sig_algo(const char *name)
96 for (i = 0; i < ARRAY_SIZE(image_sig_algos); i++) {
97 if (!strcmp(image_sig_algos[i].name, name))
98 return &image_sig_algos[i];
105 * fit_region_make_list() - Make a list of image regions
107 * Given a list of fdt_regions, create a list of image_regions. This is a
108 * simple conversion routine since the FDT and image code use different
112 * @fdt_regions: Pointer to FDT regions
113 * @count: Number of FDT regions
114 * @region: Pointer to image regions, which must hold @count records. If
115 * region is NULL, then (except for an SPL build) the array will be
117 * @return: Pointer to image regions
119 struct image_region *fit_region_make_list(const void *fit,
120 struct fdt_region *fdt_regions, int count,
121 struct image_region *region)
125 debug("Hash regions:\n");
126 debug("%10s %10s\n", "Offset", "Size");
129 * Use malloc() except in SPL (to save code size). In SPL the caller
130 * must allocate the array.
132 #ifndef CONFIG_SPL_BUILD
134 region = calloc(sizeof(*region), count);
138 for (i = 0; i < count; i++) {
139 debug("%10x %10x\n", fdt_regions[i].offset,
140 fdt_regions[i].size);
141 region[i].data = fit + fdt_regions[i].offset;
142 region[i].size = fdt_regions[i].size;
148 static int fit_image_setup_verify(struct image_sign_info *info,
149 const void *fit, int noffset, int required_keynode,
154 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
155 *err_msgp = "Can't get hash algo property";
158 memset(info, '\0', sizeof(*info));
159 info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
160 info->fit = (void *)fit;
161 info->node_offset = noffset;
162 info->algo = image_get_sig_algo(algo_name);
163 info->fdt_blob = gd_fdt_blob();
164 info->required_keynode = required_keynode;
165 printf("%s:%s", algo_name, info->keyname);
168 *err_msgp = "Unknown signature algorithm";
175 int fit_image_check_sig(const void *fit, int noffset, const void *data,
176 size_t size, int required_keynode, char **err_msgp)
178 struct image_sign_info info;
179 struct image_region region;
184 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
188 if (fit_image_hash_get_value(fit, noffset, &fit_value,
190 *err_msgp = "Can't get hash value property";
197 if (info.algo->verify(&info, ®ion, 1, fit_value, fit_value_len)) {
198 *err_msgp = "Verification failed";
205 static int fit_image_verify_sig(const void *fit, int image_noffset,
206 const char *data, size_t size, const void *sig_blob,
214 /* Process all hash subnodes of the component image node */
215 for (noffset = fdt_first_subnode(fit, image_noffset);
217 noffset = fdt_next_subnode(fit, noffset)) {
218 const char *name = fit_get_name(fit, noffset, NULL);
220 if (!strncmp(name, FIT_SIG_NODENAME,
221 strlen(FIT_SIG_NODENAME))) {
222 ret = fit_image_check_sig(fit, noffset, data,
234 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
235 err_msg = "Corrupted or truncated tree";
239 return verified ? 0 : -EPERM;
242 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
243 err_msg, fit_get_name(fit, noffset, NULL),
244 fit_get_name(fit, image_noffset, NULL));
248 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
249 const char *data, size_t size, const void *sig_blob,
252 int verify_count = 0;
256 /* Work out what we need to verify */
258 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
260 debug("%s: No signature node found: %s\n", __func__,
261 fdt_strerror(sig_node));
265 for (noffset = fdt_first_subnode(sig_blob, sig_node);
267 noffset = fdt_next_subnode(sig_blob, noffset)) {
268 const char *required;
271 required = fdt_getprop(sig_blob, noffset, "required", NULL);
272 if (!required || strcmp(required, "image"))
274 ret = fit_image_verify_sig(fit, image_noffset, data, size,
277 printf("Failed to verify required signature '%s'\n",
278 fit_get_name(sig_blob, noffset, NULL));
290 int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
293 char * const exc_prop[] = {"data"};
294 const char *prop, *end, *name;
295 struct image_sign_info info;
296 const uint32_t *strings;
304 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
305 fit_get_name(fit, noffset, NULL),
306 fit_get_name(gd_fdt_blob(), required_keynode, NULL));
308 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
312 if (fit_image_hash_get_value(fit, noffset, &fit_value,
314 *err_msgp = "Can't get hash value property";
318 /* Count the number of strings in the property */
319 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
320 end = prop ? prop + prop_len : prop;
321 for (name = prop, count = 0; name < end; name++)
325 *err_msgp = "Can't get hashed-nodes property";
329 /* Add a sanity check here since we are using the stack */
330 if (count > IMAGE_MAX_HASHED_NODES) {
331 *err_msgp = "Number of hashed nodes exceeds maximum";
335 /* Create a list of node names from those strings */
336 char *node_inc[count];
338 debug("Hash nodes (%d):\n", count);
339 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
340 debug(" '%s'\n", name);
341 node_inc[i] = (char *)name;
345 * Each node can generate one region for each sub-node. Allow for
346 * 7 sub-nodes (hash@1, signature@1, etc.) and some extra.
348 max_regions = 20 + count * 7;
349 struct fdt_region fdt_regions[max_regions];
351 /* Get a list of regions to hash */
352 count = fdt_find_regions(fit, node_inc, count,
353 exc_prop, ARRAY_SIZE(exc_prop),
354 fdt_regions, max_regions - 1,
355 path, sizeof(path), 0);
357 *err_msgp = "Failed to hash configuration";
361 *err_msgp = "No data to hash";
364 if (count >= max_regions - 1) {
365 *err_msgp = "Too many hash regions";
369 /* Add the strings */
370 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
372 fdt_regions[count].offset = fdt_off_dt_strings(fit) +
373 fdt32_to_cpu(strings[0]);
374 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
378 /* Allocate the region list on the stack */
379 struct image_region region[count];
381 fit_region_make_list(fit, fdt_regions, count, region);
382 if (info.algo->verify(&info, region, count, fit_value,
384 *err_msgp = "Verification failed";
391 static int fit_config_verify_sig(const void *fit, int conf_noffset,
392 const void *sig_blob, int sig_offset)
399 /* Process all hash subnodes of the component conf node */
400 for (noffset = fdt_first_subnode(fit, conf_noffset);
402 noffset = fdt_next_subnode(fit, noffset)) {
403 const char *name = fit_get_name(fit, noffset, NULL);
405 if (!strncmp(name, FIT_SIG_NODENAME,
406 strlen(FIT_SIG_NODENAME))) {
407 ret = fit_config_check_sig(fit, noffset, sig_offset,
419 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
420 err_msg = "Corrupted or truncated tree";
424 return verified ? 0 : -EPERM;
427 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
428 err_msg, fit_get_name(fit, noffset, NULL),
429 fit_get_name(fit, conf_noffset, NULL));
433 int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
434 const void *sig_blob)
439 /* Work out what we need to verify */
440 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
442 debug("%s: No signature node found: %s\n", __func__,
443 fdt_strerror(sig_node));
447 for (noffset = fdt_first_subnode(sig_blob, sig_node);
449 noffset = fdt_next_subnode(sig_blob, noffset)) {
450 const char *required;
453 required = fdt_getprop(sig_blob, noffset, "required", NULL);
454 if (!required || strcmp(required, "conf"))
456 ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
459 printf("Failed to verify required signature '%s'\n",
460 fit_get_name(sig_blob, noffset, NULL));
468 int fit_config_verify(const void *fit, int conf_noffset)
470 return !fit_config_verify_required_sigs(fit, conf_noffset,