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