xref: /aosp_15_r20/external/arm-trusted-firmware/drivers/auth/auth_mod.c (revision 54fd6939e177f8ff529b10183254802c76df6d08)
1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park  * Copyright (c) 2015-2021, ARM Limited and Contributors. All rights reserved.
3*54fd6939SJiyong Park  *
4*54fd6939SJiyong Park  * SPDX-License-Identifier: BSD-3-Clause
5*54fd6939SJiyong Park  */
6*54fd6939SJiyong Park 
7*54fd6939SJiyong Park #include <assert.h>
8*54fd6939SJiyong Park #include <stdint.h>
9*54fd6939SJiyong Park #include <string.h>
10*54fd6939SJiyong Park 
11*54fd6939SJiyong Park #include <platform_def.h>
12*54fd6939SJiyong Park 
13*54fd6939SJiyong Park #include <common/debug.h>
14*54fd6939SJiyong Park #include <common/tbbr/cot_def.h>
15*54fd6939SJiyong Park #include <drivers/auth/auth_common.h>
16*54fd6939SJiyong Park #include <drivers/auth/auth_mod.h>
17*54fd6939SJiyong Park #include <drivers/auth/crypto_mod.h>
18*54fd6939SJiyong Park #include <drivers/auth/img_parser_mod.h>
19*54fd6939SJiyong Park #include <drivers/fwu/fwu.h>
20*54fd6939SJiyong Park #include <lib/fconf/fconf_tbbr_getter.h>
21*54fd6939SJiyong Park #include <plat/common/platform.h>
22*54fd6939SJiyong Park 
23*54fd6939SJiyong Park /* ASN.1 tags */
24*54fd6939SJiyong Park #define ASN1_INTEGER                 0x02
25*54fd6939SJiyong Park 
26*54fd6939SJiyong Park #define return_if_error(rc) \
27*54fd6939SJiyong Park 	do { \
28*54fd6939SJiyong Park 		if (rc != 0) { \
29*54fd6939SJiyong Park 			return rc; \
30*54fd6939SJiyong Park 		} \
31*54fd6939SJiyong Park 	} while (0)
32*54fd6939SJiyong Park 
33*54fd6939SJiyong Park #pragma weak plat_set_nv_ctr2
34*54fd6939SJiyong Park 
35*54fd6939SJiyong Park 
cmp_auth_param_type_desc(const auth_param_type_desc_t * a,const auth_param_type_desc_t * b)36*54fd6939SJiyong Park static int cmp_auth_param_type_desc(const auth_param_type_desc_t *a,
37*54fd6939SJiyong Park 		const auth_param_type_desc_t *b)
38*54fd6939SJiyong Park {
39*54fd6939SJiyong Park 	if ((a->type == b->type) && (a->cookie == b->cookie)) {
40*54fd6939SJiyong Park 		return 0;
41*54fd6939SJiyong Park 	}
42*54fd6939SJiyong Park 	return 1;
43*54fd6939SJiyong Park }
44*54fd6939SJiyong Park 
45*54fd6939SJiyong Park /*
46*54fd6939SJiyong Park  * This function obtains the requested authentication parameter data from the
47*54fd6939SJiyong Park  * information extracted from the parent image after its authentication.
48*54fd6939SJiyong Park  */
auth_get_param(const auth_param_type_desc_t * param_type_desc,const auth_img_desc_t * img_desc,void ** param,unsigned int * len)49*54fd6939SJiyong Park static int auth_get_param(const auth_param_type_desc_t *param_type_desc,
50*54fd6939SJiyong Park 			  const auth_img_desc_t *img_desc,
51*54fd6939SJiyong Park 			  void **param, unsigned int *len)
52*54fd6939SJiyong Park {
53*54fd6939SJiyong Park 	int i;
54*54fd6939SJiyong Park 
55*54fd6939SJiyong Park 	if (img_desc->authenticated_data == NULL)
56*54fd6939SJiyong Park 		return 1;
57*54fd6939SJiyong Park 
58*54fd6939SJiyong Park 	for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
59*54fd6939SJiyong Park 		if (0 == cmp_auth_param_type_desc(param_type_desc,
60*54fd6939SJiyong Park 				img_desc->authenticated_data[i].type_desc)) {
61*54fd6939SJiyong Park 			*param = img_desc->authenticated_data[i].data.ptr;
62*54fd6939SJiyong Park 			*len = img_desc->authenticated_data[i].data.len;
63*54fd6939SJiyong Park 			return 0;
64*54fd6939SJiyong Park 		}
65*54fd6939SJiyong Park 	}
66*54fd6939SJiyong Park 
67*54fd6939SJiyong Park 	return 1;
68*54fd6939SJiyong Park }
69*54fd6939SJiyong Park 
70*54fd6939SJiyong Park /*
71*54fd6939SJiyong Park  * Authenticate an image by matching the data hash
72*54fd6939SJiyong Park  *
73*54fd6939SJiyong Park  * This function implements 'AUTH_METHOD_HASH'. To authenticate an image using
74*54fd6939SJiyong Park  * this method, the image must contain:
75*54fd6939SJiyong Park  *
76*54fd6939SJiyong Park  *   - The data to calculate the hash from
77*54fd6939SJiyong Park  *
78*54fd6939SJiyong Park  * The parent image must contain:
79*54fd6939SJiyong Park  *
80*54fd6939SJiyong Park  *   - The hash to be matched with (including hash algorithm)
81*54fd6939SJiyong Park  *
82*54fd6939SJiyong Park  * For a successful authentication, both hashes must match. The function calls
83*54fd6939SJiyong Park  * the crypto-module to check this matching.
84*54fd6939SJiyong Park  *
85*54fd6939SJiyong Park  * Parameters:
86*54fd6939SJiyong Park  *   param: parameters to perform the hash authentication
87*54fd6939SJiyong Park  *   img_desc: pointer to image descriptor so we can know the image type
88*54fd6939SJiyong Park  *             and parent image
89*54fd6939SJiyong Park  *   img: pointer to image in memory
90*54fd6939SJiyong Park  *   img_len: length of image (in bytes)
91*54fd6939SJiyong Park  *
92*54fd6939SJiyong Park  * Return:
93*54fd6939SJiyong Park  *   0 = success, Otherwise = error
94*54fd6939SJiyong Park  */
auth_hash(const auth_method_param_hash_t * param,const auth_img_desc_t * img_desc,void * img,unsigned int img_len)95*54fd6939SJiyong Park static int auth_hash(const auth_method_param_hash_t *param,
96*54fd6939SJiyong Park 		     const auth_img_desc_t *img_desc,
97*54fd6939SJiyong Park 		     void *img, unsigned int img_len)
98*54fd6939SJiyong Park {
99*54fd6939SJiyong Park 	void *data_ptr, *hash_der_ptr;
100*54fd6939SJiyong Park 	unsigned int data_len, hash_der_len;
101*54fd6939SJiyong Park 	int rc = 0;
102*54fd6939SJiyong Park 
103*54fd6939SJiyong Park 	/* Get the hash from the parent image. This hash will be DER encoded
104*54fd6939SJiyong Park 	 * and contain the hash algorithm */
105*54fd6939SJiyong Park 	rc = auth_get_param(param->hash, img_desc->parent,
106*54fd6939SJiyong Park 			&hash_der_ptr, &hash_der_len);
107*54fd6939SJiyong Park 	return_if_error(rc);
108*54fd6939SJiyong Park 
109*54fd6939SJiyong Park 	/* Get the data to be hashed from the current image */
110*54fd6939SJiyong Park 	rc = img_parser_get_auth_param(img_desc->img_type, param->data,
111*54fd6939SJiyong Park 			img, img_len, &data_ptr, &data_len);
112*54fd6939SJiyong Park 	return_if_error(rc);
113*54fd6939SJiyong Park 
114*54fd6939SJiyong Park 	/* Ask the crypto module to verify this hash */
115*54fd6939SJiyong Park 	rc = crypto_mod_verify_hash(data_ptr, data_len,
116*54fd6939SJiyong Park 				    hash_der_ptr, hash_der_len);
117*54fd6939SJiyong Park 
118*54fd6939SJiyong Park 	return rc;
119*54fd6939SJiyong Park }
120*54fd6939SJiyong Park 
121*54fd6939SJiyong Park /*
122*54fd6939SJiyong Park  * Authenticate by digital signature
123*54fd6939SJiyong Park  *
124*54fd6939SJiyong Park  * This function implements 'AUTH_METHOD_SIG'. To authenticate an image using
125*54fd6939SJiyong Park  * this method, the image must contain:
126*54fd6939SJiyong Park  *
127*54fd6939SJiyong Park  *   - Data to be signed
128*54fd6939SJiyong Park  *   - Signature
129*54fd6939SJiyong Park  *   - Signature algorithm
130*54fd6939SJiyong Park  *
131*54fd6939SJiyong Park  * We rely on the image parser module to extract this data from the image.
132*54fd6939SJiyong Park  * The parent image must contain:
133*54fd6939SJiyong Park  *
134*54fd6939SJiyong Park  *   - Public key (or a hash of it)
135*54fd6939SJiyong Park  *
136*54fd6939SJiyong Park  * If the parent image contains only a hash of the key, we will try to obtain
137*54fd6939SJiyong Park  * the public key from the image itself (i.e. self-signed certificates). In that
138*54fd6939SJiyong Park  * case, the signature verification is considered just an integrity check and
139*54fd6939SJiyong Park  * the authentication is established by calculating the hash of the key and
140*54fd6939SJiyong Park  * comparing it with the hash obtained from the parent.
141*54fd6939SJiyong Park  *
142*54fd6939SJiyong Park  * If the image has no parent (NULL), it means it has to be authenticated using
143*54fd6939SJiyong Park  * the ROTPK stored in the platform. Again, this ROTPK could be the key itself
144*54fd6939SJiyong Park  * or a hash of it.
145*54fd6939SJiyong Park  *
146*54fd6939SJiyong Park  * Return: 0 = success, Otherwise = error
147*54fd6939SJiyong Park  */
auth_signature(const auth_method_param_sig_t * param,const auth_img_desc_t * img_desc,void * img,unsigned int img_len)148*54fd6939SJiyong Park static int auth_signature(const auth_method_param_sig_t *param,
149*54fd6939SJiyong Park 			  const auth_img_desc_t *img_desc,
150*54fd6939SJiyong Park 			  void *img, unsigned int img_len)
151*54fd6939SJiyong Park {
152*54fd6939SJiyong Park 	void *data_ptr, *pk_ptr, *pk_hash_ptr, *sig_ptr, *sig_alg_ptr;
153*54fd6939SJiyong Park 	unsigned int data_len, pk_len, pk_hash_len, sig_len, sig_alg_len;
154*54fd6939SJiyong Park 	unsigned int flags = 0;
155*54fd6939SJiyong Park 	int rc = 0;
156*54fd6939SJiyong Park 
157*54fd6939SJiyong Park 	/* Get the data to be signed from current image */
158*54fd6939SJiyong Park 	rc = img_parser_get_auth_param(img_desc->img_type, param->data,
159*54fd6939SJiyong Park 			img, img_len, &data_ptr, &data_len);
160*54fd6939SJiyong Park 	return_if_error(rc);
161*54fd6939SJiyong Park 
162*54fd6939SJiyong Park 	/* Get the signature from current image */
163*54fd6939SJiyong Park 	rc = img_parser_get_auth_param(img_desc->img_type, param->sig,
164*54fd6939SJiyong Park 			img, img_len, &sig_ptr, &sig_len);
165*54fd6939SJiyong Park 	return_if_error(rc);
166*54fd6939SJiyong Park 
167*54fd6939SJiyong Park 	/* Get the signature algorithm from current image */
168*54fd6939SJiyong Park 	rc = img_parser_get_auth_param(img_desc->img_type, param->alg,
169*54fd6939SJiyong Park 			img, img_len, &sig_alg_ptr, &sig_alg_len);
170*54fd6939SJiyong Park 	return_if_error(rc);
171*54fd6939SJiyong Park 
172*54fd6939SJiyong Park 	/* Get the public key from the parent. If there is no parent (NULL),
173*54fd6939SJiyong Park 	 * the certificate has been signed with the ROTPK, so we have to get
174*54fd6939SJiyong Park 	 * the PK from the platform */
175*54fd6939SJiyong Park 	if (img_desc->parent) {
176*54fd6939SJiyong Park 		rc = auth_get_param(param->pk, img_desc->parent,
177*54fd6939SJiyong Park 				&pk_ptr, &pk_len);
178*54fd6939SJiyong Park 	} else {
179*54fd6939SJiyong Park 		rc = plat_get_rotpk_info(param->pk->cookie, &pk_ptr, &pk_len,
180*54fd6939SJiyong Park 				&flags);
181*54fd6939SJiyong Park 	}
182*54fd6939SJiyong Park 	return_if_error(rc);
183*54fd6939SJiyong Park 
184*54fd6939SJiyong Park 	if (flags & (ROTPK_IS_HASH | ROTPK_NOT_DEPLOYED)) {
185*54fd6939SJiyong Park 		/* If the PK is a hash of the key or if the ROTPK is not
186*54fd6939SJiyong Park 		   deployed on the platform, retrieve the key from the image */
187*54fd6939SJiyong Park 		pk_hash_ptr = pk_ptr;
188*54fd6939SJiyong Park 		pk_hash_len = pk_len;
189*54fd6939SJiyong Park 		rc = img_parser_get_auth_param(img_desc->img_type,
190*54fd6939SJiyong Park 					param->pk, img, img_len,
191*54fd6939SJiyong Park 					&pk_ptr, &pk_len);
192*54fd6939SJiyong Park 		return_if_error(rc);
193*54fd6939SJiyong Park 
194*54fd6939SJiyong Park 		/* Ask the crypto module to verify the signature */
195*54fd6939SJiyong Park 		rc = crypto_mod_verify_signature(data_ptr, data_len,
196*54fd6939SJiyong Park 						 sig_ptr, sig_len,
197*54fd6939SJiyong Park 						 sig_alg_ptr, sig_alg_len,
198*54fd6939SJiyong Park 						 pk_ptr, pk_len);
199*54fd6939SJiyong Park 		return_if_error(rc);
200*54fd6939SJiyong Park 
201*54fd6939SJiyong Park 		if (flags & ROTPK_NOT_DEPLOYED) {
202*54fd6939SJiyong Park 			NOTICE("ROTPK is not deployed on platform. "
203*54fd6939SJiyong Park 				"Skipping ROTPK verification.\n");
204*54fd6939SJiyong Park 		} else {
205*54fd6939SJiyong Park 			/* Ask the crypto-module to verify the key hash */
206*54fd6939SJiyong Park 			rc = crypto_mod_verify_hash(pk_ptr, pk_len,
207*54fd6939SJiyong Park 				    pk_hash_ptr, pk_hash_len);
208*54fd6939SJiyong Park 		}
209*54fd6939SJiyong Park 	} else {
210*54fd6939SJiyong Park 		/* Ask the crypto module to verify the signature */
211*54fd6939SJiyong Park 		rc = crypto_mod_verify_signature(data_ptr, data_len,
212*54fd6939SJiyong Park 						 sig_ptr, sig_len,
213*54fd6939SJiyong Park 						 sig_alg_ptr, sig_alg_len,
214*54fd6939SJiyong Park 						 pk_ptr, pk_len);
215*54fd6939SJiyong Park 	}
216*54fd6939SJiyong Park 
217*54fd6939SJiyong Park 	return rc;
218*54fd6939SJiyong Park }
219*54fd6939SJiyong Park 
220*54fd6939SJiyong Park /*
221*54fd6939SJiyong Park  * Authenticate by Non-Volatile counter
222*54fd6939SJiyong Park  *
223*54fd6939SJiyong Park  * To protect the system against rollback, the platform includes a non-volatile
224*54fd6939SJiyong Park  * counter whose value can only be increased. All certificates include a counter
225*54fd6939SJiyong Park  * value that should not be lower than the value stored in the platform. If the
226*54fd6939SJiyong Park  * value is larger, the counter in the platform must be updated to the new value
227*54fd6939SJiyong Park  * (provided it has been authenticated).
228*54fd6939SJiyong Park  *
229*54fd6939SJiyong Park  * Return: 0 = success, Otherwise = error
230*54fd6939SJiyong Park  * Returns additionally,
231*54fd6939SJiyong Park  * cert_nv_ctr -> NV counter value present in the certificate
232*54fd6939SJiyong Park  * need_nv_ctr_upgrade = 0 -> platform NV counter upgrade is not needed
233*54fd6939SJiyong Park  * need_nv_ctr_upgrade = 1 -> platform NV counter upgrade is needed
234*54fd6939SJiyong Park  */
auth_nvctr(const auth_method_param_nv_ctr_t * param,const auth_img_desc_t * img_desc,void * img,unsigned int img_len,unsigned int * cert_nv_ctr,bool * need_nv_ctr_upgrade)235*54fd6939SJiyong Park static int auth_nvctr(const auth_method_param_nv_ctr_t *param,
236*54fd6939SJiyong Park 		      const auth_img_desc_t *img_desc,
237*54fd6939SJiyong Park 		      void *img, unsigned int img_len,
238*54fd6939SJiyong Park 		      unsigned int *cert_nv_ctr,
239*54fd6939SJiyong Park 		      bool *need_nv_ctr_upgrade)
240*54fd6939SJiyong Park {
241*54fd6939SJiyong Park 	char *p;
242*54fd6939SJiyong Park 	void *data_ptr = NULL;
243*54fd6939SJiyong Park 	unsigned int data_len, len, i;
244*54fd6939SJiyong Park 	unsigned int plat_nv_ctr;
245*54fd6939SJiyong Park 	int rc = 0;
246*54fd6939SJiyong Park 	bool is_trial_run = false;
247*54fd6939SJiyong Park 
248*54fd6939SJiyong Park 	/* Get the counter value from current image. The AM expects the IPM
249*54fd6939SJiyong Park 	 * to return the counter value as a DER encoded integer */
250*54fd6939SJiyong Park 	rc = img_parser_get_auth_param(img_desc->img_type, param->cert_nv_ctr,
251*54fd6939SJiyong Park 				       img, img_len, &data_ptr, &data_len);
252*54fd6939SJiyong Park 	return_if_error(rc);
253*54fd6939SJiyong Park 
254*54fd6939SJiyong Park 	/* Parse the DER encoded integer */
255*54fd6939SJiyong Park 	assert(data_ptr);
256*54fd6939SJiyong Park 	p = (char *)data_ptr;
257*54fd6939SJiyong Park 	if (*p != ASN1_INTEGER) {
258*54fd6939SJiyong Park 		/* Invalid ASN.1 integer */
259*54fd6939SJiyong Park 		return 1;
260*54fd6939SJiyong Park 	}
261*54fd6939SJiyong Park 	p++;
262*54fd6939SJiyong Park 
263*54fd6939SJiyong Park 	/* NV-counters are unsigned integers up to 32-bit */
264*54fd6939SJiyong Park 	len = (unsigned int)(*p & 0x7f);
265*54fd6939SJiyong Park 	if ((*p & 0x80) || (len > 4)) {
266*54fd6939SJiyong Park 		return 1;
267*54fd6939SJiyong Park 	}
268*54fd6939SJiyong Park 	p++;
269*54fd6939SJiyong Park 
270*54fd6939SJiyong Park 	/* Check the number is not negative */
271*54fd6939SJiyong Park 	if (*p & 0x80) {
272*54fd6939SJiyong Park 		return 1;
273*54fd6939SJiyong Park 	}
274*54fd6939SJiyong Park 
275*54fd6939SJiyong Park 	/* Convert to unsigned int. This code is for a little-endian CPU */
276*54fd6939SJiyong Park 	*cert_nv_ctr = 0;
277*54fd6939SJiyong Park 	for (i = 0; i < len; i++) {
278*54fd6939SJiyong Park 		*cert_nv_ctr = (*cert_nv_ctr << 8) | *p++;
279*54fd6939SJiyong Park 	}
280*54fd6939SJiyong Park 
281*54fd6939SJiyong Park 	/* Get the counter from the platform */
282*54fd6939SJiyong Park 	rc = plat_get_nv_ctr(param->plat_nv_ctr->cookie, &plat_nv_ctr);
283*54fd6939SJiyong Park 	return_if_error(rc);
284*54fd6939SJiyong Park 
285*54fd6939SJiyong Park 	if (*cert_nv_ctr < plat_nv_ctr) {
286*54fd6939SJiyong Park 		/* Invalid NV-counter */
287*54fd6939SJiyong Park 		return 1;
288*54fd6939SJiyong Park 	} else if (*cert_nv_ctr > plat_nv_ctr) {
289*54fd6939SJiyong Park #if PSA_FWU_SUPPORT && IMAGE_BL2
290*54fd6939SJiyong Park 		is_trial_run = fwu_is_trial_run_state();
291*54fd6939SJiyong Park #endif /* PSA_FWU_SUPPORT && IMAGE_BL2 */
292*54fd6939SJiyong Park 		*need_nv_ctr_upgrade = !is_trial_run;
293*54fd6939SJiyong Park 	}
294*54fd6939SJiyong Park 
295*54fd6939SJiyong Park 	return 0;
296*54fd6939SJiyong Park }
297*54fd6939SJiyong Park 
plat_set_nv_ctr2(void * cookie,const auth_img_desc_t * img_desc __unused,unsigned int nv_ctr)298*54fd6939SJiyong Park int plat_set_nv_ctr2(void *cookie, const auth_img_desc_t *img_desc __unused,
299*54fd6939SJiyong Park 		unsigned int nv_ctr)
300*54fd6939SJiyong Park {
301*54fd6939SJiyong Park 	return plat_set_nv_ctr(cookie, nv_ctr);
302*54fd6939SJiyong Park }
303*54fd6939SJiyong Park 
304*54fd6939SJiyong Park /*
305*54fd6939SJiyong Park  * Return the parent id in the output parameter '*parent_id'
306*54fd6939SJiyong Park  *
307*54fd6939SJiyong Park  * Return value:
308*54fd6939SJiyong Park  *   0 = Image has parent, 1 = Image has no parent or parent is authenticated
309*54fd6939SJiyong Park  */
auth_mod_get_parent_id(unsigned int img_id,unsigned int * parent_id)310*54fd6939SJiyong Park int auth_mod_get_parent_id(unsigned int img_id, unsigned int *parent_id)
311*54fd6939SJiyong Park {
312*54fd6939SJiyong Park 	const auth_img_desc_t *img_desc = NULL;
313*54fd6939SJiyong Park 
314*54fd6939SJiyong Park 	assert(parent_id != NULL);
315*54fd6939SJiyong Park 	/* Get the image descriptor */
316*54fd6939SJiyong Park 	img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
317*54fd6939SJiyong Park 
318*54fd6939SJiyong Park 	/* Check if the image has no parent (ROT) */
319*54fd6939SJiyong Park 	if (img_desc->parent == NULL) {
320*54fd6939SJiyong Park 		*parent_id = 0;
321*54fd6939SJiyong Park 		return 1;
322*54fd6939SJiyong Park 	}
323*54fd6939SJiyong Park 
324*54fd6939SJiyong Park 	/* Check if the parent has already been authenticated */
325*54fd6939SJiyong Park 	if (auth_img_flags[img_desc->parent->img_id] & IMG_FLAG_AUTHENTICATED) {
326*54fd6939SJiyong Park 		*parent_id = 0;
327*54fd6939SJiyong Park 		return 1;
328*54fd6939SJiyong Park 	}
329*54fd6939SJiyong Park 
330*54fd6939SJiyong Park 	*parent_id = img_desc->parent->img_id;
331*54fd6939SJiyong Park 	return 0;
332*54fd6939SJiyong Park }
333*54fd6939SJiyong Park 
334*54fd6939SJiyong Park /*
335*54fd6939SJiyong Park  * Initialize the different modules in the authentication framework
336*54fd6939SJiyong Park  */
auth_mod_init(void)337*54fd6939SJiyong Park void auth_mod_init(void)
338*54fd6939SJiyong Park {
339*54fd6939SJiyong Park 	/* Check we have a valid CoT registered */
340*54fd6939SJiyong Park 	assert(cot_desc_ptr != NULL);
341*54fd6939SJiyong Park 
342*54fd6939SJiyong Park 	/* Crypto module */
343*54fd6939SJiyong Park 	crypto_mod_init();
344*54fd6939SJiyong Park 
345*54fd6939SJiyong Park 	/* Image parser module */
346*54fd6939SJiyong Park 	img_parser_init();
347*54fd6939SJiyong Park }
348*54fd6939SJiyong Park 
349*54fd6939SJiyong Park /*
350*54fd6939SJiyong Park  * Authenticate a certificate/image
351*54fd6939SJiyong Park  *
352*54fd6939SJiyong Park  * Return: 0 = success, Otherwise = error
353*54fd6939SJiyong Park  */
auth_mod_verify_img(unsigned int img_id,void * img_ptr,unsigned int img_len)354*54fd6939SJiyong Park int auth_mod_verify_img(unsigned int img_id,
355*54fd6939SJiyong Park 			void *img_ptr,
356*54fd6939SJiyong Park 			unsigned int img_len)
357*54fd6939SJiyong Park {
358*54fd6939SJiyong Park 	const auth_img_desc_t *img_desc = NULL;
359*54fd6939SJiyong Park 	const auth_method_desc_t *auth_method = NULL;
360*54fd6939SJiyong Park 	void *param_ptr;
361*54fd6939SJiyong Park 	unsigned int param_len;
362*54fd6939SJiyong Park 	int rc, i;
363*54fd6939SJiyong Park 	unsigned int cert_nv_ctr = 0;
364*54fd6939SJiyong Park 	bool need_nv_ctr_upgrade = false;
365*54fd6939SJiyong Park 	bool sig_auth_done = false;
366*54fd6939SJiyong Park 	const auth_method_param_nv_ctr_t *nv_ctr_param = NULL;
367*54fd6939SJiyong Park 
368*54fd6939SJiyong Park 	/* Get the image descriptor from the chain of trust */
369*54fd6939SJiyong Park 	img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
370*54fd6939SJiyong Park 
371*54fd6939SJiyong Park 	/* Ask the parser to check the image integrity */
372*54fd6939SJiyong Park 	rc = img_parser_check_integrity(img_desc->img_type, img_ptr, img_len);
373*54fd6939SJiyong Park 	return_if_error(rc);
374*54fd6939SJiyong Park 
375*54fd6939SJiyong Park 	/* Authenticate the image using the methods indicated in the image
376*54fd6939SJiyong Park 	 * descriptor. */
377*54fd6939SJiyong Park 	if (img_desc->img_auth_methods == NULL)
378*54fd6939SJiyong Park 		return 1;
379*54fd6939SJiyong Park 	for (i = 0 ; i < AUTH_METHOD_NUM ; i++) {
380*54fd6939SJiyong Park 		auth_method = &img_desc->img_auth_methods[i];
381*54fd6939SJiyong Park 		switch (auth_method->type) {
382*54fd6939SJiyong Park 		case AUTH_METHOD_NONE:
383*54fd6939SJiyong Park 			rc = 0;
384*54fd6939SJiyong Park 			break;
385*54fd6939SJiyong Park 		case AUTH_METHOD_HASH:
386*54fd6939SJiyong Park 			rc = auth_hash(&auth_method->param.hash,
387*54fd6939SJiyong Park 					img_desc, img_ptr, img_len);
388*54fd6939SJiyong Park 			break;
389*54fd6939SJiyong Park 		case AUTH_METHOD_SIG:
390*54fd6939SJiyong Park 			rc = auth_signature(&auth_method->param.sig,
391*54fd6939SJiyong Park 					img_desc, img_ptr, img_len);
392*54fd6939SJiyong Park 			sig_auth_done = true;
393*54fd6939SJiyong Park 			break;
394*54fd6939SJiyong Park 		case AUTH_METHOD_NV_CTR:
395*54fd6939SJiyong Park 			nv_ctr_param = &auth_method->param.nv_ctr;
396*54fd6939SJiyong Park 			rc = auth_nvctr(nv_ctr_param,
397*54fd6939SJiyong Park 					img_desc, img_ptr, img_len,
398*54fd6939SJiyong Park 					&cert_nv_ctr, &need_nv_ctr_upgrade);
399*54fd6939SJiyong Park 			break;
400*54fd6939SJiyong Park 		default:
401*54fd6939SJiyong Park 			/* Unknown authentication method */
402*54fd6939SJiyong Park 			rc = 1;
403*54fd6939SJiyong Park 			break;
404*54fd6939SJiyong Park 		}
405*54fd6939SJiyong Park 		return_if_error(rc);
406*54fd6939SJiyong Park 	}
407*54fd6939SJiyong Park 
408*54fd6939SJiyong Park 	/*
409*54fd6939SJiyong Park 	 * Do platform NV counter upgrade only if the certificate gets
410*54fd6939SJiyong Park 	 * authenticated, and platform NV-counter upgrade is needed.
411*54fd6939SJiyong Park 	 */
412*54fd6939SJiyong Park 	if (need_nv_ctr_upgrade && sig_auth_done) {
413*54fd6939SJiyong Park 		rc = plat_set_nv_ctr2(nv_ctr_param->plat_nv_ctr->cookie,
414*54fd6939SJiyong Park 				      img_desc, cert_nv_ctr);
415*54fd6939SJiyong Park 		return_if_error(rc);
416*54fd6939SJiyong Park 	}
417*54fd6939SJiyong Park 
418*54fd6939SJiyong Park 	/* Extract the parameters indicated in the image descriptor to
419*54fd6939SJiyong Park 	 * authenticate the children images. */
420*54fd6939SJiyong Park 	if (img_desc->authenticated_data != NULL) {
421*54fd6939SJiyong Park 		for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
422*54fd6939SJiyong Park 			if (img_desc->authenticated_data[i].type_desc == NULL) {
423*54fd6939SJiyong Park 				continue;
424*54fd6939SJiyong Park 			}
425*54fd6939SJiyong Park 
426*54fd6939SJiyong Park 			/* Get the parameter from the image parser module */
427*54fd6939SJiyong Park 			rc = img_parser_get_auth_param(img_desc->img_type,
428*54fd6939SJiyong Park 					img_desc->authenticated_data[i].type_desc,
429*54fd6939SJiyong Park 					img_ptr, img_len, &param_ptr, &param_len);
430*54fd6939SJiyong Park 			return_if_error(rc);
431*54fd6939SJiyong Park 
432*54fd6939SJiyong Park 			/* Check parameter size */
433*54fd6939SJiyong Park 			if (param_len > img_desc->authenticated_data[i].data.len) {
434*54fd6939SJiyong Park 				return 1;
435*54fd6939SJiyong Park 			}
436*54fd6939SJiyong Park 
437*54fd6939SJiyong Park 			/* Copy the parameter for later use */
438*54fd6939SJiyong Park 			memcpy((void *)img_desc->authenticated_data[i].data.ptr,
439*54fd6939SJiyong Park 					(void *)param_ptr, param_len);
440*54fd6939SJiyong Park 		}
441*54fd6939SJiyong Park 	}
442*54fd6939SJiyong Park 
443*54fd6939SJiyong Park 	/* Mark image as authenticated */
444*54fd6939SJiyong Park 	auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED;
445*54fd6939SJiyong Park 
446*54fd6939SJiyong Park 	return 0;
447*54fd6939SJiyong Park }
448