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