Blame view
common/image-sig.c
13.8 KB
83d290c56 SPDX: Convert all... |
1 |
// SPDX-License-Identifier: GPL-2.0+ |
3e569a6b1 image: Add signin... |
2 3 |
/* * Copyright (c) 2013, Google Inc. |
3e569a6b1 image: Add signin... |
4 5 6 7 8 9 10 |
*/ #ifdef USE_HOSTCC #include "mkimage.h" #include <time.h> #else #include <common.h> |
56518e710 image: Support si... |
11 12 |
#include <malloc.h> DECLARE_GLOBAL_DATA_PTR; |
3e569a6b1 image: Add signin... |
13 |
#endif /* !USE_HOSTCC*/ |
3e569a6b1 image: Add signin... |
14 |
#include <image.h> |
2b9912e6a includes: move op... |
15 16 |
#include <u-boot/rsa.h> #include <u-boot/rsa-checksum.h> |
3e569a6b1 image: Add signin... |
17 |
|
4d0985295 image: Add suppor... |
18 |
#define IMAGE_MAX_HASHED_NODES 100 |
646257d1f rsa: add sha256-r... |
19 |
#ifdef USE_HOSTCC |
29a23f9d6 tools, fit_check_... |
20 21 |
void *host_blob; void image_set_host_blob(void *blob) |
646257d1f rsa: add sha256-r... |
22 |
{ |
29a23f9d6 tools, fit_check_... |
23 24 25 26 27 |
host_blob = blob; } void *image_get_host_blob(void) { return host_blob; |
646257d1f rsa: add sha256-r... |
28 29 30 31 32 |
} #endif struct checksum_algo checksum_algos[] = { { |
8ec87df37 image-sig: use de... |
33 34 35 36 |
.name = "sha1", .checksum_len = SHA1_SUM_LEN, .der_len = SHA1_DER_LEN, .der_prefix = sha1_der_prefix, |
646257d1f rsa: add sha256-r... |
37 |
#if IMAGE_ENABLE_SIGN |
8ec87df37 image-sig: use de... |
38 |
.calculate_sign = EVP_sha1, |
29a23f9d6 tools, fit_check_... |
39 |
#endif |
8ec87df37 image-sig: use de... |
40 |
.calculate = hash_calculate, |
646257d1f rsa: add sha256-r... |
41 42 |
}, { |
8ec87df37 image-sig: use de... |
43 44 45 46 |
.name = "sha256", .checksum_len = SHA256_SUM_LEN, .der_len = SHA256_DER_LEN, .der_prefix = sha256_der_prefix, |
646257d1f rsa: add sha256-r... |
47 |
#if IMAGE_ENABLE_SIGN |
8ec87df37 image-sig: use de... |
48 |
.calculate_sign = EVP_sha256, |
29a23f9d6 tools, fit_check_... |
49 |
#endif |
8ec87df37 image-sig: use de... |
50 |
.calculate = hash_calculate, |
0c1d74fda image: Add crypto... |
51 52 53 54 55 56 |
} }; struct crypto_algo crypto_algos[] = { { |
8ec87df37 image-sig: use de... |
57 58 59 60 61 |
.name = "rsa2048", .key_len = RSA2048_BYTES, .sign = rsa_sign, .add_verify_data = rsa_add_verify_data, .verify = rsa_verify, |
db1b5f3d2 rsa: add sha256,r... |
62 63 |
}, { |
8ec87df37 image-sig: use de... |
64 65 66 67 68 |
.name = "rsa4096", .key_len = RSA4096_BYTES, .sign = rsa_sign, .add_verify_data = rsa_add_verify_data, .verify = rsa_verify, |
646257d1f rsa: add sha256-r... |
69 |
} |
db1b5f3d2 rsa: add sha256,r... |
70 |
|
646257d1f rsa: add sha256-r... |
71 |
}; |
db1b5f3d2 rsa: add sha256,r... |
72 |
|
20031567e rsa: add a struct... |
73 74 75 76 77 |
struct padding_algo padding_algos[] = { { .name = "pkcs-1.5", .verify = padding_pkcs_15_verify, }, |
061daa0b6 rsa: add support ... |
78 79 80 81 82 83 |
#ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT { .name = "pss", .verify = padding_pss_verify, } #endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */ |
20031567e rsa: add a struct... |
84 |
}; |
83dd98e01 image: Combine im... |
85 86 87 88 |
struct checksum_algo *image_get_checksum_algo(const char *full_name) { int i; const char *name; |
1ed8c1373 common: image-sig... |
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC) static bool done; if (!done) { done = true; for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) { checksum_algos[i].name += gd->reloc_off; #if IMAGE_ENABLE_SIGN checksum_algos[i].calculate_sign += gd->reloc_off; #endif checksum_algos[i].calculate += gd->reloc_off; } } #endif |
83dd98e01 image: Combine im... |
103 104 105 106 107 108 |
for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) { name = checksum_algos[i].name; /* Make sure names match and next char is a comma */ if (!strncmp(name, full_name, strlen(name)) && full_name[strlen(name)] == ',') return &checksum_algos[i]; |
19c402afa image: Add RSA su... |
109 |
} |
db1b5f3d2 rsa: add sha256,r... |
110 |
|
83dd98e01 image: Combine im... |
111 112 |
return NULL; } |
3e569a6b1 image: Add signin... |
113 |
|
83dd98e01 image: Combine im... |
114 |
struct crypto_algo *image_get_crypto_algo(const char *full_name) |
3e569a6b1 image: Add signin... |
115 116 |
{ int i; |
83dd98e01 image: Combine im... |
117 |
const char *name; |
1ed8c1373 common: image-sig... |
118 119 120 121 122 123 124 125 126 127 128 129 130 |
#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC) static bool done; if (!done) { done = true; for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) { crypto_algos[i].name += gd->reloc_off; crypto_algos[i].sign += gd->reloc_off; crypto_algos[i].add_verify_data += gd->reloc_off; crypto_algos[i].verify += gd->reloc_off; } } #endif |
83dd98e01 image: Combine im... |
131 132 133 134 135 |
/* Move name to after the comma */ name = strchr(full_name, ','); if (!name) return NULL; name += 1; |
3e569a6b1 image: Add signin... |
136 |
|
83dd98e01 image: Combine im... |
137 138 139 |
for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) { if (!strcmp(crypto_algos[i].name, name)) return &crypto_algos[i]; |
3e569a6b1 image: Add signin... |
140 141 142 143 |
} return NULL; } |
56518e710 image: Support si... |
144 |
|
20031567e rsa: add a struct... |
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
struct padding_algo *image_get_padding_algo(const char *name) { int i; if (!name) return NULL; for (i = 0; i < ARRAY_SIZE(padding_algos); i++) { if (!strcmp(padding_algos[i].name, name)) return &padding_algos[i]; } return NULL; } |
4d0985295 image: Add suppor... |
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
/** * fit_region_make_list() - Make a list of image regions * * Given a list of fdt_regions, create a list of image_regions. This is a * simple conversion routine since the FDT and image code use different * structures. * * @fit: FIT image * @fdt_regions: Pointer to FDT regions * @count: Number of FDT regions * @region: Pointer to image regions, which must hold @count records. If * region is NULL, then (except for an SPL build) the array will be * allocated. * @return: Pointer to image regions */ struct image_region *fit_region_make_list(const void *fit, struct fdt_region *fdt_regions, int count, struct image_region *region) { int i; debug("Hash regions: "); debug("%10s %10s ", "Offset", "Size"); /* * Use malloc() except in SPL (to save code size). In SPL the caller * must allocate the array. */ #ifndef CONFIG_SPL_BUILD if (!region) region = calloc(sizeof(*region), count); #endif if (!region) return NULL; for (i = 0; i < count; i++) { debug("%10x %10x ", fdt_regions[i].offset, fdt_regions[i].size); region[i].data = fit + fdt_regions[i].offset; region[i].size = fdt_regions[i].size; } return region; } |
56518e710 image: Support si... |
205 206 207 208 209 |
static int fit_image_setup_verify(struct image_sign_info *info, const void *fit, int noffset, int required_keynode, char **err_msgp) { char *algo_name; |
20031567e rsa: add a struct... |
210 |
const char *padding_name; |
56518e710 image: Support si... |
211 |
|
72239fc85 vboot: Add FIT_SI... |
212 213 214 215 |
if (fdt_totalsize(fit) > CONFIG_FIT_SIGNATURE_MAX_SIZE) { *err_msgp = "Total size too large"; return 1; } |
56518e710 image: Support si... |
216 217 218 219 |
if (fit_image_hash_get_algo(fit, noffset, &algo_name)) { *err_msgp = "Can't get hash algo property"; return -1; } |
20031567e rsa: add a struct... |
220 221 222 223 |
padding_name = fdt_getprop(fit, noffset, "padding", NULL); if (!padding_name) padding_name = RSA_DEFAULT_PADDING_NAME; |
56518e710 image: Support si... |
224 |
memset(info, '\0', sizeof(*info)); |
72188f546 image: Use consta... |
225 |
info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
56518e710 image: Support si... |
226 227 |
info->fit = (void *)fit; info->node_offset = noffset; |
83dd98e01 image: Combine im... |
228 229 230 |
info->name = algo_name; info->checksum = image_get_checksum_algo(algo_name); info->crypto = image_get_crypto_algo(algo_name); |
20031567e rsa: add a struct... |
231 |
info->padding = image_get_padding_algo(padding_name); |
56518e710 image: Support si... |
232 233 234 |
info->fdt_blob = gd_fdt_blob(); info->required_keynode = required_keynode; printf("%s:%s", algo_name, info->keyname); |
19495dd9b rsa: reject image... |
235 |
if (!info->checksum || !info->crypto || !info->padding) { |
56518e710 image: Support si... |
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
*err_msgp = "Unknown signature algorithm"; return -1; } return 0; } int fit_image_check_sig(const void *fit, int noffset, const void *data, size_t size, int required_keynode, char **err_msgp) { struct image_sign_info info; struct image_region region; uint8_t *fit_value; int fit_value_len; *err_msgp = NULL; if (fit_image_setup_verify(&info, fit, noffset, required_keynode, err_msgp)) return -1; if (fit_image_hash_get_value(fit, noffset, &fit_value, &fit_value_len)) { *err_msgp = "Can't get hash value property"; return -1; } region.data = data; region.size = size; |
83dd98e01 image: Combine im... |
264 |
if (info.crypto->verify(&info, ®ion, 1, fit_value, fit_value_len)) { |
56518e710 image: Support si... |
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
*err_msgp = "Verification failed"; return -1; } return 0; } static int fit_image_verify_sig(const void *fit, int image_noffset, const char *data, size_t size, const void *sig_blob, int sig_offset) { int noffset; char *err_msg = ""; int verified = 0; int ret; /* Process all hash subnodes of the component image node */ |
df87e6b1b libfdt: Sync fdt_... |
282 |
fdt_for_each_subnode(noffset, fit, image_noffset) { |
56518e710 image: Support si... |
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
const char *name = fit_get_name(fit, noffset, NULL); if (!strncmp(name, FIT_SIG_NODENAME, strlen(FIT_SIG_NODENAME))) { ret = fit_image_check_sig(fit, noffset, data, size, -1, &err_msg); if (ret) { puts("- "); } else { puts("+ "); verified = 1; break; } } } if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { err_msg = "Corrupted or truncated tree"; goto error; } return verified ? 0 : -EPERM; error: printf(" error! %s for '%s' hash node in '%s' image node ", err_msg, fit_get_name(fit, noffset, NULL), fit_get_name(fit, image_noffset, NULL)); return -1; } int fit_image_verify_required_sigs(const void *fit, int image_noffset, const char *data, size_t size, const void *sig_blob, int *no_sigsp) { int verify_count = 0; int noffset; int sig_node; /* Work out what we need to verify */ *no_sigsp = 1; sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME); if (sig_node < 0) { debug("%s: No signature node found: %s ", __func__, fdt_strerror(sig_node)); return 0; } |
df87e6b1b libfdt: Sync fdt_... |
332 |
fdt_for_each_subnode(noffset, sig_blob, sig_node) { |
56518e710 image: Support si... |
333 334 |
const char *required; int ret; |
72188f546 image: Use consta... |
335 336 |
required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED, NULL); |
56518e710 image: Support si... |
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
if (!required || strcmp(required, "image")) continue; ret = fit_image_verify_sig(fit, image_noffset, data, size, sig_blob, noffset); if (ret) { printf("Failed to verify required signature '%s' ", fit_get_name(sig_blob, noffset, NULL)); return ret; } verify_count++; } if (verify_count) *no_sigsp = 0; return 0; } |
4d0985295 image: Add suppor... |
355 |
|
67acad3db image: Check hash... |
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
/** * fit_config_check_sig() - Check the signature of a config * * @fit: FIT to check * @noffset: Offset of configuration node (e.g. /configurations/conf-1) * @required_keynode: Offset in the control FDT of the required key node, * if any. If this is given, then the configuration wil not * pass verification unless that key is used. If this is * -1 then any signature will do. * @conf_noffset: Offset of the configuration subnode being checked (e.g. * /configurations/conf-1/kernel) * @err_msgp: In the event of an error, this will be pointed to a * help error string to display to the user. * @return 0 if all verified ok, <0 on error */ static int fit_config_check_sig(const void *fit, int noffset, int required_keynode, int conf_noffset, char **err_msgp) |
4d0985295 image: Add suppor... |
374 375 376 377 378 |
{ char * const exc_prop[] = {"data"}; const char *prop, *end, *name; struct image_sign_info info; const uint32_t *strings; |
67acad3db image: Check hash... |
379 |
const char *config_name; |
4d0985295 image: Add suppor... |
380 381 |
uint8_t *fit_value; int fit_value_len; |
67acad3db image: Check hash... |
382 |
bool found_config; |
4d0985295 image: Add suppor... |
383 384 385 386 |
int max_regions; int i, prop_len; char path[200]; int count; |
67acad3db image: Check hash... |
387 |
config_name = fit_get_name(fit, conf_noffset, NULL); |
4d0985295 image: Add suppor... |
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
debug("%s: fdt=%p, conf='%s', sig='%s' ", __func__, gd_fdt_blob(), fit_get_name(fit, noffset, NULL), fit_get_name(gd_fdt_blob(), required_keynode, NULL)); *err_msgp = NULL; if (fit_image_setup_verify(&info, fit, noffset, required_keynode, err_msgp)) return -1; if (fit_image_hash_get_value(fit, noffset, &fit_value, &fit_value_len)) { *err_msgp = "Can't get hash value property"; return -1; } /* Count the number of strings in the property */ prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len); end = prop ? prop + prop_len : prop; for (name = prop, count = 0; name < end; name++) if (!*name) count++; if (!count) { *err_msgp = "Can't get hashed-nodes property"; return -1; } |
f1c85688a image-sig: Ensure... |
413 414 415 416 |
if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') { *err_msgp = "hashed-nodes property must be null-terminated"; return -1; } |
4d0985295 image: Add suppor... |
417 418 419 420 421 422 423 424 425 426 427 |
/* Add a sanity check here since we are using the stack */ if (count > IMAGE_MAX_HASHED_NODES) { *err_msgp = "Number of hashed nodes exceeds maximum"; return -1; } /* Create a list of node names from those strings */ char *node_inc[count]; debug("Hash nodes (%d): ", count); |
67acad3db image: Check hash... |
428 |
found_config = false; |
4d0985295 image: Add suppor... |
429 430 431 432 |
for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) { debug(" '%s' ", name); node_inc[i] = (char *)name; |
67acad3db image: Check hash... |
433 434 435 436 437 438 439 440 441 442 |
if (!strncmp(FIT_CONFS_PATH, name, strlen(FIT_CONFS_PATH)) && name[sizeof(FIT_CONFS_PATH) - 1] == '/' && !strcmp(name + sizeof(FIT_CONFS_PATH), config_name)) { debug(" (found config node %s)", config_name); found_config = true; } } if (!found_config) { *err_msgp = "Selected config not in hashed nodes"; return -1; |
4d0985295 image: Add suppor... |
443 444 445 446 |
} /* * Each node can generate one region for each sub-node. Allow for |
b2267e8a2 fix incorrect usa... |
447 |
* 7 sub-nodes (hash-1, signature-1, etc.) and some extra. |
4d0985295 image: Add suppor... |
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
*/ max_regions = 20 + count * 7; struct fdt_region fdt_regions[max_regions]; /* Get a list of regions to hash */ count = fdt_find_regions(fit, node_inc, count, exc_prop, ARRAY_SIZE(exc_prop), fdt_regions, max_regions - 1, path, sizeof(path), 0); if (count < 0) { *err_msgp = "Failed to hash configuration"; return -1; } if (count == 0) { *err_msgp = "No data to hash"; return -1; } if (count >= max_regions - 1) { *err_msgp = "Too many hash regions"; return -1; } /* Add the strings */ strings = fdt_getprop(fit, noffset, "hashed-strings", NULL); if (strings) { |
7346c1e19 vboot: Do not use... |
473 474 475 476 477 |
/* * The strings region offset must be a static 0x0. * This is set in tool/image-host.c */ fdt_regions[count].offset = fdt_off_dt_strings(fit); |
4d0985295 image: Add suppor... |
478 479 480 481 482 483 484 485 |
fdt_regions[count].size = fdt32_to_cpu(strings[1]); count++; } /* Allocate the region list on the stack */ struct image_region region[count]; fit_region_make_list(fit, fdt_regions, count, region); |
83dd98e01 image: Combine im... |
486 487 |
if (info.crypto->verify(&info, region, count, fit_value, fit_value_len)) { |
4d0985295 image: Add suppor... |
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
*err_msgp = "Verification failed"; return -1; } return 0; } static int fit_config_verify_sig(const void *fit, int conf_noffset, const void *sig_blob, int sig_offset) { int noffset; char *err_msg = ""; int verified = 0; int ret; /* Process all hash subnodes of the component conf node */ |
df87e6b1b libfdt: Sync fdt_... |
504 |
fdt_for_each_subnode(noffset, fit, conf_noffset) { |
4d0985295 image: Add suppor... |
505 506 507 508 509 |
const char *name = fit_get_name(fit, noffset, NULL); if (!strncmp(name, FIT_SIG_NODENAME, strlen(FIT_SIG_NODENAME))) { ret = fit_config_check_sig(fit, noffset, sig_offset, |
67acad3db image: Check hash... |
510 |
conf_noffset, &err_msg); |
4d0985295 image: Add suppor... |
511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
if (ret) { puts("- "); } else { puts("+ "); verified = 1; break; } } } if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { err_msg = "Corrupted or truncated tree"; goto error; } |
472f9113d image: Return an ... |
525 526 |
if (verified) return 0; |
4d0985295 image: Add suppor... |
527 528 529 530 531 532 533 |
error: printf(" error! %s for '%s' hash node in '%s' config node ", err_msg, fit_get_name(fit, noffset, NULL), fit_get_name(fit, conf_noffset, NULL)); |
472f9113d image: Return an ... |
534 |
return -EPERM; |
4d0985295 image: Add suppor... |
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
} int fit_config_verify_required_sigs(const void *fit, int conf_noffset, const void *sig_blob) { int noffset; int sig_node; /* Work out what we need to verify */ sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME); if (sig_node < 0) { debug("%s: No signature node found: %s ", __func__, fdt_strerror(sig_node)); return 0; } |
df87e6b1b libfdt: Sync fdt_... |
551 |
fdt_for_each_subnode(noffset, sig_blob, sig_node) { |
4d0985295 image: Add suppor... |
552 553 |
const char *required; int ret; |
72188f546 image: Use consta... |
554 555 |
required = fdt_getprop(sig_blob, noffset, FIT_KEY_REQUIRED, NULL); |
4d0985295 image: Add suppor... |
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 |
if (!required || strcmp(required, "conf")) continue; ret = fit_config_verify_sig(fit, conf_noffset, sig_blob, noffset); if (ret) { printf("Failed to verify required signature '%s' ", fit_get_name(sig_blob, noffset, NULL)); return ret; } } return 0; } int fit_config_verify(const void *fit, int conf_noffset) { |
12df2abe3 Reverse the meani... |
573 574 |
return fit_config_verify_required_sigs(fit, conf_noffset, gd_fdt_blob()); |
4d0985295 image: Add suppor... |
575 |
} |