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