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