xref: /rk3399_ARM-atf/include/drivers/auth/crypto_mod.h (revision 0331bd22c6082776a3d82b3cd3a1e5771643562b)
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 
7c3cf06f1SAntonio Nino Diaz #ifndef CRYPTO_MOD_H
8c3cf06f1SAntonio Nino Diaz #define CRYPTO_MOD_H
905799ae0SJuan Castillo 
102bf4f27fSManish V Badarkhe #define	CRYPTO_AUTH_VERIFY_ONLY			1
112bf4f27fSManish V Badarkhe #define	CRYPTO_HASH_CALC_ONLY			2
122bf4f27fSManish V Badarkhe #define	CRYPTO_AUTH_VERIFY_AND_HASH_CALC	3
132bf4f27fSManish V Badarkhe 
1405799ae0SJuan Castillo /* Return values */
1505799ae0SJuan Castillo enum crypto_ret_value {
1605799ae0SJuan Castillo 	CRYPTO_SUCCESS = 0,
1705799ae0SJuan Castillo 	CRYPTO_ERR_INIT,
1805799ae0SJuan Castillo 	CRYPTO_ERR_HASH,
1905799ae0SJuan Castillo 	CRYPTO_ERR_SIGNATURE,
207cda17bbSSumit Garg 	CRYPTO_ERR_DECRYPTION,
2105799ae0SJuan Castillo 	CRYPTO_ERR_UNKNOWN
2205799ae0SJuan Castillo };
2305799ae0SJuan Castillo 
247cda17bbSSumit Garg #define CRYPTO_MAX_IV_SIZE		16U
257cda17bbSSumit Garg #define CRYPTO_MAX_TAG_SIZE		16U
267cda17bbSSumit Garg 
277cda17bbSSumit Garg /* Decryption algorithm */
287cda17bbSSumit Garg enum crypto_dec_algo {
297cda17bbSSumit Garg 	CRYPTO_GCM_DECRYPT = 0
307cda17bbSSumit Garg };
317cda17bbSSumit Garg 
3214db963fSManish V Badarkhe /* Message digest algorithm */
3314db963fSManish V Badarkhe enum crypto_md_algo {
3414db963fSManish V Badarkhe 	CRYPTO_MD_SHA256,
3514db963fSManish V Badarkhe 	CRYPTO_MD_SHA384,
3614db963fSManish V Badarkhe 	CRYPTO_MD_SHA512,
3714db963fSManish V Badarkhe };
3814db963fSManish V Badarkhe 
3914db963fSManish V Badarkhe /* Maximum size as per the known stronger hash algorithm i.e.SHA512 */
4014db963fSManish V Badarkhe #define CRYPTO_MD_MAX_SIZE		64U
4114db963fSManish V Badarkhe 
4205799ae0SJuan Castillo /*
4305799ae0SJuan Castillo  * Cryptographic library descriptor
4405799ae0SJuan Castillo  */
4505799ae0SJuan Castillo typedef struct crypto_lib_desc_s {
4605799ae0SJuan Castillo 	const char *name;
4705799ae0SJuan Castillo 
4805799ae0SJuan Castillo 	/* Initialize library. This function is not expected to fail. All errors
491b491eeaSElyes Haouas 	 * must be handled inside the function, asserting or panicking in case of
5005799ae0SJuan Castillo 	 * a non-recoverable error */
5105799ae0SJuan Castillo 	void (*init)(void);
5205799ae0SJuan Castillo 
5305799ae0SJuan Castillo 	/* Verify a digital signature. Return one of the
5405799ae0SJuan Castillo 	 * 'enum crypto_ret_value' options */
5505799ae0SJuan Castillo 	int (*verify_signature)(void *data_ptr, unsigned int data_len,
5605799ae0SJuan Castillo 				void *sig_ptr, unsigned int sig_len,
5705799ae0SJuan Castillo 				void *sig_alg, unsigned int sig_alg_len,
5805799ae0SJuan Castillo 				void *pk_ptr, unsigned int pk_len);
5905799ae0SJuan Castillo 
6005799ae0SJuan Castillo 	/* Verify a hash. Return one of the 'enum crypto_ret_value' options */
6105799ae0SJuan Castillo 	int (*verify_hash)(void *data_ptr, unsigned int data_len,
6205799ae0SJuan Castillo 			   void *digest_info_ptr, unsigned int digest_info_len);
638c105290SAlexei Fedorov 
648c105290SAlexei Fedorov 	/* Calculate a hash. Return hash value */
6514db963fSManish V Badarkhe 	int (*calc_hash)(enum crypto_md_algo md_alg, void *data_ptr,
6614db963fSManish V Badarkhe 			 unsigned int data_len,
6714db963fSManish V Badarkhe 			 unsigned char output[CRYPTO_MD_MAX_SIZE]);
688c105290SAlexei Fedorov 
694ac5b394SYann Gautier 	/* Convert Public key (optional) */
704ac5b394SYann Gautier 	int (*convert_pk)(void *full_pk_ptr, unsigned int full_pk_len,
714ac5b394SYann Gautier 			  void **hashed_pk_ptr, unsigned int *hashed_pk_len);
724ac5b394SYann Gautier 
737cda17bbSSumit Garg 	/*
747cda17bbSSumit Garg 	 * Authenticated decryption. Return one of the
757cda17bbSSumit Garg 	 * 'enum crypto_ret_value' options.
767cda17bbSSumit Garg 	 */
777cda17bbSSumit Garg 	int (*auth_decrypt)(enum crypto_dec_algo dec_algo, void *data_ptr,
787cda17bbSSumit Garg 			    size_t len, const void *key, unsigned int key_len,
797cda17bbSSumit Garg 			    unsigned int key_flags, const void *iv,
807cda17bbSSumit Garg 			    unsigned int iv_len, const void *tag,
817cda17bbSSumit Garg 			    unsigned int tag_len);
82*0331bd22SLauren Wehrmeister 
83*0331bd22SLauren Wehrmeister 	/*
84*0331bd22SLauren Wehrmeister 	 * Finish using the crypto library,
85*0331bd22SLauren Wehrmeister 	 * anything to be done to wrap up crypto usage done here.
86*0331bd22SLauren Wehrmeister 	 */
87*0331bd22SLauren Wehrmeister 	void (*finish)(void);
8805799ae0SJuan Castillo } crypto_lib_desc_t;
8905799ae0SJuan Castillo 
9005799ae0SJuan Castillo /* Public functions */
910aa0b3afSManish V Badarkhe #if CRYPTO_SUPPORT
9205799ae0SJuan Castillo void crypto_mod_init(void);
930aa0b3afSManish V Badarkhe #else
940aa0b3afSManish V Badarkhe static inline void crypto_mod_init(void)
950aa0b3afSManish V Badarkhe {
960aa0b3afSManish V Badarkhe }
970aa0b3afSManish V Badarkhe #endif /* CRYPTO_SUPPORT */
980aa0b3afSManish V Badarkhe 
99dee99f10SYann Gautier #if (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY) || \
100dee99f10SYann Gautier     (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC)
10105799ae0SJuan Castillo int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len,
10205799ae0SJuan Castillo 				void *sig_ptr, unsigned int sig_len,
103735181b6SRoberto Vargas 				void *sig_alg_ptr, unsigned int sig_alg_len,
10405799ae0SJuan Castillo 				void *pk_ptr, unsigned int pk_len);
10505799ae0SJuan Castillo int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len,
10605799ae0SJuan Castillo 			   void *digest_info_ptr, unsigned int digest_info_len);
107dee99f10SYann Gautier #endif /* (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY) || \
108dee99f10SYann Gautier 	  (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC) */
1092bf4f27fSManish V Badarkhe 
1107cda17bbSSumit Garg int crypto_mod_auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
1117cda17bbSSumit Garg 			    size_t len, const void *key, unsigned int key_len,
1127cda17bbSSumit Garg 			    unsigned int key_flags, const void *iv,
1137cda17bbSSumit Garg 			    unsigned int iv_len, const void *tag,
1147cda17bbSSumit Garg 			    unsigned int tag_len);
11505799ae0SJuan Castillo 
116dee99f10SYann Gautier #if (CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY) || \
117dee99f10SYann Gautier     (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC)
11814db963fSManish V Badarkhe int crypto_mod_calc_hash(enum crypto_md_algo alg, void *data_ptr,
11914db963fSManish V Badarkhe 			 unsigned int data_len,
12014db963fSManish V Badarkhe 			 unsigned char output[CRYPTO_MD_MAX_SIZE]);
121dee99f10SYann Gautier #endif /* (CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY) || \
122dee99f10SYann Gautier 	  (CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC) */
1238c105290SAlexei Fedorov 
1244ac5b394SYann Gautier int crypto_mod_convert_pk(void *full_pk_ptr, unsigned int full_pk_len,
1254ac5b394SYann Gautier 			  void **hashed_pk_ptr, unsigned int *hashed_pk_len);
1264ac5b394SYann Gautier 
127*0331bd22SLauren Wehrmeister #if CRYPTO_SUPPORT
128*0331bd22SLauren Wehrmeister void crypto_mod_finish(void);
129*0331bd22SLauren Wehrmeister #else
130*0331bd22SLauren Wehrmeister static inline void crypto_mod_finish(void)
131*0331bd22SLauren Wehrmeister {
132*0331bd22SLauren Wehrmeister }
133*0331bd22SLauren Wehrmeister #endif /* CRYPTO_SUPPORT */
134*0331bd22SLauren Wehrmeister 
13505799ae0SJuan Castillo /* Macro to register a cryptographic library */
1368c105290SAlexei Fedorov #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \
137*0331bd22SLauren Wehrmeister 			    _calc_hash, _auth_decrypt, _convert_pk, _finish) \
1388c105290SAlexei Fedorov 	const crypto_lib_desc_t crypto_lib_desc = { \
1398c105290SAlexei Fedorov 		.name = _name, \
1408c105290SAlexei Fedorov 		.init = _init, \
1418c105290SAlexei Fedorov 		.verify_signature = _verify_signature, \
1428c105290SAlexei Fedorov 		.verify_hash = _verify_hash, \
1437cda17bbSSumit Garg 		.calc_hash = _calc_hash, \
1444ac5b394SYann Gautier 		.auth_decrypt = _auth_decrypt, \
145*0331bd22SLauren Wehrmeister 		.convert_pk = _convert_pk, \
146*0331bd22SLauren Wehrmeister 		.finish = _finish \
1478c105290SAlexei Fedorov 	}
14805799ae0SJuan Castillo 
1493b94189aSRoberto Vargas extern const crypto_lib_desc_t crypto_lib_desc;
1503b94189aSRoberto Vargas 
151c3cf06f1SAntonio Nino Diaz #endif /* CRYPTO_MOD_H */
152