bignum.h 26 KB

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