image-sig.c 12 KB

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