xref: /rk3399_ARM-atf/drivers/auth/mbedtls/mbedtls_crypto.c (revision 7cda17bb0f92db39d123a4f2a1732c9978556453)
17d37aa17SJuan Castillo /*
28c105290SAlexei Fedorov  * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
37d37aa17SJuan Castillo  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
57d37aa17SJuan Castillo  */
67d37aa17SJuan Castillo 
7*7cda17bbSSumit Garg #include <assert.h>
87d37aa17SJuan Castillo #include <stddef.h>
97d37aa17SJuan Castillo #include <string.h>
107d37aa17SJuan Castillo 
117d37aa17SJuan Castillo /* mbed TLS headers */
12*7cda17bbSSumit 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>
177d37aa17SJuan Castillo 
1809d40e0eSAntonio Nino Diaz #include <common/debug.h>
1909d40e0eSAntonio Nino Diaz #include <drivers/auth/crypto_mod.h>
2009d40e0eSAntonio Nino Diaz #include <drivers/auth/mbedtls/mbedtls_common.h>
2109d40e0eSAntonio Nino Diaz #include <drivers/auth/mbedtls/mbedtls_config.h>
22*7cda17bbSSumit Garg #include <plat/common/platform.h>
2309d40e0eSAntonio Nino Diaz 
247d37aa17SJuan Castillo #define LIB_NAME		"mbed TLS"
257d37aa17SJuan Castillo 
267d37aa17SJuan Castillo /*
277d37aa17SJuan Castillo  * AlgorithmIdentifier  ::=  SEQUENCE  {
287d37aa17SJuan Castillo  *     algorithm               OBJECT IDENTIFIER,
297d37aa17SJuan Castillo  *     parameters              ANY DEFINED BY algorithm OPTIONAL
307d37aa17SJuan Castillo  * }
317d37aa17SJuan Castillo  *
327d37aa17SJuan Castillo  * SubjectPublicKeyInfo  ::=  SEQUENCE  {
337d37aa17SJuan Castillo  *     algorithm            AlgorithmIdentifier,
347d37aa17SJuan Castillo  *     subjectPublicKey     BIT STRING
357d37aa17SJuan Castillo  * }
367d37aa17SJuan Castillo  *
377d37aa17SJuan Castillo  * DigestInfo ::= SEQUENCE {
387d37aa17SJuan Castillo  *     digestAlgorithm AlgorithmIdentifier,
397d37aa17SJuan Castillo  *     digest OCTET STRING
407d37aa17SJuan Castillo  * }
417d37aa17SJuan Castillo  */
427d37aa17SJuan Castillo 
437d37aa17SJuan Castillo /*
447d37aa17SJuan Castillo  * Initialize the library and export the descriptor
457d37aa17SJuan Castillo  */
467d37aa17SJuan Castillo static void init(void)
477d37aa17SJuan Castillo {
487d37aa17SJuan Castillo 	/* Initialize mbed TLS */
497d37aa17SJuan Castillo 	mbedtls_init();
507d37aa17SJuan Castillo }
517d37aa17SJuan Castillo 
527d37aa17SJuan Castillo /*
537d37aa17SJuan Castillo  * Verify a signature.
547d37aa17SJuan Castillo  *
557d37aa17SJuan Castillo  * Parameters are passed using the DER encoding format following the ASN.1
567d37aa17SJuan Castillo  * structures detailed above.
577d37aa17SJuan Castillo  */
587d37aa17SJuan Castillo static int verify_signature(void *data_ptr, unsigned int data_len,
597d37aa17SJuan Castillo 			    void *sig_ptr, unsigned int sig_len,
607d37aa17SJuan Castillo 			    void *sig_alg, unsigned int sig_alg_len,
617d37aa17SJuan Castillo 			    void *pk_ptr, unsigned int pk_len)
627d37aa17SJuan Castillo {
63649dbf6fSJuan Castillo 	mbedtls_asn1_buf sig_oid, sig_params;
64649dbf6fSJuan Castillo 	mbedtls_asn1_buf signature;
65649dbf6fSJuan Castillo 	mbedtls_md_type_t md_alg;
66649dbf6fSJuan Castillo 	mbedtls_pk_type_t pk_alg;
671001202dSSoby Mathew 	mbedtls_pk_context pk = {0};
687d37aa17SJuan Castillo 	int rc;
697d37aa17SJuan Castillo 	void *sig_opts = NULL;
70649dbf6fSJuan Castillo 	const mbedtls_md_info_t *md_info;
717d37aa17SJuan Castillo 	unsigned char *p, *end;
72649dbf6fSJuan Castillo 	unsigned char hash[MBEDTLS_MD_MAX_SIZE];
737d37aa17SJuan Castillo 
747d37aa17SJuan Castillo 	/* Get pointers to signature OID and parameters */
757d37aa17SJuan Castillo 	p = (unsigned char *)sig_alg;
767d37aa17SJuan Castillo 	end = (unsigned char *)(p + sig_alg_len);
77649dbf6fSJuan Castillo 	rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, &sig_params);
787d37aa17SJuan Castillo 	if (rc != 0) {
797d37aa17SJuan Castillo 		return CRYPTO_ERR_SIGNATURE;
807d37aa17SJuan Castillo 	}
817d37aa17SJuan Castillo 
827d37aa17SJuan Castillo 	/* Get the actual signature algorithm (MD + PK) */
831001202dSSoby Mathew 	rc = mbedtls_x509_get_sig_alg(&sig_oid, &sig_params, &md_alg, &pk_alg, &sig_opts);
847d37aa17SJuan Castillo 	if (rc != 0) {
857d37aa17SJuan Castillo 		return CRYPTO_ERR_SIGNATURE;
867d37aa17SJuan Castillo 	}
877d37aa17SJuan Castillo 
887d37aa17SJuan Castillo 	/* Parse the public key */
89649dbf6fSJuan Castillo 	mbedtls_pk_init(&pk);
907d37aa17SJuan Castillo 	p = (unsigned char *)pk_ptr;
917d37aa17SJuan Castillo 	end = (unsigned char *)(p + pk_len);
92649dbf6fSJuan Castillo 	rc = mbedtls_pk_parse_subpubkey(&p, end, &pk);
937d37aa17SJuan Castillo 	if (rc != 0) {
941001202dSSoby Mathew 		rc = CRYPTO_ERR_SIGNATURE;
951001202dSSoby Mathew 		goto end2;
967d37aa17SJuan Castillo 	}
977d37aa17SJuan Castillo 
987d37aa17SJuan Castillo 	/* Get the signature (bitstring) */
997d37aa17SJuan Castillo 	p = (unsigned char *)sig_ptr;
1007d37aa17SJuan Castillo 	end = (unsigned char *)(p + sig_len);
1017d37aa17SJuan Castillo 	signature.tag = *p;
102649dbf6fSJuan Castillo 	rc = mbedtls_asn1_get_bitstring_null(&p, end, &signature.len);
1037d37aa17SJuan Castillo 	if (rc != 0) {
1047d37aa17SJuan Castillo 		rc = CRYPTO_ERR_SIGNATURE;
1051001202dSSoby Mathew 		goto end1;
1067d37aa17SJuan Castillo 	}
1077d37aa17SJuan Castillo 	signature.p = p;
1087d37aa17SJuan Castillo 
1097d37aa17SJuan Castillo 	/* Calculate the hash of the data */
110649dbf6fSJuan Castillo 	md_info = mbedtls_md_info_from_type(md_alg);
1117d37aa17SJuan Castillo 	if (md_info == NULL) {
1127d37aa17SJuan Castillo 		rc = CRYPTO_ERR_SIGNATURE;
1131001202dSSoby Mathew 		goto end1;
1147d37aa17SJuan Castillo 	}
1157d37aa17SJuan Castillo 	p = (unsigned char *)data_ptr;
116649dbf6fSJuan Castillo 	rc = mbedtls_md(md_info, p, data_len, hash);
1177d37aa17SJuan Castillo 	if (rc != 0) {
1187d37aa17SJuan Castillo 		rc = CRYPTO_ERR_SIGNATURE;
1191001202dSSoby Mathew 		goto end1;
1207d37aa17SJuan Castillo 	}
1217d37aa17SJuan Castillo 
1227d37aa17SJuan Castillo 	/* Verify the signature */
123649dbf6fSJuan Castillo 	rc = mbedtls_pk_verify_ext(pk_alg, sig_opts, &pk, md_alg, hash,
124649dbf6fSJuan Castillo 			mbedtls_md_get_size(md_info),
125649dbf6fSJuan Castillo 			signature.p, signature.len);
1267d37aa17SJuan Castillo 	if (rc != 0) {
1277d37aa17SJuan Castillo 		rc = CRYPTO_ERR_SIGNATURE;
1281001202dSSoby Mathew 		goto end1;
1297d37aa17SJuan Castillo 	}
1307d37aa17SJuan Castillo 
1317d37aa17SJuan Castillo 	/* Signature verification success */
1327d37aa17SJuan Castillo 	rc = CRYPTO_SUCCESS;
1337d37aa17SJuan Castillo 
1341001202dSSoby Mathew end1:
135649dbf6fSJuan Castillo 	mbedtls_pk_free(&pk);
1361001202dSSoby Mathew end2:
1371001202dSSoby Mathew 	mbedtls_free(sig_opts);
1387d37aa17SJuan Castillo 	return rc;
1397d37aa17SJuan Castillo }
1407d37aa17SJuan Castillo 
1417d37aa17SJuan Castillo /*
1427d37aa17SJuan Castillo  * Match a hash
1437d37aa17SJuan Castillo  *
1447d37aa17SJuan Castillo  * Digest info is passed in DER format following the ASN.1 structure detailed
1457d37aa17SJuan Castillo  * above.
1467d37aa17SJuan Castillo  */
1477d37aa17SJuan Castillo static int verify_hash(void *data_ptr, unsigned int data_len,
1487d37aa17SJuan Castillo 		       void *digest_info_ptr, unsigned int digest_info_len)
1497d37aa17SJuan Castillo {
150649dbf6fSJuan Castillo 	mbedtls_asn1_buf hash_oid, params;
151649dbf6fSJuan Castillo 	mbedtls_md_type_t md_alg;
152649dbf6fSJuan Castillo 	const mbedtls_md_info_t *md_info;
1537d37aa17SJuan Castillo 	unsigned char *p, *end, *hash;
154649dbf6fSJuan Castillo 	unsigned char data_hash[MBEDTLS_MD_MAX_SIZE];
1557d37aa17SJuan Castillo 	size_t len;
1567d37aa17SJuan Castillo 	int rc;
1577d37aa17SJuan Castillo 
158649dbf6fSJuan Castillo 	/* Digest info should be an MBEDTLS_ASN1_SEQUENCE */
1597d37aa17SJuan Castillo 	p = (unsigned char *)digest_info_ptr;
160aa856917SSandrine Bailleux 	end = p + digest_info_len;
161649dbf6fSJuan Castillo 	rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
162649dbf6fSJuan Castillo 				  MBEDTLS_ASN1_SEQUENCE);
1637d37aa17SJuan Castillo 	if (rc != 0) {
1647d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
1657d37aa17SJuan Castillo 	}
1667d37aa17SJuan Castillo 
1677d37aa17SJuan Castillo 	/* Get the hash algorithm */
168649dbf6fSJuan Castillo 	rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, &params);
1697d37aa17SJuan Castillo 	if (rc != 0) {
1707d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
1717d37aa17SJuan Castillo 	}
1727d37aa17SJuan Castillo 
173649dbf6fSJuan Castillo 	rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg);
1747d37aa17SJuan Castillo 	if (rc != 0) {
1757d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
1767d37aa17SJuan Castillo 	}
1777d37aa17SJuan Castillo 
178649dbf6fSJuan Castillo 	md_info = mbedtls_md_info_from_type(md_alg);
1797d37aa17SJuan Castillo 	if (md_info == NULL) {
1807d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
1817d37aa17SJuan Castillo 	}
1827d37aa17SJuan Castillo 
1837d37aa17SJuan Castillo 	/* Hash should be octet string type */
184649dbf6fSJuan Castillo 	rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
1857d37aa17SJuan Castillo 	if (rc != 0) {
1867d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
1877d37aa17SJuan Castillo 	}
1887d37aa17SJuan Castillo 
1897d37aa17SJuan Castillo 	/* Length of hash must match the algorithm's size */
190649dbf6fSJuan Castillo 	if (len != mbedtls_md_get_size(md_info)) {
1917d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
1927d37aa17SJuan Castillo 	}
1937d37aa17SJuan Castillo 	hash = p;
1947d37aa17SJuan Castillo 
1957d37aa17SJuan Castillo 	/* Calculate the hash of the data */
1967d37aa17SJuan Castillo 	p = (unsigned char *)data_ptr;
197649dbf6fSJuan Castillo 	rc = mbedtls_md(md_info, p, data_len, data_hash);
1987d37aa17SJuan Castillo 	if (rc != 0) {
1997d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
2007d37aa17SJuan Castillo 	}
2017d37aa17SJuan Castillo 
2027d37aa17SJuan Castillo 	/* Compare values */
203fabd21adSAntonio Nino Diaz 	rc = memcmp(data_hash, hash, mbedtls_md_get_size(md_info));
2047d37aa17SJuan Castillo 	if (rc != 0) {
2057d37aa17SJuan Castillo 		return CRYPTO_ERR_HASH;
2067d37aa17SJuan Castillo 	}
2077d37aa17SJuan Castillo 
2087d37aa17SJuan Castillo 	return CRYPTO_SUCCESS;
2097d37aa17SJuan Castillo }
2107d37aa17SJuan Castillo 
2118c105290SAlexei Fedorov #if MEASURED_BOOT
2128c105290SAlexei Fedorov /*
2138c105290SAlexei Fedorov  * Calculate a hash
2148c105290SAlexei Fedorov  *
2158c105290SAlexei Fedorov  * output points to the computed hash
2168c105290SAlexei Fedorov  */
2178c105290SAlexei Fedorov int calc_hash(unsigned int alg, void *data_ptr,
2188c105290SAlexei Fedorov 	      unsigned int data_len, unsigned char *output)
2198c105290SAlexei Fedorov {
2208c105290SAlexei Fedorov 	const mbedtls_md_info_t *md_info;
2218c105290SAlexei Fedorov 
2228c105290SAlexei Fedorov 	md_info = mbedtls_md_info_from_type((mbedtls_md_type_t)alg);
2238c105290SAlexei Fedorov 	if (md_info == NULL) {
2248c105290SAlexei Fedorov 		return CRYPTO_ERR_HASH;
2258c105290SAlexei Fedorov 	}
2268c105290SAlexei Fedorov 
2278c105290SAlexei Fedorov 	/* Calculate the hash of the data */
2288c105290SAlexei Fedorov 	return mbedtls_md(md_info, data_ptr, data_len, output);
2298c105290SAlexei Fedorov }
2308c105290SAlexei Fedorov #endif /* MEASURED_BOOT */
2318c105290SAlexei Fedorov 
232*7cda17bbSSumit Garg #if TF_MBEDTLS_USE_AES_GCM
233*7cda17bbSSumit Garg /*
234*7cda17bbSSumit Garg  * Stack based buffer allocation for decryption operation. It could
235*7cda17bbSSumit Garg  * be configured to balance stack usage vs execution speed.
236*7cda17bbSSumit Garg  */
237*7cda17bbSSumit Garg #define DEC_OP_BUF_SIZE		128
238*7cda17bbSSumit Garg 
239*7cda17bbSSumit Garg static int aes_gcm_decrypt(void *data_ptr, size_t len, const void *key,
240*7cda17bbSSumit Garg 			   unsigned int key_len, const void *iv,
241*7cda17bbSSumit Garg 			   unsigned int iv_len, const void *tag,
242*7cda17bbSSumit Garg 			   unsigned int tag_len)
243*7cda17bbSSumit Garg {
244*7cda17bbSSumit Garg 	mbedtls_gcm_context ctx;
245*7cda17bbSSumit Garg 	mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
246*7cda17bbSSumit Garg 	unsigned char buf[DEC_OP_BUF_SIZE];
247*7cda17bbSSumit Garg 	unsigned char tag_buf[CRYPTO_MAX_TAG_SIZE];
248*7cda17bbSSumit Garg 	unsigned char *pt = data_ptr;
249*7cda17bbSSumit Garg 	size_t dec_len;
250*7cda17bbSSumit Garg 	int diff, i, rc;
251*7cda17bbSSumit Garg 
252*7cda17bbSSumit Garg 	mbedtls_gcm_init(&ctx);
253*7cda17bbSSumit Garg 
254*7cda17bbSSumit Garg 	rc = mbedtls_gcm_setkey(&ctx, cipher, key, key_len * 8);
255*7cda17bbSSumit Garg 	if (rc != 0) {
256*7cda17bbSSumit Garg 		rc = CRYPTO_ERR_DECRYPTION;
257*7cda17bbSSumit Garg 		goto exit_gcm;
258*7cda17bbSSumit Garg 	}
259*7cda17bbSSumit Garg 
260*7cda17bbSSumit Garg 	rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len, NULL, 0);
261*7cda17bbSSumit Garg 	if (rc != 0) {
262*7cda17bbSSumit Garg 		rc = CRYPTO_ERR_DECRYPTION;
263*7cda17bbSSumit Garg 		goto exit_gcm;
264*7cda17bbSSumit Garg 	}
265*7cda17bbSSumit Garg 
266*7cda17bbSSumit Garg 	while (len > 0) {
267*7cda17bbSSumit Garg 		dec_len = MIN(sizeof(buf), len);
268*7cda17bbSSumit Garg 
269*7cda17bbSSumit Garg 		rc = mbedtls_gcm_update(&ctx, dec_len, pt, buf);
270*7cda17bbSSumit Garg 		if (rc != 0) {
271*7cda17bbSSumit Garg 			rc = CRYPTO_ERR_DECRYPTION;
272*7cda17bbSSumit Garg 			goto exit_gcm;
273*7cda17bbSSumit Garg 		}
274*7cda17bbSSumit Garg 
275*7cda17bbSSumit Garg 		memcpy(pt, buf, dec_len);
276*7cda17bbSSumit Garg 		pt += dec_len;
277*7cda17bbSSumit Garg 		len -= dec_len;
278*7cda17bbSSumit Garg 	}
279*7cda17bbSSumit Garg 
280*7cda17bbSSumit Garg 	rc = mbedtls_gcm_finish(&ctx, tag_buf, sizeof(tag_buf));
281*7cda17bbSSumit Garg 	if (rc != 0) {
282*7cda17bbSSumit Garg 		rc = CRYPTO_ERR_DECRYPTION;
283*7cda17bbSSumit Garg 		goto exit_gcm;
284*7cda17bbSSumit Garg 	}
285*7cda17bbSSumit Garg 
286*7cda17bbSSumit Garg 	/* Check tag in "constant-time" */
287*7cda17bbSSumit Garg 	for (diff = 0, i = 0; i < tag_len; i++)
288*7cda17bbSSumit Garg 		diff |= ((const unsigned char *)tag)[i] ^ tag_buf[i];
289*7cda17bbSSumit Garg 
290*7cda17bbSSumit Garg 	if (diff != 0) {
291*7cda17bbSSumit Garg 		rc = CRYPTO_ERR_DECRYPTION;
292*7cda17bbSSumit Garg 		goto exit_gcm;
293*7cda17bbSSumit Garg 	}
294*7cda17bbSSumit Garg 
295*7cda17bbSSumit Garg 	/* GCM decryption success */
296*7cda17bbSSumit Garg 	rc = CRYPTO_SUCCESS;
297*7cda17bbSSumit Garg 
298*7cda17bbSSumit Garg exit_gcm:
299*7cda17bbSSumit Garg 	mbedtls_gcm_free(&ctx);
300*7cda17bbSSumit Garg 	return rc;
301*7cda17bbSSumit Garg }
302*7cda17bbSSumit Garg 
303*7cda17bbSSumit Garg /*
304*7cda17bbSSumit Garg  * Authenticated decryption of an image
305*7cda17bbSSumit Garg  */
306*7cda17bbSSumit Garg static int auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
307*7cda17bbSSumit Garg 			size_t len, const void *key, unsigned int key_len,
308*7cda17bbSSumit Garg 			unsigned int key_flags, const void *iv,
309*7cda17bbSSumit Garg 			unsigned int iv_len, const void *tag,
310*7cda17bbSSumit Garg 			unsigned int tag_len)
311*7cda17bbSSumit Garg {
312*7cda17bbSSumit Garg 	int rc;
313*7cda17bbSSumit Garg 
314*7cda17bbSSumit Garg 	assert((key_flags & ENC_KEY_IS_IDENTIFIER) == 0);
315*7cda17bbSSumit Garg 
316*7cda17bbSSumit Garg 	switch (dec_algo) {
317*7cda17bbSSumit Garg 	case CRYPTO_GCM_DECRYPT:
318*7cda17bbSSumit Garg 		rc = aes_gcm_decrypt(data_ptr, len, key, key_len, iv, iv_len,
319*7cda17bbSSumit Garg 				     tag, tag_len);
320*7cda17bbSSumit Garg 		if (rc != 0)
321*7cda17bbSSumit Garg 			return rc;
322*7cda17bbSSumit Garg 		break;
323*7cda17bbSSumit Garg 	default:
324*7cda17bbSSumit Garg 		return CRYPTO_ERR_DECRYPTION;
325*7cda17bbSSumit Garg 	}
326*7cda17bbSSumit Garg 
327*7cda17bbSSumit Garg 	return CRYPTO_SUCCESS;
328*7cda17bbSSumit Garg }
329*7cda17bbSSumit Garg #endif /* TF_MBEDTLS_USE_AES_GCM */
330*7cda17bbSSumit Garg 
3317d37aa17SJuan Castillo /*
3327d37aa17SJuan Castillo  * Register crypto library descriptor
3337d37aa17SJuan Castillo  */
3348c105290SAlexei Fedorov #if MEASURED_BOOT
335*7cda17bbSSumit Garg #if TF_MBEDTLS_USE_AES_GCM
336*7cda17bbSSumit Garg REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
337*7cda17bbSSumit Garg 		    auth_decrypt);
3388c105290SAlexei Fedorov #else
339*7cda17bbSSumit Garg REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
340*7cda17bbSSumit Garg 		    NULL);
341*7cda17bbSSumit Garg #endif
342*7cda17bbSSumit Garg #else /* MEASURED_BOOT */
343*7cda17bbSSumit Garg #if TF_MBEDTLS_USE_AES_GCM
344*7cda17bbSSumit Garg REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash,
345*7cda17bbSSumit Garg 		    auth_decrypt);
346*7cda17bbSSumit Garg #else
347*7cda17bbSSumit Garg REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL);
348*7cda17bbSSumit Garg #endif
3498c105290SAlexei Fedorov #endif /* MEASURED_BOOT */
350