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