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