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