xref: /optee_os/ta/pkcs11/src/processing_asymm.c (revision 0442c956edfb70be378634fd51dbec6e21550a5a)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2018-2020, Linaro Limited
4  */
5 
6 #include <assert.h>
7 #include <compiler.h>
8 #include <tee_api_defines.h>
9 #include <tee_internal_api.h>
10 #include <tee_internal_api_extensions.h>
11 
12 #include "attributes.h"
13 #include "pkcs11_helpers.h"
14 #include "pkcs11_token.h"
15 #include "processing.h"
16 #include "serializer.h"
17 
18 bool processing_is_tee_asymm(uint32_t proc_id)
19 {
20 	switch (proc_id) {
21 	/* RSA flavors */
22 	case PKCS11_CKM_RSA_PKCS:
23 	case PKCS11_CKM_MD5_RSA_PKCS:
24 	case PKCS11_CKM_SHA1_RSA_PKCS:
25 	case PKCS11_CKM_SHA224_RSA_PKCS:
26 	case PKCS11_CKM_SHA256_RSA_PKCS:
27 	case PKCS11_CKM_SHA384_RSA_PKCS:
28 	case PKCS11_CKM_SHA512_RSA_PKCS:
29 	/* EC flavors */
30 	case PKCS11_CKM_ECDSA:
31 	case PKCS11_CKM_ECDSA_SHA1:
32 	case PKCS11_CKM_ECDSA_SHA224:
33 	case PKCS11_CKM_ECDSA_SHA256:
34 	case PKCS11_CKM_ECDSA_SHA384:
35 	case PKCS11_CKM_ECDSA_SHA512:
36 		return true;
37 	default:
38 		return false;
39 	}
40 }
41 
42 static enum pkcs11_rc
43 pkcs2tee_algorithm(uint32_t *tee_id, uint32_t *tee_hash_id,
44 		   enum processing_func function __unused,
45 		   struct pkcs11_attribute_head *proc_params,
46 		   struct pkcs11_object *obj)
47 {
48 	static const struct {
49 		enum pkcs11_mechanism_id mech_id;
50 		uint32_t tee_id;
51 		uint32_t tee_hash_id;
52 	} pkcs2tee_algo[] = {
53 		/* RSA flavors */
54 		{ PKCS11_CKM_RSA_PKCS, TEE_ALG_RSAES_PKCS1_V1_5, 0 },
55 		{ PKCS11_CKM_MD5_RSA_PKCS, TEE_ALG_RSASSA_PKCS1_V1_5_MD5,
56 		  TEE_ALG_MD5 },
57 		{ PKCS11_CKM_SHA1_RSA_PKCS, TEE_ALG_RSASSA_PKCS1_V1_5_SHA1,
58 		  TEE_ALG_SHA1 },
59 		{ PKCS11_CKM_SHA224_RSA_PKCS, TEE_ALG_RSASSA_PKCS1_V1_5_SHA224,
60 		  TEE_ALG_SHA224 },
61 		{ PKCS11_CKM_SHA256_RSA_PKCS, TEE_ALG_RSASSA_PKCS1_V1_5_SHA256,
62 		  TEE_ALG_SHA256 },
63 		{ PKCS11_CKM_SHA384_RSA_PKCS, TEE_ALG_RSASSA_PKCS1_V1_5_SHA384,
64 		  TEE_ALG_SHA384 },
65 		{ PKCS11_CKM_SHA512_RSA_PKCS, TEE_ALG_RSASSA_PKCS1_V1_5_SHA512,
66 		  TEE_ALG_SHA512 },
67 		/* EC flavors (Must find key size from the object) */
68 		{ PKCS11_CKM_ECDSA, 1, 0 },
69 		{ PKCS11_CKM_ECDSA_SHA1, 1, TEE_ALG_SHA1 },
70 		{ PKCS11_CKM_ECDSA_SHA224, 1, TEE_ALG_SHA224 },
71 		{ PKCS11_CKM_ECDSA_SHA256, 1, TEE_ALG_SHA256 },
72 		{ PKCS11_CKM_ECDSA_SHA384, 1, TEE_ALG_SHA384 },
73 		{ PKCS11_CKM_ECDSA_SHA512, 1, TEE_ALG_SHA512 },
74 	};
75 	size_t n = 0;
76 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
77 
78 	for (n = 0; n < ARRAY_SIZE(pkcs2tee_algo); n++) {
79 		if (pkcs2tee_algo[n].mech_id == proc_params->id) {
80 			*tee_id = pkcs2tee_algo[n].tee_id;
81 			*tee_hash_id = pkcs2tee_algo[n].tee_hash_id;
82 			break;
83 		}
84 	}
85 
86 	if (n == ARRAY_SIZE(pkcs2tee_algo))
87 		return PKCS11_RV_NOT_IMPLEMENTED;
88 
89 	switch (proc_params->id) {
90 	case PKCS11_CKM_ECDSA:
91 	case PKCS11_CKM_ECDSA_SHA1:
92 	case PKCS11_CKM_ECDSA_SHA224:
93 	case PKCS11_CKM_ECDSA_SHA256:
94 	case PKCS11_CKM_ECDSA_SHA384:
95 	case PKCS11_CKM_ECDSA_SHA512:
96 		rc = pkcs2tee_algo_ecdsa(tee_id, proc_params, obj);
97 		break;
98 	default:
99 		rc = PKCS11_CKR_OK;
100 		break;
101 	}
102 
103 	/*
104 	 * PKCS#11 uses single mechanism CKM_RSA_PKCS for both ciphering and
105 	 * authentication whereas GPD TEE expects TEE_ALG_RSAES_PKCS1_V1_5 for
106 	 * ciphering and TEE_ALG_RSASSA_PKCS1_V1_5 for authentication.
107 	 */
108 	if (*tee_id == TEE_ALG_RSAES_PKCS1_V1_5 &&
109 	    (function == PKCS11_FUNCTION_SIGN ||
110 	     function == PKCS11_FUNCTION_VERIFY))
111 		*tee_id = TEE_ALG_RSASSA_PKCS1_V1_5;
112 
113 	return rc;
114 }
115 
116 static enum pkcs11_rc pkcs2tee_key_type(uint32_t *tee_type,
117 					struct pkcs11_object *obj,
118 					enum processing_func function)
119 {
120 	enum pkcs11_class_id class = get_class(obj->attributes);
121 	enum pkcs11_key_type type = get_key_type(obj->attributes);
122 
123 	switch (class) {
124 	case PKCS11_CKO_PUBLIC_KEY:
125 	case PKCS11_CKO_PRIVATE_KEY:
126 		break;
127 	default:
128 		TEE_Panic(class);
129 		break;
130 	}
131 
132 	switch (type) {
133 	case PKCS11_CKK_EC:
134 		assert(function != PKCS11_FUNCTION_DERIVE);
135 
136 		if (class == PKCS11_CKO_PRIVATE_KEY)
137 			*tee_type = TEE_TYPE_ECDSA_KEYPAIR;
138 		else
139 			*tee_type = TEE_TYPE_ECDSA_PUBLIC_KEY;
140 		break;
141 	case PKCS11_CKK_RSA:
142 		if (class == PKCS11_CKO_PRIVATE_KEY)
143 			*tee_type = TEE_TYPE_RSA_KEYPAIR;
144 		else
145 			*tee_type = TEE_TYPE_RSA_PUBLIC_KEY;
146 		break;
147 	default:
148 		TEE_Panic(type);
149 		break;
150 	}
151 
152 	return PKCS11_CKR_OK;
153 }
154 
155 static enum pkcs11_rc
156 allocate_tee_operation(struct pkcs11_session *session,
157 		       enum processing_func function,
158 		       struct pkcs11_attribute_head *params,
159 		       struct pkcs11_object *obj)
160 {
161 	uint32_t size = (uint32_t)get_object_key_bit_size(obj);
162 	uint32_t algo = 0;
163 	uint32_t hash_algo = 0;
164 	uint32_t mode = 0;
165 	uint32_t hash_mode = 0;
166 	TEE_Result res = TEE_ERROR_GENERIC;
167 	struct active_processing *processing = session->processing;
168 
169 	assert(processing->tee_op_handle == TEE_HANDLE_NULL);
170 	assert(processing->tee_hash_op_handle == TEE_HANDLE_NULL);
171 
172 	if (pkcs2tee_algorithm(&algo, &hash_algo, function, params, obj))
173 		return PKCS11_CKR_FUNCTION_FAILED;
174 
175 	pkcs2tee_mode(&mode, function);
176 
177 	if (hash_algo) {
178 		pkcs2tee_mode(&hash_mode, PKCS11_FUNCTION_DIGEST);
179 
180 		res = TEE_AllocateOperation(&processing->tee_hash_op_handle,
181 					    hash_algo, hash_mode, 0);
182 		if (res) {
183 			EMSG("TEE_AllocateOp. failed %#"PRIx32" %#"PRIx32,
184 			     hash_algo, hash_mode);
185 
186 			if (res == TEE_ERROR_NOT_SUPPORTED)
187 				return PKCS11_CKR_MECHANISM_INVALID;
188 			return tee2pkcs_error(res);
189 		}
190 		processing->tee_hash_algo = hash_algo;
191 	}
192 
193 	res = TEE_AllocateOperation(&processing->tee_op_handle,
194 				    algo, mode, size);
195 	if (res)
196 		EMSG("TEE_AllocateOp. failed %#"PRIx32" %#"PRIx32" %#"PRIx32,
197 		     algo, mode, size);
198 
199 	if (res == TEE_ERROR_NOT_SUPPORTED)
200 		return PKCS11_CKR_MECHANISM_INVALID;
201 
202 	if (res != TEE_SUCCESS &&
203 	    processing->tee_hash_op_handle != TEE_HANDLE_NULL) {
204 		TEE_FreeOperation(session->processing->tee_hash_op_handle);
205 		processing->tee_hash_op_handle = TEE_HANDLE_NULL;
206 		processing->tee_hash_algo = 0;
207 	}
208 
209 	return tee2pkcs_error(res);
210 }
211 
212 static enum pkcs11_rc load_tee_key(struct pkcs11_session *session,
213 				   struct pkcs11_object *obj,
214 				   enum processing_func function)
215 {
216 	TEE_Attribute *tee_attrs = NULL;
217 	size_t tee_attrs_count = 0;
218 	size_t object_size = 0;
219 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
220 	TEE_Result res = TEE_ERROR_GENERIC;
221 	enum pkcs11_class_id __maybe_unused class = get_class(obj->attributes);
222 	enum pkcs11_key_type type = get_key_type(obj->attributes);
223 
224 	assert(class == PKCS11_CKO_PUBLIC_KEY ||
225 	       class == PKCS11_CKO_PRIVATE_KEY);
226 
227 	if (obj->key_handle != TEE_HANDLE_NULL) {
228 		switch (type) {
229 		case PKCS11_CKK_RSA:
230 			/* RSA loaded keys can be reused */
231 			assert((obj->key_type == TEE_TYPE_RSA_PUBLIC_KEY &&
232 				class == PKCS11_CKO_PUBLIC_KEY) ||
233 			       (obj->key_type == TEE_TYPE_RSA_KEYPAIR &&
234 				class == PKCS11_CKO_PRIVATE_KEY));
235 			goto key_ready;
236 		case PKCS11_CKK_EC:
237 			/* Reuse EC TEE key only if already DSA or DH */
238 			switch (obj->key_type) {
239 			case TEE_TYPE_ECDSA_PUBLIC_KEY:
240 			case TEE_TYPE_ECDSA_KEYPAIR:
241 				if (function != PKCS11_FUNCTION_DERIVE)
242 					goto key_ready;
243 				break;
244 			default:
245 				assert(0);
246 				break;
247 			}
248 			break;
249 		default:
250 			assert(0);
251 			break;
252 		}
253 
254 		TEE_CloseObject(obj->key_handle);
255 		obj->key_handle = TEE_HANDLE_NULL;
256 	}
257 
258 	rc = pkcs2tee_key_type(&obj->key_type, obj, function);
259 	if (rc)
260 		return rc;
261 
262 	object_size = get_object_key_bit_size(obj);
263 	if (!object_size)
264 		return PKCS11_CKR_GENERAL_ERROR;
265 
266 	switch (type) {
267 	case PKCS11_CKK_RSA:
268 		rc = load_tee_rsa_key_attrs(&tee_attrs, &tee_attrs_count, obj);
269 		break;
270 	case PKCS11_CKK_EC:
271 		rc = load_tee_ec_key_attrs(&tee_attrs, &tee_attrs_count, obj);
272 		break;
273 	default:
274 		break;
275 	}
276 	if (rc)
277 		return rc;
278 
279 	res = TEE_AllocateTransientObject(obj->key_type, object_size,
280 					  &obj->key_handle);
281 	if (res) {
282 		DMSG("TEE_AllocateTransientObject failed, %#"PRIx32, res);
283 
284 		return tee2pkcs_error(res);
285 	}
286 
287 	res = TEE_PopulateTransientObject(obj->key_handle,
288 					  tee_attrs, tee_attrs_count);
289 
290 	TEE_Free(tee_attrs);
291 
292 	if (res) {
293 		DMSG("TEE_PopulateTransientObject failed, %#"PRIx32, res);
294 
295 		goto error;
296 	}
297 
298 key_ready:
299 	res = TEE_SetOperationKey(session->processing->tee_op_handle,
300 				  obj->key_handle);
301 	if (res) {
302 		DMSG("TEE_SetOperationKey failed, %#"PRIx32, res);
303 
304 		goto error;
305 	}
306 
307 	return PKCS11_CKR_OK;
308 
309 error:
310 	TEE_FreeTransientObject(obj->key_handle);
311 	obj->key_handle = TEE_HANDLE_NULL;
312 	return tee2pkcs_error(res);
313 }
314 
315 static enum pkcs11_rc
316 init_tee_operation(struct pkcs11_session *session __unused,
317 		   struct pkcs11_attribute_head *proc_params __unused)
318 {
319 	return PKCS11_CKR_OK;
320 }
321 
322 enum pkcs11_rc init_asymm_operation(struct pkcs11_session *session,
323 				    enum processing_func function,
324 				    struct pkcs11_attribute_head *proc_params,
325 				    struct pkcs11_object *obj)
326 {
327 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
328 
329 	assert(processing_is_tee_asymm(proc_params->id));
330 
331 	rc = allocate_tee_operation(session, function, proc_params, obj);
332 	if (rc)
333 		return rc;
334 
335 	rc = load_tee_key(session, obj, function);
336 	if (rc)
337 		return rc;
338 
339 	return init_tee_operation(session, proc_params);
340 }
341 
342 /*
343  * step_sym_step - step (update/oneshot/final) on a symmetric crypto operation
344  *
345  * @session - current session
346  * @function - processing function (encrypt, decrypt, sign, ...)
347  * @step - step ID in the processing (oneshot, update, final)
348  * @ptypes - invocation parameter types
349  * @params - invocation parameter references
350  */
351 enum pkcs11_rc step_asymm_operation(struct pkcs11_session *session,
352 				    enum processing_func function,
353 				    enum processing_step step,
354 				    uint32_t ptypes, TEE_Param *params)
355 {
356 	enum pkcs11_rc rc = PKCS11_CKR_GENERAL_ERROR;
357 	TEE_Result res = TEE_ERROR_GENERIC;
358 	void *in_buf = NULL;
359 	void *in2_buf = NULL;
360 	void *out_buf = NULL;
361 	void *hash_buf = NULL;
362 	uint32_t in_size = 0;
363 	uint32_t in2_size = 0;
364 	uint32_t out_size = 0;
365 	uint32_t hash_size = 0;
366 	TEE_Attribute *tee_attrs = NULL;
367 	size_t tee_attrs_count = 0;
368 	bool output_data = false;
369 	struct active_processing *proc = session->processing;
370 	size_t sz = 0;
371 
372 	if (TEE_PARAM_TYPE_GET(ptypes, 1) == TEE_PARAM_TYPE_MEMREF_INPUT) {
373 		in_buf = params[1].memref.buffer;
374 		in_size = params[1].memref.size;
375 		if (in_size && !in_buf)
376 			return PKCS11_CKR_ARGUMENTS_BAD;
377 	}
378 	if (TEE_PARAM_TYPE_GET(ptypes, 2) == TEE_PARAM_TYPE_MEMREF_INPUT) {
379 		in2_buf = params[2].memref.buffer;
380 		in2_size = params[2].memref.size;
381 		if (in2_size && !in2_buf)
382 			return PKCS11_CKR_ARGUMENTS_BAD;
383 	}
384 	if (TEE_PARAM_TYPE_GET(ptypes, 2) == TEE_PARAM_TYPE_MEMREF_OUTPUT) {
385 		out_buf = params[2].memref.buffer;
386 		out_size = params[2].memref.size;
387 		if (out_size && !out_buf)
388 			return PKCS11_CKR_ARGUMENTS_BAD;
389 	}
390 	if (TEE_PARAM_TYPE_GET(ptypes, 3) != TEE_PARAM_TYPE_NONE)
391 		return PKCS11_CKR_ARGUMENTS_BAD;
392 
393 	switch (step) {
394 	case PKCS11_FUNC_STEP_ONESHOT:
395 	case PKCS11_FUNC_STEP_UPDATE:
396 	case PKCS11_FUNC_STEP_FINAL:
397 		break;
398 	default:
399 		return PKCS11_CKR_GENERAL_ERROR;
400 	}
401 
402 	/*
403 	 * Handle multi stage update step for mechas needing hash
404 	 * calculation
405 	 */
406 	if (step == PKCS11_FUNC_STEP_UPDATE) {
407 		switch (proc->mecha_type) {
408 		case PKCS11_CKM_ECDSA_SHA1:
409 		case PKCS11_CKM_ECDSA_SHA224:
410 		case PKCS11_CKM_ECDSA_SHA256:
411 		case PKCS11_CKM_ECDSA_SHA384:
412 		case PKCS11_CKM_ECDSA_SHA512:
413 		case PKCS11_CKM_MD5_RSA_PKCS:
414 		case PKCS11_CKM_SHA1_RSA_PKCS:
415 		case PKCS11_CKM_SHA224_RSA_PKCS:
416 		case PKCS11_CKM_SHA256_RSA_PKCS:
417 		case PKCS11_CKM_SHA384_RSA_PKCS:
418 		case PKCS11_CKM_SHA512_RSA_PKCS:
419 			assert(proc->tee_hash_op_handle != TEE_HANDLE_NULL);
420 
421 			TEE_DigestUpdate(proc->tee_hash_op_handle, in_buf,
422 					 in_size);
423 			break;
424 		default:
425 			/*
426 			 * Other mechanism do not expect multi stage
427 			 * operation
428 			 */
429 			rc = PKCS11_CKR_GENERAL_ERROR;
430 			break;
431 		}
432 
433 		goto out;
434 	}
435 
436 	/*
437 	 * Handle multi stage one shot and final steps for mechas needing hash
438 	 * calculation
439 	 */
440 	switch (proc->mecha_type) {
441 	case PKCS11_CKM_ECDSA_SHA1:
442 	case PKCS11_CKM_ECDSA_SHA224:
443 	case PKCS11_CKM_ECDSA_SHA256:
444 	case PKCS11_CKM_ECDSA_SHA384:
445 	case PKCS11_CKM_ECDSA_SHA512:
446 	case PKCS11_CKM_MD5_RSA_PKCS:
447 	case PKCS11_CKM_SHA1_RSA_PKCS:
448 	case PKCS11_CKM_SHA224_RSA_PKCS:
449 	case PKCS11_CKM_SHA256_RSA_PKCS:
450 	case PKCS11_CKM_SHA384_RSA_PKCS:
451 	case PKCS11_CKM_SHA512_RSA_PKCS:
452 		assert(proc->tee_hash_op_handle != TEE_HANDLE_NULL);
453 
454 		hash_size = TEE_ALG_GET_DIGEST_SIZE(proc->tee_hash_algo);
455 		hash_buf = TEE_Malloc(hash_size, 0);
456 		if (!hash_buf)
457 			return PKCS11_CKR_DEVICE_MEMORY;
458 
459 		res = TEE_DigestDoFinal(proc->tee_hash_op_handle,
460 					in_buf, in_size, hash_buf,
461 					&hash_size);
462 
463 		rc = tee2pkcs_error(res);
464 		if (rc != PKCS11_CKR_OK)
465 			goto out;
466 
467 		break;
468 	default:
469 		break;
470 	}
471 
472 	/*
473 	 * Finalize either provided hash or calculated hash with signing
474 	 * operation
475 	 */
476 
477 	/* First determine amount of bytes for signing operation */
478 	switch (proc->mecha_type) {
479 	case PKCS11_CKM_ECDSA:
480 		sz = ecdsa_get_input_max_byte_size(proc->tee_op_handle);
481 		if (!in_size || !sz) {
482 			rc = PKCS11_CKR_FUNCTION_FAILED;
483 			goto out;
484 		}
485 
486 		/*
487 		 * Note 3) Input the entire raw digest. Internally, this will
488 		 * be truncated to the appropriate number of bits.
489 		 */
490 		if (in_size > sz)
491 			in_size = sz;
492 
493 		if (function == PKCS11_FUNCTION_VERIFY && in2_size != 2 * sz) {
494 			rc = PKCS11_CKR_SIGNATURE_LEN_RANGE;
495 			goto out;
496 		}
497 		break;
498 	case PKCS11_CKM_ECDSA_SHA1:
499 	case PKCS11_CKM_ECDSA_SHA224:
500 	case PKCS11_CKM_ECDSA_SHA256:
501 	case PKCS11_CKM_ECDSA_SHA384:
502 	case PKCS11_CKM_ECDSA_SHA512:
503 		/* Get key size in bytes */
504 		sz = ecdsa_get_input_max_byte_size(proc->tee_op_handle);
505 		if (!sz) {
506 			rc = PKCS11_CKR_FUNCTION_FAILED;
507 			goto out;
508 		}
509 
510 		if (function == PKCS11_FUNCTION_VERIFY &&
511 		    in2_size != 2 * sz) {
512 			rc = PKCS11_CKR_SIGNATURE_LEN_RANGE;
513 			goto out;
514 		}
515 		break;
516 	case PKCS11_CKM_RSA_PKCS:
517 	case PKCS11_CKM_MD5_RSA_PKCS:
518 	case PKCS11_CKM_SHA1_RSA_PKCS:
519 	case PKCS11_CKM_SHA224_RSA_PKCS:
520 	case PKCS11_CKM_SHA256_RSA_PKCS:
521 	case PKCS11_CKM_SHA384_RSA_PKCS:
522 	case PKCS11_CKM_SHA512_RSA_PKCS:
523 		/* Get key size in bytes */
524 		sz = rsa_get_input_max_byte_size(proc->tee_op_handle);
525 		if (!sz) {
526 			rc = PKCS11_CKR_FUNCTION_FAILED;
527 			goto out;
528 		}
529 
530 		if (function == PKCS11_FUNCTION_VERIFY && in2_size != sz) {
531 			rc = PKCS11_CKR_SIGNATURE_LEN_RANGE;
532 			goto out;
533 		}
534 		break;
535 	default:
536 		break;
537 	}
538 
539 	/* Next perform actual signing operation */
540 	switch (proc->mecha_type) {
541 	case PKCS11_CKM_ECDSA:
542 	case PKCS11_CKM_RSA_PKCS:
543 		/* For operations using provided input data */
544 		switch (function) {
545 		case PKCS11_FUNCTION_ENCRYPT:
546 			res = TEE_AsymmetricEncrypt(proc->tee_op_handle,
547 						    tee_attrs, tee_attrs_count,
548 						    in_buf, in_size,
549 						    out_buf, &out_size);
550 			output_data = true;
551 			rc = tee2pkcs_error(res);
552 			break;
553 
554 		case PKCS11_FUNCTION_DECRYPT:
555 			res = TEE_AsymmetricDecrypt(proc->tee_op_handle,
556 						    tee_attrs, tee_attrs_count,
557 						    in_buf, in_size,
558 						    out_buf, &out_size);
559 			output_data = true;
560 			rc = tee2pkcs_error(res);
561 			break;
562 
563 		case PKCS11_FUNCTION_SIGN:
564 			res = TEE_AsymmetricSignDigest(proc->tee_op_handle,
565 						       tee_attrs,
566 						       tee_attrs_count,
567 						       in_buf, in_size,
568 						       out_buf, &out_size);
569 			output_data = true;
570 			rc = tee2pkcs_error(res);
571 			break;
572 
573 		case PKCS11_FUNCTION_VERIFY:
574 			res = TEE_AsymmetricVerifyDigest(proc->tee_op_handle,
575 							 tee_attrs,
576 							 tee_attrs_count,
577 							 in_buf, in_size,
578 							 in2_buf, in2_size);
579 			rc = tee2pkcs_error(res);
580 			break;
581 
582 		default:
583 			TEE_Panic(function);
584 			break;
585 		}
586 		break;
587 	case PKCS11_CKM_ECDSA_SHA1:
588 	case PKCS11_CKM_ECDSA_SHA224:
589 	case PKCS11_CKM_ECDSA_SHA256:
590 	case PKCS11_CKM_ECDSA_SHA384:
591 	case PKCS11_CKM_ECDSA_SHA512:
592 	case PKCS11_CKM_MD5_RSA_PKCS:
593 	case PKCS11_CKM_SHA1_RSA_PKCS:
594 	case PKCS11_CKM_SHA224_RSA_PKCS:
595 	case PKCS11_CKM_SHA256_RSA_PKCS:
596 	case PKCS11_CKM_SHA384_RSA_PKCS:
597 	case PKCS11_CKM_SHA512_RSA_PKCS:
598 		/* For operations having hash operation use calculated hash */
599 		switch (function) {
600 		case PKCS11_FUNCTION_SIGN:
601 			res = TEE_AsymmetricSignDigest(proc->tee_op_handle,
602 						       tee_attrs,
603 						       tee_attrs_count,
604 						       hash_buf, hash_size,
605 						       out_buf, &out_size);
606 			output_data = true;
607 			rc = tee2pkcs_error(res);
608 			break;
609 
610 		case PKCS11_FUNCTION_VERIFY:
611 			res = TEE_AsymmetricVerifyDigest(proc->tee_op_handle,
612 							 tee_attrs,
613 							 tee_attrs_count,
614 							 hash_buf, hash_size,
615 							 in2_buf, in2_size);
616 			rc = tee2pkcs_error(res);
617 			break;
618 
619 		default:
620 			TEE_Panic(function);
621 			break;
622 		}
623 		break;
624 	default:
625 		TEE_Panic(proc->mecha_type);
626 		break;
627 	}
628 
629 out:
630 	if (output_data &&
631 	    (rc == PKCS11_CKR_OK || rc == PKCS11_CKR_BUFFER_TOO_SMALL)) {
632 		switch (TEE_PARAM_TYPE_GET(ptypes, 2)) {
633 		case TEE_PARAM_TYPE_MEMREF_OUTPUT:
634 		case TEE_PARAM_TYPE_MEMREF_INOUT:
635 			params[2].memref.size = out_size;
636 			break;
637 		default:
638 			rc = PKCS11_CKR_GENERAL_ERROR;
639 			break;
640 		}
641 	}
642 
643 	TEE_Free(hash_buf);
644 	TEE_Free(tee_attrs);
645 
646 	return rc;
647 }
648