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