xref: /rk3399_ARM-atf/include/drivers/auth/crypto_mod.h (revision 7cda17bb0f92db39d123a4f2a1732c9978556453)
105799ae0SJuan Castillo /*
28c105290SAlexei Fedorov  * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
305799ae0SJuan Castillo  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
505799ae0SJuan Castillo  */
605799ae0SJuan Castillo 
7c3cf06f1SAntonio Nino Diaz #ifndef CRYPTO_MOD_H
8c3cf06f1SAntonio Nino Diaz #define CRYPTO_MOD_H
905799ae0SJuan Castillo 
1005799ae0SJuan Castillo /* Return values */
1105799ae0SJuan Castillo enum crypto_ret_value {
1205799ae0SJuan Castillo 	CRYPTO_SUCCESS = 0,
1305799ae0SJuan Castillo 	CRYPTO_ERR_INIT,
1405799ae0SJuan Castillo 	CRYPTO_ERR_HASH,
1505799ae0SJuan Castillo 	CRYPTO_ERR_SIGNATURE,
16*7cda17bbSSumit Garg 	CRYPTO_ERR_DECRYPTION,
1705799ae0SJuan Castillo 	CRYPTO_ERR_UNKNOWN
1805799ae0SJuan Castillo };
1905799ae0SJuan Castillo 
20*7cda17bbSSumit Garg #define CRYPTO_MAX_IV_SIZE		16U
21*7cda17bbSSumit Garg #define CRYPTO_MAX_TAG_SIZE		16U
22*7cda17bbSSumit Garg 
23*7cda17bbSSumit Garg /* Decryption algorithm */
24*7cda17bbSSumit Garg enum crypto_dec_algo {
25*7cda17bbSSumit Garg 	CRYPTO_GCM_DECRYPT = 0
26*7cda17bbSSumit Garg };
27*7cda17bbSSumit Garg 
2805799ae0SJuan Castillo /*
2905799ae0SJuan Castillo  * Cryptographic library descriptor
3005799ae0SJuan Castillo  */
3105799ae0SJuan Castillo typedef struct crypto_lib_desc_s {
3205799ae0SJuan Castillo 	const char *name;
3305799ae0SJuan Castillo 
3405799ae0SJuan Castillo 	/* Initialize library. This function is not expected to fail. All errors
3505799ae0SJuan Castillo 	 * must be handled inside the function, asserting or panicing in case of
3605799ae0SJuan Castillo 	 * a non-recoverable error */
3705799ae0SJuan Castillo 	void (*init)(void);
3805799ae0SJuan Castillo 
3905799ae0SJuan Castillo 	/* Verify a digital signature. Return one of the
4005799ae0SJuan Castillo 	 * 'enum crypto_ret_value' options */
4105799ae0SJuan Castillo 	int (*verify_signature)(void *data_ptr, unsigned int data_len,
4205799ae0SJuan Castillo 				void *sig_ptr, unsigned int sig_len,
4305799ae0SJuan Castillo 				void *sig_alg, unsigned int sig_alg_len,
4405799ae0SJuan Castillo 				void *pk_ptr, unsigned int pk_len);
4505799ae0SJuan Castillo 
4605799ae0SJuan Castillo 	/* Verify a hash. Return one of the 'enum crypto_ret_value' options */
4705799ae0SJuan Castillo 	int (*verify_hash)(void *data_ptr, unsigned int data_len,
4805799ae0SJuan Castillo 			   void *digest_info_ptr, unsigned int digest_info_len);
498c105290SAlexei Fedorov 
508c105290SAlexei Fedorov #if MEASURED_BOOT
518c105290SAlexei Fedorov 	/* Calculate a hash. Return hash value */
528c105290SAlexei Fedorov 	int (*calc_hash)(unsigned int alg, void *data_ptr,
538c105290SAlexei Fedorov 			 unsigned int data_len, unsigned char *output);
548c105290SAlexei Fedorov #endif /* MEASURED_BOOT */
558c105290SAlexei Fedorov 
56*7cda17bbSSumit Garg 	/*
57*7cda17bbSSumit Garg 	 * Authenticated decryption. Return one of the
58*7cda17bbSSumit Garg 	 * 'enum crypto_ret_value' options.
59*7cda17bbSSumit Garg 	 */
60*7cda17bbSSumit Garg 	int (*auth_decrypt)(enum crypto_dec_algo dec_algo, void *data_ptr,
61*7cda17bbSSumit Garg 			    size_t len, const void *key, unsigned int key_len,
62*7cda17bbSSumit Garg 			    unsigned int key_flags, const void *iv,
63*7cda17bbSSumit Garg 			    unsigned int iv_len, const void *tag,
64*7cda17bbSSumit Garg 			    unsigned int tag_len);
6505799ae0SJuan Castillo } crypto_lib_desc_t;
6605799ae0SJuan Castillo 
6705799ae0SJuan Castillo /* Public functions */
6805799ae0SJuan Castillo void crypto_mod_init(void);
6905799ae0SJuan Castillo int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len,
7005799ae0SJuan Castillo 				void *sig_ptr, unsigned int sig_len,
71735181b6SRoberto Vargas 				void *sig_alg_ptr, unsigned int sig_alg_len,
7205799ae0SJuan Castillo 				void *pk_ptr, unsigned int pk_len);
7305799ae0SJuan Castillo int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len,
7405799ae0SJuan Castillo 			   void *digest_info_ptr, unsigned int digest_info_len);
75*7cda17bbSSumit Garg int crypto_mod_auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
76*7cda17bbSSumit Garg 			    size_t len, const void *key, unsigned int key_len,
77*7cda17bbSSumit Garg 			    unsigned int key_flags, const void *iv,
78*7cda17bbSSumit Garg 			    unsigned int iv_len, const void *tag,
79*7cda17bbSSumit Garg 			    unsigned int tag_len);
8005799ae0SJuan Castillo 
818c105290SAlexei Fedorov #if MEASURED_BOOT
828c105290SAlexei Fedorov int crypto_mod_calc_hash(unsigned int alg, void *data_ptr,
838c105290SAlexei Fedorov 			 unsigned int data_len, unsigned char *output);
848c105290SAlexei Fedorov 
8505799ae0SJuan Castillo /* Macro to register a cryptographic library */
868c105290SAlexei Fedorov #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \
87*7cda17bbSSumit Garg 			    _calc_hash, _auth_decrypt) \
888c105290SAlexei Fedorov 	const crypto_lib_desc_t crypto_lib_desc = { \
898c105290SAlexei Fedorov 		.name = _name, \
908c105290SAlexei Fedorov 		.init = _init, \
918c105290SAlexei Fedorov 		.verify_signature = _verify_signature, \
928c105290SAlexei Fedorov 		.verify_hash = _verify_hash, \
93*7cda17bbSSumit Garg 		.calc_hash = _calc_hash, \
94*7cda17bbSSumit Garg 		.auth_decrypt = _auth_decrypt \
958c105290SAlexei Fedorov 	}
968c105290SAlexei Fedorov #else
97*7cda17bbSSumit Garg #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \
98*7cda17bbSSumit Garg 			    _auth_decrypt) \
9905799ae0SJuan Castillo 	const crypto_lib_desc_t crypto_lib_desc = { \
10005799ae0SJuan Castillo 		.name = _name, \
10105799ae0SJuan Castillo 		.init = _init, \
10205799ae0SJuan Castillo 		.verify_signature = _verify_signature, \
103*7cda17bbSSumit Garg 		.verify_hash = _verify_hash, \
104*7cda17bbSSumit Garg 		.auth_decrypt = _auth_decrypt \
10505799ae0SJuan Castillo 	}
1068c105290SAlexei Fedorov #endif	/* MEASURED_BOOT */
10705799ae0SJuan Castillo 
1083b94189aSRoberto Vargas extern const crypto_lib_desc_t crypto_lib_desc;
1093b94189aSRoberto Vargas 
110c3cf06f1SAntonio Nino Diaz #endif /* CRYPTO_MOD_H */
111