sp_int.h (44021B)
1 /* sp_int.h 2 * 3 * Copyright (C) 2006-2025 wolfSSL Inc. 4 * 5 * This file is part of wolfSSL. 6 * 7 * wolfSSL is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * wolfSSL is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 */ 21 22 /* 23 DESCRIPTION 24 This library provides single precision (SP) integer math functions. 25 26 */ 27 #ifndef WOLF_CRYPT_SP_INT_H 28 #define WOLF_CRYPT_SP_INT_H 29 30 #ifndef WOLFSSL_LINUXKM 31 #include <limits.h> 32 #endif 33 #include <wolfssl/wolfcrypt/settings.h> 34 #include <wolfssl/wolfcrypt/types.h> 35 #include <wolfssl/wolfcrypt/hash.h> 36 37 #ifdef __cplusplus 38 extern "C" { 39 #endif 40 41 #if defined(WOLFSSL_SP_ARM_ARCH) && !defined(WOLFSSL_ARM_ARCH) 42 #define WOLFSSL_ARM_ARCH WOLFSSL_SP_ARM_ARCH 43 #endif 44 45 #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \ 46 !defined(WOLFSSL_SP_INT_NEGATIVE) 47 #define WOLFSSL_SP_INT_NEGATIVE 48 #endif 49 50 /* Find smallest type for smallest bits. */ 51 #if UCHAR_MAX == 255 52 #define SP_UCHAR_BITS 8 53 54 typedef unsigned char sp_uint8; 55 typedef char sp_int8; 56 #elif UCHAR_MAX == 127 57 #define SP_UCHAR_BITS 7 58 59 typedef unsigned char sp_uint7; 60 typedef char sp_int7; 61 #else 62 #error "Size of unsigned short not detected" 63 #endif 64 65 #if USHRT_MAX == 65535 66 #define SP_USHORT_BITS 16 67 68 typedef unsigned short sp_uint16; 69 typedef short sp_int16; 70 #elif USHRT_MAX == 255 71 #define SP_USHORT_BITS 8 72 73 #if USHRT_MAX > UCHAR_MAX 74 typedef unsigned short sp_uint8; 75 typedef short sp_int8; 76 #endif 77 #else 78 #error "Size of unsigned short not detected" 79 #endif 80 81 #if UINT_MAX == 4294967295UL 82 #define SP_UINT_BITS 32 83 84 typedef unsigned int sp_uint32; 85 typedef int sp_int32; 86 #elif UINT_MAX == 65535 87 #define SP_UINT_BITS 16 88 89 #if UINT_MAX > USHRT_MAX 90 typedef unsigned int sp_uint16; 91 typedef int sp_int16; 92 #endif 93 #elif UINT_MAX == 255 94 #define SP_UINT_BITS 8 95 96 #if UINT_MAX > USHRT_MAX 97 typedef unsigned int sp_uint8; 98 typedef int sp_int8; 99 #endif 100 #else 101 #error "Size of unsigned int not detected" 102 #endif 103 104 #if defined(__WATCOMC__) && defined(__WATCOM_INT64__) 105 /* For older Watcom C compiler force types */ 106 #define SP_ULLONG_BITS 64 107 typedef unsigned __int64 sp_uint64; 108 typedef __int64 sp_int64; 109 110 #else 111 112 /* 32-bit type */ 113 #if defined(WOLF_C89) && !defined(NO_64BIT) && \ 114 ULONG_MAX == 18446744073709551615UL 115 #define SP_ULONG_BITS 64 116 117 typedef unsigned long sp_uint64; 118 typedef long sp_int64; 119 #elif !defined(WOLF_C89) && !defined(NO_64BIT) && \ 120 ULONG_MAX == 18446744073709551615ULL && \ 121 /* sanity check pre-processor supports 64-bit ULL types */ \ 122 4294967295UL != 18446744073709551615ULL 123 #define SP_ULONG_BITS 64 124 125 typedef unsigned long sp_uint64; 126 typedef long sp_int64; 127 #elif ULONG_MAX == 4294967295UL 128 #define SP_ULONG_BITS 32 129 130 #if ULONG_MAX > UINT_MAX 131 typedef unsigned long sp_uint32; 132 typedef long sp_int32; 133 #endif 134 #elif ULONG_MAX == 65535 135 #define SP_ULONG_BITS 16 136 137 #if ULONG_MAX > UINT_MAX 138 typedef unsigned long sp_uint16; 139 typedef long sp_int16; 140 #endif 141 #else 142 #error "Size of unsigned long not detected" 143 #endif 144 145 /* 64-bit type */ 146 #ifdef ULLONG_MAX 147 #if defined(WOLF_C89) && ULLONG_MAX == 18446744073709551615UL 148 #define SP_ULLONG_BITS 64 149 150 #if SP_ULLONG_BITS > SP_ULONG_BITS 151 typedef unsigned long long sp_uint64; 152 typedef long long sp_int64; 153 #endif 154 #elif !defined(WOLF_C89) && ULLONG_MAX == 18446744073709551615ULL 155 #define SP_ULLONG_BITS 64 156 157 #if SP_ULLONG_BITS > SP_ULONG_BITS 158 typedef unsigned long long sp_uint64; 159 typedef long long sp_int64; 160 #endif 161 #elif ULLONG_MAX == 4294967295UL 162 #define SP_ULLONG_BITS 32 163 164 #if SP_ULLONG_BITS > SP_ULONG_BITS 165 typedef unsigned long long sp_uint32; 166 typedef long long sp_int32; 167 #endif 168 #elif ULLONG_MAX == 65535 169 #define SP_ULLONG_BITS 16 170 171 #if SP_ULLONG_BITS > SP_ULONG_BITS 172 typedef unsigned long long sp_uint16; 173 typedef long long sp_int16; 174 #endif 175 #else 176 #error "Size of unsigned long long not detected" 177 #endif 178 #elif (SP_ULONG_BITS == 32) && !defined(NO_64BIT) 179 #define SP_ULLONG_BITS 64 180 /* Speculatively use long long as the 64-bit type as we don't have one 181 * otherwise. */ 182 typedef unsigned long long sp_uint64; 183 typedef long long sp_int64; 184 #else 185 #define SP_ULLONG_BITS 0 186 #endif 187 188 #endif /* __WATCOMC__ */ 189 190 #ifdef WOLFSSL_SP_DIV_32 191 #define WOLFSSL_SP_DIV_WORD_HALF 192 #endif 193 194 /* Make sure WOLFSSL_SP_ASM build option defined when requested */ 195 #if !defined(WOLFSSL_SP_ASM) && ( \ 196 defined(WOLFSSL_SP_X86_64_ASM) || defined(WOLFSSL_SP_ARM32_ASM) || \ 197 defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM_THUMB_ASM) || \ 198 defined(WOLFSSL_SP_ARM_CORTEX_M_ASM)) 199 #define WOLFSSL_SP_ASM 200 #endif 201 202 203 /* Determine the number of bits to use in each word. */ 204 #ifdef SP_WORD_SIZE 205 #elif defined(WOLFSSL_DSP_BUILD) 206 #define SP_WORD_SIZE 32 207 #elif defined(WOLFSSL_SP_X86_64) && !defined(WOLFSSL_SP_X86_64_ASM) && \ 208 !defined(HAVE___UINT128_T) 209 #define SP_WORD_SIZE 32 210 #elif defined(WOLFSSL_SP_X86_64_ASM) || defined(WOLFSSL_SP_X86_64) 211 #if SP_ULONG_BITS == 64 || SP_ULLONG_BITS == 64 212 #define SP_WORD_SIZE 64 213 #ifndef HAVE_INTEL_AVX1 214 #define HAVE_INTEL_AVX1 215 #endif 216 #if !defined(NO_AVX2_SUPPORT) && !defined(HAVE_INTEL_AVX2) 217 #define HAVE_INTEL_AVX2 218 #endif 219 #elif SP_ULONG_BITS == 32 220 #define SP_WORD_SIZE 32 221 #undef WOLFSSL_SP_ASM 222 #elif SP_ULONG_BITS == 16 223 #define SP_WORD_SIZE 16 224 #undef WOLFSSL_SP_ASM 225 #endif 226 #elif defined(WOLFSSL_SP_X86) 227 #define SP_WORD_SIZE 32 228 #elif defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM64) 229 #define SP_WORD_SIZE 64 230 #elif defined(WOLFSSL_SP_ARM32_ASM) || defined(WOLFSSL_SP_ARM32) 231 #define SP_WORD_SIZE 32 232 #elif defined(WOLFSSL_SP_ARM_THUMB_ASM) || defined(WOLFSSL_SP_ARM_THUMB) 233 #define SP_WORD_SIZE 32 234 #elif defined(WOLFSSL_SP_PPC) 235 #define SP_WORD_SIZE 32 236 #elif defined(WOLFSSL_SP_PPC64) 237 #define SP_WORD_SIZE 64 238 #elif defined(WOLFSSL_SP_MIPS) 239 #define SP_WORD_SIZE 32 240 #elif defined(WOLFSSL_SP_MIPS64) 241 #define SP_WORD_SIZE 64 242 #elif defined(WOLFSSL_SP_RISCV32) 243 #define SP_WORD_SIZE 32 244 #elif defined(WOLFSSL_SP_RISCV64) 245 #define SP_WORD_SIZE 64 246 #elif defined(WOLFSSL_SP_S390X) 247 #define SP_WORD_SIZE 64 248 #endif 249 250 /* If no predefined or assembly required size then use maximum available 251 * with compiler. 252 */ 253 #ifndef SP_WORD_SIZE 254 #ifdef NO_64BIT 255 #define SP_WORD_SIZE 16 256 #elif !defined(HAVE___UINT128_T) || defined(_WIN32) 257 #define SP_WORD_SIZE 32 258 #else 259 #define SP_WORD_SIZE 64 260 #endif 261 #endif 262 263 /* Define the types used. */ 264 #if defined(HAVE___UINT128_T) && !defined(NO_INT128) 265 #ifdef __SIZEOF_INT128__ 266 typedef __uint128_t sp_uint128; 267 typedef __int128_t sp_int128; 268 #else 269 typedef unsigned long sp_uint128 __attribute__ ((mode(TI))); 270 typedef long sp_int128 __attribute__ ((mode(TI))); 271 #endif 272 #ifndef WOLFSSL_UINT128_T_DEFINED 273 #ifdef __SIZEOF_INT128__ 274 typedef __uint128_t uint128_t; 275 typedef __int128_t int128_t; 276 #else 277 typedef unsigned long uint128_t __attribute__ ((mode(TI))); 278 typedef long int128_t __attribute__ ((mode(TI))); 279 #endif 280 #define WOLFSSL_UINT128_T_DEFINED 281 #endif 282 #endif 283 284 #if SP_WORD_SIZE == 8 285 #define SP_WORD_SIZEOF 1 286 287 typedef sp_uint8 sp_int_digit; 288 typedef sp_int8 sp_int_sdigit; 289 typedef sp_uint16 sp_int_word; 290 typedef sp_int16 sp_int_sword; 291 292 #define SP_MASK 0xffU 293 #elif SP_WORD_SIZE == 16 294 #define SP_WORD_SIZEOF 2 295 296 typedef sp_uint16 sp_int_digit; 297 typedef sp_int16 sp_int_sdigit; 298 typedef sp_uint32 sp_int_word; 299 typedef sp_int32 sp_int_sword; 300 301 #define SP_MASK 0xffffU 302 #elif SP_WORD_SIZE == 32 303 #define SP_WORD_SIZEOF 4 304 305 typedef sp_uint32 sp_int_digit; 306 typedef sp_int32 sp_int_sdigit; 307 typedef sp_uint64 sp_int_word; 308 typedef sp_int64 sp_int_sword; 309 310 #define SP_MASK 0xffffffffU 311 #elif SP_WORD_SIZE == 64 312 #define SP_WORD_SIZEOF 8 313 314 typedef sp_uint64 sp_int_digit; 315 typedef sp_int64 sp_int_sdigit; 316 #if (defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \ 317 !defined(_WIN64) && defined(WOLFSSL_UINT128_T_DEFINED) 318 typedef sp_uint128 sp_int_word; 319 typedef sp_int128 sp_int_sword; 320 #endif 321 322 #define SP_MASK 0xffffffffffffffffUL 323 #else 324 #error Word size not defined 325 #endif 326 327 /* Define an SP digit. */ 328 #ifndef WOLFSSL_SP_ASM 329 /* SP C code uses n/m bits and therefore needs a signed type. */ 330 #if SP_WORD_SIZE == 8 331 typedef sp_int8 sp_digit; 332 #elif SP_WORD_SIZE == 16 333 typedef sp_int16 sp_digit; 334 #elif SP_WORD_SIZE == 32 335 typedef sp_int32 sp_digit; 336 #elif SP_WORD_SIZE == 64 337 typedef sp_int64 sp_digit; 338 #endif 339 #else 340 /* SP ASM code uses full size and needs an unsigned type. */ 341 #if SP_WORD_SIZE == 8 342 typedef sp_uint8 sp_digit; 343 #elif SP_WORD_SIZE == 16 344 typedef sp_uint16 sp_digit; 345 #elif SP_WORD_SIZE == 32 346 typedef sp_uint32 sp_digit; 347 #elif SP_WORD_SIZE == 64 348 typedef sp_uint64 sp_digit; 349 #endif 350 #endif 351 352 /** Number of bits in a half a word. */ 353 #define SP_HALF_SIZE (SP_WORD_SIZE / 2) 354 /** Maximum value that can be held in a half a word. */ 355 #define SP_HALF_MAX (((sp_digit)1 << SP_HALF_SIZE) - 1) 356 /** Maximum value that can be held in a word. */ 357 #define SP_DIGIT_MAX SP_MASK 358 /* Number of bits to shift to divide by word size. */ 359 #if SP_WORD_SIZE == 8 360 #define SP_WORD_SHIFT 3 361 #elif SP_WORD_SIZE == 16 362 #define SP_WORD_SHIFT 4 363 #elif SP_WORD_SIZE == 32 364 #define SP_WORD_SHIFT 5 365 #elif SP_WORD_SIZE == 64 366 #define SP_WORD_SHIFT 6 367 #endif 368 /* Mask of word size. */ 369 #define SP_WORD_MASK (SP_WORD_SIZE - 1) 370 371 /* For debugging only - format string for different digit sizes. */ 372 #if SP_WORD_SIZE == 64 373 #if SP_ULONG_BITS == 64 374 #define SP_PRINT_FMT "%016lx" 375 #else 376 #define SP_PRINT_FMT "%016llx" 377 #endif 378 #elif SP_WORD_SIZE == 32 379 #if SP_UINT_BITS == 32 380 #define SP_PRINT_FMT "%08x" 381 #else 382 #define SP_PRINT_FMT "%08lx" 383 #endif 384 #elif SP_WORD_SIZE == 16 385 #define SP_PRINT_FMT "%04x" 386 #elif SP_WORD_SIZE == 8 387 #define SP_PRINT_FMT "%02x" 388 #endif 389 390 391 #if defined(WOLFSSL_HAVE_SP_ECC) && defined(WOLFSSL_SP_NONBLOCK) 392 /* Non-blocking ECC operation context. */ 393 typedef struct sp_ecc_ctx { 394 #ifdef WOLFSSL_SP_521 395 XALIGNED(4) byte data[66*80]; /* stack data */ 396 #elif defined(WOLFSSL_SP_384) 397 XALIGNED(4) byte data[48*80]; /* stack data */ 398 #else 399 XALIGNED(4) byte data[32*80]; /* stack data */ 400 #endif 401 } sp_ecc_ctx_t; 402 #endif 403 404 #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) 405 #include <wolfssl/wolfcrypt/random.h> 406 407 #ifndef SP_INT_BITS 408 #ifdef SP_INT_DIGITS 409 #define SP_INT_BITS (((SP_INT_DIGITS - 1) * SP_WORD_SIZE) / 2) 410 #else 411 /* Calculate number of bits to have in an sp_int based on features 412 * compiled in. 413 */ 414 #ifdef WOLFSSL_MYSQL_COMPATIBLE 415 /* MySQL wants to be able to use 8192-bit numbers. */ 416 #define SP_INT_BITS 8192 417 #elif !defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_HAVE_SP_DH) && \ 418 !defined(WOLFSSL_HAVE_SP_ECC) 419 /* Not using SP - must be SP math all. */ 420 #if !defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA) 421 /* Support max size FFHDE parameters compiled in. */ 422 #if !defined(NO_DH) && defined(HAVE_FFDHE_8192) 423 #define SP_INT_BITS 8192 424 #elif !defined(NO_DH) && defined(HAVE_FFDHE_6144) 425 #define SP_INT_BITS 6144 426 #elif !defined(NO_DH) && defined(HAVE_FFDHE_4096) 427 #define SP_INT_BITS 4096 428 #else 429 /* Default to max 3072 for general RSA and DH. */ 430 #define SP_INT_BITS 3072 431 #endif 432 #elif defined(WOLFCRYPT_HAVE_SAKKE) 433 #define SP_INT_BITS 1024 434 #elif defined(HAVE_ECC) 435 /* P521 is the largest supported ECC algorithm curve. */ 436 #define SP_INT_BITS 521 437 #elif !defined(NO_PWDBASED) && defined(HAVE_PKCS12) 438 /* wc_PKCS12_PBKDF_ex() */ 439 #define SP_INT_BITS (64 * 8) 440 #else 441 #define SP_INT_BITS 128 442 #endif 443 #elif !defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_HAVE_SP_DH) 444 /* Not use SP_RSA or DH but are using SP ECC. */ 445 #if defined(WOLFCRYPT_HAVE_SAKKE) 446 #define SP_INT_BITS 1024 447 #elif defined(WOLFSSL_SP_521) || defined(WOLFSSL_SP_MATH_ALL) 448 /* P521 is the largest supported ECC algorithm curve. */ 449 #define SP_INT_BITS 521 450 #elif defined(WOLFSSL_SP_384) 451 /* No generic support - largest curve P384. */ 452 #define SP_INT_BITS 384 453 #else 454 /* No generic support - largest curve P256. */ 455 #define SP_INT_BITS 256 456 #endif 457 /* SP RSA and DH supported so base on max size of RSA/DH in SP. */ 458 #elif defined(WOLFSSL_SP_4096) 459 #define SP_INT_BITS 4096 460 #elif !defined(WOLFSSL_SP_NO_3072) || defined(WOLFSSL_SP_MATH_ALL) 461 #define SP_INT_BITS 3072 462 #else 463 #define SP_INT_BITS 2048 464 #endif 465 #endif 466 #endif 467 468 #ifndef SP_INT_DIGITS 469 /* Calculate number of digits to have in an sp_int based on maximum size of 470 * numbers in bits that will be used. 471 * Double the size to hold multiplication result. 472 * Add one to accommodate extra digit used by sp_mul(), sp_mulmod(), 473 * sp_sqr(), sp_sqrmod() and sp_mont_red(). 474 */ 475 #define SP_INT_DIGITS \ 476 (((SP_INT_BITS + SP_WORD_SIZE - 1) / SP_WORD_SIZE) * 2 + 1) 477 #endif 478 479 #ifndef SP_INT_MAX_BITS 480 /* Convert number digits to number of bits. */ 481 #define SP_INT_MAX_BITS (SP_INT_DIGITS * SP_WORD_SIZE) 482 #endif 483 484 #if SP_WORD_SIZE < 32 485 /* Maximum number of digits in a number to mul or sqr. */ 486 #define SP_MUL_SQR_DIGITS (SP_INT_MAX_BITS / 2 / SP_WORD_SIZE) 487 /* Maximum value of partial in mul/sqr. */ 488 #define SP_MUL_SQR_MAX_PARTIAL \ 489 (SP_MUL_SQR_DIGITS * ((1 << SP_WORD_SIZE) - 1)) 490 /* Maximum value in an sp_int_word. */ 491 #define SP_INT_WORD_MAX ((1 << (SP_WORD_SIZE * 2)) - 1) 492 493 #if SP_MUL_SQR_MAX_PARTIAL > SP_INT_WORD_MAX 494 /* The sum of the partials in the multiplication/square can exceed the 495 * size of a word. This will overflow the word and loose data. 496 * Use an implementation that handles carry after every add and uses an 497 * extra temporary word for overflowing high word. 498 */ 499 #define SP_WORD_OVERFLOW 500 #endif 501 #endif 502 503 504 #ifndef NO_FILESYSTEM 505 /* Output is formatted to be used with script that checks calculations. */ 506 507 /* Print out a number in big endian. */ 508 #ifndef WOLFSSL_SP_INT_NEGATIVE 509 /* Print out a positive multi-precision number. 510 * 511 * @param [in] a SP integer to print. 512 * @param [in] s String that describes the use of the number. 513 */ 514 #define sp_print(a, s) \ 515 do { \ 516 int ii; \ 517 fprintf(stderr, "%s=0x0", s); \ 518 for (ii = (a)->used-1; ii >= 0; ii--) { \ 519 fprintf(stderr, SP_PRINT_FMT, (a)->dp[ii]); \ 520 } \ 521 fprintf(stderr, "\n"); \ 522 } \ 523 while (0) 524 #else 525 /* Print out a multi-precision number. 526 * 527 * @param [in] a SP integer to print. 528 * @param [in] s String that describes the use of the number. 529 */ 530 #define sp_print(a, s) \ 531 do { \ 532 int ii; \ 533 fprintf(stderr, "%s=0x", s); \ 534 if ((a)->sign == MP_NEG) { \ 535 fprintf(stderr, "-"); \ 536 } \ 537 fprintf(stderr, "0"); \ 538 for (ii = (a)->used-1; ii >= 0; ii--) { \ 539 fprintf(stderr, SP_PRINT_FMT, (a)->dp[ii]); \ 540 } \ 541 fprintf(stderr, "\n"); \ 542 } \ 543 while (0) 544 #endif 545 546 /* Print out a single multi-precision digit. 547 * 548 * @param [in] a SP integer digit to print. 549 * @param [in] s String that describes the use of the number. 550 */ 551 #define sp_print_digit(a, s) \ 552 do { \ 553 fprintf(stderr, "%s=0x0", s); \ 554 fprintf(stderr, SP_PRINT_FMT, a); \ 555 fprintf(stderr, "\n"); \ 556 } \ 557 while (0) 558 559 /* Print out an integer. 560 * 561 * @param [in] a Number to print. 562 * @param [in] s String that describes the use of the number. 563 */ 564 #define sp_print_int(a, s) \ 565 do { \ 566 fprintf(stderr, "%s=0x0%x\n", s, a); \ 567 } \ 568 while (0) 569 570 #else 571 572 /* No filesystem, no output 573 * TODO: Use logging API? 574 */ 575 #define sp_print(a, s) WC_DO_NOTHING 576 #define sp_print_digit(a, s) WC_DO_NOTHING 577 #define sp_print_int(a, s) WC_DO_NOTHING 578 579 #endif /* !NO_FILESYSTEM */ 580 581 /* Returns whether multi-precision number is odd 582 * 583 * Assumes a is not NULL. 584 * 585 * @param [in] a SP integer to check. 586 * @return 1 when odd. 587 * @return 0 when even. 588 */ 589 #define sp_isodd(a) (((a)->used != 0) && ((a)->dp[0] & 1)) 590 /* Returns whether multi-precision number is even 591 * 592 * Assumes a is not NULL. 593 * 594 * @param [in] a SP integer to check. 595 * @return 1 when even. 596 * @return 0 when odd. 597 */ 598 #define sp_iseven(a) (((a)->used != 0) && (((a)->dp[0] & 1) == 0)) 599 /* Returns whether multi-precision number has the value zero. 600 * 601 * Assumes a is not NULL. 602 * 603 * @param [in] a SP integer to check. 604 * @return 1 when zero. 605 * @return 0 when not zero. 606 */ 607 #define sp_iszero(a) ((a)->used == 0) 608 609 #ifndef WOLFSSL_SP_INT_NEGATIVE 610 /* Returns whether multi-precision number has the value one. 611 * 612 * Assumes a is not NULL. 613 * 614 * @param [in] a SP integer to check. 615 * @return 1 when one. 616 * @return 0 when not one. 617 */ 618 #define sp_isone(a) (((a)->used == 1) && ((a)->dp[0] == 1)) 619 #else 620 /* Returns whether multi-precision number has the value of positive one. 621 * 622 * Assumes a is not NULL. 623 * 624 * @param [in] a SP integer to check. 625 * @return 1 when one. 626 * @return 0 when not one. 627 */ 628 #define sp_isone(a) \ 629 (((a)->used == 1) && ((a)->dp[0] == 1) && ((a)->sign == MP_ZPOS)) 630 #endif 631 632 #ifndef WOLFSSL_SP_INT_NEGATIVE 633 /* Returns whether multi-precision number has the value 'd'. 634 * 635 * Assumes a is not NULL. 636 * 637 * @param [in] a SP integer to check. 638 * @param [in] d SP integer digit. 639 * @return 1 when one. 640 * @return 0 when not one. 641 */ 642 #define sp_isword(a, d) \ 643 ((((d) == 0) && sp_iszero(a)) || (((a)->used == 1) && ((a)->dp[0] == (d)))) 644 #else 645 /* Returns whether multi-precision number has the value 'd'. 646 * 647 * Assumes a is not NULL. 648 * 649 * @param [in] a SP integer to check. 650 * @param [in] d SP integer digit. 651 * @return 1 when one. 652 * @return 0 when not one. 653 */ 654 #define sp_isword(a, d) \ 655 ((((d) == 0) && sp_iszero(a)) || \ 656 (((a)->used == 1) && ((a)->dp[0] == (d)) && ((a)->sign == MP_ZPOS))) 657 #endif 658 659 #ifndef WOLFSSL_SP_INT_NEGATIVE 660 /* Calculate the absolute value of the multi-precision number. 661 * 662 * Negative support not compiled in so just copies. 663 * 664 * @param [in] a SP integer to calculate absolute value of. 665 * @param [out] r SP integer to hold result. 666 * 667 * @return MP_OKAY on success. 668 * @return MP_VAL when a or r is NULL. 669 */ 670 #define sp_abs(a, b) sp_copy(a, b) 671 /* Returns whether multi-precision number is negative. 672 * 673 * Negative support not compiled in so always returns 0 (false). 674 * 675 * @param [in] a SP integer to check. 676 * @param [in] d SP integer digit. 677 * @return 0 indicating not negative always. 678 */ 679 #define sp_isneg(a) (0) 680 /* Sets the multi-precision number negative. 681 * 682 * Negative support not compiled in, so does nothing. */ 683 #define sp_setneg(a) WC_DO_NOTHING 684 #else 685 /* Returns whether multi-precision number is negative. 686 * 687 * Assumes a is not NULL. 688 * 689 * @param [in] a SP integer to check. 690 * @param [in] d SP integer digit. 691 * @return 1 when negative. 692 * @return 0 when not negative. 693 */ 694 #define sp_isneg(a) ((a)->sign == MP_NEG) 695 /* Sets the multi-precision number negative. */ 696 #define sp_setneg(a) ((a)->sign = MP_NEG) 697 #endif 698 699 /* Number of bits used based on used field only. */ 700 #define sp_bitsused(a) ((a)->used * SP_WORD_SIZE) 701 702 /* Updates the used count to exclude leading zeros. 703 * 704 * Assumes a is not NULL. 705 * 706 * @param [in] a SP integer to update. 707 */ 708 #define sp_clamp(a) \ 709 do { \ 710 int ii; \ 711 if ((a)->used > 0) { \ 712 for (ii = (int)(a)->used - 1; ii >= 0; ii--) { \ 713 if ((a)->dp[ii] != 0) { \ 714 break; \ 715 } \ 716 } \ 717 (a)->used = (wc_mp_size_t)(ii + 1); \ 718 } \ 719 } while (0) 720 721 /* Check the compiled and linked math implementation are the same. 722 * Use the number of bits in a digit as indication of how code was compiled. 723 * 724 * @return 1 when the number of bits are the same. 725 * @return 0 when the number of bits are different. 726 */ 727 #define CheckFastMathSettings() (SP_WORD_SIZE == CheckRunTimeFastMath()) 728 729 730 /** 731 * A result of NO. 732 * e.g. Is prime? NO. 733 */ 734 #define MP_NO 0 735 /** 736 * A result of YES. 737 * e.g. Is prime? YES. 738 */ 739 #define MP_YES 1 740 741 #ifdef WOLFSSL_SP_INT_NEGATIVE 742 /** Number is 0/positive. */ 743 #define MP_ZPOS 0 744 /** Number is negative. */ 745 #define MP_NEG 1 746 #endif 747 748 /** Radix is base 10 or decimal. */ 749 #define MP_RADIX_DEC 10 750 /** Radix is base 16 or hexadecimal. */ 751 #define MP_RADIX_HEX 16 752 753 /** Result of comparison is that the first number is greater than second. */ 754 #define MP_GT 1 755 /** Result of comparison is they are equal. */ 756 #define MP_EQ 0 757 /** Result of comparison is that the first number is less than second. */ 758 #define MP_LT (-1) 759 760 /* ERROR VALUES */ 761 762 /* MP_MEM, MP_VAL, MP_WOULDBLOCK, and MP_NOT_INF are defined in error-crypt.h */ 763 764 /** Error value on success. */ 765 #define MP_OKAY 0 766 767 #define FP_WOULDBLOCK MP_WOULDBLOCK 768 /* Unused error. Defined for backward compatibility. */ 769 #define MP_RANGE MP_NOT_INF 770 #ifdef USE_FAST_MATH 771 /* For old FIPS, need FP_MEM defined for old implementation. */ 772 #define FP_MEM MP_MEM 773 #endif 774 775 /* Number of bits in each word/digit. */ 776 #define DIGIT_BIT SP_WORD_SIZE 777 /* Mask of all used bits in word/digit. */ 778 #define MP_MASK SP_MASK 779 780 #ifdef MP_LOW_MEM 781 /* Use algorithms that use less memory. */ 782 #define WOLFSSL_SP_LOW_MEM 783 #endif 784 785 786 /* The number of bytes to a sp_int with 'cnt' digits. 787 * Must have at least one digit. 788 */ 789 #define MP_INT_SIZEOF(cnt) \ 790 (sizeof(sp_int_minimal) + (((cnt) <= 1) ? 0 : ((size_t)((cnt) - 1))) * \ 791 sizeof(sp_int_digit)) 792 /* The address of the next sp_int after one with 'cnt' digits. */ 793 #define MP_INT_NEXT(t, cnt) \ 794 (sp_int*)(((byte*)(t)) + MP_INT_SIZEOF(cnt)) 795 796 #define MP_INT_SIZEOF_DIGITS(cnt) (MP_INT_SIZEOF(cnt) / sizeof(sp_int_digit)) 797 798 /* Calculate the number of words required to support a number of bits. */ 799 #define MP_BITS_CNT(bits) \ 800 ((unsigned int)(((((bits) + SP_WORD_SIZE - 1) / SP_WORD_SIZE) * 2 + 1))) 801 802 #if !defined(WOLFSSL_SP_NO_DYN_STACK) && defined(__STDC_VERSION__) && \ 803 (__STDC_VERSION__ >= 199901L) && \ 804 (defined(WOLFSSL_SP_NO_MALLOC) || \ 805 !(defined(WOLFSSL_SMALL_STACK) || defined(SP_ALLOC))) 806 #define WOLFSSL_SP_DYN_STACK 807 #endif 808 809 #ifdef WOLFSSL_SMALL_STACK 810 /* 811 * Dynamic memory allocation of mp_int. 812 */ 813 /* Declare a dynamically allocated mp_int. */ 814 #define DECL_MP_INT_SIZE_DYN(name, bits, max) \ 815 sp_int* name = NULL 816 /* Declare a dynamically allocated mp_int. */ 817 #define DECL_MP_INT_SIZE(name, bits) \ 818 sp_int* name = NULL 819 /* Allocate an mp_int of minimal size and zero out. */ 820 #define NEW_MP_INT_SIZE(name, bits, heap, type) \ 821 do { \ 822 (name) = (mp_int*)XMALLOC(MP_INT_SIZEOF(MP_BITS_CNT(bits)), heap, type); \ 823 if ((name) != NULL) { \ 824 XMEMSET(name, 0, MP_INT_SIZEOF(MP_BITS_CNT(bits))); \ 825 } \ 826 } \ 827 while (0) 828 /* Dispose of dynamically allocated mp_int. */ 829 #define FREE_MP_INT_SIZE(name, heap, type) \ 830 XFREE(name, heap, type) 831 /* Type to cast to when using size marcos. */ 832 #define MP_INT_SIZE sp_int 833 /* Must check mp_int pointer for NULL. */ 834 #define MP_INT_SIZE_CHECK_NULL 835 #else 836 /* 837 * Static allocation of mp_int. 838 */ 839 #ifdef WOLFSSL_SP_DYN_STACK 840 /* Declare a dynamically allocated mp_int. */ 841 #define DECL_MP_INT_SIZE_DYN(name, bits, max) \ 842 sp_int_digit name##d[MP_INT_SIZEOF_DIGITS(MP_BITS_CNT(bits))]; \ 843 sp_int* (name) = (sp_int*)name##d 844 #elif defined(__cplusplus) 845 /* C++ doesn't tolerate parentheses around "name" (-Wparentheses) */ 846 #define DECL_MP_INT_SIZE_DYN(name, bits, max) \ 847 sp_int_digit name##d[MP_INT_SIZEOF_DIGITS(MP_BITS_CNT(max))]; \ 848 sp_int* name = (sp_int*)name##d 849 #else 850 /* Declare a dynamically allocated mp_int. */ 851 #define DECL_MP_INT_SIZE_DYN(name, bits, max) \ 852 sp_int_digit name##d[MP_INT_SIZEOF_DIGITS(MP_BITS_CNT(max))]; \ 853 sp_int* (name) = (sp_int*)name##d 854 #endif 855 /* Declare a statically allocated mp_int. */ 856 #define DECL_MP_INT_SIZE(name, bits) \ 857 sp_int_digit name##d[MP_INT_SIZEOF_DIGITS(MP_BITS_CNT(bits))]; \ 858 sp_int* (name) = (sp_int*)name##d 859 /* Zero out mp_int of minimal size. */ 860 #define NEW_MP_INT_SIZE(name, bits, heap, type) \ 861 XMEMSET(name, 0, MP_INT_SIZEOF(MP_BITS_CNT(bits))) 862 /* Dispose of static mp_int. */ 863 #define FREE_MP_INT_SIZE(name, heap, type) WC_DO_NOTHING 864 /* Type to force compiler to not complain about size. */ 865 #define MP_INT_SIZE sp_int_minimal 866 #endif 867 868 /* Initialize an mp_int to a specific size. */ 869 #define INIT_MP_INT_SIZE(name, bits) \ 870 mp_init_size(name, MP_BITS_CNT(bits)) 871 872 873 #ifdef HAVE_WOLF_BIGINT 874 /* Raw big integer as a big-endian byte array. 875 * 876 * Useful for when using hardware - canonical format. 877 */ 878 typedef struct WC_BIGINT { 879 /* Dynamically allocated buffer that is big-endian byte array. */ 880 byte* buf; 881 /* Length of buffer in bytes. */ 882 word32 len; 883 /* Hint for heap used to allocate buffer. */ 884 void* heap; 885 } WC_BIGINT; 886 887 /* Ensure WC_BIGINT defined once. */ 888 #define WOLF_BIGINT_DEFINED 889 #endif 890 891 #if SP_INT_DIGITS < (65536 / SP_WORD_SIZEOF) 892 /* Type for number of digits. */ 893 typedef word16 sp_size_t; 894 #else 895 /* Type for number of digits. */ 896 typedef unsigned int sp_size_t; 897 #endif 898 899 /* Type for number of digits. */ 900 #define wc_mp_size_t sp_size_t 901 #ifdef WOLFSSL_SP_INT_NEGATIVE 902 typedef sp_uint8 sp_sign_t; 903 #define wc_mp_sign_t sp_sign_t 904 #endif 905 906 /** 907 * SP integer. 908 * 909 * dp at end so user can allocate a smaller amount and set size. 910 */ 911 typedef struct sp_int { 912 /** Number of words that contain data. */ 913 sp_size_t used; 914 /** Maximum number of words in data. */ 915 sp_size_t size; 916 #ifdef WOLFSSL_SP_INT_NEGATIVE 917 /** Indicates whether number is 0/positive or negative. */ 918 sp_sign_t sign; 919 #endif 920 #ifdef HAVE_WOLF_BIGINT 921 /** Unsigned binary (big endian) representation of number. */ 922 struct WC_BIGINT raw; 923 #endif 924 /** Data of number. */ 925 XALIGNED(SP_WORD_SIZEOF) sp_int_digit dp[SP_INT_DIGITS]; 926 } sp_int; 927 928 typedef struct sp_int_minimal { 929 /** Number of words that contain data. */ 930 sp_size_t used; 931 /** Maximum number of words in data. */ 932 sp_size_t size; 933 #ifdef WOLFSSL_SP_INT_NEGATIVE 934 /** Indicates whether number is 0/positive or negative. */ 935 sp_sign_t sign; 936 #endif 937 #ifdef HAVE_WOLF_BIGINT 938 /** Unsigned binary (big endian) representation of number. */ 939 struct WC_BIGINT raw; 940 #endif 941 /** First digit of number. */ 942 XALIGNED(SP_WORD_SIZEOF) sp_int_digit dp[1]; 943 } sp_int_minimal; 944 945 /* MP_INT_SIZEOF_DIGITS() requires that sizeof(sp_int) is a multiple of 946 * sizeof(sp_int_digit). 947 */ 948 wc_static_assert(sizeof(struct sp_int) % sizeof(sp_int_digit) == 0); 949 wc_static_assert(sizeof(struct sp_int_minimal) % sizeof(sp_int_digit) == 0); 950 951 /* Multi-precision integer type is SP integer type. */ 952 typedef sp_int mp_int; 953 /* Multi-precision integer digit type is SP integer digit type. 954 * Type is unsigned. 955 */ 956 typedef sp_int_digit mp_digit; 957 958 959 /* Include the maths operations that are not implementation specific. */ 960 #include <wolfssl/wolfcrypt/wolfmath.h> 961 962 /* 963 * Function prototypes. 964 */ 965 966 MP_API int sp_init(sp_int* a); 967 MP_API int sp_init_size(sp_int* a, unsigned int size); 968 MP_API int sp_init_multi(sp_int* n1, sp_int* n2, sp_int* n3, sp_int* n4, 969 sp_int* n5, sp_int* n6); 970 MP_API void sp_free(sp_int* a); 971 MP_API int sp_grow(sp_int* a, int l); 972 973 MP_API void sp_zero(sp_int* a); 974 MP_API void sp_clear(sp_int* a); 975 MP_API void sp_forcezero(sp_int* a); 976 MP_API int sp_init_copy (sp_int* r, const sp_int* a); 977 978 MP_API int sp_copy(const sp_int* a, sp_int* r); 979 MP_API int sp_exch(sp_int* a, sp_int* b); 980 MP_API int sp_cond_swap_ct(sp_int* a, sp_int* b, int cnt, int swap); 981 MP_API int sp_cond_swap_ct_ex(sp_int* a, sp_int* b, int cnt, int swap, 982 sp_int* t); 983 984 #ifdef WOLFSSL_SP_INT_NEGATIVE 985 MP_API int sp_abs(const sp_int* a, sp_int* r); 986 #endif 987 #ifdef WOLFSSL_SP_MATH_ALL 988 MP_API int sp_cmp_mag(const sp_int* a, const sp_int* b); 989 #endif 990 MP_API int sp_cmp(const sp_int* a, const sp_int* b); 991 MP_API int sp_cmp_ct(const sp_int* a, const sp_int* b, unsigned int n); 992 993 MP_API int sp_is_bit_set(const sp_int* a, unsigned int b); 994 MP_API int sp_count_bits(const sp_int* a); 995 #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY) 996 MP_API int sp_cnt_lsb(const sp_int* a); 997 #endif 998 MP_API int sp_leading_bit(const sp_int* a); 999 MP_API int sp_set_bit(sp_int* a, int i); 1000 MP_API int sp_2expt(sp_int* a, int e); 1001 1002 MP_API int sp_set(sp_int* a, sp_int_digit d); 1003 MP_API int sp_set_int(sp_int* a, unsigned long n); 1004 MP_API int sp_cmp_d(const sp_int* a, sp_int_digit d); 1005 MP_API int sp_add_d(const sp_int* a, sp_int_digit d, sp_int* r); 1006 MP_API int sp_sub_d(const sp_int* a, sp_int_digit d, sp_int* r); 1007 MP_API int sp_mul_d(const sp_int* a, sp_int_digit d, sp_int* r); 1008 #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \ 1009 defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || \ 1010 defined(WC_MP_TO_RADIX) 1011 MP_API int sp_div_d(const sp_int* a, sp_int_digit d, sp_int* r, 1012 sp_int_digit* rem); 1013 #endif 1014 #if defined(WOLFSSL_SP_MATH_ALL) || (defined(HAVE_ECC) && \ 1015 defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA) 1016 MP_API int sp_mod_d(const sp_int* a, sp_int_digit d, sp_int_digit* r); 1017 #endif 1018 #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC) 1019 MP_API int sp_div_2_mod_ct(const sp_int* a, const sp_int* m, sp_int* r); 1020 MP_API int sp_div_2(const sp_int* a, sp_int* r); 1021 #endif 1022 1023 MP_API int sp_add(const sp_int* a, const sp_int* b, sp_int* r); 1024 MP_API int sp_sub(const sp_int* a, const sp_int* b, sp_int* r); 1025 #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \ 1026 (!defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_CUSTOM_CURVES)) || \ 1027 defined(WOLFCRYPT_HAVE_ECCSI) || defined(WOLFCRYPT_HAVE_SAKKE) 1028 MP_API int sp_addmod(const sp_int* a, const sp_int* b, const sp_int* m, 1029 sp_int* r); 1030 #endif 1031 #if defined(WOLFSSL_SP_MATH_ALL) && (!defined(WOLFSSL_RSA_VERIFY_ONLY) || \ 1032 defined(HAVE_ECC)) 1033 MP_API int sp_submod(const sp_int* a, const sp_int* b, const sp_int* m, 1034 sp_int* r); 1035 #endif 1036 #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC) 1037 MP_API int sp_submod_ct(const sp_int* a, const sp_int* b, const sp_int* m, 1038 sp_int* r); 1039 MP_API int sp_addmod_ct(const sp_int* a, const sp_int* b, const sp_int* m, 1040 sp_int* r); 1041 #endif 1042 #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC) 1043 MP_API void sp_xor_ct(const sp_int* a, const sp_int* b, int len, sp_int* r); 1044 #endif 1045 1046 MP_API int sp_lshd(sp_int* a, int s); 1047 #ifdef WOLFSSL_SP_MATH_ALL 1048 MP_API void sp_rshd(sp_int* a, int c); 1049 #endif 1050 MP_API int sp_rshb(const sp_int* a, int n, sp_int* r); 1051 1052 #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \ 1053 (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \ 1054 !defined(WOLFSSL_RSA_PUBLIC_ONLY)) 1055 MP_API int sp_div(const sp_int* a, const sp_int* d, sp_int* r, sp_int* rem); 1056 #endif 1057 MP_API int sp_mod(const sp_int* a, const sp_int* m, sp_int* r); 1058 1059 MP_API int sp_mul(const sp_int* a, const sp_int* b, sp_int* r); 1060 MP_API int sp_mulmod(const sp_int* a, const sp_int* b, const sp_int* m, 1061 sp_int* r); 1062 1063 MP_API int sp_invmod(const sp_int* a, const sp_int* m, sp_int* r); 1064 #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC) 1065 MP_API int sp_invmod_mont_ct(const sp_int* a, const sp_int* m, sp_int* r, 1066 sp_int_digit mp); 1067 #endif 1068 1069 MP_API int sp_exptmod_ex(const sp_int* b, const sp_int* e, int digits, 1070 const sp_int* m, sp_int* r); 1071 MP_API int sp_exptmod(const sp_int* b, const sp_int* e, const sp_int* m, 1072 sp_int* r); 1073 #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) 1074 MP_API int sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m, 1075 sp_int* r); 1076 #endif 1077 1078 #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_ALL) 1079 MP_API int sp_div_2d(const sp_int* a, int e, sp_int* r, sp_int* rem); 1080 MP_API int sp_mul_2d(const sp_int* a, int e, sp_int* r); 1081 #endif 1082 #if defined(WOLFSSL_SP_MATH_ALL) || defined(HAVE_ECC) || defined(OPENSSL_ALL) 1083 MP_API int sp_mod_2d(const sp_int* a, int e, sp_int* r); 1084 #endif 1085 1086 MP_API int sp_sqr(const sp_int* a, sp_int* r); 1087 MP_API int sp_sqrmod(const sp_int* a, const sp_int* m, sp_int* r); 1088 1089 MP_API int sp_mont_red_ex(sp_int* a, const sp_int* m, sp_int_digit mp, int ct); 1090 #define sp_mont_red(a, m, mp) sp_mont_red_ex(a, m, mp, 0) 1091 MP_API int sp_mont_setup(const sp_int* m, sp_int_digit* rho); 1092 MP_API int sp_mont_norm(sp_int* norm, const sp_int* m); 1093 1094 MP_API int sp_unsigned_bin_size(const sp_int* a); 1095 MP_API int sp_read_unsigned_bin(sp_int* a, const byte* in, word32 inSz); 1096 MP_API int sp_to_unsigned_bin(const sp_int* a, byte* out); 1097 MP_API int sp_to_unsigned_bin_len(const sp_int* a, byte* out, int outSz); 1098 MP_API int sp_to_unsigned_bin_len_ct(const sp_int* a, byte* out, int outSz); 1099 #ifdef WOLFSSL_SP_MATH_ALL 1100 MP_API int sp_to_unsigned_bin_at_pos(int o, const sp_int* a, 1101 unsigned char* out); 1102 #endif 1103 1104 MP_API int sp_read_radix(sp_int* a, const char* in, int radix); 1105 MP_API int sp_tohex(const sp_int* a, char* str); 1106 MP_API int sp_todecimal(const sp_int* a, char* str); 1107 #if defined(WOLFSSL_SP_MATH_ALL) || defined(WC_MP_TO_RADIX) 1108 MP_API int sp_toradix(const sp_int* a, char* str, int radix); 1109 MP_API int sp_radix_size(const sp_int* a, int radix, int* size); 1110 #endif 1111 1112 MP_API int sp_rand_prime(sp_int* r, int len, WC_RNG* rng, void* heap); 1113 MP_API int sp_prime_is_prime(const sp_int* a, int t, int* result); 1114 MP_API int sp_prime_is_prime_ex(const sp_int* a, int t, int* result, 1115 WC_RNG* rng); 1116 #if !defined(NO_RSA) || defined(WOLFSSL_KEY_GEN) 1117 MP_API int sp_gcd(const sp_int* a, const sp_int* b, sp_int* r); 1118 #endif 1119 #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \ 1120 (!defined(WC_RSA_BLINDING) || defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) 1121 MP_API int sp_lcm(const sp_int* a, const sp_int* b, sp_int* r); 1122 #endif 1123 1124 WOLFSSL_API word32 CheckRunTimeFastMath(void); 1125 1126 #ifdef WOLFSSL_CHECK_MEM_ZERO 1127 WOLFSSL_LOCAL void sp_memzero_add(const char* name, sp_int* sp); 1128 WOLFSSL_LOCAL void sp_memzero_check(sp_int* sp); 1129 #endif 1130 1131 1132 /* Map mp functions to SP math versions. */ 1133 /* Different name or signature. */ 1134 #define mp_mul_2(a, r) sp_mul_2d(a, 1, r) 1135 #define mp_div_3(a, r, rem) sp_div_d(a, 3, r, rem) 1136 #define mp_rshb(A,x) sp_rshb(A,x,A) 1137 #define mp_is_bit_set(a,b) sp_is_bit_set(a,(unsigned int)(b)) 1138 #define mp_montgomery_reduce(a, m, mp) sp_mont_red_ex(a, m, mp, 0) 1139 #define mp_montgomery_reduce_ct(a, m, mp) sp_mont_red_ex(a, m, mp, 1) 1140 #define mp_montgomery_setup sp_mont_setup 1141 #define mp_montgomery_calc_normalization sp_mont_norm 1142 1143 /* Macros mappings. */ 1144 #define mp_isodd sp_isodd 1145 #define mp_iseven sp_iseven 1146 #define mp_iszero sp_iszero 1147 #define mp_isone sp_isone 1148 #define mp_isword sp_isword 1149 #define mp_abs sp_abs 1150 #define mp_isneg sp_isneg 1151 #define mp_setneg sp_setneg 1152 #define mp_bitsused sp_bitsused 1153 #define mp_clamp sp_clamp 1154 1155 /* One to one mappings. */ 1156 #define mp_init sp_init 1157 #define mp_init_size sp_init_size 1158 #define mp_init_multi sp_init_multi 1159 #define mp_free sp_free 1160 #define mp_grow sp_grow 1161 #define mp_zero sp_zero 1162 #define mp_clear sp_clear 1163 #define mp_forcezero sp_forcezero 1164 #define mp_copy sp_copy 1165 #define mp_init_copy sp_init_copy 1166 #define mp_exch sp_exch 1167 #define mp_cond_swap_ct sp_cond_swap_ct 1168 #define mp_cond_swap_ct_ex sp_cond_swap_ct_ex 1169 #define mp_cmp_mag sp_cmp_mag 1170 #define mp_cmp sp_cmp 1171 #define mp_cmp_ct sp_cmp_ct 1172 #define mp_count_bits sp_count_bits 1173 #define mp_cnt_lsb sp_cnt_lsb 1174 #define mp_leading_bit sp_leading_bit 1175 #define mp_set_bit sp_set_bit 1176 #define mp_2expt sp_2expt 1177 #define mp_set sp_set 1178 #define mp_set_int sp_set_int 1179 #define mp_cmp_d sp_cmp_d 1180 #define mp_add_d sp_add_d 1181 #define mp_sub_d sp_sub_d 1182 #define mp_mul_d sp_mul_d 1183 #define mp_div_d sp_div_d 1184 #define mp_mod_d sp_mod_d 1185 #define mp_div_2_mod_ct sp_div_2_mod_ct 1186 #define mp_div_2 sp_div_2 1187 #define mp_add sp_add 1188 #define mp_sub sp_sub 1189 1190 #define mp_addmod_ct sp_addmod_ct 1191 #define mp_submod_ct sp_submod_ct 1192 #define mp_xor_ct sp_xor_ct 1193 #define mp_lshd sp_lshd 1194 #define mp_rshd sp_rshd 1195 #define mp_div sp_div 1196 #define mp_mul sp_mul 1197 #define mp_invmod sp_invmod 1198 #define mp_invmod_mont_ct sp_invmod_mont_ct 1199 #define mp_exptmod_ex sp_exptmod_ex 1200 #define mp_exptmod_nct sp_exptmod_nct 1201 #define mp_div_2d sp_div_2d 1202 #define mp_mod_2d sp_mod_2d 1203 #define mp_mul_2d sp_mul_2d 1204 #define mp_sqr sp_sqr 1205 1206 #define mp_unsigned_bin_size sp_unsigned_bin_size 1207 #define mp_read_unsigned_bin sp_read_unsigned_bin 1208 #define mp_to_unsigned_bin sp_to_unsigned_bin 1209 #define mp_to_unsigned_bin_len sp_to_unsigned_bin_len 1210 #define mp_to_unsigned_bin_len_ct sp_to_unsigned_bin_len_ct 1211 #define mp_to_unsigned_bin_at_pos sp_to_unsigned_bin_at_pos 1212 #define mp_read_radix sp_read_radix 1213 #define mp_tohex sp_tohex 1214 #define mp_todecimal sp_todecimal 1215 #define mp_toradix sp_toradix 1216 #define mp_radix_size sp_radix_size 1217 1218 #define mp_rand_prime sp_rand_prime 1219 #define mp_prime_is_prime sp_prime_is_prime 1220 #define mp_prime_is_prime_ex sp_prime_is_prime_ex 1221 #define mp_gcd sp_gcd 1222 #define mp_lcm sp_lcm 1223 1224 #define mp_memzero_add sp_memzero_add 1225 #define mp_memzero_check sp_memzero_check 1226 1227 /* Allow for Hardware Based Mod Math */ 1228 /* Avoid redeclaration warnings */ 1229 #ifndef WOLFSSL_USE_HW_MP 1230 #define mp_mod sp_mod 1231 #define mp_addmod sp_addmod 1232 #define mp_submod sp_submod 1233 #define mp_mulmod sp_mulmod 1234 #define mp_exptmod sp_exptmod 1235 #define mp_sqrmod sp_sqrmod 1236 #endif 1237 1238 #ifdef WOLFSSL_DEBUG_MATH 1239 #define mp_dump(d, a, v) sp_print(a, d) 1240 #endif 1241 1242 #endif /* WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */ 1243 1244 #ifdef __cplusplus 1245 } /* extern "C" */ 1246 #endif 1247 1248 #endif /* WOLF_CRYPT_SP_H */