Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 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. |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include "mkimage.h" |
Simon Glass | a51991d | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 12 | #include <bootm.h> |
Masahiro Yamada | 6dd1052 | 2020-04-16 18:30:18 +0900 | [diff] [blame] | 13 | #include <fdt_region.h> |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 14 | #include <image.h> |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 15 | #include <version.h> |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 16 | |
| 17 | /** |
Simon Glass | 0ce6e3d | 2013-05-07 06:11:56 +0000 | [diff] [blame] | 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 | |
| 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 Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 41 | return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO; |
Simon Glass | 0ce6e3d | 2013-05-07 06:11:56 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | /** |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 48 | * 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 |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 58 | * Return: 0 if ok, -1 on error |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 59 | */ |
| 60 | static 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 Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 64 | const char *node_name; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 65 | int value_len; |
Jan Kiszka | 27beec2 | 2022-01-14 10:21:17 +0100 | [diff] [blame] | 66 | const char *algo; |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 67 | int ret; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 68 | |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 69 | node_name = fit_get_name(fit, noffset, NULL); |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 70 | |
| 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 Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 73 | node_name, image_name); |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 74 | return -ENOENT; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 75 | } |
| 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 Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 79 | algo, node_name, image_name); |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 80 | return -EPROTONOSUPPORT; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 83 | ret = fit_set_hash_value(fit, noffset, value, value_len); |
| 84 | if (ret) { |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 85 | printf("Can't set hash value for '%s' hash node in '%s' image node\n", |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 86 | node_name, image_name); |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 87 | return ret; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /** |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 94 | * fit_image_write_sig() - write the signature to a FIT |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 95 | * |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 96 | * 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 | */ |
| 108 | static int fit_image_write_sig(void *fit, int noffset, uint8_t *value, |
| 109 | int value_len, const char *comment, const char *region_prop, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 110 | int region_proplen, const char *cmdname, const char *algo_name) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 111 | { |
| 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 Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 132 | if (!ret) { |
| 133 | time_t timestamp = imagetool_get_source_date(cmdname, |
| 134 | time(NULL)); |
Ming Liu | f75f1d7 | 2021-05-31 09:04:51 +0200 | [diff] [blame] | 135 | uint32_t t = cpu_to_uimage(timestamp); |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 136 | |
Ming Liu | f75f1d7 | 2021-05-31 09:04:51 +0200 | [diff] [blame] | 137 | ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t, |
| 138 | sizeof(uint32_t)); |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 139 | } |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 140 | if (region_prop && !ret) { |
| 141 | uint32_t strdata[2]; |
| 142 | |
| 143 | ret = fdt_setprop(fit, noffset, "hashed-nodes", |
| 144 | region_prop, region_proplen); |
Teddy Reed | 9767bf4 | 2018-06-09 11:45:20 -0400 | [diff] [blame] | 145 | /* This is a legacy offset, it is unused, and must remain 0. */ |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 146 | 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 Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 153 | if (algo_name && !ret) |
| 154 | ret = fdt_setprop_string(fit, noffset, "algo", algo_name); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 155 | |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | static int fit_image_setup_sig(struct image_sign_info *info, |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 160 | const char *keydir, const char *keyfile, void *fit, |
| 161 | const char *image_name, int noffset, const char *require_keys, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 162 | const char *engine_id, const char *algo_name) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 163 | { |
| 164 | const char *node_name; |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 165 | const char *padding_name; |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 166 | |
| 167 | node_name = fit_get_name(fit, noffset, NULL); |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 168 | 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 Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 176 | padding_name = fdt_getprop(fit, noffset, "padding", NULL); |
| 177 | |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 178 | memset(info, '\0', sizeof(*info)); |
| 179 | info->keydir = keydir; |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 180 | info->keyfile = keyfile; |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 181 | info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 182 | info->fit = fit; |
| 183 | info->node_offset = noffset; |
Masahiro Yamada | c1f137b | 2017-10-27 13:25:21 +0900 | [diff] [blame] | 184 | info->name = strdup(algo_name); |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 185 | info->checksum = image_get_checksum_algo(algo_name); |
| 186 | info->crypto = image_get_crypto_algo(algo_name); |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 187 | info->padding = image_get_padding_algo(padding_name); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 188 | info->require_keys = require_keys; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 189 | info->engine_id = engine_id; |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 190 | if (!info->checksum || !info->crypto) { |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 191 | 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 |
| 203 | * generate a signed hash of the supplised data and store it in the node. |
| 204 | * |
| 205 | * @keydir: Directory containing keys to use for signing |
| 206 | * @keydest: Destination FDT blob to write public keys into |
| 207 | * @fit: pointer to the FIT format image header |
| 208 | * @image_name: name of image being processes (used to display errors) |
| 209 | * @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 McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 214 | * @engine_id: Engine to use for signing |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 215 | * Return: 0 if ok, -1 on error |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 216 | */ |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 217 | static int fit_image_process_sig(const char *keydir, const char *keyfile, |
| 218 | void *keydest, void *fit, const char *image_name, |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 219 | int noffset, const void *data, size_t size, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 220 | const char *comment, int require_keys, const char *engine_id, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 221 | const char *cmdname, const char *algo_name) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 222 | { |
| 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 Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 230 | if (fit_image_setup_sig(&info, keydir, keyfile, fit, image_name, |
| 231 | noffset, require_keys ? "image" : NULL, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 232 | engine_id, algo_name)) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 233 | return -1; |
| 234 | |
| 235 | node_name = fit_get_name(fit, noffset, NULL); |
| 236 | region.data = data; |
| 237 | region.size = size; |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 238 | ret = info.crypto->sign(&info, ®ion, 1, &value, &value_len); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 239 | 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 Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 250 | NULL, 0, cmdname, algo_name); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 251 | if (ret) { |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 252 | 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 Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 255 | 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 Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 261 | info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 262 | |
mario.six@gdsys.cc | dc4053a | 2016-07-22 08:58:40 +0200 | [diff] [blame] | 263 | /* |
| 264 | * Write the public key into the supplied FDT file; this might fail |
mario.six@gdsys.cc | d019e15 | 2016-07-19 11:07:06 +0200 | [diff] [blame] | 265 | * several times, since we try signing with successively increasing |
mario.six@gdsys.cc | dc4053a | 2016-07-22 08:58:40 +0200 | [diff] [blame] | 266 | * size values |
| 267 | */ |
Masahiro Yamada | 0148676 | 2017-10-27 15:04:20 +0900 | [diff] [blame] | 268 | 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 Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 280 | static 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 Schuchardt | d697a2d | 2020-10-08 20:51:24 +0200 | [diff] [blame] | 304 | printf("File %s don't have the expected size (size=%lld, expected=%d)\n", |
| 305 | filename, (long long)sbuf.st_size, expected_size); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 306 | 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 Cascadian | 396e4ed | 2021-09-28 10:11:46 -0700 | [diff] [blame] | 319 | printf("Can't read all file %s (read %zd bytes, expected %lld)\n", |
Heinrich Schuchardt | d697a2d | 2020-10-08 20:51:24 +0200 | [diff] [blame] | 320 | filename, n, (long long)sbuf.st_size); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 321 | goto err; |
| 322 | } |
| 323 | |
| 324 | ret = 0; |
| 325 | |
| 326 | err: |
| 327 | close(fd); |
| 328 | return ret; |
| 329 | } |
| 330 | |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 331 | static int get_random_data(void *data, int size) |
| 332 | { |
| 333 | unsigned char *tmp = data; |
| 334 | struct timespec date; |
Simon Glass | 7a1745c | 2021-05-13 19:39:20 -0600 | [diff] [blame] | 335 | int i, ret; |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 336 | |
| 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 Glass | 7a1745c | 2021-05-13 19:39:20 -0600 | [diff] [blame] | 344 | if (ret) { |
| 345 | printf("%s: clock_gettime has failed (%s)\n", __func__, |
| 346 | strerror(errno)); |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 347 | goto out; |
| 348 | } |
| 349 | |
Philippe Reynes | 95f2ab2 | 2020-11-13 16:37:46 +0100 | [diff] [blame] | 350 | srandom(date.tv_nsec); |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 351 | |
| 352 | for (i = 0; i < size; i++) { |
Philippe Reynes | 95f2ab2 | 2020-11-13 16:37:46 +0100 | [diff] [blame] | 353 | *tmp = random() & 0xff; |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 354 | tmp++; |
| 355 | } |
| 356 | |
| 357 | out: |
| 358 | return ret; |
| 359 | } |
| 360 | |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 361 | static 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 Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 364 | int noffset) |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 365 | { |
| 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 Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 371 | printf("Can't get algo name for cipher in image '%s'\n", |
| 372 | image_name); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 373 | goto out; |
| 374 | } |
| 375 | |
| 376 | info->keydir = keydir; |
| 377 | |
| 378 | /* Read the key name */ |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 379 | info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 380 | if (!info->keyname) { |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 381 | printf("Can't get key name for cipher in image '%s'\n", |
| 382 | image_name); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 383 | goto out; |
| 384 | } |
| 385 | |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 386 | /* |
| 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 Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 392 | info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 393 | |
| 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 Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 418 | 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 Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 424 | |
| 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 Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 435 | |
| 436 | out: |
| 437 | return ret; |
| 438 | } |
| 439 | |
| 440 | int 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 Oppenlander | be47211 | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 446 | /* Replace data with ciphered data */ |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 447 | ret = fdt_setprop(fit, image_noffset, FIT_DATA_PROP, |
| 448 | data_ciphered, data_ciphered_len); |
Patrick Oppenlander | be47211 | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 449 | if (ret == -FDT_ERR_NOSPACE) { |
| 450 | ret = -ENOSPC; |
| 451 | goto out; |
| 452 | } |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 453 | if (ret) { |
Patrick Oppenlander | be47211 | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 454 | printf("Can't replace data with ciphered data (err = %d)\n", ret); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 455 | goto out; |
| 456 | } |
| 457 | |
| 458 | /* add non ciphered data size */ |
| 459 | ret = fdt_setprop_u32(fit, image_noffset, "data-size-unciphered", size); |
Patrick Oppenlander | be47211 | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 460 | if (ret == -FDT_ERR_NOSPACE) { |
| 461 | ret = -ENOSPC; |
| 462 | goto out; |
| 463 | } |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 464 | 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 | |
| 473 | static int |
| 474 | fit_image_process_cipher(const char *keydir, void *keydest, void *fit, |
| 475 | const char *image_name, int image_noffset, |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 476 | int node_noffset, const void *data, size_t size, |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 477 | 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 Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 487 | image_noffset, node_noffset); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 488 | 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 Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 500 | * And, if needed, write the iv in the FIT file |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 501 | */ |
| 502 | if (keydest) { |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 503 | ret = info.cipher->add_cipher_data(&info, keydest, fit, node_noffset); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 504 | 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 | |
| 522 | int 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 Oppenlander | a4fafb3 | 2020-07-30 14:22:15 +1000 | [diff] [blame] | 530 | int cipher_node_offset, len; |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 531 | |
| 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 Oppenlander | a4fafb3 | 2020-07-30 14:22:15 +1000 | [diff] [blame] | 545 | /* |
| 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 Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 558 | |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 559 | /* 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 Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 567 | } |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 568 | 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 Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 572 | } |
| 573 | |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 574 | /** |
| 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 Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 578 | * |
| 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 Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 582 | * |
| 583 | * Input component image node structure: |
| 584 | * |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 585 | * o image-1 (at image_noffset) |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 586 | * | - data = [binary data] |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 587 | * o hash-1 |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 588 | * |- algo = "sha1" |
| 589 | * |
| 590 | * Output component image node structure: |
| 591 | * |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 592 | * o image-1 (at image_noffset) |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 593 | * | - data = [binary data] |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 594 | * o hash-1 |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 595 | * |- algo = "sha1" |
| 596 | * |- value = sha1(data) |
| 597 | * |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 598 | * For signature details, please see doc/uImage.FIT/signature.txt |
| 599 | * |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 600 | * @keydir Directory containing *.key and *.crt files (or NULL) |
| 601 | * @keydest FDT Blob to write public keys into (NULL if none) |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 602 | * @fit: Pointer to the FIT format image header |
| 603 | * @image_noffset: Requested component image node |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 604 | * @comment: Comment to add to signature nodes |
| 605 | * @require_keys: Mark all keys as 'required' |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 606 | * @engine_id: Engine to use for signing |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 607 | * @return: 0 on success, <0 on failure |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 608 | */ |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 609 | int 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 Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 612 | const char *cmdname, const char* algo_name) |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 613 | { |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 614 | const char *image_name; |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 615 | const void *data; |
| 616 | size_t size; |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 617 | int noffset; |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 618 | |
| 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 Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 625 | image_name = fit_get_name(fit, image_noffset, NULL); |
| 626 | |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 627 | /* Process all hash subnodes of the component image node */ |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 628 | 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 Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 637 | * names, e.g. hash-1, hash-2, signature-1, etc. |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 638 | */ |
| 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 Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 644 | } else if (IMAGE_ENABLE_SIGN && (keydir || keyfile) && |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 645 | !strncmp(node_name, FIT_SIG_NODENAME, |
| 646 | strlen(FIT_SIG_NODENAME))) { |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 647 | ret = fit_image_process_sig(keydir, keyfile, keydest, |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 648 | fit, image_name, noffset, data, size, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 649 | comment, require_keys, engine_id, cmdname, |
| 650 | algo_name); |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 651 | } |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 652 | if (ret) |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 653 | return ret; |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 659 | struct strlist { |
| 660 | int count; |
| 661 | char **strings; |
| 662 | }; |
| 663 | |
| 664 | static void strlist_init(struct strlist *list) |
| 665 | { |
| 666 | memset(list, '\0', sizeof(*list)); |
| 667 | } |
| 668 | |
| 669 | static 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 | |
| 678 | static 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 | |
| 692 | static const char *fit_config_get_image_list(void *fit, int noffset, |
| 693 | int *lenp, int *allow_missingp) |
| 694 | { |
| 695 | static const char default_list[] = FIT_KERNEL_PROP "\0" |
| 696 | FIT_FDT_PROP; |
| 697 | const char *prop; |
| 698 | |
| 699 | /* If there is an "image" property, use that */ |
| 700 | 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 | |
Philippe Reynes | 856bcc8 | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 713 | static int fit_config_add_hash(void *fit, const char *conf_name, const char *sig_name, |
| 714 | struct strlist *node_inc, const char *iname, int image_noffset) |
| 715 | { |
| 716 | char name[200], path[200]; |
| 717 | int noffset; |
| 718 | int hash_count; |
| 719 | int ret; |
| 720 | |
| 721 | ret = fdt_get_path(fit, image_noffset, path, sizeof(path)); |
| 722 | if (ret < 0) |
| 723 | goto err_path; |
| 724 | if (strlist_add(node_inc, path)) |
| 725 | goto err_mem; |
| 726 | |
| 727 | snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, |
| 728 | conf_name); |
| 729 | |
| 730 | /* Add all this image's hashes */ |
| 731 | hash_count = 0; |
| 732 | for (noffset = fdt_first_subnode(fit, image_noffset); |
| 733 | noffset >= 0; |
| 734 | noffset = fdt_next_subnode(fit, noffset)) { |
| 735 | const char *name = fit_get_name(fit, noffset, NULL); |
| 736 | |
| 737 | if (strncmp(name, FIT_HASH_NODENAME, |
| 738 | strlen(FIT_HASH_NODENAME))) |
| 739 | continue; |
| 740 | ret = fdt_get_path(fit, noffset, path, sizeof(path)); |
| 741 | if (ret < 0) |
| 742 | goto err_path; |
| 743 | if (strlist_add(node_inc, path)) |
| 744 | goto err_mem; |
| 745 | hash_count++; |
| 746 | } |
| 747 | |
| 748 | if (!hash_count) { |
| 749 | printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n", |
| 750 | conf_name, sig_name, iname); |
| 751 | return -ENOMSG; |
| 752 | } |
| 753 | |
| 754 | /* Add this image's cipher node if present */ |
| 755 | noffset = fdt_subnode_offset(fit, image_noffset, |
| 756 | FIT_CIPHER_NODENAME); |
| 757 | if (noffset != -FDT_ERR_NOTFOUND) { |
| 758 | if (noffset < 0) { |
| 759 | printf("Failed to get cipher node in configuration '%s/%s' image '%s': %s\n", |
| 760 | conf_name, sig_name, iname, |
| 761 | fdt_strerror(noffset)); |
| 762 | return -EIO; |
| 763 | } |
| 764 | ret = fdt_get_path(fit, noffset, path, sizeof(path)); |
| 765 | if (ret < 0) |
| 766 | goto err_path; |
| 767 | if (strlist_add(node_inc, path)) |
| 768 | goto err_mem; |
| 769 | } |
| 770 | |
| 771 | return 0; |
| 772 | |
| 773 | err_mem: |
| 774 | printf("Out of memory processing configuration '%s/%s'\n", conf_name, |
| 775 | sig_name); |
| 776 | return -ENOMEM; |
| 777 | |
| 778 | err_path: |
| 779 | printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n", |
| 780 | iname, conf_name, sig_name, fdt_strerror(ret)); |
| 781 | return -ENOENT; |
| 782 | } |
| 783 | |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 784 | static int fit_config_get_hash_list(void *fit, int conf_noffset, |
| 785 | int sig_offset, struct strlist *node_inc) |
| 786 | { |
| 787 | int allow_missing; |
| 788 | const char *prop, *iname, *end; |
| 789 | const char *conf_name, *sig_name; |
Philippe Reynes | 856bcc8 | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 790 | char name[200]; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 791 | int image_count; |
| 792 | int ret, len; |
| 793 | |
| 794 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
| 795 | sig_name = fit_get_name(fit, sig_offset, NULL); |
| 796 | |
| 797 | /* |
| 798 | * Build a list of nodes we need to hash. We always need the root |
| 799 | * node and the configuration. |
| 800 | */ |
| 801 | strlist_init(node_inc); |
| 802 | snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name); |
| 803 | if (strlist_add(node_inc, "/") || |
| 804 | strlist_add(node_inc, name)) |
| 805 | goto err_mem; |
| 806 | |
| 807 | /* Get a list of images that we intend to sign */ |
Heiko Schocher | fdfd520 | 2014-03-03 12:19:23 +0100 | [diff] [blame] | 808 | prop = fit_config_get_image_list(fit, sig_offset, &len, |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 809 | &allow_missing); |
| 810 | if (!prop) |
| 811 | return 0; |
| 812 | |
| 813 | /* Locate the images */ |
| 814 | end = prop + len; |
| 815 | image_count = 0; |
| 816 | for (iname = prop; iname < end; iname += strlen(iname) + 1) { |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 817 | int image_noffset; |
Philippe Reynes | 1f7d9a2 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 818 | int index, max_index; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 819 | |
Philippe Reynes | 1f7d9a2 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 820 | max_index = fdt_stringlist_count(fit, conf_noffset, iname); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 821 | |
Philippe Reynes | 1f7d9a2 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 822 | for (index = 0; index < max_index; index++) { |
| 823 | image_noffset = fit_conf_get_prop_node_index(fit, conf_noffset, |
| 824 | iname, index); |
| 825 | |
| 826 | if (image_noffset < 0) { |
| 827 | printf("Failed to find image '%s' in configuration '%s/%s'\n", |
| 828 | iname, conf_name, sig_name); |
| 829 | if (allow_missing) |
| 830 | continue; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 831 | |
Philippe Reynes | 1f7d9a2 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 832 | return -ENOENT; |
| 833 | } |
Patrick Oppenlander | 5964c3f | 2020-07-30 14:30:47 +1000 | [diff] [blame] | 834 | |
Philippe Reynes | 1f7d9a2 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 835 | ret = fit_config_add_hash(fit, conf_name, |
| 836 | sig_name, node_inc, |
| 837 | iname, image_noffset); |
| 838 | if (ret < 0) |
| 839 | return ret; |
| 840 | |
| 841 | image_count++; |
| 842 | } |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | if (!image_count) { |
| 846 | printf("Failed to find any images for configuration '%s/%s'\n", |
| 847 | conf_name, sig_name); |
| 848 | return -ENOMSG; |
| 849 | } |
| 850 | |
| 851 | return 0; |
| 852 | |
| 853 | err_mem: |
| 854 | printf("Out of memory processing configuration '%s/%s'\n", conf_name, |
| 855 | sig_name); |
| 856 | return -ENOMEM; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | static int fit_config_get_data(void *fit, int conf_noffset, int noffset, |
| 860 | struct image_region **regionp, int *region_countp, |
| 861 | char **region_propp, int *region_proplen) |
| 862 | { |
| 863 | char * const exc_prop[] = {"data"}; |
| 864 | struct strlist node_inc; |
| 865 | struct image_region *region; |
| 866 | struct fdt_region fdt_regions[100]; |
| 867 | const char *conf_name, *sig_name; |
| 868 | char path[200]; |
| 869 | int count, i; |
| 870 | char *region_prop; |
| 871 | int ret, len; |
| 872 | |
| 873 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
Masahiro Yamada | 2fc6b6f | 2017-10-19 19:16:21 +0900 | [diff] [blame] | 874 | sig_name = fit_get_name(fit, noffset, NULL); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 875 | debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name); |
| 876 | |
| 877 | /* Get a list of nodes we want to hash */ |
| 878 | ret = fit_config_get_hash_list(fit, conf_noffset, noffset, &node_inc); |
| 879 | if (ret) |
| 880 | return ret; |
| 881 | |
| 882 | /* Get a list of regions to hash */ |
| 883 | count = fdt_find_regions(fit, node_inc.strings, node_inc.count, |
| 884 | exc_prop, ARRAY_SIZE(exc_prop), |
| 885 | fdt_regions, ARRAY_SIZE(fdt_regions), |
| 886 | path, sizeof(path), 1); |
| 887 | if (count < 0) { |
| 888 | printf("Failed to hash configuration '%s/%s': %s\n", conf_name, |
| 889 | sig_name, fdt_strerror(ret)); |
| 890 | return -EIO; |
| 891 | } |
| 892 | if (count == 0) { |
| 893 | printf("No data to hash for configuration '%s/%s': %s\n", |
| 894 | conf_name, sig_name, fdt_strerror(ret)); |
| 895 | return -EINVAL; |
| 896 | } |
| 897 | |
| 898 | /* Build our list of data blocks */ |
| 899 | region = fit_region_make_list(fit, fdt_regions, count, NULL); |
| 900 | if (!region) { |
| 901 | printf("Out of memory hashing configuration '%s/%s'\n", |
| 902 | conf_name, sig_name); |
| 903 | return -ENOMEM; |
| 904 | } |
| 905 | |
| 906 | /* Create a list of all hashed properties */ |
| 907 | debug("Hash nodes:\n"); |
| 908 | for (i = len = 0; i < node_inc.count; i++) { |
| 909 | debug(" %s\n", node_inc.strings[i]); |
| 910 | len += strlen(node_inc.strings[i]) + 1; |
| 911 | } |
| 912 | region_prop = malloc(len); |
| 913 | if (!region_prop) { |
| 914 | printf("Out of memory setting up regions for configuration '%s/%s'\n", |
| 915 | conf_name, sig_name); |
| 916 | return -ENOMEM; |
| 917 | } |
| 918 | for (i = len = 0; i < node_inc.count; |
| 919 | len += strlen(node_inc.strings[i]) + 1, i++) |
| 920 | strcpy(region_prop + len, node_inc.strings[i]); |
| 921 | strlist_free(&node_inc); |
| 922 | |
| 923 | *region_countp = count; |
| 924 | *regionp = region; |
| 925 | *region_propp = region_prop; |
| 926 | *region_proplen = len; |
| 927 | |
| 928 | return 0; |
| 929 | } |
| 930 | |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 931 | static int fit_config_process_sig(const char *keydir, const char *keyfile, |
| 932 | void *keydest, void *fit, const char *conf_name, |
| 933 | int conf_noffset, int noffset, const char *comment, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 934 | int require_keys, const char *engine_id, const char *cmdname, |
| 935 | const char *algo_name) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 936 | { |
| 937 | struct image_sign_info info; |
| 938 | const char *node_name; |
| 939 | struct image_region *region; |
| 940 | char *region_prop; |
| 941 | int region_proplen; |
| 942 | int region_count; |
| 943 | uint8_t *value; |
| 944 | uint value_len; |
| 945 | int ret; |
| 946 | |
| 947 | node_name = fit_get_name(fit, noffset, NULL); |
| 948 | if (fit_config_get_data(fit, conf_noffset, noffset, ®ion, |
| 949 | ®ion_count, ®ion_prop, ®ion_proplen)) |
| 950 | return -1; |
| 951 | |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 952 | if (fit_image_setup_sig(&info, keydir, keyfile, fit, conf_name, noffset, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 953 | require_keys ? "conf" : NULL, engine_id, |
| 954 | algo_name)) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 955 | return -1; |
| 956 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 957 | ret = info.crypto->sign(&info, region, region_count, &value, |
| 958 | &value_len); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 959 | free(region); |
| 960 | if (ret) { |
| 961 | printf("Failed to sign '%s' signature node in '%s' conf node\n", |
| 962 | node_name, conf_name); |
| 963 | |
| 964 | /* We allow keys to be missing */ |
| 965 | if (ret == -ENOENT) |
| 966 | return 0; |
| 967 | return -1; |
| 968 | } |
| 969 | |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 970 | ret = fit_image_write_sig(fit, noffset, value, value_len, comment, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 971 | region_prop, region_proplen, cmdname, |
| 972 | algo_name); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 973 | if (ret) { |
| 974 | if (ret == -FDT_ERR_NOSPACE) |
| 975 | return -ENOSPC; |
| 976 | printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n", |
| 977 | node_name, conf_name, fdt_strerror(ret)); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 978 | return -1; |
| 979 | } |
| 980 | free(value); |
| 981 | free(region_prop); |
| 982 | |
| 983 | /* Get keyname again, as FDT has changed and invalidated our pointer */ |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 984 | info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 985 | |
| 986 | /* Write the public key into the supplied FDT file */ |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 987 | if (keydest) { |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 988 | ret = info.crypto->add_verify_data(&info, keydest); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 989 | if (ret) { |
Masahiro Yamada | fe391a7 | 2017-10-27 15:04:21 +0900 | [diff] [blame] | 990 | printf("Failed to add verification data for '%s' signature node in '%s' configuration node\n", |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 991 | node_name, conf_name); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 992 | } |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 993 | return ret; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | return 0; |
| 997 | } |
| 998 | |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 999 | static int fit_config_add_verification_data(const char *keydir, |
| 1000 | const char *keyfile, void *keydest, void *fit, int conf_noffset, |
| 1001 | const char *comment, int require_keys, const char *engine_id, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1002 | const char *cmdname, const char *algo_name) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1003 | { |
| 1004 | const char *conf_name; |
| 1005 | int noffset; |
| 1006 | |
| 1007 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
| 1008 | |
| 1009 | /* Process all hash subnodes of the configuration node */ |
| 1010 | for (noffset = fdt_first_subnode(fit, conf_noffset); |
| 1011 | noffset >= 0; |
| 1012 | noffset = fdt_next_subnode(fit, noffset)) { |
| 1013 | const char *node_name; |
| 1014 | int ret = 0; |
| 1015 | |
| 1016 | node_name = fit_get_name(fit, noffset, NULL); |
| 1017 | if (!strncmp(node_name, FIT_SIG_NODENAME, |
| 1018 | strlen(FIT_SIG_NODENAME))) { |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1019 | ret = fit_config_process_sig(keydir, keyfile, keydest, |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1020 | fit, conf_name, conf_noffset, noffset, comment, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1021 | require_keys, engine_id, cmdname, algo_name); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1022 | } |
| 1023 | if (ret) |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 1030 | int fit_cipher_data(const char *keydir, void *keydest, void *fit, |
| 1031 | const char *comment, int require_keys, |
| 1032 | const char *engine_id, const char *cmdname) |
| 1033 | { |
| 1034 | int images_noffset; |
| 1035 | int noffset; |
| 1036 | int ret; |
| 1037 | |
| 1038 | /* Find images parent node offset */ |
| 1039 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 1040 | if (images_noffset < 0) { |
| 1041 | printf("Can't find images parent node '%s' (%s)\n", |
| 1042 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 1043 | return images_noffset; |
| 1044 | } |
| 1045 | |
| 1046 | /* Process its subnodes, print out component images details */ |
| 1047 | for (noffset = fdt_first_subnode(fit, images_noffset); |
| 1048 | noffset >= 0; |
| 1049 | noffset = fdt_next_subnode(fit, noffset)) { |
| 1050 | /* |
| 1051 | * Direct child node of the images parent node, |
| 1052 | * i.e. component image node. |
| 1053 | */ |
| 1054 | ret = fit_image_cipher_data(keydir, keydest, |
| 1055 | fit, noffset, comment, |
| 1056 | require_keys, engine_id, |
| 1057 | cmdname); |
| 1058 | if (ret) |
| 1059 | return ret; |
| 1060 | } |
| 1061 | |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1065 | int fit_add_verification_data(const char *keydir, const char *keyfile, |
| 1066 | void *keydest, void *fit, const char *comment, |
| 1067 | int require_keys, const char *engine_id, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1068 | const char *cmdname, const char *algo_name) |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 1069 | { |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1070 | int images_noffset, confs_noffset; |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 1071 | int noffset; |
| 1072 | int ret; |
| 1073 | |
| 1074 | /* Find images parent node offset */ |
| 1075 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 1076 | if (images_noffset < 0) { |
| 1077 | printf("Can't find images parent node '%s' (%s)\n", |
| 1078 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 1079 | return images_noffset; |
| 1080 | } |
| 1081 | |
| 1082 | /* Process its subnodes, print out component images details */ |
| 1083 | for (noffset = fdt_first_subnode(fit, images_noffset); |
| 1084 | noffset >= 0; |
| 1085 | noffset = fdt_next_subnode(fit, noffset)) { |
| 1086 | /* |
| 1087 | * Direct child node of the images parent node, |
| 1088 | * i.e. component image node. |
| 1089 | */ |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1090 | ret = fit_image_add_verification_data(keydir, keyfile, keydest, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 1091 | fit, noffset, comment, require_keys, engine_id, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1092 | cmdname, algo_name); |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 1093 | if (ret) |
| 1094 | return ret; |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1097 | /* If there are no keys, we can't sign configurations */ |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1098 | if (!IMAGE_ENABLE_SIGN || !(keydir || keyfile)) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1099 | return 0; |
| 1100 | |
| 1101 | /* Find configurations parent node offset */ |
| 1102 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); |
| 1103 | if (confs_noffset < 0) { |
| 1104 | printf("Can't find images parent node '%s' (%s)\n", |
Heiko Schocher | 441f48d | 2014-08-11 11:02:17 +0200 | [diff] [blame] | 1105 | FIT_CONFS_PATH, fdt_strerror(confs_noffset)); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1106 | return -ENOENT; |
| 1107 | } |
| 1108 | |
| 1109 | /* Process its subnodes, print out component images details */ |
| 1110 | for (noffset = fdt_first_subnode(fit, confs_noffset); |
| 1111 | noffset >= 0; |
| 1112 | noffset = fdt_next_subnode(fit, noffset)) { |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1113 | ret = fit_config_add_verification_data(keydir, keyfile, keydest, |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1114 | fit, noffset, comment, |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 1115 | require_keys, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1116 | engine_id, cmdname, |
| 1117 | algo_name); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1118 | if (ret) |
| 1119 | return ret; |
| 1120 | } |
| 1121 | |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 1122 | return 0; |
| 1123 | } |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1124 | |
| 1125 | #ifdef CONFIG_FIT_SIGNATURE |
Simon Glass | 0571232 | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 1126 | int fit_check_sign(const void *fit, const void *key, |
| 1127 | const char *fit_uname_config) |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1128 | { |
| 1129 | int cfg_noffset; |
| 1130 | int ret; |
| 1131 | |
Simon Glass | 0571232 | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 1132 | cfg_noffset = fit_conf_get_node(fit, fit_uname_config); |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1133 | if (!cfg_noffset) |
| 1134 | return -1; |
| 1135 | |
Simon Glass | a559bb2 | 2020-03-18 11:43:56 -0600 | [diff] [blame] | 1136 | printf("Verifying Hash Integrity for node '%s'... ", |
| 1137 | fdt_get_name(fit, cfg_noffset, NULL)); |
Simon Glass | a51991d | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1138 | ret = fit_config_verify(fit, cfg_noffset); |
| 1139 | if (ret) |
| 1140 | return ret; |
Simon Glass | 0571232 | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 1141 | printf("Verified OK, loading images\n"); |
Simon Glass | a51991d | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1142 | ret = bootm_host_load_images(fit, cfg_noffset); |
| 1143 | |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1144 | return ret; |
| 1145 | } |
| 1146 | #endif |