2 * Copyright (c) 2013, Google Inc.
4 * SPDX-License-Identifier: GPL-2.0+
13 DECLARE_GLOBAL_DATA_PTR
;
14 #endif /* !USE_HOSTCC*/
16 #include <u-boot/rsa.h>
17 #include <u-boot/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 fdt_for_each_subnode(fit
, noffset
, image_noffset
) {
216 const char *name
= fit_get_name(fit
, noffset
, NULL
);
218 if (!strncmp(name
, FIT_SIG_NODENAME
,
219 strlen(FIT_SIG_NODENAME
))) {
220 ret
= fit_image_check_sig(fit
, noffset
, data
,
232 if (noffset
== -FDT_ERR_TRUNCATED
|| noffset
== -FDT_ERR_BADSTRUCTURE
) {
233 err_msg
= "Corrupted or truncated tree";
237 return verified
? 0 : -EPERM
;
240 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
241 err_msg
, fit_get_name(fit
, noffset
, NULL
),
242 fit_get_name(fit
, image_noffset
, NULL
));
246 int fit_image_verify_required_sigs(const void *fit
, int image_noffset
,
247 const char *data
, size_t size
, const void *sig_blob
,
250 int verify_count
= 0;
254 /* Work out what we need to verify */
256 sig_node
= fdt_subnode_offset(sig_blob
, 0, FIT_SIG_NODENAME
);
258 debug("%s: No signature node found: %s\n", __func__
,
259 fdt_strerror(sig_node
));
263 fdt_for_each_subnode(sig_blob
, noffset
, sig_node
) {
264 const char *required
;
267 required
= fdt_getprop(sig_blob
, noffset
, "required", NULL
);
268 if (!required
|| strcmp(required
, "image"))
270 ret
= fit_image_verify_sig(fit
, image_noffset
, data
, size
,
273 printf("Failed to verify required signature '%s'\n",
274 fit_get_name(sig_blob
, noffset
, NULL
));
286 int fit_config_check_sig(const void *fit
, int noffset
, int required_keynode
,
289 char * const exc_prop
[] = {"data"};
290 const char *prop
, *end
, *name
;
291 struct image_sign_info info
;
292 const uint32_t *strings
;
300 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__
, gd_fdt_blob(),
301 fit_get_name(fit
, noffset
, NULL
),
302 fit_get_name(gd_fdt_blob(), required_keynode
, NULL
));
304 if (fit_image_setup_verify(&info
, fit
, noffset
, required_keynode
,
308 if (fit_image_hash_get_value(fit
, noffset
, &fit_value
,
310 *err_msgp
= "Can't get hash value property";
314 /* Count the number of strings in the property */
315 prop
= fdt_getprop(fit
, noffset
, "hashed-nodes", &prop_len
);
316 end
= prop
? prop
+ prop_len
: prop
;
317 for (name
= prop
, count
= 0; name
< end
; name
++)
321 *err_msgp
= "Can't get hashed-nodes property";
325 /* Add a sanity check here since we are using the stack */
326 if (count
> IMAGE_MAX_HASHED_NODES
) {
327 *err_msgp
= "Number of hashed nodes exceeds maximum";
331 /* Create a list of node names from those strings */
332 char *node_inc
[count
];
334 debug("Hash nodes (%d):\n", count
);
335 for (name
= prop
, i
= 0; name
< end
; name
+= strlen(name
) + 1, i
++) {
336 debug(" '%s'\n", name
);
337 node_inc
[i
] = (char *)name
;
341 * Each node can generate one region for each sub-node. Allow for
342 * 7 sub-nodes (hash@1, signature@1, etc.) and some extra.
344 max_regions
= 20 + count
* 7;
345 struct fdt_region fdt_regions
[max_regions
];
347 /* Get a list of regions to hash */
348 count
= fdt_find_regions(fit
, node_inc
, count
,
349 exc_prop
, ARRAY_SIZE(exc_prop
),
350 fdt_regions
, max_regions
- 1,
351 path
, sizeof(path
), 0);
353 *err_msgp
= "Failed to hash configuration";
357 *err_msgp
= "No data to hash";
360 if (count
>= max_regions
- 1) {
361 *err_msgp
= "Too many hash regions";
365 /* Add the strings */
366 strings
= fdt_getprop(fit
, noffset
, "hashed-strings", NULL
);
368 fdt_regions
[count
].offset
= fdt_off_dt_strings(fit
) +
369 fdt32_to_cpu(strings
[0]);
370 fdt_regions
[count
].size
= fdt32_to_cpu(strings
[1]);
374 /* Allocate the region list on the stack */
375 struct image_region region
[count
];
377 fit_region_make_list(fit
, fdt_regions
, count
, region
);
378 if (info
.algo
->verify(&info
, region
, count
, fit_value
,
380 *err_msgp
= "Verification failed";
387 static int fit_config_verify_sig(const void *fit
, int conf_noffset
,
388 const void *sig_blob
, int sig_offset
)
395 /* Process all hash subnodes of the component conf node */
396 fdt_for_each_subnode(fit
, noffset
, conf_noffset
) {
397 const char *name
= fit_get_name(fit
, noffset
, NULL
);
399 if (!strncmp(name
, FIT_SIG_NODENAME
,
400 strlen(FIT_SIG_NODENAME
))) {
401 ret
= fit_config_check_sig(fit
, noffset
, sig_offset
,
413 if (noffset
== -FDT_ERR_TRUNCATED
|| noffset
== -FDT_ERR_BADSTRUCTURE
) {
414 err_msg
= "Corrupted or truncated tree";
418 return verified
? 0 : -EPERM
;
421 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
422 err_msg
, fit_get_name(fit
, noffset
, NULL
),
423 fit_get_name(fit
, conf_noffset
, NULL
));
427 int fit_config_verify_required_sigs(const void *fit
, int conf_noffset
,
428 const void *sig_blob
)
433 /* Work out what we need to verify */
434 sig_node
= fdt_subnode_offset(sig_blob
, 0, FIT_SIG_NODENAME
);
436 debug("%s: No signature node found: %s\n", __func__
,
437 fdt_strerror(sig_node
));
441 fdt_for_each_subnode(sig_blob
, noffset
, sig_node
) {
442 const char *required
;
445 required
= fdt_getprop(sig_blob
, noffset
, "required", NULL
);
446 if (!required
|| strcmp(required
, "conf"))
448 ret
= fit_config_verify_sig(fit
, conf_noffset
, sig_blob
,
451 printf("Failed to verify required signature '%s'\n",
452 fit_get_name(sig_blob
, noffset
, NULL
));
460 int fit_config_verify(const void *fit
, int conf_noffset
)
462 return fit_config_verify_required_sigs(fit
, conf_noffset
,