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. 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 checksum_algo *image_get_checksum_algo(const char *full_name)
  68. {
  69. int i;
  70. const char *name;
  71. for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
  72. name = checksum_algos[i].name;
  73. /* Make sure names match and next char is a comma */
  74. if (!strncmp(name, full_name, strlen(name)) &&
  75. full_name[strlen(name)] == ',')
  76. return &checksum_algos[i];
  77. }
  78. return NULL;
  79. }
  80. struct crypto_algo *image_get_crypto_algo(const char *full_name)
  81. {
  82. int i;
  83. const char *name;
  84. /* Move name to after the comma */
  85. name = strchr(full_name, ',');
  86. if (!name)
  87. return NULL;
  88. name += 1;
  89. for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
  90. if (!strcmp(crypto_algos[i].name, name))
  91. return &crypto_algos[i];
  92. }
  93. return NULL;
  94. }
  95. /**
  96. * fit_region_make_list() - Make a list of image regions
  97. *
  98. * Given a list of fdt_regions, create a list of image_regions. This is a
  99. * simple conversion routine since the FDT and image code use different
  100. * structures.
  101. *
  102. * @fit: FIT image
  103. * @fdt_regions: Pointer to FDT regions
  104. * @count: Number of FDT regions
  105. * @region: Pointer to image regions, which must hold @count records. If
  106. * region is NULL, then (except for an SPL build) the array will be
  107. * allocated.
  108. * @return: Pointer to image regions
  109. */
  110. struct image_region *fit_region_make_list(const void *fit,
  111. struct fdt_region *fdt_regions, int count,
  112. struct image_region *region)
  113. {
  114. int i;
  115. debug("Hash regions:\n");
  116. debug("%10s %10s\n", "Offset", "Size");
  117. /*
  118. * Use malloc() except in SPL (to save code size). In SPL the caller
  119. * must allocate the array.
  120. */
  121. #ifndef CONFIG_SPL_BUILD
  122. if (!region)
  123. region = calloc(sizeof(*region), count);
  124. #endif
  125. if (!region)
  126. return NULL;
  127. for (i = 0; i < count; i++) {
  128. debug("%10x %10x\n", fdt_regions[i].offset,
  129. fdt_regions[i].size);
  130. region[i].data = fit + fdt_regions[i].offset;
  131. region[i].size = fdt_regions[i].size;
  132. }
  133. return region;
  134. }
  135. static int fit_image_setup_verify(struct image_sign_info *info,
  136. const void *fit, int noffset, int required_keynode,
  137. char **err_msgp)
  138. {
  139. char *algo_name;
  140. if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
  141. *err_msgp = "Can't get hash algo property";
  142. return -1;
  143. }
  144. memset(info, '\0', sizeof(*info));
  145. info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
  146. info->fit = (void *)fit;
  147. info->node_offset = noffset;
  148. info->name = algo_name;
  149. info->checksum = image_get_checksum_algo(algo_name);
  150. info->crypto = image_get_crypto_algo(algo_name);
  151. info->fdt_blob = gd_fdt_blob();
  152. info->required_keynode = required_keynode;
  153. printf("%s:%s", algo_name, info->keyname);
  154. if (!info->checksum || !info->crypto) {
  155. *err_msgp = "Unknown signature algorithm";
  156. return -1;
  157. }
  158. return 0;
  159. }
  160. int fit_image_check_sig(const void *fit, int noffset, const void *data,
  161. size_t size, int required_keynode, char **err_msgp)
  162. {
  163. struct image_sign_info info;
  164. struct image_region region;
  165. uint8_t *fit_value;
  166. int fit_value_len;
  167. *err_msgp = NULL;
  168. if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
  169. err_msgp))
  170. return -1;
  171. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  172. &fit_value_len)) {
  173. *err_msgp = "Can't get hash value property";
  174. return -1;
  175. }
  176. region.data = data;
  177. region.size = size;
  178. if (info.crypto->verify(&info, &region, 1, fit_value, fit_value_len)) {
  179. *err_msgp = "Verification failed";
  180. return -1;
  181. }
  182. return 0;
  183. }
  184. static int fit_image_verify_sig(const void *fit, int image_noffset,
  185. const char *data, size_t size, const void *sig_blob,
  186. int sig_offset)
  187. {
  188. int noffset;
  189. char *err_msg = "";
  190. int verified = 0;
  191. int ret;
  192. /* Process all hash subnodes of the component image node */
  193. fdt_for_each_subnode(noffset, fit, image_noffset) {
  194. const char *name = fit_get_name(fit, noffset, NULL);
  195. if (!strncmp(name, FIT_SIG_NODENAME,
  196. strlen(FIT_SIG_NODENAME))) {
  197. ret = fit_image_check_sig(fit, noffset, data,
  198. size, -1, &err_msg);
  199. if (ret) {
  200. puts("- ");
  201. } else {
  202. puts("+ ");
  203. verified = 1;
  204. break;
  205. }
  206. }
  207. }
  208. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  209. err_msg = "Corrupted or truncated tree";
  210. goto error;
  211. }
  212. return verified ? 0 : -EPERM;
  213. error:
  214. printf(" error!\n%s for '%s' hash node in '%s' image node\n",
  215. err_msg, fit_get_name(fit, noffset, NULL),
  216. fit_get_name(fit, image_noffset, NULL));
  217. return -1;
  218. }
  219. int fit_image_verify_required_sigs(const void *fit, int image_noffset,
  220. const char *data, size_t size, const void *sig_blob,
  221. int *no_sigsp)
  222. {
  223. int verify_count = 0;
  224. int noffset;
  225. int sig_node;
  226. /* Work out what we need to verify */
  227. *no_sigsp = 1;
  228. sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
  229. if (sig_node < 0) {
  230. debug("%s: No signature node found: %s\n", __func__,
  231. fdt_strerror(sig_node));
  232. return 0;
  233. }
  234. fdt_for_each_subnode(noffset, sig_blob, sig_node) {
  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.crypto->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. fdt_for_each_subnode(noffset, fit, conf_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. fdt_for_each_subnode(noffset, sig_blob, sig_node) {
  388. const char *required;
  389. int ret;
  390. required = fdt_getprop(sig_blob, noffset, "required", NULL);
  391. if (!required || strcmp(required, "conf"))
  392. continue;
  393. ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
  394. noffset);
  395. if (ret) {
  396. printf("Failed to verify required signature '%s'\n",
  397. fit_get_name(sig_blob, noffset, NULL));
  398. return ret;
  399. }
  400. }
  401. return 0;
  402. }
  403. int fit_config_verify(const void *fit, int conf_noffset)
  404. {
  405. return fit_config_verify_required_sigs(fit, conf_noffset,
  406. gd_fdt_blob());
  407. }