image-sig.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. hash_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. hash_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. hash_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. fdt_for_each_subnode(fit, noffset, image_noffset) {
  190. const char *name = fit_get_name(fit, noffset, NULL);
  191. if (!strncmp(name, FIT_SIG_NODENAME,
  192. strlen(FIT_SIG_NODENAME))) {
  193. ret = fit_image_check_sig(fit, noffset, data,
  194. size, -1, &err_msg);
  195. if (ret) {
  196. puts("- ");
  197. } else {
  198. puts("+ ");
  199. verified = 1;
  200. break;
  201. }
  202. }
  203. }
  204. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  205. err_msg = "Corrupted or truncated tree";
  206. goto error;
  207. }
  208. return verified ? 0 : -EPERM;
  209. error:
  210. printf(" error!\n%s for '%s' hash node in '%s' image node\n",
  211. err_msg, fit_get_name(fit, noffset, NULL),
  212. fit_get_name(fit, image_noffset, NULL));
  213. return -1;
  214. }
  215. int fit_image_verify_required_sigs(const void *fit, int image_noffset,
  216. const char *data, size_t size, const void *sig_blob,
  217. int *no_sigsp)
  218. {
  219. int verify_count = 0;
  220. int noffset;
  221. int sig_node;
  222. /* Work out what we need to verify */
  223. *no_sigsp = 1;
  224. sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
  225. if (sig_node < 0) {
  226. debug("%s: No signature node found: %s\n", __func__,
  227. fdt_strerror(sig_node));
  228. return 0;
  229. }
  230. fdt_for_each_subnode(sig_blob, noffset, sig_node) {
  231. const char *required;
  232. int ret;
  233. required = fdt_getprop(sig_blob, noffset, "required", NULL);
  234. if (!required || strcmp(required, "image"))
  235. continue;
  236. ret = fit_image_verify_sig(fit, image_noffset, data, size,
  237. sig_blob, noffset);
  238. if (ret) {
  239. printf("Failed to verify required signature '%s'\n",
  240. fit_get_name(sig_blob, noffset, NULL));
  241. return ret;
  242. }
  243. verify_count++;
  244. }
  245. if (verify_count)
  246. *no_sigsp = 0;
  247. return 0;
  248. }
  249. int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
  250. char **err_msgp)
  251. {
  252. char * const exc_prop[] = {"data"};
  253. const char *prop, *end, *name;
  254. struct image_sign_info info;
  255. const uint32_t *strings;
  256. uint8_t *fit_value;
  257. int fit_value_len;
  258. int max_regions;
  259. int i, prop_len;
  260. char path[200];
  261. int count;
  262. debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
  263. fit_get_name(fit, noffset, NULL),
  264. fit_get_name(gd_fdt_blob(), required_keynode, NULL));
  265. *err_msgp = NULL;
  266. if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
  267. err_msgp))
  268. return -1;
  269. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  270. &fit_value_len)) {
  271. *err_msgp = "Can't get hash value property";
  272. return -1;
  273. }
  274. /* Count the number of strings in the property */
  275. prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
  276. end = prop ? prop + prop_len : prop;
  277. for (name = prop, count = 0; name < end; name++)
  278. if (!*name)
  279. count++;
  280. if (!count) {
  281. *err_msgp = "Can't get hashed-nodes property";
  282. return -1;
  283. }
  284. /* Add a sanity check here since we are using the stack */
  285. if (count > IMAGE_MAX_HASHED_NODES) {
  286. *err_msgp = "Number of hashed nodes exceeds maximum";
  287. return -1;
  288. }
  289. /* Create a list of node names from those strings */
  290. char *node_inc[count];
  291. debug("Hash nodes (%d):\n", count);
  292. for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
  293. debug(" '%s'\n", name);
  294. node_inc[i] = (char *)name;
  295. }
  296. /*
  297. * Each node can generate one region for each sub-node. Allow for
  298. * 7 sub-nodes (hash@1, signature@1, etc.) and some extra.
  299. */
  300. max_regions = 20 + count * 7;
  301. struct fdt_region fdt_regions[max_regions];
  302. /* Get a list of regions to hash */
  303. count = fdt_find_regions(fit, node_inc, count,
  304. exc_prop, ARRAY_SIZE(exc_prop),
  305. fdt_regions, max_regions - 1,
  306. path, sizeof(path), 0);
  307. if (count < 0) {
  308. *err_msgp = "Failed to hash configuration";
  309. return -1;
  310. }
  311. if (count == 0) {
  312. *err_msgp = "No data to hash";
  313. return -1;
  314. }
  315. if (count >= max_regions - 1) {
  316. *err_msgp = "Too many hash regions";
  317. return -1;
  318. }
  319. /* Add the strings */
  320. strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
  321. if (strings) {
  322. fdt_regions[count].offset = fdt_off_dt_strings(fit) +
  323. fdt32_to_cpu(strings[0]);
  324. fdt_regions[count].size = fdt32_to_cpu(strings[1]);
  325. count++;
  326. }
  327. /* Allocate the region list on the stack */
  328. struct image_region region[count];
  329. fit_region_make_list(fit, fdt_regions, count, region);
  330. if (info.algo->verify(&info, region, count, fit_value,
  331. fit_value_len)) {
  332. *err_msgp = "Verification failed";
  333. return -1;
  334. }
  335. return 0;
  336. }
  337. static int fit_config_verify_sig(const void *fit, int conf_noffset,
  338. const void *sig_blob, int sig_offset)
  339. {
  340. int noffset;
  341. char *err_msg = "";
  342. int verified = 0;
  343. int ret;
  344. /* Process all hash subnodes of the component conf node */
  345. fdt_for_each_subnode(fit, noffset, conf_noffset) {
  346. const char *name = fit_get_name(fit, noffset, NULL);
  347. if (!strncmp(name, FIT_SIG_NODENAME,
  348. strlen(FIT_SIG_NODENAME))) {
  349. ret = fit_config_check_sig(fit, noffset, sig_offset,
  350. &err_msg);
  351. if (ret) {
  352. puts("- ");
  353. } else {
  354. puts("+ ");
  355. verified = 1;
  356. break;
  357. }
  358. }
  359. }
  360. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  361. err_msg = "Corrupted or truncated tree";
  362. goto error;
  363. }
  364. return verified ? 0 : -EPERM;
  365. error:
  366. printf(" error!\n%s for '%s' hash node in '%s' config node\n",
  367. err_msg, fit_get_name(fit, noffset, NULL),
  368. fit_get_name(fit, conf_noffset, NULL));
  369. return -1;
  370. }
  371. int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
  372. const void *sig_blob)
  373. {
  374. int noffset;
  375. int sig_node;
  376. /* Work out what we need to verify */
  377. sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
  378. if (sig_node < 0) {
  379. debug("%s: No signature node found: %s\n", __func__,
  380. fdt_strerror(sig_node));
  381. return 0;
  382. }
  383. fdt_for_each_subnode(sig_blob, noffset, sig_node) {
  384. const char *required;
  385. int ret;
  386. required = fdt_getprop(sig_blob, noffset, "required", NULL);
  387. if (!required || strcmp(required, "conf"))
  388. continue;
  389. ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
  390. noffset);
  391. if (ret) {
  392. printf("Failed to verify required signature '%s'\n",
  393. fit_get_name(sig_blob, noffset, NULL));
  394. return ret;
  395. }
  396. }
  397. return 0;
  398. }
  399. int fit_config_verify(const void *fit, int conf_noffset)
  400. {
  401. return fit_config_verify_required_sigs(fit, conf_noffset,
  402. gd_fdt_blob());
  403. }