xref: /optee_os/core/crypto/crypto.c (revision e1770e7153676d40582092005fbffb16cfeafd09)
1 /*
2  * Copyright (c) 2017, Linaro Limited
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-2-Clause
6  */
7 
8 #include <compiler.h>
9 #include <crypto/crypto.h>
10 #include <kernel/panic.h>
11 
12 #if !defined(_CFG_CRYPTO_WITH_HASH)
13 TEE_Result crypto_hash_get_ctx_size(uint32_t algo __unused,
14 				    size_t *size __unused)
15 {
16 	return TEE_ERROR_NOT_IMPLEMENTED;
17 }
18 
19 TEE_Result crypto_hash_init(void *ctx __unused, uint32_t algo __unused)
20 {
21 	return TEE_ERROR_NOT_IMPLEMENTED;
22 }
23 TEE_Result crypto_hash_update(void *ctx __unused, uint32_t algo __unused,
24 			      const uint8_t *data __unused, size_t len __unused)
25 {
26 	return TEE_ERROR_NOT_IMPLEMENTED;
27 }
28 TEE_Result crypto_hash_final(void *ctx __unused, uint32_t algo __unused,
29 			     uint8_t *digest __unused, size_t len __unused)
30 {
31 	return TEE_ERROR_NOT_IMPLEMENTED;
32 }
33 #endif /*_CFG_CRYPTO_WITH_HASH*/
34 
35 #if !defined(_CFG_CRYPTO_WITH_CIPHER)
36 TEE_Result crypto_cipher_get_ctx_size(uint32_t algo, size_t *size)
37 {
38 	return TEE_ERROR_NOT_IMPLEMENTED
39 }
40 
41 TEE_Result crypto_cipher_init(void *ctx __unused, uint32_t algo __unused,
42 			      TEE_OperationMode mode __unused,
43 			      const uint8_t *key1 __unused,
44 			      size_t key1_len __unused,
45 			      const uint8_t *key2 __unused,
46 			      size_t key2_len __unused,
47 			      const uint8_t *iv __unused,
48 			      size_t iv_len __unused)
49 {
50 	return TEE_ERROR_NOT_IMPLEMENTED
51 }
52 
53 TEE_Result crypto_cipher_update(void *ctx __unused, uint32_t algo __unused,
54 				TEE_OperationMode mode __unused,
55 				bool last_block __unused,
56 				const uint8_t *data __unused,
57 				size_t len __unused, uint8_t *dst __unused)
58 {
59 	return TEE_ERROR_NOT_IMPLEMENTED
60 }
61 
62 void crypto_cipher_final(void *ctx __unused, uint32_t algo __unused)
63 {
64 }
65 
66 TEE_Result crypto_cipher_get_block_size(uint32_t algo __unused,
67 					size_t *size __unused)
68 {
69 	return TEE_ERROR_NOT_IMPLEMENTED
70 }
71 #endif /*_CFG_CRYPTO_WITH_CIPHER*/
72 
73 #if !defined(_CFG_CRYPTO_WITH_MAC)
74 TEE_Result crypto_mac_get_ctx_size(uint32_t algo __unused,
75 				   size_t *size __unused)
76 {
77 	return TEE_ERROR_NOT_IMPLEMENTED;
78 }
79 
80 TEE_Result crypto_mac_init(void *ctx __unused, uint32_t algo __unused,
81 			   const uint8_t *key __unused, size_t len __unused)
82 {
83 	return TEE_ERROR_NOT_IMPLEMENTED;
84 }
85 
86 TEE_Result crypto_mac_update(void *ctx __unused, uint32_t algo __unused,
87 			     const uint8_t *data __unused, size_t len __unused)
88 {
89 	return TEE_ERROR_NOT_IMPLEMENTED;
90 }
91 
92 TEE_Result crypto_mac_final(void *ctx __unused, uint32_t algo __unused,
93 			    uint8_t *digest __unused,
94 			    size_t digest_len __unused)
95 {
96 	return TEE_ERROR_NOT_IMPLEMENTED;
97 }
98 #endif /*_CFG_CRYPTO_WITH_MAC*/
99 
100 #if !defined(_CFG_CRYPTO_WITH_AUTHENC)
101 TEE_Result crypto_authenc_get_ctx_size(uint32_t algo __unused,
102 				       size_t *size __unused)
103 {
104 	return TEE_ERROR_NOT_IMPLEMENTED;
105 }
106 
107 TEE_Result crypto_authenc_init(void *ctx __unused, uint32_t algo __unused,
108 			       TEE_OperationMode mode __unused,
109 			       const uint8_t *key __unused,
110 			       size_t key_len __unused,
111 			       const uint8_t *nonce __unused,
112 			       size_t nonce_len __unused,
113 			       size_t tag_len __unused,
114 			       size_t aad_len __unused,
115 			       size_t payload_len __unused)
116 {
117 	return TEE_ERROR_NOT_IMPLEMENTED;
118 }
119 
120 TEE_Result crypto_authenc_update_aad(void *ctx __unused, uint32_t algo __unused,
121 				     TEE_OperationMode mode __unused,
122 				     const uint8_t *data __unused,
123 				     size_t len __unused)
124 {
125 	return TEE_ERROR_NOT_IMPLEMENTED;
126 }
127 
128 TEE_Result crypto_authenc_update_payload(void *ctx __unused,
129 					 uint32_t algo __unused,
130 					 TEE_OperationMode mode __unused,
131 					 const uint8_t *src_data __unused,
132 					 size_t src_len __unused,
133 					 uint8_t *dst_data __unused,
134 					 size_t *dst_len __unused)
135 {
136 	return TEE_ERROR_NOT_IMPLEMENTED;
137 }
138 
139 TEE_Result crypto_authenc_enc_final(void *ctx __unused, uint32_t algo __unused,
140 				    const uint8_t *src_data __unused,
141 				    size_t src_len __unused,
142 				    uint8_t *dst_data __unused,
143 				    size_t *dst_len __unused,
144 				    uint8_t *dst_tag __unused,
145 				    size_t *dst_tag_len __unused)
146 {
147 	return TEE_ERROR_NOT_IMPLEMENTED;
148 }
149 
150 TEE_Result crypto_authenc_dec_final(void *ctx __unused, uint32_t algo __unused,
151 				    const uint8_t *src_data __unused,
152 				    size_t src_len __unused,
153 				    uint8_t *dst_data __unused,
154 				    size_t *dst_len __unused,
155 				    const uint8_t *tag __unused,
156 				    size_t tag_len __unused)
157 {
158 	return TEE_ERROR_NOT_IMPLEMENTED;
159 }
160 
161 void crypto_authenc_final(void *ctx __unused, uint32_t algo __unused)
162 {
163 }
164 #endif /*_CFG_CRYPTO_WITH_AUTHENC*/
165 
166 #if !defined(_CFG_CRYPTO_WITH_ACIPHER)
167 struct bignum *crypto_bignum_allocate(size_t size_bits __unused)
168 {
169 	return NULL;
170 }
171 
172 TEE_Result crypto_bignum_bin2bn(const uint8_t *from __unused,
173 				size_t fromsize __unused,
174 				struct bignum *to __unused)
175 {
176 	return TEE_ERROR_NOT_IMPLEMENTED;
177 }
178 
179 size_t crypto_bignum_num_bytes(struct bignum *a __unused)
180 {
181 	return 0;
182 }
183 
184 size_t crypto_bignum_num_bits(struct bignum *a __unused)
185 {
186 	return 0;
187 }
188 
189 /*
190  * crypto_bignum_allocate() and crypto_bignum_bin2bn() failing should be
191  * enough to guarantee that the functions calling this function aren't
192  * called, but just in case add a panic() here to avoid unexpected
193  * behavoir.
194  */
195 static void bignum_cant_happen(void)
196 {
197 	volatile bool b = true;
198 
199 	/* Avoid warning about function does not return */
200 	if (b)
201 		panic();
202 }
203 
204 void crypto_bignum_bn2bin(const struct bignum *from __unused,
205 			  uint8_t *to __unused)
206 {
207 	bignum_cant_happen();
208 }
209 
210 void crypto_bignum_copy(struct bignum *to __unused,
211 			const struct bignum *from __unused)
212 {
213 	bignum_cant_happen();
214 }
215 
216 void crypto_bignum_free(struct bignum *a)
217 {
218 	if (a)
219 		panic();
220 }
221 
222 void crypto_bignum_clear(struct bignum *a __unused)
223 {
224 	bignum_cant_happen();
225 }
226 
227 /* return -1 if a<b, 0 if a==b, +1 if a>b */
228 int32_t crypto_bignum_compare(struct bignum *a __unused,
229 			      struct bignum *b __unused)
230 {
231 	bignum_cant_happen();
232 	return -1;
233 }
234 #endif /*!_CFG_CRYPTO_WITH_ACIPHER*/
235 
236 #if !defined(CFG_CRYPTO_RSA) || !defined(_CFG_CRYPTO_WITH_ACIPHER)
237 TEE_Result crypto_acipher_alloc_rsa_keypair(struct rsa_keypair *s __unused,
238 					    size_t key_size_bits __unused)
239 {
240 	return TEE_ERROR_NOT_IMPLEMENTED;
241 }
242 
243 TEE_Result
244 crypto_acipher_alloc_rsa_public_key(struct rsa_public_key *s __unused,
245 				    size_t key_size_bits __unused)
246 {
247 	return TEE_ERROR_NOT_IMPLEMENTED;
248 }
249 
250 void crypto_acipher_free_rsa_public_key(struct rsa_public_key *s __unused)
251 {
252 }
253 
254 TEE_Result crypto_acipher_gen_rsa_key(struct rsa_keypair *key __unused,
255 				      size_t key_size __unused)
256 {
257 	return TEE_ERROR_NOT_IMPLEMENTED;
258 }
259 
260 TEE_Result crypto_acipher_rsanopad_decrypt(struct rsa_keypair *key __unused,
261 					   const uint8_t *src __unused,
262 					   size_t src_len __unused,
263 					   uint8_t *dst __unused,
264 					   size_t *dst_len __unused)
265 {
266 	return TEE_ERROR_NOT_IMPLEMENTED;
267 }
268 
269 TEE_Result crypto_acipher_rsanopad_encrypt(struct rsa_public_key *key __unused,
270 					   const uint8_t *src __unused,
271 					   size_t src_len __unused,
272 					   uint8_t *dst __unused,
273 					   size_t *dst_len __unused)
274 {
275 	return TEE_ERROR_NOT_IMPLEMENTED;
276 }
277 
278 TEE_Result crypto_acipher_rsaes_decrypt(uint32_t algo __unused,
279 					struct rsa_keypair *key __unused,
280 					const uint8_t *label __unused,
281 					size_t label_len __unused,
282 					const uint8_t *src __unused,
283 					size_t src_len __unused,
284 					uint8_t *dst __unused,
285 					size_t *dst_len __unused)
286 {
287 	return TEE_ERROR_NOT_IMPLEMENTED;
288 }
289 
290 TEE_Result crypto_acipher_rsaes_encrypt(uint32_t algo __unused,
291 					struct rsa_public_key *key __unused,
292 					const uint8_t *label __unused,
293 					size_t label_len __unused,
294 					const uint8_t *src __unused,
295 					size_t src_len __unused,
296 					uint8_t *dst __unused,
297 					size_t *dst_len __unused)
298 {
299 	return TEE_ERROR_NOT_IMPLEMENTED;
300 }
301 
302 TEE_Result crypto_acipher_rsassa_sign(uint32_t algo __unused,
303 				      struct rsa_keypair *key __unused,
304 				      int salt_len __unused,
305 				      const uint8_t *msg __unused,
306 				      size_t msg_len __unused,
307 				      uint8_t *sig __unused,
308 				      size_t *sig_len __unused)
309 {
310 	return TEE_ERROR_NOT_IMPLEMENTED;
311 }
312 
313 TEE_Result crypto_acipher_rsassa_verify(uint32_t algo __unused,
314 					struct rsa_public_key *key __unused,
315 					int salt_len __unused,
316 					const uint8_t *msg __unused,
317 					size_t msg_len __unused,
318 					const uint8_t *sig __unused,
319 					size_t sig_len __unused)
320 {
321 	return TEE_ERROR_NOT_IMPLEMENTED;
322 }
323 #endif /*!CFG_CRYPTO_RSA || !_CFG_CRYPTO_WITH_ACIPHER*/
324 
325 #if !defined(CFG_CRYPTO_DSA) || !defined(_CFG_CRYPTO_WITH_ACIPHER)
326 TEE_Result crypto_acipher_alloc_dsa_keypair(struct dsa_keypair *s __unused,
327 					    size_t key_size_bits __unused)
328 {
329 	return TEE_ERROR_NOT_IMPLEMENTED;
330 }
331 
332 TEE_Result
333 crypto_acipher_alloc_dsa_public_key(struct dsa_public_key *s __unused,
334 				    size_t key_size_bits __unused)
335 {
336 	return TEE_ERROR_NOT_IMPLEMENTED;
337 }
338 
339 TEE_Result crypto_acipher_gen_dsa_key(struct dsa_keypair *key __unused,
340 				      size_t key_size __unused)
341 {
342 	return TEE_ERROR_NOT_IMPLEMENTED;
343 }
344 
345 TEE_Result crypto_acipher_dsa_sign(uint32_t algo __unused,
346 				   struct dsa_keypair *key __unused,
347 				   const uint8_t *msg __unused,
348 				   size_t msg_len __unused,
349 				   uint8_t *sig __unused,
350 				   size_t *sig_len __unused)
351 {
352 	return TEE_ERROR_NOT_IMPLEMENTED;
353 }
354 
355 TEE_Result crypto_acipher_dsa_verify(uint32_t algo __unused,
356 				     struct dsa_public_key *key __unused,
357 				     const uint8_t *msg __unused,
358 				     size_t msg_len __unused,
359 				     const uint8_t *sig __unused,
360 				     size_t sig_len __unused)
361 {
362 	return TEE_ERROR_NOT_IMPLEMENTED;
363 }
364 #endif /*!CFG_CRYPTO_DSA || !_CFG_CRYPTO_WITH_ACIPHER*/
365 
366 #if !defined(CFG_CRYPTO_DH) || !defined(_CFG_CRYPTO_WITH_ACIPHER)
367 TEE_Result crypto_acipher_alloc_dh_keypair(struct dh_keypair *s __unused,
368 					   size_t key_size_bits __unused)
369 {
370 	return TEE_ERROR_NOT_IMPLEMENTED;
371 }
372 
373 TEE_Result crypto_acipher_gen_dh_key(struct dh_keypair *key __unused,
374 				     struct bignum *q __unused,
375 				     size_t xbits __unused)
376 {
377 	return TEE_ERROR_NOT_IMPLEMENTED;
378 }
379 
380 TEE_Result
381 crypto_acipher_dh_shared_secret(struct dh_keypair *private_key __unused,
382 				struct bignum *public_key __unused,
383 				struct bignum *secret __unused)
384 {
385 	return TEE_ERROR_NOT_IMPLEMENTED;
386 }
387 #endif /*!CFG_CRYPTO_DH || !_CFG_CRYPTO_WITH_ACIPHER*/
388 
389 #if !defined(CFG_CRYPTO_ECC) || !defined(_CFG_CRYPTO_WITH_ACIPHER)
390 TEE_Result
391 crypto_acipher_alloc_ecc_public_key(struct ecc_public_key *s __unused,
392 				    size_t key_size_bits __unused)
393 {
394 	return TEE_ERROR_NOT_IMPLEMENTED;
395 }
396 
397 TEE_Result crypto_acipher_alloc_ecc_keypair(struct ecc_keypair *s __unused,
398 					    size_t key_size_bits __unused)
399 {
400 	return TEE_ERROR_NOT_IMPLEMENTED;
401 }
402 
403 void crypto_acipher_free_ecc_public_key(struct ecc_public_key *s __unused)
404 {
405 }
406 
407 TEE_Result crypto_acipher_gen_ecc_key(struct ecc_keypair *key __unused)
408 {
409 	return TEE_ERROR_NOT_IMPLEMENTED;
410 }
411 
412 TEE_Result crypto_acipher_ecc_sign(uint32_t algo __unused,
413 				   struct ecc_keypair *key __unused,
414 				   const uint8_t *msg __unused,
415 				   size_t msg_len __unused,
416 				   uint8_t *sig __unused,
417 				   size_t *sig_len __unused)
418 {
419 	return TEE_ERROR_NOT_IMPLEMENTED;
420 }
421 
422 TEE_Result crypto_acipher_ecc_verify(uint32_t algo __unused,
423 				     struct ecc_public_key *key __unused,
424 				     const uint8_t *msg __unused,
425 				     size_t msg_len __unused,
426 				     const uint8_t *sig __unused,
427 				     size_t sig_len __unused)
428 {
429 	return TEE_ERROR_NOT_IMPLEMENTED;
430 }
431 
432 TEE_Result
433 crypto_acipher_ecc_shared_secret(struct ecc_keypair *private_key __unused,
434 				 struct ecc_public_key *public_key __unused,
435 				 void *secret __unused,
436 				 unsigned long *secret_len __unused)
437 {
438 	return TEE_ERROR_NOT_IMPLEMENTED;
439 }
440 #endif /*!CFG_CRYPTO_ECC || !_CFG_CRYPTO_WITH_ACIPHER*/
441