1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * Copyright (c) 2021, SumUp Services GmbH
5  */
6 #include <config.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <string_ext.h>
10 #include <tee_api.h>
11 #include <tee_api_defines_extensions.h>
12 #include <tee_internal_api_extensions.h>
13 #include <utee_syscalls.h>
14 #include <utee_defines.h>
15 #include <util.h>
16 #include "tee_api_private.h"
17 
18 struct __TEE_OperationHandle {
19 	TEE_OperationInfo info;
20 	TEE_ObjectHandle key1;
21 	TEE_ObjectHandle key2;
22 	uint32_t operationState;/* Operation state : INITIAL or ACTIVE */
23 	uint8_t *buffer;	/* buffer to collect complete blocks */
24 	bool buffer_two_blocks;	/* True if two blocks need to be buffered */
25 	size_t block_size;	/* Block size of cipher */
26 	size_t buffer_offs;	/* Offset in buffer */
27 	uint32_t state;		/* Handle to state in TEE Core */
28 };
29 
30 /* Cryptographic Operations API - Generic Operation Functions */
31 
TEE_AllocateOperation(TEE_OperationHandle * operation,uint32_t algorithm,uint32_t mode,uint32_t maxKeySize)32 TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation,
33 				 uint32_t algorithm, uint32_t mode,
34 				 uint32_t maxKeySize)
35 {
36 	TEE_Result res;
37 	TEE_OperationHandle op = TEE_HANDLE_NULL;
38 	uint32_t handle_state = 0;
39 	size_t block_size = 1;
40 	uint32_t req_key_usage;
41 	bool with_private_key = false;
42 	bool buffer_two_blocks = false;
43 
44 	if (!operation)
45 		TEE_Panic(0);
46 
47 	if (algorithm == TEE_ALG_AES_XTS || algorithm == TEE_ALG_SM2_KEP)
48 		handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
49 
50 	/* Check algorithm max key size */
51 	switch (algorithm) {
52 	case TEE_ALG_DSA_SHA1:
53 		if (maxKeySize < 512)
54 			return TEE_ERROR_NOT_SUPPORTED;
55 		if (maxKeySize > 1024)
56 			return TEE_ERROR_NOT_SUPPORTED;
57 		if (maxKeySize % 64 != 0)
58 			return TEE_ERROR_NOT_SUPPORTED;
59 		break;
60 
61 	case TEE_ALG_DSA_SHA224:
62 		if (maxKeySize != 2048)
63 			return TEE_ERROR_NOT_SUPPORTED;
64 		break;
65 
66 	case TEE_ALG_DSA_SHA256:
67 		if (maxKeySize != 2048 && maxKeySize != 3072)
68 			return TEE_ERROR_NOT_SUPPORTED;
69 		break;
70 
71 	case TEE_ALG_ECDSA_P192:
72 	case TEE_ALG_ECDH_P192:
73 		if (maxKeySize != 192)
74 			return TEE_ERROR_NOT_SUPPORTED;
75 		break;
76 
77 	case TEE_ALG_ECDSA_P224:
78 	case TEE_ALG_ECDH_P224:
79 		if (maxKeySize != 224)
80 			return TEE_ERROR_NOT_SUPPORTED;
81 		break;
82 
83 	case TEE_ALG_ECDSA_P256:
84 	case TEE_ALG_ECDH_P256:
85 	case TEE_ALG_SM2_PKE:
86 	case TEE_ALG_SM2_DSA_SM3:
87 		if (maxKeySize != 256)
88 			return TEE_ERROR_NOT_SUPPORTED;
89 		break;
90 
91 	case TEE_ALG_SM2_KEP:
92 		/* Two 256-bit keys */
93 		if (maxKeySize != 512)
94 			return TEE_ERROR_NOT_SUPPORTED;
95 		break;
96 
97 	case TEE_ALG_ECDSA_P384:
98 	case TEE_ALG_ECDH_P384:
99 		if (maxKeySize != 384)
100 			return TEE_ERROR_NOT_SUPPORTED;
101 		break;
102 
103 	case TEE_ALG_ECDSA_P521:
104 	case TEE_ALG_ECDH_P521:
105 		if (maxKeySize != 521)
106 			return TEE_ERROR_NOT_SUPPORTED;
107 		break;
108 
109 	default:
110 		break;
111 	}
112 
113 	/* Check algorithm mode (and maxKeySize for digests) */
114 	switch (algorithm) {
115 	case TEE_ALG_AES_CTS:
116 	case TEE_ALG_AES_XTS:
117 		buffer_two_blocks = true;
118 		fallthrough;
119 	case TEE_ALG_AES_ECB_NOPAD:
120 	case TEE_ALG_AES_CBC_NOPAD:
121 	case TEE_ALG_AES_CCM:
122 	case TEE_ALG_DES_ECB_NOPAD:
123 	case TEE_ALG_DES_CBC_NOPAD:
124 	case TEE_ALG_DES3_ECB_NOPAD:
125 	case TEE_ALG_DES3_CBC_NOPAD:
126 	case TEE_ALG_SM4_ECB_NOPAD:
127 	case TEE_ALG_SM4_CBC_NOPAD:
128 	case TEE_ALG_SM4_CTR:
129 		if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES)
130 			block_size = TEE_AES_BLOCK_SIZE;
131 		else if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_SM4)
132 			block_size = TEE_SM4_BLOCK_SIZE;
133 		else
134 			block_size = TEE_DES_BLOCK_SIZE;
135 		fallthrough;
136 	case TEE_ALG_AES_CTR:
137 	case TEE_ALG_AES_GCM:
138 		if (mode == TEE_MODE_ENCRYPT)
139 			req_key_usage = TEE_USAGE_ENCRYPT;
140 		else if (mode == TEE_MODE_DECRYPT)
141 			req_key_usage = TEE_USAGE_DECRYPT;
142 		else
143 			return TEE_ERROR_NOT_SUPPORTED;
144 		break;
145 
146 #if defined(CFG_CRYPTO_RSASSA_NA1)
147 	case TEE_ALG_RSASSA_PKCS1_V1_5:
148 #endif
149 	case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
150 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
151 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
152 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
153 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
154 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
155 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
156 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
157 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
158 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
159 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
160 	case TEE_ALG_DSA_SHA1:
161 	case TEE_ALG_DSA_SHA224:
162 	case TEE_ALG_DSA_SHA256:
163 	case TEE_ALG_ECDSA_P192:
164 	case TEE_ALG_ECDSA_P224:
165 	case TEE_ALG_ECDSA_P256:
166 	case TEE_ALG_ECDSA_P384:
167 	case TEE_ALG_ECDSA_P521:
168 	case TEE_ALG_SM2_DSA_SM3:
169 		if (mode == TEE_MODE_SIGN) {
170 			with_private_key = true;
171 			req_key_usage = TEE_USAGE_SIGN;
172 		} else if (mode == TEE_MODE_VERIFY) {
173 			req_key_usage = TEE_USAGE_VERIFY;
174 		} else {
175 			return TEE_ERROR_NOT_SUPPORTED;
176 		}
177 		break;
178 
179 	case TEE_ALG_RSAES_PKCS1_V1_5:
180 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
181 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
182 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
183 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
184 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
185 	case TEE_ALG_SM2_PKE:
186 		if (mode == TEE_MODE_ENCRYPT) {
187 			req_key_usage = TEE_USAGE_ENCRYPT;
188 		} else if (mode == TEE_MODE_DECRYPT) {
189 			with_private_key = true;
190 			req_key_usage = TEE_USAGE_DECRYPT;
191 		} else {
192 			return TEE_ERROR_NOT_SUPPORTED;
193 		}
194 		break;
195 
196 	case TEE_ALG_RSA_NOPAD:
197 		if (mode == TEE_MODE_ENCRYPT) {
198 			req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY;
199 		} else if (mode == TEE_MODE_DECRYPT) {
200 			with_private_key = true;
201 			req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN;
202 		} else {
203 			return TEE_ERROR_NOT_SUPPORTED;
204 		}
205 		break;
206 
207 	case TEE_ALG_DH_DERIVE_SHARED_SECRET:
208 	case TEE_ALG_ECDH_P192:
209 	case TEE_ALG_ECDH_P224:
210 	case TEE_ALG_ECDH_P256:
211 	case TEE_ALG_ECDH_P384:
212 	case TEE_ALG_ECDH_P521:
213 	case TEE_ALG_HKDF_MD5_DERIVE_KEY:
214 	case TEE_ALG_HKDF_SHA1_DERIVE_KEY:
215 	case TEE_ALG_HKDF_SHA224_DERIVE_KEY:
216 	case TEE_ALG_HKDF_SHA256_DERIVE_KEY:
217 	case TEE_ALG_HKDF_SHA384_DERIVE_KEY:
218 	case TEE_ALG_HKDF_SHA512_DERIVE_KEY:
219 	case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY:
220 	case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY:
221 	case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY:
222 	case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY:
223 	case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY:
224 	case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY:
225 	case TEE_ALG_SM2_KEP:
226 		if (mode != TEE_MODE_DERIVE)
227 			return TEE_ERROR_NOT_SUPPORTED;
228 		with_private_key = true;
229 		req_key_usage = TEE_USAGE_DERIVE;
230 		break;
231 
232 	case TEE_ALG_MD5:
233 	case TEE_ALG_SHA1:
234 	case TEE_ALG_SHA224:
235 	case TEE_ALG_SHA256:
236 	case TEE_ALG_SHA384:
237 	case TEE_ALG_SHA512:
238 	case TEE_ALG_SM3:
239 		if (mode != TEE_MODE_DIGEST)
240 			return TEE_ERROR_NOT_SUPPORTED;
241 		if (maxKeySize)
242 			return TEE_ERROR_NOT_SUPPORTED;
243 		/* v1.1: flags always set for digest operations */
244 		handle_state |= TEE_HANDLE_FLAG_KEY_SET;
245 		req_key_usage = 0;
246 		break;
247 
248 	case TEE_ALG_DES_CBC_MAC_NOPAD:
249 	case TEE_ALG_AES_CBC_MAC_NOPAD:
250 	case TEE_ALG_AES_CBC_MAC_PKCS5:
251 	case TEE_ALG_AES_CMAC:
252 	case TEE_ALG_DES_CBC_MAC_PKCS5:
253 	case TEE_ALG_DES3_CBC_MAC_NOPAD:
254 	case TEE_ALG_DES3_CBC_MAC_PKCS5:
255 	case TEE_ALG_DES3_CMAC:
256 	case TEE_ALG_HMAC_MD5:
257 	case TEE_ALG_HMAC_SHA1:
258 	case TEE_ALG_HMAC_SHA224:
259 	case TEE_ALG_HMAC_SHA256:
260 	case TEE_ALG_HMAC_SHA384:
261 	case TEE_ALG_HMAC_SHA512:
262 	case TEE_ALG_HMAC_SM3:
263 		if (mode != TEE_MODE_MAC)
264 			return TEE_ERROR_NOT_SUPPORTED;
265 		req_key_usage = TEE_USAGE_MAC;
266 		break;
267 
268 	default:
269 		return TEE_ERROR_NOT_SUPPORTED;
270 	}
271 
272 	op = TEE_Malloc(sizeof(*op), TEE_MALLOC_FILL_ZERO);
273 	if (!op)
274 		return TEE_ERROR_OUT_OF_MEMORY;
275 
276 	op->info.algorithm = algorithm;
277 	op->info.operationClass = TEE_ALG_GET_CLASS(algorithm);
278 #ifdef CFG_CRYPTO_RSASSA_NA1
279 	if (algorithm == TEE_ALG_RSASSA_PKCS1_V1_5)
280 		op->info.operationClass = TEE_OPERATION_ASYMMETRIC_SIGNATURE;
281 #endif
282 	op->info.mode = mode;
283 	op->info.digestLength = TEE_ALG_GET_DIGEST_SIZE(algorithm);
284 	op->info.maxKeySize = maxKeySize;
285 	op->info.requiredKeyUsage = req_key_usage;
286 	op->info.handleState = handle_state;
287 
288 	if (block_size > 1) {
289 		size_t buffer_size = block_size;
290 
291 		if (buffer_two_blocks)
292 			buffer_size *= 2;
293 
294 		op->buffer = TEE_Malloc(buffer_size,
295 					TEE_USER_MEM_HINT_NO_FILL_ZERO);
296 		if (op->buffer == NULL) {
297 			res = TEE_ERROR_OUT_OF_MEMORY;
298 			goto out;
299 		}
300 	}
301 	op->block_size = block_size;
302 	op->buffer_two_blocks = buffer_two_blocks;
303 
304 	if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) {
305 		uint32_t mks = maxKeySize;
306 		TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm,
307 						       with_private_key);
308 
309 		/*
310 		 * If two keys are expected the max key size is the sum of
311 		 * the size of both keys.
312 		 */
313 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS)
314 			mks /= 2;
315 
316 		res = TEE_AllocateTransientObject(key_type, mks, &op->key1);
317 		if (res != TEE_SUCCESS)
318 			goto out;
319 
320 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) {
321 			res = TEE_AllocateTransientObject(key_type, mks,
322 							  &op->key2);
323 			if (res != TEE_SUCCESS)
324 				goto out;
325 		}
326 	}
327 
328 	res = _utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1,
329 				     (unsigned long)op->key2, &op->state);
330 	if (res != TEE_SUCCESS)
331 		goto out;
332 
333 	/*
334 	 * Initialize digest operations
335 	 * Other multi-stage operations initialized w/ TEE_xxxInit functions
336 	 * Non-applicable on asymmetric operations
337 	 */
338 	if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) {
339 		res = _utee_hash_init(op->state, NULL, 0);
340 		if (res != TEE_SUCCESS)
341 			goto out;
342 		/* v1.1: flags always set for digest operations */
343 		op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
344 	}
345 
346 	op->operationState = TEE_OPERATION_STATE_INITIAL;
347 
348 	*operation = op;
349 
350 out:
351 	if (res != TEE_SUCCESS) {
352 		if (res != TEE_ERROR_OUT_OF_MEMORY &&
353 		    res != TEE_ERROR_NOT_SUPPORTED)
354 			TEE_Panic(res);
355 		if (op) {
356 			if (op->state) {
357 				TEE_FreeOperation(op);
358 			} else {
359 				TEE_Free(op->buffer);
360 				TEE_FreeTransientObject(op->key1);
361 				TEE_FreeTransientObject(op->key2);
362 				TEE_Free(op);
363 			}
364 		}
365 	}
366 
367 	return res;
368 }
369 
TEE_FreeOperation(TEE_OperationHandle operation)370 void TEE_FreeOperation(TEE_OperationHandle operation)
371 {
372 	TEE_Result res;
373 
374 	if (operation == TEE_HANDLE_NULL)
375 		TEE_Panic(0);
376 
377 	/*
378 	 * Note that keys should not be freed here, since they are
379 	 * claimed by the operation they will be freed by
380 	 * utee_cryp_state_free().
381 	 */
382 	res = _utee_cryp_state_free(operation->state);
383 	if (res != TEE_SUCCESS)
384 		TEE_Panic(res);
385 
386 	TEE_Free(operation->buffer);
387 	TEE_Free(operation);
388 }
389 
TEE_GetOperationInfo(TEE_OperationHandle operation,TEE_OperationInfo * operationInfo)390 void TEE_GetOperationInfo(TEE_OperationHandle operation,
391 			  TEE_OperationInfo *operationInfo)
392 {
393 	if (operation == TEE_HANDLE_NULL)
394 		TEE_Panic(0);
395 
396 	__utee_check_out_annotation(operationInfo, sizeof(*operationInfo));
397 
398 	*operationInfo = operation->info;
399 	if (operationInfo->handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) {
400 		operationInfo->keySize = 0;
401 		operationInfo->requiredKeyUsage = 0;
402 	}
403 }
404 
TEE_GetOperationInfoMultiple(TEE_OperationHandle op,TEE_OperationInfoMultiple * op_info,uint32_t * size)405 TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle op,
406 					TEE_OperationInfoMultiple *op_info,
407 					uint32_t *size)
408 {
409 	TEE_Result res = TEE_SUCCESS;
410 	TEE_ObjectInfo kinfo = { };
411 	size_t max_key_count = 0;
412 	bool two_keys = false;
413 
414 	if (op == TEE_HANDLE_NULL) {
415 		res = TEE_ERROR_BAD_PARAMETERS;
416 		goto out;
417 	}
418 
419 	__utee_check_outbuf_annotation(op_info, size);
420 
421 	if (*size < sizeof(*op_info)) {
422 		res = TEE_ERROR_BAD_PARAMETERS;
423 		goto out;
424 	}
425 	max_key_count = (*size - sizeof(*op_info)) /
426 			sizeof(TEE_OperationInfoKey);
427 
428 	TEE_MemFill(op_info, 0, *size);
429 
430 	/* Two keys flag (TEE_ALG_AES_XTS only) */
431 	two_keys = op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
432 
433 	if (op->info.mode == TEE_MODE_DIGEST) {
434 		op_info->numberOfKeys = 0;
435 	} else if (!two_keys) {
436 		if (max_key_count < 1) {
437 			res = TEE_ERROR_SHORT_BUFFER;
438 			goto out;
439 		}
440 
441 		res = TEE_GetObjectInfo1(op->key1, &kinfo);
442 		/* Key1 is not a valid handle, "can't happen". */
443 		if (res)
444 			goto out;
445 
446 		op_info->keyInformation[0].keySize = kinfo.keySize;
447 		op_info->keyInformation[0].requiredKeyUsage =
448 			op->info.requiredKeyUsage;
449 		op_info->numberOfKeys = 1;
450 	} else {
451 		if (max_key_count < 2) {
452 			res = TEE_ERROR_SHORT_BUFFER;
453 			goto out;
454 		}
455 
456 		res = TEE_GetObjectInfo1(op->key1, &kinfo);
457 		/* Key1 is not a valid handle, "can't happen". */
458 		if (res)
459 			goto out;
460 
461 		op_info->keyInformation[0].keySize = kinfo.keySize;
462 		op_info->keyInformation[0].requiredKeyUsage =
463 			op->info.requiredKeyUsage;
464 
465 		res = TEE_GetObjectInfo1(op->key2, &kinfo);
466 		/* Key2 is not a valid handle, "can't happen". */
467 		if (res)
468 			goto out;
469 
470 		op_info->keyInformation[1].keySize = kinfo.keySize;
471 		op_info->keyInformation[1].requiredKeyUsage =
472 			op->info.requiredKeyUsage;
473 
474 		op_info->numberOfKeys = 2;
475 	}
476 
477 	op_info->algorithm = op->info.algorithm;
478 	op_info->operationClass = op->info.operationClass;
479 	op_info->mode = op->info.mode;
480 	op_info->digestLength = op->info.digestLength;
481 	op_info->maxKeySize = op->info.maxKeySize;
482 	op_info->handleState = op->info.handleState;
483 	op_info->operationState = op->operationState;
484 
485 out:
486 	if (res != TEE_SUCCESS &&
487 	    res != TEE_ERROR_SHORT_BUFFER)
488 		TEE_Panic(res);
489 
490 	return res;
491 }
492 
TEE_ResetOperation(TEE_OperationHandle operation)493 void TEE_ResetOperation(TEE_OperationHandle operation)
494 {
495 	TEE_Result res;
496 
497 	if (operation == TEE_HANDLE_NULL)
498 		TEE_Panic(0);
499 
500 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET))
501 			TEE_Panic(0);
502 
503 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
504 
505 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
506 		res = _utee_hash_init(operation->state, NULL, 0);
507 		if (res != TEE_SUCCESS)
508 			TEE_Panic(res);
509 		operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
510 	} else {
511 		operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
512 	}
513 }
514 
TEE_SetOperationKey(TEE_OperationHandle operation,TEE_ObjectHandle key)515 TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation,
516 			       TEE_ObjectHandle key)
517 {
518 	TEE_Result res;
519 	uint32_t key_size = 0;
520 	TEE_ObjectInfo key_info;
521 
522 	if (operation == TEE_HANDLE_NULL) {
523 		res = TEE_ERROR_BAD_PARAMETERS;
524 		goto out;
525 	}
526 
527 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
528 		res = TEE_ERROR_BAD_PARAMETERS;
529 		goto out;
530 	}
531 
532 	if (key == TEE_HANDLE_NULL) {
533 		/* Operation key cleared */
534 		TEE_ResetTransientObject(operation->key1);
535 		operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
536 		return TEE_SUCCESS;
537 	}
538 
539 	/* No key for digest operation */
540 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
541 		res = TEE_ERROR_BAD_PARAMETERS;
542 		goto out;
543 	}
544 
545 	/* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */
546 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
547 	    0) {
548 		res = TEE_ERROR_BAD_PARAMETERS;
549 		goto out;
550 	}
551 
552 	res = TEE_GetObjectInfo1(key, &key_info);
553 	/* Key is not a valid handle */
554 	if (res != TEE_SUCCESS)
555 		goto out;
556 
557 	/* Supplied key has to meet required usage */
558 	if ((key_info.objectUsage & operation->info.requiredKeyUsage) !=
559 	    operation->info.requiredKeyUsage) {
560 		res = TEE_ERROR_BAD_PARAMETERS;
561 		goto out;
562 	}
563 
564 	if (operation->info.maxKeySize < key_info.keySize) {
565 		res = TEE_ERROR_BAD_PARAMETERS;
566 		goto out;
567 	}
568 
569 	key_size = key_info.keySize;
570 
571 	TEE_ResetTransientObject(operation->key1);
572 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
573 
574 	res = TEE_CopyObjectAttributes1(operation->key1, key);
575 	if (res != TEE_SUCCESS)
576 		goto out;
577 
578 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
579 
580 	operation->info.keySize = key_size;
581 
582 out:
583 	if (res != TEE_SUCCESS  &&
584 	    res != TEE_ERROR_CORRUPT_OBJECT &&
585 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
586 		TEE_Panic(res);
587 
588 	return res;
589 }
590 
TEE_SetOperationKey2(TEE_OperationHandle operation,TEE_ObjectHandle key1,TEE_ObjectHandle key2)591 TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation,
592 				TEE_ObjectHandle key1, TEE_ObjectHandle key2)
593 {
594 	TEE_Result res;
595 	uint32_t key_size = 0;
596 	TEE_ObjectInfo key_info1;
597 	TEE_ObjectInfo key_info2;
598 
599 	if (operation == TEE_HANDLE_NULL) {
600 		res = TEE_ERROR_BAD_PARAMETERS;
601 		goto out;
602 	}
603 
604 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
605 		res = TEE_ERROR_BAD_PARAMETERS;
606 		goto out;
607 	}
608 
609 	/*
610 	 * Key1/Key2 and/or are not initialized and
611 	 * Either both keys are NULL or both are not NULL
612 	 */
613 	if (!key1 && !key2) {
614 		/* Clear the keys */
615 		TEE_ResetTransientObject(operation->key1);
616 		TEE_ResetTransientObject(operation->key2);
617 		operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
618 		return TEE_SUCCESS;
619 	} else if (!key1 || !key2) {
620 		/* Both keys are obviously not valid. */
621 		res = TEE_ERROR_BAD_PARAMETERS;
622 		goto out;
623 	}
624 
625 	/* No key for digest operation */
626 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
627 		res = TEE_ERROR_BAD_PARAMETERS;
628 		goto out;
629 	}
630 
631 	/* Two keys flag expected (TEE_ALG_AES_XTS and TEE_ALG_SM2_KEP only) */
632 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) ==
633 	    0) {
634 		res = TEE_ERROR_BAD_PARAMETERS;
635 		goto out;
636 	}
637 
638 	res = TEE_GetObjectInfo1(key1, &key_info1);
639 	/* Key1 is not a valid handle */
640 	if (res != TEE_SUCCESS)
641 		goto out;
642 
643 	/* Supplied key has to meet required usage */
644 	if ((key_info1.objectUsage & operation->info.
645 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
646 		res = TEE_ERROR_BAD_PARAMETERS;
647 		goto out;
648 	}
649 
650 	res = TEE_GetObjectInfo1(key2, &key_info2);
651 	/* Key2 is not a valid handle */
652 	if (res != TEE_SUCCESS) {
653 		if (res == TEE_ERROR_CORRUPT_OBJECT)
654 			res = TEE_ERROR_CORRUPT_OBJECT_2;
655 		goto out;
656 	}
657 
658 	/* Supplied key has to meet required usage */
659 	if ((key_info2.objectUsage & operation->info.
660 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
661 		res = TEE_ERROR_BAD_PARAMETERS;
662 		goto out;
663 	}
664 
665 	/*
666 	 * All the multi key algorithm currently supported requires the keys to
667 	 * be of equal size.
668 	 */
669 	if (key_info1.keySize != key_info2.keySize) {
670 		res = TEE_ERROR_BAD_PARAMETERS;
671 		goto out;
672 
673 	}
674 
675 	if (operation->info.maxKeySize < key_info1.keySize) {
676 		res = TEE_ERROR_BAD_PARAMETERS;
677 		goto out;
678 	}
679 
680 	/*
681 	 * Odd that only the size of one key should be reported while
682 	 * size of two key are used when allocating the operation.
683 	 */
684 	key_size = key_info1.keySize;
685 
686 	TEE_ResetTransientObject(operation->key1);
687 	TEE_ResetTransientObject(operation->key2);
688 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
689 
690 	res = TEE_CopyObjectAttributes1(operation->key1, key1);
691 	if (res != TEE_SUCCESS)
692 		goto out;
693 	res = TEE_CopyObjectAttributes1(operation->key2, key2);
694 	if (res != TEE_SUCCESS) {
695 		if (res == TEE_ERROR_CORRUPT_OBJECT)
696 			res = TEE_ERROR_CORRUPT_OBJECT_2;
697 		goto out;
698 	}
699 
700 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
701 
702 	operation->info.keySize = key_size;
703 
704 out:
705 	if (res != TEE_SUCCESS  &&
706 	    res != TEE_ERROR_CORRUPT_OBJECT &&
707 	    res != TEE_ERROR_CORRUPT_OBJECT_2 &&
708 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE &&
709 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2)
710 		TEE_Panic(res);
711 
712 	return res;
713 }
714 
TEE_CopyOperation(TEE_OperationHandle dst_op,TEE_OperationHandle src_op)715 void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op)
716 {
717 	TEE_Result res;
718 
719 	if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL)
720 		TEE_Panic(0);
721 	if (dst_op->info.algorithm != src_op->info.algorithm)
722 		TEE_Panic(0);
723 	if (dst_op->info.mode != src_op->info.mode)
724 		TEE_Panic(0);
725 	if (src_op->info.operationClass != TEE_OPERATION_DIGEST) {
726 		TEE_ObjectHandle key1 = TEE_HANDLE_NULL;
727 		TEE_ObjectHandle key2 = TEE_HANDLE_NULL;
728 
729 		if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) {
730 			key1 = src_op->key1;
731 			key2 = src_op->key2;
732 		}
733 
734 		if ((src_op->info.handleState &
735 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) {
736 			TEE_SetOperationKey(dst_op, key1);
737 		} else {
738 			TEE_SetOperationKey2(dst_op, key1, key2);
739 		}
740 	}
741 	dst_op->info.handleState = src_op->info.handleState;
742 	dst_op->info.keySize = src_op->info.keySize;
743 	dst_op->info.digestLength = src_op->info.digestLength;
744 	dst_op->operationState = src_op->operationState;
745 
746 	if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks ||
747 	    dst_op->block_size != src_op->block_size)
748 		TEE_Panic(0);
749 
750 	if (dst_op->buffer != NULL) {
751 		if (src_op->buffer == NULL)
752 			TEE_Panic(0);
753 
754 		memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs);
755 		dst_op->buffer_offs = src_op->buffer_offs;
756 	} else if (src_op->buffer != NULL) {
757 		TEE_Panic(0);
758 	}
759 
760 	res = _utee_cryp_state_copy(dst_op->state, src_op->state);
761 	if (res != TEE_SUCCESS)
762 		TEE_Panic(res);
763 }
764 
765 /* Cryptographic Operations API - Message Digest Functions */
766 
init_hash_operation(TEE_OperationHandle operation,const void * IV,uint32_t IVLen)767 static void init_hash_operation(TEE_OperationHandle operation, const void *IV,
768 				uint32_t IVLen)
769 {
770 	TEE_Result res;
771 
772 	/*
773 	 * Note : IV and IVLen are never used in current implementation
774 	 * This is why coherent values of IV and IVLen are not checked
775 	 */
776 	res = _utee_hash_init(operation->state, IV, IVLen);
777 	if (res != TEE_SUCCESS)
778 		TEE_Panic(res);
779 	operation->buffer_offs = 0;
780 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
781 }
782 
TEE_DigestUpdate(TEE_OperationHandle operation,const void * chunk,uint32_t chunkSize)783 void TEE_DigestUpdate(TEE_OperationHandle operation,
784 		      const void *chunk, uint32_t chunkSize)
785 {
786 	TEE_Result res = TEE_ERROR_GENERIC;
787 
788 	if (operation == TEE_HANDLE_NULL ||
789 	    operation->info.operationClass != TEE_OPERATION_DIGEST)
790 		TEE_Panic(0);
791 
792 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
793 
794 	res = _utee_hash_update(operation->state, chunk, chunkSize);
795 	if (res != TEE_SUCCESS)
796 		TEE_Panic(res);
797 }
798 
TEE_DigestDoFinal(TEE_OperationHandle operation,const void * chunk,uint32_t chunkLen,void * hash,uint32_t * hashLen)799 TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk,
800 			     uint32_t chunkLen, void *hash, uint32_t *hashLen)
801 {
802 	TEE_Result res;
803 	uint64_t hl;
804 
805 	if ((operation == TEE_HANDLE_NULL) ||
806 	    (!chunk && chunkLen) ||
807 	    (operation->info.operationClass != TEE_OPERATION_DIGEST)) {
808 		res = TEE_ERROR_BAD_PARAMETERS;
809 		goto out;
810 	}
811 	__utee_check_inout_annotation(hashLen, sizeof(*hashLen));
812 
813 	hl = *hashLen;
814 	res = _utee_hash_final(operation->state, chunk, chunkLen, hash, &hl);
815 	*hashLen = hl;
816 	if (res != TEE_SUCCESS)
817 		goto out;
818 
819 	/* Reset operation state */
820 	init_hash_operation(operation, NULL, 0);
821 
822 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
823 
824 out:
825 	if (res != TEE_SUCCESS &&
826 	    res != TEE_ERROR_SHORT_BUFFER)
827 		TEE_Panic(res);
828 
829 	return res;
830 }
831 
832 /* Cryptographic Operations API - Symmetric Cipher Functions */
833 
TEE_CipherInit(TEE_OperationHandle operation,const void * IV,uint32_t IVLen)834 void TEE_CipherInit(TEE_OperationHandle operation, const void *IV,
835 		    uint32_t IVLen)
836 {
837 	TEE_Result res;
838 
839 	if (operation == TEE_HANDLE_NULL)
840 		TEE_Panic(0);
841 
842 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
843 		TEE_Panic(0);
844 
845 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
846 	    !(operation->key1))
847 		TEE_Panic(0);
848 
849 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
850 		TEE_ResetOperation(operation);
851 
852 	if (IV && IVLen) {
853 		if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
854 		    operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
855 		    operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
856 		    operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD)
857 			TEE_Panic(0);
858 	}
859 
860 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
861 
862 	res = _utee_cipher_init(operation->state, IV, IVLen);
863 	if (res != TEE_SUCCESS)
864 		TEE_Panic(res);
865 
866 	operation->buffer_offs = 0;
867 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
868 }
869 
tee_buffer_update(TEE_OperationHandle op,TEE_Result (* update_func)(unsigned long state,const void * src,size_t slen,void * dst,uint64_t * dlen),const void * src_data,size_t src_len,void * dest_data,uint64_t * dest_len)870 static TEE_Result tee_buffer_update(
871 		TEE_OperationHandle op,
872 		TEE_Result(*update_func)(unsigned long state, const void *src,
873 				size_t slen, void *dst, uint64_t *dlen),
874 		const void *src_data, size_t src_len,
875 		void *dest_data, uint64_t *dest_len)
876 {
877 	TEE_Result res;
878 	const uint8_t *src = src_data;
879 	size_t slen = src_len;
880 	uint8_t *dst = dest_data;
881 	size_t dlen = *dest_len;
882 	size_t acc_dlen = 0;
883 	uint64_t tmp_dlen;
884 	size_t l;
885 	size_t buffer_size;
886 	size_t buffer_left;
887 
888 	if (!src) {
889 		if (slen)
890 			TEE_Panic(0);
891 		goto out;
892 	}
893 
894 	if (op->buffer_two_blocks) {
895 		buffer_size = op->block_size * 2;
896 		buffer_left = 1;
897 	} else {
898 		buffer_size = op->block_size;
899 		buffer_left = 0;
900 	}
901 
902 	if (op->buffer_offs > 0) {
903 		/* Fill up complete block */
904 		if (op->buffer_offs < op->block_size)
905 			l = MIN(slen, op->block_size - op->buffer_offs);
906 		else
907 			l = MIN(slen, buffer_size - op->buffer_offs);
908 		memcpy(op->buffer + op->buffer_offs, src, l);
909 		op->buffer_offs += l;
910 		src += l;
911 		slen -= l;
912 		if ((op->buffer_offs % op->block_size) != 0)
913 			goto out;	/* Nothing left to do */
914 	}
915 
916 	/* If we can feed from buffer */
917 	if ((op->buffer_offs > 0) &&
918 	    ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) {
919 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
920 				op->block_size);
921 		l = MIN(op->buffer_offs, l);
922 		tmp_dlen = dlen;
923 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
924 		if (res != TEE_SUCCESS)
925 			TEE_Panic(res);
926 		dst += tmp_dlen;
927 		dlen -= tmp_dlen;
928 		acc_dlen += tmp_dlen;
929 		op->buffer_offs -= l;
930 		if (op->buffer_offs > 0) {
931 			/*
932 			 * Slen is small enough to be contained in rest buffer.
933 			 */
934 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
935 			memcpy(op->buffer + op->buffer_offs, src, slen);
936 			op->buffer_offs += slen;
937 			goto out;	/* Nothing left to do */
938 		}
939 	}
940 
941 	if (slen >= (buffer_size + buffer_left)) {
942 		/* Buffer is empty, feed as much as possible from src */
943 		if (op->info.algorithm == TEE_ALG_AES_CTS)
944 			l = ROUNDUP(slen - buffer_size, op->block_size);
945 		else
946 			l = ROUNDUP(slen - buffer_size + 1, op->block_size);
947 
948 		tmp_dlen = dlen;
949 		res = update_func(op->state, src, l, dst, &tmp_dlen);
950 		if (res != TEE_SUCCESS)
951 			TEE_Panic(res);
952 		src += l;
953 		slen -= l;
954 		dst += tmp_dlen;
955 		dlen -= tmp_dlen;
956 		acc_dlen += tmp_dlen;
957 	}
958 
959 	/* Slen is small enough to be contained in buffer. */
960 	memcpy(op->buffer + op->buffer_offs, src, slen);
961 	op->buffer_offs += slen;
962 
963 out:
964 	*dest_len = acc_dlen;
965 	return TEE_SUCCESS;
966 }
967 
TEE_CipherUpdate(TEE_OperationHandle operation,const void * srcData,uint32_t srcLen,void * destData,uint32_t * destLen)968 TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData,
969 			    uint32_t srcLen, void *destData, uint32_t *destLen)
970 {
971 	TEE_Result res;
972 	size_t req_dlen;
973 	uint64_t dl;
974 
975 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
976 		res = TEE_ERROR_BAD_PARAMETERS;
977 		goto out;
978 	}
979 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
980 
981 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
982 		res = TEE_ERROR_BAD_PARAMETERS;
983 		goto out;
984 	}
985 
986 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
987 		res = TEE_ERROR_BAD_PARAMETERS;
988 		goto out;
989 	}
990 
991 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
992 		res = TEE_ERROR_BAD_PARAMETERS;
993 		goto out;
994 	}
995 
996 	if (!srcData && !srcLen) {
997 		*destLen = 0;
998 		res = TEE_SUCCESS;
999 		goto out;
1000 	}
1001 
1002 	/* Calculate required dlen */
1003 	if (operation->block_size > 1) {
1004 		req_dlen = ((operation->buffer_offs + srcLen) /
1005 			    operation->block_size) * operation->block_size;
1006 	} else {
1007 		req_dlen = srcLen;
1008 	}
1009 	if (operation->buffer_two_blocks) {
1010 		if (req_dlen > operation->block_size * 2)
1011 			req_dlen -= operation->block_size * 2;
1012 		else
1013 			req_dlen = 0;
1014 	}
1015 	/*
1016 	 * Check that required destLen is big enough before starting to feed
1017 	 * data to the algorithm. Errors during feeding of data are fatal as we
1018 	 * can't restore sync with this API.
1019 	 */
1020 	if (*destLen < req_dlen) {
1021 		*destLen = req_dlen;
1022 		res = TEE_ERROR_SHORT_BUFFER;
1023 		goto out;
1024 	}
1025 
1026 	dl = *destLen;
1027 	if (operation->block_size > 1) {
1028 		res = tee_buffer_update(operation, _utee_cipher_update, srcData,
1029 					srcLen, destData, &dl);
1030 	} else {
1031 		if (srcLen > 0) {
1032 			res = _utee_cipher_update(operation->state, srcData,
1033 						  srcLen, destData, &dl);
1034 		} else {
1035 			res = TEE_SUCCESS;
1036 			dl = 0;
1037 		}
1038 	}
1039 	*destLen = dl;
1040 
1041 out:
1042 	if (res != TEE_SUCCESS &&
1043 	    res != TEE_ERROR_SHORT_BUFFER)
1044 		TEE_Panic(res);
1045 
1046 	return res;
1047 }
1048 
TEE_CipherDoFinal(TEE_OperationHandle operation,const void * srcData,uint32_t srcLen,void * destData,uint32_t * destLen)1049 TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation,
1050 			     const void *srcData, uint32_t srcLen,
1051 			     void *destData, uint32_t *destLen)
1052 {
1053 	TEE_Result res = TEE_SUCCESS;
1054 	uint8_t *dst = destData;
1055 	size_t acc_dlen = 0;
1056 	uint64_t tmp_dlen = 0;
1057 	size_t req_dlen = 0;
1058 
1059 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1060 		res = TEE_ERROR_BAD_PARAMETERS;
1061 		goto out;
1062 	}
1063 	if (destLen)
1064 		__utee_check_inout_annotation(destLen, sizeof(*destLen));
1065 
1066 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
1067 		res = TEE_ERROR_BAD_PARAMETERS;
1068 		goto out;
1069 	}
1070 
1071 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1072 		res = TEE_ERROR_BAD_PARAMETERS;
1073 		goto out;
1074 	}
1075 
1076 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1077 		res = TEE_ERROR_BAD_PARAMETERS;
1078 		goto out;
1079 	}
1080 
1081 	/*
1082 	 * Check that the final block doesn't require padding for those
1083 	 * algorithms that requires client to supply padding.
1084 	 */
1085 	if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
1086 	    operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
1087 	    operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
1088 	    operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
1089 	    operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
1090 	    operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD ||
1091 	    operation->info.algorithm == TEE_ALG_SM4_ECB_NOPAD ||
1092 	    operation->info.algorithm == TEE_ALG_SM4_CBC_NOPAD) {
1093 		if (((operation->buffer_offs + srcLen) % operation->block_size)
1094 		    != 0) {
1095 			res = TEE_ERROR_BAD_PARAMETERS;
1096 			goto out;
1097 		}
1098 	}
1099 
1100 	/*
1101 	 * Check that required destLen is big enough before starting to feed
1102 	 * data to the algorithm. Errors during feeding of data are fatal as we
1103 	 * can't restore sync with this API.
1104 	 */
1105 	if (operation->block_size > 1) {
1106 		req_dlen = operation->buffer_offs + srcLen;
1107 	} else {
1108 		req_dlen = srcLen;
1109 	}
1110 	if (destLen)
1111 		tmp_dlen = *destLen;
1112 	if (tmp_dlen < req_dlen) {
1113 		if (destLen)
1114 			*destLen = req_dlen;
1115 		res = TEE_ERROR_SHORT_BUFFER;
1116 		goto out;
1117 	}
1118 
1119 	if (operation->block_size > 1) {
1120 		if (srcLen) {
1121 			res = tee_buffer_update(operation, _utee_cipher_update,
1122 						srcData, srcLen, dst,
1123 						&tmp_dlen);
1124 			if (res != TEE_SUCCESS)
1125 				goto out;
1126 
1127 			dst += tmp_dlen;
1128 			acc_dlen += tmp_dlen;
1129 
1130 			tmp_dlen = *destLen - acc_dlen;
1131 		}
1132 		res = _utee_cipher_final(operation->state, operation->buffer,
1133 					 operation->buffer_offs, dst,
1134 					 &tmp_dlen);
1135 	} else {
1136 		res = _utee_cipher_final(operation->state, srcData, srcLen, dst,
1137 					 &tmp_dlen);
1138 	}
1139 	if (res != TEE_SUCCESS)
1140 		goto out;
1141 
1142 	acc_dlen += tmp_dlen;
1143 	if (destLen)
1144 		*destLen = acc_dlen;
1145 
1146 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1147 
1148 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1149 
1150 out:
1151 	if (res != TEE_SUCCESS &&
1152 	    res != TEE_ERROR_SHORT_BUFFER)
1153 		TEE_Panic(res);
1154 
1155 	return res;
1156 }
1157 
1158 /* Cryptographic Operations API - MAC Functions */
1159 
TEE_MACInit(TEE_OperationHandle operation,const void * IV,uint32_t IVLen)1160 void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen)
1161 {
1162 	if (operation == TEE_HANDLE_NULL)
1163 		TEE_Panic(0);
1164 
1165 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1166 		TEE_Panic(0);
1167 
1168 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
1169 	    !(operation->key1))
1170 		TEE_Panic(0);
1171 
1172 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
1173 		TEE_ResetOperation(operation);
1174 
1175 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1176 
1177 	init_hash_operation(operation, IV, IVLen);
1178 }
1179 
TEE_MACUpdate(TEE_OperationHandle operation,const void * chunk,uint32_t chunkSize)1180 void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk,
1181 		   uint32_t chunkSize)
1182 {
1183 	TEE_Result res;
1184 
1185 	if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
1186 		TEE_Panic(0);
1187 
1188 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1189 		TEE_Panic(0);
1190 
1191 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1192 		TEE_Panic(0);
1193 
1194 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE)
1195 		TEE_Panic(0);
1196 
1197 	res = _utee_hash_update(operation->state, chunk, chunkSize);
1198 	if (res != TEE_SUCCESS)
1199 		TEE_Panic(res);
1200 }
1201 
TEE_MACComputeFinal(TEE_OperationHandle operation,const void * message,uint32_t messageLen,void * mac,uint32_t * macLen)1202 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation,
1203 			       const void *message, uint32_t messageLen,
1204 			       void *mac, uint32_t *macLen)
1205 {
1206 	TEE_Result res;
1207 	uint64_t ml;
1208 
1209 	if (operation == TEE_HANDLE_NULL || (!message && messageLen)) {
1210 		res = TEE_ERROR_BAD_PARAMETERS;
1211 		goto out;
1212 	}
1213 	__utee_check_inout_annotation(macLen, sizeof(*macLen));
1214 
1215 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
1216 		res = TEE_ERROR_BAD_PARAMETERS;
1217 		goto out;
1218 	}
1219 
1220 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1221 		res = TEE_ERROR_BAD_PARAMETERS;
1222 		goto out;
1223 	}
1224 
1225 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1226 		res = TEE_ERROR_BAD_PARAMETERS;
1227 		goto out;
1228 	}
1229 
1230 	ml = *macLen;
1231 	res = _utee_hash_final(operation->state, message, messageLen, mac, &ml);
1232 	*macLen = ml;
1233 	if (res != TEE_SUCCESS)
1234 		goto out;
1235 
1236 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1237 
1238 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1239 
1240 out:
1241 	if (res != TEE_SUCCESS &&
1242 	    res != TEE_ERROR_SHORT_BUFFER)
1243 		TEE_Panic(res);
1244 
1245 	return res;
1246 }
1247 
TEE_MACCompareFinal(TEE_OperationHandle operation,const void * message,uint32_t messageLen,const void * mac,uint32_t macLen)1248 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
1249 			       const void *message, uint32_t messageLen,
1250 			       const void *mac, uint32_t macLen)
1251 {
1252 	TEE_Result res;
1253 	uint8_t computed_mac[TEE_MAX_HASH_SIZE] = { 0 };
1254 	uint32_t computed_mac_size = TEE_MAX_HASH_SIZE;
1255 
1256 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
1257 		res = TEE_ERROR_BAD_PARAMETERS;
1258 		goto out;
1259 	}
1260 
1261 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1262 		res = TEE_ERROR_BAD_PARAMETERS;
1263 		goto out;
1264 	}
1265 
1266 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1267 		res = TEE_ERROR_BAD_PARAMETERS;
1268 		goto out;
1269 	}
1270 
1271 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
1272 				  &computed_mac_size);
1273 	if (res != TEE_SUCCESS)
1274 		goto out;
1275 
1276 	if (computed_mac_size != macLen) {
1277 		res = TEE_ERROR_MAC_INVALID;
1278 		goto out;
1279 	}
1280 
1281 	if (consttime_memcmp(mac, computed_mac, computed_mac_size) != 0) {
1282 		res = TEE_ERROR_MAC_INVALID;
1283 		goto out;
1284 	}
1285 
1286 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1287 
1288 out:
1289 	if (res != TEE_SUCCESS &&
1290 	    res != TEE_ERROR_MAC_INVALID)
1291 		TEE_Panic(res);
1292 
1293 	return res;
1294 }
1295 
1296 /* Cryptographic Operations API - Authenticated Encryption Functions */
1297 
TEE_AEInit(TEE_OperationHandle operation,const void * nonce,uint32_t nonceLen,uint32_t tagLen,uint32_t AADLen,uint32_t payloadLen)1298 TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce,
1299 		      uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen,
1300 		      uint32_t payloadLen)
1301 {
1302 	TEE_Result res;
1303 
1304 	if (operation == TEE_HANDLE_NULL || nonce == NULL) {
1305 		res = TEE_ERROR_BAD_PARAMETERS;
1306 		goto out;
1307 	}
1308 
1309 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1310 		res = TEE_ERROR_BAD_PARAMETERS;
1311 		goto out;
1312 	}
1313 
1314 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
1315 		res = TEE_ERROR_BAD_PARAMETERS;
1316 		goto out;
1317 	}
1318 
1319 	/*
1320 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
1321 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
1322 	 * according to the same principle so we have to check here instead to
1323 	 * be GP compliant.
1324 	 */
1325 	if (operation->info.algorithm == TEE_ALG_AES_GCM) {
1326 		/*
1327 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
1328 		 */
1329 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) {
1330 			res = TEE_ERROR_NOT_SUPPORTED;
1331 			goto out;
1332 		}
1333 	}
1334 
1335 	res = _utee_authenc_init(operation->state, nonce, nonceLen, tagLen / 8,
1336 				 AADLen, payloadLen);
1337 	if (res != TEE_SUCCESS)
1338 		goto out;
1339 
1340 	operation->info.digestLength = tagLen / 8;
1341 	operation->buffer_offs = 0;
1342 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1343 
1344 out:
1345 	if (res != TEE_SUCCESS &&
1346 	    res != TEE_ERROR_NOT_SUPPORTED)
1347 			TEE_Panic(res);
1348 
1349 	return res;
1350 }
1351 
TEE_AEUpdateAAD(TEE_OperationHandle operation,const void * AADdata,uint32_t AADdataLen)1352 void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata,
1353 		     uint32_t AADdataLen)
1354 {
1355 	TEE_Result res;
1356 
1357 	if (operation == TEE_HANDLE_NULL ||
1358 	    (AADdata == NULL && AADdataLen != 0))
1359 		TEE_Panic(0);
1360 
1361 	if (operation->info.operationClass != TEE_OPERATION_AE)
1362 		TEE_Panic(0);
1363 
1364 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1365 		TEE_Panic(0);
1366 
1367 	res = _utee_authenc_update_aad(operation->state, AADdata, AADdataLen);
1368 
1369 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1370 
1371 	if (res != TEE_SUCCESS)
1372 		TEE_Panic(res);
1373 }
1374 
TEE_AEUpdate(TEE_OperationHandle operation,const void * srcData,uint32_t srcLen,void * destData,uint32_t * destLen)1375 TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData,
1376 			uint32_t srcLen, void *destData, uint32_t *destLen)
1377 {
1378 	TEE_Result res = TEE_SUCCESS;
1379 	size_t req_dlen = 0;
1380 	uint64_t dl = 0;
1381 
1382 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1383 		res = TEE_ERROR_BAD_PARAMETERS;
1384 		goto out;
1385 	}
1386 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1387 
1388 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1389 		res = TEE_ERROR_BAD_PARAMETERS;
1390 		goto out;
1391 	}
1392 
1393 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1394 		res = TEE_ERROR_BAD_PARAMETERS;
1395 		goto out;
1396 	}
1397 
1398 	if (!srcData && !srcLen) {
1399 		*destLen = 0;
1400 		res = TEE_SUCCESS;
1401 		goto out;
1402 	}
1403 
1404 	/*
1405 	 * Check that required destLen is big enough before starting to feed
1406 	 * data to the algorithm. Errors during feeding of data are fatal as we
1407 	 * can't restore sync with this API.
1408 	 */
1409 	if (operation->block_size > 1) {
1410 		req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen,
1411 				     operation->block_size);
1412 	} else {
1413 		req_dlen = srcLen;
1414 	}
1415 
1416 	dl = *destLen;
1417 	if (dl < req_dlen) {
1418 		*destLen = req_dlen;
1419 		res = TEE_ERROR_SHORT_BUFFER;
1420 		goto out;
1421 	}
1422 
1423 	if (operation->block_size > 1) {
1424 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1425 					srcData, srcLen, destData, &dl);
1426 	} else {
1427 		if (srcLen > 0) {
1428 			res = _utee_authenc_update_payload(operation->state,
1429 							   srcData, srcLen,
1430 							   destData, &dl);
1431 		} else {
1432 			dl = 0;
1433 			res = TEE_SUCCESS;
1434 		}
1435 	}
1436 	if (res != TEE_SUCCESS)
1437 		goto out;
1438 
1439 	*destLen = dl;
1440 
1441 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1442 
1443 out:
1444 	if (res != TEE_SUCCESS &&
1445 	    res != TEE_ERROR_SHORT_BUFFER)
1446 			TEE_Panic(res);
1447 
1448 	return res;
1449 }
1450 
TEE_AEEncryptFinal(TEE_OperationHandle operation,const void * srcData,uint32_t srcLen,void * destData,uint32_t * destLen,void * tag,uint32_t * tagLen)1451 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation,
1452 			      const void *srcData, uint32_t srcLen,
1453 			      void *destData, uint32_t *destLen, void *tag,
1454 			      uint32_t *tagLen)
1455 {
1456 	TEE_Result res;
1457 	uint8_t *dst = destData;
1458 	size_t acc_dlen = 0;
1459 	uint64_t tmp_dlen;
1460 	size_t req_dlen;
1461 	uint64_t tl;
1462 
1463 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1464 		res = TEE_ERROR_BAD_PARAMETERS;
1465 		goto out;
1466 	}
1467 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1468 	__utee_check_inout_annotation(tagLen, sizeof(*tagLen));
1469 
1470 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1471 		res = TEE_ERROR_BAD_PARAMETERS;
1472 		goto out;
1473 	}
1474 
1475 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1476 		res = TEE_ERROR_BAD_PARAMETERS;
1477 		goto out;
1478 	}
1479 
1480 	/*
1481 	 * Check that required destLen is big enough before starting to feed
1482 	 * data to the algorithm. Errors during feeding of data are fatal as we
1483 	 * can't restore sync with this API.
1484 	 *
1485 	 * Need to check this before update_payload since sync would be lost if
1486 	 * we return short buffer after that.
1487 	 */
1488 	res = TEE_ERROR_GENERIC;
1489 
1490 	req_dlen = operation->buffer_offs + srcLen;
1491 	if (*destLen < req_dlen) {
1492 		*destLen = req_dlen;
1493 		res = TEE_ERROR_SHORT_BUFFER;
1494 	}
1495 
1496 	if (*tagLen < operation->info.digestLength) {
1497 		*tagLen = operation->info.digestLength;
1498 		res = TEE_ERROR_SHORT_BUFFER;
1499 	}
1500 
1501 	if (res == TEE_ERROR_SHORT_BUFFER)
1502 		goto out;
1503 
1504 	tl = *tagLen;
1505 	tmp_dlen = *destLen - acc_dlen;
1506 	if (operation->block_size > 1) {
1507 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1508 					srcData, srcLen, dst, &tmp_dlen);
1509 		if (res != TEE_SUCCESS)
1510 			goto out;
1511 
1512 		dst += tmp_dlen;
1513 		acc_dlen += tmp_dlen;
1514 
1515 		tmp_dlen = *destLen - acc_dlen;
1516 		res = _utee_authenc_enc_final(operation->state,
1517 					      operation->buffer,
1518 					      operation->buffer_offs, dst,
1519 					      &tmp_dlen, tag, &tl);
1520 	} else {
1521 		res = _utee_authenc_enc_final(operation->state, srcData,
1522 					      srcLen, dst, &tmp_dlen,
1523 					      tag, &tl);
1524 	}
1525 	*tagLen = tl;
1526 	if (res != TEE_SUCCESS)
1527 		goto out;
1528 
1529 	acc_dlen += tmp_dlen;
1530 	*destLen = acc_dlen;
1531 
1532 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1533 
1534 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1535 
1536 out:
1537 	if (res != TEE_SUCCESS &&
1538 	    res != TEE_ERROR_SHORT_BUFFER)
1539 			TEE_Panic(res);
1540 
1541 	return res;
1542 }
1543 
TEE_AEDecryptFinal(TEE_OperationHandle operation,const void * srcData,uint32_t srcLen,void * destData,uint32_t * destLen,void * tag,uint32_t tagLen)1544 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation,
1545 			      const void *srcData, uint32_t srcLen,
1546 			      void *destData, uint32_t *destLen, void *tag,
1547 			      uint32_t tagLen)
1548 {
1549 	TEE_Result res;
1550 	uint8_t *dst = destData;
1551 	size_t acc_dlen = 0;
1552 	uint64_t tmp_dlen;
1553 	size_t req_dlen;
1554 
1555 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen)) {
1556 		res = TEE_ERROR_BAD_PARAMETERS;
1557 		goto out;
1558 	}
1559 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1560 
1561 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1562 		res = TEE_ERROR_BAD_PARAMETERS;
1563 		goto out;
1564 	}
1565 
1566 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1567 		res = TEE_ERROR_BAD_PARAMETERS;
1568 		goto out;
1569 	}
1570 
1571 	/*
1572 	 * Check that required destLen is big enough before starting to feed
1573 	 * data to the algorithm. Errors during feeding of data are fatal as we
1574 	 * can't restore sync with this API.
1575 	 */
1576 	req_dlen = operation->buffer_offs + srcLen;
1577 	if (*destLen < req_dlen) {
1578 		*destLen = req_dlen;
1579 		res = TEE_ERROR_SHORT_BUFFER;
1580 		goto out;
1581 	}
1582 
1583 	tmp_dlen = *destLen - acc_dlen;
1584 	if (operation->block_size > 1) {
1585 		res = tee_buffer_update(operation, _utee_authenc_update_payload,
1586 					srcData, srcLen, dst, &tmp_dlen);
1587 		if (res != TEE_SUCCESS)
1588 			goto out;
1589 
1590 		dst += tmp_dlen;
1591 		acc_dlen += tmp_dlen;
1592 
1593 		tmp_dlen = *destLen - acc_dlen;
1594 		res = _utee_authenc_dec_final(operation->state,
1595 					      operation->buffer,
1596 					      operation->buffer_offs, dst,
1597 					      &tmp_dlen, tag, tagLen);
1598 	} else {
1599 		res = _utee_authenc_dec_final(operation->state, srcData,
1600 					      srcLen, dst, &tmp_dlen,
1601 					      tag, tagLen);
1602 	}
1603 	if (res != TEE_SUCCESS)
1604 		goto out;
1605 
1606 	/* Supplied tagLen should match what we initiated with */
1607 	if (tagLen != operation->info.digestLength)
1608 		res = TEE_ERROR_MAC_INVALID;
1609 
1610 	acc_dlen += tmp_dlen;
1611 	*destLen = acc_dlen;
1612 
1613 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1614 
1615 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1616 
1617 out:
1618 	if (res != TEE_SUCCESS &&
1619 	    res != TEE_ERROR_SHORT_BUFFER &&
1620 	    res != TEE_ERROR_MAC_INVALID)
1621 			TEE_Panic(res);
1622 
1623 	return res;
1624 }
1625 
1626 /* Cryptographic Operations API - Asymmetric Functions */
1627 
TEE_AsymmetricEncrypt(TEE_OperationHandle operation,const TEE_Attribute * params,uint32_t paramCount,const void * srcData,uint32_t srcLen,void * destData,uint32_t * destLen)1628 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation,
1629 				 const TEE_Attribute *params,
1630 				 uint32_t paramCount, const void *srcData,
1631 				 uint32_t srcLen, void *destData,
1632 				 uint32_t *destLen)
1633 {
1634 	TEE_Result res = TEE_SUCCESS;
1635 	struct utee_attribute ua[paramCount];
1636 	uint64_t dl = 0;
1637 
1638 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen))
1639 		TEE_Panic(0);
1640 
1641 	__utee_check_attr_in_annotation(params, paramCount);
1642 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1643 
1644 	if (!operation->key1)
1645 		TEE_Panic(0);
1646 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1647 		TEE_Panic(0);
1648 	if (operation->info.mode != TEE_MODE_ENCRYPT)
1649 		TEE_Panic(0);
1650 
1651 	__utee_from_attr(ua, params, paramCount);
1652 	dl = *destLen;
1653 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
1654 				  srcLen, destData, &dl);
1655 	*destLen = dl;
1656 
1657 	if (res != TEE_SUCCESS &&
1658 	    res != TEE_ERROR_SHORT_BUFFER &&
1659 	    res != TEE_ERROR_BAD_PARAMETERS)
1660 		TEE_Panic(res);
1661 
1662 	return res;
1663 }
1664 
TEE_AsymmetricDecrypt(TEE_OperationHandle operation,const TEE_Attribute * params,uint32_t paramCount,const void * srcData,uint32_t srcLen,void * destData,uint32_t * destLen)1665 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation,
1666 				 const TEE_Attribute *params,
1667 				 uint32_t paramCount, const void *srcData,
1668 				 uint32_t srcLen, void *destData,
1669 				 uint32_t *destLen)
1670 {
1671 	TEE_Result res = TEE_SUCCESS;
1672 	struct utee_attribute ua[paramCount];
1673 	uint64_t dl = 0;
1674 
1675 	if (operation == TEE_HANDLE_NULL || (!srcData && srcLen))
1676 		TEE_Panic(0);
1677 
1678 	__utee_check_attr_in_annotation(params, paramCount);
1679 	__utee_check_inout_annotation(destLen, sizeof(*destLen));
1680 
1681 	if (!operation->key1)
1682 		TEE_Panic(0);
1683 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1684 		TEE_Panic(0);
1685 	if (operation->info.mode != TEE_MODE_DECRYPT)
1686 		TEE_Panic(0);
1687 
1688 	__utee_from_attr(ua, params, paramCount);
1689 	dl = *destLen;
1690 	res = _utee_asymm_operate(operation->state, ua, paramCount, srcData,
1691 				  srcLen, destData, &dl);
1692 	*destLen = dl;
1693 
1694 	if (res != TEE_SUCCESS &&
1695 	    res != TEE_ERROR_SHORT_BUFFER &&
1696 	    res != TEE_ERROR_BAD_PARAMETERS)
1697 		TEE_Panic(res);
1698 
1699 	return res;
1700 }
1701 
TEE_AsymmetricSignDigest(TEE_OperationHandle operation,const TEE_Attribute * params,uint32_t paramCount,const void * digest,uint32_t digestLen,void * signature,uint32_t * signatureLen)1702 TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation,
1703 				    const TEE_Attribute *params,
1704 				    uint32_t paramCount, const void *digest,
1705 				    uint32_t digestLen, void *signature,
1706 				    uint32_t *signatureLen)
1707 {
1708 	TEE_Result res = TEE_SUCCESS;
1709 	struct utee_attribute ua[paramCount];
1710 	uint64_t sl = 0;
1711 
1712 	if (operation == TEE_HANDLE_NULL || (!digest && digestLen))
1713 		TEE_Panic(0);
1714 
1715 	__utee_check_attr_in_annotation(params, paramCount);
1716 	__utee_check_inout_annotation(signatureLen, sizeof(*signatureLen));
1717 
1718 	if (!operation->key1)
1719 		TEE_Panic(0);
1720 	if (operation->info.operationClass !=
1721 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1722 		TEE_Panic(0);
1723 	if (operation->info.mode != TEE_MODE_SIGN)
1724 		TEE_Panic(0);
1725 
1726 	__utee_from_attr(ua, params, paramCount);
1727 	sl = *signatureLen;
1728 	res = _utee_asymm_operate(operation->state, ua, paramCount, digest,
1729 				  digestLen, signature, &sl);
1730 	*signatureLen = sl;
1731 
1732 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
1733 		TEE_Panic(res);
1734 
1735 	return res;
1736 }
1737 
TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation,const TEE_Attribute * params,uint32_t paramCount,const void * digest,uint32_t digestLen,const void * signature,uint32_t signatureLen)1738 TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation,
1739 				      const TEE_Attribute *params,
1740 				      uint32_t paramCount, const void *digest,
1741 				      uint32_t digestLen,
1742 				      const void *signature,
1743 				      uint32_t signatureLen)
1744 {
1745 	TEE_Result res;
1746 	struct utee_attribute ua[paramCount];
1747 
1748 	if (operation == TEE_HANDLE_NULL ||
1749 	    (digest == NULL && digestLen != 0) ||
1750 	    (signature == NULL && signatureLen != 0))
1751 		TEE_Panic(0);
1752 
1753 	__utee_check_attr_in_annotation(params, paramCount);
1754 
1755 	if (!operation->key1)
1756 		TEE_Panic(0);
1757 	if (operation->info.operationClass !=
1758 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1759 		TEE_Panic(0);
1760 	if (operation->info.mode != TEE_MODE_VERIFY)
1761 		TEE_Panic(0);
1762 
1763 	__utee_from_attr(ua, params, paramCount);
1764 	res = _utee_asymm_verify(operation->state, ua, paramCount, digest,
1765 				 digestLen, signature, signatureLen);
1766 
1767 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
1768 		TEE_Panic(res);
1769 
1770 	return res;
1771 }
1772 
1773 /* Cryptographic Operations API - Key Derivation Functions */
1774 
TEE_DeriveKey(TEE_OperationHandle operation,const TEE_Attribute * params,uint32_t paramCount,TEE_ObjectHandle derivedKey)1775 void TEE_DeriveKey(TEE_OperationHandle operation,
1776 		   const TEE_Attribute *params, uint32_t paramCount,
1777 		   TEE_ObjectHandle derivedKey)
1778 {
1779 	TEE_Result res;
1780 	TEE_ObjectInfo key_info;
1781 	struct utee_attribute ua[paramCount];
1782 
1783 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
1784 		TEE_Panic(0);
1785 
1786 	__utee_check_attr_in_annotation(params, paramCount);
1787 
1788 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
1789 	    TEE_OPERATION_KEY_DERIVATION)
1790 		TEE_Panic(0);
1791 
1792 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
1793 		TEE_Panic(0);
1794 	if (!operation->key1)
1795 		TEE_Panic(0);
1796 	if (operation->info.mode != TEE_MODE_DERIVE)
1797 		TEE_Panic(0);
1798 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
1799 		TEE_Panic(0);
1800 
1801 	res = _utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info);
1802 	if (res != TEE_SUCCESS)
1803 		TEE_Panic(res);
1804 
1805 	if (key_info.objectType != TEE_TYPE_GENERIC_SECRET)
1806 		TEE_Panic(0);
1807 	if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1808 		TEE_Panic(0);
1809 
1810 	__utee_from_attr(ua, params, paramCount);
1811 	res = _utee_cryp_derive_key(operation->state, ua, paramCount,
1812 				    (unsigned long)derivedKey);
1813 	if (res != TEE_SUCCESS)
1814 		TEE_Panic(res);
1815 }
1816 
1817 /* Cryptographic Operations API - Random Number Generation Functions */
1818 
TEE_GenerateRandom(void * randomBuffer,uint32_t randomBufferLen)1819 void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen)
1820 {
1821 	TEE_Result res;
1822 
1823 	res = _utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
1824 	if (res != TEE_SUCCESS)
1825 		TEE_Panic(res);
1826 }
1827 
rand(void)1828 int rand(void)
1829 {
1830 	int rc;
1831 
1832 	TEE_GenerateRandom(&rc, sizeof(rc));
1833 
1834 	/*
1835 	 * RAND_MAX is the larges int, INT_MAX which is all bits but the
1836 	 * highest bit set.
1837 	 */
1838 	return rc & RAND_MAX;
1839 }
1840 
TEE_IsAlgorithmSupported(uint32_t alg,uint32_t element)1841 TEE_Result TEE_IsAlgorithmSupported(uint32_t alg, uint32_t element)
1842 {
1843 	if (IS_ENABLED(CFG_CRYPTO_AES)) {
1844 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
1845 			if (alg == TEE_ALG_AES_ECB_NOPAD)
1846 				goto check_element_none;
1847 		}
1848 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
1849 			if (alg == TEE_ALG_AES_CBC_NOPAD)
1850 				goto check_element_none;
1851 		}
1852 		if (IS_ENABLED(CFG_CRYPTO_CTR)) {
1853 			if (alg == TEE_ALG_AES_CTR)
1854 				goto check_element_none;
1855 		}
1856 		if (IS_ENABLED(CFG_CRYPTO_CTS)) {
1857 			if (alg == TEE_ALG_AES_CTS)
1858 				goto check_element_none;
1859 		}
1860 		if (IS_ENABLED(CFG_CRYPTO_XTS)) {
1861 			if (alg == TEE_ALG_AES_XTS)
1862 				goto check_element_none;
1863 		}
1864 		if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) {
1865 			if (alg == TEE_ALG_AES_CBC_MAC_NOPAD ||
1866 			    alg == TEE_ALG_AES_CBC_MAC_PKCS5)
1867 				goto check_element_none;
1868 		}
1869 		if (IS_ENABLED(CFG_CRYPTO_CMAC)) {
1870 			if (alg == TEE_ALG_AES_CMAC)
1871 				goto check_element_none;
1872 		}
1873 		if (IS_ENABLED(CFG_CRYPTO_CCM)) {
1874 			if (alg == TEE_ALG_AES_CCM)
1875 				goto check_element_none;
1876 		}
1877 		if (IS_ENABLED(CFG_CRYPTO_GCM)) {
1878 			if (alg == TEE_ALG_AES_GCM)
1879 				goto check_element_none;
1880 		}
1881 	}
1882 	if (IS_ENABLED(CFG_CRYPTO_DES)) {
1883 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
1884 			if (alg == TEE_ALG_DES_ECB_NOPAD ||
1885 			    alg == TEE_ALG_DES3_ECB_NOPAD)
1886 				goto check_element_none;
1887 		}
1888 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
1889 			if (alg == TEE_ALG_DES_CBC_NOPAD ||
1890 			    alg == TEE_ALG_DES3_CBC_NOPAD)
1891 				goto check_element_none;
1892 		}
1893 		if (IS_ENABLED(CFG_CRYPTO_CBC_MAC)) {
1894 			if (alg == TEE_ALG_DES_CBC_MAC_NOPAD ||
1895 			    alg == TEE_ALG_DES_CBC_MAC_PKCS5 ||
1896 			    alg == TEE_ALG_DES3_CBC_MAC_NOPAD ||
1897 			    alg == TEE_ALG_DES3_CBC_MAC_PKCS5)
1898 				goto check_element_none;
1899 		}
1900 	}
1901 	if (IS_ENABLED(CFG_CRYPTO_MD5)) {
1902 		if (alg == TEE_ALG_MD5)
1903 			goto check_element_none;
1904 	}
1905 	if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
1906 		if (alg == TEE_ALG_SHA1)
1907 			goto check_element_none;
1908 	}
1909 	if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
1910 		if (alg == TEE_ALG_SHA224)
1911 			goto check_element_none;
1912 	}
1913 	if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
1914 		if (alg == TEE_ALG_SHA256)
1915 			goto check_element_none;
1916 	}
1917 	if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
1918 		if (alg == TEE_ALG_SHA384)
1919 			goto check_element_none;
1920 	}
1921 	if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
1922 		if (alg == TEE_ALG_SHA512)
1923 			goto check_element_none;
1924 	}
1925 	if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) {
1926 		if (alg == TEE_ALG_MD5SHA1)
1927 			goto check_element_none;
1928 	}
1929 	if (IS_ENABLED(CFG_CRYPTO_HMAC)) {
1930 		if (IS_ENABLED(CFG_CRYPTO_MD5)) {
1931 			if (alg == TEE_ALG_HMAC_MD5)
1932 				goto check_element_none;
1933 		}
1934 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
1935 			if (alg == TEE_ALG_HMAC_SHA1)
1936 				goto check_element_none;
1937 		}
1938 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
1939 			if (alg == TEE_ALG_HMAC_SHA224)
1940 				goto check_element_none;
1941 		}
1942 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
1943 			if (alg == TEE_ALG_HMAC_SHA256)
1944 				goto check_element_none;
1945 		}
1946 		if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
1947 			if (alg == TEE_ALG_HMAC_SHA384)
1948 				goto check_element_none;
1949 		}
1950 		if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
1951 			if (alg == TEE_ALG_HMAC_SHA512)
1952 				goto check_element_none;
1953 		}
1954 		if (IS_ENABLED(CFG_CRYPTO_SM3)) {
1955 			if (alg == TEE_ALG_HMAC_SM3)
1956 				goto check_element_none;
1957 		}
1958 	}
1959 	if (IS_ENABLED(CFG_CRYPTO_SM3)) {
1960 		if (alg == TEE_ALG_SM3)
1961 			goto check_element_none;
1962 	}
1963 	if (IS_ENABLED(CFG_CRYPTO_SM4)) {
1964 		if (IS_ENABLED(CFG_CRYPTO_ECB)) {
1965 			if (alg == TEE_ALG_SM4_ECB_NOPAD)
1966 				goto check_element_none;
1967 		}
1968 		if (IS_ENABLED(CFG_CRYPTO_CBC)) {
1969 			if (alg == TEE_ALG_SM4_CBC_NOPAD)
1970 				goto check_element_none;
1971 		}
1972 		if (IS_ENABLED(CFG_CRYPTO_CTR)) {
1973 			if (alg == TEE_ALG_SM4_CTR)
1974 				goto check_element_none;
1975 		}
1976 	}
1977 	if (IS_ENABLED(CFG_CRYPTO_RSA)) {
1978 		if (IS_ENABLED(CFG_CRYPTO_MD5)) {
1979 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5)
1980 				goto check_element_none;
1981 		}
1982 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
1983 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1 ||
1984 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1 ||
1985 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1)
1986 				goto check_element_none;
1987 		}
1988 		if (IS_ENABLED(CFG_CRYPTO_MD5) && IS_ENABLED(CFG_CRYPTO_SHA1)) {
1989 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1)
1990 				goto check_element_none;
1991 		}
1992 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
1993 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224 ||
1994 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224 ||
1995 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224)
1996 				goto check_element_none;
1997 		}
1998 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
1999 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256 ||
2000 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256 ||
2001 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256)
2002 				goto check_element_none;
2003 		}
2004 		if (IS_ENABLED(CFG_CRYPTO_SHA384)) {
2005 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384 ||
2006 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384 ||
2007 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384)
2008 				goto check_element_none;
2009 		}
2010 		if (IS_ENABLED(CFG_CRYPTO_SHA512)) {
2011 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 ||
2012 			    alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 ||
2013 			    alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512)
2014 				goto check_element_none;
2015 		}
2016 		if (IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) {
2017 			if (alg == TEE_ALG_RSASSA_PKCS1_V1_5)
2018 				goto check_element_none;
2019 		}
2020 		if (alg == TEE_ALG_RSA_NOPAD)
2021 			goto check_element_none;
2022 	}
2023 	if (IS_ENABLED(CFG_CRYPTO_DSA)) {
2024 		if (IS_ENABLED(CFG_CRYPTO_SHA1)) {
2025 			if (alg == TEE_ALG_DSA_SHA1)
2026 				goto check_element_none;
2027 		}
2028 		if (IS_ENABLED(CFG_CRYPTO_SHA224)) {
2029 			if (alg == TEE_ALG_DSA_SHA224)
2030 				goto check_element_none;
2031 		}
2032 		if (IS_ENABLED(CFG_CRYPTO_SHA256)) {
2033 			if (alg == TEE_ALG_DSA_SHA256)
2034 				goto check_element_none;
2035 		}
2036 	}
2037 	if (IS_ENABLED(CFG_CRYPTO_DH)) {
2038 		if (alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
2039 			goto check_element_none;
2040 	}
2041 	if (IS_ENABLED(CFG_CRYPTO_ECC)) {
2042 		if ((alg == TEE_ALG_ECDH_P192 || alg == TEE_ALG_ECDSA_P192) &&
2043 		    element == TEE_ECC_CURVE_NIST_P192)
2044 			return TEE_SUCCESS;
2045 		if ((alg == TEE_ALG_ECDH_P224 || alg == TEE_ALG_ECDSA_P224) &&
2046 		    element == TEE_ECC_CURVE_NIST_P224)
2047 			return TEE_SUCCESS;
2048 		if ((alg == TEE_ALG_ECDH_P256 || alg == TEE_ALG_ECDSA_P256) &&
2049 		    element == TEE_ECC_CURVE_NIST_P256)
2050 			return TEE_SUCCESS;
2051 		if ((alg == TEE_ALG_ECDH_P384 || alg == TEE_ALG_ECDSA_P384) &&
2052 		    element == TEE_ECC_CURVE_NIST_P384)
2053 			return TEE_SUCCESS;
2054 		if ((alg == TEE_ALG_ECDH_P521 || alg == TEE_ALG_ECDSA_P521) &&
2055 		    element == TEE_ECC_CURVE_NIST_P521)
2056 			return TEE_SUCCESS;
2057 	}
2058 	if (IS_ENABLED(CFG_CRYPTO_SM2_DSA)) {
2059 		if (alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2)
2060 			return TEE_SUCCESS;
2061 	}
2062 	if (IS_ENABLED(CFG_CRYPTO_SM2_KEP)) {
2063 		if (alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2)
2064 			return TEE_SUCCESS;
2065 	}
2066 	if (IS_ENABLED(CFG_CRYPTO_SM2_PKE)) {
2067 		if (alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2)
2068 			return TEE_SUCCESS;
2069 	}
2070 
2071 	return TEE_ERROR_NOT_SUPPORTED;
2072 check_element_none:
2073 	if (element == TEE_CRYPTO_ELEMENT_NONE)
2074 		return TEE_SUCCESS;
2075 	return TEE_ERROR_NOT_SUPPORTED;
2076 }
2077