image-host.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013, Google Inc.
  4. *
  5. * (C) Copyright 2008 Semihalf
  6. *
  7. * (C) Copyright 2000-2006
  8. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  9. */
  10. #include "mkimage.h"
  11. #include <bootm.h>
  12. #include <fdt_region.h>
  13. #include <image.h>
  14. #include <version.h>
  15. #include <openssl/pem.h>
  16. #include <openssl/evp.h>
  17. /**
  18. * fit_set_hash_value - set hash value in requested has node
  19. * @fit: pointer to the FIT format image header
  20. * @noffset: hash node offset
  21. * @value: hash value to be set
  22. * @value_len: hash value length
  23. *
  24. * fit_set_hash_value() attempts to set hash value in a node at offset
  25. * given and returns operation status to the caller.
  26. *
  27. * returns
  28. * 0, on success
  29. * -1, on failure
  30. */
  31. static int fit_set_hash_value(void *fit, int noffset, uint8_t *value,
  32. int value_len)
  33. {
  34. int ret;
  35. ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
  36. if (ret) {
  37. printf("Can't set hash '%s' property for '%s' node(%s)\n",
  38. FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
  39. fdt_strerror(ret));
  40. return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
  41. }
  42. return 0;
  43. }
  44. /**
  45. * fit_image_process_hash - Process a single subnode of the images/ node
  46. *
  47. * Check each subnode and process accordingly. For hash nodes we generate
  48. * a hash of the supplied data and store it in the node.
  49. *
  50. * @fit: pointer to the FIT format image header
  51. * @image_name: name of image being processed (used to display errors)
  52. * @noffset: subnode offset
  53. * @data: data to process
  54. * @size: size of data in bytes
  55. * Return: 0 if ok, -1 on error
  56. */
  57. static int fit_image_process_hash(void *fit, const char *image_name,
  58. int noffset, const void *data, size_t size)
  59. {
  60. uint8_t value[FIT_MAX_HASH_LEN];
  61. const char *node_name;
  62. int value_len;
  63. const char *algo;
  64. int ret;
  65. node_name = fit_get_name(fit, noffset, NULL);
  66. if (fit_image_hash_get_algo(fit, noffset, &algo)) {
  67. printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
  68. node_name, image_name);
  69. return -ENOENT;
  70. }
  71. if (calculate_hash(data, size, algo, value, &value_len)) {
  72. printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
  73. algo, node_name, image_name);
  74. return -EPROTONOSUPPORT;
  75. }
  76. ret = fit_set_hash_value(fit, noffset, value, value_len);
  77. if (ret) {
  78. printf("Can't set hash value for '%s' hash node in '%s' image node\n",
  79. node_name, image_name);
  80. return ret;
  81. }
  82. return 0;
  83. }
  84. /**
  85. * fit_image_write_sig() - write the signature to a FIT
  86. *
  87. * This writes the signature and signer data to the FIT.
  88. *
  89. * @fit: pointer to the FIT format image header
  90. * @noffset: hash node offset
  91. * @value: signature value to be set
  92. * @value_len: signature value length
  93. * @comment: Text comment to write (NULL for none)
  94. *
  95. * returns
  96. * 0, on success
  97. * -FDT_ERR_..., on failure
  98. */
  99. static int fit_image_write_sig(void *fit, int noffset, uint8_t *value,
  100. int value_len, const char *comment, const char *region_prop,
  101. int region_proplen, const char *cmdname, const char *algo_name)
  102. {
  103. int string_size;
  104. int ret;
  105. /*
  106. * Get the current string size, before we update the FIT and add
  107. * more
  108. */
  109. string_size = fdt_size_dt_strings(fit);
  110. ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
  111. if (!ret) {
  112. ret = fdt_setprop_string(fit, noffset, "signer-name",
  113. "mkimage");
  114. }
  115. if (!ret) {
  116. ret = fdt_setprop_string(fit, noffset, "signer-version",
  117. PLAIN_VERSION);
  118. }
  119. if (comment && !ret)
  120. ret = fdt_setprop_string(fit, noffset, "comment", comment);
  121. if (!ret) {
  122. time_t timestamp = imagetool_get_source_date(cmdname,
  123. time(NULL));
  124. uint32_t t = cpu_to_uimage(timestamp);
  125. ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
  126. sizeof(uint32_t));
  127. }
  128. if (region_prop && !ret) {
  129. uint32_t strdata[2];
  130. ret = fdt_setprop(fit, noffset, "hashed-nodes",
  131. region_prop, region_proplen);
  132. /* This is a legacy offset, it is unused, and must remain 0. */
  133. strdata[0] = 0;
  134. strdata[1] = cpu_to_fdt32(string_size);
  135. if (!ret) {
  136. ret = fdt_setprop(fit, noffset, "hashed-strings",
  137. strdata, sizeof(strdata));
  138. }
  139. }
  140. if (algo_name && !ret)
  141. ret = fdt_setprop_string(fit, noffset, "algo", algo_name);
  142. return ret;
  143. }
  144. static int fit_image_setup_sig(struct image_sign_info *info,
  145. const char *keydir, const char *keyfile, void *fit,
  146. const char *image_name, int noffset, const char *require_keys,
  147. const char *engine_id, const char *algo_name)
  148. {
  149. const char *node_name;
  150. const char *padding_name;
  151. node_name = fit_get_name(fit, noffset, NULL);
  152. if (!algo_name) {
  153. if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
  154. printf("Can't get algo property for '%s' signature node in '%s' image node\n",
  155. node_name, image_name);
  156. return -1;
  157. }
  158. }
  159. padding_name = fdt_getprop(fit, noffset, "padding", NULL);
  160. memset(info, '\0', sizeof(*info));
  161. info->keydir = keydir;
  162. info->keyfile = keyfile;
  163. info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  164. info->fit = fit;
  165. info->node_offset = noffset;
  166. info->name = strdup(algo_name);
  167. info->checksum = image_get_checksum_algo(algo_name);
  168. info->crypto = image_get_crypto_algo(algo_name);
  169. info->padding = image_get_padding_algo(padding_name);
  170. info->require_keys = require_keys;
  171. info->engine_id = engine_id;
  172. if (!info->checksum || !info->crypto) {
  173. printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n",
  174. algo_name, node_name, image_name);
  175. return -1;
  176. }
  177. return 0;
  178. }
  179. /**
  180. * fit_image_process_sig- Process a single subnode of the images/ node
  181. *
  182. * Check each subnode and process accordingly. For signature nodes we
  183. * generate a signed hash of the supplied data and store it in the node.
  184. *
  185. * @keydir: Directory containing keys to use for signing
  186. * @keydest: Destination FDT blob to write public keys into (NULL if none)
  187. * @fit: pointer to the FIT format image header
  188. * @image_name: name of image being processed (used to display errors)
  189. * @noffset: subnode offset
  190. * @data: data to process
  191. * @size: size of data in bytes
  192. * @comment: Comment to add to signature nodes
  193. * @require_keys: Mark all keys as 'required'
  194. * @engine_id: Engine to use for signing
  195. * Return: keydest node if @keydest is non-NULL, else 0 if none; -ve error code
  196. * on failure
  197. */
  198. static int fit_image_process_sig(const char *keydir, const char *keyfile,
  199. void *keydest, void *fit, const char *image_name,
  200. int noffset, const void *data, size_t size,
  201. const char *comment, int require_keys, const char *engine_id,
  202. const char *cmdname, const char *algo_name)
  203. {
  204. struct image_sign_info info;
  205. struct image_region region;
  206. const char *node_name;
  207. uint8_t *value;
  208. uint value_len;
  209. int ret;
  210. if (fit_image_setup_sig(&info, keydir, keyfile, fit, image_name,
  211. noffset, require_keys ? "image" : NULL,
  212. engine_id, algo_name))
  213. return -1;
  214. node_name = fit_get_name(fit, noffset, NULL);
  215. region.data = data;
  216. region.size = size;
  217. ret = info.crypto->sign(&info, &region, 1, &value, &value_len);
  218. if (ret) {
  219. printf("Failed to sign '%s' signature node in '%s' image node: %d\n",
  220. node_name, image_name, ret);
  221. /* We allow keys to be missing */
  222. if (ret == -ENOENT)
  223. return 0;
  224. return -1;
  225. }
  226. ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
  227. NULL, 0, cmdname, algo_name);
  228. if (ret) {
  229. if (ret == -FDT_ERR_NOSPACE)
  230. return -ENOSPC;
  231. printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
  232. node_name, image_name, fdt_strerror(ret));
  233. return -1;
  234. }
  235. free(value);
  236. /* Get keyname again, as FDT has changed and invalidated our pointer */
  237. info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  238. /*
  239. * Write the public key into the supplied FDT file; this might fail
  240. * several times, since we try signing with successively increasing
  241. * size values
  242. */
  243. if (keydest) {
  244. ret = info.crypto->add_verify_data(&info, keydest);
  245. if (ret < 0) {
  246. printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
  247. node_name, image_name);
  248. return ret;
  249. }
  250. /* Return the node that was written to */
  251. return ret;
  252. }
  253. return 0;
  254. }
  255. static int fit_image_read_data(char *filename, unsigned char *data,
  256. int expected_size)
  257. {
  258. struct stat sbuf;
  259. int fd, ret = -1;
  260. ssize_t n;
  261. /* Open file */
  262. fd = open(filename, O_RDONLY | O_BINARY);
  263. if (fd < 0) {
  264. printf("Can't open file %s (err=%d => %s)\n",
  265. filename, errno, strerror(errno));
  266. return -1;
  267. }
  268. /* Compute file size */
  269. if (fstat(fd, &sbuf) < 0) {
  270. printf("Can't fstat file %s (err=%d => %s)\n",
  271. filename, errno, strerror(errno));
  272. goto err;
  273. }
  274. /* Check file size */
  275. if (sbuf.st_size != expected_size) {
  276. printf("File %s don't have the expected size (size=%lld, expected=%d)\n",
  277. filename, (long long)sbuf.st_size, expected_size);
  278. goto err;
  279. }
  280. /* Read data */
  281. n = read(fd, data, sbuf.st_size);
  282. if (n < 0) {
  283. printf("Can't read file %s (err=%d => %s)\n",
  284. filename, errno, strerror(errno));
  285. goto err;
  286. }
  287. /* Check that we have read all the file */
  288. if (n != sbuf.st_size) {
  289. printf("Can't read all file %s (read %zd bytes, expected %lld)\n",
  290. filename, n, (long long)sbuf.st_size);
  291. goto err;
  292. }
  293. ret = 0;
  294. err:
  295. close(fd);
  296. return ret;
  297. }
  298. static int get_random_data(void *data, int size)
  299. {
  300. unsigned char *tmp = data;
  301. struct timespec date;
  302. int i, ret;
  303. if (!tmp) {
  304. printf("%s: pointer data is NULL\n", __func__);
  305. ret = -1;
  306. goto out;
  307. }
  308. ret = clock_gettime(CLOCK_MONOTONIC, &date);
  309. if (ret) {
  310. printf("%s: clock_gettime has failed (%s)\n", __func__,
  311. strerror(errno));
  312. goto out;
  313. }
  314. srandom(date.tv_nsec);
  315. for (i = 0; i < size; i++) {
  316. *tmp = random() & 0xff;
  317. tmp++;
  318. }
  319. out:
  320. return ret;
  321. }
  322. static int fit_image_setup_cipher(struct image_cipher_info *info,
  323. const char *keydir, void *fit,
  324. const char *image_name, int image_noffset,
  325. int noffset)
  326. {
  327. char *algo_name;
  328. char filename[128];
  329. int ret = -1;
  330. if (fit_image_cipher_get_algo(fit, noffset, &algo_name)) {
  331. printf("Can't get algo name for cipher in image '%s'\n",
  332. image_name);
  333. goto out;
  334. }
  335. info->keydir = keydir;
  336. /* Read the key name */
  337. info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  338. if (!info->keyname) {
  339. printf("Can't get key name for cipher in image '%s'\n",
  340. image_name);
  341. goto out;
  342. }
  343. /*
  344. * Read the IV name
  345. *
  346. * If this property is not provided then mkimage will generate
  347. * a random IV and store it in the FIT image
  348. */
  349. info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL);
  350. info->fit = fit;
  351. info->node_noffset = noffset;
  352. info->name = algo_name;
  353. info->cipher = image_get_cipher_algo(algo_name);
  354. if (!info->cipher) {
  355. printf("Can't get algo for cipher '%s'\n", image_name);
  356. goto out;
  357. }
  358. /* Read the key in the file */
  359. snprintf(filename, sizeof(filename), "%s/%s%s",
  360. info->keydir, info->keyname, ".bin");
  361. info->key = malloc(info->cipher->key_len);
  362. if (!info->key) {
  363. printf("Can't allocate memory for key\n");
  364. ret = -1;
  365. goto out;
  366. }
  367. ret = fit_image_read_data(filename, (unsigned char *)info->key,
  368. info->cipher->key_len);
  369. if (ret < 0)
  370. goto out;
  371. info->iv = malloc(info->cipher->iv_len);
  372. if (!info->iv) {
  373. printf("Can't allocate memory for iv\n");
  374. ret = -1;
  375. goto out;
  376. }
  377. if (info->ivname) {
  378. /* Read the IV in the file */
  379. snprintf(filename, sizeof(filename), "%s/%s%s",
  380. info->keydir, info->ivname, ".bin");
  381. ret = fit_image_read_data(filename, (unsigned char *)info->iv,
  382. info->cipher->iv_len);
  383. } else {
  384. /* Generate an ramdom IV */
  385. ret = get_random_data((void *)info->iv, info->cipher->iv_len);
  386. }
  387. out:
  388. return ret;
  389. }
  390. int fit_image_write_cipher(void *fit, int image_noffset, int noffset,
  391. const void *data, size_t size,
  392. unsigned char *data_ciphered, int data_ciphered_len)
  393. {
  394. int ret = -1;
  395. /* Replace data with ciphered data */
  396. ret = fdt_setprop(fit, image_noffset, FIT_DATA_PROP,
  397. data_ciphered, data_ciphered_len);
  398. if (ret == -FDT_ERR_NOSPACE) {
  399. ret = -ENOSPC;
  400. goto out;
  401. }
  402. if (ret) {
  403. printf("Can't replace data with ciphered data (err = %d)\n", ret);
  404. goto out;
  405. }
  406. /* add non ciphered data size */
  407. ret = fdt_setprop_u32(fit, image_noffset, "data-size-unciphered", size);
  408. if (ret == -FDT_ERR_NOSPACE) {
  409. ret = -ENOSPC;
  410. goto out;
  411. }
  412. if (ret) {
  413. printf("Can't add unciphered data size (err = %d)\n", ret);
  414. goto out;
  415. }
  416. out:
  417. return ret;
  418. }
  419. static int
  420. fit_image_process_cipher(const char *keydir, void *keydest, void *fit,
  421. const char *image_name, int image_noffset,
  422. int node_noffset, const void *data, size_t size,
  423. const char *cmdname)
  424. {
  425. struct image_cipher_info info;
  426. unsigned char *data_ciphered = NULL;
  427. int data_ciphered_len;
  428. int ret;
  429. memset(&info, 0, sizeof(info));
  430. ret = fit_image_setup_cipher(&info, keydir, fit, image_name,
  431. image_noffset, node_noffset);
  432. if (ret)
  433. goto out;
  434. ret = info.cipher->encrypt(&info, data, size,
  435. &data_ciphered, &data_ciphered_len);
  436. if (ret)
  437. goto out;
  438. /*
  439. * Write the public key into the supplied FDT file; this might fail
  440. * several times, since we try signing with successively increasing
  441. * size values
  442. * And, if needed, write the iv in the FIT file
  443. */
  444. if (keydest) {
  445. ret = info.cipher->add_cipher_data(&info, keydest, fit, node_noffset);
  446. if (ret) {
  447. printf("Failed to add verification data for cipher '%s' in image '%s'\n",
  448. info.keyname, image_name);
  449. goto out;
  450. }
  451. }
  452. ret = fit_image_write_cipher(fit, image_noffset, node_noffset,
  453. data, size,
  454. data_ciphered, data_ciphered_len);
  455. out:
  456. free(data_ciphered);
  457. free((void *)info.key);
  458. free((void *)info.iv);
  459. return ret;
  460. }
  461. int fit_image_cipher_data(const char *keydir, void *keydest,
  462. void *fit, int image_noffset, const char *comment,
  463. int require_keys, const char *engine_id,
  464. const char *cmdname)
  465. {
  466. const char *image_name;
  467. const void *data;
  468. size_t size;
  469. int cipher_node_offset, len;
  470. /* Get image name */
  471. image_name = fit_get_name(fit, image_noffset, NULL);
  472. if (!image_name) {
  473. printf("Can't get image name\n");
  474. return -1;
  475. }
  476. /* Get image data and data length */
  477. if (fit_image_get_data(fit, image_noffset, &data, &size)) {
  478. printf("Can't get image data/size\n");
  479. return -1;
  480. }
  481. /*
  482. * Don't cipher ciphered data.
  483. *
  484. * If the data-size-unciphered property is present the data for this
  485. * image is already encrypted. This is important as 'mkimage -F' can be
  486. * run multiple times on a FIT image.
  487. */
  488. if (fdt_getprop(fit, image_noffset, "data-size-unciphered", &len))
  489. return 0;
  490. if (len != -FDT_ERR_NOTFOUND) {
  491. printf("Failure testing for data-size-unciphered\n");
  492. return -1;
  493. }
  494. /* Process cipher node if present */
  495. cipher_node_offset = fdt_subnode_offset(fit, image_noffset,
  496. FIT_CIPHER_NODENAME);
  497. if (cipher_node_offset == -FDT_ERR_NOTFOUND)
  498. return 0;
  499. if (cipher_node_offset < 0) {
  500. printf("Failure getting cipher node\n");
  501. return -1;
  502. }
  503. if (!IMAGE_ENABLE_ENCRYPT || !keydir)
  504. return 0;
  505. return fit_image_process_cipher(keydir, keydest, fit, image_name,
  506. image_noffset, cipher_node_offset, data, size, cmdname);
  507. }
  508. /**
  509. * fit_image_add_verification_data() - calculate/set verig. data for image node
  510. *
  511. * This adds hash and signature values for an component image node.
  512. *
  513. * All existing hash subnodes are checked, if algorithm property is set to
  514. * one of the supported hash algorithms, hash value is computed and
  515. * corresponding hash node property is set, for example:
  516. *
  517. * Input component image node structure:
  518. *
  519. * o image-1 (at image_noffset)
  520. * | - data = [binary data]
  521. * o hash-1
  522. * |- algo = "sha1"
  523. *
  524. * Output component image node structure:
  525. *
  526. * o image-1 (at image_noffset)
  527. * | - data = [binary data]
  528. * o hash-1
  529. * |- algo = "sha1"
  530. * |- value = sha1(data)
  531. *
  532. * For signature details, please see doc/uImage.FIT/signature.txt
  533. *
  534. * @keydir Directory containing *.key and *.crt files (or NULL)
  535. * @keydest FDT Blob to write public keys into (NULL if none)
  536. * @fit: Pointer to the FIT format image header
  537. * @image_noffset: Requested component image node
  538. * @comment: Comment to add to signature nodes
  539. * @require_keys: Mark all keys as 'required'
  540. * @engine_id: Engine to use for signing
  541. * @return: 0 on success, <0 on failure
  542. */
  543. int fit_image_add_verification_data(const char *keydir, const char *keyfile,
  544. void *keydest, void *fit, int image_noffset,
  545. const char *comment, int require_keys, const char *engine_id,
  546. const char *cmdname, const char* algo_name)
  547. {
  548. const char *image_name;
  549. const void *data;
  550. size_t size;
  551. int noffset;
  552. /* Get image data and data length */
  553. if (fit_image_get_data(fit, image_noffset, &data, &size)) {
  554. printf("Can't get image data/size\n");
  555. return -1;
  556. }
  557. image_name = fit_get_name(fit, image_noffset, NULL);
  558. /* Process all hash subnodes of the component image node */
  559. for (noffset = fdt_first_subnode(fit, image_noffset);
  560. noffset >= 0;
  561. noffset = fdt_next_subnode(fit, noffset)) {
  562. const char *node_name;
  563. int ret = 0;
  564. /*
  565. * Check subnode name, must be equal to "hash" or "signature".
  566. * Multiple hash nodes require unique unit node
  567. * names, e.g. hash-1, hash-2, signature-1, etc.
  568. */
  569. node_name = fit_get_name(fit, noffset, NULL);
  570. if (!strncmp(node_name, FIT_HASH_NODENAME,
  571. strlen(FIT_HASH_NODENAME))) {
  572. ret = fit_image_process_hash(fit, image_name, noffset,
  573. data, size);
  574. } else if (IMAGE_ENABLE_SIGN && (keydir || keyfile) &&
  575. !strncmp(node_name, FIT_SIG_NODENAME,
  576. strlen(FIT_SIG_NODENAME))) {
  577. ret = fit_image_process_sig(keydir, keyfile, keydest,
  578. fit, image_name, noffset, data, size,
  579. comment, require_keys, engine_id, cmdname,
  580. algo_name);
  581. }
  582. if (ret < 0)
  583. return ret;
  584. }
  585. return 0;
  586. }
  587. struct strlist {
  588. int count;
  589. char **strings;
  590. };
  591. static void strlist_init(struct strlist *list)
  592. {
  593. memset(list, '\0', sizeof(*list));
  594. }
  595. static void strlist_free(struct strlist *list)
  596. {
  597. int i;
  598. for (i = 0; i < list->count; i++)
  599. free(list->strings[i]);
  600. free(list->strings);
  601. }
  602. static int strlist_add(struct strlist *list, const char *str)
  603. {
  604. char *dup;
  605. dup = strdup(str);
  606. list->strings = realloc(list->strings,
  607. (list->count + 1) * sizeof(char *));
  608. if (!list || !str)
  609. return -1;
  610. list->strings[list->count++] = dup;
  611. return 0;
  612. }
  613. static const char *fit_config_get_image_list(const void *fit, int noffset,
  614. int *lenp, int *allow_missingp)
  615. {
  616. static const char default_list[] = FIT_KERNEL_PROP "\0"
  617. FIT_FDT_PROP;
  618. const char *prop;
  619. /* If there is an "sign-image" property, use that */
  620. prop = fdt_getprop(fit, noffset, "sign-images", lenp);
  621. if (prop) {
  622. *allow_missingp = 0;
  623. return *lenp ? prop : NULL;
  624. }
  625. /* Default image list */
  626. *allow_missingp = 1;
  627. *lenp = sizeof(default_list);
  628. return default_list;
  629. }
  630. /**
  631. * fit_config_add_hash() - Add a list of nodes to hash for an image
  632. *
  633. * This adds a list of paths to image nodes (as referred to by a particular
  634. * offset) that need to be hashed, to protect a configuration
  635. *
  636. * @fit: Pointer to the FIT format image header
  637. * @image_noffset: Offset of image to process (e.g. /images/kernel-1)
  638. * @node_inc: List of nodes to add to
  639. * @conf_name Configuration-node name, child of /configurations node (only
  640. * used for error messages)
  641. * @sig_name Signature-node name (only used for error messages)
  642. * @iname: Name of image being processed (e.g. "kernel-1" (only used
  643. * for error messages)
  644. */
  645. static int fit_config_add_hash(const void *fit, int image_noffset,
  646. struct strlist *node_inc, const char *conf_name,
  647. const char *sig_name, const char *iname)
  648. {
  649. char path[200];
  650. int noffset;
  651. int hash_count;
  652. int ret;
  653. ret = fdt_get_path(fit, image_noffset, path, sizeof(path));
  654. if (ret < 0)
  655. goto err_path;
  656. if (strlist_add(node_inc, path))
  657. goto err_mem;
  658. /* Add all this image's hashes */
  659. hash_count = 0;
  660. for (noffset = fdt_first_subnode(fit, image_noffset);
  661. noffset >= 0;
  662. noffset = fdt_next_subnode(fit, noffset)) {
  663. const char *name = fit_get_name(fit, noffset, NULL);
  664. if (strncmp(name, FIT_HASH_NODENAME,
  665. strlen(FIT_HASH_NODENAME)))
  666. continue;
  667. ret = fdt_get_path(fit, noffset, path, sizeof(path));
  668. if (ret < 0)
  669. goto err_path;
  670. if (strlist_add(node_inc, path))
  671. goto err_mem;
  672. hash_count++;
  673. }
  674. if (!hash_count) {
  675. printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n",
  676. conf_name, sig_name, iname);
  677. return -ENOMSG;
  678. }
  679. /* Add this image's cipher node if present */
  680. noffset = fdt_subnode_offset(fit, image_noffset,
  681. FIT_CIPHER_NODENAME);
  682. if (noffset != -FDT_ERR_NOTFOUND) {
  683. if (noffset < 0) {
  684. printf("Failed to get cipher node in configuration '%s/%s' image '%s': %s\n",
  685. conf_name, sig_name, iname,
  686. fdt_strerror(noffset));
  687. return -EIO;
  688. }
  689. ret = fdt_get_path(fit, noffset, path, sizeof(path));
  690. if (ret < 0)
  691. goto err_path;
  692. if (strlist_add(node_inc, path))
  693. goto err_mem;
  694. }
  695. return 0;
  696. err_mem:
  697. printf("Out of memory processing configuration '%s/%s'\n", conf_name,
  698. sig_name);
  699. return -ENOMEM;
  700. err_path:
  701. printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n",
  702. iname, conf_name, sig_name, fdt_strerror(ret));
  703. return -ENOENT;
  704. }
  705. /**
  706. * fit_config_get_hash_list() - Get the regions to sign
  707. *
  708. * This calculates a list of nodes to hash for this particular configuration,
  709. * returning it as a string list (struct strlist, not a devicetree string list)
  710. *
  711. * @fit: Pointer to the FIT format image header
  712. * @conf_noffset: Offset of configuration node to sign (child of
  713. * /configurations node)
  714. * @sig_offset: Offset of signature node containing info about how to sign it
  715. * (child of 'signatures' node)
  716. * @return 0 if OK, -ENOENT if an image referred to by the configuration cannot
  717. * be found, -ENOMSG if ther were no images in the configuration
  718. */
  719. static int fit_config_get_hash_list(const void *fit, int conf_noffset,
  720. int sig_offset, struct strlist *node_inc)
  721. {
  722. int allow_missing;
  723. const char *prop, *iname, *end;
  724. const char *conf_name, *sig_name;
  725. char name[200];
  726. int image_count;
  727. int ret, len;
  728. conf_name = fit_get_name(fit, conf_noffset, NULL);
  729. sig_name = fit_get_name(fit, sig_offset, NULL);
  730. /*
  731. * Build a list of nodes we need to hash. We always need the root
  732. * node and the configuration.
  733. */
  734. strlist_init(node_inc);
  735. snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name);
  736. if (strlist_add(node_inc, "/") ||
  737. strlist_add(node_inc, name))
  738. goto err_mem;
  739. /* Get a list of images that we intend to sign */
  740. prop = fit_config_get_image_list(fit, sig_offset, &len,
  741. &allow_missing);
  742. if (!prop)
  743. return 0;
  744. /* Locate the images */
  745. end = prop + len;
  746. image_count = 0;
  747. for (iname = prop; iname < end; iname += strlen(iname) + 1) {
  748. int image_noffset;
  749. int index, max_index;
  750. max_index = fdt_stringlist_count(fit, conf_noffset, iname);
  751. for (index = 0; index < max_index; index++) {
  752. image_noffset = fit_conf_get_prop_node_index(fit, conf_noffset,
  753. iname, index);
  754. if (image_noffset < 0) {
  755. printf("Failed to find image '%s' in configuration '%s/%s'\n",
  756. iname, conf_name, sig_name);
  757. if (allow_missing)
  758. continue;
  759. return -ENOENT;
  760. }
  761. ret = fit_config_add_hash(fit, image_noffset, node_inc,
  762. conf_name, sig_name, iname);
  763. if (ret < 0)
  764. return ret;
  765. image_count++;
  766. }
  767. }
  768. if (!image_count) {
  769. printf("Failed to find any images for configuration '%s/%s'\n",
  770. conf_name, sig_name);
  771. return -ENOMSG;
  772. }
  773. return 0;
  774. err_mem:
  775. printf("Out of memory processing configuration '%s/%s'\n", conf_name,
  776. sig_name);
  777. return -ENOMEM;
  778. }
  779. /**
  780. * fit_config_get_regions() - Get the regions to sign
  781. *
  782. * This calculates a list of node to hash for this particular configuration,
  783. * then finds which regions of the devicetree they correspond to.
  784. *
  785. * @fit: Pointer to the FIT format image header
  786. * @conf_noffset: Offset of configuration node to sign (child of
  787. * /configurations node)
  788. * @sig_offset: Offset of signature node containing info about how to sign it
  789. * (child of 'signatures' node)
  790. * @regionp: Returns list of regions that need to be hashed (allocated; must be
  791. * freed by the caller)
  792. * @region_count: Returns number of regions
  793. * @region_propp: Returns string-list property containing the list of nodes
  794. * that correspond to the regions. Each entry is a full path to the node.
  795. * This is in devicetree format, i.e. a \0 between each string. This is
  796. * allocated and must be freed by the caller.
  797. * @region_proplen: Returns length of *@@region_propp in bytes
  798. * @return 0 if OK, -ENOMEM if out of memory, -EIO if the regions to hash could
  799. * not be found, -EINVAL if no registers were found to hash
  800. */
  801. static int fit_config_get_regions(const void *fit, int conf_noffset,
  802. int sig_offset, struct image_region **regionp,
  803. int *region_countp, char **region_propp,
  804. int *region_proplen)
  805. {
  806. char * const exc_prop[] = {
  807. FIT_DATA_PROP,
  808. FIT_DATA_SIZE_PROP,
  809. FIT_DATA_POSITION_PROP,
  810. FIT_DATA_OFFSET_PROP,
  811. };
  812. struct strlist node_inc;
  813. struct image_region *region;
  814. struct fdt_region fdt_regions[100];
  815. const char *conf_name, *sig_name;
  816. char path[200];
  817. int count, i;
  818. char *region_prop;
  819. int ret, len;
  820. conf_name = fit_get_name(fit, conf_noffset, NULL);
  821. sig_name = fit_get_name(fit, sig_offset, NULL);
  822. debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
  823. /* Get a list of nodes we want to hash */
  824. ret = fit_config_get_hash_list(fit, conf_noffset, sig_offset,
  825. &node_inc);
  826. if (ret)
  827. return ret;
  828. /* Get a list of regions to hash */
  829. count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
  830. exc_prop, ARRAY_SIZE(exc_prop),
  831. fdt_regions, ARRAY_SIZE(fdt_regions),
  832. path, sizeof(path), 1);
  833. if (count < 0) {
  834. printf("Failed to hash configuration '%s/%s': %s\n", conf_name,
  835. sig_name, fdt_strerror(ret));
  836. return -EIO;
  837. }
  838. if (count == 0) {
  839. printf("No data to hash for configuration '%s/%s': %s\n",
  840. conf_name, sig_name, fdt_strerror(ret));
  841. return -EINVAL;
  842. }
  843. /* Build our list of data blocks */
  844. region = fit_region_make_list(fit, fdt_regions, count, NULL);
  845. if (!region) {
  846. printf("Out of memory hashing configuration '%s/%s'\n",
  847. conf_name, sig_name);
  848. return -ENOMEM;
  849. }
  850. /* Create a list of all hashed properties */
  851. debug("Hash nodes:\n");
  852. for (i = len = 0; i < node_inc.count; i++) {
  853. debug(" %s\n", node_inc.strings[i]);
  854. len += strlen(node_inc.strings[i]) + 1;
  855. }
  856. region_prop = malloc(len);
  857. if (!region_prop) {
  858. printf("Out of memory setting up regions for configuration '%s/%s'\n",
  859. conf_name, sig_name);
  860. return -ENOMEM;
  861. }
  862. for (i = len = 0; i < node_inc.count;
  863. len += strlen(node_inc.strings[i]) + 1, i++)
  864. strcpy(region_prop + len, node_inc.strings[i]);
  865. strlist_free(&node_inc);
  866. *region_countp = count;
  867. *regionp = region;
  868. *region_propp = region_prop;
  869. *region_proplen = len;
  870. return 0;
  871. }
  872. /**
  873. * fit_config_process_sig - Process a single subnode of the configurations/ node
  874. *
  875. * Generate a signed hash of the supplied data and store it in the node.
  876. *
  877. * @keydir: Directory containing keys to use for signing
  878. * @keydest: Destination FDT blob to write public keys into (NULL if none)
  879. * @fit: pointer to the FIT format image header
  880. * @conf_name name of config being processed (used to display errors)
  881. * @conf_noffset: Offset of configuration node, e.g. '/configurations/conf-1'
  882. * @noffset: subnode offset, e.g. '/configurations/conf-1/sig-1'
  883. * @comment: Comment to add to signature nodes
  884. * @require_keys: Mark all keys as 'required'
  885. * @engine_id: Engine to use for signing
  886. * @cmdname: Command name used when reporting errors
  887. * @return keydest node if @keydest is non-NULL, else 0 if none; -ve error code
  888. * on failure
  889. */
  890. static int fit_config_process_sig(const char *keydir, const char *keyfile,
  891. void *keydest, void *fit, const char *conf_name,
  892. int conf_noffset, int noffset, const char *comment,
  893. int require_keys, const char *engine_id, const char *cmdname,
  894. const char *algo_name)
  895. {
  896. struct image_sign_info info;
  897. const char *node_name;
  898. struct image_region *region;
  899. char *region_prop;
  900. int region_proplen;
  901. int region_count;
  902. uint8_t *value;
  903. uint value_len;
  904. int ret;
  905. node_name = fit_get_name(fit, noffset, NULL);
  906. if (fit_config_get_regions(fit, conf_noffset, noffset, &region,
  907. &region_count, &region_prop,
  908. &region_proplen))
  909. return -1;
  910. if (fit_image_setup_sig(&info, keydir, keyfile, fit, conf_name, noffset,
  911. require_keys ? "conf" : NULL, engine_id,
  912. algo_name))
  913. return -1;
  914. ret = info.crypto->sign(&info, region, region_count, &value,
  915. &value_len);
  916. free(region);
  917. if (ret) {
  918. printf("Failed to sign '%s' signature node in '%s' conf node\n",
  919. node_name, conf_name);
  920. /* We allow keys to be missing */
  921. if (ret == -ENOENT)
  922. return 0;
  923. return -1;
  924. }
  925. ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
  926. region_prop, region_proplen, cmdname,
  927. algo_name);
  928. if (ret) {
  929. if (ret == -FDT_ERR_NOSPACE)
  930. return -ENOSPC;
  931. printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
  932. node_name, conf_name, fdt_strerror(ret));
  933. return -1;
  934. }
  935. free(value);
  936. free(region_prop);
  937. /* Get keyname again, as FDT has changed and invalidated our pointer */
  938. info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  939. /* Write the public key into the supplied FDT file */
  940. if (keydest) {
  941. ret = info.crypto->add_verify_data(&info, keydest);
  942. if (ret < 0) {
  943. printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n",
  944. node_name, conf_name);
  945. }
  946. return ret;
  947. }
  948. return 0;
  949. }
  950. static int fit_config_add_verification_data(const char *keydir,
  951. const char *keyfile, void *keydest, void *fit, int conf_noffset,
  952. const char *comment, int require_keys, const char *engine_id,
  953. const char *cmdname, const char *algo_name,
  954. struct image_summary *summary)
  955. {
  956. const char *conf_name;
  957. int noffset;
  958. conf_name = fit_get_name(fit, conf_noffset, NULL);
  959. /* Process all hash subnodes of the configuration node */
  960. for (noffset = fdt_first_subnode(fit, conf_noffset);
  961. noffset >= 0;
  962. noffset = fdt_next_subnode(fit, noffset)) {
  963. const char *node_name;
  964. int ret = 0;
  965. node_name = fit_get_name(fit, noffset, NULL);
  966. if (!strncmp(node_name, FIT_SIG_NODENAME,
  967. strlen(FIT_SIG_NODENAME))) {
  968. ret = fit_config_process_sig(keydir, keyfile, keydest,
  969. fit, conf_name, conf_noffset, noffset, comment,
  970. require_keys, engine_id, cmdname, algo_name);
  971. if (ret < 0)
  972. return ret;
  973. summary->sig_offset = noffset;
  974. fdt_get_path(fit, noffset, summary->sig_path,
  975. sizeof(summary->sig_path));
  976. if (keydest) {
  977. summary->keydest_offset = ret;
  978. fdt_get_path(keydest, ret,
  979. summary->keydest_path,
  980. sizeof(summary->keydest_path));
  981. }
  982. }
  983. }
  984. return 0;
  985. }
  986. /*
  987. * 0) open file (open)
  988. * 1) read certificate (PEM_read_X509)
  989. * 2) get public key (X509_get_pubkey)
  990. * 3) provide der format (d2i_RSAPublicKey)
  991. */
  992. static int read_pub_key(const char *keydir, const void *name,
  993. unsigned char **pubkey, int *pubkey_len)
  994. {
  995. char path[1024];
  996. EVP_PKEY *key = NULL;
  997. X509 *cert;
  998. FILE *f;
  999. int ret;
  1000. memset(path, 0, 1024);
  1001. snprintf(path, sizeof(path), "%s/%s.crt", keydir, (char *)name);
  1002. /* Open certificate file */
  1003. f = fopen(path, "r");
  1004. if (!f) {
  1005. fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n",
  1006. path, strerror(errno));
  1007. return -EACCES;
  1008. }
  1009. /* Read the certificate */
  1010. cert = NULL;
  1011. if (!PEM_read_X509(f, &cert, NULL, NULL)) {
  1012. printf("Couldn't read certificate");
  1013. ret = -EINVAL;
  1014. goto err_cert;
  1015. }
  1016. /* Get the public key from the certificate. */
  1017. key = X509_get_pubkey(cert);
  1018. if (!key) {
  1019. printf("Couldn't read public key\n");
  1020. ret = -EINVAL;
  1021. goto err_pubkey;
  1022. }
  1023. /* Get DER form */
  1024. ret = i2d_PublicKey(key, pubkey);
  1025. if (ret < 0) {
  1026. printf("Couldn't get DER form\n");
  1027. ret = -EINVAL;
  1028. goto err_pubkey;
  1029. }
  1030. *pubkey_len = ret;
  1031. ret = 0;
  1032. err_pubkey:
  1033. X509_free(cert);
  1034. err_cert:
  1035. fclose(f);
  1036. return ret;
  1037. }
  1038. int fit_pre_load_data(const char *keydir, void *keydest, void *fit)
  1039. {
  1040. int pre_load_noffset;
  1041. const void *algo_name;
  1042. const void *key_name;
  1043. unsigned char *pubkey = NULL;
  1044. int ret, pubkey_len;
  1045. if (!keydir || !keydest || !fit)
  1046. return 0;
  1047. /* Search node pre-load sig */
  1048. pre_load_noffset = fdt_path_offset(keydest, IMAGE_PRE_LOAD_PATH);
  1049. if (pre_load_noffset < 0) {
  1050. ret = 0;
  1051. goto out;
  1052. }
  1053. algo_name = fdt_getprop(keydest, pre_load_noffset, "algo-name", NULL);
  1054. key_name = fdt_getprop(keydest, pre_load_noffset, "key-name", NULL);
  1055. /* Check that all mandatory properties are present */
  1056. if (!algo_name || !key_name) {
  1057. if (!algo_name)
  1058. printf("The property algo-name is missing in the node %s\n",
  1059. IMAGE_PRE_LOAD_PATH);
  1060. if (!key_name)
  1061. printf("The property key-name is missing in the node %s\n",
  1062. IMAGE_PRE_LOAD_PATH);
  1063. ret = -EINVAL;
  1064. goto out;
  1065. }
  1066. /* Read public key */
  1067. ret = read_pub_key(keydir, key_name, &pubkey, &pubkey_len);
  1068. if (ret < 0)
  1069. goto out;
  1070. /* Add the public key to the device tree */
  1071. ret = fdt_setprop(keydest, pre_load_noffset, "public-key",
  1072. pubkey, pubkey_len);
  1073. if (ret)
  1074. printf("Can't set public-key in node %s (ret = %d)\n",
  1075. IMAGE_PRE_LOAD_PATH, ret);
  1076. out:
  1077. return ret;
  1078. }
  1079. int fit_cipher_data(const char *keydir, void *keydest, void *fit,
  1080. const char *comment, int require_keys,
  1081. const char *engine_id, const char *cmdname)
  1082. {
  1083. int images_noffset;
  1084. int noffset;
  1085. int ret;
  1086. /* Find images parent node offset */
  1087. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  1088. if (images_noffset < 0) {
  1089. printf("Can't find images parent node '%s' (%s)\n",
  1090. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  1091. return images_noffset;
  1092. }
  1093. /* Process its subnodes, print out component images details */
  1094. for (noffset = fdt_first_subnode(fit, images_noffset);
  1095. noffset >= 0;
  1096. noffset = fdt_next_subnode(fit, noffset)) {
  1097. /*
  1098. * Direct child node of the images parent node,
  1099. * i.e. component image node.
  1100. */
  1101. ret = fit_image_cipher_data(keydir, keydest,
  1102. fit, noffset, comment,
  1103. require_keys, engine_id,
  1104. cmdname);
  1105. if (ret)
  1106. return ret;
  1107. }
  1108. return 0;
  1109. }
  1110. int fit_add_verification_data(const char *keydir, const char *keyfile,
  1111. void *keydest, void *fit, const char *comment,
  1112. int require_keys, const char *engine_id,
  1113. const char *cmdname, const char *algo_name,
  1114. struct image_summary *summary)
  1115. {
  1116. int images_noffset, confs_noffset;
  1117. int noffset;
  1118. int ret;
  1119. /* Find images parent node offset */
  1120. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  1121. if (images_noffset < 0) {
  1122. printf("Can't find images parent node '%s' (%s)\n",
  1123. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  1124. return images_noffset;
  1125. }
  1126. /* Process its subnodes, print out component images details */
  1127. for (noffset = fdt_first_subnode(fit, images_noffset);
  1128. noffset >= 0;
  1129. noffset = fdt_next_subnode(fit, noffset)) {
  1130. /*
  1131. * Direct child node of the images parent node,
  1132. * i.e. component image node.
  1133. */
  1134. ret = fit_image_add_verification_data(keydir, keyfile, keydest,
  1135. fit, noffset, comment, require_keys, engine_id,
  1136. cmdname, algo_name);
  1137. if (ret) {
  1138. printf("Can't add verification data for node '%s' (%s)\n",
  1139. fdt_get_name(fit, noffset, NULL),
  1140. fdt_strerror(ret));
  1141. return ret;
  1142. }
  1143. }
  1144. /* If there are no keys, we can't sign configurations */
  1145. if (!IMAGE_ENABLE_SIGN || !(keydir || keyfile))
  1146. return 0;
  1147. /* Find configurations parent node offset */
  1148. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1149. if (confs_noffset < 0) {
  1150. printf("Can't find images parent node '%s' (%s)\n",
  1151. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  1152. return -ENOENT;
  1153. }
  1154. /* Process its subnodes, print out component images details */
  1155. for (noffset = fdt_first_subnode(fit, confs_noffset);
  1156. noffset >= 0;
  1157. noffset = fdt_next_subnode(fit, noffset)) {
  1158. ret = fit_config_add_verification_data(keydir, keyfile, keydest,
  1159. fit, noffset, comment,
  1160. require_keys,
  1161. engine_id, cmdname,
  1162. algo_name, summary);
  1163. if (ret)
  1164. return ret;
  1165. }
  1166. return 0;
  1167. }
  1168. #ifdef CONFIG_FIT_SIGNATURE
  1169. int fit_check_sign(const void *fit, const void *key,
  1170. const char *fit_uname_config)
  1171. {
  1172. int cfg_noffset;
  1173. int ret;
  1174. cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
  1175. if (!cfg_noffset)
  1176. return -1;
  1177. printf("Verifying Hash Integrity for node '%s'... ",
  1178. fdt_get_name(fit, cfg_noffset, NULL));
  1179. ret = fit_config_verify(fit, cfg_noffset);
  1180. if (ret)
  1181. return ret;
  1182. printf("Verified OK, loading images\n");
  1183. ret = bootm_host_load_images(fit, cfg_noffset);
  1184. return ret;
  1185. }
  1186. #endif