xref: /optee_os/lib/libutee/tee_api_arith_mpi.c (revision af4bcf3432c6014655711a901d31f4430626f1c5)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2018, Linaro limited
4  */
5 #include <assert.h>
6 #include <mbedtls/bignum.h>
7 #include <mempool.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <tee_api.h>
11 #include <tee_arith_internal.h>
12 #include <utee_defines.h>
13 #include <utee_syscalls.h>
14 #include <util.h>
15 
16 #define MPI_MEMPOOL_SIZE	(12 * 1024)
17 
18 static void __noreturn api_panic(const char *func, int line, const char *msg)
19 {
20 	printf("Panic function %s, line %d: %s\n", func, line, msg);
21 	TEE_Panic(0xB16127 /*BIGINT*/);
22 	while (1)
23 		; /* Panic will crash the thread */
24 }
25 
26 #define API_PANIC(x) api_panic(__func__, __LINE__, x)
27 
28 static void __noreturn mpi_panic(const char *func, int line, int rc)
29 {
30 	printf("Panic function %s, line %d, code %d\n", func, line, rc);
31 	TEE_Panic(0xB16127 /*BIGINT*/);
32 	while (1)
33 		; /* Panic will crash the thread */
34 }
35 
36 #define MPI_CHECK(x) do { \
37 		int _rc = (x); \
38 		 \
39 		if (_rc) \
40 			mpi_panic(__func__, __LINE__, _rc); \
41 	} while (0)
42 
43 void _TEE_MathAPI_Init(void)
44 {
45 	static uint8_t data[MPI_MEMPOOL_SIZE] __aligned(MEMPOOL_ALIGN);
46 
47 	mbedtls_mpi_mempool = mempool_alloc_pool(data, sizeof(data), NULL);
48 	if (!mbedtls_mpi_mempool)
49 		API_PANIC("Failed to initialize memory pool");
50 }
51 
52 struct bigint_hdr {
53 	int32_t sign;
54 	uint16_t alloc_size;
55 	uint16_t nblimbs;
56 };
57 
58 #define BIGINT_HDR_SIZE_IN_U32	2
59 
60 static TEE_Result copy_mpi_to_bigint(mbedtls_mpi *mpi, TEE_BigInt *bigInt)
61 {
62 	struct bigint_hdr *hdr = (struct bigint_hdr *)bigInt;
63 	size_t n = mpi->n;
64 
65 	/* Trim of eventual insignificant zeroes */
66 	while (n && !mpi->p[n - 1])
67 		n--;
68 
69 	if (hdr->alloc_size < n)
70 		return TEE_ERROR_OVERFLOW;
71 
72 	hdr->nblimbs = n;
73 	hdr->sign = mpi->s;
74 	memcpy(hdr + 1, mpi->p, mpi->n * sizeof(mbedtls_mpi_uint));
75 
76 	return TEE_SUCCESS;
77 }
78 
79 /*
80  * Initializes a MPI.
81  *
82  * A temporary MPI is allocated and if a bigInt is supplied the MPI is
83  * initialized with the value of the bigInt.
84  */
85 static void get_mpi(mbedtls_mpi *mpi, const TEE_BigInt *bigInt)
86 {
87 	/*
88 	 * The way the GP spec is defining the bignums it's
89 	 * difficult/tricky to do it using 64-bit arithmetics given that
90 	 * we'd need 64-bit alignment of the data as well.
91 	 */
92 	COMPILE_TIME_ASSERT(sizeof(mbedtls_mpi_uint) == sizeof(uint32_t));
93 
94 	/*
95 	 * The struct bigint_hdr is the overhead added to the bigint and
96 	 * is required to take exactly 2 uint32_t.
97 	 */
98 	COMPILE_TIME_ASSERT(sizeof(struct bigint_hdr) ==
99 			    sizeof(uint32_t) * BIGINT_HDR_SIZE_IN_U32);
100 
101 	mbedtls_mpi_init_mempool(mpi);
102 
103 	if (bigInt) {
104 		const struct bigint_hdr *hdr = (struct bigint_hdr *)bigInt;
105 		const mbedtls_mpi_uint *p = (const mbedtls_mpi_uint *)(hdr + 1);
106 		size_t n = hdr->nblimbs;
107 
108 		/* Trim of eventual insignificant zeroes */
109 		while (n && !p[n - 1])
110 			n--;
111 
112 		MPI_CHECK(mbedtls_mpi_grow(mpi, n));
113 		mpi->s = hdr->sign;
114 		memcpy(mpi->p, p, n * sizeof(mbedtls_mpi_uint));
115 	}
116 }
117 
118 void TEE_BigIntInit(TEE_BigInt *bigInt, size_t len)
119 {
120 	struct bigint_hdr *hdr = (struct bigint_hdr *)bigInt;
121 
122 	static_assert(MBEDTLS_MPI_MAX_LIMBS + BIGINT_HDR_SIZE_IN_U32 >=
123 		      CFG_TA_BIGNUM_MAX_BITS / 32);
124 
125 	memset(bigInt, 0, len * sizeof(uint32_t));
126 	hdr->sign = 1;
127 
128 	/* "gpd.tee.arith.maxBigIntSize" is assigned CFG_TA_BIGNUM_MAX_BITS */
129 	if (len > CFG_TA_BIGNUM_MAX_BITS / 4)
130 		API_PANIC("Too large bigint");
131 	hdr->alloc_size = len - BIGINT_HDR_SIZE_IN_U32;
132 }
133 
134 void __GP11_TEE_BigIntInit(TEE_BigInt *bigInt, uint32_t len)
135 {
136 	TEE_BigIntInit(bigInt, len);
137 }
138 
139 TEE_Result TEE_BigIntConvertFromOctetString(TEE_BigInt *dest,
140 					    const uint8_t *buffer,
141 					    size_t bufferLen, int32_t sign)
142 {
143 	TEE_Result res;
144 	mbedtls_mpi mpi_dest;
145 
146 	get_mpi(&mpi_dest, NULL);
147 
148 	if (mbedtls_mpi_read_binary(&mpi_dest,  buffer, bufferLen))
149 		res = TEE_ERROR_OVERFLOW;
150 	else
151 		res = TEE_SUCCESS;
152 
153 	if (sign < 0)
154 		mpi_dest.s = -1;
155 
156 	if (!res)
157 		res = copy_mpi_to_bigint(&mpi_dest, dest);
158 
159 	mbedtls_mpi_free(&mpi_dest);
160 
161 	return res;
162 }
163 
164 TEE_Result __GP11_TEE_BigIntConvertFromOctetString(TEE_BigInt *dest,
165 						   const uint8_t *buffer,
166 						   uint32_t bufferLen,
167 						   int32_t sign)
168 {
169 	return TEE_BigIntConvertFromOctetString(dest, buffer, bufferLen, sign);
170 }
171 
172 TEE_Result TEE_BigIntConvertToOctetString(uint8_t *buffer, size_t *bufferLen,
173 					  const TEE_BigInt *bigInt)
174 {
175 	TEE_Result res = TEE_SUCCESS;
176 	mbedtls_mpi mpi;
177 	size_t sz;
178 
179 	get_mpi(&mpi, bigInt);
180 
181 	sz = mbedtls_mpi_size(&mpi);
182 	if (sz <= *bufferLen)
183 		MPI_CHECK(mbedtls_mpi_write_binary(&mpi, buffer, sz));
184 	else
185 		res = TEE_ERROR_SHORT_BUFFER;
186 
187 	*bufferLen = sz;
188 
189 	mbedtls_mpi_free(&mpi);
190 
191 	return res;
192 }
193 
194 TEE_Result __GP11_TEE_BigIntConvertToOctetString(uint8_t *buffer,
195 						 uint32_t *bufferLen,
196 						 const TEE_BigInt *bigInt)
197 {
198 	TEE_Result res = TEE_SUCCESS;
199 	size_t l = *bufferLen;
200 
201 	res = TEE_BigIntConvertToOctetString(buffer, &l, bigInt);
202 	*bufferLen = l;
203 	return res;
204 }
205 
206 void TEE_BigIntConvertFromS32(TEE_BigInt *dest, int32_t shortVal)
207 {
208 	mbedtls_mpi mpi;
209 
210 	get_mpi(&mpi, dest);
211 
212 	MPI_CHECK(mbedtls_mpi_lset(&mpi, shortVal));
213 
214 	MPI_CHECK(copy_mpi_to_bigint(&mpi, dest));
215 	mbedtls_mpi_free(&mpi);
216 }
217 
218 TEE_Result TEE_BigIntConvertToS32(int32_t *dest, const TEE_BigInt *src)
219 {
220 	TEE_Result res = TEE_SUCCESS;
221 	mbedtls_mpi mpi;
222 	uint32_t v;
223 
224 	get_mpi(&mpi, src);
225 
226 	if (mbedtls_mpi_write_binary(&mpi, (void *)&v, sizeof(v))) {
227 		res = TEE_ERROR_OVERFLOW;
228 		goto out;
229 	}
230 
231 	if (mpi.s > 0) {
232 		if (ADD_OVERFLOW(0, TEE_U32_FROM_BIG_ENDIAN(v), dest))
233 			res = TEE_ERROR_OVERFLOW;
234 	} else {
235 		if (SUB_OVERFLOW(0, TEE_U32_FROM_BIG_ENDIAN(v), dest))
236 			res = TEE_ERROR_OVERFLOW;
237 	}
238 
239 out:
240 	mbedtls_mpi_free(&mpi);
241 
242 	return res;
243 }
244 
245 int32_t TEE_BigIntCmp(const TEE_BigInt *op1, const TEE_BigInt *op2)
246 {
247 	mbedtls_mpi mpi1;
248 	mbedtls_mpi mpi2;
249 	int32_t rc;
250 
251 	get_mpi(&mpi1, op1);
252 	get_mpi(&mpi2, op2);
253 
254 	rc = mbedtls_mpi_cmp_mpi(&mpi1, &mpi2);
255 
256 	mbedtls_mpi_free(&mpi1);
257 	mbedtls_mpi_free(&mpi2);
258 
259 	return rc;
260 }
261 
262 int32_t TEE_BigIntCmpS32(const TEE_BigInt *op, int32_t shortVal)
263 {
264 	mbedtls_mpi mpi;
265 	int32_t rc;
266 
267 	get_mpi(&mpi, op);
268 
269 	rc = mbedtls_mpi_cmp_int(&mpi, shortVal);
270 
271 	mbedtls_mpi_free(&mpi);
272 
273 	return rc;
274 }
275 
276 void TEE_BigIntShiftRight(TEE_BigInt *dest, const TEE_BigInt *op, size_t bits)
277 {
278 	mbedtls_mpi mpi_dest;
279 	mbedtls_mpi mpi_op;
280 
281 	get_mpi(&mpi_dest, dest);
282 
283 	if (dest == op) {
284 		MPI_CHECK(mbedtls_mpi_shift_r(&mpi_dest, bits));
285 		goto out;
286 	}
287 
288 	get_mpi(&mpi_op, op);
289 
290 	if (mbedtls_mpi_size(&mpi_dest) >= mbedtls_mpi_size(&mpi_op)) {
291 		MPI_CHECK(mbedtls_mpi_copy(&mpi_dest, &mpi_op));
292 		MPI_CHECK(mbedtls_mpi_shift_r(&mpi_dest, bits));
293 	} else {
294 		mbedtls_mpi mpi_t;
295 
296 		get_mpi(&mpi_t, NULL);
297 
298 		/*
299 		 * We're using a temporary buffer to avoid the corner case
300 		 * where destination is unexpectedly overflowed by up to
301 		 * @bits number of bits.
302 		 */
303 		MPI_CHECK(mbedtls_mpi_copy(&mpi_t, &mpi_op));
304 		MPI_CHECK(mbedtls_mpi_shift_r(&mpi_t, bits));
305 		MPI_CHECK(mbedtls_mpi_copy(&mpi_dest, &mpi_t));
306 
307 		mbedtls_mpi_free(&mpi_t);
308 	}
309 
310 	mbedtls_mpi_free(&mpi_op);
311 
312 out:
313 	MPI_CHECK(copy_mpi_to_bigint(&mpi_dest, dest));
314 	mbedtls_mpi_free(&mpi_dest);
315 }
316 
317 void __GP11_TEE_BigIntShiftRight(TEE_BigInt *dest, const TEE_BigInt *op,
318 				 uint32_t bits)
319 {
320 	TEE_BigIntShiftRight(dest, op, bits);
321 }
322 
323 bool TEE_BigIntGetBit(const TEE_BigInt *src, uint32_t bitIndex)
324 {
325 	bool rc;
326 	mbedtls_mpi mpi;
327 
328 	get_mpi(&mpi, src);
329 
330 	rc = mbedtls_mpi_get_bit(&mpi, bitIndex);
331 
332 	mbedtls_mpi_free(&mpi);
333 
334 	return rc;
335 }
336 
337 uint32_t TEE_BigIntGetBitCount(const TEE_BigInt *src)
338 {
339 	uint32_t rc;
340 	mbedtls_mpi mpi;
341 
342 	get_mpi(&mpi, src);
343 
344 	rc = mbedtls_mpi_bitlen(&mpi);
345 
346 	mbedtls_mpi_free(&mpi);
347 
348 	return rc;
349 }
350 
351 TEE_Result TEE_BigIntAssign(TEE_BigInt *dest, const TEE_BigInt *src)
352 {
353 	const struct bigint_hdr *src_hdr = (struct bigint_hdr *)src;
354 	struct bigint_hdr *dst_hdr = (struct bigint_hdr *)dest;
355 
356 	if (dst_hdr == src_hdr)
357 		return TEE_SUCCESS;
358 
359 	if (dst_hdr->alloc_size < src_hdr->nblimbs)
360 		return TEE_ERROR_OVERFLOW;
361 
362 	dst_hdr->nblimbs = src_hdr->nblimbs;
363 	dst_hdr->sign = src_hdr->sign;
364 	memcpy(dst_hdr + 1, src_hdr + 1, src_hdr->nblimbs * sizeof(uint32_t));
365 
366 	return TEE_SUCCESS;
367 }
368 
369 static void bigint_binary(TEE_BigInt *dest, const TEE_BigInt *op1,
370 			  const TEE_BigInt *op2,
371 			  int (*func)(mbedtls_mpi *X, const mbedtls_mpi *A,
372 				      const mbedtls_mpi *B))
373 {
374 	mbedtls_mpi mpi_dest;
375 	mbedtls_mpi mpi_op1;
376 	mbedtls_mpi mpi_op2;
377 	mbedtls_mpi *pop1 = &mpi_op1;
378 	mbedtls_mpi *pop2 = &mpi_op2;
379 
380 	get_mpi(&mpi_dest, dest);
381 
382 	if (op1 == dest)
383 		pop1 = &mpi_dest;
384 	else
385 		get_mpi(&mpi_op1, op1);
386 
387 	if (op2 == dest)
388 		pop2 = &mpi_dest;
389 	else if (op2 == op1)
390 		pop2 = pop1;
391 	else
392 		get_mpi(&mpi_op2, op2);
393 
394 	MPI_CHECK(func(&mpi_dest, pop1, pop2));
395 
396 	MPI_CHECK(copy_mpi_to_bigint(&mpi_dest, dest));
397 	mbedtls_mpi_free(&mpi_dest);
398 	if (pop1 == &mpi_op1)
399 		mbedtls_mpi_free(&mpi_op1);
400 	if (pop2 == &mpi_op2)
401 		mbedtls_mpi_free(&mpi_op2);
402 }
403 
404 static void bigint_binary_mod(TEE_BigInt *dest, const TEE_BigInt *op1,
405 			      const TEE_BigInt *op2, const TEE_BigInt *n,
406 			      int (*func)(mbedtls_mpi *X, const mbedtls_mpi *A,
407 					  const mbedtls_mpi *B))
408 {
409 	mbedtls_mpi mpi_dest;
410 	mbedtls_mpi mpi_op1;
411 	mbedtls_mpi mpi_op2;
412 	mbedtls_mpi mpi_n;
413 	mbedtls_mpi *pop1 = &mpi_op1;
414 	mbedtls_mpi *pop2 = &mpi_op2;
415 	mbedtls_mpi mpi_t;
416 
417 	if (TEE_BigIntCmpS32(n, 2) < 0)
418 		API_PANIC("Modulus is too short");
419 
420 	get_mpi(&mpi_dest, dest);
421 	get_mpi(&mpi_n, n);
422 
423 	if (op1 == dest)
424 		pop1 = &mpi_dest;
425 	else
426 		get_mpi(&mpi_op1, op1);
427 
428 	if (op2 == dest)
429 		pop2 = &mpi_dest;
430 	else if (op2 == op1)
431 		pop2 = pop1;
432 	else
433 		get_mpi(&mpi_op2, op2);
434 
435 	get_mpi(&mpi_t, NULL);
436 
437 	MPI_CHECK(func(&mpi_t, pop1, pop2));
438 	MPI_CHECK(mbedtls_mpi_mod_mpi(&mpi_dest, &mpi_t, &mpi_n));
439 
440 	MPI_CHECK(copy_mpi_to_bigint(&mpi_dest, dest));
441 	mbedtls_mpi_free(&mpi_dest);
442 	if (pop1 == &mpi_op1)
443 		mbedtls_mpi_free(&mpi_op1);
444 	if (pop2 == &mpi_op2)
445 		mbedtls_mpi_free(&mpi_op2);
446 	mbedtls_mpi_free(&mpi_t);
447 	mbedtls_mpi_free(&mpi_n);
448 }
449 
450 void TEE_BigIntAdd(TEE_BigInt *dest, const TEE_BigInt *op1,
451 		   const TEE_BigInt *op2)
452 {
453 	bigint_binary(dest, op1, op2, mbedtls_mpi_add_mpi);
454 }
455 
456 void TEE_BigIntSub(TEE_BigInt *dest, const TEE_BigInt *op1,
457 		   const TEE_BigInt *op2)
458 {
459 	bigint_binary(dest, op1, op2, mbedtls_mpi_sub_mpi);
460 }
461 
462 void TEE_BigIntNeg(TEE_BigInt *dest, const TEE_BigInt *src)
463 {
464 	mbedtls_mpi mpi_dest;
465 
466 	get_mpi(&mpi_dest, dest);
467 
468 	if (dest != src) {
469 		mbedtls_mpi mpi_src;
470 
471 		get_mpi(&mpi_src, src);
472 
473 		MPI_CHECK(mbedtls_mpi_copy(&mpi_dest, &mpi_src));
474 
475 		mbedtls_mpi_free(&mpi_src);
476 	}
477 
478 	mpi_dest.s *= -1;
479 
480 	MPI_CHECK(copy_mpi_to_bigint(&mpi_dest, dest));
481 	mbedtls_mpi_free(&mpi_dest);
482 }
483 
484 void TEE_BigIntMul(TEE_BigInt *dest, const TEE_BigInt *op1,
485 		   const TEE_BigInt *op2)
486 {
487 	size_t bs1 = TEE_BigIntGetBitCount(op1);
488 	size_t bs2 = TEE_BigIntGetBitCount(op2);
489 	size_t s = TEE_BigIntSizeInU32(bs1) + TEE_BigIntSizeInU32(bs2);
490 	TEE_BigInt zero[TEE_BigIntSizeInU32(1)] = { 0 };
491 	TEE_BigInt *tmp = NULL;
492 
493 	tmp = mempool_alloc(mbedtls_mpi_mempool, sizeof(uint32_t) * s);
494 	if (!tmp)
495 		TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);
496 
497 	TEE_BigIntInit(tmp, s);
498 	TEE_BigIntInit(zero, TEE_BigIntSizeInU32(1));
499 
500 	bigint_binary(tmp, op1, op2, mbedtls_mpi_mul_mpi);
501 
502 	TEE_BigIntAdd(dest, tmp, zero);
503 
504 	mempool_free(mbedtls_mpi_mempool, tmp);
505 }
506 
507 void TEE_BigIntSquare(TEE_BigInt *dest, const TEE_BigInt *op)
508 {
509 	TEE_BigIntMul(dest, op, op);
510 }
511 
512 void TEE_BigIntDiv(TEE_BigInt *dest_q, TEE_BigInt *dest_r,
513 		   const TEE_BigInt *op1, const TEE_BigInt *op2)
514 {
515 	mbedtls_mpi mpi_dest_q;
516 	mbedtls_mpi mpi_dest_r;
517 	mbedtls_mpi mpi_op1;
518 	mbedtls_mpi mpi_op2;
519 	mbedtls_mpi *pop1 = &mpi_op1;
520 	mbedtls_mpi *pop2 = &mpi_op2;
521 
522 	get_mpi(&mpi_dest_q, dest_q);
523 	get_mpi(&mpi_dest_r, dest_r);
524 
525 	if (op1 == dest_q)
526 		pop1 = &mpi_dest_q;
527 	else if (op1 == dest_r)
528 		pop1 = &mpi_dest_r;
529 	else
530 		get_mpi(&mpi_op1, op1);
531 
532 	if (op2 == dest_q)
533 		pop2 = &mpi_dest_q;
534 	else if (op2 == dest_r)
535 		pop2 = &mpi_dest_r;
536 	else if (op2 == op1)
537 		pop2 = pop1;
538 	else
539 		get_mpi(&mpi_op2, op2);
540 
541 	MPI_CHECK(mbedtls_mpi_div_mpi(&mpi_dest_q, &mpi_dest_r, pop1, pop2));
542 
543 	if (dest_q)
544 		MPI_CHECK(copy_mpi_to_bigint(&mpi_dest_q, dest_q));
545 	if (dest_r)
546 		MPI_CHECK(copy_mpi_to_bigint(&mpi_dest_r, dest_r));
547 	mbedtls_mpi_free(&mpi_dest_q);
548 	mbedtls_mpi_free(&mpi_dest_r);
549 	if (pop1 == &mpi_op1)
550 		mbedtls_mpi_free(&mpi_op1);
551 	if (pop2 == &mpi_op2)
552 		mbedtls_mpi_free(&mpi_op2);
553 }
554 
555 void TEE_BigIntMod(TEE_BigInt *dest, const TEE_BigInt *op, const TEE_BigInt *n)
556 {
557 	if (TEE_BigIntCmpS32(n, 2) < 0)
558 		API_PANIC("Modulus is too short");
559 
560 	bigint_binary(dest, op, n, mbedtls_mpi_mod_mpi);
561 }
562 
563 void TEE_BigIntAddMod(TEE_BigInt *dest, const TEE_BigInt *op1,
564 		      const TEE_BigInt *op2, const TEE_BigInt *n)
565 {
566 	bigint_binary_mod(dest, op1, op2, n, mbedtls_mpi_add_mpi);
567 }
568 
569 void TEE_BigIntSubMod(TEE_BigInt *dest, const TEE_BigInt *op1,
570 		      const TEE_BigInt *op2, const TEE_BigInt *n)
571 {
572 	bigint_binary_mod(dest, op1, op2, n, mbedtls_mpi_sub_mpi);
573 }
574 
575 void TEE_BigIntMulMod(TEE_BigInt *dest, const TEE_BigInt *op1,
576 		      const TEE_BigInt *op2, const TEE_BigInt *n)
577 {
578 	bigint_binary_mod(dest, op1, op2, n, mbedtls_mpi_mul_mpi);
579 }
580 
581 void TEE_BigIntSquareMod(TEE_BigInt *dest, const TEE_BigInt *op,
582 			 const TEE_BigInt *n)
583 {
584 	TEE_BigIntMulMod(dest, op, op, n);
585 }
586 
587 void TEE_BigIntInvMod(TEE_BigInt *dest, const TEE_BigInt *op,
588 		      const TEE_BigInt *n)
589 {
590 	mbedtls_mpi mpi_dest;
591 	mbedtls_mpi mpi_op;
592 	mbedtls_mpi mpi_n;
593 	mbedtls_mpi *pop = &mpi_op;
594 
595 	if (TEE_BigIntCmpS32(n, 2) < 0 || TEE_BigIntCmpS32(op, 0) == 0)
596 		API_PANIC("too small modulus or trying to invert zero");
597 
598 	get_mpi(&mpi_dest, dest);
599 	get_mpi(&mpi_n, n);
600 
601 	if (op == dest)
602 		pop = &mpi_dest;
603 	else
604 		get_mpi(&mpi_op, op);
605 
606 	MPI_CHECK(mbedtls_mpi_inv_mod(&mpi_dest, pop, &mpi_n));
607 
608 	MPI_CHECK(copy_mpi_to_bigint(&mpi_dest, dest));
609 	mbedtls_mpi_free(&mpi_dest);
610 	mbedtls_mpi_free(&mpi_n);
611 	if (pop == &mpi_op)
612 		mbedtls_mpi_free(&mpi_op);
613 }
614 
615 bool TEE_BigIntRelativePrime(const TEE_BigInt *op1, const TEE_BigInt *op2)
616 {
617 	bool rc;
618 	mbedtls_mpi mpi_op1;
619 	mbedtls_mpi mpi_op2;
620 	mbedtls_mpi *pop2 = &mpi_op2;
621 	mbedtls_mpi gcd;
622 
623 	get_mpi(&mpi_op1, op1);
624 
625 	if (op2 == op1)
626 		pop2 = &mpi_op1;
627 	else
628 		get_mpi(&mpi_op2, op2);
629 
630 	get_mpi(&gcd, NULL);
631 
632 	MPI_CHECK(mbedtls_mpi_gcd(&gcd, &mpi_op1, &mpi_op2));
633 
634 	rc = !mbedtls_mpi_cmp_int(&gcd, 1);
635 
636 	mbedtls_mpi_free(&gcd);
637 	mbedtls_mpi_free(&mpi_op1);
638 	if (pop2 == &mpi_op2)
639 		mbedtls_mpi_free(&mpi_op2);
640 
641 	return rc;
642 }
643 
644 static bool mpi_is_odd(mbedtls_mpi *x)
645 {
646 	return mbedtls_mpi_get_bit(x, 0);
647 }
648 
649 static bool mpi_is_even(mbedtls_mpi *x)
650 {
651 	return !mpi_is_odd(x);
652 }
653 
654 /*
655  * Based on libmpa implementation __mpa_egcd(), modified to work with MPI
656  * instead.
657  */
658 static void mpi_egcd(mbedtls_mpi *gcd, mbedtls_mpi *a, mbedtls_mpi *b,
659 		     mbedtls_mpi *x_in, mbedtls_mpi *y_in)
660 {
661 	mbedtls_mpi_uint k;
662 	mbedtls_mpi A;
663 	mbedtls_mpi B;
664 	mbedtls_mpi C;
665 	mbedtls_mpi D;
666 	mbedtls_mpi x;
667 	mbedtls_mpi y;
668 	mbedtls_mpi u;
669 
670 	get_mpi(&A, NULL);
671 	get_mpi(&B, NULL);
672 	get_mpi(&C, NULL);
673 	get_mpi(&D, NULL);
674 	get_mpi(&x, NULL);
675 	get_mpi(&y, NULL);
676 	get_mpi(&u, NULL);
677 
678 	/* have y < x from assumption */
679 	if (!mbedtls_mpi_cmp_int(y_in, 0)) {
680 		MPI_CHECK(mbedtls_mpi_lset(a, 1));
681 		MPI_CHECK(mbedtls_mpi_lset(b, 0));
682 		MPI_CHECK(mbedtls_mpi_copy(gcd, x_in));
683 		goto out;
684 	}
685 
686 	MPI_CHECK(mbedtls_mpi_copy(&x, x_in));
687 	MPI_CHECK(mbedtls_mpi_copy(&y, y_in));
688 
689 	k = 0;
690 	while (mpi_is_even(&x) && mpi_is_even(&y)) {
691 		k++;
692 		MPI_CHECK(mbedtls_mpi_shift_r(&x, 1));
693 		MPI_CHECK(mbedtls_mpi_shift_r(&y, 1));
694 	}
695 
696 	MPI_CHECK(mbedtls_mpi_copy(&u, &x));
697 	MPI_CHECK(mbedtls_mpi_copy(gcd, &y));
698 	MPI_CHECK(mbedtls_mpi_lset(&A, 1));
699 	MPI_CHECK(mbedtls_mpi_lset(&B, 0));
700 	MPI_CHECK(mbedtls_mpi_lset(&C, 0));
701 	MPI_CHECK(mbedtls_mpi_lset(&D, 1));
702 
703 	while (mbedtls_mpi_cmp_int(&u, 0)) {
704 		while (mpi_is_even(&u)) {
705 			MPI_CHECK(mbedtls_mpi_shift_r(&u, 1));
706 			if (mpi_is_odd(&A) || mpi_is_odd(&B)) {
707 				MPI_CHECK(mbedtls_mpi_add_mpi(&A, &A, &y));
708 				MPI_CHECK(mbedtls_mpi_sub_mpi(&B, &B, &x));
709 			}
710 			MPI_CHECK(mbedtls_mpi_shift_r(&A, 1));
711 			MPI_CHECK(mbedtls_mpi_shift_r(&B, 1));
712 		}
713 
714 		while (mpi_is_even(gcd)) {
715 			MPI_CHECK(mbedtls_mpi_shift_r(gcd, 1));
716 			if (mpi_is_odd(&C) || mpi_is_odd(&D)) {
717 				MPI_CHECK(mbedtls_mpi_add_mpi(&C, &C, &y));
718 				MPI_CHECK(mbedtls_mpi_sub_mpi(&D, &D, &x));
719 			}
720 			MPI_CHECK(mbedtls_mpi_shift_r(&C, 1));
721 			MPI_CHECK(mbedtls_mpi_shift_r(&D, 1));
722 
723 		}
724 
725 		if (mbedtls_mpi_cmp_mpi(&u, gcd) >= 0) {
726 			MPI_CHECK(mbedtls_mpi_sub_mpi(&u, &u, gcd));
727 			MPI_CHECK(mbedtls_mpi_sub_mpi(&A, &A, &C));
728 			MPI_CHECK(mbedtls_mpi_sub_mpi(&B, &B, &D));
729 		} else {
730 			MPI_CHECK(mbedtls_mpi_sub_mpi(gcd, gcd, &u));
731 			MPI_CHECK(mbedtls_mpi_sub_mpi(&C, &C, &A));
732 			MPI_CHECK(mbedtls_mpi_sub_mpi(&D, &D, &B));
733 		}
734 	}
735 
736 	MPI_CHECK(mbedtls_mpi_copy(a, &C));
737 	MPI_CHECK(mbedtls_mpi_copy(b, &D));
738 	MPI_CHECK(mbedtls_mpi_shift_l(gcd, k));
739 
740 out:
741 	mbedtls_mpi_free(&A);
742 	mbedtls_mpi_free(&B);
743 	mbedtls_mpi_free(&C);
744 	mbedtls_mpi_free(&D);
745 	mbedtls_mpi_free(&x);
746 	mbedtls_mpi_free(&y);
747 	mbedtls_mpi_free(&u);
748 }
749 
750 void TEE_BigIntComputeExtendedGcd(TEE_BigInt *gcd, TEE_BigInt *u,
751 				  TEE_BigInt *v, const TEE_BigInt *op1,
752 				  const TEE_BigInt *op2)
753 {
754 	mbedtls_mpi mpi_gcd_res;
755 	mbedtls_mpi mpi_op1;
756 	mbedtls_mpi mpi_op2;
757 	mbedtls_mpi *pop2 = &mpi_op2;
758 
759 	get_mpi(&mpi_gcd_res, gcd);
760 	get_mpi(&mpi_op1, op1);
761 
762 	if (op2 == op1)
763 		pop2 = &mpi_op1;
764 	else
765 		get_mpi(&mpi_op2, op2);
766 
767 	if (!u && !v) {
768 		MPI_CHECK(mbedtls_mpi_gcd(&mpi_gcd_res, &mpi_op1, pop2));
769 	} else {
770 		mbedtls_mpi mpi_u;
771 		mbedtls_mpi mpi_v;
772 		int8_t s1 = mpi_op1.s;
773 		int8_t s2 = pop2->s;
774 		int cmp;
775 
776 		mpi_op1.s = 1;
777 		pop2->s = 1;
778 
779 		get_mpi(&mpi_u, u);
780 		get_mpi(&mpi_v, v);
781 
782 		cmp = mbedtls_mpi_cmp_abs(&mpi_op1, pop2);
783 		if (cmp == 0) {
784 			MPI_CHECK(mbedtls_mpi_copy(&mpi_gcd_res, &mpi_op1));
785 			MPI_CHECK(mbedtls_mpi_lset(&mpi_u, 1));
786 			MPI_CHECK(mbedtls_mpi_lset(&mpi_v, 0));
787 		} else if (cmp > 0) {
788 			mpi_egcd(&mpi_gcd_res, &mpi_u, &mpi_v, &mpi_op1, pop2);
789 		} else {
790 			mpi_egcd(&mpi_gcd_res, &mpi_v, &mpi_u, pop2, &mpi_op1);
791 		}
792 
793 		mpi_u.s *= s1;
794 		mpi_v.s *= s2;
795 
796 		MPI_CHECK(copy_mpi_to_bigint(&mpi_u, u));
797 		MPI_CHECK(copy_mpi_to_bigint(&mpi_v, v));
798 		mbedtls_mpi_free(&mpi_u);
799 		mbedtls_mpi_free(&mpi_v);
800 	}
801 
802 	MPI_CHECK(copy_mpi_to_bigint(&mpi_gcd_res, gcd));
803 	mbedtls_mpi_free(&mpi_gcd_res);
804 	mbedtls_mpi_free(&mpi_op1);
805 	if (pop2 == &mpi_op2)
806 		mbedtls_mpi_free(&mpi_op2);
807 }
808 
809 static int rng_read(void *ignored __unused, unsigned char *buf, size_t blen)
810 {
811 	if (_utee_cryp_random_number_generate(buf, blen))
812 		return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
813 	return 0;
814 }
815 
816 int32_t TEE_BigIntIsProbablePrime(const TEE_BigInt *op,
817 				  uint32_t confidenceLevel __unused)
818 {
819 	int rc;
820 	mbedtls_mpi mpi_op;
821 
822 	get_mpi(&mpi_op, op);
823 
824 	rc = mbedtls_mpi_is_prime(&mpi_op, rng_read, NULL);
825 
826 	mbedtls_mpi_free(&mpi_op);
827 
828 	if (rc)
829 		return 0;
830 
831 	return 1;
832 }
833 
834 /*
835  * Not so fast FMM implementation based on the normal big int functions.
836  *
837  * Note that these functions (along with all the other functions in this
838  * file) only are used directly by the TA doing bigint arithmetics on its
839  * own. Performance of RSA operations in TEE Internal API are not affected
840  * by this.
841  */
842 void TEE_BigIntInitFMM(TEE_BigIntFMM *bigIntFMM, size_t len)
843 {
844 	TEE_BigIntInit(bigIntFMM, len);
845 }
846 
847 void __GP11_TEE_BigIntInitFMM(TEE_BigIntFMM *bigIntFMM, uint32_t len)
848 {
849 	TEE_BigIntInitFMM(bigIntFMM, len);
850 }
851 
852 void TEE_BigIntInitFMMContext(TEE_BigIntFMMContext *context __unused,
853 			      size_t len __unused,
854 			      const TEE_BigInt *modulus __unused)
855 {
856 }
857 
858 void __GP11_TEE_BigIntInitFMMContext(TEE_BigIntFMMContext *context,
859 				     uint32_t len, const TEE_BigInt *modulus)
860 {
861 	TEE_BigIntInitFMMContext(context, len, modulus);
862 }
863 
864 TEE_Result TEE_BigIntInitFMMContext1(TEE_BigIntFMMContext *context __unused,
865 				     size_t len __unused,
866 				     const TEE_BigInt *modulus __unused)
867 {
868 	return TEE_SUCCESS;
869 }
870 
871 size_t TEE_BigIntFMMSizeInU32(size_t modulusSizeInBits)
872 {
873 	return TEE_BigIntSizeInU32(modulusSizeInBits);
874 }
875 
876 uint32_t __GP11_TEE_BigIntFMMSizeInU32(uint32_t modulusSizeInBits)
877 {
878 	return TEE_BigIntFMMSizeInU32(modulusSizeInBits);
879 }
880 
881 size_t TEE_BigIntFMMContextSizeInU32(size_t modulusSizeInBits __unused)
882 {
883 	/* Return something larger than 0 to keep malloc() and friends happy */
884 	return 1;
885 }
886 
887 uint32_t __GP11_TEE_BigIntFMMContextSizeInU32(uint32_t modulusSizeInBits)
888 {
889 	return TEE_BigIntFMMContextSizeInU32(modulusSizeInBits);
890 }
891 
892 void TEE_BigIntConvertToFMM(TEE_BigIntFMM *dest, const TEE_BigInt *src,
893 			    const TEE_BigInt *n,
894 			    const TEE_BigIntFMMContext *context __unused)
895 {
896 	TEE_BigIntMod(dest, src, n);
897 }
898 
899 void TEE_BigIntConvertFromFMM(TEE_BigInt *dest, const TEE_BigIntFMM *src,
900 			      const TEE_BigInt *n __unused,
901 			      const TEE_BigIntFMMContext *context __unused)
902 {
903 	mbedtls_mpi mpi_dst;
904 	mbedtls_mpi mpi_src;
905 
906 	get_mpi(&mpi_dst, dest);
907 	get_mpi(&mpi_src, src);
908 
909 	MPI_CHECK(mbedtls_mpi_copy(&mpi_dst, &mpi_src));
910 
911 	MPI_CHECK(copy_mpi_to_bigint(&mpi_dst, dest));
912 	mbedtls_mpi_free(&mpi_dst);
913 	mbedtls_mpi_free(&mpi_src);
914 }
915 
916 void TEE_BigIntComputeFMM(TEE_BigIntFMM *dest, const TEE_BigIntFMM *op1,
917 			  const TEE_BigIntFMM *op2, const TEE_BigInt *n,
918 			  const TEE_BigIntFMMContext *context __unused)
919 {
920 	mbedtls_mpi mpi_dst;
921 	mbedtls_mpi mpi_op1;
922 	mbedtls_mpi mpi_op2;
923 	mbedtls_mpi mpi_n;
924 	mbedtls_mpi mpi_t;
925 
926 	get_mpi(&mpi_dst, dest);
927 	get_mpi(&mpi_op1, op1);
928 	get_mpi(&mpi_op2, op2);
929 	get_mpi(&mpi_n, n);
930 	get_mpi(&mpi_t, NULL);
931 
932 	MPI_CHECK(mbedtls_mpi_mul_mpi(&mpi_t, &mpi_op1, &mpi_op2));
933 	MPI_CHECK(mbedtls_mpi_mod_mpi(&mpi_dst, &mpi_t, &mpi_n));
934 
935 	mbedtls_mpi_free(&mpi_t);
936 	mbedtls_mpi_free(&mpi_n);
937 	mbedtls_mpi_free(&mpi_op2);
938 	mbedtls_mpi_free(&mpi_op1);
939 	MPI_CHECK(copy_mpi_to_bigint(&mpi_dst, dest));
940 	mbedtls_mpi_free(&mpi_dst);
941 }
942