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
22 void image_set_host_blob(void *blob)
26 void *image_get_host_blob(void)
32 struct checksum_algo checksum_algos[] = {
35 .checksum_len = SHA1_SUM_LEN,
36 .der_len = SHA1_DER_LEN,
37 .der_prefix = sha1_der_prefix,
39 .calculate_sign = EVP_sha1,
41 .calculate = hash_calculate,
45 .checksum_len = SHA256_SUM_LEN,
46 .der_len = SHA256_DER_LEN,
47 .der_prefix = sha256_der_prefix,
49 .calculate_sign = EVP_sha256,
51 .calculate = hash_calculate,
56 struct crypto_algo crypto_algos[] = {
59 .key_len = RSA2048_BYTES,
61 .add_verify_data = rsa_add_verify_data,
66 .key_len = RSA4096_BYTES,
68 .add_verify_data = rsa_add_verify_data,
74 struct padding_algo padding_algos[] = {
77 .verify = padding_pkcs_15_verify,
79 #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
82 .verify = padding_pss_verify,
84 #endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */
87 struct checksum_algo *image_get_checksum_algo(const char *full_name)
92 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
97 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
98 checksum_algos[i].name += gd->reloc_off;
100 checksum_algos[i].calculate_sign += gd->reloc_off;
102 checksum_algos[i].calculate += gd->reloc_off;
107 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
108 name = checksum_algos[i].name;
109 /* Make sure names match and next char is a comma */
110 if (!strncmp(name, full_name, strlen(name)) &&
111 full_name[strlen(name)] == ',')
112 return &checksum_algos[i];
118 struct crypto_algo *image_get_crypto_algo(const char *full_name)
123 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
128 for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
129 crypto_algos[i].name += gd->reloc_off;
130 crypto_algos[i].sign += gd->reloc_off;
131 crypto_algos[i].add_verify_data += gd->reloc_off;
132 crypto_algos[i].verify += gd->reloc_off;
137 /* Move name to after the comma */
138 name = strchr(full_name, ',');
143 for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
144 if (!strcmp(crypto_algos[i].name, name))
145 return &crypto_algos[i];
151 struct padding_algo *image_get_padding_algo(const char *name)
158 for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
159 if (!strcmp(padding_algos[i].name, name))
160 return &padding_algos[i];
167 * fit_region_make_list() - Make a list of image regions
169 * Given a list of fdt_regions, create a list of image_regions. This is a
170 * simple conversion routine since the FDT and image code use different
174 * @fdt_regions: Pointer to FDT regions
175 * @count: Number of FDT regions
176 * @region: Pointer to image regions, which must hold @count records. If
177 * region is NULL, then (except for an SPL build) the array will be
179 * @return: Pointer to image regions
181 struct image_region *fit_region_make_list(const void *fit,
182 struct fdt_region *fdt_regions, int count,
183 struct image_region *region)
187 debug("Hash regions:\n");
188 debug("%10s %10s\n", "Offset", "Size");
191 * Use malloc() except in SPL (to save code size). In SPL the caller
192 * must allocate the array.
194 #ifndef CONFIG_SPL_BUILD
196 region = calloc(sizeof(*region), count);
200 for (i = 0; i < count; i++) {
201 debug("%10x %10x\n", fdt_regions[i].offset,
202 fdt_regions[i].size);
203 region[i].data = fit + fdt_regions[i].offset;
204 region[i].size = fdt_regions[i].size;
210 static int fit_image_setup_verify(struct image_sign_info *info,
211 const void *fit, int noffset, int required_keynode,
215 const char *padding_name;
217 if (fdt_totalsize(fit) > CONFIG_FIT_SIGNATURE_MAX_SIZE) {
218 *err_msgp = "Total size too large";
222 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
223 *err_msgp = "Can't get hash algo property";
227 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
229 padding_name = RSA_DEFAULT_PADDING_NAME;
231 memset(info, '\0', sizeof(*info));
232 info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
233 info->fit = (void *)fit;
234 info->node_offset = noffset;
235 info->name = algo_name;
236 info->checksum = image_get_checksum_algo(algo_name);
237 info->crypto = image_get_crypto_algo(algo_name);
238 info->padding = image_get_padding_algo(padding_name);
239 info->fdt_blob = gd_fdt_blob();
240 info->required_keynode = required_keynode;
241 printf("%s:%s", algo_name, info->keyname);
243 if (!info->checksum || !info->crypto || !info->padding) {
244 *err_msgp = "Unknown signature algorithm";
251 int fit_image_check_sig(const void *fit, int noffset, const void *data,
252 size_t size, int required_keynode, char **err_msgp)
254 struct image_sign_info info;
255 struct image_region region;
260 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
264 if (fit_image_hash_get_value(fit, noffset, &fit_value,
266 *err_msgp = "Can't get hash value property";
273 if (info.crypto->verify(&info, ®ion, 1, fit_value, fit_value_len)) {
274 *err_msgp = "Verification failed";
281 static int fit_image_verify_sig(const void *fit, int image_noffset,
282 const char *data, size_t size, const void *sig_blob,
290 /* Process all hash subnodes of the component image node */
291 fdt_for_each_subnode(noffset, fit, image_noffset) {
292 const char *name = fit_get_name(fit, noffset, NULL);
294 if (!strncmp(name, FIT_SIG_NODENAME,
295 strlen(FIT_SIG_NODENAME))) {
296 ret = fit_image_check_sig(fit, noffset, data,
308 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
309 err_msg = "Corrupted or truncated tree";
313 return verified ? 0 : -EPERM;
316 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
317 err_msg, fit_get_name(fit, noffset, NULL),
318 fit_get_name(fit, image_noffset, NULL));
322 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
323 const char *data, size_t size, const void *sig_blob,
326 int verify_count = 0;
330 /* Work out what we need to verify */
332 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
334 debug("%s: No signature node found: %s\n", __func__,
335 fdt_strerror(sig_node));
339 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
340 const char *required;
343 required = fdt_getprop(sig_blob, noffset, "required", NULL);
344 if (!required || strcmp(required, "image"))
346 ret = fit_image_verify_sig(fit, image_noffset, data, size,
349 printf("Failed to verify required signature '%s'\n",
350 fit_get_name(sig_blob, noffset, NULL));
362 int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
365 char * const exc_prop[] = {"data"};
366 const char *prop, *end, *name;
367 struct image_sign_info info;
368 const uint32_t *strings;
376 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
377 fit_get_name(fit, noffset, NULL),
378 fit_get_name(gd_fdt_blob(), required_keynode, NULL));
380 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
384 if (fit_image_hash_get_value(fit, noffset, &fit_value,
386 *err_msgp = "Can't get hash value property";
390 /* Count the number of strings in the property */
391 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
392 end = prop ? prop + prop_len : prop;
393 for (name = prop, count = 0; name < end; name++)
397 *err_msgp = "Can't get hashed-nodes property";
401 if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
402 *err_msgp = "hashed-nodes property must be null-terminated";
406 /* Add a sanity check here since we are using the stack */
407 if (count > IMAGE_MAX_HASHED_NODES) {
408 *err_msgp = "Number of hashed nodes exceeds maximum";
412 /* Create a list of node names from those strings */
413 char *node_inc[count];
415 debug("Hash nodes (%d):\n", count);
416 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
417 debug(" '%s'\n", name);
418 node_inc[i] = (char *)name;
422 * Each node can generate one region for each sub-node. Allow for
423 * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
425 max_regions = 20 + count * 7;
426 struct fdt_region fdt_regions[max_regions];
428 /* Get a list of regions to hash */
429 count = fdt_find_regions(fit, node_inc, count,
430 exc_prop, ARRAY_SIZE(exc_prop),
431 fdt_regions, max_regions - 1,
432 path, sizeof(path), 0);
434 *err_msgp = "Failed to hash configuration";
438 *err_msgp = "No data to hash";
441 if (count >= max_regions - 1) {
442 *err_msgp = "Too many hash regions";
446 /* Add the strings */
447 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
450 * The strings region offset must be a static 0x0.
451 * This is set in tool/image-host.c
453 fdt_regions[count].offset = fdt_off_dt_strings(fit);
454 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
458 /* Allocate the region list on the stack */
459 struct image_region region[count];
461 fit_region_make_list(fit, fdt_regions, count, region);
462 if (info.crypto->verify(&info, region, count, fit_value,
464 *err_msgp = "Verification failed";
471 static int fit_config_verify_sig(const void *fit, int conf_noffset,
472 const void *sig_blob, int sig_offset)
479 /* Process all hash subnodes of the component conf node */
480 fdt_for_each_subnode(noffset, fit, conf_noffset) {
481 const char *name = fit_get_name(fit, noffset, NULL);
483 if (!strncmp(name, FIT_SIG_NODENAME,
484 strlen(FIT_SIG_NODENAME))) {
485 ret = fit_config_check_sig(fit, noffset, sig_offset,
497 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
498 err_msg = "Corrupted or truncated tree";
502 return verified ? 0 : -EPERM;
505 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
506 err_msg, fit_get_name(fit, noffset, NULL),
507 fit_get_name(fit, conf_noffset, NULL));
511 int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
512 const void *sig_blob)
517 /* Work out what we need to verify */
518 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
520 debug("%s: No signature node found: %s\n", __func__,
521 fdt_strerror(sig_node));
525 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
526 const char *required;
529 required = fdt_getprop(sig_blob, noffset, "required", NULL);
530 if (!required || strcmp(required, "conf"))
532 ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
535 printf("Failed to verify required signature '%s'\n",
536 fit_get_name(sig_blob, noffset, NULL));
544 int fit_config_verify(const void *fit, int conf_noffset)
546 return fit_config_verify_required_sigs(fit, conf_noffset,