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