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 checksum_algo *image_get_checksum_algo(const char *full_name)
79 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
80 name = checksum_algos[i].name;
81 /* Make sure names match and next char is a comma */
82 if (!strncmp(name, full_name, strlen(name)) &&
83 full_name[strlen(name)] == ',')
84 return &checksum_algos[i];
90 struct crypto_algo *image_get_crypto_algo(const char *full_name)
95 /* Move name to after the comma */
96 name = strchr(full_name, ',');
101 for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
102 if (!strcmp(crypto_algos[i].name, name))
103 return &crypto_algos[i];
110 * fit_region_make_list() - Make a list of image regions
112 * Given a list of fdt_regions, create a list of image_regions. This is a
113 * simple conversion routine since the FDT and image code use different
117 * @fdt_regions: Pointer to FDT regions
118 * @count: Number of FDT regions
119 * @region: Pointer to image regions, which must hold @count records. If
120 * region is NULL, then (except for an SPL build) the array will be
122 * @return: Pointer to image regions
124 struct image_region *fit_region_make_list(const void *fit,
125 struct fdt_region *fdt_regions, int count,
126 struct image_region *region)
130 debug("Hash regions:\n");
131 debug("%10s %10s\n", "Offset", "Size");
134 * Use malloc() except in SPL (to save code size). In SPL the caller
135 * must allocate the array.
137 #ifndef CONFIG_SPL_BUILD
139 region = calloc(sizeof(*region), count);
143 for (i = 0; i < count; i++) {
144 debug("%10x %10x\n", fdt_regions[i].offset,
145 fdt_regions[i].size);
146 region[i].data = fit + fdt_regions[i].offset;
147 region[i].size = fdt_regions[i].size;
153 static int fit_image_setup_verify(struct image_sign_info *info,
154 const void *fit, int noffset, int required_keynode,
159 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
160 *err_msgp = "Can't get hash algo property";
163 memset(info, '\0', sizeof(*info));
164 info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
165 info->fit = (void *)fit;
166 info->node_offset = noffset;
167 info->name = algo_name;
168 info->checksum = image_get_checksum_algo(algo_name);
169 info->crypto = image_get_crypto_algo(algo_name);
170 info->fdt_blob = gd_fdt_blob();
171 info->required_keynode = required_keynode;
172 printf("%s:%s", algo_name, info->keyname);
174 if (!info->checksum || !info->crypto) {
175 *err_msgp = "Unknown signature algorithm";
182 int fit_image_check_sig(const void *fit, int noffset, const void *data,
183 size_t size, int required_keynode, char **err_msgp)
185 struct image_sign_info info;
186 struct image_region region;
191 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
195 if (fit_image_hash_get_value(fit, noffset, &fit_value,
197 *err_msgp = "Can't get hash value property";
204 if (info.crypto->verify(&info, ®ion, 1, fit_value, fit_value_len)) {
205 *err_msgp = "Verification failed";
212 static int fit_image_verify_sig(const void *fit, int image_noffset,
213 const char *data, size_t size, const void *sig_blob,
221 /* Process all hash subnodes of the component image node */
222 fdt_for_each_subnode(noffset, fit, image_noffset) {
223 const char *name = fit_get_name(fit, noffset, NULL);
225 if (!strncmp(name, FIT_SIG_NODENAME,
226 strlen(FIT_SIG_NODENAME))) {
227 ret = fit_image_check_sig(fit, noffset, data,
239 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
240 err_msg = "Corrupted or truncated tree";
244 return verified ? 0 : -EPERM;
247 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
248 err_msg, fit_get_name(fit, noffset, NULL),
249 fit_get_name(fit, image_noffset, NULL));
253 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
254 const char *data, size_t size, const void *sig_blob,
257 int verify_count = 0;
261 /* Work out what we need to verify */
263 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
265 debug("%s: No signature node found: %s\n", __func__,
266 fdt_strerror(sig_node));
270 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
271 const char *required;
274 required = fdt_getprop(sig_blob, noffset, "required", NULL);
275 if (!required || strcmp(required, "image"))
277 ret = fit_image_verify_sig(fit, image_noffset, data, size,
280 printf("Failed to verify required signature '%s'\n",
281 fit_get_name(sig_blob, noffset, NULL));
293 int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
296 char * const exc_prop[] = {"data"};
297 const char *prop, *end, *name;
298 struct image_sign_info info;
299 const uint32_t *strings;
307 debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
308 fit_get_name(fit, noffset, NULL),
309 fit_get_name(gd_fdt_blob(), required_keynode, NULL));
311 if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
315 if (fit_image_hash_get_value(fit, noffset, &fit_value,
317 *err_msgp = "Can't get hash value property";
321 /* Count the number of strings in the property */
322 prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
323 end = prop ? prop + prop_len : prop;
324 for (name = prop, count = 0; name < end; name++)
328 *err_msgp = "Can't get hashed-nodes property";
332 /* Add a sanity check here since we are using the stack */
333 if (count > IMAGE_MAX_HASHED_NODES) {
334 *err_msgp = "Number of hashed nodes exceeds maximum";
338 /* Create a list of node names from those strings */
339 char *node_inc[count];
341 debug("Hash nodes (%d):\n", count);
342 for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
343 debug(" '%s'\n", name);
344 node_inc[i] = (char *)name;
348 * Each node can generate one region for each sub-node. Allow for
349 * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
351 max_regions = 20 + count * 7;
352 struct fdt_region fdt_regions[max_regions];
354 /* Get a list of regions to hash */
355 count = fdt_find_regions(fit, node_inc, count,
356 exc_prop, ARRAY_SIZE(exc_prop),
357 fdt_regions, max_regions - 1,
358 path, sizeof(path), 0);
360 *err_msgp = "Failed to hash configuration";
364 *err_msgp = "No data to hash";
367 if (count >= max_regions - 1) {
368 *err_msgp = "Too many hash regions";
372 /* Add the strings */
373 strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
375 fdt_regions[count].offset = fdt_off_dt_strings(fit) +
376 fdt32_to_cpu(strings[0]);
377 fdt_regions[count].size = fdt32_to_cpu(strings[1]);
381 /* Allocate the region list on the stack */
382 struct image_region region[count];
384 fit_region_make_list(fit, fdt_regions, count, region);
385 if (info.crypto->verify(&info, region, count, fit_value,
387 *err_msgp = "Verification failed";
394 static int fit_config_verify_sig(const void *fit, int conf_noffset,
395 const void *sig_blob, int sig_offset)
402 /* Process all hash subnodes of the component conf node */
403 fdt_for_each_subnode(noffset, fit, conf_noffset) {
404 const char *name = fit_get_name(fit, noffset, NULL);
406 if (!strncmp(name, FIT_SIG_NODENAME,
407 strlen(FIT_SIG_NODENAME))) {
408 ret = fit_config_check_sig(fit, noffset, sig_offset,
420 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
421 err_msg = "Corrupted or truncated tree";
425 return verified ? 0 : -EPERM;
428 printf(" error!\n%s for '%s' hash node in '%s' config node\n",
429 err_msg, fit_get_name(fit, noffset, NULL),
430 fit_get_name(fit, conf_noffset, NULL));
434 int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
435 const void *sig_blob)
440 /* Work out what we need to verify */
441 sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
443 debug("%s: No signature node found: %s\n", __func__,
444 fdt_strerror(sig_node));
448 fdt_for_each_subnode(noffset, sig_blob, sig_node) {
449 const char *required;
452 required = fdt_getprop(sig_blob, noffset, "required", NULL);
453 if (!required || strcmp(required, "conf"))
455 ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
458 printf("Failed to verify required signature '%s'\n",
459 fit_get_name(sig_blob, noffset, NULL));
467 int fit_config_verify(const void *fit, int conf_noffset)
469 return fit_config_verify_required_sigs(fit, conf_noffset,