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