bignum.h 26 KB

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