xref: /optee_os/core/tee/tee_svc_cryp.c (revision 0bea8609906caec2e0af1a2115cc783b8a910b38)
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(struct tee_ta_session *sess,
971 						  uint16_t conv_func,
972 						  const TEE_Attribute *attr,
973 						  void *data, size_t data_size)
974 {
975 	TEE_Result res;
976 	struct tee_cryp_obj_secret *obj;
977 	struct bignum *bn;
978 
979 	if (!attr)
980 		return TEE_ERROR_BAD_STATE;
981 
982 	if (conv_func != TEE_TYPE_CONV_FUNC_VALUE && !attr->content.ref.buffer)
983 		return TEE_ERROR_BAD_PARAMETERS;
984 
985 	switch (conv_func) {
986 	case TEE_TYPE_CONV_FUNC_NONE:
987 		/* No conversion data size has to match exactly */
988 		if (attr->content.ref.length != data_size)
989 			return TEE_ERROR_BAD_PARAMETERS;
990 		return tee_svc_copy_from_user(sess, data,
991 					      attr->content.ref.buffer,
992 					      data_size);
993 	case TEE_TYPE_CONV_FUNC_SECRET:
994 		if (!TEE_ALIGNMENT_IS_OK(data, struct tee_cryp_obj_secret))
995 			return TEE_ERROR_BAD_STATE;
996 		obj = (struct tee_cryp_obj_secret *)(void *)data;
997 
998 		/* Data size has to fit in allocated buffer */
999 		if (attr->content.ref.length >
1000 		    (data_size - sizeof(struct tee_cryp_obj_secret)))
1001 			return TEE_ERROR_BAD_PARAMETERS;
1002 
1003 		res = tee_svc_copy_from_user(sess, obj + 1,
1004 					     attr->content.ref.buffer,
1005 					     attr->content.ref.length);
1006 		if (res == TEE_SUCCESS)
1007 			obj->key_size = attr->content.ref.length;
1008 		return res;
1009 
1010 	case TEE_TYPE_CONV_FUNC_BIGNUM:
1011 		/* Check data can be accessed */
1012 		res = tee_mmu_check_access_rights(
1013 			sess->ctx,
1014 			TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER,
1015 			(tee_uaddr_t)attr->content.ref.buffer,
1016 			attr->content.ref.length);
1017 		if (res != TEE_SUCCESS)
1018 			return res;
1019 
1020 		/*
1021 		 * Read the array of bytes (stored in attr->content.ref.buffer)
1022 		 * and convert it to a bignum (pointed to by data)
1023 		 */
1024 		bn = *(struct bignum **)data;
1025 		if (!crypto_ops.bignum.bin2bn)
1026 			return TEE_ERROR_NOT_IMPLEMENTED;
1027 		res = crypto_ops.bignum.bin2bn(attr->content.ref.buffer,
1028 					       attr->content.ref.length,
1029 					       bn);
1030 		return res;
1031 
1032 	case TEE_TYPE_CONV_FUNC_VALUE:
1033 		/*
1034 		 * a value attribute consists of two uint32 but have not
1035 		 * seen anything that actaully would need that so this fills
1036 		 * the data from the first value and discards the second value
1037 		 */
1038 		*(uint32_t *)data = attr->content.value.a;
1039 
1040 		return TEE_SUCCESS;
1041 
1042 	default:
1043 		return TEE_ERROR_BAD_STATE;
1044 	}
1045 }
1046 
1047 enum attr_usage {
1048 	ATTR_USAGE_POPULATE,
1049 	ATTR_USAGE_GENERATE_KEY
1050 };
1051 
1052 static TEE_Result tee_svc_cryp_check_attr(
1053 		enum attr_usage usage,
1054 		const struct tee_cryp_obj_type_props *type_props,
1055 		TEE_Attribute *attrs,
1056 		uint32_t attr_count)
1057 {
1058 	uint32_t required_flag;
1059 	uint32_t opt_flag;
1060 	bool all_opt_needed;
1061 	uint32_t req_attrs = 0;
1062 	uint32_t opt_grp_attrs = 0;
1063 	uint32_t attrs_found = 0;
1064 	size_t n;
1065 
1066 	if (usage == ATTR_USAGE_POPULATE) {
1067 		required_flag = TEE_TYPE_ATTR_REQUIRED;
1068 		opt_flag = TEE_TYPE_ATTR_OPTIONAL_GROUP;
1069 		all_opt_needed = true;
1070 	} else {
1071 		required_flag = TEE_TYPE_ATTR_GEN_KEY_REQ;
1072 		opt_flag = TEE_TYPE_ATTR_GEN_KEY_OPT;
1073 		all_opt_needed = false;
1074 	}
1075 
1076 	/*
1077 	 * First find out which attributes are required and which belong to
1078 	 * the optional group
1079 	 */
1080 	for (n = 0; n < type_props->num_type_attrs; n++) {
1081 		uint32_t bit = 1 << n;
1082 		uint32_t flags = type_props->type_attrs[n].flags;
1083 
1084 		if (flags & required_flag)
1085 			req_attrs |= bit;
1086 		else if (flags & opt_flag)
1087 			opt_grp_attrs |= bit;
1088 	}
1089 
1090 	/*
1091 	 * Verify that all required attributes are in place and
1092 	 * that the same attribute isn't repeated.
1093 	 */
1094 	for (n = 0; n < attr_count; n++) {
1095 		int idx =
1096 		    tee_svc_cryp_obj_find_type_attr_idx(attrs[n].attributeID,
1097 							type_props);
1098 		if (idx >= 0) {
1099 			uint32_t bit = 1 << idx;
1100 
1101 			if ((attrs_found & bit) != 0)
1102 				return TEE_ERROR_ITEM_NOT_FOUND;
1103 
1104 			attrs_found |= bit;
1105 		}
1106 	}
1107 	/* Required attribute missing */
1108 	if ((attrs_found & req_attrs) != req_attrs)
1109 		return TEE_ERROR_ITEM_NOT_FOUND;
1110 
1111 	/*
1112 	 * If the flag says that "if one of the optional attributes are included
1113 	 * all of them has to be included" this must be checked.
1114 	 */
1115 	if (all_opt_needed && (attrs_found & opt_grp_attrs) != 0 &&
1116 	    (attrs_found & opt_grp_attrs) != opt_grp_attrs)
1117 		return TEE_ERROR_ITEM_NOT_FOUND;
1118 
1119 	return TEE_SUCCESS;
1120 }
1121 
1122 static TEE_Result tee_svc_cryp_obj_populate_type(
1123 		struct tee_ta_session *sess,
1124 		struct tee_obj *o,
1125 		const struct tee_cryp_obj_type_props *type_props,
1126 		const TEE_Attribute *attrs,
1127 		uint32_t attr_count)
1128 {
1129 	TEE_Result res;
1130 	uint32_t have_attrs = 0;
1131 	size_t obj_size = 0;
1132 	size_t n;
1133 
1134 	for (n = 0; n < attr_count; n++) {
1135 		size_t raw_size;
1136 		void *raw_data;
1137 		int idx =
1138 		    tee_svc_cryp_obj_find_type_attr_idx(attrs[n].attributeID,
1139 							type_props);
1140 		if (idx < 0)
1141 			continue;
1142 
1143 		have_attrs |= 1 << idx;
1144 
1145 		res = tee_svc_cryp_obj_get_raw_data(o, type_props, idx,
1146 						    &raw_data, &raw_size);
1147 		if (res != TEE_SUCCESS)
1148 			return res;
1149 
1150 		res =
1151 		    tee_svc_cryp_obj_store_attr_raw(
1152 			    sess, type_props->type_attrs[idx].conv_func,
1153 			    attrs + n, raw_data, raw_size);
1154 		if (res != TEE_SUCCESS)
1155 			return res;
1156 
1157 		/*
1158 		 * First attr_idx signifies the attribute that gives the size
1159 		 * of the object
1160 		 */
1161 		if (type_props->type_attrs[idx].flags &
1162 		    TEE_TYPE_ATTR_SIZE_INDICATOR) {
1163 			obj_size += attrs[n].content.ref.length * 8;
1164 		}
1165 	}
1166 
1167 	/*
1168 	 * We have to do it like this because the parity bits aren't counted
1169 	 * when telling the size of the key in bits.
1170 	 */
1171 	if (o->info.objectType == TEE_TYPE_DES ||
1172 	    o->info.objectType == TEE_TYPE_DES3)
1173 		obj_size -= obj_size / 8; /* Exclude parity in size of key */
1174 
1175 	o->have_attrs = have_attrs;
1176 	o->info.objectSize = obj_size;
1177 	return TEE_SUCCESS;
1178 }
1179 
1180 TEE_Result tee_svc_cryp_obj_populate(uint32_t obj, TEE_Attribute *attrs,
1181 				     uint32_t attr_count)
1182 {
1183 	TEE_Result res;
1184 	struct tee_ta_session *sess;
1185 	struct tee_obj *o;
1186 	const struct tee_cryp_obj_type_props *type_props;
1187 
1188 	res = tee_ta_get_current_session(&sess);
1189 	if (res != TEE_SUCCESS)
1190 		return res;
1191 
1192 	res = tee_obj_get(sess->ctx, obj, &o);
1193 	if (res != TEE_SUCCESS)
1194 		return res;
1195 
1196 	/* Must be a transient object */
1197 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
1198 		return TEE_ERROR_BAD_PARAMETERS;
1199 
1200 	/* Must not be initialized already */
1201 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1202 		return TEE_ERROR_BAD_PARAMETERS;
1203 
1204 	type_props = tee_svc_find_type_props(o->info.objectType);
1205 	if (!type_props)
1206 		return TEE_ERROR_NOT_IMPLEMENTED;
1207 
1208 	res = tee_svc_cryp_check_attr(ATTR_USAGE_POPULATE, type_props, attrs,
1209 				      attr_count);
1210 	if (res != TEE_SUCCESS)
1211 		return res;
1212 
1213 	res = tee_svc_cryp_obj_populate_type(sess, o, type_props, attrs,
1214 					     attr_count);
1215 	if (res == TEE_SUCCESS)
1216 		o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
1217 
1218 	return res;
1219 }
1220 
1221 TEE_Result tee_svc_cryp_obj_copy(uint32_t dst, uint32_t src)
1222 {
1223 	TEE_Result res;
1224 	struct tee_ta_session *sess;
1225 	struct tee_obj *dst_o;
1226 	struct tee_obj *src_o;
1227 
1228 	res = tee_ta_get_current_session(&sess);
1229 	if (res != TEE_SUCCESS)
1230 		return res;
1231 
1232 	res = tee_obj_get(sess->ctx, dst, &dst_o);
1233 	if (res != TEE_SUCCESS)
1234 		return res;
1235 
1236 	res = tee_obj_get(sess->ctx, src, &src_o);
1237 	if (res != TEE_SUCCESS)
1238 		return res;
1239 
1240 	if ((src_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
1241 		return TEE_ERROR_BAD_PARAMETERS;
1242 	if ((dst_o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
1243 		return TEE_ERROR_BAD_PARAMETERS;
1244 	if ((dst_o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1245 		return TEE_ERROR_BAD_PARAMETERS;
1246 
1247 	if (dst_o->info.objectType == src_o->info.objectType) {
1248 		/* Copy whole data */
1249 
1250 		if (dst_o->data_size != src_o->data_size)
1251 			return TEE_ERROR_BAD_STATE;
1252 		if (dst_o->cleanup != src_o->cleanup)
1253 			return TEE_ERROR_BAD_STATE;
1254 
1255 		dst_o->have_attrs = src_o->have_attrs;
1256 
1257 		switch (src_o->info.objectType) {
1258 		case TEE_TYPE_RSA_PUBLIC_KEY:
1259 			copy_rsa_public_key(dst_o->data, src_o->data);
1260 			break;
1261 		case TEE_TYPE_RSA_KEYPAIR:
1262 			copy_rsa_keypair(dst_o->data, src_o->data);
1263 			break;
1264 		case TEE_TYPE_DSA_PUBLIC_KEY:
1265 			copy_dsa_public_key(dst_o->data, src_o->data);
1266 			break;
1267 		case TEE_TYPE_DSA_KEYPAIR:
1268 			copy_dsa_keypair(dst_o->data, src_o->data);
1269 			break;
1270 		case TEE_TYPE_DH_KEYPAIR:
1271 			copy_dh_keypair(dst_o->data, src_o->data);
1272 			break;
1273 		default:
1274 			/* Generic case */
1275 			memcpy(dst_o->data, src_o->data, src_o->data_size);
1276 		}
1277 	} else if (dst_o->info.objectType == TEE_TYPE_RSA_PUBLIC_KEY &&
1278 		   src_o->info.objectType == TEE_TYPE_RSA_KEYPAIR) {
1279 		/* Extract public key from RSA key pair */
1280 		size_t n;
1281 
1282 		extract_rsa_public_key(dst_o->data, src_o->data);
1283 		dst_o->have_attrs = 0;
1284 		for (n = 0; n < TEE_ARRAY_SIZE(tee_cryp_obj_rsa_pub_key_attrs);
1285 		     n++)
1286 			dst_o->have_attrs |= 1 << n;
1287 
1288 	} else if (dst_o->info.objectType == TEE_TYPE_DSA_PUBLIC_KEY &&
1289 		   src_o->info.objectType == TEE_TYPE_DSA_KEYPAIR) {
1290 		/* Extract public key from DSA key pair */
1291 		size_t n;
1292 
1293 		extract_dsa_public_key(dst_o->data, src_o->data);
1294 		dst_o->have_attrs = 0;
1295 		for (n = 0; n < TEE_ARRAY_SIZE(tee_cryp_obj_dsa_pub_key_attrs);
1296 		     n++)
1297 			dst_o->have_attrs |= 1 << n;
1298 
1299 	} else {
1300 		return TEE_ERROR_BAD_PARAMETERS;
1301 	}
1302 
1303 	dst_o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
1304 	dst_o->info.objectSize = src_o->info.objectSize;
1305 	dst_o->info.objectUsage = src_o->info.objectUsage;
1306 	return TEE_SUCCESS;
1307 }
1308 
1309 static TEE_Result tee_svc_obj_generate_key_rsa(
1310 	struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props,
1311 	uint32_t key_size)
1312 {
1313 	TEE_Result res;
1314 	struct rsa_keypair *key = o->data;
1315 	uint32_t e = TEE_U32_TO_BIG_ENDIAN(65537);
1316 
1317 	TEE_ASSERT(sizeof(struct rsa_keypair) == o->data_size);
1318 	if (!crypto_ops.acipher.gen_rsa_key || !crypto_ops.bignum.bin2bn)
1319 		return TEE_ERROR_NOT_IMPLEMENTED;
1320 	if (!GET_ATTRIBUTE(o, type_props, TEE_ATTR_RSA_PUBLIC_EXPONENT))
1321 		crypto_ops.bignum.bin2bn((const uint8_t *)&e, sizeof(e),
1322 					 key->e);
1323 	res = crypto_ops.acipher.gen_rsa_key(o->data, key_size);
1324 	if (res != TEE_SUCCESS)
1325 		return res;
1326 
1327 	/* Set bits for all known attributes for this object type */
1328 	o->have_attrs = (1 << type_props->num_type_attrs) - 1;
1329 
1330 	return TEE_SUCCESS;
1331 }
1332 
1333 static TEE_Result tee_svc_obj_generate_key_dsa(
1334 	struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props,
1335 	uint32_t key_size)
1336 {
1337 	TEE_Result res;
1338 
1339 	TEE_ASSERT(sizeof(struct dsa_keypair) == o->data_size);
1340 	if (!crypto_ops.acipher.gen_dsa_key)
1341 		return TEE_ERROR_NOT_IMPLEMENTED;
1342 	res = crypto_ops.acipher.gen_dsa_key(o->data, key_size);
1343 	if (res != TEE_SUCCESS)
1344 		return res;
1345 
1346 	/* Set bits for all known attributes for this object type */
1347 	o->have_attrs = (1 << type_props->num_type_attrs) - 1;
1348 
1349 	return TEE_SUCCESS;
1350 }
1351 
1352 static TEE_Result tee_svc_obj_generate_key_dh(
1353 	struct tee_ta_session *sess,
1354 	struct tee_obj *o, const struct tee_cryp_obj_type_props *type_props,
1355 	uint32_t key_size __unused,
1356 	const TEE_Attribute *params, uint32_t param_count)
1357 {
1358 	TEE_Result res;
1359 	struct dh_keypair *tee_dh_key;
1360 	struct bignum *dh_q = NULL;
1361 	uint32_t dh_xbits = 0;
1362 
1363 	TEE_ASSERT(sizeof(struct dh_keypair) == o->data_size);
1364 
1365 	/* Copy the present attributes into the obj before starting */
1366 	res = tee_svc_cryp_obj_populate_type(
1367 			sess, o, type_props, params, param_count);
1368 	if (res != TEE_SUCCESS)
1369 		return res;
1370 
1371 	tee_dh_key = (struct dh_keypair *)o->data;
1372 
1373 	if (GET_ATTRIBUTE(o, type_props, TEE_ATTR_DH_SUBPRIME))
1374 		dh_q = tee_dh_key->q;
1375 	if (GET_ATTRIBUTE(o, type_props, TEE_ATTR_DH_X_BITS))
1376 		dh_xbits = tee_dh_key->xbits;
1377 	if (!crypto_ops.acipher.gen_dh_key)
1378 		return TEE_ERROR_NOT_IMPLEMENTED;
1379 	res = crypto_ops.acipher.gen_dh_key(tee_dh_key, dh_q, dh_xbits);
1380 	if (res != TEE_SUCCESS)
1381 		return res;
1382 
1383 	/* Set bits for the generated public and private key */
1384 	SET_ATTRIBUTE(o, type_props, TEE_ATTR_DH_PUBLIC_VALUE);
1385 	SET_ATTRIBUTE(o, type_props, TEE_ATTR_DH_PRIVATE_VALUE);
1386 	SET_ATTRIBUTE(o, type_props, TEE_ATTR_DH_X_BITS);
1387 	return TEE_SUCCESS;
1388 }
1389 
1390 TEE_Result tee_svc_obj_generate_key(
1391 	uint32_t obj, uint32_t key_size,
1392 	const TEE_Attribute *params, uint32_t param_count)
1393 {
1394 	TEE_Result res;
1395 	struct tee_ta_session *sess;
1396 	const struct tee_cryp_obj_type_props *type_props;
1397 	struct tee_obj *o;
1398 	struct tee_cryp_obj_secret *key;
1399 	size_t byte_size;
1400 
1401 	res = tee_ta_get_current_session(&sess);
1402 	if (res != TEE_SUCCESS)
1403 		return res;
1404 
1405 	res = tee_obj_get(sess->ctx, obj, &o);
1406 	if (res != TEE_SUCCESS)
1407 		return res;
1408 
1409 	/* Must be a transient object */
1410 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0)
1411 		return TEE_ERROR_BAD_STATE;
1412 
1413 	/* Must not be initialized already */
1414 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != 0)
1415 		return TEE_ERROR_BAD_STATE;
1416 
1417 	/* Find description of object */
1418 	type_props = tee_svc_find_type_props(o->info.objectType);
1419 	if (!type_props)
1420 		return TEE_ERROR_NOT_SUPPORTED;
1421 
1422 	/* Check that maxObjectSize follows restrictions */
1423 	if (key_size % type_props->quanta != 0)
1424 		return TEE_ERROR_NOT_SUPPORTED;
1425 	if (key_size < type_props->min_size)
1426 		return TEE_ERROR_NOT_SUPPORTED;
1427 	if (key_size > type_props->max_size)
1428 		return TEE_ERROR_NOT_SUPPORTED;
1429 
1430 	res = tee_svc_cryp_check_attr(ATTR_USAGE_GENERATE_KEY, type_props,
1431 				      (TEE_Attribute *)params, param_count);
1432 	if (res != TEE_SUCCESS)
1433 		return res;
1434 
1435 	switch (o->info.objectType) {
1436 	case TEE_TYPE_AES:
1437 	case TEE_TYPE_DES:
1438 	case TEE_TYPE_DES3:
1439 	case TEE_TYPE_HMAC_MD5:
1440 	case TEE_TYPE_HMAC_SHA1:
1441 	case TEE_TYPE_HMAC_SHA224:
1442 	case TEE_TYPE_HMAC_SHA256:
1443 	case TEE_TYPE_HMAC_SHA384:
1444 	case TEE_TYPE_HMAC_SHA512:
1445 	case TEE_TYPE_GENERIC_SECRET:
1446 		byte_size = key_size / 8;
1447 
1448 		/*
1449 		 * We have to do it like this because the parity bits aren't
1450 		 * counted when telling the size of the key in bits.
1451 		 */
1452 		if (o->info.objectType == TEE_TYPE_DES ||
1453 		    o->info.objectType == TEE_TYPE_DES3) {
1454 			byte_size = (key_size + key_size / 7) / 8;
1455 		}
1456 
1457 		key = (struct tee_cryp_obj_secret *)o->data;
1458 		if (byte_size > (o->data_size - sizeof(*key)))
1459 			return TEE_ERROR_EXCESS_DATA;
1460 
1461 		res = get_rng_array((void *)(key + 1), byte_size);
1462 		if (res != TEE_SUCCESS)
1463 			return res;
1464 
1465 		/* Force the last bit to have exactly a value on byte_size */
1466 		((char *)key)[sizeof(key->key_size) + byte_size - 1] |= 0x80;
1467 		key->key_size = byte_size;
1468 
1469 		/* Set bits for all known attributes for this object type */
1470 		o->have_attrs = (1 << type_props->num_type_attrs) - 1;
1471 
1472 		break;
1473 
1474 	case TEE_TYPE_RSA_KEYPAIR:
1475 		res = tee_svc_obj_generate_key_rsa(o, type_props, key_size);
1476 		if (res != TEE_SUCCESS)
1477 			return res;
1478 		break;
1479 
1480 	case TEE_TYPE_DSA_KEYPAIR:
1481 		res = tee_svc_obj_generate_key_dsa(o, type_props, key_size);
1482 		if (res != TEE_SUCCESS)
1483 			return res;
1484 		break;
1485 
1486 	case TEE_TYPE_DH_KEYPAIR:
1487 		res = tee_svc_obj_generate_key_dh(
1488 			sess, o, type_props, key_size, params, param_count);
1489 		if (res != TEE_SUCCESS)
1490 			return res;
1491 		break;
1492 
1493 	default:
1494 		return TEE_ERROR_BAD_FORMAT;
1495 	}
1496 
1497 	o->info.objectSize = key_size;
1498 	o->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
1499 	return TEE_SUCCESS;
1500 }
1501 
1502 static TEE_Result tee_svc_cryp_get_state(struct tee_ta_session *sess,
1503 					 uint32_t state_id,
1504 					 struct tee_cryp_state **state)
1505 {
1506 	struct tee_cryp_state *s;
1507 
1508 	TAILQ_FOREACH(s, &sess->ctx->cryp_states, link) {
1509 		if (state_id == (uint32_t) s) {
1510 			*state = s;
1511 			return TEE_SUCCESS;
1512 		}
1513 	}
1514 	return TEE_ERROR_BAD_PARAMETERS;
1515 }
1516 
1517 static void cryp_state_free(struct tee_ta_ctx *ctx, struct tee_cryp_state *cs)
1518 {
1519 	struct tee_obj *o;
1520 
1521 	if (tee_obj_get(ctx, cs->key1, &o) == TEE_SUCCESS)
1522 		tee_obj_close(ctx, o);
1523 	if (tee_obj_get(ctx, cs->key2, &o) == TEE_SUCCESS)
1524 		tee_obj_close(ctx, o);
1525 
1526 	TAILQ_REMOVE(&ctx->cryp_states, cs, link);
1527 	if (cs->ctx_finalize != NULL)
1528 		cs->ctx_finalize(cs->ctx, cs->algo);
1529 	free(cs->ctx);
1530 	free(cs);
1531 }
1532 
1533 static TEE_Result tee_svc_cryp_check_key_type(const struct tee_obj *o,
1534 					      uint32_t algo,
1535 					      TEE_OperationMode mode)
1536 {
1537 	uint32_t req_key_type;
1538 
1539 	switch (TEE_ALG_GET_MAIN_ALG(algo)) {
1540 	case TEE_MAIN_ALGO_MD5:
1541 		req_key_type = TEE_TYPE_HMAC_MD5;
1542 		break;
1543 	case TEE_MAIN_ALGO_SHA1:
1544 		req_key_type = TEE_TYPE_HMAC_SHA1;
1545 		break;
1546 	case TEE_MAIN_ALGO_SHA224:
1547 		req_key_type = TEE_TYPE_HMAC_SHA224;
1548 		break;
1549 	case TEE_MAIN_ALGO_SHA256:
1550 		req_key_type = TEE_TYPE_HMAC_SHA256;
1551 		break;
1552 	case TEE_MAIN_ALGO_SHA384:
1553 		req_key_type = TEE_TYPE_HMAC_SHA384;
1554 		break;
1555 	case TEE_MAIN_ALGO_SHA512:
1556 		req_key_type = TEE_TYPE_HMAC_SHA512;
1557 		break;
1558 	case TEE_MAIN_ALGO_AES:
1559 		req_key_type = TEE_TYPE_AES;
1560 		break;
1561 	case TEE_MAIN_ALGO_DES:
1562 		req_key_type = TEE_TYPE_DES;
1563 		break;
1564 	case TEE_MAIN_ALGO_DES3:
1565 		req_key_type = TEE_TYPE_DES3;
1566 		break;
1567 	case TEE_MAIN_ALGO_RSA:
1568 		if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY)
1569 			req_key_type = TEE_TYPE_RSA_PUBLIC_KEY;
1570 		else
1571 			req_key_type = TEE_TYPE_RSA_KEYPAIR;
1572 		break;
1573 	case TEE_MAIN_ALGO_DSA:
1574 		if (mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY)
1575 			req_key_type = TEE_TYPE_DSA_PUBLIC_KEY;
1576 		else
1577 			req_key_type = TEE_TYPE_DSA_KEYPAIR;
1578 		break;
1579 	case TEE_MAIN_ALGO_DH:
1580 		req_key_type = TEE_TYPE_DH_KEYPAIR;
1581 		break;
1582 #if defined(CFG_CRYPTO_HKDF)
1583 	case TEE_MAIN_ALGO_HKDF:
1584 		req_key_type = TEE_TYPE_HKDF_IKM;
1585 		break;
1586 #endif
1587 #if defined(CFG_CRYPTO_CONCAT_KDF)
1588 	case TEE_MAIN_ALGO_CONCAT_KDF:
1589 		req_key_type = TEE_TYPE_CONCAT_KDF_Z;
1590 		break;
1591 #endif
1592 #if defined(CFG_CRYPTO_PBKDF2)
1593 	case TEE_MAIN_ALGO_PBKDF2:
1594 		req_key_type = TEE_TYPE_PBKDF2_PASSWORD;
1595 		break;
1596 #endif
1597 	default:
1598 		return TEE_ERROR_BAD_PARAMETERS;
1599 	}
1600 
1601 	if (req_key_type != o->info.objectType)
1602 		return TEE_ERROR_BAD_PARAMETERS;
1603 	return TEE_SUCCESS;
1604 }
1605 
1606 TEE_Result tee_svc_cryp_state_alloc(uint32_t algo, uint32_t mode,
1607 				    uint32_t key1, uint32_t key2,
1608 				    uint32_t *state)
1609 {
1610 	TEE_Result res;
1611 	struct tee_cryp_state *cs;
1612 	struct tee_ta_session *sess;
1613 	struct tee_obj *o1 = NULL;
1614 	struct tee_obj *o2 = NULL;
1615 
1616 	res = tee_ta_get_current_session(&sess);
1617 	if (res != TEE_SUCCESS)
1618 		return res;
1619 
1620 	if (key1 != 0) {
1621 		res = tee_obj_get(sess->ctx, key1, &o1);
1622 		if (res != TEE_SUCCESS)
1623 			return res;
1624 		if (o1->busy)
1625 			return TEE_ERROR_BAD_PARAMETERS;
1626 		res = tee_svc_cryp_check_key_type(o1, algo, mode);
1627 		if (res != TEE_SUCCESS)
1628 			return res;
1629 	}
1630 	if (key2 != 0) {
1631 		res = tee_obj_get(sess->ctx, key2, &o2);
1632 		if (res != TEE_SUCCESS)
1633 			return res;
1634 		if (o2->busy)
1635 			return TEE_ERROR_BAD_PARAMETERS;
1636 		res = tee_svc_cryp_check_key_type(o2, algo, mode);
1637 		if (res != TEE_SUCCESS)
1638 			return res;
1639 	}
1640 
1641 	cs = calloc(1, sizeof(struct tee_cryp_state));
1642 	if (!cs)
1643 		return TEE_ERROR_OUT_OF_MEMORY;
1644 	TAILQ_INSERT_TAIL(&sess->ctx->cryp_states, cs, link);
1645 	cs->algo = algo;
1646 	cs->mode = mode;
1647 
1648 	switch (TEE_ALG_GET_CLASS(algo)) {
1649 	case TEE_OPERATION_CIPHER:
1650 		if ((algo == TEE_ALG_AES_XTS && (key1 == 0 || key2 == 0)) ||
1651 		    (algo != TEE_ALG_AES_XTS && (key1 == 0 || key2 != 0))) {
1652 			res = TEE_ERROR_BAD_PARAMETERS;
1653 		} else {
1654 			if (crypto_ops.cipher.get_ctx_size)
1655 				res = crypto_ops.cipher.get_ctx_size(algo,
1656 								&cs->ctx_size);
1657 			else
1658 				res = TEE_ERROR_NOT_IMPLEMENTED;
1659 			if (res != TEE_SUCCESS)
1660 				break;
1661 			cs->ctx = calloc(1, cs->ctx_size);
1662 			if (!cs->ctx)
1663 				res = TEE_ERROR_OUT_OF_MEMORY;
1664 		}
1665 		break;
1666 	case TEE_OPERATION_AE:
1667 		if (key1 == 0 || key2 != 0) {
1668 			res = TEE_ERROR_BAD_PARAMETERS;
1669 		} else {
1670 			if (crypto_ops.authenc.get_ctx_size)
1671 				res = crypto_ops.authenc.get_ctx_size(algo,
1672 								&cs->ctx_size);
1673 			else
1674 				res = TEE_ERROR_NOT_IMPLEMENTED;
1675 			if (res != TEE_SUCCESS)
1676 				break;
1677 			cs->ctx = calloc(1, cs->ctx_size);
1678 			if (!cs->ctx)
1679 				res = TEE_ERROR_OUT_OF_MEMORY;
1680 		}
1681 		break;
1682 	case TEE_OPERATION_MAC:
1683 		if (key1 == 0 || key2 != 0) {
1684 			res = TEE_ERROR_BAD_PARAMETERS;
1685 		} else {
1686 			if (crypto_ops.mac.get_ctx_size)
1687 				res = crypto_ops.mac.get_ctx_size(algo,
1688 								&cs->ctx_size);
1689 			else
1690 				res = TEE_ERROR_NOT_IMPLEMENTED;
1691 			if (res != TEE_SUCCESS)
1692 				break;
1693 			cs->ctx = calloc(1, cs->ctx_size);
1694 			if (!cs->ctx)
1695 				res = TEE_ERROR_OUT_OF_MEMORY;
1696 		}
1697 		break;
1698 	case TEE_OPERATION_DIGEST:
1699 		if (key1 != 0 || key2 != 0) {
1700 			res = TEE_ERROR_BAD_PARAMETERS;
1701 		} else {
1702 			if (crypto_ops.hash.get_ctx_size)
1703 				res = crypto_ops.hash.get_ctx_size(algo,
1704 								&cs->ctx_size);
1705 			else
1706 				res = TEE_ERROR_NOT_IMPLEMENTED;
1707 			if (res != TEE_SUCCESS)
1708 				break;
1709 			cs->ctx = calloc(1, cs->ctx_size);
1710 			if (!cs->ctx)
1711 				res = TEE_ERROR_OUT_OF_MEMORY;
1712 		}
1713 		break;
1714 	case TEE_OPERATION_ASYMMETRIC_CIPHER:
1715 	case TEE_OPERATION_ASYMMETRIC_SIGNATURE:
1716 		if (key1 == 0 || key2 != 0)
1717 			res = TEE_ERROR_BAD_PARAMETERS;
1718 		break;
1719 	case TEE_OPERATION_KEY_DERIVATION:
1720 		if (key1 == 0 || key2 != 0)
1721 			res = TEE_ERROR_BAD_PARAMETERS;
1722 		break;
1723 	default:
1724 		res = TEE_ERROR_NOT_SUPPORTED;
1725 		break;
1726 	}
1727 	if (res != TEE_SUCCESS)
1728 		goto out;
1729 
1730 	res = tee_svc_copy_to_user(sess, state, &cs, sizeof(uint32_t));
1731 	if (res != TEE_SUCCESS)
1732 		goto out;
1733 
1734 	/* Register keys */
1735 	if (o1 != NULL) {
1736 		o1->busy = true;
1737 		cs->key1 = key1;
1738 	}
1739 	if (o2 != NULL) {
1740 		o2->busy = true;
1741 		cs->key2 = key2;
1742 	}
1743 
1744 out:
1745 	if (res != TEE_SUCCESS)
1746 		cryp_state_free(sess->ctx, cs);
1747 	return res;
1748 }
1749 
1750 TEE_Result tee_svc_cryp_state_copy(uint32_t dst, uint32_t src)
1751 {
1752 	TEE_Result res;
1753 	struct tee_cryp_state *cs_dst;
1754 	struct tee_cryp_state *cs_src;
1755 	struct tee_ta_session *sess;
1756 
1757 	res = tee_ta_get_current_session(&sess);
1758 	if (res != TEE_SUCCESS)
1759 		return res;
1760 
1761 	res = tee_svc_cryp_get_state(sess, dst, &cs_dst);
1762 	if (res != TEE_SUCCESS)
1763 		return res;
1764 	res = tee_svc_cryp_get_state(sess, src, &cs_src);
1765 	if (res != TEE_SUCCESS)
1766 		return res;
1767 	if (cs_dst->algo != cs_src->algo || cs_dst->mode != cs_src->mode)
1768 		return TEE_ERROR_BAD_PARAMETERS;
1769 	/* "Can't happen" */
1770 	if (cs_dst->ctx_size != cs_src->ctx_size)
1771 		return TEE_ERROR_BAD_STATE;
1772 
1773 	memcpy(cs_dst->ctx, cs_src->ctx, cs_src->ctx_size);
1774 	return TEE_SUCCESS;
1775 }
1776 
1777 void tee_svc_cryp_free_states(struct tee_ta_ctx *ctx)
1778 {
1779 	struct tee_cryp_state_head *states = &ctx->cryp_states;
1780 
1781 	while (!TAILQ_EMPTY(states))
1782 		cryp_state_free(ctx, TAILQ_FIRST(states));
1783 }
1784 
1785 TEE_Result tee_svc_cryp_state_free(uint32_t state)
1786 {
1787 	TEE_Result res;
1788 	struct tee_cryp_state *cs;
1789 	struct tee_ta_session *sess;
1790 
1791 	res = tee_ta_get_current_session(&sess);
1792 	if (res != TEE_SUCCESS)
1793 		return res;
1794 
1795 	res = tee_svc_cryp_get_state(sess, state, &cs);
1796 	if (res != TEE_SUCCESS)
1797 		return res;
1798 	cryp_state_free(sess->ctx, cs);
1799 	return TEE_SUCCESS;
1800 }
1801 
1802 /* iv and iv_len are ignored for some algorithms */
1803 TEE_Result tee_svc_hash_init(uint32_t state, const void *iv __unused,
1804 		size_t iv_len __unused)
1805 {
1806 	TEE_Result res;
1807 	struct tee_cryp_state *cs;
1808 	struct tee_ta_session *sess;
1809 
1810 	res = tee_ta_get_current_session(&sess);
1811 	if (res != TEE_SUCCESS)
1812 		return res;
1813 
1814 	res = tee_svc_cryp_get_state(sess, state, &cs);
1815 	if (res != TEE_SUCCESS)
1816 		return res;
1817 
1818 	switch (TEE_ALG_GET_CLASS(cs->algo)) {
1819 	case TEE_OPERATION_DIGEST:
1820 		if (!crypto_ops.hash.init)
1821 			return TEE_ERROR_NOT_IMPLEMENTED;
1822 		res = crypto_ops.hash.init(cs->ctx, cs->algo);
1823 		if (res != TEE_SUCCESS)
1824 			return res;
1825 		break;
1826 	case TEE_OPERATION_MAC:
1827 		{
1828 			struct tee_obj *o;
1829 			struct tee_cryp_obj_secret *key;
1830 
1831 			res = tee_obj_get(sess->ctx, cs->key1, &o);
1832 			if (res != TEE_SUCCESS)
1833 				return res;
1834 			if ((o->info.handleFlags &
1835 			     TEE_HANDLE_FLAG_INITIALIZED) == 0)
1836 				return TEE_ERROR_BAD_PARAMETERS;
1837 
1838 			key = (struct tee_cryp_obj_secret *)o->data;
1839 			if (!crypto_ops.mac.init)
1840 				return TEE_ERROR_NOT_IMPLEMENTED;
1841 			res = crypto_ops.mac.init(cs->ctx, cs->algo,
1842 						  (void *)(key + 1),
1843 						  key->key_size);
1844 			if (res != TEE_SUCCESS)
1845 				return res;
1846 			break;
1847 		}
1848 	default:
1849 		return TEE_ERROR_BAD_PARAMETERS;
1850 	}
1851 
1852 	return TEE_SUCCESS;
1853 }
1854 
1855 TEE_Result tee_svc_hash_update(uint32_t state, const void *chunk,
1856 			       size_t chunk_size)
1857 {
1858 	TEE_Result res;
1859 	struct tee_cryp_state *cs;
1860 	struct tee_ta_session *sess;
1861 
1862 	/* No data, but size provided isn't valid parameters. */
1863 	if (!chunk && chunk_size)
1864 		return TEE_ERROR_BAD_PARAMETERS;
1865 
1866 	/* Zero length hash is valid, but nothing we need to do. */
1867 	if (!chunk_size)
1868 		return TEE_SUCCESS;
1869 
1870 	res = tee_ta_get_current_session(&sess);
1871 	if (res != TEE_SUCCESS)
1872 		return res;
1873 
1874 	res = tee_mmu_check_access_rights(sess->ctx,
1875 					  TEE_MEMORY_ACCESS_READ |
1876 					  TEE_MEMORY_ACCESS_ANY_OWNER,
1877 					  (tee_uaddr_t)chunk, chunk_size);
1878 	if (res != TEE_SUCCESS)
1879 		return res;
1880 
1881 	res = tee_svc_cryp_get_state(sess, state, &cs);
1882 	if (res != TEE_SUCCESS)
1883 		return res;
1884 
1885 	switch (TEE_ALG_GET_CLASS(cs->algo)) {
1886 	case TEE_OPERATION_DIGEST:
1887 		if (!crypto_ops.hash.update)
1888 			return TEE_ERROR_NOT_IMPLEMENTED;
1889 		res = crypto_ops.hash.update(cs->ctx, cs->algo, chunk,
1890 					     chunk_size);
1891 		if (res != TEE_SUCCESS)
1892 			return res;
1893 		break;
1894 	case TEE_OPERATION_MAC:
1895 		if (!crypto_ops.mac.update)
1896 			return TEE_ERROR_NOT_IMPLEMENTED;
1897 		res = crypto_ops.mac.update(cs->ctx, cs->algo, chunk,
1898 					    chunk_size);
1899 		if (res != TEE_SUCCESS)
1900 			return res;
1901 		break;
1902 	default:
1903 		return TEE_ERROR_BAD_PARAMETERS;
1904 	}
1905 
1906 	return TEE_SUCCESS;
1907 }
1908 
1909 TEE_Result tee_svc_hash_final(uint32_t state, const void *chunk,
1910 			      size_t chunk_size, void *hash, size_t *hash_len)
1911 {
1912 	TEE_Result res, res2;
1913 	size_t hash_size;
1914 	size_t hlen;
1915 	struct tee_cryp_state *cs;
1916 	struct tee_ta_session *sess;
1917 
1918 	/* No data, but size provided isn't valid parameters. */
1919 	if (!chunk && chunk_size)
1920 		return TEE_ERROR_BAD_PARAMETERS;
1921 
1922 	res = tee_ta_get_current_session(&sess);
1923 	if (res != TEE_SUCCESS)
1924 		return res;
1925 
1926 	res = tee_mmu_check_access_rights(sess->ctx,
1927 					  TEE_MEMORY_ACCESS_READ |
1928 					  TEE_MEMORY_ACCESS_ANY_OWNER,
1929 					  (tee_uaddr_t)chunk, chunk_size);
1930 	if (res != TEE_SUCCESS)
1931 		return res;
1932 
1933 	res = tee_svc_copy_from_user(sess, &hlen, hash_len, sizeof(size_t));
1934 	if (res != TEE_SUCCESS)
1935 		return res;
1936 
1937 	res = tee_mmu_check_access_rights(sess->ctx,
1938 					  TEE_MEMORY_ACCESS_READ |
1939 					  TEE_MEMORY_ACCESS_WRITE |
1940 					  TEE_MEMORY_ACCESS_ANY_OWNER,
1941 					  (tee_uaddr_t)hash, hlen);
1942 	if (res != TEE_SUCCESS)
1943 		return res;
1944 
1945 	res = tee_svc_cryp_get_state(sess, state, &cs);
1946 	if (res != TEE_SUCCESS)
1947 		return res;
1948 
1949 	switch (TEE_ALG_GET_CLASS(cs->algo)) {
1950 	case TEE_OPERATION_DIGEST:
1951 		if (!crypto_ops.hash.update || !crypto_ops.hash.final)
1952 			return TEE_ERROR_NOT_IMPLEMENTED;
1953 		res = tee_hash_get_digest_size(cs->algo, &hash_size);
1954 		if (res != TEE_SUCCESS)
1955 			return res;
1956 		if (*hash_len < hash_size) {
1957 			res = TEE_ERROR_SHORT_BUFFER;
1958 			goto out;
1959 		}
1960 
1961 		if (chunk_size) {
1962 			res = crypto_ops.hash.update(cs->ctx, cs->algo, chunk,
1963 						     chunk_size);
1964 			if (res != TEE_SUCCESS)
1965 				return res;
1966 		}
1967 
1968 		res = crypto_ops.hash.final(cs->ctx, cs->algo, hash,
1969 					    hash_size);
1970 		if (res != TEE_SUCCESS)
1971 			return res;
1972 		break;
1973 
1974 	case TEE_OPERATION_MAC:
1975 		if (!crypto_ops.mac.update || !crypto_ops.mac.final)
1976 			return TEE_ERROR_NOT_IMPLEMENTED;
1977 		res = tee_mac_get_digest_size(cs->algo, &hash_size);
1978 		if (res != TEE_SUCCESS)
1979 			return res;
1980 		if (*hash_len < hash_size) {
1981 			res = TEE_ERROR_SHORT_BUFFER;
1982 			goto out;
1983 		}
1984 
1985 		if (chunk_size) {
1986 			res = crypto_ops.mac.update(cs->ctx, cs->algo, chunk,
1987 						    chunk_size);
1988 			if (res != TEE_SUCCESS)
1989 				return res;
1990 		}
1991 
1992 		res = crypto_ops.mac.final(cs->ctx, cs->algo, hash, hash_size);
1993 		if (res != TEE_SUCCESS)
1994 			return res;
1995 		break;
1996 
1997 	default:
1998 		return TEE_ERROR_BAD_PARAMETERS;
1999 	}
2000 out:
2001 	res2 =
2002 	    tee_svc_copy_to_user(sess, hash_len, &hash_size, sizeof(*hash_len));
2003 	if (res2 != TEE_SUCCESS)
2004 		return res2;
2005 	return res;
2006 }
2007 
2008 TEE_Result tee_svc_cipher_init(uint32_t state, const void *iv, size_t iv_len)
2009 {
2010 	TEE_Result res;
2011 	struct tee_cryp_state *cs;
2012 	struct tee_ta_session *sess;
2013 	struct tee_obj *o;
2014 	struct tee_cryp_obj_secret *key1;
2015 
2016 	res = tee_ta_get_current_session(&sess);
2017 	if (res != TEE_SUCCESS)
2018 		return res;
2019 
2020 	res = tee_svc_cryp_get_state(sess, state, &cs);
2021 	if (res != TEE_SUCCESS)
2022 		return res;
2023 
2024 	res = tee_mmu_check_access_rights(sess->ctx,
2025 					  TEE_MEMORY_ACCESS_READ |
2026 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2027 					  (tee_uaddr_t) iv, iv_len);
2028 	if (res != TEE_SUCCESS)
2029 		return res;
2030 
2031 	res = tee_obj_get(sess->ctx, cs->key1, &o);
2032 	if (res != TEE_SUCCESS)
2033 		return res;
2034 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
2035 		return TEE_ERROR_BAD_PARAMETERS;
2036 
2037 	key1 = (struct tee_cryp_obj_secret *)o->data;
2038 
2039 	if (!crypto_ops.cipher.init)
2040 		return TEE_ERROR_NOT_IMPLEMENTED;
2041 
2042 	if (tee_obj_get(sess->ctx, cs->key2, &o) == TEE_SUCCESS) {
2043 		struct tee_cryp_obj_secret *key2 =
2044 		    (struct tee_cryp_obj_secret *)o->data;
2045 
2046 		if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
2047 			return TEE_ERROR_BAD_PARAMETERS;
2048 
2049 		res = crypto_ops.cipher.init(cs->ctx, cs->algo, cs->mode,
2050 					     (uint8_t *)(key1 + 1),
2051 					     key1->key_size,
2052 					     (uint8_t *)(key2 + 1),
2053 					     key2->key_size,
2054 					     iv, iv_len);
2055 	} else {
2056 		res = crypto_ops.cipher.init(cs->ctx, cs->algo, cs->mode,
2057 					     (uint8_t *)(key1 + 1),
2058 					     key1->key_size,
2059 					     NULL,
2060 					     0,
2061 					     iv, iv_len);
2062 	}
2063 	if (res != TEE_SUCCESS)
2064 		return res;
2065 
2066 	cs->ctx_finalize = crypto_ops.cipher.final;
2067 	return TEE_SUCCESS;
2068 }
2069 
2070 static TEE_Result tee_svc_cipher_update_helper(uint32_t state, bool last_block,
2071 					       const void *src, size_t src_len,
2072 					       void *dst, size_t *dst_len)
2073 {
2074 	TEE_Result res;
2075 	struct tee_cryp_state *cs;
2076 	struct tee_ta_session *sess;
2077 	size_t dlen;
2078 
2079 	res = tee_ta_get_current_session(&sess);
2080 	if (res != TEE_SUCCESS)
2081 		return res;
2082 
2083 	res = tee_svc_cryp_get_state(sess, state, &cs);
2084 	if (res != TEE_SUCCESS)
2085 		return res;
2086 
2087 	res = tee_mmu_check_access_rights(sess->ctx,
2088 					  TEE_MEMORY_ACCESS_READ |
2089 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2090 					  (tee_uaddr_t)src, src_len);
2091 	if (res != TEE_SUCCESS)
2092 		return res;
2093 
2094 	if (!dst_len) {
2095 		dlen = 0;
2096 	} else {
2097 		res =
2098 		    tee_svc_copy_from_user(sess, &dlen, dst_len,
2099 					   sizeof(size_t));
2100 		if (res != TEE_SUCCESS)
2101 			return res;
2102 
2103 		res = tee_mmu_check_access_rights(sess->ctx,
2104 						  TEE_MEMORY_ACCESS_READ |
2105 						  TEE_MEMORY_ACCESS_WRITE |
2106 						  TEE_MEMORY_ACCESS_ANY_OWNER,
2107 						  (tee_uaddr_t)dst, dlen);
2108 		if (res != TEE_SUCCESS)
2109 			return res;
2110 	}
2111 
2112 	if (dlen < src_len) {
2113 		res = TEE_ERROR_SHORT_BUFFER;
2114 		goto out;
2115 	}
2116 
2117 	if (src_len > 0) {
2118 		/* Permit src_len == 0 to finalize the operation */
2119 		res = tee_do_cipher_update(cs->ctx, cs->algo, cs->mode,
2120 					   last_block, src, src_len, dst);
2121 	}
2122 
2123 	if (last_block && cs->ctx_finalize != NULL) {
2124 		cs->ctx_finalize(cs->ctx, cs->mode);
2125 		cs->ctx_finalize = NULL;
2126 	}
2127 
2128 out:
2129 	if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) &&
2130 	    dst_len != NULL) {
2131 		TEE_Result res2 = tee_svc_copy_to_user(sess, dst_len, &src_len,
2132 						       sizeof(size_t));
2133 		if (res2 != TEE_SUCCESS)
2134 			res = res2;
2135 	}
2136 
2137 	return res;
2138 }
2139 
2140 TEE_Result tee_svc_cipher_update(uint32_t state, const void *src,
2141 				 size_t src_len, void *dst, size_t *dst_len)
2142 {
2143 	return tee_svc_cipher_update_helper(state, false /* last_block */,
2144 					    src, src_len, dst, dst_len);
2145 }
2146 
2147 TEE_Result tee_svc_cipher_final(uint32_t state, const void *src,
2148 				size_t src_len, void *dst, size_t *dst_len)
2149 {
2150 	return tee_svc_cipher_update_helper(state, true /* last_block */,
2151 					    src, src_len, dst, dst_len);
2152 }
2153 
2154 #if defined(CFG_CRYPTO_HKDF)
2155 static TEE_Result get_hkdf_params(const TEE_Attribute *params,
2156 				  uint32_t param_count,
2157 				  void **salt, size_t *salt_len, void **info,
2158 				  size_t *info_len, size_t *okm_len)
2159 {
2160 	size_t n;
2161 	enum { SALT = 0x1, LENGTH = 0x2, INFO = 0x4 };
2162 	uint8_t found = 0;
2163 
2164 	*salt = *info = NULL;
2165 	*salt_len = *info_len = *okm_len = 0;
2166 
2167 	for (n = 0; n < param_count; n++) {
2168 		switch (params[n].attributeID) {
2169 		case TEE_ATTR_HKDF_SALT:
2170 			if (!(found & SALT)) {
2171 				*salt = params[n].content.ref.buffer;
2172 				*salt_len = params[n].content.ref.length;
2173 				found |= SALT;
2174 			}
2175 			break;
2176 		case TEE_ATTR_HKDF_OKM_LENGTH:
2177 			if (!(found & LENGTH)) {
2178 				*okm_len = params[n].content.value.a;
2179 				found |= LENGTH;
2180 			}
2181 			break;
2182 		case TEE_ATTR_HKDF_INFO:
2183 			if (!(found & INFO)) {
2184 				*info = params[n].content.ref.buffer;
2185 				*info_len = params[n].content.ref.length;
2186 				found |= INFO;
2187 			}
2188 			break;
2189 		default:
2190 			/* Unexpected attribute */
2191 			return TEE_ERROR_BAD_PARAMETERS;
2192 		}
2193 
2194 	}
2195 
2196 	if (!(found & LENGTH))
2197 		return TEE_ERROR_BAD_PARAMETERS;
2198 
2199 	return TEE_SUCCESS;
2200 }
2201 #endif
2202 
2203 #if defined(CFG_CRYPTO_CONCAT_KDF)
2204 static TEE_Result get_concat_kdf_params(const TEE_Attribute *params,
2205 					uint32_t param_count,
2206 					void **other_info,
2207 					size_t *other_info_len,
2208 					size_t *derived_key_len)
2209 {
2210 	size_t n;
2211 	enum { LENGTH = 0x1, INFO = 0x2 };
2212 	uint8_t found = 0;
2213 
2214 	*other_info = NULL;
2215 	*other_info_len = *derived_key_len = 0;
2216 
2217 	for (n = 0; n < param_count; n++) {
2218 		switch (params[n].attributeID) {
2219 		case TEE_ATTR_CONCAT_KDF_OTHER_INFO:
2220 			if (!(found & INFO)) {
2221 				*other_info = params[n].content.ref.buffer;
2222 				*other_info_len = params[n].content.ref.length;
2223 				found |= INFO;
2224 			}
2225 			break;
2226 		case TEE_ATTR_CONCAT_KDF_DKM_LENGTH:
2227 			if (!(found & LENGTH)) {
2228 				*derived_key_len = params[n].content.value.a;
2229 				found |= LENGTH;
2230 			}
2231 			break;
2232 		default:
2233 			/* Unexpected attribute */
2234 			return TEE_ERROR_BAD_PARAMETERS;
2235 		}
2236 	}
2237 
2238 	if (!(found & LENGTH))
2239 		return TEE_ERROR_BAD_PARAMETERS;
2240 
2241 	return TEE_SUCCESS;
2242 }
2243 #endif
2244 
2245 #if defined(CFG_CRYPTO_PBKDF2)
2246 static TEE_Result get_pbkdf2_params(const TEE_Attribute *params,
2247 				   uint32_t param_count, void **salt,
2248 				   size_t *salt_len, size_t *derived_key_len,
2249 				   size_t *iteration_count)
2250 {
2251 	size_t n;
2252 	enum { SALT = 0x1, LENGTH = 0x2, COUNT = 0x4 };
2253 	uint8_t found = 0;
2254 
2255 	*salt = NULL;
2256 	*salt_len = *derived_key_len = *iteration_count = 0;
2257 
2258 	for (n = 0; n < param_count; n++) {
2259 		switch (params[n].attributeID) {
2260 		case TEE_ATTR_PBKDF2_SALT:
2261 			if (!(found & SALT)) {
2262 				*salt = params[n].content.ref.buffer;
2263 				*salt_len = params[n].content.ref.length;
2264 				found |= SALT;
2265 			}
2266 			break;
2267 		case TEE_ATTR_PBKDF2_DKM_LENGTH:
2268 			if (!(found & LENGTH)) {
2269 				*derived_key_len = params[n].content.value.a;
2270 				found |= LENGTH;
2271 			}
2272 			break;
2273 		case TEE_ATTR_PBKDF2_ITERATION_COUNT:
2274 			if (!(found & COUNT)) {
2275 				*iteration_count = params[n].content.value.a;
2276 				found |= COUNT;
2277 			}
2278 			break;
2279 		default:
2280 			/* Unexpected attribute */
2281 			return TEE_ERROR_BAD_PARAMETERS;
2282 		}
2283 	}
2284 
2285 	if ((found & (LENGTH|COUNT)) != (LENGTH|COUNT))
2286 		return TEE_ERROR_BAD_PARAMETERS;
2287 
2288 	return TEE_SUCCESS;
2289 }
2290 #endif
2291 
2292 TEE_Result tee_svc_cryp_derive_key(uint32_t state, const TEE_Attribute *params,
2293 				   uint32_t param_count, uint32_t derived_key)
2294 {
2295 	TEE_Result res = TEE_ERROR_NOT_SUPPORTED;
2296 	struct tee_ta_session *sess;
2297 	struct tee_obj *ko;
2298 	struct tee_obj *so;
2299 	struct tee_cryp_state *cs;
2300 	struct tee_cryp_obj_secret *sk;
2301 	const struct tee_cryp_obj_type_props *type_props;
2302 
2303 	res = tee_ta_get_current_session(&sess);
2304 	if (res != TEE_SUCCESS)
2305 		return res;
2306 
2307 	res = tee_svc_cryp_get_state(sess, state, &cs);
2308 	if (res != TEE_SUCCESS)
2309 		return res;
2310 
2311 	/* Get key set in operation */
2312 	res = tee_obj_get(sess->ctx, cs->key1, &ko);
2313 	if (res != TEE_SUCCESS)
2314 		return res;
2315 
2316 	res = tee_obj_get(sess->ctx, derived_key, &so);
2317 	if (res != TEE_SUCCESS)
2318 		return res;
2319 
2320 	/* Find information needed about the object to initialize */
2321 	sk = (struct tee_cryp_obj_secret *)so->data;
2322 
2323 	/* Find description of object */
2324 	type_props = tee_svc_find_type_props(so->info.objectType);
2325 	if (!type_props)
2326 		return TEE_ERROR_NOT_SUPPORTED;
2327 
2328 	if (cs->algo == TEE_ALG_DH_DERIVE_SHARED_SECRET) {
2329 		size_t alloc_size;
2330 		struct bignum *pub;
2331 		struct bignum *ss;
2332 
2333 		if (!crypto_ops.bignum.allocate ||
2334 		    !crypto_ops.bignum.free ||
2335 		    !crypto_ops.bignum.bin2bn ||
2336 		    !crypto_ops.bignum.bn2bin ||
2337 		    !crypto_ops.bignum.num_bytes ||
2338 		    !crypto_ops.acipher.dh_shared_secret)
2339 			return TEE_ERROR_NOT_IMPLEMENTED;
2340 		if (param_count != 1 ||
2341 		    params[0].attributeID != TEE_ATTR_DH_PUBLIC_VALUE)
2342 			return TEE_ERROR_BAD_PARAMETERS;
2343 
2344 		alloc_size = params[0].content.ref.length * 8;
2345 		pub = crypto_ops.bignum.allocate(alloc_size);
2346 		ss = crypto_ops.bignum.allocate(alloc_size);
2347 		if (pub && ss) {
2348 			crypto_ops.bignum.bin2bn(params[0].content.ref.buffer,
2349 					params[0].content.ref.length, pub);
2350 			res = crypto_ops.acipher.dh_shared_secret(ko->data,
2351 								  pub, ss);
2352 			if (res == TEE_SUCCESS) {
2353 				sk->key_size = crypto_ops.bignum.num_bytes(ss);
2354 				crypto_ops.bignum.bn2bin(ss,
2355 							 (uint8_t *)(sk + 1));
2356 				so->info.handleFlags |=
2357 						TEE_HANDLE_FLAG_INITIALIZED;
2358 				SET_ATTRIBUTE(so, type_props,
2359 					      TEE_ATTR_SECRET_VALUE);
2360 			}
2361 		} else {
2362 			res = TEE_ERROR_OUT_OF_MEMORY;
2363 		}
2364 		crypto_ops.bignum.free(pub);
2365 		crypto_ops.bignum.free(ss);
2366 	}
2367 #if defined(CFG_CRYPTO_HKDF)
2368 	else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_HKDF) {
2369 		void *salt, *info;
2370 		size_t salt_len, info_len, okm_len;
2371 		uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo);
2372 		struct tee_cryp_obj_secret *ik = ko->data;
2373 		const uint8_t *ikm = (const uint8_t *)(ik + 1);
2374 
2375 		res = get_hkdf_params(params, param_count, &salt, &salt_len,
2376 				      &info, &info_len, &okm_len);
2377 		if (res != TEE_SUCCESS)
2378 			return res;
2379 
2380 		/* Requested size must fit into the output object's buffer */
2381 		if (okm_len >
2382 			ko->data_size - sizeof(struct tee_cryp_obj_secret))
2383 			return TEE_ERROR_BAD_PARAMETERS;
2384 
2385 		res = tee_cryp_hkdf(hash_id, ikm, ik->key_size, salt, salt_len,
2386 				    info, info_len, (uint8_t *)(sk + 1),
2387 				    okm_len);
2388 		if (res == TEE_SUCCESS) {
2389 			sk->key_size = okm_len;
2390 			so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
2391 			SET_ATTRIBUTE(so, type_props, TEE_ATTR_SECRET_VALUE);
2392 		}
2393 	}
2394 #endif
2395 #if defined(CFG_CRYPTO_CONCAT_KDF)
2396 	else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_CONCAT_KDF) {
2397 		void *info;
2398 		size_t info_len, derived_key_len;
2399 		uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo);
2400 		struct tee_cryp_obj_secret *ss = ko->data;
2401 		const uint8_t *shared_secret = (const uint8_t *)(ss + 1);
2402 
2403 		res = get_concat_kdf_params(params, param_count, &info,
2404 					    &info_len, &derived_key_len);
2405 		if (res != TEE_SUCCESS)
2406 			return res;
2407 
2408 		/* Requested size must fit into the output object's buffer */
2409 		if (derived_key_len >
2410 		    ko->data_size - sizeof(struct tee_cryp_obj_secret))
2411 			return TEE_ERROR_BAD_PARAMETERS;
2412 
2413 		res = tee_cryp_concat_kdf(hash_id, shared_secret, ss->key_size,
2414 					  info, info_len, (uint8_t *)(sk + 1),
2415 					  derived_key_len);
2416 		if (res == TEE_SUCCESS) {
2417 			sk->key_size = derived_key_len;
2418 			so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
2419 			SET_ATTRIBUTE(so, type_props, TEE_ATTR_SECRET_VALUE);
2420 		}
2421 	}
2422 #endif
2423 #if defined(CFG_CRYPTO_PBKDF2)
2424 	else if (TEE_ALG_GET_MAIN_ALG(cs->algo) == TEE_MAIN_ALGO_PBKDF2) {
2425 		void *salt;
2426 		size_t salt_len, iteration_count, derived_key_len;
2427 		uint32_t hash_id = TEE_ALG_GET_DIGEST_HASH(cs->algo);
2428 		struct tee_cryp_obj_secret *ss = ko->data;
2429 		const uint8_t *password = (const uint8_t *)(ss + 1);
2430 
2431 		res = get_pbkdf2_params(params, param_count, &salt, &salt_len,
2432 					&derived_key_len, &iteration_count);
2433 		if (res != TEE_SUCCESS)
2434 			return res;
2435 
2436 		/* Requested size must fit into the output object's buffer */
2437 		if (derived_key_len >
2438 			ko->data_size - sizeof(struct tee_cryp_obj_secret))
2439 			return TEE_ERROR_BAD_PARAMETERS;
2440 
2441 		res = tee_cryp_pbkdf2(hash_id, password, ss->key_size, salt,
2442 				      salt_len, iteration_count,
2443 				      (uint8_t *)(sk + 1), derived_key_len);
2444 		if (res == TEE_SUCCESS) {
2445 			sk->key_size = derived_key_len;
2446 			so->info.handleFlags |= TEE_HANDLE_FLAG_INITIALIZED;
2447 			SET_ATTRIBUTE(so, type_props, TEE_ATTR_SECRET_VALUE);
2448 		}
2449 	}
2450 #endif
2451 	else
2452 		res = TEE_ERROR_NOT_SUPPORTED;
2453 
2454 	return res;
2455 }
2456 
2457 TEE_Result tee_svc_cryp_random_number_generate(void *buf, size_t blen)
2458 {
2459 	TEE_Result res;
2460 	struct tee_ta_session *sess;
2461 
2462 	res = tee_ta_get_current_session(&sess);
2463 	if (res != TEE_SUCCESS)
2464 		return res;
2465 
2466 	res = tee_mmu_check_access_rights(sess->ctx,
2467 					  TEE_MEMORY_ACCESS_WRITE |
2468 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2469 					  (tee_uaddr_t)buf, blen);
2470 	if (res != TEE_SUCCESS)
2471 		return res;
2472 
2473 	res = get_rng_array(buf, blen);
2474 	if (res != TEE_SUCCESS)
2475 		return res;
2476 
2477 	return res;
2478 }
2479 
2480 TEE_Result tee_svc_authenc_init(uint32_t state, const void *nonce,
2481 				size_t nonce_len, size_t tag_len,
2482 				size_t aad_len, size_t payload_len)
2483 {
2484 	TEE_Result res;
2485 	struct tee_cryp_state *cs;
2486 	struct tee_ta_session *sess;
2487 	struct tee_obj *o;
2488 	struct tee_cryp_obj_secret *key;
2489 
2490 	res = tee_ta_get_current_session(&sess);
2491 	if (res != TEE_SUCCESS)
2492 		return res;
2493 
2494 	res = tee_svc_cryp_get_state(sess, state, &cs);
2495 	if (res != TEE_SUCCESS)
2496 		return res;
2497 
2498 	res = tee_obj_get(sess->ctx, cs->key1, &o);
2499 	if (res != TEE_SUCCESS)
2500 		return res;
2501 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
2502 		return TEE_ERROR_BAD_PARAMETERS;
2503 
2504 	if (!crypto_ops.authenc.init)
2505 		return TEE_ERROR_NOT_IMPLEMENTED;
2506 	key = (struct tee_cryp_obj_secret *)o->data;
2507 	res = crypto_ops.authenc.init(cs->ctx, cs->algo, cs->mode,
2508 				      (uint8_t *)(key + 1), key->key_size,
2509 				      nonce, nonce_len, tag_len, aad_len,
2510 				      payload_len);
2511 	if (res != TEE_SUCCESS)
2512 		return res;
2513 
2514 	cs->ctx_finalize = (tee_cryp_ctx_finalize_func_t)
2515 				crypto_ops.authenc.final;
2516 	return TEE_SUCCESS;
2517 }
2518 
2519 TEE_Result tee_svc_authenc_update_aad(uint32_t state, const void *aad_data,
2520 				      size_t aad_data_len)
2521 {
2522 	TEE_Result res;
2523 	struct tee_cryp_state *cs;
2524 	struct tee_ta_session *sess;
2525 
2526 	res = tee_ta_get_current_session(&sess);
2527 	if (res != TEE_SUCCESS)
2528 		return res;
2529 
2530 	res = tee_mmu_check_access_rights(sess->ctx,
2531 					  TEE_MEMORY_ACCESS_READ |
2532 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2533 					  (tee_uaddr_t) aad_data,
2534 					  aad_data_len);
2535 	if (res != TEE_SUCCESS)
2536 		return res;
2537 
2538 	res = tee_svc_cryp_get_state(sess, state, &cs);
2539 	if (res != TEE_SUCCESS)
2540 		return res;
2541 
2542 	if (!crypto_ops.authenc.update_aad)
2543 		return TEE_ERROR_NOT_IMPLEMENTED;
2544 	res = crypto_ops.authenc.update_aad(cs->ctx, cs->algo, cs->mode,
2545 					    aad_data, aad_data_len);
2546 	if (res != TEE_SUCCESS)
2547 		return res;
2548 
2549 	return TEE_SUCCESS;
2550 }
2551 
2552 TEE_Result tee_svc_authenc_update_payload(uint32_t state, const void *src_data,
2553 					  size_t src_len, void *dst_data,
2554 					  size_t *dst_len)
2555 {
2556 	TEE_Result res;
2557 	struct tee_cryp_state *cs;
2558 	struct tee_ta_session *sess;
2559 	size_t dlen;
2560 
2561 	res = tee_ta_get_current_session(&sess);
2562 	if (res != TEE_SUCCESS)
2563 		return res;
2564 
2565 	res = tee_svc_cryp_get_state(sess, state, &cs);
2566 	if (res != TEE_SUCCESS)
2567 		return res;
2568 
2569 	res = tee_mmu_check_access_rights(sess->ctx,
2570 					  TEE_MEMORY_ACCESS_READ |
2571 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2572 					  (tee_uaddr_t) src_data, src_len);
2573 	if (res != TEE_SUCCESS)
2574 		return res;
2575 
2576 	res = tee_svc_copy_from_user(sess, &dlen, dst_len, sizeof(size_t));
2577 	if (res != TEE_SUCCESS)
2578 		return res;
2579 
2580 	res = tee_mmu_check_access_rights(sess->ctx,
2581 					  TEE_MEMORY_ACCESS_READ |
2582 					  TEE_MEMORY_ACCESS_WRITE |
2583 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2584 					  (tee_uaddr_t)dst_data, dlen);
2585 	if (res != TEE_SUCCESS)
2586 		return res;
2587 
2588 	if (dlen < src_len) {
2589 		res = TEE_ERROR_SHORT_BUFFER;
2590 		goto out;
2591 	}
2592 
2593 	if (!crypto_ops.authenc.update_payload)
2594 		return TEE_ERROR_NOT_IMPLEMENTED;
2595 	res = crypto_ops.authenc.update_payload(cs->ctx, cs->algo, cs->mode,
2596 						src_data, src_len, dst_data,
2597 						&dlen);
2598 
2599 out:
2600 	if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) {
2601 		TEE_Result res2 = tee_svc_copy_to_user(sess, dst_len, &dlen,
2602 						       sizeof(size_t));
2603 		if (res2 != TEE_SUCCESS)
2604 			res = res2;
2605 	}
2606 
2607 	return res;
2608 }
2609 
2610 TEE_Result tee_svc_authenc_enc_final(uint32_t state, const void *src_data,
2611 				     size_t src_len, void *dst_data,
2612 				     size_t *dst_len, void *tag,
2613 				     size_t *tag_len)
2614 {
2615 	TEE_Result res;
2616 	struct tee_cryp_state *cs;
2617 	struct tee_ta_session *sess;
2618 	size_t dlen;
2619 	size_t tlen;
2620 
2621 	res = tee_ta_get_current_session(&sess);
2622 	if (res != TEE_SUCCESS)
2623 		return res;
2624 
2625 	res = tee_svc_cryp_get_state(sess, state, &cs);
2626 	if (res != TEE_SUCCESS)
2627 		return res;
2628 
2629 	if (cs->mode != TEE_MODE_ENCRYPT)
2630 		return TEE_ERROR_BAD_PARAMETERS;
2631 
2632 	res = tee_mmu_check_access_rights(sess->ctx,
2633 					  TEE_MEMORY_ACCESS_READ |
2634 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2635 					  (tee_uaddr_t)src_data, src_len);
2636 	if (res != TEE_SUCCESS)
2637 		return res;
2638 
2639 	if (!dst_len) {
2640 		dlen = 0;
2641 	} else {
2642 		res =
2643 		    tee_svc_copy_from_user(sess, &dlen, dst_len,
2644 					   sizeof(size_t));
2645 		if (res != TEE_SUCCESS)
2646 			return res;
2647 
2648 		res = tee_mmu_check_access_rights(sess->ctx,
2649 						  TEE_MEMORY_ACCESS_READ |
2650 						  TEE_MEMORY_ACCESS_WRITE |
2651 						  TEE_MEMORY_ACCESS_ANY_OWNER,
2652 						  (tee_uaddr_t)dst_data, dlen);
2653 		if (res != TEE_SUCCESS)
2654 			return res;
2655 	}
2656 
2657 	if (dlen < src_len) {
2658 		res = TEE_ERROR_SHORT_BUFFER;
2659 		goto out;
2660 	}
2661 
2662 	res = tee_svc_copy_from_user(sess, &tlen, tag_len, sizeof(size_t));
2663 	if (res != TEE_SUCCESS)
2664 		return res;
2665 
2666 	res = tee_mmu_check_access_rights(sess->ctx,
2667 					  TEE_MEMORY_ACCESS_READ |
2668 					  TEE_MEMORY_ACCESS_WRITE |
2669 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2670 					  (tee_uaddr_t)tag, tlen);
2671 	if (res != TEE_SUCCESS)
2672 		return res;
2673 
2674 	if (!crypto_ops.authenc.enc_final)
2675 		return TEE_ERROR_NOT_IMPLEMENTED;
2676 	res = crypto_ops.authenc.enc_final(cs->ctx, cs->algo, src_data,
2677 					   src_len, dst_data, &dlen, tag,
2678 					   &tlen);
2679 
2680 out:
2681 	if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) {
2682 		TEE_Result res2;
2683 
2684 		if (dst_len != NULL) {
2685 			res2 = tee_svc_copy_to_user(sess, dst_len, &dlen,
2686 						    sizeof(size_t));
2687 			if (res2 != TEE_SUCCESS)
2688 				return res2;
2689 		}
2690 
2691 		res2 =
2692 		    tee_svc_copy_to_user(sess, tag_len, &tlen, sizeof(size_t));
2693 		if (res2 != TEE_SUCCESS)
2694 			return res2;
2695 	}
2696 
2697 	return res;
2698 }
2699 
2700 TEE_Result tee_svc_authenc_dec_final(uint32_t state, const void *src_data,
2701 				     size_t src_len, void *dst_data,
2702 				     size_t *dst_len, const void *tag,
2703 				     size_t tag_len)
2704 {
2705 	TEE_Result res;
2706 	struct tee_cryp_state *cs;
2707 	struct tee_ta_session *sess;
2708 	size_t dlen;
2709 
2710 	res = tee_ta_get_current_session(&sess);
2711 	if (res != TEE_SUCCESS)
2712 		return res;
2713 
2714 	res = tee_svc_cryp_get_state(sess, state, &cs);
2715 	if (res != TEE_SUCCESS)
2716 		return res;
2717 
2718 	if (cs->mode != TEE_MODE_DECRYPT)
2719 		return TEE_ERROR_BAD_PARAMETERS;
2720 
2721 	res = tee_mmu_check_access_rights(sess->ctx,
2722 					  TEE_MEMORY_ACCESS_READ |
2723 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2724 					  (tee_uaddr_t)src_data, src_len);
2725 	if (res != TEE_SUCCESS)
2726 		return res;
2727 
2728 	if (!dst_len) {
2729 		dlen = 0;
2730 	} else {
2731 		res =
2732 		    tee_svc_copy_from_user(sess, &dlen, dst_len,
2733 					   sizeof(size_t));
2734 		if (res != TEE_SUCCESS)
2735 			return res;
2736 
2737 		res = tee_mmu_check_access_rights(sess->ctx,
2738 						  TEE_MEMORY_ACCESS_READ |
2739 						  TEE_MEMORY_ACCESS_WRITE |
2740 						  TEE_MEMORY_ACCESS_ANY_OWNER,
2741 						  (tee_uaddr_t)dst_data, dlen);
2742 		if (res != TEE_SUCCESS)
2743 			return res;
2744 	}
2745 
2746 	if (dlen < src_len) {
2747 		res = TEE_ERROR_SHORT_BUFFER;
2748 		goto out;
2749 	}
2750 
2751 	res = tee_mmu_check_access_rights(sess->ctx,
2752 					  TEE_MEMORY_ACCESS_READ |
2753 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2754 					  (tee_uaddr_t)tag, tag_len);
2755 	if (res != TEE_SUCCESS)
2756 		return res;
2757 
2758 	if (!crypto_ops.authenc.dec_final)
2759 		return TEE_ERROR_NOT_IMPLEMENTED;
2760 	res = crypto_ops.authenc.dec_final(cs->ctx, cs->algo, src_data,
2761 					   src_len, dst_data, &dlen, tag,
2762 					   tag_len);
2763 
2764 out:
2765 	if ((res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) &&
2766 	    dst_len != NULL) {
2767 		TEE_Result res2;
2768 
2769 		res2 =
2770 		    tee_svc_copy_to_user(sess, dst_len, &dlen,
2771 					 sizeof(size_t));
2772 		if (res2 != TEE_SUCCESS)
2773 			return res2;
2774 	}
2775 
2776 	return res;
2777 }
2778 
2779 static void tee_svc_asymm_pkcs1_get_salt_len(const TEE_Attribute *params,
2780 					     uint32_t num_params, int *salt_len)
2781 {
2782 	size_t n;
2783 
2784 	for (n = 0; n < num_params; n++) {
2785 		if (params[n].attributeID == TEE_ATTR_RSA_PSS_SALT_LENGTH) {
2786 			*salt_len = params[n].content.value.a;
2787 			return;
2788 		}
2789 	}
2790 	*salt_len = -1;
2791 }
2792 
2793 TEE_Result tee_svc_asymm_operate(uint32_t state, const TEE_Attribute *params,
2794 				 uint32_t num_params, const void *src_data,
2795 				 size_t src_len, void *dst_data,
2796 				 size_t *dst_len)
2797 {
2798 	TEE_Result res;
2799 	struct tee_cryp_state *cs;
2800 	struct tee_ta_session *sess;
2801 	size_t dlen;
2802 	struct tee_obj *o;
2803 	void *label = NULL;
2804 	size_t label_len = 0;
2805 	size_t n;
2806 	int salt_len;
2807 
2808 	res = tee_ta_get_current_session(&sess);
2809 	if (res != TEE_SUCCESS)
2810 		return res;
2811 
2812 	res = tee_svc_cryp_get_state(sess, state, &cs);
2813 	if (res != TEE_SUCCESS)
2814 		return res;
2815 
2816 	res = tee_mmu_check_access_rights(
2817 		sess->ctx,
2818 		TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_ANY_OWNER,
2819 		(tee_uaddr_t) src_data, src_len);
2820 	if (res != TEE_SUCCESS)
2821 		return res;
2822 
2823 	res = tee_svc_copy_from_user(sess, &dlen, dst_len, sizeof(size_t));
2824 	if (res != TEE_SUCCESS)
2825 		return res;
2826 
2827 	res = tee_mmu_check_access_rights(
2828 		sess->ctx,
2829 		TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE |
2830 			TEE_MEMORY_ACCESS_ANY_OWNER,
2831 		(tee_uaddr_t) dst_data, dlen);
2832 	if (res != TEE_SUCCESS)
2833 		return res;
2834 
2835 	res = tee_obj_get(sess->ctx, cs->key1, &o);
2836 	if (res != TEE_SUCCESS)
2837 		return res;
2838 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
2839 		return TEE_ERROR_GENERIC;
2840 
2841 	switch (cs->algo) {
2842 	case TEE_ALG_RSA_NOPAD:
2843 		if (cs->mode == TEE_MODE_ENCRYPT) {
2844 			if (crypto_ops.acipher.rsanopad_encrypt)
2845 				res = crypto_ops.acipher.rsanopad_encrypt(
2846 					o->data, src_data, src_len,
2847 					dst_data, &dlen);
2848 			else
2849 				res = TEE_ERROR_NOT_IMPLEMENTED;
2850 		} else if (cs->mode == TEE_MODE_DECRYPT) {
2851 			if (crypto_ops.acipher.rsanopad_decrypt)
2852 				res = crypto_ops.acipher.rsanopad_decrypt(
2853 					o->data, src_data, src_len, dst_data,
2854 					&dlen);
2855 			else
2856 				res = TEE_ERROR_NOT_IMPLEMENTED;
2857 		} else {
2858 			/*
2859 			 * We will panic because "the mode is not compatible
2860 			 * with the function"
2861 			 */
2862 			return TEE_ERROR_GENERIC;
2863 		}
2864 		break;
2865 
2866 	case TEE_ALG_RSAES_PKCS1_V1_5:
2867 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1:
2868 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224:
2869 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256:
2870 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384:
2871 	case TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512:
2872 		for (n = 0; n < num_params; n++) {
2873 			if (params[n].attributeID == TEE_ATTR_RSA_OAEP_LABEL) {
2874 				label = params[n].content.ref.buffer;
2875 				label_len = params[n].content.ref.length;
2876 				break;
2877 			}
2878 		}
2879 
2880 		if (cs->mode == TEE_MODE_ENCRYPT) {
2881 			if (crypto_ops.acipher.rsaes_encrypt)
2882 				res = crypto_ops.acipher.rsaes_encrypt(
2883 					cs->algo, o->data, label, label_len,
2884 					src_data, src_len, dst_data, &dlen);
2885 			else
2886 				res = TEE_ERROR_NOT_IMPLEMENTED;
2887 		} else if (cs->mode == TEE_MODE_DECRYPT) {
2888 			if (crypto_ops.acipher.rsaes_decrypt)
2889 				res = crypto_ops.acipher.rsaes_decrypt(
2890 					cs->algo, o->data,
2891 					label, label_len,
2892 					src_data, src_len, dst_data, &dlen);
2893 			else
2894 				res = TEE_ERROR_NOT_IMPLEMENTED;
2895 		} else {
2896 			res = TEE_ERROR_BAD_PARAMETERS;
2897 		}
2898 		break;
2899 
2900 	case TEE_ALG_RSASSA_PKCS1_V1_5_MD5:
2901 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA1:
2902 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA224:
2903 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA256:
2904 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA384:
2905 	case TEE_ALG_RSASSA_PKCS1_V1_5_SHA512:
2906 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1:
2907 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224:
2908 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256:
2909 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384:
2910 	case TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512:
2911 		if (cs->mode != TEE_MODE_SIGN) {
2912 			res = TEE_ERROR_BAD_PARAMETERS;
2913 			break;
2914 		}
2915 		tee_svc_asymm_pkcs1_get_salt_len(params, num_params, &salt_len);
2916 
2917 		if (!crypto_ops.acipher.rsassa_sign) {
2918 			res = TEE_ERROR_NOT_IMPLEMENTED;
2919 			break;
2920 		}
2921 		res = crypto_ops.acipher.rsassa_sign(cs->algo, o->data,
2922 						     salt_len, src_data,
2923 						     src_len, dst_data, &dlen);
2924 		break;
2925 
2926 	case TEE_ALG_DSA_SHA1:
2927 		if (!crypto_ops.acipher.dsa_sign) {
2928 			res = TEE_ERROR_NOT_IMPLEMENTED;
2929 			break;
2930 		}
2931 		res = crypto_ops.acipher.dsa_sign(cs->algo, o->data, src_data,
2932 						  src_len, dst_data, &dlen);
2933 		break;
2934 
2935 	default:
2936 		res = TEE_ERROR_BAD_PARAMETERS;
2937 		break;
2938 	}
2939 
2940 	if (res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER) {
2941 		TEE_Result res2;
2942 
2943 		res2 =
2944 		    tee_svc_copy_to_user(sess, dst_len, &dlen, sizeof(size_t));
2945 		if (res2 != TEE_SUCCESS)
2946 			return res2;
2947 	}
2948 
2949 	return res;
2950 }
2951 
2952 TEE_Result tee_svc_asymm_verify(uint32_t state, const TEE_Attribute *params,
2953 				uint32_t num_params, const void *data,
2954 				size_t data_len, const void *sig,
2955 				size_t sig_len)
2956 {
2957 	TEE_Result res;
2958 	struct tee_cryp_state *cs;
2959 	struct tee_ta_session *sess;
2960 	struct tee_obj *o;
2961 	size_t hash_size;
2962 	int salt_len;
2963 
2964 	res = tee_ta_get_current_session(&sess);
2965 	if (res != TEE_SUCCESS)
2966 		return res;
2967 
2968 	res = tee_svc_cryp_get_state(sess, state, &cs);
2969 	if (res != TEE_SUCCESS)
2970 		return res;
2971 
2972 	if (cs->mode != TEE_MODE_VERIFY)
2973 		return TEE_ERROR_BAD_PARAMETERS;
2974 
2975 	res = tee_mmu_check_access_rights(sess->ctx,
2976 					  TEE_MEMORY_ACCESS_READ |
2977 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2978 					  (tee_uaddr_t)data, data_len);
2979 	if (res != TEE_SUCCESS)
2980 		return res;
2981 
2982 	res = tee_mmu_check_access_rights(sess->ctx,
2983 					  TEE_MEMORY_ACCESS_READ |
2984 					  TEE_MEMORY_ACCESS_ANY_OWNER,
2985 					  (tee_uaddr_t)sig, sig_len);
2986 	if (res != TEE_SUCCESS)
2987 		return res;
2988 
2989 	res = tee_obj_get(sess->ctx, cs->key1, &o);
2990 	if (res != TEE_SUCCESS)
2991 		return res;
2992 	if ((o->info.handleFlags & TEE_HANDLE_FLAG_INITIALIZED) == 0)
2993 		return TEE_ERROR_BAD_PARAMETERS;
2994 
2995 	res = tee_hash_get_digest_size(TEE_DIGEST_HASH_TO_ALGO(cs->algo),
2996 				       &hash_size);
2997 	if (res != TEE_SUCCESS)
2998 		return res;
2999 
3000 	if (data_len != hash_size)
3001 		return TEE_ERROR_BAD_PARAMETERS;
3002 
3003 	switch (TEE_ALG_GET_MAIN_ALG(cs->algo)) {
3004 	case TEE_MAIN_ALGO_RSA:
3005 		tee_svc_asymm_pkcs1_get_salt_len(params, num_params, &salt_len);
3006 		if (!crypto_ops.acipher.rsassa_verify) {
3007 			res = TEE_ERROR_NOT_IMPLEMENTED;
3008 			break;
3009 		}
3010 		res = crypto_ops.acipher.rsassa_verify(cs->algo, o->data,
3011 						       salt_len, data,
3012 						       data_len, sig, sig_len);
3013 		break;
3014 
3015 	case TEE_MAIN_ALGO_DSA:
3016 		if (!crypto_ops.acipher.dsa_verify) {
3017 			res = TEE_ERROR_NOT_IMPLEMENTED;
3018 			break;
3019 		}
3020 		res = crypto_ops.acipher.dsa_verify(cs->algo, o->data, data,
3021 						    data_len, sig, sig_len);
3022 		break;
3023 
3024 	default:
3025 		res = TEE_ERROR_NOT_SUPPORTED;
3026 	}
3027 
3028 	return res;
3029 }
3030