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