xref: /optee_os/lib/libutee/tee_api_operations.c (revision e889e80b67e71243ef28182e4bd32c9c92bcae17)
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 
38 struct __TEE_OperationHandle {
39 	TEE_OperationInfo info;
40 	TEE_ObjectHandle key1;
41 	TEE_ObjectHandle key2;
42 	uint8_t *buffer;	/* buffer to collect complete blocks */
43 	bool buffer_two_blocks;	/* True if two blocks need to be buffered */
44 	size_t block_size;	/* Block size of cipher */
45 	size_t buffer_offs;	/* Offset in buffer */
46 	uint32_t state;		/* Handle to state in TEE Core */
47 	uint32_t ae_tag_len;	/*
48 				 * tag_len in bytes for AE operation else unused
49 				 */
50 };
51 
52 /* Cryptographic Operations API - Generic Operation Functions */
53 
54 TEE_Result TEE_AllocateOperation(TEE_OperationHandle *operation,
55 				 uint32_t algorithm, uint32_t mode,
56 				 uint32_t maxKeySize)
57 {
58 	TEE_Result res;
59 	TEE_OperationHandle op = TEE_HANDLE_NULL;
60 	uint32_t handle_state = 0;
61 	size_t block_size = 1;
62 	uint32_t req_key_usage;
63 	bool with_private_key = false;
64 	bool buffer_two_blocks = false;
65 
66 	if (!operation)
67 		TEE_Panic(0);
68 
69 	if (algorithm == TEE_ALG_AES_XTS)
70 		handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
71 
72 	/* Check algorithm max key size */
73 	switch (algorithm) {
74 	case TEE_ALG_DSA_SHA1:
75 		if (maxKeySize < 512)
76 			return TEE_ERROR_NOT_SUPPORTED;
77 		if (maxKeySize > 1024)
78 			return TEE_ERROR_NOT_SUPPORTED;
79 		if (maxKeySize % 64 != 0)
80 			return TEE_ERROR_NOT_SUPPORTED;
81 		break;
82 
83 	case TEE_ALG_DSA_SHA224:
84 		if (maxKeySize != 2048)
85 			return TEE_ERROR_NOT_SUPPORTED;
86 		break;
87 
88 	case TEE_ALG_DSA_SHA256:
89 		if (maxKeySize != 2048 && maxKeySize != 3072)
90 			return TEE_ERROR_NOT_SUPPORTED;
91 		break;
92 
93 	case TEE_ALG_ECDSA_P192:
94 	case TEE_ALG_ECDH_P192:
95 		if (maxKeySize != 192)
96 			return TEE_ERROR_NOT_SUPPORTED;
97 		break;
98 
99 	case TEE_ALG_ECDSA_P224:
100 	case TEE_ALG_ECDH_P224:
101 		if (maxKeySize != 224)
102 			return TEE_ERROR_NOT_SUPPORTED;
103 		break;
104 
105 	case TEE_ALG_ECDSA_P256:
106 	case TEE_ALG_ECDH_P256:
107 		if (maxKeySize != 256)
108 			return TEE_ERROR_NOT_SUPPORTED;
109 		break;
110 
111 	case TEE_ALG_ECDSA_P384:
112 	case TEE_ALG_ECDH_P384:
113 		if (maxKeySize != 384)
114 			return TEE_ERROR_NOT_SUPPORTED;
115 		break;
116 
117 	case TEE_ALG_ECDSA_P521:
118 	case TEE_ALG_ECDH_P521:
119 		if (maxKeySize != 521)
120 			return TEE_ERROR_NOT_SUPPORTED;
121 		break;
122 
123 	default:
124 		break;
125 	}
126 
127 	/* Check algorithm mode */
128 	switch (algorithm) {
129 	case TEE_ALG_AES_CTS:
130 	case TEE_ALG_AES_XTS:
131 		buffer_two_blocks = true;
132 	 /*FALLTHROUGH*/ case TEE_ALG_AES_ECB_NOPAD:
133 	case TEE_ALG_AES_CBC_NOPAD:
134 	case TEE_ALG_AES_CTR:
135 	case TEE_ALG_AES_CCM:
136 	case TEE_ALG_AES_GCM:
137 	case TEE_ALG_DES_ECB_NOPAD:
138 	case TEE_ALG_DES_CBC_NOPAD:
139 	case TEE_ALG_DES3_ECB_NOPAD:
140 	case TEE_ALG_DES3_CBC_NOPAD:
141 		if (TEE_ALG_GET_MAIN_ALG(algorithm) == TEE_MAIN_ALGO_AES)
142 			block_size = TEE_AES_BLOCK_SIZE;
143 		else
144 			block_size = TEE_DES_BLOCK_SIZE;
145 
146 		if (mode == TEE_MODE_ENCRYPT)
147 			req_key_usage = TEE_USAGE_ENCRYPT;
148 		else if (mode == TEE_MODE_DECRYPT)
149 			req_key_usage = TEE_USAGE_DECRYPT;
150 		else
151 			return TEE_ERROR_NOT_SUPPORTED;
152 		break;
153 
154 	case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
155 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
156 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
157 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
158 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
159 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
160 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
161 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
162 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
163 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
164 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
165 	case TEE_ALG_DSA_SHA1:
166 	case TEE_ALG_DSA_SHA224:
167 	case TEE_ALG_DSA_SHA256:
168 	case TEE_ALG_ECDSA_P192:
169 	case TEE_ALG_ECDSA_P224:
170 	case TEE_ALG_ECDSA_P256:
171 	case TEE_ALG_ECDSA_P384:
172 	case TEE_ALG_ECDSA_P521:
173 		if (mode == TEE_MODE_SIGN) {
174 			with_private_key = true;
175 			req_key_usage = TEE_USAGE_SIGN;
176 		} else if (mode == TEE_MODE_VERIFY) {
177 			req_key_usage = TEE_USAGE_VERIFY;
178 		} else {
179 			return TEE_ERROR_NOT_SUPPORTED;
180 		}
181 		break;
182 
183 	case TEE_ALG_RSAES_PKCS1_V1_5:
184 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
185 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
186 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
187 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
188 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
189 		if (mode == TEE_MODE_ENCRYPT) {
190 			req_key_usage = TEE_USAGE_ENCRYPT;
191 		} else if (mode == TEE_MODE_DECRYPT) {
192 			with_private_key = true;
193 			req_key_usage = TEE_USAGE_DECRYPT;
194 		} else {
195 			return TEE_ERROR_NOT_SUPPORTED;
196 		}
197 		break;
198 
199 	case TEE_ALG_RSA_NOPAD:
200 		if (mode == TEE_MODE_ENCRYPT) {
201 			req_key_usage = TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY;
202 		} else if (mode == TEE_MODE_DECRYPT) {
203 			with_private_key = true;
204 			req_key_usage = TEE_USAGE_DECRYPT | TEE_USAGE_SIGN;
205 		} else {
206 			return TEE_ERROR_NOT_SUPPORTED;
207 		}
208 		break;
209 
210 	case TEE_ALG_DH_DERIVE_SHARED_SECRET:
211 	case TEE_ALG_ECDH_P192:
212 	case TEE_ALG_ECDH_P224:
213 	case TEE_ALG_ECDH_P256:
214 	case TEE_ALG_ECDH_P384:
215 	case TEE_ALG_ECDH_P521:
216 	case TEE_ALG_HKDF_MD5_DERIVE_KEY:
217 	case TEE_ALG_HKDF_SHA1_DERIVE_KEY:
218 	case TEE_ALG_HKDF_SHA224_DERIVE_KEY:
219 	case TEE_ALG_HKDF_SHA256_DERIVE_KEY:
220 	case TEE_ALG_HKDF_SHA384_DERIVE_KEY:
221 	case TEE_ALG_HKDF_SHA512_DERIVE_KEY:
222 	case TEE_ALG_CONCAT_KDF_SHA1_DERIVE_KEY:
223 	case TEE_ALG_CONCAT_KDF_SHA224_DERIVE_KEY:
224 	case TEE_ALG_CONCAT_KDF_SHA256_DERIVE_KEY:
225 	case TEE_ALG_CONCAT_KDF_SHA384_DERIVE_KEY:
226 	case TEE_ALG_CONCAT_KDF_SHA512_DERIVE_KEY:
227 	case TEE_ALG_PBKDF2_HMAC_SHA1_DERIVE_KEY:
228 		if (mode != TEE_MODE_DERIVE)
229 			return TEE_ERROR_NOT_SUPPORTED;
230 		with_private_key = true;
231 		req_key_usage = TEE_USAGE_DERIVE;
232 		break;
233 
234 	case TEE_ALG_MD5:
235 	case TEE_ALG_SHA1:
236 	case TEE_ALG_SHA224:
237 	case TEE_ALG_SHA256:
238 	case TEE_ALG_SHA384:
239 	case TEE_ALG_SHA512:
240 		if (mode != TEE_MODE_DIGEST)
241 			return TEE_ERROR_NOT_SUPPORTED;
242 		handle_state |= TEE_HANDLE_FLAG_KEY_SET;
243 		req_key_usage = 0;
244 		break;
245 
246 	case TEE_ALG_DES_CBC_MAC_NOPAD:
247 	case TEE_ALG_AES_CBC_MAC_NOPAD:
248 	case TEE_ALG_AES_CBC_MAC_PKCS5:
249 	case TEE_ALG_AES_CMAC:
250 	case TEE_ALG_DES_CBC_MAC_PKCS5:
251 	case TEE_ALG_DES3_CBC_MAC_NOPAD:
252 	case TEE_ALG_DES3_CBC_MAC_PKCS5:
253 	case TEE_ALG_HMAC_MD5:
254 	case TEE_ALG_HMAC_SHA1:
255 	case TEE_ALG_HMAC_SHA224:
256 	case TEE_ALG_HMAC_SHA256:
257 	case TEE_ALG_HMAC_SHA384:
258 	case TEE_ALG_HMAC_SHA512:
259 		if (mode != TEE_MODE_MAC)
260 			return TEE_ERROR_NOT_SUPPORTED;
261 		req_key_usage = TEE_USAGE_MAC;
262 		break;
263 
264 	default:
265 		return TEE_ERROR_NOT_SUPPORTED;
266 	}
267 
268 	op = TEE_Malloc(sizeof(*op), 0);
269 	if (!op)
270 		return TEE_ERROR_OUT_OF_MEMORY;
271 
272 	op->info.algorithm = algorithm;
273 	op->info.operationClass = TEE_ALG_GET_CLASS(algorithm);
274 	op->info.mode = mode;
275 	op->info.maxKeySize = maxKeySize;
276 	op->info.requiredKeyUsage = req_key_usage;
277 	op->info.handleState = handle_state;
278 
279 	if (block_size > 1) {
280 		size_t buffer_size = block_size;
281 
282 		if (buffer_two_blocks)
283 			buffer_size *= 2;
284 
285 		op->buffer = TEE_Malloc(buffer_size,
286 					TEE_USER_MEM_HINT_NO_FILL_ZERO);
287 		if (op->buffer == NULL) {
288 			res = TEE_ERROR_OUT_OF_MEMORY;
289 			goto err0;
290 		}
291 	}
292 	op->block_size = block_size;
293 	op->buffer_two_blocks = buffer_two_blocks;
294 
295 	if (TEE_ALG_GET_CLASS(algorithm) != TEE_OPERATION_DIGEST) {
296 		uint32_t mks = maxKeySize;
297 		TEE_ObjectType key_type = TEE_ALG_GET_KEY_TYPE(algorithm,
298 						       with_private_key);
299 
300 		/*
301 		 * If two keys are expected the max key size is the sum of
302 		 * the size of both keys.
303 		 */
304 		if (op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS)
305 			mks /= 2;
306 
307 		res = TEE_AllocateTransientObject(key_type, mks, &op->key1);
308 		if (res != TEE_SUCCESS)
309 			goto err1;
310 
311 		if ((op->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
312 		    0) {
313 			res = TEE_AllocateTransientObject(key_type, mks,
314 							  &op->key2);
315 			if (res != TEE_SUCCESS)
316 				goto err2;
317 		}
318 	}
319 
320 	res = utee_cryp_state_alloc(algorithm, mode, (uint32_t) op->key1,
321 				    (uint32_t) op->key2, &op->state);
322 	if (res != TEE_SUCCESS) {
323 		if ((op->info.handleState &
324 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 0)
325 			goto err2;
326 		goto err1;
327 	}
328 
329 	/* For multi-stage operation do an "init". */
330 	TEE_ResetOperation(op);
331 	*operation = op;
332 	goto out;
333 
334 err2:
335 	TEE_FreeTransientObject(op->key2);
336 err1:
337 	TEE_FreeTransientObject(op->key1);
338 err0:
339 	TEE_FreeOperation(op);
340 
341 	if (res != TEE_SUCCESS &&
342 	    res != TEE_ERROR_OUT_OF_MEMORY &&
343 	    res != TEE_ERROR_NOT_SUPPORTED)
344 		TEE_Panic(0);
345 out:
346 	return res;
347 }
348 
349 void TEE_FreeOperation(TEE_OperationHandle operation)
350 {
351 	TEE_Result res;
352 
353 	if (operation == TEE_HANDLE_NULL)
354 		TEE_Panic(0);
355 
356 	/*
357 	 * Note that keys should not be freed here, since they are
358 	 * claimed by the operation they will be freed by
359 	 * utee_cryp_state_free().
360 	 */
361 	res = utee_cryp_state_free(operation->state);
362 	if (res != TEE_SUCCESS)
363 		TEE_Panic(0);
364 
365 	TEE_Free(operation->buffer);
366 	TEE_Free(operation);
367 }
368 
369 void TEE_GetOperationInfo(TEE_OperationHandle operation,
370 			  TEE_OperationInfo *operationInfo)
371 {
372 	if (operation == TEE_HANDLE_NULL)
373 		TEE_Panic(0);
374 
375 	if (operationInfo == NULL)
376 		TEE_Panic(0);
377 
378 	*operationInfo = operation->info;
379 }
380 
381 void TEE_ResetOperation(TEE_OperationHandle operation)
382 {
383 	TEE_Result res;
384 
385 	if (operation == TEE_HANDLE_NULL)
386 		TEE_Panic(0);
387 	if (operation->info.operationClass == TEE_OPERATION_DIGEST) {
388 		res = utee_hash_init(operation->state, NULL, 0);
389 		if (res != TEE_SUCCESS)
390 			TEE_Panic(res);
391 	}
392 	operation->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
393 }
394 
395 TEE_Result TEE_SetOperationKey(TEE_OperationHandle operation,
396 			       TEE_ObjectHandle key)
397 {
398 	TEE_Result res;
399 	uint32_t key_size = 0;
400 
401 	if (operation == TEE_HANDLE_NULL)
402 		TEE_Panic(0);
403 
404 	/* No key for digests */
405 	if (operation->info.operationClass == TEE_OPERATION_DIGEST)
406 		TEE_Panic(0);
407 
408 	/* Two keys expected */
409 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
410 	    0)
411 		TEE_Panic(0);
412 
413 	if (key != TEE_HANDLE_NULL) {
414 		TEE_ObjectInfo key_info;
415 
416 		res = TEE_GetObjectInfo1(key, &key_info);
417 		if (res != TEE_SUCCESS)
418 			goto err;
419 
420 		/* Supplied key has to meet required usage */
421 		if ((key_info.objectUsage & operation->info.requiredKeyUsage) !=
422 		    operation->info.requiredKeyUsage) {
423 			TEE_Panic(0);
424 		}
425 
426 		if (operation->info.maxKeySize < key_info.keySize)
427 			TEE_Panic(0);
428 
429 		key_size = key_info.keySize;
430 	}
431 
432 	TEE_ResetTransientObject(operation->key1);
433 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
434 
435 	if (key != TEE_HANDLE_NULL) {
436 		res = TEE_CopyObjectAttributes1(operation->key1, key);
437 		if (res != TEE_SUCCESS)
438 			goto err;
439 
440 		operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
441 	}
442 
443 	operation->info.keySize = key_size;
444 
445 	goto out;
446 
447 err:
448 	if (res == TEE_ERROR_CORRUPT_OBJECT ||
449 	    res == TEE_ERROR_STORAGE_NOT_AVAILABLE)
450 		return res;
451 	TEE_Panic(0);
452 out:
453 	return TEE_SUCCESS;
454 }
455 
456 TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation,
457 				TEE_ObjectHandle key1, TEE_ObjectHandle key2)
458 {
459 	TEE_Result res;
460 	uint32_t key_size = 0;
461 
462 	if (operation == TEE_HANDLE_NULL)
463 		TEE_Panic(0);
464 
465 	/* Two keys not expected */
466 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) ==
467 	    0)
468 		TEE_Panic(0);
469 
470 	/* Either both keys are NULL or both are not NULL */
471 	if ((key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) &&
472 	    key1 != key2)
473 		TEE_Panic(0);
474 
475 	if (key1 != TEE_HANDLE_NULL) {
476 		TEE_ObjectInfo key_info1;
477 		TEE_ObjectInfo key_info2;
478 
479 		res = TEE_GetObjectInfo1(key1, &key_info1);
480 		if (res != TEE_SUCCESS)
481 			goto err;
482 
483 		/* Supplied key has to meet required usage */
484 		if ((key_info1.objectUsage & operation->info.
485 		     requiredKeyUsage) != operation->info.requiredKeyUsage) {
486 			TEE_Panic(0);
487 		}
488 
489 		res = TEE_GetObjectInfo1(key2, &key_info2);
490 		if (res != TEE_SUCCESS) {
491 			if (res == TEE_ERROR_CORRUPT_OBJECT)
492 				res = TEE_ERROR_CORRUPT_OBJECT_2;
493 			goto err;
494 		}
495 
496 		/* Supplied key has to meet required usage */
497 		if ((key_info2.objectUsage & operation->info.
498 		     requiredKeyUsage) != operation->info.requiredKeyUsage) {
499 			TEE_Panic(0);
500 		}
501 
502 		/*
503 		 * AES-XTS (the only multi key algorithm supported, requires the
504 		 * keys to be of equal size.
505 		 */
506 		if (operation->info.algorithm == TEE_ALG_AES_XTS &&
507 		    key_info1.keySize != key_info2.keySize)
508 			TEE_Panic(0);
509 
510 		if (operation->info.maxKeySize < key_info1.keySize)
511 			TEE_Panic(0);
512 
513 		/*
514 		 * Odd that only the size of one key should be reported while
515 		 * size of two key are used when allocating the operation.
516 		 */
517 		key_size = key_info1.keySize;
518 	}
519 
520 	TEE_ResetTransientObject(operation->key1);
521 	TEE_ResetTransientObject(operation->key2);
522 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
523 
524 	if (key1 != TEE_HANDLE_NULL) {
525 		res = TEE_CopyObjectAttributes1(operation->key1, key1);
526 		if (res != TEE_SUCCESS)
527 			goto err;
528 
529 		res = TEE_CopyObjectAttributes1(operation->key2, key2);
530 		if (res != TEE_SUCCESS) {
531 			if (res == TEE_ERROR_CORRUPT_OBJECT)
532 				res = TEE_ERROR_CORRUPT_OBJECT_2;
533 			goto err;
534 		}
535 
536 		operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
537 	}
538 
539 	operation->info.keySize = key_size;
540 
541 	goto out;
542 
543 err:
544 	if (res == TEE_ERROR_CORRUPT_OBJECT ||
545 	    res == TEE_ERROR_CORRUPT_OBJECT_2 ||
546 	    res == TEE_ERROR_STORAGE_NOT_AVAILABLE ||
547 	    res == TEE_ERROR_STORAGE_NOT_AVAILABLE_2)
548 		return res;
549 	TEE_Panic(0);
550 out:
551 	return TEE_SUCCESS;
552 }
553 
554 void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op)
555 {
556 	TEE_Result res;
557 
558 	if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL)
559 		TEE_Panic(0);
560 	if (dst_op->info.algorithm != src_op->info.algorithm)
561 		TEE_Panic(0);
562 	if (src_op->info.operationClass != TEE_OPERATION_DIGEST) {
563 		TEE_ObjectHandle key1 = TEE_HANDLE_NULL;
564 		TEE_ObjectHandle key2 = TEE_HANDLE_NULL;
565 
566 		if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) {
567 			key1 = src_op->key1;
568 			key2 = src_op->key2;
569 		}
570 
571 		if ((src_op->info.handleState &
572 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) {
573 			TEE_SetOperationKey(dst_op, key1);
574 		} else {
575 			TEE_SetOperationKey2(dst_op, key1, key2);
576 		}
577 	}
578 	dst_op->info.handleState = src_op->info.handleState;
579 	dst_op->info.keySize = src_op->info.keySize;
580 
581 	if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks ||
582 	    dst_op->block_size != src_op->block_size)
583 		TEE_Panic(0);
584 
585 	if (dst_op->buffer != NULL) {
586 		if (src_op->buffer == NULL)
587 			TEE_Panic(0);
588 
589 		memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs);
590 		dst_op->buffer_offs = src_op->buffer_offs;
591 	} else if (src_op->buffer != NULL) {
592 		TEE_Panic(0);
593 	}
594 
595 	res = utee_cryp_state_copy(dst_op->state, src_op->state);
596 	if (res != TEE_SUCCESS)
597 		TEE_Panic(res);
598 }
599 
600 /* Cryptographic Operations API - Message Digest Functions */
601 
602 void TEE_DigestUpdate(TEE_OperationHandle operation,
603 		      void *chunk, uint32_t chunkSize)
604 {
605 	TEE_Result res = TEE_ERROR_GENERIC;
606 
607 	if (operation == TEE_HANDLE_NULL ||
608 	    operation->info.operationClass != TEE_OPERATION_DIGEST)
609 		TEE_Panic(0);
610 
611 	res = utee_hash_update(operation->state, chunk, chunkSize);
612 	if (res != TEE_SUCCESS)
613 		TEE_Panic(res);
614 }
615 
616 TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk,
617 			     uint32_t chunkLen, void *hash, uint32_t *hashLen)
618 {
619 	if ((operation == TEE_HANDLE_NULL) || (!chunk && chunkLen) ||
620 	    !hash || !hashLen ||
621 	    (operation->info.operationClass != TEE_OPERATION_DIGEST))
622 		TEE_Panic(0);
623 
624 	return utee_hash_final(operation->state, chunk, chunkLen, hash,
625 			       hashLen);
626 }
627 
628 /* Cryptographic Operations API - Symmetric Cipher Functions */
629 
630 void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen)
631 {
632 	TEE_Result res;
633 
634 	if (operation == TEE_HANDLE_NULL)
635 		TEE_Panic(0);
636 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
637 		TEE_Panic(0);
638 	res = utee_cipher_init(operation->state, IV, IVLen);
639 	if (res != TEE_SUCCESS)
640 		TEE_Panic(res);
641 	operation->buffer_offs = 0;
642 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
643 }
644 
645 static TEE_Result tee_buffer_update(
646 		TEE_OperationHandle op,
647 		TEE_Result(*update_func) (uint32_t state, const void *src,
648 					  size_t slen, void *dst, uint32_t *dlen),
649 		const void *src_data, size_t src_len,
650 		void *dest_data, uint32_t *dest_len)
651 {
652 	TEE_Result res;
653 	const uint8_t *src = src_data;
654 	size_t slen = src_len;
655 	uint8_t *dst = dest_data;
656 	size_t dlen = *dest_len;
657 	size_t acc_dlen = 0;
658 	uint32_t tmp_dlen;
659 	size_t l;
660 	size_t buffer_size;
661 	size_t buffer_left;
662 
663 	if (op->buffer_two_blocks) {
664 		buffer_size = op->block_size * 2;
665 		buffer_left = 1;
666 	} else {
667 		buffer_size = op->block_size;
668 		buffer_left = 0;
669 	}
670 
671 	if (op->buffer_offs > 0) {
672 		/* Fill up complete block */
673 		if (op->buffer_offs < op->block_size)
674 			l = MIN(slen, op->block_size - op->buffer_offs);
675 		else
676 			l = MIN(slen, buffer_size - op->buffer_offs);
677 		memcpy(op->buffer + op->buffer_offs, src, l);
678 		op->buffer_offs += l;
679 		src += l;
680 		slen -= l;
681 		if ((op->buffer_offs % op->block_size) != 0)
682 			goto out;	/* Nothing left to do */
683 	}
684 
685 	/* If we can feed from buffer */
686 	if ((op->buffer_offs > 0) &&
687 	    ((op->buffer_offs + slen) >= (buffer_size + buffer_left))) {
688 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
689 				op->block_size);
690 		l = MIN(op->buffer_offs, l);
691 		tmp_dlen = dlen;
692 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
693 		if (res != TEE_SUCCESS)
694 			TEE_Panic(res);
695 		dst += tmp_dlen;
696 		dlen -= tmp_dlen;
697 		acc_dlen += tmp_dlen;
698 		op->buffer_offs -= l;
699 		if (op->buffer_offs > 0) {
700 			/*
701 			 * Slen is small enough to be contained in rest buffer.
702 			 */
703 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
704 			memcpy(op->buffer + op->buffer_offs, src, slen);
705 			op->buffer_offs += slen;
706 			goto out;	/* Nothing left to do */
707 		}
708 	}
709 
710 	if (slen >= (buffer_size + buffer_left)) {
711 		/* Buffer is empty, feed as much as possible from src */
712 		if (TEE_ALIGNMENT_IS_OK(src, uint32_t)) {
713 			l = ROUNDUP(slen - buffer_size + 1, op->block_size);
714 
715 			tmp_dlen = dlen;
716 			res = update_func(op->state, src, l, dst, &tmp_dlen);
717 			if (res != TEE_SUCCESS)
718 				TEE_Panic(res);
719 			src += l;
720 			slen -= l;
721 			dst += tmp_dlen;
722 			dlen -= tmp_dlen;
723 			acc_dlen += tmp_dlen;
724 		} else {
725 			/*
726 			 * Supplied data isn't well aligned, we're forced to
727 			 * feed through the buffer.
728 			 */
729 			while (slen >= op->block_size) {
730 				memcpy(op->buffer, src, op->block_size);
731 
732 				tmp_dlen = dlen;
733 				res =
734 				    update_func(op->state, op->buffer,
735 						op->block_size, dst, &tmp_dlen);
736 				if (res != TEE_SUCCESS)
737 					TEE_Panic(res);
738 				src += op->block_size;
739 				slen -= op->block_size;
740 				dst += tmp_dlen;
741 				dlen -= tmp_dlen;
742 				acc_dlen += tmp_dlen;
743 			}
744 		}
745 	}
746 
747 	/* Slen is small enough to be contained in buffer. */
748 	memcpy(op->buffer + op->buffer_offs, src, slen);
749 	op->buffer_offs += slen;
750 
751 out:
752 	*dest_len = acc_dlen;
753 	return TEE_SUCCESS;
754 }
755 
756 TEE_Result TEE_CipherUpdate(TEE_OperationHandle op, const void *srcData,
757 			    uint32_t srcLen, void *destData, uint32_t *destLen)
758 {
759 	size_t req_dlen;
760 
761 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
762 	    destLen == NULL || (destData == NULL && *destLen != 0))
763 		TEE_Panic(0);
764 	if (op->info.operationClass != TEE_OPERATION_CIPHER)
765 		TEE_Panic(0);
766 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
767 		TEE_Panic(0);
768 
769 	/* Calculate required dlen */
770 	req_dlen = ((op->buffer_offs + srcLen) / op->block_size) *
771 	    op->block_size;
772 	if (op->buffer_two_blocks) {
773 		if (req_dlen > op->block_size * 2)
774 			req_dlen -= op->block_size * 2;
775 		else
776 			req_dlen = 0;
777 	}
778 	/*
779 	 * Check that required destLen is big enough before starting to feed
780 	 * data to the algorithm. Errors during feeding of data are fatal as we
781 	 * can't restore sync with this API.
782 	 */
783 	if (*destLen < req_dlen) {
784 		*destLen = req_dlen;
785 		return TEE_ERROR_SHORT_BUFFER;
786 	}
787 
788 	tee_buffer_update(op, utee_cipher_update, srcData, srcLen, destData,
789 			  destLen);
790 
791 	return TEE_SUCCESS;
792 }
793 
794 TEE_Result TEE_CipherDoFinal(TEE_OperationHandle op,
795 			     const void *srcData, uint32_t srcLen, void *destData,
796 			     uint32_t *destLen)
797 {
798 	TEE_Result res;
799 	uint8_t *dst = destData;
800 	size_t acc_dlen = 0;
801 	uint32_t tmp_dlen;
802 	size_t req_dlen;
803 
804 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
805 	    destLen == NULL || (destData == NULL && *destLen != 0))
806 		TEE_Panic(0);
807 	if (op->info.operationClass != TEE_OPERATION_CIPHER)
808 		TEE_Panic(0);
809 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
810 		TEE_Panic(0);
811 
812 	/*
813 	 * Check that the final block doesn't require padding for those
814 	 * algorithms that requires client to supply padding.
815 	 */
816 	if (op->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
817 	    op->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
818 	    op->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
819 	    op->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
820 	    op->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
821 	    op->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) {
822 		if (((op->buffer_offs + srcLen) % op->block_size) != 0)
823 			return TEE_ERROR_BAD_PARAMETERS;
824 	}
825 
826 	/*
827 	 * Check that required destLen is big enough before starting to feed
828 	 * data to the algorithm. Errors during feeding of data are fatal as we
829 	 * can't restore sync with this API.
830 	 */
831 	req_dlen = op->buffer_offs + srcLen;
832 	if (*destLen < req_dlen) {
833 		*destLen = req_dlen;
834 		return TEE_ERROR_SHORT_BUFFER;
835 	}
836 
837 	tmp_dlen = *destLen - acc_dlen;
838 	tee_buffer_update(op, utee_cipher_update, srcData, srcLen, dst,
839 			  &tmp_dlen);
840 	dst += tmp_dlen;
841 	acc_dlen += tmp_dlen;
842 
843 	tmp_dlen = *destLen - acc_dlen;
844 	res = utee_cipher_final(op->state, op->buffer, op->buffer_offs,
845 				dst, &tmp_dlen);
846 	if (res != TEE_SUCCESS)
847 		TEE_Panic(res);
848 	acc_dlen += tmp_dlen;
849 
850 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
851 	*destLen = acc_dlen;
852 	return TEE_SUCCESS;
853 }
854 
855 /* Cryptographic Operations API - MAC Functions */
856 
857 void TEE_MACInit(TEE_OperationHandle operation, const void *IV, uint32_t IVLen)
858 {
859 	TEE_Result res;
860 
861 	if (operation == TEE_HANDLE_NULL)
862 		TEE_Panic(0);
863 	if (IV == NULL && IVLen != 0)
864 		TEE_Panic(0);
865 	if (operation->info.operationClass != TEE_OPERATION_MAC)
866 		TEE_Panic(0);
867 	res = utee_hash_init(operation->state, IV, IVLen);
868 	if (res != TEE_SUCCESS)
869 		TEE_Panic(res);
870 	operation->buffer_offs = 0;
871 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
872 }
873 
874 void TEE_MACUpdate(TEE_OperationHandle op, const void *chunk, uint32_t chunkSize)
875 {
876 	TEE_Result res;
877 
878 	if (op == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
879 		TEE_Panic(0);
880 	if (op->info.operationClass != TEE_OPERATION_MAC)
881 		TEE_Panic(0);
882 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
883 		TEE_Panic(0);
884 
885 	res = utee_hash_update(op->state, chunk, chunkSize);
886 	if (res != TEE_SUCCESS)
887 		TEE_Panic(res);
888 }
889 
890 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle op,
891 			       const void *message, uint32_t messageLen,
892 			       void *mac, uint32_t *macLen)
893 {
894 	TEE_Result res;
895 
896 	if (op == TEE_HANDLE_NULL || (message == NULL && messageLen != 0) ||
897 	    mac == NULL || macLen == NULL)
898 		TEE_Panic(0);
899 	if (op->info.operationClass != TEE_OPERATION_MAC)
900 		TEE_Panic(0);
901 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
902 		TEE_Panic(0);
903 
904 	res = utee_hash_final(op->state, message, messageLen, mac, macLen);
905 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
906 	return res;
907 }
908 
909 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
910 			       const void *message, uint32_t messageLen,
911 			       const void *mac, uint32_t macLen)
912 {
913 	TEE_Result res;
914 	uint8_t computed_mac[TEE_MAX_HASH_SIZE];
915 	uint32_t computed_mac_size = TEE_MAX_HASH_SIZE;
916 
917 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
918 				  &computed_mac_size);
919 	if (res != TEE_SUCCESS)
920 		return res;
921 	if (computed_mac_size != macLen)
922 		return TEE_ERROR_MAC_INVALID;
923 	if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0)
924 		return TEE_ERROR_MAC_INVALID;
925 	return TEE_SUCCESS;
926 }
927 
928 /* Cryptographic Operations API - Authenticated Encryption Functions */
929 
930 TEE_Result TEE_AEInit(TEE_OperationHandle op, const void *nonce,
931 		      uint32_t nonceLen, uint32_t tagLen, uint32_t AADLen,
932 		      uint32_t payloadLen)
933 {
934 	TEE_Result res;
935 
936 	if (op == TEE_HANDLE_NULL || nonce == NULL)
937 		TEE_Panic(0);
938 	if (op->info.operationClass != TEE_OPERATION_AE)
939 		TEE_Panic(0);
940 
941 	/*
942 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
943 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
944 	 * according to the same principle so we have to check here instead to
945 	 * be GP compliant.
946 	 */
947 	if (op->info.algorithm == TEE_ALG_AES_GCM) {
948 		/*
949 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
950 		 */
951 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0))
952 			return TEE_ERROR_NOT_SUPPORTED;
953 	}
954 
955 	res = utee_authenc_init(op->state, nonce, nonceLen, tagLen / 8, AADLen,
956 				payloadLen);
957 	if (res != TEE_SUCCESS) {
958 		if (res != TEE_ERROR_NOT_SUPPORTED)
959 			TEE_Panic(res);
960 		return res;
961 	}
962 	op->ae_tag_len = tagLen / 8;
963 
964 	op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
965 	return TEE_SUCCESS;
966 }
967 
968 void TEE_AEUpdateAAD(TEE_OperationHandle op, const void *AADdata,
969 		     uint32_t AADdataLen)
970 {
971 	TEE_Result res;
972 
973 	if (op == TEE_HANDLE_NULL || (AADdata == NULL && AADdataLen != 0))
974 		TEE_Panic(0);
975 	if (op->info.operationClass != TEE_OPERATION_AE)
976 		TEE_Panic(0);
977 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
978 		TEE_Panic(0);
979 
980 	res = utee_authenc_update_aad(op->state, AADdata, AADdataLen);
981 	if (res != TEE_SUCCESS)
982 		TEE_Panic(res);
983 }
984 
985 TEE_Result TEE_AEUpdate(TEE_OperationHandle op, const void *srcData,
986 			uint32_t srcLen, void *destData, uint32_t *destLen)
987 {
988 	size_t req_dlen;
989 
990 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
991 	    destLen == NULL || (destData == NULL && *destLen != 0))
992 		TEE_Panic(0);
993 	if (op->info.operationClass != TEE_OPERATION_AE)
994 		TEE_Panic(0);
995 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
996 		TEE_Panic(0);
997 
998 	/*
999 	 * Check that required destLen is big enough before starting to feed
1000 	 * data to the algorithm. Errors during feeding of data are fatal as we
1001 	 * can't restore sync with this API.
1002 	 */
1003 	req_dlen = ROUNDDOWN(op->buffer_offs + srcLen, op->block_size);
1004 	if (*destLen < req_dlen) {
1005 		*destLen = req_dlen;
1006 		return TEE_ERROR_SHORT_BUFFER;
1007 	}
1008 
1009 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
1010 			  destData, destLen);
1011 
1012 	return TEE_SUCCESS;
1013 }
1014 
1015 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle op,
1016 			      const void *srcData, uint32_t srcLen,
1017 			      void *destData, uint32_t *destLen, void *tag,
1018 			      uint32_t *tagLen)
1019 {
1020 	TEE_Result res;
1021 	uint8_t *dst = destData;
1022 	size_t acc_dlen = 0;
1023 	uint32_t tmp_dlen;
1024 	size_t req_dlen;
1025 
1026 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1027 	    destLen == NULL || (destData == NULL && *destLen != 0) ||
1028 	    tag == NULL || tagLen == NULL)
1029 		TEE_Panic(0);
1030 	if (op->info.operationClass != TEE_OPERATION_AE)
1031 		TEE_Panic(0);
1032 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1033 		TEE_Panic(0);
1034 
1035 	/*
1036 	 * Check that required destLen is big enough before starting to feed
1037 	 * data to the algorithm. Errors during feeding of data are fatal as we
1038 	 * can't restore sync with this API.
1039 	 */
1040 	req_dlen = op->buffer_offs + srcLen;
1041 	if (*destLen < req_dlen) {
1042 		*destLen = req_dlen;
1043 		return TEE_ERROR_SHORT_BUFFER;
1044 	}
1045 
1046 	/*
1047 	 * Need to check this before update_payload since sync would be lost if
1048 	 * we return short buffer after that.
1049 	 */
1050 	if (*tagLen < op->ae_tag_len) {
1051 		*tagLen = op->ae_tag_len;
1052 		return TEE_ERROR_SHORT_BUFFER;
1053 	}
1054 
1055 	tmp_dlen = *destLen - acc_dlen;
1056 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
1057 			  dst, &tmp_dlen);
1058 	dst += tmp_dlen;
1059 	acc_dlen += tmp_dlen;
1060 
1061 	tmp_dlen = *destLen - acc_dlen;
1062 	res =
1063 	    utee_authenc_enc_final(op->state, op->buffer, op->buffer_offs, dst,
1064 				   &tmp_dlen, tag, tagLen);
1065 	if (res != TEE_SUCCESS)
1066 		TEE_Panic(res);
1067 	acc_dlen += tmp_dlen;
1068 
1069 	*destLen = acc_dlen;
1070 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1071 
1072 	return res;
1073 }
1074 
1075 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle op,
1076 			      const void *srcData, uint32_t srcLen,
1077 			      void *destData, uint32_t *destLen, const void *tag,
1078 			      uint32_t tagLen)
1079 {
1080 	TEE_Result res;
1081 	uint8_t *dst = destData;
1082 	size_t acc_dlen = 0;
1083 	uint32_t tmp_dlen;
1084 	size_t req_dlen;
1085 
1086 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1087 	    destLen == NULL || (destData == NULL && *destLen != 0) ||
1088 	    (tag == NULL && tagLen != 0))
1089 		TEE_Panic(0);
1090 	if (op->info.operationClass != TEE_OPERATION_AE)
1091 		TEE_Panic(0);
1092 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1093 		TEE_Panic(0);
1094 
1095 	/*
1096 	 * Check that required destLen is big enough before starting to feed
1097 	 * data to the algorithm. Errors during feeding of data are fatal as we
1098 	 * can't restore sync with this API.
1099 	 */
1100 	req_dlen = op->buffer_offs + srcLen;
1101 	if (*destLen < req_dlen) {
1102 		*destLen = req_dlen;
1103 		return TEE_ERROR_SHORT_BUFFER;
1104 	}
1105 
1106 	tmp_dlen = *destLen - acc_dlen;
1107 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
1108 			  dst, &tmp_dlen);
1109 	dst += tmp_dlen;
1110 	acc_dlen += tmp_dlen;
1111 
1112 	tmp_dlen = *destLen - acc_dlen;
1113 	res =
1114 	    utee_authenc_dec_final(op->state, op->buffer, op->buffer_offs, dst,
1115 				   &tmp_dlen, tag, tagLen);
1116 	if (res != TEE_SUCCESS && res != TEE_ERROR_MAC_INVALID)
1117 		TEE_Panic(res);
1118 	/* Supplied tagLen should match what we initiated with */
1119 	if (tagLen != op->ae_tag_len)
1120 		res = TEE_ERROR_MAC_INVALID;
1121 
1122 	acc_dlen += tmp_dlen;
1123 
1124 	*destLen = acc_dlen;
1125 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
1126 
1127 	return res;
1128 }
1129 
1130 /* Cryptographic Operations API - Asymmetric Functions */
1131 
1132 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle op,
1133 				 const TEE_Attribute *params,
1134 				 uint32_t paramCount, const void *srcData,
1135 				 uint32_t srcLen, void *destData,
1136 				 uint32_t *destLen)
1137 {
1138 	TEE_Result res;
1139 
1140 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1141 	    destLen == NULL || (destData == NULL && *destLen != 0))
1142 		TEE_Panic(0);
1143 	if (paramCount != 0 && params == NULL)
1144 		TEE_Panic(0);
1145 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1146 		TEE_Panic(0);
1147 	if (op->info.mode != TEE_MODE_ENCRYPT)
1148 		TEE_Panic(0);
1149 
1150 	res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen,
1151 				 destData, destLen);
1152 	if (res != TEE_SUCCESS &&
1153 	    res != TEE_ERROR_SHORT_BUFFER &&
1154 	    res != TEE_ERROR_BAD_PARAMETERS)
1155 		TEE_Panic(res);
1156 	return res;
1157 }
1158 
1159 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle op,
1160 				 const TEE_Attribute *params,
1161 				 uint32_t paramCount, const void *srcData,
1162 				 uint32_t srcLen, void *destData,
1163 				 uint32_t *destLen)
1164 {
1165 	TEE_Result res;
1166 
1167 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1168 	    destLen == NULL || (destData == NULL && *destLen != 0))
1169 		TEE_Panic(0);
1170 	if (paramCount != 0 && params == NULL)
1171 		TEE_Panic(0);
1172 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1173 		TEE_Panic(0);
1174 	if (op->info.mode != TEE_MODE_DECRYPT)
1175 		TEE_Panic(0);
1176 
1177 	res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen,
1178 				 destData, destLen);
1179 	if (res != TEE_SUCCESS &&
1180 	    res != TEE_ERROR_SHORT_BUFFER &&
1181 	    res != TEE_ERROR_BAD_PARAMETERS)
1182 		TEE_Panic(res);
1183 	return res;
1184 }
1185 
1186 TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle op,
1187 				    const TEE_Attribute *params,
1188 				    uint32_t paramCount, const void *digest,
1189 				    uint32_t digestLen, void *signature,
1190 				    uint32_t *signatureLen)
1191 {
1192 	TEE_Result res;
1193 
1194 	if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) ||
1195 	    signature == NULL || signatureLen == NULL)
1196 		TEE_Panic(0);
1197 	if (paramCount != 0 && params == NULL)
1198 		TEE_Panic(0);
1199 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1200 		TEE_Panic(0);
1201 	if (op->info.mode != TEE_MODE_SIGN)
1202 		TEE_Panic(0);
1203 
1204 	res =
1205 	    utee_asymm_operate(op->state, params, paramCount, digest, digestLen,
1206 			       signature, signatureLen);
1207 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
1208 		TEE_Panic(res);
1209 	return res;
1210 }
1211 
1212 TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle op,
1213 				      const TEE_Attribute *params,
1214 				      uint32_t paramCount, const void *digest,
1215 				      uint32_t digestLen, const void *signature,
1216 				      uint32_t signatureLen)
1217 {
1218 	TEE_Result res;
1219 
1220 	if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) ||
1221 	    (signature == NULL && signatureLen != 0))
1222 		TEE_Panic(0);
1223 	if (paramCount != 0 && params == NULL)
1224 		TEE_Panic(0);
1225 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1226 		TEE_Panic(0);
1227 	if (op->info.mode != TEE_MODE_VERIFY)
1228 		TEE_Panic(0);
1229 
1230 	res =
1231 	    utee_asymm_verify(op->state, params, paramCount, digest, digestLen,
1232 			      signature, signatureLen);
1233 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
1234 		TEE_Panic(res);
1235 	return res;
1236 }
1237 
1238 /* Cryptographic Operations API - Key Derivation Functions */
1239 
1240 void TEE_DeriveKey(TEE_OperationHandle operation,
1241 		   const TEE_Attribute *params, uint32_t paramCount,
1242 		   TEE_ObjectHandle derivedKey)
1243 {
1244 	TEE_Result res;
1245 	TEE_ObjectInfo key_info;
1246 
1247 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
1248 		TEE_Panic(0);
1249 	if (paramCount != 0 && params == NULL)
1250 		TEE_Panic(0);
1251 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
1252 			TEE_OPERATION_KEY_DERIVATION)
1253 		TEE_Panic(0);
1254 
1255 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
1256 		TEE_Panic(0);
1257 	if (operation->info.mode != TEE_MODE_DERIVE)
1258 		TEE_Panic(0);
1259 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
1260 		TEE_Panic(0);
1261 
1262 	res = utee_cryp_obj_get_info((uint32_t) derivedKey, &key_info);
1263 	if (res != TEE_SUCCESS)
1264 		TEE_Panic(0);
1265 
1266 	if (key_info.objectType != TEE_TYPE_GENERIC_SECRET)
1267 		TEE_Panic(0);
1268 	if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1269 		TEE_Panic(0);
1270 
1271 	res = utee_cryp_derive_key(operation->state, params, paramCount,
1272 				   (uint32_t) derivedKey);
1273 	if (res != TEE_SUCCESS)
1274 		TEE_Panic(res);
1275 }
1276 
1277 /* Cryptographic Operations API - Random Number Generation Functions */
1278 
1279 void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen)
1280 {
1281 	TEE_Result res;
1282 
1283 	res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
1284 	if (res != TEE_SUCCESS)
1285 		TEE_Panic(res);
1286 }
1287