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