sp_int.h 40 KB

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