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