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