xref: /optee_os/lib/libutee/tee_api_operations.c (revision fc26c92ac5472c31f237b046a09480b830faecc4)
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 	uint32_t key_size = 0;
316 
317 	if (operation == TEE_HANDLE_NULL)
318 		TEE_Panic(0);
319 
320 	/* No key for digests */
321 	if (operation->info.operationClass == TEE_OPERATION_DIGEST)
322 		TEE_Panic(0);
323 
324 	/* Two keys expected */
325 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) !=
326 	    0)
327 		TEE_Panic(0);
328 
329 	if (key != TEE_HANDLE_NULL) {
330 		TEE_ObjectInfo key_info;
331 
332 		TEE_GetObjectInfo(key, &key_info);
333 		/* Supplied key has to meet required usage */
334 		if ((key_info.objectUsage & operation->info.requiredKeyUsage) !=
335 		    operation->info.requiredKeyUsage) {
336 			TEE_Panic(0);
337 		}
338 
339 		if (operation->info.maxKeySize < key_info.objectSize)
340 			TEE_Panic(0);
341 
342 		key_size = key_info.objectSize;
343 	}
344 
345 	TEE_ResetTransientObject(operation->key1);
346 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
347 
348 	if (key != TEE_HANDLE_NULL) {
349 		TEE_CopyObjectAttributes(operation->key1, key);
350 		operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
351 	}
352 
353 	operation->info.keySize = key_size;
354 
355 	return TEE_SUCCESS;
356 }
357 
358 TEE_Result TEE_SetOperationKey2(TEE_OperationHandle operation,
359 				TEE_ObjectHandle key1, TEE_ObjectHandle key2)
360 {
361 	uint32_t key_size = 0;
362 
363 	if (operation == TEE_HANDLE_NULL)
364 		TEE_Panic(0);
365 
366 	/* Two keys not expected */
367 	if ((operation->info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) ==
368 	    0)
369 		TEE_Panic(0);
370 
371 	/* Either both keys are NULL or both are not NULL */
372 	if ((key1 == TEE_HANDLE_NULL || key2 == TEE_HANDLE_NULL) &&
373 	    key1 != key2)
374 		TEE_Panic(0);
375 
376 	if (key1 != TEE_HANDLE_NULL) {
377 		TEE_ObjectInfo key_info1;
378 		TEE_ObjectInfo key_info2;
379 
380 		TEE_GetObjectInfo(key1, &key_info1);
381 		/* Supplied key has to meet required usage */
382 		if ((key_info1.objectUsage & operation->info.
383 		     requiredKeyUsage) != operation->info.requiredKeyUsage) {
384 			TEE_Panic(0);
385 		}
386 
387 		TEE_GetObjectInfo(key2, &key_info2);
388 		/* Supplied key has to meet required usage */
389 		if ((key_info2.objectUsage & operation->info.
390 		     requiredKeyUsage) != operation->info.requiredKeyUsage) {
391 			TEE_Panic(0);
392 		}
393 
394 		/*
395 		 * AES-XTS (the only multi key algorithm supported, requires the
396 		 * keys to be of equal size.
397 		 */
398 		if (operation->info.algorithm == TEE_ALG_AES_XTS &&
399 		    key_info1.objectSize != key_info2.objectSize)
400 			TEE_Panic(0);
401 
402 		if (operation->info.maxKeySize < key_info1.objectSize)
403 			TEE_Panic(0);
404 
405 		/*
406 		 * Odd that only the size of one key should be reported while
407 		 * size of two key are used when allocating the operation.
408 		 */
409 		key_size = key_info1.objectSize;
410 	}
411 
412 	TEE_ResetTransientObject(operation->key1);
413 	TEE_ResetTransientObject(operation->key2);
414 	operation->info.handleState &= ~TEE_HANDLE_FLAG_KEY_SET;
415 
416 	if (key1 != TEE_HANDLE_NULL) {
417 		TEE_CopyObjectAttributes(operation->key1, key1);
418 		TEE_CopyObjectAttributes(operation->key2, key2);
419 		operation->info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
420 	}
421 
422 	operation->info.keySize = key_size;
423 
424 	return TEE_SUCCESS;
425 }
426 
427 void TEE_CopyOperation(TEE_OperationHandle dst_op, TEE_OperationHandle src_op)
428 {
429 	TEE_Result res;
430 
431 	if (dst_op == TEE_HANDLE_NULL || src_op == TEE_HANDLE_NULL)
432 		TEE_Panic(0);
433 	if (dst_op->info.algorithm != src_op->info.algorithm)
434 		TEE_Panic(0);
435 	if (src_op->info.operationClass != TEE_OPERATION_DIGEST) {
436 		TEE_ObjectHandle key1 = TEE_HANDLE_NULL;
437 		TEE_ObjectHandle key2 = TEE_HANDLE_NULL;
438 
439 		if (src_op->info.handleState & TEE_HANDLE_FLAG_KEY_SET) {
440 			key1 = src_op->key1;
441 			key2 = src_op->key2;
442 		}
443 
444 		if ((src_op->info.handleState &
445 		     TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0) {
446 			TEE_SetOperationKey(dst_op, key1);
447 		} else {
448 			TEE_SetOperationKey2(dst_op, key1, key2);
449 		}
450 	}
451 	dst_op->info.handleState = src_op->info.handleState;
452 	dst_op->info.keySize = src_op->info.keySize;
453 
454 	if (dst_op->buffer_two_blocks != src_op->buffer_two_blocks ||
455 	    dst_op->block_size != src_op->block_size)
456 		TEE_Panic(0);
457 
458 	if (dst_op->buffer != NULL) {
459 		if (src_op->buffer == NULL)
460 			TEE_Panic(0);
461 
462 		memcpy(dst_op->buffer, src_op->buffer, src_op->buffer_offs);
463 		dst_op->buffer_offs = src_op->buffer_offs;
464 	} else if (src_op->buffer != NULL) {
465 		TEE_Panic(0);
466 	}
467 
468 	res = utee_cryp_state_copy(dst_op->state, src_op->state);
469 	if (res != TEE_SUCCESS)
470 		TEE_Panic(res);
471 }
472 
473 /* Cryptographic Operations API - Message Digest Functions */
474 
475 void TEE_DigestUpdate(TEE_OperationHandle operation,
476 		      void *chunk, size_t chunkSize)
477 {
478 	TEE_Result res = TEE_ERROR_GENERIC;
479 
480 	if (operation == TEE_HANDLE_NULL ||
481 	    operation->info.operationClass != TEE_OPERATION_DIGEST)
482 		TEE_Panic(0);
483 
484 	res = utee_hash_update(operation->state, chunk, chunkSize);
485 	if (res != TEE_SUCCESS)
486 		TEE_Panic(res);
487 }
488 
489 TEE_Result TEE_DigestDoFinal(TEE_OperationHandle operation, const void *chunk,
490 			     size_t chunkLen, void *hash, size_t *hashLen)
491 {
492 	if ((operation == TEE_HANDLE_NULL) || (!chunk && chunkLen) ||
493 	    !hash || !hashLen ||
494 	    (operation->info.operationClass != TEE_OPERATION_DIGEST))
495 		TEE_Panic(0);
496 
497 	return utee_hash_final(operation->state, chunk, chunkLen, hash,
498 			       hashLen);
499 }
500 
501 /* Cryptographic Operations API - Symmetric Cipher Functions */
502 
503 void TEE_CipherInit(TEE_OperationHandle operation, const void *IV, size_t IVLen)
504 {
505 	TEE_Result res;
506 
507 	if (operation == TEE_HANDLE_NULL)
508 		TEE_Panic(0);
509 	if (operation->info.operationClass != TEE_OPERATION_CIPHER)
510 		TEE_Panic(0);
511 	res = utee_cipher_init(operation->state, IV, IVLen);
512 	if (res != TEE_SUCCESS)
513 		TEE_Panic(res);
514 	operation->buffer_offs = 0;
515 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
516 }
517 
518 static TEE_Result tee_buffer_update(
519 		TEE_OperationHandle op,
520 		TEE_Result(*update_func) (uint32_t state, const void *src,
521 					  size_t slen, void *dst, size_t *dlen),
522 		const void *src_data, size_t src_len,
523 		void *dest_data, size_t *dest_len)
524 {
525 	TEE_Result res;
526 	const uint8_t *src = src_data;
527 	size_t slen = src_len;
528 	uint8_t *dst = dest_data;
529 	size_t dlen = *dest_len;
530 	size_t acc_dlen = 0;
531 	size_t tmp_dlen;
532 	size_t l;
533 	size_t buffer_size;
534 
535 	if (op->buffer_two_blocks)
536 		buffer_size = op->block_size * 2;
537 	else
538 		buffer_size = op->block_size;
539 
540 	if (op->buffer_offs > 0) {
541 		/* Fill up complete block */
542 		if (op->buffer_offs < op->block_size)
543 			l = MIN(slen, op->block_size - op->buffer_offs);
544 		else
545 			l = MIN(slen, buffer_size - op->buffer_offs);
546 		memcpy(op->buffer + op->buffer_offs, src, l);
547 		op->buffer_offs += l;
548 		src += l;
549 		slen -= l;
550 		if ((op->buffer_offs % op->block_size) != 0)
551 			goto out;	/* Nothing left to do */
552 	}
553 
554 	/* If we can feed from buffer */
555 	if (op->buffer_offs > 0 && (op->buffer_offs + slen) > buffer_size) {
556 		l = ROUNDUP(op->buffer_offs + slen - buffer_size,
557 				op->block_size);
558 		l = MIN(op->buffer_offs, l);
559 		tmp_dlen = dlen;
560 		res = update_func(op->state, op->buffer, l, dst, &tmp_dlen);
561 		if (res != TEE_SUCCESS)
562 			TEE_Panic(res);
563 		dst += tmp_dlen;
564 		dlen -= tmp_dlen;
565 		acc_dlen += tmp_dlen;
566 		op->buffer_offs -= l;
567 		if (op->buffer_offs > 0) {
568 			/*
569 			 * Slen is small enough to be contained in rest buffer.
570 			 */
571 			memcpy(op->buffer, op->buffer + l, buffer_size - l);
572 			memcpy(op->buffer + op->buffer_offs, src, slen);
573 			op->buffer_offs += slen;
574 			goto out;	/* Nothing left to do */
575 		}
576 	}
577 
578 	if (slen > buffer_size) {
579 		/* Buffer is empty, feed as much as possible from src */
580 		if (TEE_ALIGNMENT_IS_OK(src, uint32_t)) {
581 			l = ROUNDUP(slen - buffer_size + 1, op->block_size);
582 
583 			tmp_dlen = dlen;
584 			res = update_func(op->state, src, l, dst, &tmp_dlen);
585 			if (res != TEE_SUCCESS)
586 				TEE_Panic(res);
587 			src += l;
588 			slen -= l;
589 			dst += tmp_dlen;
590 			dlen -= tmp_dlen;
591 			acc_dlen += tmp_dlen;
592 		} else {
593 			/*
594 			 * Supplied data isn't well aligned, we're forced to
595 			 * feed through the buffer.
596 			 */
597 			while (slen >= op->block_size) {
598 				memcpy(op->buffer, src, op->block_size);
599 
600 				tmp_dlen = dlen;
601 				res =
602 				    update_func(op->state, op->buffer,
603 						op->block_size, dst, &tmp_dlen);
604 				if (res != TEE_SUCCESS)
605 					TEE_Panic(res);
606 				src += op->block_size;
607 				slen -= op->block_size;
608 				dst += tmp_dlen;
609 				dlen -= tmp_dlen;
610 				acc_dlen += tmp_dlen;
611 			}
612 		}
613 	}
614 
615 	/* Slen is small enough to be contained in buffer. */
616 	memcpy(op->buffer + op->buffer_offs, src, slen);
617 	op->buffer_offs += slen;
618 
619 out:
620 	*dest_len = acc_dlen;
621 	return TEE_SUCCESS;
622 }
623 
624 TEE_Result TEE_CipherUpdate(TEE_OperationHandle op, const void *srcData,
625 			    size_t srcLen, void *destData, size_t *destLen)
626 {
627 	size_t req_dlen;
628 
629 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
630 	    destLen == NULL || (destData == NULL && *destLen != 0))
631 		TEE_Panic(0);
632 	if (op->info.operationClass != TEE_OPERATION_CIPHER)
633 		TEE_Panic(0);
634 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
635 		TEE_Panic(0);
636 
637 	/* Calculate required dlen */
638 	req_dlen = ((op->buffer_offs + srcLen) / op->block_size) *
639 	    op->block_size;
640 	if (op->buffer_two_blocks) {
641 		if (req_dlen > op->block_size * 2)
642 			req_dlen -= op->block_size * 2;
643 		else
644 			req_dlen = 0;
645 	}
646 	/*
647 	 * Check that required destLen is big enough before starting to feed
648 	 * data to the algorithm. Errors during feeding of data are fatal as we
649 	 * can't restore sync with this API.
650 	 */
651 	if (*destLen < req_dlen) {
652 		*destLen = req_dlen;
653 		return TEE_ERROR_SHORT_BUFFER;
654 	}
655 
656 	tee_buffer_update(op, utee_cipher_update, srcData, srcLen, destData,
657 			  destLen);
658 
659 	return TEE_SUCCESS;
660 }
661 
662 TEE_Result TEE_CipherDoFinal(TEE_OperationHandle op,
663 			     const void *srcData, size_t srcLen, void *destData,
664 			     size_t *destLen)
665 {
666 	TEE_Result res;
667 	uint8_t *dst = destData;
668 	size_t acc_dlen = 0;
669 	size_t tmp_dlen;
670 	size_t req_dlen;
671 
672 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
673 	    destLen == NULL || (destData == NULL && *destLen != 0))
674 		TEE_Panic(0);
675 	if (op->info.operationClass != TEE_OPERATION_CIPHER)
676 		TEE_Panic(0);
677 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
678 		TEE_Panic(0);
679 
680 	/*
681 	 * Check that the final block doesn't require padding for those
682 	 * algorithms that requires client to supply padding.
683 	 */
684 	if (op->info.algorithm == TEE_ALG_AES_ECB_NOPAD ||
685 	    op->info.algorithm == TEE_ALG_AES_CBC_NOPAD ||
686 	    op->info.algorithm == TEE_ALG_DES_ECB_NOPAD ||
687 	    op->info.algorithm == TEE_ALG_DES_CBC_NOPAD ||
688 	    op->info.algorithm == TEE_ALG_DES3_ECB_NOPAD ||
689 	    op->info.algorithm == TEE_ALG_DES3_CBC_NOPAD) {
690 		if (((op->buffer_offs + srcLen) % op->block_size) != 0)
691 			return TEE_ERROR_BAD_PARAMETERS;
692 	}
693 
694 	/*
695 	 * Check that required destLen is big enough before starting to feed
696 	 * data to the algorithm. Errors during feeding of data are fatal as we
697 	 * can't restore sync with this API.
698 	 */
699 	req_dlen = op->buffer_offs + srcLen;
700 	if (*destLen < req_dlen) {
701 		*destLen = req_dlen;
702 		return TEE_ERROR_SHORT_BUFFER;
703 	}
704 
705 	tmp_dlen = *destLen - acc_dlen;
706 	tee_buffer_update(op, utee_cipher_update, srcData, srcLen, dst,
707 			  &tmp_dlen);
708 	dst += tmp_dlen;
709 	acc_dlen += tmp_dlen;
710 
711 	tmp_dlen = *destLen - acc_dlen;
712 	res = utee_cipher_final(op->state, op->buffer, op->buffer_offs,
713 				dst, &tmp_dlen);
714 	if (res != TEE_SUCCESS)
715 		TEE_Panic(res);
716 	acc_dlen += tmp_dlen;
717 
718 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
719 	*destLen = acc_dlen;
720 	return TEE_SUCCESS;
721 }
722 
723 /* Cryptographic Operations API - MAC Functions */
724 
725 void TEE_MACInit(TEE_OperationHandle operation, const void *IV, size_t IVLen)
726 {
727 	TEE_Result res;
728 
729 	if (operation == TEE_HANDLE_NULL)
730 		TEE_Panic(0);
731 	if (IV == NULL && IVLen != 0)
732 		TEE_Panic(0);
733 	if (operation->info.operationClass != TEE_OPERATION_MAC)
734 		TEE_Panic(0);
735 	res = utee_hash_init(operation->state, IV, IVLen);
736 	if (res != TEE_SUCCESS)
737 		TEE_Panic(res);
738 	operation->buffer_offs = 0;
739 	operation->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
740 }
741 
742 void TEE_MACUpdate(TEE_OperationHandle op, const void *chunk, size_t chunkSize)
743 {
744 	TEE_Result res;
745 
746 	if (op == TEE_HANDLE_NULL || (chunk == NULL && chunkSize != 0))
747 		TEE_Panic(0);
748 	if (op->info.operationClass != TEE_OPERATION_MAC)
749 		TEE_Panic(0);
750 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
751 		TEE_Panic(0);
752 
753 	res = utee_hash_update(op->state, chunk, chunkSize);
754 	if (res != TEE_SUCCESS)
755 		TEE_Panic(res);
756 }
757 
758 TEE_Result TEE_MACComputeFinal(TEE_OperationHandle op,
759 			       const void *message, size_t messageLen,
760 			       void *mac, size_t *macLen)
761 {
762 	TEE_Result res;
763 
764 	if (op == TEE_HANDLE_NULL || (message == NULL && messageLen != 0) ||
765 	    mac == NULL || macLen == NULL)
766 		TEE_Panic(0);
767 	if (op->info.operationClass != TEE_OPERATION_MAC)
768 		TEE_Panic(0);
769 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
770 		TEE_Panic(0);
771 
772 	res = utee_hash_final(op->state, message, messageLen, mac, macLen);
773 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
774 	return res;
775 }
776 
777 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation,
778 			       const void *message, size_t messageLen,
779 			       const void *mac, size_t macLen)
780 {
781 	TEE_Result res;
782 	uint8_t computed_mac[TEE_MAX_HASH_SIZE];
783 	size_t computed_mac_size = TEE_MAX_HASH_SIZE;
784 
785 	res = TEE_MACComputeFinal(operation, message, messageLen, computed_mac,
786 				  &computed_mac_size);
787 	if (res != TEE_SUCCESS)
788 		return res;
789 	if (computed_mac_size != macLen)
790 		return TEE_ERROR_MAC_INVALID;
791 	if (buf_compare_ct(mac, computed_mac, computed_mac_size) != 0)
792 		return TEE_ERROR_MAC_INVALID;
793 	return TEE_SUCCESS;
794 }
795 
796 /* Cryptographic Operations API - Authenticated Encryption Functions */
797 
798 TEE_Result TEE_AEInit(TEE_OperationHandle op, const void *nonce,
799 		      size_t nonceLen, uint32_t tagLen, uint32_t AADLen,
800 		      uint32_t payloadLen)
801 {
802 	TEE_Result res;
803 
804 	if (op == TEE_HANDLE_NULL || nonce == NULL)
805 		TEE_Panic(0);
806 	if (op->info.operationClass != TEE_OPERATION_AE)
807 		TEE_Panic(0);
808 
809 	/*
810 	 * AES-CCM tag len is specified by AES-CCM spec and handled in TEE Core
811 	 * in the implementation. But AES-GCM spec doesn't specify the tag len
812 	 * according to the same principle so we have to check here instead to
813 	 * be GP compliant.
814 	 */
815 	if (op->info.algorithm == TEE_ALG_AES_GCM) {
816 		/*
817 		 * From GP spec: For AES-GCM, can be 128, 120, 112, 104, or 96
818 		 */
819 		if (tagLen < 96 || tagLen > 128 || (tagLen % 8 != 0))
820 			return TEE_ERROR_NOT_SUPPORTED;
821 	}
822 
823 	res = utee_authenc_init(op->state, nonce, nonceLen, tagLen / 8, AADLen,
824 				payloadLen);
825 	if (res != TEE_SUCCESS) {
826 		if (res != TEE_ERROR_NOT_SUPPORTED)
827 			TEE_Panic(res);
828 		return res;
829 	}
830 	op->ae_tag_len = tagLen / 8;
831 
832 	op->info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
833 	return TEE_SUCCESS;
834 }
835 
836 void TEE_AEUpdateAAD(TEE_OperationHandle op, const void *AADdata,
837 		     size_t AADdataLen)
838 {
839 	TEE_Result res;
840 
841 	if (op == TEE_HANDLE_NULL || (AADdata == NULL && AADdataLen != 0))
842 		TEE_Panic(0);
843 	if (op->info.operationClass != TEE_OPERATION_AE)
844 		TEE_Panic(0);
845 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
846 		TEE_Panic(0);
847 
848 	res = utee_authenc_update_aad(op->state, AADdata, AADdataLen);
849 	if (res != TEE_SUCCESS)
850 		TEE_Panic(res);
851 }
852 
853 TEE_Result TEE_AEUpdate(TEE_OperationHandle op, const void *srcData,
854 			size_t srcLen, void *destData, size_t *destLen)
855 {
856 	size_t req_dlen;
857 
858 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
859 	    destLen == NULL || (destData == NULL && *destLen != 0))
860 		TEE_Panic(0);
861 	if (op->info.operationClass != TEE_OPERATION_AE)
862 		TEE_Panic(0);
863 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
864 		TEE_Panic(0);
865 
866 	/*
867 	 * Check that required destLen is big enough before starting to feed
868 	 * data to the algorithm. Errors during feeding of data are fatal as we
869 	 * can't restore sync with this API.
870 	 */
871 	req_dlen = ROUNDDOWN(op->buffer_offs + srcLen, op->block_size);
872 	if (*destLen < req_dlen) {
873 		*destLen = req_dlen;
874 		return TEE_ERROR_SHORT_BUFFER;
875 	}
876 
877 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
878 			  destData, destLen);
879 
880 	return TEE_SUCCESS;
881 }
882 
883 TEE_Result TEE_AEEncryptFinal(TEE_OperationHandle op,
884 			      const void *srcData, size_t srcLen,
885 			      void *destData, size_t *destLen, void *tag,
886 			      size_t *tagLen)
887 {
888 	TEE_Result res;
889 	uint8_t *dst = destData;
890 	size_t acc_dlen = 0;
891 	size_t tmp_dlen;
892 	size_t req_dlen;
893 
894 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
895 	    destLen == NULL || (destData == NULL && *destLen != 0) ||
896 	    tag == NULL || tagLen == NULL)
897 		TEE_Panic(0);
898 	if (op->info.operationClass != TEE_OPERATION_AE)
899 		TEE_Panic(0);
900 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
901 		TEE_Panic(0);
902 
903 	/*
904 	 * Check that required destLen is big enough before starting to feed
905 	 * data to the algorithm. Errors during feeding of data are fatal as we
906 	 * can't restore sync with this API.
907 	 */
908 	req_dlen = op->buffer_offs + srcLen;
909 	if (*destLen < req_dlen) {
910 		*destLen = req_dlen;
911 		return TEE_ERROR_SHORT_BUFFER;
912 	}
913 
914 	/*
915 	 * Need to check this before update_payload since sync would be lost if
916 	 * we return short buffer after that.
917 	 */
918 	if (*tagLen < op->ae_tag_len) {
919 		*tagLen = op->ae_tag_len;
920 		return TEE_ERROR_SHORT_BUFFER;
921 	}
922 
923 	tmp_dlen = *destLen - acc_dlen;
924 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
925 			  dst, &tmp_dlen);
926 	dst += tmp_dlen;
927 	acc_dlen += tmp_dlen;
928 
929 	tmp_dlen = *destLen - acc_dlen;
930 	res =
931 	    utee_authenc_enc_final(op->state, op->buffer, op->buffer_offs, dst,
932 				   &tmp_dlen, tag, tagLen);
933 	if (res != TEE_SUCCESS)
934 		TEE_Panic(res);
935 	acc_dlen += tmp_dlen;
936 
937 	*destLen = acc_dlen;
938 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
939 
940 	return res;
941 }
942 
943 TEE_Result TEE_AEDecryptFinal(TEE_OperationHandle op,
944 			      const void *srcData, size_t srcLen,
945 			      void *destData, size_t *destLen, const void *tag,
946 			      size_t tagLen)
947 {
948 	TEE_Result res;
949 	uint8_t *dst = destData;
950 	size_t acc_dlen = 0;
951 	size_t tmp_dlen;
952 	size_t req_dlen;
953 
954 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
955 	    destLen == NULL || (destData == NULL && *destLen != 0) ||
956 	    (tag == NULL && tagLen != 0))
957 		TEE_Panic(0);
958 	if (op->info.operationClass != TEE_OPERATION_AE)
959 		TEE_Panic(0);
960 	if ((op->info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0)
961 		TEE_Panic(0);
962 
963 	/*
964 	 * Check that required destLen is big enough before starting to feed
965 	 * data to the algorithm. Errors during feeding of data are fatal as we
966 	 * can't restore sync with this API.
967 	 */
968 	req_dlen = op->buffer_offs + srcLen;
969 	if (*destLen < req_dlen) {
970 		*destLen = req_dlen;
971 		return TEE_ERROR_SHORT_BUFFER;
972 	}
973 
974 	tmp_dlen = *destLen - acc_dlen;
975 	tee_buffer_update(op, utee_authenc_update_payload, srcData, srcLen,
976 			  dst, &tmp_dlen);
977 	dst += tmp_dlen;
978 	acc_dlen += tmp_dlen;
979 
980 	tmp_dlen = *destLen - acc_dlen;
981 	res =
982 	    utee_authenc_dec_final(op->state, op->buffer, op->buffer_offs, dst,
983 				   &tmp_dlen, tag, tagLen);
984 	if (res != TEE_SUCCESS && res != TEE_ERROR_MAC_INVALID)
985 		TEE_Panic(res);
986 	/* Supplied tagLen should match what we initiated with */
987 	if (tagLen != op->ae_tag_len)
988 		res = TEE_ERROR_MAC_INVALID;
989 
990 	acc_dlen += tmp_dlen;
991 
992 	*destLen = acc_dlen;
993 	op->info.handleState &= ~TEE_HANDLE_FLAG_INITIALIZED;
994 
995 	return res;
996 }
997 
998 /* Cryptographic Operations API - Asymmetric Functions */
999 
1000 TEE_Result TEE_AsymmetricEncrypt(TEE_OperationHandle op,
1001 				 const TEE_Attribute *params,
1002 				 uint32_t paramCount, const void *srcData,
1003 				 size_t srcLen, void *destData,
1004 				 size_t *destLen)
1005 {
1006 	TEE_Result res;
1007 
1008 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1009 	    destLen == NULL || (destData == NULL && *destLen != 0))
1010 		TEE_Panic(0);
1011 	if (paramCount != 0 && params == NULL)
1012 		TEE_Panic(0);
1013 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1014 		TEE_Panic(0);
1015 	if (op->info.mode != TEE_MODE_ENCRYPT)
1016 		TEE_Panic(0);
1017 
1018 	res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen,
1019 				 destData, destLen);
1020 	if (res != TEE_SUCCESS &&
1021 	    res != TEE_ERROR_SHORT_BUFFER &&
1022 	    res != TEE_ERROR_BAD_PARAMETERS)
1023 		TEE_Panic(res);
1024 	return res;
1025 }
1026 
1027 TEE_Result TEE_AsymmetricDecrypt(TEE_OperationHandle op,
1028 				 const TEE_Attribute *params,
1029 				 uint32_t paramCount, const void *srcData,
1030 				 size_t srcLen, void *destData,
1031 				 size_t *destLen)
1032 {
1033 	TEE_Result res;
1034 
1035 	if (op == TEE_HANDLE_NULL || (srcData == NULL && srcLen != 0) ||
1036 	    destLen == NULL || (destData == NULL && *destLen != 0))
1037 		TEE_Panic(0);
1038 	if (paramCount != 0 && params == NULL)
1039 		TEE_Panic(0);
1040 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER)
1041 		TEE_Panic(0);
1042 	if (op->info.mode != TEE_MODE_DECRYPT)
1043 		TEE_Panic(0);
1044 
1045 	res = utee_asymm_operate(op->state, params, paramCount, srcData, srcLen,
1046 				 destData, destLen);
1047 	if (res != TEE_SUCCESS &&
1048 	    res != TEE_ERROR_SHORT_BUFFER &&
1049 	    res != TEE_ERROR_BAD_PARAMETERS)
1050 		TEE_Panic(res);
1051 	return res;
1052 }
1053 
1054 TEE_Result TEE_AsymmetricSignDigest(TEE_OperationHandle op,
1055 				    const TEE_Attribute *params,
1056 				    uint32_t paramCount, const void *digest,
1057 				    size_t digestLen, void *signature,
1058 				    size_t *signatureLen)
1059 {
1060 	TEE_Result res;
1061 
1062 	if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) ||
1063 	    signature == NULL || signatureLen == NULL)
1064 		TEE_Panic(0);
1065 	if (paramCount != 0 && params == NULL)
1066 		TEE_Panic(0);
1067 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1068 		TEE_Panic(0);
1069 	if (op->info.mode != TEE_MODE_SIGN)
1070 		TEE_Panic(0);
1071 
1072 	res =
1073 	    utee_asymm_operate(op->state, params, paramCount, digest, digestLen,
1074 			       signature, signatureLen);
1075 	if (res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER)
1076 		TEE_Panic(res);
1077 	return res;
1078 }
1079 
1080 TEE_Result TEE_AsymmetricVerifyDigest(TEE_OperationHandle op,
1081 				      const TEE_Attribute *params,
1082 				      uint32_t paramCount, const void *digest,
1083 				      size_t digestLen, const void *signature,
1084 				      size_t signatureLen)
1085 {
1086 	TEE_Result res;
1087 
1088 	if (op == TEE_HANDLE_NULL || (digest == NULL && digestLen != 0) ||
1089 	    (signature == NULL && signatureLen != 0))
1090 		TEE_Panic(0);
1091 	if (paramCount != 0 && params == NULL)
1092 		TEE_Panic(0);
1093 	if (op->info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE)
1094 		TEE_Panic(0);
1095 	if (op->info.mode != TEE_MODE_VERIFY)
1096 		TEE_Panic(0);
1097 
1098 	res =
1099 	    utee_asymm_verify(op->state, params, paramCount, digest, digestLen,
1100 			      signature, signatureLen);
1101 	if (res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID)
1102 		TEE_Panic(res);
1103 	return res;
1104 }
1105 
1106 /* Cryptographic Operations API - Key Derivation Functions */
1107 
1108 void TEE_DeriveKey(TEE_OperationHandle operation,
1109 		   const TEE_Attribute *params, uint32_t paramCount,
1110 		   TEE_ObjectHandle derivedKey)
1111 {
1112 	TEE_Result res;
1113 	TEE_ObjectInfo key_info;
1114 
1115 	if (operation == TEE_HANDLE_NULL || derivedKey == 0)
1116 		TEE_Panic(0);
1117 	if (paramCount != 0 && params == NULL)
1118 		TEE_Panic(0);
1119 	if (TEE_ALG_GET_CLASS(operation->info.algorithm) !=
1120 			TEE_OPERATION_KEY_DERIVATION)
1121 		TEE_Panic(0);
1122 
1123 	if (operation->info.operationClass != TEE_OPERATION_KEY_DERIVATION)
1124 		TEE_Panic(0);
1125 	if (operation->info.mode != TEE_MODE_DERIVE)
1126 		TEE_Panic(0);
1127 	if ((operation->info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0)
1128 		TEE_Panic(0);
1129 
1130 	res = utee_cryp_obj_get_info((uint32_t) derivedKey, &key_info);
1131 	if (res != TEE_SUCCESS)
1132 		TEE_Panic(0);
1133 
1134 	if (key_info.objectType != TEE_TYPE_GENERIC_SECRET)
1135 		TEE_Panic(0);
1136 	if ((key_info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1137 		TEE_Panic(0);
1138 
1139 	res = utee_cryp_derive_key(operation->state, params, paramCount,
1140 				   (uint32_t) derivedKey);
1141 	if (res != TEE_SUCCESS)
1142 		TEE_Panic(res);
1143 }
1144 
1145 /* Cryptographic Operations API - Random Number Generation Functions */
1146 
1147 void TEE_GenerateRandom(void *randomBuffer, size_t randomBufferLen)
1148 {
1149 	TEE_Result res;
1150 
1151 	res = utee_cryp_random_number_generate(randomBuffer, randomBufferLen);
1152 	if (res != TEE_SUCCESS)
1153 		TEE_Panic(res);
1154 }
1155