xref: /optee_os/lib/libutee/tee_api_operations.c (revision b36311adfbef58609a631a0bc5269916b412f5d6)
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 #include "tee_api_private.h"
38 
39 struct __TEE_OperationHandle {
40 	TEE_OperationInfo info;
41 	TEE_ObjectHandle key1;
42 	TEE_ObjectHandle key2;
43 	uint32_t operationState;/* Operation state : INITIAL or ACTIVE */
44 	uint8_t *buffer;	/* buffer to collect complete blocks */
45 	bool buffer_two_blocks;	/* True if two blocks need to be buffered */
46 	size_t block_size;	/* Block size of cipher */
47 	size_t buffer_offs;	/* Offset in buffer */
48 	uint32_t state;		/* Handle to state in TEE Core */
49 	uint32_t ae_tag_len;	/*
50 				 * tag_len in bytes for AE operation else unused
51 				 */
52 };
53 
54 /* Cryptographic Operations API - Generic Operation Functions */
55 
56 TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation,
57 				 uint32_t algorithm, uint32_t mode,
58 				 uint32_t maxKeySize)
59 {
60 	TEE_Result res;
61 	TEE_OperationHandle op = TEE_HANDLE_NULL;
62 	uint32_t handle_state = 0;
63 	size_t block_size = 1;
64 	uint32_t req_key_usage;
65 	bool with_private_key = false;
66 	bool buffer_two_blocks = false;
67 
68 	if (!operation)
69 		TEE_Panic(0);
70 
71 	if (algorithm == TEE_ALG_AES_XTS)
72 		handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
73 
74 	/* Check algorithm max key size */
75 	switch (algorithm) {
76 	case TEE_ALG_DSA_SHA1:
77 		if (maxKeySize < 512)
78 			return TEE_ERROR_NOT_SUPPORTED;
79 		if (maxKeySize > 1024)
80 			return TEE_ERROR_NOT_SUPPORTED;
81 		if (maxKeySize % 64 != 0)
82 			return TEE_ERROR_NOT_SUPPORTED;
83 		break;
84 
85 	case TEE_ALG_DSA_SHA224:
86 		if (maxKeySize != 2048)
87 			return TEE_ERROR_NOT_SUPPORTED;
88 		break;
89 
90 	case TEE_ALG_DSA_SHA256:
91 		if (maxKeySize != 2048 && maxKeySize != 3072)
92 			return TEE_ERROR_NOT_SUPPORTED;
93 		break;
94 
95 	case TEE_ALG_ECDSA_P192:
96 	case TEE_ALG_ECDH_P192:
97 		if (maxKeySize != 192)
98 			return TEE_ERROR_NOT_SUPPORTED;
99 		break;
100 
101 	case TEE_ALG_ECDSA_P224:
102 	case TEE_ALG_ECDH_P224:
103 		if (maxKeySize != 224)
104 			return TEE_ERROR_NOT_SUPPORTED;
105 		break;
106 
107 	case TEE_ALG_ECDSA_P256:
108 	case TEE_ALG_ECDH_P256:
109 		if (maxKeySize != 256)
110 			return TEE_ERROR_NOT_SUPPORTED;
111 		break;
112 
113 	case TEE_ALG_ECDSA_P384:
114 	case TEE_ALG_ECDH_P384:
115 		if (maxKeySize != 384)
116 			return TEE_ERROR_NOT_SUPPORTED;
117 		break;
118 
119 	case TEE_ALG_ECDSA_P521:
120 	case TEE_ALG_ECDH_P521:
121 		if (maxKeySize != 521)
122 			return TEE_ERROR_NOT_SUPPORTED;
123 		break;
124 
125 	default:
126 		break;
127 	}
128 
129 	/* Check algorithm mode */
130 	switch (algorithm) {
131 	case TEE_ALG_AES_CTS:
132 	case TEE_ALG_AES_XTS:
133 		buffer_two_blocks = true;
134 		/* FALLTHROUGH */
135 	case TEE_ALG_AES_ECB_NOPAD:
136 	case TEE_ALG_AES_CBC_NOPAD:
137 	case TEE_ALG_AES_CTR:
138 	case TEE_ALG_AES_CCM:
139 	case TEE_ALG_AES_GCM:
140 	case TEE_ALG_DES_ECB_NOPAD:
141 	case TEE_ALG_DES_CBC_NOPAD:
142 	case TEE_ALG_DES3_ECB_NOPAD:
143 	case TEE_ALG_DES3_CBC_NOPAD:
144 		if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES)
145 			block_size = TEE_AES_BLOCK_SIZE;
146 		else
147 			block_size = TEE_DES_BLOCK_SIZE;
148 
149 		if (mode == TEE_MODE_ENCRYPT)
150 			req_key_usage = TEE_USAGE_ENCRYPT;
151 		else if (mode == TEE_MODE_DECRYPT)
152 			req_key_usage = TEE_USAGE_DECRYPT;
153 		else
154 			return TEE_ERROR_NOT_SUPPORTED;
155 		break;
156 
157 	case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
158 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
159 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
160 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
161 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
162 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
163 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
164 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
165 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
166 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
167 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
168 	case TEE_ALG_DSA_SHA1:
169 	case TEE_ALG_DSA_SHA224:
170 	case TEE_ALG_DSA_SHA256:
171 	case TEE_ALG_ECDSA_P192:
172 	case TEE_ALG_ECDSA_P224:
173 	case TEE_ALG_ECDSA_P256:
174 	case TEE_ALG_ECDSA_P384:
175 	case TEE_ALG_ECDSA_P521:
176 		if (mode == TEE_MODE_SIGN) {
177 			with_private_key = true;
178 			req_key_usage = TEE_USAGE_SIGN;
179 		} else if (mode == TEE_MODE_VERIFY) {
180 			req_key_usage = TEE_USAGE_VERIFY;
181 		} else {
182 			return TEE_ERROR_NOT_SUPPORTED;
183 		}
184 		break;
185 
186 	case TEE_ALG_RSAES_PKCS1_V1_5:
187 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
188 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
189 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
190 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
191 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
192 		if (mode == TEE_MODE_ENCRYPT) {
193 			req_key_usage = TEE_USAGE_ENCRYPT;
194 		} else if (mode == TEE_MODE_DECRYPT) {
195 			with_private_key = true;
196 			req_key_usage = TEE_USAGE_DECRYPT;
197 		} else {
198 			return TEE_ERROR_NOT_SUPPORTED;
199 		}
200 		break;
201 
202 	case TEE_ALG_RSA_NOPAD:
203 		if (mode == TEE_MODE_ENCRYPT) {
204 			req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY;
205 		} else if (mode == TEE_MODE_DECRYPT) {
206 			with_private_key = true;
207 			req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN;
208 		} else {
209 			return TEE_ERROR_NOT_SUPPORTED;
210 		}
211 		break;
212 
213 	case TEE_ALG_DH_DERIVE_SHARED_SECRET:
214 	case TEE_ALG_ECDH_P192:
215 	case TEE_ALG_ECDH_P224:
216 	case TEE_ALG_ECDH_P256:
217 	case TEE_ALG_ECDH_P384:
218 	case TEE_ALG_ECDH_P521:
219 	case TEE_ALG_HKDF_MD5_DERIVE_KEY:
220 	case TEE_ALG_HKDF_SHA1_DERIVE_KEY:
221 	case TEE_ALG_HKDF_SHA224_DERIVE_KEY:
222 	case TEE_ALG_HKDF_SHA256_DERIVE_KEY:
223 	case TEE_ALG_HKDF_SHA384_DERIVE_KEY:
224 	case TEE_ALG_HKDF_SHA512_DERIVE_KEY:
225 	case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY:
226 	case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY:
227 	case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY:
228 	case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY:
229 	case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY:
230 	case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY:
231 		if (mode != TEE_MODE_DERIVE)
232 			return TEE_ERROR_NOT_SUPPORTED;
233 		with_private_key = true;
234 		req_key_usage = TEE_USAGE_DERIVE;
235 		break;
236 
237 	case TEE_ALG_MD5:
238 	case TEE_ALG_SHA1:
239 	case TEE_ALG_SHA224:
240 	case TEE_ALG_SHA256:
241 	case TEE_ALG_SHA384:
242 	case TEE_ALG_SHA512:
243 		if (mode != TEE_MODE_DIGEST)
244 			return TEE_ERROR_NOT_SUPPORTED;
245 		/* v1.1: flags always set for digest operations */
246 		handle_state |= TEE_HANDLE_FLAG_KEY_SET;
247 		req_key_usage = 0;
248 		break;
249 
250 	case TEE_ALG_DES_CBC_MAC_NOPAD:
251 	case TEE_ALG_AES_CBC_MAC_NOPAD:
252 	case TEE_ALG_AES_CBC_MAC_PKCS5:
253 	case TEE_ALG_AES_CMAC:
254 	case TEE_ALG_DES_CBC_MAC_PKCS5:
255 	case TEE_ALG_DES3_CBC_MAC_NOPAD:
256 	case TEE_ALG_DES3_CBC_MAC_PKCS5:
257 	case TEE_ALG_HMAC_MD5:
258 	case TEE_ALG_HMAC_SHA1:
259 	case TEE_ALG_HMAC_SHA224:
260 	case TEE_ALG_HMAC_SHA256:
261 	case TEE_ALG_HMAC_SHA384:
262 	case TEE_ALG_HMAC_SHA512:
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 	op->info.mode = mode;
279 	op->info.maxKeySize = maxKeySize;
280 	op->info.requiredKeyUsage = req_key_usage;
281 	op->info.handleState = handle_state;
282 
283 	if (block_size > 1) {
284 		size_t buffer_size = block_size;
285 
286 		if (buffer_two_blocks)
287 			buffer_size *= 2;
288 
289 		op->buffer = TEE_Malloc(buffer_size,
290 					TEE_USER_MEM_HINT_NO_FILL_ZERO);
291 		if (op->buffer == NULL) {
292 			res = TEE_ERROR_OUT_OF_MEMORY;
293 			goto out;
294 		}
295 	}
296 	op->block_size = block_size;
297 	op->buffer_two_blocks = buffer_two_blocks;
298 
299 	if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) {
300 		uint32_t mks = maxKeySize;
301 		TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm,
302 						       with_private_key);
303 
304 		/*
305 		 * If two keys are expected the max key size is the sum of
306 		 * the size of both keys.
307 		 */
308 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS)
309 			mks /= 2;
310 
311 		res = TEE_AllocateTransientObject(key_type, mks, &op->key1);
312 		if (res != TEE_SUCCESS)
313 			goto out;
314 
315 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) {
316 			res = TEE_AllocateTransientObject(key_type, mks,
317 							  &op->key2);
318 			if (res != TEE_SUCCESS)
319 				goto out;
320 		}
321 	}
322 
323 	res = utee_cryp_state_alloc(algorithm, mode, (unsigned long)op->key1,
324 				    (unsigned long)op->key2, &op->state);
325 	if (res != TEE_SUCCESS)
326 		goto out;
327 
328 	/*
329 	 * Initialize digest operations
330 	 * Other multi-stage operations initialized w/ TEE_xxxInit functions
331 	 * Non-applicable on asymmetric operations
332 	 */
333 	if (TEE_ALG_GET_CLASS(algorithm) == TEE_OPERATION_DIGEST) {
334 		res = utee_hash_init(op->state, NULL, 0);
335 		if (res != TEE_SUCCESS)
336 			goto out;
337 		/* v1.1: flags always set for digest operations */
338 		op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
339 	}
340 
341 	op->operationState = TEE_OPERATION_STATE_INITIAL;
342 
343 	*operation = op;
344 
345 out:
346 	if (res != TEE_SUCCESS) {
347 		if (res != TEE_ERROR_OUT_OF_MEMORY &&
348 		    res != TEE_ERROR_NOT_SUPPORTED)
349 			TEE_Panic(res);
350 		if (op) {
351 			if (op->state) {
352 				TEE_FreeOperation(op);
353 			} else {
354 				TEE_Free(op->buffer);
355 				TEE_FreeTransientObject(op->key1);
356 				TEE_FreeTransientObject(op->key2);
357 				TEE_Free(op);
358 			}
359 		}
360 	}
361 
362 	return res;
363 }
364 
365 void TEE_FreeOperation(TEE_OperationHandle operation)
366 {
367 	TEE_Result res;
368 
369 	if (operation == TEE_HANDLE_NULL)
370 		TEE_Panic(0);
371 
372 	/*
373 	 * Note that keys should not be freed here, since they are
374 	 * claimed by the operation they will be freed by
375 	 * utee_cryp_state_free().
376 	 */
377 	res = utee_cryp_state_free(operation->state);
378 	if (res != TEE_SUCCESS)
379 		TEE_Panic(res);
380 
381 	TEE_Free(operation->buffer);
382 	TEE_Free(operation);
383 }
384 
385 void TEE_GetOperationInfo(TEE_OperationHandle operation,
386 			  TEE_OperationInfo *operationInfo)
387 {
388 	if (operation == TEE_HANDLE_NULL)
389 		TEE_Panic(0);
390 
391 	if (!operationInfo)
392 		TEE_Panic(0);
393 
394 	*operationInfo = operation->info;
395 }
396 
397 TEE_Result TEE_GetOperationInfoMultiple(TEE_OperationHandle operation,
398 			  TEE_OperationInfoMultiple *operationInfoMultiple,
399 			  uint32_t *operationSize)
400 {
401 	TEE_Result res = TEE_SUCCESS;
402 	TEE_ObjectInfo key_info1;
403 	TEE_ObjectInfo key_info2;
404 	uint32_t num_of_keys;
405 	size_t n;
406 
407 	if (operation == TEE_HANDLE_NULL) {
408 		res = TEE_ERROR_BAD_PARAMETERS;
409 		goto out;
410 	}
411 
412 	if (!operationInfoMultiple) {
413 		res = TEE_ERROR_BAD_PARAMETERS;
414 		goto out;
415 	}
416 
417 	if (!operationSize) {
418 		res = TEE_ERROR_BAD_PARAMETERS;
419 		goto out;
420 	}
421 
422 	num_of_keys = (*operationSize-sizeof(TEE_OperationInfoMultiple))/
423 			sizeof(TEE_OperationInfoKey);
424 
425 	if (num_of_keys > 2) {
426 		res = TEE_ERROR_BAD_PARAMETERS;
427 		goto out;
428 	}
429 
430 	/* Two keys flag (TEE_ALG_AES_XTS only) */
431 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
432 	    0 &&
433 	    (num_of_keys != 2)) {
434 		res = TEE_ERROR_SHORT_BUFFER;
435 		goto out;
436 	}
437 
438 	/* Clear */
439 	for (n = 0; n < num_of_keys; n++) {
440 		operationInfoMultiple->keyInformation[n].keySize = 0;
441 		operationInfoMultiple->keyInformation[n].requiredKeyUsage = 0;
442 	}
443 
444 	if (num_of_keys == 2) {
445 		res = TEE_GetObjectInfo1(operation->key2, &key_info2);
446 		/* Key2 is not a valid handle */
447 		if (res != TEE_SUCCESS)
448 			goto out;
449 
450 		operationInfoMultiple->keyInformation[1].keySize =
451 			key_info2.keySize;
452 		operationInfoMultiple->keyInformation[1].requiredKeyUsage =
453 			operation->info.requiredKeyUsage;
454 	}
455 
456 	if (num_of_keys >= 1) {
457 		res = TEE_GetObjectInfo1(operation->key1, &key_info1);
458 		/* Key1 is not a valid handle */
459 		if (res != TEE_SUCCESS) {
460 			if (num_of_keys == 2) {
461 				operationInfoMultiple->keyInformation[1].
462 							keySize = 0;
463 				operationInfoMultiple->keyInformation[1].
464 							requiredKeyUsage = 0;
465 			}
466 			goto out;
467 		}
468 
469 		operationInfoMultiple->keyInformation[0].keySize =
470 			key_info1.keySize;
471 		operationInfoMultiple->keyInformation[0].requiredKeyUsage =
472 			operation->info.requiredKeyUsage;
473 	}
474 
475 	/* No key */
476 	operationInfoMultiple->algorithm = operation->info.algorithm;
477 	operationInfoMultiple->operationClass = operation->info.operationClass;
478 	operationInfoMultiple->mode = operation->info.mode;
479 	operationInfoMultiple->digestLength = operation->info.digestLength;
480 	operationInfoMultiple->maxKeySize = operation->info.maxKeySize;
481 	operationInfoMultiple->handleState = operation->info.handleState;
482 	operationInfoMultiple->operationState = operation->operationState;
483 	operationInfoMultiple->numberOfKeys = num_of_keys;
484 
485 out:
486 	if (res != TEE_SUCCESS &&
487 	    res != TEE_ERROR_SHORT_BUFFER)
488 		TEE_Panic(res);
489 
490 	return res;
491 }
492 
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 
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 		res = TEE_ERROR_BAD_PARAMETERS;
536 		goto out;
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 
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 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) {
614 		/* Clear operation key1 (if needed) */
615 		if (key1 == TEE_HANDLE_NULL)
616 			TEE_ResetTransientObject(operation->key1);
617 		/* Clear operation key2 (if needed) */
618 		if (key2 == TEE_HANDLE_NULL)
619 			TEE_ResetTransientObject(operation->key2);
620 		res = TEE_ERROR_BAD_PARAMETERS;
621 		goto out;
622 	}
623 
624 	/* No key for digest operation */
625 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
626 		res = TEE_ERROR_BAD_PARAMETERS;
627 		goto out;
628 	}
629 
630 	/* Two keys flag expected (TEE_ALG_AES_XTS only) */
631 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) ==
632 	    0) {
633 		res = TEE_ERROR_BAD_PARAMETERS;
634 		goto out;
635 	}
636 
637 	res = TEE_GetObjectInfo1(key1, &key_info1);
638 	/* Key1 is not a valid handle */
639 	if (res != TEE_SUCCESS)
640 		goto out;
641 
642 	/* Supplied key has to meet required usage */
643 	if ((key_info1.objectUsage & operation->info.
644 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
645 		res = TEE_ERROR_BAD_PARAMETERS;
646 		goto out;
647 	}
648 
649 	res = TEE_GetObjectInfo1(key2, &key_info2);
650 	/* Key2 is not a valid handle */
651 	if (res != TEE_SUCCESS) {
652 		if (res == TEE_ERROR_CORRUPT_OBJECT)
653 			res = TEE_ERROR_CORRUPT_OBJECT_2;
654 		goto out;
655 	}
656 
657 	/* Supplied key has to meet required usage */
658 	if ((key_info2.objectUsage & operation->info.
659 	     requiredKeyUsage) != operation->info.requiredKeyUsage) {
660 		res = TEE_ERROR_BAD_PARAMETERS;
661 		goto out;
662 	}
663 
664 	/*
665 	 * AES-XTS (the only multi key algorithm supported, requires the
666 	 * keys to be of equal size.
667 	 */
668 	if (operation->info.algorithm == TEE_ALG_AES_XTS &&
669 	    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 
694 	res = TEE_CopyObjectAttributes1(operation->key2, key2);
695 	if (res != TEE_SUCCESS) {
696 		if (res == TEE_ERROR_CORRUPT_OBJECT)
697 			res = TEE_ERROR_CORRUPT_OBJECT_2;
698 		goto out;
699 	}
700 
701 	operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
702 
703 	operation->info.keySize = key_size;
704 
705 out:
706 	if (res != TEE_SUCCESS  &&
707 	    res != TEE_ERROR_CORRUPT_OBJECT &&
708 	    res != TEE_ERROR_CORRUPT_OBJECT_2 &&
709 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE &&
710 	    res != TEE_ERROR_STORAGE_NOT_AVAILABLE_2)
711 		TEE_Panic(res);
712 
713 	return res;
714 }
715 
716 void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op)
717 {
718 	TEE_Result res;
719 
720 	if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL)
721 		TEE_Panic(0);
722 	if (dst_op->info.algorithm != src_op->info.algorithm)
723 		TEE_Panic(0);
724 	if (src_op->info.operationClass != TEE_OPERATION_DIGEST) {
725 		TEE_ObjectHandle key1 = TEE_HANDLE_NULL;
726 		TEE_ObjectHandle key2 = TEE_HANDLE_NULL;
727 
728 		if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) {
729 			key1 = src_op->key1;
730 			key2 = src_op->key2;
731 		}
732 
733 		if ((src_op->info.handleState &
734 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) {
735 			TEE_SetOperationKey(dst_op, key1);
736 		} else {
737 			TEE_SetOperationKey2(dst_op, key1, key2);
738 		}
739 	}
740 	dst_op->info.handleState = src_op->info.handleState;
741 	dst_op->info.keySize = src_op->info.keySize;
742 	dst_op->operationState = src_op->operationState;
743 
744 	if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks ||
745 	    dst_op->block_size != src_op->block_size)
746 		TEE_Panic(0);
747 
748 	if (dst_op->buffer != NULL) {
749 		if (src_op->buffer == NULL)
750 			TEE_Panic(0);
751 
752 		memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs);
753 		dst_op->buffer_offs = src_op->buffer_offs;
754 	} else if (src_op->buffer != NULL) {
755 		TEE_Panic(0);
756 	}
757 
758 	res = utee_cryp_state_copy(dst_op->state, src_op->state);
759 	if (res != TEE_SUCCESS)
760 		TEE_Panic(res);
761 }
762 
763 /* Cryptographic Operations API - Message Digest Functions */
764 
765 static void init_hash_operation(TEE_OperationHandle operation, void *IV,
766 				uint32_t IVLen)
767 {
768 	TEE_Result res;
769 
770 	/*
771 	 * Note : IV and IVLen are never used in current implementation
772 	 * This is why coherent values of IV and IVLen are not checked
773 	 */
774 	res = utee_hash_init(operation->state, IV, IVLen);
775 	if (res != TEE_SUCCESS)
776 		TEE_Panic(res);
777 	operation->buffer_offs = 0;
778 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
779 }
780 
781 void TEE_DigestUpdate(TEE_OperationHandle operation,
782 		      void *chunk, uint32_t chunkSize)
783 {
784 	TEE_Result res = TEE_ERROR_GENERIC;
785 
786 	if (operation == TEE_HANDLE_NULL ||
787 	    operation->info.operationClass != TEE_OPERATION_DIGEST)
788 		TEE_Panic(0);
789 
790 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
791 
792 	res = utee_hash_update(operation->state, chunk, chunkSize);
793 	if (res != TEE_SUCCESS)
794 		TEE_Panic(res);
795 }
796 
797 TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, void *chunk,
798 			     uint32_t chunkLen, void *hash, uint32_t *hashLen)
799 {
800 	TEE_Result res;
801 	uint64_t hl;
802 
803 	if ((operation == TEE_HANDLE_NULL) ||
804 	    (!chunk && chunkLen) ||
805 	    !hash ||
806 	    !hashLen ||
807 	    (operation->info.operationClass != TEE_OPERATION_DIGEST)) {
808 		res = TEE_ERROR_BAD_PARAMETERS;
809 		goto out;
810 	}
811 
812 	hl = *hashLen;
813 	res = utee_hash_final(operation->state, chunk, chunkLen, hash, &hl);
814 	*hashLen = hl;
815 	if (res != TEE_SUCCESS)
816 		goto out;
817 
818 	/* Reset operation state */
819 	init_hash_operation(operation, NULL, 0);
820 
821 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
822 
823 out:
824 	if (res != TEE_SUCCESS &&
825 	    res != TEE_ERROR_SHORT_BUFFER)
826 		TEE_Panic(res);
827 
828 	return res;
829 }
830 
831 /* Cryptographic Operations API - Symmetric Cipher Functions */
832 
833 void TEE_CipherInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen)
834 {
835 	TEE_Result res;
836 
837 	if (operation == TEE_HANDLE_NULL)
838 		TEE_Panic(0);
839 
840 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
841 		TEE_Panic(0);
842 
843 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
844 	    !(operation->key1))
845 		TEE_Panic(0);
846 
847 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
848 		TEE_ResetOperation(operation);
849 
850 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
851 
852 	res = utee_cipher_init(operation->state, IV, IVLen);
853 	if (res != TEE_SUCCESS)
854 		TEE_Panic(res);
855 
856 	operation->buffer_offs = 0;
857 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
858 }
859 
860 static TEE_Result tee_buffer_update(
861 		TEE_OperationHandle op,
862 		TEE_Result(*update_func)(unsigned long state, const void *src,
863 				size_t slen, void *dst, uint64_t *dlen),
864 		const void *src_data, size_t src_len,
865 		void *dest_data, uint64_t *dest_len)
866 {
867 	TEE_Result res;
868 	const uint8_t *src = src_data;
869 	size_t slen = src_len;
870 	uint8_t *dst = dest_data;
871 	size_t dlen = *dest_len;
872 	size_t acc_dlen = 0;
873 	uint64_t tmp_dlen;
874 	size_t l;
875 	size_t buffer_size;
876 	size_t buffer_left;
877 
878 	if (!src) {
879 		if (slen)
880 			TEE_Panic(0);
881 		goto out;
882 	}
883 
884 	if (op->buffer_two_blocks) {
885 		buffer_size = op->block_size * 2;
886 		buffer_left = 1;
887 	} else {
888 		buffer_size = op->block_size;
889 		buffer_left = 0;
890 	}
891 
892 	if (op->buffer_offs > 0) {
893 		/* Fill up complete block */
894 		if (op->buffer_offs < op->block_size)
895 			l = MIN(slen, op->block_size - op->buffer_offs);
896 		else
897 			l = MIN(slen, buffer_size - op->buffer_offs);
898 		memcpy(op->buffer + op->buffer_offs, src, l);
899 		op->buffer_offs += l;
900 		src += l;
901 		slen -= l;
902 		if ((op->buffer_offs % op->block_size) != 0)
903 			goto out;	/* Nothing left to do */
904 	}
905 
906 	/* If we can feed from buffer */
907 	if ((op->buffer_offs > 0) &&
908 	    ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) {
909 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
910 				op->block_size);
911 		l = MIN(op->buffer_offs, l);
912 		tmp_dlen = dlen;
913 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
914 		if (res != TEE_SUCCESS)
915 			TEE_Panic(res);
916 		dst += tmp_dlen;
917 		dlen -= tmp_dlen;
918 		acc_dlen += tmp_dlen;
919 		op->buffer_offs -= l;
920 		if (op->buffer_offs > 0) {
921 			/*
922 			 * Slen is small enough to be contained in rest buffer.
923 			 */
924 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
925 			memcpy(op->buffer + op->buffer_offs, src, slen);
926 			op->buffer_offs += slen;
927 			goto out;	/* Nothing left to do */
928 		}
929 	}
930 
931 	if (slen >= (buffer_size + buffer_left)) {
932 		/* Buffer is empty, feed as much as possible from src */
933 		if (op->info.algorithm == TEE_ALG_AES_CTS)
934 			l = ROUNDUP(slen - buffer_size, op->block_size);
935 		else
936 			l = ROUNDUP(slen - buffer_size + 1, op->block_size);
937 
938 		tmp_dlen = dlen;
939 		res = update_func(op->state, src, l, dst, &tmp_dlen);
940 		if (res != TEE_SUCCESS)
941 			TEE_Panic(res);
942 		src += l;
943 		slen -= l;
944 		dst += tmp_dlen;
945 		dlen -= tmp_dlen;
946 		acc_dlen += tmp_dlen;
947 	}
948 
949 	/* Slen is small enough to be contained in buffer. */
950 	memcpy(op->buffer + op->buffer_offs, src, slen);
951 	op->buffer_offs += slen;
952 
953 out:
954 	*dest_len = acc_dlen;
955 	return TEE_SUCCESS;
956 }
957 
958 TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, void *srcData,
959 			    uint32_t srcLen, void *destData, uint32_t *destLen)
960 {
961 	TEE_Result res;
962 	size_t req_dlen;
963 	uint64_t dl;
964 
965 	if (operation == TEE_HANDLE_NULL ||
966 	    (srcData == NULL && srcLen != 0) ||
967 	    destLen == NULL ||
968 	    (destData == NULL && *destLen != 0)) {
969 		res = TEE_ERROR_BAD_PARAMETERS;
970 		goto out;
971 	}
972 
973 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
974 		res = TEE_ERROR_BAD_PARAMETERS;
975 		goto out;
976 	}
977 
978 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
979 		res = TEE_ERROR_BAD_PARAMETERS;
980 		goto out;
981 	}
982 
983 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
984 		res = TEE_ERROR_BAD_PARAMETERS;
985 		goto out;
986 	}
987 
988 	if (!srcData && !srcLen) {
989 		*destLen = 0;
990 		res = TEE_SUCCESS;
991 		goto out;
992 	}
993 
994 	/* Calculate required dlen */
995 	req_dlen = ((operation->buffer_offs + srcLen) / operation->block_size) *
996 	    operation->block_size;
997 	if (operation->buffer_two_blocks) {
998 		if (req_dlen > operation->block_size * 2)
999 			req_dlen -= operation->block_size * 2;
1000 		else
1001 			req_dlen = 0;
1002 	}
1003 	/*
1004 	 * Check that required destLen is big enough before starting to feed
1005 	 * data to the algorithm. Errors during feeding of data are fatal as we
1006 	 * can't restore sync with this API.
1007 	 */
1008 	if (*destLen < req_dlen) {
1009 		*destLen = req_dlen;
1010 		res = TEE_ERROR_SHORT_BUFFER;
1011 		goto out;
1012 	}
1013 
1014 	dl = *destLen;
1015 	res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen,
1016 				destData, &dl);
1017 	*destLen = dl;
1018 
1019 out:
1020 	if (res != TEE_SUCCESS &&
1021 	    res != TEE_ERROR_SHORT_BUFFER)
1022 		TEE_Panic(res);
1023 
1024 	return res;
1025 }
1026 
1027 TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation,
1028 			     void *srcData, uint32_t srcLen, void *destData,
1029 			     uint32_t *destLen)
1030 {
1031 	TEE_Result res;
1032 	uint8_t *dst = destData;
1033 	size_t acc_dlen = 0;
1034 	uint64_t tmp_dlen;
1035 	size_t req_dlen;
1036 
1037 	if (operation == TEE_HANDLE_NULL ||
1038 	    (srcData == NULL && srcLen != 0) ||
1039 	    destLen == NULL ||
1040 	    (destData == NULL && *destLen != 0)) {
1041 		res = TEE_ERROR_BAD_PARAMETERS;
1042 		goto out;
1043 	}
1044 
1045 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
1046 		res = TEE_ERROR_BAD_PARAMETERS;
1047 		goto out;
1048 	}
1049 
1050 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1051 		res = TEE_ERROR_BAD_PARAMETERS;
1052 		goto out;
1053 	}
1054 
1055 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1056 		res = TEE_ERROR_BAD_PARAMETERS;
1057 		goto out;
1058 	}
1059 
1060 	/*
1061 	 * Check that the final block doesn't require padding for those
1062 	 * algorithms that requires client to supply padding.
1063 	 */
1064 	if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
1065 	    operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
1066 	    operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
1067 	    operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
1068 	    operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
1069 	    operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) {
1070 		if (((operation->buffer_offs + srcLen) % operation->block_size)
1071 		    != 0) {
1072 			res = TEE_ERROR_BAD_PARAMETERS;
1073 			goto out;
1074 		}
1075 	}
1076 
1077 	/*
1078 	 * Check that required destLen is big enough before starting to feed
1079 	 * data to the algorithm. Errors during feeding of data are fatal as we
1080 	 * can't restore sync with this API.
1081 	 */
1082 	req_dlen = operation->buffer_offs + srcLen;
1083 	if (*destLen < req_dlen) {
1084 		*destLen = req_dlen;
1085 		res = TEE_ERROR_SHORT_BUFFER;
1086 		goto out;
1087 	}
1088 
1089 	tmp_dlen = *destLen - acc_dlen;
1090 	res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen,
1091 				dst, &tmp_dlen);
1092 	if (res != TEE_SUCCESS)
1093 		goto out;
1094 
1095 	dst += tmp_dlen;
1096 	acc_dlen += tmp_dlen;
1097 
1098 	tmp_dlen = *destLen - acc_dlen;
1099 	res = utee_cipher_final(operation->state, operation->buffer,
1100 				operation->buffer_offs, dst, &tmp_dlen);
1101 	if (res != TEE_SUCCESS)
1102 		goto out;
1103 
1104 	acc_dlen += tmp_dlen;
1105 	*destLen = acc_dlen;
1106 
1107 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1108 
1109 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1110 
1111 out:
1112 	if (res != TEE_SUCCESS &&
1113 	    res != TEE_ERROR_SHORT_BUFFER)
1114 		TEE_Panic(res);
1115 
1116 	return res;
1117 }
1118 
1119 /* Cryptographic Operations API - MAC Functions */
1120 
1121 void TEE_MACInit(TEE_OperationHandle operation, void *IV, uint32_t IVLen)
1122 {
1123 	if (operation == TEE_HANDLE_NULL)
1124 		TEE_Panic(0);
1125 
1126 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1127 		TEE_Panic(0);
1128 
1129 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
1130 	    !(operation->key1))
1131 		TEE_Panic(0);
1132 
1133 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
1134 		TEE_ResetOperation(operation);
1135 
1136 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1137 
1138 	init_hash_operation(operation, IV, IVLen);
1139 }
1140 
1141 void TEE_MACUpdate(TEE_OperationHandle operation, void *chunk,
1142 		   uint32_t chunkSize)
1143 {
1144 	TEE_Result res;
1145 
1146 	if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
1147 		TEE_Panic(0);
1148 
1149 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1150 		TEE_Panic(0);
1151 
1152 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1153 		TEE_Panic(0);
1154 
1155 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE)
1156 		TEE_Panic(0);
1157 
1158 	res = utee_hash_update(operation->state, chunk, chunkSize);
1159 	if (res != TEE_SUCCESS)
1160 		TEE_Panic(res);
1161 }
1162 
1163 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation,
1164 			       void *message, uint32_t messageLen,
1165 			       void *mac, uint32_t *macLen)
1166 {
1167 	TEE_Result res;
1168 	uint64_t ml;
1169 
1170 	if (operation == TEE_HANDLE_NULL ||
1171 	    (message == NULL && messageLen != 0) ||
1172 	    mac == NULL ||
1173 	    macLen == NULL) {
1174 		res = TEE_ERROR_BAD_PARAMETERS;
1175 		goto out;
1176 	}
1177 
1178 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
1179 		res = TEE_ERROR_BAD_PARAMETERS;
1180 		goto out;
1181 	}
1182 
1183 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1184 		res = TEE_ERROR_BAD_PARAMETERS;
1185 		goto out;
1186 	}
1187 
1188 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1189 		res = TEE_ERROR_BAD_PARAMETERS;
1190 		goto out;
1191 	}
1192 
1193 	ml = *macLen;
1194 	res = utee_hash_final(operation->state, message, messageLen, mac, &ml);
1195 	*macLen = ml;
1196 	if (res != TEE_SUCCESS)
1197 		goto out;
1198 
1199 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1200 
1201 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1202 
1203 out:
1204 	if (res != TEE_SUCCESS &&
1205 	    res != TEE_ERROR_SHORT_BUFFER)
1206 		TEE_Panic(res);
1207 
1208 	return res;
1209 }
1210 
1211 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
1212 			       void *message, uint32_t messageLen,
1213 			       void *mac, uint32_t macLen)
1214 {
1215 	TEE_Result res;
1216 	uint8_t computed_mac[TEE_MAX_HASH_SIZE];
1217 	uint32_t computed_mac_size = TEE_MAX_HASH_SIZE;
1218 
1219 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
1220 		res = TEE_ERROR_BAD_PARAMETERS;
1221 		goto out;
1222 	}
1223 
1224 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1225 		res = TEE_ERROR_BAD_PARAMETERS;
1226 		goto out;
1227 	}
1228 
1229 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1230 		res = TEE_ERROR_BAD_PARAMETERS;
1231 		goto out;
1232 	}
1233 
1234 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
1235 				  &computed_mac_size);
1236 	if (res != TEE_SUCCESS)
1237 		goto out;
1238 
1239 	if (computed_mac_size != macLen) {
1240 		res = TEE_ERROR_MAC_INVALID;
1241 		goto out;
1242 	}
1243 
1244 	if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) {
1245 		res = TEE_ERROR_MAC_INVALID;
1246 		goto out;
1247 	}
1248 
1249 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1250 
1251 out:
1252 	if (res != TEE_SUCCESS &&
1253 	    res != TEE_ERROR_MAC_INVALID)
1254 		TEE_Panic(res);
1255 
1256 	return res;
1257 }
1258 
1259 /* Cryptographic Operations API - Authenticated Encryption Functions */
1260 
1261 TEE_Result TEE_AEInit(TEE_OperationHandle operation, void *nonce,
1262 		      uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen,
1263 		      uint32_t payloadLen)
1264 {
1265 	TEE_Result res;
1266 
1267 	if (operation == TEE_HANDLE_NULL || nonce == NULL) {
1268 		res = TEE_ERROR_BAD_PARAMETERS;
1269 		goto out;
1270 	}
1271 
1272 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1273 		res = TEE_ERROR_BAD_PARAMETERS;
1274 		goto out;
1275 	}
1276 
1277 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
1278 		res = TEE_ERROR_BAD_PARAMETERS;
1279 		goto out;
1280 	}
1281 
1282 	/*
1283 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
1284 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
1285 	 * according to the same principle so we have to check here instead to
1286 	 * be GP compliant.
1287 	 */
1288 	if (operation->info.algorithm == TEE_ALG_AES_GCM) {
1289 		/*
1290 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
1291 		 */
1292 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) {
1293 			res = TEE_ERROR_NOT_SUPPORTED;
1294 			goto out;
1295 		}
1296 	}
1297 
1298 	res = utee_authenc_init(operation->state, nonce, nonceLen,
1299 				tagLen / 8, AADLen, payloadLen);
1300 	if (res != TEE_SUCCESS)
1301 		goto out;
1302 
1303 	operation->ae_tag_len = tagLen / 8;
1304 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1305 
1306 out:
1307 	if (res != TEE_SUCCESS &&
1308 	    res != TEE_ERROR_NOT_SUPPORTED)
1309 			TEE_Panic(res);
1310 
1311 	return res;
1312 }
1313 
1314 void TEE_AEUpdateAAD(TEE_OperationHandle operation, void *AADdata,
1315 		     uint32_t AADdataLen)
1316 {
1317 	TEE_Result res;
1318 
1319 	if (operation == TEE_HANDLE_NULL ||
1320 	    (AADdata == NULL && AADdataLen != 0))
1321 		TEE_Panic(0);
1322 
1323 	if (operation->info.operationClass != TEE_OPERATION_AE)
1324 		TEE_Panic(0);
1325 
1326 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1327 		TEE_Panic(0);
1328 
1329 	res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen);
1330 
1331 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1332 
1333 	if (res != TEE_SUCCESS)
1334 		TEE_Panic(res);
1335 }
1336 
1337 TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, void *srcData,
1338 			uint32_t srcLen, void *destData, uint32_t *destLen)
1339 {
1340 	TEE_Result res;
1341 	size_t req_dlen;
1342 	uint64_t dl;
1343 
1344 	if (operation == TEE_HANDLE_NULL ||
1345 	    (srcData == NULL && srcLen != 0) ||
1346 	    destLen == NULL ||
1347 	    (destData == NULL && *destLen != 0)) {
1348 		res = TEE_ERROR_BAD_PARAMETERS;
1349 		goto out;
1350 	}
1351 
1352 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1353 		res = TEE_ERROR_BAD_PARAMETERS;
1354 		goto out;
1355 	}
1356 
1357 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1358 		res = TEE_ERROR_BAD_PARAMETERS;
1359 		goto out;
1360 	}
1361 
1362 	if (!srcData && !srcLen) {
1363 		*destLen = 0;
1364 		res = TEE_SUCCESS;
1365 		goto out;
1366 	}
1367 
1368 	/*
1369 	 * Check that required destLen is big enough before starting to feed
1370 	 * data to the algorithm. Errors during feeding of data are fatal as we
1371 	 * can't restore sync with this API.
1372 	 */
1373 	req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen,
1374 			     operation->block_size);
1375 	if (*destLen < req_dlen) {
1376 		*destLen = req_dlen;
1377 		res = TEE_ERROR_SHORT_BUFFER;
1378 		goto out;
1379 	}
1380 
1381 	dl = *destLen;
1382 	res = tee_buffer_update(operation, utee_authenc_update_payload, srcData,
1383 				srcLen, destData, &dl);
1384 	*destLen = dl;
1385 
1386 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1387 
1388 out:
1389 	if (res != TEE_SUCCESS &&
1390 	    res != TEE_ERROR_SHORT_BUFFER)
1391 			TEE_Panic(res);
1392 
1393 	return res;
1394 }
1395 
1396 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation,
1397 			      void *srcData, uint32_t srcLen,
1398 			      void *destData, uint32_t *destLen, void *tag,
1399 			      uint32_t *tagLen)
1400 {
1401 	TEE_Result res;
1402 	uint8_t *dst = destData;
1403 	size_t acc_dlen = 0;
1404 	uint64_t tmp_dlen;
1405 	size_t req_dlen;
1406 	uint64_t tl;
1407 
1408 	if (operation == TEE_HANDLE_NULL ||
1409 	    (srcData == NULL && srcLen != 0) ||
1410 	    destLen == NULL ||
1411 	    (destData == NULL && *destLen != 0) ||
1412 	    tag == NULL || tagLen == NULL) {
1413 		res = TEE_ERROR_BAD_PARAMETERS;
1414 		goto out;
1415 	}
1416 
1417 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1418 		res = TEE_ERROR_BAD_PARAMETERS;
1419 		goto out;
1420 	}
1421 
1422 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1423 		res = TEE_ERROR_BAD_PARAMETERS;
1424 		goto out;
1425 	}
1426 
1427 	/*
1428 	 * Check that required destLen is big enough before starting to feed
1429 	 * data to the algorithm. Errors during feeding of data are fatal as we
1430 	 * can't restore sync with this API.
1431 	 */
1432 	req_dlen = operation->buffer_offs + srcLen;
1433 	if (*destLen < req_dlen) {
1434 		*destLen = req_dlen;
1435 		res = TEE_ERROR_SHORT_BUFFER;
1436 		goto out;
1437 	}
1438 
1439 	/*
1440 	 * Need to check this before update_payload since sync would be lost if
1441 	 * we return short buffer after that.
1442 	 */
1443 	if (*tagLen < operation->ae_tag_len) {
1444 		*tagLen = operation->ae_tag_len;
1445 		res = TEE_ERROR_SHORT_BUFFER;
1446 		goto out;
1447 	}
1448 
1449 	tmp_dlen = *destLen - acc_dlen;
1450 	res = tee_buffer_update(operation, utee_authenc_update_payload, srcData,
1451 				srcLen, dst, &tmp_dlen);
1452 	if (res != TEE_SUCCESS)
1453 		goto out;
1454 
1455 	dst += tmp_dlen;
1456 	acc_dlen += tmp_dlen;
1457 
1458 	tmp_dlen = *destLen - acc_dlen;
1459 	tl = *tagLen;
1460 	res = utee_authenc_enc_final(operation->state, operation->buffer,
1461 				     operation->buffer_offs, dst, &tmp_dlen,
1462 				     tag, &tl);
1463 	*tagLen = tl;
1464 	if (res != TEE_SUCCESS)
1465 		goto out;
1466 
1467 	acc_dlen += tmp_dlen;
1468 	*destLen = acc_dlen;
1469 
1470 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1471 
1472 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1473 
1474 out:
1475 	if (res != TEE_SUCCESS &&
1476 	    res != TEE_ERROR_SHORT_BUFFER)
1477 			TEE_Panic(res);
1478 
1479 	return res;
1480 }
1481 
1482 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation,
1483 			      void *srcData, uint32_t srcLen,
1484 			      void *destData, uint32_t *destLen, void *tag,
1485 			      uint32_t tagLen)
1486 {
1487 	TEE_Result res;
1488 	uint8_t *dst = destData;
1489 	size_t acc_dlen = 0;
1490 	uint64_t tmp_dlen;
1491 	size_t req_dlen;
1492 
1493 	if (operation == TEE_HANDLE_NULL ||
1494 	    (srcData == NULL && srcLen != 0) ||
1495 	    destLen == NULL ||
1496 	    (destData == NULL && *destLen != 0) ||
1497 	    (tag == NULL && tagLen != 0)) {
1498 		res = TEE_ERROR_BAD_PARAMETERS;
1499 		goto out;
1500 	}
1501 
1502 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1503 		res = TEE_ERROR_BAD_PARAMETERS;
1504 		goto out;
1505 	}
1506 
1507 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1508 		res = TEE_ERROR_BAD_PARAMETERS;
1509 		goto out;
1510 	}
1511 
1512 	/*
1513 	 * Check that required destLen is big enough before starting to feed
1514 	 * data to the algorithm. Errors during feeding of data are fatal as we
1515 	 * can't restore sync with this API.
1516 	 */
1517 	req_dlen = operation->buffer_offs + srcLen;
1518 	if (*destLen < req_dlen) {
1519 		*destLen = req_dlen;
1520 		res = TEE_ERROR_SHORT_BUFFER;
1521 		goto out;
1522 	}
1523 
1524 	tmp_dlen = *destLen - acc_dlen;
1525 	res = tee_buffer_update(operation, utee_authenc_update_payload, srcData,
1526 				srcLen, dst, &tmp_dlen);
1527 	if (res != TEE_SUCCESS)
1528 		goto out;
1529 
1530 	dst += tmp_dlen;
1531 	acc_dlen += tmp_dlen;
1532 
1533 	tmp_dlen = *destLen - acc_dlen;
1534 	res = utee_authenc_dec_final(operation->state, operation->buffer,
1535 				     operation->buffer_offs, dst, &tmp_dlen,
1536 				     tag, tagLen);
1537 	if (res != TEE_SUCCESS)
1538 		goto out;
1539 
1540 	/* Supplied tagLen should match what we initiated with */
1541 	if (tagLen != operation->ae_tag_len)
1542 		res = TEE_ERROR_MAC_INVALID;
1543 
1544 	acc_dlen += tmp_dlen;
1545 	*destLen = acc_dlen;
1546 
1547 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1548 
1549 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1550 
1551 out:
1552 	if (res != TEE_SUCCESS &&
1553 	    res != TEE_ERROR_SHORT_BUFFER &&
1554 	    res != TEE_ERROR_MAC_INVALID)
1555 			TEE_Panic(res);
1556 
1557 	return res;
1558 }
1559 
1560 /* Cryptographic Operations API - Asymmetric Functions */
1561 
1562 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation,
1563 				 TEE_Attribute *params,
1564 				 uint32_t paramCount, void *srcData,
1565 				 uint32_t srcLen, void *destData,
1566 				 uint32_t *destLen)
1567 {
1568 	TEE_Result res;
1569 	struct utee_attribute ua[paramCount];
1570 	uint64_t dl;
1571 
1572 	if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1573 	    destLen == NULL || (destData == NULL && *destLen != 0))
1574 		TEE_Panic(0);
1575 	if (params == NULL && paramCount != 0)
1576 		TEE_Panic(0);
1577 	if (!operation->key1)
1578 		TEE_Panic(0);
1579 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1580 		TEE_Panic(0);
1581 	if (operation->info.mode != TEE_MODE_ENCRYPT)
1582 		TEE_Panic(0);
1583 
1584 	__utee_from_attr(ua, params, paramCount);
1585 	dl = *destLen;
1586 	res = utee_asymm_operate(operation->state, ua, paramCount, srcData,
1587 				 srcLen, destData, &dl);
1588 	*destLen = dl;
1589 
1590 	if (res != TEE_SUCCESS &&
1591 	    res != TEE_ERROR_SHORT_BUFFER &&
1592 	    res != TEE_ERROR_BAD_PARAMETERS)
1593 		TEE_Panic(res);
1594 
1595 	return res;
1596 }
1597 
1598 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation,
1599 				 TEE_Attribute *params,
1600 				 uint32_t paramCount, void *srcData,
1601 				 uint32_t srcLen, void *destData,
1602 				 uint32_t *destLen)
1603 {
1604 	TEE_Result res;
1605 	struct utee_attribute ua[paramCount];
1606 	uint64_t dl;
1607 
1608 	if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1609 	    destLen == NULL || (destData == NULL && *destLen != 0))
1610 		TEE_Panic(0);
1611 	if (params == NULL && paramCount != 0)
1612 		TEE_Panic(0);
1613 	if (!operation->key1)
1614 		TEE_Panic(0);
1615 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1616 		TEE_Panic(0);
1617 	if (operation->info.mode != TEE_MODE_DECRYPT)
1618 		TEE_Panic(0);
1619 
1620 	__utee_from_attr(ua, params, paramCount);
1621 	dl = *destLen;
1622 	res = utee_asymm_operate(operation->state, ua, paramCount, srcData,
1623 				 srcLen, destData, &dl);
1624 	*destLen = dl;
1625 
1626 	if (res != TEE_SUCCESS &&
1627 	    res != TEE_ERROR_SHORT_BUFFER &&
1628 	    res != TEE_ERROR_BAD_PARAMETERS)
1629 		TEE_Panic(res);
1630 
1631 	return res;
1632 }
1633 
1634 TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation,
1635 				    TEE_Attribute *params,
1636 				    uint32_t paramCount, void *digest,
1637 				    uint32_t digestLen, void *signature,
1638 				    uint32_t *signatureLen)
1639 {
1640 	TEE_Result res;
1641 	struct utee_attribute ua[paramCount];
1642 	uint64_t sl;
1643 
1644 	if (operation == TEE_HANDLE_NULL ||
1645 	    (digest == NULL && digestLen != 0) ||
1646 	    signature == NULL || signatureLen == NULL)
1647 		TEE_Panic(0);
1648 	if (params == NULL && paramCount != 0)
1649 		TEE_Panic(0);
1650 	if (!operation->key1)
1651 		TEE_Panic(0);
1652 	if (operation->info.operationClass !=
1653 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1654 		TEE_Panic(0);
1655 	if (operation->info.mode != TEE_MODE_SIGN)
1656 		TEE_Panic(0);
1657 
1658 	__utee_from_attr(ua, params, paramCount);
1659 	sl = *signatureLen;
1660 	res = utee_asymm_operate(operation->state, ua, paramCount, digest,
1661 				 digestLen, signature, &sl);
1662 	*signatureLen = sl;
1663 
1664 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
1665 		TEE_Panic(res);
1666 
1667 	return res;
1668 }
1669 
1670 TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation,
1671 				      TEE_Attribute *params,
1672 				      uint32_t paramCount, void *digest,
1673 				      uint32_t digestLen, void *signature,
1674 				      uint32_t signatureLen)
1675 {
1676 	TEE_Result res;
1677 	struct utee_attribute ua[paramCount];
1678 
1679 	if (operation == TEE_HANDLE_NULL ||
1680 	    (digest == NULL && digestLen != 0) ||
1681 	    (signature == NULL && signatureLen != 0))
1682 		TEE_Panic(0);
1683 	if (params == NULL && paramCount != 0)
1684 		TEE_Panic(0);
1685 	if (!operation->key1)
1686 		TEE_Panic(0);
1687 	if (operation->info.operationClass !=
1688 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1689 		TEE_Panic(0);
1690 	if (operation->info.mode != TEE_MODE_VERIFY)
1691 		TEE_Panic(0);
1692 
1693 	__utee_from_attr(ua, params, paramCount);
1694 	res = utee_asymm_verify(operation->state, ua, paramCount, digest,
1695 				digestLen, signature, signatureLen);
1696 
1697 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
1698 		TEE_Panic(res);
1699 
1700 	return res;
1701 }
1702 
1703 /* Cryptographic Operations API - Key Derivation Functions */
1704 
1705 void TEE_DeriveKey(TEE_OperationHandle operation,
1706 		   const TEE_Attribute *params, uint32_t paramCount,
1707 		   TEE_ObjectHandle derivedKey)
1708 {
1709 	TEE_Result res;
1710 	TEE_ObjectInfo key_info;
1711 	struct utee_attribute ua[paramCount];
1712 
1713 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
1714 		TEE_Panic(0);
1715 	if (params == NULL && paramCount != 0)
1716 		TEE_Panic(0);
1717 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
1718 	    TEE_OPERATION_KEY_DERIVATION)
1719 		TEE_Panic(0);
1720 
1721 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
1722 		TEE_Panic(0);
1723 	if (!operation->key1)
1724 		TEE_Panic(0);
1725 	if (operation->info.mode != TEE_MODE_DERIVE)
1726 		TEE_Panic(0);
1727 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
1728 		TEE_Panic(0);
1729 
1730 	res = utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info);
1731 	if (res != TEE_SUCCESS)
1732 		TEE_Panic(res);
1733 
1734 	if (key_info.objectType != TEE_TYPE_GENERIC_SECRET)
1735 		TEE_Panic(0);
1736 	if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1737 		TEE_Panic(0);
1738 
1739 	__utee_from_attr(ua, params, paramCount);
1740 	res = utee_cryp_derive_key(operation->state, ua, paramCount,
1741 				   (unsigned long)derivedKey);
1742 	if (res != TEE_SUCCESS)
1743 		TEE_Panic(res);
1744 }
1745 
1746 /* Cryptographic Operations API - Random Number Generation Functions */
1747 
1748 void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen)
1749 {
1750 	TEE_Result res;
1751 
1752 	res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
1753 	if (res != TEE_SUCCESS)
1754 		TEE_Panic(res);
1755 }
1756