blob: a5d47a4f99722c18fafc7c72c050c259f5c26a77 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass604f23d2013-05-07 06:11:54 +00002/*
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.
Simon Glass604f23d2013-05-07 06:11:54 +00009 */
10
11#include "mkimage.h"
Simon Glassce1400f2014-06-12 07:24:53 -060012#include <bootm.h>
Masahiro Yamada64045a62020-04-16 18:30:18 +090013#include <fdt_region.h>
Simon Glass604f23d2013-05-07 06:11:54 +000014#include <image.h>
Simon Glass56518e72013-06-13 15:10:01 -070015#include <version.h>
Simon Glass604f23d2013-05-07 06:11:54 +000016
17/**
Simon Glassb7260912013-05-07 06:11:56 +000018 * 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 */
31static int fit_set_hash_value(void *fit, int noffset, uint8_t *value,
32 int value_len)
33{
34 int ret;
35
36 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
37 if (ret) {
38 printf("Can't set hash '%s' property for '%s' node(%s)\n",
39 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
40 fdt_strerror(ret));
Simon Glass1152a052016-07-03 09:40:44 -060041 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
Simon Glassb7260912013-05-07 06:11:56 +000042 }
43
44 return 0;
45}
46
47/**
Simon Glass94e5fa42013-05-07 06:11:55 +000048 * fit_image_process_hash - Process a single subnode of the images/ node
49 *
50 * Check each subnode and process accordingly. For hash nodes we generate
Simon Glass70e6bcc2021-11-12 12:28:06 -070051 * a hash of the supplied data and store it in the node.
Simon Glass94e5fa42013-05-07 06:11:55 +000052 *
53 * @fit: pointer to the FIT format image header
Simon Glass70e6bcc2021-11-12 12:28:06 -070054 * @image_name: name of image being processed (used to display errors)
Simon Glass94e5fa42013-05-07 06:11:55 +000055 * @noffset: subnode offset
56 * @data: data to process
57 * @size: size of data in bytes
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010058 * Return: 0 if ok, -1 on error
Simon Glass94e5fa42013-05-07 06:11:55 +000059 */
60static int fit_image_process_hash(void *fit, const char *image_name,
61 int noffset, const void *data, size_t size)
62{
63 uint8_t value[FIT_MAX_HASH_LEN];
Simon Glassbbb467d2013-05-07 06:12:01 +000064 const char *node_name;
Simon Glass94e5fa42013-05-07 06:11:55 +000065 int value_len;
Jan Kiszka4550ce92022-01-14 10:21:17 +010066 const char *algo;
Simon Glass1152a052016-07-03 09:40:44 -060067 int ret;
Simon Glass94e5fa42013-05-07 06:11:55 +000068
Simon Glassbbb467d2013-05-07 06:12:01 +000069 node_name = fit_get_name(fit, noffset, NULL);
Simon Glass94e5fa42013-05-07 06:11:55 +000070
71 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
72 printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
Simon Glassbbb467d2013-05-07 06:12:01 +000073 node_name, image_name);
Simon Glass1152a052016-07-03 09:40:44 -060074 return -ENOENT;
Simon Glass94e5fa42013-05-07 06:11:55 +000075 }
76
77 if (calculate_hash(data, size, algo, value, &value_len)) {
78 printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
Simon Glassbbb467d2013-05-07 06:12:01 +000079 algo, node_name, image_name);
Simon Glass1152a052016-07-03 09:40:44 -060080 return -EPROTONOSUPPORT;
Simon Glass94e5fa42013-05-07 06:11:55 +000081 }
82
Simon Glass1152a052016-07-03 09:40:44 -060083 ret = fit_set_hash_value(fit, noffset, value, value_len);
84 if (ret) {
Simon Glass94e5fa42013-05-07 06:11:55 +000085 printf("Can't set hash value for '%s' hash node in '%s' image node\n",
Simon Glassbbb467d2013-05-07 06:12:01 +000086 node_name, image_name);
Simon Glass1152a052016-07-03 09:40:44 -060087 return ret;
Simon Glass94e5fa42013-05-07 06:11:55 +000088 }
89
90 return 0;
91}
92
93/**
Simon Glass56518e72013-06-13 15:10:01 -070094 * fit_image_write_sig() - write the signature to a FIT
Simon Glass604f23d2013-05-07 06:11:54 +000095 *
Simon Glass56518e72013-06-13 15:10:01 -070096 * This writes the signature and signer data to the FIT.
97 *
98 * @fit: pointer to the FIT format image header
99 * @noffset: hash node offset
100 * @value: signature value to be set
101 * @value_len: signature value length
102 * @comment: Text comment to write (NULL for none)
103 *
104 * returns
105 * 0, on success
106 * -FDT_ERR_..., on failure
107 */
108static int fit_image_write_sig(void *fit, int noffset, uint8_t *value,
109 int value_len, const char *comment, const char *region_prop,
Jan Kiszka5902a392022-01-14 10:21:19 +0100110 int region_proplen, const char *cmdname, const char *algo_name)
Simon Glass56518e72013-06-13 15:10:01 -0700111{
112 int string_size;
113 int ret;
114
115 /*
116 * Get the current string size, before we update the FIT and add
117 * more
118 */
119 string_size = fdt_size_dt_strings(fit);
120
121 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
122 if (!ret) {
123 ret = fdt_setprop_string(fit, noffset, "signer-name",
124 "mkimage");
125 }
126 if (!ret) {
127 ret = fdt_setprop_string(fit, noffset, "signer-version",
128 PLAIN_VERSION);
129 }
130 if (comment && !ret)
131 ret = fdt_setprop_string(fit, noffset, "comment", comment);
Alex Kiernan795f4522018-06-20 20:10:52 +0000132 if (!ret) {
133 time_t timestamp = imagetool_get_source_date(cmdname,
134 time(NULL));
Ming Liu7c397992021-05-31 09:04:51 +0200135 uint32_t t = cpu_to_uimage(timestamp);
Alex Kiernan795f4522018-06-20 20:10:52 +0000136
Ming Liu7c397992021-05-31 09:04:51 +0200137 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
138 sizeof(uint32_t));
Alex Kiernan795f4522018-06-20 20:10:52 +0000139 }
Simon Glass56518e72013-06-13 15:10:01 -0700140 if (region_prop && !ret) {
141 uint32_t strdata[2];
142
143 ret = fdt_setprop(fit, noffset, "hashed-nodes",
144 region_prop, region_proplen);
Teddy Reed7346c1e2018-06-09 11:45:20 -0400145 /* This is a legacy offset, it is unused, and must remain 0. */
Simon Glass56518e72013-06-13 15:10:01 -0700146 strdata[0] = 0;
147 strdata[1] = cpu_to_fdt32(string_size);
148 if (!ret) {
149 ret = fdt_setprop(fit, noffset, "hashed-strings",
150 strdata, sizeof(strdata));
151 }
152 }
Jan Kiszka5902a392022-01-14 10:21:19 +0100153 if (algo_name && !ret)
154 ret = fdt_setprop_string(fit, noffset, "algo", algo_name);
Simon Glass56518e72013-06-13 15:10:01 -0700155
156 return ret;
157}
158
159static int fit_image_setup_sig(struct image_sign_info *info,
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -0600160 const char *keydir, const char *keyfile, void *fit,
161 const char *image_name, int noffset, const char *require_keys,
Jan Kiszka5902a392022-01-14 10:21:19 +0100162 const char *engine_id, const char *algo_name)
Simon Glass56518e72013-06-13 15:10:01 -0700163{
164 const char *node_name;
Philippe Reynes20031562018-11-14 13:51:00 +0100165 const char *padding_name;
Simon Glass56518e72013-06-13 15:10:01 -0700166
167 node_name = fit_get_name(fit, noffset, NULL);
Jan Kiszka5902a392022-01-14 10:21:19 +0100168 if (!algo_name) {
169 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
170 printf("Can't get algo property for '%s' signature node in '%s' image node\n",
171 node_name, image_name);
172 return -1;
173 }
Simon Glass56518e72013-06-13 15:10:01 -0700174 }
175
Philippe Reynes20031562018-11-14 13:51:00 +0100176 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
177
Simon Glass56518e72013-06-13 15:10:01 -0700178 memset(info, '\0', sizeof(*info));
179 info->keydir = keydir;
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -0600180 info->keyfile = keyfile;
Simon Glass72188f52020-03-18 11:44:06 -0600181 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glass56518e72013-06-13 15:10:01 -0700182 info->fit = fit;
183 info->node_offset = noffset;
Masahiro Yamada1d88a992017-10-27 13:25:21 +0900184 info->name = strdup(algo_name);
Andrew Duda83dd98e2016-11-08 18:53:41 +0000185 info->checksum = image_get_checksum_algo(algo_name);
186 info->crypto = image_get_crypto_algo(algo_name);
Philippe Reynes20031562018-11-14 13:51:00 +0100187 info->padding = image_get_padding_algo(padding_name);
Simon Glass56518e72013-06-13 15:10:01 -0700188 info->require_keys = require_keys;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600189 info->engine_id = engine_id;
Andrew Duda83dd98e2016-11-08 18:53:41 +0000190 if (!info->checksum || !info->crypto) {
Simon Glass56518e72013-06-13 15:10:01 -0700191 printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n",
192 algo_name, node_name, image_name);
193 return -1;
194 }
195
196 return 0;
197}
198
199/**
200 * fit_image_process_sig- Process a single subnode of the images/ node
201 *
202 * Check each subnode and process accordingly. For signature nodes we
Simon Glass70e6bcc2021-11-12 12:28:06 -0700203 * generate a signed hash of the supplied data and store it in the node.
Simon Glass56518e72013-06-13 15:10:01 -0700204 *
205 * @keydir: Directory containing keys to use for signing
Simon Glass70e6bcc2021-11-12 12:28:06 -0700206 * @keydest: Destination FDT blob to write public keys into (NULL if none)
Simon Glass56518e72013-06-13 15:10:01 -0700207 * @fit: pointer to the FIT format image header
Simon Glass70e6bcc2021-11-12 12:28:06 -0700208 * @image_name: name of image being processed (used to display errors)
Simon Glass56518e72013-06-13 15:10:01 -0700209 * @noffset: subnode offset
210 * @data: data to process
211 * @size: size of data in bytes
212 * @comment: Comment to add to signature nodes
213 * @require_keys: Mark all keys as 'required'
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600214 * @engine_id: Engine to use for signing
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100215 * Return: 0 if ok, -1 on error
Simon Glass56518e72013-06-13 15:10:01 -0700216 */
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -0600217static int fit_image_process_sig(const char *keydir, const char *keyfile,
218 void *keydest, void *fit, const char *image_name,
Simon Glass56518e72013-06-13 15:10:01 -0700219 int noffset, const void *data, size_t size,
Alex Kiernan795f4522018-06-20 20:10:52 +0000220 const char *comment, int require_keys, const char *engine_id,
Jan Kiszka5902a392022-01-14 10:21:19 +0100221 const char *cmdname, const char *algo_name)
Simon Glass56518e72013-06-13 15:10:01 -0700222{
223 struct image_sign_info info;
224 struct image_region region;
225 const char *node_name;
226 uint8_t *value;
227 uint value_len;
228 int ret;
229
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -0600230 if (fit_image_setup_sig(&info, keydir, keyfile, fit, image_name,
231 noffset, require_keys ? "image" : NULL,
Jan Kiszka5902a392022-01-14 10:21:19 +0100232 engine_id, algo_name))
Simon Glass56518e72013-06-13 15:10:01 -0700233 return -1;
234
235 node_name = fit_get_name(fit, noffset, NULL);
236 region.data = data;
237 region.size = size;
Andrew Duda83dd98e2016-11-08 18:53:41 +0000238 ret = info.crypto->sign(&info, &region, 1, &value, &value_len);
Simon Glass56518e72013-06-13 15:10:01 -0700239 if (ret) {
240 printf("Failed to sign '%s' signature node in '%s' image node: %d\n",
241 node_name, image_name, ret);
242
243 /* We allow keys to be missing */
244 if (ret == -ENOENT)
245 return 0;
246 return -1;
247 }
248
249 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
Jan Kiszka5902a392022-01-14 10:21:19 +0100250 NULL, 0, cmdname, algo_name);
Simon Glass56518e72013-06-13 15:10:01 -0700251 if (ret) {
Simon Glassa9468112014-06-02 22:04:53 -0600252 if (ret == -FDT_ERR_NOSPACE)
253 return -ENOSPC;
254 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
Simon Glass56518e72013-06-13 15:10:01 -0700255 node_name, image_name, fdt_strerror(ret));
256 return -1;
257 }
258 free(value);
259
260 /* Get keyname again, as FDT has changed and invalidated our pointer */
Simon Glass72188f52020-03-18 11:44:06 -0600261 info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glass56518e72013-06-13 15:10:01 -0700262
mario.six@gdsys.cc713fb2d2016-07-22 08:58:40 +0200263 /*
264 * Write the public key into the supplied FDT file; this might fail
mario.six@gdsys.ccc236ebd2016-07-19 11:07:06 +0200265 * several times, since we try signing with successively increasing
mario.six@gdsys.cc713fb2d2016-07-22 08:58:40 +0200266 * size values
267 */
Masahiro Yamada6793d012017-10-27 15:04:20 +0900268 if (keydest) {
269 ret = info.crypto->add_verify_data(&info, keydest);
270 if (ret) {
271 printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
272 node_name, image_name);
273 return ret;
274 }
275 }
Simon Glass56518e72013-06-13 15:10:01 -0700276
277 return 0;
278}
279
Philippe Reynes7298e422019-12-18 18:25:41 +0100280static int fit_image_read_data(char *filename, unsigned char *data,
281 int expected_size)
282{
283 struct stat sbuf;
284 int fd, ret = -1;
285 ssize_t n;
286
287 /* Open file */
288 fd = open(filename, O_RDONLY | O_BINARY);
289 if (fd < 0) {
290 printf("Can't open file %s (err=%d => %s)\n",
291 filename, errno, strerror(errno));
292 return -1;
293 }
294
295 /* Compute file size */
296 if (fstat(fd, &sbuf) < 0) {
297 printf("Can't fstat file %s (err=%d => %s)\n",
298 filename, errno, strerror(errno));
299 goto err;
300 }
301
302 /* Check file size */
303 if (sbuf.st_size != expected_size) {
Heinrich Schuchardt3311eda2020-10-08 20:51:24 +0200304 printf("File %s don't have the expected size (size=%lld, expected=%d)\n",
305 filename, (long long)sbuf.st_size, expected_size);
Philippe Reynes7298e422019-12-18 18:25:41 +0100306 goto err;
307 }
308
309 /* Read data */
310 n = read(fd, data, sbuf.st_size);
311 if (n < 0) {
312 printf("Can't read file %s (err=%d => %s)\n",
313 filename, errno, strerror(errno));
314 goto err;
315 }
316
317 /* Check that we have read all the file */
318 if (n != sbuf.st_size) {
Vagrant Cascadian2c6bcab2021-09-28 10:11:46 -0700319 printf("Can't read all file %s (read %zd bytes, expected %lld)\n",
Heinrich Schuchardt3311eda2020-10-08 20:51:24 +0200320 filename, n, (long long)sbuf.st_size);
Philippe Reynes7298e422019-12-18 18:25:41 +0100321 goto err;
322 }
323
324 ret = 0;
325
326err:
327 close(fd);
328 return ret;
329}
330
Philippe Reynesa6982a62020-09-17 15:01:46 +0200331static int get_random_data(void *data, int size)
332{
333 unsigned char *tmp = data;
334 struct timespec date;
Simon Glass7f0f4e12021-05-13 19:39:20 -0600335 int i, ret;
Philippe Reynesa6982a62020-09-17 15:01:46 +0200336
337 if (!tmp) {
338 printf("%s: pointer data is NULL\n", __func__);
339 ret = -1;
340 goto out;
341 }
342
343 ret = clock_gettime(CLOCK_MONOTONIC, &date);
Simon Glass7f0f4e12021-05-13 19:39:20 -0600344 if (ret) {
345 printf("%s: clock_gettime has failed (%s)\n", __func__,
346 strerror(errno));
Philippe Reynesa6982a62020-09-17 15:01:46 +0200347 goto out;
348 }
349
Philippe Reynescc34f042020-11-13 16:37:46 +0100350 srandom(date.tv_nsec);
Philippe Reynesa6982a62020-09-17 15:01:46 +0200351
352 for (i = 0; i < size; i++) {
Philippe Reynescc34f042020-11-13 16:37:46 +0100353 *tmp = random() & 0xff;
Philippe Reynesa6982a62020-09-17 15:01:46 +0200354 tmp++;
355 }
356
357 out:
358 return ret;
359}
360
Philippe Reynes7298e422019-12-18 18:25:41 +0100361static int fit_image_setup_cipher(struct image_cipher_info *info,
362 const char *keydir, void *fit,
363 const char *image_name, int image_noffset,
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000364 int noffset)
Philippe Reynes7298e422019-12-18 18:25:41 +0100365{
366 char *algo_name;
367 char filename[128];
368 int ret = -1;
369
370 if (fit_image_cipher_get_algo(fit, noffset, &algo_name)) {
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000371 printf("Can't get algo name for cipher in image '%s'\n",
372 image_name);
Philippe Reynes7298e422019-12-18 18:25:41 +0100373 goto out;
374 }
375
376 info->keydir = keydir;
377
378 /* Read the key name */
Simon Glass72188f52020-03-18 11:44:06 -0600379 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Philippe Reynes7298e422019-12-18 18:25:41 +0100380 if (!info->keyname) {
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000381 printf("Can't get key name for cipher in image '%s'\n",
382 image_name);
Philippe Reynes7298e422019-12-18 18:25:41 +0100383 goto out;
384 }
385
Philippe Reynesa6982a62020-09-17 15:01:46 +0200386 /*
387 * Read the IV name
388 *
389 * If this property is not provided then mkimage will generate
390 * a random IV and store it in the FIT image
391 */
Philippe Reynes7298e422019-12-18 18:25:41 +0100392 info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL);
Philippe Reynes7298e422019-12-18 18:25:41 +0100393
394 info->fit = fit;
395 info->node_noffset = noffset;
396 info->name = algo_name;
397
398 info->cipher = image_get_cipher_algo(algo_name);
399 if (!info->cipher) {
400 printf("Can't get algo for cipher '%s'\n", image_name);
401 goto out;
402 }
403
404 /* Read the key in the file */
405 snprintf(filename, sizeof(filename), "%s/%s%s",
406 info->keydir, info->keyname, ".bin");
407 info->key = malloc(info->cipher->key_len);
408 if (!info->key) {
409 printf("Can't allocate memory for key\n");
410 ret = -1;
411 goto out;
412 }
413 ret = fit_image_read_data(filename, (unsigned char *)info->key,
414 info->cipher->key_len);
415 if (ret < 0)
416 goto out;
417
Philippe Reynes7298e422019-12-18 18:25:41 +0100418 info->iv = malloc(info->cipher->iv_len);
419 if (!info->iv) {
420 printf("Can't allocate memory for iv\n");
421 ret = -1;
422 goto out;
423 }
Philippe Reynesa6982a62020-09-17 15:01:46 +0200424
425 if (info->ivname) {
426 /* Read the IV in the file */
427 snprintf(filename, sizeof(filename), "%s/%s%s",
428 info->keydir, info->ivname, ".bin");
429 ret = fit_image_read_data(filename, (unsigned char *)info->iv,
430 info->cipher->iv_len);
431 } else {
432 /* Generate an ramdom IV */
433 ret = get_random_data((void *)info->iv, info->cipher->iv_len);
434 }
Philippe Reynes7298e422019-12-18 18:25:41 +0100435
436 out:
437 return ret;
438}
439
440int fit_image_write_cipher(void *fit, int image_noffset, int noffset,
441 const void *data, size_t size,
442 unsigned char *data_ciphered, int data_ciphered_len)
443{
444 int ret = -1;
445
Patrick Oppenlander04aeebb2020-07-30 14:22:14 +1000446 /* Replace data with ciphered data */
Philippe Reynes7298e422019-12-18 18:25:41 +0100447 ret = fdt_setprop(fit, image_noffset, FIT_DATA_PROP,
448 data_ciphered, data_ciphered_len);
Patrick Oppenlander04aeebb2020-07-30 14:22:14 +1000449 if (ret == -FDT_ERR_NOSPACE) {
450 ret = -ENOSPC;
451 goto out;
452 }
Philippe Reynes7298e422019-12-18 18:25:41 +0100453 if (ret) {
Patrick Oppenlander04aeebb2020-07-30 14:22:14 +1000454 printf("Can't replace data with ciphered data (err = %d)\n", ret);
Philippe Reynes7298e422019-12-18 18:25:41 +0100455 goto out;
456 }
457
458 /* add non ciphered data size */
459 ret = fdt_setprop_u32(fit, image_noffset, "data-size-unciphered", size);
Patrick Oppenlander04aeebb2020-07-30 14:22:14 +1000460 if (ret == -FDT_ERR_NOSPACE) {
461 ret = -ENOSPC;
462 goto out;
463 }
Philippe Reynes7298e422019-12-18 18:25:41 +0100464 if (ret) {
465 printf("Can't add unciphered data size (err = %d)\n", ret);
466 goto out;
467 }
468
469 out:
470 return ret;
471}
472
473static int
474fit_image_process_cipher(const char *keydir, void *keydest, void *fit,
475 const char *image_name, int image_noffset,
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000476 int node_noffset, const void *data, size_t size,
Philippe Reynes7298e422019-12-18 18:25:41 +0100477 const char *cmdname)
478{
479 struct image_cipher_info info;
480 unsigned char *data_ciphered = NULL;
481 int data_ciphered_len;
482 int ret;
483
484 memset(&info, 0, sizeof(info));
485
486 ret = fit_image_setup_cipher(&info, keydir, fit, image_name,
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000487 image_noffset, node_noffset);
Philippe Reynes7298e422019-12-18 18:25:41 +0100488 if (ret)
489 goto out;
490
491 ret = info.cipher->encrypt(&info, data, size,
492 &data_ciphered, &data_ciphered_len);
493 if (ret)
494 goto out;
495
496 /*
497 * Write the public key into the supplied FDT file; this might fail
498 * several times, since we try signing with successively increasing
499 * size values
Philippe Reynesa6982a62020-09-17 15:01:46 +0200500 * And, if needed, write the iv in the FIT file
Philippe Reynes7298e422019-12-18 18:25:41 +0100501 */
502 if (keydest) {
Philippe Reynesa6982a62020-09-17 15:01:46 +0200503 ret = info.cipher->add_cipher_data(&info, keydest, fit, node_noffset);
Philippe Reynes7298e422019-12-18 18:25:41 +0100504 if (ret) {
505 printf("Failed to add verification data for cipher '%s' in image '%s'\n",
506 info.keyname, image_name);
507 goto out;
508 }
509 }
510
511 ret = fit_image_write_cipher(fit, image_noffset, node_noffset,
512 data, size,
513 data_ciphered, data_ciphered_len);
514
515 out:
516 free(data_ciphered);
517 free((void *)info.key);
518 free((void *)info.iv);
519 return ret;
520}
521
522int fit_image_cipher_data(const char *keydir, void *keydest,
523 void *fit, int image_noffset, const char *comment,
524 int require_keys, const char *engine_id,
525 const char *cmdname)
526{
527 const char *image_name;
528 const void *data;
529 size_t size;
Patrick Oppenlanderb33e5cc2020-07-30 14:22:15 +1000530 int cipher_node_offset, len;
Philippe Reynes7298e422019-12-18 18:25:41 +0100531
532 /* Get image name */
533 image_name = fit_get_name(fit, image_noffset, NULL);
534 if (!image_name) {
535 printf("Can't get image name\n");
536 return -1;
537 }
538
539 /* Get image data and data length */
540 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
541 printf("Can't get image data/size\n");
542 return -1;
543 }
544
Patrick Oppenlanderb33e5cc2020-07-30 14:22:15 +1000545 /*
546 * Don't cipher ciphered data.
547 *
548 * If the data-size-unciphered property is present the data for this
549 * image is already encrypted. This is important as 'mkimage -F' can be
550 * run multiple times on a FIT image.
551 */
552 if (fdt_getprop(fit, image_noffset, "data-size-unciphered", &len))
553 return 0;
554 if (len != -FDT_ERR_NOTFOUND) {
555 printf("Failure testing for data-size-unciphered\n");
556 return -1;
557 }
Philippe Reynes7298e422019-12-18 18:25:41 +0100558
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000559 /* Process cipher node if present */
560 cipher_node_offset = fdt_subnode_offset(fit, image_noffset,
561 FIT_CIPHER_NODENAME);
562 if (cipher_node_offset == -FDT_ERR_NOTFOUND)
563 return 0;
564 if (cipher_node_offset < 0) {
565 printf("Failure getting cipher node\n");
566 return -1;
Philippe Reynes7298e422019-12-18 18:25:41 +0100567 }
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000568 if (!IMAGE_ENABLE_ENCRYPT || !keydir)
569 return 0;
570 return fit_image_process_cipher(keydir, keydest, fit, image_name,
571 image_noffset, cipher_node_offset, data, size, cmdname);
Philippe Reynes7298e422019-12-18 18:25:41 +0100572}
573
Simon Glass56518e72013-06-13 15:10:01 -0700574/**
575 * fit_image_add_verification_data() - calculate/set verig. data for image node
576 *
577 * This adds hash and signature values for an component image node.
Simon Glassbbb467d2013-05-07 06:12:01 +0000578 *
579 * All existing hash subnodes are checked, if algorithm property is set to
580 * one of the supported hash algorithms, hash value is computed and
581 * corresponding hash node property is set, for example:
Simon Glass604f23d2013-05-07 06:11:54 +0000582 *
583 * Input component image node structure:
584 *
Andre Przywarab2267e82017-12-04 02:05:10 +0000585 * o image-1 (at image_noffset)
Simon Glass604f23d2013-05-07 06:11:54 +0000586 * | - data = [binary data]
Andre Przywarab2267e82017-12-04 02:05:10 +0000587 * o hash-1
Simon Glass604f23d2013-05-07 06:11:54 +0000588 * |- algo = "sha1"
589 *
590 * Output component image node structure:
591 *
Andre Przywarab2267e82017-12-04 02:05:10 +0000592 * o image-1 (at image_noffset)
Simon Glass604f23d2013-05-07 06:11:54 +0000593 * | - data = [binary data]
Andre Przywarab2267e82017-12-04 02:05:10 +0000594 * o hash-1
Simon Glass604f23d2013-05-07 06:11:54 +0000595 * |- algo = "sha1"
596 * |- value = sha1(data)
597 *
Simon Glassbbb467d2013-05-07 06:12:01 +0000598 * For signature details, please see doc/uImage.FIT/signature.txt
599 *
Simon Glass56518e72013-06-13 15:10:01 -0700600 * @keydir Directory containing *.key and *.crt files (or NULL)
601 * @keydest FDT Blob to write public keys into (NULL if none)
Simon Glassbbb467d2013-05-07 06:12:01 +0000602 * @fit: Pointer to the FIT format image header
603 * @image_noffset: Requested component image node
Simon Glass56518e72013-06-13 15:10:01 -0700604 * @comment: Comment to add to signature nodes
605 * @require_keys: Mark all keys as 'required'
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600606 * @engine_id: Engine to use for signing
Simon Glassbbb467d2013-05-07 06:12:01 +0000607 * @return: 0 on success, <0 on failure
Simon Glass604f23d2013-05-07 06:11:54 +0000608 */
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -0600609int fit_image_add_verification_data(const char *keydir, const char *keyfile,
610 void *keydest, void *fit, int image_noffset,
611 const char *comment, int require_keys, const char *engine_id,
Jan Kiszka5902a392022-01-14 10:21:19 +0100612 const char *cmdname, const char* algo_name)
Simon Glass604f23d2013-05-07 06:11:54 +0000613{
Simon Glassbbb467d2013-05-07 06:12:01 +0000614 const char *image_name;
Simon Glass604f23d2013-05-07 06:11:54 +0000615 const void *data;
616 size_t size;
Simon Glass604f23d2013-05-07 06:11:54 +0000617 int noffset;
Simon Glass604f23d2013-05-07 06:11:54 +0000618
619 /* Get image data and data length */
620 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
621 printf("Can't get image data/size\n");
622 return -1;
623 }
624
Simon Glass94e5fa42013-05-07 06:11:55 +0000625 image_name = fit_get_name(fit, image_noffset, NULL);
626
Simon Glass604f23d2013-05-07 06:11:54 +0000627 /* Process all hash subnodes of the component image node */
Simon Glassbbb467d2013-05-07 06:12:01 +0000628 for (noffset = fdt_first_subnode(fit, image_noffset);
629 noffset >= 0;
630 noffset = fdt_next_subnode(fit, noffset)) {
631 const char *node_name;
632 int ret = 0;
633
634 /*
635 * Check subnode name, must be equal to "hash" or "signature".
636 * Multiple hash nodes require unique unit node
Andre Przywarab2267e82017-12-04 02:05:10 +0000637 * names, e.g. hash-1, hash-2, signature-1, etc.
Simon Glassbbb467d2013-05-07 06:12:01 +0000638 */
639 node_name = fit_get_name(fit, noffset, NULL);
640 if (!strncmp(node_name, FIT_HASH_NODENAME,
641 strlen(FIT_HASH_NODENAME))) {
642 ret = fit_image_process_hash(fit, image_name, noffset,
643 data, size);
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -0600644 } else if (IMAGE_ENABLE_SIGN && (keydir || keyfile) &&
Simon Glass56518e72013-06-13 15:10:01 -0700645 !strncmp(node_name, FIT_SIG_NODENAME,
646 strlen(FIT_SIG_NODENAME))) {
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -0600647 ret = fit_image_process_sig(keydir, keyfile, keydest,
Simon Glass56518e72013-06-13 15:10:01 -0700648 fit, image_name, noffset, data, size,
Jan Kiszka5902a392022-01-14 10:21:19 +0100649 comment, require_keys, engine_id, cmdname,
650 algo_name);
Simon Glass604f23d2013-05-07 06:11:54 +0000651 }
Simon Glassbbb467d2013-05-07 06:12:01 +0000652 if (ret)
Simon Glass1152a052016-07-03 09:40:44 -0600653 return ret;
Simon Glassbbb467d2013-05-07 06:12:01 +0000654 }
655
656 return 0;
657}
658
Simon Glass4d098522013-06-13 15:10:09 -0700659struct strlist {
660 int count;
661 char **strings;
662};
663
664static void strlist_init(struct strlist *list)
665{
666 memset(list, '\0', sizeof(*list));
667}
668
669static void strlist_free(struct strlist *list)
670{
671 int i;
672
673 for (i = 0; i < list->count; i++)
674 free(list->strings[i]);
675 free(list->strings);
676}
677
678static int strlist_add(struct strlist *list, const char *str)
679{
680 char *dup;
681
682 dup = strdup(str);
683 list->strings = realloc(list->strings,
684 (list->count + 1) * sizeof(char *));
685 if (!list || !str)
686 return -1;
687 list->strings[list->count++] = dup;
688
689 return 0;
690}
691
Simon Glass70e6bcc2021-11-12 12:28:06 -0700692static const char *fit_config_get_image_list(const void *fit, int noffset,
693 int *lenp, int *allow_missingp)
Simon Glass4d098522013-06-13 15:10:09 -0700694{
695 static const char default_list[] = FIT_KERNEL_PROP "\0"
696 FIT_FDT_PROP;
697 const char *prop;
698
Simon Glass70e6bcc2021-11-12 12:28:06 -0700699 /* If there is an "sign-image" property, use that */
Simon Glass4d098522013-06-13 15:10:09 -0700700 prop = fdt_getprop(fit, noffset, "sign-images", lenp);
701 if (prop) {
702 *allow_missingp = 0;
703 return *lenp ? prop : NULL;
704 }
705
706 /* Default image list */
707 *allow_missingp = 1;
708 *lenp = sizeof(default_list);
709
710 return default_list;
711}
712
Simon Glass70e6bcc2021-11-12 12:28:06 -0700713/**
714 * fit_config_add_hash() - Add a list of nodes to hash for an image
715 *
716 * This adds a list of paths to image nodes (as referred to by a particular
717 * offset) that need to be hashed, to protect a configuration
718 *
719 * @fit: Pointer to the FIT format image header
720 * @image_noffset: Offset of image to process (e.g. /images/kernel-1)
721 * @node_inc: List of nodes to add to
722 * @conf_name Configuration-node name, child of /configurations node (only
723 * used for error messages)
724 * @sig_name Signature-node name (only used for error messages)
725 * @iname: Name of image being processed (e.g. "kernel-1" (only used
726 * for error messages)
727 */
728static int fit_config_add_hash(const void *fit, int image_noffset,
729 struct strlist *node_inc, const char *conf_name,
730 const char *sig_name, const char *iname)
Philippe Reynes5a4116f2020-11-24 14:39:47 +0100731{
732 char name[200], path[200];
733 int noffset;
734 int hash_count;
735 int ret;
736
737 ret = fdt_get_path(fit, image_noffset, path, sizeof(path));
738 if (ret < 0)
739 goto err_path;
740 if (strlist_add(node_inc, path))
741 goto err_mem;
742
743 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH,
744 conf_name);
745
746 /* Add all this image's hashes */
747 hash_count = 0;
748 for (noffset = fdt_first_subnode(fit, image_noffset);
749 noffset >= 0;
750 noffset = fdt_next_subnode(fit, noffset)) {
751 const char *name = fit_get_name(fit, noffset, NULL);
752
753 if (strncmp(name, FIT_HASH_NODENAME,
754 strlen(FIT_HASH_NODENAME)))
755 continue;
756 ret = fdt_get_path(fit, noffset, path, sizeof(path));
757 if (ret < 0)
758 goto err_path;
759 if (strlist_add(node_inc, path))
760 goto err_mem;
761 hash_count++;
762 }
763
764 if (!hash_count) {
765 printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n",
766 conf_name, sig_name, iname);
767 return -ENOMSG;
768 }
769
770 /* Add this image's cipher node if present */
771 noffset = fdt_subnode_offset(fit, image_noffset,
772 FIT_CIPHER_NODENAME);
773 if (noffset != -FDT_ERR_NOTFOUND) {
774 if (noffset < 0) {
775 printf("Failed to get cipher node in configuration '%s/%s' image '%s': %s\n",
776 conf_name, sig_name, iname,
777 fdt_strerror(noffset));
778 return -EIO;
779 }
780 ret = fdt_get_path(fit, noffset, path, sizeof(path));
781 if (ret < 0)
782 goto err_path;
783 if (strlist_add(node_inc, path))
784 goto err_mem;
785 }
786
787 return 0;
788
789err_mem:
790 printf("Out of memory processing configuration '%s/%s'\n", conf_name,
791 sig_name);
792 return -ENOMEM;
793
794err_path:
795 printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n",
796 iname, conf_name, sig_name, fdt_strerror(ret));
797 return -ENOENT;
798}
799
Simon Glass70e6bcc2021-11-12 12:28:06 -0700800/**
801 * fit_config_get_hash_list() - Get the regions to sign
802 *
803 * This calculates a list of nodes to hash for this particular configuration,
804 * returning it as a string list (struct strlist, not a devicetree string list)
805 *
806 * @fit: Pointer to the FIT format image header
807 * @conf_noffset: Offset of configuration node to sign (child of
808 * /configurations node)
809 * @sig_offset: Offset of signature node containing info about how to sign it
810 * (child of 'signatures' node)
811 * @return 0 if OK, -ENOENT if an image referred to by the configuration cannot
812 * be found, -ENOMSG if ther were no images in the configuration
813 */
814static int fit_config_get_hash_list(const void *fit, int conf_noffset,
Simon Glass4d098522013-06-13 15:10:09 -0700815 int sig_offset, struct strlist *node_inc)
816{
817 int allow_missing;
818 const char *prop, *iname, *end;
819 const char *conf_name, *sig_name;
Philippe Reynes5a4116f2020-11-24 14:39:47 +0100820 char name[200];
Simon Glass4d098522013-06-13 15:10:09 -0700821 int image_count;
822 int ret, len;
823
824 conf_name = fit_get_name(fit, conf_noffset, NULL);
825 sig_name = fit_get_name(fit, sig_offset, NULL);
826
827 /*
828 * Build a list of nodes we need to hash. We always need the root
829 * node and the configuration.
830 */
831 strlist_init(node_inc);
832 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name);
833 if (strlist_add(node_inc, "/") ||
834 strlist_add(node_inc, name))
835 goto err_mem;
836
837 /* Get a list of images that we intend to sign */
Heiko Schocher66b36f82014-03-03 12:19:23 +0100838 prop = fit_config_get_image_list(fit, sig_offset, &len,
Simon Glass4d098522013-06-13 15:10:09 -0700839 &allow_missing);
840 if (!prop)
841 return 0;
842
843 /* Locate the images */
844 end = prop + len;
845 image_count = 0;
846 for (iname = prop; iname < end; iname += strlen(iname) + 1) {
Simon Glass4d098522013-06-13 15:10:09 -0700847 int image_noffset;
Philippe Reynesedfeba72020-11-24 14:39:48 +0100848 int index, max_index;
Simon Glass4d098522013-06-13 15:10:09 -0700849
Philippe Reynesedfeba72020-11-24 14:39:48 +0100850 max_index = fdt_stringlist_count(fit, conf_noffset, iname);
Simon Glass4d098522013-06-13 15:10:09 -0700851
Philippe Reynesedfeba72020-11-24 14:39:48 +0100852 for (index = 0; index < max_index; index++) {
853 image_noffset = fit_conf_get_prop_node_index(fit, conf_noffset,
854 iname, index);
855
856 if (image_noffset < 0) {
857 printf("Failed to find image '%s' in configuration '%s/%s'\n",
858 iname, conf_name, sig_name);
859 if (allow_missing)
860 continue;
861
862 return -ENOENT;
863 }
864
Simon Glass70e6bcc2021-11-12 12:28:06 -0700865 ret = fit_config_add_hash(fit, image_noffset, node_inc,
866 conf_name, sig_name, iname);
Philippe Reynesedfeba72020-11-24 14:39:48 +0100867 if (ret < 0)
868 return ret;
869
870 image_count++;
Simon Glass4d098522013-06-13 15:10:09 -0700871 }
Simon Glass4d098522013-06-13 15:10:09 -0700872 }
873
874 if (!image_count) {
875 printf("Failed to find any images for configuration '%s/%s'\n",
876 conf_name, sig_name);
877 return -ENOMSG;
878 }
879
880 return 0;
881
882err_mem:
883 printf("Out of memory processing configuration '%s/%s'\n", conf_name,
884 sig_name);
885 return -ENOMEM;
Simon Glass4d098522013-06-13 15:10:09 -0700886}
887
Simon Glass70e6bcc2021-11-12 12:28:06 -0700888/**
889 * fit_config_get_regions() - Get the regions to sign
890 *
891 * This calculates a list of node to hash for this particular configuration,
892 * then finds which regions of the devicetree they correspond to.
893 *
894 * @fit: Pointer to the FIT format image header
895 * @conf_noffset: Offset of configuration node to sign (child of
896 * /configurations node)
897 * @sig_offset: Offset of signature node containing info about how to sign it
898 * (child of 'signatures' node)
899 * @regionp: Returns list of regions that need to be hashed (allocated; must be
900 * freed by the caller)
901 * @region_count: Returns number of regions
902 * @region_propp: Returns string-list property containing the list of nodes
903 * that correspond to the regions. Each entry is a full path to the node.
904 * This is in devicetree format, i.e. a \0 between each string. This is
905 * allocated and must be freed by the caller.
906 * @region_proplen: Returns length of *@@region_propp in bytes
907 * @return 0 if OK, -ENOMEM if out of memory, -EIO if the regions to hash could
908 * not be found, -EINVAL if no registers were found to hash
909 */
910static int fit_config_get_regions(const void *fit, int conf_noffset,
911 int sig_offset, struct image_region **regionp,
912 int *region_countp, char **region_propp,
913 int *region_proplen)
Simon Glass4d098522013-06-13 15:10:09 -0700914{
915 char * const exc_prop[] = {"data"};
916 struct strlist node_inc;
917 struct image_region *region;
918 struct fdt_region fdt_regions[100];
919 const char *conf_name, *sig_name;
920 char path[200];
921 int count, i;
922 char *region_prop;
923 int ret, len;
924
925 conf_name = fit_get_name(fit, conf_noffset, NULL);
Simon Glass70e6bcc2021-11-12 12:28:06 -0700926 sig_name = fit_get_name(fit, sig_offset, NULL);
Simon Glass4d098522013-06-13 15:10:09 -0700927 debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
928
929 /* Get a list of nodes we want to hash */
Simon Glass70e6bcc2021-11-12 12:28:06 -0700930 ret = fit_config_get_hash_list(fit, conf_noffset, sig_offset,
931 &node_inc);
Simon Glass4d098522013-06-13 15:10:09 -0700932 if (ret)
933 return ret;
934
935 /* Get a list of regions to hash */
936 count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
937 exc_prop, ARRAY_SIZE(exc_prop),
938 fdt_regions, ARRAY_SIZE(fdt_regions),
939 path, sizeof(path), 1);
940 if (count < 0) {
941 printf("Failed to hash configuration '%s/%s': %s\n", conf_name,
942 sig_name, fdt_strerror(ret));
943 return -EIO;
944 }
945 if (count == 0) {
946 printf("No data to hash for configuration '%s/%s': %s\n",
947 conf_name, sig_name, fdt_strerror(ret));
948 return -EINVAL;
949 }
950
951 /* Build our list of data blocks */
952 region = fit_region_make_list(fit, fdt_regions, count, NULL);
953 if (!region) {
954 printf("Out of memory hashing configuration '%s/%s'\n",
955 conf_name, sig_name);
956 return -ENOMEM;
957 }
958
959 /* Create a list of all hashed properties */
960 debug("Hash nodes:\n");
961 for (i = len = 0; i < node_inc.count; i++) {
962 debug(" %s\n", node_inc.strings[i]);
963 len += strlen(node_inc.strings[i]) + 1;
964 }
965 region_prop = malloc(len);
966 if (!region_prop) {
967 printf("Out of memory setting up regions for configuration '%s/%s'\n",
968 conf_name, sig_name);
969 return -ENOMEM;
970 }
971 for (i = len = 0; i < node_inc.count;
972 len += strlen(node_inc.strings[i]) + 1, i++)
973 strcpy(region_prop + len, node_inc.strings[i]);
974 strlist_free(&node_inc);
975
976 *region_countp = count;
977 *regionp = region;
978 *region_propp = region_prop;
979 *region_proplen = len;
980
981 return 0;
982}
983
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -0600984static int fit_config_process_sig(const char *keydir, const char *keyfile,
Simon Glass70e6bcc2021-11-12 12:28:06 -0700985 void *keydest, void *fit, const char *conf_name,
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -0600986 int conf_noffset, int noffset, const char *comment,
Jan Kiszka5902a392022-01-14 10:21:19 +0100987 int require_keys, const char *engine_id, const char *cmdname,
988 const char *algo_name)
Simon Glass4d098522013-06-13 15:10:09 -0700989{
990 struct image_sign_info info;
991 const char *node_name;
992 struct image_region *region;
993 char *region_prop;
994 int region_proplen;
995 int region_count;
996 uint8_t *value;
997 uint value_len;
998 int ret;
999
1000 node_name = fit_get_name(fit, noffset, NULL);
Simon Glass70e6bcc2021-11-12 12:28:06 -07001001 if (fit_config_get_regions(fit, conf_noffset, noffset, &region,
1002 &region_count, &region_prop,
1003 &region_proplen))
Simon Glass4d098522013-06-13 15:10:09 -07001004 return -1;
1005
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -06001006 if (fit_image_setup_sig(&info, keydir, keyfile, fit, conf_name, noffset,
Jan Kiszka5902a392022-01-14 10:21:19 +01001007 require_keys ? "conf" : NULL, engine_id,
1008 algo_name))
Simon Glass4d098522013-06-13 15:10:09 -07001009 return -1;
1010
Andrew Duda83dd98e2016-11-08 18:53:41 +00001011 ret = info.crypto->sign(&info, region, region_count, &value,
1012 &value_len);
Simon Glass4d098522013-06-13 15:10:09 -07001013 free(region);
1014 if (ret) {
1015 printf("Failed to sign '%s' signature node in '%s' conf node\n",
1016 node_name, conf_name);
1017
1018 /* We allow keys to be missing */
1019 if (ret == -ENOENT)
1020 return 0;
1021 return -1;
1022 }
1023
Simon Glassa9468112014-06-02 22:04:53 -06001024 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
Jan Kiszka5902a392022-01-14 10:21:19 +01001025 region_prop, region_proplen, cmdname,
1026 algo_name);
Simon Glassa9468112014-06-02 22:04:53 -06001027 if (ret) {
1028 if (ret == -FDT_ERR_NOSPACE)
1029 return -ENOSPC;
1030 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
1031 node_name, conf_name, fdt_strerror(ret));
Simon Glass4d098522013-06-13 15:10:09 -07001032 return -1;
1033 }
1034 free(value);
1035 free(region_prop);
1036
1037 /* Get keyname again, as FDT has changed and invalidated our pointer */
Simon Glass72188f52020-03-18 11:44:06 -06001038 info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glass4d098522013-06-13 15:10:09 -07001039
1040 /* Write the public key into the supplied FDT file */
Simon Glassa9468112014-06-02 22:04:53 -06001041 if (keydest) {
Andrew Duda83dd98e2016-11-08 18:53:41 +00001042 ret = info.crypto->add_verify_data(&info, keydest);
Simon Glassa9468112014-06-02 22:04:53 -06001043 if (ret) {
Masahiro Yamada76b9cba2017-10-27 15:04:21 +09001044 printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n",
Simon Glassa9468112014-06-02 22:04:53 -06001045 node_name, conf_name);
Simon Glassa9468112014-06-02 22:04:53 -06001046 }
Simon Glass597a8b22014-06-12 07:24:42 -06001047 return ret;
Simon Glass4d098522013-06-13 15:10:09 -07001048 }
1049
1050 return 0;
1051}
1052
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -06001053static int fit_config_add_verification_data(const char *keydir,
1054 const char *keyfile, void *keydest, void *fit, int conf_noffset,
1055 const char *comment, int require_keys, const char *engine_id,
Jan Kiszka5902a392022-01-14 10:21:19 +01001056 const char *cmdname, const char *algo_name)
Simon Glass4d098522013-06-13 15:10:09 -07001057{
1058 const char *conf_name;
1059 int noffset;
1060
1061 conf_name = fit_get_name(fit, conf_noffset, NULL);
1062
1063 /* Process all hash subnodes of the configuration node */
1064 for (noffset = fdt_first_subnode(fit, conf_noffset);
1065 noffset >= 0;
1066 noffset = fdt_next_subnode(fit, noffset)) {
1067 const char *node_name;
1068 int ret = 0;
1069
1070 node_name = fit_get_name(fit, noffset, NULL);
1071 if (!strncmp(node_name, FIT_SIG_NODENAME,
1072 strlen(FIT_SIG_NODENAME))) {
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -06001073 ret = fit_config_process_sig(keydir, keyfile, keydest,
Simon Glass4d098522013-06-13 15:10:09 -07001074 fit, conf_name, conf_noffset, noffset, comment,
Jan Kiszka5902a392022-01-14 10:21:19 +01001075 require_keys, engine_id, cmdname, algo_name);
Simon Glass4d098522013-06-13 15:10:09 -07001076 }
1077 if (ret)
1078 return ret;
1079 }
1080
1081 return 0;
1082}
1083
Philippe Reynes7298e422019-12-18 18:25:41 +01001084int fit_cipher_data(const char *keydir, void *keydest, void *fit,
1085 const char *comment, int require_keys,
1086 const char *engine_id, const char *cmdname)
1087{
1088 int images_noffset;
1089 int noffset;
1090 int ret;
1091
1092 /* Find images parent node offset */
1093 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1094 if (images_noffset < 0) {
1095 printf("Can't find images parent node '%s' (%s)\n",
1096 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1097 return images_noffset;
1098 }
1099
1100 /* Process its subnodes, print out component images details */
1101 for (noffset = fdt_first_subnode(fit, images_noffset);
1102 noffset >= 0;
1103 noffset = fdt_next_subnode(fit, noffset)) {
1104 /*
1105 * Direct child node of the images parent node,
1106 * i.e. component image node.
1107 */
1108 ret = fit_image_cipher_data(keydir, keydest,
1109 fit, noffset, comment,
1110 require_keys, engine_id,
1111 cmdname);
1112 if (ret)
1113 return ret;
1114 }
1115
1116 return 0;
1117}
1118
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -06001119int fit_add_verification_data(const char *keydir, const char *keyfile,
1120 void *keydest, void *fit, const char *comment,
1121 int require_keys, const char *engine_id,
Jan Kiszka5902a392022-01-14 10:21:19 +01001122 const char *cmdname, const char *algo_name)
Simon Glassbbb467d2013-05-07 06:12:01 +00001123{
Simon Glass4d098522013-06-13 15:10:09 -07001124 int images_noffset, confs_noffset;
Simon Glassbbb467d2013-05-07 06:12:01 +00001125 int noffset;
1126 int ret;
1127
1128 /* Find images parent node offset */
1129 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1130 if (images_noffset < 0) {
1131 printf("Can't find images parent node '%s' (%s)\n",
1132 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1133 return images_noffset;
1134 }
1135
1136 /* Process its subnodes, print out component images details */
1137 for (noffset = fdt_first_subnode(fit, images_noffset);
1138 noffset >= 0;
1139 noffset = fdt_next_subnode(fit, noffset)) {
1140 /*
1141 * Direct child node of the images parent node,
1142 * i.e. component image node.
1143 */
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -06001144 ret = fit_image_add_verification_data(keydir, keyfile, keydest,
Alex Kiernan795f4522018-06-20 20:10:52 +00001145 fit, noffset, comment, require_keys, engine_id,
Jan Kiszka5902a392022-01-14 10:21:19 +01001146 cmdname, algo_name);
Simon Glassbbb467d2013-05-07 06:12:01 +00001147 if (ret)
1148 return ret;
Simon Glass604f23d2013-05-07 06:11:54 +00001149 }
1150
Simon Glass4d098522013-06-13 15:10:09 -07001151 /* If there are no keys, we can't sign configurations */
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -06001152 if (!IMAGE_ENABLE_SIGN || !(keydir || keyfile))
Simon Glass4d098522013-06-13 15:10:09 -07001153 return 0;
1154
1155 /* Find configurations parent node offset */
1156 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1157 if (confs_noffset < 0) {
1158 printf("Can't find images parent node '%s' (%s)\n",
Heiko Schocher04a710a2014-08-11 11:02:17 +02001159 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Simon Glass4d098522013-06-13 15:10:09 -07001160 return -ENOENT;
1161 }
1162
1163 /* Process its subnodes, print out component images details */
1164 for (noffset = fdt_first_subnode(fit, confs_noffset);
1165 noffset >= 0;
1166 noffset = fdt_next_subnode(fit, noffset)) {
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -06001167 ret = fit_config_add_verification_data(keydir, keyfile, keydest,
Simon Glass4d098522013-06-13 15:10:09 -07001168 fit, noffset, comment,
George McCollisterf1ca1fd2017-01-06 13:14:17 -06001169 require_keys,
Jan Kiszka5902a392022-01-14 10:21:19 +01001170 engine_id, cmdname,
1171 algo_name);
Simon Glass4d098522013-06-13 15:10:09 -07001172 if (ret)
1173 return ret;
1174 }
1175
Simon Glass604f23d2013-05-07 06:11:54 +00001176 return 0;
1177}
Heiko Schocher29a23f92014-03-03 12:19:30 +01001178
1179#ifdef CONFIG_FIT_SIGNATURE
Simon Glassc3aa81e2020-03-18 11:44:03 -06001180int fit_check_sign(const void *fit, const void *key,
1181 const char *fit_uname_config)
Heiko Schocher29a23f92014-03-03 12:19:30 +01001182{
1183 int cfg_noffset;
1184 int ret;
1185
Simon Glassc3aa81e2020-03-18 11:44:03 -06001186 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
Heiko Schocher29a23f92014-03-03 12:19:30 +01001187 if (!cfg_noffset)
1188 return -1;
1189
Simon Glass382cf622020-03-18 11:43:56 -06001190 printf("Verifying Hash Integrity for node '%s'... ",
1191 fdt_get_name(fit, cfg_noffset, NULL));
Simon Glassce1400f2014-06-12 07:24:53 -06001192 ret = fit_config_verify(fit, cfg_noffset);
1193 if (ret)
1194 return ret;
Simon Glassc3aa81e2020-03-18 11:44:03 -06001195 printf("Verified OK, loading images\n");
Simon Glassce1400f2014-06-12 07:24:53 -06001196 ret = bootm_host_load_images(fit, cfg_noffset);
1197
Heiko Schocher29a23f92014-03-03 12:19:30 +01001198 return ret;
1199}
1200#endif