xref: /rk3399_ARM-atf/drivers/auth/mbedtls/mbedtls_psa_crypto.c (revision 255ce97d609a93ab5528a653735abc46c2627e8f)
138f89369SManish V Badarkhe /*
238f89369SManish V Badarkhe  * Copyright (c) 2023, Arm Limited. All rights reserved.
338f89369SManish V Badarkhe  *
438f89369SManish V Badarkhe  * SPDX-License-Identifier: BSD-3-Clause
538f89369SManish V Badarkhe  */
638f89369SManish V Badarkhe 
738f89369SManish V Badarkhe #include <assert.h>
838f89369SManish V Badarkhe #include <stddef.h>
938f89369SManish V Badarkhe #include <string.h>
1038f89369SManish V Badarkhe 
1138f89369SManish V Badarkhe /* mbed TLS headers */
1238f89369SManish V Badarkhe #include <mbedtls/gcm.h>
1338f89369SManish V Badarkhe #include <mbedtls/md.h>
1438f89369SManish V Badarkhe #include <mbedtls/memory_buffer_alloc.h>
1538f89369SManish V Badarkhe #include <mbedtls/oid.h>
1638f89369SManish V Badarkhe #include <mbedtls/platform.h>
1738f89369SManish V Badarkhe #include <mbedtls/version.h>
1838f89369SManish V Badarkhe #include <mbedtls/x509.h>
194eaaaa19SManish V Badarkhe #include <psa/crypto.h>
204eaaaa19SManish V Badarkhe #include <psa/crypto_platform.h>
214eaaaa19SManish V Badarkhe #include <psa/crypto_types.h>
224eaaaa19SManish V Badarkhe #include <psa/crypto_values.h>
2338f89369SManish V Badarkhe 
2438f89369SManish V Badarkhe #include <common/debug.h>
2538f89369SManish V Badarkhe #include <drivers/auth/crypto_mod.h>
2638f89369SManish V Badarkhe #include <drivers/auth/mbedtls/mbedtls_common.h>
2738f89369SManish V Badarkhe #include <plat/common/platform.h>
2838f89369SManish V Badarkhe 
2938f89369SManish V Badarkhe #define LIB_NAME		"mbed TLS PSA"
3038f89369SManish V Badarkhe 
31*255ce97dSManish V Badarkhe /* Maximum length of R_S pair in the ECDSA signature in bytes */
32*255ce97dSManish V Badarkhe #define MAX_ECDSA_R_S_PAIR_LEN	64U
33*255ce97dSManish V Badarkhe 
34*255ce97dSManish V Badarkhe /* Size of ASN.1 length and tag in bytes*/
35*255ce97dSManish V Badarkhe #define SIZE_OF_ASN1_LEN	1U
36*255ce97dSManish V Badarkhe #define SIZE_OF_ASN1_TAG	1U
37*255ce97dSManish V Badarkhe 
3838f89369SManish V Badarkhe #if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
3938f89369SManish V Badarkhe CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
4038f89369SManish V Badarkhe /*
4138f89369SManish V Badarkhe  * CRYPTO_MD_MAX_SIZE value is as per current stronger algorithm available
4238f89369SManish V Badarkhe  * so make sure that mbed TLS MD maximum size must be lesser than this.
4338f89369SManish V Badarkhe  */
4438f89369SManish V Badarkhe CASSERT(CRYPTO_MD_MAX_SIZE >= MBEDTLS_MD_MAX_SIZE,
4538f89369SManish V Badarkhe 	assert_mbedtls_md_size_overflow);
4638f89369SManish V Badarkhe 
4738f89369SManish V Badarkhe #endif /*
4838f89369SManish V Badarkhe 	* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
4938f89369SManish V Badarkhe 	* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
5038f89369SManish V Badarkhe 	*/
5138f89369SManish V Badarkhe 
522ed061c4SManish V Badarkhe static inline psa_algorithm_t mbedtls_md_psa_alg_from_type(
532ed061c4SManish V Badarkhe 						mbedtls_md_type_t md_type)
542ed061c4SManish V Badarkhe {
552ed061c4SManish V Badarkhe 	assert((md_type == MBEDTLS_MD_SHA256) ||
562ed061c4SManish V Badarkhe 	       (md_type == MBEDTLS_MD_SHA384) ||
572ed061c4SManish V Badarkhe 	       (md_type == MBEDTLS_MD_SHA512));
582ed061c4SManish V Badarkhe 
592ed061c4SManish V Badarkhe 	return PSA_ALG_CATEGORY_HASH | (psa_algorithm_t) (md_type + 0x5);
602ed061c4SManish V Badarkhe }
612ed061c4SManish V Badarkhe 
6238f89369SManish V Badarkhe /*
6338f89369SManish V Badarkhe  * AlgorithmIdentifier  ::=  SEQUENCE  {
6438f89369SManish V Badarkhe  *     algorithm               OBJECT IDENTIFIER,
6538f89369SManish V Badarkhe  *     parameters              ANY DEFINED BY algorithm OPTIONAL
6638f89369SManish V Badarkhe  * }
6738f89369SManish V Badarkhe  *
6838f89369SManish V Badarkhe  * SubjectPublicKeyInfo  ::=  SEQUENCE  {
6938f89369SManish V Badarkhe  *     algorithm            AlgorithmIdentifier,
7038f89369SManish V Badarkhe  *     subjectPublicKey     BIT STRING
7138f89369SManish V Badarkhe  * }
7238f89369SManish V Badarkhe  *
7338f89369SManish V Badarkhe  * DigestInfo ::= SEQUENCE {
7438f89369SManish V Badarkhe  *     digestAlgorithm AlgorithmIdentifier,
7538f89369SManish V Badarkhe  *     digest OCTET STRING
7638f89369SManish V Badarkhe  * }
7738f89369SManish V Badarkhe  */
7838f89369SManish V Badarkhe 
7938f89369SManish V Badarkhe /*
804eaaaa19SManish V Badarkhe  * We pretend using an external RNG (through MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
814eaaaa19SManish V Badarkhe  * mbedTLS config option) so we need to provide an implementation of
824eaaaa19SManish V Badarkhe  * mbedtls_psa_external_get_random(). Provide a fake one, since we do not
834eaaaa19SManish V Badarkhe  * actually have any external RNG and TF-A itself doesn't engage in
844eaaaa19SManish V Badarkhe  * cryptographic operations that demands randomness.
854eaaaa19SManish V Badarkhe  */
864eaaaa19SManish V Badarkhe psa_status_t mbedtls_psa_external_get_random(
874eaaaa19SManish V Badarkhe 			mbedtls_psa_external_random_context_t *context,
884eaaaa19SManish V Badarkhe 			uint8_t *output, size_t output_size,
894eaaaa19SManish V Badarkhe 			size_t *output_length)
904eaaaa19SManish V Badarkhe {
914eaaaa19SManish V Badarkhe 	return PSA_ERROR_INSUFFICIENT_ENTROPY;
924eaaaa19SManish V Badarkhe }
934eaaaa19SManish V Badarkhe 
944eaaaa19SManish V Badarkhe /*
9538f89369SManish V Badarkhe  * Initialize the library and export the descriptor
9638f89369SManish V Badarkhe  */
9738f89369SManish V Badarkhe static void init(void)
9838f89369SManish V Badarkhe {
9938f89369SManish V Badarkhe 	/* Initialize mbed TLS */
10038f89369SManish V Badarkhe 	mbedtls_init();
1014eaaaa19SManish V Badarkhe 
1024eaaaa19SManish V Badarkhe 	/* Initialise PSA mbedTLS */
1034eaaaa19SManish V Badarkhe 	psa_status_t status = psa_crypto_init();
1044eaaaa19SManish V Badarkhe 
1054eaaaa19SManish V Badarkhe 	if (status != PSA_SUCCESS) {
1064eaaaa19SManish V Badarkhe 		ERROR("Failed to initialize %s crypto (%d).\n", LIB_NAME, status);
1074eaaaa19SManish V Badarkhe 		panic();
1084eaaaa19SManish V Badarkhe 	}
1094eaaaa19SManish V Badarkhe 
1104eaaaa19SManish V Badarkhe 	INFO("PSA crypto initialized successfully!\n");
11138f89369SManish V Badarkhe }
11238f89369SManish V Badarkhe 
11338f89369SManish V Badarkhe #if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
11438f89369SManish V Badarkhe CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
115eaa62e82SManish V Badarkhe 
116eaa62e82SManish V Badarkhe static void construct_psa_key_alg_and_type(mbedtls_pk_type_t pk_alg,
117eaa62e82SManish V Badarkhe 					   mbedtls_md_type_t md_alg,
118*255ce97dSManish V Badarkhe 					   psa_ecc_family_t psa_ecc_family,
119eaa62e82SManish V Badarkhe 					   psa_algorithm_t *psa_alg,
120eaa62e82SManish V Badarkhe 					   psa_key_type_t *psa_key_type)
121eaa62e82SManish V Badarkhe {
122eaa62e82SManish V Badarkhe 	psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
123eaa62e82SManish V Badarkhe 
124eaa62e82SManish V Badarkhe 	switch (pk_alg) {
125eaa62e82SManish V Badarkhe 	case MBEDTLS_PK_RSASSA_PSS:
126eaa62e82SManish V Badarkhe 		*psa_alg = PSA_ALG_RSA_PSS(psa_md_alg);
127eaa62e82SManish V Badarkhe 		*psa_key_type = PSA_KEY_TYPE_RSA_PUBLIC_KEY;
128eaa62e82SManish V Badarkhe 		break;
129*255ce97dSManish V Badarkhe 	case MBEDTLS_PK_ECDSA:
130*255ce97dSManish V Badarkhe 		*psa_alg = PSA_ALG_ECDSA(psa_md_alg);
131*255ce97dSManish V Badarkhe 		*psa_key_type = PSA_KEY_TYPE_ECC_PUBLIC_KEY(psa_ecc_family);
132*255ce97dSManish V Badarkhe 		break;
133eaa62e82SManish V Badarkhe 	default:
134eaa62e82SManish V Badarkhe 		*psa_alg = PSA_ALG_NONE;
135eaa62e82SManish V Badarkhe 		*psa_key_type = PSA_KEY_TYPE_NONE;
136eaa62e82SManish V Badarkhe 		break;
137eaa62e82SManish V Badarkhe 	}
138eaa62e82SManish V Badarkhe }
139eaa62e82SManish V Badarkhe 
140*255ce97dSManish V Badarkhe 
141*255ce97dSManish V Badarkhe #if TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA || \
142*255ce97dSManish V Badarkhe TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA
143*255ce97dSManish V Badarkhe 
144*255ce97dSManish V Badarkhe /*
145*255ce97dSManish V Badarkhe  * This is a helper function to detect padding byte (if the MSB bit of the
146*255ce97dSManish V Badarkhe  * first data byte is set to 1, for example 0x80) and on detection, ignore the
147*255ce97dSManish V Badarkhe  * padded byte(0x00) and increase the buffer pointer beyond padded byte and
148*255ce97dSManish V Badarkhe  * decrease the length of the buffer by 1.
149*255ce97dSManish V Badarkhe  *
150*255ce97dSManish V Badarkhe  * On Success returns 0, error otherwise.
151*255ce97dSManish V Badarkhe  **/
152*255ce97dSManish V Badarkhe static inline int ignore_asn1_int_padding_byte(unsigned char **buf_start,
153*255ce97dSManish V Badarkhe 					       size_t *buf_len)
154*255ce97dSManish V Badarkhe {
155*255ce97dSManish V Badarkhe 	unsigned char *local_buf = *buf_start;
156*255ce97dSManish V Badarkhe 
157*255ce97dSManish V Badarkhe 	/* Check for negative number */
158*255ce97dSManish V Badarkhe 	if ((local_buf[0] & 0x80U) != 0U) {
159*255ce97dSManish V Badarkhe 		return -1;
160*255ce97dSManish V Badarkhe 	}
161*255ce97dSManish V Badarkhe 
162*255ce97dSManish V Badarkhe 	if ((local_buf[0] == 0U) && (local_buf[1] > 0x7FU) &&
163*255ce97dSManish V Badarkhe 	    (*buf_len > 1U)) {
164*255ce97dSManish V Badarkhe 		*buf_start = &local_buf[1];
165*255ce97dSManish V Badarkhe 		(*buf_len)--;
166*255ce97dSManish V Badarkhe 	}
167*255ce97dSManish V Badarkhe 
168*255ce97dSManish V Badarkhe 	return 0;
169*255ce97dSManish V Badarkhe }
170*255ce97dSManish V Badarkhe 
171*255ce97dSManish V Badarkhe /*
172*255ce97dSManish V Badarkhe  * This is a helper function that gets a pointer to the encoded ECDSA publicKey
173*255ce97dSManish V Badarkhe  * and its length (as per RFC5280) and returns corresponding decoded publicKey
174*255ce97dSManish V Badarkhe  * and its length. As well, it retrieves the family of ECC key in the PSA
175*255ce97dSManish V Badarkhe  * format.
176*255ce97dSManish V Badarkhe  *
177*255ce97dSManish V Badarkhe  * This function returns error(CRYPTO_ERR_SIGNATURE) on ASN.1 parsing failure,
178*255ce97dSManish V Badarkhe  * otherwise success(0).
179*255ce97dSManish V Badarkhe  **/
180*255ce97dSManish V Badarkhe static int get_ecdsa_pkinfo_from_asn1(unsigned char **pk_start,
181*255ce97dSManish V Badarkhe 				      unsigned int *pk_len,
182*255ce97dSManish V Badarkhe 				      psa_ecc_family_t *psa_ecc_family)
183*255ce97dSManish V Badarkhe {
184*255ce97dSManish V Badarkhe 	mbedtls_asn1_buf alg_oid, alg_params;
185*255ce97dSManish V Badarkhe 	mbedtls_ecp_group_id grp_id;
186*255ce97dSManish V Badarkhe 	int rc;
187*255ce97dSManish V Badarkhe 	unsigned char *pk_end;
188*255ce97dSManish V Badarkhe 	size_t len;
189*255ce97dSManish V Badarkhe 	size_t curve_bits;
190*255ce97dSManish V Badarkhe 	unsigned char *pk_ptr = *pk_start;
191*255ce97dSManish V Badarkhe 
192*255ce97dSManish V Badarkhe 	pk_end = pk_ptr + *pk_len;
193*255ce97dSManish V Badarkhe 	rc = mbedtls_asn1_get_tag(&pk_ptr, pk_end, &len,
194*255ce97dSManish V Badarkhe 				  MBEDTLS_ASN1_CONSTRUCTED |
195*255ce97dSManish V Badarkhe 				  MBEDTLS_ASN1_SEQUENCE);
196*255ce97dSManish V Badarkhe 	if (rc != 0) {
197*255ce97dSManish V Badarkhe 		return CRYPTO_ERR_SIGNATURE;
198*255ce97dSManish V Badarkhe 	}
199*255ce97dSManish V Badarkhe 
200*255ce97dSManish V Badarkhe 	pk_end = pk_ptr + len;
201*255ce97dSManish V Badarkhe 	rc = mbedtls_asn1_get_alg(&pk_ptr, pk_end, &alg_oid, &alg_params);
202*255ce97dSManish V Badarkhe 	if (rc != 0) {
203*255ce97dSManish V Badarkhe 		return CRYPTO_ERR_SIGNATURE;
204*255ce97dSManish V Badarkhe 	}
205*255ce97dSManish V Badarkhe 
206*255ce97dSManish V Badarkhe 	if (alg_params.tag == MBEDTLS_ASN1_OID) {
207*255ce97dSManish V Badarkhe 		if (mbedtls_oid_get_ec_grp(&alg_params, &grp_id) != 0) {
208*255ce97dSManish V Badarkhe 			return CRYPTO_ERR_SIGNATURE;
209*255ce97dSManish V Badarkhe 		}
210*255ce97dSManish V Badarkhe 		*psa_ecc_family = mbedtls_ecc_group_to_psa(grp_id,
211*255ce97dSManish V Badarkhe 							   &curve_bits);
212*255ce97dSManish V Badarkhe 	} else {
213*255ce97dSManish V Badarkhe 		return CRYPTO_ERR_SIGNATURE;
214*255ce97dSManish V Badarkhe 	}
215*255ce97dSManish V Badarkhe 
216*255ce97dSManish V Badarkhe 	pk_end = pk_ptr + len - (alg_oid.len + alg_params.len +
217*255ce97dSManish V Badarkhe 		 2 * (SIZE_OF_ASN1_LEN + SIZE_OF_ASN1_TAG));
218*255ce97dSManish V Badarkhe 	rc = mbedtls_asn1_get_bitstring_null(&pk_ptr, pk_end, &len);
219*255ce97dSManish V Badarkhe 	if (rc != 0) {
220*255ce97dSManish V Badarkhe 		return CRYPTO_ERR_SIGNATURE;
221*255ce97dSManish V Badarkhe 	}
222*255ce97dSManish V Badarkhe 
223*255ce97dSManish V Badarkhe 	*pk_start = pk_ptr;
224*255ce97dSManish V Badarkhe 	*pk_len = len;
225*255ce97dSManish V Badarkhe 
226*255ce97dSManish V Badarkhe 	return rc;
227*255ce97dSManish V Badarkhe }
228*255ce97dSManish V Badarkhe 
229*255ce97dSManish V Badarkhe /*
230*255ce97dSManish V Badarkhe  * Ecdsa-Sig-Value  ::=  SEQUENCE  {
231*255ce97dSManish V Badarkhe  *   r     INTEGER,
232*255ce97dSManish V Badarkhe  *   s     INTEGER
233*255ce97dSManish V Badarkhe  * }
234*255ce97dSManish V Badarkhe  *
235*255ce97dSManish V Badarkhe  * This helper function that gets a pointer to the encoded ECDSA signature and
236*255ce97dSManish V Badarkhe  * its length (as per RFC5280) and returns corresponding decoded signature
237*255ce97dSManish V Badarkhe  * (R_S pair) and its size.
238*255ce97dSManish V Badarkhe  *
239*255ce97dSManish V Badarkhe  * This function returns error(CRYPTO_ERR_SIGNATURE) on ASN.1 parsing failure,
240*255ce97dSManish V Badarkhe  * otherwise success(0).
241*255ce97dSManish V Badarkhe  **/
242*255ce97dSManish V Badarkhe static int get_ecdsa_signature_from_asn1(unsigned char *sig_ptr,
243*255ce97dSManish V Badarkhe 					 size_t *sig_len,
244*255ce97dSManish V Badarkhe 					 unsigned char *r_s_pair)
245*255ce97dSManish V Badarkhe {
246*255ce97dSManish V Badarkhe 	int rc;
247*255ce97dSManish V Badarkhe 	unsigned char *sig_end;
248*255ce97dSManish V Badarkhe 	size_t len, r_len, s_len;
249*255ce97dSManish V Badarkhe 
250*255ce97dSManish V Badarkhe 	sig_end = sig_ptr + *sig_len;
251*255ce97dSManish V Badarkhe 	rc = mbedtls_asn1_get_tag(&sig_ptr, sig_end, &len,
252*255ce97dSManish V Badarkhe 				  MBEDTLS_ASN1_CONSTRUCTED |
253*255ce97dSManish V Badarkhe 				  MBEDTLS_ASN1_SEQUENCE);
254*255ce97dSManish V Badarkhe 	if (rc != 0) {
255*255ce97dSManish V Badarkhe 		return CRYPTO_ERR_SIGNATURE;
256*255ce97dSManish V Badarkhe 	}
257*255ce97dSManish V Badarkhe 
258*255ce97dSManish V Badarkhe 	sig_end = sig_ptr + len;
259*255ce97dSManish V Badarkhe 	rc = mbedtls_asn1_get_tag(&sig_ptr, sig_end, &r_len,
260*255ce97dSManish V Badarkhe 				  MBEDTLS_ASN1_INTEGER);
261*255ce97dSManish V Badarkhe 	if (rc != 0) {
262*255ce97dSManish V Badarkhe 		return CRYPTO_ERR_SIGNATURE;
263*255ce97dSManish V Badarkhe 	}
264*255ce97dSManish V Badarkhe 
265*255ce97dSManish V Badarkhe 	if (ignore_asn1_int_padding_byte(&sig_ptr, &r_len) != 0) {
266*255ce97dSManish V Badarkhe 		return CRYPTO_ERR_SIGNATURE;
267*255ce97dSManish V Badarkhe 	}
268*255ce97dSManish V Badarkhe 
269*255ce97dSManish V Badarkhe 	(void)memcpy((void *)&r_s_pair[0], (const void *)sig_ptr, r_len);
270*255ce97dSManish V Badarkhe 
271*255ce97dSManish V Badarkhe 	sig_ptr = sig_ptr + r_len;
272*255ce97dSManish V Badarkhe 	sig_end = sig_ptr + len - (r_len + (SIZE_OF_ASN1_LEN +
273*255ce97dSManish V Badarkhe 		  SIZE_OF_ASN1_TAG));
274*255ce97dSManish V Badarkhe 	rc = mbedtls_asn1_get_tag(&sig_ptr, sig_end, &s_len,
275*255ce97dSManish V Badarkhe 				  MBEDTLS_ASN1_INTEGER);
276*255ce97dSManish V Badarkhe 	if (rc != 0) {
277*255ce97dSManish V Badarkhe 		return CRYPTO_ERR_SIGNATURE;
278*255ce97dSManish V Badarkhe 	}
279*255ce97dSManish V Badarkhe 
280*255ce97dSManish V Badarkhe 	if (ignore_asn1_int_padding_byte(&sig_ptr, &s_len) != 0) {
281*255ce97dSManish V Badarkhe 		return CRYPTO_ERR_SIGNATURE;
282*255ce97dSManish V Badarkhe 	}
283*255ce97dSManish V Badarkhe 
284*255ce97dSManish V Badarkhe 	(void)memcpy((void *)&r_s_pair[r_len], (const void *)sig_ptr, s_len);
285*255ce97dSManish V Badarkhe 
286*255ce97dSManish V Badarkhe 	*sig_len = s_len + r_len;
287*255ce97dSManish V Badarkhe 
288*255ce97dSManish V Badarkhe 	return 0;
289*255ce97dSManish V Badarkhe }
290*255ce97dSManish V Badarkhe #endif /*
291*255ce97dSManish V Badarkhe 	* TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA || \
292*255ce97dSManish V Badarkhe 	* TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA
293*255ce97dSManish V Badarkhe 	**/
294*255ce97dSManish V Badarkhe 
29538f89369SManish V Badarkhe /*
29638f89369SManish V Badarkhe  * Verify a signature.
29738f89369SManish V Badarkhe  *
29838f89369SManish V Badarkhe  * Parameters are passed using the DER encoding format following the ASN.1
29938f89369SManish V Badarkhe  * structures detailed above.
30038f89369SManish V Badarkhe  */
30138f89369SManish V Badarkhe static int verify_signature(void *data_ptr, unsigned int data_len,
30238f89369SManish V Badarkhe 			    void *sig_ptr, unsigned int sig_len,
30338f89369SManish V Badarkhe 			    void *sig_alg, unsigned int sig_alg_len,
30438f89369SManish V Badarkhe 			    void *pk_ptr, unsigned int pk_len)
30538f89369SManish V Badarkhe {
30638f89369SManish V Badarkhe 	mbedtls_asn1_buf sig_oid, sig_params;
30738f89369SManish V Badarkhe 	mbedtls_asn1_buf signature;
30838f89369SManish V Badarkhe 	mbedtls_md_type_t md_alg;
30938f89369SManish V Badarkhe 	mbedtls_pk_type_t pk_alg;
31038f89369SManish V Badarkhe 	int rc;
31138f89369SManish V Badarkhe 	void *sig_opts = NULL;
31238f89369SManish V Badarkhe 	unsigned char *p, *end;
313*255ce97dSManish V Badarkhe 	unsigned char *local_sig_ptr;
314*255ce97dSManish V Badarkhe 	size_t local_sig_len;
315*255ce97dSManish V Badarkhe 	psa_ecc_family_t psa_ecc_family = 0U;
316*255ce97dSManish V Badarkhe 	__unused unsigned char reformatted_sig[MAX_ECDSA_R_S_PAIR_LEN] = {0};
317eaa62e82SManish V Badarkhe 
318eaa62e82SManish V Badarkhe 	/* construct PSA key algo and type */
319eaa62e82SManish V Badarkhe 	psa_status_t status = PSA_SUCCESS;
320eaa62e82SManish V Badarkhe 	psa_key_attributes_t psa_key_attr = PSA_KEY_ATTRIBUTES_INIT;
321eaa62e82SManish V Badarkhe 	psa_key_id_t psa_key_id = PSA_KEY_ID_NULL;
322eaa62e82SManish V Badarkhe 	psa_key_type_t psa_key_type;
323eaa62e82SManish V Badarkhe 	psa_algorithm_t psa_alg;
32438f89369SManish V Badarkhe 
32538f89369SManish V Badarkhe 	/* Get pointers to signature OID and parameters */
32638f89369SManish V Badarkhe 	p = (unsigned char *)sig_alg;
32738f89369SManish V Badarkhe 	end = (unsigned char *)(p + sig_alg_len);
32838f89369SManish V Badarkhe 	rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, &sig_params);
32938f89369SManish V Badarkhe 	if (rc != 0) {
33038f89369SManish V Badarkhe 		return CRYPTO_ERR_SIGNATURE;
33138f89369SManish V Badarkhe 	}
33238f89369SManish V Badarkhe 
33338f89369SManish V Badarkhe 	/* Get the actual signature algorithm (MD + PK) */
33438f89369SManish V Badarkhe 	rc = mbedtls_x509_get_sig_alg(&sig_oid, &sig_params, &md_alg, &pk_alg, &sig_opts);
33538f89369SManish V Badarkhe 	if (rc != 0) {
33638f89369SManish V Badarkhe 		return CRYPTO_ERR_SIGNATURE;
33738f89369SManish V Badarkhe 	}
33838f89369SManish V Badarkhe 
33938f89369SManish V Badarkhe 	/* Get the signature (bitstring) */
34038f89369SManish V Badarkhe 	p = (unsigned char *)sig_ptr;
34138f89369SManish V Badarkhe 	end = (unsigned char *)(p + sig_len);
34238f89369SManish V Badarkhe 	signature.tag = *p;
34338f89369SManish V Badarkhe 	rc = mbedtls_asn1_get_bitstring_null(&p, end, &signature.len);
34438f89369SManish V Badarkhe 	if ((rc != 0) || ((size_t)(end - p) != signature.len)) {
34538f89369SManish V Badarkhe 		rc = CRYPTO_ERR_SIGNATURE;
346eaa62e82SManish V Badarkhe 		goto end2;
34738f89369SManish V Badarkhe 	}
348*255ce97dSManish V Badarkhe 
349*255ce97dSManish V Badarkhe 	local_sig_ptr = p;
350*255ce97dSManish V Badarkhe 	local_sig_len = signature.len;
351*255ce97dSManish V Badarkhe 
352*255ce97dSManish V Badarkhe #if TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA || \
353*255ce97dSManish V Badarkhe TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA
354*255ce97dSManish V Badarkhe 	if (pk_alg == MBEDTLS_PK_ECDSA) {
355*255ce97dSManish V Badarkhe 		rc = get_ecdsa_signature_from_asn1(local_sig_ptr,
356*255ce97dSManish V Badarkhe 						   &local_sig_len,
357*255ce97dSManish V Badarkhe 						   reformatted_sig);
358*255ce97dSManish V Badarkhe 		if (rc != 0) {
359*255ce97dSManish V Badarkhe 			goto end2;
360*255ce97dSManish V Badarkhe 		}
361*255ce97dSManish V Badarkhe 
362*255ce97dSManish V Badarkhe 		local_sig_ptr = reformatted_sig;
363*255ce97dSManish V Badarkhe 
364*255ce97dSManish V Badarkhe 		rc = get_ecdsa_pkinfo_from_asn1((unsigned char **)&pk_ptr,
365*255ce97dSManish V Badarkhe 						&pk_len,
366*255ce97dSManish V Badarkhe 						&psa_ecc_family);
367*255ce97dSManish V Badarkhe 		if (rc != 0) {
368*255ce97dSManish V Badarkhe 			goto end2;
369*255ce97dSManish V Badarkhe 		}
370*255ce97dSManish V Badarkhe 	}
371*255ce97dSManish V Badarkhe #endif /*
372*255ce97dSManish V Badarkhe 	* TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA || \
373*255ce97dSManish V Badarkhe 	* TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA
374*255ce97dSManish V Badarkhe 	**/
37538f89369SManish V Badarkhe 
376eaa62e82SManish V Badarkhe 	/* Convert this pk_alg and md_alg to PSA key type and key algorithm */
377*255ce97dSManish V Badarkhe 	construct_psa_key_alg_and_type(pk_alg, md_alg, psa_ecc_family,
378eaa62e82SManish V Badarkhe 				       &psa_alg, &psa_key_type);
379eaa62e82SManish V Badarkhe 
380eaa62e82SManish V Badarkhe 
381eaa62e82SManish V Badarkhe 	if ((psa_alg == PSA_ALG_NONE) || (psa_key_type == PSA_KEY_TYPE_NONE)) {
38238f89369SManish V Badarkhe 		rc = CRYPTO_ERR_SIGNATURE;
383eaa62e82SManish V Badarkhe 		goto end2;
38438f89369SManish V Badarkhe 	}
38538f89369SManish V Badarkhe 
386eaa62e82SManish V Badarkhe 	/* filled-in key_attributes */
387eaa62e82SManish V Badarkhe 	psa_set_key_algorithm(&psa_key_attr, psa_alg);
388eaa62e82SManish V Badarkhe 	psa_set_key_type(&psa_key_attr, psa_key_type);
389eaa62e82SManish V Badarkhe 	psa_set_key_usage_flags(&psa_key_attr, PSA_KEY_USAGE_VERIFY_MESSAGE);
390eaa62e82SManish V Badarkhe 
391eaa62e82SManish V Badarkhe 	/* Get the key_id using import API */
392eaa62e82SManish V Badarkhe 	status = psa_import_key(&psa_key_attr,
393eaa62e82SManish V Badarkhe 				pk_ptr,
394eaa62e82SManish V Badarkhe 				(size_t)pk_len,
395eaa62e82SManish V Badarkhe 				&psa_key_id);
396eaa62e82SManish V Badarkhe 
397eaa62e82SManish V Badarkhe 	if (status != PSA_SUCCESS) {
398eaa62e82SManish V Badarkhe 		rc = CRYPTO_ERR_SIGNATURE;
399eaa62e82SManish V Badarkhe 		goto end2;
400eaa62e82SManish V Badarkhe 	}
401eaa62e82SManish V Badarkhe 
402eaa62e82SManish V Badarkhe 	/*
403eaa62e82SManish V Badarkhe 	 * Hash calculation and Signature verification of the given data payload
404eaa62e82SManish V Badarkhe 	 * is wrapped under the psa_verify_message function.
405eaa62e82SManish V Badarkhe 	 */
406eaa62e82SManish V Badarkhe 	status = psa_verify_message(psa_key_id, psa_alg,
407eaa62e82SManish V Badarkhe 				    data_ptr, data_len,
408*255ce97dSManish V Badarkhe 				    local_sig_ptr, local_sig_len);
409eaa62e82SManish V Badarkhe 
410eaa62e82SManish V Badarkhe 	if (status != PSA_SUCCESS) {
41138f89369SManish V Badarkhe 		rc = CRYPTO_ERR_SIGNATURE;
41238f89369SManish V Badarkhe 		goto end1;
41338f89369SManish V Badarkhe 	}
41438f89369SManish V Badarkhe 
41538f89369SManish V Badarkhe 	/* Signature verification success */
41638f89369SManish V Badarkhe 	rc = CRYPTO_SUCCESS;
41738f89369SManish V Badarkhe 
41838f89369SManish V Badarkhe end1:
419eaa62e82SManish V Badarkhe 	/*
420eaa62e82SManish V Badarkhe 	 * Destroy the key if it is created successfully
421eaa62e82SManish V Badarkhe 	 */
422eaa62e82SManish V Badarkhe 	psa_destroy_key(psa_key_id);
42338f89369SManish V Badarkhe end2:
42438f89369SManish V Badarkhe 	mbedtls_free(sig_opts);
42538f89369SManish V Badarkhe 	return rc;
42638f89369SManish V Badarkhe }
42738f89369SManish V Badarkhe 
42838f89369SManish V Badarkhe /*
42938f89369SManish V Badarkhe  * Match a hash
43038f89369SManish V Badarkhe  *
43138f89369SManish V Badarkhe  * Digest info is passed in DER format following the ASN.1 structure detailed
43238f89369SManish V Badarkhe  * above.
43338f89369SManish V Badarkhe  */
43438f89369SManish V Badarkhe static int verify_hash(void *data_ptr, unsigned int data_len,
43538f89369SManish V Badarkhe 		       void *digest_info_ptr, unsigned int digest_info_len)
43638f89369SManish V Badarkhe {
43738f89369SManish V Badarkhe 	mbedtls_asn1_buf hash_oid, params;
43838f89369SManish V Badarkhe 	mbedtls_md_type_t md_alg;
43938f89369SManish V Badarkhe 	unsigned char *p, *end, *hash;
44038f89369SManish V Badarkhe 	size_t len;
44138f89369SManish V Badarkhe 	int rc;
4422ed061c4SManish V Badarkhe 	psa_status_t status;
4432ed061c4SManish V Badarkhe 	psa_algorithm_t psa_md_alg;
44438f89369SManish V Badarkhe 
44538f89369SManish V Badarkhe 	/*
44638f89369SManish V Badarkhe 	 * Digest info should be an MBEDTLS_ASN1_SEQUENCE, but padding after
44738f89369SManish V Badarkhe 	 * it is allowed.  This is necessary to support multiple hash
44838f89369SManish V Badarkhe 	 * algorithms.
44938f89369SManish V Badarkhe 	 */
45038f89369SManish V Badarkhe 	p = (unsigned char *)digest_info_ptr;
45138f89369SManish V Badarkhe 	end = p + digest_info_len;
45238f89369SManish V Badarkhe 	rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
45338f89369SManish V Badarkhe 				  MBEDTLS_ASN1_SEQUENCE);
45438f89369SManish V Badarkhe 	if (rc != 0) {
45538f89369SManish V Badarkhe 		return CRYPTO_ERR_HASH;
45638f89369SManish V Badarkhe 	}
45738f89369SManish V Badarkhe 
45838f89369SManish V Badarkhe 	end = p + len;
45938f89369SManish V Badarkhe 
46038f89369SManish V Badarkhe 	/* Get the hash algorithm */
46138f89369SManish V Badarkhe 	rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, &params);
46238f89369SManish V Badarkhe 	if (rc != 0) {
46338f89369SManish V Badarkhe 		return CRYPTO_ERR_HASH;
46438f89369SManish V Badarkhe 	}
46538f89369SManish V Badarkhe 
46638f89369SManish V Badarkhe 	/* Hash should be octet string type and consume all bytes */
46738f89369SManish V Badarkhe 	rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
46838f89369SManish V Badarkhe 	if ((rc != 0) || ((size_t)(end - p) != len)) {
46938f89369SManish V Badarkhe 		return CRYPTO_ERR_HASH;
47038f89369SManish V Badarkhe 	}
47138f89369SManish V Badarkhe 	hash = p;
47238f89369SManish V Badarkhe 
4732ed061c4SManish V Badarkhe 	rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg);
47438f89369SManish V Badarkhe 	if (rc != 0) {
47538f89369SManish V Badarkhe 		return CRYPTO_ERR_HASH;
47638f89369SManish V Badarkhe 	}
47738f89369SManish V Badarkhe 
4782ed061c4SManish V Badarkhe 	/* convert the md_alg to psa_algo */
4792ed061c4SManish V Badarkhe 	psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
4802ed061c4SManish V Badarkhe 
4812ed061c4SManish V Badarkhe 	/* Length of hash must match the algorithm's size */
4822ed061c4SManish V Badarkhe 	if (len != PSA_HASH_LENGTH(psa_md_alg)) {
4832ed061c4SManish V Badarkhe 		return CRYPTO_ERR_HASH;
4842ed061c4SManish V Badarkhe 	}
4852ed061c4SManish V Badarkhe 
4862ed061c4SManish V Badarkhe 	/*
4872ed061c4SManish V Badarkhe 	 * Calculate Hash and compare it against the retrieved hash from
4882ed061c4SManish V Badarkhe 	 * the certificate (one shot API).
4892ed061c4SManish V Badarkhe 	 */
4902ed061c4SManish V Badarkhe 	status = psa_hash_compare(psa_md_alg,
4912ed061c4SManish V Badarkhe 				  data_ptr, (size_t)data_len,
4922ed061c4SManish V Badarkhe 				  (const uint8_t *)hash, len);
4932ed061c4SManish V Badarkhe 
4942ed061c4SManish V Badarkhe 	if (status != PSA_SUCCESS) {
49538f89369SManish V Badarkhe 		return CRYPTO_ERR_HASH;
49638f89369SManish V Badarkhe 	}
49738f89369SManish V Badarkhe 
49838f89369SManish V Badarkhe 	return CRYPTO_SUCCESS;
49938f89369SManish V Badarkhe }
50038f89369SManish V Badarkhe #endif /*
50138f89369SManish V Badarkhe 	* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
50238f89369SManish V Badarkhe 	* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
50338f89369SManish V Badarkhe 	*/
50438f89369SManish V Badarkhe 
50538f89369SManish V Badarkhe #if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
50638f89369SManish V Badarkhe CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
50738f89369SManish V Badarkhe /*
50838f89369SManish V Badarkhe  * Map a generic crypto message digest algorithm to the corresponding macro used
50938f89369SManish V Badarkhe  * by Mbed TLS.
51038f89369SManish V Badarkhe  */
51138f89369SManish V Badarkhe static inline mbedtls_md_type_t md_type(enum crypto_md_algo algo)
51238f89369SManish V Badarkhe {
51338f89369SManish V Badarkhe 	switch (algo) {
51438f89369SManish V Badarkhe 	case CRYPTO_MD_SHA512:
51538f89369SManish V Badarkhe 		return MBEDTLS_MD_SHA512;
51638f89369SManish V Badarkhe 	case CRYPTO_MD_SHA384:
51738f89369SManish V Badarkhe 		return MBEDTLS_MD_SHA384;
51838f89369SManish V Badarkhe 	case CRYPTO_MD_SHA256:
51938f89369SManish V Badarkhe 		return MBEDTLS_MD_SHA256;
52038f89369SManish V Badarkhe 	default:
52138f89369SManish V Badarkhe 		/* Invalid hash algorithm. */
52238f89369SManish V Badarkhe 		return MBEDTLS_MD_NONE;
52338f89369SManish V Badarkhe 	}
52438f89369SManish V Badarkhe }
52538f89369SManish V Badarkhe 
52638f89369SManish V Badarkhe /*
52738f89369SManish V Badarkhe  * Calculate a hash
52838f89369SManish V Badarkhe  *
52938f89369SManish V Badarkhe  * output points to the computed hash
53038f89369SManish V Badarkhe  */
53138f89369SManish V Badarkhe static int calc_hash(enum crypto_md_algo md_algo, void *data_ptr,
53238f89369SManish V Badarkhe 		     unsigned int data_len,
53338f89369SManish V Badarkhe 		     unsigned char output[CRYPTO_MD_MAX_SIZE])
53438f89369SManish V Badarkhe {
535484b5869SManish V Badarkhe 	size_t hash_length;
536484b5869SManish V Badarkhe 	psa_status_t status;
537484b5869SManish V Badarkhe 	psa_algorithm_t psa_md_alg;
53838f89369SManish V Badarkhe 
539484b5869SManish V Badarkhe 	/* convert the md_alg to psa_algo */
540484b5869SManish V Badarkhe 	psa_md_alg = mbedtls_md_psa_alg_from_type(md_type(md_algo));
54138f89369SManish V Badarkhe 
54238f89369SManish V Badarkhe 	/*
54338f89369SManish V Badarkhe 	 * Calculate the hash of the data, it is safe to pass the
54438f89369SManish V Badarkhe 	 * 'output' hash buffer pointer considering its size is always
54538f89369SManish V Badarkhe 	 * bigger than or equal to MBEDTLS_MD_MAX_SIZE.
54638f89369SManish V Badarkhe 	 */
547484b5869SManish V Badarkhe 	status = psa_hash_compute(psa_md_alg, data_ptr, (size_t)data_len,
548484b5869SManish V Badarkhe 				  (uint8_t *)output, CRYPTO_MD_MAX_SIZE,
549484b5869SManish V Badarkhe 				  &hash_length);
550484b5869SManish V Badarkhe 	if (status != PSA_SUCCESS) {
551484b5869SManish V Badarkhe 		return CRYPTO_ERR_HASH;
552484b5869SManish V Badarkhe 	}
553484b5869SManish V Badarkhe 
554484b5869SManish V Badarkhe 	return CRYPTO_SUCCESS;
55538f89369SManish V Badarkhe }
55638f89369SManish V Badarkhe #endif /*
55738f89369SManish V Badarkhe 	* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
55838f89369SManish V Badarkhe 	* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
55938f89369SManish V Badarkhe 	*/
56038f89369SManish V Badarkhe 
56138f89369SManish V Badarkhe #if TF_MBEDTLS_USE_AES_GCM
56238f89369SManish V Badarkhe /*
56338f89369SManish V Badarkhe  * Stack based buffer allocation for decryption operation. It could
56438f89369SManish V Badarkhe  * be configured to balance stack usage vs execution speed.
56538f89369SManish V Badarkhe  */
56638f89369SManish V Badarkhe #define DEC_OP_BUF_SIZE		128
56738f89369SManish V Badarkhe 
56838f89369SManish V Badarkhe static int aes_gcm_decrypt(void *data_ptr, size_t len, const void *key,
56938f89369SManish V Badarkhe 			   unsigned int key_len, const void *iv,
57038f89369SManish V Badarkhe 			   unsigned int iv_len, const void *tag,
57138f89369SManish V Badarkhe 			   unsigned int tag_len)
57238f89369SManish V Badarkhe {
57338f89369SManish V Badarkhe 	mbedtls_gcm_context ctx;
57438f89369SManish V Badarkhe 	mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
57538f89369SManish V Badarkhe 	unsigned char buf[DEC_OP_BUF_SIZE];
57638f89369SManish V Badarkhe 	unsigned char tag_buf[CRYPTO_MAX_TAG_SIZE];
57738f89369SManish V Badarkhe 	unsigned char *pt = data_ptr;
57838f89369SManish V Badarkhe 	size_t dec_len;
57938f89369SManish V Badarkhe 	int diff, i, rc;
58038f89369SManish V Badarkhe 	size_t output_length __unused;
58138f89369SManish V Badarkhe 
58238f89369SManish V Badarkhe 	mbedtls_gcm_init(&ctx);
58338f89369SManish V Badarkhe 
58438f89369SManish V Badarkhe 	rc = mbedtls_gcm_setkey(&ctx, cipher, key, key_len * 8);
58538f89369SManish V Badarkhe 	if (rc != 0) {
58638f89369SManish V Badarkhe 		rc = CRYPTO_ERR_DECRYPTION;
58738f89369SManish V Badarkhe 		goto exit_gcm;
58838f89369SManish V Badarkhe 	}
58938f89369SManish V Badarkhe 
59038f89369SManish V Badarkhe #if (MBEDTLS_VERSION_MAJOR < 3)
59138f89369SManish V Badarkhe 	rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len, NULL, 0);
59238f89369SManish V Badarkhe #else
59338f89369SManish V Badarkhe 	rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len);
59438f89369SManish V Badarkhe #endif
59538f89369SManish V Badarkhe 	if (rc != 0) {
59638f89369SManish V Badarkhe 		rc = CRYPTO_ERR_DECRYPTION;
59738f89369SManish V Badarkhe 		goto exit_gcm;
59838f89369SManish V Badarkhe 	}
59938f89369SManish V Badarkhe 
60038f89369SManish V Badarkhe 	while (len > 0) {
60138f89369SManish V Badarkhe 		dec_len = MIN(sizeof(buf), len);
60238f89369SManish V Badarkhe 
60338f89369SManish V Badarkhe #if (MBEDTLS_VERSION_MAJOR < 3)
60438f89369SManish V Badarkhe 		rc = mbedtls_gcm_update(&ctx, dec_len, pt, buf);
60538f89369SManish V Badarkhe #else
60638f89369SManish V Badarkhe 		rc = mbedtls_gcm_update(&ctx, pt, dec_len, buf, sizeof(buf), &output_length);
60738f89369SManish V Badarkhe #endif
60838f89369SManish V Badarkhe 
60938f89369SManish V Badarkhe 		if (rc != 0) {
61038f89369SManish V Badarkhe 			rc = CRYPTO_ERR_DECRYPTION;
61138f89369SManish V Badarkhe 			goto exit_gcm;
61238f89369SManish V Badarkhe 		}
61338f89369SManish V Badarkhe 
61438f89369SManish V Badarkhe 		memcpy(pt, buf, dec_len);
61538f89369SManish V Badarkhe 		pt += dec_len;
61638f89369SManish V Badarkhe 		len -= dec_len;
61738f89369SManish V Badarkhe 	}
61838f89369SManish V Badarkhe 
61938f89369SManish V Badarkhe #if (MBEDTLS_VERSION_MAJOR < 3)
62038f89369SManish V Badarkhe 	rc = mbedtls_gcm_finish(&ctx, tag_buf, sizeof(tag_buf));
62138f89369SManish V Badarkhe #else
62238f89369SManish V Badarkhe 	rc = mbedtls_gcm_finish(&ctx, NULL, 0, &output_length, tag_buf, sizeof(tag_buf));
62338f89369SManish V Badarkhe #endif
62438f89369SManish V Badarkhe 
62538f89369SManish V Badarkhe 	if (rc != 0) {
62638f89369SManish V Badarkhe 		rc = CRYPTO_ERR_DECRYPTION;
62738f89369SManish V Badarkhe 		goto exit_gcm;
62838f89369SManish V Badarkhe 	}
62938f89369SManish V Badarkhe 
63038f89369SManish V Badarkhe 	/* Check tag in "constant-time" */
63138f89369SManish V Badarkhe 	for (diff = 0, i = 0; i < tag_len; i++)
63238f89369SManish V Badarkhe 		diff |= ((const unsigned char *)tag)[i] ^ tag_buf[i];
63338f89369SManish V Badarkhe 
63438f89369SManish V Badarkhe 	if (diff != 0) {
63538f89369SManish V Badarkhe 		rc = CRYPTO_ERR_DECRYPTION;
63638f89369SManish V Badarkhe 		goto exit_gcm;
63738f89369SManish V Badarkhe 	}
63838f89369SManish V Badarkhe 
63938f89369SManish V Badarkhe 	/* GCM decryption success */
64038f89369SManish V Badarkhe 	rc = CRYPTO_SUCCESS;
64138f89369SManish V Badarkhe 
64238f89369SManish V Badarkhe exit_gcm:
64338f89369SManish V Badarkhe 	mbedtls_gcm_free(&ctx);
64438f89369SManish V Badarkhe 	return rc;
64538f89369SManish V Badarkhe }
64638f89369SManish V Badarkhe 
64738f89369SManish V Badarkhe /*
64838f89369SManish V Badarkhe  * Authenticated decryption of an image
64938f89369SManish V Badarkhe  */
65038f89369SManish V Badarkhe static int auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
65138f89369SManish V Badarkhe 			size_t len, const void *key, unsigned int key_len,
65238f89369SManish V Badarkhe 			unsigned int key_flags, const void *iv,
65338f89369SManish V Badarkhe 			unsigned int iv_len, const void *tag,
65438f89369SManish V Badarkhe 			unsigned int tag_len)
65538f89369SManish V Badarkhe {
65638f89369SManish V Badarkhe 	int rc;
65738f89369SManish V Badarkhe 
65838f89369SManish V Badarkhe 	assert((key_flags & ENC_KEY_IS_IDENTIFIER) == 0);
65938f89369SManish V Badarkhe 
66038f89369SManish V Badarkhe 	switch (dec_algo) {
66138f89369SManish V Badarkhe 	case CRYPTO_GCM_DECRYPT:
66238f89369SManish V Badarkhe 		rc = aes_gcm_decrypt(data_ptr, len, key, key_len, iv, iv_len,
66338f89369SManish V Badarkhe 				     tag, tag_len);
66438f89369SManish V Badarkhe 		if (rc != 0)
66538f89369SManish V Badarkhe 			return rc;
66638f89369SManish V Badarkhe 		break;
66738f89369SManish V Badarkhe 	default:
66838f89369SManish V Badarkhe 		return CRYPTO_ERR_DECRYPTION;
66938f89369SManish V Badarkhe 	}
67038f89369SManish V Badarkhe 
67138f89369SManish V Badarkhe 	return CRYPTO_SUCCESS;
67238f89369SManish V Badarkhe }
67338f89369SManish V Badarkhe #endif /* TF_MBEDTLS_USE_AES_GCM */
67438f89369SManish V Badarkhe 
67538f89369SManish V Badarkhe /*
67638f89369SManish V Badarkhe  * Register crypto library descriptor
67738f89369SManish V Badarkhe  */
67838f89369SManish V Badarkhe #if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
67938f89369SManish V Badarkhe #if TF_MBEDTLS_USE_AES_GCM
68038f89369SManish V Badarkhe REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
68138f89369SManish V Badarkhe 		    auth_decrypt, NULL);
68238f89369SManish V Badarkhe #else
68338f89369SManish V Badarkhe REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
68438f89369SManish V Badarkhe 		    NULL, NULL);
68538f89369SManish V Badarkhe #endif
68638f89369SManish V Badarkhe #elif CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY
68738f89369SManish V Badarkhe #if TF_MBEDTLS_USE_AES_GCM
68838f89369SManish V Badarkhe REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL,
68938f89369SManish V Badarkhe 		    auth_decrypt, NULL);
69038f89369SManish V Badarkhe #else
69138f89369SManish V Badarkhe REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL,
69238f89369SManish V Badarkhe 		    NULL, NULL);
69338f89369SManish V Badarkhe #endif
69438f89369SManish V Badarkhe #elif CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY
69538f89369SManish V Badarkhe REGISTER_CRYPTO_LIB(LIB_NAME, init, NULL, NULL, calc_hash, NULL, NULL);
69638f89369SManish V Badarkhe #endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
697