xref: /optee_os/core/include/crypto/crypto_impl.h (revision 5b25c76ac40f830867e3d60800120ffd7874e8dc)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2019, Linaro Limited
4  */
5 
6 #ifndef __CRYPTO_CRYPTO_IMPL_H
7 #define __CRYPTO_CRYPTO_IMPL_H
8 
9 #include <tee_api_types.h>
10 
11 /*
12  * The crypto context used by the crypto_hash_*() functions is defined by
13  * struct crypto_hash_ctx.
14  */
15 struct crypto_hash_ctx {
16 	const struct crypto_hash_ops *ops;
17 };
18 
19 struct crypto_hash_ops {
20 	TEE_Result (*init)(struct crypto_hash_ctx *ctx);
21 	TEE_Result (*update)(struct crypto_hash_ctx *ctx, const uint8_t *data,
22 			     size_t len);
23 	TEE_Result (*final)(struct crypto_hash_ctx *ctx, uint8_t *digest,
24 			    size_t len);
25 	void (*free_ctx)(struct crypto_hash_ctx *ctx);
26 	void (*copy_state)(struct crypto_hash_ctx *dst_ctx,
27 			   struct crypto_hash_ctx *src_ctx);
28 };
29 
30 #define CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(name, type) \
31 	static inline TEE_Result \
32 	crypto_##name##_alloc_ctx(struct crypto_##type##_ctx **ctx __unused) \
33 	{ return TEE_ERROR_NOT_IMPLEMENTED; }
34 
35 #if defined(CFG_CRYPTO_MD5)
36 TEE_Result crypto_md5_alloc_ctx(struct crypto_hash_ctx **ctx);
37 #else
38 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(md5, hash)
39 #endif
40 
41 #if defined(CFG_CRYPTO_SHA1)
42 TEE_Result crypto_sha1_alloc_ctx(struct crypto_hash_ctx **ctx);
43 #else
44 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha1, hash)
45 #endif
46 
47 #if defined(CFG_CRYPTO_SHA224)
48 TEE_Result crypto_sha224_alloc_ctx(struct crypto_hash_ctx **ctx);
49 #else
50 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha224, hash)
51 #endif
52 
53 #if defined(CFG_CRYPTO_SHA256)
54 TEE_Result crypto_sha256_alloc_ctx(struct crypto_hash_ctx **ctx);
55 #else
56 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha256, hash)
57 #endif
58 
59 #if defined(CFG_CRYPTO_SHA384)
60 TEE_Result crypto_sha384_alloc_ctx(struct crypto_hash_ctx **ctx);
61 #else
62 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha384, hash)
63 #endif
64 
65 #if defined(CFG_CRYPTO_SHA512)
66 TEE_Result crypto_sha512_alloc_ctx(struct crypto_hash_ctx **ctx);
67 #else
68 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sha512, hash)
69 #endif
70 
71 #if defined(CFG_CRYPTO_SM3)
72 TEE_Result crypto_sm3_alloc_ctx(struct crypto_hash_ctx **ctx);
73 #else
74 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sm3, hash)
75 #endif
76 
77 /*
78  * The crypto context used by the crypto_mac_*() functions is defined by
79  * struct crypto_mac_ctx.
80  */
81 struct crypto_mac_ctx {
82 	const struct crypto_mac_ops *ops;
83 };
84 
85 struct crypto_mac_ops {
86 	TEE_Result (*init)(struct crypto_mac_ctx *ctx, const uint8_t *key,
87 			   size_t len);
88 	TEE_Result (*update)(struct crypto_mac_ctx *ctx, const uint8_t *data,
89 			     size_t len);
90 	TEE_Result (*final)(struct crypto_mac_ctx *ctx, uint8_t *digest,
91 			    size_t len);
92 	void (*free_ctx)(struct crypto_mac_ctx *ctx);
93 	void (*copy_state)(struct crypto_mac_ctx *dst_ctx,
94 			   struct crypto_mac_ctx *src_ctx);
95 };
96 
97 #if defined(CFG_CRYPTO_HMAC)
98 TEE_Result crypto_hmac_md5_alloc_ctx(struct crypto_mac_ctx **ctx);
99 TEE_Result crypto_hmac_sha1_alloc_ctx(struct crypto_mac_ctx **ctx);
100 TEE_Result crypto_hmac_sha224_alloc_ctx(struct crypto_mac_ctx **ctx);
101 TEE_Result crypto_hmac_sha256_alloc_ctx(struct crypto_mac_ctx **ctx);
102 TEE_Result crypto_hmac_sha384_alloc_ctx(struct crypto_mac_ctx **ctx);
103 TEE_Result crypto_hmac_sha512_alloc_ctx(struct crypto_mac_ctx **ctx);
104 #else
105 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_md5, mac)
106 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha1, mac)
107 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha224, mac)
108 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha256, mac)
109 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha384, mac)
110 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sha512, mac)
111 #endif
112 
113 #if defined(CFG_CRYPTO_SM3) && defined(CFG_CRYPTO_HMAC)
114 TEE_Result crypto_hmac_sm3_alloc_ctx(struct crypto_mac_ctx **ctx);
115 #else
116 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(hmac_sm3, mac)
117 #endif
118 
119 #if defined(CFG_CRYPTO_CBC_MAC)
120 TEE_Result crypto_aes_cbc_mac_nopad_alloc_ctx(struct crypto_mac_ctx **ctx);
121 TEE_Result crypto_aes_cbc_mac_pkcs5_alloc_ctx(struct crypto_mac_ctx **ctx);
122 TEE_Result crypto_des_cbc_mac_nopad_alloc_ctx(struct crypto_mac_ctx **ctx);
123 TEE_Result crypto_des_cbc_mac_pkcs5_alloc_ctx(struct crypto_mac_ctx **ctx);
124 TEE_Result crypto_des3_cbc_mac_nopad_alloc_ctx(struct crypto_mac_ctx **ctx);
125 TEE_Result crypto_des3_cbc_mac_pkcs5_alloc_ctx(struct crypto_mac_ctx **ctx);
126 #else
127 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cbc_mac_nopad, mac)
128 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cbc_mac_pkcs5, mac)
129 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des_cbc_mac_nopad, mac)
130 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des_cbc_mac_pkcs5, mac)
131 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_cbc_mac_nopad, mac)
132 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_cbc_mac_pkcs5, mac)
133 #endif
134 
135 #if defined(CFG_CRYPTO_CMAC)
136 TEE_Result crypto_aes_cmac_alloc_ctx(struct crypto_mac_ctx **ctx);
137 #else
138 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cmac, mac)
139 #endif
140 
141 /*
142  * The crypto context used by the crypto_cipher_*() functions is defined by
143  * struct crypto_cipher_ctx.
144  */
145 struct crypto_cipher_ctx {
146 	const struct crypto_cipher_ops *ops;
147 };
148 
149 struct crypto_cipher_ops {
150 	TEE_Result (*init)(struct crypto_cipher_ctx *ctx,
151 			   TEE_OperationMode mode,
152 			   const uint8_t *key1, size_t key1_len,
153 			   const uint8_t *key2, size_t key2_len,
154 			   const uint8_t *iv, size_t iv_len);
155 	TEE_Result (*update)(struct crypto_cipher_ctx *ctx, bool last_block,
156 			     const uint8_t *data, size_t len, uint8_t *dst);
157 	void (*final)(struct crypto_cipher_ctx *ctx);
158 
159 	void (*free_ctx)(struct crypto_cipher_ctx *ctx);
160 	void (*copy_state)(struct crypto_cipher_ctx *dst_ctx,
161 			   struct crypto_cipher_ctx *src_ctx);
162 };
163 
164 #if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_ECB)
165 TEE_Result crypto_aes_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx);
166 #else
167 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_ecb, cipher)
168 #endif
169 
170 #if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_CBC)
171 TEE_Result crypto_aes_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx);
172 #else
173 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cbc, cipher)
174 #endif
175 
176 #if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_CTR)
177 TEE_Result crypto_aes_ctr_alloc_ctx(struct crypto_cipher_ctx **ctx);
178 #else
179 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_ctr, cipher)
180 #endif
181 
182 #if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_CTS)
183 TEE_Result crypto_aes_cts_alloc_ctx(struct crypto_cipher_ctx **ctx);
184 #else
185 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_cts, cipher)
186 #endif
187 
188 #if defined(CFG_CRYPTO_AES) && defined(CFG_CRYPTO_XTS)
189 TEE_Result crypto_aes_xts_alloc_ctx(struct crypto_cipher_ctx **ctx);
190 #else
191 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(aes_xts, cipher)
192 #endif
193 
194 #if defined(CFG_CRYPTO_DES) && defined(CFG_CRYPTO_ECB)
195 TEE_Result crypto_des_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx);
196 TEE_Result crypto_des3_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx);
197 #else
198 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des_ecb, cipher)
199 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_ecb, cipher)
200 #endif
201 
202 #if defined(CFG_CRYPTO_DES) && defined(CFG_CRYPTO_CBC)
203 TEE_Result crypto_des_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx);
204 TEE_Result crypto_des3_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx);
205 #else
206 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des_cbc, cipher)
207 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(des3_cbc, cipher)
208 #endif
209 
210 #if defined(CFG_CRYPTO_SM4) && defined(CFG_CRYPTO_ECB)
211 TEE_Result crypto_sm4_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx);
212 #else
213 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sm4_ecb, cipher)
214 #endif
215 
216 #if defined(CFG_CRYPTO_SM4) && defined(CFG_CRYPTO_CBC)
217 TEE_Result crypto_sm4_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx);
218 #else
219 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sm4_cbc, cipher)
220 #endif
221 
222 #if defined(CFG_CRYPTO_SM4) && defined(CFG_CRYPTO_CTR)
223 TEE_Result crypto_sm4_ctr_alloc_ctx(struct crypto_cipher_ctx **ctx);
224 #else
225 CRYPTO_ALLOC_CTX_NOT_IMPLEMENTED(sm4_ctr, cipher)
226 #endif
227 
228 /*
229  * The crypto context used by the crypto_authen_*() functions below is
230  * defined by struct crypto_authenc_ctx.
231  */
232 struct crypto_authenc_ctx {
233 	const struct crypto_authenc_ops *ops;
234 };
235 
236 struct crypto_authenc_ops {
237 	TEE_Result (*init)(struct crypto_authenc_ctx *ctx,
238 			   TEE_OperationMode mode,
239 			   const uint8_t *key, size_t key_len,
240 			   const uint8_t *nonce, size_t nonce_len,
241 			   size_t tag_len, size_t aad_len,
242 			   size_t payload_len);
243 	TEE_Result (*update_aad)(struct crypto_authenc_ctx *ctx,
244 				 const uint8_t *data, size_t len);
245 	TEE_Result (*update_payload)(struct crypto_authenc_ctx *ctx,
246 				     TEE_OperationMode mode,
247 				     const uint8_t *src_data, size_t len,
248 				     uint8_t *dst_data);
249 	TEE_Result (*enc_final)(struct crypto_authenc_ctx *ctx,
250 				const uint8_t *src_data, size_t len,
251 				uint8_t *dst_data, uint8_t *dst_tag,
252 				size_t *dst_tag_len);
253 	TEE_Result (*dec_final)(struct crypto_authenc_ctx *ctx,
254 				const uint8_t *src_data, size_t len,
255 				uint8_t *dst_data, const uint8_t *tag,
256 				size_t tag_len);
257 	void (*final)(struct crypto_authenc_ctx *ctx);
258 	void (*free_ctx)(struct crypto_authenc_ctx *ctx);
259 	void (*copy_state)(struct crypto_authenc_ctx *dst_ctx,
260 			   struct crypto_authenc_ctx *src_ctx);
261 };
262 
263 TEE_Result crypto_aes_ccm_alloc_ctx(struct crypto_authenc_ctx **ctx);
264 TEE_Result crypto_aes_gcm_alloc_ctx(struct crypto_authenc_ctx **ctx);
265 
266 #ifdef CFG_CRYPTO_DRV_HASH
267 TEE_Result drvcrypt_hash_alloc_ctx(struct crypto_hash_ctx **ctx, uint32_t algo);
268 #else
269 static inline TEE_Result
270 drvcrypt_hash_alloc_ctx(struct crypto_hash_ctx **ctx __unused,
271 			uint32_t algo __unused)
272 {
273 	return TEE_ERROR_NOT_IMPLEMENTED;
274 }
275 #endif /* CFG_CRYPTO_DRV_HASH */
276 
277 #ifdef CFG_CRYPTO_DRV_CIPHER
278 TEE_Result drvcrypt_cipher_alloc_ctx(struct crypto_cipher_ctx **ctx,
279 				     uint32_t algo);
280 #else
281 static inline TEE_Result
282 drvcrypt_cipher_alloc_ctx(struct crypto_cipher_ctx **ctx __unused,
283 			  uint32_t algo __unused)
284 {
285 	return TEE_ERROR_NOT_IMPLEMENTED;
286 }
287 #endif /* CFG_CRYPTO_DRV_CIPHER */
288 #endif /*__CRYPTO_CRYPTO_IMPL_H*/
289