xref: /rk3399_ARM-atf/drivers/auth/auth_mod.c (revision f05b4894acfb681017bfda71fb16e6079a4f6a43)
1 /*
2  * Copyright (c) 2015-2025, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <stdint.h>
9 #include <string.h>
10 
11 #include <platform_def.h>
12 
13 #include <common/debug.h>
14 #include <common/tbbr/cot_def.h>
15 #include <drivers/auth/auth_common.h>
16 #include <drivers/auth/auth_mod.h>
17 #include <drivers/auth/auth_util.h>
18 #include <drivers/auth/crypto_mod.h>
19 #include <drivers/auth/img_parser_mod.h>
20 #include <drivers/fwu/fwu.h>
21 #include <lib/fconf/fconf_tbbr_getter.h>
22 #include <plat/common/platform.h>
23 
24 #include <tools_share/zero_oid.h>
25 
26 /* ASN.1 tags */
27 #define ASN1_INTEGER                 0x02
28 
29 #pragma weak plat_set_nv_ctr2
30 
31 static int cmp_auth_param_type_desc(const auth_param_type_desc_t *a,
32 		const auth_param_type_desc_t *b)
33 {
34 	if ((a->type == b->type) && (a->cookie == b->cookie)) {
35 		return 0;
36 	}
37 	return 1;
38 }
39 
40 /*
41  * This function obtains the requested authentication parameter data from the
42  * information extracted from the parent image after its authentication.
43  */
44 static int auth_get_param(const auth_param_type_desc_t *param_type_desc,
45 			  const auth_img_desc_t *img_desc,
46 			  void **param, unsigned int *len)
47 {
48 	int i;
49 
50 	if (img_desc->authenticated_data == NULL)
51 		return 1;
52 
53 	for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
54 		if (0 == cmp_auth_param_type_desc(param_type_desc,
55 				img_desc->authenticated_data[i].type_desc)) {
56 			*param = img_desc->authenticated_data[i].data.ptr;
57 			*len = img_desc->authenticated_data[i].data.len;
58 			return 0;
59 		}
60 	}
61 
62 	return 1;
63 }
64 
65 /*
66  * Authenticate an image by matching the data hash
67  *
68  * This function implements 'AUTH_METHOD_HASH'. To authenticate an image using
69  * this method, the image must contain:
70  *
71  *   - The data to calculate the hash from
72  *
73  * The parent image must contain:
74  *
75  *   - The hash to be matched with (including hash algorithm)
76  *
77  * For a successful authentication, both hashes must match. The function calls
78  * the crypto-module to check this matching.
79  *
80  * Parameters:
81  *   param: parameters to perform the hash authentication
82  *   img_desc: pointer to image descriptor so we can know the image type
83  *             and parent image
84  *   img: pointer to image in memory
85  *   img_len: length of image (in bytes)
86  *
87  * Return:
88  *   0 = success, Otherwise = error
89  */
90 static int auth_hash(const auth_method_param_hash_t *param,
91 		     const auth_img_desc_t *img_desc,
92 		     void *img, unsigned int img_len)
93 {
94 	void *data_ptr, *hash_der_ptr;
95 	unsigned int data_len, hash_der_len;
96 	int rc;
97 
98 	/* Get the hash from the parent image. This hash will be DER encoded
99 	 * and contain the hash algorithm */
100 	rc = auth_get_param(param->hash, img_desc->parent,
101 			&hash_der_ptr, &hash_der_len);
102 	if (rc != 0) {
103 		VERBOSE("[TBB] %s():%d failed with error code %d.\n",
104 			__func__, __LINE__, rc);
105 		return rc;
106 	}
107 
108 	/* Get the data to be hashed from the current image */
109 	rc = img_parser_get_auth_param(img_desc->img_type, param->data,
110 			img, img_len, &data_ptr, &data_len);
111 	if (rc != 0) {
112 		VERBOSE("[TBB] %s():%d failed with error code %d.\n",
113 			__func__, __LINE__, rc);
114 		return rc;
115 	}
116 
117 	/* Ask the crypto module to verify this hash */
118 	rc = crypto_mod_verify_hash(data_ptr, data_len,
119 				    hash_der_ptr, hash_der_len);
120 	if (rc != 0) {
121 		VERBOSE("[TBB] %s():%d failed with error code %d.\n",
122 			__func__, __LINE__, rc);
123 		return rc;
124 	}
125 
126 	return 0;
127 }
128 
129 /*
130  * Authenticate by digital signature
131  *
132  * This function implements 'AUTH_METHOD_SIG'. To authenticate an image using
133  * this method, the image must contain:
134  *
135  *   - Data to be signed
136  *   - Signature
137  *   - Signature algorithm
138  *
139  * We rely on the image parser module to extract this data from the image.
140  * The parent image must contain:
141  *
142  *   - Public key (or a hash of it)
143  *
144  * If the parent image contains only a hash of the key, we will try to obtain
145  * the public key from the image itself (i.e. self-signed certificates). In that
146  * case, the signature verification is considered just an integrity check and
147  * the authentication is established by calculating the hash of the key and
148  * comparing it with the hash obtained from the parent.
149  *
150  * If the image has no parent (NULL), it means it has to be authenticated using
151  * the ROTPK stored in the platform. Again, this ROTPK could be the key itself
152  * or a hash of it.
153  *
154  * Return: 0 = success, Otherwise = error
155  */
156 static int auth_signature(const auth_method_param_sig_t *param,
157 			  const auth_img_desc_t *img_desc,
158 			  void *img, unsigned int img_len)
159 {
160 	void *data_ptr, *pk_ptr, *cnv_pk_ptr, *pk_plat_ptr, *sig_ptr, *sig_alg_ptr, *pk_oid;
161 	unsigned int data_len, pk_len, cnv_pk_len, pk_plat_len, sig_len, sig_alg_len;
162 	unsigned int flags = 0;
163 	int rc;
164 
165 	/* Get the data to be signed from current image */
166 	rc = img_parser_get_auth_param(img_desc->img_type, param->data,
167 			img, img_len, &data_ptr, &data_len);
168 	if (rc != 0) {
169 		VERBOSE("[TBB] %s():%d failed with error code %d.\n",
170 			__func__, __LINE__, rc);
171 		return rc;
172 	}
173 
174 	/* Get the signature from current image */
175 	rc = img_parser_get_auth_param(img_desc->img_type, param->sig,
176 			img, img_len, &sig_ptr, &sig_len);
177 	if (rc != 0) {
178 		VERBOSE("[TBB] %s():%d failed with error code %d.\n",
179 			__func__, __LINE__, rc);
180 		return rc;
181 	}
182 
183 	/* Get the signature algorithm from current image */
184 	rc = img_parser_get_auth_param(img_desc->img_type, param->alg,
185 			img, img_len, &sig_alg_ptr, &sig_alg_len);
186 	if (rc != 0) {
187 		VERBOSE("[TBB] %s():%d failed with error code %d.\n",
188 			__func__, __LINE__, rc);
189 		return rc;
190 	}
191 
192 	/*
193 	 * Set Zero-OID for ROTPK(subject key) as a the certificate
194 	 * does not hold Key-OID information for ROTPK.
195 	 */
196 	if (param->pk->cookie != NULL) {
197 		pk_oid = param->pk->cookie;
198 	} else {
199 		pk_oid = ZERO_OID;
200 	}
201 
202 	set_current_pk_oid(pk_oid);
203 
204 	/* Get the public key from the parent. If there is no parent (NULL),
205 	 * the certificate has been signed with the ROTPK, so we have to get
206 	 * the PK from the platform */
207 	if (img_desc->parent != NULL) {
208 		rc = auth_get_param(param->pk, img_desc->parent,
209 				&pk_ptr, &pk_len);
210 		if (rc != 0) {
211 			VERBOSE("[TBB] %s():%d failed with error code %d.\n",
212 				__func__, __LINE__, rc);
213 			return rc;
214 		}
215 	} else {
216 		/*
217 		 * Root certificates are signed with the ROTPK, so we have to
218 		 * get it from the platform.
219 		 */
220 		rc = plat_get_rotpk_info(param->pk->cookie, &pk_plat_ptr,
221 					 &pk_plat_len, &flags);
222 		if (rc != 0) {
223 			VERBOSE("[TBB] %s():%d failed with error code %d.\n",
224 				__func__, __LINE__, rc);
225 			return rc;
226 		}
227 
228 		assert(is_rotpk_flags_valid(flags));
229 
230 		/* Also retrieve the key from the image. */
231 		rc = img_parser_get_auth_param(img_desc->img_type,
232 					       param->pk, img, img_len,
233 					       &pk_ptr, &pk_len);
234 		if (rc != 0) {
235 			VERBOSE("[TBB] %s():%d failed with error code %d.\n",
236 				__func__, __LINE__, rc);
237 			return rc;
238 		}
239 
240 		/*
241 		 * Validate the certificate's key against the platform ROTPK.
242 		 *
243 		 * Platform may store key in one of the following way -
244 		 * 1. Hash of ROTPK
245 		 * 2. Hash if prefixed, suffixed or modified ROTPK
246 		 * 3. Full ROTPK
247 		 */
248 		if ((flags & ROTPK_NOT_DEPLOYED) != 0U) {
249 			NOTICE("ROTPK is not deployed on platform. "
250 				"Skipping ROTPK verification.\n");
251 		} else if ((flags & ROTPK_IS_HASH) != 0U) {
252 			/*
253 			 * platform may store the hash of a prefixed,
254 			 * suffixed or modified pk
255 			 */
256 			rc = crypto_mod_convert_pk(pk_ptr, pk_len, &cnv_pk_ptr, &cnv_pk_len);
257 			if (rc != 0) {
258 				VERBOSE("[TBB] %s():%d failed with error code %d.\n",
259 					__func__, __LINE__, rc);
260 				return rc;
261 			}
262 
263 			/*
264 			 * The hash of the certificate's public key must match
265 			 * the hash of the ROTPK.
266 			 */
267 			rc = crypto_mod_verify_hash(cnv_pk_ptr, cnv_pk_len,
268 						    pk_plat_ptr, pk_plat_len);
269 			if (rc != 0) {
270 				VERBOSE("[TBB] %s():%d failed with error code %d.\n",
271 					__func__, __LINE__, rc);
272 				return rc;
273 			}
274 		} else {
275 			/* Platform supports full ROTPK */
276 			if ((pk_len != pk_plat_len) ||
277 			    (memcmp(pk_plat_ptr, pk_ptr, pk_len) != 0)) {
278 				ERROR("plat and cert ROTPK len mismatch\n");
279 				return -1;
280 			}
281 		}
282 
283 		/*
284 		 * Public key is verified at this stage, notify platform
285 		 * to measure and publish it.
286 		 */
287 		rc = plat_mboot_measure_key(pk_oid, pk_ptr, pk_len);
288 		if (rc != 0) {
289 			VERBOSE("[TBB] %s():%d failed with error code %d.\n",
290 				__func__, __LINE__, rc);
291 		}
292 	}
293 
294 	/* Ask the crypto module to verify the signature */
295 	rc = crypto_mod_verify_signature(data_ptr, data_len,
296 					 sig_ptr, sig_len,
297 					 sig_alg_ptr, sig_alg_len,
298 					 pk_ptr, pk_len);
299 	if (rc != 0) {
300 		VERBOSE("[TBB] %s():%d failed with error code %d.\n",
301 			__func__, __LINE__, rc);
302 		return rc;
303 	}
304 
305 	return 0;
306 }
307 
308 /*
309  * Authenticate by Non-Volatile counter
310  *
311  * To protect the system against rollback, the platform includes a non-volatile
312  * counter whose value can only be increased. All certificates include a counter
313  * value that should not be lower than the value stored in the platform. If the
314  * value is larger, the counter in the platform must be updated to the new value
315  * (provided it has been authenticated).
316  *
317  * Return: 0 = success, Otherwise = error
318  * Returns additionally,
319  * cert_nv_ctr -> NV counter value present in the certificate
320  * need_nv_ctr_upgrade = 0 -> platform NV counter upgrade is not needed
321  * need_nv_ctr_upgrade = 1 -> platform NV counter upgrade is needed
322  */
323 static int auth_nvctr(const auth_method_param_nv_ctr_t *param,
324 		      const auth_img_desc_t *img_desc,
325 		      void *img, unsigned int img_len,
326 		      unsigned int *cert_nv_ctr,
327 		      bool *need_nv_ctr_upgrade)
328 {
329 	unsigned char *p;
330 	void *data_ptr = NULL;
331 	unsigned int data_len, len, i;
332 	unsigned int plat_nv_ctr;
333 	int rc;
334 
335 	/* Get the counter value from current image. The AM expects the IPM
336 	 * to return the counter value as a DER encoded integer */
337 	rc = img_parser_get_auth_param(img_desc->img_type, param->cert_nv_ctr,
338 				       img, img_len, &data_ptr, &data_len);
339 	if (rc != 0) {
340 		VERBOSE("[TBB] %s():%d failed with error code %d.\n",
341 			__func__, __LINE__, rc);
342 		return rc;
343 	}
344 
345 	/* Parse the DER encoded integer */
346 	assert(data_ptr);
347 	p = (unsigned char *)data_ptr;
348 
349 	/*
350 	 * Integers must be at least 3 bytes: 1 for tag, 1 for length, and 1
351 	 * for value.  The first byte (tag) must be ASN1_INTEGER.
352 	 */
353 	if ((data_len < 3) || (*p != ASN1_INTEGER)) {
354 		/* Invalid ASN.1 integer */
355 		return 1;
356 	}
357 	p++;
358 
359 	/*
360 	 * NV-counters are unsigned integers up to 31 bits.  Trailing
361 	 * padding is not allowed.
362 	 */
363 	len = (unsigned int)*p;
364 	if ((len > 4) || (data_len - 2 != len)) {
365 		return 1;
366 	}
367 	p++;
368 
369 	/* Check the number is not negative */
370 	if (*p & 0x80) {
371 		return 1;
372 	}
373 
374 	/* Convert to unsigned int. This code is for a little-endian CPU */
375 	*cert_nv_ctr = 0;
376 	for (i = 0; i < len; i++) {
377 		*cert_nv_ctr = (*cert_nv_ctr << 8) | *p++;
378 	}
379 
380 	/* Get the counter from the platform */
381 	rc = plat_get_nv_ctr(param->plat_nv_ctr->cookie, &plat_nv_ctr);
382 	if (rc != 0) {
383 		VERBOSE("[TBB] %s():%d failed with error code %d.\n",
384 			__func__, __LINE__, rc);
385 		return rc;
386 	}
387 
388 	if (*cert_nv_ctr < plat_nv_ctr) {
389 		/* Invalid NV-counter */
390 		return 1;
391 	} else if (*cert_nv_ctr > plat_nv_ctr) {
392 #if PSA_FWU_SUPPORT && IMAGE_BL2
393 		if (fwu_get_active_bank_state() == FWU_BANK_STATE_ACCEPTED) {
394 			*need_nv_ctr_upgrade = true;
395 		} else {
396 			*need_nv_ctr_upgrade = false;
397 		}
398 #else
399 		*need_nv_ctr_upgrade = true;
400 #endif /* PSA_FWU_SUPPORT && IMAGE_BL2 */
401 	}
402 
403 	return 0;
404 }
405 
406 int plat_set_nv_ctr2(void *cookie, const auth_img_desc_t *img_desc __unused,
407 		unsigned int nv_ctr)
408 {
409 	return plat_set_nv_ctr(cookie, nv_ctr);
410 }
411 
412 /*
413  * Return the parent id in the output parameter '*parent_id'
414  *
415  * Return value:
416  *   0 = Image has parent, 1 = Image has no parent or parent is authenticated
417  */
418 int auth_mod_get_parent_id(unsigned int img_id, unsigned int *parent_id)
419 {
420 	const auth_img_desc_t *img_desc = NULL;
421 
422 	assert(parent_id != NULL);
423 	/* Get the image descriptor */
424 	img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
425 
426 	/* Check if the image has no parent (ROT) */
427 	if (img_desc->parent == NULL) {
428 		*parent_id = 0;
429 		return 1;
430 	}
431 
432 	/* Check if the parent has already been authenticated */
433 	if (auth_img_flags[img_desc->parent->img_id] & IMG_FLAG_AUTHENTICATED) {
434 		*parent_id = 0;
435 		return 1;
436 	}
437 
438 	*parent_id = img_desc->parent->img_id;
439 	return 0;
440 }
441 
442 /*
443  * Initialize the different modules in the authentication framework
444  */
445 void auth_mod_init(void)
446 {
447 	/* Check we have a valid CoT registered */
448 	assert(cot_desc_ptr != NULL);
449 
450 	/* Image parser module */
451 	img_parser_init();
452 }
453 
454 /*
455  * Authenticate a certificate/image
456  *
457  * Return: 0 = success, Otherwise = error
458  */
459 int auth_mod_verify_img(unsigned int img_id,
460 			void *img_ptr,
461 			unsigned int img_len)
462 {
463 	const auth_img_desc_t *img_desc = NULL;
464 	const auth_param_type_desc_t *type_desc = NULL;
465 	const auth_method_desc_t *auth_method = NULL;
466 	void *param_ptr;
467 	unsigned int param_len;
468 	int rc, i;
469 	unsigned int cert_nv_ctr = 0;
470 	bool need_nv_ctr_upgrade = false;
471 	bool sig_auth_done = false;
472 	const auth_method_param_nv_ctr_t *nv_ctr_param = NULL;
473 
474 	/* Get the image descriptor from the chain of trust */
475 	img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
476 
477 	/* Ask the parser to check the image integrity */
478 	rc = img_parser_check_integrity(img_desc->img_type, img_ptr, img_len);
479 	if (rc != 0) {
480 		VERBOSE("[TBB] %s():%d failed with error code %d.\n",
481 			__func__, __LINE__, rc);
482 		return rc;
483 	}
484 
485 	/* Authenticate the image using the methods indicated in the image
486 	 * descriptor. */
487 	if (img_desc->img_auth_methods == NULL)
488 		return 1;
489 	for (i = 0 ; i < AUTH_METHOD_NUM ; i++) {
490 		auth_method = &img_desc->img_auth_methods[i];
491 		switch (auth_method->type) {
492 		case AUTH_METHOD_NONE:
493 			rc = 0;
494 			break;
495 		case AUTH_METHOD_HASH:
496 			rc = auth_hash(&auth_method->param.hash,
497 					img_desc, img_ptr, img_len);
498 			break;
499 		case AUTH_METHOD_SIG:
500 			rc = auth_signature(&auth_method->param.sig,
501 					img_desc, img_ptr, img_len);
502 			sig_auth_done = true;
503 			break;
504 		case AUTH_METHOD_NV_CTR:
505 			nv_ctr_param = &auth_method->param.nv_ctr;
506 			rc = auth_nvctr(nv_ctr_param,
507 					img_desc, img_ptr, img_len,
508 					&cert_nv_ctr, &need_nv_ctr_upgrade);
509 			break;
510 		default:
511 			/* Unknown authentication method */
512 			rc = 1;
513 			break;
514 		}
515 		if (rc != 0) {
516 			VERBOSE("[TBB] %s():%d failed with error code %d.\n",
517 				__func__, __LINE__, rc);
518 			return rc;
519 		}
520 	}
521 
522 	/*
523 	 * Do platform NV counter upgrade only if the certificate gets
524 	 * authenticated, and platform NV-counter upgrade is needed.
525 	 */
526 	if (need_nv_ctr_upgrade && sig_auth_done) {
527 		rc = plat_set_nv_ctr2(nv_ctr_param->plat_nv_ctr->cookie,
528 				      img_desc, cert_nv_ctr);
529 		if (rc != 0) {
530 			VERBOSE("[TBB] %s():%d failed with error code %d.\n",
531 				__func__, __LINE__, rc);
532 			return rc;
533 		}
534 	}
535 
536 	/* Extract the parameters indicated in the image descriptor to
537 	 * authenticate the children images. */
538 	if (img_desc->authenticated_data != NULL) {
539 		for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
540 			if (img_desc->authenticated_data[i].type_desc == NULL) {
541 				continue;
542 			}
543 
544 			/* Get the parameter from the image parser module */
545 			rc = img_parser_get_auth_param(img_desc->img_type,
546 					img_desc->authenticated_data[i].type_desc,
547 					img_ptr, img_len, &param_ptr, &param_len);
548 			if (rc != 0) {
549 				VERBOSE("[TBB] %s():%d failed with error code %d.\n",
550 					__func__, __LINE__, rc);
551 				return rc;
552 			}
553 
554 			/* Check parameter size */
555 			if (param_len > img_desc->authenticated_data[i].data.len) {
556 				return 1;
557 			}
558 
559 			/* Copy the parameter for later use */
560 			memcpy((void *)img_desc->authenticated_data[i].data.ptr,
561 					(void *)param_ptr, param_len);
562 
563 			/*
564 			 * If this is a public key then measure and publicise
565 			 * it.
566 			 */
567 			type_desc = img_desc->authenticated_data[i].type_desc;
568 			if (type_desc->type == AUTH_PARAM_PUB_KEY) {
569 				rc = plat_mboot_measure_key(type_desc->cookie,
570 							    param_ptr,
571 							    param_len);
572 				if (rc != 0) {
573 					VERBOSE("[TBB] %s():%d failed with error code %d.\n",
574 						__func__, __LINE__, rc);
575 				}
576 			}
577 		}
578 	}
579 
580 	/* Mark image as authenticated */
581 	auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED;
582 
583 	return 0;
584 }
585