xref: /rk3399_ARM-atf/drivers/auth/mbedtls/mbedtls_crypto.c (revision 0aa0b3afd643379805440dddffffeb21dab52523)
17d37aa17SJuan Castillo /*
2*0aa0b3afSManish V Badarkhe  * Copyright (c) 2015-2022, 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>
1793ee2799SMadhukar Pappireddy #include <mbedtls/x509.h>
187d37aa17SJuan Castillo 
1909d40e0eSAntonio Nino Diaz #include <common/debug.h>
2009d40e0eSAntonio Nino Diaz #include <drivers/auth/crypto_mod.h>
2109d40e0eSAntonio Nino Diaz #include <drivers/auth/mbedtls/mbedtls_common.h>
2209d40e0eSAntonio Nino Diaz #include <drivers/auth/mbedtls/mbedtls_config.h>
237cda17bbSSumit Garg #include <plat/common/platform.h>
2409d40e0eSAntonio Nino Diaz 
257d37aa17SJuan Castillo #define LIB_NAME		"mbed TLS"
267d37aa17SJuan Castillo 
2714db963fSManish V Badarkhe #if MEASURED_BOOT
2814db963fSManish V Badarkhe /*
2914db963fSManish V Badarkhe  * CRYPTO_MD_MAX_SIZE value is as per current stronger algorithm available
3014db963fSManish V Badarkhe  * so make sure that mbed TLS MD maximum size must be lesser than this.
3114db963fSManish V Badarkhe  */
3214db963fSManish V Badarkhe CASSERT(CRYPTO_MD_MAX_SIZE >= MBEDTLS_MD_MAX_SIZE,
3314db963fSManish V Badarkhe 	assert_mbedtls_md_size_overflow);
3414db963fSManish V Badarkhe 
3514db963fSManish V Badarkhe #endif /* MEASURED_BOOT */
3614db963fSManish V Badarkhe 
377d37aa17SJuan Castillo /*
387d37aa17SJuan Castillo  * AlgorithmIdentifier  ::=  SEQUENCE  {
397d37aa17SJuan Castillo  *     algorithm               OBJECT IDENTIFIER,
407d37aa17SJuan Castillo  *     parameters              ANY DEFINED BY algorithm OPTIONAL
417d37aa17SJuan Castillo  * }
427d37aa17SJuan Castillo  *
437d37aa17SJuan Castillo  * SubjectPublicKeyInfo  ::=  SEQUENCE  {
447d37aa17SJuan Castillo  *     algorithm            AlgorithmIdentifier,
457d37aa17SJuan Castillo  *     subjectPublicKey     BIT STRING
467d37aa17SJuan Castillo  * }
477d37aa17SJuan Castillo  *
487d37aa17SJuan Castillo  * DigestInfo ::= SEQUENCE {
497d37aa17SJuan Castillo  *     digestAlgorithm AlgorithmIdentifier,
507d37aa17SJuan Castillo  *     digest OCTET STRING
517d37aa17SJuan Castillo  * }
527d37aa17SJuan Castillo  */
537d37aa17SJuan Castillo 
547d37aa17SJuan Castillo /*
557d37aa17SJuan Castillo  * Initialize the library and export the descriptor
567d37aa17SJuan Castillo  */
577d37aa17SJuan Castillo static void init(void)
587d37aa17SJuan Castillo {
597d37aa17SJuan Castillo 	/* Initialize mbed TLS */
607d37aa17SJuan Castillo 	mbedtls_init();
617d37aa17SJuan Castillo }
627d37aa17SJuan Castillo 
63*0aa0b3afSManish V Badarkhe #if TRUSTED_BOARD_BOOT
647d37aa17SJuan Castillo /*
657d37aa17SJuan Castillo  * Verify a signature.
667d37aa17SJuan Castillo  *
677d37aa17SJuan Castillo  * Parameters are passed using the DER encoding format following the ASN.1
687d37aa17SJuan Castillo  * structures detailed above.
697d37aa17SJuan Castillo  */
707d37aa17SJuan Castillo static int verify_signature(void *data_ptr, unsigned int data_len,
717d37aa17SJuan Castillo 			    void *sig_ptr, unsigned int sig_len,
727d37aa17SJuan Castillo 			    void *sig_alg, unsigned int sig_alg_len,
737d37aa17SJuan Castillo 			    void *pk_ptr, unsigned int pk_len)
747d37aa17SJuan Castillo {
75649dbf6fSJuan Castillo 	mbedtls_asn1_buf sig_oid, sig_params;
76649dbf6fSJuan Castillo 	mbedtls_asn1_buf signature;
77649dbf6fSJuan Castillo 	mbedtls_md_type_t md_alg;
78649dbf6fSJuan Castillo 	mbedtls_pk_type_t pk_alg;
791001202dSSoby Mathew 	mbedtls_pk_context pk = {0};
807d37aa17SJuan Castillo 	int rc;
817d37aa17SJuan Castillo 	void *sig_opts = NULL;
82649dbf6fSJuan Castillo 	const mbedtls_md_info_t *md_info;
837d37aa17SJuan Castillo 	unsigned char *p, *end;
84649dbf6fSJuan Castillo 	unsigned char hash[MBEDTLS_MD_MAX_SIZE];
857d37aa17SJuan Castillo 
867d37aa17SJuan Castillo 	/* Get pointers to signature OID and parameters */
877d37aa17SJuan Castillo 	p = (unsigned char *)sig_alg;
887d37aa17SJuan Castillo 	end = (unsigned char *)(p + sig_alg_len);
89649dbf6fSJuan Castillo 	rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, &sig_params);
907d37aa17SJuan Castillo 	if (rc != 0) {
917d37aa17SJuan Castillo 		return CRYPTO_ERR_SIGNATURE;
927d37aa17SJuan Castillo 	}
937d37aa17SJuan Castillo 
947d37aa17SJuan Castillo 	/* Get the actual signature algorithm (MD + PK) */
951001202dSSoby Mathew 	rc = mbedtls_x509_get_sig_alg(&sig_oid, &sig_params, &md_alg, &pk_alg, &sig_opts);
967d37aa17SJuan Castillo 	if (rc != 0) {
977d37aa17SJuan Castillo 		return CRYPTO_ERR_SIGNATURE;
987d37aa17SJuan Castillo 	}
997d37aa17SJuan Castillo 
1007d37aa17SJuan Castillo 	/* Parse the public key */
101649dbf6fSJuan Castillo 	mbedtls_pk_init(&pk);
1027d37aa17SJuan Castillo 	p = (unsigned char *)pk_ptr;
1037d37aa17SJuan Castillo 	end = (unsigned char *)(p + pk_len);
104649dbf6fSJuan Castillo 	rc = mbedtls_pk_parse_subpubkey(&p, end, &pk);
1057d37aa17SJuan Castillo 	if (rc != 0) {
1061001202dSSoby Mathew 		rc = CRYPTO_ERR_SIGNATURE;
1071001202dSSoby Mathew 		goto end2;
1087d37aa17SJuan Castillo 	}
1097d37aa17SJuan Castillo 
1107d37aa17SJuan Castillo 	/* Get the signature (bitstring) */
1117d37aa17SJuan Castillo 	p = (unsigned char *)sig_ptr;
1127d37aa17SJuan Castillo 	end = (unsigned char *)(p + sig_len);
1137d37aa17SJuan Castillo 	signature.tag = *p;
114649dbf6fSJuan Castillo 	rc = mbedtls_asn1_get_bitstring_null(&p, end, &signature.len);
1157d37aa17SJuan Castillo 	if (rc != 0) {
1167d37aa17SJuan Castillo 		rc = CRYPTO_ERR_SIGNATURE;
1171001202dSSoby Mathew 		goto end1;
1187d37aa17SJuan Castillo 	}
1197d37aa17SJuan Castillo 	signature.p = p;
1207d37aa17SJuan Castillo 
1217d37aa17SJuan Castillo 	/* Calculate the hash of the data */
122649dbf6fSJuan Castillo 	md_info = mbedtls_md_info_from_type(md_alg);
1237d37aa17SJuan Castillo 	if (md_info == NULL) {
1247d37aa17SJuan Castillo 		rc = CRYPTO_ERR_SIGNATURE;
1251001202dSSoby Mathew 		goto end1;
1267d37aa17SJuan Castillo 	}
1277d37aa17SJuan Castillo 	p = (unsigned char *)data_ptr;
128649dbf6fSJuan Castillo 	rc = mbedtls_md(md_info, p, data_len, hash);
1297d37aa17SJuan Castillo 	if (rc != 0) {
1307d37aa17SJuan Castillo 		rc = CRYPTO_ERR_SIGNATURE;
1311001202dSSoby Mathew 		goto end1;
1327d37aa17SJuan Castillo 	}
1337d37aa17SJuan Castillo 
1347d37aa17SJuan Castillo 	/* Verify the signature */
135649dbf6fSJuan Castillo 	rc = mbedtls_pk_verify_ext(pk_alg, sig_opts, &pk, md_alg, hash,
136649dbf6fSJuan Castillo 			mbedtls_md_get_size(md_info),
137649dbf6fSJuan Castillo 			signature.p, signature.len);
1387d37aa17SJuan Castillo 	if (rc != 0) {
1397d37aa17SJuan Castillo 		rc = CRYPTO_ERR_SIGNATURE;
1401001202dSSoby Mathew 		goto end1;
1417d37aa17SJuan Castillo 	}
1427d37aa17SJuan Castillo 
1437d37aa17SJuan Castillo 	/* Signature verification success */
1447d37aa17SJuan Castillo 	rc = CRYPTO_SUCCESS;
1457d37aa17SJuan Castillo 
1461001202dSSoby Mathew end1:
147649dbf6fSJuan Castillo 	mbedtls_pk_free(&pk);
1481001202dSSoby Mathew end2:
1491001202dSSoby Mathew 	mbedtls_free(sig_opts);
1507d37aa17SJuan Castillo 	return rc;
1517d37aa17SJuan Castillo }
1527d37aa17SJuan Castillo 
1537d37aa17SJuan Castillo /*
1547d37aa17SJuan Castillo  * Match a hash
1557d37aa17SJuan Castillo  *
1567d37aa17SJuan Castillo  * Digest info is passed in DER format following the ASN.1 structure detailed
1577d37aa17SJuan Castillo  * above.
1587d37aa17SJuan Castillo  */
1597d37aa17SJuan Castillo static int verify_hash(void *data_ptr, unsigned int data_len,
1607d37aa17SJuan Castillo 		       void *digest_info_ptr, unsigned int digest_info_len)
1617d37aa17SJuan Castillo {
162649dbf6fSJuan Castillo 	mbedtls_asn1_buf hash_oid, params;
163649dbf6fSJuan Castillo 	mbedtls_md_type_t md_alg;
164649dbf6fSJuan Castillo 	const mbedtls_md_info_t *md_info;
1657d37aa17SJuan Castillo 	unsigned char *p, *end, *hash;
166649dbf6fSJuan Castillo 	unsigned char data_hash[MBEDTLS_MD_MAX_SIZE];
1677d37aa17SJuan Castillo 	size_t len;
1687d37aa17SJuan Castillo 	int rc;
1697d37aa17SJuan Castillo 
170649dbf6fSJuan Castillo 	/* Digest info should be an MBEDTLS_ASN1_SEQUENCE */
1717d37aa17SJuan Castillo 	p = (unsigned char *)digest_info_ptr;
172aa856917SSandrine Bailleux 	end = p + digest_info_len;
173649dbf6fSJuan Castillo 	rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
174649dbf6fSJuan Castillo 				  MBEDTLS_ASN1_SEQUENCE);
1757d37aa17SJuan Castillo 	if (rc != 0) {
1767d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
1777d37aa17SJuan Castillo 	}
1787d37aa17SJuan Castillo 
1797d37aa17SJuan Castillo 	/* Get the hash algorithm */
180649dbf6fSJuan Castillo 	rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, &params);
1817d37aa17SJuan Castillo 	if (rc != 0) {
1827d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
1837d37aa17SJuan Castillo 	}
1847d37aa17SJuan Castillo 
185649dbf6fSJuan Castillo 	rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg);
1867d37aa17SJuan Castillo 	if (rc != 0) {
1877d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
1887d37aa17SJuan Castillo 	}
1897d37aa17SJuan Castillo 
190649dbf6fSJuan Castillo 	md_info = mbedtls_md_info_from_type(md_alg);
1917d37aa17SJuan Castillo 	if (md_info == NULL) {
1927d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
1937d37aa17SJuan Castillo 	}
1947d37aa17SJuan Castillo 
1957d37aa17SJuan Castillo 	/* Hash should be octet string type */
196649dbf6fSJuan Castillo 	rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
1977d37aa17SJuan Castillo 	if (rc != 0) {
1987d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
1997d37aa17SJuan Castillo 	}
2007d37aa17SJuan Castillo 
2017d37aa17SJuan Castillo 	/* Length of hash must match the algorithm's size */
202649dbf6fSJuan Castillo 	if (len != mbedtls_md_get_size(md_info)) {
2037d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
2047d37aa17SJuan Castillo 	}
2057d37aa17SJuan Castillo 	hash = p;
2067d37aa17SJuan Castillo 
2077d37aa17SJuan Castillo 	/* Calculate the hash of the data */
2087d37aa17SJuan Castillo 	p = (unsigned char *)data_ptr;
209649dbf6fSJuan Castillo 	rc = mbedtls_md(md_info, p, data_len, data_hash);
2107d37aa17SJuan Castillo 	if (rc != 0) {
2117d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
2127d37aa17SJuan Castillo 	}
2137d37aa17SJuan Castillo 
2147d37aa17SJuan Castillo 	/* Compare values */
215fabd21adSAntonio Nino Diaz 	rc = memcmp(data_hash, hash, mbedtls_md_get_size(md_info));
2167d37aa17SJuan Castillo 	if (rc != 0) {
2177d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
2187d37aa17SJuan Castillo 	}
2197d37aa17SJuan Castillo 
2207d37aa17SJuan Castillo 	return CRYPTO_SUCCESS;
2217d37aa17SJuan Castillo }
222*0aa0b3afSManish V Badarkhe #endif /* TRUSTED_BOARD_BOOT */
2237d37aa17SJuan Castillo 
2248c105290SAlexei Fedorov #if MEASURED_BOOT
2258c105290SAlexei Fedorov /*
22614db963fSManish V Badarkhe  * Map a generic crypto message digest algorithm to the corresponding macro used
22714db963fSManish V Badarkhe  * by Mbed TLS.
22814db963fSManish V Badarkhe  */
22914db963fSManish V Badarkhe static inline mbedtls_md_type_t md_type(enum crypto_md_algo algo)
23014db963fSManish V Badarkhe {
23114db963fSManish V Badarkhe 	switch (algo) {
23214db963fSManish V Badarkhe 	case CRYPTO_MD_SHA512:
23314db963fSManish V Badarkhe 		return MBEDTLS_MD_SHA512;
23414db963fSManish V Badarkhe 	case CRYPTO_MD_SHA384:
23514db963fSManish V Badarkhe 		return MBEDTLS_MD_SHA384;
23614db963fSManish V Badarkhe 	case CRYPTO_MD_SHA256:
23714db963fSManish V Badarkhe 		return MBEDTLS_MD_SHA256;
23814db963fSManish V Badarkhe 	default:
23914db963fSManish V Badarkhe 		/* Invalid hash algorithm. */
24014db963fSManish V Badarkhe 		return MBEDTLS_MD_NONE;
24114db963fSManish V Badarkhe 	}
24214db963fSManish V Badarkhe }
24314db963fSManish V Badarkhe 
24414db963fSManish V Badarkhe /*
2458c105290SAlexei Fedorov  * Calculate a hash
2468c105290SAlexei Fedorov  *
2478c105290SAlexei Fedorov  * output points to the computed hash
2488c105290SAlexei Fedorov  */
24914db963fSManish V Badarkhe static int calc_hash(enum crypto_md_algo md_algo, void *data_ptr,
25014db963fSManish V Badarkhe 		     unsigned int data_len,
25114db963fSManish V Badarkhe 		     unsigned char output[CRYPTO_MD_MAX_SIZE])
2528c105290SAlexei Fedorov {
2538c105290SAlexei Fedorov 	const mbedtls_md_info_t *md_info;
2548c105290SAlexei Fedorov 
25514db963fSManish V Badarkhe 	md_info = mbedtls_md_info_from_type(md_type(md_algo));
2568c105290SAlexei Fedorov 	if (md_info == NULL) {
2578c105290SAlexei Fedorov 		return CRYPTO_ERR_HASH;
2588c105290SAlexei Fedorov 	}
2598c105290SAlexei Fedorov 
26014db963fSManish V Badarkhe 	/*
26114db963fSManish V Badarkhe 	 * Calculate the hash of the data, it is safe to pass the
26214db963fSManish V Badarkhe 	 * 'output' hash buffer pointer considering its size is always
26314db963fSManish V Badarkhe 	 * bigger than or equal to MBEDTLS_MD_MAX_SIZE.
26414db963fSManish V Badarkhe 	 */
2658c105290SAlexei Fedorov 	return mbedtls_md(md_info, data_ptr, data_len, output);
2668c105290SAlexei Fedorov }
2678c105290SAlexei Fedorov #endif /* MEASURED_BOOT */
2688c105290SAlexei Fedorov 
2697cda17bbSSumit Garg #if TF_MBEDTLS_USE_AES_GCM
2707cda17bbSSumit Garg /*
2717cda17bbSSumit Garg  * Stack based buffer allocation for decryption operation. It could
2727cda17bbSSumit Garg  * be configured to balance stack usage vs execution speed.
2737cda17bbSSumit Garg  */
2747cda17bbSSumit Garg #define DEC_OP_BUF_SIZE		128
2757cda17bbSSumit Garg 
2767cda17bbSSumit Garg static int aes_gcm_decrypt(void *data_ptr, size_t len, const void *key,
2777cda17bbSSumit Garg 			   unsigned int key_len, const void *iv,
2787cda17bbSSumit Garg 			   unsigned int iv_len, const void *tag,
2797cda17bbSSumit Garg 			   unsigned int tag_len)
2807cda17bbSSumit Garg {
2817cda17bbSSumit Garg 	mbedtls_gcm_context ctx;
2827cda17bbSSumit Garg 	mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
2837cda17bbSSumit Garg 	unsigned char buf[DEC_OP_BUF_SIZE];
2847cda17bbSSumit Garg 	unsigned char tag_buf[CRYPTO_MAX_TAG_SIZE];
2857cda17bbSSumit Garg 	unsigned char *pt = data_ptr;
2867cda17bbSSumit Garg 	size_t dec_len;
2877cda17bbSSumit Garg 	int diff, i, rc;
2887cda17bbSSumit Garg 
2897cda17bbSSumit Garg 	mbedtls_gcm_init(&ctx);
2907cda17bbSSumit Garg 
2917cda17bbSSumit Garg 	rc = mbedtls_gcm_setkey(&ctx, cipher, key, key_len * 8);
2927cda17bbSSumit Garg 	if (rc != 0) {
2937cda17bbSSumit Garg 		rc = CRYPTO_ERR_DECRYPTION;
2947cda17bbSSumit Garg 		goto exit_gcm;
2957cda17bbSSumit Garg 	}
2967cda17bbSSumit Garg 
2977cda17bbSSumit Garg 	rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len, NULL, 0);
2987cda17bbSSumit Garg 	if (rc != 0) {
2997cda17bbSSumit Garg 		rc = CRYPTO_ERR_DECRYPTION;
3007cda17bbSSumit Garg 		goto exit_gcm;
3017cda17bbSSumit Garg 	}
3027cda17bbSSumit Garg 
3037cda17bbSSumit Garg 	while (len > 0) {
3047cda17bbSSumit Garg 		dec_len = MIN(sizeof(buf), len);
3057cda17bbSSumit Garg 
3067cda17bbSSumit Garg 		rc = mbedtls_gcm_update(&ctx, dec_len, pt, buf);
3077cda17bbSSumit Garg 		if (rc != 0) {
3087cda17bbSSumit Garg 			rc = CRYPTO_ERR_DECRYPTION;
3097cda17bbSSumit Garg 			goto exit_gcm;
3107cda17bbSSumit Garg 		}
3117cda17bbSSumit Garg 
3127cda17bbSSumit Garg 		memcpy(pt, buf, dec_len);
3137cda17bbSSumit Garg 		pt += dec_len;
3147cda17bbSSumit Garg 		len -= dec_len;
3157cda17bbSSumit Garg 	}
3167cda17bbSSumit Garg 
3177cda17bbSSumit Garg 	rc = mbedtls_gcm_finish(&ctx, tag_buf, sizeof(tag_buf));
3187cda17bbSSumit Garg 	if (rc != 0) {
3197cda17bbSSumit Garg 		rc = CRYPTO_ERR_DECRYPTION;
3207cda17bbSSumit Garg 		goto exit_gcm;
3217cda17bbSSumit Garg 	}
3227cda17bbSSumit Garg 
3237cda17bbSSumit Garg 	/* Check tag in "constant-time" */
3247cda17bbSSumit Garg 	for (diff = 0, i = 0; i < tag_len; i++)
3257cda17bbSSumit Garg 		diff |= ((const unsigned char *)tag)[i] ^ tag_buf[i];
3267cda17bbSSumit Garg 
3277cda17bbSSumit Garg 	if (diff != 0) {
3287cda17bbSSumit Garg 		rc = CRYPTO_ERR_DECRYPTION;
3297cda17bbSSumit Garg 		goto exit_gcm;
3307cda17bbSSumit Garg 	}
3317cda17bbSSumit Garg 
3327cda17bbSSumit Garg 	/* GCM decryption success */
3337cda17bbSSumit Garg 	rc = CRYPTO_SUCCESS;
3347cda17bbSSumit Garg 
3357cda17bbSSumit Garg exit_gcm:
3367cda17bbSSumit Garg 	mbedtls_gcm_free(&ctx);
3377cda17bbSSumit Garg 	return rc;
3387cda17bbSSumit Garg }
3397cda17bbSSumit Garg 
3407cda17bbSSumit Garg /*
3417cda17bbSSumit Garg  * Authenticated decryption of an image
3427cda17bbSSumit Garg  */
3437cda17bbSSumit Garg static int auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
3447cda17bbSSumit Garg 			size_t len, const void *key, unsigned int key_len,
3457cda17bbSSumit Garg 			unsigned int key_flags, const void *iv,
3467cda17bbSSumit Garg 			unsigned int iv_len, const void *tag,
3477cda17bbSSumit Garg 			unsigned int tag_len)
3487cda17bbSSumit Garg {
3497cda17bbSSumit Garg 	int rc;
3507cda17bbSSumit Garg 
3517cda17bbSSumit Garg 	assert((key_flags & ENC_KEY_IS_IDENTIFIER) == 0);
3527cda17bbSSumit Garg 
3537cda17bbSSumit Garg 	switch (dec_algo) {
3547cda17bbSSumit Garg 	case CRYPTO_GCM_DECRYPT:
3557cda17bbSSumit Garg 		rc = aes_gcm_decrypt(data_ptr, len, key, key_len, iv, iv_len,
3567cda17bbSSumit Garg 				     tag, tag_len);
3577cda17bbSSumit Garg 		if (rc != 0)
3587cda17bbSSumit Garg 			return rc;
3597cda17bbSSumit Garg 		break;
3607cda17bbSSumit Garg 	default:
3617cda17bbSSumit Garg 		return CRYPTO_ERR_DECRYPTION;
3627cda17bbSSumit Garg 	}
3637cda17bbSSumit Garg 
3647cda17bbSSumit Garg 	return CRYPTO_SUCCESS;
3657cda17bbSSumit Garg }
3667cda17bbSSumit Garg #endif /* TF_MBEDTLS_USE_AES_GCM */
3677cda17bbSSumit Garg 
3687d37aa17SJuan Castillo /*
3697d37aa17SJuan Castillo  * Register crypto library descriptor
3707d37aa17SJuan Castillo  */
371*0aa0b3afSManish V Badarkhe #if MEASURED_BOOT && TRUSTED_BOARD_BOOT
3727cda17bbSSumit Garg #if TF_MBEDTLS_USE_AES_GCM
3737cda17bbSSumit Garg REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
3747cda17bbSSumit Garg 		    auth_decrypt);
3758c105290SAlexei Fedorov #else
3767cda17bbSSumit Garg REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
3777cda17bbSSumit Garg 		    NULL);
3787cda17bbSSumit Garg #endif
379*0aa0b3afSManish V Badarkhe #elif TRUSTED_BOARD_BOOT
3807cda17bbSSumit Garg #if TF_MBEDTLS_USE_AES_GCM
3817cda17bbSSumit Garg REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash,
3827cda17bbSSumit Garg 		    auth_decrypt);
3837cda17bbSSumit Garg #else
3847cda17bbSSumit Garg REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL);
3857cda17bbSSumit Garg #endif
386*0aa0b3afSManish V Badarkhe #elif MEASURED_BOOT
387*0aa0b3afSManish V Badarkhe REGISTER_CRYPTO_LIB(LIB_NAME, init, calc_hash);
388*0aa0b3afSManish V Badarkhe #endif /* MEASURED_BOOT && TRUSTED_BOARD_BOOT */
389