1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013, Google Inc.
12 DECLARE_GLOBAL_DATA_PTR;
13 #endif /* !USE_HOSTCC*/
14 #include <fdt_region.h>
16 #include <u-boot/rsa.h>
17 #include <u-boot/rsa-checksum.h>
19 #define IMAGE_MAX_HASHED_NODES 100
24 void image_set_host_blob(void *blob)
29 void *image_get_host_blob(void)
36 * fit_region_make_list() - Make a list of image regions
38 * Given a list of fdt_regions, create a list of image_regions. This is a
39 * simple conversion routine since the FDT and image code use different
43 * @fdt_regions: Pointer to FDT regions
44 * @count: Number of FDT regions
45 * @region: Pointer to image regions, which must hold @count records. If
46 * region is NULL, then (except for an SPL build) the array will be
48 * @return: Pointer to image regions
50 struct image_region *fit_region_make_list(const void *fit,
51 struct fdt_region *fdt_regions,
53 struct image_region *region)
57 debug("Hash regions:\n");
58 debug("%10s %10s\n", "Offset", "Size");
61 * Use malloc() except in SPL (to save code size). In SPL the caller
62 * must allocate the array.
64 #ifndef CONFIG_SPL_BUILD
66 region = calloc(sizeof(*region), count);
70 for (i = 0; i < count; i++) {
71 debug("%10x %10x\n", fdt_regions[i].offset,
73 region[i].data = fit + fdt_regions[i].offset;
74 region[i].size = fdt_regions[i].size;
80 static int fit_image_setup_verify(struct image_sign_info *info,
81 const void *fit, int noffset,
82 int required_keynode, char **err_msgp)
85 const char *padding_name;
87 if (fdt_totalsize(fit) > CONFIG_FIT_SIGNATURE_MAX_SIZE) {
88 *err_msgp = "Total size too large";
92 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
93 *err_msgp = "Can't get hash algo property";
97 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
99 padding_name = RSA_DEFAULT_PADDING_NAME;
101 memset(info, '\0', sizeof(*info));
102 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
103 info->fit = (void *)fit;
104 info->node_offset = noffset;
105 info->name = algo_name;
106 info->checksum = image_get_checksum_algo(algo_name);
107 info->crypto = image_get_crypto_algo(algo_name);
108 info->padding = image_get_padding_algo(padding_name);
109 info->fdt_blob = gd_fdt_blob();
110 info->required_keynode = required_keynode;
111 printf("%s:%s", algo_name, info->keyname);
113 if (!info->checksum || !info->crypto || !info->padding) {
114 *err_msgp = "Unknown signature algorithm";
121 int fit_image_check_sig(const void *fit, int noffset, const void *data,
122 size_t size, int required_keynode, char **err_msgp)
124 struct image_sign_info info;
125 struct image_region region;
130 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
134 if (fit_image_hash_get_value(fit, noffset, &fit_value,
136 *err_msgp = "Can't get hash value property";
143 if (info.crypto->verify(&info, ®ion, 1, fit_value, fit_value_len)) {
144 *err_msgp = "Verification failed";
151 static int fit_image_verify_sig(const void *fit, int image_noffset,
152 const char *data, size_t size,
153 const void *sig_blob, int sig_offset)
160 /* Process all hash subnodes of the component image node */
161 fdt_for_each_subnode(noffset, fit, image_noffset) {
162 const char *name = fit_get_name(fit, noffset, NULL);
164 if (!strncmp(name, FIT_SIG_NODENAME,
165 strlen(FIT_SIG_NODENAME))) {
166 ret = fit_image_check_sig(fit, noffset, data,
178 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
179 err_msg = "Corrupted or truncated tree";
183 return verified ? 0 : -EPERM;
186 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
187 err_msg, fit_get_name(fit, noffset, NULL),
188 fit_get_name(fit, image_noffset, NULL));
192 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
193 const char *data, size_t size,
194 const void *sig_blob, int *no_sigsp)
196 int verify_count = 0;
200 /* Work out what we need to verify */
202 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
204 debug("%s: No signature node found: %s\n", __func__,
205 fdt_strerror(sig_node));
209 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
210 const char *required;
213 required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED,
215 if (!required || strcmp(required, "image"))
217 ret = fit_image_verify_sig(fit, image_noffset, data, size,
220 printf("Failed to verify required signature '%s'\n",
221 fit_get_name(sig_blob, noffset, NULL));
234 * fit_config_check_sig() - Check the signature of a config
237 * @noffset: Offset of configuration node (e.g. /configurations/conf-1)
238 * @required_keynode: Offset in the control FDT of the required key node,
239 * if any. If this is given, then the configuration wil not
240 * pass verification unless that key is used. If this is
241 * -1 then any signature will do.
242 * @conf_noffset: Offset of the configuration subnode being checked (e.g.
243 * /configurations/conf-1/kernel)
244 * @err_msgp: In the event of an error, this will be pointed to a
245 * help error string to display to the user.
246 * @return 0 if all verified ok, <0 on error
248 static int fit_config_check_sig(const void *fit, int noffset,
249 int required_keynode, int conf_noffset,
252 char * const exc_prop[] = {"data"};
253 const char *prop, *end, *name;
254 struct image_sign_info info;
255 const uint32_t *strings;
256 const char *config_name;
265 config_name = fit_get_name(fit, conf_noffset, NULL);
266 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
267 fit_get_name(fit, noffset, NULL),
268 fit_get_name(gd_fdt_blob(), required_keynode, NULL));
270 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
274 if (fit_image_hash_get_value(fit, noffset, &fit_value,
276 *err_msgp = "Can't get hash value property";
280 /* Count the number of strings in the property */
281 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
282 end = prop ? prop + prop_len : prop;
283 for (name = prop, count = 0; name < end; name++)
287 *err_msgp = "Can't get hashed-nodes property";
291 if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
292 *err_msgp = "hashed-nodes property must be null-terminated";
296 /* Add a sanity check here since we are using the stack */
297 if (count > IMAGE_MAX_HASHED_NODES) {
298 *err_msgp = "Number of hashed nodes exceeds maximum";
302 /* Create a list of node names from those strings */
303 char *node_inc[count];
305 debug("Hash nodes (%d):\n", count);
306 found_config = false;
307 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
308 debug(" '%s'\n", name);
309 node_inc[i] = (char *)name;
310 if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) &&
311 name[sizeof(FIT_CONFS_PATH) - 1] == '/' &&
312 !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) {
313 debug(" (found config node %s)", config_name);
318 *err_msgp = "Selected config not in hashed nodes";
323 * Each node can generate one region for each sub-node. Allow for
324 * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
326 max_regions = 20 + count * 7;
327 struct fdt_region fdt_regions[max_regions];
329 /* Get a list of regions to hash */
330 count = fdt_find_regions(fit, node_inc, count,
331 exc_prop, ARRAY_SIZE(exc_prop),
332 fdt_regions, max_regions - 1,
333 path, sizeof(path), 0);
335 *err_msgp = "Failed to hash configuration";
339 *err_msgp = "No data to hash";
342 if (count >= max_regions - 1) {
343 *err_msgp = "Too many hash regions";
347 /* Add the strings */
348 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
351 * The strings region offset must be a static 0x0.
352 * This is set in tool/image-host.c
354 fdt_regions[count].offset = fdt_off_dt_strings(fit);
355 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
359 /* Allocate the region list on the stack */
360 struct image_region region[count];
362 fit_region_make_list(fit, fdt_regions, count, region);
363 if (info.crypto->verify(&info, region, count, fit_value,
365 *err_msgp = "Verification failed";
372 static int fit_config_verify_sig(const void *fit, int conf_noffset,
373 const void *sig_blob, int sig_offset)
380 /* Process all hash subnodes of the component conf node */
381 fdt_for_each_subnode(noffset, fit, conf_noffset) {
382 const char *name = fit_get_name(fit, noffset, NULL);
384 if (!strncmp(name, FIT_SIG_NODENAME,
385 strlen(FIT_SIG_NODENAME))) {
386 ret = fit_config_check_sig(fit, noffset, sig_offset,
387 conf_noffset, &err_msg);
398 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
399 err_msg = "Corrupted or truncated tree";
407 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
408 err_msg, fit_get_name(fit, noffset, NULL),
409 fit_get_name(fit, conf_noffset, NULL));
413 int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
414 const void *sig_blob)
419 /* Work out what we need to verify */
420 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
422 debug("%s: No signature node found: %s\n", __func__,
423 fdt_strerror(sig_node));
427 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
428 const char *required;
431 required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED,
433 if (!required || strcmp(required, "conf"))
435 ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
438 printf("Failed to verify required signature '%s'\n",
439 fit_get_name(sig_blob, noffset, NULL));
447 int fit_config_verify(const void *fit, int conf_noffset)
449 return fit_config_verify_required_sigs(fit, conf_noffset,