xref: /optee_os/lib/libutee/tee_api_operations.c (revision 8f07fe6f754c642bcc33224df2772359b36f7d2b)
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, const 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 		      const 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, const 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, const void *IV,
834 		    uint32_t IVLen)
835 {
836 	TEE_Result res;
837 
838 	if (operation == TEE_HANDLE_NULL)
839 		TEE_Panic(0);
840 
841 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
842 		TEE_Panic(0);
843 
844 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
845 	    !(operation->key1))
846 		TEE_Panic(0);
847 
848 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
849 		TEE_ResetOperation(operation);
850 
851 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
852 
853 	res = utee_cipher_init(operation->state, IV, IVLen);
854 	if (res != TEE_SUCCESS)
855 		TEE_Panic(res);
856 
857 	operation->buffer_offs = 0;
858 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
859 }
860 
861 static TEE_Result tee_buffer_update(
862 		TEE_OperationHandle op,
863 		TEE_Result(*update_func)(unsigned long state, const void *src,
864 				size_t slen, void *dst, uint64_t *dlen),
865 		const void *src_data, size_t src_len,
866 		void *dest_data, uint64_t *dest_len)
867 {
868 	TEE_Result res;
869 	const uint8_t *src = src_data;
870 	size_t slen = src_len;
871 	uint8_t *dst = dest_data;
872 	size_t dlen = *dest_len;
873 	size_t acc_dlen = 0;
874 	uint64_t tmp_dlen;
875 	size_t l;
876 	size_t buffer_size;
877 	size_t buffer_left;
878 
879 	if (!src) {
880 		if (slen)
881 			TEE_Panic(0);
882 		goto out;
883 	}
884 
885 	if (op->buffer_two_blocks) {
886 		buffer_size = op->block_size * 2;
887 		buffer_left = 1;
888 	} else {
889 		buffer_size = op->block_size;
890 		buffer_left = 0;
891 	}
892 
893 	if (op->buffer_offs > 0) {
894 		/* Fill up complete block */
895 		if (op->buffer_offs < op->block_size)
896 			l = MIN(slen, op->block_size - op->buffer_offs);
897 		else
898 			l = MIN(slen, buffer_size - op->buffer_offs);
899 		memcpy(op->buffer + op->buffer_offs, src, l);
900 		op->buffer_offs += l;
901 		src += l;
902 		slen -= l;
903 		if ((op->buffer_offs % op->block_size) != 0)
904 			goto out;	/* Nothing left to do */
905 	}
906 
907 	/* If we can feed from buffer */
908 	if ((op->buffer_offs > 0) &&
909 	    ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) {
910 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
911 				op->block_size);
912 		l = MIN(op->buffer_offs, l);
913 		tmp_dlen = dlen;
914 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
915 		if (res != TEE_SUCCESS)
916 			TEE_Panic(res);
917 		dst += tmp_dlen;
918 		dlen -= tmp_dlen;
919 		acc_dlen += tmp_dlen;
920 		op->buffer_offs -= l;
921 		if (op->buffer_offs > 0) {
922 			/*
923 			 * Slen is small enough to be contained in rest buffer.
924 			 */
925 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
926 			memcpy(op->buffer + op->buffer_offs, src, slen);
927 			op->buffer_offs += slen;
928 			goto out;	/* Nothing left to do */
929 		}
930 	}
931 
932 	if (slen >= (buffer_size + buffer_left)) {
933 		/* Buffer is empty, feed as much as possible from src */
934 		if (op->info.algorithm == TEE_ALG_AES_CTS)
935 			l = ROUNDUP(slen - buffer_size, op->block_size);
936 		else
937 			l = ROUNDUP(slen - buffer_size + 1, op->block_size);
938 
939 		tmp_dlen = dlen;
940 		res = update_func(op->state, src, l, dst, &tmp_dlen);
941 		if (res != TEE_SUCCESS)
942 			TEE_Panic(res);
943 		src += l;
944 		slen -= l;
945 		dst += tmp_dlen;
946 		dlen -= tmp_dlen;
947 		acc_dlen += tmp_dlen;
948 	}
949 
950 	/* Slen is small enough to be contained in buffer. */
951 	memcpy(op->buffer + op->buffer_offs, src, slen);
952 	op->buffer_offs += slen;
953 
954 out:
955 	*dest_len = acc_dlen;
956 	return TEE_SUCCESS;
957 }
958 
959 TEE_Result TEE_CipherUpdate(TEE_OperationHandle operation, const void *srcData,
960 			    uint32_t srcLen, void *destData, uint32_t *destLen)
961 {
962 	TEE_Result res;
963 	size_t req_dlen;
964 	uint64_t dl;
965 
966 	if (operation == TEE_HANDLE_NULL ||
967 	    (srcData == NULL && srcLen != 0) ||
968 	    destLen == NULL ||
969 	    (destData == NULL && *destLen != 0)) {
970 		res = TEE_ERROR_BAD_PARAMETERS;
971 		goto out;
972 	}
973 
974 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
975 		res = TEE_ERROR_BAD_PARAMETERS;
976 		goto out;
977 	}
978 
979 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
980 		res = TEE_ERROR_BAD_PARAMETERS;
981 		goto out;
982 	}
983 
984 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
985 		res = TEE_ERROR_BAD_PARAMETERS;
986 		goto out;
987 	}
988 
989 	if (!srcData && !srcLen) {
990 		*destLen = 0;
991 		res = TEE_SUCCESS;
992 		goto out;
993 	}
994 
995 	/* Calculate required dlen */
996 	req_dlen = ((operation->buffer_offs + srcLen) / operation->block_size) *
997 	    operation->block_size;
998 	if (operation->buffer_two_blocks) {
999 		if (req_dlen > operation->block_size * 2)
1000 			req_dlen -= operation->block_size * 2;
1001 		else
1002 			req_dlen = 0;
1003 	}
1004 	/*
1005 	 * Check that required destLen is big enough before starting to feed
1006 	 * data to the algorithm. Errors during feeding of data are fatal as we
1007 	 * can't restore sync with this API.
1008 	 */
1009 	if (*destLen < req_dlen) {
1010 		*destLen = req_dlen;
1011 		res = TEE_ERROR_SHORT_BUFFER;
1012 		goto out;
1013 	}
1014 
1015 	dl = *destLen;
1016 	res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen,
1017 				destData, &dl);
1018 	*destLen = dl;
1019 
1020 out:
1021 	if (res != TEE_SUCCESS &&
1022 	    res != TEE_ERROR_SHORT_BUFFER)
1023 		TEE_Panic(res);
1024 
1025 	return res;
1026 }
1027 
1028 TEE_Result TEE_CipherDoFinal(TEE_OperationHandle operation,
1029 			     const void *srcData, uint32_t srcLen,
1030 			     void *destData, uint32_t *destLen)
1031 {
1032 	TEE_Result res;
1033 	uint8_t *dst = destData;
1034 	size_t acc_dlen = 0;
1035 	uint64_t tmp_dlen;
1036 	size_t req_dlen;
1037 
1038 	if (operation == TEE_HANDLE_NULL ||
1039 	    (srcData == NULL && srcLen != 0) ||
1040 	    destLen == NULL ||
1041 	    (destData == NULL && *destLen != 0)) {
1042 		res = TEE_ERROR_BAD_PARAMETERS;
1043 		goto out;
1044 	}
1045 
1046 	if (operation->info.operationClass != TEE_OPERATION_CIPHER) {
1047 		res = TEE_ERROR_BAD_PARAMETERS;
1048 		goto out;
1049 	}
1050 
1051 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1052 		res = TEE_ERROR_BAD_PARAMETERS;
1053 		goto out;
1054 	}
1055 
1056 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1057 		res = TEE_ERROR_BAD_PARAMETERS;
1058 		goto out;
1059 	}
1060 
1061 	/*
1062 	 * Check that the final block doesn't require padding for those
1063 	 * algorithms that requires client to supply padding.
1064 	 */
1065 	if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
1066 	    operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
1067 	    operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
1068 	    operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
1069 	    operation->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
1070 	    operation->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) {
1071 		if (((operation->buffer_offs + srcLen) % operation->block_size)
1072 		    != 0) {
1073 			res = TEE_ERROR_BAD_PARAMETERS;
1074 			goto out;
1075 		}
1076 	}
1077 
1078 	/*
1079 	 * Check that required destLen is big enough before starting to feed
1080 	 * data to the algorithm. Errors during feeding of data are fatal as we
1081 	 * can't restore sync with this API.
1082 	 */
1083 	req_dlen = operation->buffer_offs + srcLen;
1084 	if (*destLen < req_dlen) {
1085 		*destLen = req_dlen;
1086 		res = TEE_ERROR_SHORT_BUFFER;
1087 		goto out;
1088 	}
1089 
1090 	tmp_dlen = *destLen - acc_dlen;
1091 	res = tee_buffer_update(operation, utee_cipher_update, srcData, srcLen,
1092 				dst, &tmp_dlen);
1093 	if (res != TEE_SUCCESS)
1094 		goto out;
1095 
1096 	dst += tmp_dlen;
1097 	acc_dlen += tmp_dlen;
1098 
1099 	tmp_dlen = *destLen - acc_dlen;
1100 	res = utee_cipher_final(operation->state, operation->buffer,
1101 				operation->buffer_offs, dst, &tmp_dlen);
1102 	if (res != TEE_SUCCESS)
1103 		goto out;
1104 
1105 	acc_dlen += tmp_dlen;
1106 	*destLen = acc_dlen;
1107 
1108 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1109 
1110 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1111 
1112 out:
1113 	if (res != TEE_SUCCESS &&
1114 	    res != TEE_ERROR_SHORT_BUFFER)
1115 		TEE_Panic(res);
1116 
1117 	return res;
1118 }
1119 
1120 /* Cryptographic Operations API - MAC Functions */
1121 
1122 void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen)
1123 {
1124 	if (operation == TEE_HANDLE_NULL)
1125 		TEE_Panic(0);
1126 
1127 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1128 		TEE_Panic(0);
1129 
1130 	if (!(operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) ||
1131 	    !(operation->key1))
1132 		TEE_Panic(0);
1133 
1134 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL)
1135 		TEE_ResetOperation(operation);
1136 
1137 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1138 
1139 	init_hash_operation(operation, IV, IVLen);
1140 }
1141 
1142 void TEE_MACUpdate(TEE_OperationHandle operation, const void *chunk,
1143 		   uint32_t chunkSize)
1144 {
1145 	TEE_Result res;
1146 
1147 	if (operation == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
1148 		TEE_Panic(0);
1149 
1150 	if (operation->info.operationClass != TEE_OPERATION_MAC)
1151 		TEE_Panic(0);
1152 
1153 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1154 		TEE_Panic(0);
1155 
1156 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE)
1157 		TEE_Panic(0);
1158 
1159 	res = utee_hash_update(operation->state, chunk, chunkSize);
1160 	if (res != TEE_SUCCESS)
1161 		TEE_Panic(res);
1162 }
1163 
1164 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle operation,
1165 			       const void *message, uint32_t messageLen,
1166 			       void *mac, uint32_t *macLen)
1167 {
1168 	TEE_Result res;
1169 	uint64_t ml;
1170 
1171 	if (operation == TEE_HANDLE_NULL ||
1172 	    (message == NULL && messageLen != 0) ||
1173 	    mac == NULL ||
1174 	    macLen == NULL) {
1175 		res = TEE_ERROR_BAD_PARAMETERS;
1176 		goto out;
1177 	}
1178 
1179 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
1180 		res = TEE_ERROR_BAD_PARAMETERS;
1181 		goto out;
1182 	}
1183 
1184 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1185 		res = TEE_ERROR_BAD_PARAMETERS;
1186 		goto out;
1187 	}
1188 
1189 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1190 		res = TEE_ERROR_BAD_PARAMETERS;
1191 		goto out;
1192 	}
1193 
1194 	ml = *macLen;
1195 	res = utee_hash_final(operation->state, message, messageLen, mac, &ml);
1196 	*macLen = ml;
1197 	if (res != TEE_SUCCESS)
1198 		goto out;
1199 
1200 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1201 
1202 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1203 
1204 out:
1205 	if (res != TEE_SUCCESS &&
1206 	    res != TEE_ERROR_SHORT_BUFFER)
1207 		TEE_Panic(res);
1208 
1209 	return res;
1210 }
1211 
1212 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
1213 			       const void *message, uint32_t messageLen,
1214 			       const void *mac, uint32_t macLen)
1215 {
1216 	TEE_Result res;
1217 	uint8_t computed_mac[TEE_MAX_HASH_SIZE];
1218 	uint32_t computed_mac_size = TEE_MAX_HASH_SIZE;
1219 
1220 	if (operation->info.operationClass != TEE_OPERATION_MAC) {
1221 		res = TEE_ERROR_BAD_PARAMETERS;
1222 		goto out;
1223 	}
1224 
1225 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1226 		res = TEE_ERROR_BAD_PARAMETERS;
1227 		goto out;
1228 	}
1229 
1230 	if (operation->operationState != TEE_OPERATION_STATE_ACTIVE) {
1231 		res = TEE_ERROR_BAD_PARAMETERS;
1232 		goto out;
1233 	}
1234 
1235 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
1236 				  &computed_mac_size);
1237 	if (res != TEE_SUCCESS)
1238 		goto out;
1239 
1240 	if (computed_mac_size != macLen) {
1241 		res = TEE_ERROR_MAC_INVALID;
1242 		goto out;
1243 	}
1244 
1245 	if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0) {
1246 		res = TEE_ERROR_MAC_INVALID;
1247 		goto out;
1248 	}
1249 
1250 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1251 
1252 out:
1253 	if (res != TEE_SUCCESS &&
1254 	    res != TEE_ERROR_MAC_INVALID)
1255 		TEE_Panic(res);
1256 
1257 	return res;
1258 }
1259 
1260 /* Cryptographic Operations API - Authenticated Encryption Functions */
1261 
1262 TEE_Result TEE_AEInit(TEE_OperationHandle operation, const void *nonce,
1263 		      uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen,
1264 		      uint32_t payloadLen)
1265 {
1266 	TEE_Result res;
1267 
1268 	if (operation == TEE_HANDLE_NULL || nonce == NULL) {
1269 		res = TEE_ERROR_BAD_PARAMETERS;
1270 		goto out;
1271 	}
1272 
1273 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1274 		res = TEE_ERROR_BAD_PARAMETERS;
1275 		goto out;
1276 	}
1277 
1278 	if (operation->operationState != TEE_OPERATION_STATE_INITIAL) {
1279 		res = TEE_ERROR_BAD_PARAMETERS;
1280 		goto out;
1281 	}
1282 
1283 	/*
1284 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
1285 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
1286 	 * according to the same principle so we have to check here instead to
1287 	 * be GP compliant.
1288 	 */
1289 	if (operation->info.algorithm == TEE_ALG_AES_GCM) {
1290 		/*
1291 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
1292 		 */
1293 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0)) {
1294 			res = TEE_ERROR_NOT_SUPPORTED;
1295 			goto out;
1296 		}
1297 	}
1298 
1299 	res = utee_authenc_init(operation->state, nonce, nonceLen,
1300 				tagLen / 8, AADLen, payloadLen);
1301 	if (res != TEE_SUCCESS)
1302 		goto out;
1303 
1304 	operation->ae_tag_len = tagLen / 8;
1305 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1306 
1307 out:
1308 	if (res != TEE_SUCCESS &&
1309 	    res != TEE_ERROR_NOT_SUPPORTED)
1310 			TEE_Panic(res);
1311 
1312 	return res;
1313 }
1314 
1315 void TEE_AEUpdateAAD(TEE_OperationHandle operation, const void *AADdata,
1316 		     uint32_t AADdataLen)
1317 {
1318 	TEE_Result res;
1319 
1320 	if (operation == TEE_HANDLE_NULL ||
1321 	    (AADdata == NULL && AADdataLen != 0))
1322 		TEE_Panic(0);
1323 
1324 	if (operation->info.operationClass != TEE_OPERATION_AE)
1325 		TEE_Panic(0);
1326 
1327 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1328 		TEE_Panic(0);
1329 
1330 	res = utee_authenc_update_aad(operation->state, AADdata, AADdataLen);
1331 
1332 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1333 
1334 	if (res != TEE_SUCCESS)
1335 		TEE_Panic(res);
1336 }
1337 
1338 TEE_Result TEE_AEUpdate(TEE_OperationHandle operation, const void *srcData,
1339 			uint32_t srcLen, void *destData, uint32_t *destLen)
1340 {
1341 	TEE_Result res;
1342 	size_t req_dlen;
1343 	uint64_t dl;
1344 
1345 	if (operation == TEE_HANDLE_NULL ||
1346 	    (srcData == NULL && srcLen != 0) ||
1347 	    destLen == NULL ||
1348 	    (destData == NULL && *destLen != 0)) {
1349 		res = TEE_ERROR_BAD_PARAMETERS;
1350 		goto out;
1351 	}
1352 
1353 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1354 		res = TEE_ERROR_BAD_PARAMETERS;
1355 		goto out;
1356 	}
1357 
1358 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1359 		res = TEE_ERROR_BAD_PARAMETERS;
1360 		goto out;
1361 	}
1362 
1363 	if (!srcData && !srcLen) {
1364 		*destLen = 0;
1365 		res = TEE_SUCCESS;
1366 		goto out;
1367 	}
1368 
1369 	/*
1370 	 * Check that required destLen is big enough before starting to feed
1371 	 * data to the algorithm. Errors during feeding of data are fatal as we
1372 	 * can't restore sync with this API.
1373 	 */
1374 	req_dlen = ROUNDDOWN(operation->buffer_offs + srcLen,
1375 			     operation->block_size);
1376 	if (*destLen < req_dlen) {
1377 		*destLen = req_dlen;
1378 		res = TEE_ERROR_SHORT_BUFFER;
1379 		goto out;
1380 	}
1381 
1382 	dl = *destLen;
1383 	res = tee_buffer_update(operation, utee_authenc_update_payload, srcData,
1384 				srcLen, destData, &dl);
1385 	*destLen = dl;
1386 
1387 	operation->operationState = TEE_OPERATION_STATE_ACTIVE;
1388 
1389 out:
1390 	if (res != TEE_SUCCESS &&
1391 	    res != TEE_ERROR_SHORT_BUFFER)
1392 			TEE_Panic(res);
1393 
1394 	return res;
1395 }
1396 
1397 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle operation,
1398 			      const void *srcData, uint32_t srcLen,
1399 			      void *destData, uint32_t *destLen, void *tag,
1400 			      uint32_t *tagLen)
1401 {
1402 	TEE_Result res;
1403 	uint8_t *dst = destData;
1404 	size_t acc_dlen = 0;
1405 	uint64_t tmp_dlen;
1406 	size_t req_dlen;
1407 	uint64_t tl;
1408 
1409 	if (operation == TEE_HANDLE_NULL ||
1410 	    (srcData == NULL && srcLen != 0) ||
1411 	    destLen == NULL ||
1412 	    (destData == NULL && *destLen != 0) ||
1413 	    tag == NULL || tagLen == NULL) {
1414 		res = TEE_ERROR_BAD_PARAMETERS;
1415 		goto out;
1416 	}
1417 
1418 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1419 		res = TEE_ERROR_BAD_PARAMETERS;
1420 		goto out;
1421 	}
1422 
1423 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1424 		res = TEE_ERROR_BAD_PARAMETERS;
1425 		goto out;
1426 	}
1427 
1428 	/*
1429 	 * Check that required destLen is big enough before starting to feed
1430 	 * data to the algorithm. Errors during feeding of data are fatal as we
1431 	 * can't restore sync with this API.
1432 	 */
1433 	req_dlen = operation->buffer_offs + srcLen;
1434 	if (*destLen < req_dlen) {
1435 		*destLen = req_dlen;
1436 		res = TEE_ERROR_SHORT_BUFFER;
1437 		goto out;
1438 	}
1439 
1440 	/*
1441 	 * Need to check this before update_payload since sync would be lost if
1442 	 * we return short buffer after that.
1443 	 */
1444 	if (*tagLen < operation->ae_tag_len) {
1445 		*tagLen = operation->ae_tag_len;
1446 		res = TEE_ERROR_SHORT_BUFFER;
1447 		goto out;
1448 	}
1449 
1450 	tmp_dlen = *destLen - acc_dlen;
1451 	res = tee_buffer_update(operation, utee_authenc_update_payload, srcData,
1452 				srcLen, dst, &tmp_dlen);
1453 	if (res != TEE_SUCCESS)
1454 		goto out;
1455 
1456 	dst += tmp_dlen;
1457 	acc_dlen += tmp_dlen;
1458 
1459 	tmp_dlen = *destLen - acc_dlen;
1460 	tl = *tagLen;
1461 	res = utee_authenc_enc_final(operation->state, operation->buffer,
1462 				     operation->buffer_offs, dst, &tmp_dlen,
1463 				     tag, &tl);
1464 	*tagLen = tl;
1465 	if (res != TEE_SUCCESS)
1466 		goto out;
1467 
1468 	acc_dlen += tmp_dlen;
1469 	*destLen = acc_dlen;
1470 
1471 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1472 
1473 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1474 
1475 out:
1476 	if (res != TEE_SUCCESS &&
1477 	    res != TEE_ERROR_SHORT_BUFFER)
1478 			TEE_Panic(res);
1479 
1480 	return res;
1481 }
1482 
1483 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle operation,
1484 			      const void *srcData, uint32_t srcLen,
1485 			      void *destData, uint32_t *destLen, void *tag,
1486 			      uint32_t tagLen)
1487 {
1488 	TEE_Result res;
1489 	uint8_t *dst = destData;
1490 	size_t acc_dlen = 0;
1491 	uint64_t tmp_dlen;
1492 	size_t req_dlen;
1493 
1494 	if (operation == TEE_HANDLE_NULL ||
1495 	    (srcData == NULL && srcLen != 0) ||
1496 	    destLen == NULL ||
1497 	    (destData == NULL && *destLen != 0) ||
1498 	    (tag == NULL && tagLen != 0)) {
1499 		res = TEE_ERROR_BAD_PARAMETERS;
1500 		goto out;
1501 	}
1502 
1503 	if (operation->info.operationClass != TEE_OPERATION_AE) {
1504 		res = TEE_ERROR_BAD_PARAMETERS;
1505 		goto out;
1506 	}
1507 
1508 	if ((operation->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
1509 		res = TEE_ERROR_BAD_PARAMETERS;
1510 		goto out;
1511 	}
1512 
1513 	/*
1514 	 * Check that required destLen is big enough before starting to feed
1515 	 * data to the algorithm. Errors during feeding of data are fatal as we
1516 	 * can't restore sync with this API.
1517 	 */
1518 	req_dlen = operation->buffer_offs + srcLen;
1519 	if (*destLen < req_dlen) {
1520 		*destLen = req_dlen;
1521 		res = TEE_ERROR_SHORT_BUFFER;
1522 		goto out;
1523 	}
1524 
1525 	tmp_dlen = *destLen - acc_dlen;
1526 	res = tee_buffer_update(operation, utee_authenc_update_payload, srcData,
1527 				srcLen, dst, &tmp_dlen);
1528 	if (res != TEE_SUCCESS)
1529 		goto out;
1530 
1531 	dst += tmp_dlen;
1532 	acc_dlen += tmp_dlen;
1533 
1534 	tmp_dlen = *destLen - acc_dlen;
1535 	res = utee_authenc_dec_final(operation->state, operation->buffer,
1536 				     operation->buffer_offs, dst, &tmp_dlen,
1537 				     tag, tagLen);
1538 	if (res != TEE_SUCCESS)
1539 		goto out;
1540 
1541 	/* Supplied tagLen should match what we initiated with */
1542 	if (tagLen != operation->ae_tag_len)
1543 		res = TEE_ERROR_MAC_INVALID;
1544 
1545 	acc_dlen += tmp_dlen;
1546 	*destLen = acc_dlen;
1547 
1548 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1549 
1550 	operation->operationState = TEE_OPERATION_STATE_INITIAL;
1551 
1552 out:
1553 	if (res != TEE_SUCCESS &&
1554 	    res != TEE_ERROR_SHORT_BUFFER &&
1555 	    res != TEE_ERROR_MAC_INVALID)
1556 			TEE_Panic(res);
1557 
1558 	return res;
1559 }
1560 
1561 /* Cryptographic Operations API - Asymmetric Functions */
1562 
1563 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle operation,
1564 				 const TEE_Attribute *params,
1565 				 uint32_t paramCount, const void *srcData,
1566 				 uint32_t srcLen, void *destData,
1567 				 uint32_t *destLen)
1568 {
1569 	TEE_Result res;
1570 	struct utee_attribute ua[paramCount];
1571 	uint64_t dl;
1572 
1573 	if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1574 	    destLen == NULL || (destData == NULL && *destLen != 0))
1575 		TEE_Panic(0);
1576 	if (params == NULL && paramCount != 0)
1577 		TEE_Panic(0);
1578 	if (!operation->key1)
1579 		TEE_Panic(0);
1580 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1581 		TEE_Panic(0);
1582 	if (operation->info.mode != TEE_MODE_ENCRYPT)
1583 		TEE_Panic(0);
1584 
1585 	__utee_from_attr(ua, params, paramCount);
1586 	dl = *destLen;
1587 	res = utee_asymm_operate(operation->state, ua, paramCount, srcData,
1588 				 srcLen, destData, &dl);
1589 	*destLen = dl;
1590 
1591 	if (res != TEE_SUCCESS &&
1592 	    res != TEE_ERROR_SHORT_BUFFER &&
1593 	    res != TEE_ERROR_BAD_PARAMETERS)
1594 		TEE_Panic(res);
1595 
1596 	return res;
1597 }
1598 
1599 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle operation,
1600 				 const TEE_Attribute *params,
1601 				 uint32_t paramCount, const void *srcData,
1602 				 uint32_t srcLen, void *destData,
1603 				 uint32_t *destLen)
1604 {
1605 	TEE_Result res;
1606 	struct utee_attribute ua[paramCount];
1607 	uint64_t dl;
1608 
1609 	if (operation == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1610 	    destLen == NULL || (destData == NULL && *destLen != 0))
1611 		TEE_Panic(0);
1612 	if (params == NULL && paramCount != 0)
1613 		TEE_Panic(0);
1614 	if (!operation->key1)
1615 		TEE_Panic(0);
1616 	if (operation->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1617 		TEE_Panic(0);
1618 	if (operation->info.mode != TEE_MODE_DECRYPT)
1619 		TEE_Panic(0);
1620 
1621 	__utee_from_attr(ua, params, paramCount);
1622 	dl = *destLen;
1623 	res = utee_asymm_operate(operation->state, ua, paramCount, srcData,
1624 				 srcLen, destData, &dl);
1625 	*destLen = dl;
1626 
1627 	if (res != TEE_SUCCESS &&
1628 	    res != TEE_ERROR_SHORT_BUFFER &&
1629 	    res != TEE_ERROR_BAD_PARAMETERS)
1630 		TEE_Panic(res);
1631 
1632 	return res;
1633 }
1634 
1635 TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle operation,
1636 				    const TEE_Attribute *params,
1637 				    uint32_t paramCount, const void *digest,
1638 				    uint32_t digestLen, void *signature,
1639 				    uint32_t *signatureLen)
1640 {
1641 	TEE_Result res;
1642 	struct utee_attribute ua[paramCount];
1643 	uint64_t sl;
1644 
1645 	if (operation == TEE_HANDLE_NULL ||
1646 	    (digest == NULL && digestLen != 0) ||
1647 	    signature == NULL || signatureLen == NULL)
1648 		TEE_Panic(0);
1649 	if (params == NULL && paramCount != 0)
1650 		TEE_Panic(0);
1651 	if (!operation->key1)
1652 		TEE_Panic(0);
1653 	if (operation->info.operationClass !=
1654 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1655 		TEE_Panic(0);
1656 	if (operation->info.mode != TEE_MODE_SIGN)
1657 		TEE_Panic(0);
1658 
1659 	__utee_from_attr(ua, params, paramCount);
1660 	sl = *signatureLen;
1661 	res = utee_asymm_operate(operation->state, ua, paramCount, digest,
1662 				 digestLen, signature, &sl);
1663 	*signatureLen = sl;
1664 
1665 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
1666 		TEE_Panic(res);
1667 
1668 	return res;
1669 }
1670 
1671 TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle operation,
1672 				      const TEE_Attribute *params,
1673 				      uint32_t paramCount, const void *digest,
1674 				      uint32_t digestLen,
1675 				      const void *signature,
1676 				      uint32_t signatureLen)
1677 {
1678 	TEE_Result res;
1679 	struct utee_attribute ua[paramCount];
1680 
1681 	if (operation == TEE_HANDLE_NULL ||
1682 	    (digest == NULL && digestLen != 0) ||
1683 	    (signature == NULL && signatureLen != 0))
1684 		TEE_Panic(0);
1685 	if (params == NULL && paramCount != 0)
1686 		TEE_Panic(0);
1687 	if (!operation->key1)
1688 		TEE_Panic(0);
1689 	if (operation->info.operationClass !=
1690 	    TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1691 		TEE_Panic(0);
1692 	if (operation->info.mode != TEE_MODE_VERIFY)
1693 		TEE_Panic(0);
1694 
1695 	__utee_from_attr(ua, params, paramCount);
1696 	res = utee_asymm_verify(operation->state, ua, paramCount, digest,
1697 				digestLen, signature, signatureLen);
1698 
1699 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
1700 		TEE_Panic(res);
1701 
1702 	return res;
1703 }
1704 
1705 /* Cryptographic Operations API - Key Derivation Functions */
1706 
1707 void TEE_DeriveKey(TEE_OperationHandle operation,
1708 		   const TEE_Attribute *params, uint32_t paramCount,
1709 		   TEE_ObjectHandle derivedKey)
1710 {
1711 	TEE_Result res;
1712 	TEE_ObjectInfo key_info;
1713 	struct utee_attribute ua[paramCount];
1714 
1715 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
1716 		TEE_Panic(0);
1717 	if (params == NULL && paramCount != 0)
1718 		TEE_Panic(0);
1719 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
1720 	    TEE_OPERATION_KEY_DERIVATION)
1721 		TEE_Panic(0);
1722 
1723 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
1724 		TEE_Panic(0);
1725 	if (!operation->key1)
1726 		TEE_Panic(0);
1727 	if (operation->info.mode != TEE_MODE_DERIVE)
1728 		TEE_Panic(0);
1729 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
1730 		TEE_Panic(0);
1731 
1732 	res = utee_cryp_obj_get_info((unsigned long)derivedKey, &key_info);
1733 	if (res != TEE_SUCCESS)
1734 		TEE_Panic(res);
1735 
1736 	if (key_info.objectType != TEE_TYPE_GENERIC_SECRET)
1737 		TEE_Panic(0);
1738 	if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1739 		TEE_Panic(0);
1740 
1741 	__utee_from_attr(ua, params, paramCount);
1742 	res = utee_cryp_derive_key(operation->state, ua, paramCount,
1743 				   (unsigned long)derivedKey);
1744 	if (res != TEE_SUCCESS)
1745 		TEE_Panic(res);
1746 }
1747 
1748 /* Cryptographic Operations API - Random Number Generation Functions */
1749 
1750 void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen)
1751 {
1752 	TEE_Result res;
1753 
1754 	res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
1755 	if (res != TEE_SUCCESS)
1756 		TEE_Panic(res);
1757 }
1758