xref: /rk3399_ARM-atf/include/drivers/auth/crypto_mod.h (revision 8c105290f3733eafb789e17da4a0649e85c7b360)
105799ae0SJuan Castillo /*
2*8c105290SAlexei 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,
1605799ae0SJuan Castillo 	CRYPTO_ERR_UNKNOWN
1705799ae0SJuan Castillo };
1805799ae0SJuan Castillo 
1905799ae0SJuan Castillo /*
2005799ae0SJuan Castillo  * Cryptographic library descriptor
2105799ae0SJuan Castillo  */
2205799ae0SJuan Castillo typedef struct crypto_lib_desc_s {
2305799ae0SJuan Castillo 	const char *name;
2405799ae0SJuan Castillo 
2505799ae0SJuan Castillo 	/* Initialize library. This function is not expected to fail. All errors
2605799ae0SJuan Castillo 	 * must be handled inside the function, asserting or panicing in case of
2705799ae0SJuan Castillo 	 * a non-recoverable error */
2805799ae0SJuan Castillo 	void (*init)(void);
2905799ae0SJuan Castillo 
3005799ae0SJuan Castillo 	/* Verify a digital signature. Return one of the
3105799ae0SJuan Castillo 	 * 'enum crypto_ret_value' options */
3205799ae0SJuan Castillo 	int (*verify_signature)(void *data_ptr, unsigned int data_len,
3305799ae0SJuan Castillo 				void *sig_ptr, unsigned int sig_len,
3405799ae0SJuan Castillo 				void *sig_alg, unsigned int sig_alg_len,
3505799ae0SJuan Castillo 				void *pk_ptr, unsigned int pk_len);
3605799ae0SJuan Castillo 
3705799ae0SJuan Castillo 	/* Verify a hash. Return one of the 'enum crypto_ret_value' options */
3805799ae0SJuan Castillo 	int (*verify_hash)(void *data_ptr, unsigned int data_len,
3905799ae0SJuan Castillo 			   void *digest_info_ptr, unsigned int digest_info_len);
40*8c105290SAlexei Fedorov 
41*8c105290SAlexei Fedorov #if MEASURED_BOOT
42*8c105290SAlexei Fedorov 	/* Calculate a hash. Return hash value */
43*8c105290SAlexei Fedorov 	int (*calc_hash)(unsigned int alg, void *data_ptr,
44*8c105290SAlexei Fedorov 			 unsigned int data_len, unsigned char *output);
45*8c105290SAlexei Fedorov #endif /* MEASURED_BOOT */
46*8c105290SAlexei Fedorov 
4705799ae0SJuan Castillo } crypto_lib_desc_t;
4805799ae0SJuan Castillo 
4905799ae0SJuan Castillo /* Public functions */
5005799ae0SJuan Castillo void crypto_mod_init(void);
5105799ae0SJuan Castillo int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len,
5205799ae0SJuan Castillo 				void *sig_ptr, unsigned int sig_len,
53735181b6SRoberto Vargas 				void *sig_alg_ptr, unsigned int sig_alg_len,
5405799ae0SJuan Castillo 				void *pk_ptr, unsigned int pk_len);
5505799ae0SJuan Castillo int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len,
5605799ae0SJuan Castillo 			   void *digest_info_ptr, unsigned int digest_info_len);
5705799ae0SJuan Castillo 
58*8c105290SAlexei Fedorov #if MEASURED_BOOT
59*8c105290SAlexei Fedorov int crypto_mod_calc_hash(unsigned int alg, void *data_ptr,
60*8c105290SAlexei Fedorov 			 unsigned int data_len, unsigned char *output);
61*8c105290SAlexei Fedorov 
6205799ae0SJuan Castillo /* Macro to register a cryptographic library */
63*8c105290SAlexei Fedorov #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \
64*8c105290SAlexei Fedorov 							     _calc_hash) \
65*8c105290SAlexei Fedorov 	const crypto_lib_desc_t crypto_lib_desc = { \
66*8c105290SAlexei Fedorov 		.name = _name, \
67*8c105290SAlexei Fedorov 		.init = _init, \
68*8c105290SAlexei Fedorov 		.verify_signature = _verify_signature, \
69*8c105290SAlexei Fedorov 		.verify_hash = _verify_hash, \
70*8c105290SAlexei Fedorov 		.calc_hash = _calc_hash \
71*8c105290SAlexei Fedorov 	}
72*8c105290SAlexei Fedorov #else
7305799ae0SJuan Castillo #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash) \
7405799ae0SJuan Castillo 	const crypto_lib_desc_t crypto_lib_desc = { \
7505799ae0SJuan Castillo 		.name = _name, \
7605799ae0SJuan Castillo 		.init = _init, \
7705799ae0SJuan Castillo 		.verify_signature = _verify_signature, \
7805799ae0SJuan Castillo 		.verify_hash = _verify_hash \
7905799ae0SJuan Castillo 	}
80*8c105290SAlexei Fedorov #endif	/* MEASURED_BOOT */
8105799ae0SJuan Castillo 
823b94189aSRoberto Vargas extern const crypto_lib_desc_t crypto_lib_desc;
833b94189aSRoberto Vargas 
84c3cf06f1SAntonio Nino Diaz #endif /* CRYPTO_MOD_H */
85