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