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