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