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