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