image-sig.c 10 KB

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