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