image-sig.c 11 KB

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