image-sig.c 11 KB

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