bignum.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /**
  2. * \file bignum.h
  3. *
  4. * \brief Multi-precision integer library
  5. */
  6. /*
  7. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  8. * SPDX-License-Identifier: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. *
  22. * This file is part of mbed TLS (https://tls.mbed.org)
  23. */
  24. #ifndef MBEDTLS_BIGNUM_H
  25. #define MBEDTLS_BIGNUM_H
  26. #if !defined(MBEDTLS_CONFIG_FILE)
  27. #include "config.h"
  28. #else
  29. #include MBEDTLS_CONFIG_FILE
  30. #endif
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #if defined(MBEDTLS_FS_IO)
  34. #include <stdio.h>
  35. #endif
  36. #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
  37. #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
  38. #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
  39. #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
  40. #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
  41. #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
  42. #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
  43. #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */
  44. #define MBEDTLS_MPI_CHK(f) \
  45. do \
  46. { \
  47. if( ( ret = (f) ) != 0 ) \
  48. goto cleanup; \
  49. } while( 0 )
  50. /*
  51. * Maximum size MPIs are allowed to grow to in number of limbs.
  52. */
  53. #define MBEDTLS_MPI_MAX_LIMBS 10000
  54. #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
  55. /*
  56. * Maximum window size used for modular exponentiation. Default: 6
  57. * Minimum value: 1. Maximum value: 6.
  58. *
  59. * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
  60. * for the sliding window calculation. (So 64 by default)
  61. *
  62. * Reduction in size, reduces speed.
  63. */
  64. #define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
  65. #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
  66. #if !defined(MBEDTLS_MPI_MAX_SIZE)
  67. /*
  68. * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
  69. * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
  70. *
  71. * Note: Calculations can temporarily result in larger MPIs. So the number
  72. * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
  73. */
  74. #define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
  75. #endif /* !MBEDTLS_MPI_MAX_SIZE */
  76. #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
  77. /*
  78. * When reading from files with mbedtls_mpi_read_file() and writing to files with
  79. * mbedtls_mpi_write_file() the buffer should have space
  80. * for a (short) label, the MPI (in the provided radix), the newline
  81. * characters and the '\0'.
  82. *
  83. * By default we assume at least a 10 char label, a minimum radix of 10
  84. * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
  85. * Autosized at compile time for at least a 10 char label, a minimum radix
  86. * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
  87. *
  88. * This used to be statically sized to 1250 for a maximum of 4096 bit
  89. * numbers (1234 decimal chars).
  90. *
  91. * Calculate using the formula:
  92. * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
  93. * LabelSize + 6
  94. */
  95. #define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
  96. #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
  97. #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 )
  98. /*
  99. * Define the base integer type, architecture-wise.
  100. *
  101. * 32 or 64-bit integer types can be forced regardless of the underlying
  102. * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
  103. * respectively and undefining MBEDTLS_HAVE_ASM.
  104. *
  105. * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
  106. * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
  107. */
  108. #if !defined(MBEDTLS_HAVE_INT32)
  109. #if defined(_MSC_VER) && defined(_M_AMD64)
  110. /* Always choose 64-bit when using MSC */
  111. #if !defined(MBEDTLS_HAVE_INT64)
  112. #define MBEDTLS_HAVE_INT64
  113. #endif /* !MBEDTLS_HAVE_INT64 */
  114. typedef int64_t mbedtls_mpi_sint;
  115. typedef uint64_t mbedtls_mpi_uint;
  116. #elif defined(__GNUC__) && ( \
  117. defined(__amd64__) || defined(__x86_64__) || \
  118. defined(__ppc64__) || defined(__powerpc64__) || \
  119. defined(__ia64__) || defined(__alpha__) || \
  120. ( defined(__sparc__) && defined(__arch64__) ) || \
  121. defined(__s390x__) || defined(__mips64) )
  122. #if !defined(MBEDTLS_HAVE_INT64)
  123. #define MBEDTLS_HAVE_INT64
  124. #endif /* MBEDTLS_HAVE_INT64 */
  125. typedef int64_t mbedtls_mpi_sint;
  126. typedef uint64_t mbedtls_mpi_uint;
  127. #if !defined(MBEDTLS_NO_UDBL_DIVISION)
  128. /* mbedtls_t_udbl defined as 128-bit unsigned int */
  129. typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
  130. #define MBEDTLS_HAVE_UDBL
  131. #endif /* !MBEDTLS_NO_UDBL_DIVISION */
  132. #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
  133. /*
  134. * __ARMCC_VERSION is defined for both armcc and armclang and
  135. * __aarch64__ is only defined by armclang when compiling 64-bit code
  136. */
  137. #if !defined(MBEDTLS_HAVE_INT64)
  138. #define MBEDTLS_HAVE_INT64
  139. #endif /* !MBEDTLS_HAVE_INT64 */
  140. typedef int64_t mbedtls_mpi_sint;
  141. typedef uint64_t mbedtls_mpi_uint;
  142. #if !defined(MBEDTLS_NO_UDBL_DIVISION)
  143. /* mbedtls_t_udbl defined as 128-bit unsigned int */
  144. typedef __uint128_t mbedtls_t_udbl;
  145. #define MBEDTLS_HAVE_UDBL
  146. #endif /* !MBEDTLS_NO_UDBL_DIVISION */
  147. #elif defined(MBEDTLS_HAVE_INT64)
  148. /* Force 64-bit integers with unknown compiler */
  149. typedef int64_t mbedtls_mpi_sint;
  150. typedef uint64_t mbedtls_mpi_uint;
  151. #endif
  152. #endif /* !MBEDTLS_HAVE_INT32 */
  153. #if !defined(MBEDTLS_HAVE_INT64)
  154. /* Default to 32-bit compilation */
  155. #if !defined(MBEDTLS_HAVE_INT32)
  156. #define MBEDTLS_HAVE_INT32
  157. #endif /* !MBEDTLS_HAVE_INT32 */
  158. typedef int32_t mbedtls_mpi_sint;
  159. typedef uint32_t mbedtls_mpi_uint;
  160. #if !defined(MBEDTLS_NO_UDBL_DIVISION)
  161. typedef uint64_t mbedtls_t_udbl;
  162. #define MBEDTLS_HAVE_UDBL
  163. #endif /* !MBEDTLS_NO_UDBL_DIVISION */
  164. #endif /* !MBEDTLS_HAVE_INT64 */
  165. #ifdef __cplusplus
  166. extern "C" {
  167. #endif
  168. /**
  169. * \brief MPI structure
  170. */
  171. typedef struct mbedtls_mpi
  172. {
  173. int s; /*!< integer sign */
  174. size_t n; /*!< total # of limbs */
  175. mbedtls_mpi_uint *p; /*!< pointer to limbs */
  176. }
  177. mbedtls_mpi;
  178. /**
  179. * \brief Initialize an MPI context.
  180. *
  181. * This makes the MPI ready to be set or freed,
  182. * but does not define a value for the MPI.
  183. *
  184. * \param X The MPI context to initialize. This must not be \c NULL.
  185. */
  186. void mbedtls_mpi_init( mbedtls_mpi *X );
  187. /**
  188. * \brief This function frees the components of an MPI context.
  189. *
  190. * \param X The MPI context to be cleared. This may be \c NULL,
  191. * in which case this function is a no-op. If it is
  192. * not \c NULL, it must point to an initialized MPI.
  193. */
  194. void mbedtls_mpi_free( mbedtls_mpi *X );
  195. /**
  196. * \brief Enlarge an MPI to the specified number of limbs.
  197. *
  198. * \note This function does nothing if the MPI is
  199. * already large enough.
  200. *
  201. * \param X The MPI to grow. It must be initialized.
  202. * \param nblimbs The target number of limbs.
  203. *
  204. * \return \c 0 if successful.
  205. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  206. * \return Another negative error code on other kinds of failure.
  207. */
  208. int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
  209. /**
  210. * \brief This function resizes an MPI downwards, keeping at least the
  211. * specified number of limbs.
  212. *
  213. * If \c X is smaller than \c nblimbs, it is resized up
  214. * instead.
  215. *
  216. * \param X The MPI to shrink. This must point to an initialized MPI.
  217. * \param nblimbs The minimum number of limbs to keep.
  218. *
  219. * \return \c 0 if successful.
  220. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  221. * (this can only happen when resizing up).
  222. * \return Another negative error code on other kinds of failure.
  223. */
  224. int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
  225. /**
  226. * \brief Make a copy of an MPI.
  227. *
  228. * \param X The destination MPI. This must point to an initialized MPI.
  229. * \param Y The source MPI. This must point to an initialized MPI.
  230. *
  231. * \note The limb-buffer in the destination MPI is enlarged
  232. * if necessary to hold the value in the source MPI.
  233. *
  234. * \return \c 0 if successful.
  235. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  236. * \return Another negative error code on other kinds of failure.
  237. */
  238. int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
  239. /**
  240. * \brief Swap the contents of two MPIs.
  241. *
  242. * \param X The first MPI. It must be initialized.
  243. * \param Y The second MPI. It must be initialized.
  244. */
  245. void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
  246. /**
  247. * \brief Perform a safe conditional copy of MPI which doesn't
  248. * reveal whether the condition was true or not.
  249. *
  250. * \param X The MPI to conditionally assign to. This must point
  251. * to an initialized MPI.
  252. * \param Y The MPI to be assigned from. This must point to an
  253. * initialized MPI.
  254. * \param assign The condition deciding whether to perform the
  255. * assignment or not. Possible values:
  256. * * \c 1: Perform the assignment `X = Y`.
  257. * * \c 0: Keep the original value of \p X.
  258. *
  259. * \note This function is equivalent to
  260. * `if( assign ) mbedtls_mpi_copy( X, Y );`
  261. * except that it avoids leaking any information about whether
  262. * the assignment was done or not (the above code may leak
  263. * information through branch prediction and/or memory access
  264. * patterns analysis).
  265. *
  266. * \return \c 0 if successful.
  267. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  268. * \return Another negative error code on other kinds of failure.
  269. */
  270. int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
  271. /**
  272. * \brief Perform a safe conditional swap which doesn't
  273. * reveal whether the condition was true or not.
  274. *
  275. * \param X The first MPI. This must be initialized.
  276. * \param Y The second MPI. This must be initialized.
  277. * \param assign The condition deciding whether to perform
  278. * the swap or not. Possible values:
  279. * * \c 1: Swap the values of \p X and \p Y.
  280. * * \c 0: Keep the original values of \p X and \p Y.
  281. *
  282. * \note This function is equivalent to
  283. * if( assign ) mbedtls_mpi_swap( X, Y );
  284. * except that it avoids leaking any information about whether
  285. * the assignment was done or not (the above code may leak
  286. * information through branch prediction and/or memory access
  287. * patterns analysis).
  288. *
  289. * \return \c 0 if successful.
  290. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  291. * \return Another negative error code on other kinds of failure.
  292. *
  293. */
  294. int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
  295. /**
  296. * \brief Store integer value in MPI.
  297. *
  298. * \param X The MPI to set. This must be initialized.
  299. * \param z The value to use.
  300. *
  301. * \return \c 0 if successful.
  302. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  303. * \return Another negative error code on other kinds of failure.
  304. */
  305. int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
  306. /**
  307. * \brief Get a specific bit from an MPI.
  308. *
  309. * \param X The MPI to query. This must be initialized.
  310. * \param pos Zero-based index of the bit to query.
  311. *
  312. * \return \c 0 or \c 1 on success, depending on whether bit \c pos
  313. * of \c X is unset or set.
  314. * \return A negative error code on failure.
  315. */
  316. int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
  317. /**
  318. * \brief Modify a specific bit in an MPI.
  319. *
  320. * \note This function will grow the target MPI if necessary to set a
  321. * bit to \c 1 in a not yet existing limb. It will not grow if
  322. * the bit should be set to \c 0.
  323. *
  324. * \param X The MPI to modify. This must be initialized.
  325. * \param pos Zero-based index of the bit to modify.
  326. * \param val The desired value of bit \c pos: \c 0 or \c 1.
  327. *
  328. * \return \c 0 if successful.
  329. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  330. * \return Another negative error code on other kinds of failure.
  331. */
  332. int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
  333. /**
  334. * \brief Return the number of bits of value \c 0 before the
  335. * least significant bit of value \c 1.
  336. *
  337. * \note This is the same as the zero-based index of
  338. * the least significant bit of value \c 1.
  339. *
  340. * \param X The MPI to query.
  341. *
  342. * \return The number of bits of value \c 0 before the least significant
  343. * bit of value \c 1 in \p X.
  344. */
  345. size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
  346. /**
  347. * \brief Return the number of bits up to and including the most
  348. * significant bit of value \c 1.
  349. *
  350. * * \note This is same as the one-based index of the most
  351. * significant bit of value \c 1.
  352. *
  353. * \param X The MPI to query. This must point to an initialized MPI.
  354. *
  355. * \return The number of bits up to and including the most
  356. * significant bit of value \c 1.
  357. */
  358. size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
  359. /**
  360. * \brief Return the total size of an MPI value in bytes.
  361. *
  362. * \param X The MPI to use. This must point to an initialized MPI.
  363. *
  364. * \note The value returned by this function may be less than
  365. * the number of bytes used to store \p X internally.
  366. * This happens if and only if there are trailing bytes
  367. * of value zero.
  368. *
  369. * \return The least number of bytes capable of storing
  370. * the absolute value of \p X.
  371. */
  372. size_t mbedtls_mpi_size( const mbedtls_mpi *X );
  373. /**
  374. * \brief Import an MPI from an ASCII string.
  375. *
  376. * \param X The destination MPI. This must point to an initialized MPI.
  377. * \param radix The numeric base of the input string.
  378. * \param s Null-terminated string buffer.
  379. *
  380. * \return \c 0 if successful.
  381. * \return A negative error code on failure.
  382. */
  383. int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
  384. /**
  385. * \brief Export an MPI to an ASCII string.
  386. *
  387. * \param X The source MPI. This must point to an initialized MPI.
  388. * \param radix The numeric base of the output string.
  389. * \param buf The buffer to write the string to. This must be writable
  390. * buffer of length \p buflen Bytes.
  391. * \param buflen The available size in Bytes of \p buf.
  392. * \param olen The address at which to store the length of the string
  393. * written, including the final \c NULL byte. This must
  394. * not be \c NULL.
  395. *
  396. * \note You can call this function with `buflen == 0` to obtain the
  397. * minimum required buffer size in `*olen`.
  398. *
  399. * \return \c 0 if successful.
  400. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the target buffer \p buf
  401. * is too small to hold the value of \p X in the desired base.
  402. * In this case, `*olen` is nonetheless updated to contain the
  403. * size of \p buf required for a successful call.
  404. * \return Another negative error code on different kinds of failure.
  405. */
  406. int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
  407. char *buf, size_t buflen, size_t *olen );
  408. #if defined(MBEDTLS_FS_IO)
  409. /**
  410. * \brief Read an MPI from a line in an opened file.
  411. *
  412. * \param X The destination MPI. This must point to an initialized MPI.
  413. * \param radix The numeric base of the string representation used
  414. * in the source line.
  415. * \param fin The input file handle to use. This must not be \c NULL.
  416. *
  417. * \note On success, this function advances the file stream
  418. * to the end of the current line or to EOF.
  419. *
  420. * The function returns \c 0 on an empty line.
  421. *
  422. * Leading whitespaces are ignored, as is a
  423. * '0x' prefix for radix \c 16.
  424. *
  425. * \return \c 0 if successful.
  426. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the file read buffer
  427. * is too small.
  428. * \return Another negative error code on failure.
  429. */
  430. int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
  431. /**
  432. * \brief Export an MPI into an opened file.
  433. *
  434. * \param p A string prefix to emit prior to the MPI data.
  435. * For example, this might be a label, or "0x" when
  436. * printing in base \c 16. This may be \c NULL if no prefix
  437. * is needed.
  438. * \param X The source MPI. This must point to an initialized MPI.
  439. * \param radix The numeric base to be used in the emitted string.
  440. * \param fout The output file handle. This may be \c NULL, in which case
  441. * the output is written to \c stdout.
  442. *
  443. * \return \c 0 if successful.
  444. * \return A negative error code on failure.
  445. */
  446. int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X,
  447. int radix, FILE *fout );
  448. #endif /* MBEDTLS_FS_IO */
  449. /**
  450. * \brief Import an MPI from unsigned big endian binary data.
  451. *
  452. * \param X The destination MPI. This must point to an initialized MPI.
  453. * \param buf The input buffer. This must be a readable buffer of length
  454. * \p buflen Bytes.
  455. * \param buflen The length of the input buffer \p p in Bytes.
  456. *
  457. * \return \c 0 if successful.
  458. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  459. * \return Another negative error code on different kinds of failure.
  460. */
  461. int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf,
  462. size_t buflen );
  463. /**
  464. * \brief Export an MPI into unsigned big endian binary data
  465. * of fixed size.
  466. *
  467. * \param X The source MPI. This must point to an initialized MPI.
  468. * \param buf The output buffer. This must be a writable buffer of length
  469. * \p buflen Bytes.
  470. * \param buflen The size of the output buffer \p buf in Bytes.
  471. *
  472. * \return \c 0 if successful.
  473. * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
  474. * large enough to hold the value of \p X.
  475. * \return Another negative error code on different kinds of failure.
  476. */
  477. int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf,
  478. size_t buflen );
  479. /**
  480. * \brief Perform a left-shift on an MPI: X <<= count
  481. *
  482. * \param X The MPI to shift. This must point to an initialized MPI.
  483. * \param count The number of bits to shift by.
  484. *
  485. * \return \c 0 if successful.
  486. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  487. * \return Another negative error code on different kinds of failure.
  488. */
  489. int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
  490. /**
  491. * \brief Perform a right-shift on an MPI: X >>= count
  492. *
  493. * \param X The MPI to shift. This must point to an initialized MPI.
  494. * \param count The number of bits to shift by.
  495. *
  496. * \return \c 0 if successful.
  497. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  498. * \return Another negative error code on different kinds of failure.
  499. */
  500. int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
  501. /**
  502. * \brief Compare the absolute values of two MPIs.
  503. *
  504. * \param X The left-hand MPI. This must point to an initialized MPI.
  505. * \param Y The right-hand MPI. This must point to an initialized MPI.
  506. *
  507. * \return \c 1 if `|X|` is greater than `|Y|`.
  508. * \return \c -1 if `|X|` is lesser than `|Y|`.
  509. * \return \c 0 if `|X|` is equal to `|Y|`.
  510. */
  511. int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
  512. /**
  513. * \brief Compare two MPIs.
  514. *
  515. * \param X The left-hand MPI. This must point to an initialized MPI.
  516. * \param Y The right-hand MPI. This must point to an initialized MPI.
  517. *
  518. * \return \c 1 if \p X is greater than \p Y.
  519. * \return \c -1 if \p X is lesser than \p Y.
  520. * \return \c 0 if \p X is equal to \p Y.
  521. */
  522. int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
  523. /**
  524. * \brief Compare an MPI with an integer.
  525. *
  526. * \param X The left-hand MPI. This must point to an initialized MPI.
  527. * \param z The integer value to compare \p X to.
  528. *
  529. * \return \c 1 if \p X is greater than \p z.
  530. * \return \c -1 if \p X is lesser than \p z.
  531. * \return \c 0 if \p X is equal to \p z.
  532. */
  533. int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
  534. /**
  535. * \brief Perform an unsigned addition of MPIs: X = |A| + |B|
  536. *
  537. * \param X The destination MPI. This must point to an initialized MPI.
  538. * \param A The first summand. This must point to an initialized MPI.
  539. * \param B The second summand. This must point to an initialized MPI.
  540. *
  541. * \return \c 0 if successful.
  542. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  543. * \return Another negative error code on different kinds of failure.
  544. */
  545. int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A,
  546. const mbedtls_mpi *B );
  547. /**
  548. * \brief Perform an unsigned subtraction of MPIs: X = |A| - |B|
  549. *
  550. * \param X The destination MPI. This must point to an initialized MPI.
  551. * \param A The minuend. This must point to an initialized MPI.
  552. * \param B The subtrahend. This must point to an initialized MPI.
  553. *
  554. * \return \c 0 if successful.
  555. * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is greater than \p A.
  556. * \return Another negative error code on different kinds of failure.
  557. *
  558. */
  559. int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A,
  560. const mbedtls_mpi *B );
  561. /**
  562. * \brief Perform a signed addition of MPIs: X = A + B
  563. *
  564. * \param X The destination MPI. This must point to an initialized MPI.
  565. * \param A The first summand. This must point to an initialized MPI.
  566. * \param B The second summand. This must point to an initialized MPI.
  567. *
  568. * \return \c 0 if successful.
  569. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  570. * \return Another negative error code on different kinds of failure.
  571. */
  572. int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A,
  573. const mbedtls_mpi *B );
  574. /**
  575. * \brief Perform a signed subtraction of MPIs: X = A - B
  576. *
  577. * \param X The destination MPI. This must point to an initialized MPI.
  578. * \param A The minuend. This must point to an initialized MPI.
  579. * \param B The subtrahend. This must point to an initialized MPI.
  580. *
  581. * \return \c 0 if successful.
  582. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  583. * \return Another negative error code on different kinds of failure.
  584. */
  585. int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A,
  586. const mbedtls_mpi *B );
  587. /**
  588. * \brief Perform a signed addition of an MPI and an integer: X = A + b
  589. *
  590. * \param X The destination MPI. This must point to an initialized MPI.
  591. * \param A The first summand. This must point to an initialized MPI.
  592. * \param b The second summand.
  593. *
  594. * \return \c 0 if successful.
  595. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  596. * \return Another negative error code on different kinds of failure.
  597. */
  598. int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A,
  599. mbedtls_mpi_sint b );
  600. /**
  601. * \brief Perform a signed subtraction of an MPI and an integer:
  602. * X = A - b
  603. *
  604. * \param X The destination MPI. This must point to an initialized MPI.
  605. * \param A The minuend. This must point to an initialized MPI.
  606. * \param b The subtrahend.
  607. *
  608. * \return \c 0 if successful.
  609. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  610. * \return Another negative error code on different kinds of failure.
  611. */
  612. int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A,
  613. mbedtls_mpi_sint b );
  614. /**
  615. * \brief Perform a multiplication of two MPIs: X = A * B
  616. *
  617. * \param X The destination MPI. This must point to an initialized MPI.
  618. * \param A The first factor. This must point to an initialized MPI.
  619. * \param B The second factor. This must point to an initialized MPI.
  620. *
  621. * \return \c 0 if successful.
  622. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  623. * \return Another negative error code on different kinds of failure.
  624. *
  625. */
  626. int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A,
  627. const mbedtls_mpi *B );
  628. /**
  629. * \brief Perform a multiplication of an MPI with an unsigned integer:
  630. * X = A * b
  631. *
  632. * \param X The destination MPI. This must point to an initialized MPI.
  633. * \param A The first factor. This must point to an initialized MPI.
  634. * \param b The second factor.
  635. *
  636. * \return \c 0 if successful.
  637. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  638. * \return Another negative error code on different kinds of failure.
  639. *
  640. */
  641. int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A,
  642. mbedtls_mpi_uint b );
  643. /**
  644. * \brief Perform a division with remainder of two MPIs:
  645. * A = Q * B + R
  646. *
  647. * \param Q The destination MPI for the quotient.
  648. * This may be \c NULL if the value of the
  649. * quotient is not needed.
  650. * \param R The destination MPI for the remainder value.
  651. * This may be \c NULL if the value of the
  652. * remainder is not needed.
  653. * \param A The dividend. This must point to an initialized MPi.
  654. * \param B The divisor. This must point to an initialized MPI.
  655. *
  656. * \return \c 0 if successful.
  657. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  658. * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero.
  659. * \return Another negative error code on different kinds of failure.
  660. */
  661. int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
  662. const mbedtls_mpi *B );
  663. /**
  664. * \brief Perform a division with remainder of an MPI by an integer:
  665. * A = Q * b + R
  666. *
  667. * \param Q The destination MPI for the quotient.
  668. * This may be \c NULL if the value of the
  669. * quotient is not needed.
  670. * \param R The destination MPI for the remainder value.
  671. * This may be \c NULL if the value of the
  672. * remainder is not needed.
  673. * \param A The dividend. This must point to an initialized MPi.
  674. * \param b The divisor.
  675. *
  676. * \return \c 0 if successful.
  677. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  678. * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero.
  679. * \return Another negative error code on different kinds of failure.
  680. */
  681. int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
  682. mbedtls_mpi_sint b );
  683. /**
  684. * \brief Perform a modular reduction. R = A mod B
  685. *
  686. * \param R The destination MPI for the residue value.
  687. * This must point to an initialized MPI.
  688. * \param A The MPI to compute the residue of.
  689. * This must point to an initialized MPI.
  690. * \param B The base of the modular reduction.
  691. * This must point to an initialized MPI.
  692. *
  693. * \return \c 0 if successful.
  694. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  695. * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero.
  696. * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is negative.
  697. * \return Another negative error code on different kinds of failure.
  698. *
  699. */
  700. int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A,
  701. const mbedtls_mpi *B );
  702. /**
  703. * \brief Perform a modular reduction with respect to an integer.
  704. * r = A mod b
  705. *
  706. * \param r The address at which to store the residue.
  707. * This must not be \c NULL.
  708. * \param A The MPI to compute the residue of.
  709. * This must point to an initialized MPi.
  710. * \param b The integer base of the modular reduction.
  711. *
  712. * \return \c 0 if successful.
  713. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  714. * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero.
  715. * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p b is negative.
  716. * \return Another negative error code on different kinds of failure.
  717. */
  718. int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A,
  719. mbedtls_mpi_sint b );
  720. /**
  721. * \brief Perform a sliding-window exponentiation: X = A^E mod N
  722. *
  723. * \param X The destination MPI. This must point to an initialized MPI.
  724. * \param A The base of the exponentiation.
  725. * This must point to an initialized MPI.
  726. * \param E The exponent MPI. This must point to an initialized MPI.
  727. * \param N The base for the modular reduction. This must point to an
  728. * initialized MPI.
  729. * \param _RR A helper MPI depending solely on \p N which can be used to
  730. * speed-up multiple modular exponentiations for the same value
  731. * of \p N. This may be \c NULL. If it is not \c NULL, it must
  732. * point to an initialized MPI. If it hasn't been used after
  733. * the call to mbedtls_mpi_init(), this function will compute
  734. * the helper value and store it in \p _RR for reuse on
  735. * subsequent calls to this function. Otherwise, the function
  736. * will assume that \p _RR holds the helper value set by a
  737. * previous call to mbedtls_mpi_exp_mod(), and reuse it.
  738. *
  739. * \return \c 0 if successful.
  740. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  741. * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \c N is negative or
  742. * even, or if \c E is negative.
  743. * \return Another negative error code on different kinds of failures.
  744. *
  745. */
  746. int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
  747. const mbedtls_mpi *E, const mbedtls_mpi *N,
  748. mbedtls_mpi *_RR );
  749. /**
  750. * \brief Fill an MPI with a number of random bytes.
  751. *
  752. * \param X The destination MPI. This must point to an initialized MPI.
  753. * \param size The number of random bytes to generate.
  754. * \param f_rng The RNG function to use. This must not be \c NULL.
  755. * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
  756. * \c NULL if \p f_rng doesn't need a context argument.
  757. *
  758. * \return \c 0 if successful.
  759. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  760. * \return Another negative error code on failure.
  761. *
  762. * \note The bytes obtained from the RNG are interpreted
  763. * as a big-endian representation of an MPI; this can
  764. * be relevant in applications like deterministic ECDSA.
  765. */
  766. int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
  767. int (*f_rng)(void *, unsigned char *, size_t),
  768. void *p_rng );
  769. /**
  770. * \brief Compute the greatest common divisor: G = gcd(A, B)
  771. *
  772. * \param G The destination MPI. This must point to an initialized MPI.
  773. * \param A The first operand. This must point to an initialized MPI.
  774. * \param B The second operand. This must point to an initialized MPI.
  775. *
  776. * \return \c 0 if successful.
  777. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  778. * \return Another negative error code on different kinds of failure.
  779. */
  780. int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A,
  781. const mbedtls_mpi *B );
  782. /**
  783. * \brief Compute the modular inverse: X = A^-1 mod N
  784. *
  785. * \param X The destination MPI. This must point to an initialized MPI.
  786. * \param A The MPI to calculate the modular inverse of. This must point
  787. * to an initialized MPI.
  788. * \param N The base of the modular inversion. This must point to an
  789. * initialized MPI.
  790. *
  791. * \return \c 0 if successful.
  792. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  793. * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p N is less than
  794. * or equal to one.
  795. * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p has no modular inverse
  796. * with respect to \p N.
  797. */
  798. int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
  799. const mbedtls_mpi *N );
  800. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  801. #if defined(MBEDTLS_DEPRECATED_WARNING)
  802. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  803. #else
  804. #define MBEDTLS_DEPRECATED
  805. #endif
  806. /**
  807. * \brief Perform a Miller-Rabin primality test with error
  808. * probability of 2<sup>-80</sup>.
  809. *
  810. * \deprecated Superseded by mbedtls_mpi_is_prime_ext() which allows
  811. * specifying the number of Miller-Rabin rounds.
  812. *
  813. * \param X The MPI to check for primality.
  814. * This must point to an initialized MPI.
  815. * \param f_rng The RNG function to use. This must not be \c NULL.
  816. * \param p_rng The RNG parameter to be passed to \p f_rng.
  817. * This may be \c NULL if \p f_rng doesn't use a
  818. * context parameter.
  819. *
  820. * \return \c 0 if successful, i.e. \p X is probably prime.
  821. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  822. * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p X is not prime.
  823. * \return Another negative error code on other kinds of failure.
  824. */
  825. MBEDTLS_DEPRECATED int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
  826. int (*f_rng)(void *, unsigned char *, size_t),
  827. void *p_rng );
  828. #undef MBEDTLS_DEPRECATED
  829. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  830. /**
  831. * \brief Miller-Rabin primality test.
  832. *
  833. * \warning If \p X is potentially generated by an adversary, for example
  834. * when validating cryptographic parameters that you didn't
  835. * generate yourself and that are supposed to be prime, then
  836. * \p rounds should be at least the half of the security
  837. * strength of the cryptographic algorithm. On the other hand,
  838. * if \p X is chosen uniformly or non-adversially (as is the
  839. * case when mbedtls_mpi_gen_prime calls this function), then
  840. * \p rounds can be much lower.
  841. *
  842. * \param X The MPI to check for primality.
  843. * This must point to an initialized MPI.
  844. * \param rounds The number of bases to perform the Miller-Rabin primality
  845. * test for. The probability of returning 0 on a composite is
  846. * at most 2<sup>-2*\p rounds</sup>.
  847. * \param f_rng The RNG function to use. This must not be \c NULL.
  848. * \param p_rng The RNG parameter to be passed to \p f_rng.
  849. * This may be \c NULL if \p f_rng doesn't use
  850. * a context parameter.
  851. *
  852. * \return \c 0 if successful, i.e. \p X is probably prime.
  853. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  854. * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p X is not prime.
  855. * \return Another negative error code on other kinds of failure.
  856. */
  857. int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
  858. int (*f_rng)(void *, unsigned char *, size_t),
  859. void *p_rng );
  860. /**
  861. * \brief Flags for mbedtls_mpi_gen_prime()
  862. *
  863. * Each of these flags is a constraint on the result X returned by
  864. * mbedtls_mpi_gen_prime().
  865. */
  866. typedef enum {
  867. MBEDTLS_MPI_GEN_PRIME_FLAG_DH = 0x0001, /**< (X-1)/2 is prime too */
  868. MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR = 0x0002, /**< lower error rate from 2<sup>-80</sup> to 2<sup>-128</sup> */
  869. } mbedtls_mpi_gen_prime_flag_t;
  870. /**
  871. * \brief Generate a prime number.
  872. *
  873. * \param X The destination MPI to store the generated prime in.
  874. * This must point to an initialized MPi.
  875. * \param nbits The required size of the destination MPI in bits.
  876. * This must be between \c 3 and #MBEDTLS_MPI_MAX_BITS.
  877. * \param flags A mask of flags of type #mbedtls_mpi_gen_prime_flag_t.
  878. * \param f_rng The RNG function to use. This must not be \c NULL.
  879. * \param p_rng The RNG parameter to be passed to \p f_rng.
  880. * This may be \c NULL if \p f_rng doesn't use
  881. * a context parameter.
  882. *
  883. * \return \c 0 if successful, in which case \p X holds a
  884. * probably prime number.
  885. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
  886. * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if `nbits` is not between
  887. * \c 3 and #MBEDTLS_MPI_MAX_BITS.
  888. */
  889. int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
  890. int (*f_rng)(void *, unsigned char *, size_t),
  891. void *p_rng );
  892. #if defined(MBEDTLS_SELF_TEST)
  893. /**
  894. * \brief Checkup routine
  895. *
  896. * \return 0 if successful, or 1 if the test failed
  897. */
  898. int mbedtls_mpi_self_test( int verbose );
  899. #endif /* MBEDTLS_SELF_TEST */
  900. #ifdef __cplusplus
  901. }
  902. #endif
  903. #endif /* bignum.h */