xref: /optee_os/lib/libutee/tee_api_operations.c (revision dea1f2b63e922bc01051f65024ee9ce77fdd7c09)
1 /*
2  * Copyright (c) 2014, STMicroelectronics International N.V.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include <stdlib.h>
28 #include <string.h>
29 #include <string_ext.h>
30 
31 #include <tee_api.h>
32 #include <tee_api_defines_extensions.h>
33 #include <tee_internal_api_extensions.h>
34 #include <utee_syscalls.h>
35 #include <utee_defines.h>
36 #include <util.h>
37 
38 struct __TEE_OperationHandle {
39 	TEE_OperationInfo info;
40 	TEE_ObjectHandle key1;
41 	TEE_ObjectHandle key2;
42 	uint8_t *buffer;	/* buffer to collect complete blocks */
43 	bool buffer_two_blocks;	/* True if two blocks need to be buffered */
44 	size_t block_size;	/* Block size of cipher */
45 	size_t buffer_offs;	/* Offset in buffer */
46 	uint32_t state;		/* Handle to state in TEE Core */
47 	uint32_t ae_tag_len;	/*
48 				 * tag_len in bytes for AE operation else unused
49 				 */
50 };
51 
52 /* Cryptographic Operations API - Generic Operation Functions */
53 
54 TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation,
55 				 uint32_t algorithm, uint32_t mode,
56 				 uint32_t maxKeySize)
57 {
58 	TEE_Result res;
59 	TEE_OperationHandle op = TEE_HANDLE_NULL;
60 	uint32_t handle_state = 0;
61 	size_t block_size = 1;
62 	uint32_t req_key_usage;
63 	bool with_private_key = false;
64 	bool buffer_two_blocks = false;
65 
66 	if (!operation)
67 		TEE_Panic(0);
68 
69 	if (algorithm == TEE_ALG_AES_XTS)
70 		handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
71 
72 	/* Check algorithm max key size */
73 	switch (algorithm) {
74 	case TEE_ALG_DSA_SHA1:
75 		if (maxKeySize < 512)
76 			return TEE_ERROR_NOT_SUPPORTED;
77 		if (maxKeySize > 1024)
78 			return TEE_ERROR_NOT_SUPPORTED;
79 		if (maxKeySize % 64 != 0)
80 			return TEE_ERROR_NOT_SUPPORTED;
81 		break;
82 
83 	case TEE_ALG_DSA_SHA224:
84 		if (maxKeySize != 2048)
85 			return TEE_ERROR_NOT_SUPPORTED;
86 		break;
87 
88 	case TEE_ALG_DSA_SHA256:
89 		if (maxKeySize != 2048 && maxKeySize != 3072)
90 			return TEE_ERROR_NOT_SUPPORTED;
91 		break;
92 
93 	case TEE_ALG_ECDSA_P192:
94 	case TEE_ALG_ECDH_P192:
95 		if (maxKeySize != 192)
96 			return TEE_ERROR_NOT_SUPPORTED;
97 		break;
98 
99 	case TEE_ALG_ECDSA_P224:
100 	case TEE_ALG_ECDH_P224:
101 		if (maxKeySize != 224)
102 			return TEE_ERROR_NOT_SUPPORTED;
103 		break;
104 
105 	case TEE_ALG_ECDSA_P256:
106 	case TEE_ALG_ECDH_P256:
107 		if (maxKeySize != 256)
108 			return TEE_ERROR_NOT_SUPPORTED;
109 		break;
110 
111 	case TEE_ALG_ECDSA_P384:
112 	case TEE_ALG_ECDH_P384:
113 		if (maxKeySize != 384)
114 			return TEE_ERROR_NOT_SUPPORTED;
115 		break;
116 
117 	case TEE_ALG_ECDSA_P521:
118 	case TEE_ALG_ECDH_P521:
119 		if (maxKeySize != 521)
120 			return TEE_ERROR_NOT_SUPPORTED;
121 		break;
122 
123 	default:
124 		break;
125 	}
126 
127 	/* Check algorithm mode */
128 	switch (algorithm) {
129 	case TEE_ALG_AES_CTS:
130 	case TEE_ALG_AES_XTS:
131 		buffer_two_blocks = true;
132 	 /*FALLTHROUGH*/ case TEE_ALG_AES_ECB_NOPAD:
133 	case TEE_ALG_AES_CBC_NOPAD:
134 	case TEE_ALG_AES_CTR:
135 	case TEE_ALG_AES_CCM:
136 	case TEE_ALG_AES_GCM:
137 	case TEE_ALG_DES_ECB_NOPAD:
138 	case TEE_ALG_DES_CBC_NOPAD:
139 	case TEE_ALG_DES3_ECB_NOPAD:
140 	case TEE_ALG_DES3_CBC_NOPAD:
141 		if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES)
142 			block_size = TEE_AES_BLOCK_SIZE;
143 		else
144 			block_size = TEE_DES_BLOCK_SIZE;
145 
146 		if (mode == TEE_MODE_ENCRYPT)
147 			req_key_usage = TEE_USAGE_ENCRYPT;
148 		else if (mode == TEE_MODE_DECRYPT)
149 			req_key_usage = TEE_USAGE_DECRYPT;
150 		else
151 			return TEE_ERROR_NOT_SUPPORTED;
152 		break;
153 
154 	case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
155 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
156 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
157 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
158 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
159 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
160 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
161 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
162 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
163 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
164 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
165 	case TEE_ALG_DSA_SHA1:
166 	case TEE_ALG_DSA_SHA224:
167 	case TEE_ALG_DSA_SHA256:
168 	case TEE_ALG_ECDSA_P192:
169 	case TEE_ALG_ECDSA_P224:
170 	case TEE_ALG_ECDSA_P256:
171 	case TEE_ALG_ECDSA_P384:
172 	case TEE_ALG_ECDSA_P521:
173 		if (mode == TEE_MODE_SIGN) {
174 			with_private_key = true;
175 			req_key_usage = TEE_USAGE_SIGN;
176 		} else if (mode == TEE_MODE_VERIFY) {
177 			req_key_usage = TEE_USAGE_VERIFY;
178 		} else {
179 			return TEE_ERROR_NOT_SUPPORTED;
180 		}
181 		break;
182 
183 	case TEE_ALG_RSAES_PKCS1_V1_5:
184 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
185 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
186 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
187 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
188 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
189 		if (mode == TEE_MODE_ENCRYPT) {
190 			req_key_usage = TEE_USAGE_ENCRYPT;
191 		} else if (mode == TEE_MODE_DECRYPT) {
192 			with_private_key = true;
193 			req_key_usage = TEE_USAGE_DECRYPT;
194 		} else {
195 			return TEE_ERROR_NOT_SUPPORTED;
196 		}
197 		break;
198 
199 	case TEE_ALG_RSA_NOPAD:
200 		if (mode == TEE_MODE_ENCRYPT) {
201 			req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY;
202 		} else if (mode == TEE_MODE_DECRYPT) {
203 			with_private_key = true;
204 			req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN;
205 		} else {
206 			return TEE_ERROR_NOT_SUPPORTED;
207 		}
208 		break;
209 
210 	case TEE_ALG_DH_DERIVE_SHARED_SECRET:
211 	case TEE_ALG_ECDH_P192:
212 	case TEE_ALG_ECDH_P224:
213 	case TEE_ALG_ECDH_P256:
214 	case TEE_ALG_ECDH_P384:
215 	case TEE_ALG_ECDH_P521:
216 	case TEE_ALG_HKDF_MD5_DERIVE_KEY:
217 	case TEE_ALG_HKDF_SHA1_DERIVE_KEY:
218 	case TEE_ALG_HKDF_SHA224_DERIVE_KEY:
219 	case TEE_ALG_HKDF_SHA256_DERIVE_KEY:
220 	case TEE_ALG_HKDF_SHA384_DERIVE_KEY:
221 	case TEE_ALG_HKDF_SHA512_DERIVE_KEY:
222 	case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY:
223 	case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY:
224 	case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY:
225 	case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY:
226 	case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY:
227 	case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY:
228 		if (mode != TEE_MODE_DERIVE)
229 			return TEE_ERROR_NOT_SUPPORTED;
230 		with_private_key = true;
231 		req_key_usage = TEE_USAGE_DERIVE;
232 		break;
233 
234 	case TEE_ALG_MD5:
235 	case TEE_ALG_SHA1:
236 	case TEE_ALG_SHA224:
237 	case TEE_ALG_SHA256:
238 	case TEE_ALG_SHA384:
239 	case TEE_ALG_SHA512:
240 		if (mode != TEE_MODE_DIGEST)
241 			return TEE_ERROR_NOT_SUPPORTED;
242 		handle_state |= TEE_HANDLE_FLAG_KEY_SET;
243 		req_key_usage = 0;
244 		break;
245 
246 	case TEE_ALG_DES_CBC_MAC_NOPAD:
247 	case TEE_ALG_AES_CBC_MAC_NOPAD:
248 	case TEE_ALG_AES_CBC_MAC_PKCS5:
249 	case TEE_ALG_AES_CMAC:
250 	case TEE_ALG_DES_CBC_MAC_PKCS5:
251 	case TEE_ALG_DES3_CBC_MAC_NOPAD:
252 	case TEE_ALG_DES3_CBC_MAC_PKCS5:
253 	case TEE_ALG_HMAC_MD5:
254 	case TEE_ALG_HMAC_SHA1:
255 	case TEE_ALG_HMAC_SHA224:
256 	case TEE_ALG_HMAC_SHA256:
257 	case TEE_ALG_HMAC_SHA384:
258 	case TEE_ALG_HMAC_SHA512:
259 		if (mode != TEE_MODE_MAC)
260 			return TEE_ERROR_NOT_SUPPORTED;
261 		req_key_usage = TEE_USAGE_MAC;
262 		break;
263 
264 	default:
265 		return TEE_ERROR_NOT_SUPPORTED;
266 	}
267 
268 	op = TEE_Malloc(sizeof(*op), 0);
269 	if (!op)
270 		return TEE_ERROR_OUT_OF_MEMORY;
271 
272 	op->info.algorithm = algorithm;
273 	op->info.operationClass = TEE_ALG_GET_CLASS(algorithm);
274 	op->info.mode = mode;
275 	op->info.maxKeySize = maxKeySize;
276 	op->info.requiredKeyUsage = req_key_usage;
277 	op->info.handleState = handle_state;
278 
279 	if (block_size > 1) {
280 		size_t buffer_size = block_size;
281 
282 		if (buffer_two_blocks)
283 			buffer_size *= 2;
284 
285 		op->buffer = TEE_Malloc(buffer_size,
286 					TEE_USER_MEM_HINT_NO_FILL_ZERO);
287 		if (op->buffer == NULL) {
288 			res = TEE_ERROR_OUT_OF_MEMORY;
289 			goto err0;
290 		}
291 	}
292 	op->block_size = block_size;
293 	op->buffer_two_blocks = buffer_two_blocks;
294 
295 	if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) {
296 		uint32_t mks = maxKeySize;
297 		TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm,
298 						       with_private_key);
299 
300 		/*
301 		 * If two keys are expected the max key size is the sum of
302 		 * the size of both keys.
303 		 */
304 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS)
305 			mks /= 2;
306 
307 		res = TEE_AllocateTransientObject(key_type, mks, &op->key1);
308 		if (res != TEE_SUCCESS)
309 			goto err1;
310 
311 		if ((op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
312 		    0) {
313 			res = TEE_AllocateTransientObject(key_type, mks,
314 							  &op->key2);
315 			if (res != TEE_SUCCESS)
316 				goto err2;
317 		}
318 	}
319 
320 	res = utee_cryp_state_alloc(algorithm, mode, (uint32_t) op->key1,
321 				    (uint32_t) op->key2, &op->state);
322 	if (res != TEE_SUCCESS) {
323 		if ((op->info.handleState &
324 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 0)
325 			goto err2;
326 		goto err1;
327 	}
328 
329 	/* For multi-stage operation do an "init". */
330 	TEE_ResetOperation(op);
331 	*operation = op;
332 	goto out;
333 
334 err2:
335 	TEE_FreeTransientObject(op->key2);
336 err1:
337 	TEE_FreeTransientObject(op->key1);
338 err0:
339 	TEE_FreeOperation(op);
340 
341 	if (res != TEE_SUCCESS &&
342 	    res != TEE_ERROR_OUT_OF_MEMORY &&
343 	    res != TEE_ERROR_NOT_SUPPORTED)
344 		TEE_Panic(0);
345 out:
346 	return res;
347 }
348 
349 void TEE_FreeOperation(TEE_OperationHandle operation)
350 {
351 	TEE_Result res;
352 
353 	if (operation == TEE_HANDLE_NULL)
354 		TEE_Panic(0);
355 
356 	/*
357 	 * Note that keys should not be freed here, since they are
358 	 * claimed by the operation they will be freed by
359 	 * utee_cryp_state_free().
360 	 */
361 	res = utee_cryp_state_free(operation->state);
362 	if (res != TEE_SUCCESS)
363 		TEE_Panic(0);
364 
365 	TEE_Free(operation->buffer);
366 	TEE_Free(operation);
367 }
368 
369 void TEE_GetOperationInfo(TEE_OperationHandle operation,
370 			  TEE_OperationInfo *operationInfo)
371 {
372 	if (operation == TEE_HANDLE_NULL)
373 		TEE_Panic(0);
374 
375 	if (operationInfo == NULL)
376 		TEE_Panic(0);
377 
378 	*operationInfo = operation->info;
379 }
380 
381 void TEE_ResetOperation(TEE_OperationHandle operation)
382 {
383 	TEE_Result res;
384 
385 	if (operation == TEE_HANDLE_NULL)
386 		TEE_Panic(0);
387 
388 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) != 0) {
389 		if (operation->info.keySize == 0)
390 			TEE_Panic(0);
391 	}
392 
393 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
394 		res = utee_hash_init(operation->state, NULL, 0);
395 		if (res != TEE_SUCCESS)
396 			TEE_Panic(res);
397 	}
398 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
399 }
400 
401 TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation,
402 			       TEE_ObjectHandle key)
403 {
404 	TEE_Result res;
405 	uint32_t key_size = 0;
406 	TEE_ObjectInfo key_info;
407 
408 	/* Operation is not a valid handle */
409 	if (operation == TEE_HANDLE_NULL) {
410 		res = TEE_ERROR_BAD_PARAMETERS;
411 		goto out;
412 	}
413 
414 	/* Key is not initialized */
415 	if (key == TEE_HANDLE_NULL) {
416 		/* Operation key cleared */
417 		TEE_ResetTransientObject(operation->key1);
418 		res = TEE_ERROR_BAD_PARAMETERS;
419 		goto out;
420 	}
421 
422 	/* No key for digest operation */
423 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
424 		res = TEE_ERROR_BAD_PARAMETERS;
425 		goto out;
426 	}
427 
428 	/* Two keys flag not expected (TEE_ALG_AES_XTS excluded) */
429 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
430 	    0) {
431 		res = TEE_ERROR_BAD_PARAMETERS;
432 		goto out;
433 	}
434 
435 	res = TEE_GetObjectInfo1(key, &key_info);
436 	/* Key is not a valid handle */
437 	if (res != TEE_SUCCESS)
438 		goto out;
439 
440 	/* Supplied key has to meet required usage */
441 	if ((key_info.objectUsage & operation->info.requiredKeyUsage) !=
442 	    operation->info.requiredKeyUsage) {
443 		res = TEE_ERROR_BAD_PARAMETERS;
444 		goto out;
445 	}
446 
447 	if (operation->info.maxKeySize < key_info.keySize) {
448 		res = TEE_ERROR_BAD_PARAMETERS;
449 		goto out;
450 	}
451 
452 	key_size = key_info.keySize;
453 
454 	TEE_ResetTransientObject(operation->key1);
455 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
456 
457 	res = TEE_CopyObjectAttributes1(operation->key1, key);
458 	if (res != TEE_SUCCESS)
459 		goto out;
460 
461 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
462 
463 	operation->info.keySize = key_size;
464 
465 out:
466 	if (res != TEE_SUCCESS  &&
467 	    res != TEE_ERROR_CORRUPT_OBJECT &&
468 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE)
469 		TEE_Panic(0);
470 
471 	return res;
472 }
473 
474 TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation,
475 				TEE_ObjectHandle key1, TEE_ObjectHandle key2)
476 {
477 	TEE_Result res;
478 	uint32_t key_size = 0;
479 	TEE_ObjectInfo key_info1;
480 	TEE_ObjectInfo key_info2;
481 
482 	/* Operation is not a valid handle */
483 	if (operation == TEE_HANDLE_NULL) {
484 		res = TEE_ERROR_BAD_PARAMETERS;
485 		goto out;
486 	}
487 
488 	/*
489 	 * Key1/Key2 and/or are not initialized and
490 	 * Either both keys are NULL or both are not NULL
491 	 */
492 	if (key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) {
493 		/* Clear operation key1 (if needed) */
494 		if (key1 == TEE_HANDLE_NULL)
495 			TEE_ResetTransientObject(operation->key1);
496 		/* Clear operation key2 (if needed) */
497 		if (key2 == TEE_HANDLE_NULL)
498 			TEE_ResetTransientObject(operation->key2);
499 		res = TEE_ERROR_BAD_PARAMETERS;
500 		goto out;
501 	}
502 
503 	/* No key for digest operation */
504 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
505 		res = TEE_ERROR_BAD_PARAMETERS;
506 		goto out;
507 	}
508 
509 	/* Two keys flag expected (TEE_ALG_AES_XTS only) */
510 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) ==
511 	    0) {
512 		res = TEE_ERROR_BAD_PARAMETERS;
513 		goto out;
514 	}
515 
516 	res = TEE_GetObjectInfo1(key1, &key_info1);
517 	/* Key1 is not a valid handle */
518 	if (res != TEE_SUCCESS)
519 		goto out;
520 
521 	/* Supplied key has to meet required usage */
522 	if ((key_info1.objectUsage & operation->info.
523 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
524 		res = TEE_ERROR_BAD_PARAMETERS;
525 		goto out;
526 	}
527 
528 	res = TEE_GetObjectInfo1(key2, &key_info2);
529 	/* Key2 is not a valid handle */
530 	if (res != TEE_SUCCESS) {
531 		if (res == TEE_ERROR_CORRUPT_OBJECT)
532 			res = TEE_ERROR_CORRUPT_OBJECT_2;
533 		goto out;
534 	}
535 
536 	/* Supplied key has to meet required usage */
537 	if ((key_info2.objectUsage & operation->info.
538 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
539 		res = TEE_ERROR_BAD_PARAMETERS;
540 		goto out;
541 	}
542 
543 	/*
544 	 * AES-XTS (the only multi key algorithm supported, requires the
545 	 * keys to be of equal size.
546 	 */
547 	if (operation->info.algorithm == TEE_ALG_AES_XTS &&
548 	    key_info1.keySize != key_info2.keySize) {
549 		res = TEE_ERROR_BAD_PARAMETERS;
550 		goto out;
551 
552 	}
553 
554 	if (operation->info.maxKeySize < key_info1.keySize) {
555 		res = TEE_ERROR_BAD_PARAMETERS;
556 		goto out;
557 	}
558 
559 	/*
560 	 * Odd that only the size of one key should be reported while
561 	 * size of two key are used when allocating the operation.
562 	 */
563 	key_size = key_info1.keySize;
564 
565 	TEE_ResetTransientObject(operation->key1);
566 	TEE_ResetTransientObject(operation->key2);
567 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
568 
569 	res = TEE_CopyObjectAttributes1(operation->key1, key1);
570 	if (res != TEE_SUCCESS)
571 		goto out;
572 
573 	res = TEE_CopyObjectAttributes1(operation->key2, key2);
574 	if (res != TEE_SUCCESS) {
575 		if (res == TEE_ERROR_CORRUPT_OBJECT)
576 			res = TEE_ERROR_CORRUPT_OBJECT_2;
577 		goto out;
578 	}
579 
580 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
581 
582 	operation->info.keySize = key_size;
583 
584 out:
585 	if (res != TEE_SUCCESS  &&
586 	    res != TEE_ERROR_CORRUPT_OBJECT &&
587 	    res != TEE_ERROR_CORRUPT_OBJECT_2 &&
588 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE &&
589 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2)
590 		TEE_Panic(0);
591 
592 	return res;
593 }
594 
595 void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op)
596 {
597 	TEE_Result res;
598 
599 	if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL)
600 		TEE_Panic(0);
601 	if (dst_op->info.algorithm != src_op->info.algorithm)
602 		TEE_Panic(0);
603 	if (src_op->info.operationClass != TEE_OPERATION_DIGEST) {
604 		TEE_ObjectHandle key1 = TEE_HANDLE_NULL;
605 		TEE_ObjectHandle key2 = TEE_HANDLE_NULL;
606 
607 		if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) {
608 			key1 = src_op->key1;
609 			key2 = src_op->key2;
610 		}
611 
612 		if ((src_op->info.handleState &
613 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) {
614 			TEE_SetOperationKey(dst_op, key1);
615 		} else {
616 			TEE_SetOperationKey2(dst_op, key1, key2);
617 		}
618 	}
619 	dst_op->info.handleState = src_op->info.handleState;
620 	dst_op->info.keySize = src_op->info.keySize;
621 
622 	if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks ||
623 	    dst_op->block_size != src_op->block_size)
624 		TEE_Panic(0);
625 
626 	if (dst_op->buffer != NULL) {
627 		if (src_op->buffer == NULL)
628 			TEE_Panic(0);
629 
630 		memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs);
631 		dst_op->buffer_offs = src_op->buffer_offs;
632 	} else if (src_op->buffer != NULL) {
633 		TEE_Panic(0);
634 	}
635 
636 	res = utee_cryp_state_copy(dst_op->state, src_op->state);
637 	if (res != TEE_SUCCESS)
638 		TEE_Panic(res);
639 }
640 
641 /* Cryptographic Operations API - Message Digest Functions */
642 
643 void TEE_DigestUpdate(TEE_OperationHandle operation,
644 		      void *chunk, uint32_t chunkSize)
645 {
646 	TEE_Result res = TEE_ERROR_GENERIC;
647 
648 	if (operation == TEE_HANDLE_NULL ||
649 	    operation->info.operationClass != TEE_OPERATION_DIGEST)
650 		TEE_Panic(0);
651 
652 	res = utee_hash_update(operation->state, chunk, chunkSize);
653 	if (res != TEE_SUCCESS)
654 		TEE_Panic(res);
655 }
656 
657 TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk,
658 			     uint32_t chunkLen, void *hash, uint32_t *hashLen)
659 {
660 	TEE_Result res;
661 
662 	if ((operation == TEE_HANDLE_NULL) ||
663 	    (!chunk && chunkLen) ||
664 	    !hash ||
665 	    !hashLen ||
666 	    (operation->info.operationClass != TEE_OPERATION_DIGEST)) {
667 		res = TEE_ERROR_BAD_PARAMETERS;
668 		goto out;
669 	}
670 
671 	res = utee_hash_final(operation->state, chunk, chunkLen, hash,
672 			       hashLen);
673 
674 out:
675 	if (res != TEE_SUCCESS &&
676 	    res != TEE_ERROR_SHORT_BUFFER)
677 		TEE_Panic(0);
678 
679 	return res;
680 }
681 
682 /* Cryptographic Operations API - Symmetric Cipher Functions */
683 
684 void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen)
685 {
686 	TEE_Result res;
687 
688 	if (operation == TEE_HANDLE_NULL)
689 		TEE_Panic(0);
690 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
691 		TEE_Panic(0);
692 	res = utee_cipher_init(operation->state, IV, IVLen);
693 	if (res != TEE_SUCCESS)
694 		TEE_Panic(res);
695 	operation->buffer_offs = 0;
696 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
697 }
698 
699 static TEE_Result tee_buffer_update(
700 		TEE_OperationHandle op,
701 		TEE_Result(*update_func) (uint32_t state, const void *src,
702 					  size_t slen, void *dst, uint32_t *dlen),
703 		const void *src_data, size_t src_len,
704 		void *dest_data, uint32_t *dest_len)
705 {
706 	TEE_Result res;
707 	const uint8_t *src = src_data;
708 	size_t slen = src_len;
709 	uint8_t *dst = dest_data;
710 	size_t dlen = *dest_len;
711 	size_t acc_dlen = 0;
712 	uint32_t tmp_dlen;
713 	size_t l;
714 	size_t buffer_size;
715 	size_t buffer_left;
716 
717 	if (op->buffer_two_blocks) {
718 		buffer_size = op->block_size * 2;
719 		buffer_left = 1;
720 	} else {
721 		buffer_size = op->block_size;
722 		buffer_left = 0;
723 	}
724 
725 	if (op->buffer_offs > 0) {
726 		/* Fill up complete block */
727 		if (op->buffer_offs < op->block_size)
728 			l = MIN(slen, op->block_size - op->buffer_offs);
729 		else
730 			l = MIN(slen, buffer_size - op->buffer_offs);
731 		memcpy(op->buffer + op->buffer_offs, src, l);
732 		op->buffer_offs += l;
733 		src += l;
734 		slen -= l;
735 		if ((op->buffer_offs % op->block_size) != 0)
736 			goto out;	/* Nothing left to do */
737 	}
738 
739 	/* If we can feed from buffer */
740 	if ((op->buffer_offs > 0) &&
741 	    ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) {
742 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
743 				op->block_size);
744 		l = MIN(op->buffer_offs, l);
745 		tmp_dlen = dlen;
746 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
747 		if (res != TEE_SUCCESS)
748 			TEE_Panic(res);
749 		dst += tmp_dlen;
750 		dlen -= tmp_dlen;
751 		acc_dlen += tmp_dlen;
752 		op->buffer_offs -= l;
753 		if (op->buffer_offs > 0) {
754 			/*
755 			 * Slen is small enough to be contained in rest buffer.
756 			 */
757 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
758 			memcpy(op->buffer + op->buffer_offs, src, slen);
759 			op->buffer_offs += slen;
760 			goto out;	/* Nothing left to do */
761 		}
762 	}
763 
764 	if (slen >= (buffer_size + buffer_left)) {
765 		/* Buffer is empty, feed as much as possible from src */
766 		if (TEE_ALIGNMENT_IS_OK(src, uint32_t)) {
767 			l = ROUNDUP(slen - buffer_size + 1, op->block_size);
768 
769 			tmp_dlen = dlen;
770 			res = update_func(op->state, src, l, dst, &tmp_dlen);
771 			if (res != TEE_SUCCESS)
772 				TEE_Panic(res);
773 			src += l;
774 			slen -= l;
775 			dst += tmp_dlen;
776 			dlen -= tmp_dlen;
777 			acc_dlen += tmp_dlen;
778 		} else {
779 			/*
780 			 * Supplied data isn't well aligned, we're forced to
781 			 * feed through the buffer.
782 			 */
783 			while (slen >= op->block_size) {
784 				memcpy(op->buffer, src, op->block_size);
785 
786 				tmp_dlen = dlen;
787 				res =
788 				    update_func(op->state, op->buffer,
789 						op->block_size, dst, &tmp_dlen);
790 				if (res != TEE_SUCCESS)
791 					TEE_Panic(res);
792 				src += op->block_size;
793 				slen -= op->block_size;
794 				dst += tmp_dlen;
795 				dlen -= tmp_dlen;
796 				acc_dlen += tmp_dlen;
797 			}
798 		}
799 	}
800 
801 	/* Slen is small enough to be contained in buffer. */
802 	memcpy(op->buffer + op->buffer_offs, src, slen);
803 	op->buffer_offs += slen;
804 
805 out:
806 	*dest_len = acc_dlen;
807 	return TEE_SUCCESS;
808 }
809 
810 TEE_Result TEE_CipherUpdate(TEE_OperationHandle op, const void *srcData,
811 			    uint32_t srcLen, void *destData, uint32_t *destLen)
812 {
813 	TEE_Result res;
814 	size_t req_dlen;
815 
816 	if (op == TEE_HANDLE_NULL ||
817 	    (srcData == NULL && srcLen != 0) ||
818 	    destLen == NULL ||
819 	    (destData == NULL && *destLen != 0)) {
820 		res = TEE_ERROR_BAD_PARAMETERS;
821 		goto out;
822 	}
823 
824 	if (op->info.operationClass != TEE_OPERATION_CIPHER) {
825 		res = TEE_ERROR_BAD_PARAMETERS;
826 		goto out;
827 	}
828 
829 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
830 		res = TEE_ERROR_BAD_PARAMETERS;
831 		goto out;
832 	}
833 
834 	/* Calculate required dlen */
835 	req_dlen = ((op->buffer_offs + srcLen) / op->block_size) *
836 	    op->block_size;
837 	if (op->buffer_two_blocks) {
838 		if (req_dlen > op->block_size * 2)
839 			req_dlen -= op->block_size * 2;
840 		else
841 			req_dlen = 0;
842 	}
843 	/*
844 	 * Check that required destLen is big enough before starting to feed
845 	 * data to the algorithm. Errors during feeding of data are fatal as we
846 	 * can't restore sync with this API.
847 	 */
848 	if (*destLen < req_dlen) {
849 		*destLen = req_dlen;
850 		res = TEE_ERROR_SHORT_BUFFER;
851 		goto out;
852 	}
853 
854 	res = tee_buffer_update(op, utee_cipher_update, srcData, srcLen,
855 				destData, destLen);
856 
857 out:
858 	if (res != TEE_SUCCESS &&
859 	    res != TEE_ERROR_SHORT_BUFFER)
860 		TEE_Panic(0);
861 
862 	return res;
863 }
864 
865 TEE_Result TEE_CipherDoFinal(TEE_OperationHandle op,
866 			     const void *srcData, uint32_t srcLen, void *destData,
867 			     uint32_t *destLen)
868 {
869 	TEE_Result res;
870 	uint8_t *dst = destData;
871 	size_t acc_dlen = 0;
872 	uint32_t tmp_dlen;
873 	size_t req_dlen;
874 
875 	if (op == TEE_HANDLE_NULL ||
876 	    (srcData == NULL && srcLen != 0) ||
877 	    destLen == NULL ||
878 	    (destData == NULL && *destLen != 0)) {
879 		res = TEE_ERROR_BAD_PARAMETERS;
880 		goto out;
881 	}
882 
883 	if (op->info.operationClass != TEE_OPERATION_CIPHER) {
884 		res = TEE_ERROR_BAD_PARAMETERS;
885 		goto out;
886 	}
887 
888 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
889 		res = TEE_ERROR_BAD_PARAMETERS;
890 		goto out;
891 	}
892 
893 	/*
894 	 * Check that the final block doesn't require padding for those
895 	 * algorithms that requires client to supply padding.
896 	 */
897 	if (op->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
898 	    op->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
899 	    op->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
900 	    op->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
901 	    op->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
902 	    op->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) {
903 		if (((op->buffer_offs + srcLen) % op->block_size) != 0) {
904 			res = TEE_ERROR_BAD_PARAMETERS;
905 			goto out;
906 		}
907 	}
908 
909 	/*
910 	 * Check that required destLen is big enough before starting to feed
911 	 * data to the algorithm. Errors during feeding of data are fatal as we
912 	 * can't restore sync with this API.
913 	 */
914 	req_dlen = op->buffer_offs + srcLen;
915 	if (*destLen < req_dlen) {
916 		*destLen = req_dlen;
917 		res = TEE_ERROR_SHORT_BUFFER;
918 		goto out;
919 	}
920 
921 	tmp_dlen = *destLen - acc_dlen;
922 	res = tee_buffer_update(op, utee_cipher_update, srcData, srcLen, dst,
923 				&tmp_dlen);
924 	if (res != TEE_SUCCESS)
925 		goto out;
926 
927 	dst += tmp_dlen;
928 	acc_dlen += tmp_dlen;
929 
930 	tmp_dlen = *destLen - acc_dlen;
931 	res = utee_cipher_final(op->state, op->buffer, op->buffer_offs,
932 				dst, &tmp_dlen);
933 	if (res != TEE_SUCCESS)
934 		goto out;
935 
936 	acc_dlen += tmp_dlen;
937 
938 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
939 	*destLen = acc_dlen;
940 
941 out:
942 	if (res != TEE_SUCCESS &&
943 	    res != TEE_ERROR_SHORT_BUFFER)
944 		TEE_Panic(0);
945 
946 	return res;
947 }
948 
949 /* Cryptographic Operations API - MAC Functions */
950 
951 void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen)
952 {
953 	TEE_Result res;
954 
955 	if (operation == TEE_HANDLE_NULL)
956 		TEE_Panic(0);
957 	if (IV == NULL && IVLen != 0)
958 		TEE_Panic(0);
959 	if (operation->info.operationClass != TEE_OPERATION_MAC)
960 		TEE_Panic(0);
961 	res = utee_hash_init(operation->state, IV, IVLen);
962 	if (res != TEE_SUCCESS)
963 		TEE_Panic(res);
964 	operation->buffer_offs = 0;
965 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
966 }
967 
968 void TEE_MACUpdate(TEE_OperationHandle op, const void *chunk, uint32_t chunkSize)
969 {
970 	TEE_Result res;
971 
972 	if (op == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
973 		TEE_Panic(0);
974 	if (op->info.operationClass != TEE_OPERATION_MAC)
975 		TEE_Panic(0);
976 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
977 		TEE_Panic(0);
978 
979 	res = utee_hash_update(op->state, chunk, chunkSize);
980 	if (res != TEE_SUCCESS)
981 		TEE_Panic(res);
982 }
983 
984 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle op,
985 			       const void *message, uint32_t messageLen,
986 			       void *mac, uint32_t *macLen)
987 {
988 	TEE_Result res;
989 
990 	if (op == TEE_HANDLE_NULL || (message == NULL && messageLen != 0) ||
991 	    mac == NULL || macLen == NULL)
992 		TEE_Panic(0);
993 	if (op->info.operationClass != TEE_OPERATION_MAC)
994 		TEE_Panic(0);
995 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
996 		TEE_Panic(0);
997 
998 	res = utee_hash_final(op->state, message, messageLen, mac, macLen);
999 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1000 	return res;
1001 }
1002 
1003 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
1004 			       const void *message, uint32_t messageLen,
1005 			       const void *mac, uint32_t macLen)
1006 {
1007 	TEE_Result res;
1008 	uint8_t computed_mac[TEE_MAX_HASH_SIZE];
1009 	uint32_t computed_mac_size = TEE_MAX_HASH_SIZE;
1010 
1011 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
1012 				  &computed_mac_size);
1013 	if (res != TEE_SUCCESS)
1014 		return res;
1015 	if (computed_mac_size != macLen)
1016 		return TEE_ERROR_MAC_INVALID;
1017 	if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0)
1018 		return TEE_ERROR_MAC_INVALID;
1019 	return TEE_SUCCESS;
1020 }
1021 
1022 /* Cryptographic Operations API - Authenticated Encryption Functions */
1023 
1024 TEE_Result TEE_AEInit(TEE_OperationHandle op, const void *nonce,
1025 		      uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen,
1026 		      uint32_t payloadLen)
1027 {
1028 	TEE_Result res;
1029 
1030 	if (op == TEE_HANDLE_NULL || nonce == NULL)
1031 		TEE_Panic(0);
1032 	if (op->info.operationClass != TEE_OPERATION_AE)
1033 		TEE_Panic(0);
1034 
1035 	/*
1036 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
1037 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
1038 	 * according to the same principle so we have to check here instead to
1039 	 * be GP compliant.
1040 	 */
1041 	if (op->info.algorithm == TEE_ALG_AES_GCM) {
1042 		/*
1043 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
1044 		 */
1045 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0))
1046 			return TEE_ERROR_NOT_SUPPORTED;
1047 	}
1048 
1049 	res = utee_authenc_init(op->state, nonce, nonceLen, tagLen / 8, AADLen,
1050 				payloadLen);
1051 	if (res != TEE_SUCCESS) {
1052 		if (res != TEE_ERROR_NOT_SUPPORTED)
1053 			TEE_Panic(res);
1054 		return res;
1055 	}
1056 	op->ae_tag_len = tagLen / 8;
1057 
1058 	op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1059 	return TEE_SUCCESS;
1060 }
1061 
1062 void TEE_AEUpdateAAD(TEE_OperationHandle op, const void *AADdata,
1063 		     uint32_t AADdataLen)
1064 {
1065 	TEE_Result res;
1066 
1067 	if (op == TEE_HANDLE_NULL || (AADdata == NULL && AADdataLen != 0))
1068 		TEE_Panic(0);
1069 	if (op->info.operationClass != TEE_OPERATION_AE)
1070 		TEE_Panic(0);
1071 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1072 		TEE_Panic(0);
1073 
1074 	res = utee_authenc_update_aad(op->state, AADdata, AADdataLen);
1075 	if (res != TEE_SUCCESS)
1076 		TEE_Panic(res);
1077 }
1078 
1079 TEE_Result TEE_AEUpdate(TEE_OperationHandle op, const void *srcData,
1080 			uint32_t srcLen, void *destData, uint32_t *destLen)
1081 {
1082 	size_t req_dlen;
1083 
1084 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1085 	    destLen == NULL || (destData == NULL && *destLen != 0))
1086 		TEE_Panic(0);
1087 	if (op->info.operationClass != TEE_OPERATION_AE)
1088 		TEE_Panic(0);
1089 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1090 		TEE_Panic(0);
1091 
1092 	/*
1093 	 * Check that required destLen is big enough before starting to feed
1094 	 * data to the algorithm. Errors during feeding of data are fatal as we
1095 	 * can't restore sync with this API.
1096 	 */
1097 	req_dlen = ROUNDDOWN(op->buffer_offs + srcLen, op->block_size);
1098 	if (*destLen < req_dlen) {
1099 		*destLen = req_dlen;
1100 		return TEE_ERROR_SHORT_BUFFER;
1101 	}
1102 
1103 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
1104 			  destData, destLen);
1105 
1106 	return TEE_SUCCESS;
1107 }
1108 
1109 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle op,
1110 			      const void *srcData, uint32_t srcLen,
1111 			      void *destData, uint32_t *destLen, void *tag,
1112 			      uint32_t *tagLen)
1113 {
1114 	TEE_Result res;
1115 	uint8_t *dst = destData;
1116 	size_t acc_dlen = 0;
1117 	uint32_t tmp_dlen;
1118 	size_t req_dlen;
1119 
1120 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1121 	    destLen == NULL || (destData == NULL && *destLen != 0) ||
1122 	    tag == NULL || tagLen == NULL)
1123 		TEE_Panic(0);
1124 	if (op->info.operationClass != TEE_OPERATION_AE)
1125 		TEE_Panic(0);
1126 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1127 		TEE_Panic(0);
1128 
1129 	/*
1130 	 * Check that required destLen is big enough before starting to feed
1131 	 * data to the algorithm. Errors during feeding of data are fatal as we
1132 	 * can't restore sync with this API.
1133 	 */
1134 	req_dlen = op->buffer_offs + srcLen;
1135 	if (*destLen < req_dlen) {
1136 		*destLen = req_dlen;
1137 		return TEE_ERROR_SHORT_BUFFER;
1138 	}
1139 
1140 	/*
1141 	 * Need to check this before update_payload since sync would be lost if
1142 	 * we return short buffer after that.
1143 	 */
1144 	if (*tagLen < op->ae_tag_len) {
1145 		*tagLen = op->ae_tag_len;
1146 		return TEE_ERROR_SHORT_BUFFER;
1147 	}
1148 
1149 	tmp_dlen = *destLen - acc_dlen;
1150 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
1151 			  dst, &tmp_dlen);
1152 	dst += tmp_dlen;
1153 	acc_dlen += tmp_dlen;
1154 
1155 	tmp_dlen = *destLen - acc_dlen;
1156 	res =
1157 	    utee_authenc_enc_final(op->state, op->buffer, op->buffer_offs, dst,
1158 				   &tmp_dlen, tag, tagLen);
1159 	if (res != TEE_SUCCESS)
1160 		TEE_Panic(res);
1161 	acc_dlen += tmp_dlen;
1162 
1163 	*destLen = acc_dlen;
1164 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1165 
1166 	return res;
1167 }
1168 
1169 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle op,
1170 			      const void *srcData, uint32_t srcLen,
1171 			      void *destData, uint32_t *destLen, const void *tag,
1172 			      uint32_t tagLen)
1173 {
1174 	TEE_Result res;
1175 	uint8_t *dst = destData;
1176 	size_t acc_dlen = 0;
1177 	uint32_t tmp_dlen;
1178 	size_t req_dlen;
1179 
1180 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1181 	    destLen == NULL || (destData == NULL && *destLen != 0) ||
1182 	    (tag == NULL && tagLen != 0))
1183 		TEE_Panic(0);
1184 	if (op->info.operationClass != TEE_OPERATION_AE)
1185 		TEE_Panic(0);
1186 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1187 		TEE_Panic(0);
1188 
1189 	/*
1190 	 * Check that required destLen is big enough before starting to feed
1191 	 * data to the algorithm. Errors during feeding of data are fatal as we
1192 	 * can't restore sync with this API.
1193 	 */
1194 	req_dlen = op->buffer_offs + srcLen;
1195 	if (*destLen < req_dlen) {
1196 		*destLen = req_dlen;
1197 		return TEE_ERROR_SHORT_BUFFER;
1198 	}
1199 
1200 	tmp_dlen = *destLen - acc_dlen;
1201 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
1202 			  dst, &tmp_dlen);
1203 	dst += tmp_dlen;
1204 	acc_dlen += tmp_dlen;
1205 
1206 	tmp_dlen = *destLen - acc_dlen;
1207 	res =
1208 	    utee_authenc_dec_final(op->state, op->buffer, op->buffer_offs, dst,
1209 				   &tmp_dlen, tag, tagLen);
1210 	if (res != TEE_SUCCESS && res != TEE_ERROR_MAC_INVALID)
1211 		TEE_Panic(res);
1212 	/* Supplied tagLen should match what we initiated with */
1213 	if (tagLen != op->ae_tag_len)
1214 		res = TEE_ERROR_MAC_INVALID;
1215 
1216 	acc_dlen += tmp_dlen;
1217 
1218 	*destLen = acc_dlen;
1219 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1220 
1221 	return res;
1222 }
1223 
1224 /* Cryptographic Operations API - Asymmetric Functions */
1225 
1226 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle op,
1227 				 const TEE_Attribute *params,
1228 				 uint32_t paramCount, const void *srcData,
1229 				 uint32_t srcLen, void *destData,
1230 				 uint32_t *destLen)
1231 {
1232 	TEE_Result res;
1233 
1234 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1235 	    destLen == NULL || (destData == NULL && *destLen != 0))
1236 		TEE_Panic(0);
1237 	if (paramCount != 0 && params == NULL)
1238 		TEE_Panic(0);
1239 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1240 		TEE_Panic(0);
1241 	if (op->info.mode != TEE_MODE_ENCRYPT)
1242 		TEE_Panic(0);
1243 
1244 	res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen,
1245 				 destData, destLen);
1246 	if (res != TEE_SUCCESS &&
1247 	    res != TEE_ERROR_SHORT_BUFFER &&
1248 	    res != TEE_ERROR_BAD_PARAMETERS)
1249 		TEE_Panic(res);
1250 	return res;
1251 }
1252 
1253 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle op,
1254 				 const TEE_Attribute *params,
1255 				 uint32_t paramCount, const void *srcData,
1256 				 uint32_t srcLen, void *destData,
1257 				 uint32_t *destLen)
1258 {
1259 	TEE_Result res;
1260 
1261 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1262 	    destLen == NULL || (destData == NULL && *destLen != 0))
1263 		TEE_Panic(0);
1264 	if (paramCount != 0 && params == NULL)
1265 		TEE_Panic(0);
1266 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1267 		TEE_Panic(0);
1268 	if (op->info.mode != TEE_MODE_DECRYPT)
1269 		TEE_Panic(0);
1270 
1271 	res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen,
1272 				 destData, destLen);
1273 	if (res != TEE_SUCCESS &&
1274 	    res != TEE_ERROR_SHORT_BUFFER &&
1275 	    res != TEE_ERROR_BAD_PARAMETERS)
1276 		TEE_Panic(res);
1277 	return res;
1278 }
1279 
1280 TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle op,
1281 				    const TEE_Attribute *params,
1282 				    uint32_t paramCount, const void *digest,
1283 				    uint32_t digestLen, void *signature,
1284 				    uint32_t *signatureLen)
1285 {
1286 	TEE_Result res;
1287 
1288 	if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) ||
1289 	    signature == NULL || signatureLen == NULL)
1290 		TEE_Panic(0);
1291 	if (paramCount != 0 && params == NULL)
1292 		TEE_Panic(0);
1293 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1294 		TEE_Panic(0);
1295 	if (op->info.mode != TEE_MODE_SIGN)
1296 		TEE_Panic(0);
1297 
1298 	res =
1299 	    utee_asymm_operate(op->state, params, paramCount, digest, digestLen,
1300 			       signature, signatureLen);
1301 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
1302 		TEE_Panic(res);
1303 	return res;
1304 }
1305 
1306 TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle op,
1307 				      const TEE_Attribute *params,
1308 				      uint32_t paramCount, const void *digest,
1309 				      uint32_t digestLen, const void *signature,
1310 				      uint32_t signatureLen)
1311 {
1312 	TEE_Result res;
1313 
1314 	if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) ||
1315 	    (signature == NULL && signatureLen != 0))
1316 		TEE_Panic(0);
1317 	if (paramCount != 0 && params == NULL)
1318 		TEE_Panic(0);
1319 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1320 		TEE_Panic(0);
1321 	if (op->info.mode != TEE_MODE_VERIFY)
1322 		TEE_Panic(0);
1323 
1324 	res =
1325 	    utee_asymm_verify(op->state, params, paramCount, digest, digestLen,
1326 			      signature, signatureLen);
1327 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
1328 		TEE_Panic(res);
1329 	return res;
1330 }
1331 
1332 /* Cryptographic Operations API - Key Derivation Functions */
1333 
1334 void TEE_DeriveKey(TEE_OperationHandle operation,
1335 		   const TEE_Attribute *params, uint32_t paramCount,
1336 		   TEE_ObjectHandle derivedKey)
1337 {
1338 	TEE_Result res;
1339 	TEE_ObjectInfo key_info;
1340 
1341 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
1342 		TEE_Panic(0);
1343 	if (paramCount != 0 && params == NULL)
1344 		TEE_Panic(0);
1345 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
1346 			TEE_OPERATION_KEY_DERIVATION)
1347 		TEE_Panic(0);
1348 
1349 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
1350 		TEE_Panic(0);
1351 	if (operation->info.mode != TEE_MODE_DERIVE)
1352 		TEE_Panic(0);
1353 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
1354 		TEE_Panic(0);
1355 
1356 	res = utee_cryp_obj_get_info((uint32_t) derivedKey, &key_info);
1357 	if (res != TEE_SUCCESS)
1358 		TEE_Panic(0);
1359 
1360 	if (key_info.objectType != TEE_TYPE_GENERIC_SECRET)
1361 		TEE_Panic(0);
1362 	if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1363 		TEE_Panic(0);
1364 
1365 	res = utee_cryp_derive_key(operation->state, params, paramCount,
1366 				   (uint32_t) derivedKey);
1367 	if (res != TEE_SUCCESS)
1368 		TEE_Panic(res);
1369 }
1370 
1371 /* Cryptographic Operations API - Random Number Generation Functions */
1372 
1373 void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen)
1374 {
1375 	TEE_Result res;
1376 
1377 	res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
1378 	if (res != TEE_SUCCESS)
1379 		TEE_Panic(res);
1380 }
1381