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