xref: /rk3399_ARM-atf/drivers/auth/crypto_mod.c (revision f1318bffd4615701d3043df8b569e56a5dba074e)
105799ae0SJuan Castillo /*
2*0331bd22SLauren Wehrmeister  * Copyright (c) 2015-2025, Arm Limited and Contributors. All rights reserved.
305799ae0SJuan Castillo  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
505799ae0SJuan Castillo  */
605799ae0SJuan Castillo 
705799ae0SJuan Castillo #include <assert.h>
809d40e0eSAntonio Nino Diaz 
909d40e0eSAntonio Nino Diaz #include <common/debug.h>
1009d40e0eSAntonio Nino Diaz #include <drivers/auth/crypto_mod.h>
1105799ae0SJuan Castillo 
1205799ae0SJuan Castillo /* Variable exported by the crypto library through REGISTER_CRYPTO_LIB() */
1305799ae0SJuan Castillo 
1405799ae0SJuan Castillo /*
1505799ae0SJuan Castillo  * The crypto module is responsible for verifying digital signatures and hashes.
1605799ae0SJuan Castillo  * It relies on a crypto library to perform the cryptographic operations.
1705799ae0SJuan Castillo  *
1805799ae0SJuan Castillo  * The crypto module itself does not impose any specific format on signatures,
1905799ae0SJuan Castillo  * signature algorithm, keys or hashes, but most cryptographic libraries will
2005799ae0SJuan Castillo  * take the parameters as the following DER encoded ASN.1 structures:
2105799ae0SJuan Castillo  *
2205799ae0SJuan Castillo  *     AlgorithmIdentifier ::= SEQUENCE  {
2305799ae0SJuan Castillo  *         algorithm        OBJECT IDENTIFIER,
2405799ae0SJuan Castillo  *         parameters       ANY DEFINED BY algorithm OPTIONAL
2505799ae0SJuan Castillo  *     }
2605799ae0SJuan Castillo  *
2705799ae0SJuan Castillo  *     DigestInfo ::= SEQUENCE {
2805799ae0SJuan Castillo  *         digestAlgorithm  AlgorithmIdentifier,
2905799ae0SJuan Castillo  *         digest           OCTET STRING
3005799ae0SJuan Castillo  *     }
3105799ae0SJuan Castillo  *
3205799ae0SJuan Castillo  *     SubjectPublicKeyInfo ::= SEQUENCE  {
3305799ae0SJuan Castillo  *         algorithm        AlgorithmIdentifier,
3405799ae0SJuan Castillo  *         subjectPublicKey BIT STRING
3505799ae0SJuan Castillo  *     }
3605799ae0SJuan Castillo  *
3705799ae0SJuan Castillo  *     SignatureAlgorithm ::= AlgorithmIdentifier
3805799ae0SJuan Castillo  *
3905799ae0SJuan Castillo  *     SignatureValue ::= BIT STRING
4005799ae0SJuan Castillo  */
4105799ae0SJuan Castillo 
4205799ae0SJuan Castillo /*
4305799ae0SJuan Castillo  * Perform some static checking and call the library initialization function
4405799ae0SJuan Castillo  */
crypto_mod_init(void)4505799ae0SJuan Castillo void crypto_mod_init(void)
4605799ae0SJuan Castillo {
4705799ae0SJuan Castillo 	assert(crypto_lib_desc.name != NULL);
4805799ae0SJuan Castillo 	assert(crypto_lib_desc.init != NULL);
492bf4f27fSManish V Badarkhe #if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
502bf4f27fSManish V Badarkhe CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
5105799ae0SJuan Castillo 	assert(crypto_lib_desc.verify_signature != NULL);
5205799ae0SJuan Castillo 	assert(crypto_lib_desc.verify_hash != NULL);
532bf4f27fSManish V Badarkhe #endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
542bf4f27fSManish V Badarkhe 	  CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
552bf4f27fSManish V Badarkhe 
562bf4f27fSManish V Badarkhe #if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
572bf4f27fSManish V Badarkhe CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
580aa0b3afSManish V Badarkhe 	assert(crypto_lib_desc.calc_hash != NULL);
592bf4f27fSManish V Badarkhe #endif /* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
602bf4f27fSManish V Badarkhe 	  CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
6105799ae0SJuan Castillo 
6205799ae0SJuan Castillo 	/* Initialize the cryptographic library */
6305799ae0SJuan Castillo 	crypto_lib_desc.init();
6405799ae0SJuan Castillo 	INFO("Using crypto library '%s'\n", crypto_lib_desc.name);
6505799ae0SJuan Castillo }
6605799ae0SJuan Castillo 
672bf4f27fSManish V Badarkhe #if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
682bf4f27fSManish V Badarkhe CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
6905799ae0SJuan Castillo /*
7005799ae0SJuan Castillo  * Function to verify a digital signature
7105799ae0SJuan Castillo  *
7205799ae0SJuan Castillo  * Parameters:
7305799ae0SJuan Castillo  *
7405799ae0SJuan Castillo  *   data_ptr, data_len: signed data
7505799ae0SJuan Castillo  *   sig_ptr, sig_len: the digital signature
7605799ae0SJuan Castillo  *   sig_alg_ptr, sig_alg_len: the digital signature algorithm
7705799ae0SJuan Castillo  *   pk_ptr, pk_len: the public key
7805799ae0SJuan Castillo  */
crypto_mod_verify_signature(void * data_ptr,unsigned int data_len,void * sig_ptr,unsigned int sig_len,void * sig_alg_ptr,unsigned int sig_alg_len,void * pk_ptr,unsigned int pk_len)7905799ae0SJuan Castillo int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len,
8005799ae0SJuan Castillo 				void *sig_ptr, unsigned int sig_len,
8105799ae0SJuan Castillo 				void *sig_alg_ptr, unsigned int sig_alg_len,
8205799ae0SJuan Castillo 				void *pk_ptr, unsigned int pk_len)
8305799ae0SJuan Castillo {
8405799ae0SJuan Castillo 	assert(data_ptr != NULL);
8505799ae0SJuan Castillo 	assert(data_len != 0);
8605799ae0SJuan Castillo 	assert(sig_ptr != NULL);
8705799ae0SJuan Castillo 	assert(sig_len != 0);
8805799ae0SJuan Castillo 	assert(sig_alg_ptr != NULL);
8905799ae0SJuan Castillo 	assert(sig_alg_len != 0);
9005799ae0SJuan Castillo 	assert(pk_ptr != NULL);
9105799ae0SJuan Castillo 	assert(pk_len != 0);
9205799ae0SJuan Castillo 
9305799ae0SJuan Castillo 	return crypto_lib_desc.verify_signature(data_ptr, data_len,
9405799ae0SJuan Castillo 						sig_ptr, sig_len,
9505799ae0SJuan Castillo 						sig_alg_ptr, sig_alg_len,
9605799ae0SJuan Castillo 						pk_ptr, pk_len);
9705799ae0SJuan Castillo }
9805799ae0SJuan Castillo 
9905799ae0SJuan Castillo /*
10005799ae0SJuan Castillo  * Verify a hash by comparison
10105799ae0SJuan Castillo  *
10205799ae0SJuan Castillo  * Parameters:
10305799ae0SJuan Castillo  *
10405799ae0SJuan Castillo  *   data_ptr, data_len: data to be hashed
10505799ae0SJuan Castillo  *   digest_info_ptr, digest_info_len: hash to be compared
10605799ae0SJuan Castillo  */
crypto_mod_verify_hash(void * data_ptr,unsigned int data_len,void * digest_info_ptr,unsigned int digest_info_len)10705799ae0SJuan Castillo int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len,
10805799ae0SJuan Castillo 			   void *digest_info_ptr, unsigned int digest_info_len)
10905799ae0SJuan Castillo {
11005799ae0SJuan Castillo 	assert(data_ptr != NULL);
11105799ae0SJuan Castillo 	assert(data_len != 0);
11205799ae0SJuan Castillo 	assert(digest_info_ptr != NULL);
11305799ae0SJuan Castillo 	assert(digest_info_len != 0);
11405799ae0SJuan Castillo 
11505799ae0SJuan Castillo 	return crypto_lib_desc.verify_hash(data_ptr, data_len,
11605799ae0SJuan Castillo 					   digest_info_ptr, digest_info_len);
11705799ae0SJuan Castillo }
1182bf4f27fSManish V Badarkhe #endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
1192bf4f27fSManish V Badarkhe 	  CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
1208c105290SAlexei Fedorov 
1212bf4f27fSManish V Badarkhe #if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
1222bf4f27fSManish V Badarkhe CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
1238c105290SAlexei Fedorov /*
1248c105290SAlexei Fedorov  * Calculate a hash
1258c105290SAlexei Fedorov  *
1268c105290SAlexei Fedorov  * Parameters:
1278c105290SAlexei Fedorov  *
1288c105290SAlexei Fedorov  *   alg: message digest algorithm
1298c105290SAlexei Fedorov  *   data_ptr, data_len: data to be hashed
1308c105290SAlexei Fedorov  *   output: resulting hash
1318c105290SAlexei Fedorov  */
crypto_mod_calc_hash(enum crypto_md_algo alg,void * data_ptr,unsigned int data_len,unsigned char output[CRYPTO_MD_MAX_SIZE])13214db963fSManish V Badarkhe int crypto_mod_calc_hash(enum crypto_md_algo alg, void *data_ptr,
13314db963fSManish V Badarkhe 			 unsigned int data_len,
13414db963fSManish V Badarkhe 			 unsigned char output[CRYPTO_MD_MAX_SIZE])
1358c105290SAlexei Fedorov {
1368c105290SAlexei Fedorov 	assert(data_ptr != NULL);
1378c105290SAlexei Fedorov 	assert(data_len != 0);
1388c105290SAlexei Fedorov 	assert(output != NULL);
1398c105290SAlexei Fedorov 
1408c105290SAlexei Fedorov 	return crypto_lib_desc.calc_hash(alg, data_ptr, data_len, output);
1418c105290SAlexei Fedorov }
1422bf4f27fSManish V Badarkhe #endif /* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
1432bf4f27fSManish V Badarkhe 	  CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
1447cda17bbSSumit Garg 
crypto_mod_convert_pk(void * full_pk_ptr,unsigned int full_pk_len,void ** hashed_pk_ptr,unsigned int * hashed_pk_len)1454ac5b394SYann Gautier int crypto_mod_convert_pk(void *full_pk_ptr, unsigned int full_pk_len,
1464ac5b394SYann Gautier 			  void **hashed_pk_ptr, unsigned int *hashed_pk_len)
1474ac5b394SYann Gautier {
1484ac5b394SYann Gautier 	if (crypto_lib_desc.convert_pk != NULL) {
1494ac5b394SYann Gautier 		return crypto_lib_desc.convert_pk(full_pk_ptr, full_pk_len,
1504ac5b394SYann Gautier 						  hashed_pk_ptr, hashed_pk_len);
1514ac5b394SYann Gautier 	}
1524ac5b394SYann Gautier 
1534ac5b394SYann Gautier 	*hashed_pk_ptr = full_pk_ptr;
1544ac5b394SYann Gautier 	*hashed_pk_len = full_pk_len;
1554ac5b394SYann Gautier 
1564ac5b394SYann Gautier 	return 0;
1574ac5b394SYann Gautier }
1584ac5b394SYann Gautier 
1597cda17bbSSumit Garg /*
1607cda17bbSSumit Garg  * Authenticated decryption of data
1617cda17bbSSumit Garg  *
1627cda17bbSSumit Garg  * Parameters:
1637cda17bbSSumit Garg  *
1647cda17bbSSumit Garg  *   dec_algo: authenticated decryption algorithm
1657cda17bbSSumit Garg  *   data_ptr, len: data to be decrypted (inout param)
1667cda17bbSSumit Garg  *   key, key_len, key_flags: symmetric decryption key
1677cda17bbSSumit Garg  *   iv, iv_len: initialization vector
1687cda17bbSSumit Garg  *   tag, tag_len: authentication tag
1697cda17bbSSumit Garg  */
crypto_mod_auth_decrypt(enum crypto_dec_algo dec_algo,void * data_ptr,size_t len,const void * key,unsigned int key_len,unsigned int key_flags,const void * iv,unsigned int iv_len,const void * tag,unsigned int tag_len)1707cda17bbSSumit Garg int crypto_mod_auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
1717cda17bbSSumit Garg 			    size_t len, const void *key, unsigned int key_len,
1727cda17bbSSumit Garg 			    unsigned int key_flags, const void *iv,
1737cda17bbSSumit Garg 			    unsigned int iv_len, const void *tag,
1747cda17bbSSumit Garg 			    unsigned int tag_len)
1757cda17bbSSumit Garg {
1767cda17bbSSumit Garg 	assert(crypto_lib_desc.auth_decrypt != NULL);
1777cda17bbSSumit Garg 	assert(data_ptr != NULL);
1787cda17bbSSumit Garg 	assert(len != 0U);
1797cda17bbSSumit Garg 	assert(key != NULL);
1807cda17bbSSumit Garg 	assert(key_len != 0U);
1817cda17bbSSumit Garg 	assert(iv != NULL);
1827cda17bbSSumit Garg 	assert((iv_len != 0U) && (iv_len <= CRYPTO_MAX_IV_SIZE));
1837cda17bbSSumit Garg 	assert(tag != NULL);
1847cda17bbSSumit Garg 	assert((tag_len != 0U) && (tag_len <= CRYPTO_MAX_TAG_SIZE));
1857cda17bbSSumit Garg 
1867cda17bbSSumit Garg 	return crypto_lib_desc.auth_decrypt(dec_algo, data_ptr, len, key,
1877cda17bbSSumit Garg 					    key_len, key_flags, iv, iv_len, tag,
1887cda17bbSSumit Garg 					    tag_len);
1897cda17bbSSumit Garg }
190*0331bd22SLauren Wehrmeister 
191*0331bd22SLauren Wehrmeister /* Perform end of psa crypto usage calls to finish */
crypto_mod_finish(void)192*0331bd22SLauren Wehrmeister void crypto_mod_finish(void)
193*0331bd22SLauren Wehrmeister {
194*0331bd22SLauren Wehrmeister 	if (crypto_lib_desc.finish != NULL) {
195*0331bd22SLauren Wehrmeister 		crypto_lib_desc.finish();
196*0331bd22SLauren Wehrmeister 		INFO("Finished using crypto library '%s'\n", crypto_lib_desc.name);
197*0331bd22SLauren Wehrmeister 	}
198*0331bd22SLauren Wehrmeister }
199