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