image-sig.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013, Google Inc.
  4. */
  5. #ifdef USE_HOSTCC
  6. #include "mkimage.h"
  7. #include <time.h>
  8. #else
  9. #include <common.h>
  10. #include <malloc.h>
  11. DECLARE_GLOBAL_DATA_PTR;
  12. #endif /* !USE_HOSTCC*/
  13. #include <image.h>
  14. #include <u-boot/rsa.h>
  15. #include <u-boot/rsa-checksum.h>
  16. #define IMAGE_MAX_HASHED_NODES 100
  17. #ifdef USE_HOSTCC
  18. void *host_blob;
  19. void image_set_host_blob(void *blob)
  20. {
  21. host_blob = blob;
  22. }
  23. void *image_get_host_blob(void)
  24. {
  25. return host_blob;
  26. }
  27. #endif
  28. struct checksum_algo checksum_algos[] = {
  29. {
  30. .name = "sha1",
  31. .checksum_len = SHA1_SUM_LEN,
  32. .der_len = SHA1_DER_LEN,
  33. .der_prefix = sha1_der_prefix,
  34. #if IMAGE_ENABLE_SIGN
  35. .calculate_sign = EVP_sha1,
  36. #endif
  37. .calculate = hash_calculate,
  38. },
  39. {
  40. .name = "sha256",
  41. .checksum_len = SHA256_SUM_LEN,
  42. .der_len = SHA256_DER_LEN,
  43. .der_prefix = sha256_der_prefix,
  44. #if IMAGE_ENABLE_SIGN
  45. .calculate_sign = EVP_sha256,
  46. #endif
  47. .calculate = hash_calculate,
  48. }
  49. };
  50. struct crypto_algo crypto_algos[] = {
  51. {
  52. .name = "rsa2048",
  53. .key_len = RSA2048_BYTES,
  54. .sign = rsa_sign,
  55. .add_verify_data = rsa_add_verify_data,
  56. .verify = rsa_verify,
  57. },
  58. {
  59. .name = "rsa4096",
  60. .key_len = RSA4096_BYTES,
  61. .sign = rsa_sign,
  62. .add_verify_data = rsa_add_verify_data,
  63. .verify = rsa_verify,
  64. }
  65. };
  66. struct padding_algo padding_algos[] = {
  67. {
  68. .name = "pkcs-1.5",
  69. .verify = padding_pkcs_15_verify,
  70. },
  71. #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
  72. {
  73. .name = "pss",
  74. .verify = padding_pss_verify,
  75. }
  76. #endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */
  77. };
  78. struct checksum_algo *image_get_checksum_algo(const char *full_name)
  79. {
  80. int i;
  81. const char *name;
  82. #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
  83. static bool done;
  84. if (!done) {
  85. done = true;
  86. for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
  87. checksum_algos[i].name += gd->reloc_off;
  88. #if IMAGE_ENABLE_SIGN
  89. checksum_algos[i].calculate_sign += gd->reloc_off;
  90. #endif
  91. checksum_algos[i].calculate += gd->reloc_off;
  92. }
  93. }
  94. #endif
  95. for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
  96. name = checksum_algos[i].name;
  97. /* Make sure names match and next char is a comma */
  98. if (!strncmp(name, full_name, strlen(name)) &&
  99. full_name[strlen(name)] == ',')
  100. return &checksum_algos[i];
  101. }
  102. return NULL;
  103. }
  104. struct crypto_algo *image_get_crypto_algo(const char *full_name)
  105. {
  106. int i;
  107. const char *name;
  108. #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
  109. static bool done;
  110. if (!done) {
  111. done = true;
  112. for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
  113. crypto_algos[i].name += gd->reloc_off;
  114. crypto_algos[i].sign += gd->reloc_off;
  115. crypto_algos[i].add_verify_data += gd->reloc_off;
  116. crypto_algos[i].verify += gd->reloc_off;
  117. }
  118. }
  119. #endif
  120. /* Move name to after the comma */
  121. name = strchr(full_name, ',');
  122. if (!name)
  123. return NULL;
  124. name += 1;
  125. for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
  126. if (!strcmp(crypto_algos[i].name, name))
  127. return &crypto_algos[i];
  128. }
  129. return NULL;
  130. }
  131. struct padding_algo *image_get_padding_algo(const char *name)
  132. {
  133. int i;
  134. if (!name)
  135. return NULL;
  136. for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
  137. if (!strcmp(padding_algos[i].name, name))
  138. return &padding_algos[i];
  139. }
  140. return NULL;
  141. }
  142. /**
  143. * fit_region_make_list() - Make a list of image regions
  144. *
  145. * Given a list of fdt_regions, create a list of image_regions. This is a
  146. * simple conversion routine since the FDT and image code use different
  147. * structures.
  148. *
  149. * @fit: FIT image
  150. * @fdt_regions: Pointer to FDT regions
  151. * @count: Number of FDT regions
  152. * @region: Pointer to image regions, which must hold @count records. If
  153. * region is NULL, then (except for an SPL build) the array will be
  154. * allocated.
  155. * @return: Pointer to image regions
  156. */
  157. struct image_region *fit_region_make_list(const void *fit,
  158. struct fdt_region *fdt_regions, int count,
  159. struct image_region *region)
  160. {
  161. int i;
  162. debug("Hash regions:\n");
  163. debug("%10s %10s\n", "Offset", "Size");
  164. /*
  165. * Use malloc() except in SPL (to save code size). In SPL the caller
  166. * must allocate the array.
  167. */
  168. #ifndef CONFIG_SPL_BUILD
  169. if (!region)
  170. region = calloc(sizeof(*region), count);
  171. #endif
  172. if (!region)
  173. return NULL;
  174. for (i = 0; i < count; i++) {
  175. debug("%10x %10x\n", fdt_regions[i].offset,
  176. fdt_regions[i].size);
  177. region[i].data = fit + fdt_regions[i].offset;
  178. region[i].size = fdt_regions[i].size;
  179. }
  180. return region;
  181. }
  182. static int fit_image_setup_verify(struct image_sign_info *info,
  183. const void *fit, int noffset, int required_keynode,
  184. char **err_msgp)
  185. {
  186. char *algo_name;
  187. const char *padding_name;
  188. if (fdt_totalsize(fit) > CONFIG_FIT_SIGNATURE_MAX_SIZE) {
  189. *err_msgp = "Total size too large";
  190. return 1;
  191. }
  192. if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
  193. *err_msgp = "Can't get hash algo property";
  194. return -1;
  195. }
  196. padding_name = fdt_getprop(fit, noffset, "padding", NULL);
  197. if (!padding_name)
  198. padding_name = RSA_DEFAULT_PADDING_NAME;
  199. memset(info, '\0', sizeof(*info));
  200. info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
  201. info->fit = (void *)fit;
  202. info->node_offset = noffset;
  203. info->name = algo_name;
  204. info->checksum = image_get_checksum_algo(algo_name);
  205. info->crypto = image_get_crypto_algo(algo_name);
  206. info->padding = image_get_padding_algo(padding_name);
  207. info->fdt_blob = gd_fdt_blob();
  208. info->required_keynode = required_keynode;
  209. printf("%s:%s", algo_name, info->keyname);
  210. if (!info->checksum || !info->crypto || !info->padding) {
  211. *err_msgp = "Unknown signature algorithm";
  212. return -1;
  213. }
  214. return 0;
  215. }
  216. int fit_image_check_sig(const void *fit, int noffset, const void *data,
  217. size_t size, int required_keynode, char **err_msgp)
  218. {
  219. struct image_sign_info info;
  220. struct image_region region;
  221. uint8_t *fit_value;
  222. int fit_value_len;
  223. *err_msgp = NULL;
  224. if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
  225. err_msgp))
  226. return -1;
  227. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  228. &fit_value_len)) {
  229. *err_msgp = "Can't get hash value property";
  230. return -1;
  231. }
  232. region.data = data;
  233. region.size = size;
  234. if (info.crypto->verify(&info, &region, 1, fit_value, fit_value_len)) {
  235. *err_msgp = "Verification failed";
  236. return -1;
  237. }
  238. return 0;
  239. }
  240. static int fit_image_verify_sig(const void *fit, int image_noffset,
  241. const char *data, size_t size, const void *sig_blob,
  242. int sig_offset)
  243. {
  244. int noffset;
  245. char *err_msg = "";
  246. int verified = 0;
  247. int ret;
  248. /* Process all hash subnodes of the component image node */
  249. fdt_for_each_subnode(noffset, fit, image_noffset) {
  250. const char *name = fit_get_name(fit, noffset, NULL);
  251. if (!strncmp(name, FIT_SIG_NODENAME,
  252. strlen(FIT_SIG_NODENAME))) {
  253. ret = fit_image_check_sig(fit, noffset, data,
  254. size, -1, &err_msg);
  255. if (ret) {
  256. puts("- ");
  257. } else {
  258. puts("+ ");
  259. verified = 1;
  260. break;
  261. }
  262. }
  263. }
  264. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  265. err_msg = "Corrupted or truncated tree";
  266. goto error;
  267. }
  268. return verified ? 0 : -EPERM;
  269. error:
  270. printf(" error!\n%s for '%s' hash node in '%s' image node\n",
  271. err_msg, fit_get_name(fit, noffset, NULL),
  272. fit_get_name(fit, image_noffset, NULL));
  273. return -1;
  274. }
  275. int fit_image_verify_required_sigs(const void *fit, int image_noffset,
  276. const char *data, size_t size, const void *sig_blob,
  277. int *no_sigsp)
  278. {
  279. int verify_count = 0;
  280. int noffset;
  281. int sig_node;
  282. /* Work out what we need to verify */
  283. *no_sigsp = 1;
  284. sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
  285. if (sig_node < 0) {
  286. debug("%s: No signature node found: %s\n", __func__,
  287. fdt_strerror(sig_node));
  288. return 0;
  289. }
  290. fdt_for_each_subnode(noffset, sig_blob, sig_node) {
  291. const char *required;
  292. int ret;
  293. required = fdt_getprop(sig_blob, noffset, "required", NULL);
  294. if (!required || strcmp(required, "image"))
  295. continue;
  296. ret = fit_image_verify_sig(fit, image_noffset, data, size,
  297. sig_blob, noffset);
  298. if (ret) {
  299. printf("Failed to verify required signature '%s'\n",
  300. fit_get_name(sig_blob, noffset, NULL));
  301. return ret;
  302. }
  303. verify_count++;
  304. }
  305. if (verify_count)
  306. *no_sigsp = 0;
  307. return 0;
  308. }
  309. int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
  310. char **err_msgp)
  311. {
  312. char * const exc_prop[] = {"data"};
  313. const char *prop, *end, *name;
  314. struct image_sign_info info;
  315. const uint32_t *strings;
  316. uint8_t *fit_value;
  317. int fit_value_len;
  318. int max_regions;
  319. int i, prop_len;
  320. char path[200];
  321. int count;
  322. debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
  323. fit_get_name(fit, noffset, NULL),
  324. fit_get_name(gd_fdt_blob(), required_keynode, NULL));
  325. *err_msgp = NULL;
  326. if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
  327. err_msgp))
  328. return -1;
  329. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  330. &fit_value_len)) {
  331. *err_msgp = "Can't get hash value property";
  332. return -1;
  333. }
  334. /* Count the number of strings in the property */
  335. prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
  336. end = prop ? prop + prop_len : prop;
  337. for (name = prop, count = 0; name < end; name++)
  338. if (!*name)
  339. count++;
  340. if (!count) {
  341. *err_msgp = "Can't get hashed-nodes property";
  342. return -1;
  343. }
  344. if (prop && prop_len > 0 && prop[prop_len - 1] != '\0') {
  345. *err_msgp = "hashed-nodes property must be null-terminated";
  346. return -1;
  347. }
  348. /* Add a sanity check here since we are using the stack */
  349. if (count > IMAGE_MAX_HASHED_NODES) {
  350. *err_msgp = "Number of hashed nodes exceeds maximum";
  351. return -1;
  352. }
  353. /* Create a list of node names from those strings */
  354. char *node_inc[count];
  355. debug("Hash nodes (%d):\n", count);
  356. for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
  357. debug(" '%s'\n", name);
  358. node_inc[i] = (char *)name;
  359. }
  360. /*
  361. * Each node can generate one region for each sub-node. Allow for
  362. * 7 sub-nodes (hash-1, signature-1, etc.) and some extra.
  363. */
  364. max_regions = 20 + count * 7;
  365. struct fdt_region fdt_regions[max_regions];
  366. /* Get a list of regions to hash */
  367. count = fdt_find_regions(fit, node_inc, count,
  368. exc_prop, ARRAY_SIZE(exc_prop),
  369. fdt_regions, max_regions - 1,
  370. path, sizeof(path), 0);
  371. if (count < 0) {
  372. *err_msgp = "Failed to hash configuration";
  373. return -1;
  374. }
  375. if (count == 0) {
  376. *err_msgp = "No data to hash";
  377. return -1;
  378. }
  379. if (count >= max_regions - 1) {
  380. *err_msgp = "Too many hash regions";
  381. return -1;
  382. }
  383. /* Add the strings */
  384. strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
  385. if (strings) {
  386. /*
  387. * The strings region offset must be a static 0x0.
  388. * This is set in tool/image-host.c
  389. */
  390. fdt_regions[count].offset = fdt_off_dt_strings(fit);
  391. fdt_regions[count].size = fdt32_to_cpu(strings[1]);
  392. count++;
  393. }
  394. /* Allocate the region list on the stack */
  395. struct image_region region[count];
  396. fit_region_make_list(fit, fdt_regions, count, region);
  397. if (info.crypto->verify(&info, region, count, fit_value,
  398. fit_value_len)) {
  399. *err_msgp = "Verification failed";
  400. return -1;
  401. }
  402. return 0;
  403. }
  404. static int fit_config_verify_sig(const void *fit, int conf_noffset,
  405. const void *sig_blob, int sig_offset)
  406. {
  407. int noffset;
  408. char *err_msg = "";
  409. int verified = 0;
  410. int ret;
  411. /* Process all hash subnodes of the component conf node */
  412. fdt_for_each_subnode(noffset, fit, conf_noffset) {
  413. const char *name = fit_get_name(fit, noffset, NULL);
  414. if (!strncmp(name, FIT_SIG_NODENAME,
  415. strlen(FIT_SIG_NODENAME))) {
  416. ret = fit_config_check_sig(fit, noffset, sig_offset,
  417. &err_msg);
  418. if (ret) {
  419. puts("- ");
  420. } else {
  421. puts("+ ");
  422. verified = 1;
  423. break;
  424. }
  425. }
  426. }
  427. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  428. err_msg = "Corrupted or truncated tree";
  429. goto error;
  430. }
  431. return verified ? 0 : -EPERM;
  432. error:
  433. printf(" error!\n%s for '%s' hash node in '%s' config node\n",
  434. err_msg, fit_get_name(fit, noffset, NULL),
  435. fit_get_name(fit, conf_noffset, NULL));
  436. return -1;
  437. }
  438. int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
  439. const void *sig_blob)
  440. {
  441. int noffset;
  442. int sig_node;
  443. /* Work out what we need to verify */
  444. sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
  445. if (sig_node < 0) {
  446. debug("%s: No signature node found: %s\n", __func__,
  447. fdt_strerror(sig_node));
  448. return 0;
  449. }
  450. fdt_for_each_subnode(noffset, sig_blob, sig_node) {
  451. const char *required;
  452. int ret;
  453. required = fdt_getprop(sig_blob, noffset, "required", NULL);
  454. if (!required || strcmp(required, "conf"))
  455. continue;
  456. ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
  457. noffset);
  458. if (ret) {
  459. printf("Failed to verify required signature '%s'\n",
  460. fit_get_name(sig_blob, noffset, NULL));
  461. return ret;
  462. }
  463. }
  464. return 0;
  465. }
  466. int fit_config_verify(const void *fit, int conf_noffset)
  467. {
  468. return fit_config_verify_required_sigs(fit, conf_noffset,
  469. gd_fdt_blob());
  470. }