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