image-sig.c 12 KB

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