xref: /rk3399_ARM-atf/drivers/auth/auth_mod.c (revision f1e693a77548950cfffcb1d5a4b67cf349e0aed9)
105799ae0SJuan Castillo /*
2*f1e693a7SManish V Badarkhe  * Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved.
305799ae0SJuan Castillo  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
505799ae0SJuan Castillo  */
605799ae0SJuan Castillo 
705799ae0SJuan Castillo #include <assert.h>
805799ae0SJuan Castillo #include <stdint.h>
905799ae0SJuan Castillo #include <string.h>
1005799ae0SJuan Castillo 
1109d40e0eSAntonio Nino Diaz #include <platform_def.h>
1209d40e0eSAntonio Nino Diaz 
1309d40e0eSAntonio Nino Diaz #include <common/debug.h>
1409d40e0eSAntonio Nino Diaz #include <common/tbbr/cot_def.h>
1509d40e0eSAntonio Nino Diaz #include <drivers/auth/auth_common.h>
1609d40e0eSAntonio Nino Diaz #include <drivers/auth/auth_mod.h>
1709d40e0eSAntonio Nino Diaz #include <drivers/auth/crypto_mod.h>
1809d40e0eSAntonio Nino Diaz #include <drivers/auth/img_parser_mod.h>
19c0bfc88fSManish V Badarkhe #include <drivers/fwu/fwu.h>
20ab1981dbSLouis Mayencourt #include <lib/fconf/fconf_tbbr_getter.h>
2109d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
2209d40e0eSAntonio Nino Diaz 
2348279d52SJuan Castillo /* ASN.1 tags */
2448279d52SJuan Castillo #define ASN1_INTEGER                 0x02
2548279d52SJuan Castillo 
2605799ae0SJuan Castillo #define return_if_error(rc) \
2705799ae0SJuan Castillo 	do { \
2805799ae0SJuan Castillo 		if (rc != 0) { \
2905799ae0SJuan Castillo 			return rc; \
3005799ae0SJuan Castillo 		} \
3105799ae0SJuan Castillo 	} while (0)
3205799ae0SJuan Castillo 
33d35dee23Sdp-arm #pragma weak plat_set_nv_ctr2
3440f9f644SNicolas Toromanoff #pragma weak plat_convert_pk
35d35dee23Sdp-arm 
3605799ae0SJuan Castillo static int cmp_auth_param_type_desc(const auth_param_type_desc_t *a,
3705799ae0SJuan Castillo 		const auth_param_type_desc_t *b)
3805799ae0SJuan Castillo {
3905799ae0SJuan Castillo 	if ((a->type == b->type) && (a->cookie == b->cookie)) {
4005799ae0SJuan Castillo 		return 0;
4105799ae0SJuan Castillo 	}
4205799ae0SJuan Castillo 	return 1;
4305799ae0SJuan Castillo }
4405799ae0SJuan Castillo 
4505799ae0SJuan Castillo /*
4605799ae0SJuan Castillo  * This function obtains the requested authentication parameter data from the
4705799ae0SJuan Castillo  * information extracted from the parent image after its authentication.
4805799ae0SJuan Castillo  */
4905799ae0SJuan Castillo static int auth_get_param(const auth_param_type_desc_t *param_type_desc,
5005799ae0SJuan Castillo 			  const auth_img_desc_t *img_desc,
5105799ae0SJuan Castillo 			  void **param, unsigned int *len)
5205799ae0SJuan Castillo {
5305799ae0SJuan Castillo 	int i;
5405799ae0SJuan Castillo 
5530070427SJoel Hutton 	if (img_desc->authenticated_data == NULL)
5630070427SJoel Hutton 		return 1;
5730070427SJoel Hutton 
5805799ae0SJuan Castillo 	for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
5905799ae0SJuan Castillo 		if (0 == cmp_auth_param_type_desc(param_type_desc,
6005799ae0SJuan Castillo 				img_desc->authenticated_data[i].type_desc)) {
6105799ae0SJuan Castillo 			*param = img_desc->authenticated_data[i].data.ptr;
6205799ae0SJuan Castillo 			*len = img_desc->authenticated_data[i].data.len;
6305799ae0SJuan Castillo 			return 0;
6405799ae0SJuan Castillo 		}
6505799ae0SJuan Castillo 	}
6605799ae0SJuan Castillo 
6705799ae0SJuan Castillo 	return 1;
6805799ae0SJuan Castillo }
6905799ae0SJuan Castillo 
7005799ae0SJuan Castillo /*
7105799ae0SJuan Castillo  * Authenticate an image by matching the data hash
7205799ae0SJuan Castillo  *
7305799ae0SJuan Castillo  * This function implements 'AUTH_METHOD_HASH'. To authenticate an image using
7405799ae0SJuan Castillo  * this method, the image must contain:
7505799ae0SJuan Castillo  *
7605799ae0SJuan Castillo  *   - The data to calculate the hash from
7705799ae0SJuan Castillo  *
7805799ae0SJuan Castillo  * The parent image must contain:
7905799ae0SJuan Castillo  *
8005799ae0SJuan Castillo  *   - The hash to be matched with (including hash algorithm)
8105799ae0SJuan Castillo  *
8205799ae0SJuan Castillo  * For a successful authentication, both hashes must match. The function calls
8305799ae0SJuan Castillo  * the crypto-module to check this matching.
8405799ae0SJuan Castillo  *
8505799ae0SJuan Castillo  * Parameters:
8605799ae0SJuan Castillo  *   param: parameters to perform the hash authentication
8705799ae0SJuan Castillo  *   img_desc: pointer to image descriptor so we can know the image type
8805799ae0SJuan Castillo  *             and parent image
8905799ae0SJuan Castillo  *   img: pointer to image in memory
9005799ae0SJuan Castillo  *   img_len: length of image (in bytes)
9105799ae0SJuan Castillo  *
9205799ae0SJuan Castillo  * Return:
9305799ae0SJuan Castillo  *   0 = success, Otherwise = error
9405799ae0SJuan Castillo  */
9505799ae0SJuan Castillo static int auth_hash(const auth_method_param_hash_t *param,
9605799ae0SJuan Castillo 		     const auth_img_desc_t *img_desc,
9705799ae0SJuan Castillo 		     void *img, unsigned int img_len)
9805799ae0SJuan Castillo {
9905799ae0SJuan Castillo 	void *data_ptr, *hash_der_ptr;
10005799ae0SJuan Castillo 	unsigned int data_len, hash_der_len;
10105799ae0SJuan Castillo 	int rc = 0;
10205799ae0SJuan Castillo 
10305799ae0SJuan Castillo 	/* Get the hash from the parent image. This hash will be DER encoded
10405799ae0SJuan Castillo 	 * and contain the hash algorithm */
10505799ae0SJuan Castillo 	rc = auth_get_param(param->hash, img_desc->parent,
10605799ae0SJuan Castillo 			&hash_der_ptr, &hash_der_len);
10705799ae0SJuan Castillo 	return_if_error(rc);
10805799ae0SJuan Castillo 
10905799ae0SJuan Castillo 	/* Get the data to be hashed from the current image */
11005799ae0SJuan Castillo 	rc = img_parser_get_auth_param(img_desc->img_type, param->data,
11105799ae0SJuan Castillo 			img, img_len, &data_ptr, &data_len);
11205799ae0SJuan Castillo 	return_if_error(rc);
11305799ae0SJuan Castillo 
11405799ae0SJuan Castillo 	/* Ask the crypto module to verify this hash */
11505799ae0SJuan Castillo 	rc = crypto_mod_verify_hash(data_ptr, data_len,
11605799ae0SJuan Castillo 				    hash_der_ptr, hash_der_len);
11705799ae0SJuan Castillo 
11805799ae0SJuan Castillo 	return rc;
11905799ae0SJuan Castillo }
12005799ae0SJuan Castillo 
12105799ae0SJuan Castillo /*
12205799ae0SJuan Castillo  * Authenticate by digital signature
12305799ae0SJuan Castillo  *
12405799ae0SJuan Castillo  * This function implements 'AUTH_METHOD_SIG'. To authenticate an image using
12505799ae0SJuan Castillo  * this method, the image must contain:
12605799ae0SJuan Castillo  *
12705799ae0SJuan Castillo  *   - Data to be signed
12805799ae0SJuan Castillo  *   - Signature
12905799ae0SJuan Castillo  *   - Signature algorithm
13005799ae0SJuan Castillo  *
13105799ae0SJuan Castillo  * We rely on the image parser module to extract this data from the image.
13205799ae0SJuan Castillo  * The parent image must contain:
13305799ae0SJuan Castillo  *
13405799ae0SJuan Castillo  *   - Public key (or a hash of it)
13505799ae0SJuan Castillo  *
13605799ae0SJuan Castillo  * If the parent image contains only a hash of the key, we will try to obtain
13705799ae0SJuan Castillo  * the public key from the image itself (i.e. self-signed certificates). In that
13805799ae0SJuan Castillo  * case, the signature verification is considered just an integrity check and
13905799ae0SJuan Castillo  * the authentication is established by calculating the hash of the key and
14005799ae0SJuan Castillo  * comparing it with the hash obtained from the parent.
14105799ae0SJuan Castillo  *
14205799ae0SJuan Castillo  * If the image has no parent (NULL), it means it has to be authenticated using
14305799ae0SJuan Castillo  * the ROTPK stored in the platform. Again, this ROTPK could be the key itself
14405799ae0SJuan Castillo  * or a hash of it.
14505799ae0SJuan Castillo  *
14605799ae0SJuan Castillo  * Return: 0 = success, Otherwise = error
14705799ae0SJuan Castillo  */
14805799ae0SJuan Castillo static int auth_signature(const auth_method_param_sig_t *param,
14905799ae0SJuan Castillo 			  const auth_img_desc_t *img_desc,
15005799ae0SJuan Castillo 			  void *img, unsigned int img_len)
15105799ae0SJuan Castillo {
152*f1e693a7SManish V Badarkhe 	void *data_ptr, *pk_ptr, *pk_plat_ptr, *sig_ptr, *sig_alg_ptr;
153*f1e693a7SManish V Badarkhe 	unsigned int data_len, pk_len, pk_plat_len, sig_len, sig_alg_len;
15405799ae0SJuan Castillo 	unsigned int flags = 0;
15505799ae0SJuan Castillo 	int rc = 0;
15605799ae0SJuan Castillo 
15705799ae0SJuan Castillo 	/* Get the data to be signed from current image */
15805799ae0SJuan Castillo 	rc = img_parser_get_auth_param(img_desc->img_type, param->data,
15905799ae0SJuan Castillo 			img, img_len, &data_ptr, &data_len);
16005799ae0SJuan Castillo 	return_if_error(rc);
16105799ae0SJuan Castillo 
16205799ae0SJuan Castillo 	/* Get the signature from current image */
16305799ae0SJuan Castillo 	rc = img_parser_get_auth_param(img_desc->img_type, param->sig,
16405799ae0SJuan Castillo 			img, img_len, &sig_ptr, &sig_len);
16505799ae0SJuan Castillo 	return_if_error(rc);
16605799ae0SJuan Castillo 
16705799ae0SJuan Castillo 	/* Get the signature algorithm from current image */
16805799ae0SJuan Castillo 	rc = img_parser_get_auth_param(img_desc->img_type, param->alg,
16905799ae0SJuan Castillo 			img, img_len, &sig_alg_ptr, &sig_alg_len);
17005799ae0SJuan Castillo 	return_if_error(rc);
17105799ae0SJuan Castillo 
17205799ae0SJuan Castillo 	/* Get the public key from the parent. If there is no parent (NULL),
17305799ae0SJuan Castillo 	 * the certificate has been signed with the ROTPK, so we have to get
17405799ae0SJuan Castillo 	 * the PK from the platform */
175*f1e693a7SManish V Badarkhe 	if (img_desc->parent != NULL) {
17605799ae0SJuan Castillo 		rc = auth_get_param(param->pk, img_desc->parent,
17705799ae0SJuan Castillo 				&pk_ptr, &pk_len);
178*f1e693a7SManish V Badarkhe 		return_if_error(rc);
17905799ae0SJuan Castillo 	} else {
180*f1e693a7SManish V Badarkhe 		/*
181*f1e693a7SManish V Badarkhe 		 * Root certificates are signed with the ROTPK, so we have to
182*f1e693a7SManish V Badarkhe 		 * get it from the platform.
183*f1e693a7SManish V Badarkhe 		 */
184*f1e693a7SManish V Badarkhe 		rc = plat_get_rotpk_info(param->pk->cookie, &pk_plat_ptr,
185*f1e693a7SManish V Badarkhe 					 &pk_plat_len, &flags);
18605799ae0SJuan Castillo 		return_if_error(rc);
18705799ae0SJuan Castillo 
188*f1e693a7SManish V Badarkhe 		assert(is_rotpk_flags_valid(flags));
189*f1e693a7SManish V Badarkhe 
190*f1e693a7SManish V Badarkhe 		/* Also retrieve the key from the image. */
19105799ae0SJuan Castillo 		rc = img_parser_get_auth_param(img_desc->img_type,
19205799ae0SJuan Castillo 					       param->pk, img, img_len,
19305799ae0SJuan Castillo 					       &pk_ptr, &pk_len);
19405799ae0SJuan Castillo 		return_if_error(rc);
19505799ae0SJuan Castillo 
196*f1e693a7SManish V Badarkhe 		/*
197*f1e693a7SManish V Badarkhe 		 * Validate the certificate's key against the platform ROTPK.
198*f1e693a7SManish V Badarkhe 		 *
199*f1e693a7SManish V Badarkhe 		 * Platform may store key in one of the following way -
200*f1e693a7SManish V Badarkhe 		 * 1. Hash of ROTPK
201*f1e693a7SManish V Badarkhe 		 * 2. Hash if prefixed, suffixed or modified ROTPK
202*f1e693a7SManish V Badarkhe 		 * 3. Full ROTPK
203*f1e693a7SManish V Badarkhe 		 */
204*f1e693a7SManish V Badarkhe 		if ((flags & ROTPK_NOT_DEPLOYED) != 0U) {
20504943d33SSoby Mathew 			NOTICE("ROTPK is not deployed on platform. "
20604943d33SSoby Mathew 				"Skipping ROTPK verification.\n");
207*f1e693a7SManish V Badarkhe 		} else if ((flags & ROTPK_IS_HASH) != 0U) {
208*f1e693a7SManish V Badarkhe 			/*
209*f1e693a7SManish V Badarkhe 			 * platform may store the hash of a prefixed,
210*f1e693a7SManish V Badarkhe 			 * suffixed or modified pk
211*f1e693a7SManish V Badarkhe 			 */
21240f9f644SNicolas Toromanoff 			rc = plat_convert_pk(pk_ptr, pk_len, &pk_ptr, &pk_len);
21340f9f644SNicolas Toromanoff 			return_if_error(rc);
21440f9f644SNicolas Toromanoff 
215*f1e693a7SManish V Badarkhe 			/*
216*f1e693a7SManish V Badarkhe 			 * The hash of the certificate's public key must match
217*f1e693a7SManish V Badarkhe 			 * the hash of the ROTPK.
218*f1e693a7SManish V Badarkhe 			 */
21905799ae0SJuan Castillo 			rc = crypto_mod_verify_hash(pk_ptr, pk_len,
220*f1e693a7SManish V Badarkhe 						    pk_plat_ptr, pk_plat_len);
221*f1e693a7SManish V Badarkhe 			return_if_error(rc);
22205799ae0SJuan Castillo 		} else {
223*f1e693a7SManish V Badarkhe 			/* Platform supports full ROTPK */
224*f1e693a7SManish V Badarkhe 			if ((pk_len != pk_plat_len) ||
225*f1e693a7SManish V Badarkhe 			    (memcmp(pk_plat_ptr, pk_ptr, pk_len) != 0)) {
226*f1e693a7SManish V Badarkhe 				ERROR("plat and cert ROTPK len mismatch\n");
227*f1e693a7SManish V Badarkhe 				return -1;
228*f1e693a7SManish V Badarkhe 			}
229*f1e693a7SManish V Badarkhe 		}
230*f1e693a7SManish V Badarkhe 	}
231*f1e693a7SManish V Badarkhe 
23205799ae0SJuan Castillo 	/* Ask the crypto module to verify the signature */
23305799ae0SJuan Castillo 	rc = crypto_mod_verify_signature(data_ptr, data_len,
23405799ae0SJuan Castillo 					 sig_ptr, sig_len,
23505799ae0SJuan Castillo 					 sig_alg_ptr, sig_alg_len,
23605799ae0SJuan Castillo 					 pk_ptr, pk_len);
23705799ae0SJuan Castillo 
23805799ae0SJuan Castillo 	return rc;
23905799ae0SJuan Castillo }
24005799ae0SJuan Castillo 
24105799ae0SJuan Castillo /*
24248279d52SJuan Castillo  * Authenticate by Non-Volatile counter
24348279d52SJuan Castillo  *
24448279d52SJuan Castillo  * To protect the system against rollback, the platform includes a non-volatile
24548279d52SJuan Castillo  * counter whose value can only be increased. All certificates include a counter
24648279d52SJuan Castillo  * value that should not be lower than the value stored in the platform. If the
247a2a5a945SManish V Badarkhe  * value is larger, the counter in the platform must be updated to the new value
248a2a5a945SManish V Badarkhe  * (provided it has been authenticated).
24948279d52SJuan Castillo  *
25048279d52SJuan Castillo  * Return: 0 = success, Otherwise = error
251a2a5a945SManish V Badarkhe  * Returns additionally,
252a2a5a945SManish V Badarkhe  * cert_nv_ctr -> NV counter value present in the certificate
253a2a5a945SManish V Badarkhe  * need_nv_ctr_upgrade = 0 -> platform NV counter upgrade is not needed
254a2a5a945SManish V Badarkhe  * need_nv_ctr_upgrade = 1 -> platform NV counter upgrade is needed
25548279d52SJuan Castillo  */
25648279d52SJuan Castillo static int auth_nvctr(const auth_method_param_nv_ctr_t *param,
25748279d52SJuan Castillo 		      const auth_img_desc_t *img_desc,
258a2a5a945SManish V Badarkhe 		      void *img, unsigned int img_len,
259a2a5a945SManish V Badarkhe 		      unsigned int *cert_nv_ctr,
260a2a5a945SManish V Badarkhe 		      bool *need_nv_ctr_upgrade)
26148279d52SJuan Castillo {
262abb8f936SDemi Marie Obenour 	unsigned char *p;
26348279d52SJuan Castillo 	void *data_ptr = NULL;
26448279d52SJuan Castillo 	unsigned int data_len, len, i;
265a2a5a945SManish V Badarkhe 	unsigned int plat_nv_ctr;
26648279d52SJuan Castillo 	int rc = 0;
267c0bfc88fSManish V Badarkhe 	bool is_trial_run = false;
26848279d52SJuan Castillo 
26948279d52SJuan Castillo 	/* Get the counter value from current image. The AM expects the IPM
27048279d52SJuan Castillo 	 * to return the counter value as a DER encoded integer */
27148279d52SJuan Castillo 	rc = img_parser_get_auth_param(img_desc->img_type, param->cert_nv_ctr,
27248279d52SJuan Castillo 				       img, img_len, &data_ptr, &data_len);
27348279d52SJuan Castillo 	return_if_error(rc);
27448279d52SJuan Castillo 
27548279d52SJuan Castillo 	/* Parse the DER encoded integer */
27648279d52SJuan Castillo 	assert(data_ptr);
277abb8f936SDemi Marie Obenour 	p = (unsigned char *)data_ptr;
278abb8f936SDemi Marie Obenour 
279abb8f936SDemi Marie Obenour 	/*
280abb8f936SDemi Marie Obenour 	 * Integers must be at least 3 bytes: 1 for tag, 1 for length, and 1
281abb8f936SDemi Marie Obenour 	 * for value.  The first byte (tag) must be ASN1_INTEGER.
282abb8f936SDemi Marie Obenour 	 */
283abb8f936SDemi Marie Obenour 	if ((data_len < 3) || (*p != ASN1_INTEGER)) {
28448279d52SJuan Castillo 		/* Invalid ASN.1 integer */
28548279d52SJuan Castillo 		return 1;
28648279d52SJuan Castillo 	}
28748279d52SJuan Castillo 	p++;
28848279d52SJuan Castillo 
289abb8f936SDemi Marie Obenour 	/*
290abb8f936SDemi Marie Obenour 	 * NV-counters are unsigned integers up to 31 bits.  Trailing
291abb8f936SDemi Marie Obenour 	 * padding is not allowed.
292abb8f936SDemi Marie Obenour 	 */
293abb8f936SDemi Marie Obenour 	len = (unsigned int)*p;
294abb8f936SDemi Marie Obenour 	if ((len > 4) || (data_len - 2 != len)) {
29548279d52SJuan Castillo 		return 1;
29648279d52SJuan Castillo 	}
29748279d52SJuan Castillo 	p++;
29848279d52SJuan Castillo 
29948279d52SJuan Castillo 	/* Check the number is not negative */
30048279d52SJuan Castillo 	if (*p & 0x80) {
30148279d52SJuan Castillo 		return 1;
30248279d52SJuan Castillo 	}
30348279d52SJuan Castillo 
30448279d52SJuan Castillo 	/* Convert to unsigned int. This code is for a little-endian CPU */
305a2a5a945SManish V Badarkhe 	*cert_nv_ctr = 0;
30648279d52SJuan Castillo 	for (i = 0; i < len; i++) {
307a2a5a945SManish V Badarkhe 		*cert_nv_ctr = (*cert_nv_ctr << 8) | *p++;
30848279d52SJuan Castillo 	}
30948279d52SJuan Castillo 
31048279d52SJuan Castillo 	/* Get the counter from the platform */
31148279d52SJuan Castillo 	rc = plat_get_nv_ctr(param->plat_nv_ctr->cookie, &plat_nv_ctr);
31248279d52SJuan Castillo 	return_if_error(rc);
31348279d52SJuan Castillo 
314a2a5a945SManish V Badarkhe 	if (*cert_nv_ctr < plat_nv_ctr) {
31548279d52SJuan Castillo 		/* Invalid NV-counter */
31648279d52SJuan Castillo 		return 1;
317a2a5a945SManish V Badarkhe 	} else if (*cert_nv_ctr > plat_nv_ctr) {
318c0bfc88fSManish V Badarkhe #if PSA_FWU_SUPPORT && IMAGE_BL2
319c0bfc88fSManish V Badarkhe 		is_trial_run = fwu_is_trial_run_state();
320c0bfc88fSManish V Badarkhe #endif /* PSA_FWU_SUPPORT && IMAGE_BL2 */
321c0bfc88fSManish V Badarkhe 		*need_nv_ctr_upgrade = !is_trial_run;
32248279d52SJuan Castillo 	}
32348279d52SJuan Castillo 
32448279d52SJuan Castillo 	return 0;
32548279d52SJuan Castillo }
32648279d52SJuan Castillo 
327d35dee23Sdp-arm int plat_set_nv_ctr2(void *cookie, const auth_img_desc_t *img_desc __unused,
328d35dee23Sdp-arm 		unsigned int nv_ctr)
329d35dee23Sdp-arm {
330d35dee23Sdp-arm 	return plat_set_nv_ctr(cookie, nv_ctr);
331d35dee23Sdp-arm }
332d35dee23Sdp-arm 
33340f9f644SNicolas Toromanoff int plat_convert_pk(void *full_pk_ptr, unsigned int full_pk_len,
33440f9f644SNicolas Toromanoff 		    void **hashed_pk_ptr, unsigned int *hashed_pk_len)
33540f9f644SNicolas Toromanoff {
33640f9f644SNicolas Toromanoff 	*hashed_pk_ptr = full_pk_ptr;
33740f9f644SNicolas Toromanoff 	*hashed_pk_len = full_pk_len;
33840f9f644SNicolas Toromanoff 
33940f9f644SNicolas Toromanoff 	return 0;
34040f9f644SNicolas Toromanoff }
34140f9f644SNicolas Toromanoff 
34248279d52SJuan Castillo /*
34305799ae0SJuan Castillo  * Return the parent id in the output parameter '*parent_id'
34405799ae0SJuan Castillo  *
34505799ae0SJuan Castillo  * Return value:
34605799ae0SJuan Castillo  *   0 = Image has parent, 1 = Image has no parent or parent is authenticated
34705799ae0SJuan Castillo  */
34805799ae0SJuan Castillo int auth_mod_get_parent_id(unsigned int img_id, unsigned int *parent_id)
34905799ae0SJuan Castillo {
35005799ae0SJuan Castillo 	const auth_img_desc_t *img_desc = NULL;
35105799ae0SJuan Castillo 
35205799ae0SJuan Castillo 	assert(parent_id != NULL);
35305799ae0SJuan Castillo 	/* Get the image descriptor */
354ab1981dbSLouis Mayencourt 	img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
35505799ae0SJuan Castillo 
35605799ae0SJuan Castillo 	/* Check if the image has no parent (ROT) */
35705799ae0SJuan Castillo 	if (img_desc->parent == NULL) {
35805799ae0SJuan Castillo 		*parent_id = 0;
35905799ae0SJuan Castillo 		return 1;
36005799ae0SJuan Castillo 	}
36105799ae0SJuan Castillo 
36205799ae0SJuan Castillo 	/* Check if the parent has already been authenticated */
36305799ae0SJuan Castillo 	if (auth_img_flags[img_desc->parent->img_id] & IMG_FLAG_AUTHENTICATED) {
36405799ae0SJuan Castillo 		*parent_id = 0;
36505799ae0SJuan Castillo 		return 1;
36605799ae0SJuan Castillo 	}
36705799ae0SJuan Castillo 
36805799ae0SJuan Castillo 	*parent_id = img_desc->parent->img_id;
36905799ae0SJuan Castillo 	return 0;
37005799ae0SJuan Castillo }
37105799ae0SJuan Castillo 
37205799ae0SJuan Castillo /*
37305799ae0SJuan Castillo  * Initialize the different modules in the authentication framework
37405799ae0SJuan Castillo  */
37505799ae0SJuan Castillo void auth_mod_init(void)
37605799ae0SJuan Castillo {
37705799ae0SJuan Castillo 	/* Check we have a valid CoT registered */
37805799ae0SJuan Castillo 	assert(cot_desc_ptr != NULL);
37905799ae0SJuan Castillo 
38005799ae0SJuan Castillo 	/* Image parser module */
38105799ae0SJuan Castillo 	img_parser_init();
38205799ae0SJuan Castillo }
38305799ae0SJuan Castillo 
38405799ae0SJuan Castillo /*
38505799ae0SJuan Castillo  * Authenticate a certificate/image
38605799ae0SJuan Castillo  *
38705799ae0SJuan Castillo  * Return: 0 = success, Otherwise = error
38805799ae0SJuan Castillo  */
38905799ae0SJuan Castillo int auth_mod_verify_img(unsigned int img_id,
39005799ae0SJuan Castillo 			void *img_ptr,
39105799ae0SJuan Castillo 			unsigned int img_len)
39205799ae0SJuan Castillo {
39305799ae0SJuan Castillo 	const auth_img_desc_t *img_desc = NULL;
39405799ae0SJuan Castillo 	const auth_method_desc_t *auth_method = NULL;
39505799ae0SJuan Castillo 	void *param_ptr;
39605799ae0SJuan Castillo 	unsigned int param_len;
39705799ae0SJuan Castillo 	int rc, i;
398a2a5a945SManish V Badarkhe 	unsigned int cert_nv_ctr = 0;
399a2a5a945SManish V Badarkhe 	bool need_nv_ctr_upgrade = false;
400a2a5a945SManish V Badarkhe 	bool sig_auth_done = false;
401a2a5a945SManish V Badarkhe 	const auth_method_param_nv_ctr_t *nv_ctr_param = NULL;
40205799ae0SJuan Castillo 
40305799ae0SJuan Castillo 	/* Get the image descriptor from the chain of trust */
404ab1981dbSLouis Mayencourt 	img_desc = FCONF_GET_PROPERTY(tbbr, cot, img_id);
40505799ae0SJuan Castillo 
40605799ae0SJuan Castillo 	/* Ask the parser to check the image integrity */
40705799ae0SJuan Castillo 	rc = img_parser_check_integrity(img_desc->img_type, img_ptr, img_len);
40805799ae0SJuan Castillo 	return_if_error(rc);
40905799ae0SJuan Castillo 
41005799ae0SJuan Castillo 	/* Authenticate the image using the methods indicated in the image
41105799ae0SJuan Castillo 	 * descriptor. */
41230070427SJoel Hutton 	if (img_desc->img_auth_methods == NULL)
41330070427SJoel Hutton 		return 1;
41405799ae0SJuan Castillo 	for (i = 0 ; i < AUTH_METHOD_NUM ; i++) {
41505799ae0SJuan Castillo 		auth_method = &img_desc->img_auth_methods[i];
41605799ae0SJuan Castillo 		switch (auth_method->type) {
41705799ae0SJuan Castillo 		case AUTH_METHOD_NONE:
41805799ae0SJuan Castillo 			rc = 0;
41905799ae0SJuan Castillo 			break;
42005799ae0SJuan Castillo 		case AUTH_METHOD_HASH:
42105799ae0SJuan Castillo 			rc = auth_hash(&auth_method->param.hash,
42205799ae0SJuan Castillo 					img_desc, img_ptr, img_len);
42305799ae0SJuan Castillo 			break;
42405799ae0SJuan Castillo 		case AUTH_METHOD_SIG:
42505799ae0SJuan Castillo 			rc = auth_signature(&auth_method->param.sig,
42605799ae0SJuan Castillo 					img_desc, img_ptr, img_len);
427a2a5a945SManish V Badarkhe 			sig_auth_done = true;
42805799ae0SJuan Castillo 			break;
42948279d52SJuan Castillo 		case AUTH_METHOD_NV_CTR:
430a2a5a945SManish V Badarkhe 			nv_ctr_param = &auth_method->param.nv_ctr;
431a2a5a945SManish V Badarkhe 			rc = auth_nvctr(nv_ctr_param,
432a2a5a945SManish V Badarkhe 					img_desc, img_ptr, img_len,
433a2a5a945SManish V Badarkhe 					&cert_nv_ctr, &need_nv_ctr_upgrade);
43448279d52SJuan Castillo 			break;
43505799ae0SJuan Castillo 		default:
43605799ae0SJuan Castillo 			/* Unknown authentication method */
43705799ae0SJuan Castillo 			rc = 1;
43805799ae0SJuan Castillo 			break;
43905799ae0SJuan Castillo 		}
44005799ae0SJuan Castillo 		return_if_error(rc);
44105799ae0SJuan Castillo 	}
44205799ae0SJuan Castillo 
443a2a5a945SManish V Badarkhe 	/*
444a2a5a945SManish V Badarkhe 	 * Do platform NV counter upgrade only if the certificate gets
445a2a5a945SManish V Badarkhe 	 * authenticated, and platform NV-counter upgrade is needed.
446a2a5a945SManish V Badarkhe 	 */
447a2a5a945SManish V Badarkhe 	if (need_nv_ctr_upgrade && sig_auth_done) {
448a2a5a945SManish V Badarkhe 		rc = plat_set_nv_ctr2(nv_ctr_param->plat_nv_ctr->cookie,
449a2a5a945SManish V Badarkhe 				      img_desc, cert_nv_ctr);
450a2a5a945SManish V Badarkhe 		return_if_error(rc);
451a2a5a945SManish V Badarkhe 	}
452a2a5a945SManish V Badarkhe 
45305799ae0SJuan Castillo 	/* Extract the parameters indicated in the image descriptor to
45405799ae0SJuan Castillo 	 * authenticate the children images. */
45530070427SJoel Hutton 	if (img_desc->authenticated_data != NULL) {
45605799ae0SJuan Castillo 		for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
45705799ae0SJuan Castillo 			if (img_desc->authenticated_data[i].type_desc == NULL) {
45805799ae0SJuan Castillo 				continue;
45905799ae0SJuan Castillo 			}
46005799ae0SJuan Castillo 
46105799ae0SJuan Castillo 			/* Get the parameter from the image parser module */
46205799ae0SJuan Castillo 			rc = img_parser_get_auth_param(img_desc->img_type,
46305799ae0SJuan Castillo 					img_desc->authenticated_data[i].type_desc,
46405799ae0SJuan Castillo 					img_ptr, img_len, &param_ptr, &param_len);
46505799ae0SJuan Castillo 			return_if_error(rc);
46605799ae0SJuan Castillo 
46705799ae0SJuan Castillo 			/* Check parameter size */
46805799ae0SJuan Castillo 			if (param_len > img_desc->authenticated_data[i].data.len) {
46905799ae0SJuan Castillo 				return 1;
47005799ae0SJuan Castillo 			}
47105799ae0SJuan Castillo 
47205799ae0SJuan Castillo 			/* Copy the parameter for later use */
47305799ae0SJuan Castillo 			memcpy((void *)img_desc->authenticated_data[i].data.ptr,
47405799ae0SJuan Castillo 					(void *)param_ptr, param_len);
47505799ae0SJuan Castillo 		}
47630070427SJoel Hutton 	}
47705799ae0SJuan Castillo 
47805799ae0SJuan Castillo 	/* Mark image as authenticated */
47905799ae0SJuan Castillo 	auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED;
48005799ae0SJuan Castillo 
48105799ae0SJuan Castillo 	return 0;
48205799ae0SJuan Castillo }
483