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 static void bigint_binary(TEE_BigInt *dest, const TEE_BigInt *op1, 352 const TEE_BigInt *op2, 353 int (*func)(mbedtls_mpi *X, const mbedtls_mpi *A, 354 const mbedtls_mpi *B)) 355 { 356 mbedtls_mpi mpi_dest; 357 mbedtls_mpi mpi_op1; 358 mbedtls_mpi mpi_op2; 359 mbedtls_mpi *pop1 = &mpi_op1; 360 mbedtls_mpi *pop2 = &mpi_op2; 361 362 get_mpi(&mpi_dest, dest); 363 364 if (op1 == dest) 365 pop1 = &mpi_dest; 366 else 367 get_mpi(&mpi_op1, op1); 368 369 if (op2 == dest) 370 pop2 = &mpi_dest; 371 else if (op2 == op1) 372 pop2 = pop1; 373 else 374 get_mpi(&mpi_op2, op2); 375 376 MPI_CHECK(func(&mpi_dest, pop1, pop2)); 377 378 MPI_CHECK(copy_mpi_to_bigint(&mpi_dest, dest)); 379 mbedtls_mpi_free(&mpi_dest); 380 if (pop1 == &mpi_op1) 381 mbedtls_mpi_free(&mpi_op1); 382 if (pop2 == &mpi_op2) 383 mbedtls_mpi_free(&mpi_op2); 384 } 385 386 static void bigint_binary_mod(TEE_BigInt *dest, const TEE_BigInt *op1, 387 const TEE_BigInt *op2, const TEE_BigInt *n, 388 int (*func)(mbedtls_mpi *X, const mbedtls_mpi *A, 389 const mbedtls_mpi *B)) 390 { 391 mbedtls_mpi mpi_dest; 392 mbedtls_mpi mpi_op1; 393 mbedtls_mpi mpi_op2; 394 mbedtls_mpi mpi_n; 395 mbedtls_mpi *pop1 = &mpi_op1; 396 mbedtls_mpi *pop2 = &mpi_op2; 397 mbedtls_mpi mpi_t; 398 399 if (TEE_BigIntCmpS32(n, 2) < 0) 400 API_PANIC("Modulus is too short"); 401 402 get_mpi(&mpi_dest, dest); 403 get_mpi(&mpi_n, n); 404 405 if (op1 == dest) 406 pop1 = &mpi_dest; 407 else 408 get_mpi(&mpi_op1, op1); 409 410 if (op2 == dest) 411 pop2 = &mpi_dest; 412 else if (op2 == op1) 413 pop2 = pop1; 414 else 415 get_mpi(&mpi_op2, op2); 416 417 get_mpi(&mpi_t, NULL); 418 419 MPI_CHECK(func(&mpi_t, pop1, pop2)); 420 MPI_CHECK(mbedtls_mpi_mod_mpi(&mpi_dest, &mpi_t, &mpi_n)); 421 422 MPI_CHECK(copy_mpi_to_bigint(&mpi_dest, dest)); 423 mbedtls_mpi_free(&mpi_dest); 424 if (pop1 == &mpi_op1) 425 mbedtls_mpi_free(&mpi_op1); 426 if (pop2 == &mpi_op2) 427 mbedtls_mpi_free(&mpi_op2); 428 mbedtls_mpi_free(&mpi_t); 429 mbedtls_mpi_free(&mpi_n); 430 } 431 432 void TEE_BigIntAdd(TEE_BigInt *dest, const TEE_BigInt *op1, 433 const TEE_BigInt *op2) 434 { 435 bigint_binary(dest, op1, op2, mbedtls_mpi_add_mpi); 436 } 437 438 void TEE_BigIntSub(TEE_BigInt *dest, const TEE_BigInt *op1, 439 const TEE_BigInt *op2) 440 { 441 bigint_binary(dest, op1, op2, mbedtls_mpi_sub_mpi); 442 } 443 444 void TEE_BigIntNeg(TEE_BigInt *dest, const TEE_BigInt *src) 445 { 446 mbedtls_mpi mpi_dest; 447 448 get_mpi(&mpi_dest, dest); 449 450 if (dest != src) { 451 mbedtls_mpi mpi_src; 452 453 get_mpi(&mpi_src, src); 454 455 MPI_CHECK(mbedtls_mpi_copy(&mpi_dest, &mpi_src)); 456 457 mbedtls_mpi_free(&mpi_src); 458 } 459 460 mpi_dest.s *= -1; 461 462 MPI_CHECK(copy_mpi_to_bigint(&mpi_dest, dest)); 463 mbedtls_mpi_free(&mpi_dest); 464 } 465 466 void TEE_BigIntMul(TEE_BigInt *dest, const TEE_BigInt *op1, 467 const TEE_BigInt *op2) 468 { 469 size_t bs1 = TEE_BigIntGetBitCount(op1); 470 size_t bs2 = TEE_BigIntGetBitCount(op2); 471 size_t s = TEE_BigIntSizeInU32(bs1) + TEE_BigIntSizeInU32(bs2); 472 TEE_BigInt zero[TEE_BigIntSizeInU32(1)] = { 0 }; 473 TEE_BigInt *tmp = NULL; 474 475 tmp = mempool_alloc(mbedtls_mpi_mempool, sizeof(uint32_t) * s); 476 if (!tmp) 477 TEE_Panic(TEE_ERROR_OUT_OF_MEMORY); 478 479 TEE_BigIntInit(tmp, s); 480 TEE_BigIntInit(zero, TEE_BigIntSizeInU32(1)); 481 482 bigint_binary(tmp, op1, op2, mbedtls_mpi_mul_mpi); 483 484 TEE_BigIntAdd(dest, tmp, zero); 485 486 mempool_free(mbedtls_mpi_mempool, tmp); 487 } 488 489 void TEE_BigIntSquare(TEE_BigInt *dest, const TEE_BigInt *op) 490 { 491 TEE_BigIntMul(dest, op, op); 492 } 493 494 void TEE_BigIntDiv(TEE_BigInt *dest_q, TEE_BigInt *dest_r, 495 const TEE_BigInt *op1, const TEE_BigInt *op2) 496 { 497 mbedtls_mpi mpi_dest_q; 498 mbedtls_mpi mpi_dest_r; 499 mbedtls_mpi mpi_op1; 500 mbedtls_mpi mpi_op2; 501 mbedtls_mpi *pop1 = &mpi_op1; 502 mbedtls_mpi *pop2 = &mpi_op2; 503 504 get_mpi(&mpi_dest_q, dest_q); 505 get_mpi(&mpi_dest_r, dest_r); 506 507 if (op1 == dest_q) 508 pop1 = &mpi_dest_q; 509 else if (op1 == dest_r) 510 pop1 = &mpi_dest_r; 511 else 512 get_mpi(&mpi_op1, op1); 513 514 if (op2 == dest_q) 515 pop2 = &mpi_dest_q; 516 else if (op2 == dest_r) 517 pop2 = &mpi_dest_r; 518 else if (op2 == op1) 519 pop2 = pop1; 520 else 521 get_mpi(&mpi_op2, op2); 522 523 MPI_CHECK(mbedtls_mpi_div_mpi(&mpi_dest_q, &mpi_dest_r, pop1, pop2)); 524 525 if (dest_q) 526 MPI_CHECK(copy_mpi_to_bigint(&mpi_dest_q, dest_q)); 527 if (dest_r) 528 MPI_CHECK(copy_mpi_to_bigint(&mpi_dest_r, dest_r)); 529 mbedtls_mpi_free(&mpi_dest_q); 530 mbedtls_mpi_free(&mpi_dest_r); 531 if (pop1 == &mpi_op1) 532 mbedtls_mpi_free(&mpi_op1); 533 if (pop2 == &mpi_op2) 534 mbedtls_mpi_free(&mpi_op2); 535 } 536 537 void TEE_BigIntMod(TEE_BigInt *dest, const TEE_BigInt *op, const TEE_BigInt *n) 538 { 539 if (TEE_BigIntCmpS32(n, 2) < 0) 540 API_PANIC("Modulus is too short"); 541 542 bigint_binary(dest, op, n, mbedtls_mpi_mod_mpi); 543 } 544 545 void TEE_BigIntAddMod(TEE_BigInt *dest, const TEE_BigInt *op1, 546 const TEE_BigInt *op2, const TEE_BigInt *n) 547 { 548 bigint_binary_mod(dest, op1, op2, n, mbedtls_mpi_add_mpi); 549 } 550 551 void TEE_BigIntSubMod(TEE_BigInt *dest, const TEE_BigInt *op1, 552 const TEE_BigInt *op2, const TEE_BigInt *n) 553 { 554 bigint_binary_mod(dest, op1, op2, n, mbedtls_mpi_sub_mpi); 555 } 556 557 void TEE_BigIntMulMod(TEE_BigInt *dest, const TEE_BigInt *op1, 558 const TEE_BigInt *op2, const TEE_BigInt *n) 559 { 560 bigint_binary_mod(dest, op1, op2, n, mbedtls_mpi_mul_mpi); 561 } 562 563 void TEE_BigIntSquareMod(TEE_BigInt *dest, const TEE_BigInt *op, 564 const TEE_BigInt *n) 565 { 566 TEE_BigIntMulMod(dest, op, op, n); 567 } 568 569 void TEE_BigIntInvMod(TEE_BigInt *dest, const TEE_BigInt *op, 570 const TEE_BigInt *n) 571 { 572 mbedtls_mpi mpi_dest; 573 mbedtls_mpi mpi_op; 574 mbedtls_mpi mpi_n; 575 mbedtls_mpi *pop = &mpi_op; 576 577 if (TEE_BigIntCmpS32(n, 2) < 0 || TEE_BigIntCmpS32(op, 0) == 0) 578 API_PANIC("too small modulus or trying to invert zero"); 579 580 get_mpi(&mpi_dest, dest); 581 get_mpi(&mpi_n, n); 582 583 if (op == dest) 584 pop = &mpi_dest; 585 else 586 get_mpi(&mpi_op, op); 587 588 MPI_CHECK(mbedtls_mpi_inv_mod(&mpi_dest, pop, &mpi_n)); 589 590 MPI_CHECK(copy_mpi_to_bigint(&mpi_dest, dest)); 591 mbedtls_mpi_free(&mpi_dest); 592 mbedtls_mpi_free(&mpi_n); 593 if (pop == &mpi_op) 594 mbedtls_mpi_free(&mpi_op); 595 } 596 597 bool TEE_BigIntRelativePrime(const TEE_BigInt *op1, const TEE_BigInt *op2) 598 { 599 bool rc; 600 mbedtls_mpi mpi_op1; 601 mbedtls_mpi mpi_op2; 602 mbedtls_mpi *pop2 = &mpi_op2; 603 mbedtls_mpi gcd; 604 605 get_mpi(&mpi_op1, op1); 606 607 if (op2 == op1) 608 pop2 = &mpi_op1; 609 else 610 get_mpi(&mpi_op2, op2); 611 612 get_mpi(&gcd, NULL); 613 614 MPI_CHECK(mbedtls_mpi_gcd(&gcd, &mpi_op1, &mpi_op2)); 615 616 rc = !mbedtls_mpi_cmp_int(&gcd, 1); 617 618 mbedtls_mpi_free(&gcd); 619 mbedtls_mpi_free(&mpi_op1); 620 if (pop2 == &mpi_op2) 621 mbedtls_mpi_free(&mpi_op2); 622 623 return rc; 624 } 625 626 static bool mpi_is_odd(mbedtls_mpi *x) 627 { 628 return mbedtls_mpi_get_bit(x, 0); 629 } 630 631 static bool mpi_is_even(mbedtls_mpi *x) 632 { 633 return !mpi_is_odd(x); 634 } 635 636 /* 637 * Based on libmpa implementation __mpa_egcd(), modified to work with MPI 638 * instead. 639 */ 640 static void mpi_egcd(mbedtls_mpi *gcd, mbedtls_mpi *a, mbedtls_mpi *b, 641 mbedtls_mpi *x_in, mbedtls_mpi *y_in) 642 { 643 mbedtls_mpi_uint k; 644 mbedtls_mpi A; 645 mbedtls_mpi B; 646 mbedtls_mpi C; 647 mbedtls_mpi D; 648 mbedtls_mpi x; 649 mbedtls_mpi y; 650 mbedtls_mpi u; 651 652 get_mpi(&A, NULL); 653 get_mpi(&B, NULL); 654 get_mpi(&C, NULL); 655 get_mpi(&D, NULL); 656 get_mpi(&x, NULL); 657 get_mpi(&y, NULL); 658 get_mpi(&u, NULL); 659 660 /* have y < x from assumption */ 661 if (!mbedtls_mpi_cmp_int(y_in, 0)) { 662 MPI_CHECK(mbedtls_mpi_lset(a, 1)); 663 MPI_CHECK(mbedtls_mpi_lset(b, 0)); 664 MPI_CHECK(mbedtls_mpi_copy(gcd, x_in)); 665 goto out; 666 } 667 668 MPI_CHECK(mbedtls_mpi_copy(&x, x_in)); 669 MPI_CHECK(mbedtls_mpi_copy(&y, y_in)); 670 671 k = 0; 672 while (mpi_is_even(&x) && mpi_is_even(&y)) { 673 k++; 674 MPI_CHECK(mbedtls_mpi_shift_r(&x, 1)); 675 MPI_CHECK(mbedtls_mpi_shift_r(&y, 1)); 676 } 677 678 MPI_CHECK(mbedtls_mpi_copy(&u, &x)); 679 MPI_CHECK(mbedtls_mpi_copy(gcd, &y)); 680 MPI_CHECK(mbedtls_mpi_lset(&A, 1)); 681 MPI_CHECK(mbedtls_mpi_lset(&B, 0)); 682 MPI_CHECK(mbedtls_mpi_lset(&C, 0)); 683 MPI_CHECK(mbedtls_mpi_lset(&D, 1)); 684 685 while (mbedtls_mpi_cmp_int(&u, 0)) { 686 while (mpi_is_even(&u)) { 687 MPI_CHECK(mbedtls_mpi_shift_r(&u, 1)); 688 if (mpi_is_odd(&A) || mpi_is_odd(&B)) { 689 MPI_CHECK(mbedtls_mpi_add_mpi(&A, &A, &y)); 690 MPI_CHECK(mbedtls_mpi_sub_mpi(&B, &B, &x)); 691 } 692 MPI_CHECK(mbedtls_mpi_shift_r(&A, 1)); 693 MPI_CHECK(mbedtls_mpi_shift_r(&B, 1)); 694 } 695 696 while (mpi_is_even(gcd)) { 697 MPI_CHECK(mbedtls_mpi_shift_r(gcd, 1)); 698 if (mpi_is_odd(&C) || mpi_is_odd(&D)) { 699 MPI_CHECK(mbedtls_mpi_add_mpi(&C, &C, &y)); 700 MPI_CHECK(mbedtls_mpi_sub_mpi(&D, &D, &x)); 701 } 702 MPI_CHECK(mbedtls_mpi_shift_r(&C, 1)); 703 MPI_CHECK(mbedtls_mpi_shift_r(&D, 1)); 704 705 } 706 707 if (mbedtls_mpi_cmp_mpi(&u, gcd) >= 0) { 708 MPI_CHECK(mbedtls_mpi_sub_mpi(&u, &u, gcd)); 709 MPI_CHECK(mbedtls_mpi_sub_mpi(&A, &A, &C)); 710 MPI_CHECK(mbedtls_mpi_sub_mpi(&B, &B, &D)); 711 } else { 712 MPI_CHECK(mbedtls_mpi_sub_mpi(gcd, gcd, &u)); 713 MPI_CHECK(mbedtls_mpi_sub_mpi(&C, &C, &A)); 714 MPI_CHECK(mbedtls_mpi_sub_mpi(&D, &D, &B)); 715 } 716 } 717 718 MPI_CHECK(mbedtls_mpi_copy(a, &C)); 719 MPI_CHECK(mbedtls_mpi_copy(b, &D)); 720 MPI_CHECK(mbedtls_mpi_shift_l(gcd, k)); 721 722 out: 723 mbedtls_mpi_free(&A); 724 mbedtls_mpi_free(&B); 725 mbedtls_mpi_free(&C); 726 mbedtls_mpi_free(&D); 727 mbedtls_mpi_free(&x); 728 mbedtls_mpi_free(&y); 729 mbedtls_mpi_free(&u); 730 } 731 732 void TEE_BigIntComputeExtendedGcd(TEE_BigInt *gcd, TEE_BigInt *u, 733 TEE_BigInt *v, const TEE_BigInt *op1, 734 const TEE_BigInt *op2) 735 { 736 mbedtls_mpi mpi_gcd_res; 737 mbedtls_mpi mpi_op1; 738 mbedtls_mpi mpi_op2; 739 mbedtls_mpi *pop2 = &mpi_op2; 740 741 get_mpi(&mpi_gcd_res, gcd); 742 get_mpi(&mpi_op1, op1); 743 744 if (op2 == op1) 745 pop2 = &mpi_op1; 746 else 747 get_mpi(&mpi_op2, op2); 748 749 if (!u && !v) { 750 MPI_CHECK(mbedtls_mpi_gcd(&mpi_gcd_res, &mpi_op1, pop2)); 751 } else { 752 mbedtls_mpi mpi_u; 753 mbedtls_mpi mpi_v; 754 int8_t s1 = mpi_op1.s; 755 int8_t s2 = pop2->s; 756 int cmp; 757 758 mpi_op1.s = 1; 759 pop2->s = 1; 760 761 get_mpi(&mpi_u, u); 762 get_mpi(&mpi_v, v); 763 764 cmp = mbedtls_mpi_cmp_abs(&mpi_op1, pop2); 765 if (cmp == 0) { 766 MPI_CHECK(mbedtls_mpi_copy(&mpi_gcd_res, &mpi_op1)); 767 MPI_CHECK(mbedtls_mpi_lset(&mpi_u, 1)); 768 MPI_CHECK(mbedtls_mpi_lset(&mpi_v, 0)); 769 } else if (cmp > 0) { 770 mpi_egcd(&mpi_gcd_res, &mpi_u, &mpi_v, &mpi_op1, pop2); 771 } else { 772 mpi_egcd(&mpi_gcd_res, &mpi_v, &mpi_u, pop2, &mpi_op1); 773 } 774 775 mpi_u.s *= s1; 776 mpi_v.s *= s2; 777 778 MPI_CHECK(copy_mpi_to_bigint(&mpi_u, u)); 779 MPI_CHECK(copy_mpi_to_bigint(&mpi_v, v)); 780 mbedtls_mpi_free(&mpi_u); 781 mbedtls_mpi_free(&mpi_v); 782 } 783 784 MPI_CHECK(copy_mpi_to_bigint(&mpi_gcd_res, gcd)); 785 mbedtls_mpi_free(&mpi_gcd_res); 786 mbedtls_mpi_free(&mpi_op1); 787 if (pop2 == &mpi_op2) 788 mbedtls_mpi_free(&mpi_op2); 789 } 790 791 static int rng_read(void *ignored __unused, unsigned char *buf, size_t blen) 792 { 793 if (_utee_cryp_random_number_generate(buf, blen)) 794 return MBEDTLS_ERR_MPI_FILE_IO_ERROR; 795 return 0; 796 } 797 798 int32_t TEE_BigIntIsProbablePrime(const TEE_BigInt *op, 799 uint32_t confidenceLevel __unused) 800 { 801 int rc; 802 mbedtls_mpi mpi_op; 803 804 get_mpi(&mpi_op, op); 805 806 rc = mbedtls_mpi_is_prime(&mpi_op, rng_read, NULL); 807 808 mbedtls_mpi_free(&mpi_op); 809 810 if (rc) 811 return 0; 812 813 return 1; 814 } 815 816 /* 817 * Not so fast FMM implementation based on the normal big int functions. 818 * 819 * Note that these functions (along with all the other functions in this 820 * file) only are used directly by the TA doing bigint arithmetics on its 821 * own. Performance of RSA operations in TEE Internal API are not affected 822 * by this. 823 */ 824 void TEE_BigIntInitFMM(TEE_BigIntFMM *bigIntFMM, size_t len) 825 { 826 TEE_BigIntInit(bigIntFMM, len); 827 } 828 829 void __GP11_TEE_BigIntInitFMM(TEE_BigIntFMM *bigIntFMM, uint32_t len) 830 { 831 TEE_BigIntInitFMM(bigIntFMM, len); 832 } 833 834 void TEE_BigIntInitFMMContext(TEE_BigIntFMMContext *context __unused, 835 size_t len __unused, 836 const TEE_BigInt *modulus __unused) 837 { 838 } 839 840 void __GP11_TEE_BigIntInitFMMContext(TEE_BigIntFMMContext *context, 841 uint32_t len, const TEE_BigInt *modulus) 842 { 843 TEE_BigIntInitFMMContext(context, len, modulus); 844 } 845 846 TEE_Result TEE_BigIntInitFMMContext1(TEE_BigIntFMMContext *context __unused, 847 size_t len __unused, 848 const TEE_BigInt *modulus __unused) 849 { 850 return TEE_SUCCESS; 851 } 852 853 size_t TEE_BigIntFMMSizeInU32(size_t modulusSizeInBits) 854 { 855 return TEE_BigIntSizeInU32(modulusSizeInBits); 856 } 857 858 uint32_t __GP11_TEE_BigIntFMMSizeInU32(uint32_t modulusSizeInBits) 859 { 860 return TEE_BigIntFMMSizeInU32(modulusSizeInBits); 861 } 862 863 size_t TEE_BigIntFMMContextSizeInU32(size_t modulusSizeInBits __unused) 864 { 865 /* Return something larger than 0 to keep malloc() and friends happy */ 866 return 1; 867 } 868 869 uint32_t __GP11_TEE_BigIntFMMContextSizeInU32(uint32_t modulusSizeInBits) 870 { 871 return TEE_BigIntFMMContextSizeInU32(modulusSizeInBits); 872 } 873 874 void TEE_BigIntConvertToFMM(TEE_BigIntFMM *dest, const TEE_BigInt *src, 875 const TEE_BigInt *n, 876 const TEE_BigIntFMMContext *context __unused) 877 { 878 TEE_BigIntMod(dest, src, n); 879 } 880 881 void TEE_BigIntConvertFromFMM(TEE_BigInt *dest, const TEE_BigIntFMM *src, 882 const TEE_BigInt *n __unused, 883 const TEE_BigIntFMMContext *context __unused) 884 { 885 mbedtls_mpi mpi_dst; 886 mbedtls_mpi mpi_src; 887 888 get_mpi(&mpi_dst, dest); 889 get_mpi(&mpi_src, src); 890 891 MPI_CHECK(mbedtls_mpi_copy(&mpi_dst, &mpi_src)); 892 893 MPI_CHECK(copy_mpi_to_bigint(&mpi_dst, dest)); 894 mbedtls_mpi_free(&mpi_dst); 895 mbedtls_mpi_free(&mpi_src); 896 } 897 898 void TEE_BigIntComputeFMM(TEE_BigIntFMM *dest, const TEE_BigIntFMM *op1, 899 const TEE_BigIntFMM *op2, const TEE_BigInt *n, 900 const TEE_BigIntFMMContext *context __unused) 901 { 902 mbedtls_mpi mpi_dst; 903 mbedtls_mpi mpi_op1; 904 mbedtls_mpi mpi_op2; 905 mbedtls_mpi mpi_n; 906 mbedtls_mpi mpi_t; 907 908 get_mpi(&mpi_dst, dest); 909 get_mpi(&mpi_op1, op1); 910 get_mpi(&mpi_op2, op2); 911 get_mpi(&mpi_n, n); 912 get_mpi(&mpi_t, NULL); 913 914 MPI_CHECK(mbedtls_mpi_mul_mpi(&mpi_t, &mpi_op1, &mpi_op2)); 915 MPI_CHECK(mbedtls_mpi_mod_mpi(&mpi_dst, &mpi_t, &mpi_n)); 916 917 mbedtls_mpi_free(&mpi_t); 918 mbedtls_mpi_free(&mpi_n); 919 mbedtls_mpi_free(&mpi_op2); 920 mbedtls_mpi_free(&mpi_op1); 921 MPI_CHECK(copy_mpi_to_bigint(&mpi_dst, dest)); 922 mbedtls_mpi_free(&mpi_dst); 923 } 924