bignum.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /**
  2. * \file bignum.h
  3. *
  4. * \brief Multi-precision integer library
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: Apache-2.0
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10. * not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #ifndef MBEDTLS_BIGNUM_H
  24. #define MBEDTLS_BIGNUM_H
  25. #if !defined(MBEDTLS_CONFIG_FILE)
  26. #include "config.h"
  27. #else
  28. #include MBEDTLS_CONFIG_FILE
  29. #endif
  30. #include <stddef.h>
  31. #include <stdint.h>
  32. #if defined(MBEDTLS_FS_IO)
  33. #include <stdio.h>
  34. #endif
  35. #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
  36. #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
  37. #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
  38. #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
  39. #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
  40. #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
  41. #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
  42. #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */
  43. #define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
  44. /*
  45. * Maximum size MPIs are allowed to grow to in number of limbs.
  46. */
  47. #define MBEDTLS_MPI_MAX_LIMBS 10000
  48. #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
  49. /*
  50. * Maximum window size used for modular exponentiation. Default: 6
  51. * Minimum value: 1. Maximum value: 6.
  52. *
  53. * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
  54. * for the sliding window calculation. (So 64 by default)
  55. *
  56. * Reduction in size, reduces speed.
  57. */
  58. #define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
  59. #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
  60. #if !defined(MBEDTLS_MPI_MAX_SIZE)
  61. /*
  62. * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
  63. * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
  64. *
  65. * Note: Calculations can results temporarily in larger MPIs. So the number
  66. * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
  67. */
  68. #define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
  69. #endif /* !MBEDTLS_MPI_MAX_SIZE */
  70. #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
  71. /*
  72. * When reading from files with mbedtls_mpi_read_file() and writing to files with
  73. * mbedtls_mpi_write_file() the buffer should have space
  74. * for a (short) label, the MPI (in the provided radix), the newline
  75. * characters and the '\0'.
  76. *
  77. * By default we assume at least a 10 char label, a minimum radix of 10
  78. * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
  79. * Autosized at compile time for at least a 10 char label, a minimum radix
  80. * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
  81. *
  82. * This used to be statically sized to 1250 for a maximum of 4096 bit
  83. * numbers (1234 decimal chars).
  84. *
  85. * Calculate using the formula:
  86. * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
  87. * LabelSize + 6
  88. */
  89. #define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
  90. #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
  91. #define MBEDTLS_MPI_RW_BUFFER_SIZE ( ((MBEDTLS_MPI_MAX_BITS_SCALE100 + MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
  92. /*
  93. * Define the base integer type, architecture-wise.
  94. *
  95. * 32 or 64-bit integer types can be forced regardless of the underlying
  96. * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
  97. * respectively and undefining MBEDTLS_HAVE_ASM.
  98. *
  99. * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
  100. * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
  101. */
  102. #if !defined(MBEDTLS_HAVE_INT32)
  103. #if defined(_MSC_VER) && defined(_M_AMD64)
  104. /* Always choose 64-bit when using MSC */
  105. #if !defined(MBEDTLS_HAVE_INT64)
  106. #define MBEDTLS_HAVE_INT64
  107. #endif /* !MBEDTLS_HAVE_INT64 */
  108. typedef int64_t mbedtls_mpi_sint;
  109. typedef uint64_t mbedtls_mpi_uint;
  110. #elif defined(__GNUC__) && ( \
  111. defined(__amd64__) || defined(__x86_64__) || \
  112. defined(__ppc64__) || defined(__powerpc64__) || \
  113. defined(__ia64__) || defined(__alpha__) || \
  114. ( defined(__sparc__) && defined(__arch64__) ) || \
  115. defined(__s390x__) || defined(__mips64) )
  116. #if !defined(MBEDTLS_HAVE_INT64)
  117. #define MBEDTLS_HAVE_INT64
  118. #endif /* MBEDTLS_HAVE_INT64 */
  119. typedef int64_t mbedtls_mpi_sint;
  120. typedef uint64_t mbedtls_mpi_uint;
  121. #if !defined(MBEDTLS_NO_UDBL_DIVISION)
  122. /* mbedtls_t_udbl defined as 128-bit unsigned int */
  123. typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
  124. #define MBEDTLS_HAVE_UDBL
  125. #endif /* !MBEDTLS_NO_UDBL_DIVISION */
  126. #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
  127. /*
  128. * __ARMCC_VERSION is defined for both armcc and armclang and
  129. * __aarch64__ is only defined by armclang when compiling 64-bit code
  130. */
  131. #if !defined(MBEDTLS_HAVE_INT64)
  132. #define MBEDTLS_HAVE_INT64
  133. #endif /* !MBEDTLS_HAVE_INT64 */
  134. typedef int64_t mbedtls_mpi_sint;
  135. typedef uint64_t mbedtls_mpi_uint;
  136. #if !defined(MBEDTLS_NO_UDBL_DIVISION)
  137. /* mbedtls_t_udbl defined as 128-bit unsigned int */
  138. typedef __uint128_t mbedtls_t_udbl;
  139. #define MBEDTLS_HAVE_UDBL
  140. #endif /* !MBEDTLS_NO_UDBL_DIVISION */
  141. #elif defined(MBEDTLS_HAVE_INT64)
  142. /* Force 64-bit integers with unknown compiler */
  143. typedef int64_t mbedtls_mpi_sint;
  144. typedef uint64_t mbedtls_mpi_uint;
  145. #endif
  146. #endif /* !MBEDTLS_HAVE_INT32 */
  147. #if !defined(MBEDTLS_HAVE_INT64)
  148. /* Default to 32-bit compilation */
  149. #if !defined(MBEDTLS_HAVE_INT32)
  150. #define MBEDTLS_HAVE_INT32
  151. #endif /* !MBEDTLS_HAVE_INT32 */
  152. typedef int32_t mbedtls_mpi_sint;
  153. typedef uint32_t mbedtls_mpi_uint;
  154. #if !defined(MBEDTLS_NO_UDBL_DIVISION)
  155. typedef uint64_t mbedtls_t_udbl;
  156. #define MBEDTLS_HAVE_UDBL
  157. #endif /* !MBEDTLS_NO_UDBL_DIVISION */
  158. #endif /* !MBEDTLS_HAVE_INT64 */
  159. #ifdef __cplusplus
  160. extern "C" {
  161. #endif
  162. /**
  163. * \brief MPI structure
  164. */
  165. typedef struct
  166. {
  167. int s; /*!< integer sign */
  168. size_t n; /*!< total # of limbs */
  169. mbedtls_mpi_uint *p; /*!< pointer to limbs */
  170. }
  171. mbedtls_mpi;
  172. /**
  173. * \brief Initialize one MPI (make internal references valid)
  174. * This just makes it ready to be set or freed,
  175. * but does not define a value for the MPI.
  176. *
  177. * \param X One MPI to initialize.
  178. */
  179. void mbedtls_mpi_init( mbedtls_mpi *X );
  180. /**
  181. * \brief Unallocate one MPI
  182. *
  183. * \param X One MPI to unallocate.
  184. */
  185. void mbedtls_mpi_free( mbedtls_mpi *X );
  186. /**
  187. * \brief Enlarge to the specified number of limbs
  188. *
  189. * \param X MPI to grow
  190. * \param nblimbs The target number of limbs
  191. *
  192. * \return 0 if successful,
  193. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  194. */
  195. int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
  196. /**
  197. * \brief Resize down, keeping at least the specified number of limbs
  198. *
  199. * \param X MPI to shrink
  200. * \param nblimbs The minimum number of limbs to keep
  201. *
  202. * \return 0 if successful,
  203. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  204. */
  205. int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
  206. /**
  207. * \brief Copy the contents of Y into X
  208. *
  209. * \param X Destination MPI
  210. * \param Y Source MPI
  211. *
  212. * \return 0 if successful,
  213. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  214. */
  215. int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
  216. /**
  217. * \brief Swap the contents of X and Y
  218. *
  219. * \param X First MPI value
  220. * \param Y Second MPI value
  221. */
  222. void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
  223. /**
  224. * \brief Safe conditional assignement X = Y if assign is 1
  225. *
  226. * \param X MPI to conditionally assign to
  227. * \param Y Value to be assigned
  228. * \param assign 1: perform the assignment, 0: keep X's original value
  229. *
  230. * \return 0 if successful,
  231. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  232. *
  233. * \note This function is equivalent to
  234. * if( assign ) mbedtls_mpi_copy( X, Y );
  235. * except that it avoids leaking any information about whether
  236. * the assignment was done or not (the above code may leak
  237. * information through branch prediction and/or memory access
  238. * patterns analysis).
  239. */
  240. int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
  241. /**
  242. * \brief Safe conditional swap X <-> Y if swap is 1
  243. *
  244. * \param X First mbedtls_mpi value
  245. * \param Y Second mbedtls_mpi value
  246. * \param assign 1: perform the swap, 0: keep X and Y's original values
  247. *
  248. * \return 0 if successful,
  249. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  250. *
  251. * \note This function is equivalent to
  252. * if( assign ) mbedtls_mpi_swap( X, Y );
  253. * except that it avoids leaking any information about whether
  254. * the assignment was done or not (the above code may leak
  255. * information through branch prediction and/or memory access
  256. * patterns analysis).
  257. */
  258. int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
  259. /**
  260. * \brief Set value from integer
  261. *
  262. * \param X MPI to set
  263. * \param z Value to use
  264. *
  265. * \return 0 if successful,
  266. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  267. */
  268. int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
  269. /**
  270. * \brief Get a specific bit from X
  271. *
  272. * \param X MPI to use
  273. * \param pos Zero-based index of the bit in X
  274. *
  275. * \return Either a 0 or a 1
  276. */
  277. int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
  278. /**
  279. * \brief Set a bit of X to a specific value of 0 or 1
  280. *
  281. * \note Will grow X if necessary to set a bit to 1 in a not yet
  282. * existing limb. Will not grow if bit should be set to 0
  283. *
  284. * \param X MPI to use
  285. * \param pos Zero-based index of the bit in X
  286. * \param val The value to set the bit to (0 or 1)
  287. *
  288. * \return 0 if successful,
  289. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  290. * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
  291. */
  292. int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
  293. /**
  294. * \brief Return the number of zero-bits before the least significant
  295. * '1' bit
  296. *
  297. * Note: Thus also the zero-based index of the least significant '1' bit
  298. *
  299. * \param X MPI to use
  300. */
  301. size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
  302. /**
  303. * \brief Return the number of bits up to and including the most
  304. * significant '1' bit'
  305. *
  306. * Note: Thus also the one-based index of the most significant '1' bit
  307. *
  308. * \param X MPI to use
  309. */
  310. size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
  311. /**
  312. * \brief Return the total size in bytes
  313. *
  314. * \param X MPI to use
  315. */
  316. size_t mbedtls_mpi_size( const mbedtls_mpi *X );
  317. /**
  318. * \brief Import from an ASCII string
  319. *
  320. * \param X Destination MPI
  321. * \param radix Input numeric base
  322. * \param s Null-terminated string buffer
  323. *
  324. * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
  325. */
  326. int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
  327. /**
  328. * \brief Export into an ASCII string
  329. *
  330. * \param X Source MPI
  331. * \param radix Output numeric base
  332. * \param buf Buffer to write the string to
  333. * \param buflen Length of buf
  334. * \param olen Length of the string written, including final NUL byte
  335. *
  336. * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
  337. * *olen is always updated to reflect the amount
  338. * of data that has (or would have) been written.
  339. *
  340. * \note Call this function with buflen = 0 to obtain the
  341. * minimum required buffer size in *olen.
  342. */
  343. int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
  344. char *buf, size_t buflen, size_t *olen );
  345. #if defined(MBEDTLS_FS_IO)
  346. /**
  347. * \brief Read MPI from a line in an opened file
  348. *
  349. * \param X Destination MPI
  350. * \param radix Input numeric base
  351. * \param fin Input file handle
  352. *
  353. * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
  354. * the file read buffer is too small or a
  355. * MBEDTLS_ERR_MPI_XXX error code
  356. *
  357. * \note On success, this function advances the file stream
  358. * to the end of the current line or to EOF.
  359. *
  360. * The function returns 0 on an empty line.
  361. *
  362. * Leading whitespaces are ignored, as is a
  363. * '0x' prefix for radix 16.
  364. *
  365. */
  366. int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
  367. /**
  368. * \brief Write X into an opened file, or stdout if fout is NULL
  369. *
  370. * \param p Prefix, can be NULL
  371. * \param X Source MPI
  372. * \param radix Output numeric base
  373. * \param fout Output file handle (can be NULL)
  374. *
  375. * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
  376. *
  377. * \note Set fout == NULL to print X on the console.
  378. */
  379. int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
  380. #endif /* MBEDTLS_FS_IO */
  381. /**
  382. * \brief Import X from unsigned binary data, big endian
  383. *
  384. * \param X Destination MPI
  385. * \param buf Input buffer
  386. * \param buflen Input buffer size
  387. *
  388. * \return 0 if successful,
  389. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  390. */
  391. int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
  392. /**
  393. * \brief Export X into unsigned binary data, big endian.
  394. * Always fills the whole buffer, which will start with zeros
  395. * if the number is smaller.
  396. *
  397. * \param X Source MPI
  398. * \param buf Output buffer
  399. * \param buflen Output buffer size
  400. *
  401. * \return 0 if successful,
  402. * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
  403. */
  404. int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
  405. /**
  406. * \brief Left-shift: X <<= count
  407. *
  408. * \param X MPI to shift
  409. * \param count Amount to shift
  410. *
  411. * \return 0 if successful,
  412. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  413. */
  414. int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
  415. /**
  416. * \brief Right-shift: X >>= count
  417. *
  418. * \param X MPI to shift
  419. * \param count Amount to shift
  420. *
  421. * \return 0 if successful,
  422. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  423. */
  424. int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
  425. /**
  426. * \brief Compare unsigned values
  427. *
  428. * \param X Left-hand MPI
  429. * \param Y Right-hand MPI
  430. *
  431. * \return 1 if |X| is greater than |Y|,
  432. * -1 if |X| is lesser than |Y| or
  433. * 0 if |X| is equal to |Y|
  434. */
  435. int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
  436. /**
  437. * \brief Compare signed values
  438. *
  439. * \param X Left-hand MPI
  440. * \param Y Right-hand MPI
  441. *
  442. * \return 1 if X is greater than Y,
  443. * -1 if X is lesser than Y or
  444. * 0 if X is equal to Y
  445. */
  446. int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
  447. /**
  448. * \brief Compare signed values
  449. *
  450. * \param X Left-hand MPI
  451. * \param z The integer value to compare to
  452. *
  453. * \return 1 if X is greater than z,
  454. * -1 if X is lesser than z or
  455. * 0 if X is equal to z
  456. */
  457. int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
  458. /**
  459. * \brief Unsigned addition: X = |A| + |B|
  460. *
  461. * \param X Destination MPI
  462. * \param A Left-hand MPI
  463. * \param B Right-hand MPI
  464. *
  465. * \return 0 if successful,
  466. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  467. */
  468. int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
  469. /**
  470. * \brief Unsigned subtraction: X = |A| - |B|
  471. *
  472. * \param X Destination MPI
  473. * \param A Left-hand MPI
  474. * \param B Right-hand MPI
  475. *
  476. * \return 0 if successful,
  477. * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
  478. */
  479. int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
  480. /**
  481. * \brief Signed addition: X = A + B
  482. *
  483. * \param X Destination MPI
  484. * \param A Left-hand MPI
  485. * \param B Right-hand MPI
  486. *
  487. * \return 0 if successful,
  488. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  489. */
  490. int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
  491. /**
  492. * \brief Signed subtraction: X = A - B
  493. *
  494. * \param X Destination MPI
  495. * \param A Left-hand MPI
  496. * \param B Right-hand MPI
  497. *
  498. * \return 0 if successful,
  499. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  500. */
  501. int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
  502. /**
  503. * \brief Signed addition: X = A + b
  504. *
  505. * \param X Destination MPI
  506. * \param A Left-hand MPI
  507. * \param b The integer value to add
  508. *
  509. * \return 0 if successful,
  510. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  511. */
  512. int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
  513. /**
  514. * \brief Signed subtraction: X = A - b
  515. *
  516. * \param X Destination MPI
  517. * \param A Left-hand MPI
  518. * \param b The integer value to subtract
  519. *
  520. * \return 0 if successful,
  521. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  522. */
  523. int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
  524. /**
  525. * \brief Baseline multiplication: X = A * B
  526. *
  527. * \param X Destination MPI
  528. * \param A Left-hand MPI
  529. * \param B Right-hand MPI
  530. *
  531. * \return 0 if successful,
  532. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  533. */
  534. int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
  535. /**
  536. * \brief Baseline multiplication: X = A * b
  537. *
  538. * \param X Destination MPI
  539. * \param A Left-hand MPI
  540. * \param b The unsigned integer value to multiply with
  541. *
  542. * \note b is unsigned
  543. *
  544. * \return 0 if successful,
  545. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  546. */
  547. int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
  548. /**
  549. * \brief Division by mbedtls_mpi: A = Q * B + R
  550. *
  551. * \param Q Destination MPI for the quotient
  552. * \param R Destination MPI for the rest value
  553. * \param A Left-hand MPI
  554. * \param B Right-hand MPI
  555. *
  556. * \return 0 if successful,
  557. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  558. * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
  559. *
  560. * \note Either Q or R can be NULL.
  561. */
  562. int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
  563. /**
  564. * \brief Division by int: A = Q * b + R
  565. *
  566. * \param Q Destination MPI for the quotient
  567. * \param R Destination MPI for the rest value
  568. * \param A Left-hand MPI
  569. * \param b Integer to divide by
  570. *
  571. * \return 0 if successful,
  572. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  573. * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
  574. *
  575. * \note Either Q or R can be NULL.
  576. */
  577. int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b );
  578. /**
  579. * \brief Modulo: R = A mod B
  580. *
  581. * \param R Destination MPI for the rest value
  582. * \param A Left-hand MPI
  583. * \param B Right-hand MPI
  584. *
  585. * \return 0 if successful,
  586. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  587. * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
  588. * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
  589. */
  590. int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
  591. /**
  592. * \brief Modulo: r = A mod b
  593. *
  594. * \param r Destination mbedtls_mpi_uint
  595. * \param A Left-hand MPI
  596. * \param b Integer to divide by
  597. *
  598. * \return 0 if successful,
  599. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  600. * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
  601. * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
  602. */
  603. int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
  604. /**
  605. * \brief Sliding-window exponentiation: X = A^E mod N
  606. *
  607. * \param X Destination MPI
  608. * \param A Left-hand MPI
  609. * \param E Exponent MPI
  610. * \param N Modular MPI
  611. * \param _RR Speed-up MPI used for recalculations
  612. *
  613. * \return 0 if successful,
  614. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  615. * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
  616. * if E is negative
  617. *
  618. * \note _RR is used to avoid re-computing R*R mod N across
  619. * multiple calls, which speeds up things a bit. It can
  620. * be set to NULL if the extra performance is unneeded.
  621. */
  622. int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR );
  623. /**
  624. * \brief Fill an MPI X with size bytes of random
  625. *
  626. * \param X Destination MPI
  627. * \param size Size in bytes
  628. * \param f_rng RNG function
  629. * \param p_rng RNG parameter
  630. *
  631. * \return 0 if successful,
  632. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  633. */
  634. int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
  635. int (*f_rng)(void *, unsigned char *, size_t),
  636. void *p_rng );
  637. /**
  638. * \brief Greatest common divisor: G = gcd(A, B)
  639. *
  640. * \param G Destination MPI
  641. * \param A Left-hand MPI
  642. * \param B Right-hand MPI
  643. *
  644. * \return 0 if successful,
  645. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  646. */
  647. int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
  648. /**
  649. * \brief Modular inverse: X = A^-1 mod N
  650. *
  651. * \param X Destination MPI
  652. * \param A Left-hand MPI
  653. * \param N Right-hand MPI
  654. *
  655. * \return 0 if successful,
  656. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  657. * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is <= 1,
  658. MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N.
  659. */
  660. int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
  661. /**
  662. * \brief Miller-Rabin primality test
  663. *
  664. * \param X MPI to check
  665. * \param f_rng RNG function
  666. * \param p_rng RNG parameter
  667. *
  668. * \return 0 if successful (probably prime),
  669. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  670. * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
  671. */
  672. int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
  673. int (*f_rng)(void *, unsigned char *, size_t),
  674. void *p_rng );
  675. /**
  676. * \brief Prime number generation
  677. *
  678. * \param X Destination MPI
  679. * \param nbits Required size of X in bits
  680. * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
  681. * \param dh_flag If 1, then (X-1)/2 will be prime too
  682. * \param f_rng RNG function
  683. * \param p_rng RNG parameter
  684. *
  685. * \return 0 if successful (probably prime),
  686. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  687. * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
  688. */
  689. int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
  690. int (*f_rng)(void *, unsigned char *, size_t),
  691. void *p_rng );
  692. /**
  693. * \brief Checkup routine
  694. *
  695. * \return 0 if successful, or 1 if the test failed
  696. */
  697. int mbedtls_mpi_self_test( int verbose );
  698. #ifdef __cplusplus
  699. }
  700. #endif
  701. #endif /* bignum.h */