image-sig.c 11 KB

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