xref: /optee_os/core/tee/tee_svc_cryp.c (revision 338b123ee66ca6694372564000226549c99806b1)
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 				obj_size += (attrs[n].content.ref.length * 8);
1580 			}
1581 		}
1582 	}
1583 
1584 	/*
1585 	 * We have to do it like this because the parity bits aren't counted
1586 	 * when telling the size of the key in bits.
1587 	 */
1588 	if (o->info.objectType == TEE_TYPE_DES ||
1589 	    o->info.objectType == TEE_TYPE_DES3)
1590 		obj_size -= obj_size / 8; /* Exclude parity in size of key */
1591 
1592 	o->have_attrs = have_attrs;
1593 	o->info.keySize = obj_size;
1594 
1595 	return TEE_SUCCESS;
1596 }
1597 
1598 TEE_Result syscall_cryp_obj_populate(unsigned long obj,
1599 			struct utee_attribute *usr_attrs,
1600 			unsigned long attr_count)
1601 {
1602 	TEE_Result res;
1603 	struct tee_ta_session *sess;
1604 	struct tee_obj *o;
1605 	const struct tee_cryp_obj_type_props *type_props;
1606 	TEE_Attribute *attrs = NULL;
1607 
1608 	res = tee_ta_get_current_session(&sess);
1609 	if (res != TEE_SUCCESS)
1610 		return res;
1611 
1612 	res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o);
1613 	if (res != TEE_SUCCESS)
1614 		return res;
1615 
1616 	/* Must be a transient object */
1617 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
1618 		return TEE_ERROR_BAD_PARAMETERS;
1619 
1620 	/* Must not be initialized already */
1621 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1622 		return TEE_ERROR_BAD_PARAMETERS;
1623 
1624 	type_props = tee_svc_find_type_props(o->info.objectType);
1625 	if (!type_props)
1626 		return TEE_ERROR_NOT_IMPLEMENTED;
1627 
1628 	size_t alloc_size = 0;
1629 
1630 	if (MUL_OVERFLOW(sizeof(TEE_Attribute), attr_count, &alloc_size))
1631 		return TEE_ERROR_OVERFLOW;
1632 
1633 	attrs = malloc(alloc_size);
1634 	if (!attrs)
1635 		return TEE_ERROR_OUT_OF_MEMORY;
1636 
1637 	res = copy_in_attrs(to_user_ta_ctx(sess->ctx), usr_attrs, attr_count,
1638 			    attrs);
1639 	if (res != TEE_SUCCESS)
1640 		goto out;
1641 
1642 	res = tee_svc_cryp_check_attr(ATTR_USAGE_POPULATE, type_props,
1643 				      attrs, attr_count);
1644 	if (res != TEE_SUCCESS)
1645 		goto out;
1646 
1647 	res = tee_svc_cryp_obj_populate_type(o, type_props, attrs, attr_count);
1648 	if (res == TEE_SUCCESS)
1649 		o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
1650 
1651 out:
1652 	free_wipe(attrs);
1653 	return res;
1654 }
1655 
1656 TEE_Result syscall_cryp_obj_copy(unsigned long dst, unsigned long src)
1657 {
1658 	TEE_Result res;
1659 	struct tee_ta_session *sess;
1660 	struct tee_obj *dst_o;
1661 	struct tee_obj *src_o;
1662 
1663 	res = tee_ta_get_current_session(&sess);
1664 	if (res != TEE_SUCCESS)
1665 		return res;
1666 
1667 	res = tee_obj_get(to_user_ta_ctx(sess->ctx),
1668 			  uref_to_vaddr(dst), &dst_o);
1669 	if (res != TEE_SUCCESS)
1670 		return res;
1671 
1672 	res = tee_obj_get(to_user_ta_ctx(sess->ctx),
1673 			  uref_to_vaddr(src), &src_o);
1674 	if (res != TEE_SUCCESS)
1675 		return res;
1676 
1677 	if ((src_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1678 		return TEE_ERROR_BAD_PARAMETERS;
1679 	if ((dst_o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
1680 		return TEE_ERROR_BAD_PARAMETERS;
1681 	if ((dst_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1682 		return TEE_ERROR_BAD_PARAMETERS;
1683 
1684 	res = tee_obj_attr_copy_from(dst_o, src_o);
1685 	if (res != TEE_SUCCESS)
1686 		return res;
1687 
1688 	dst_o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
1689 	dst_o->info.keySize = src_o->info.keySize;
1690 	dst_o->info.objectUsage = src_o->info.objectUsage;
1691 	return TEE_SUCCESS;
1692 }
1693 
1694 static TEE_Result check_pub_rsa_key(struct bignum *e)
1695 {
1696 	size_t n = crypto_bignum_num_bytes(e);
1697 	uint8_t bin_key[256 / 8] = { 0 };
1698 
1699 	/*
1700 	 * NIST SP800-56B requires public RSA key to be an odd integer in
1701 	 * the range 65537 <= e < 2^256.
1702 	 */
1703 
1704 	if (n > sizeof(bin_key) || n < 3)
1705 		return TEE_ERROR_BAD_PARAMETERS;
1706 
1707 	crypto_bignum_bn2bin(e, bin_key);
1708 
1709 	if (!(bin_key[0] & 1)) /* key must be odd */
1710 		return TEE_ERROR_BAD_PARAMETERS;
1711 
1712 	if (n == 3) {
1713 		uint32_t key = 0;
1714 
1715 		for (n = 0; n < 3; n++) {
1716 			key <<= 8;
1717 			key |= bin_key[n];
1718 		}
1719 
1720 		if (key < 65537)
1721 			return TEE_ERROR_BAD_PARAMETERS;
1722 	}
1723 
1724 	/* key is larger than 65537 */
1725 	return TEE_SUCCESS;
1726 }
1727 
1728 static TEE_Result tee_svc_obj_generate_key_rsa(
1729 	struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props,
1730 	uint32_t key_size,
1731 	const TEE_Attribute *params, uint32_t param_count)
1732 {
1733 	TEE_Result res = TEE_SUCCESS;
1734 	struct rsa_keypair *key = o->attr;
1735 	uint32_t e = TEE_U32_TO_BIG_ENDIAN(65537);
1736 
1737 	/* Copy the present attributes into the obj before starting */
1738 	res = tee_svc_cryp_obj_populate_type(o, type_props, params,
1739 					     param_count);
1740 	if (res != TEE_SUCCESS)
1741 		return res;
1742 	if (get_attribute(o, type_props, TEE_ATTR_RSA_PUBLIC_EXPONENT)) {
1743 		res = check_pub_rsa_key(key->e);
1744 		if (res)
1745 			return res;
1746 	} else {
1747 		crypto_bignum_bin2bn((const uint8_t *)&e, sizeof(e), key->e);
1748 	}
1749 	res = crypto_acipher_gen_rsa_key(key, key_size);
1750 	if (res != TEE_SUCCESS)
1751 		return res;
1752 
1753 	/* Set bits for all known attributes for this object type */
1754 	o->have_attrs = (1 << type_props->num_type_attrs) - 1;
1755 
1756 	return TEE_SUCCESS;
1757 }
1758 
1759 static TEE_Result tee_svc_obj_generate_key_dsa(
1760 	struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props,
1761 	uint32_t key_size, const TEE_Attribute *params, uint32_t param_count)
1762 {
1763 	TEE_Result res;
1764 
1765 	/* Copy the present attributes into the obj before starting */
1766 	res = tee_svc_cryp_obj_populate_type(o, type_props, params,
1767 					     param_count);
1768 	if (res != TEE_SUCCESS)
1769 		return res;
1770 
1771 	res = crypto_acipher_gen_dsa_key(o->attr, key_size);
1772 	if (res != TEE_SUCCESS)
1773 		return res;
1774 
1775 	/* Set bits for all known attributes for this object type */
1776 	o->have_attrs = (1 << type_props->num_type_attrs) - 1;
1777 
1778 	return TEE_SUCCESS;
1779 }
1780 
1781 static TEE_Result tee_svc_obj_generate_key_dh(
1782 	struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props,
1783 	uint32_t key_size, const TEE_Attribute *params, uint32_t param_count)
1784 {
1785 	TEE_Result res;
1786 	struct dh_keypair *tee_dh_key;
1787 	struct bignum *dh_q = NULL;
1788 	uint32_t dh_xbits = 0;
1789 
1790 	/* Copy the present attributes into the obj before starting */
1791 	res = tee_svc_cryp_obj_populate_type(o, type_props, params,
1792 					     param_count);
1793 	if (res != TEE_SUCCESS)
1794 		return res;
1795 
1796 	tee_dh_key = (struct dh_keypair *)o->attr;
1797 
1798 	if (get_attribute(o, type_props, TEE_ATTR_DH_SUBPRIME))
1799 		dh_q = tee_dh_key->q;
1800 	if (get_attribute(o, type_props, TEE_ATTR_DH_X_BITS))
1801 		dh_xbits = tee_dh_key->xbits;
1802 	res = crypto_acipher_gen_dh_key(tee_dh_key, dh_q, dh_xbits, key_size);
1803 	if (res != TEE_SUCCESS)
1804 		return res;
1805 
1806 	/* Set bits for the generated public and private key */
1807 	set_attribute(o, type_props, TEE_ATTR_DH_PUBLIC_VALUE);
1808 	set_attribute(o, type_props, TEE_ATTR_DH_PRIVATE_VALUE);
1809 	set_attribute(o, type_props, TEE_ATTR_DH_X_BITS);
1810 	return TEE_SUCCESS;
1811 }
1812 
1813 static TEE_Result tee_svc_obj_generate_key_ecc(
1814 	struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props,
1815 	uint32_t key_size, const TEE_Attribute *params, uint32_t param_count)
1816 {
1817 	TEE_Result res;
1818 	struct ecc_keypair *tee_ecc_key;
1819 
1820 	/* Copy the present attributes into the obj before starting */
1821 	res = tee_svc_cryp_obj_populate_type(o, type_props, params,
1822 					     param_count);
1823 	if (res != TEE_SUCCESS)
1824 		return res;
1825 
1826 	tee_ecc_key = (struct ecc_keypair *)o->attr;
1827 
1828 	res = crypto_acipher_gen_ecc_key(tee_ecc_key, key_size);
1829 	if (res != TEE_SUCCESS)
1830 		return res;
1831 
1832 	/* Set bits for the generated public and private key */
1833 	set_attribute(o, type_props, TEE_ATTR_ECC_PRIVATE_VALUE);
1834 	set_attribute(o, type_props, TEE_ATTR_ECC_PUBLIC_VALUE_X);
1835 	set_attribute(o, type_props, TEE_ATTR_ECC_PUBLIC_VALUE_Y);
1836 	set_attribute(o, type_props, TEE_ATTR_ECC_CURVE);
1837 	return TEE_SUCCESS;
1838 }
1839 
1840 TEE_Result syscall_obj_generate_key(unsigned long obj, unsigned long key_size,
1841 			const struct utee_attribute *usr_params,
1842 			unsigned long param_count)
1843 {
1844 	TEE_Result res;
1845 	struct tee_ta_session *sess;
1846 	const struct tee_cryp_obj_type_props *type_props;
1847 	struct tee_obj *o;
1848 	struct tee_cryp_obj_secret *key;
1849 	size_t byte_size;
1850 	TEE_Attribute *params = NULL;
1851 
1852 	res = tee_ta_get_current_session(&sess);
1853 	if (res != TEE_SUCCESS)
1854 		return res;
1855 
1856 	res = tee_obj_get(to_user_ta_ctx(sess->ctx), uref_to_vaddr(obj), &o);
1857 	if (res != TEE_SUCCESS)
1858 		return res;
1859 
1860 	/* Must be a transient object */
1861 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
1862 		return TEE_ERROR_BAD_STATE;
1863 
1864 	/* Must not be initialized already */
1865 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1866 		return TEE_ERROR_BAD_STATE;
1867 
1868 	/* Find description of object */
1869 	type_props = tee_svc_find_type_props(o->info.objectType);
1870 	if (!type_props)
1871 		return TEE_ERROR_NOT_SUPPORTED;
1872 
1873 	/* Check that maxKeySize follows restrictions */
1874 	if (key_size % type_props->quanta != 0)
1875 		return TEE_ERROR_NOT_SUPPORTED;
1876 	if (key_size < type_props->min_size)
1877 		return TEE_ERROR_NOT_SUPPORTED;
1878 	if (key_size > type_props->max_size)
1879 		return TEE_ERROR_NOT_SUPPORTED;
1880 
1881 	size_t alloc_size = 0;
1882 
1883 	if (MUL_OVERFLOW(sizeof(TEE_Attribute), param_count, &alloc_size))
1884 		return TEE_ERROR_OVERFLOW;
1885 
1886 	params = malloc(alloc_size);
1887 	if (!params)
1888 		return TEE_ERROR_OUT_OF_MEMORY;
1889 	res = copy_in_attrs(to_user_ta_ctx(sess->ctx), usr_params, param_count,
1890 			    params);
1891 	if (res != TEE_SUCCESS)
1892 		goto out;
1893 
1894 	res = tee_svc_cryp_check_attr(ATTR_USAGE_GENERATE_KEY, type_props,
1895 				      params, param_count);
1896 	if (res != TEE_SUCCESS)
1897 		goto out;
1898 
1899 	switch (o->info.objectType) {
1900 	case TEE_TYPE_AES:
1901 	case TEE_TYPE_DES:
1902 	case TEE_TYPE_DES3:
1903 	case TEE_TYPE_SM4:
1904 	case TEE_TYPE_HMAC_MD5:
1905 	case TEE_TYPE_HMAC_SHA1:
1906 	case TEE_TYPE_HMAC_SHA224:
1907 	case TEE_TYPE_HMAC_SHA256:
1908 	case TEE_TYPE_HMAC_SHA384:
1909 	case TEE_TYPE_HMAC_SHA512:
1910 	case TEE_TYPE_HMAC_SM3:
1911 	case TEE_TYPE_GENERIC_SECRET:
1912 		byte_size = key_size / 8;
1913 
1914 		/*
1915 		 * We have to do it like this because the parity bits aren't
1916 		 * counted when telling the size of the key in bits.
1917 		 */
1918 		if (o->info.objectType == TEE_TYPE_DES ||
1919 		    o->info.objectType == TEE_TYPE_DES3) {
1920 			byte_size = (key_size + key_size / 7) / 8;
1921 		}
1922 
1923 		key = (struct tee_cryp_obj_secret *)o->attr;
1924 		if (byte_size > key->alloc_size) {
1925 			res = TEE_ERROR_EXCESS_DATA;
1926 			goto out;
1927 		}
1928 
1929 		res = crypto_rng_read((void *)(key + 1), byte_size);
1930 		if (res != TEE_SUCCESS)
1931 			goto out;
1932 
1933 		key->key_size = byte_size;
1934 
1935 		/* Set bits for all known attributes for this object type */
1936 		o->have_attrs = (1 << type_props->num_type_attrs) - 1;
1937 
1938 		break;
1939 
1940 	case TEE_TYPE_RSA_KEYPAIR:
1941 		res = tee_svc_obj_generate_key_rsa(o, type_props, key_size,
1942 						   params, param_count);
1943 		if (res != TEE_SUCCESS)
1944 			goto out;
1945 		break;
1946 
1947 	case TEE_TYPE_DSA_KEYPAIR:
1948 		res = tee_svc_obj_generate_key_dsa(o, type_props, key_size,
1949 						   params, param_count);
1950 		if (res != TEE_SUCCESS)
1951 			goto out;
1952 		break;
1953 
1954 	case TEE_TYPE_DH_KEYPAIR:
1955 		res = tee_svc_obj_generate_key_dh(o, type_props, key_size,
1956 						  params, param_count);
1957 		if (res != TEE_SUCCESS)
1958 			goto out;
1959 		break;
1960 
1961 	case TEE_TYPE_ECDSA_KEYPAIR:
1962 	case TEE_TYPE_ECDH_KEYPAIR:
1963 	case TEE_TYPE_SM2_PKE_KEYPAIR:
1964 		res = tee_svc_obj_generate_key_ecc(o, type_props, key_size,
1965 						  params, param_count);
1966 		if (res != TEE_SUCCESS)
1967 			goto out;
1968 		break;
1969 
1970 	default:
1971 		res = TEE_ERROR_BAD_FORMAT;
1972 	}
1973 
1974 out:
1975 	free_wipe(params);
1976 	if (res == TEE_SUCCESS) {
1977 		o->info.keySize = key_size;
1978 		o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
1979 	}
1980 	return res;
1981 }
1982 
1983 static TEE_Result tee_svc_cryp_get_state(struct tee_ta_session *sess,
1984 					 uint32_t state_id,
1985 					 struct tee_cryp_state **state)
1986 {
1987 	struct tee_cryp_state *s;
1988 	struct user_ta_ctx *utc = to_user_ta_ctx(sess->ctx);
1989 
1990 	TAILQ_FOREACH(s, &utc->cryp_states, link) {
1991 		if (state_id == (vaddr_t)s) {
1992 			*state = s;
1993 			return TEE_SUCCESS;
1994 		}
1995 	}
1996 	return TEE_ERROR_BAD_PARAMETERS;
1997 }
1998 
1999 static void cryp_state_free(struct user_ta_ctx *utc, struct tee_cryp_state *cs)
2000 {
2001 	struct tee_obj *o;
2002 
2003 	if (tee_obj_get(utc, cs->key1, &o) == TEE_SUCCESS)
2004 		tee_obj_close(utc, o);
2005 	if (tee_obj_get(utc, cs->key2, &o) == TEE_SUCCESS)
2006 		tee_obj_close(utc, o);
2007 
2008 	TAILQ_REMOVE(&utc->cryp_states, cs, link);
2009 	if (cs->ctx_finalize != NULL)
2010 		cs->ctx_finalize(cs->ctx);
2011 
2012 	switch (TEE_ALG_GET_CLASS(cs->algo)) {
2013 	case TEE_OPERATION_CIPHER:
2014 		crypto_cipher_free_ctx(cs->ctx);
2015 		break;
2016 	case TEE_OPERATION_AE:
2017 		crypto_authenc_free_ctx(cs->ctx);
2018 		break;
2019 	case TEE_OPERATION_DIGEST:
2020 		crypto_hash_free_ctx(cs->ctx);
2021 		break;
2022 	case TEE_OPERATION_MAC:
2023 		crypto_mac_free_ctx(cs->ctx);
2024 		break;
2025 	default:
2026 		assert(!cs->ctx);
2027 	}
2028 
2029 	free(cs);
2030 }
2031 
2032 static TEE_Result tee_svc_cryp_check_key_type(const struct tee_obj *o,
2033 					      uint32_t algo,
2034 					      TEE_OperationMode mode)
2035 {
2036 	uint32_t req_key_type;
2037 	uint32_t req_key_type2 = 0;
2038 
2039 	switch (TEE_ALG_GET_MAIN_ALG(algo)) {
2040 	case TEE_MAIN_ALGO_MD5:
2041 		req_key_type = TEE_TYPE_HMAC_MD5;
2042 		break;
2043 	case TEE_MAIN_ALGO_SHA1:
2044 		req_key_type = TEE_TYPE_HMAC_SHA1;
2045 		break;
2046 	case TEE_MAIN_ALGO_SHA224:
2047 		req_key_type = TEE_TYPE_HMAC_SHA224;
2048 		break;
2049 	case TEE_MAIN_ALGO_SHA256:
2050 		req_key_type = TEE_TYPE_HMAC_SHA256;
2051 		break;
2052 	case TEE_MAIN_ALGO_SHA384:
2053 		req_key_type = TEE_TYPE_HMAC_SHA384;
2054 		break;
2055 	case TEE_MAIN_ALGO_SHA512:
2056 		req_key_type = TEE_TYPE_HMAC_SHA512;
2057 		break;
2058 	case TEE_MAIN_ALGO_SM3:
2059 		req_key_type = TEE_TYPE_HMAC_SM3;
2060 		break;
2061 	case TEE_MAIN_ALGO_AES:
2062 		req_key_type = TEE_TYPE_AES;
2063 		break;
2064 	case TEE_MAIN_ALGO_DES:
2065 		req_key_type = TEE_TYPE_DES;
2066 		break;
2067 	case TEE_MAIN_ALGO_DES3:
2068 		req_key_type = TEE_TYPE_DES3;
2069 		break;
2070 	case TEE_MAIN_ALGO_SM4:
2071 		req_key_type = TEE_TYPE_SM4;
2072 		break;
2073 	case TEE_MAIN_ALGO_RSA:
2074 		req_key_type = TEE_TYPE_RSA_KEYPAIR;
2075 		if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY)
2076 			req_key_type2 = TEE_TYPE_RSA_PUBLIC_KEY;
2077 		break;
2078 	case TEE_MAIN_ALGO_DSA:
2079 		req_key_type = TEE_TYPE_DSA_KEYPAIR;
2080 		if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY)
2081 			req_key_type2 = TEE_TYPE_DSA_PUBLIC_KEY;
2082 		break;
2083 	case TEE_MAIN_ALGO_DH:
2084 		req_key_type = TEE_TYPE_DH_KEYPAIR;
2085 		break;
2086 	case TEE_MAIN_ALGO_ECDSA:
2087 		req_key_type = TEE_TYPE_ECDSA_KEYPAIR;
2088 		if (mode == TEE_MODE_VERIFY)
2089 			req_key_type2 = TEE_TYPE_ECDSA_PUBLIC_KEY;
2090 		break;
2091 	case TEE_MAIN_ALGO_ECDH:
2092 		req_key_type = TEE_TYPE_ECDH_KEYPAIR;
2093 		break;
2094 	case TEE_MAIN_ALGO_SM2_PKE:
2095 		if (mode == TEE_MODE_ENCRYPT)
2096 			req_key_type = TEE_TYPE_SM2_PKE_PUBLIC_KEY;
2097 		else
2098 			req_key_type = TEE_TYPE_SM2_PKE_KEYPAIR;
2099 		break;
2100 	case TEE_MAIN_ALGO_SM2_DSA_SM3:
2101 		if (mode == TEE_MODE_VERIFY)
2102 			req_key_type = TEE_TYPE_SM2_DSA_PUBLIC_KEY;
2103 		else
2104 			req_key_type = TEE_TYPE_SM2_DSA_KEYPAIR;
2105 		break;
2106 #if defined(CFG_CRYPTO_SM2_KEP)
2107 	case TEE_MAIN_ALGO_SM2_KEP:
2108 		req_key_type = TEE_TYPE_SM2_KEP_KEYPAIR;
2109 		req_key_type2 = TEE_TYPE_SM2_KEP_PUBLIC_KEY;
2110 		break;
2111 #endif
2112 #if defined(CFG_CRYPTO_HKDF)
2113 	case TEE_MAIN_ALGO_HKDF:
2114 		req_key_type = TEE_TYPE_HKDF_IKM;
2115 		break;
2116 #endif
2117 #if defined(CFG_CRYPTO_CONCAT_KDF)
2118 	case TEE_MAIN_ALGO_CONCAT_KDF:
2119 		req_key_type = TEE_TYPE_CONCAT_KDF_Z;
2120 		break;
2121 #endif
2122 #if defined(CFG_CRYPTO_PBKDF2)
2123 	case TEE_MAIN_ALGO_PBKDF2:
2124 		req_key_type = TEE_TYPE_PBKDF2_PASSWORD;
2125 		break;
2126 #endif
2127 	default:
2128 		return TEE_ERROR_BAD_PARAMETERS;
2129 	}
2130 
2131 	if (req_key_type != o->info.objectType &&
2132 	    req_key_type2 != o->info.objectType)
2133 		return TEE_ERROR_BAD_PARAMETERS;
2134 	return TEE_SUCCESS;
2135 }
2136 
2137 TEE_Result syscall_cryp_state_alloc(unsigned long algo, unsigned long mode,
2138 			unsigned long key1, unsigned long key2,
2139 			uint32_t *state)
2140 {
2141 	TEE_Result res;
2142 	struct tee_cryp_state *cs;
2143 	struct tee_ta_session *sess;
2144 	struct tee_obj *o1 = NULL;
2145 	struct tee_obj *o2 = NULL;
2146 	struct user_ta_ctx *utc;
2147 
2148 	res = tee_ta_get_current_session(&sess);
2149 	if (res != TEE_SUCCESS)
2150 		return res;
2151 	utc = to_user_ta_ctx(sess->ctx);
2152 
2153 	if (key1 != 0) {
2154 		res = tee_obj_get(utc, uref_to_vaddr(key1), &o1);
2155 		if (res != TEE_SUCCESS)
2156 			return res;
2157 		if (o1->busy)
2158 			return TEE_ERROR_BAD_PARAMETERS;
2159 		res = tee_svc_cryp_check_key_type(o1, algo, mode);
2160 		if (res != TEE_SUCCESS)
2161 			return res;
2162 	}
2163 	if (key2 != 0) {
2164 		res = tee_obj_get(utc, uref_to_vaddr(key2), &o2);
2165 		if (res != TEE_SUCCESS)
2166 			return res;
2167 		if (o2->busy)
2168 			return TEE_ERROR_BAD_PARAMETERS;
2169 		res = tee_svc_cryp_check_key_type(o2, algo, mode);
2170 		if (res != TEE_SUCCESS)
2171 			return res;
2172 	}
2173 
2174 	cs = calloc(1, sizeof(struct tee_cryp_state));
2175 	if (!cs)
2176 		return TEE_ERROR_OUT_OF_MEMORY;
2177 	TAILQ_INSERT_TAIL(&utc->cryp_states, cs, link);
2178 	cs->algo = algo;
2179 	cs->mode = mode;
2180 	cs->state = CRYP_STATE_UNINITIALIZED;
2181 
2182 	switch (TEE_ALG_GET_CLASS(algo)) {
2183 	case TEE_OPERATION_CIPHER:
2184 		if ((algo == TEE_ALG_AES_XTS && (key1 == 0 || key2 == 0)) ||
2185 		    (algo != TEE_ALG_AES_XTS && (key1 == 0 || key2 != 0))) {
2186 			res = TEE_ERROR_BAD_PARAMETERS;
2187 		} else {
2188 			res = crypto_cipher_alloc_ctx(&cs->ctx, algo);
2189 			if (res != TEE_SUCCESS)
2190 				break;
2191 		}
2192 		break;
2193 	case TEE_OPERATION_AE:
2194 		if (key1 == 0 || key2 != 0) {
2195 			res = TEE_ERROR_BAD_PARAMETERS;
2196 		} else {
2197 			res = crypto_authenc_alloc_ctx(&cs->ctx, algo);
2198 			if (res != TEE_SUCCESS)
2199 				break;
2200 		}
2201 		break;
2202 	case TEE_OPERATION_MAC:
2203 		if (key1 == 0 || key2 != 0) {
2204 			res = TEE_ERROR_BAD_PARAMETERS;
2205 		} else {
2206 			res = crypto_mac_alloc_ctx(&cs->ctx, algo);
2207 			if (res != TEE_SUCCESS)
2208 				break;
2209 		}
2210 		break;
2211 	case TEE_OPERATION_DIGEST:
2212 		if (key1 != 0 || key2 != 0) {
2213 			res = TEE_ERROR_BAD_PARAMETERS;
2214 		} else {
2215 			res = crypto_hash_alloc_ctx(&cs->ctx, algo);
2216 			if (res != TEE_SUCCESS)
2217 				break;
2218 		}
2219 		break;
2220 	case TEE_OPERATION_ASYMMETRIC_CIPHER:
2221 	case TEE_OPERATION_ASYMMETRIC_SIGNATURE:
2222 		if (algo == TEE_ALG_RSASSA_PKCS1_V1_5 &&
2223 		    !IS_ENABLED(CFG_CRYPTO_RSASSA_NA1)) {
2224 			res = TEE_ERROR_NOT_SUPPORTED;
2225 			break;
2226 		}
2227 		if (key1 == 0 || key2 != 0)
2228 			res = TEE_ERROR_BAD_PARAMETERS;
2229 		break;
2230 	case TEE_OPERATION_KEY_DERIVATION:
2231 		if (algo == TEE_ALG_SM2_KEP) {
2232 			if (key1 == 0 || key2 == 0)
2233 				res = TEE_ERROR_BAD_PARAMETERS;
2234 		} else {
2235 			if (key1 == 0 || key2 != 0)
2236 				res = TEE_ERROR_BAD_PARAMETERS;
2237 		}
2238 		break;
2239 	default:
2240 		res = TEE_ERROR_NOT_SUPPORTED;
2241 		break;
2242 	}
2243 	if (res != TEE_SUCCESS)
2244 		goto out;
2245 
2246 	res = copy_kaddr_to_uref(state, cs);
2247 	if (res != TEE_SUCCESS)
2248 		goto out;
2249 
2250 	/* Register keys */
2251 	if (o1 != NULL) {
2252 		o1->busy = true;
2253 		cs->key1 = (vaddr_t)o1;
2254 	}
2255 	if (o2 != NULL) {
2256 		o2->busy = true;
2257 		cs->key2 = (vaddr_t)o2;
2258 	}
2259 
2260 out:
2261 	if (res != TEE_SUCCESS)
2262 		cryp_state_free(utc, cs);
2263 	return res;
2264 }
2265 
2266 TEE_Result syscall_cryp_state_copy(unsigned long dst, unsigned long src)
2267 {
2268 	TEE_Result res;
2269 	struct tee_cryp_state *cs_dst;
2270 	struct tee_cryp_state *cs_src;
2271 	struct tee_ta_session *sess;
2272 
2273 	res = tee_ta_get_current_session(&sess);
2274 	if (res != TEE_SUCCESS)
2275 		return res;
2276 
2277 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(dst), &cs_dst);
2278 	if (res != TEE_SUCCESS)
2279 		return res;
2280 
2281 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(src), &cs_src);
2282 	if (res != TEE_SUCCESS)
2283 		return res;
2284 	if (cs_dst->algo != cs_src->algo || cs_dst->mode != cs_src->mode)
2285 		return TEE_ERROR_BAD_PARAMETERS;
2286 
2287 	switch (TEE_ALG_GET_CLASS(cs_src->algo)) {
2288 	case TEE_OPERATION_CIPHER:
2289 		crypto_cipher_copy_state(cs_dst->ctx, cs_src->ctx);
2290 		break;
2291 	case TEE_OPERATION_AE:
2292 		crypto_authenc_copy_state(cs_dst->ctx, cs_src->ctx);
2293 		break;
2294 	case TEE_OPERATION_DIGEST:
2295 		crypto_hash_copy_state(cs_dst->ctx, cs_src->ctx);
2296 		break;
2297 	case TEE_OPERATION_MAC:
2298 		crypto_mac_copy_state(cs_dst->ctx, cs_src->ctx);
2299 		break;
2300 	default:
2301 		return TEE_ERROR_BAD_STATE;
2302 	}
2303 
2304 	cs_dst->state = cs_src->state;
2305 
2306 	return TEE_SUCCESS;
2307 }
2308 
2309 void tee_svc_cryp_free_states(struct user_ta_ctx *utc)
2310 {
2311 	struct tee_cryp_state_head *states = &utc->cryp_states;
2312 
2313 	while (!TAILQ_EMPTY(states))
2314 		cryp_state_free(utc, TAILQ_FIRST(states));
2315 }
2316 
2317 TEE_Result syscall_cryp_state_free(unsigned long state)
2318 {
2319 	TEE_Result res;
2320 	struct tee_cryp_state *cs;
2321 	struct tee_ta_session *sess;
2322 
2323 	res = tee_ta_get_current_session(&sess);
2324 	if (res != TEE_SUCCESS)
2325 		return res;
2326 
2327 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
2328 	if (res != TEE_SUCCESS)
2329 		return res;
2330 	cryp_state_free(to_user_ta_ctx(sess->ctx), cs);
2331 	return TEE_SUCCESS;
2332 }
2333 
2334 TEE_Result syscall_hash_init(unsigned long state,
2335 			     const void *iv __maybe_unused,
2336 			     size_t iv_len __maybe_unused)
2337 {
2338 	TEE_Result res;
2339 	struct tee_cryp_state *cs;
2340 	struct tee_ta_session *sess;
2341 
2342 	res = tee_ta_get_current_session(&sess);
2343 	if (res != TEE_SUCCESS)
2344 		return res;
2345 
2346 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
2347 	if (res != TEE_SUCCESS)
2348 		return res;
2349 
2350 	switch (TEE_ALG_GET_CLASS(cs->algo)) {
2351 	case TEE_OPERATION_DIGEST:
2352 		res = crypto_hash_init(cs->ctx);
2353 		if (res != TEE_SUCCESS)
2354 			return res;
2355 		break;
2356 	case TEE_OPERATION_MAC:
2357 		{
2358 			struct tee_obj *o;
2359 			struct tee_cryp_obj_secret *key;
2360 
2361 			res = tee_obj_get(to_user_ta_ctx(sess->ctx),
2362 					  cs->key1, &o);
2363 			if (res != TEE_SUCCESS)
2364 				return res;
2365 			if ((o->info.handleFlags &
2366 			     TEE_HANDLE_FLAG_INITIALIZED) == 0)
2367 				return TEE_ERROR_BAD_PARAMETERS;
2368 
2369 			key = (struct tee_cryp_obj_secret *)o->attr;
2370 			res = crypto_mac_init(cs->ctx, (void *)(key + 1),
2371 					      key->key_size);
2372 			if (res != TEE_SUCCESS)
2373 				return res;
2374 			break;
2375 		}
2376 	default:
2377 		return TEE_ERROR_BAD_PARAMETERS;
2378 	}
2379 
2380 	cs->state = CRYP_STATE_INITIALIZED;
2381 
2382 	return TEE_SUCCESS;
2383 }
2384 
2385 TEE_Result syscall_hash_update(unsigned long state, const void *chunk,
2386 			size_t chunk_size)
2387 {
2388 	struct tee_ta_session *sess = NULL;
2389 	struct tee_cryp_state *cs = NULL;
2390 	TEE_Result res = TEE_SUCCESS;
2391 
2392 	/* No data, but size provided isn't valid parameters. */
2393 	if (!chunk && chunk_size)
2394 		return TEE_ERROR_BAD_PARAMETERS;
2395 
2396 	/* Zero length hash is valid, but nothing we need to do. */
2397 	if (!chunk_size)
2398 		return TEE_SUCCESS;
2399 
2400 	res = tee_ta_get_current_session(&sess);
2401 	if (res != TEE_SUCCESS)
2402 		return res;
2403 
2404 	res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
2405 					  TEE_MEMORY_ACCESS_READ |
2406 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2407 					  (uaddr_t)chunk, chunk_size);
2408 	if (res != TEE_SUCCESS)
2409 		return res;
2410 
2411 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
2412 	if (res != TEE_SUCCESS)
2413 		return res;
2414 
2415 	if (cs->state != CRYP_STATE_INITIALIZED)
2416 		return TEE_ERROR_BAD_STATE;
2417 
2418 	switch (TEE_ALG_GET_CLASS(cs->algo)) {
2419 	case TEE_OPERATION_DIGEST:
2420 		res = crypto_hash_update(cs->ctx, chunk, chunk_size);
2421 		if (res != TEE_SUCCESS)
2422 			return res;
2423 		break;
2424 	case TEE_OPERATION_MAC:
2425 		res = crypto_mac_update(cs->ctx, chunk, chunk_size);
2426 		if (res != TEE_SUCCESS)
2427 			return res;
2428 		break;
2429 	default:
2430 		return TEE_ERROR_BAD_PARAMETERS;
2431 	}
2432 
2433 	return TEE_SUCCESS;
2434 }
2435 
2436 TEE_Result syscall_hash_final(unsigned long state, const void *chunk,
2437 			size_t chunk_size, void *hash, uint64_t *hash_len)
2438 {
2439 	struct tee_ta_session *sess = NULL;
2440 	struct tee_cryp_state *cs = NULL;
2441 	TEE_Result res2 = TEE_SUCCESS;
2442 	TEE_Result res = TEE_SUCCESS;
2443 	size_t hash_size = 0;
2444 	size_t hlen = 0;
2445 
2446 	/* No data, but size provided isn't valid parameters. */
2447 	if (!chunk && chunk_size)
2448 		return TEE_ERROR_BAD_PARAMETERS;
2449 
2450 	res = tee_ta_get_current_session(&sess);
2451 	if (res != TEE_SUCCESS)
2452 		return res;
2453 
2454 	res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
2455 					  TEE_MEMORY_ACCESS_READ |
2456 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2457 					  (uaddr_t)chunk, chunk_size);
2458 	if (res != TEE_SUCCESS)
2459 		return res;
2460 
2461 	res = get_user_u64_as_size_t(&hlen, hash_len);
2462 	if (res != TEE_SUCCESS)
2463 		return res;
2464 
2465 	res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
2466 					  TEE_MEMORY_ACCESS_READ |
2467 					  TEE_MEMORY_ACCESS_WRITE |
2468 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2469 					  (uaddr_t)hash, hlen);
2470 	if (res != TEE_SUCCESS)
2471 		return res;
2472 
2473 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
2474 	if (res != TEE_SUCCESS)
2475 		return res;
2476 
2477 	if (cs->state != CRYP_STATE_INITIALIZED)
2478 		return TEE_ERROR_BAD_STATE;
2479 
2480 	switch (TEE_ALG_GET_CLASS(cs->algo)) {
2481 	case TEE_OPERATION_DIGEST:
2482 		res = tee_alg_get_digest_size(cs->algo, &hash_size);
2483 		if (res != TEE_SUCCESS)
2484 			return res;
2485 		if (hlen < hash_size) {
2486 			res = TEE_ERROR_SHORT_BUFFER;
2487 			goto out;
2488 		}
2489 
2490 		if (chunk_size) {
2491 			res = crypto_hash_update(cs->ctx, chunk, chunk_size);
2492 			if (res != TEE_SUCCESS)
2493 				return res;
2494 		}
2495 
2496 		res = crypto_hash_final(cs->ctx, hash, hash_size);
2497 		if (res != TEE_SUCCESS)
2498 			return res;
2499 		break;
2500 
2501 	case TEE_OPERATION_MAC:
2502 		res = tee_alg_get_digest_size(cs->algo, &hash_size);
2503 		if (res != TEE_SUCCESS)
2504 			return res;
2505 		if (hlen < hash_size) {
2506 			res = TEE_ERROR_SHORT_BUFFER;
2507 			goto out;
2508 		}
2509 
2510 		if (chunk_size) {
2511 			res = crypto_mac_update(cs->ctx, chunk, chunk_size);
2512 			if (res != TEE_SUCCESS)
2513 				return res;
2514 		}
2515 
2516 		res = crypto_mac_final(cs->ctx, hash, hash_size);
2517 		if (res != TEE_SUCCESS)
2518 			return res;
2519 		break;
2520 
2521 	default:
2522 		return TEE_ERROR_BAD_PARAMETERS;
2523 	}
2524 out:
2525 	res2 = put_user_u64(hash_len, hash_size);
2526 	if (res2 != TEE_SUCCESS)
2527 		return res2;
2528 	return res;
2529 }
2530 
2531 TEE_Result syscall_cipher_init(unsigned long state, const void *iv,
2532 			size_t iv_len)
2533 {
2534 	struct tee_cryp_obj_secret *key1 = NULL;
2535 	struct tee_ta_session *sess = NULL;
2536 	struct tee_cryp_state *cs = NULL;
2537 	struct user_ta_ctx *utc = NULL;
2538 	TEE_Result res = TEE_SUCCESS;
2539 	struct tee_obj *o = NULL;
2540 
2541 	res = tee_ta_get_current_session(&sess);
2542 	if (res != TEE_SUCCESS)
2543 		return res;
2544 	utc = to_user_ta_ctx(sess->ctx);
2545 
2546 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
2547 	if (res != TEE_SUCCESS)
2548 		return res;
2549 
2550 	if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_CIPHER)
2551 		return TEE_ERROR_BAD_STATE;
2552 
2553 	res = tee_mmu_check_access_rights(&utc->uctx,
2554 					  TEE_MEMORY_ACCESS_READ |
2555 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2556 					  (uaddr_t)iv, iv_len);
2557 	if (res != TEE_SUCCESS)
2558 		return res;
2559 
2560 	res = tee_obj_get(utc, cs->key1, &o);
2561 	if (res != TEE_SUCCESS)
2562 		return res;
2563 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
2564 		return TEE_ERROR_BAD_PARAMETERS;
2565 
2566 	key1 = o->attr;
2567 
2568 	if (tee_obj_get(utc, cs->key2, &o) == TEE_SUCCESS) {
2569 		struct tee_cryp_obj_secret *key2 = o->attr;
2570 
2571 		if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
2572 			return TEE_ERROR_BAD_PARAMETERS;
2573 
2574 		res = crypto_cipher_init(cs->ctx, cs->mode,
2575 					 (uint8_t *)(key1 + 1), key1->key_size,
2576 					 (uint8_t *)(key2 + 1), key2->key_size,
2577 					 iv, iv_len);
2578 	} else {
2579 		res = crypto_cipher_init(cs->ctx, cs->mode,
2580 					 (uint8_t *)(key1 + 1), key1->key_size,
2581 					 NULL, 0, iv, iv_len);
2582 	}
2583 	if (res != TEE_SUCCESS)
2584 		return res;
2585 
2586 	cs->ctx_finalize = crypto_cipher_final;
2587 	cs->state = CRYP_STATE_INITIALIZED;
2588 
2589 	return TEE_SUCCESS;
2590 }
2591 
2592 static TEE_Result tee_svc_cipher_update_helper(unsigned long state,
2593 			bool last_block, const void *src, size_t src_len,
2594 			void *dst, uint64_t *dst_len)
2595 {
2596 	struct tee_ta_session *sess = NULL;
2597 	struct tee_cryp_state *cs = NULL;
2598 	TEE_Result res = TEE_SUCCESS;
2599 	size_t dlen = 0;
2600 
2601 	res = tee_ta_get_current_session(&sess);
2602 	if (res != TEE_SUCCESS)
2603 		return res;
2604 
2605 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
2606 	if (res != TEE_SUCCESS)
2607 		return res;
2608 
2609 	if (cs->state != CRYP_STATE_INITIALIZED)
2610 		return TEE_ERROR_BAD_STATE;
2611 
2612 	res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
2613 					  TEE_MEMORY_ACCESS_READ |
2614 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2615 					  (uaddr_t)src, src_len);
2616 	if (res != TEE_SUCCESS)
2617 		return res;
2618 
2619 	if (!dst_len) {
2620 		dlen = 0;
2621 	} else {
2622 		struct user_mode_ctx *uctx = &to_user_ta_ctx(sess->ctx)->uctx;
2623 		uint32_t flags = TEE_MEMORY_ACCESS_READ |
2624 				 TEE_MEMORY_ACCESS_WRITE |
2625 				 TEE_MEMORY_ACCESS_ANY_OWNER;
2626 
2627 		res = get_user_u64_as_size_t(&dlen, dst_len);
2628 		if (res != TEE_SUCCESS)
2629 			return res;
2630 
2631 		res = tee_mmu_check_access_rights(uctx, flags, (uaddr_t)dst,
2632 						  dlen);
2633 		if (res != TEE_SUCCESS)
2634 			return res;
2635 	}
2636 
2637 	if (dlen < src_len) {
2638 		res = TEE_ERROR_SHORT_BUFFER;
2639 		goto out;
2640 	}
2641 
2642 	if (src_len > 0) {
2643 		/* Permit src_len == 0 to finalize the operation */
2644 		res = tee_do_cipher_update(cs->ctx, cs->algo, cs->mode,
2645 					   last_block, src, src_len, dst);
2646 	}
2647 
2648 	if (last_block && cs->ctx_finalize != NULL) {
2649 		cs->ctx_finalize(cs->ctx);
2650 		cs->ctx_finalize = NULL;
2651 	}
2652 
2653 out:
2654 	if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) &&
2655 	    dst_len != NULL) {
2656 		TEE_Result res2;
2657 
2658 		res2 = put_user_u64(dst_len, src_len);
2659 		if (res2 != TEE_SUCCESS)
2660 			res = res2;
2661 	}
2662 
2663 	return res;
2664 }
2665 
2666 TEE_Result syscall_cipher_update(unsigned long state, const void *src,
2667 			size_t src_len, void *dst, uint64_t *dst_len)
2668 {
2669 	return tee_svc_cipher_update_helper(state, false /* last_block */,
2670 					    src, src_len, dst, dst_len);
2671 }
2672 
2673 TEE_Result syscall_cipher_final(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, true /* last_block */,
2677 					    src, src_len, dst, dst_len);
2678 }
2679 
2680 #if defined(CFG_CRYPTO_HKDF)
2681 static TEE_Result get_hkdf_params(const TEE_Attribute *params,
2682 				  uint32_t param_count,
2683 				  void **salt, size_t *salt_len, void **info,
2684 				  size_t *info_len, size_t *okm_len)
2685 {
2686 	size_t n;
2687 	enum { SALT = 0x1, LENGTH = 0x2, INFO = 0x4 };
2688 	uint8_t found = 0;
2689 
2690 	*salt = *info = NULL;
2691 	*salt_len = *info_len = *okm_len = 0;
2692 
2693 	for (n = 0; n < param_count; n++) {
2694 		switch (params[n].attributeID) {
2695 		case TEE_ATTR_HKDF_SALT:
2696 			if (!(found & SALT)) {
2697 				*salt = params[n].content.ref.buffer;
2698 				*salt_len = params[n].content.ref.length;
2699 				found |= SALT;
2700 			}
2701 			break;
2702 		case TEE_ATTR_HKDF_OKM_LENGTH:
2703 			if (!(found & LENGTH)) {
2704 				*okm_len = params[n].content.value.a;
2705 				found |= LENGTH;
2706 			}
2707 			break;
2708 		case TEE_ATTR_HKDF_INFO:
2709 			if (!(found & INFO)) {
2710 				*info = params[n].content.ref.buffer;
2711 				*info_len = params[n].content.ref.length;
2712 				found |= INFO;
2713 			}
2714 			break;
2715 		default:
2716 			/* Unexpected attribute */
2717 			return TEE_ERROR_BAD_PARAMETERS;
2718 		}
2719 
2720 	}
2721 
2722 	if (!(found & LENGTH))
2723 		return TEE_ERROR_BAD_PARAMETERS;
2724 
2725 	return TEE_SUCCESS;
2726 }
2727 #endif
2728 
2729 #if defined(CFG_CRYPTO_CONCAT_KDF)
2730 static TEE_Result get_concat_kdf_params(const TEE_Attribute *params,
2731 					uint32_t param_count,
2732 					void **other_info,
2733 					size_t *other_info_len,
2734 					size_t *derived_key_len)
2735 {
2736 	size_t n;
2737 	enum { LENGTH = 0x1, INFO = 0x2 };
2738 	uint8_t found = 0;
2739 
2740 	*other_info = NULL;
2741 	*other_info_len = *derived_key_len = 0;
2742 
2743 	for (n = 0; n < param_count; n++) {
2744 		switch (params[n].attributeID) {
2745 		case TEE_ATTR_CONCAT_KDF_OTHER_INFO:
2746 			if (!(found & INFO)) {
2747 				*other_info = params[n].content.ref.buffer;
2748 				*other_info_len = params[n].content.ref.length;
2749 				found |= INFO;
2750 			}
2751 			break;
2752 		case TEE_ATTR_CONCAT_KDF_DKM_LENGTH:
2753 			if (!(found & LENGTH)) {
2754 				*derived_key_len = params[n].content.value.a;
2755 				found |= LENGTH;
2756 			}
2757 			break;
2758 		default:
2759 			/* Unexpected attribute */
2760 			return TEE_ERROR_BAD_PARAMETERS;
2761 		}
2762 	}
2763 
2764 	if (!(found & LENGTH))
2765 		return TEE_ERROR_BAD_PARAMETERS;
2766 
2767 	return TEE_SUCCESS;
2768 }
2769 #endif
2770 
2771 #if defined(CFG_CRYPTO_PBKDF2)
2772 static TEE_Result get_pbkdf2_params(const TEE_Attribute *params,
2773 				   uint32_t param_count, void **salt,
2774 				   size_t *salt_len, size_t *derived_key_len,
2775 				   size_t *iteration_count)
2776 {
2777 	size_t n;
2778 	enum { SALT = 0x1, LENGTH = 0x2, COUNT = 0x4 };
2779 	uint8_t found = 0;
2780 
2781 	*salt = NULL;
2782 	*salt_len = *derived_key_len = *iteration_count = 0;
2783 
2784 	for (n = 0; n < param_count; n++) {
2785 		switch (params[n].attributeID) {
2786 		case TEE_ATTR_PBKDF2_SALT:
2787 			if (!(found & SALT)) {
2788 				*salt = params[n].content.ref.buffer;
2789 				*salt_len = params[n].content.ref.length;
2790 				found |= SALT;
2791 			}
2792 			break;
2793 		case TEE_ATTR_PBKDF2_DKM_LENGTH:
2794 			if (!(found & LENGTH)) {
2795 				*derived_key_len = params[n].content.value.a;
2796 				found |= LENGTH;
2797 			}
2798 			break;
2799 		case TEE_ATTR_PBKDF2_ITERATION_COUNT:
2800 			if (!(found & COUNT)) {
2801 				*iteration_count = params[n].content.value.a;
2802 				found |= COUNT;
2803 			}
2804 			break;
2805 		default:
2806 			/* Unexpected attribute */
2807 			return TEE_ERROR_BAD_PARAMETERS;
2808 		}
2809 	}
2810 
2811 	if ((found & (LENGTH|COUNT)) != (LENGTH|COUNT))
2812 		return TEE_ERROR_BAD_PARAMETERS;
2813 
2814 	return TEE_SUCCESS;
2815 }
2816 #endif
2817 
2818 #if defined(CFG_CRYPTO_SM2_KEP)
2819 static TEE_Result get_sm2_kep_params(const TEE_Attribute *params,
2820 				     uint32_t param_count,
2821 				     struct ecc_public_key *peer_key,
2822 				     struct ecc_public_key *peer_eph_key,
2823 				     struct sm2_kep_parms *kep_parms)
2824 {
2825 	TEE_Result res = TEE_ERROR_GENERIC;
2826 	size_t n;
2827 	enum {
2828 		IS_INITIATOR,
2829 		PEER_KEY_X,
2830 		PEER_KEY_Y,
2831 		PEER_EPH_KEY_X,
2832 		PEER_EPH_KEY_Y,
2833 		INITIATOR_ID,
2834 		RESPONDER_ID,
2835 	};
2836 	uint8_t mandatory = BIT(IS_INITIATOR) | BIT(PEER_KEY_X) |
2837 		BIT(PEER_KEY_Y) | BIT(PEER_EPH_KEY_X) | BIT(PEER_EPH_KEY_Y) |
2838 		BIT(INITIATOR_ID) | BIT(RESPONDER_ID);
2839 	uint8_t found = 0;
2840 
2841 	res = crypto_acipher_alloc_ecc_public_key(peer_key, 256);
2842 	if (res)
2843 		goto out;
2844 
2845 	res = crypto_acipher_alloc_ecc_public_key(peer_eph_key, 256);
2846 	if (res)
2847 		goto out;
2848 
2849 	peer_key->curve = TEE_ECC_CURVE_SM2;
2850 	peer_eph_key->curve = TEE_ECC_CURVE_SM2;
2851 
2852 	for (n = 0; n < param_count; n++) {
2853 		const TEE_Attribute *p = &params[n];
2854 
2855 		switch (p->attributeID) {
2856 		case TEE_ATTR_SM2_KEP_USER:
2857 			kep_parms->is_initiator = !p->content.value.a;
2858 			found |= BIT(IS_INITIATOR);
2859 			break;
2860 		case TEE_ATTR_ECC_PUBLIC_VALUE_X:
2861 			crypto_bignum_bin2bn(p->content.ref.buffer,
2862 					     p->content.ref.length,
2863 					     peer_key->x);
2864 			found |= BIT(PEER_KEY_X);
2865 			break;
2866 		case TEE_ATTR_ECC_PUBLIC_VALUE_Y:
2867 			crypto_bignum_bin2bn(p->content.ref.buffer,
2868 					     p->content.ref.length,
2869 					     peer_key->y);
2870 			found |= BIT(PEER_KEY_Y);
2871 			break;
2872 		case TEE_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_X:
2873 			crypto_bignum_bin2bn(p->content.ref.buffer,
2874 					     p->content.ref.length,
2875 					     peer_eph_key->x);
2876 			found |= BIT(PEER_EPH_KEY_X);
2877 			break;
2878 		case TEE_ATTR_ECC_EPHEMERAL_PUBLIC_VALUE_Y:
2879 			crypto_bignum_bin2bn(p->content.ref.buffer,
2880 					     p->content.ref.length,
2881 					     peer_eph_key->y);
2882 			found |= BIT(PEER_EPH_KEY_Y);
2883 			break;
2884 		case TEE_ATTR_SM2_ID_INITIATOR:
2885 			kep_parms->initiator_id = p->content.ref.buffer;
2886 			kep_parms->initiator_id_len = p->content.ref.length;
2887 			found |= BIT(INITIATOR_ID);
2888 			break;
2889 		case TEE_ATTR_SM2_ID_RESPONDER:
2890 			kep_parms->responder_id = p->content.ref.buffer;
2891 			kep_parms->responder_id_len = p->content.ref.length;
2892 			found |= BIT(RESPONDER_ID);
2893 			break;
2894 		case TEE_ATTR_SM2_KEP_CONFIRMATION_IN:
2895 			kep_parms->conf_in = p->content.ref.buffer;
2896 			kep_parms->conf_in_len = p->content.ref.length;
2897 			break;
2898 		case TEE_ATTR_SM2_KEP_CONFIRMATION_OUT:
2899 			kep_parms->conf_out = p->content.ref.buffer;
2900 			kep_parms->conf_out_len = p->content.ref.length;
2901 			break;
2902 		default:
2903 			/* Unexpected attribute */
2904 			res = TEE_ERROR_BAD_PARAMETERS;
2905 			goto out;
2906 		}
2907 	}
2908 
2909 	if ((found & mandatory) != mandatory) {
2910 		res = TEE_ERROR_BAD_PARAMETERS;
2911 		goto out;
2912 	}
2913 
2914 	return TEE_SUCCESS;
2915 out:
2916 	crypto_acipher_free_ecc_public_key(peer_key);
2917 	crypto_acipher_free_ecc_public_key(peer_eph_key);
2918 	return res;
2919 }
2920 #endif
2921 
2922 TEE_Result syscall_cryp_derive_key(unsigned long state,
2923 			const struct utee_attribute *usr_params,
2924 			unsigned long param_count, unsigned long derived_key)
2925 {
2926 	TEE_Result res = TEE_ERROR_NOT_SUPPORTED;
2927 	struct tee_ta_session *sess;
2928 	struct tee_obj *ko;
2929 	struct tee_obj *so;
2930 	struct tee_cryp_state *cs;
2931 	struct tee_cryp_obj_secret *sk;
2932 	const struct tee_cryp_obj_type_props *type_props;
2933 	TEE_Attribute *params = NULL;
2934 	struct user_ta_ctx *utc;
2935 
2936 	res = tee_ta_get_current_session(&sess);
2937 	if (res != TEE_SUCCESS)
2938 		return res;
2939 	utc = to_user_ta_ctx(sess->ctx);
2940 
2941 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
2942 	if (res != TEE_SUCCESS)
2943 		return res;
2944 
2945 	size_t alloc_size = 0;
2946 
2947 	if (MUL_OVERFLOW(sizeof(TEE_Attribute), param_count, &alloc_size))
2948 		return TEE_ERROR_OVERFLOW;
2949 
2950 	params = malloc(alloc_size);
2951 	if (!params)
2952 		return TEE_ERROR_OUT_OF_MEMORY;
2953 	res = copy_in_attrs(utc, usr_params, param_count, params);
2954 	if (res != TEE_SUCCESS)
2955 		goto out;
2956 
2957 	/* Get key set in operation */
2958 	res = tee_obj_get(utc, cs->key1, &ko);
2959 	if (res != TEE_SUCCESS)
2960 		goto out;
2961 
2962 	res = tee_obj_get(utc, uref_to_vaddr(derived_key), &so);
2963 	if (res != TEE_SUCCESS)
2964 		goto out;
2965 
2966 	/* Find information needed about the object to initialize */
2967 	sk = so->attr;
2968 
2969 	/* Find description of object */
2970 	type_props = tee_svc_find_type_props(so->info.objectType);
2971 	if (!type_props) {
2972 		res = TEE_ERROR_NOT_SUPPORTED;
2973 		goto out;
2974 	}
2975 
2976 	if (cs->algo == TEE_ALG_DH_DERIVE_SHARED_SECRET) {
2977 		struct bignum *pub;
2978 		struct bignum *ss;
2979 
2980 		if (param_count != 1 ||
2981 		    params[0].attributeID != TEE_ATTR_DH_PUBLIC_VALUE) {
2982 			res = TEE_ERROR_BAD_PARAMETERS;
2983 			goto out;
2984 		}
2985 
2986 		size_t bin_size = params[0].content.ref.length;
2987 
2988 		if (MUL_OVERFLOW(bin_size, 8, &alloc_size)) {
2989 			res = TEE_ERROR_OVERFLOW;
2990 			goto out;
2991 		}
2992 
2993 		pub = crypto_bignum_allocate(alloc_size);
2994 		ss = crypto_bignum_allocate(alloc_size);
2995 		if (pub && ss) {
2996 			crypto_bignum_bin2bn(params[0].content.ref.buffer,
2997 					     bin_size, pub);
2998 			res = crypto_acipher_dh_shared_secret(ko->attr,
2999 							      pub, ss);
3000 			if (res == TEE_SUCCESS) {
3001 				sk->key_size = crypto_bignum_num_bytes(ss);
3002 				crypto_bignum_bn2bin(ss, (uint8_t *)(sk + 1));
3003 				so->info.handleFlags |=
3004 						TEE_HANDLE_FLAG_INITIALIZED;
3005 				set_attribute(so, type_props,
3006 					      TEE_ATTR_SECRET_VALUE);
3007 			}
3008 		} else {
3009 			res = TEE_ERROR_OUT_OF_MEMORY;
3010 		}
3011 		crypto_bignum_free(pub);
3012 		crypto_bignum_free(ss);
3013 	} else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_ECDH) {
3014 		struct ecc_public_key key_public;
3015 		uint8_t *pt_secret;
3016 		unsigned long pt_secret_len;
3017 
3018 		if (param_count != 2 ||
3019 		    params[0].attributeID != TEE_ATTR_ECC_PUBLIC_VALUE_X ||
3020 		    params[1].attributeID != TEE_ATTR_ECC_PUBLIC_VALUE_Y) {
3021 			res = TEE_ERROR_BAD_PARAMETERS;
3022 			goto out;
3023 		}
3024 
3025 		switch (cs->algo) {
3026 		case TEE_ALG_ECDH_P192:
3027 			alloc_size = 192;
3028 			break;
3029 		case TEE_ALG_ECDH_P224:
3030 			alloc_size = 224;
3031 			break;
3032 		case TEE_ALG_ECDH_P256:
3033 			alloc_size = 256;
3034 			break;
3035 		case TEE_ALG_ECDH_P384:
3036 			alloc_size = 384;
3037 			break;
3038 		case TEE_ALG_ECDH_P521:
3039 			alloc_size = 521;
3040 			break;
3041 		default:
3042 			res = TEE_ERROR_NOT_IMPLEMENTED;
3043 			goto out;
3044 		}
3045 
3046 		/* Create the public key */
3047 		res = crypto_acipher_alloc_ecc_public_key(&key_public,
3048 							  alloc_size);
3049 		if (res != TEE_SUCCESS)
3050 			goto out;
3051 		key_public.curve = ((struct ecc_keypair *)ko->attr)->curve;
3052 		crypto_bignum_bin2bn(params[0].content.ref.buffer,
3053 				     params[0].content.ref.length,
3054 				     key_public.x);
3055 		crypto_bignum_bin2bn(params[1].content.ref.buffer,
3056 				     params[1].content.ref.length,
3057 				     key_public.y);
3058 
3059 		pt_secret = (uint8_t *)(sk + 1);
3060 		pt_secret_len = sk->alloc_size;
3061 		res = crypto_acipher_ecc_shared_secret(ko->attr, &key_public,
3062 						       pt_secret,
3063 						       &pt_secret_len);
3064 
3065 		if (res == TEE_SUCCESS) {
3066 			sk->key_size = pt_secret_len;
3067 			so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
3068 			set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE);
3069 		}
3070 
3071 		/* free the public key */
3072 		crypto_acipher_free_ecc_public_key(&key_public);
3073 	}
3074 #if defined(CFG_CRYPTO_HKDF)
3075 	else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_HKDF) {
3076 		void *salt, *info;
3077 		size_t salt_len, info_len, okm_len;
3078 		uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo);
3079 		struct tee_cryp_obj_secret *ik = ko->attr;
3080 		const uint8_t *ikm = (const uint8_t *)(ik + 1);
3081 
3082 		res = get_hkdf_params(params, param_count, &salt, &salt_len,
3083 				      &info, &info_len, &okm_len);
3084 		if (res != TEE_SUCCESS)
3085 			goto out;
3086 
3087 		/* Requested size must fit into the output object's buffer */
3088 		if (okm_len > ik->alloc_size) {
3089 			res = TEE_ERROR_BAD_PARAMETERS;
3090 			goto out;
3091 		}
3092 
3093 		res = tee_cryp_hkdf(hash_id, ikm, ik->key_size, salt, salt_len,
3094 				    info, info_len, (uint8_t *)(sk + 1),
3095 				    okm_len);
3096 		if (res == TEE_SUCCESS) {
3097 			sk->key_size = okm_len;
3098 			so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
3099 			set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE);
3100 		}
3101 	}
3102 #endif
3103 #if defined(CFG_CRYPTO_CONCAT_KDF)
3104 	else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_CONCAT_KDF) {
3105 		void *info;
3106 		size_t info_len, derived_key_len;
3107 		uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo);
3108 		struct tee_cryp_obj_secret *ss = ko->attr;
3109 		const uint8_t *shared_secret = (const uint8_t *)(ss + 1);
3110 
3111 		res = get_concat_kdf_params(params, param_count, &info,
3112 					    &info_len, &derived_key_len);
3113 		if (res != TEE_SUCCESS)
3114 			goto out;
3115 
3116 		/* Requested size must fit into the output object's buffer */
3117 		if (derived_key_len > ss->alloc_size) {
3118 			res = TEE_ERROR_BAD_PARAMETERS;
3119 			goto out;
3120 		}
3121 
3122 		res = tee_cryp_concat_kdf(hash_id, shared_secret, ss->key_size,
3123 					  info, info_len, (uint8_t *)(sk + 1),
3124 					  derived_key_len);
3125 		if (res == TEE_SUCCESS) {
3126 			sk->key_size = derived_key_len;
3127 			so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
3128 			set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE);
3129 		}
3130 	}
3131 #endif
3132 #if defined(CFG_CRYPTO_PBKDF2)
3133 	else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_PBKDF2) {
3134 		void *salt;
3135 		size_t salt_len, iteration_count, derived_key_len;
3136 		uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo);
3137 		struct tee_cryp_obj_secret *ss = ko->attr;
3138 		const uint8_t *password = (const uint8_t *)(ss + 1);
3139 
3140 		res = get_pbkdf2_params(params, param_count, &salt, &salt_len,
3141 					&derived_key_len, &iteration_count);
3142 		if (res != TEE_SUCCESS)
3143 			goto out;
3144 
3145 		/* Requested size must fit into the output object's buffer */
3146 		if (derived_key_len > ss->alloc_size) {
3147 			res = TEE_ERROR_BAD_PARAMETERS;
3148 			goto out;
3149 		}
3150 
3151 		res = tee_cryp_pbkdf2(hash_id, password, ss->key_size, salt,
3152 				      salt_len, iteration_count,
3153 				      (uint8_t *)(sk + 1), derived_key_len);
3154 		if (res == TEE_SUCCESS) {
3155 			sk->key_size = derived_key_len;
3156 			so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
3157 			set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE);
3158 		}
3159 	}
3160 #endif
3161 #if defined(CFG_CRYPTO_SM2_KEP)
3162 	else if (cs->algo == TEE_ALG_SM2_KEP) {
3163 		struct ecc_public_key peer_eph_key = { };
3164 		struct ecc_public_key peer_key = { };
3165 		struct sm2_kep_parms kep_parms = {
3166 			.out = (uint8_t *)(sk + 1),
3167 			.out_len = so->info.maxKeySize,
3168 		};
3169 		struct tee_obj *ko2 = NULL;
3170 
3171 		res = tee_obj_get(utc, cs->key2, &ko2);
3172 		if (res != TEE_SUCCESS)
3173 			goto out;
3174 
3175 		res = get_sm2_kep_params(params, param_count, &peer_key,
3176 					 &peer_eph_key, &kep_parms);
3177 		if (res != TEE_SUCCESS)
3178 			goto out;
3179 
3180 		/*
3181 		 * key1 is our private keypair, key2 is our ephemeral public key
3182 		 */
3183 		res = crypto_acipher_sm2_kep_derive(ko->attr, /* key1 */
3184 						    ko2->attr, /* key2 */
3185 						    &peer_key, &peer_eph_key,
3186 						    &kep_parms);
3187 
3188 		if (res == TEE_SUCCESS) {
3189 			sk->key_size = kep_parms.out_len;
3190 			so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
3191 			set_attribute(so, type_props, TEE_ATTR_SECRET_VALUE);
3192 		}
3193 		crypto_acipher_free_ecc_public_key(&peer_key);
3194 		crypto_acipher_free_ecc_public_key(&peer_eph_key);
3195 	}
3196 #endif
3197 	else
3198 		res = TEE_ERROR_NOT_SUPPORTED;
3199 
3200 out:
3201 	free_wipe(params);
3202 	return res;
3203 }
3204 
3205 TEE_Result syscall_cryp_random_number_generate(void *buf, size_t blen)
3206 {
3207 	struct tee_ta_session *sess = NULL;
3208 	TEE_Result res = TEE_SUCCESS;
3209 
3210 	res = tee_ta_get_current_session(&sess);
3211 	if (res != TEE_SUCCESS)
3212 		return res;
3213 
3214 	res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
3215 					  TEE_MEMORY_ACCESS_WRITE,
3216 					  (uaddr_t)buf, blen);
3217 	if (res != TEE_SUCCESS)
3218 		return res;
3219 
3220 	res = crypto_rng_read(buf, blen);
3221 	if (res != TEE_SUCCESS)
3222 		return res;
3223 
3224 	return res;
3225 }
3226 
3227 TEE_Result syscall_authenc_init(unsigned long state, const void *nonce,
3228 				size_t nonce_len, size_t tag_len,
3229 				size_t aad_len, size_t payload_len)
3230 {
3231 	struct tee_cryp_obj_secret *key = NULL;
3232 	struct tee_ta_session *sess = NULL;
3233 	struct tee_cryp_state *cs = NULL;
3234 	TEE_Result res = TEE_SUCCESS;
3235 	struct tee_obj *o = NULL;
3236 
3237 	res = tee_ta_get_current_session(&sess);
3238 	if (res != TEE_SUCCESS)
3239 		return res;
3240 
3241 	res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
3242 					  TEE_MEMORY_ACCESS_READ |
3243 					  TEE_MEMORY_ACCESS_ANY_OWNER,
3244 					  (uaddr_t)nonce, nonce_len);
3245 	if (res != TEE_SUCCESS)
3246 		return res;
3247 
3248 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
3249 	if (res != TEE_SUCCESS)
3250 		return res;
3251 
3252 	res = tee_obj_get(to_user_ta_ctx(sess->ctx), cs->key1, &o);
3253 	if (res != TEE_SUCCESS)
3254 		return res;
3255 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
3256 		return TEE_ERROR_BAD_PARAMETERS;
3257 
3258 	key = o->attr;
3259 	res = crypto_authenc_init(cs->ctx, cs->mode, (uint8_t *)(key + 1),
3260 				  key->key_size, nonce, nonce_len, tag_len,
3261 				  aad_len, payload_len);
3262 	if (res != TEE_SUCCESS)
3263 		return res;
3264 
3265 	cs->ctx_finalize = crypto_authenc_final;
3266 	cs->state = CRYP_STATE_INITIALIZED;
3267 
3268 	return TEE_SUCCESS;
3269 }
3270 
3271 TEE_Result syscall_authenc_update_aad(unsigned long state,
3272 				      const void *aad_data, size_t aad_data_len)
3273 {
3274 	TEE_Result res = TEE_SUCCESS;
3275 	struct tee_cryp_state *cs;
3276 	struct tee_ta_session *sess;
3277 
3278 	res = tee_ta_get_current_session(&sess);
3279 	if (res != TEE_SUCCESS)
3280 		return res;
3281 
3282 	res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
3283 					  TEE_MEMORY_ACCESS_READ |
3284 					  TEE_MEMORY_ACCESS_ANY_OWNER,
3285 					  (uaddr_t) aad_data,
3286 					  aad_data_len);
3287 	if (res != TEE_SUCCESS)
3288 		return res;
3289 
3290 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
3291 	if (res != TEE_SUCCESS)
3292 		return res;
3293 
3294 	if (cs->state != CRYP_STATE_INITIALIZED)
3295 		return TEE_ERROR_BAD_STATE;
3296 
3297 	if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE)
3298 		return TEE_ERROR_BAD_STATE;
3299 
3300 	res = crypto_authenc_update_aad(cs->ctx, cs->mode, aad_data,
3301 					aad_data_len);
3302 	if (res != TEE_SUCCESS)
3303 		return res;
3304 
3305 	return TEE_SUCCESS;
3306 }
3307 
3308 TEE_Result syscall_authenc_update_payload(unsigned long state,
3309 					  const void *src_data,
3310 					  size_t src_len, void *dst_data,
3311 					  uint64_t *dst_len)
3312 {
3313 	struct tee_ta_session *sess = NULL;
3314 	struct tee_cryp_state *cs = NULL;
3315 	TEE_Result res = TEE_SUCCESS;
3316 	size_t dlen = 0;
3317 
3318 	res = tee_ta_get_current_session(&sess);
3319 	if (res != TEE_SUCCESS)
3320 		return res;
3321 
3322 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
3323 	if (res != TEE_SUCCESS)
3324 		return res;
3325 
3326 	if (cs->state != CRYP_STATE_INITIALIZED)
3327 		return TEE_ERROR_BAD_STATE;
3328 
3329 	if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE)
3330 		return TEE_ERROR_BAD_STATE;
3331 
3332 	res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
3333 					  TEE_MEMORY_ACCESS_READ |
3334 					  TEE_MEMORY_ACCESS_ANY_OWNER,
3335 					  (uaddr_t)src_data, src_len);
3336 	if (res != TEE_SUCCESS)
3337 		return res;
3338 
3339 	res = get_user_u64_as_size_t(&dlen, dst_len);
3340 	if (res != TEE_SUCCESS)
3341 		return res;
3342 
3343 	res = tee_mmu_check_access_rights(&to_user_ta_ctx(sess->ctx)->uctx,
3344 					  TEE_MEMORY_ACCESS_READ |
3345 					  TEE_MEMORY_ACCESS_WRITE |
3346 					  TEE_MEMORY_ACCESS_ANY_OWNER,
3347 					  (uaddr_t)dst_data, dlen);
3348 	if (res != TEE_SUCCESS)
3349 		return res;
3350 
3351 	if (dlen < src_len) {
3352 		res = TEE_ERROR_SHORT_BUFFER;
3353 		goto out;
3354 	}
3355 
3356 	res = crypto_authenc_update_payload(cs->ctx, cs->mode, src_data,
3357 					    src_len, dst_data, &dlen);
3358 out:
3359 	if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) {
3360 		TEE_Result res2 = put_user_u64(dst_len, dlen);
3361 
3362 		if (res2 != TEE_SUCCESS)
3363 			res = res2;
3364 	}
3365 
3366 	return res;
3367 }
3368 
3369 TEE_Result syscall_authenc_enc_final(unsigned long state, const void *src_data,
3370 				     size_t src_len, void *dst_data,
3371 				     uint64_t *dst_len, void *tag,
3372 				     uint64_t *tag_len)
3373 {
3374 	struct tee_ta_session *sess = NULL;
3375 	struct user_mode_ctx *uctx = NULL;
3376 	struct tee_cryp_state *cs = NULL;
3377 	TEE_Result res = TEE_SUCCESS;
3378 	size_t dlen = 0;
3379 	size_t tlen = 0;
3380 
3381 	res = tee_ta_get_current_session(&sess);
3382 	if (res != TEE_SUCCESS)
3383 		return res;
3384 
3385 	uctx = &to_user_ta_ctx(sess->ctx)->uctx;
3386 
3387 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
3388 	if (res != TEE_SUCCESS)
3389 		return res;
3390 
3391 	if (cs->state != CRYP_STATE_INITIALIZED)
3392 		return TEE_ERROR_BAD_STATE;
3393 
3394 	if (cs->mode != TEE_MODE_ENCRYPT)
3395 		return TEE_ERROR_BAD_PARAMETERS;
3396 
3397 	if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE)
3398 		return TEE_ERROR_BAD_STATE;
3399 
3400 	res = tee_mmu_check_access_rights(uctx,
3401 					  TEE_MEMORY_ACCESS_READ |
3402 					  TEE_MEMORY_ACCESS_ANY_OWNER,
3403 					  (uaddr_t)src_data, src_len);
3404 	if (res != TEE_SUCCESS)
3405 		return res;
3406 
3407 	if (!dst_len) {
3408 		dlen = 0;
3409 	} else {
3410 		res = get_user_u64_as_size_t(&dlen, dst_len);
3411 		if (res != TEE_SUCCESS)
3412 			return res;
3413 
3414 		res = tee_mmu_check_access_rights(uctx,
3415 						  TEE_MEMORY_ACCESS_READ |
3416 						  TEE_MEMORY_ACCESS_WRITE |
3417 						  TEE_MEMORY_ACCESS_ANY_OWNER,
3418 						  (uaddr_t)dst_data, dlen);
3419 		if (res != TEE_SUCCESS)
3420 			return res;
3421 	}
3422 
3423 	if (dlen < src_len) {
3424 		res = TEE_ERROR_SHORT_BUFFER;
3425 		goto out;
3426 	}
3427 
3428 	res = get_user_u64_as_size_t(&tlen, tag_len);
3429 	if (res != TEE_SUCCESS)
3430 		return res;
3431 
3432 	res = tee_mmu_check_access_rights(uctx,
3433 					  TEE_MEMORY_ACCESS_READ |
3434 					  TEE_MEMORY_ACCESS_WRITE |
3435 					  TEE_MEMORY_ACCESS_ANY_OWNER,
3436 					  (uaddr_t)tag, tlen);
3437 	if (res != TEE_SUCCESS)
3438 		return res;
3439 
3440 	res = crypto_authenc_enc_final(cs->ctx, src_data, src_len, dst_data,
3441 				       &dlen, tag, &tlen);
3442 
3443 out:
3444 	if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) {
3445 		TEE_Result res2 = TEE_SUCCESS;
3446 
3447 		if (dst_len != NULL) {
3448 			res2 = put_user_u64(dst_len, dlen);
3449 			if (res2 != TEE_SUCCESS)
3450 				return res2;
3451 		}
3452 
3453 		res2 = put_user_u64(tag_len, tlen);
3454 		if (res2 != TEE_SUCCESS)
3455 			return res2;
3456 	}
3457 
3458 	return res;
3459 }
3460 
3461 TEE_Result syscall_authenc_dec_final(unsigned long state,
3462 			const void *src_data, size_t src_len, void *dst_data,
3463 			uint64_t *dst_len, const void *tag, size_t tag_len)
3464 {
3465 	struct tee_ta_session *sess = NULL;
3466 	struct user_mode_ctx *uctx = NULL;
3467 	struct tee_cryp_state *cs = NULL;
3468 	TEE_Result res = TEE_SUCCESS;
3469 	size_t dlen = 0;
3470 
3471 	res = tee_ta_get_current_session(&sess);
3472 	if (res != TEE_SUCCESS)
3473 		return res;
3474 
3475 	uctx = &to_user_ta_ctx(sess->ctx)->uctx;
3476 
3477 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
3478 	if (res != TEE_SUCCESS)
3479 		return res;
3480 
3481 	if (cs->state != CRYP_STATE_INITIALIZED)
3482 		return TEE_ERROR_BAD_STATE;
3483 
3484 	if (cs->mode != TEE_MODE_DECRYPT)
3485 		return TEE_ERROR_BAD_PARAMETERS;
3486 
3487 	if (TEE_ALG_GET_CLASS(cs->algo) != TEE_OPERATION_AE)
3488 		return TEE_ERROR_BAD_STATE;
3489 
3490 	res = tee_mmu_check_access_rights(uctx,
3491 					  TEE_MEMORY_ACCESS_READ |
3492 					  TEE_MEMORY_ACCESS_ANY_OWNER,
3493 					  (uaddr_t)src_data, src_len);
3494 	if (res != TEE_SUCCESS)
3495 		return res;
3496 
3497 	if (!dst_len) {
3498 		dlen = 0;
3499 	} else {
3500 		res = get_user_u64_as_size_t(&dlen, dst_len);
3501 		if (res != TEE_SUCCESS)
3502 			return res;
3503 
3504 		res = tee_mmu_check_access_rights(uctx,
3505 						  TEE_MEMORY_ACCESS_READ |
3506 						  TEE_MEMORY_ACCESS_WRITE |
3507 						  TEE_MEMORY_ACCESS_ANY_OWNER,
3508 						  (uaddr_t)dst_data, dlen);
3509 		if (res != TEE_SUCCESS)
3510 			return res;
3511 	}
3512 
3513 	if (dlen < src_len) {
3514 		res = TEE_ERROR_SHORT_BUFFER;
3515 		goto out;
3516 	}
3517 
3518 	res = tee_mmu_check_access_rights(uctx, TEE_MEMORY_ACCESS_READ,
3519 					  (uaddr_t)tag, tag_len);
3520 	if (res != TEE_SUCCESS)
3521 		return res;
3522 
3523 	res = crypto_authenc_dec_final(cs->ctx, src_data, src_len, dst_data,
3524 				       &dlen, tag, tag_len);
3525 
3526 out:
3527 	if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) &&
3528 	    dst_len != NULL) {
3529 		TEE_Result res2 = put_user_u64(dst_len, dlen);
3530 
3531 		if (res2 != TEE_SUCCESS)
3532 			return res2;
3533 	}
3534 
3535 	return res;
3536 }
3537 
3538 static int pkcs1_get_salt_len(const TEE_Attribute *params, uint32_t num_params,
3539 			      size_t default_len)
3540 {
3541 	size_t n;
3542 
3543 	assert(default_len < INT_MAX);
3544 
3545 	for (n = 0; n < num_params; n++) {
3546 		if (params[n].attributeID == TEE_ATTR_RSA_PSS_SALT_LENGTH) {
3547 			if (params[n].content.value.a < INT_MAX)
3548 				return params[n].content.value.a;
3549 			break;
3550 		}
3551 	}
3552 	/*
3553 	 * If salt length isn't provided use the default value which is
3554 	 * the length of the digest.
3555 	 */
3556 	return default_len;
3557 }
3558 
3559 TEE_Result syscall_asymm_operate(unsigned long state,
3560 			const struct utee_attribute *usr_params,
3561 			size_t num_params, const void *src_data, size_t src_len,
3562 			void *dst_data, uint64_t *dst_len)
3563 {
3564 	TEE_Result res;
3565 	struct tee_cryp_state *cs;
3566 	struct tee_ta_session *sess;
3567 	size_t dlen;
3568 	struct tee_obj *o;
3569 	void *label = NULL;
3570 	size_t label_len = 0;
3571 	size_t n;
3572 	int salt_len;
3573 	TEE_Attribute *params = NULL;
3574 	struct user_ta_ctx *utc;
3575 
3576 	res = tee_ta_get_current_session(&sess);
3577 	if (res != TEE_SUCCESS)
3578 		return res;
3579 	utc = to_user_ta_ctx(sess->ctx);
3580 
3581 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
3582 	if (res != TEE_SUCCESS)
3583 		return res;
3584 
3585 	res = tee_mmu_check_access_rights(&utc->uctx,
3586 					  TEE_MEMORY_ACCESS_READ |
3587 					  TEE_MEMORY_ACCESS_ANY_OWNER,
3588 					  (uaddr_t)src_data, src_len);
3589 	if (res != TEE_SUCCESS)
3590 		return res;
3591 
3592 	res = get_user_u64_as_size_t(&dlen, dst_len);
3593 	if (res != TEE_SUCCESS)
3594 		return res;
3595 
3596 	res = tee_mmu_check_access_rights(&utc->uctx,
3597 					  TEE_MEMORY_ACCESS_READ |
3598 					  TEE_MEMORY_ACCESS_WRITE |
3599 					  TEE_MEMORY_ACCESS_ANY_OWNER,
3600 					  (uaddr_t)dst_data, dlen);
3601 	if (res != TEE_SUCCESS)
3602 		return res;
3603 
3604 	size_t alloc_size = 0;
3605 
3606 	if (MUL_OVERFLOW(sizeof(TEE_Attribute), num_params, &alloc_size))
3607 		return TEE_ERROR_OVERFLOW;
3608 
3609 	params = malloc(alloc_size);
3610 	if (!params)
3611 		return TEE_ERROR_OUT_OF_MEMORY;
3612 	res = copy_in_attrs(utc, usr_params, num_params, params);
3613 	if (res != TEE_SUCCESS)
3614 		goto out;
3615 
3616 	res = tee_obj_get(utc, cs->key1, &o);
3617 	if (res != TEE_SUCCESS)
3618 		goto out;
3619 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
3620 		res = TEE_ERROR_GENERIC;
3621 		goto out;
3622 	}
3623 
3624 	switch (cs->algo) {
3625 	case TEE_ALG_RSA_NOPAD:
3626 		if (cs->mode == TEE_MODE_ENCRYPT) {
3627 			res = crypto_acipher_rsanopad_encrypt(o->attr, src_data,
3628 							      src_len, dst_data,
3629 							      &dlen);
3630 		} else if (cs->mode == TEE_MODE_DECRYPT) {
3631 			res = crypto_acipher_rsanopad_decrypt(o->attr, src_data,
3632 							      src_len, dst_data,
3633 							      &dlen);
3634 		} else {
3635 			/*
3636 			 * We will panic because "the mode is not compatible
3637 			 * with the function"
3638 			 */
3639 			res = TEE_ERROR_GENERIC;
3640 		}
3641 		break;
3642 
3643 	case TEE_ALG_SM2_PKE:
3644 		if (cs->mode == TEE_MODE_ENCRYPT) {
3645 			res = crypto_acipher_sm2_pke_encrypt(o->attr, src_data,
3646 							     src_len, dst_data,
3647 							     &dlen);
3648 		} else if (cs->mode == TEE_MODE_DECRYPT) {
3649 			res = crypto_acipher_sm2_pke_decrypt(o->attr, src_data,
3650 							     src_len, dst_data,
3651 							     &dlen);
3652 		} else {
3653 			res = TEE_ERROR_GENERIC;
3654 		}
3655 		break;
3656 
3657 	case TEE_ALG_RSAES_PKCS1_V1_5:
3658 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
3659 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
3660 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
3661 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
3662 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
3663 		for (n = 0; n < num_params; n++) {
3664 			if (params[n].attributeID == TEE_ATTR_RSA_OAEP_LABEL) {
3665 				label = params[n].content.ref.buffer;
3666 				label_len = params[n].content.ref.length;
3667 				break;
3668 			}
3669 		}
3670 
3671 		if (cs->mode == TEE_MODE_ENCRYPT) {
3672 			res = crypto_acipher_rsaes_encrypt(cs->algo, o->attr,
3673 							   label, label_len,
3674 							   src_data, src_len,
3675 							   dst_data, &dlen);
3676 		} else if (cs->mode == TEE_MODE_DECRYPT) {
3677 			res = crypto_acipher_rsaes_decrypt(
3678 					cs->algo, o->attr, label, label_len,
3679 					src_data, src_len, dst_data, &dlen);
3680 		} else {
3681 			res = TEE_ERROR_BAD_PARAMETERS;
3682 		}
3683 		break;
3684 
3685 #if defined(CFG_CRYPTO_RSASSA_NA1)
3686 	case TEE_ALG_RSASSA_PKCS1_V1_5:
3687 #endif
3688 	case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
3689 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
3690 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
3691 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
3692 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
3693 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
3694 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
3695 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
3696 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
3697 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
3698 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
3699 		if (cs->mode != TEE_MODE_SIGN) {
3700 			res = TEE_ERROR_BAD_PARAMETERS;
3701 			break;
3702 		}
3703 		salt_len = pkcs1_get_salt_len(params, num_params, src_len);
3704 		res = crypto_acipher_rsassa_sign(cs->algo, o->attr, salt_len,
3705 						 src_data, src_len, dst_data,
3706 						 &dlen);
3707 		break;
3708 
3709 	case TEE_ALG_DSA_SHA1:
3710 	case TEE_ALG_DSA_SHA224:
3711 	case TEE_ALG_DSA_SHA256:
3712 		res = crypto_acipher_dsa_sign(cs->algo, o->attr, src_data,
3713 					      src_len, dst_data, &dlen);
3714 		break;
3715 	case TEE_ALG_ECDSA_P192:
3716 	case TEE_ALG_ECDSA_P224:
3717 	case TEE_ALG_ECDSA_P256:
3718 	case TEE_ALG_ECDSA_P384:
3719 	case TEE_ALG_ECDSA_P521:
3720 		res = crypto_acipher_ecc_sign(cs->algo, o->attr, src_data,
3721 					      src_len, dst_data, &dlen);
3722 		break;
3723 	case TEE_ALG_SM2_DSA_SM3:
3724 		res = crypto_acipher_sm2_dsa_sign(cs->algo, o->attr, src_data,
3725 						  src_len, dst_data, &dlen);
3726 		break;
3727 
3728 	default:
3729 		res = TEE_ERROR_BAD_PARAMETERS;
3730 		break;
3731 	}
3732 
3733 out:
3734 	free_wipe(params);
3735 
3736 	if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) {
3737 		TEE_Result res2 = put_user_u64(dst_len, dlen);
3738 
3739 		if (res2 != TEE_SUCCESS)
3740 			return res2;
3741 	}
3742 
3743 	return res;
3744 }
3745 
3746 TEE_Result syscall_asymm_verify(unsigned long state,
3747 			const struct utee_attribute *usr_params,
3748 			size_t num_params, const void *data, size_t data_len,
3749 			const void *sig, size_t sig_len)
3750 {
3751 	struct tee_ta_session *sess = NULL;
3752 	struct tee_cryp_state *cs = NULL;
3753 	struct user_ta_ctx *utc = NULL;
3754 	TEE_Result res = TEE_SUCCESS;
3755 	TEE_Attribute *params = NULL;
3756 	struct tee_obj *o = NULL;
3757 	size_t hash_size = 0;
3758 	uint32_t hash_algo = 0;
3759 	int salt_len = 0;
3760 
3761 	res = tee_ta_get_current_session(&sess);
3762 	if (res != TEE_SUCCESS)
3763 		return res;
3764 	utc = to_user_ta_ctx(sess->ctx);
3765 
3766 	res = tee_svc_cryp_get_state(sess, uref_to_vaddr(state), &cs);
3767 	if (res != TEE_SUCCESS)
3768 		return res;
3769 
3770 	if (cs->mode != TEE_MODE_VERIFY)
3771 		return TEE_ERROR_BAD_PARAMETERS;
3772 
3773 	res = tee_mmu_check_access_rights(&utc->uctx,
3774 					  TEE_MEMORY_ACCESS_READ |
3775 					  TEE_MEMORY_ACCESS_ANY_OWNER,
3776 					  (uaddr_t)data, data_len);
3777 	if (res != TEE_SUCCESS)
3778 		return res;
3779 
3780 	res = tee_mmu_check_access_rights(&utc->uctx,
3781 					  TEE_MEMORY_ACCESS_READ |
3782 					  TEE_MEMORY_ACCESS_ANY_OWNER,
3783 					  (uaddr_t)sig, sig_len);
3784 	if (res != TEE_SUCCESS)
3785 		return res;
3786 
3787 	size_t alloc_size = 0;
3788 
3789 	if (MUL_OVERFLOW(sizeof(TEE_Attribute), num_params, &alloc_size))
3790 		return TEE_ERROR_OVERFLOW;
3791 
3792 	params = malloc(alloc_size);
3793 	if (!params)
3794 		return TEE_ERROR_OUT_OF_MEMORY;
3795 	res = copy_in_attrs(utc, usr_params, num_params, params);
3796 	if (res != TEE_SUCCESS)
3797 		goto out;
3798 
3799 	res = tee_obj_get(utc, cs->key1, &o);
3800 	if (res != TEE_SUCCESS)
3801 		goto out;
3802 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0) {
3803 		res = TEE_ERROR_BAD_PARAMETERS;
3804 		goto out;
3805 	}
3806 
3807 	switch (TEE_ALG_GET_MAIN_ALG(cs->algo)) {
3808 	case TEE_MAIN_ALGO_RSA:
3809 		if (cs->algo != TEE_ALG_RSASSA_PKCS1_V1_5) {
3810 			hash_algo = TEE_DIGEST_HASH_TO_ALGO(cs->algo);
3811 			res = tee_alg_get_digest_size(hash_algo, &hash_size);
3812 			if (res != TEE_SUCCESS)
3813 				break;
3814 			if (data_len != hash_size) {
3815 				res = TEE_ERROR_BAD_PARAMETERS;
3816 				break;
3817 			}
3818 			salt_len = pkcs1_get_salt_len(params, num_params,
3819 						      hash_size);
3820 		}
3821 		res = crypto_acipher_rsassa_verify(cs->algo, o->attr, salt_len,
3822 						   data, data_len, sig,
3823 						   sig_len);
3824 		break;
3825 
3826 	case TEE_MAIN_ALGO_DSA:
3827 		hash_algo = TEE_DIGEST_HASH_TO_ALGO(cs->algo);
3828 		res = tee_alg_get_digest_size(hash_algo, &hash_size);
3829 		if (res != TEE_SUCCESS)
3830 			break;
3831 		/*
3832 		 * Depending on the DSA algorithm (NIST), the digital signature
3833 		 * output size may be truncated to the size of a key pair
3834 		 * (Q prime size). Q prime size must be less or equal than the
3835 		 * hash output length of the hash algorithm involved.
3836 		 */
3837 		if (data_len > hash_size) {
3838 			res = TEE_ERROR_BAD_PARAMETERS;
3839 			break;
3840 		}
3841 		res = crypto_acipher_dsa_verify(cs->algo, o->attr, data,
3842 						data_len, sig, sig_len);
3843 		break;
3844 
3845 	case TEE_MAIN_ALGO_ECDSA:
3846 		res = crypto_acipher_ecc_verify(cs->algo, o->attr, data,
3847 						data_len, sig, sig_len);
3848 		break;
3849 
3850 	case TEE_MAIN_ALGO_SM2_DSA_SM3:
3851 		res = crypto_acipher_sm2_dsa_verify(cs->algo, o->attr, data,
3852 						    data_len, sig, sig_len);
3853 		break;
3854 
3855 	default:
3856 		res = TEE_ERROR_NOT_SUPPORTED;
3857 	}
3858 
3859 out:
3860 	free_wipe(params);
3861 	return res;
3862 }
3863