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