bignum.h 24 KB

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