xref: /rk3399_rockchip-uboot/common/image-sig.c (revision 219050bf6ad6b4121755d77da9d0ba571be6c3b1)
1 /*
2  * Copyright (c) 2013, Google Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #ifdef USE_HOSTCC
8 #include "mkimage.h"
9 #include <time.h>
10 #else
11 #include <common.h>
12 #include <malloc.h>
13 #include <optee_include/OpteeClientInterface.h>
14 DECLARE_GLOBAL_DATA_PTR;
15 #endif /* !USE_HOSTCC*/
16 #include <image.h>
17 #include <u-boot/rsa.h>
18 #include <u-boot/rsa-checksum.h>
19 
20 #define IMAGE_MAX_HASHED_NODES		100
21 
22 #ifdef USE_HOSTCC
23 void *host_blob;
24 void image_set_host_blob(void *blob)
25 {
26 	host_blob = blob;
27 }
28 void *image_get_host_blob(void)
29 {
30 	return host_blob;
31 }
32 #endif
33 
34 struct checksum_algo checksum_algos[] = {
35 	{
36 		.name = "sha1",
37 		.checksum_len = SHA1_SUM_LEN,
38 		.der_len = SHA1_DER_LEN,
39 		.der_prefix = sha1_der_prefix,
40 #if IMAGE_ENABLE_SIGN
41 		.calculate_sign = EVP_sha1,
42 #endif
43 		.calculate = hash_calculate,
44 	},
45 	{
46 		.name = "sha256",
47 		.checksum_len = SHA256_SUM_LEN,
48 		.der_len = SHA256_DER_LEN,
49 		.der_prefix = sha256_der_prefix,
50 #if IMAGE_ENABLE_SIGN
51 		.calculate_sign = EVP_sha256,
52 #endif
53 		.calculate = hash_calculate,
54 	}
55 
56 };
57 
58 struct crypto_algo crypto_algos[] = {
59 	{
60 		.name = "rsa2048",
61 		.key_len = RSA2048_BYTES,
62 		.sign = rsa_sign,
63 		.add_verify_data = rsa_add_verify_data,
64 		.verify = rsa_verify,
65 	},
66 	{
67 		.name = "rsa4096",
68 		.key_len = RSA4096_BYTES,
69 		.sign = rsa_sign,
70 		.add_verify_data = rsa_add_verify_data,
71 		.verify = rsa_verify,
72 	}
73 
74 };
75 
76 struct padding_algo padding_algos[] = {
77 	{
78 		.name = "pkcs-1.5",
79 		.verify = padding_pkcs_15_verify,
80 	},
81 };
82 
83 struct checksum_algo *image_get_checksum_algo(const char *full_name)
84 {
85 	int i;
86 	const char *name;
87 
88 	for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
89 		name = checksum_algos[i].name;
90 		/* Make sure names match and next char is a comma */
91 		if (!strncmp(name, full_name, strlen(name)) &&
92 		    full_name[strlen(name)] == ',')
93 			return &checksum_algos[i];
94 	}
95 
96 	return NULL;
97 }
98 
99 struct crypto_algo *image_get_crypto_algo(const char *full_name)
100 {
101 	int i;
102 	const char *name;
103 
104 	/* Move name to after the comma */
105 	name = strchr(full_name, ',');
106 	if (!name)
107 		return NULL;
108 	name += 1;
109 
110 	for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
111 		if (!strcmp(crypto_algos[i].name, name))
112 			return &crypto_algos[i];
113 	}
114 
115 	return NULL;
116 }
117 
118 struct padding_algo *image_get_padding_algo(const char *name)
119 {
120 	int i;
121 
122 	if (!name)
123 		return NULL;
124 
125 	for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
126 		if (!strcmp(padding_algos[i].name, name))
127 			return &padding_algos[i];
128 	}
129 
130 	return NULL;
131 }
132 
133 /**
134  * fit_region_make_list() - Make a list of image regions
135  *
136  * Given a list of fdt_regions, create a list of image_regions. This is a
137  * simple conversion routine since the FDT and image code use different
138  * structures.
139  *
140  * @fit: FIT image
141  * @fdt_regions: Pointer to FDT regions
142  * @count: Number of FDT regions
143  * @region: Pointer to image regions, which must hold @count records. If
144  * region is NULL, then (except for an SPL build) the array will be
145  * allocated.
146  * @return: Pointer to image regions
147  */
148 struct image_region *fit_region_make_list(const void *fit,
149 		struct fdt_region *fdt_regions, int count,
150 		struct image_region *region)
151 {
152 	int i;
153 
154 	debug("Hash regions:\n");
155 	debug("%10s %10s\n", "Offset", "Size");
156 
157 	/*
158 	 * Use malloc() except in SPL (to save code size). In SPL the caller
159 	 * must allocate the array.
160 	 */
161 #ifndef CONFIG_SPL_BUILD
162 	if (!region)
163 		region = calloc(sizeof(*region), count);
164 #endif
165 	if (!region)
166 		return NULL;
167 	for (i = 0; i < count; i++) {
168 		debug("%10x %10x\n", fdt_regions[i].offset,
169 		      fdt_regions[i].size);
170 		region[i].data = fit + fdt_regions[i].offset;
171 		region[i].size = fdt_regions[i].size;
172 	}
173 
174 	return region;
175 }
176 
177 static int fit_image_setup_verify(struct image_sign_info *info,
178 		const void *fit, int noffset, int required_keynode,
179 		char **err_msgp)
180 {
181 	char *algo_name;
182 	const char *padding_name;
183 
184 	if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
185 		*err_msgp = "Can't get hash algo property";
186 		return -1;
187 	}
188 
189 	padding_name = fdt_getprop(fit, noffset, "padding", NULL);
190 	if (!padding_name)
191 		padding_name = RSA_DEFAULT_PADDING_NAME;
192 
193 	memset(info, '\0', sizeof(*info));
194 	info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
195 	info->fit = (void *)fit;
196 	info->node_offset = noffset;
197 	info->name = algo_name;
198 	info->checksum = image_get_checksum_algo(algo_name);
199 	info->crypto = image_get_crypto_algo(algo_name);
200 	info->padding = image_get_padding_algo(padding_name);
201 	info->fdt_blob = gd_fdt_blob();
202 	info->required_keynode = required_keynode;
203 	printf("%s:%s", algo_name, info->keyname);
204 
205 	if (!info->checksum || !info->crypto) {
206 		*err_msgp = "Unknown signature algorithm";
207 		return -1;
208 	}
209 
210 	return 0;
211 }
212 
213 int fit_image_check_sig(const void *fit, int noffset, const void *data,
214 		size_t size, int required_keynode, char **err_msgp)
215 {
216 	struct image_sign_info info;
217 	struct image_region region;
218 	uint8_t *fit_value;
219 	int fit_value_len;
220 
221 	*err_msgp = NULL;
222 	if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
223 				   err_msgp))
224 		return -1;
225 
226 	if (fit_image_hash_get_value(fit, noffset, &fit_value,
227 				     &fit_value_len)) {
228 		*err_msgp = "Can't get hash value property";
229 		return -1;
230 	}
231 
232 	region.data = data;
233 	region.size = size;
234 
235 	if (info.crypto->verify(&info, &region, 1, fit_value, fit_value_len)) {
236 		*err_msgp = "Verification failed";
237 		return -1;
238 	}
239 
240 	return 0;
241 }
242 
243 static int fit_image_verify_sig(const void *fit, int image_noffset,
244 		const char *data, size_t size, const void *sig_blob,
245 		int sig_offset)
246 {
247 	int noffset;
248 	char *err_msg = "";
249 	int verified = 0;
250 	int ret;
251 
252 	/* Process all hash subnodes of the component image node */
253 	fdt_for_each_subnode(noffset, fit, image_noffset) {
254 		const char *name = fit_get_name(fit, noffset, NULL);
255 
256 		if (!strncmp(name, FIT_SIG_NODENAME,
257 			     strlen(FIT_SIG_NODENAME))) {
258 			ret = fit_image_check_sig(fit, noffset, data,
259 							size, -1, &err_msg);
260 			if (ret) {
261 				puts("- ");
262 			} else {
263 				puts("+ ");
264 				verified = 1;
265 				break;
266 			}
267 		}
268 	}
269 
270 	if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
271 		err_msg = "Corrupted or truncated tree";
272 		goto error;
273 	}
274 
275 	if (verified)
276 		return 0;
277 
278 error:
279 	printf(" error!\n%s for '%s' hash node in '%s' image node\n",
280 	       err_msg, fit_get_name(fit, noffset, NULL),
281 	       fit_get_name(fit, image_noffset, NULL));
282 	return -1;
283 }
284 
285 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
286 		const char *data, size_t size, const void *sig_blob,
287 		int *no_sigsp)
288 {
289 	int verify_count = 0;
290 	int noffset;
291 	int sig_node;
292 
293 	/* Work out what we need to verify */
294 	*no_sigsp = 1;
295 	sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
296 	if (sig_node < 0) {
297 		printf("No RSA key found\n");
298 		return -EINVAL;
299 	}
300 
301 	fdt_for_each_subnode(noffset, sig_blob, sig_node) {
302 		const char *required;
303 		int ret;
304 
305 		required = fdt_getprop(sig_blob, noffset, "required", NULL);
306 		if (!required || strcmp(required, "image"))
307 			continue;
308 		ret = fit_image_verify_sig(fit, image_noffset, data, size,
309 					sig_blob, noffset);
310 		if (ret) {
311 			printf("Failed to verify required signature '%s'\n",
312 			       fit_get_name(sig_blob, noffset, NULL));
313 			return ret;
314 		}
315 		verify_count++;
316 	}
317 
318 	if (verify_count)
319 		*no_sigsp = 0;
320 
321 	return 0;
322 }
323 
324 int fit_config_check_sig(const void *fit, int noffset, int required_keynode,
325 			 char **err_msgp)
326 {
327 	char * const exc_prop[] = {"data"};
328 	const char *prop, *end, *name;
329 	struct image_sign_info info;
330 	const uint32_t *strings;
331 	uint8_t *fit_value;
332 	int fit_value_len;
333 	int max_regions;
334 	int i, prop_len;
335 	char path[200];
336 	int count;
337 
338 	debug("%s: fdt=%p, conf='%s', sig='%s'\n", __func__, gd_fdt_blob(),
339 	      fit_get_name(fit, noffset, NULL),
340 	      fit_get_name(gd_fdt_blob(), required_keynode, NULL));
341 	*err_msgp = NULL;
342 	if (fit_image_setup_verify(&info, fit, noffset, required_keynode,
343 				   err_msgp))
344 		return -1;
345 
346 	if (fit_image_hash_get_value(fit, noffset, &fit_value,
347 				     &fit_value_len)) {
348 		*err_msgp = "Can't get hash value property";
349 		return -1;
350 	}
351 
352 	/* Count the number of strings in the property */
353 	prop = fdt_getprop(fit, noffset, "hashed-nodes", &prop_len);
354 	end = prop ? prop + prop_len : prop;
355 	for (name = prop, count = 0; name < end; name++)
356 		if (!*name)
357 			count++;
358 	if (!count) {
359 		*err_msgp = "Can't get hashed-nodes property";
360 		return -1;
361 	}
362 
363 	/* Add a sanity check here since we are using the stack */
364 	if (count > IMAGE_MAX_HASHED_NODES) {
365 		*err_msgp = "Number of hashed nodes exceeds maximum";
366 		return -1;
367 	}
368 
369 	/* Create a list of node names from those strings */
370 	char *node_inc[count];
371 
372 	debug("Hash nodes (%d):\n", count);
373 	for (name = prop, i = 0; name < end; name += strlen(name) + 1, i++) {
374 		debug("   '%s'\n", name);
375 		node_inc[i] = (char *)name;
376 	}
377 
378 	/*
379 	 * Each node can generate one region for each sub-node. Allow for
380 	 * 7 sub-nodes (hash@1, signature@1, etc.) and some extra.
381 	 */
382 	max_regions = 20 + count * 7;
383 	struct fdt_region fdt_regions[max_regions];
384 
385 	/* Get a list of regions to hash */
386 	count = fdt_find_regions(fit, node_inc, count,
387 			exc_prop, ARRAY_SIZE(exc_prop),
388 			fdt_regions, max_regions - 1,
389 			path, sizeof(path), 0);
390 	if (count < 0) {
391 		*err_msgp = "Failed to hash configuration";
392 		return -1;
393 	}
394 	if (count == 0) {
395 		*err_msgp = "No data to hash";
396 		return -1;
397 	}
398 	if (count >= max_regions - 1) {
399 		*err_msgp = "Too many hash regions";
400 		return -1;
401 	}
402 
403 	/* Add the strings */
404 	strings = fdt_getprop(fit, noffset, "hashed-strings", NULL);
405 	if (strings) {
406 		fdt_regions[count].offset = fdt_off_dt_strings(fit) +
407 				fdt32_to_cpu(strings[0]);
408 		fdt_regions[count].size = fdt32_to_cpu(strings[1]);
409 		count++;
410 	}
411 
412 	/* Allocate the region list on the stack */
413 	struct image_region region[count];
414 
415 	fit_region_make_list(fit, fdt_regions, count, region);
416 	if (info.crypto->verify(&info, region, count, fit_value,
417 				fit_value_len)) {
418 		*err_msgp = "Verification failed";
419 		return -1;
420 	}
421 
422 	return 0;
423 }
424 
425 static int fit_config_verify_sig(const void *fit, int conf_noffset,
426 		const void *sig_blob, int sig_offset)
427 {
428 	int noffset;
429 	char *err_msg = "";
430 	int verified = 0;
431 	int ret;
432 
433 	/* Process all hash subnodes of the component conf node */
434 	fdt_for_each_subnode(noffset, fit, conf_noffset) {
435 		const char *name = fit_get_name(fit, noffset, NULL);
436 
437 		if (!strncmp(name, FIT_SIG_NODENAME,
438 			     strlen(FIT_SIG_NODENAME))) {
439 			ret = fit_config_check_sig(fit, noffset, sig_offset,
440 						   &err_msg);
441 			if (ret) {
442 				puts("- ");
443 			} else {
444 				puts("+ ");
445 				verified = 1;
446 				break;
447 			}
448 		}
449 	}
450 
451 	if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
452 		err_msg = "Corrupted or truncated tree";
453 		goto error;
454 	}
455 
456 	if (verified)
457 		return 0;
458 
459 error:
460 	printf(" error!\n%s for '%s' hash node in '%s' config node\n",
461 	       err_msg, fit_get_name(fit, noffset, NULL),
462 	       fit_get_name(fit, conf_noffset, NULL));
463 	return -1;
464 }
465 
466 int fit_config_verify_required_sigs(const void *fit, int conf_noffset,
467 		const void *sig_blob)
468 {
469 	int noffset;
470 	int sig_node;
471 
472 	/* Work out what we need to verify */
473 	sig_node = fdt_subnode_offset(sig_blob, 0, FIT_SIG_NODENAME);
474 	if (sig_node < 0) {
475 		printf("No RSA key found\n");
476 		return -EINVAL;
477 	}
478 
479 	fdt_for_each_subnode(noffset, sig_blob, sig_node) {
480 		const char *required;
481 		int ret;
482 
483 		required = fdt_getprop(sig_blob, noffset, "required", NULL);
484 		if (!required || strcmp(required, "conf"))
485 			continue;
486 		ret = fit_config_verify_sig(fit, conf_noffset, sig_blob,
487 					    noffset);
488 		if (ret) {
489 			printf("Failed to verify required signature '%s'\n",
490 			       fit_get_name(sig_blob, noffset, NULL));
491 			return ret;
492 		}
493 	}
494 
495 	return 0;
496 }
497 
498 int fit_config_verify(const void *fit, int conf_noffset)
499 {
500 	return fit_config_verify_required_sigs(fit, conf_noffset,
501 					       gd_fdt_blob());
502 }
503 
504 #ifndef USE_HOSTCC
505 #if CONFIG_IS_ENABLED(FIT_ROLLBACK_PROTECT)
506 __weak int fit_read_otp_rollback_index(uint32_t fit_index, uint32_t *otp_index)
507 {
508 	*otp_index = 0;
509 
510 	return 0;
511 }
512 __weak int fit_rollback_index_verify(const void *fit, uint32_t rollback_fd,
513 				     uint32_t *this_index, uint32_t *min_index)
514 {
515 	*this_index = 0;
516 	*min_index = 0;
517 
518 	return 0;
519 }
520 #endif
521 #endif
522