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