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