xref: /rk3399_ARM-atf/include/drivers/auth/crypto_mod.h (revision 4ac5b3949d874c4e0cd74fce8360a554bfd4cd3f)
1 /*
2  * Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef CRYPTO_MOD_H
8 #define CRYPTO_MOD_H
9 
10 #define	CRYPTO_AUTH_VERIFY_ONLY			1
11 #define	CRYPTO_HASH_CALC_ONLY			2
12 #define	CRYPTO_AUTH_VERIFY_AND_HASH_CALC	3
13 
14 /* Return values */
15 enum crypto_ret_value {
16 	CRYPTO_SUCCESS = 0,
17 	CRYPTO_ERR_INIT,
18 	CRYPTO_ERR_HASH,
19 	CRYPTO_ERR_SIGNATURE,
20 	CRYPTO_ERR_DECRYPTION,
21 	CRYPTO_ERR_UNKNOWN
22 };
23 
24 #define CRYPTO_MAX_IV_SIZE		16U
25 #define CRYPTO_MAX_TAG_SIZE		16U
26 
27 /* Decryption algorithm */
28 enum crypto_dec_algo {
29 	CRYPTO_GCM_DECRYPT = 0
30 };
31 
32 /* Message digest algorithm */
33 enum crypto_md_algo {
34 	CRYPTO_MD_SHA256,
35 	CRYPTO_MD_SHA384,
36 	CRYPTO_MD_SHA512,
37 };
38 
39 /* Maximum size as per the known stronger hash algorithm i.e.SHA512 */
40 #define CRYPTO_MD_MAX_SIZE		64U
41 
42 /*
43  * Cryptographic library descriptor
44  */
45 typedef struct crypto_lib_desc_s {
46 	const char *name;
47 
48 	/* Initialize library. This function is not expected to fail. All errors
49 	 * must be handled inside the function, asserting or panicing in case of
50 	 * a non-recoverable error */
51 	void (*init)(void);
52 
53 	/* Verify a digital signature. Return one of the
54 	 * 'enum crypto_ret_value' options */
55 #if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
56 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
57 	int (*verify_signature)(void *data_ptr, unsigned int data_len,
58 				void *sig_ptr, unsigned int sig_len,
59 				void *sig_alg, unsigned int sig_alg_len,
60 				void *pk_ptr, unsigned int pk_len);
61 
62 	/* Verify a hash. Return one of the 'enum crypto_ret_value' options */
63 	int (*verify_hash)(void *data_ptr, unsigned int data_len,
64 			   void *digest_info_ptr, unsigned int digest_info_len);
65 #endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
66 	  CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
67 
68 #if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
69 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
70 	/* Calculate a hash. Return hash value */
71 	int (*calc_hash)(enum crypto_md_algo md_alg, void *data_ptr,
72 			 unsigned int data_len,
73 			 unsigned char output[CRYPTO_MD_MAX_SIZE]);
74 #endif /* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
75 	  CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
76 
77 	/* Convert Public key (optional) */
78 	int (*convert_pk)(void *full_pk_ptr, unsigned int full_pk_len,
79 			  void **hashed_pk_ptr, unsigned int *hashed_pk_len);
80 
81 	/*
82 	 * Authenticated decryption. Return one of the
83 	 * 'enum crypto_ret_value' options.
84 	 */
85 	int (*auth_decrypt)(enum crypto_dec_algo dec_algo, void *data_ptr,
86 			    size_t len, const void *key, unsigned int key_len,
87 			    unsigned int key_flags, const void *iv,
88 			    unsigned int iv_len, const void *tag,
89 			    unsigned int tag_len);
90 } crypto_lib_desc_t;
91 
92 /* Public functions */
93 #if CRYPTO_SUPPORT
94 void crypto_mod_init(void);
95 #else
96 static inline void crypto_mod_init(void)
97 {
98 }
99 #endif /* CRYPTO_SUPPORT */
100 
101 #if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
102 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
103 int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len,
104 				void *sig_ptr, unsigned int sig_len,
105 				void *sig_alg_ptr, unsigned int sig_alg_len,
106 				void *pk_ptr, unsigned int pk_len);
107 int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len,
108 			   void *digest_info_ptr, unsigned int digest_info_len);
109 #endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
110 	  CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
111 
112 int crypto_mod_auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
113 			    size_t len, const void *key, unsigned int key_len,
114 			    unsigned int key_flags, const void *iv,
115 			    unsigned int iv_len, const void *tag,
116 			    unsigned int tag_len);
117 
118 #if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
119 CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
120 int crypto_mod_calc_hash(enum crypto_md_algo alg, void *data_ptr,
121 			 unsigned int data_len,
122 			 unsigned char output[CRYPTO_MD_MAX_SIZE]);
123 #endif /* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
124 	  CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
125 
126 int crypto_mod_convert_pk(void *full_pk_ptr, unsigned int full_pk_len,
127 			  void **hashed_pk_ptr, unsigned int *hashed_pk_len);
128 
129 #if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
130 /* Macro to register a cryptographic library */
131 #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \
132 			    _calc_hash, _auth_decrypt, _convert_pk) \
133 	const crypto_lib_desc_t crypto_lib_desc = { \
134 		.name = _name, \
135 		.init = _init, \
136 		.verify_signature = _verify_signature, \
137 		.verify_hash = _verify_hash, \
138 		.calc_hash = _calc_hash, \
139 		.auth_decrypt = _auth_decrypt, \
140 		.convert_pk = _convert_pk \
141 	}
142 #elif CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY
143 #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \
144 			    _auth_decrypt, _convert_pk) \
145 	const crypto_lib_desc_t crypto_lib_desc = { \
146 		.name = _name, \
147 		.init = _init, \
148 		.verify_signature = _verify_signature, \
149 		.verify_hash = _verify_hash, \
150 		.auth_decrypt = _auth_decrypt, \
151 		.convert_pk = _convert_pk \
152 	}
153 #elif CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY
154 #define REGISTER_CRYPTO_LIB(_name, _init, _calc_hash) \
155 	const crypto_lib_desc_t crypto_lib_desc = { \
156 		.name = _name, \
157 		.init = _init, \
158 		.calc_hash = _calc_hash, \
159 	}
160 #endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
161 
162 extern const crypto_lib_desc_t crypto_lib_desc;
163 
164 #endif /* CRYPTO_MOD_H */
165