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