xref: /optee_os/core/tee/tee_svc_cryp.c (revision 2f4d97e7664270c92f4fd9d35fcddcfa4fd5f667)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * Copyright (c) 2020, 2022 Linaro Limited
5  * Copyright (c) 2022, Technology Innovation Institute (TII)
6  */
7 
8 #include <assert.h>
9 #include <bitstring.h>
10 #include <compiler.h>
11 #include <config.h>
12 #include <crypto/crypto.h>
13 #include <kernel/tee_ta_manager.h>
14 #include <kernel/user_access.h>
15 #include <memtag.h>
16 #include <mm/vm.h>
17 #include <stdlib_ext.h>
18 #include <string_ext.h>
19 #include <string.h>
20 #include <sys/queue.h>
21 #include <tee_api_defines_extensions.h>
22 #include <tee_api_types.h>
23 #include <tee/tee_cryp_utl.h>
24 #include <tee/tee_obj.h>
25 #include <tee/tee_svc_cryp.h>
26 #include <tee/tee_svc.h>
27 #include <trace.h>
28 #include <utee_defines.h>
29 #include <util.h>
30 #if defined(CFG_CRYPTO_HKDF)
31 #include <tee/tee_cryp_hkdf.h>
32 #endif
33 #if defined(CFG_CRYPTO_CONCAT_KDF)
34 #include <tee/tee_cryp_concat_kdf.h>
35 #endif
36 #if defined(CFG_CRYPTO_PBKDF2)
37 #include <tee/tee_cryp_pbkdf2.h>
38 #endif
39 
40 enum cryp_state {
41 	CRYP_STATE_INITIALIZED = 0,
42 	CRYP_STATE_UNINITIALIZED
43 };
44 
45 typedef void (*tee_cryp_ctx_finalize_func_t) (void *ctx);
46 struct tee_cryp_state {
47 	TAILQ_ENTRY(tee_cryp_state) link;
48 	uint32_t algo;
49 	uint32_t mode;
50 	vaddr_t key1;
51 	vaddr_t key2;
52 	void *ctx;
53 	tee_cryp_ctx_finalize_func_t ctx_finalize;
54 	enum cryp_state state;
55 };
56 
57 struct tee_cryp_obj_secret {
58 	uint32_t key_size;
59 	uint32_t alloc_size;
60 
61 	/*
62 	 * Pseudo code visualize layout of structure
63 	 * Next follows data, such as:
64 	 *	uint8_t data[alloc_size]
65 	 * key_size must never exceed alloc_size
66 	 */
67 };
68 
69 #define TEE_TYPE_ATTR_OPTIONAL		BIT(0)
70 #define TEE_TYPE_ATTR_REQUIRED		BIT(1)
71 #define TEE_TYPE_ATTR_OPTIONAL_GROUP	BIT(2)
72 #define TEE_TYPE_ATTR_SIZE_INDICATOR	BIT(3)
73 #define TEE_TYPE_ATTR_GEN_KEY_OPT	BIT(4)
74 #define TEE_TYPE_ATTR_GEN_KEY_REQ	BIT(5)
75 #define TEE_TYPE_ATTR_BIGNUM_MAXBITS	BIT(6)
76 
77     /* Handle storing of generic secret keys of varying lengths */
78 #define ATTR_OPS_INDEX_SECRET     0
79     /* Convert to/from big-endian byte array and provider-specific bignum */
80 #define ATTR_OPS_INDEX_BIGNUM     1
81     /* Convert to/from value attribute depending on direction */
82 #define ATTR_OPS_INDEX_VALUE      2
83     /* Convert to/from curve25519 attribute depending on direction */
84 #define ATTR_OPS_INDEX_25519      3
85 
86     /* Curve25519 key bytes size is always 32 bytes*/
87 #define KEY_SIZE_BYTES_25519 UL(32)
88 
89 struct tee_cryp_obj_type_attrs {
90 	uint32_t attr_id;
91 	uint16_t flags;
92 	uint16_t ops_index;
93 	uint16_t raw_offs;
94 	uint16_t raw_size;
95 };
96 
97 #define RAW_DATA(_x, _y)	\
98 	.raw_offs = offsetof(_x, _y), .raw_size = MEMBER_SIZE(_x, _y)
99 
100 static const struct tee_cryp_obj_type_attrs
101 	tee_cryp_obj_secret_value_attrs[] = {
102 	{
103 	.attr_id = TEE_ATTR_SECRET_VALUE,
104 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR,
105 	.ops_index = ATTR_OPS_INDEX_SECRET,
106 	.raw_offs = 0,
107 	.raw_size = 0
108 	},
109 };
110 
111 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_rsa_pub_key_attrs[] = {
112 	{
113 	.attr_id = TEE_ATTR_RSA_MODULUS,
114 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR,
115 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
116 	RAW_DATA(struct rsa_public_key, n)
117 	},
118 
119 	{
120 	.attr_id = TEE_ATTR_RSA_PUBLIC_EXPONENT,
121 	.flags = TEE_TYPE_ATTR_REQUIRED,
122 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
123 	RAW_DATA(struct rsa_public_key, e)
124 	},
125 };
126 
127 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_rsa_keypair_attrs[] = {
128 	{
129 	.attr_id = TEE_ATTR_RSA_MODULUS,
130 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR,
131 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
132 	RAW_DATA(struct rsa_keypair, n)
133 	},
134 
135 	{
136 	.attr_id = TEE_ATTR_RSA_PUBLIC_EXPONENT,
137 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_OPT,
138 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
139 	RAW_DATA(struct rsa_keypair, e)
140 	},
141 
142 	{
143 	.attr_id = TEE_ATTR_RSA_PRIVATE_EXPONENT,
144 	.flags = TEE_TYPE_ATTR_REQUIRED,
145 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
146 	RAW_DATA(struct rsa_keypair, d)
147 	},
148 
149 	{
150 	.attr_id = TEE_ATTR_RSA_PRIME1,
151 	.flags = TEE_TYPE_ATTR_OPTIONAL_GROUP,
152 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
153 	RAW_DATA(struct rsa_keypair, p)
154 	},
155 
156 	{
157 	.attr_id = TEE_ATTR_RSA_PRIME2,
158 	.flags = TEE_TYPE_ATTR_OPTIONAL_GROUP,
159 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
160 	RAW_DATA(struct rsa_keypair, q)
161 	},
162 
163 	{
164 	.attr_id = TEE_ATTR_RSA_EXPONENT1,
165 	.flags = TEE_TYPE_ATTR_OPTIONAL_GROUP,
166 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
167 	RAW_DATA(struct rsa_keypair, dp)
168 	},
169 
170 	{
171 	.attr_id = TEE_ATTR_RSA_EXPONENT2,
172 	.flags = TEE_TYPE_ATTR_OPTIONAL_GROUP,
173 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
174 	RAW_DATA(struct rsa_keypair, dq)
175 	},
176 
177 	{
178 	.attr_id = TEE_ATTR_RSA_COEFFICIENT,
179 	.flags = TEE_TYPE_ATTR_OPTIONAL_GROUP,
180 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
181 	RAW_DATA(struct rsa_keypair, qp)
182 	},
183 };
184 
185 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_dsa_pub_key_attrs[] = {
186 	{
187 	.attr_id = TEE_ATTR_DSA_PRIME,
188 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_BIGNUM_MAXBITS |
189 		 TEE_TYPE_ATTR_SIZE_INDICATOR,
190 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
191 	RAW_DATA(struct dsa_public_key, p)
192 	},
193 
194 	{
195 	.attr_id = TEE_ATTR_DSA_SUBPRIME,
196 	.flags = TEE_TYPE_ATTR_REQUIRED,
197 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
198 	RAW_DATA(struct dsa_public_key, q)
199 	},
200 
201 	{
202 	.attr_id = TEE_ATTR_DSA_BASE,
203 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_BIGNUM_MAXBITS,
204 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
205 	RAW_DATA(struct dsa_public_key, g)
206 	},
207 
208 	{
209 	.attr_id = TEE_ATTR_DSA_PUBLIC_VALUE,
210 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_BIGNUM_MAXBITS,
211 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
212 	RAW_DATA(struct dsa_public_key, y)
213 	},
214 };
215 
216 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_dsa_keypair_attrs[] = {
217 	{
218 	.attr_id = TEE_ATTR_DSA_PRIME,
219 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ |
220 		 TEE_TYPE_ATTR_BIGNUM_MAXBITS | TEE_TYPE_ATTR_SIZE_INDICATOR,
221 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
222 	RAW_DATA(struct dsa_keypair, p)
223 	},
224 
225 	{
226 	.attr_id = TEE_ATTR_DSA_SUBPRIME,
227 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ,
228 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
229 	RAW_DATA(struct dsa_keypair, q)
230 	},
231 
232 	{
233 	.attr_id = TEE_ATTR_DSA_BASE,
234 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ |
235 		 TEE_TYPE_ATTR_BIGNUM_MAXBITS,
236 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
237 	RAW_DATA(struct dsa_keypair, g)
238 	},
239 
240 	{
241 	.attr_id = TEE_ATTR_DSA_PRIVATE_VALUE,
242 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_BIGNUM_MAXBITS,
243 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
244 	RAW_DATA(struct dsa_keypair, x)
245 	},
246 
247 	{
248 	.attr_id = TEE_ATTR_DSA_PUBLIC_VALUE,
249 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_BIGNUM_MAXBITS,
250 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
251 	RAW_DATA(struct dsa_keypair, y)
252 	},
253 };
254 
255 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_dh_keypair_attrs[] = {
256 	{
257 	.attr_id = TEE_ATTR_DH_PRIME,
258 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR |
259 		 TEE_TYPE_ATTR_GEN_KEY_REQ,
260 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
261 	RAW_DATA(struct dh_keypair, p)
262 	},
263 
264 	{
265 	.attr_id = TEE_ATTR_DH_BASE,
266 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_GEN_KEY_REQ,
267 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
268 	RAW_DATA(struct dh_keypair, g)
269 	},
270 
271 	{
272 	.attr_id = TEE_ATTR_DH_PUBLIC_VALUE,
273 	.flags = TEE_TYPE_ATTR_REQUIRED,
274 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
275 	RAW_DATA(struct dh_keypair, y)
276 	},
277 
278 	{
279 	.attr_id = TEE_ATTR_DH_PRIVATE_VALUE,
280 	.flags = TEE_TYPE_ATTR_REQUIRED,
281 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
282 	RAW_DATA(struct dh_keypair, x)
283 	},
284 
285 	{
286 	.attr_id = TEE_ATTR_DH_SUBPRIME,
287 	.flags = TEE_TYPE_ATTR_OPTIONAL_GROUP |	 TEE_TYPE_ATTR_GEN_KEY_OPT,
288 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
289 	RAW_DATA(struct dh_keypair, q)
290 	},
291 
292 	{
293 	.attr_id = TEE_ATTR_DH_X_BITS,
294 	.flags = TEE_TYPE_ATTR_GEN_KEY_OPT,
295 	.ops_index = ATTR_OPS_INDEX_VALUE,
296 	RAW_DATA(struct dh_keypair, xbits)
297 	},
298 };
299 
300 #if defined(CFG_CRYPTO_HKDF)
301 static const struct tee_cryp_obj_type_attrs
302 	tee_cryp_obj_hkdf_ikm_attrs[] = {
303 	{
304 	.attr_id = TEE_ATTR_HKDF_IKM,
305 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR,
306 	.ops_index = ATTR_OPS_INDEX_SECRET,
307 	.raw_offs = 0,
308 	.raw_size = 0
309 	},
310 };
311 #endif
312 
313 #if defined(CFG_CRYPTO_CONCAT_KDF)
314 static const struct tee_cryp_obj_type_attrs
315 	tee_cryp_obj_concat_kdf_z_attrs[] = {
316 	{
317 	.attr_id = TEE_ATTR_CONCAT_KDF_Z,
318 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR,
319 	.ops_index = ATTR_OPS_INDEX_SECRET,
320 	.raw_offs = 0,
321 	.raw_size = 0
322 	},
323 };
324 #endif
325 
326 #if defined(CFG_CRYPTO_PBKDF2)
327 static const struct tee_cryp_obj_type_attrs
328 	tee_cryp_obj_pbkdf2_passwd_attrs[] = {
329 	{
330 	.attr_id = TEE_ATTR_PBKDF2_PASSWORD,
331 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR,
332 	.ops_index = ATTR_OPS_INDEX_SECRET,
333 	.raw_offs = 0,
334 	.raw_size = 0
335 	},
336 };
337 #endif
338 
339 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_ecc_pub_key_attrs[] = {
340 	{
341 	.attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_X,
342 	.flags = TEE_TYPE_ATTR_REQUIRED,
343 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
344 	RAW_DATA(struct ecc_public_key, x)
345 	},
346 
347 	{
348 	.attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_Y,
349 	.flags = TEE_TYPE_ATTR_REQUIRED,
350 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
351 	RAW_DATA(struct ecc_public_key, y)
352 	},
353 
354 	{
355 	.attr_id = TEE_ATTR_ECC_CURVE,
356 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR,
357 	.ops_index = ATTR_OPS_INDEX_VALUE,
358 	RAW_DATA(struct ecc_public_key, curve)
359 	},
360 };
361 
362 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_ecc_keypair_attrs[] = {
363 	{
364 	.attr_id = TEE_ATTR_ECC_PRIVATE_VALUE,
365 	.flags = TEE_TYPE_ATTR_REQUIRED,
366 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
367 	RAW_DATA(struct ecc_keypair, d)
368 	},
369 
370 	{
371 	.attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_X,
372 	.flags = TEE_TYPE_ATTR_REQUIRED,
373 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
374 	RAW_DATA(struct ecc_keypair, x)
375 	},
376 
377 	{
378 	.attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_Y,
379 	.flags = TEE_TYPE_ATTR_REQUIRED,
380 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
381 	RAW_DATA(struct ecc_keypair, y)
382 	},
383 
384 	{
385 	.attr_id = TEE_ATTR_ECC_CURVE,
386 	.flags = TEE_TYPE_ATTR_REQUIRED | TEE_TYPE_ATTR_SIZE_INDICATOR |
387 		 TEE_TYPE_ATTR_GEN_KEY_REQ,
388 	.ops_index = ATTR_OPS_INDEX_VALUE,
389 	RAW_DATA(struct ecc_keypair, curve)
390 	},
391 };
392 
393 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_sm2_pub_key_attrs[] = {
394 	{
395 	.attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_X,
396 	.flags = TEE_TYPE_ATTR_REQUIRED,
397 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
398 	RAW_DATA(struct ecc_public_key, x)
399 	},
400 
401 	{
402 	.attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_Y,
403 	.flags = TEE_TYPE_ATTR_REQUIRED,
404 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
405 	RAW_DATA(struct ecc_public_key, y)
406 	},
407 };
408 
409 static const struct tee_cryp_obj_type_attrs tee_cryp_obj_sm2_keypair_attrs[] = {
410 	{
411 	.attr_id = TEE_ATTR_ECC_PRIVATE_VALUE,
412 	.flags = TEE_TYPE_ATTR_REQUIRED,
413 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
414 	RAW_DATA(struct ecc_keypair, d)
415 	},
416 
417 	{
418 	.attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_X,
419 	.flags = TEE_TYPE_ATTR_REQUIRED,
420 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
421 	RAW_DATA(struct ecc_keypair, x)
422 	},
423 
424 	{
425 	.attr_id = TEE_ATTR_ECC_PUBLIC_VALUE_Y,
426 	.flags = TEE_TYPE_ATTR_REQUIRED,
427 	.ops_index = ATTR_OPS_INDEX_BIGNUM,
428 	RAW_DATA(struct ecc_keypair, y)
429 	},
430 };
431 
432 static
433 const struct tee_cryp_obj_type_attrs tee_cryp_obj_x25519_keypair_attrs[] = {
434 	{
435 	.attr_id = TEE_ATTR_X25519_PRIVATE_VALUE,
436 	.flags = TEE_TYPE_ATTR_REQUIRED,
437 	.ops_index = ATTR_OPS_INDEX_25519,
438 	RAW_DATA(struct x25519_keypair, priv)
439 	},
440 
441 	{
442 	.attr_id = TEE_ATTR_X25519_PUBLIC_VALUE,
443 	.flags = TEE_TYPE_ATTR_REQUIRED,
444 	.ops_index = ATTR_OPS_INDEX_25519,
445 	RAW_DATA(struct x25519_keypair, pub)
446 	},
447 };
448 
449 struct tee_cryp_obj_type_props {
450 	TEE_ObjectType obj_type;
451 	uint16_t min_size;	/* may not be smaller than this */
452 	uint16_t max_size;	/* may not be larger than this */
453 	uint16_t alloc_size;	/* this many bytes are allocated to hold data */
454 	uint8_t quanta;		/* may only be an multiple of this */
455 
456 	uint8_t num_type_attrs;
457 	const struct tee_cryp_obj_type_attrs *type_attrs;
458 };
459 
460 #define PROP(obj_type, quanta, min_size, max_size, alloc_size, type_attrs) \
461 		{ (obj_type), (min_size), (max_size), (alloc_size), (quanta), \
462 		  ARRAY_SIZE(type_attrs), (type_attrs) }
463 
464 static const struct tee_cryp_obj_type_props tee_cryp_obj_props[] = {
465 	PROP(TEE_TYPE_AES, 64, 128, 256,	/* valid sizes 128, 192, 256 */
466 		256 / 8 + sizeof(struct tee_cryp_obj_secret),
467 		tee_cryp_obj_secret_value_attrs),
468 	PROP(TEE_TYPE_DES, 64, 64, 64,
469 	     /* Valid size 64 with parity */
470 	     64 / 8 + sizeof(struct tee_cryp_obj_secret),
471 	     tee_cryp_obj_secret_value_attrs),
472 	PROP(TEE_TYPE_DES3, 64, 128, 192,
473 	     /* Valid sizes 128, 192 with parity */
474 	     192 / 8 + sizeof(struct tee_cryp_obj_secret),
475 	     tee_cryp_obj_secret_value_attrs),
476 	PROP(TEE_TYPE_SM4, 128, 128, 128,
477 		128 / 8 + sizeof(struct tee_cryp_obj_secret),
478 		tee_cryp_obj_secret_value_attrs),
479 	PROP(TEE_TYPE_HMAC_MD5, 8, 64, 512,
480 		512 / 8 + sizeof(struct tee_cryp_obj_secret),
481 		tee_cryp_obj_secret_value_attrs),
482 	PROP(TEE_TYPE_HMAC_SHA1, 8, 80, 512,
483 		512 / 8 + sizeof(struct tee_cryp_obj_secret),
484 		tee_cryp_obj_secret_value_attrs),
485 	PROP(TEE_TYPE_HMAC_SHA224, 8, 112, 512,
486 		512 / 8 + sizeof(struct tee_cryp_obj_secret),
487 		tee_cryp_obj_secret_value_attrs),
488 	PROP(TEE_TYPE_HMAC_SHA256, 8, 192, 1024,
489 		1024 / 8 + sizeof(struct tee_cryp_obj_secret),
490 		tee_cryp_obj_secret_value_attrs),
491 	PROP(TEE_TYPE_HMAC_SHA384, 8, 256, 1024,
492 		1024 / 8 + sizeof(struct tee_cryp_obj_secret),
493 		tee_cryp_obj_secret_value_attrs),
494 	PROP(TEE_TYPE_HMAC_SHA512, 8, 256, 1024,
495 		1024 / 8 + sizeof(struct tee_cryp_obj_secret),
496 		tee_cryp_obj_secret_value_attrs),
497 	PROP(TEE_TYPE_HMAC_SM3, 8, 80, 1024,
498 		512 / 8 + sizeof(struct tee_cryp_obj_secret),
499 		tee_cryp_obj_secret_value_attrs),
500 	PROP(TEE_TYPE_GENERIC_SECRET, 8, 0, 4096,
501 		4096 / 8 + sizeof(struct tee_cryp_obj_secret),
502 		tee_cryp_obj_secret_value_attrs),
503 #if defined(CFG_CRYPTO_HKDF)
504 	PROP(TEE_TYPE_HKDF_IKM, 8, 0, 4096,
505 		4096 / 8 + sizeof(struct tee_cryp_obj_secret),
506 		tee_cryp_obj_hkdf_ikm_attrs),
507 #endif
508 #if defined(CFG_CRYPTO_CONCAT_KDF)
509 	PROP(TEE_TYPE_CONCAT_KDF_Z, 8, 0, 4096,
510 		4096 / 8 + sizeof(struct tee_cryp_obj_secret),
511 		tee_cryp_obj_concat_kdf_z_attrs),
512 #endif
513 #if defined(CFG_CRYPTO_PBKDF2)
514 	PROP(TEE_TYPE_PBKDF2_PASSWORD, 8, 0, 4096,
515 		4096 / 8 + sizeof(struct tee_cryp_obj_secret),
516 		tee_cryp_obj_pbkdf2_passwd_attrs),
517 #endif
518 	PROP(TEE_TYPE_RSA_PUBLIC_KEY, 1, 256, CFG_CORE_BIGNUM_MAX_BITS,
519 		sizeof(struct rsa_public_key),
520 		tee_cryp_obj_rsa_pub_key_attrs),
521 
522 	PROP(TEE_TYPE_RSA_KEYPAIR, 1, 256, CFG_CORE_BIGNUM_MAX_BITS,
523 		sizeof(struct rsa_keypair),
524 		tee_cryp_obj_rsa_keypair_attrs),
525 
526 	PROP(TEE_TYPE_DSA_PUBLIC_KEY, 64, 512, 3072,
527 		sizeof(struct dsa_public_key),
528 		tee_cryp_obj_dsa_pub_key_attrs),
529 
530 	PROP(TEE_TYPE_DSA_KEYPAIR, 64, 512, 3072,
531 		sizeof(struct dsa_keypair),
532 		tee_cryp_obj_dsa_keypair_attrs),
533 
534 	PROP(TEE_TYPE_DH_KEYPAIR, 1, 256, 2048,
535 		sizeof(struct dh_keypair),
536 		tee_cryp_obj_dh_keypair_attrs),
537 
538 	PROP(TEE_TYPE_ECDSA_PUBLIC_KEY, 1, 192, 521,
539 		sizeof(struct ecc_public_key),
540 		tee_cryp_obj_ecc_pub_key_attrs),
541 
542 	PROP(TEE_TYPE_ECDSA_KEYPAIR, 1, 192, 521,
543 		sizeof(struct ecc_keypair),
544 		tee_cryp_obj_ecc_keypair_attrs),
545 
546 	PROP(TEE_TYPE_ECDH_PUBLIC_KEY, 1, 192, 521,
547 		sizeof(struct ecc_public_key),
548 		tee_cryp_obj_ecc_pub_key_attrs),
549 
550 	PROP(TEE_TYPE_ECDH_KEYPAIR, 1, 192, 521,
551 		sizeof(struct ecc_keypair),
552 		tee_cryp_obj_ecc_keypair_attrs),
553 
554 	PROP(TEE_TYPE_SM2_DSA_PUBLIC_KEY, 1, 256, 256,
555 	     sizeof(struct ecc_public_key),
556 	     tee_cryp_obj_sm2_pub_key_attrs),
557 
558 	PROP(TEE_TYPE_SM2_DSA_KEYPAIR, 1, 256, 256,
559 	     sizeof(struct ecc_keypair),
560 	     tee_cryp_obj_sm2_keypair_attrs),
561 
562 	PROP(TEE_TYPE_SM2_PKE_PUBLIC_KEY, 1, 256, 256,
563 	     sizeof(struct ecc_public_key),
564 	     tee_cryp_obj_sm2_pub_key_attrs),
565 
566 	PROP(TEE_TYPE_SM2_PKE_KEYPAIR, 1, 256, 256,
567 	     sizeof(struct ecc_keypair),
568 	     tee_cryp_obj_sm2_keypair_attrs),
569 
570 	PROP(TEE_TYPE_SM2_KEP_PUBLIC_KEY, 1, 256, 256,
571 	     sizeof(struct ecc_public_key),
572 	     tee_cryp_obj_sm2_pub_key_attrs),
573 
574 	PROP(TEE_TYPE_SM2_KEP_KEYPAIR, 1, 256, 256,
575 	     sizeof(struct ecc_keypair),
576 	     tee_cryp_obj_sm2_keypair_attrs),
577 
578 	PROP(TEE_TYPE_X25519_KEYPAIR, 1, 256, 256,
579 	     sizeof(struct x25519_keypair),
580 	     tee_cryp_obj_x25519_keypair_attrs),
581 };
582 
583 struct attr_ops {
584 	TEE_Result (*from_user)(void *attr, const void *buffer, size_t size);
585 	TEE_Result (*to_user)(void *attr, struct ts_session *sess,
586 			      void *buffer, uint64_t *size);
587 	TEE_Result (*to_binary)(void *attr, void *data, size_t data_len,
588 			    size_t *offs);
589 	bool (*from_binary)(void *attr, const void *data, size_t data_len,
590 			    size_t *offs);
591 	TEE_Result (*from_obj)(void *attr, void *src_attr);
592 	void (*free)(void *attr);
593 	void (*clear)(void *attr);
594 };
595 
596 static TEE_Result op_u32_to_binary_helper(uint32_t v, uint8_t *data,
597 				    size_t data_len, size_t *offs)
598 {
599 	uint32_t field;
600 	size_t next_offs;
601 
602 	if (ADD_OVERFLOW(*offs, sizeof(field), &next_offs))
603 		return TEE_ERROR_OVERFLOW;
604 
605 	if (data && next_offs <= data_len) {
606 		field = TEE_U32_TO_BIG_ENDIAN(v);
607 		memcpy(data + *offs, &field, sizeof(field));
608 	}
609 	(*offs) = next_offs;
610 
611 	return TEE_SUCCESS;
612 }
613 
614 static bool op_u32_from_binary_helper(uint32_t *v, const uint8_t *data,
615 				      size_t data_len, size_t *offs)
616 {
617 	uint32_t field;
618 
619 	if (!data || (*offs + sizeof(field)) > data_len)
620 		return false;
621 
622 	memcpy(&field, data + *offs, sizeof(field));
623 	*v = TEE_U32_FROM_BIG_ENDIAN(field);
624 	(*offs) += sizeof(field);
625 	return true;
626 }
627 
628 static TEE_Result op_attr_secret_value_from_user(void *attr, const void *buffer,
629 						 size_t size)
630 {
631 	struct tee_cryp_obj_secret *key = attr;
632 
633 	/* Data size has to fit in allocated buffer */
634 	if (size > key->alloc_size)
635 		return TEE_ERROR_SECURITY;
636 	memcpy(key + 1, buffer, size);
637 	key->key_size = size;
638 	return TEE_SUCCESS;
639 }
640 
641 static TEE_Result op_attr_secret_value_to_user(void *attr,
642 					       struct ts_session *sess __unused,
643 					       void *buffer, uint64_t *size)
644 {
645 	TEE_Result res;
646 	struct tee_cryp_obj_secret *key = attr;
647 	uint64_t s;
648 	uint64_t key_size;
649 
650 	res = copy_from_user(&s, size, sizeof(s));
651 	if (res != TEE_SUCCESS)
652 		return res;
653 
654 	key_size = key->key_size;
655 	res = copy_to_user(size, &key_size, sizeof(key_size));
656 	if (res != TEE_SUCCESS)
657 		return res;
658 
659 	if (s < key->key_size || !buffer)
660 		return TEE_ERROR_SHORT_BUFFER;
661 
662 	return copy_to_user(buffer, key + 1, key->key_size);
663 }
664 
665 static TEE_Result op_attr_secret_value_to_binary(void *attr, void *data,
666 					   size_t data_len, size_t *offs)
667 {
668 	TEE_Result res;
669 	struct tee_cryp_obj_secret *key = attr;
670 	size_t next_offs;
671 
672 	res = op_u32_to_binary_helper(key->key_size, data, data_len, offs);
673 	if (res != TEE_SUCCESS)
674 		return res;
675 
676 	if (ADD_OVERFLOW(*offs, key->key_size, &next_offs))
677 		return TEE_ERROR_OVERFLOW;
678 
679 	if (data && next_offs <= data_len)
680 		memcpy((uint8_t *)data + *offs, key + 1, key->key_size);
681 	(*offs) = next_offs;
682 
683 	return TEE_SUCCESS;
684 }
685 
686 static bool op_attr_secret_value_from_binary(void *attr, const void *data,
687 					     size_t data_len, size_t *offs)
688 {
689 	struct tee_cryp_obj_secret *key = attr;
690 	uint32_t s;
691 
692 	if (!op_u32_from_binary_helper(&s, data, data_len, offs))
693 		return false;
694 
695 	if ((*offs + s) > data_len)
696 		return false;
697 
698 	/* Data size has to fit in allocated buffer */
699 	if (s > key->alloc_size)
700 		return false;
701 	key->key_size = s;
702 	memcpy(key + 1, (const uint8_t *)data + *offs, s);
703 	(*offs) += s;
704 	return true;
705 }
706 
707 
708 static TEE_Result op_attr_secret_value_from_obj(void *attr, void *src_attr)
709 {
710 	struct tee_cryp_obj_secret *key = attr;
711 	struct tee_cryp_obj_secret *src_key = src_attr;
712 
713 	if (src_key->key_size > key->alloc_size)
714 		return TEE_ERROR_BAD_STATE;
715 	memcpy(key + 1, src_key + 1, src_key->key_size);
716 	key->key_size = src_key->key_size;
717 	return TEE_SUCCESS;
718 }
719 
720 static void op_attr_secret_value_clear(void *attr)
721 {
722 	struct tee_cryp_obj_secret *key = attr;
723 
724 	key->key_size = 0;
725 	memzero_explicit(key + 1, key->alloc_size);
726 }
727 
728 static TEE_Result op_attr_bignum_from_user(void *attr, const void *buffer,
729 					   size_t size)
730 {
731 	struct bignum **bn = attr;
732 
733 	return crypto_bignum_bin2bn(buffer, size, *bn);
734 }
735 
736 static TEE_Result op_attr_bignum_to_user(void *attr,
737 					 struct ts_session *sess,
738 					 void *buffer, uint64_t *size)
739 {
740 	TEE_Result res = TEE_SUCCESS;
741 	struct bignum **bn = attr;
742 	uint64_t req_size = 0;
743 	uint64_t s = 0;
744 
745 	res = copy_from_user(&s, size, sizeof(s));
746 	if (res != TEE_SUCCESS)
747 		return res;
748 
749 	req_size = crypto_bignum_num_bytes(*bn);
750 	res = copy_to_user(size, &req_size, sizeof(req_size));
751 	if (res != TEE_SUCCESS)
752 		return res;
753 	if (!req_size)
754 		return TEE_SUCCESS;
755 	if (s < req_size || !buffer)
756 		return TEE_ERROR_SHORT_BUFFER;
757 
758 	buffer = memtag_strip_tag(buffer);
759 
760 	/* Check we can access data using supplied user mode pointer */
761 	res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
762 				     TEE_MEMORY_ACCESS_READ |
763 				     TEE_MEMORY_ACCESS_WRITE |
764 				     TEE_MEMORY_ACCESS_ANY_OWNER,
765 				     (uaddr_t)buffer, req_size);
766 	if (res != TEE_SUCCESS)
767 		return res;
768 	/*
769 	* Write the bignum (wich raw data points to) into an array of
770 	* bytes (stored in buffer)
771 	*/
772 	crypto_bignum_bn2bin(*bn, buffer);
773 	return TEE_SUCCESS;
774 }
775 
776 static TEE_Result op_attr_bignum_to_binary(void *attr, void *data,
777 					   size_t data_len, size_t *offs)
778 {
779 	TEE_Result res;
780 	struct bignum **bn = attr;
781 	uint32_t n = crypto_bignum_num_bytes(*bn);
782 	size_t next_offs;
783 
784 	res = op_u32_to_binary_helper(n, data, data_len, offs);
785 	if (res != TEE_SUCCESS)
786 		return res;
787 
788 	if (ADD_OVERFLOW(*offs, n, &next_offs))
789 		return TEE_ERROR_OVERFLOW;
790 
791 	if (data && next_offs <= data_len)
792 		crypto_bignum_bn2bin(*bn, (uint8_t *)data + *offs);
793 	(*offs) = next_offs;
794 
795 	return TEE_SUCCESS;
796 }
797 
798 static bool op_attr_bignum_from_binary(void *attr, const void *data,
799 				       size_t data_len, size_t *offs)
800 {
801 	struct bignum **bn = attr;
802 	uint32_t n;
803 
804 	if (!op_u32_from_binary_helper(&n, data, data_len, offs))
805 		return false;
806 
807 	if ((*offs + n) > data_len)
808 		return false;
809 	if (crypto_bignum_bin2bn((const uint8_t *)data + *offs, n, *bn))
810 		return false;
811 	(*offs) += n;
812 	return true;
813 }
814 
815 static TEE_Result op_attr_bignum_from_obj(void *attr, void *src_attr)
816 {
817 	struct bignum **bn = attr;
818 	struct bignum **src_bn = src_attr;
819 
820 	crypto_bignum_copy(*bn, *src_bn);
821 	return TEE_SUCCESS;
822 }
823 
824 static void op_attr_bignum_clear(void *attr)
825 {
826 	struct bignum **bn = attr;
827 
828 	crypto_bignum_clear(*bn);
829 }
830 
831 static void op_attr_bignum_free(void *attr)
832 {
833 	struct bignum **bn = attr;
834 
835 	crypto_bignum_free(*bn);
836 	*bn = NULL;
837 }
838 
839 static TEE_Result op_attr_value_from_user(void *attr, const void *buffer,
840 					  size_t size)
841 {
842 	uint32_t *v = attr;
843 
844 	if (size != sizeof(uint32_t) * 2)
845 		return TEE_ERROR_GENERIC; /* "can't happen */
846 
847 	/* Note that only the first value is copied */
848 	memcpy(v, buffer, sizeof(uint32_t));
849 	return TEE_SUCCESS;
850 }
851 
852 static TEE_Result op_attr_value_to_user(void *attr,
853 					struct ts_session *sess __unused,
854 					void *buffer, uint64_t *size)
855 {
856 	TEE_Result res;
857 	uint32_t *v = attr;
858 	uint64_t s;
859 	uint32_t value[2] = { *v };
860 	uint64_t req_size = sizeof(value);
861 
862 	res = copy_from_user(&s, size, sizeof(s));
863 	if (res != TEE_SUCCESS)
864 		return res;
865 
866 	if (s < req_size || !buffer)
867 		return TEE_ERROR_SHORT_BUFFER;
868 
869 	return copy_to_user(buffer, value, req_size);
870 }
871 
872 static TEE_Result op_attr_value_to_binary(void *attr, void *data,
873 					  size_t data_len, size_t *offs)
874 {
875 	uint32_t *v = attr;
876 
877 	return op_u32_to_binary_helper(*v, data, data_len, offs);
878 }
879 
880 static bool op_attr_value_from_binary(void *attr, const void *data,
881 				      size_t data_len, size_t *offs)
882 {
883 	uint32_t *v = attr;
884 
885 	return op_u32_from_binary_helper(v, data, data_len, offs);
886 }
887 
888 static TEE_Result op_attr_value_from_obj(void *attr, void *src_attr)
889 {
890 	uint32_t *v = attr;
891 	uint32_t *src_v = src_attr;
892 
893 	*v = *src_v;
894 	return TEE_SUCCESS;
895 }
896 
897 static void op_attr_value_clear(void *attr)
898 {
899 	uint32_t *v = attr;
900 
901 	*v = 0;
902 }
903 
904 static TEE_Result op_attr_25519_from_user(void *attr, const void *buffer,
905 					  size_t size)
906 {
907 	uint8_t **key = attr;
908 
909 	if (size != KEY_SIZE_BYTES_25519 || !*key)
910 		return TEE_ERROR_SECURITY;
911 
912 	memcpy(*key, buffer, size);
913 
914 	return TEE_SUCCESS;
915 }
916 
917 static TEE_Result op_attr_25519_to_user(void *attr,
918 					struct ts_session *sess __unused,
919 					void *buffer, uint64_t *size)
920 {
921 	TEE_Result res = TEE_ERROR_GENERIC;
922 	uint8_t **key = attr;
923 	uint64_t s = 0;
924 	uint64_t key_size = (uint64_t)KEY_SIZE_BYTES_25519;
925 
926 	res = copy_from_user(&s, size, sizeof(s));
927 	if (res != TEE_SUCCESS)
928 		return res;
929 
930 	res = copy_to_user(size, &key_size, sizeof(key_size));
931 	if (res != TEE_SUCCESS)
932 		return res;
933 
934 	if (s < key_size || !buffer)
935 		return TEE_ERROR_SHORT_BUFFER;
936 
937 	return copy_to_user(buffer, *key, key_size);
938 }
939 
940 static TEE_Result op_attr_25519_to_binary(void *attr, void *data,
941 					  size_t data_len, size_t *offs)
942 {
943 	TEE_Result res = TEE_ERROR_GENERIC;
944 	uint8_t **key = attr;
945 	size_t next_offs = 0;
946 	uint64_t key_size = (uint64_t)KEY_SIZE_BYTES_25519;
947 
948 	res = op_u32_to_binary_helper(key_size, data, data_len, offs);
949 	if (res != TEE_SUCCESS)
950 		return res;
951 
952 	if (ADD_OVERFLOW(*offs, key_size, &next_offs))
953 		return TEE_ERROR_OVERFLOW;
954 
955 	if (data && next_offs <= data_len)
956 		memcpy((uint8_t *)data + *offs, *key, key_size);
957 	*offs = next_offs;
958 
959 	return TEE_SUCCESS;
960 }
961 
962 static bool op_attr_25519_from_binary(void *attr, const void *data,
963 				      size_t data_len, size_t *offs)
964 {
965 	uint8_t **key = attr;
966 	uint32_t s = 0;
967 
968 	if (!op_u32_from_binary_helper(&s, data, data_len, offs))
969 		return false;
970 
971 	if (*offs + s > data_len)
972 		return false;
973 
974 	if (s > (uint32_t)KEY_SIZE_BYTES_25519)
975 		return false;
976 
977 	memcpy(*key, (const uint8_t *)data + *offs, s);
978 	*offs += s;
979 	return true;
980 }
981 
982 static TEE_Result op_attr_25519_from_obj(void *attr, void *src_attr)
983 {
984 	uint8_t **key = attr;
985 	uint8_t **src_key = src_attr;
986 
987 	if (!*key || !*src_key)
988 		return TEE_ERROR_SECURITY;
989 
990 	memcpy(*key, *src_key, KEY_SIZE_BYTES_25519);
991 
992 	return TEE_SUCCESS;
993 }
994 
995 static void op_attr_25519_clear(void *attr)
996 {
997 	uint8_t **key = attr;
998 
999 	assert(*key);
1000 
1001 	memzero_explicit(*key, KEY_SIZE_BYTES_25519);
1002 }
1003 
1004 static void op_attr_25519_free(void *attr)
1005 {
1006 	uint8_t **key = attr;
1007 
1008 	op_attr_25519_clear(attr);
1009 	free(*key);
1010 }
1011 
1012 static const struct attr_ops attr_ops[] = {
1013 	[ATTR_OPS_INDEX_SECRET] = {
1014 		.from_user = op_attr_secret_value_from_user,
1015 		.to_user = op_attr_secret_value_to_user,
1016 		.to_binary = op_attr_secret_value_to_binary,
1017 		.from_binary = op_attr_secret_value_from_binary,
1018 		.from_obj = op_attr_secret_value_from_obj,
1019 		.free = op_attr_secret_value_clear, /* not a typo */
1020 		.clear = op_attr_secret_value_clear,
1021 	},
1022 	[ATTR_OPS_INDEX_BIGNUM] = {
1023 		.from_user = op_attr_bignum_from_user,
1024 		.to_user = op_attr_bignum_to_user,
1025 		.to_binary = op_attr_bignum_to_binary,
1026 		.from_binary = op_attr_bignum_from_binary,
1027 		.from_obj = op_attr_bignum_from_obj,
1028 		.free = op_attr_bignum_free,
1029 		.clear = op_attr_bignum_clear,
1030 	},
1031 	[ATTR_OPS_INDEX_VALUE] = {
1032 		.from_user = op_attr_value_from_user,
1033 		.to_user = op_attr_value_to_user,
1034 		.to_binary = op_attr_value_to_binary,
1035 		.from_binary = op_attr_value_from_binary,
1036 		.from_obj = op_attr_value_from_obj,
1037 		.free = op_attr_value_clear, /* not a typo */
1038 		.clear = op_attr_value_clear,
1039 	},
1040 	[ATTR_OPS_INDEX_25519] = {
1041 		.from_user = op_attr_25519_from_user,
1042 		.to_user = op_attr_25519_to_user,
1043 		.to_binary = op_attr_25519_to_binary,
1044 		.from_binary = op_attr_25519_from_binary,
1045 		.from_obj = op_attr_25519_from_obj,
1046 		.free = op_attr_25519_free,
1047 		.clear = op_attr_25519_clear,
1048 	},
1049 };
1050 
1051 static TEE_Result get_user_u64_as_size_t(size_t *dst, uint64_t *src)
1052 {
1053 	uint64_t d = 0;
1054 	TEE_Result res = copy_from_user(&d, src, sizeof(d));
1055 
1056 	/*
1057 	 * On 32-bit systems a size_t can't hold a uint64_t so we need to
1058 	 * check that the value isn't too large.
1059 	 */
1060 	if (!res && ADD_OVERFLOW(0, d, dst))
1061 		return TEE_ERROR_OVERFLOW;
1062 
1063 	return res;
1064 }
1065 
1066 static TEE_Result put_user_u64(uint64_t *dst, size_t value)
1067 {
1068 	uint64_t v = value;
1069 
1070 	return copy_to_user(dst, &v, sizeof(v));
1071 }
1072 
1073 TEE_Result syscall_cryp_obj_get_info(unsigned long obj, TEE_ObjectInfo *info)
1074 {
1075 	struct ts_session *sess = ts_get_current_session();
1076 	TEE_Result res = TEE_SUCCESS;
1077 	struct tee_obj *o = NULL;
1078 
1079 	res = tee_obj_get(to_user_ta_ctx(sess->ctx),
1080 			  uref_to_vaddr(obj), &o);
1081 	if (res != TEE_SUCCESS)
1082 		goto exit;
1083 
1084 	res = copy_to_user_private(info, &o->info, sizeof(o->info));
1085 
1086 exit:
1087 	return res;
1088 }
1089 
1090 TEE_Result syscall_cryp_obj_restrict_usage(unsigned long obj,
1091 			unsigned long usage)
1092 {
1093 	struct ts_session *sess = ts_get_current_session();
1094 	TEE_Result res = TEE_SUCCESS;
1095 	struct tee_obj *o = NULL;
1096 
1097 	res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o);
1098 	if (res != TEE_SUCCESS)
1099 		goto exit;
1100 
1101 	o->info.objectUsage &= usage;
1102 
1103 exit:
1104 	return res;
1105 }
1106 
1107 static int tee_svc_cryp_obj_find_type_attr_idx(
1108 		uint32_t attr_id,
1109 		const struct tee_cryp_obj_type_props *type_props)
1110 {
1111 	size_t n;
1112 
1113 	for (n = 0; n < type_props->num_type_attrs; n++) {
1114 		if (attr_id == type_props->type_attrs[n].attr_id)
1115 			return n;
1116 	}
1117 	return -1;
1118 }
1119 
1120 static const struct tee_cryp_obj_type_props *tee_svc_find_type_props(
1121 		TEE_ObjectType obj_type)
1122 {
1123 	size_t n;
1124 
1125 	for (n = 0; n < ARRAY_SIZE(tee_cryp_obj_props); n++) {
1126 		if (tee_cryp_obj_props[n].obj_type == obj_type)
1127 			return tee_cryp_obj_props + n;
1128 	}
1129 
1130 	return NULL;
1131 }
1132 
1133 /* Set an attribute on an object */
1134 static void set_attribute(struct tee_obj *o,
1135 			  const struct tee_cryp_obj_type_props *props,
1136 			  uint32_t attr)
1137 {
1138 	int idx = tee_svc_cryp_obj_find_type_attr_idx(attr, props);
1139 
1140 	if (idx < 0)
1141 		return;
1142 	o->have_attrs |= BIT(idx);
1143 }
1144 
1145 /* Get an attribute on an object */
1146 static uint32_t get_attribute(const struct tee_obj *o,
1147 			      const struct tee_cryp_obj_type_props *props,
1148 			      uint32_t attr)
1149 {
1150 	int idx = tee_svc_cryp_obj_find_type_attr_idx(attr, props);
1151 
1152 	if (idx < 0)
1153 		return 0;
1154 	return o->have_attrs & BIT(idx);
1155 }
1156 
1157 TEE_Result syscall_cryp_obj_get_attr(unsigned long obj, unsigned long attr_id,
1158 			void *buffer, uint64_t *size)
1159 {
1160 	struct ts_session *sess = ts_get_current_session();
1161 	TEE_Result res = TEE_SUCCESS;
1162 	struct tee_obj *o = NULL;
1163 	const struct tee_cryp_obj_type_props *type_props = NULL;
1164 	int idx = 0;
1165 	const struct attr_ops *ops = NULL;
1166 	void *attr = NULL;
1167 
1168 	res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o);
1169 	if (res != TEE_SUCCESS)
1170 		return TEE_ERROR_ITEM_NOT_FOUND;
1171 
1172 	/* Check that the object is initialized */
1173 	if (!(o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED))
1174 		return TEE_ERROR_BAD_PARAMETERS;
1175 
1176 	/* Check that getting the attribute is allowed */
1177 	if (!(attr_id & TEE_ATTR_FLAG_PUBLIC) &&
1178 	    !(o->info.objectUsage & TEE_USAGE_EXTRACTABLE))
1179 		return TEE_ERROR_BAD_PARAMETERS;
1180 
1181 	type_props = tee_svc_find_type_props(o->info.objectType);
1182 	if (!type_props) {
1183 		/* Unknown object type, "can't happen" */
1184 		return TEE_ERROR_BAD_STATE;
1185 	}
1186 
1187 	idx = tee_svc_cryp_obj_find_type_attr_idx(attr_id, type_props);
1188 	if ((idx < 0) || ((o->have_attrs & (1 << idx)) == 0))
1189 		return TEE_ERROR_ITEM_NOT_FOUND;
1190 
1191 	ops = attr_ops + type_props->type_attrs[idx].ops_index;
1192 	attr = (uint8_t *)o->attr + type_props->type_attrs[idx].raw_offs;
1193 	return ops->to_user(attr, sess, buffer, size);
1194 }
1195 
1196 void tee_obj_attr_free(struct tee_obj *o)
1197 {
1198 	const struct tee_cryp_obj_type_props *tp;
1199 	size_t n;
1200 
1201 	if (!o->attr)
1202 		return;
1203 	tp = tee_svc_find_type_props(o->info.objectType);
1204 	if (!tp)
1205 		return;
1206 
1207 	for (n = 0; n < tp->num_type_attrs; n++) {
1208 		const struct tee_cryp_obj_type_attrs *ta = tp->type_attrs + n;
1209 
1210 		attr_ops[ta->ops_index].free((uint8_t *)o->attr + ta->raw_offs);
1211 	}
1212 }
1213 
1214 void tee_obj_attr_clear(struct tee_obj *o)
1215 {
1216 	const struct tee_cryp_obj_type_props *tp;
1217 	size_t n;
1218 
1219 	if (!o->attr)
1220 		return;
1221 	tp = tee_svc_find_type_props(o->info.objectType);
1222 	if (!tp)
1223 		return;
1224 
1225 	for (n = 0; n < tp->num_type_attrs; n++) {
1226 		const struct tee_cryp_obj_type_attrs *ta = tp->type_attrs + n;
1227 
1228 		attr_ops[ta->ops_index].clear((uint8_t *)o->attr +
1229 					      ta->raw_offs);
1230 	}
1231 }
1232 
1233 TEE_Result tee_obj_attr_to_binary(struct tee_obj *o, void *data,
1234 				  size_t *data_len)
1235 {
1236 	const struct tee_cryp_obj_type_props *tp;
1237 	size_t n;
1238 	size_t offs = 0;
1239 	size_t len = data ? *data_len : 0;
1240 	TEE_Result res;
1241 
1242 	if (o->info.objectType == TEE_TYPE_DATA) {
1243 		*data_len = 0;
1244 		return TEE_SUCCESS; /* pure data object */
1245 	}
1246 	if (!o->attr)
1247 		return TEE_ERROR_BAD_STATE;
1248 	tp = tee_svc_find_type_props(o->info.objectType);
1249 	if (!tp)
1250 		return TEE_ERROR_BAD_STATE;
1251 
1252 	for (n = 0; n < tp->num_type_attrs; n++) {
1253 		const struct tee_cryp_obj_type_attrs *ta = tp->type_attrs + n;
1254 		void *attr = (uint8_t *)o->attr + ta->raw_offs;
1255 
1256 		res = attr_ops[ta->ops_index].to_binary(attr, data, len, &offs);
1257 		if (res != TEE_SUCCESS)
1258 			return res;
1259 	}
1260 
1261 	*data_len = offs;
1262 	if (data && offs > len)
1263 		return TEE_ERROR_SHORT_BUFFER;
1264 	return TEE_SUCCESS;
1265 }
1266 
1267 TEE_Result tee_obj_attr_from_binary(struct tee_obj *o, const void *data,
1268 				    size_t data_len)
1269 {
1270 	const struct tee_cryp_obj_type_props *tp;
1271 	size_t n;
1272 	size_t offs = 0;
1273 
1274 	if (o->info.objectType == TEE_TYPE_DATA)
1275 		return TEE_SUCCESS; /* pure data object */
1276 	if (!o->attr)
1277 		return TEE_ERROR_BAD_STATE;
1278 	tp = tee_svc_find_type_props(o->info.objectType);
1279 	if (!tp)
1280 		return TEE_ERROR_BAD_STATE;
1281 
1282 	for (n = 0; n < tp->num_type_attrs; n++) {
1283 		const struct tee_cryp_obj_type_attrs *ta = tp->type_attrs + n;
1284 		void *attr = (uint8_t *)o->attr + ta->raw_offs;
1285 
1286 		if (!attr_ops[ta->ops_index].from_binary(attr, data, data_len,
1287 							 &offs))
1288 			return TEE_ERROR_CORRUPT_OBJECT;
1289 	}
1290 	return TEE_SUCCESS;
1291 }
1292 
1293 TEE_Result tee_obj_attr_copy_from(struct tee_obj *o, const struct tee_obj *src)
1294 {
1295 	TEE_Result res;
1296 	const struct tee_cryp_obj_type_props *tp;
1297 	const struct tee_cryp_obj_type_attrs *ta;
1298 	size_t n;
1299 	uint32_t have_attrs = 0;
1300 	void *attr;
1301 	void *src_attr;
1302 
1303 	if (o->info.objectType == TEE_TYPE_DATA)
1304 		return TEE_SUCCESS; /* pure data object */
1305 	if (!o->attr)
1306 		return TEE_ERROR_BAD_STATE;
1307 	tp = tee_svc_find_type_props(o->info.objectType);
1308 	if (!tp)
1309 		return TEE_ERROR_BAD_STATE;
1310 
1311 	if (o->info.objectType == src->info.objectType) {
1312 		have_attrs = src->have_attrs;
1313 		for (n = 0; n < tp->num_type_attrs; n++) {
1314 			ta = tp->type_attrs + n;
1315 			attr = (uint8_t *)o->attr + ta->raw_offs;
1316 			src_attr = (uint8_t *)src->attr + ta->raw_offs;
1317 			res = attr_ops[ta->ops_index].from_obj(attr, src_attr);
1318 			if (res != TEE_SUCCESS)
1319 				return res;
1320 		}
1321 	} else {
1322 		const struct tee_cryp_obj_type_props *tp_src;
1323 		int idx;
1324 
1325 		if (o->info.objectType == TEE_TYPE_RSA_PUBLIC_KEY) {
1326 			if (src->info.objectType != TEE_TYPE_RSA_KEYPAIR)
1327 				return TEE_ERROR_BAD_PARAMETERS;
1328 		} else if (o->info.objectType == TEE_TYPE_DSA_PUBLIC_KEY) {
1329 			if (src->info.objectType != TEE_TYPE_DSA_KEYPAIR)
1330 				return TEE_ERROR_BAD_PARAMETERS;
1331 		} else if (o->info.objectType == TEE_TYPE_ECDSA_PUBLIC_KEY) {
1332 			if (src->info.objectType != TEE_TYPE_ECDSA_KEYPAIR)
1333 				return TEE_ERROR_BAD_PARAMETERS;
1334 		} else if (o->info.objectType == TEE_TYPE_ECDH_PUBLIC_KEY) {
1335 			if (src->info.objectType != TEE_TYPE_ECDH_KEYPAIR)
1336 				return TEE_ERROR_BAD_PARAMETERS;
1337 		} else if (o->info.objectType == TEE_TYPE_SM2_DSA_PUBLIC_KEY) {
1338 			if (src->info.objectType != TEE_TYPE_SM2_DSA_KEYPAIR)
1339 				return TEE_ERROR_BAD_PARAMETERS;
1340 		} else if (o->info.objectType == TEE_TYPE_SM2_PKE_PUBLIC_KEY) {
1341 			if (src->info.objectType != TEE_TYPE_SM2_PKE_KEYPAIR)
1342 				return TEE_ERROR_BAD_PARAMETERS;
1343 		} else if (o->info.objectType == TEE_TYPE_SM2_KEP_PUBLIC_KEY) {
1344 			if (src->info.objectType != TEE_TYPE_SM2_KEP_KEYPAIR)
1345 				return TEE_ERROR_BAD_PARAMETERS;
1346 		} else {
1347 			return TEE_ERROR_BAD_PARAMETERS;
1348 		}
1349 
1350 		tp_src = tee_svc_find_type_props(src->info.objectType);
1351 		if (!tp_src)
1352 			return TEE_ERROR_BAD_STATE;
1353 
1354 		have_attrs = BIT32(tp->num_type_attrs) - 1;
1355 		for (n = 0; n < tp->num_type_attrs; n++) {
1356 			ta = tp->type_attrs + n;
1357 
1358 			idx = tee_svc_cryp_obj_find_type_attr_idx(ta->attr_id,
1359 								  tp_src);
1360 			if (idx < 0)
1361 				return TEE_ERROR_BAD_STATE;
1362 
1363 			attr = (uint8_t *)o->attr + ta->raw_offs;
1364 			src_attr = (uint8_t *)src->attr +
1365 				   tp_src->type_attrs[idx].raw_offs;
1366 			res = attr_ops[ta->ops_index].from_obj(attr, src_attr);
1367 			if (res != TEE_SUCCESS)
1368 				return res;
1369 		}
1370 	}
1371 
1372 	o->have_attrs = have_attrs;
1373 	return TEE_SUCCESS;
1374 }
1375 
1376 static bool is_gp_legacy_des_key_size(TEE_ObjectType type, size_t sz)
1377 {
1378 	return IS_ENABLED(CFG_COMPAT_GP10_DES) &&
1379 	       ((type == TEE_TYPE_DES && sz == 56) ||
1380 		(type == TEE_TYPE_DES3 && (sz == 112 || sz == 168)));
1381 }
1382 
1383 static TEE_Result check_key_size(const struct tee_cryp_obj_type_props *props,
1384 				 size_t key_size)
1385 {
1386 	size_t sz = key_size;
1387 
1388 	/*
1389 	 * In GP Internal API Specification 1.0 the partity bits aren't
1390 	 * counted when telling the size of the key in bits so add them
1391 	 * here if missing.
1392 	 */
1393 	if (is_gp_legacy_des_key_size(props->obj_type, sz))
1394 		sz += sz / 7;
1395 
1396 	if (sz % props->quanta != 0)
1397 		return TEE_ERROR_NOT_SUPPORTED;
1398 	if (sz < props->min_size)
1399 		return TEE_ERROR_NOT_SUPPORTED;
1400 	if (sz > props->max_size)
1401 		return TEE_ERROR_NOT_SUPPORTED;
1402 
1403 	return TEE_SUCCESS;
1404 }
1405 
1406 TEE_Result tee_obj_set_type(struct tee_obj *o, uint32_t obj_type,
1407 			    size_t max_key_size)
1408 {
1409 	TEE_Result res = TEE_SUCCESS;
1410 	const struct tee_cryp_obj_type_props *type_props;
1411 
1412 	/* Can only set type for newly allocated objs */
1413 	if (o->attr)
1414 		return TEE_ERROR_BAD_STATE;
1415 
1416 	/*
1417 	 * Verify that maxKeySize is supported and find out how
1418 	 * much should be allocated.
1419 	 */
1420 
1421 	if (obj_type == TEE_TYPE_DATA) {
1422 		if (max_key_size)
1423 			return TEE_ERROR_NOT_SUPPORTED;
1424 	} else {
1425 		/* Find description of object */
1426 		type_props = tee_svc_find_type_props(obj_type);
1427 		if (!type_props)
1428 			return TEE_ERROR_NOT_SUPPORTED;
1429 
1430 		/* Check that max_key_size follows restrictions */
1431 		res = check_key_size(type_props, max_key_size);
1432 		if (res)
1433 			return res;
1434 
1435 		o->attr = calloc(1, type_props->alloc_size);
1436 		if (!o->attr)
1437 			return TEE_ERROR_OUT_OF_MEMORY;
1438 	}
1439 
1440 	/* If we have a key structure, pre-allocate the bignums inside */
1441 	switch (obj_type) {
1442 	case TEE_TYPE_RSA_PUBLIC_KEY:
1443 		res = crypto_acipher_alloc_rsa_public_key(o->attr,
1444 							  max_key_size);
1445 		break;
1446 	case TEE_TYPE_RSA_KEYPAIR:
1447 		res = crypto_acipher_alloc_rsa_keypair(o->attr, max_key_size);
1448 		break;
1449 	case TEE_TYPE_DSA_PUBLIC_KEY:
1450 		res = crypto_acipher_alloc_dsa_public_key(o->attr,
1451 							  max_key_size);
1452 		break;
1453 	case TEE_TYPE_DSA_KEYPAIR:
1454 		res = crypto_acipher_alloc_dsa_keypair(o->attr, max_key_size);
1455 		break;
1456 	case TEE_TYPE_DH_KEYPAIR:
1457 		res = crypto_acipher_alloc_dh_keypair(o->attr, max_key_size);
1458 		break;
1459 	case TEE_TYPE_ECDSA_PUBLIC_KEY:
1460 	case TEE_TYPE_ECDH_PUBLIC_KEY:
1461 	case TEE_TYPE_SM2_DSA_PUBLIC_KEY:
1462 	case TEE_TYPE_SM2_PKE_PUBLIC_KEY:
1463 	case TEE_TYPE_SM2_KEP_PUBLIC_KEY:
1464 		res = crypto_acipher_alloc_ecc_public_key(o->attr, obj_type,
1465 							  max_key_size);
1466 		break;
1467 	case TEE_TYPE_ECDSA_KEYPAIR:
1468 	case TEE_TYPE_ECDH_KEYPAIR:
1469 	case TEE_TYPE_SM2_DSA_KEYPAIR:
1470 	case TEE_TYPE_SM2_PKE_KEYPAIR:
1471 	case TEE_TYPE_SM2_KEP_KEYPAIR:
1472 		res = crypto_acipher_alloc_ecc_keypair(o->attr, obj_type,
1473 						       max_key_size);
1474 		break;
1475 	case TEE_TYPE_X25519_KEYPAIR:
1476 		res = crypto_acipher_alloc_x25519_keypair(o->attr,
1477 							  max_key_size);
1478 		break;
1479 	default:
1480 		if (obj_type != TEE_TYPE_DATA) {
1481 			struct tee_cryp_obj_secret *key = o->attr;
1482 
1483 			key->alloc_size = type_props->alloc_size -
1484 					  sizeof(*key);
1485 		}
1486 		break;
1487 	}
1488 
1489 	if (res != TEE_SUCCESS)
1490 		return res;
1491 
1492 	o->info.objectType = obj_type;
1493 	o->info.maxKeySize = max_key_size;
1494 	o->info.objectUsage = TEE_USAGE_DEFAULT;
1495 
1496 	return TEE_SUCCESS;
1497 }
1498 
1499 TEE_Result syscall_cryp_obj_alloc(unsigned long obj_type,
1500 			unsigned long max_key_size, uint32_t *obj)
1501 {
1502 	struct ts_session *sess = ts_get_current_session();
1503 	TEE_Result res = TEE_SUCCESS;
1504 	struct tee_obj *o = NULL;
1505 
1506 
1507 	o = tee_obj_alloc();
1508 	if (!o)
1509 		return TEE_ERROR_OUT_OF_MEMORY;
1510 
1511 	res = tee_obj_set_type(o, obj_type, max_key_size);
1512 	if (res != TEE_SUCCESS) {
1513 		tee_obj_free(o);
1514 		return res;
1515 	}
1516 
1517 	tee_obj_add(to_user_ta_ctx(sess->ctx), o);
1518 
1519 	res = copy_kaddr_to_uref(obj, o);
1520 	if (res != TEE_SUCCESS)
1521 		tee_obj_close(to_user_ta_ctx(sess->ctx), o);
1522 	return res;
1523 }
1524 
1525 TEE_Result syscall_cryp_obj_close(unsigned long obj)
1526 {
1527 	struct ts_session *sess = ts_get_current_session();
1528 	TEE_Result res = TEE_SUCCESS;
1529 	struct tee_obj *o = NULL;
1530 
1531 	res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o);
1532 	if (res != TEE_SUCCESS)
1533 		return res;
1534 
1535 	/*
1536 	 * If it's busy it's used by an operation, a client should never have
1537 	 * this handle.
1538 	 */
1539 	if (o->busy)
1540 		return TEE_ERROR_ITEM_NOT_FOUND;
1541 
1542 	tee_obj_close(to_user_ta_ctx(sess->ctx), o);
1543 	return TEE_SUCCESS;
1544 }
1545 
1546 TEE_Result syscall_cryp_obj_reset(unsigned long obj)
1547 {
1548 	struct ts_session *sess = ts_get_current_session();
1549 	TEE_Result res = TEE_SUCCESS;
1550 	struct tee_obj *o = NULL;
1551 
1552 	res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o);
1553 	if (res != TEE_SUCCESS)
1554 		return res;
1555 
1556 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) == 0) {
1557 		tee_obj_attr_clear(o);
1558 		o->info.keySize = 0;
1559 		o->info.objectUsage = TEE_USAGE_DEFAULT;
1560 	} else {
1561 		return TEE_ERROR_BAD_PARAMETERS;
1562 	}
1563 
1564 	/* the object is no more initialized */
1565 	o->info.handleFlags &= ~TEE_HANDLE_FLAG_INITIALIZED;
1566 
1567 	return TEE_SUCCESS;
1568 }
1569 
1570 static TEE_Result copy_in_attrs(struct user_ta_ctx *utc,
1571 			const struct utee_attribute *usr_attrs,
1572 			uint32_t attr_count, TEE_Attribute *attrs)
1573 {
1574 	TEE_Result res = TEE_SUCCESS;
1575 	size_t size = 0;
1576 	uint32_t n = 0;
1577 
1578 	if (MUL_OVERFLOW(sizeof(struct utee_attribute), attr_count, &size))
1579 		return TEE_ERROR_OVERFLOW;
1580 
1581 	usr_attrs = memtag_strip_tag_const(usr_attrs);
1582 
1583 	res = vm_check_access_rights(&utc->uctx,
1584 				     TEE_MEMORY_ACCESS_READ |
1585 				     TEE_MEMORY_ACCESS_ANY_OWNER,
1586 				     (uaddr_t)usr_attrs, size);
1587 	if (res != TEE_SUCCESS)
1588 		return res;
1589 
1590 	for (n = 0; n < attr_count; n++) {
1591 		attrs[n].attributeID = usr_attrs[n].attribute_id;
1592 		if (attrs[n].attributeID & TEE_ATTR_FLAG_VALUE) {
1593 			attrs[n].content.value.a = usr_attrs[n].a;
1594 			attrs[n].content.value.b = usr_attrs[n].b;
1595 		} else {
1596 			uintptr_t buf = usr_attrs[n].a;
1597 			size_t len = usr_attrs[n].b;
1598 			uint32_t flags = TEE_MEMORY_ACCESS_READ |
1599 					 TEE_MEMORY_ACCESS_ANY_OWNER;
1600 
1601 			buf = memtag_strip_tag_vaddr((void *)buf);
1602 
1603 			res = vm_check_access_rights(&utc->uctx, flags, buf,
1604 						     len);
1605 			if (res != TEE_SUCCESS)
1606 				return res;
1607 			attrs[n].content.ref.buffer = (void *)buf;
1608 			attrs[n].content.ref.length = len;
1609 		}
1610 	}
1611 
1612 	return TEE_SUCCESS;
1613 }
1614 
1615 enum attr_usage {
1616 	ATTR_USAGE_POPULATE,
1617 	ATTR_USAGE_GENERATE_KEY
1618 };
1619 
1620 static TEE_Result tee_svc_cryp_check_attr(enum attr_usage usage,
1621 					  const struct tee_cryp_obj_type_props
1622 						*type_props,
1623 					  const TEE_Attribute *attrs,
1624 					  uint32_t attr_count)
1625 {
1626 	uint32_t required_flag = 0;
1627 	uint32_t opt_flag = 0;
1628 	bool all_opt_needed = false;
1629 	uint32_t req_attrs = 0;
1630 	uint32_t opt_grp_attrs = 0;
1631 	uint32_t attrs_found = 0;
1632 	size_t n = 0;
1633 	uint32_t bit = 0;
1634 	uint32_t flags = 0;
1635 	int idx = 0;
1636 
1637 	if (usage == ATTR_USAGE_POPULATE) {
1638 		required_flag = TEE_TYPE_ATTR_REQUIRED;
1639 		opt_flag = TEE_TYPE_ATTR_OPTIONAL_GROUP;
1640 		all_opt_needed = true;
1641 	} else {
1642 		required_flag = TEE_TYPE_ATTR_GEN_KEY_REQ;
1643 		opt_flag = TEE_TYPE_ATTR_GEN_KEY_OPT;
1644 		all_opt_needed = false;
1645 	}
1646 
1647 	/*
1648 	 * First find out which attributes are required and which belong to
1649 	 * the optional group
1650 	 */
1651 	for (n = 0; n < type_props->num_type_attrs; n++) {
1652 		bit = 1 << n;
1653 		flags = type_props->type_attrs[n].flags;
1654 
1655 		if (flags & required_flag)
1656 			req_attrs |= bit;
1657 		else if (flags & opt_flag)
1658 			opt_grp_attrs |= bit;
1659 	}
1660 
1661 	/*
1662 	 * Verify that all required attributes are in place and
1663 	 * that the same attribute isn't repeated.
1664 	 */
1665 	for (n = 0; n < attr_count; n++) {
1666 		idx = tee_svc_cryp_obj_find_type_attr_idx(
1667 							attrs[n].attributeID,
1668 							type_props);
1669 
1670 		/* attribute not defined in current object type */
1671 		if (idx < 0)
1672 			return TEE_ERROR_ITEM_NOT_FOUND;
1673 
1674 		bit = 1 << idx;
1675 
1676 		/* attribute not repeated */
1677 		if ((attrs_found & bit) != 0)
1678 			return TEE_ERROR_ITEM_NOT_FOUND;
1679 
1680 		/*
1681 		 * Attribute not defined in current object type for this
1682 		 * usage.
1683 		 */
1684 		if (!(bit & (req_attrs | opt_grp_attrs)))
1685 			return TEE_ERROR_ITEM_NOT_FOUND;
1686 
1687 		attrs_found |= bit;
1688 	}
1689 	/* Required attribute missing */
1690 	if ((attrs_found & req_attrs) != req_attrs)
1691 		return TEE_ERROR_ITEM_NOT_FOUND;
1692 
1693 	/*
1694 	 * If the flag says that "if one of the optional attributes are included
1695 	 * all of them has to be included" this must be checked.
1696 	 */
1697 	if (all_opt_needed && (attrs_found & opt_grp_attrs) != 0 &&
1698 	    (attrs_found & opt_grp_attrs) != opt_grp_attrs)
1699 		return TEE_ERROR_ITEM_NOT_FOUND;
1700 
1701 	return TEE_SUCCESS;
1702 }
1703 
1704 static TEE_Result get_ec_key_size(uint32_t curve, size_t *key_size)
1705 {
1706 	switch (curve) {
1707 	case TEE_ECC_CURVE_NIST_P192:
1708 		*key_size = 192;
1709 		break;
1710 	case TEE_ECC_CURVE_NIST_P224:
1711 		*key_size = 224;
1712 		break;
1713 	case TEE_ECC_CURVE_NIST_P256:
1714 		*key_size = 256;
1715 		break;
1716 	case TEE_ECC_CURVE_NIST_P384:
1717 		*key_size = 384;
1718 		break;
1719 	case TEE_ECC_CURVE_NIST_P521:
1720 		*key_size = 521;
1721 		break;
1722 	case TEE_ECC_CURVE_SM2:
1723 		*key_size = 256;
1724 		break;
1725 	default:
1726 		return TEE_ERROR_NOT_SUPPORTED;
1727 	}
1728 
1729 	return TEE_SUCCESS;
1730 }
1731 
1732 static size_t get_used_bits(const TEE_Attribute *a)
1733 {
1734 	int nbits = a->content.ref.length * 8;
1735 	int v = 0;
1736 
1737 	bit_ffs(a->content.ref.buffer, nbits, &v);
1738 	return nbits - v;
1739 }
1740 
1741 static TEE_Result tee_svc_cryp_obj_populate_type(
1742 		struct tee_obj *o,
1743 		const struct tee_cryp_obj_type_props *type_props,
1744 		const TEE_Attribute *attrs,
1745 		uint32_t attr_count)
1746 {
1747 	TEE_Result res = TEE_SUCCESS;
1748 	uint32_t have_attrs = 0;
1749 	size_t obj_size = 0;
1750 	size_t n = 0;
1751 	int idx = 0;
1752 	const struct attr_ops *ops = NULL;
1753 	void *attr = NULL;
1754 
1755 	for (n = 0; n < attr_count; n++) {
1756 		idx = tee_svc_cryp_obj_find_type_attr_idx(
1757 							attrs[n].attributeID,
1758 							type_props);
1759 		/* attribute not defined in current object type */
1760 		if (idx < 0)
1761 			return TEE_ERROR_ITEM_NOT_FOUND;
1762 
1763 		have_attrs |= BIT32(idx);
1764 		ops = attr_ops + type_props->type_attrs[idx].ops_index;
1765 		attr = (uint8_t *)o->attr +
1766 		       type_props->type_attrs[idx].raw_offs;
1767 		if (attrs[n].attributeID & TEE_ATTR_FLAG_VALUE)
1768 			res = ops->from_user(attr, &attrs[n].content.value,
1769 					     sizeof(attrs[n].content.value));
1770 		else
1771 			res = ops->from_user(attr, attrs[n].content.ref.buffer,
1772 					     attrs[n].content.ref.length);
1773 		if (res != TEE_SUCCESS)
1774 			return res;
1775 
1776 		/*
1777 		 * The attribute that gives the size of the object is
1778 		 * flagged with TEE_TYPE_ATTR_SIZE_INDICATOR.
1779 		 */
1780 		if (type_props->type_attrs[idx].flags &
1781 		    TEE_TYPE_ATTR_SIZE_INDICATOR) {
1782 			/* There should be only one */
1783 			if (obj_size)
1784 				return TEE_ERROR_BAD_STATE;
1785 
1786 			/*
1787 			 * For ECDSA/ECDH we need to translate curve into
1788 			 * object size
1789 			 */
1790 			if (attrs[n].attributeID == TEE_ATTR_ECC_CURVE) {
1791 				res = get_ec_key_size(attrs[n].content.value.a,
1792 						      &obj_size);
1793 				if (res != TEE_SUCCESS)
1794 					return res;
1795 			} else {
1796 				TEE_ObjectType obj_type = o->info.objectType;
1797 				size_t sz = o->info.maxKeySize;
1798 
1799 				obj_size = attrs[n].content.ref.length * 8;
1800 				/* Drop the parity bits for legacy objects */
1801 				if (is_gp_legacy_des_key_size(obj_type, sz))
1802 					obj_size -= obj_size / 8;
1803 			}
1804 			if (obj_size > o->info.maxKeySize)
1805 				return TEE_ERROR_BAD_STATE;
1806 			res = check_key_size(type_props, obj_size);
1807 			if (res != TEE_SUCCESS)
1808 				return TEE_ERROR_BAD_PARAMETERS;
1809 		}
1810 
1811 		/*
1812 		 * Bignum attributes limited by the number of bits in
1813 		 * o->info.keySize are flagged with
1814 		 * TEE_TYPE_ATTR_BIGNUM_MAXBITS.
1815 		 */
1816 		if (type_props->type_attrs[idx].flags &
1817 		    TEE_TYPE_ATTR_BIGNUM_MAXBITS) {
1818 			if (get_used_bits(attrs + n) > o->info.maxKeySize)
1819 				return TEE_ERROR_BAD_STATE;
1820 		}
1821 	}
1822 
1823 	o->have_attrs = have_attrs;
1824 	o->info.keySize = obj_size;
1825 	/*
1826 	 * In GP Internal API Specification 1.0 the partity bits aren't
1827 	 * counted when telling the size of the key in bits so remove the
1828 	 * parity bits here.
1829 	 */
1830 	if (is_gp_legacy_des_key_size(o->info.objectType, o->info.maxKeySize))
1831 		o->info.keySize -= o->info.keySize / 8;
1832 
1833 	return TEE_SUCCESS;
1834 }
1835 
1836 TEE_Result syscall_cryp_obj_populate(unsigned long obj,
1837 			struct utee_attribute *usr_attrs,
1838 			unsigned long attr_count)
1839 {
1840 	struct ts_session *sess = ts_get_current_session();
1841 	TEE_Result res = TEE_SUCCESS;
1842 	struct tee_obj *o = NULL;
1843 	const struct tee_cryp_obj_type_props *type_props = NULL;
1844 	TEE_Attribute *attrs = NULL;
1845 	size_t alloc_size = 0;
1846 
1847 	res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o);
1848 	if (res != TEE_SUCCESS)
1849 		return res;
1850 
1851 	/* Must be a transient object */
1852 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
1853 		return TEE_ERROR_BAD_PARAMETERS;
1854 
1855 	/* Must not be initialized already */
1856 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1857 		return TEE_ERROR_BAD_PARAMETERS;
1858 
1859 	type_props = tee_svc_find_type_props(o->info.objectType);
1860 	if (!type_props)
1861 		return TEE_ERROR_NOT_IMPLEMENTED;
1862 
1863 	if (MUL_OVERFLOW(sizeof(TEE_Attribute), attr_count, &alloc_size))
1864 		return TEE_ERROR_OVERFLOW;
1865 
1866 	attrs = malloc(alloc_size);
1867 	if (!attrs)
1868 		return TEE_ERROR_OUT_OF_MEMORY;
1869 
1870 	res = copy_in_attrs(to_user_ta_ctx(sess->ctx), usr_attrs, attr_count,
1871 			    attrs);
1872 	if (res != TEE_SUCCESS)
1873 		goto out;
1874 
1875 	res = tee_svc_cryp_check_attr(ATTR_USAGE_POPULATE, type_props,
1876 				      attrs, attr_count);
1877 	if (res != TEE_SUCCESS)
1878 		goto out;
1879 
1880 	res = tee_svc_cryp_obj_populate_type(o, type_props, attrs, attr_count);
1881 	if (res == TEE_SUCCESS)
1882 		o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
1883 
1884 out:
1885 	free_wipe(attrs);
1886 	return res;
1887 }
1888 
1889 TEE_Result syscall_cryp_obj_copy(unsigned long dst, unsigned long src)
1890 {
1891 	struct ts_session *sess = ts_get_current_session();
1892 	TEE_Result res = TEE_SUCCESS;
1893 	struct tee_obj *dst_o = NULL;
1894 	struct tee_obj *src_o = NULL;
1895 
1896 	res = tee_obj_get(to_user_ta_ctx(sess->ctx),
1897 			  uref_to_vaddr(dst), &dst_o);
1898 	if (res != TEE_SUCCESS)
1899 		return res;
1900 
1901 	res = tee_obj_get(to_user_ta_ctx(sess->ctx),
1902 			  uref_to_vaddr(src), &src_o);
1903 	if (res != TEE_SUCCESS)
1904 		return res;
1905 
1906 	if ((src_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1907 		return TEE_ERROR_BAD_PARAMETERS;
1908 	if ((dst_o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
1909 		return TEE_ERROR_BAD_PARAMETERS;
1910 	if ((dst_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1911 		return TEE_ERROR_BAD_PARAMETERS;
1912 
1913 	res = tee_obj_attr_copy_from(dst_o, src_o);
1914 	if (res != TEE_SUCCESS)
1915 		return res;
1916 
1917 	dst_o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
1918 	dst_o->info.keySize = src_o->info.keySize;
1919 	dst_o->info.objectUsage = src_o->info.objectUsage;
1920 	return TEE_SUCCESS;
1921 }
1922 
1923 static TEE_Result check_pub_rsa_key(struct bignum *e)
1924 {
1925 	size_t n = crypto_bignum_num_bytes(e);
1926 	uint8_t bin_key[256 / 8] = { 0 };
1927 
1928 	/*
1929 	 * NIST SP800-56B requires public RSA key to be an odd integer in
1930 	 * the range 65537 <= e < 2^256.
1931 	 */
1932 
1933 	if (n > sizeof(bin_key) || n < 3)
1934 		return TEE_ERROR_BAD_PARAMETERS;
1935 
1936 	crypto_bignum_bn2bin(e, bin_key);
1937 
1938 	if (!(bin_key[n - 1] & 1)) /* key must be odd */
1939 		return TEE_ERROR_BAD_PARAMETERS;
1940 
1941 	if (n == 3) {
1942 		uint32_t key = 0;
1943 
1944 		for (n = 0; n < 3; n++) {
1945 			key <<= 8;
1946 			key |= bin_key[n];
1947 		}
1948 
1949 		if (key < 65537)
1950 			return TEE_ERROR_BAD_PARAMETERS;
1951 	}
1952 
1953 	/* key is larger than 65537 */
1954 	return TEE_SUCCESS;
1955 }
1956 
1957 static TEE_Result tee_svc_obj_generate_key_rsa(
1958 	struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props,
1959 	uint32_t key_size,
1960 	const TEE_Attribute *params, uint32_t param_count)
1961 {
1962 	TEE_Result res = TEE_SUCCESS;
1963 	struct rsa_keypair *key = o->attr;
1964 	uint32_t e = TEE_U32_TO_BIG_ENDIAN(65537);
1965 
1966 	/* Copy the present attributes into the obj before starting */
1967 	res = tee_svc_cryp_obj_populate_type(o, type_props, params,
1968 					     param_count);
1969 	if (res != TEE_SUCCESS)
1970 		return res;
1971 	if (get_attribute(o, type_props, TEE_ATTR_RSA_PUBLIC_EXPONENT)) {
1972 		res = check_pub_rsa_key(key->e);
1973 		if (res)
1974 			return res;
1975 	} else {
1976 		crypto_bignum_bin2bn((const uint8_t *)&e, sizeof(e), key->e);
1977 	}
1978 	res = crypto_acipher_gen_rsa_key(key, key_size);
1979 	if (res != TEE_SUCCESS)
1980 		return res;
1981 
1982 	/* Set bits for all known attributes for this object type */
1983 	o->have_attrs = (1 << type_props->num_type_attrs) - 1;
1984 
1985 	return TEE_SUCCESS;
1986 }
1987 
1988 static TEE_Result tee_svc_obj_generate_key_dsa(
1989 	struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props,
1990 	uint32_t key_size, const TEE_Attribute *params, uint32_t param_count)
1991 {
1992 	TEE_Result res;
1993 
1994 	/* Copy the present attributes into the obj before starting */
1995 	res = tee_svc_cryp_obj_populate_type(o, type_props, params,
1996 					     param_count);
1997 	if (res != TEE_SUCCESS)
1998 		return res;
1999 
2000 	res = crypto_acipher_gen_dsa_key(o->attr, key_size);
2001 	if (res != TEE_SUCCESS)
2002 		return res;
2003 
2004 	/* Set bits for all known attributes for this object type */
2005 	o->have_attrs = (1 << type_props->num_type_attrs) - 1;
2006 
2007 	return TEE_SUCCESS;
2008 }
2009 
2010 static TEE_Result tee_svc_obj_generate_key_dh(
2011 	struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props,
2012 	uint32_t key_size, const TEE_Attribute *params, uint32_t param_count)
2013 {
2014 	TEE_Result res;
2015 	struct dh_keypair *tee_dh_key;
2016 	struct bignum *dh_q = NULL;
2017 	uint32_t dh_xbits = 0;
2018 
2019 	/* Copy the present attributes into the obj before starting */
2020 	res = tee_svc_cryp_obj_populate_type(o, type_props, params,
2021 					     param_count);
2022 	if (res != TEE_SUCCESS)
2023 		return res;
2024 
2025 	tee_dh_key = (struct dh_keypair *)o->attr;
2026 
2027 	if (get_attribute(o, type_props, TEE_ATTR_DH_SUBPRIME))
2028 		dh_q = tee_dh_key->q;
2029 	if (get_attribute(o, type_props, TEE_ATTR_DH_X_BITS))
2030 		dh_xbits = tee_dh_key->xbits;
2031 	res = crypto_acipher_gen_dh_key(tee_dh_key, dh_q, dh_xbits, key_size);
2032 	if (res != TEE_SUCCESS)
2033 		return res;
2034 
2035 	/* Set bits for the generated public and private key */
2036 	set_attribute(o, type_props, TEE_ATTR_DH_PUBLIC_VALUE);
2037 	set_attribute(o, type_props, TEE_ATTR_DH_PRIVATE_VALUE);
2038 	set_attribute(o, type_props, TEE_ATTR_DH_X_BITS);
2039 	return TEE_SUCCESS;
2040 }
2041 
2042 static TEE_Result tee_svc_obj_generate_key_ecc(
2043 	struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props,
2044 	uint32_t key_size, const TEE_Attribute *params, uint32_t param_count)
2045 {
2046 	TEE_Result res;
2047 	struct ecc_keypair *tee_ecc_key;
2048 
2049 	/* Copy the present attributes into the obj before starting */
2050 	res = tee_svc_cryp_obj_populate_type(o, type_props, params,
2051 					     param_count);
2052 	if (res != TEE_SUCCESS)
2053 		return res;
2054 
2055 	tee_ecc_key = (struct ecc_keypair *)o->attr;
2056 
2057 	res = crypto_acipher_gen_ecc_key(tee_ecc_key, key_size);
2058 	if (res != TEE_SUCCESS)
2059 		return res;
2060 
2061 	/* Set bits for the generated public and private key */
2062 	set_attribute(o, type_props, TEE_ATTR_ECC_PRIVATE_VALUE);
2063 	set_attribute(o, type_props, TEE_ATTR_ECC_PUBLIC_VALUE_X);
2064 	set_attribute(o, type_props, TEE_ATTR_ECC_PUBLIC_VALUE_Y);
2065 	set_attribute(o, type_props, TEE_ATTR_ECC_CURVE);
2066 	return TEE_SUCCESS;
2067 }
2068 
2069 static TEE_Result
2070 tee_svc_obj_generate_key_x25519(struct tee_obj *o,
2071 				const struct tee_cryp_obj_type_props
2072 							*type_props,
2073 				uint32_t key_size,
2074 				const TEE_Attribute *params,
2075 				uint32_t param_count)
2076 {
2077 	TEE_Result res = TEE_ERROR_GENERIC;
2078 	struct x25519_keypair *tee_x25519_key = NULL;
2079 
2080 	/* Copy the present attributes into the obj before starting */
2081 	res = tee_svc_cryp_obj_populate_type(o, type_props, params,
2082 					     param_count);
2083 	if (res != TEE_SUCCESS)
2084 		return res;
2085 
2086 	tee_x25519_key = (struct x25519_keypair *)o->attr;
2087 
2088 	res = crypto_acipher_gen_x25519_key(tee_x25519_key, key_size);
2089 	if (res != TEE_SUCCESS)
2090 		return res;
2091 
2092 	/* Set bits for the generated public and private key */
2093 	set_attribute(o, type_props, TEE_ATTR_X25519_PRIVATE_VALUE);
2094 	set_attribute(o, type_props, TEE_ATTR_X25519_PUBLIC_VALUE);
2095 	return TEE_SUCCESS;
2096 }
2097 
2098 TEE_Result syscall_obj_generate_key(unsigned long obj, unsigned long key_size,
2099 			const struct utee_attribute *usr_params,
2100 			unsigned long param_count)
2101 {
2102 	struct ts_session *sess = ts_get_current_session();
2103 	TEE_Result res = TEE_SUCCESS;
2104 	const struct tee_cryp_obj_type_props *type_props = NULL;
2105 	struct tee_obj *o = NULL;
2106 	struct tee_cryp_obj_secret *key = NULL;
2107 	size_t byte_size = 0;
2108 	TEE_Attribute *params = NULL;
2109 	size_t alloc_size = 0;
2110 
2111 	res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o);
2112 	if (res != TEE_SUCCESS)
2113 		return res;
2114 
2115 	/* Must be a transient object */
2116 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
2117 		return TEE_ERROR_BAD_STATE;
2118 
2119 	/* Must not be initialized already */
2120 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
2121 		return TEE_ERROR_BAD_STATE;
2122 
2123 	/* Find description of object */
2124 	type_props = tee_svc_find_type_props(o->info.objectType);
2125 	if (!type_props)
2126 		return TEE_ERROR_NOT_SUPPORTED;
2127 
2128 	/* Check that key_size follows restrictions */
2129 	res = check_key_size(type_props, key_size);
2130 	if (res)
2131 		return res;
2132 
2133 	if (MUL_OVERFLOW(sizeof(TEE_Attribute), param_count, &alloc_size))
2134 		return TEE_ERROR_OVERFLOW;
2135 
2136 	params = malloc(alloc_size);
2137 	if (!params)
2138 		return TEE_ERROR_OUT_OF_MEMORY;
2139 	res = copy_in_attrs(to_user_ta_ctx(sess->ctx), usr_params, param_count,
2140 			    params);
2141 	if (res != TEE_SUCCESS)
2142 		goto out;
2143 
2144 	res = tee_svc_cryp_check_attr(ATTR_USAGE_GENERATE_KEY, type_props,
2145 				      params, param_count);
2146 	if (res != TEE_SUCCESS)
2147 		goto out;
2148 
2149 	switch (o->info.objectType) {
2150 	case TEE_TYPE_AES:
2151 	case TEE_TYPE_DES:
2152 	case TEE_TYPE_DES3:
2153 	case TEE_TYPE_SM4:
2154 	case TEE_TYPE_HMAC_MD5:
2155 	case TEE_TYPE_HMAC_SHA1:
2156 	case TEE_TYPE_HMAC_SHA224:
2157 	case TEE_TYPE_HMAC_SHA256:
2158 	case TEE_TYPE_HMAC_SHA384:
2159 	case TEE_TYPE_HMAC_SHA512:
2160 	case TEE_TYPE_HMAC_SM3:
2161 	case TEE_TYPE_GENERIC_SECRET:
2162 		byte_size = key_size / 8;
2163 
2164 		/*
2165 		 * In GP Internal API Specification 1.0 the partity bits
2166 		 * aren't counted when telling the size of the key in bits.
2167 		 */
2168 		if (is_gp_legacy_des_key_size(o->info.objectType, key_size))
2169 			byte_size = (key_size + key_size / 7) / 8;
2170 
2171 		key = (struct tee_cryp_obj_secret *)o->attr;
2172 		if (byte_size > key->alloc_size) {
2173 			res = TEE_ERROR_EXCESS_DATA;
2174 			goto out;
2175 		}
2176 
2177 		res = crypto_rng_read((void *)(key + 1), byte_size);
2178 		if (res != TEE_SUCCESS)
2179 			goto out;
2180 
2181 		key->key_size = byte_size;
2182 
2183 		/* Set bits for all known attributes for this object type */
2184 		o->have_attrs = (1 << type_props->num_type_attrs) - 1;
2185 
2186 		break;
2187 
2188 	case TEE_TYPE_RSA_KEYPAIR:
2189 		res = tee_svc_obj_generate_key_rsa(o, type_props, key_size,
2190 						   params, param_count);
2191 		if (res != TEE_SUCCESS)
2192 			goto out;
2193 		break;
2194 
2195 	case TEE_TYPE_DSA_KEYPAIR:
2196 		res = tee_svc_obj_generate_key_dsa(o, type_props, key_size,
2197 						   params, param_count);
2198 		if (res != TEE_SUCCESS)
2199 			goto out;
2200 		break;
2201 
2202 	case TEE_TYPE_DH_KEYPAIR:
2203 		res = tee_svc_obj_generate_key_dh(o, type_props, key_size,
2204 						  params, param_count);
2205 		if (res != TEE_SUCCESS)
2206 			goto out;
2207 		break;
2208 
2209 	case TEE_TYPE_ECDSA_KEYPAIR:
2210 	case TEE_TYPE_ECDH_KEYPAIR:
2211 	case TEE_TYPE_SM2_DSA_KEYPAIR:
2212 	case TEE_TYPE_SM2_KEP_KEYPAIR:
2213 	case TEE_TYPE_SM2_PKE_KEYPAIR:
2214 		res = tee_svc_obj_generate_key_ecc(o, type_props, key_size,
2215 						  params, param_count);
2216 		if (res != TEE_SUCCESS)
2217 			goto out;
2218 		break;
2219 
2220 	case TEE_TYPE_X25519_KEYPAIR:
2221 		res = tee_svc_obj_generate_key_x25519(o, type_props, key_size,
2222 						      params, param_count);
2223 		if (res != TEE_SUCCESS)
2224 			goto out;
2225 		break;
2226 
2227 	default:
2228 		res = TEE_ERROR_BAD_FORMAT;
2229 	}
2230 
2231 out:
2232 	free_wipe(params);
2233 	if (res == TEE_SUCCESS) {
2234 		o->info.keySize = key_size;
2235 		o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
2236 	}
2237 	return res;
2238 }
2239 
2240 static TEE_Result tee_svc_cryp_get_state(struct ts_session *sess,
2241 					 vaddr_t state_id,
2242 					 struct tee_cryp_state **state)
2243 {
2244 	struct tee_cryp_state *s;
2245 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
2246 
2247 	TAILQ_FOREACH(s, &utc->cryp_states, link) {
2248 		if (state_id == (vaddr_t)s) {
2249 			*state = s;
2250 			return TEE_SUCCESS;
2251 		}
2252 	}
2253 	return TEE_ERROR_BAD_PARAMETERS;
2254 }
2255 
2256 static void cryp_state_free(struct user_ta_ctx *utc, struct tee_cryp_state *cs)
2257 {
2258 	struct tee_obj *o;
2259 
2260 	if (tee_obj_get(utc, cs->key1, &o) == TEE_SUCCESS)
2261 		tee_obj_close(utc, o);
2262 	if (tee_obj_get(utc, cs->key2, &o) == TEE_SUCCESS)
2263 		tee_obj_close(utc, o);
2264 
2265 	TAILQ_REMOVE(&utc->cryp_states, cs, link);
2266 	if (cs->ctx_finalize != NULL)
2267 		cs->ctx_finalize(cs->ctx);
2268 
2269 	switch (TEE_ALG_GET_CLASS(cs->algo)) {
2270 	case TEE_OPERATION_CIPHER:
2271 		crypto_cipher_free_ctx(cs->ctx);
2272 		break;
2273 	case TEE_OPERATION_AE:
2274 		crypto_authenc_free_ctx(cs->ctx);
2275 		break;
2276 	case TEE_OPERATION_DIGEST:
2277 		crypto_hash_free_ctx(cs->ctx);
2278 		break;
2279 	case TEE_OPERATION_MAC:
2280 		crypto_mac_free_ctx(cs->ctx);
2281 		break;
2282 	default:
2283 		assert(!cs->ctx);
2284 	}
2285 
2286 	free(cs);
2287 }
2288 
2289 static TEE_Result tee_svc_cryp_check_key_type(const struct tee_obj *o,
2290 					      uint32_t algo,
2291 					      TEE_OperationMode mode)
2292 {
2293 	uint32_t req_key_type;
2294 	uint32_t req_key_type2 = 0;
2295 
2296 	switch (TEE_ALG_GET_MAIN_ALG(algo)) {
2297 	case TEE_MAIN_ALGO_MD5:
2298 		req_key_type = TEE_TYPE_HMAC_MD5;
2299 		break;
2300 	case TEE_MAIN_ALGO_SHA1:
2301 		req_key_type = TEE_TYPE_HMAC_SHA1;
2302 		break;
2303 	case TEE_MAIN_ALGO_SHA224:
2304 		req_key_type = TEE_TYPE_HMAC_SHA224;
2305 		break;
2306 	case TEE_MAIN_ALGO_SHA256:
2307 		req_key_type = TEE_TYPE_HMAC_SHA256;
2308 		break;
2309 	case TEE_MAIN_ALGO_SHA384:
2310 		req_key_type = TEE_TYPE_HMAC_SHA384;
2311 		break;
2312 	case TEE_MAIN_ALGO_SHA512:
2313 		req_key_type = TEE_TYPE_HMAC_SHA512;
2314 		break;
2315 	case TEE_MAIN_ALGO_SM3:
2316 		req_key_type = TEE_TYPE_HMAC_SM3;
2317 		break;
2318 	case TEE_MAIN_ALGO_AES:
2319 		req_key_type = TEE_TYPE_AES;
2320 		break;
2321 	case TEE_MAIN_ALGO_DES:
2322 		req_key_type = TEE_TYPE_DES;
2323 		break;
2324 	case TEE_MAIN_ALGO_DES3:
2325 		req_key_type = TEE_TYPE_DES3;
2326 		break;
2327 	case TEE_MAIN_ALGO_SM4:
2328 		req_key_type = TEE_TYPE_SM4;
2329 		break;
2330 	case TEE_MAIN_ALGO_RSA:
2331 		req_key_type = TEE_TYPE_RSA_KEYPAIR;
2332 		if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY)
2333 			req_key_type2 = TEE_TYPE_RSA_PUBLIC_KEY;
2334 		break;
2335 	case TEE_MAIN_ALGO_DSA:
2336 		req_key_type = TEE_TYPE_DSA_KEYPAIR;
2337 		if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY)
2338 			req_key_type2 = TEE_TYPE_DSA_PUBLIC_KEY;
2339 		break;
2340 	case TEE_MAIN_ALGO_DH:
2341 		req_key_type = TEE_TYPE_DH_KEYPAIR;
2342 		break;
2343 	case TEE_MAIN_ALGO_ECDSA:
2344 		req_key_type = TEE_TYPE_ECDSA_KEYPAIR;
2345 		if (mode == TEE_MODE_VERIFY)
2346 			req_key_type2 = TEE_TYPE_ECDSA_PUBLIC_KEY;
2347 		break;
2348 	case TEE_MAIN_ALGO_ECDH:
2349 		req_key_type = TEE_TYPE_ECDH_KEYPAIR;
2350 		break;
2351 	case TEE_MAIN_ALGO_SM2_PKE:
2352 		if (mode == TEE_MODE_ENCRYPT)
2353 			req_key_type = TEE_TYPE_SM2_PKE_PUBLIC_KEY;
2354 		else
2355 			req_key_type = TEE_TYPE_SM2_PKE_KEYPAIR;
2356 		break;
2357 	case TEE_MAIN_ALGO_SM2_DSA_SM3:
2358 		if (mode == TEE_MODE_VERIFY)
2359 			req_key_type = TEE_TYPE_SM2_DSA_PUBLIC_KEY;
2360 		else
2361 			req_key_type = TEE_TYPE_SM2_DSA_KEYPAIR;
2362 		break;
2363 #if defined(CFG_CRYPTO_SM2_KEP)
2364 	case TEE_MAIN_ALGO_SM2_KEP:
2365 		req_key_type = TEE_TYPE_SM2_KEP_KEYPAIR;
2366 		req_key_type2 = TEE_TYPE_SM2_KEP_PUBLIC_KEY;
2367 		break;
2368 #endif
2369 #if defined(CFG_CRYPTO_HKDF)
2370 	case TEE_MAIN_ALGO_HKDF:
2371 		req_key_type = TEE_TYPE_HKDF_IKM;
2372 		break;
2373 #endif
2374 #if defined(CFG_CRYPTO_CONCAT_KDF)
2375 	case TEE_MAIN_ALGO_CONCAT_KDF:
2376 		req_key_type = TEE_TYPE_CONCAT_KDF_Z;
2377 		break;
2378 #endif
2379 #if defined(CFG_CRYPTO_PBKDF2)
2380 	case TEE_MAIN_ALGO_PBKDF2:
2381 		req_key_type = TEE_TYPE_PBKDF2_PASSWORD;
2382 		break;
2383 #endif
2384 	case TEE_MAIN_ALGO_X25519:
2385 		req_key_type = TEE_TYPE_X25519_KEYPAIR;
2386 		break;
2387 	default:
2388 		return TEE_ERROR_BAD_PARAMETERS;
2389 	}
2390 
2391 	if (req_key_type != o->info.objectType &&
2392 	    req_key_type2 != o->info.objectType)
2393 		return TEE_ERROR_BAD_PARAMETERS;
2394 	return TEE_SUCCESS;
2395 }
2396 
2397 TEE_Result syscall_cryp_state_alloc(unsigned long algo, unsigned long mode,
2398 			unsigned long key1, unsigned long key2,
2399 			uint32_t *state)
2400 {
2401 	struct ts_session *sess = ts_get_current_session();
2402 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
2403 	TEE_Result res = TEE_SUCCESS;
2404 	struct tee_cryp_state *cs = NULL;
2405 	struct tee_obj *o1 = NULL;
2406 	struct tee_obj *o2 = NULL;
2407 
2408 	if (key1 != 0) {
2409 		res = tee_obj_get(utc, uref_to_vaddr(key1), &o1);
2410 		if (res != TEE_SUCCESS)
2411 			return res;
2412 		if (o1->busy)
2413 			return TEE_ERROR_BAD_PARAMETERS;
2414 		res = tee_svc_cryp_check_key_type(o1, algo, mode);
2415 		if (res != TEE_SUCCESS)
2416 			return res;
2417 	}
2418 	if (key2 != 0) {
2419 		res = tee_obj_get(utc, uref_to_vaddr(key2), &o2);
2420 		if (res != TEE_SUCCESS)
2421 			return res;
2422 		if (o2->busy)
2423 			return TEE_ERROR_BAD_PARAMETERS;
2424 		res = tee_svc_cryp_check_key_type(o2, algo, mode);
2425 		if (res != TEE_SUCCESS)
2426 			return res;
2427 	}
2428 
2429 	cs = calloc(1, sizeof(struct tee_cryp_state));
2430 	if (!cs)
2431 		return TEE_ERROR_OUT_OF_MEMORY;
2432 	TAILQ_INSERT_TAIL(&utc->cryp_states, cs, link);
2433 	cs->algo = algo;
2434 	cs->mode = mode;
2435 	cs->state = CRYP_STATE_UNINITIALIZED;
2436 
2437 	switch (TEE_ALG_GET_CLASS(algo)) {
2438 	case TEE_OPERATION_CIPHER:
2439 		if ((algo == TEE_ALG_AES_XTS && (key1 == 0 || key2 == 0)) ||
2440 		    (algo != TEE_ALG_AES_XTS && (key1 == 0 || key2 != 0))) {
2441 			res = TEE_ERROR_BAD_PARAMETERS;
2442 		} else {
2443 			res = crypto_cipher_alloc_ctx(&cs->ctx, algo);
2444 			if (res != TEE_SUCCESS)
2445 				break;
2446 		}
2447 		break;
2448 	case TEE_OPERATION_AE:
2449 		if (key1 == 0 || key2 != 0) {
2450 			res = TEE_ERROR_BAD_PARAMETERS;
2451 		} else {
2452 			res = crypto_authenc_alloc_ctx(&cs->ctx, algo);
2453 			if (res != TEE_SUCCESS)
2454 				break;
2455 		}
2456 		break;
2457 	case TEE_OPERATION_MAC:
2458 		if (key1 == 0 || key2 != 0) {
2459 			res = TEE_ERROR_BAD_PARAMETERS;
2460 		} else {
2461 			res = crypto_mac_alloc_ctx(&cs->ctx, algo);
2462 			if (res != TEE_SUCCESS)
2463 				break;
2464 		}
2465 		break;
2466 	case TEE_OPERATION_DIGEST:
2467 		if (key1 != 0 || key2 != 0) {
2468 			res = TEE_ERROR_BAD_PARAMETERS;
2469 		} else {
2470 			res = crypto_hash_alloc_ctx(&cs->ctx, algo);
2471 			if (res != TEE_SUCCESS)
2472 				break;
2473 		}
2474 		break;
2475 	case TEE_OPERATION_ASYMMETRIC_CIPHER:
2476 	case TEE_OPERATION_ASYMMETRIC_SIGNATURE:
2477 		if (algo == TEE_ALG_RSASSA_PKCS1_V1_5 &&
2478 		    !IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) {
2479 			res = TEE_ERROR_NOT_SUPPORTED;
2480 			break;
2481 		}
2482 		if (key1 == 0 || key2 != 0)
2483 			res = TEE_ERROR_BAD_PARAMETERS;
2484 		break;
2485 	case TEE_OPERATION_KEY_DERIVATION:
2486 		if (algo == TEE_ALG_SM2_KEP) {
2487 			if (key1 == 0 || key2 == 0)
2488 				res = TEE_ERROR_BAD_PARAMETERS;
2489 		} else {
2490 			if (key1 == 0 || key2 != 0)
2491 				res = TEE_ERROR_BAD_PARAMETERS;
2492 		}
2493 		break;
2494 	default:
2495 		res = TEE_ERROR_NOT_SUPPORTED;
2496 		break;
2497 	}
2498 	if (res != TEE_SUCCESS)
2499 		goto out;
2500 
2501 	res = copy_kaddr_to_uref(state, cs);
2502 	if (res != TEE_SUCCESS)
2503 		goto out;
2504 
2505 	/* Register keys */
2506 	if (o1 != NULL) {
2507 		o1->busy = true;
2508 		cs->key1 = (vaddr_t)o1;
2509 	}
2510 	if (o2 != NULL) {
2511 		o2->busy = true;
2512 		cs->key2 = (vaddr_t)o2;
2513 	}
2514 
2515 out:
2516 	if (res != TEE_SUCCESS)
2517 		cryp_state_free(utc, cs);
2518 	return res;
2519 }
2520 
2521 TEE_Result syscall_cryp_state_copy(unsigned long dst, unsigned long src)
2522 {
2523 	struct ts_session *sess = ts_get_current_session();
2524 	TEE_Result res = TEE_SUCCESS;
2525 	struct tee_cryp_state *cs_dst = NULL;
2526 	struct tee_cryp_state *cs_src = NULL;
2527 
2528 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(dst), &cs_dst);
2529 	if (res != TEE_SUCCESS)
2530 		return res;
2531 
2532 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(src), &cs_src);
2533 	if (res != TEE_SUCCESS)
2534 		return res;
2535 	if (cs_dst->algo != cs_src->algo || cs_dst->mode != cs_src->mode)
2536 		return TEE_ERROR_BAD_PARAMETERS;
2537 
2538 	switch (TEE_ALG_GET_CLASS(cs_src->algo)) {
2539 	case TEE_OPERATION_CIPHER:
2540 		crypto_cipher_copy_state(cs_dst->ctx, cs_src->ctx);
2541 		break;
2542 	case TEE_OPERATION_AE:
2543 		crypto_authenc_copy_state(cs_dst->ctx, cs_src->ctx);
2544 		break;
2545 	case TEE_OPERATION_DIGEST:
2546 		crypto_hash_copy_state(cs_dst->ctx, cs_src->ctx);
2547 		break;
2548 	case TEE_OPERATION_MAC:
2549 		crypto_mac_copy_state(cs_dst->ctx, cs_src->ctx);
2550 		break;
2551 	default:
2552 		return TEE_ERROR_BAD_STATE;
2553 	}
2554 
2555 	cs_dst->state = cs_src->state;
2556 	cs_dst->ctx_finalize = cs_src->ctx_finalize;
2557 
2558 	return TEE_SUCCESS;
2559 }
2560 
2561 void tee_svc_cryp_free_states(struct user_ta_ctx *utc)
2562 {
2563 	struct tee_cryp_state_head *states = &utc->cryp_states;
2564 
2565 	while (!TAILQ_EMPTY(states))
2566 		cryp_state_free(utc, TAILQ_FIRST(states));
2567 }
2568 
2569 TEE_Result syscall_cryp_state_free(unsigned long state)
2570 {
2571 	struct ts_session *sess = ts_get_current_session();
2572 	TEE_Result res = TEE_SUCCESS;
2573 	struct tee_cryp_state *cs = NULL;
2574 
2575 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
2576 	if (res != TEE_SUCCESS)
2577 		return res;
2578 	cryp_state_free(to_user_ta_ctx(sess->ctx), cs);
2579 	return TEE_SUCCESS;
2580 }
2581 
2582 TEE_Result syscall_hash_init(unsigned long state,
2583 			     const void *iv __maybe_unused,
2584 			     size_t iv_len __maybe_unused)
2585 {
2586 	struct ts_session *sess = ts_get_current_session();
2587 	TEE_Result res = TEE_SUCCESS;
2588 	struct tee_cryp_state *cs = NULL;
2589 
2590 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
2591 	if (res != TEE_SUCCESS)
2592 		return res;
2593 
2594 	switch (TEE_ALG_GET_CLASS(cs->algo)) {
2595 	case TEE_OPERATION_DIGEST:
2596 		res = crypto_hash_init(cs->ctx);
2597 		if (res != TEE_SUCCESS)
2598 			return res;
2599 		break;
2600 	case TEE_OPERATION_MAC:
2601 		{
2602 			struct tee_obj *o;
2603 			struct tee_cryp_obj_secret *key;
2604 
2605 			res = tee_obj_get(to_user_ta_ctx(sess->ctx),
2606 					  cs->key1, &o);
2607 			if (res != TEE_SUCCESS)
2608 				return res;
2609 			if ((o->info.handleFlags &
2610 			     TEE_HANDLE_FLAG_INITIALIZED) == 0)
2611 				return TEE_ERROR_BAD_PARAMETERS;
2612 
2613 			key = (struct tee_cryp_obj_secret *)o->attr;
2614 			res = crypto_mac_init(cs->ctx, (void *)(key + 1),
2615 					      key->key_size);
2616 			if (res != TEE_SUCCESS)
2617 				return res;
2618 			break;
2619 		}
2620 	default:
2621 		return TEE_ERROR_BAD_PARAMETERS;
2622 	}
2623 
2624 	cs->state = CRYP_STATE_INITIALIZED;
2625 
2626 	return TEE_SUCCESS;
2627 }
2628 
2629 TEE_Result syscall_hash_update(unsigned long state, const void *chunk,
2630 			size_t chunk_size)
2631 {
2632 	struct ts_session *sess = ts_get_current_session();
2633 	struct tee_cryp_state *cs = NULL;
2634 	TEE_Result res = TEE_SUCCESS;
2635 
2636 	/* No data, but size provided isn't valid parameters. */
2637 	if (!chunk && chunk_size)
2638 		return TEE_ERROR_BAD_PARAMETERS;
2639 
2640 	/* Zero length hash is valid, but nothing we need to do. */
2641 	if (!chunk_size)
2642 		return TEE_SUCCESS;
2643 
2644 	chunk = memtag_strip_tag_const(chunk);
2645 
2646 	res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
2647 				     TEE_MEMORY_ACCESS_READ |
2648 				     TEE_MEMORY_ACCESS_ANY_OWNER,
2649 				     (uaddr_t)chunk, chunk_size);
2650 	if (res != TEE_SUCCESS)
2651 		return res;
2652 
2653 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
2654 	if (res != TEE_SUCCESS)
2655 		return res;
2656 
2657 	if (cs->state != CRYP_STATE_INITIALIZED)
2658 		return TEE_ERROR_BAD_STATE;
2659 
2660 	switch (TEE_ALG_GET_CLASS(cs->algo)) {
2661 	case TEE_OPERATION_DIGEST:
2662 		res = crypto_hash_update(cs->ctx, chunk, chunk_size);
2663 		if (res != TEE_SUCCESS)
2664 			return res;
2665 		break;
2666 	case TEE_OPERATION_MAC:
2667 		res = crypto_mac_update(cs->ctx, chunk, chunk_size);
2668 		if (res != TEE_SUCCESS)
2669 			return res;
2670 		break;
2671 	default:
2672 		return TEE_ERROR_BAD_PARAMETERS;
2673 	}
2674 
2675 	return TEE_SUCCESS;
2676 }
2677 
2678 TEE_Result syscall_hash_final(unsigned long state, const void *chunk,
2679 			size_t chunk_size, void *hash, uint64_t *hash_len)
2680 {
2681 	struct ts_session *sess = ts_get_current_session();
2682 	struct tee_cryp_state *cs = NULL;
2683 	TEE_Result res2 = TEE_SUCCESS;
2684 	TEE_Result res = TEE_SUCCESS;
2685 	size_t hash_size = 0;
2686 	size_t hlen = 0;
2687 
2688 	/* No data, but size provided isn't valid parameters. */
2689 	if (!chunk && chunk_size)
2690 		return TEE_ERROR_BAD_PARAMETERS;
2691 
2692 	chunk = memtag_strip_tag_const(chunk);
2693 	hash = memtag_strip_tag(hash);
2694 
2695 	res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
2696 				     TEE_MEMORY_ACCESS_READ |
2697 				     TEE_MEMORY_ACCESS_ANY_OWNER,
2698 				     (uaddr_t)chunk, chunk_size);
2699 	if (res != TEE_SUCCESS)
2700 		return res;
2701 
2702 	res = get_user_u64_as_size_t(&hlen, hash_len);
2703 	if (res != TEE_SUCCESS)
2704 		return res;
2705 
2706 	res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
2707 				     TEE_MEMORY_ACCESS_READ |
2708 				     TEE_MEMORY_ACCESS_WRITE |
2709 				     TEE_MEMORY_ACCESS_ANY_OWNER,
2710 				     (uaddr_t)hash, hlen);
2711 	if (res != TEE_SUCCESS)
2712 		return res;
2713 
2714 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
2715 	if (res != TEE_SUCCESS)
2716 		return res;
2717 
2718 	if (cs->state != CRYP_STATE_INITIALIZED)
2719 		return TEE_ERROR_BAD_STATE;
2720 
2721 	switch (TEE_ALG_GET_CLASS(cs->algo)) {
2722 	case TEE_OPERATION_DIGEST:
2723 		res = tee_alg_get_digest_size(cs->algo, &hash_size);
2724 		if (res != TEE_SUCCESS)
2725 			return res;
2726 		if (hlen < hash_size) {
2727 			res = TEE_ERROR_SHORT_BUFFER;
2728 			goto out;
2729 		}
2730 
2731 		if (chunk_size) {
2732 			res = crypto_hash_update(cs->ctx, chunk, chunk_size);
2733 			if (res != TEE_SUCCESS)
2734 				return res;
2735 		}
2736 
2737 		res = crypto_hash_final(cs->ctx, hash, hash_size);
2738 		if (res != TEE_SUCCESS)
2739 			return res;
2740 		break;
2741 
2742 	case TEE_OPERATION_MAC:
2743 		res = tee_alg_get_digest_size(cs->algo, &hash_size);
2744 		if (res != TEE_SUCCESS)
2745 			return res;
2746 		if (hlen < hash_size) {
2747 			res = TEE_ERROR_SHORT_BUFFER;
2748 			goto out;
2749 		}
2750 
2751 		if (chunk_size) {
2752 			res = crypto_mac_update(cs->ctx, chunk, chunk_size);
2753 			if (res != TEE_SUCCESS)
2754 				return res;
2755 		}
2756 
2757 		res = crypto_mac_final(cs->ctx, hash, hash_size);
2758 		if (res != TEE_SUCCESS)
2759 			return res;
2760 		break;
2761 
2762 	default:
2763 		return TEE_ERROR_BAD_PARAMETERS;
2764 	}
2765 out:
2766 	res2 = put_user_u64(hash_len, hash_size);
2767 	if (res2 != TEE_SUCCESS)
2768 		return res2;
2769 	return res;
2770 }
2771 
2772 TEE_Result syscall_cipher_init(unsigned long state, const void *iv,
2773 			size_t iv_len)
2774 {
2775 	struct ts_session *sess = ts_get_current_session();
2776 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
2777 	struct tee_cryp_obj_secret *key1 = NULL;
2778 	struct tee_cryp_state *cs = NULL;
2779 	TEE_Result res = TEE_SUCCESS;
2780 	struct tee_obj *o = NULL;
2781 
2782 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
2783 	if (res != TEE_SUCCESS)
2784 		return res;
2785 
2786 	if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_CIPHER)
2787 		return TEE_ERROR_BAD_STATE;
2788 
2789 	iv = memtag_strip_tag_const(iv);
2790 
2791 	res = vm_check_access_rights(&utc->uctx,
2792 				     TEE_MEMORY_ACCESS_READ |
2793 				     TEE_MEMORY_ACCESS_ANY_OWNER,
2794 				     (uaddr_t)iv, iv_len);
2795 	if (res != TEE_SUCCESS)
2796 		return res;
2797 
2798 	res = tee_obj_get(utc, cs->key1, &o);
2799 	if (res != TEE_SUCCESS)
2800 		return res;
2801 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
2802 		return TEE_ERROR_BAD_PARAMETERS;
2803 
2804 	key1 = o->attr;
2805 
2806 	if (tee_obj_get(utc, cs->key2, &o) == TEE_SUCCESS) {
2807 		struct tee_cryp_obj_secret *key2 = o->attr;
2808 
2809 		if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
2810 			return TEE_ERROR_BAD_PARAMETERS;
2811 
2812 		res = crypto_cipher_init(cs->ctx, cs->mode,
2813 					 (uint8_t *)(key1 + 1), key1->key_size,
2814 					 (uint8_t *)(key2 + 1), key2->key_size,
2815 					 iv, iv_len);
2816 	} else {
2817 		res = crypto_cipher_init(cs->ctx, cs->mode,
2818 					 (uint8_t *)(key1 + 1), key1->key_size,
2819 					 NULL, 0, iv, iv_len);
2820 	}
2821 	if (res != TEE_SUCCESS)
2822 		return res;
2823 
2824 	cs->ctx_finalize = crypto_cipher_final;
2825 	cs->state = CRYP_STATE_INITIALIZED;
2826 
2827 	return TEE_SUCCESS;
2828 }
2829 
2830 static TEE_Result tee_svc_cipher_update_helper(unsigned long state,
2831 			bool last_block, const void *src, size_t src_len,
2832 			void *dst, uint64_t *dst_len)
2833 {
2834 	struct ts_session *sess = ts_get_current_session();
2835 	struct tee_cryp_state *cs = NULL;
2836 	TEE_Result res = TEE_SUCCESS;
2837 	size_t dlen = 0;
2838 
2839 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
2840 	if (res != TEE_SUCCESS)
2841 		return res;
2842 
2843 	if (cs->state != CRYP_STATE_INITIALIZED)
2844 		return TEE_ERROR_BAD_STATE;
2845 
2846 	src = memtag_strip_tag_const(src);
2847 	dst = memtag_strip_tag(dst);
2848 
2849 	res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
2850 				     TEE_MEMORY_ACCESS_READ |
2851 				     TEE_MEMORY_ACCESS_ANY_OWNER,
2852 				     (uaddr_t)src, src_len);
2853 	if (res != TEE_SUCCESS)
2854 		return res;
2855 
2856 	if (!dst_len) {
2857 		dlen = 0;
2858 	} else {
2859 		struct user_mode_ctx *uctx = &to_user_ta_ctx(sess->ctx)->uctx;
2860 		uint32_t flags = TEE_MEMORY_ACCESS_READ |
2861 				 TEE_MEMORY_ACCESS_WRITE |
2862 				 TEE_MEMORY_ACCESS_ANY_OWNER;
2863 
2864 		res = get_user_u64_as_size_t(&dlen, dst_len);
2865 		if (res != TEE_SUCCESS)
2866 			return res;
2867 
2868 		res = vm_check_access_rights(uctx, flags, (uaddr_t)dst, dlen);
2869 		if (res != TEE_SUCCESS)
2870 			return res;
2871 	}
2872 
2873 	if (dlen < src_len) {
2874 		res = TEE_ERROR_SHORT_BUFFER;
2875 		goto out;
2876 	}
2877 
2878 	if (src_len > 0) {
2879 		/* Permit src_len == 0 to finalize the operation */
2880 		res = tee_do_cipher_update(cs->ctx, cs->algo, cs->mode,
2881 					   last_block, src, src_len, dst);
2882 	}
2883 
2884 	if (last_block && cs->ctx_finalize != NULL) {
2885 		cs->ctx_finalize(cs->ctx);
2886 		cs->ctx_finalize = NULL;
2887 	}
2888 
2889 out:
2890 	if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) &&
2891 	    dst_len != NULL) {
2892 		TEE_Result res2;
2893 
2894 		res2 = put_user_u64(dst_len, src_len);
2895 		if (res2 != TEE_SUCCESS)
2896 			res = res2;
2897 	}
2898 
2899 	return res;
2900 }
2901 
2902 TEE_Result syscall_cipher_update(unsigned long state, const void *src,
2903 			size_t src_len, void *dst, uint64_t *dst_len)
2904 {
2905 	return tee_svc_cipher_update_helper(state, false /* last_block */,
2906 					    src, src_len, dst, dst_len);
2907 }
2908 
2909 TEE_Result syscall_cipher_final(unsigned long state, const void *src,
2910 			size_t src_len, void *dst, uint64_t *dst_len)
2911 {
2912 	return tee_svc_cipher_update_helper(state, true /* last_block */,
2913 					    src, src_len, dst, dst_len);
2914 }
2915 
2916 #if defined(CFG_CRYPTO_HKDF)
2917 static TEE_Result get_hkdf_params(const TEE_Attribute *params,
2918 				  uint32_t param_count,
2919 				  void **salt, size_t *salt_len, void **info,
2920 				  size_t *info_len, size_t *okm_len)
2921 {
2922 	size_t n;
2923 	enum { SALT = 0x1, LENGTH = 0x2, INFO = 0x4 };
2924 	uint8_t found = 0;
2925 
2926 	*salt = *info = NULL;
2927 	*salt_len = *info_len = *okm_len = 0;
2928 
2929 	for (n = 0; n < param_count; n++) {
2930 		switch (params[n].attributeID) {
2931 		case TEE_ATTR_HKDF_SALT:
2932 			if (!(found & SALT)) {
2933 				*salt = params[n].content.ref.buffer;
2934 				*salt_len = params[n].content.ref.length;
2935 				found |= SALT;
2936 			}
2937 			break;
2938 		case TEE_ATTR_HKDF_OKM_LENGTH:
2939 			if (!(found & LENGTH)) {
2940 				*okm_len = params[n].content.value.a;
2941 				found |= LENGTH;
2942 			}
2943 			break;
2944 		case TEE_ATTR_HKDF_INFO:
2945 			if (!(found & INFO)) {
2946 				*info = params[n].content.ref.buffer;
2947 				*info_len = params[n].content.ref.length;
2948 				found |= INFO;
2949 			}
2950 			break;
2951 		default:
2952 			/* Unexpected attribute */
2953 			return TEE_ERROR_BAD_PARAMETERS;
2954 		}
2955 
2956 	}
2957 
2958 	if (!(found & LENGTH))
2959 		return TEE_ERROR_BAD_PARAMETERS;
2960 
2961 	return TEE_SUCCESS;
2962 }
2963 #endif
2964 
2965 #if defined(CFG_CRYPTO_CONCAT_KDF)
2966 static TEE_Result get_concat_kdf_params(const TEE_Attribute *params,
2967 					uint32_t param_count,
2968 					void **other_info,
2969 					size_t *other_info_len,
2970 					size_t *derived_key_len)
2971 {
2972 	size_t n;
2973 	enum { LENGTH = 0x1, INFO = 0x2 };
2974 	uint8_t found = 0;
2975 
2976 	*other_info = NULL;
2977 	*other_info_len = *derived_key_len = 0;
2978 
2979 	for (n = 0; n < param_count; n++) {
2980 		switch (params[n].attributeID) {
2981 		case TEE_ATTR_CONCAT_KDF_OTHER_INFO:
2982 			if (!(found & INFO)) {
2983 				*other_info = params[n].content.ref.buffer;
2984 				*other_info_len = params[n].content.ref.length;
2985 				found |= INFO;
2986 			}
2987 			break;
2988 		case TEE_ATTR_CONCAT_KDF_DKM_LENGTH:
2989 			if (!(found & LENGTH)) {
2990 				*derived_key_len = params[n].content.value.a;
2991 				found |= LENGTH;
2992 			}
2993 			break;
2994 		default:
2995 			/* Unexpected attribute */
2996 			return TEE_ERROR_BAD_PARAMETERS;
2997 		}
2998 	}
2999 
3000 	if (!(found & LENGTH))
3001 		return TEE_ERROR_BAD_PARAMETERS;
3002 
3003 	return TEE_SUCCESS;
3004 }
3005 #endif
3006 
3007 #if defined(CFG_CRYPTO_PBKDF2)
3008 static TEE_Result get_pbkdf2_params(const TEE_Attribute *params,
3009 				   uint32_t param_count, void **salt,
3010 				   size_t *salt_len, size_t *derived_key_len,
3011 				   size_t *iteration_count)
3012 {
3013 	size_t n;
3014 	enum { SALT = 0x1, LENGTH = 0x2, COUNT = 0x4 };
3015 	uint8_t found = 0;
3016 
3017 	*salt = NULL;
3018 	*salt_len = *derived_key_len = *iteration_count = 0;
3019 
3020 	for (n = 0; n < param_count; n++) {
3021 		switch (params[n].attributeID) {
3022 		case TEE_ATTR_PBKDF2_SALT:
3023 			if (!(found & SALT)) {
3024 				*salt = params[n].content.ref.buffer;
3025 				*salt_len = params[n].content.ref.length;
3026 				found |= SALT;
3027 			}
3028 			break;
3029 		case TEE_ATTR_PBKDF2_DKM_LENGTH:
3030 			if (!(found & LENGTH)) {
3031 				*derived_key_len = params[n].content.value.a;
3032 				found |= LENGTH;
3033 			}
3034 			break;
3035 		case TEE_ATTR_PBKDF2_ITERATION_COUNT:
3036 			if (!(found & COUNT)) {
3037 				*iteration_count = params[n].content.value.a;
3038 				found |= COUNT;
3039 			}
3040 			break;
3041 		default:
3042 			/* Unexpected attribute */
3043 			return TEE_ERROR_BAD_PARAMETERS;
3044 		}
3045 	}
3046 
3047 	if ((found & (LENGTH|COUNT)) != (LENGTH|COUNT))
3048 		return TEE_ERROR_BAD_PARAMETERS;
3049 
3050 	return TEE_SUCCESS;
3051 }
3052 #endif
3053 
3054 #if defined(CFG_CRYPTO_SM2_KEP)
3055 static TEE_Result get_sm2_kep_params(const TEE_Attribute *params,
3056 				     uint32_t param_count,
3057 				     struct ecc_public_key *peer_key,
3058 				     struct ecc_public_key *peer_eph_key,
3059 				     struct sm2_kep_parms *kep_parms)
3060 {
3061 	TEE_Result res = TEE_ERROR_GENERIC;
3062 	size_t n;
3063 	enum {
3064 		IS_INITIATOR,
3065 		PEER_KEY_X,
3066 		PEER_KEY_Y,
3067 		PEER_EPH_KEY_X,
3068 		PEER_EPH_KEY_Y,
3069 		INITIATOR_ID,
3070 		RESPONDER_ID,
3071 	};
3072 	uint8_t mandatory = BIT(IS_INITIATOR) | BIT(PEER_KEY_X) |
3073 		BIT(PEER_KEY_Y) | BIT(PEER_EPH_KEY_X) | BIT(PEER_EPH_KEY_Y) |
3074 		BIT(INITIATOR_ID) | BIT(RESPONDER_ID);
3075 	uint8_t found = 0;
3076 
3077 	res = crypto_acipher_alloc_ecc_public_key(peer_key,
3078 						  TEE_TYPE_SM2_KEP_PUBLIC_KEY,
3079 						  256);
3080 	if (res)
3081 		return res;
3082 
3083 	res = crypto_acipher_alloc_ecc_public_key(peer_eph_key,
3084 						  TEE_TYPE_SM2_KEP_PUBLIC_KEY,
3085 						  256);
3086 	if (res)
3087 		goto out_p;
3088 
3089 	peer_key->curve = TEE_ECC_CURVE_SM2;
3090 	peer_eph_key->curve = TEE_ECC_CURVE_SM2;
3091 
3092 	for (n = 0; n < param_count; n++) {
3093 		const TEE_Attribute *p = &params[n];
3094 
3095 		switch (p->attributeID) {
3096 		case TEE_ATTR_SM2_KEP_USER:
3097 			kep_parms->is_initiator = !p->content.value.a;
3098 			found |= BIT(IS_INITIATOR);
3099 			break;
3100 		case TEE_ATTR_ECC_PUBLIC_VALUE_X:
3101 			crypto_bignum_bin2bn(p->content.ref.buffer,
3102 					     p->content.ref.length,
3103 					     peer_key->x);
3104 			found |= BIT(PEER_KEY_X);
3105 			break;
3106 		case TEE_ATTR_ECC_PUBLIC_VALUE_Y:
3107 			crypto_bignum_bin2bn(p->content.ref.buffer,
3108 					     p->content.ref.length,
3109 					     peer_key->y);
3110 			found |= BIT(PEER_KEY_Y);
3111 			break;
3112 		case TEE_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_X:
3113 			crypto_bignum_bin2bn(p->content.ref.buffer,
3114 					     p->content.ref.length,
3115 					     peer_eph_key->x);
3116 			found |= BIT(PEER_EPH_KEY_X);
3117 			break;
3118 		case TEE_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_Y:
3119 			crypto_bignum_bin2bn(p->content.ref.buffer,
3120 					     p->content.ref.length,
3121 					     peer_eph_key->y);
3122 			found |= BIT(PEER_EPH_KEY_Y);
3123 			break;
3124 		case TEE_ATTR_SM2_ID_INITIATOR:
3125 			kep_parms->initiator_id = p->content.ref.buffer;
3126 			kep_parms->initiator_id_len = p->content.ref.length;
3127 			found |= BIT(INITIATOR_ID);
3128 			break;
3129 		case TEE_ATTR_SM2_ID_RESPONDER:
3130 			kep_parms->responder_id = p->content.ref.buffer;
3131 			kep_parms->responder_id_len = p->content.ref.length;
3132 			found |= BIT(RESPONDER_ID);
3133 			break;
3134 		case TEE_ATTR_SM2_KEP_CONFIRMATION_IN:
3135 			kep_parms->conf_in = p->content.ref.buffer;
3136 			kep_parms->conf_in_len = p->content.ref.length;
3137 			break;
3138 		case TEE_ATTR_SM2_KEP_CONFIRMATION_OUT:
3139 			kep_parms->conf_out = p->content.ref.buffer;
3140 			kep_parms->conf_out_len = p->content.ref.length;
3141 			break;
3142 		default:
3143 			/* Unexpected attribute */
3144 			res = TEE_ERROR_BAD_PARAMETERS;
3145 			goto out;
3146 		}
3147 	}
3148 
3149 	if ((found & mandatory) != mandatory) {
3150 		res = TEE_ERROR_BAD_PARAMETERS;
3151 		goto out;
3152 	}
3153 
3154 	return TEE_SUCCESS;
3155 out:
3156 	crypto_acipher_free_ecc_public_key(peer_eph_key);
3157 out_p:
3158 	crypto_acipher_free_ecc_public_key(peer_key);
3159 	return res;
3160 }
3161 #endif
3162 
3163 TEE_Result syscall_cryp_derive_key(unsigned long state,
3164 			const struct utee_attribute *usr_params,
3165 			unsigned long param_count, unsigned long derived_key)
3166 {
3167 	struct ts_session *sess = ts_get_current_session();
3168 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
3169 	TEE_Result res = TEE_ERROR_NOT_SUPPORTED;
3170 	struct tee_obj *ko = NULL;
3171 	struct tee_obj *so = NULL;
3172 	struct tee_cryp_state *cs = NULL;
3173 	struct tee_cryp_obj_secret *sk = NULL;
3174 	const struct tee_cryp_obj_type_props *type_props = NULL;
3175 	TEE_Attribute *params = NULL;
3176 	size_t alloc_size = 0;
3177 
3178 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
3179 	if (res != TEE_SUCCESS)
3180 		return res;
3181 
3182 	if (MUL_OVERFLOW(sizeof(TEE_Attribute), param_count, &alloc_size))
3183 		return TEE_ERROR_OVERFLOW;
3184 
3185 	params = malloc(alloc_size);
3186 	if (!params)
3187 		return TEE_ERROR_OUT_OF_MEMORY;
3188 	res = copy_in_attrs(utc, usr_params, param_count, params);
3189 	if (res != TEE_SUCCESS)
3190 		goto out;
3191 
3192 	/* Get key set in operation */
3193 	res = tee_obj_get(utc, cs->key1, &ko);
3194 	if (res != TEE_SUCCESS)
3195 		goto out;
3196 
3197 	res = tee_obj_get(utc, uref_to_vaddr(derived_key), &so);
3198 	if (res != TEE_SUCCESS)
3199 		goto out;
3200 
3201 	/* Find information needed about the object to initialize */
3202 	sk = so->attr;
3203 
3204 	/* Find description of object */
3205 	type_props = tee_svc_find_type_props(so->info.objectType);
3206 	if (!type_props) {
3207 		res = TEE_ERROR_NOT_SUPPORTED;
3208 		goto out;
3209 	}
3210 
3211 	if (cs->algo == TEE_ALG_DH_DERIVE_SHARED_SECRET) {
3212 		struct bignum *pub = NULL;
3213 		struct bignum *ss = NULL;
3214 		size_t bin_size = 0;
3215 
3216 		if (param_count != 1 ||
3217 		    params[0].attributeID != TEE_ATTR_DH_PUBLIC_VALUE) {
3218 			res = TEE_ERROR_BAD_PARAMETERS;
3219 			goto out;
3220 		}
3221 
3222 		bin_size = params[0].content.ref.length;
3223 
3224 		if (MUL_OVERFLOW(bin_size, 8, &alloc_size)) {
3225 			res = TEE_ERROR_OVERFLOW;
3226 			goto out;
3227 		}
3228 
3229 		pub = crypto_bignum_allocate(alloc_size);
3230 		ss = crypto_bignum_allocate(alloc_size);
3231 		if (pub && ss) {
3232 			crypto_bignum_bin2bn(params[0].content.ref.buffer,
3233 					     bin_size, pub);
3234 			res = crypto_acipher_dh_shared_secret(ko->attr,
3235 							      pub, ss);
3236 			if (res == TEE_SUCCESS) {
3237 				sk->key_size = crypto_bignum_num_bytes(ss);
3238 				crypto_bignum_bn2bin(ss, (uint8_t *)(sk + 1));
3239 				so->info.handleFlags |=
3240 						TEE_HANDLE_FLAG_INITIALIZED;
3241 				set_attribute(so, type_props,
3242 					      TEE_ATTR_SECRET_VALUE);
3243 			}
3244 		} else {
3245 			res = TEE_ERROR_OUT_OF_MEMORY;
3246 		}
3247 		crypto_bignum_free(pub);
3248 		crypto_bignum_free(ss);
3249 	} else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_ECDH) {
3250 		struct ecc_public_key key_public;
3251 		uint8_t *pt_secret;
3252 		unsigned long pt_secret_len;
3253 		uint32_t key_type = TEE_TYPE_ECDH_PUBLIC_KEY;
3254 
3255 		if (param_count != 2 ||
3256 		    params[0].attributeID != TEE_ATTR_ECC_PUBLIC_VALUE_X ||
3257 		    params[1].attributeID != TEE_ATTR_ECC_PUBLIC_VALUE_Y) {
3258 			res = TEE_ERROR_BAD_PARAMETERS;
3259 			goto out;
3260 		}
3261 
3262 		switch (cs->algo) {
3263 		case TEE_ALG_ECDH_P192:
3264 			alloc_size = 192;
3265 			break;
3266 		case TEE_ALG_ECDH_P224:
3267 			alloc_size = 224;
3268 			break;
3269 		case TEE_ALG_ECDH_P256:
3270 			alloc_size = 256;
3271 			break;
3272 		case TEE_ALG_ECDH_P384:
3273 			alloc_size = 384;
3274 			break;
3275 		case TEE_ALG_ECDH_P521:
3276 			alloc_size = 521;
3277 			break;
3278 		default:
3279 			res = TEE_ERROR_NOT_IMPLEMENTED;
3280 			goto out;
3281 		}
3282 
3283 		/* Create the public key */
3284 		res = crypto_acipher_alloc_ecc_public_key(&key_public, key_type,
3285 							  alloc_size);
3286 		if (res != TEE_SUCCESS)
3287 			goto out;
3288 		key_public.curve = ((struct ecc_keypair *)ko->attr)->curve;
3289 		crypto_bignum_bin2bn(params[0].content.ref.buffer,
3290 				     params[0].content.ref.length,
3291 				     key_public.x);
3292 		crypto_bignum_bin2bn(params[1].content.ref.buffer,
3293 				     params[1].content.ref.length,
3294 				     key_public.y);
3295 
3296 		pt_secret = (uint8_t *)(sk + 1);
3297 		pt_secret_len = sk->alloc_size;
3298 		res = crypto_acipher_ecc_shared_secret(ko->attr, &key_public,
3299 						       pt_secret,
3300 						       &pt_secret_len);
3301 
3302 		if (res == TEE_SUCCESS) {
3303 			sk->key_size = pt_secret_len;
3304 			so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
3305 			set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE);
3306 		}
3307 
3308 		/* free the public key */
3309 		crypto_acipher_free_ecc_public_key(&key_public);
3310 	}
3311 #if defined(CFG_CRYPTO_HKDF)
3312 	else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_HKDF) {
3313 		void *salt, *info;
3314 		size_t salt_len, info_len, okm_len;
3315 		uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo);
3316 		struct tee_cryp_obj_secret *ik = ko->attr;
3317 		const uint8_t *ikm = (const uint8_t *)(ik + 1);
3318 
3319 		res = get_hkdf_params(params, param_count, &salt, &salt_len,
3320 				      &info, &info_len, &okm_len);
3321 		if (res != TEE_SUCCESS)
3322 			goto out;
3323 
3324 		/* Requested size must fit into the output object's buffer */
3325 		if (okm_len > ik->alloc_size) {
3326 			res = TEE_ERROR_BAD_PARAMETERS;
3327 			goto out;
3328 		}
3329 
3330 		res = tee_cryp_hkdf(hash_id, ikm, ik->key_size, salt, salt_len,
3331 				    info, info_len, (uint8_t *)(sk + 1),
3332 				    okm_len);
3333 		if (res == TEE_SUCCESS) {
3334 			sk->key_size = okm_len;
3335 			so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
3336 			set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE);
3337 		}
3338 	}
3339 #endif
3340 #if defined(CFG_CRYPTO_CONCAT_KDF)
3341 	else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_CONCAT_KDF) {
3342 		void *info;
3343 		size_t info_len, derived_key_len;
3344 		uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo);
3345 		struct tee_cryp_obj_secret *ss = ko->attr;
3346 		const uint8_t *shared_secret = (const uint8_t *)(ss + 1);
3347 
3348 		res = get_concat_kdf_params(params, param_count, &info,
3349 					    &info_len, &derived_key_len);
3350 		if (res != TEE_SUCCESS)
3351 			goto out;
3352 
3353 		/* Requested size must fit into the output object's buffer */
3354 		if (derived_key_len > ss->alloc_size) {
3355 			res = TEE_ERROR_BAD_PARAMETERS;
3356 			goto out;
3357 		}
3358 
3359 		res = tee_cryp_concat_kdf(hash_id, shared_secret, ss->key_size,
3360 					  info, info_len, (uint8_t *)(sk + 1),
3361 					  derived_key_len);
3362 		if (res == TEE_SUCCESS) {
3363 			sk->key_size = derived_key_len;
3364 			so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
3365 			set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE);
3366 		}
3367 	}
3368 #endif
3369 #if defined(CFG_CRYPTO_PBKDF2)
3370 	else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_PBKDF2) {
3371 		void *salt;
3372 		size_t salt_len, iteration_count, derived_key_len;
3373 		uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo);
3374 		struct tee_cryp_obj_secret *ss = ko->attr;
3375 		const uint8_t *password = (const uint8_t *)(ss + 1);
3376 
3377 		res = get_pbkdf2_params(params, param_count, &salt, &salt_len,
3378 					&derived_key_len, &iteration_count);
3379 		if (res != TEE_SUCCESS)
3380 			goto out;
3381 
3382 		/* Requested size must fit into the output object's buffer */
3383 		if (derived_key_len > ss->alloc_size) {
3384 			res = TEE_ERROR_BAD_PARAMETERS;
3385 			goto out;
3386 		}
3387 
3388 		res = tee_cryp_pbkdf2(hash_id, password, ss->key_size, salt,
3389 				      salt_len, iteration_count,
3390 				      (uint8_t *)(sk + 1), derived_key_len);
3391 		if (res == TEE_SUCCESS) {
3392 			sk->key_size = derived_key_len;
3393 			so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
3394 			set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE);
3395 		}
3396 	}
3397 #endif
3398 #if defined(CFG_CRYPTO_SM2_KEP)
3399 	else if (cs->algo == TEE_ALG_SM2_KEP) {
3400 		struct ecc_public_key peer_eph_key = { };
3401 		struct ecc_public_key peer_key = { };
3402 		struct sm2_kep_parms kep_parms = {
3403 			.out = (uint8_t *)(sk + 1),
3404 			.out_len = so->info.maxKeySize,
3405 		};
3406 		struct tee_obj *ko2 = NULL;
3407 
3408 		res = tee_obj_get(utc, cs->key2, &ko2);
3409 		if (res != TEE_SUCCESS)
3410 			goto out;
3411 
3412 		res = get_sm2_kep_params(params, param_count, &peer_key,
3413 					 &peer_eph_key, &kep_parms);
3414 		if (res != TEE_SUCCESS)
3415 			goto out;
3416 
3417 		/*
3418 		 * key1 is our private keypair, key2 is our ephemeral public key
3419 		 */
3420 		res = crypto_acipher_sm2_kep_derive(ko->attr, /* key1 */
3421 						    ko2->attr, /* key2 */
3422 						    &peer_key, &peer_eph_key,
3423 						    &kep_parms);
3424 
3425 		if (res == TEE_SUCCESS) {
3426 			sk->key_size = kep_parms.out_len;
3427 			so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
3428 			set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE);
3429 		}
3430 		crypto_acipher_free_ecc_public_key(&peer_key);
3431 		crypto_acipher_free_ecc_public_key(&peer_eph_key);
3432 	}
3433 #endif
3434 #if defined(CFG_CRYPTO_X25519)
3435 	else if (cs->algo == TEE_ALG_X25519) {
3436 		uint8_t *x25519_pub_key = NULL;
3437 		uint8_t *pt_secret = NULL;
3438 		unsigned long pt_secret_len = 0;
3439 
3440 		if (param_count != 1 ||
3441 		    params[0].attributeID != TEE_ATTR_X25519_PUBLIC_VALUE) {
3442 			res = TEE_ERROR_BAD_PARAMETERS;
3443 			goto out;
3444 		}
3445 
3446 		/* X25519 public key size is 32 bytes */
3447 		if (params[0].content.ref.length != KEY_SIZE_BYTES_25519) {
3448 			res = TEE_ERROR_BAD_PARAMETERS;
3449 			goto out;
3450 		}
3451 
3452 		/* Set the public key */
3453 		x25519_pub_key = params[0].content.ref.buffer;
3454 
3455 		pt_secret = (uint8_t *)(sk + 1);
3456 		pt_secret_len = sk->alloc_size;
3457 		res = crypto_acipher_x25519_shared_secret(ko->attr,
3458 							  x25519_pub_key,
3459 							  pt_secret,
3460 							  &pt_secret_len);
3461 
3462 		if (res == TEE_SUCCESS) {
3463 			sk->key_size = pt_secret_len;
3464 			so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
3465 			set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE);
3466 		}
3467 	}
3468 #endif
3469 	else
3470 		res = TEE_ERROR_NOT_SUPPORTED;
3471 
3472 out:
3473 	free_wipe(params);
3474 	return res;
3475 }
3476 
3477 TEE_Result syscall_cryp_random_number_generate(void *buf, size_t blen)
3478 {
3479 	struct ts_session *sess = ts_get_current_session();
3480 	TEE_Result res = TEE_SUCCESS;
3481 
3482 	buf = memtag_strip_tag(buf);
3483 
3484 	res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
3485 				     TEE_MEMORY_ACCESS_WRITE,
3486 				     (uaddr_t)buf, blen);
3487 	if (res != TEE_SUCCESS)
3488 		return res;
3489 
3490 	res = crypto_rng_read(buf, blen);
3491 	if (res != TEE_SUCCESS)
3492 		return res;
3493 
3494 	return res;
3495 }
3496 
3497 TEE_Result syscall_authenc_init(unsigned long state, const void *nonce,
3498 				size_t nonce_len, size_t tag_len,
3499 				size_t aad_len, size_t payload_len)
3500 {
3501 	struct ts_session *sess = ts_get_current_session();
3502 	struct tee_cryp_obj_secret *key = NULL;
3503 	struct tee_cryp_state *cs = NULL;
3504 	TEE_Result res = TEE_SUCCESS;
3505 	struct tee_obj *o = NULL;
3506 
3507 	nonce = memtag_strip_tag_const(nonce);
3508 
3509 	res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
3510 				     TEE_MEMORY_ACCESS_READ |
3511 				     TEE_MEMORY_ACCESS_ANY_OWNER,
3512 				     (uaddr_t)nonce, nonce_len);
3513 	if (res != TEE_SUCCESS)
3514 		return res;
3515 
3516 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
3517 	if (res != TEE_SUCCESS)
3518 		return res;
3519 
3520 	res = tee_obj_get(to_user_ta_ctx(sess->ctx), cs->key1, &o);
3521 	if (res != TEE_SUCCESS)
3522 		return res;
3523 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
3524 		return TEE_ERROR_BAD_PARAMETERS;
3525 
3526 	key = o->attr;
3527 	res = crypto_authenc_init(cs->ctx, cs->mode, (uint8_t *)(key + 1),
3528 				  key->key_size, nonce, nonce_len, tag_len,
3529 				  aad_len, payload_len);
3530 	if (res != TEE_SUCCESS)
3531 		return res;
3532 
3533 	cs->ctx_finalize = crypto_authenc_final;
3534 	cs->state = CRYP_STATE_INITIALIZED;
3535 
3536 	return TEE_SUCCESS;
3537 }
3538 
3539 TEE_Result syscall_authenc_update_aad(unsigned long state,
3540 				      const void *aad_data, size_t aad_data_len)
3541 {
3542 	struct ts_session *sess = ts_get_current_session();
3543 	TEE_Result res = TEE_SUCCESS;
3544 	struct tee_cryp_state *cs = NULL;
3545 
3546 	aad_data = memtag_strip_tag_const(aad_data);
3547 
3548 	res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
3549 				     TEE_MEMORY_ACCESS_READ |
3550 				     TEE_MEMORY_ACCESS_ANY_OWNER,
3551 				     (uaddr_t)aad_data, aad_data_len);
3552 	if (res != TEE_SUCCESS)
3553 		return res;
3554 
3555 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
3556 	if (res != TEE_SUCCESS)
3557 		return res;
3558 
3559 	if (cs->state != CRYP_STATE_INITIALIZED)
3560 		return TEE_ERROR_BAD_STATE;
3561 
3562 	if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE)
3563 		return TEE_ERROR_BAD_STATE;
3564 
3565 	res = crypto_authenc_update_aad(cs->ctx, cs->mode, aad_data,
3566 					aad_data_len);
3567 	if (res != TEE_SUCCESS)
3568 		return res;
3569 
3570 	return TEE_SUCCESS;
3571 }
3572 
3573 TEE_Result syscall_authenc_update_payload(unsigned long state,
3574 					  const void *src_data,
3575 					  size_t src_len, void *dst_data,
3576 					  uint64_t *dst_len)
3577 {
3578 	struct ts_session *sess = ts_get_current_session();
3579 	struct tee_cryp_state *cs = NULL;
3580 	TEE_Result res = TEE_SUCCESS;
3581 	size_t dlen = 0;
3582 
3583 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
3584 	if (res != TEE_SUCCESS)
3585 		return res;
3586 
3587 	if (cs->state != CRYP_STATE_INITIALIZED)
3588 		return TEE_ERROR_BAD_STATE;
3589 
3590 	if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE)
3591 		return TEE_ERROR_BAD_STATE;
3592 
3593 	src_data = memtag_strip_tag_const(src_data);
3594 	dst_data = memtag_strip_tag(dst_data);
3595 
3596 	res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
3597 				     TEE_MEMORY_ACCESS_READ |
3598 				     TEE_MEMORY_ACCESS_ANY_OWNER,
3599 				     (uaddr_t)src_data, src_len);
3600 	if (res != TEE_SUCCESS)
3601 		return res;
3602 
3603 	res = get_user_u64_as_size_t(&dlen, dst_len);
3604 	if (res != TEE_SUCCESS)
3605 		return res;
3606 
3607 	res = vm_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
3608 				     TEE_MEMORY_ACCESS_READ |
3609 				     TEE_MEMORY_ACCESS_WRITE |
3610 				     TEE_MEMORY_ACCESS_ANY_OWNER,
3611 				     (uaddr_t)dst_data, dlen);
3612 	if (res != TEE_SUCCESS)
3613 		return res;
3614 
3615 	if (dlen < src_len) {
3616 		res = TEE_ERROR_SHORT_BUFFER;
3617 		goto out;
3618 	}
3619 
3620 	res = crypto_authenc_update_payload(cs->ctx, cs->mode, src_data,
3621 					    src_len, dst_data, &dlen);
3622 out:
3623 	if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) {
3624 		TEE_Result res2 = put_user_u64(dst_len, dlen);
3625 
3626 		if (res2 != TEE_SUCCESS)
3627 			res = res2;
3628 	}
3629 
3630 	return res;
3631 }
3632 
3633 TEE_Result syscall_authenc_enc_final(unsigned long state, const void *src_data,
3634 				     size_t src_len, void *dst_data,
3635 				     uint64_t *dst_len, void *tag,
3636 				     uint64_t *tag_len)
3637 {
3638 	struct ts_session *sess = ts_get_current_session();
3639 	struct user_mode_ctx *uctx = &to_user_ta_ctx(sess->ctx)->uctx;
3640 	struct tee_cryp_state *cs = NULL;
3641 	TEE_Result res = TEE_SUCCESS;
3642 	size_t dlen = 0;
3643 	size_t tlen = 0;
3644 
3645 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
3646 	if (res != TEE_SUCCESS)
3647 		return res;
3648 
3649 	if (cs->state != CRYP_STATE_INITIALIZED)
3650 		return TEE_ERROR_BAD_STATE;
3651 
3652 	if (cs->mode != TEE_MODE_ENCRYPT)
3653 		return TEE_ERROR_BAD_PARAMETERS;
3654 
3655 	if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE)
3656 		return TEE_ERROR_BAD_STATE;
3657 
3658 	src_data = memtag_strip_tag_const(src_data);
3659 	dst_data = memtag_strip_tag(dst_data);
3660 	tag = memtag_strip_tag(tag);
3661 
3662 	res = vm_check_access_rights(uctx,
3663 				     TEE_MEMORY_ACCESS_READ |
3664 				     TEE_MEMORY_ACCESS_ANY_OWNER,
3665 				     (uaddr_t)src_data, src_len);
3666 	if (res != TEE_SUCCESS)
3667 		return res;
3668 
3669 	if (!dst_len) {
3670 		dlen = 0;
3671 	} else {
3672 		res = get_user_u64_as_size_t(&dlen, dst_len);
3673 		if (res != TEE_SUCCESS)
3674 			return res;
3675 
3676 		res = vm_check_access_rights(uctx,
3677 					     TEE_MEMORY_ACCESS_READ |
3678 					     TEE_MEMORY_ACCESS_WRITE |
3679 					     TEE_MEMORY_ACCESS_ANY_OWNER,
3680 					     (uaddr_t)dst_data, dlen);
3681 		if (res != TEE_SUCCESS)
3682 			return res;
3683 	}
3684 
3685 	if (dlen < src_len) {
3686 		res = TEE_ERROR_SHORT_BUFFER;
3687 		goto out;
3688 	}
3689 
3690 	res = get_user_u64_as_size_t(&tlen, tag_len);
3691 	if (res != TEE_SUCCESS)
3692 		return res;
3693 
3694 	res = vm_check_access_rights(uctx,
3695 				     TEE_MEMORY_ACCESS_READ |
3696 				     TEE_MEMORY_ACCESS_WRITE |
3697 				     TEE_MEMORY_ACCESS_ANY_OWNER,
3698 				     (uaddr_t)tag, tlen);
3699 	if (res != TEE_SUCCESS)
3700 		return res;
3701 
3702 	res = crypto_authenc_enc_final(cs->ctx, src_data, src_len, dst_data,
3703 				       &dlen, tag, &tlen);
3704 
3705 out:
3706 	if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) {
3707 		TEE_Result res2 = TEE_SUCCESS;
3708 
3709 		if (dst_len != NULL) {
3710 			res2 = put_user_u64(dst_len, dlen);
3711 			if (res2 != TEE_SUCCESS)
3712 				return res2;
3713 		}
3714 
3715 		res2 = put_user_u64(tag_len, tlen);
3716 		if (res2 != TEE_SUCCESS)
3717 			return res2;
3718 	}
3719 
3720 	return res;
3721 }
3722 
3723 TEE_Result syscall_authenc_dec_final(unsigned long state,
3724 			const void *src_data, size_t src_len, void *dst_data,
3725 			uint64_t *dst_len, const void *tag, size_t tag_len)
3726 {
3727 	struct ts_session *sess = ts_get_current_session();
3728 	struct user_mode_ctx *uctx = &to_user_ta_ctx(sess->ctx)->uctx;
3729 	struct tee_cryp_state *cs = NULL;
3730 	TEE_Result res = TEE_SUCCESS;
3731 	size_t dlen = 0;
3732 
3733 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
3734 	if (res != TEE_SUCCESS)
3735 		return res;
3736 
3737 	if (cs->state != CRYP_STATE_INITIALIZED)
3738 		return TEE_ERROR_BAD_STATE;
3739 
3740 	if (cs->mode != TEE_MODE_DECRYPT)
3741 		return TEE_ERROR_BAD_PARAMETERS;
3742 
3743 	if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE)
3744 		return TEE_ERROR_BAD_STATE;
3745 
3746 	src_data = memtag_strip_tag_const(src_data);
3747 	dst_data = memtag_strip_tag(dst_data);
3748 	tag = memtag_strip_tag_const(tag);
3749 
3750 	res = vm_check_access_rights(uctx,
3751 				     TEE_MEMORY_ACCESS_READ |
3752 				     TEE_MEMORY_ACCESS_ANY_OWNER,
3753 				     (uaddr_t)src_data, src_len);
3754 	if (res != TEE_SUCCESS)
3755 		return res;
3756 
3757 	if (!dst_len) {
3758 		dlen = 0;
3759 	} else {
3760 		res = get_user_u64_as_size_t(&dlen, dst_len);
3761 		if (res != TEE_SUCCESS)
3762 			return res;
3763 
3764 		res = vm_check_access_rights(uctx,
3765 					     TEE_MEMORY_ACCESS_READ |
3766 					     TEE_MEMORY_ACCESS_WRITE |
3767 					     TEE_MEMORY_ACCESS_ANY_OWNER,
3768 					     (uaddr_t)dst_data, dlen);
3769 		if (res != TEE_SUCCESS)
3770 			return res;
3771 	}
3772 
3773 	if (dlen < src_len) {
3774 		res = TEE_ERROR_SHORT_BUFFER;
3775 		goto out;
3776 	}
3777 
3778 	res = vm_check_access_rights(uctx, TEE_MEMORY_ACCESS_READ,
3779 				     (uaddr_t)tag, tag_len);
3780 	if (res != TEE_SUCCESS)
3781 		return res;
3782 
3783 	res = crypto_authenc_dec_final(cs->ctx, src_data, src_len, dst_data,
3784 				       &dlen, tag, tag_len);
3785 
3786 out:
3787 	if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) &&
3788 	    dst_len != NULL) {
3789 		TEE_Result res2 = put_user_u64(dst_len, dlen);
3790 
3791 		if (res2 != TEE_SUCCESS)
3792 			return res2;
3793 	}
3794 
3795 	return res;
3796 }
3797 
3798 static int pkcs1_get_salt_len(const TEE_Attribute *params, uint32_t num_params,
3799 			      size_t default_len)
3800 {
3801 	size_t n;
3802 
3803 	assert(default_len < INT_MAX);
3804 
3805 	for (n = 0; n < num_params; n++) {
3806 		if (params[n].attributeID == TEE_ATTR_RSA_PSS_SALT_LENGTH) {
3807 			if (params[n].content.value.a < INT_MAX)
3808 				return params[n].content.value.a;
3809 			break;
3810 		}
3811 	}
3812 	/*
3813 	 * If salt length isn't provided use the default value which is
3814 	 * the length of the digest.
3815 	 */
3816 	return default_len;
3817 }
3818 
3819 TEE_Result syscall_asymm_operate(unsigned long state,
3820 			const struct utee_attribute *usr_params,
3821 			size_t num_params, const void *src_data, size_t src_len,
3822 			void *dst_data, uint64_t *dst_len)
3823 {
3824 	struct ts_session *sess = ts_get_current_session();
3825 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
3826 	TEE_Result res = TEE_SUCCESS;
3827 	struct tee_cryp_state *cs = NULL;
3828 	size_t dlen = 0;
3829 	struct tee_obj *o = NULL;
3830 	void *label = NULL;
3831 	size_t label_len = 0;
3832 	size_t n = 0;
3833 	int salt_len = 0;
3834 	TEE_Attribute *params = NULL;
3835 	size_t alloc_size = 0;
3836 
3837 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
3838 	if (res != TEE_SUCCESS)
3839 		return res;
3840 
3841 	src_data = memtag_strip_tag_const(src_data);
3842 	dst_data = memtag_strip_tag(dst_data);
3843 
3844 	res = vm_check_access_rights(&utc->uctx,
3845 				     TEE_MEMORY_ACCESS_READ |
3846 				     TEE_MEMORY_ACCESS_ANY_OWNER,
3847 				     (uaddr_t)src_data, src_len);
3848 	if (res != TEE_SUCCESS)
3849 		return res;
3850 
3851 	res = get_user_u64_as_size_t(&dlen, dst_len);
3852 	if (res != TEE_SUCCESS)
3853 		return res;
3854 
3855 	res = vm_check_access_rights(&utc->uctx,
3856 				     TEE_MEMORY_ACCESS_READ |
3857 				     TEE_MEMORY_ACCESS_WRITE |
3858 				     TEE_MEMORY_ACCESS_ANY_OWNER,
3859 				     (uaddr_t)dst_data, dlen);
3860 	if (res != TEE_SUCCESS)
3861 		return res;
3862 
3863 	if (MUL_OVERFLOW(sizeof(TEE_Attribute), num_params, &alloc_size))
3864 		return TEE_ERROR_OVERFLOW;
3865 
3866 	params = malloc(alloc_size);
3867 	if (!params)
3868 		return TEE_ERROR_OUT_OF_MEMORY;
3869 	res = copy_in_attrs(utc, usr_params, num_params, params);
3870 	if (res != TEE_SUCCESS)
3871 		goto out;
3872 
3873 	res = tee_obj_get(utc, cs->key1, &o);
3874 	if (res != TEE_SUCCESS)
3875 		goto out;
3876 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
3877 		res = TEE_ERROR_GENERIC;
3878 		goto out;
3879 	}
3880 
3881 	switch (cs->algo) {
3882 	case TEE_ALG_RSA_NOPAD:
3883 		if (cs->mode == TEE_MODE_ENCRYPT) {
3884 			res = crypto_acipher_rsanopad_encrypt(o->attr, src_data,
3885 							      src_len, dst_data,
3886 							      &dlen);
3887 		} else if (cs->mode == TEE_MODE_DECRYPT) {
3888 			res = crypto_acipher_rsanopad_decrypt(o->attr, src_data,
3889 							      src_len, dst_data,
3890 							      &dlen);
3891 		} else {
3892 			/*
3893 			 * We will panic because "the mode is not compatible
3894 			 * with the function"
3895 			 */
3896 			res = TEE_ERROR_GENERIC;
3897 		}
3898 		break;
3899 
3900 	case TEE_ALG_SM2_PKE:
3901 		if (cs->mode == TEE_MODE_ENCRYPT) {
3902 			res = crypto_acipher_sm2_pke_encrypt(o->attr, src_data,
3903 							     src_len, dst_data,
3904 							     &dlen);
3905 		} else if (cs->mode == TEE_MODE_DECRYPT) {
3906 			res = crypto_acipher_sm2_pke_decrypt(o->attr, src_data,
3907 							     src_len, dst_data,
3908 							     &dlen);
3909 		} else {
3910 			res = TEE_ERROR_GENERIC;
3911 		}
3912 		break;
3913 
3914 	case TEE_ALG_RSAES_PKCS1_V1_5:
3915 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
3916 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
3917 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
3918 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
3919 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
3920 		for (n = 0; n < num_params; n++) {
3921 			if (params[n].attributeID == TEE_ATTR_RSA_OAEP_LABEL) {
3922 				label = params[n].content.ref.buffer;
3923 				label_len = params[n].content.ref.length;
3924 				break;
3925 			}
3926 		}
3927 
3928 		if (cs->mode == TEE_MODE_ENCRYPT) {
3929 			res = crypto_acipher_rsaes_encrypt(cs->algo, o->attr,
3930 							   label, label_len,
3931 							   src_data, src_len,
3932 							   dst_data, &dlen);
3933 		} else if (cs->mode == TEE_MODE_DECRYPT) {
3934 			res = crypto_acipher_rsaes_decrypt(
3935 					cs->algo, o->attr, label, label_len,
3936 					src_data, src_len, dst_data, &dlen);
3937 		} else {
3938 			res = TEE_ERROR_BAD_PARAMETERS;
3939 		}
3940 		break;
3941 
3942 #if defined(CFG_CRYPTO_RSASSA_NA1)
3943 	case TEE_ALG_RSASSA_PKCS1_V1_5:
3944 #endif
3945 	case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
3946 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
3947 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
3948 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
3949 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
3950 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
3951 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
3952 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
3953 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
3954 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
3955 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
3956 		if (cs->mode != TEE_MODE_SIGN) {
3957 			res = TEE_ERROR_BAD_PARAMETERS;
3958 			break;
3959 		}
3960 		salt_len = pkcs1_get_salt_len(params, num_params, src_len);
3961 		res = crypto_acipher_rsassa_sign(cs->algo, o->attr, salt_len,
3962 						 src_data, src_len, dst_data,
3963 						 &dlen);
3964 		break;
3965 
3966 	case TEE_ALG_DSA_SHA1:
3967 	case TEE_ALG_DSA_SHA224:
3968 	case TEE_ALG_DSA_SHA256:
3969 		res = crypto_acipher_dsa_sign(cs->algo, o->attr, src_data,
3970 					      src_len, dst_data, &dlen);
3971 		break;
3972 	case TEE_ALG_ECDSA_P192:
3973 	case TEE_ALG_ECDSA_P224:
3974 	case TEE_ALG_ECDSA_P256:
3975 	case TEE_ALG_ECDSA_P384:
3976 	case TEE_ALG_ECDSA_P521:
3977 	case TEE_ALG_SM2_DSA_SM3:
3978 		res = crypto_acipher_ecc_sign(cs->algo, o->attr, src_data,
3979 					      src_len, dst_data, &dlen);
3980 		break;
3981 	default:
3982 		res = TEE_ERROR_BAD_PARAMETERS;
3983 		break;
3984 	}
3985 
3986 out:
3987 	free_wipe(params);
3988 
3989 	if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) {
3990 		TEE_Result res2 = put_user_u64(dst_len, dlen);
3991 
3992 		if (res2 != TEE_SUCCESS)
3993 			return res2;
3994 	}
3995 
3996 	return res;
3997 }
3998 
3999 TEE_Result syscall_asymm_verify(unsigned long state,
4000 			const struct utee_attribute *usr_params,
4001 			size_t num_params, const void *data, size_t data_len,
4002 			const void *sig, size_t sig_len)
4003 {
4004 	struct ts_session *sess = ts_get_current_session();
4005 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
4006 	struct tee_cryp_state *cs = NULL;
4007 	TEE_Result res = TEE_SUCCESS;
4008 	TEE_Attribute *params = NULL;
4009 	struct tee_obj *o = NULL;
4010 	size_t hash_size = 0;
4011 	uint32_t hash_algo = 0;
4012 	int salt_len = 0;
4013 	size_t alloc_size = 0;
4014 
4015 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
4016 	if (res != TEE_SUCCESS)
4017 		return res;
4018 
4019 	if (cs->mode != TEE_MODE_VERIFY)
4020 		return TEE_ERROR_BAD_PARAMETERS;
4021 
4022 	data = memtag_strip_tag_const(data);
4023 	sig = memtag_strip_tag_const(sig);
4024 
4025 	res = vm_check_access_rights(&utc->uctx,
4026 				     TEE_MEMORY_ACCESS_READ |
4027 				     TEE_MEMORY_ACCESS_ANY_OWNER,
4028 				     (uaddr_t)data, data_len);
4029 	if (res != TEE_SUCCESS)
4030 		return res;
4031 
4032 	res = vm_check_access_rights(&utc->uctx,
4033 				     TEE_MEMORY_ACCESS_READ |
4034 				     TEE_MEMORY_ACCESS_ANY_OWNER,
4035 				     (uaddr_t)sig, sig_len);
4036 	if (res != TEE_SUCCESS)
4037 		return res;
4038 
4039 	if (MUL_OVERFLOW(sizeof(TEE_Attribute), num_params, &alloc_size))
4040 		return TEE_ERROR_OVERFLOW;
4041 
4042 	params = malloc(alloc_size);
4043 	if (!params)
4044 		return TEE_ERROR_OUT_OF_MEMORY;
4045 	res = copy_in_attrs(utc, usr_params, num_params, params);
4046 	if (res != TEE_SUCCESS)
4047 		goto out;
4048 
4049 	res = tee_obj_get(utc, cs->key1, &o);
4050 	if (res != TEE_SUCCESS)
4051 		goto out;
4052 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
4053 		res = TEE_ERROR_BAD_PARAMETERS;
4054 		goto out;
4055 	}
4056 
4057 	switch (TEE_ALG_GET_MAIN_ALG(cs->algo)) {
4058 	case TEE_MAIN_ALGO_RSA:
4059 		if (cs->algo != TEE_ALG_RSASSA_PKCS1_V1_5) {
4060 			hash_algo = TEE_DIGEST_HASH_TO_ALGO(cs->algo);
4061 			res = tee_alg_get_digest_size(hash_algo, &hash_size);
4062 			if (res != TEE_SUCCESS)
4063 				break;
4064 			if (data_len != hash_size) {
4065 				res = TEE_ERROR_BAD_PARAMETERS;
4066 				break;
4067 			}
4068 			salt_len = pkcs1_get_salt_len(params, num_params,
4069 						      hash_size);
4070 		}
4071 		res = crypto_acipher_rsassa_verify(cs->algo, o->attr, salt_len,
4072 						   data, data_len, sig,
4073 						   sig_len);
4074 		break;
4075 
4076 	case TEE_MAIN_ALGO_DSA:
4077 		hash_algo = TEE_DIGEST_HASH_TO_ALGO(cs->algo);
4078 		res = tee_alg_get_digest_size(hash_algo, &hash_size);
4079 		if (res != TEE_SUCCESS)
4080 			break;
4081 
4082 		if (data_len != hash_size) {
4083 			struct dsa_public_key *key = o->attr;
4084 
4085 			/*
4086 			 * Depending on the DSA algorithm (NIST), the
4087 			 * digital signature output size may be truncated
4088 			 * to the size of a key pair (Q prime size). Q
4089 			 * prime size must be less or equal than the hash
4090 			 * output length of the hash algorithm involved.
4091 			 *
4092 			 * We're checking here in order to be able to
4093 			 * return this particular error code, which will
4094 			 * cause TEE_AsymmetricVerifyDigest() to panic as
4095 			 * required by GP. crypto_acipher_dsa_verify() is
4096 			 * implemented in the glue layer of the crypto
4097 			 * library and it might be a bit harder to catch
4098 			 * this particular case there or lead to duplicated
4099 			 * code in different crypto glue layers.
4100 			 *
4101 			 * The GP spec says that we SHOULD panic if
4102 			 * data_len != hash_size, but that would break a
4103 			 * few of the DSA tests in xtest where the
4104 			 * hash_size is larger than possible data_len. So
4105 			 * the compromise is in case data_len != hash_size
4106 			 * check that it's not smaller than what makes
4107 			 * sense.
4108 			 */
4109 			if (data_len != crypto_bignum_num_bytes(key->q)) {
4110 				res = TEE_ERROR_BAD_PARAMETERS;
4111 				break;
4112 			}
4113 		}
4114 		res = crypto_acipher_dsa_verify(cs->algo, o->attr, data,
4115 						data_len, sig, sig_len);
4116 		break;
4117 
4118 	case TEE_MAIN_ALGO_ECDSA:
4119 	case TEE_MAIN_ALGO_SM2_DSA_SM3:
4120 		res = crypto_acipher_ecc_verify(cs->algo, o->attr, data,
4121 						data_len, sig, sig_len);
4122 		break;
4123 
4124 	default:
4125 		res = TEE_ERROR_NOT_SUPPORTED;
4126 	}
4127 
4128 out:
4129 	free_wipe(params);
4130 	return res;
4131 }
4132