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[] = {
46 padding_sha256_rsa2048,
50 struct image_sig_algo image_sig_algos[] = {
67 struct image_sig_algo *image_get_sig_algo(const char *name)
71 for (i = 0; i < ARRAY_SIZE(image_sig_algos); i++) {
72 if (!strcmp(image_sig_algos[i].name, name))
73 return &image_sig_algos[i];
80 * fit_region_make_list() - Make a list of image regions
82 * Given a list of fdt_regions, create a list of image_regions. This is a
83 * simple conversion routine since the FDT and image code use different
87 * @fdt_regions: Pointer to FDT regions
88 * @count: Number of FDT regions
89 * @region: Pointer to image regions, which must hold @count records. If
90 * region is NULL, then (except for an SPL build) the array will be
92 * @return: Pointer to image regions
94 struct image_region *fit_region_make_list(const void *fit,
95 struct fdt_region *fdt_regions, int count,
96 struct image_region *region)
100 debug("Hash regions:\n");
101 debug("%10s %10s\n", "Offset", "Size");
104 * Use malloc() except in SPL (to save code size). In SPL the caller
105 * must allocate the array.
107 #ifndef CONFIG_SPL_BUILD
109 region = calloc(sizeof(*region), count);
113 for (i = 0; i < count; i++) {
114 debug("%10x %10x\n", fdt_regions[i].offset,
115 fdt_regions[i].size);
116 region[i].data = fit + fdt_regions[i].offset;
117 region[i].size = fdt_regions[i].size;
123 static int fit_image_setup_verify(struct image_sign_info *info,
124 const void *fit, int noffset, int required_keynode,
129 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
130 *err_msgp = "Can't get hash algo property";
133 memset(info, '\0', sizeof(*info));
134 info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
135 info->fit = (void *)fit;
136 info->node_offset = noffset;
137 info->algo = image_get_sig_algo(algo_name);
138 info->fdt_blob = gd_fdt_blob();
139 info->required_keynode = required_keynode;
140 printf("%s:%s", algo_name, info->keyname);
143 *err_msgp = "Unknown signature algorithm";
150 int fit_image_check_sig(const void *fit, int noffset, const void *data,
151 size_t size, int required_keynode, char **err_msgp)
153 struct image_sign_info info;
154 struct image_region region;
159 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
163 if (fit_image_hash_get_value(fit, noffset, &fit_value,
165 *err_msgp = "Can't get hash value property";
172 if (info.algo->verify(&info, ®ion, 1, fit_value, fit_value_len)) {
173 *err_msgp = "Verification failed";
180 static int fit_image_verify_sig(const void *fit, int image_noffset,
181 const char *data, size_t size, const void *sig_blob,
189 /* Process all hash subnodes of the component image node */
190 for (noffset = fdt_first_subnode(fit, image_noffset);
192 noffset = fdt_next_subnode(fit, noffset)) {
193 const char *name = fit_get_name(fit, noffset, NULL);
195 if (!strncmp(name, FIT_SIG_NODENAME,
196 strlen(FIT_SIG_NODENAME))) {
197 ret = fit_image_check_sig(fit, noffset, data,
209 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
210 err_msg = "Corrupted or truncated tree";
214 return verified ? 0 : -EPERM;
217 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
218 err_msg, fit_get_name(fit, noffset, NULL),
219 fit_get_name(fit, image_noffset, NULL));
223 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
224 const char *data, size_t size, const void *sig_blob,
227 int verify_count = 0;
231 /* Work out what we need to verify */
233 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
235 debug("%s: No signature node found: %s\n", __func__,
236 fdt_strerror(sig_node));
240 for (noffset = fdt_first_subnode(sig_blob, sig_node);
242 noffset = fdt_next_subnode(sig_blob, noffset)) {
243 const char *required;
246 required = fdt_getprop(sig_blob, noffset, "required", NULL);
247 if (!required || strcmp(required, "image"))
249 ret = fit_image_verify_sig(fit, image_noffset, data, size,
252 printf("Failed to verify required signature '%s'\n",
253 fit_get_name(sig_blob, noffset, NULL));
265 int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
268 char * const exc_prop[] = {"data"};
269 const char *prop, *end, *name;
270 struct image_sign_info info;
271 const uint32_t *strings;
279 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
280 fit_get_name(fit, noffset, NULL),
281 fit_get_name(gd_fdt_blob(), required_keynode, NULL));
283 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
287 if (fit_image_hash_get_value(fit, noffset, &fit_value,
289 *err_msgp = "Can't get hash value property";
293 /* Count the number of strings in the property */
294 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
295 end = prop ? prop + prop_len : prop;
296 for (name = prop, count = 0; name < end; name++)
300 *err_msgp = "Can't get hashed-nodes property";
304 /* Add a sanity check here since we are using the stack */
305 if (count > IMAGE_MAX_HASHED_NODES) {
306 *err_msgp = "Number of hashed nodes exceeds maximum";
310 /* Create a list of node names from those strings */
311 char *node_inc[count];
313 debug("Hash nodes (%d):\n", count);
314 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
315 debug(" '%s'\n", name);
316 node_inc[i] = (char *)name;
320 * Each node can generate one region for each sub-node. Allow for
321 * 7 sub-nodes (hash@1, signature@1, etc.) and some extra.
323 max_regions = 20 + count * 7;
324 struct fdt_region fdt_regions[max_regions];
326 /* Get a list of regions to hash */
327 count = fdt_find_regions(fit, node_inc, count,
328 exc_prop, ARRAY_SIZE(exc_prop),
329 fdt_regions, max_regions - 1,
330 path, sizeof(path), 0);
332 *err_msgp = "Failed to hash configuration";
336 *err_msgp = "No data to hash";
339 if (count >= max_regions - 1) {
340 *err_msgp = "Too many hash regions";
344 /* Add the strings */
345 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
347 fdt_regions[count].offset = fdt_off_dt_strings(fit) +
348 fdt32_to_cpu(strings[0]);
349 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
353 /* Allocate the region list on the stack */
354 struct image_region region[count];
356 fit_region_make_list(fit, fdt_regions, count, region);
357 if (info.algo->verify(&info, region, count, fit_value,
359 *err_msgp = "Verification failed";
366 static int fit_config_verify_sig(const void *fit, int conf_noffset,
367 const void *sig_blob, int sig_offset)
374 /* Process all hash subnodes of the component conf node */
375 for (noffset = fdt_first_subnode(fit, conf_noffset);
377 noffset = fdt_next_subnode(fit, noffset)) {
378 const char *name = fit_get_name(fit, noffset, NULL);
380 if (!strncmp(name, FIT_SIG_NODENAME,
381 strlen(FIT_SIG_NODENAME))) {
382 ret = fit_config_check_sig(fit, noffset, sig_offset,
394 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
395 err_msg = "Corrupted or truncated tree";
399 return verified ? 0 : -EPERM;
402 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
403 err_msg, fit_get_name(fit, noffset, NULL),
404 fit_get_name(fit, conf_noffset, NULL));
408 int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
409 const void *sig_blob)
414 /* Work out what we need to verify */
415 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
417 debug("%s: No signature node found: %s\n", __func__,
418 fdt_strerror(sig_node));
422 for (noffset = fdt_first_subnode(sig_blob, sig_node);
424 noffset = fdt_next_subnode(sig_blob, noffset)) {
425 const char *required;
428 required = fdt_getprop(sig_blob, noffset, "required", NULL);
429 if (!required || strcmp(required, "conf"))
431 ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
434 printf("Failed to verify required signature '%s'\n",
435 fit_get_name(sig_blob, noffset, NULL));
443 int fit_config_verify(const void *fit, int conf_noffset)
445 return !fit_config_verify_required_sigs(fit, conf_noffset,