1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013, Google Inc.
12 DECLARE_GLOBAL_DATA_PTR;
13 #endif /* !USE_HOSTCC*/
15 #include <u-boot/rsa.h>
16 #include <u-boot/rsa-checksum.h>
18 #define IMAGE_MAX_HASHED_NODES 100
23 void image_set_host_blob(void *blob)
28 void *image_get_host_blob(void)
35 * fit_region_make_list() - Make a list of image regions
37 * Given a list of fdt_regions, create a list of image_regions. This is a
38 * simple conversion routine since the FDT and image code use different
42 * @fdt_regions: Pointer to FDT regions
43 * @count: Number of FDT regions
44 * @region: Pointer to image regions, which must hold @count records. If
45 * region is NULL, then (except for an SPL build) the array will be
47 * @return: Pointer to image regions
49 struct image_region *fit_region_make_list(const void *fit,
50 struct fdt_region *fdt_regions,
52 struct image_region *region)
56 debug("Hash regions:\n");
57 debug("%10s %10s\n", "Offset", "Size");
60 * Use malloc() except in SPL (to save code size). In SPL the caller
61 * must allocate the array.
63 #ifndef CONFIG_SPL_BUILD
65 region = calloc(sizeof(*region), count);
69 for (i = 0; i < count; i++) {
70 debug("%10x %10x\n", fdt_regions[i].offset,
72 region[i].data = fit + fdt_regions[i].offset;
73 region[i].size = fdt_regions[i].size;
79 static int fit_image_setup_verify(struct image_sign_info *info,
80 const void *fit, int noffset,
81 int required_keynode, char **err_msgp)
84 const char *padding_name;
86 if (fdt_totalsize(fit) > CONFIG_FIT_SIGNATURE_MAX_SIZE) {
87 *err_msgp = "Total size too large";
91 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
92 *err_msgp = "Can't get hash algo property";
96 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
98 padding_name = RSA_DEFAULT_PADDING_NAME;
100 memset(info, '\0', sizeof(*info));
101 info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
102 info->fit = (void *)fit;
103 info->node_offset = noffset;
104 info->name = algo_name;
105 info->checksum = image_get_checksum_algo(algo_name);
106 info->crypto = image_get_crypto_algo(algo_name);
107 info->padding = image_get_padding_algo(padding_name);
108 info->fdt_blob = gd_fdt_blob();
109 info->required_keynode = required_keynode;
110 printf("%s:%s", algo_name, info->keyname);
112 if (!info->checksum || !info->crypto || !info->padding) {
113 *err_msgp = "Unknown signature algorithm";
120 int fit_image_check_sig(const void *fit, int noffset, const void *data,
121 size_t size, int required_keynode, char **err_msgp)
123 struct image_sign_info info;
124 struct image_region region;
129 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
133 if (fit_image_hash_get_value(fit, noffset, &fit_value,
135 *err_msgp = "Can't get hash value property";
142 if (info.crypto->verify(&info, ®ion, 1, fit_value, fit_value_len)) {
143 *err_msgp = "Verification failed";
150 static int fit_image_verify_sig(const void *fit, int image_noffset,
151 const char *data, size_t size,
152 const void *sig_blob, int sig_offset)
159 /* Process all hash subnodes of the component image node */
160 fdt_for_each_subnode(noffset, fit, image_noffset) {
161 const char *name = fit_get_name(fit, noffset, NULL);
163 if (!strncmp(name, FIT_SIG_NODENAME,
164 strlen(FIT_SIG_NODENAME))) {
165 ret = fit_image_check_sig(fit, noffset, data,
177 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
178 err_msg = "Corrupted or truncated tree";
182 return verified ? 0 : -EPERM;
185 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
186 err_msg, fit_get_name(fit, noffset, NULL),
187 fit_get_name(fit, image_noffset, NULL));
191 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
192 const char *data, size_t size,
193 const void *sig_blob, int *no_sigsp)
195 int verify_count = 0;
199 /* Work out what we need to verify */
201 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
203 debug("%s: No signature node found: %s\n", __func__,
204 fdt_strerror(sig_node));
208 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
209 const char *required;
212 required = fdt_getprop(sig_blob, noffset, "required", NULL);
213 if (!required || strcmp(required, "image"))
215 ret = fit_image_verify_sig(fit, image_noffset, data, size,
218 printf("Failed to verify required signature '%s'\n",
219 fit_get_name(sig_blob, noffset, NULL));
231 int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
234 char * const exc_prop[] = {"data"};
235 const char *prop, *end, *name;
236 struct image_sign_info info;
237 const uint32_t *strings;
245 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
246 fit_get_name(fit, noffset, NULL),
247 fit_get_name(gd_fdt_blob(), required_keynode, NULL));
249 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
253 if (fit_image_hash_get_value(fit, noffset, &fit_value,
255 *err_msgp = "Can't get hash value property";
259 /* Count the number of strings in the property */
260 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
261 end = prop ? prop + prop_len : prop;
262 for (name = prop, count = 0; name < end; name++)
266 *err_msgp = "Can't get hashed-nodes property";
270 if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
271 *err_msgp = "hashed-nodes property must be null-terminated";
275 /* Add a sanity check here since we are using the stack */
276 if (count > IMAGE_MAX_HASHED_NODES) {
277 *err_msgp = "Number of hashed nodes exceeds maximum";
281 /* Create a list of node names from those strings */
282 char *node_inc[count];
284 debug("Hash nodes (%d):\n", count);
285 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
286 debug(" '%s'\n", name);
287 node_inc[i] = (char *)name;
291 * Each node can generate one region for each sub-node. Allow for
292 * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
294 max_regions = 20 + count * 7;
295 struct fdt_region fdt_regions[max_regions];
297 /* Get a list of regions to hash */
298 count = fdt_find_regions(fit, node_inc, count,
299 exc_prop, ARRAY_SIZE(exc_prop),
300 fdt_regions, max_regions - 1,
301 path, sizeof(path), 0);
303 *err_msgp = "Failed to hash configuration";
307 *err_msgp = "No data to hash";
310 if (count >= max_regions - 1) {
311 *err_msgp = "Too many hash regions";
315 /* Add the strings */
316 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
319 * The strings region offset must be a static 0x0.
320 * This is set in tool/image-host.c
322 fdt_regions[count].offset = fdt_off_dt_strings(fit);
323 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
327 /* Allocate the region list on the stack */
328 struct image_region region[count];
330 fit_region_make_list(fit, fdt_regions, count, region);
331 if (info.crypto->verify(&info, region, count, fit_value,
333 *err_msgp = "Verification failed";
340 static int fit_config_verify_sig(const void *fit, int conf_noffset,
341 const void *sig_blob, int sig_offset)
348 /* Process all hash subnodes of the component conf node */
349 fdt_for_each_subnode(noffset, fit, conf_noffset) {
350 const char *name = fit_get_name(fit, noffset, NULL);
352 if (!strncmp(name, FIT_SIG_NODENAME,
353 strlen(FIT_SIG_NODENAME))) {
354 ret = fit_config_check_sig(fit, noffset, sig_offset,
366 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
367 err_msg = "Corrupted or truncated tree";
371 return verified ? 0 : -EPERM;
374 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
375 err_msg, fit_get_name(fit, noffset, NULL),
376 fit_get_name(fit, conf_noffset, NULL));
380 int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
381 const void *sig_blob)
386 /* Work out what we need to verify */
387 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
389 debug("%s: No signature node found: %s\n", __func__,
390 fdt_strerror(sig_node));
394 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
395 const char *required;
398 required = fdt_getprop(sig_blob, noffset, "required", NULL);
399 if (!required || strcmp(required, "conf"))
401 ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
404 printf("Failed to verify required signature '%s'\n",
405 fit_get_name(sig_blob, noffset, NULL));
413 int fit_config_verify(const void *fit, int conf_noffset)
415 return fit_config_verify_required_sigs(fit, conf_noffset,