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