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