xref: /rk3399_ARM-atf/drivers/auth/mbedtls/mbedtls_crypto.c (revision 885bd91f27fd31d46f33861b94a814fa4537ab5f)
17d37aa17SJuan Castillo /*
255aed7d7SJimmy Brisson  * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
37d37aa17SJuan Castillo  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
57d37aa17SJuan Castillo  */
67d37aa17SJuan Castillo 
77cda17bbSSumit Garg #include <assert.h>
87d37aa17SJuan Castillo #include <stddef.h>
97d37aa17SJuan Castillo #include <string.h>
107d37aa17SJuan Castillo 
117d37aa17SJuan Castillo /* mbed TLS headers */
127cda17bbSSumit Garg #include <mbedtls/gcm.h>
13649dbf6fSJuan Castillo #include <mbedtls/md.h>
14649dbf6fSJuan Castillo #include <mbedtls/memory_buffer_alloc.h>
15649dbf6fSJuan Castillo #include <mbedtls/oid.h>
16649dbf6fSJuan Castillo #include <mbedtls/platform.h>
17a8eadc51SGovindraj Raja #include <mbedtls/version.h>
1893ee2799SMadhukar Pappireddy #include <mbedtls/x509.h>
197d37aa17SJuan Castillo 
2009d40e0eSAntonio Nino Diaz #include <common/debug.h>
2109d40e0eSAntonio Nino Diaz #include <drivers/auth/crypto_mod.h>
2209d40e0eSAntonio Nino Diaz #include <drivers/auth/mbedtls/mbedtls_common.h>
23a8eadc51SGovindraj Raja 
247cda17bbSSumit Garg #include <plat/common/platform.h>
2509d40e0eSAntonio Nino Diaz 
267d37aa17SJuan Castillo #define LIB_NAME		"mbed TLS"
277d37aa17SJuan Castillo 
282bf4f27fSManish V Badarkhe #if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
292bf4f27fSManish V Badarkhe CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
3014db963fSManish V Badarkhe /*
3114db963fSManish V Badarkhe  * CRYPTO_MD_MAX_SIZE value is as per current stronger algorithm available
3214db963fSManish V Badarkhe  * so make sure that mbed TLS MD maximum size must be lesser than this.
3314db963fSManish V Badarkhe  */
3414db963fSManish V Badarkhe CASSERT(CRYPTO_MD_MAX_SIZE >= MBEDTLS_MD_MAX_SIZE,
3514db963fSManish V Badarkhe 	assert_mbedtls_md_size_overflow);
3614db963fSManish V Badarkhe 
372bf4f27fSManish V Badarkhe #endif /* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
382bf4f27fSManish V Badarkhe 	  CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
3914db963fSManish V Badarkhe 
407d37aa17SJuan Castillo /*
417d37aa17SJuan Castillo  * AlgorithmIdentifier  ::=  SEQUENCE  {
427d37aa17SJuan Castillo  *     algorithm               OBJECT IDENTIFIER,
437d37aa17SJuan Castillo  *     parameters              ANY DEFINED BY algorithm OPTIONAL
447d37aa17SJuan Castillo  * }
457d37aa17SJuan Castillo  *
467d37aa17SJuan Castillo  * SubjectPublicKeyInfo  ::=  SEQUENCE  {
477d37aa17SJuan Castillo  *     algorithm            AlgorithmIdentifier,
487d37aa17SJuan Castillo  *     subjectPublicKey     BIT STRING
497d37aa17SJuan Castillo  * }
507d37aa17SJuan Castillo  *
517d37aa17SJuan Castillo  * DigestInfo ::= SEQUENCE {
527d37aa17SJuan Castillo  *     digestAlgorithm AlgorithmIdentifier,
537d37aa17SJuan Castillo  *     digest OCTET STRING
547d37aa17SJuan Castillo  * }
557d37aa17SJuan Castillo  */
567d37aa17SJuan Castillo 
577d37aa17SJuan Castillo /*
587d37aa17SJuan Castillo  * Initialize the library and export the descriptor
597d37aa17SJuan Castillo  */
607d37aa17SJuan Castillo static void init(void)
617d37aa17SJuan Castillo {
627d37aa17SJuan Castillo 	/* Initialize mbed TLS */
637d37aa17SJuan Castillo 	mbedtls_init();
647d37aa17SJuan Castillo }
657d37aa17SJuan Castillo 
662bf4f27fSManish V Badarkhe #if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
672bf4f27fSManish V Badarkhe CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
6855aed7d7SJimmy Brisson 
6955aed7d7SJimmy Brisson 
7055aed7d7SJimmy Brisson /*
7155aed7d7SJimmy Brisson  * NOTE: This has been made internal in mbedtls 3.6.0 and the mbedtls team has
7255aed7d7SJimmy Brisson  * advised that it's better to copy out the declaration than it would be to
7355aed7d7SJimmy Brisson  * update to 3.5.2, where this function is exposed.
7455aed7d7SJimmy Brisson  */
7555aed7d7SJimmy Brisson int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid,
7655aed7d7SJimmy Brisson 			     const mbedtls_x509_buf *sig_params,
7755aed7d7SJimmy Brisson 			     mbedtls_md_type_t *md_alg,
7855aed7d7SJimmy Brisson 			     mbedtls_pk_type_t *pk_alg,
7955aed7d7SJimmy Brisson 			     void **sig_opts);
807d37aa17SJuan Castillo /*
817d37aa17SJuan Castillo  * Verify a signature.
827d37aa17SJuan Castillo  *
837d37aa17SJuan Castillo  * Parameters are passed using the DER encoding format following the ASN.1
847d37aa17SJuan Castillo  * structures detailed above.
857d37aa17SJuan Castillo  */
867d37aa17SJuan Castillo static int verify_signature(void *data_ptr, unsigned int data_len,
877d37aa17SJuan Castillo 			    void *sig_ptr, unsigned int sig_len,
887d37aa17SJuan Castillo 			    void *sig_alg, unsigned int sig_alg_len,
897d37aa17SJuan Castillo 			    void *pk_ptr, unsigned int pk_len)
907d37aa17SJuan Castillo {
91649dbf6fSJuan Castillo 	mbedtls_asn1_buf sig_oid, sig_params;
92649dbf6fSJuan Castillo 	mbedtls_asn1_buf signature;
93649dbf6fSJuan Castillo 	mbedtls_md_type_t md_alg;
94649dbf6fSJuan Castillo 	mbedtls_pk_type_t pk_alg;
951001202dSSoby Mathew 	mbedtls_pk_context pk = {0};
967d37aa17SJuan Castillo 	int rc;
977d37aa17SJuan Castillo 	void *sig_opts = NULL;
98649dbf6fSJuan Castillo 	const mbedtls_md_info_t *md_info;
997d37aa17SJuan Castillo 	unsigned char *p, *end;
100649dbf6fSJuan Castillo 	unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1017d37aa17SJuan Castillo 
1027d37aa17SJuan Castillo 	/* Get pointers to signature OID and parameters */
1037d37aa17SJuan Castillo 	p = (unsigned char *)sig_alg;
1047d37aa17SJuan Castillo 	end = (unsigned char *)(p + sig_alg_len);
105649dbf6fSJuan Castillo 	rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, &sig_params);
1067d37aa17SJuan Castillo 	if (rc != 0) {
1077d37aa17SJuan Castillo 		return CRYPTO_ERR_SIGNATURE;
1087d37aa17SJuan Castillo 	}
1097d37aa17SJuan Castillo 
1107d37aa17SJuan Castillo 	/* Get the actual signature algorithm (MD + PK) */
1111001202dSSoby Mathew 	rc = mbedtls_x509_get_sig_alg(&sig_oid, &sig_params, &md_alg, &pk_alg, &sig_opts);
1127d37aa17SJuan Castillo 	if (rc != 0) {
1137d37aa17SJuan Castillo 		return CRYPTO_ERR_SIGNATURE;
1147d37aa17SJuan Castillo 	}
1157d37aa17SJuan Castillo 
1167d37aa17SJuan Castillo 	/* Parse the public key */
117649dbf6fSJuan Castillo 	mbedtls_pk_init(&pk);
1187d37aa17SJuan Castillo 	p = (unsigned char *)pk_ptr;
1197d37aa17SJuan Castillo 	end = (unsigned char *)(p + pk_len);
120649dbf6fSJuan Castillo 	rc = mbedtls_pk_parse_subpubkey(&p, end, &pk);
1217d37aa17SJuan Castillo 	if (rc != 0) {
1221001202dSSoby Mathew 		rc = CRYPTO_ERR_SIGNATURE;
1231001202dSSoby Mathew 		goto end2;
1247d37aa17SJuan Castillo 	}
1257d37aa17SJuan Castillo 
1267d37aa17SJuan Castillo 	/* Get the signature (bitstring) */
1277d37aa17SJuan Castillo 	p = (unsigned char *)sig_ptr;
1287d37aa17SJuan Castillo 	end = (unsigned char *)(p + sig_len);
1297d37aa17SJuan Castillo 	signature.tag = *p;
130649dbf6fSJuan Castillo 	rc = mbedtls_asn1_get_bitstring_null(&p, end, &signature.len);
131a8c8c5efSDemi Marie Obenour 	if ((rc != 0) || ((size_t)(end - p) != signature.len)) {
1327d37aa17SJuan Castillo 		rc = CRYPTO_ERR_SIGNATURE;
1331001202dSSoby Mathew 		goto end1;
1347d37aa17SJuan Castillo 	}
1357d37aa17SJuan Castillo 	signature.p = p;
1367d37aa17SJuan Castillo 
1377d37aa17SJuan Castillo 	/* Calculate the hash of the data */
138649dbf6fSJuan Castillo 	md_info = mbedtls_md_info_from_type(md_alg);
1397d37aa17SJuan Castillo 	if (md_info == NULL) {
1407d37aa17SJuan Castillo 		rc = CRYPTO_ERR_SIGNATURE;
1411001202dSSoby Mathew 		goto end1;
1427d37aa17SJuan Castillo 	}
1437d37aa17SJuan Castillo 	p = (unsigned char *)data_ptr;
144649dbf6fSJuan Castillo 	rc = mbedtls_md(md_info, p, data_len, hash);
1457d37aa17SJuan Castillo 	if (rc != 0) {
1467d37aa17SJuan Castillo 		rc = CRYPTO_ERR_SIGNATURE;
1471001202dSSoby Mathew 		goto end1;
1487d37aa17SJuan Castillo 	}
1497d37aa17SJuan Castillo 
1507d37aa17SJuan Castillo 	/* Verify the signature */
151649dbf6fSJuan Castillo 	rc = mbedtls_pk_verify_ext(pk_alg, sig_opts, &pk, md_alg, hash,
152649dbf6fSJuan Castillo 			mbedtls_md_get_size(md_info),
153649dbf6fSJuan Castillo 			signature.p, signature.len);
1547d37aa17SJuan Castillo 	if (rc != 0) {
1557d37aa17SJuan Castillo 		rc = CRYPTO_ERR_SIGNATURE;
1561001202dSSoby Mathew 		goto end1;
1577d37aa17SJuan Castillo 	}
1587d37aa17SJuan Castillo 
1597d37aa17SJuan Castillo 	/* Signature verification success */
1607d37aa17SJuan Castillo 	rc = CRYPTO_SUCCESS;
1617d37aa17SJuan Castillo 
1621001202dSSoby Mathew end1:
163649dbf6fSJuan Castillo 	mbedtls_pk_free(&pk);
1641001202dSSoby Mathew end2:
1651001202dSSoby Mathew 	mbedtls_free(sig_opts);
1667d37aa17SJuan Castillo 	return rc;
1677d37aa17SJuan Castillo }
1687d37aa17SJuan Castillo 
1697d37aa17SJuan Castillo /*
1707d37aa17SJuan Castillo  * Match a hash
1717d37aa17SJuan Castillo  *
1727d37aa17SJuan Castillo  * Digest info is passed in DER format following the ASN.1 structure detailed
1737d37aa17SJuan Castillo  * above.
1747d37aa17SJuan Castillo  */
1757d37aa17SJuan Castillo static int verify_hash(void *data_ptr, unsigned int data_len,
1767d37aa17SJuan Castillo 		       void *digest_info_ptr, unsigned int digest_info_len)
1777d37aa17SJuan Castillo {
178649dbf6fSJuan Castillo 	mbedtls_asn1_buf hash_oid, params;
179649dbf6fSJuan Castillo 	mbedtls_md_type_t md_alg;
180649dbf6fSJuan Castillo 	const mbedtls_md_info_t *md_info;
1817d37aa17SJuan Castillo 	unsigned char *p, *end, *hash;
182649dbf6fSJuan Castillo 	unsigned char data_hash[MBEDTLS_MD_MAX_SIZE];
1837d37aa17SJuan Castillo 	size_t len;
1847d37aa17SJuan Castillo 	int rc;
1857d37aa17SJuan Castillo 
186f47547b3SDemi Marie Obenour 	/*
18722a53545SDemi Marie Obenour 	 * Digest info should be an MBEDTLS_ASN1_SEQUENCE, but padding after
18822a53545SDemi Marie Obenour 	 * it is allowed.  This is necessary to support multiple hash
18922a53545SDemi Marie Obenour 	 * algorithms.
190f47547b3SDemi Marie Obenour 	 */
1917d37aa17SJuan Castillo 	p = (unsigned char *)digest_info_ptr;
192aa856917SSandrine Bailleux 	end = p + digest_info_len;
193649dbf6fSJuan Castillo 	rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
194649dbf6fSJuan Castillo 				  MBEDTLS_ASN1_SEQUENCE);
19522a53545SDemi Marie Obenour 	if (rc != 0) {
1967d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
1977d37aa17SJuan Castillo 	}
1987d37aa17SJuan Castillo 
19922a53545SDemi Marie Obenour 	end = p + len;
20022a53545SDemi Marie Obenour 
2017d37aa17SJuan Castillo 	/* Get the hash algorithm */
202649dbf6fSJuan Castillo 	rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, &params);
2037d37aa17SJuan Castillo 	if (rc != 0) {
2047d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
2057d37aa17SJuan Castillo 	}
2067d37aa17SJuan Castillo 
207649dbf6fSJuan Castillo 	rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg);
2087d37aa17SJuan Castillo 	if (rc != 0) {
2097d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
2107d37aa17SJuan Castillo 	}
2117d37aa17SJuan Castillo 
212649dbf6fSJuan Castillo 	md_info = mbedtls_md_info_from_type(md_alg);
2137d37aa17SJuan Castillo 	if (md_info == NULL) {
2147d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
2157d37aa17SJuan Castillo 	}
2167d37aa17SJuan Castillo 
217f47547b3SDemi Marie Obenour 	/* Hash should be octet string type and consume all bytes */
218649dbf6fSJuan Castillo 	rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
219f47547b3SDemi Marie Obenour 	if ((rc != 0) || ((size_t)(end - p) != len)) {
2207d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
2217d37aa17SJuan Castillo 	}
2227d37aa17SJuan Castillo 
2237d37aa17SJuan Castillo 	/* Length of hash must match the algorithm's size */
224649dbf6fSJuan Castillo 	if (len != mbedtls_md_get_size(md_info)) {
2257d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
2267d37aa17SJuan Castillo 	}
2277d37aa17SJuan Castillo 	hash = p;
2287d37aa17SJuan Castillo 
2297d37aa17SJuan Castillo 	/* Calculate the hash of the data */
2307d37aa17SJuan Castillo 	p = (unsigned char *)data_ptr;
231649dbf6fSJuan Castillo 	rc = mbedtls_md(md_info, p, data_len, data_hash);
2327d37aa17SJuan Castillo 	if (rc != 0) {
2337d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
2347d37aa17SJuan Castillo 	}
2357d37aa17SJuan Castillo 
2367d37aa17SJuan Castillo 	/* Compare values */
237fabd21adSAntonio Nino Diaz 	rc = memcmp(data_hash, hash, mbedtls_md_get_size(md_info));
2387d37aa17SJuan Castillo 	if (rc != 0) {
2397d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
2407d37aa17SJuan Castillo 	}
2417d37aa17SJuan Castillo 
2427d37aa17SJuan Castillo 	return CRYPTO_SUCCESS;
2437d37aa17SJuan Castillo }
2442bf4f27fSManish V Badarkhe #endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
2452bf4f27fSManish V Badarkhe 	  CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
2467d37aa17SJuan Castillo 
2472bf4f27fSManish V Badarkhe #if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
2482bf4f27fSManish V Badarkhe CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
2498c105290SAlexei Fedorov /*
25014db963fSManish V Badarkhe  * Map a generic crypto message digest algorithm to the corresponding macro used
25114db963fSManish V Badarkhe  * by Mbed TLS.
25214db963fSManish V Badarkhe  */
25314db963fSManish V Badarkhe static inline mbedtls_md_type_t md_type(enum crypto_md_algo algo)
25414db963fSManish V Badarkhe {
25514db963fSManish V Badarkhe 	switch (algo) {
25614db963fSManish V Badarkhe 	case CRYPTO_MD_SHA512:
25714db963fSManish V Badarkhe 		return MBEDTLS_MD_SHA512;
25814db963fSManish V Badarkhe 	case CRYPTO_MD_SHA384:
25914db963fSManish V Badarkhe 		return MBEDTLS_MD_SHA384;
26014db963fSManish V Badarkhe 	case CRYPTO_MD_SHA256:
26114db963fSManish V Badarkhe 		return MBEDTLS_MD_SHA256;
26214db963fSManish V Badarkhe 	default:
26314db963fSManish V Badarkhe 		/* Invalid hash algorithm. */
26414db963fSManish V Badarkhe 		return MBEDTLS_MD_NONE;
26514db963fSManish V Badarkhe 	}
26614db963fSManish V Badarkhe }
26714db963fSManish V Badarkhe 
26814db963fSManish V Badarkhe /*
2698c105290SAlexei Fedorov  * Calculate a hash
2708c105290SAlexei Fedorov  *
2718c105290SAlexei Fedorov  * output points to the computed hash
2728c105290SAlexei Fedorov  */
27314db963fSManish V Badarkhe static int calc_hash(enum crypto_md_algo md_algo, void *data_ptr,
27414db963fSManish V Badarkhe 		     unsigned int data_len,
27514db963fSManish V Badarkhe 		     unsigned char output[CRYPTO_MD_MAX_SIZE])
2768c105290SAlexei Fedorov {
2778c105290SAlexei Fedorov 	const mbedtls_md_info_t *md_info;
278*885bd91fSRyan Everett 	int rc;
2798c105290SAlexei Fedorov 
28014db963fSManish V Badarkhe 	md_info = mbedtls_md_info_from_type(md_type(md_algo));
2818c105290SAlexei Fedorov 	if (md_info == NULL) {
2828c105290SAlexei Fedorov 		return CRYPTO_ERR_HASH;
2838c105290SAlexei Fedorov 	}
2848c105290SAlexei Fedorov 
28514db963fSManish V Badarkhe 	/*
28614db963fSManish V Badarkhe 	 * Calculate the hash of the data, it is safe to pass the
28714db963fSManish V Badarkhe 	 * 'output' hash buffer pointer considering its size is always
28814db963fSManish V Badarkhe 	 * bigger than or equal to MBEDTLS_MD_MAX_SIZE.
28914db963fSManish V Badarkhe 	 */
290*885bd91fSRyan Everett 	rc = mbedtls_md(md_info, data_ptr, data_len, output);
291*885bd91fSRyan Everett 	if (rc != 0) {
292*885bd91fSRyan Everett 		return CRYPTO_ERR_HASH;
293*885bd91fSRyan Everett 	}
294*885bd91fSRyan Everett 
295*885bd91fSRyan Everett 	return CRYPTO_SUCCESS;
2968c105290SAlexei Fedorov }
2972bf4f27fSManish V Badarkhe #endif /* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
2982bf4f27fSManish V Badarkhe 	  CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
2998c105290SAlexei Fedorov 
3007cda17bbSSumit Garg #if TF_MBEDTLS_USE_AES_GCM
3017cda17bbSSumit Garg /*
3027cda17bbSSumit Garg  * Stack based buffer allocation for decryption operation. It could
3037cda17bbSSumit Garg  * be configured to balance stack usage vs execution speed.
3047cda17bbSSumit Garg  */
3057cda17bbSSumit Garg #define DEC_OP_BUF_SIZE		128
3067cda17bbSSumit Garg 
3077cda17bbSSumit Garg static int aes_gcm_decrypt(void *data_ptr, size_t len, const void *key,
3087cda17bbSSumit Garg 			   unsigned int key_len, const void *iv,
3097cda17bbSSumit Garg 			   unsigned int iv_len, const void *tag,
3107cda17bbSSumit Garg 			   unsigned int tag_len)
3117cda17bbSSumit Garg {
3127cda17bbSSumit Garg 	mbedtls_gcm_context ctx;
3137cda17bbSSumit Garg 	mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
3147cda17bbSSumit Garg 	unsigned char buf[DEC_OP_BUF_SIZE];
3157cda17bbSSumit Garg 	unsigned char tag_buf[CRYPTO_MAX_TAG_SIZE];
3167cda17bbSSumit Garg 	unsigned char *pt = data_ptr;
3177cda17bbSSumit Garg 	size_t dec_len;
3187cda17bbSSumit Garg 	int diff, i, rc;
31951e06159SGovindraj Raja 	size_t output_length __unused;
3207cda17bbSSumit Garg 
3217cda17bbSSumit Garg 	mbedtls_gcm_init(&ctx);
3227cda17bbSSumit Garg 
3237cda17bbSSumit Garg 	rc = mbedtls_gcm_setkey(&ctx, cipher, key, key_len * 8);
3247cda17bbSSumit Garg 	if (rc != 0) {
3257cda17bbSSumit Garg 		rc = CRYPTO_ERR_DECRYPTION;
3267cda17bbSSumit Garg 		goto exit_gcm;
3277cda17bbSSumit Garg 	}
3287cda17bbSSumit Garg 
32951e06159SGovindraj Raja #if (MBEDTLS_VERSION_MAJOR < 3)
3307cda17bbSSumit Garg 	rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len, NULL, 0);
33151e06159SGovindraj Raja #else
33251e06159SGovindraj Raja 	rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len);
33351e06159SGovindraj Raja #endif
3347cda17bbSSumit Garg 	if (rc != 0) {
3357cda17bbSSumit Garg 		rc = CRYPTO_ERR_DECRYPTION;
3367cda17bbSSumit Garg 		goto exit_gcm;
3377cda17bbSSumit Garg 	}
3387cda17bbSSumit Garg 
3397cda17bbSSumit Garg 	while (len > 0) {
3407cda17bbSSumit Garg 		dec_len = MIN(sizeof(buf), len);
3417cda17bbSSumit Garg 
34251e06159SGovindraj Raja #if (MBEDTLS_VERSION_MAJOR < 3)
3437cda17bbSSumit Garg 		rc = mbedtls_gcm_update(&ctx, dec_len, pt, buf);
34451e06159SGovindraj Raja #else
34551e06159SGovindraj Raja 		rc = mbedtls_gcm_update(&ctx, pt, dec_len, buf, sizeof(buf), &output_length);
34651e06159SGovindraj Raja #endif
34751e06159SGovindraj Raja 
3487cda17bbSSumit Garg 		if (rc != 0) {
3497cda17bbSSumit Garg 			rc = CRYPTO_ERR_DECRYPTION;
3507cda17bbSSumit Garg 			goto exit_gcm;
3517cda17bbSSumit Garg 		}
3527cda17bbSSumit Garg 
3537cda17bbSSumit Garg 		memcpy(pt, buf, dec_len);
3547cda17bbSSumit Garg 		pt += dec_len;
3557cda17bbSSumit Garg 		len -= dec_len;
3567cda17bbSSumit Garg 	}
3577cda17bbSSumit Garg 
35851e06159SGovindraj Raja #if (MBEDTLS_VERSION_MAJOR < 3)
3597cda17bbSSumit Garg 	rc = mbedtls_gcm_finish(&ctx, tag_buf, sizeof(tag_buf));
36051e06159SGovindraj Raja #else
36151e06159SGovindraj Raja 	rc = mbedtls_gcm_finish(&ctx, NULL, 0, &output_length, tag_buf, sizeof(tag_buf));
36251e06159SGovindraj Raja #endif
36351e06159SGovindraj Raja 
3647cda17bbSSumit Garg 	if (rc != 0) {
3657cda17bbSSumit Garg 		rc = CRYPTO_ERR_DECRYPTION;
3667cda17bbSSumit Garg 		goto exit_gcm;
3677cda17bbSSumit Garg 	}
3687cda17bbSSumit Garg 
3697cda17bbSSumit Garg 	/* Check tag in "constant-time" */
3707cda17bbSSumit Garg 	for (diff = 0, i = 0; i < tag_len; i++)
3717cda17bbSSumit Garg 		diff |= ((const unsigned char *)tag)[i] ^ tag_buf[i];
3727cda17bbSSumit Garg 
3737cda17bbSSumit Garg 	if (diff != 0) {
3747cda17bbSSumit Garg 		rc = CRYPTO_ERR_DECRYPTION;
3757cda17bbSSumit Garg 		goto exit_gcm;
3767cda17bbSSumit Garg 	}
3777cda17bbSSumit Garg 
3787cda17bbSSumit Garg 	/* GCM decryption success */
3797cda17bbSSumit Garg 	rc = CRYPTO_SUCCESS;
3807cda17bbSSumit Garg 
3817cda17bbSSumit Garg exit_gcm:
3827cda17bbSSumit Garg 	mbedtls_gcm_free(&ctx);
3837cda17bbSSumit Garg 	return rc;
3847cda17bbSSumit Garg }
3857cda17bbSSumit Garg 
3867cda17bbSSumit Garg /*
3877cda17bbSSumit Garg  * Authenticated decryption of an image
3887cda17bbSSumit Garg  */
3897cda17bbSSumit Garg static int auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
3907cda17bbSSumit Garg 			size_t len, const void *key, unsigned int key_len,
3917cda17bbSSumit Garg 			unsigned int key_flags, const void *iv,
3927cda17bbSSumit Garg 			unsigned int iv_len, const void *tag,
3937cda17bbSSumit Garg 			unsigned int tag_len)
3947cda17bbSSumit Garg {
3957cda17bbSSumit Garg 	int rc;
3967cda17bbSSumit Garg 
3977cda17bbSSumit Garg 	assert((key_flags & ENC_KEY_IS_IDENTIFIER) == 0);
3987cda17bbSSumit Garg 
3997cda17bbSSumit Garg 	switch (dec_algo) {
4007cda17bbSSumit Garg 	case CRYPTO_GCM_DECRYPT:
4017cda17bbSSumit Garg 		rc = aes_gcm_decrypt(data_ptr, len, key, key_len, iv, iv_len,
4027cda17bbSSumit Garg 				     tag, tag_len);
4037cda17bbSSumit Garg 		if (rc != 0)
4047cda17bbSSumit Garg 			return rc;
4057cda17bbSSumit Garg 		break;
4067cda17bbSSumit Garg 	default:
4077cda17bbSSumit Garg 		return CRYPTO_ERR_DECRYPTION;
4087cda17bbSSumit Garg 	}
4097cda17bbSSumit Garg 
4107cda17bbSSumit Garg 	return CRYPTO_SUCCESS;
4117cda17bbSSumit Garg }
4127cda17bbSSumit Garg #endif /* TF_MBEDTLS_USE_AES_GCM */
4137cda17bbSSumit Garg 
4147d37aa17SJuan Castillo /*
4157d37aa17SJuan Castillo  * Register crypto library descriptor
4167d37aa17SJuan Castillo  */
4172bf4f27fSManish V Badarkhe #if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
4187cda17bbSSumit Garg #if TF_MBEDTLS_USE_AES_GCM
4197cda17bbSSumit Garg REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
4204ac5b394SYann Gautier 		    auth_decrypt, NULL);
4218c105290SAlexei Fedorov #else
4227cda17bbSSumit Garg REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
4234ac5b394SYann Gautier 		    NULL, NULL);
4247cda17bbSSumit Garg #endif
4252bf4f27fSManish V Badarkhe #elif CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY
4267cda17bbSSumit Garg #if TF_MBEDTLS_USE_AES_GCM
427dee99f10SYann Gautier REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL,
4284ac5b394SYann Gautier 		    auth_decrypt, NULL);
4297cda17bbSSumit Garg #else
430dee99f10SYann Gautier REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL,
431dee99f10SYann Gautier 		    NULL, NULL);
4327cda17bbSSumit Garg #endif
4332bf4f27fSManish V Badarkhe #elif CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY
434dee99f10SYann Gautier REGISTER_CRYPTO_LIB(LIB_NAME, init, NULL, NULL, calc_hash, NULL, NULL);
4352bf4f27fSManish V Badarkhe #endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
436