2 * Copyright (c) 2013, Google Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 DECLARE_GLOBAL_DATA_PTR;
27 #endif /* !USE_HOSTCC*/
31 #define IMAGE_MAX_HASHED_NODES 100
33 struct image_sig_algo image_sig_algos[] = {
42 struct image_sig_algo *image_get_sig_algo(const char *name)
46 for (i = 0; i < ARRAY_SIZE(image_sig_algos); i++) {
47 if (!strcmp(image_sig_algos[i].name, name))
48 return &image_sig_algos[i];
55 * fit_region_make_list() - Make a list of image regions
57 * Given a list of fdt_regions, create a list of image_regions. This is a
58 * simple conversion routine since the FDT and image code use different
62 * @fdt_regions: Pointer to FDT regions
63 * @count: Number of FDT regions
64 * @region: Pointer to image regions, which must hold @count records. If
65 * region is NULL, then (except for an SPL build) the array will be
67 * @return: Pointer to image regions
69 struct image_region *fit_region_make_list(const void *fit,
70 struct fdt_region *fdt_regions, int count,
71 struct image_region *region)
75 debug("Hash regions:\n");
76 debug("%10s %10s\n", "Offset", "Size");
79 * Use malloc() except in SPL (to save code size). In SPL the caller
80 * must allocate the array.
82 #ifndef CONFIG_SPL_BUILD
84 region = calloc(sizeof(*region), count);
88 for (i = 0; i < count; i++) {
89 debug("%10x %10x\n", fdt_regions[i].offset,
91 region[i].data = fit + fdt_regions[i].offset;
92 region[i].size = fdt_regions[i].size;
98 static int fit_image_setup_verify(struct image_sign_info *info,
99 const void *fit, int noffset, int required_keynode,
104 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
105 *err_msgp = "Can't get hash algo property";
108 memset(info, '\0', sizeof(*info));
109 info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
110 info->fit = (void *)fit;
111 info->node_offset = noffset;
112 info->algo = image_get_sig_algo(algo_name);
113 info->fdt_blob = gd_fdt_blob();
114 info->required_keynode = required_keynode;
115 printf("%s:%s", algo_name, info->keyname);
118 *err_msgp = "Unknown signature algorithm";
125 int fit_image_check_sig(const void *fit, int noffset, const void *data,
126 size_t size, int required_keynode, char **err_msgp)
128 struct image_sign_info info;
129 struct image_region region;
134 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
138 if (fit_image_hash_get_value(fit, noffset, &fit_value,
140 *err_msgp = "Can't get hash value property";
147 if (info.algo->verify(&info, ®ion, 1, fit_value, fit_value_len)) {
148 *err_msgp = "Verification failed";
155 static int fit_image_verify_sig(const void *fit, int image_noffset,
156 const char *data, size_t size, const void *sig_blob,
164 /* Process all hash subnodes of the component image node */
165 for (noffset = fdt_first_subnode(fit, image_noffset);
167 noffset = fdt_next_subnode(fit, noffset)) {
168 const char *name = fit_get_name(fit, noffset, NULL);
170 if (!strncmp(name, FIT_SIG_NODENAME,
171 strlen(FIT_SIG_NODENAME))) {
172 ret = fit_image_check_sig(fit, noffset, data,
184 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
185 err_msg = "Corrupted or truncated tree";
189 return verified ? 0 : -EPERM;
192 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
193 err_msg, fit_get_name(fit, noffset, NULL),
194 fit_get_name(fit, image_noffset, NULL));
198 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
199 const char *data, size_t size, const void *sig_blob,
202 int verify_count = 0;
206 /* Work out what we need to verify */
208 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
210 debug("%s: No signature node found: %s\n", __func__,
211 fdt_strerror(sig_node));
215 for (noffset = fdt_first_subnode(sig_blob, sig_node);
217 noffset = fdt_next_subnode(sig_blob, noffset)) {
218 const char *required;
221 required = fdt_getprop(sig_blob, noffset, "required", NULL);
222 if (!required || strcmp(required, "image"))
224 ret = fit_image_verify_sig(fit, image_noffset, data, size,
227 printf("Failed to verify required signature '%s'\n",
228 fit_get_name(sig_blob, noffset, NULL));
240 int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
243 char * const exc_prop[] = {"data"};
244 const char *prop, *end, *name;
245 struct image_sign_info info;
246 const uint32_t *strings;
254 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
255 fit_get_name(fit, noffset, NULL),
256 fit_get_name(gd_fdt_blob(), required_keynode, NULL));
258 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
262 if (fit_image_hash_get_value(fit, noffset, &fit_value,
264 *err_msgp = "Can't get hash value property";
268 /* Count the number of strings in the property */
269 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
270 end = prop ? prop + prop_len : prop;
271 for (name = prop, count = 0; name < end; name++)
275 *err_msgp = "Can't get hashed-nodes property";
279 /* Add a sanity check here since we are using the stack */
280 if (count > IMAGE_MAX_HASHED_NODES) {
281 *err_msgp = "Number of hashed nodes exceeds maximum";
285 /* Create a list of node names from those strings */
286 char *node_inc[count];
288 debug("Hash nodes (%d):\n", count);
289 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
290 debug(" '%s'\n", name);
291 node_inc[i] = (char *)name;
295 * Each node can generate one region for each sub-node. Allow for
296 * 7 sub-nodes (hash@1, signature@1, etc.) and some extra.
298 max_regions = 20 + count * 7;
299 struct fdt_region fdt_regions[max_regions];
301 /* Get a list of regions to hash */
302 count = fdt_find_regions(fit, node_inc, count,
303 exc_prop, ARRAY_SIZE(exc_prop),
304 fdt_regions, max_regions - 1,
305 path, sizeof(path), 0);
307 *err_msgp = "Failed to hash configuration";
311 *err_msgp = "No data to hash";
314 if (count >= max_regions - 1) {
315 *err_msgp = "Too many hash regions";
319 /* Add the strings */
320 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
322 fdt_regions[count].offset = fdt_off_dt_strings(fit) +
323 fdt32_to_cpu(strings[0]);
324 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
328 /* Allocate the region list on the stack */
329 struct image_region region[count];
331 fit_region_make_list(fit, fdt_regions, count, region);
332 if (info.algo->verify(&info, region, count, fit_value,
334 *err_msgp = "Verification failed";
341 static int fit_config_verify_sig(const void *fit, int conf_noffset,
342 const void *sig_blob, int sig_offset)
349 /* Process all hash subnodes of the component conf node */
350 for (noffset = fdt_first_subnode(fit, conf_noffset);
352 noffset = fdt_next_subnode(fit, noffset)) {
353 const char *name = fit_get_name(fit, noffset, NULL);
355 if (!strncmp(name, FIT_SIG_NODENAME,
356 strlen(FIT_SIG_NODENAME))) {
357 ret = fit_config_check_sig(fit, noffset, sig_offset,
369 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
370 err_msg = "Corrupted or truncated tree";
374 return verified ? 0 : -EPERM;
377 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
378 err_msg, fit_get_name(fit, noffset, NULL),
379 fit_get_name(fit, conf_noffset, NULL));
383 int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
384 const void *sig_blob)
389 /* Work out what we need to verify */
390 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
392 debug("%s: No signature node found: %s\n", __func__,
393 fdt_strerror(sig_node));
397 for (noffset = fdt_first_subnode(sig_blob, sig_node);
399 noffset = fdt_next_subnode(sig_blob, noffset)) {
400 const char *required;
403 required = fdt_getprop(sig_blob, noffset, "required", NULL);
404 if (!required || strcmp(required, "conf"))
406 ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
409 printf("Failed to verify required signature '%s'\n",
410 fit_get_name(sig_blob, noffset, NULL));
418 int fit_config_verify(const void *fit, int conf_noffset)
420 return !fit_config_verify_required_sigs(fit, conf_noffset,