sp_int.h 41 KB

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