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