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