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