blob: b4603c5f01a802134c81921d1b24034a0e458672 [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
51 * a hash of the supplised data and store it in the node.
52 *
53 * @fit: pointer to the FIT format image header
54 * @image_name: name of image being processes (used to display errors)
55 * @noffset: subnode offset
56 * @data: data to process
57 * @size: size of data in bytes
58 * @return 0 if ok, -1 on error
59 */
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;
66 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,
Alex Kiernan795f4522018-06-20 20:10:52 +0000110 int region_proplen, const char *cmdname)
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));
135
136 ret = fit_set_timestamp(fit, noffset, timestamp);
137 }
Simon Glass56518e72013-06-13 15:10:01 -0700138 if (region_prop && !ret) {
139 uint32_t strdata[2];
140
141 ret = fdt_setprop(fit, noffset, "hashed-nodes",
142 region_prop, region_proplen);
Teddy Reed7346c1e2018-06-09 11:45:20 -0400143 /* This is a legacy offset, it is unused, and must remain 0. */
Simon Glass56518e72013-06-13 15:10:01 -0700144 strdata[0] = 0;
145 strdata[1] = cpu_to_fdt32(string_size);
146 if (!ret) {
147 ret = fdt_setprop(fit, noffset, "hashed-strings",
148 strdata, sizeof(strdata));
149 }
150 }
151
152 return ret;
153}
154
155static int fit_image_setup_sig(struct image_sign_info *info,
156 const char *keydir, void *fit, const char *image_name,
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600157 int noffset, const char *require_keys, const char *engine_id)
Simon Glass56518e72013-06-13 15:10:01 -0700158{
159 const char *node_name;
160 char *algo_name;
Philippe Reynes20031562018-11-14 13:51:00 +0100161 const char *padding_name;
Simon Glass56518e72013-06-13 15:10:01 -0700162
163 node_name = fit_get_name(fit, noffset, NULL);
164 if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
165 printf("Can't get algo property for '%s' signature node in '%s' image node\n",
166 node_name, image_name);
167 return -1;
168 }
169
Philippe Reynes20031562018-11-14 13:51:00 +0100170 padding_name = fdt_getprop(fit, noffset, "padding", NULL);
171
Simon Glass56518e72013-06-13 15:10:01 -0700172 memset(info, '\0', sizeof(*info));
173 info->keydir = keydir;
Simon Glass72188f52020-03-18 11:44:06 -0600174 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glass56518e72013-06-13 15:10:01 -0700175 info->fit = fit;
176 info->node_offset = noffset;
Masahiro Yamada1d88a992017-10-27 13:25:21 +0900177 info->name = strdup(algo_name);
Andrew Duda83dd98e2016-11-08 18:53:41 +0000178 info->checksum = image_get_checksum_algo(algo_name);
179 info->crypto = image_get_crypto_algo(algo_name);
Philippe Reynes20031562018-11-14 13:51:00 +0100180 info->padding = image_get_padding_algo(padding_name);
Simon Glass56518e72013-06-13 15:10:01 -0700181 info->require_keys = require_keys;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600182 info->engine_id = engine_id;
Andrew Duda83dd98e2016-11-08 18:53:41 +0000183 if (!info->checksum || !info->crypto) {
Simon Glass56518e72013-06-13 15:10:01 -0700184 printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n",
185 algo_name, node_name, image_name);
186 return -1;
187 }
188
189 return 0;
190}
191
192/**
193 * fit_image_process_sig- Process a single subnode of the images/ node
194 *
195 * Check each subnode and process accordingly. For signature nodes we
196 * generate a signed hash of the supplised data and store it in the node.
197 *
198 * @keydir: Directory containing keys to use for signing
199 * @keydest: Destination FDT blob to write public keys into
200 * @fit: pointer to the FIT format image header
201 * @image_name: name of image being processes (used to display errors)
202 * @noffset: subnode offset
203 * @data: data to process
204 * @size: size of data in bytes
205 * @comment: Comment to add to signature nodes
206 * @require_keys: Mark all keys as 'required'
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600207 * @engine_id: Engine to use for signing
Simon Glass56518e72013-06-13 15:10:01 -0700208 * @return 0 if ok, -1 on error
209 */
210static int fit_image_process_sig(const char *keydir, void *keydest,
211 void *fit, const char *image_name,
212 int noffset, const void *data, size_t size,
Alex Kiernan795f4522018-06-20 20:10:52 +0000213 const char *comment, int require_keys, const char *engine_id,
214 const char *cmdname)
Simon Glass56518e72013-06-13 15:10:01 -0700215{
216 struct image_sign_info info;
217 struct image_region region;
218 const char *node_name;
219 uint8_t *value;
220 uint value_len;
221 int ret;
222
223 if (fit_image_setup_sig(&info, keydir, fit, image_name, noffset,
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600224 require_keys ? "image" : NULL, engine_id))
Simon Glass56518e72013-06-13 15:10:01 -0700225 return -1;
226
227 node_name = fit_get_name(fit, noffset, NULL);
228 region.data = data;
229 region.size = size;
Andrew Duda83dd98e2016-11-08 18:53:41 +0000230 ret = info.crypto->sign(&info, &region, 1, &value, &value_len);
Simon Glass56518e72013-06-13 15:10:01 -0700231 if (ret) {
232 printf("Failed to sign '%s' signature node in '%s' image node: %d\n",
233 node_name, image_name, ret);
234
235 /* We allow keys to be missing */
236 if (ret == -ENOENT)
237 return 0;
238 return -1;
239 }
240
241 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
Alex Kiernan795f4522018-06-20 20:10:52 +0000242 NULL, 0, cmdname);
Simon Glass56518e72013-06-13 15:10:01 -0700243 if (ret) {
Simon Glassa9468112014-06-02 22:04:53 -0600244 if (ret == -FDT_ERR_NOSPACE)
245 return -ENOSPC;
246 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
Simon Glass56518e72013-06-13 15:10:01 -0700247 node_name, image_name, fdt_strerror(ret));
248 return -1;
249 }
250 free(value);
251
252 /* Get keyname again, as FDT has changed and invalidated our pointer */
Simon Glass72188f52020-03-18 11:44:06 -0600253 info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glass56518e72013-06-13 15:10:01 -0700254
mario.six@gdsys.cc713fb2d2016-07-22 08:58:40 +0200255 /*
256 * Write the public key into the supplied FDT file; this might fail
mario.six@gdsys.ccc236ebd2016-07-19 11:07:06 +0200257 * several times, since we try signing with successively increasing
mario.six@gdsys.cc713fb2d2016-07-22 08:58:40 +0200258 * size values
259 */
Masahiro Yamada6793d012017-10-27 15:04:20 +0900260 if (keydest) {
261 ret = info.crypto->add_verify_data(&info, keydest);
262 if (ret) {
263 printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
264 node_name, image_name);
265 return ret;
266 }
267 }
Simon Glass56518e72013-06-13 15:10:01 -0700268
269 return 0;
270}
271
Philippe Reynes7298e422019-12-18 18:25:41 +0100272static int fit_image_read_data(char *filename, unsigned char *data,
273 int expected_size)
274{
275 struct stat sbuf;
276 int fd, ret = -1;
277 ssize_t n;
278
279 /* Open file */
280 fd = open(filename, O_RDONLY | O_BINARY);
281 if (fd < 0) {
282 printf("Can't open file %s (err=%d => %s)\n",
283 filename, errno, strerror(errno));
284 return -1;
285 }
286
287 /* Compute file size */
288 if (fstat(fd, &sbuf) < 0) {
289 printf("Can't fstat file %s (err=%d => %s)\n",
290 filename, errno, strerror(errno));
291 goto err;
292 }
293
294 /* Check file size */
295 if (sbuf.st_size != expected_size) {
296 printf("File %s don't have the expected size (size=%ld, expected=%d)\n",
297 filename, sbuf.st_size, expected_size);
298 goto err;
299 }
300
301 /* Read data */
302 n = read(fd, data, sbuf.st_size);
303 if (n < 0) {
304 printf("Can't read file %s (err=%d => %s)\n",
305 filename, errno, strerror(errno));
306 goto err;
307 }
308
309 /* Check that we have read all the file */
310 if (n != sbuf.st_size) {
Heinrich Schuchardtb21c08a2020-04-11 18:36:04 +0200311 printf("Can't read all file %s (read %zd bytes, expexted %ld)\n",
Philippe Reynes7298e422019-12-18 18:25:41 +0100312 filename, n, sbuf.st_size);
313 goto err;
314 }
315
316 ret = 0;
317
318err:
319 close(fd);
320 return ret;
321}
322
323static int fit_image_setup_cipher(struct image_cipher_info *info,
324 const char *keydir, void *fit,
325 const char *image_name, int image_noffset,
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000326 int noffset)
Philippe Reynes7298e422019-12-18 18:25:41 +0100327{
328 char *algo_name;
329 char filename[128];
330 int ret = -1;
331
332 if (fit_image_cipher_get_algo(fit, noffset, &algo_name)) {
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000333 printf("Can't get algo name for cipher in image '%s'\n",
334 image_name);
Philippe Reynes7298e422019-12-18 18:25:41 +0100335 goto out;
336 }
337
338 info->keydir = keydir;
339
340 /* Read the key name */
Simon Glass72188f52020-03-18 11:44:06 -0600341 info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Philippe Reynes7298e422019-12-18 18:25:41 +0100342 if (!info->keyname) {
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000343 printf("Can't get key name for cipher in image '%s'\n",
344 image_name);
Philippe Reynes7298e422019-12-18 18:25:41 +0100345 goto out;
346 }
347
348 /* Read the IV name */
349 info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL);
350 if (!info->ivname) {
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000351 printf("Can't get iv name for cipher in image '%s'\n",
352 image_name);
Philippe Reynes7298e422019-12-18 18:25:41 +0100353 goto out;
354 }
355
356 info->fit = fit;
357 info->node_noffset = noffset;
358 info->name = algo_name;
359
360 info->cipher = image_get_cipher_algo(algo_name);
361 if (!info->cipher) {
362 printf("Can't get algo for cipher '%s'\n", image_name);
363 goto out;
364 }
365
366 /* Read the key in the file */
367 snprintf(filename, sizeof(filename), "%s/%s%s",
368 info->keydir, info->keyname, ".bin");
369 info->key = malloc(info->cipher->key_len);
370 if (!info->key) {
371 printf("Can't allocate memory for key\n");
372 ret = -1;
373 goto out;
374 }
375 ret = fit_image_read_data(filename, (unsigned char *)info->key,
376 info->cipher->key_len);
377 if (ret < 0)
378 goto out;
379
380 /* Read the IV in the file */
381 snprintf(filename, sizeof(filename), "%s/%s%s",
382 info->keydir, info->ivname, ".bin");
383 info->iv = malloc(info->cipher->iv_len);
384 if (!info->iv) {
385 printf("Can't allocate memory for iv\n");
386 ret = -1;
387 goto out;
388 }
389 ret = fit_image_read_data(filename, (unsigned char *)info->iv,
390 info->cipher->iv_len);
391
392 out:
393 return ret;
394}
395
396int fit_image_write_cipher(void *fit, int image_noffset, int noffset,
397 const void *data, size_t size,
398 unsigned char *data_ciphered, int data_ciphered_len)
399{
400 int ret = -1;
401
Patrick Oppenlander04aeebb2020-07-30 14:22:14 +1000402 /* Replace data with ciphered data */
Philippe Reynes7298e422019-12-18 18:25:41 +0100403 ret = fdt_setprop(fit, image_noffset, FIT_DATA_PROP,
404 data_ciphered, data_ciphered_len);
Patrick Oppenlander04aeebb2020-07-30 14:22:14 +1000405 if (ret == -FDT_ERR_NOSPACE) {
406 ret = -ENOSPC;
407 goto out;
408 }
Philippe Reynes7298e422019-12-18 18:25:41 +0100409 if (ret) {
Patrick Oppenlander04aeebb2020-07-30 14:22:14 +1000410 printf("Can't replace data with ciphered data (err = %d)\n", ret);
Philippe Reynes7298e422019-12-18 18:25:41 +0100411 goto out;
412 }
413
414 /* add non ciphered data size */
415 ret = fdt_setprop_u32(fit, image_noffset, "data-size-unciphered", size);
Patrick Oppenlander04aeebb2020-07-30 14:22:14 +1000416 if (ret == -FDT_ERR_NOSPACE) {
417 ret = -ENOSPC;
418 goto out;
419 }
Philippe Reynes7298e422019-12-18 18:25:41 +0100420 if (ret) {
421 printf("Can't add unciphered data size (err = %d)\n", ret);
422 goto out;
423 }
424
425 out:
426 return ret;
427}
428
429static int
430fit_image_process_cipher(const char *keydir, void *keydest, void *fit,
431 const char *image_name, int image_noffset,
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000432 int node_noffset, const void *data, size_t size,
Philippe Reynes7298e422019-12-18 18:25:41 +0100433 const char *cmdname)
434{
435 struct image_cipher_info info;
436 unsigned char *data_ciphered = NULL;
437 int data_ciphered_len;
438 int ret;
439
440 memset(&info, 0, sizeof(info));
441
442 ret = fit_image_setup_cipher(&info, keydir, fit, image_name,
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000443 image_noffset, node_noffset);
Philippe Reynes7298e422019-12-18 18:25:41 +0100444 if (ret)
445 goto out;
446
447 ret = info.cipher->encrypt(&info, data, size,
448 &data_ciphered, &data_ciphered_len);
449 if (ret)
450 goto out;
451
452 /*
453 * Write the public key into the supplied FDT file; this might fail
454 * several times, since we try signing with successively increasing
455 * size values
456 */
457 if (keydest) {
458 ret = info.cipher->add_cipher_data(&info, keydest);
459 if (ret) {
460 printf("Failed to add verification data for cipher '%s' in image '%s'\n",
461 info.keyname, image_name);
462 goto out;
463 }
464 }
465
466 ret = fit_image_write_cipher(fit, image_noffset, node_noffset,
467 data, size,
468 data_ciphered, data_ciphered_len);
469
470 out:
471 free(data_ciphered);
472 free((void *)info.key);
473 free((void *)info.iv);
474 return ret;
475}
476
477int fit_image_cipher_data(const char *keydir, void *keydest,
478 void *fit, int image_noffset, const char *comment,
479 int require_keys, const char *engine_id,
480 const char *cmdname)
481{
482 const char *image_name;
483 const void *data;
484 size_t size;
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000485 int cipher_node_offset;
Philippe Reynes7298e422019-12-18 18:25:41 +0100486
487 /* Get image name */
488 image_name = fit_get_name(fit, image_noffset, NULL);
489 if (!image_name) {
490 printf("Can't get image name\n");
491 return -1;
492 }
493
494 /* Get image data and data length */
495 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
496 printf("Can't get image data/size\n");
497 return -1;
498 }
499
Philippe Reynes7298e422019-12-18 18:25:41 +0100500
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000501 /* Process cipher node if present */
502 cipher_node_offset = fdt_subnode_offset(fit, image_noffset,
503 FIT_CIPHER_NODENAME);
504 if (cipher_node_offset == -FDT_ERR_NOTFOUND)
505 return 0;
506 if (cipher_node_offset < 0) {
507 printf("Failure getting cipher node\n");
508 return -1;
Philippe Reynes7298e422019-12-18 18:25:41 +0100509 }
Patrick Oppenlanderc5202662020-07-30 14:22:13 +1000510 if (!IMAGE_ENABLE_ENCRYPT || !keydir)
511 return 0;
512 return fit_image_process_cipher(keydir, keydest, fit, image_name,
513 image_noffset, cipher_node_offset, data, size, cmdname);
Philippe Reynes7298e422019-12-18 18:25:41 +0100514}
515
Simon Glass56518e72013-06-13 15:10:01 -0700516/**
517 * fit_image_add_verification_data() - calculate/set verig. data for image node
518 *
519 * This adds hash and signature values for an component image node.
Simon Glassbbb467d2013-05-07 06:12:01 +0000520 *
521 * All existing hash subnodes are checked, if algorithm property is set to
522 * one of the supported hash algorithms, hash value is computed and
523 * corresponding hash node property is set, for example:
Simon Glass604f23d2013-05-07 06:11:54 +0000524 *
525 * Input component image node structure:
526 *
Andre Przywarab2267e82017-12-04 02:05:10 +0000527 * o image-1 (at image_noffset)
Simon Glass604f23d2013-05-07 06:11:54 +0000528 * | - data = [binary data]
Andre Przywarab2267e82017-12-04 02:05:10 +0000529 * o hash-1
Simon Glass604f23d2013-05-07 06:11:54 +0000530 * |- algo = "sha1"
531 *
532 * Output component image node structure:
533 *
Andre Przywarab2267e82017-12-04 02:05:10 +0000534 * o image-1 (at image_noffset)
Simon Glass604f23d2013-05-07 06:11:54 +0000535 * | - data = [binary data]
Andre Przywarab2267e82017-12-04 02:05:10 +0000536 * o hash-1
Simon Glass604f23d2013-05-07 06:11:54 +0000537 * |- algo = "sha1"
538 * |- value = sha1(data)
539 *
Simon Glassbbb467d2013-05-07 06:12:01 +0000540 * For signature details, please see doc/uImage.FIT/signature.txt
541 *
Simon Glass56518e72013-06-13 15:10:01 -0700542 * @keydir Directory containing *.key and *.crt files (or NULL)
543 * @keydest FDT Blob to write public keys into (NULL if none)
Simon Glassbbb467d2013-05-07 06:12:01 +0000544 * @fit: Pointer to the FIT format image header
545 * @image_noffset: Requested component image node
Simon Glass56518e72013-06-13 15:10:01 -0700546 * @comment: Comment to add to signature nodes
547 * @require_keys: Mark all keys as 'required'
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600548 * @engine_id: Engine to use for signing
Simon Glassbbb467d2013-05-07 06:12:01 +0000549 * @return: 0 on success, <0 on failure
Simon Glass604f23d2013-05-07 06:11:54 +0000550 */
Simon Glass56518e72013-06-13 15:10:01 -0700551int fit_image_add_verification_data(const char *keydir, void *keydest,
552 void *fit, int image_noffset, const char *comment,
Alex Kiernan795f4522018-06-20 20:10:52 +0000553 int require_keys, const char *engine_id, const char *cmdname)
Simon Glass604f23d2013-05-07 06:11:54 +0000554{
Simon Glassbbb467d2013-05-07 06:12:01 +0000555 const char *image_name;
Simon Glass604f23d2013-05-07 06:11:54 +0000556 const void *data;
557 size_t size;
Simon Glass604f23d2013-05-07 06:11:54 +0000558 int noffset;
Simon Glass604f23d2013-05-07 06:11:54 +0000559
560 /* Get image data and data length */
561 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
562 printf("Can't get image data/size\n");
563 return -1;
564 }
565
Simon Glass94e5fa42013-05-07 06:11:55 +0000566 image_name = fit_get_name(fit, image_noffset, NULL);
567
Simon Glass604f23d2013-05-07 06:11:54 +0000568 /* Process all hash subnodes of the component image node */
Simon Glassbbb467d2013-05-07 06:12:01 +0000569 for (noffset = fdt_first_subnode(fit, image_noffset);
570 noffset >= 0;
571 noffset = fdt_next_subnode(fit, noffset)) {
572 const char *node_name;
573 int ret = 0;
574
575 /*
576 * Check subnode name, must be equal to "hash" or "signature".
577 * Multiple hash nodes require unique unit node
Andre Przywarab2267e82017-12-04 02:05:10 +0000578 * names, e.g. hash-1, hash-2, signature-1, etc.
Simon Glassbbb467d2013-05-07 06:12:01 +0000579 */
580 node_name = fit_get_name(fit, noffset, NULL);
581 if (!strncmp(node_name, FIT_HASH_NODENAME,
582 strlen(FIT_HASH_NODENAME))) {
583 ret = fit_image_process_hash(fit, image_name, noffset,
584 data, size);
Simon Glass56518e72013-06-13 15:10:01 -0700585 } else if (IMAGE_ENABLE_SIGN && keydir &&
586 !strncmp(node_name, FIT_SIG_NODENAME,
587 strlen(FIT_SIG_NODENAME))) {
588 ret = fit_image_process_sig(keydir, keydest,
589 fit, image_name, noffset, data, size,
Alex Kiernan795f4522018-06-20 20:10:52 +0000590 comment, require_keys, engine_id, cmdname);
Simon Glass604f23d2013-05-07 06:11:54 +0000591 }
Simon Glassbbb467d2013-05-07 06:12:01 +0000592 if (ret)
Simon Glass1152a052016-07-03 09:40:44 -0600593 return ret;
Simon Glassbbb467d2013-05-07 06:12:01 +0000594 }
595
596 return 0;
597}
598
Simon Glass4d098522013-06-13 15:10:09 -0700599struct strlist {
600 int count;
601 char **strings;
602};
603
604static void strlist_init(struct strlist *list)
605{
606 memset(list, '\0', sizeof(*list));
607}
608
609static void strlist_free(struct strlist *list)
610{
611 int i;
612
613 for (i = 0; i < list->count; i++)
614 free(list->strings[i]);
615 free(list->strings);
616}
617
618static int strlist_add(struct strlist *list, const char *str)
619{
620 char *dup;
621
622 dup = strdup(str);
623 list->strings = realloc(list->strings,
624 (list->count + 1) * sizeof(char *));
625 if (!list || !str)
626 return -1;
627 list->strings[list->count++] = dup;
628
629 return 0;
630}
631
632static const char *fit_config_get_image_list(void *fit, int noffset,
633 int *lenp, int *allow_missingp)
634{
635 static const char default_list[] = FIT_KERNEL_PROP "\0"
636 FIT_FDT_PROP;
637 const char *prop;
638
639 /* If there is an "image" property, use that */
640 prop = fdt_getprop(fit, noffset, "sign-images", lenp);
641 if (prop) {
642 *allow_missingp = 0;
643 return *lenp ? prop : NULL;
644 }
645
646 /* Default image list */
647 *allow_missingp = 1;
648 *lenp = sizeof(default_list);
649
650 return default_list;
651}
652
653static int fit_config_get_hash_list(void *fit, int conf_noffset,
654 int sig_offset, struct strlist *node_inc)
655{
656 int allow_missing;
657 const char *prop, *iname, *end;
658 const char *conf_name, *sig_name;
659 char name[200], path[200];
660 int image_count;
661 int ret, len;
662
663 conf_name = fit_get_name(fit, conf_noffset, NULL);
664 sig_name = fit_get_name(fit, sig_offset, NULL);
665
666 /*
667 * Build a list of nodes we need to hash. We always need the root
668 * node and the configuration.
669 */
670 strlist_init(node_inc);
671 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name);
672 if (strlist_add(node_inc, "/") ||
673 strlist_add(node_inc, name))
674 goto err_mem;
675
676 /* Get a list of images that we intend to sign */
Heiko Schocher66b36f82014-03-03 12:19:23 +0100677 prop = fit_config_get_image_list(fit, sig_offset, &len,
Simon Glass4d098522013-06-13 15:10:09 -0700678 &allow_missing);
679 if (!prop)
680 return 0;
681
682 /* Locate the images */
683 end = prop + len;
684 image_count = 0;
685 for (iname = prop; iname < end; iname += strlen(iname) + 1) {
686 int noffset;
687 int image_noffset;
688 int hash_count;
689
690 image_noffset = fit_conf_get_prop_node(fit, conf_noffset,
691 iname);
692 if (image_noffset < 0) {
693 printf("Failed to find image '%s' in configuration '%s/%s'\n",
694 iname, conf_name, sig_name);
695 if (allow_missing)
696 continue;
697
698 return -ENOENT;
699 }
700
701 ret = fdt_get_path(fit, image_noffset, path, sizeof(path));
702 if (ret < 0)
703 goto err_path;
704 if (strlist_add(node_inc, path))
705 goto err_mem;
706
707 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH,
708 conf_name);
709
710 /* Add all this image's hashes */
711 hash_count = 0;
712 for (noffset = fdt_first_subnode(fit, image_noffset);
713 noffset >= 0;
714 noffset = fdt_next_subnode(fit, noffset)) {
715 const char *name = fit_get_name(fit, noffset, NULL);
716
717 if (strncmp(name, FIT_HASH_NODENAME,
718 strlen(FIT_HASH_NODENAME)))
719 continue;
720 ret = fdt_get_path(fit, noffset, path, sizeof(path));
721 if (ret < 0)
722 goto err_path;
723 if (strlist_add(node_inc, path))
724 goto err_mem;
725 hash_count++;
726 }
727
728 if (!hash_count) {
729 printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n",
730 conf_name, sig_name, iname);
731 return -ENOMSG;
732 }
733
734 image_count++;
735 }
736
737 if (!image_count) {
738 printf("Failed to find any images for configuration '%s/%s'\n",
739 conf_name, sig_name);
740 return -ENOMSG;
741 }
742
743 return 0;
744
745err_mem:
746 printf("Out of memory processing configuration '%s/%s'\n", conf_name,
747 sig_name);
748 return -ENOMEM;
749
750err_path:
751 printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n",
752 iname, conf_name, sig_name, fdt_strerror(ret));
753 return -ENOENT;
754}
755
756static int fit_config_get_data(void *fit, int conf_noffset, int noffset,
757 struct image_region **regionp, int *region_countp,
758 char **region_propp, int *region_proplen)
759{
760 char * const exc_prop[] = {"data"};
761 struct strlist node_inc;
762 struct image_region *region;
763 struct fdt_region fdt_regions[100];
764 const char *conf_name, *sig_name;
765 char path[200];
766 int count, i;
767 char *region_prop;
768 int ret, len;
769
770 conf_name = fit_get_name(fit, conf_noffset, NULL);
Masahiro Yamada16067e62017-10-19 19:16:21 +0900771 sig_name = fit_get_name(fit, noffset, NULL);
Simon Glass4d098522013-06-13 15:10:09 -0700772 debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
773
774 /* Get a list of nodes we want to hash */
775 ret = fit_config_get_hash_list(fit, conf_noffset, noffset, &node_inc);
776 if (ret)
777 return ret;
778
779 /* Get a list of regions to hash */
780 count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
781 exc_prop, ARRAY_SIZE(exc_prop),
782 fdt_regions, ARRAY_SIZE(fdt_regions),
783 path, sizeof(path), 1);
784 if (count < 0) {
785 printf("Failed to hash configuration '%s/%s': %s\n", conf_name,
786 sig_name, fdt_strerror(ret));
787 return -EIO;
788 }
789 if (count == 0) {
790 printf("No data to hash for configuration '%s/%s': %s\n",
791 conf_name, sig_name, fdt_strerror(ret));
792 return -EINVAL;
793 }
794
795 /* Build our list of data blocks */
796 region = fit_region_make_list(fit, fdt_regions, count, NULL);
797 if (!region) {
798 printf("Out of memory hashing configuration '%s/%s'\n",
799 conf_name, sig_name);
800 return -ENOMEM;
801 }
802
803 /* Create a list of all hashed properties */
804 debug("Hash nodes:\n");
805 for (i = len = 0; i < node_inc.count; i++) {
806 debug(" %s\n", node_inc.strings[i]);
807 len += strlen(node_inc.strings[i]) + 1;
808 }
809 region_prop = malloc(len);
810 if (!region_prop) {
811 printf("Out of memory setting up regions for configuration '%s/%s'\n",
812 conf_name, sig_name);
813 return -ENOMEM;
814 }
815 for (i = len = 0; i < node_inc.count;
816 len += strlen(node_inc.strings[i]) + 1, i++)
817 strcpy(region_prop + len, node_inc.strings[i]);
818 strlist_free(&node_inc);
819
820 *region_countp = count;
821 *regionp = region;
822 *region_propp = region_prop;
823 *region_proplen = len;
824
825 return 0;
826}
827
828static int fit_config_process_sig(const char *keydir, void *keydest,
829 void *fit, const char *conf_name, int conf_noffset,
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600830 int noffset, const char *comment, int require_keys,
Alex Kiernan795f4522018-06-20 20:10:52 +0000831 const char *engine_id, const char *cmdname)
Simon Glass4d098522013-06-13 15:10:09 -0700832{
833 struct image_sign_info info;
834 const char *node_name;
835 struct image_region *region;
836 char *region_prop;
837 int region_proplen;
838 int region_count;
839 uint8_t *value;
840 uint value_len;
841 int ret;
842
843 node_name = fit_get_name(fit, noffset, NULL);
844 if (fit_config_get_data(fit, conf_noffset, noffset, &region,
845 &region_count, &region_prop, &region_proplen))
846 return -1;
847
848 if (fit_image_setup_sig(&info, keydir, fit, conf_name, noffset,
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600849 require_keys ? "conf" : NULL, engine_id))
Simon Glass4d098522013-06-13 15:10:09 -0700850 return -1;
851
Andrew Duda83dd98e2016-11-08 18:53:41 +0000852 ret = info.crypto->sign(&info, region, region_count, &value,
853 &value_len);
Simon Glass4d098522013-06-13 15:10:09 -0700854 free(region);
855 if (ret) {
856 printf("Failed to sign '%s' signature node in '%s' conf node\n",
857 node_name, conf_name);
858
859 /* We allow keys to be missing */
860 if (ret == -ENOENT)
861 return 0;
862 return -1;
863 }
864
Simon Glassa9468112014-06-02 22:04:53 -0600865 ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
Alex Kiernan795f4522018-06-20 20:10:52 +0000866 region_prop, region_proplen, cmdname);
Simon Glassa9468112014-06-02 22:04:53 -0600867 if (ret) {
868 if (ret == -FDT_ERR_NOSPACE)
869 return -ENOSPC;
870 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
871 node_name, conf_name, fdt_strerror(ret));
Simon Glass4d098522013-06-13 15:10:09 -0700872 return -1;
873 }
874 free(value);
875 free(region_prop);
876
877 /* Get keyname again, as FDT has changed and invalidated our pointer */
Simon Glass72188f52020-03-18 11:44:06 -0600878 info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
Simon Glass4d098522013-06-13 15:10:09 -0700879
880 /* Write the public key into the supplied FDT file */
Simon Glassa9468112014-06-02 22:04:53 -0600881 if (keydest) {
Andrew Duda83dd98e2016-11-08 18:53:41 +0000882 ret = info.crypto->add_verify_data(&info, keydest);
Simon Glassa9468112014-06-02 22:04:53 -0600883 if (ret) {
Masahiro Yamada76b9cba2017-10-27 15:04:21 +0900884 printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n",
Simon Glassa9468112014-06-02 22:04:53 -0600885 node_name, conf_name);
Simon Glassa9468112014-06-02 22:04:53 -0600886 }
Simon Glass597a8b22014-06-12 07:24:42 -0600887 return ret;
Simon Glass4d098522013-06-13 15:10:09 -0700888 }
889
890 return 0;
891}
892
893static int fit_config_add_verification_data(const char *keydir, void *keydest,
894 void *fit, int conf_noffset, const char *comment,
Alex Kiernan795f4522018-06-20 20:10:52 +0000895 int require_keys, const char *engine_id, const char *cmdname)
Simon Glass4d098522013-06-13 15:10:09 -0700896{
897 const char *conf_name;
898 int noffset;
899
900 conf_name = fit_get_name(fit, conf_noffset, NULL);
901
902 /* Process all hash subnodes of the configuration node */
903 for (noffset = fdt_first_subnode(fit, conf_noffset);
904 noffset >= 0;
905 noffset = fdt_next_subnode(fit, noffset)) {
906 const char *node_name;
907 int ret = 0;
908
909 node_name = fit_get_name(fit, noffset, NULL);
910 if (!strncmp(node_name, FIT_SIG_NODENAME,
911 strlen(FIT_SIG_NODENAME))) {
912 ret = fit_config_process_sig(keydir, keydest,
913 fit, conf_name, conf_noffset, noffset, comment,
Alex Kiernan795f4522018-06-20 20:10:52 +0000914 require_keys, engine_id, cmdname);
Simon Glass4d098522013-06-13 15:10:09 -0700915 }
916 if (ret)
917 return ret;
918 }
919
920 return 0;
921}
922
Philippe Reynes7298e422019-12-18 18:25:41 +0100923int fit_cipher_data(const char *keydir, void *keydest, void *fit,
924 const char *comment, int require_keys,
925 const char *engine_id, const char *cmdname)
926{
927 int images_noffset;
928 int noffset;
929 int ret;
930
931 /* Find images parent node offset */
932 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
933 if (images_noffset < 0) {
934 printf("Can't find images parent node '%s' (%s)\n",
935 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
936 return images_noffset;
937 }
938
939 /* Process its subnodes, print out component images details */
940 for (noffset = fdt_first_subnode(fit, images_noffset);
941 noffset >= 0;
942 noffset = fdt_next_subnode(fit, noffset)) {
943 /*
944 * Direct child node of the images parent node,
945 * i.e. component image node.
946 */
947 ret = fit_image_cipher_data(keydir, keydest,
948 fit, noffset, comment,
949 require_keys, engine_id,
950 cmdname);
951 if (ret)
952 return ret;
953 }
954
955 return 0;
956}
957
Simon Glass56518e72013-06-13 15:10:01 -0700958int fit_add_verification_data(const char *keydir, void *keydest, void *fit,
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600959 const char *comment, int require_keys,
Alex Kiernan795f4522018-06-20 20:10:52 +0000960 const char *engine_id, const char *cmdname)
Simon Glassbbb467d2013-05-07 06:12:01 +0000961{
Simon Glass4d098522013-06-13 15:10:09 -0700962 int images_noffset, confs_noffset;
Simon Glassbbb467d2013-05-07 06:12:01 +0000963 int noffset;
964 int ret;
965
966 /* Find images parent node offset */
967 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
968 if (images_noffset < 0) {
969 printf("Can't find images parent node '%s' (%s)\n",
970 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
971 return images_noffset;
972 }
973
974 /* Process its subnodes, print out component images details */
975 for (noffset = fdt_first_subnode(fit, images_noffset);
976 noffset >= 0;
977 noffset = fdt_next_subnode(fit, noffset)) {
978 /*
979 * Direct child node of the images parent node,
980 * i.e. component image node.
981 */
Simon Glass56518e72013-06-13 15:10:01 -0700982 ret = fit_image_add_verification_data(keydir, keydest,
Alex Kiernan795f4522018-06-20 20:10:52 +0000983 fit, noffset, comment, require_keys, engine_id,
984 cmdname);
Simon Glassbbb467d2013-05-07 06:12:01 +0000985 if (ret)
986 return ret;
Simon Glass604f23d2013-05-07 06:11:54 +0000987 }
988
Simon Glass4d098522013-06-13 15:10:09 -0700989 /* If there are no keys, we can't sign configurations */
990 if (!IMAGE_ENABLE_SIGN || !keydir)
991 return 0;
992
993 /* Find configurations parent node offset */
994 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
995 if (confs_noffset < 0) {
996 printf("Can't find images parent node '%s' (%s)\n",
Heiko Schocher04a710a2014-08-11 11:02:17 +0200997 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Simon Glass4d098522013-06-13 15:10:09 -0700998 return -ENOENT;
999 }
1000
1001 /* Process its subnodes, print out component images details */
1002 for (noffset = fdt_first_subnode(fit, confs_noffset);
1003 noffset >= 0;
1004 noffset = fdt_next_subnode(fit, noffset)) {
1005 ret = fit_config_add_verification_data(keydir, keydest,
1006 fit, noffset, comment,
George McCollisterf1ca1fd2017-01-06 13:14:17 -06001007 require_keys,
Alex Kiernan795f4522018-06-20 20:10:52 +00001008 engine_id, cmdname);
Simon Glass4d098522013-06-13 15:10:09 -07001009 if (ret)
1010 return ret;
1011 }
1012
Simon Glass604f23d2013-05-07 06:11:54 +00001013 return 0;
1014}
Heiko Schocher29a23f92014-03-03 12:19:30 +01001015
1016#ifdef CONFIG_FIT_SIGNATURE
Simon Glassc3aa81e2020-03-18 11:44:03 -06001017int fit_check_sign(const void *fit, const void *key,
1018 const char *fit_uname_config)
Heiko Schocher29a23f92014-03-03 12:19:30 +01001019{
1020 int cfg_noffset;
1021 int ret;
1022
Simon Glassc3aa81e2020-03-18 11:44:03 -06001023 cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
Heiko Schocher29a23f92014-03-03 12:19:30 +01001024 if (!cfg_noffset)
1025 return -1;
1026
Simon Glass382cf622020-03-18 11:43:56 -06001027 printf("Verifying Hash Integrity for node '%s'... ",
1028 fdt_get_name(fit, cfg_noffset, NULL));
Simon Glassce1400f2014-06-12 07:24:53 -06001029 ret = fit_config_verify(fit, cfg_noffset);
1030 if (ret)
1031 return ret;
Simon Glassc3aa81e2020-03-18 11:44:03 -06001032 printf("Verified OK, loading images\n");
Simon Glassce1400f2014-06-12 07:24:53 -06001033 ret = bootm_host_load_images(fit, cfg_noffset);
1034
Heiko Schocher29a23f92014-03-03 12:19:30 +01001035 return ret;
1036}
1037#endif