4 * \brief Multi-precision integer library
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #ifndef MBEDTLS_BIGNUM_H
23 #define MBEDTLS_BIGNUM_H
25 #if !defined(MBEDTLS_CONFIG_FILE)
26 #include "mbedtls/config.h"
28 #include MBEDTLS_CONFIG_FILE
34 #if defined(MBEDTLS_FS_IO)
38 #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
39 #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
40 #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
41 #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
42 #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
43 #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
44 #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
45 #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */
47 #define MBEDTLS_MPI_CHK(f) \
50 if( ( ret = (f) ) != 0 ) \
55 * Maximum size MPIs are allowed to grow to in number of limbs.
57 #define MBEDTLS_MPI_MAX_LIMBS 10000
59 #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
61 * Maximum window size used for modular exponentiation. Default: 6
62 * Minimum value: 1. Maximum value: 6.
64 * Result is an array of ( 2 ** MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
65 * for the sliding window calculation. (So 64 by default)
67 * Reduction in size, reduces speed.
69 #define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum window size used. */
70 #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
72 #if !defined(MBEDTLS_MPI_MAX_SIZE)
74 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
75 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
77 * Note: Calculations can temporarily result in larger MPIs. So the number
78 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
80 #define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
81 #endif /* !MBEDTLS_MPI_MAX_SIZE */
83 #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
86 * When reading from files with mbedtls_mpi_read_file() and writing to files with
87 * mbedtls_mpi_write_file() the buffer should have space
88 * for a (short) label, the MPI (in the provided radix), the newline
89 * characters and the '\0'.
91 * By default we assume at least a 10 char label, a minimum radix of 10
92 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
93 * Autosized at compile time for at least a 10 char label, a minimum radix
94 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
96 * This used to be statically sized to 1250 for a maximum of 4096 bit
97 * numbers (1234 decimal chars).
99 * Calculate using the formula:
100 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
103 #define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
104 #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
105 #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 )
108 * Define the base integer type, architecture-wise.
110 * 32 or 64-bit integer types can be forced regardless of the underlying
111 * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
112 * respectively and undefining MBEDTLS_HAVE_ASM.
114 * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
115 * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
117 #if !defined(MBEDTLS_HAVE_INT32)
118 #if defined(_MSC_VER) && defined(_M_AMD64)
119 /* Always choose 64-bit when using MSC */
120 #if !defined(MBEDTLS_HAVE_INT64)
121 #define MBEDTLS_HAVE_INT64
122 #endif /* !MBEDTLS_HAVE_INT64 */
123 typedef int64_t mbedtls_mpi_sint
;
124 typedef uint64_t mbedtls_mpi_uint
;
125 #elif defined(__GNUC__) && ( \
126 defined(__amd64__) || defined(__x86_64__) || \
127 defined(__ppc64__) || defined(__powerpc64__) || \
128 defined(__ia64__) || defined(__alpha__) || \
129 ( defined(__sparc__) && defined(__arch64__) ) || \
130 defined(__s390x__) || defined(__mips64) || \
131 defined(__aarch64__) )
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 unsigned int mbedtls_t_udbl
__attribute__((mode(TI
)));
140 #define MBEDTLS_HAVE_UDBL
141 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
142 #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
144 * __ARMCC_VERSION is defined for both armcc and armclang and
145 * __aarch64__ is only defined by armclang when compiling 64-bit code
147 #if !defined(MBEDTLS_HAVE_INT64)
148 #define MBEDTLS_HAVE_INT64
149 #endif /* !MBEDTLS_HAVE_INT64 */
150 typedef int64_t mbedtls_mpi_sint
;
151 typedef uint64_t mbedtls_mpi_uint
;
152 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
153 /* mbedtls_t_udbl defined as 128-bit unsigned int */
154 typedef __uint128_t mbedtls_t_udbl
;
155 #define MBEDTLS_HAVE_UDBL
156 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
157 #elif defined(MBEDTLS_HAVE_INT64)
158 /* Force 64-bit integers with unknown compiler */
159 typedef int64_t mbedtls_mpi_sint
;
160 typedef uint64_t mbedtls_mpi_uint
;
162 #endif /* !MBEDTLS_HAVE_INT32 */
164 #if !defined(MBEDTLS_HAVE_INT64)
165 /* Default to 32-bit compilation */
166 #if !defined(MBEDTLS_HAVE_INT32)
167 #define MBEDTLS_HAVE_INT32
168 #endif /* !MBEDTLS_HAVE_INT32 */
169 typedef int32_t mbedtls_mpi_sint
;
170 typedef uint32_t mbedtls_mpi_uint
;
171 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
172 typedef uint64_t mbedtls_t_udbl
;
173 #define MBEDTLS_HAVE_UDBL
174 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
175 #endif /* !MBEDTLS_HAVE_INT64 */
182 * \brief MPI structure
184 typedef struct mbedtls_mpi
{
185 int s
; /*!< Sign: -1 if the mpi is negative, 1 otherwise */
186 size_t n
; /*!< total # of limbs */
187 mbedtls_mpi_uint
*p
; /*!< pointer to limbs */
192 * \brief Initialize an MPI context.
194 * This makes the MPI ready to be set or freed,
195 * but does not define a value for the MPI.
197 * \param X The MPI context to initialize. This must not be \c NULL.
199 void mbedtls_mpi_init(mbedtls_mpi
*X
);
202 * \brief This function frees the components of an MPI context.
204 * \param X The MPI context to be cleared. This may be \c NULL,
205 * in which case this function is a no-op. If it is
206 * not \c NULL, it must point to an initialized MPI.
208 void mbedtls_mpi_free(mbedtls_mpi
*X
);
211 * \brief Enlarge an MPI to the specified number of limbs.
213 * \note This function does nothing if the MPI is
214 * already large enough.
216 * \param X The MPI to grow. It must be initialized.
217 * \param nblimbs The target number of limbs.
219 * \return \c 0 if successful.
220 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
221 * \return Another negative error code on other kinds of failure.
223 int mbedtls_mpi_grow(mbedtls_mpi
*X
, size_t nblimbs
);
226 * \brief This function resizes an MPI downwards, keeping at least the
227 * specified number of limbs.
229 * If \c X is smaller than \c nblimbs, it is resized up
232 * \param X The MPI to shrink. This must point to an initialized MPI.
233 * \param nblimbs The minimum number of limbs to keep.
235 * \return \c 0 if successful.
236 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
237 * (this can only happen when resizing up).
238 * \return Another negative error code on other kinds of failure.
240 int mbedtls_mpi_shrink(mbedtls_mpi
*X
, size_t nblimbs
);
243 * \brief Make a copy of an MPI.
245 * \param X The destination MPI. This must point to an initialized MPI.
246 * \param Y The source MPI. This must point to an initialized MPI.
248 * \note The limb-buffer in the destination MPI is enlarged
249 * if necessary to hold the value in the source MPI.
251 * \return \c 0 if successful.
252 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
253 * \return Another negative error code on other kinds of failure.
255 int mbedtls_mpi_copy(mbedtls_mpi
*X
, const mbedtls_mpi
*Y
);
258 * \brief Swap the contents of two MPIs.
260 * \param X The first MPI. It must be initialized.
261 * \param Y The second MPI. It must be initialized.
263 void mbedtls_mpi_swap(mbedtls_mpi
*X
, mbedtls_mpi
*Y
);
266 * \brief Perform a safe conditional copy of MPI which doesn't
267 * reveal whether the condition was true or not.
269 * \param X The MPI to conditionally assign to. This must point
270 * to an initialized MPI.
271 * \param Y The MPI to be assigned from. This must point to an
273 * \param assign The condition deciding whether to perform the
274 * assignment or not. Possible values:
275 * * \c 1: Perform the assignment `X = Y`.
276 * * \c 0: Keep the original value of \p X.
278 * \note This function is equivalent to
279 * `if( assign ) mbedtls_mpi_copy( X, Y );`
280 * except that it avoids leaking any information about whether
281 * the assignment was done or not (the above code may leak
282 * information through branch prediction and/or memory access
283 * patterns analysis).
285 * \return \c 0 if successful.
286 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
287 * \return Another negative error code on other kinds of failure.
289 int mbedtls_mpi_safe_cond_assign(mbedtls_mpi
*X
, const mbedtls_mpi
*Y
, unsigned char assign
);
292 * \brief Perform a safe conditional swap which doesn't
293 * reveal whether the condition was true or not.
295 * \param X The first MPI. This must be initialized.
296 * \param Y The second MPI. This must be initialized.
297 * \param assign The condition deciding whether to perform
298 * the swap or not. Possible values:
299 * * \c 1: Swap the values of \p X and \p Y.
300 * * \c 0: Keep the original values of \p X and \p Y.
302 * \note This function is equivalent to
303 * if( assign ) mbedtls_mpi_swap( X, Y );
304 * except that it avoids leaking any information about whether
305 * the assignment was done or not (the above code may leak
306 * information through branch prediction and/or memory access
307 * patterns analysis).
309 * \return \c 0 if successful.
310 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
311 * \return Another negative error code on other kinds of failure.
314 int mbedtls_mpi_safe_cond_swap(mbedtls_mpi
*X
, mbedtls_mpi
*Y
, unsigned char assign
);
317 * \brief Store integer value in MPI.
319 * \param X The MPI to set. This must be initialized.
320 * \param z The value to use.
322 * \return \c 0 if successful.
323 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
324 * \return Another negative error code on other kinds of failure.
326 int mbedtls_mpi_lset(mbedtls_mpi
*X
, mbedtls_mpi_sint z
);
329 * \brief Get a specific bit from an MPI.
331 * \param X The MPI to query. This must be initialized.
332 * \param pos Zero-based index of the bit to query.
334 * \return \c 0 or \c 1 on success, depending on whether bit \c pos
335 * of \c X is unset or set.
336 * \return A negative error code on failure.
338 int mbedtls_mpi_get_bit(const mbedtls_mpi
*X
, size_t pos
);
341 * \brief Modify a specific bit in an MPI.
343 * \note This function will grow the target MPI if necessary to set a
344 * bit to \c 1 in a not yet existing limb. It will not grow if
345 * the bit should be set to \c 0.
347 * \param X The MPI to modify. This must be initialized.
348 * \param pos Zero-based index of the bit to modify.
349 * \param val The desired value of bit \c pos: \c 0 or \c 1.
351 * \return \c 0 if successful.
352 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
353 * \return Another negative error code on other kinds of failure.
355 int mbedtls_mpi_set_bit(mbedtls_mpi
*X
, size_t pos
, unsigned char val
);
358 * \brief Return the number of bits of value \c 0 before the
359 * least significant bit of value \c 1.
361 * \note This is the same as the zero-based index of
362 * the least significant bit of value \c 1.
364 * \param X The MPI to query.
366 * \return The number of bits of value \c 0 before the least significant
367 * bit of value \c 1 in \p X.
369 size_t mbedtls_mpi_lsb(const mbedtls_mpi
*X
);
372 * \brief Return the number of bits up to and including the most
373 * significant bit of value \c 1.
375 * * \note This is same as the one-based index of the most
376 * significant bit of value \c 1.
378 * \param X The MPI to query. This must point to an initialized MPI.
380 * \return The number of bits up to and including the most
381 * significant bit of value \c 1.
383 size_t mbedtls_mpi_bitlen(const mbedtls_mpi
*X
);
386 * \brief Return the total size of an MPI value in bytes.
388 * \param X The MPI to use. This must point to an initialized MPI.
390 * \note The value returned by this function may be less than
391 * the number of bytes used to store \p X internally.
392 * This happens if and only if there are trailing bytes
395 * \return The least number of bytes capable of storing
396 * the absolute value of \p X.
398 size_t mbedtls_mpi_size(const mbedtls_mpi
*X
);
401 * \brief Import an MPI from an ASCII string.
403 * \param X The destination MPI. This must point to an initialized MPI.
404 * \param radix The numeric base of the input string.
405 * \param s Null-terminated string buffer.
407 * \return \c 0 if successful.
408 * \return A negative error code on failure.
410 int mbedtls_mpi_read_string(mbedtls_mpi
*X
, int radix
, const char *s
);
413 * \brief Export an MPI to an ASCII string.
415 * \param X The source MPI. This must point to an initialized MPI.
416 * \param radix The numeric base of the output string.
417 * \param buf The buffer to write the string to. This must be writable
418 * buffer of length \p buflen Bytes.
419 * \param buflen The available size in Bytes of \p buf.
420 * \param olen The address at which to store the length of the string
421 * written, including the final \c NULL byte. This must
424 * \note You can call this function with `buflen == 0` to obtain the
425 * minimum required buffer size in `*olen`.
427 * \return \c 0 if successful.
428 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the target buffer \p buf
429 * is too small to hold the value of \p X in the desired base.
430 * In this case, `*olen` is nonetheless updated to contain the
431 * size of \p buf required for a successful call.
432 * \return Another negative error code on different kinds of failure.
434 int mbedtls_mpi_write_string(const mbedtls_mpi
*X
, int radix
,
435 char *buf
, size_t buflen
, size_t *olen
);
437 #if defined(MBEDTLS_FS_IO)
439 * \brief Read an MPI from a line in an opened file.
441 * \param X The destination MPI. This must point to an initialized MPI.
442 * \param radix The numeric base of the string representation used
443 * in the source line.
444 * \param fin The input file handle to use. This must not be \c NULL.
446 * \note On success, this function advances the file stream
447 * to the end of the current line or to EOF.
449 * The function returns \c 0 on an empty line.
451 * Leading whitespaces are ignored, as is a
452 * '0x' prefix for radix \c 16.
454 * \return \c 0 if successful.
455 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the file read buffer
457 * \return Another negative error code on failure.
459 int mbedtls_mpi_read_file(mbedtls_mpi
*X
, int radix
, FILE *fin
);
462 * \brief Export an MPI into an opened file.
464 * \param p A string prefix to emit prior to the MPI data.
465 * For example, this might be a label, or "0x" when
466 * printing in base \c 16. This may be \c NULL if no prefix
468 * \param X The source MPI. This must point to an initialized MPI.
469 * \param radix The numeric base to be used in the emitted string.
470 * \param fout The output file handle. This may be \c NULL, in which case
471 * the output is written to \c stdout.
473 * \return \c 0 if successful.
474 * \return A negative error code on failure.
476 int mbedtls_mpi_write_file(const char *p
, const mbedtls_mpi
*X
,
477 int radix
, FILE *fout
);
478 #endif /* MBEDTLS_FS_IO */
481 * \brief Import an MPI from unsigned big endian binary data.
483 * \param X The destination MPI. This must point to an initialized MPI.
484 * \param buf The input buffer. This must be a readable buffer of length
486 * \param buflen The length of the input buffer \p p in Bytes.
488 * \return \c 0 if successful.
489 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
490 * \return Another negative error code on different kinds of failure.
492 int mbedtls_mpi_read_binary(mbedtls_mpi
*X
, const unsigned char *buf
,
496 * \brief Import X from unsigned binary data, little endian
498 * \param X The destination MPI. This must point to an initialized MPI.
499 * \param buf The input buffer. This must be a readable buffer of length
501 * \param buflen The length of the input buffer \p p in Bytes.
503 * \return \c 0 if successful.
504 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
505 * \return Another negative error code on different kinds of failure.
507 int mbedtls_mpi_read_binary_le(mbedtls_mpi
*X
,
508 const unsigned char *buf
, size_t buflen
);
511 * \brief Export X into unsigned binary data, big endian.
512 * Always fills the whole buffer, which will start with zeros
513 * if the number is smaller.
515 * \param X The source MPI. This must point to an initialized MPI.
516 * \param buf The output buffer. This must be a writable buffer of length
518 * \param buflen The size of the output buffer \p buf in Bytes.
520 * \return \c 0 if successful.
521 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
522 * large enough to hold the value of \p X.
523 * \return Another negative error code on different kinds of failure.
525 int mbedtls_mpi_write_binary(const mbedtls_mpi
*X
, unsigned char *buf
,
529 * \brief Export X into unsigned binary data, little endian.
530 * Always fills the whole buffer, which will end with zeros
531 * if the number is smaller.
533 * \param X The source MPI. This must point to an initialized MPI.
534 * \param buf The output buffer. This must be a writable buffer of length
536 * \param buflen The size of the output buffer \p buf in Bytes.
538 * \return \c 0 if successful.
539 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
540 * large enough to hold the value of \p X.
541 * \return Another negative error code on different kinds of failure.
543 int mbedtls_mpi_write_binary_le(const mbedtls_mpi
*X
,
544 unsigned char *buf
, size_t buflen
);
547 * \brief Perform a left-shift on an MPI: X <<= count
549 * \param X The MPI to shift. This must point to an initialized MPI.
550 * \param count The number of bits to shift by.
552 * \return \c 0 if successful.
553 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
554 * \return Another negative error code on different kinds of failure.
556 int mbedtls_mpi_shift_l(mbedtls_mpi
*X
, size_t count
);
559 * \brief Perform a right-shift on an MPI: X >>= count
561 * \param X The MPI to shift. This must point to an initialized MPI.
562 * \param count The number of bits to shift by.
564 * \return \c 0 if successful.
565 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
566 * \return Another negative error code on different kinds of failure.
568 int mbedtls_mpi_shift_r(mbedtls_mpi
*X
, size_t count
);
571 * \brief Compare the absolute values of two MPIs.
573 * \param X The left-hand MPI. This must point to an initialized MPI.
574 * \param Y The right-hand MPI. This must point to an initialized MPI.
576 * \return \c 1 if `|X|` is greater than `|Y|`.
577 * \return \c -1 if `|X|` is lesser than `|Y|`.
578 * \return \c 0 if `|X|` is equal to `|Y|`.
580 int mbedtls_mpi_cmp_abs(const mbedtls_mpi
*X
, const mbedtls_mpi
*Y
);
583 * \brief Compare two MPIs.
585 * \param X The left-hand MPI. This must point to an initialized MPI.
586 * \param Y The right-hand MPI. This must point to an initialized MPI.
588 * \return \c 1 if \p X is greater than \p Y.
589 * \return \c -1 if \p X is lesser than \p Y.
590 * \return \c 0 if \p X is equal to \p Y.
592 int mbedtls_mpi_cmp_mpi(const mbedtls_mpi
*X
, const mbedtls_mpi
*Y
);
595 * \brief Check if an MPI is less than the other in constant time.
597 * \param X The left-hand MPI. This must point to an initialized MPI
598 * with the same allocated length as Y.
599 * \param Y The right-hand MPI. This must point to an initialized MPI
600 * with the same allocated length as X.
601 * \param ret The result of the comparison:
602 * \c 1 if \p X is less than \p Y.
603 * \c 0 if \p X is greater than or equal to \p Y.
605 * \return 0 on success.
606 * \return MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the allocated length of
607 * the two input MPIs is not the same.
609 int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi
*X
, const mbedtls_mpi
*Y
,
613 * \brief Compare an MPI with an integer.
615 * \param X The left-hand MPI. This must point to an initialized MPI.
616 * \param z The integer value to compare \p X to.
618 * \return \c 1 if \p X is greater than \p z.
619 * \return \c -1 if \p X is lesser than \p z.
620 * \return \c 0 if \p X is equal to \p z.
622 int mbedtls_mpi_cmp_int(const mbedtls_mpi
*X
, mbedtls_mpi_sint z
);
625 * \brief Perform an unsigned addition of MPIs: X = |A| + |B|
627 * \param X The destination MPI. This must point to an initialized MPI.
628 * \param A The first summand. This must point to an initialized MPI.
629 * \param B The second summand. This must point to an initialized MPI.
631 * \return \c 0 if successful.
632 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
633 * \return Another negative error code on different kinds of failure.
635 int mbedtls_mpi_add_abs(mbedtls_mpi
*X
, const mbedtls_mpi
*A
,
636 const mbedtls_mpi
*B
);
639 * \brief Perform an unsigned subtraction of MPIs: X = |A| - |B|
641 * \param X The destination MPI. This must point to an initialized MPI.
642 * \param A The minuend. This must point to an initialized MPI.
643 * \param B The subtrahend. This must point to an initialized MPI.
645 * \return \c 0 if successful.
646 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is greater than \p A.
647 * \return Another negative error code on different kinds of failure.
650 int mbedtls_mpi_sub_abs(mbedtls_mpi
*X
, const mbedtls_mpi
*A
,
651 const mbedtls_mpi
*B
);
654 * \brief Perform a signed addition of MPIs: X = A + B
656 * \param X The destination MPI. This must point to an initialized MPI.
657 * \param A The first summand. This must point to an initialized MPI.
658 * \param B The second summand. This must point to an initialized MPI.
660 * \return \c 0 if successful.
661 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
662 * \return Another negative error code on different kinds of failure.
664 int mbedtls_mpi_add_mpi(mbedtls_mpi
*X
, const mbedtls_mpi
*A
,
665 const mbedtls_mpi
*B
);
668 * \brief Perform a signed subtraction of MPIs: X = A - B
670 * \param X The destination MPI. This must point to an initialized MPI.
671 * \param A The minuend. This must point to an initialized MPI.
672 * \param B The subtrahend. This must point to an initialized MPI.
674 * \return \c 0 if successful.
675 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
676 * \return Another negative error code on different kinds of failure.
678 int mbedtls_mpi_sub_mpi(mbedtls_mpi
*X
, const mbedtls_mpi
*A
,
679 const mbedtls_mpi
*B
);
682 * \brief Perform a signed addition of an MPI and an integer: X = A + b
684 * \param X The destination MPI. This must point to an initialized MPI.
685 * \param A The first summand. This must point to an initialized MPI.
686 * \param b The second summand.
688 * \return \c 0 if successful.
689 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
690 * \return Another negative error code on different kinds of failure.
692 int mbedtls_mpi_add_int(mbedtls_mpi
*X
, const mbedtls_mpi
*A
,
696 * \brief Perform a signed subtraction of an MPI and an integer:
699 * \param X The destination MPI. This must point to an initialized MPI.
700 * \param A The minuend. This must point to an initialized MPI.
701 * \param b The subtrahend.
703 * \return \c 0 if successful.
704 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
705 * \return Another negative error code on different kinds of failure.
707 int mbedtls_mpi_sub_int(mbedtls_mpi
*X
, const mbedtls_mpi
*A
,
711 * \brief Perform a multiplication of two MPIs: X = A * B
713 * \param X The destination MPI. This must point to an initialized MPI.
714 * \param A The first factor. This must point to an initialized MPI.
715 * \param B The second factor. This must point to an initialized MPI.
717 * \return \c 0 if successful.
718 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
719 * \return Another negative error code on different kinds of failure.
722 int mbedtls_mpi_mul_mpi(mbedtls_mpi
*X
, const mbedtls_mpi
*A
,
723 const mbedtls_mpi
*B
);
726 * \brief Perform a multiplication of an MPI with an unsigned integer:
729 * \param X The destination MPI. This must point to an initialized MPI.
730 * \param A The first factor. This must point to an initialized MPI.
731 * \param b The second factor.
733 * \return \c 0 if successful.
734 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
735 * \return Another negative error code on different kinds of failure.
738 int mbedtls_mpi_mul_int(mbedtls_mpi
*X
, const mbedtls_mpi
*A
,
742 * \brief Perform a division with remainder of two MPIs:
745 * \param Q The destination MPI for the quotient.
746 * This may be \c NULL if the value of the
747 * quotient is not needed.
748 * \param R The destination MPI for the remainder value.
749 * This may be \c NULL if the value of the
750 * remainder is not needed.
751 * \param A The dividend. This must point to an initialized MPi.
752 * \param B The divisor. This must point to an initialized MPI.
754 * \return \c 0 if successful.
755 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
756 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero.
757 * \return Another negative error code on different kinds of failure.
759 int mbedtls_mpi_div_mpi(mbedtls_mpi
*Q
, mbedtls_mpi
*R
, const mbedtls_mpi
*A
,
760 const mbedtls_mpi
*B
);
763 * \brief Perform a division with remainder of an MPI by an integer:
766 * \param Q The destination MPI for the quotient.
767 * This may be \c NULL if the value of the
768 * quotient is not needed.
769 * \param R The destination MPI for the remainder value.
770 * This may be \c NULL if the value of the
771 * remainder is not needed.
772 * \param A The dividend. This must point to an initialized MPi.
773 * \param b The divisor.
775 * \return \c 0 if successful.
776 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
777 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero.
778 * \return Another negative error code on different kinds of failure.
780 int mbedtls_mpi_div_int(mbedtls_mpi
*Q
, mbedtls_mpi
*R
, const mbedtls_mpi
*A
,
784 * \brief Perform a modular reduction. R = A mod B
786 * \param R The destination MPI for the residue value.
787 * This must point to an initialized MPI.
788 * \param A The MPI to compute the residue of.
789 * This must point to an initialized MPI.
790 * \param B The base of the modular reduction.
791 * This must point to an initialized MPI.
793 * \return \c 0 if successful.
794 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
795 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero.
796 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is negative.
797 * \return Another negative error code on different kinds of failure.
800 int mbedtls_mpi_mod_mpi(mbedtls_mpi
*R
, const mbedtls_mpi
*A
,
801 const mbedtls_mpi
*B
);
804 * \brief Perform a modular reduction with respect to an integer.
807 * \param r The address at which to store the residue.
808 * This must not be \c NULL.
809 * \param A The MPI to compute the residue of.
810 * This must point to an initialized MPi.
811 * \param b The integer base of the modular reduction.
813 * \return \c 0 if successful.
814 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
815 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero.
816 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p b is negative.
817 * \return Another negative error code on different kinds of failure.
819 int mbedtls_mpi_mod_int(mbedtls_mpi_uint
*r
, const mbedtls_mpi
*A
,
823 * \brief Perform a sliding-window exponentiation: X = A^E mod N
825 * \param X The destination MPI. This must point to an initialized MPI.
826 * \param A The base of the exponentiation.
827 * This must point to an initialized MPI.
828 * \param E The exponent MPI. This must point to an initialized MPI.
829 * \param N The base for the modular reduction. This must point to an
831 * \param _RR A helper MPI depending solely on \p N which can be used to
832 * speed-up multiple modular exponentiations for the same value
833 * of \p N. This may be \c NULL. If it is not \c NULL, it must
834 * point to an initialized MPI. If it hasn't been used after
835 * the call to mbedtls_mpi_init(), this function will compute
836 * the helper value and store it in \p _RR for reuse on
837 * subsequent calls to this function. Otherwise, the function
838 * will assume that \p _RR holds the helper value set by a
839 * previous call to mbedtls_mpi_exp_mod(), and reuse it.
841 * \return \c 0 if successful.
842 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
843 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \c N is negative or
844 * even, or if \c E is negative.
845 * \return Another negative error code on different kinds of failures.
848 int mbedtls_mpi_exp_mod(mbedtls_mpi
*X
, const mbedtls_mpi
*A
,
849 const mbedtls_mpi
*E
, const mbedtls_mpi
*N
,
853 * \brief Fill an MPI with a number of random bytes.
855 * \param X The destination MPI. This must point to an initialized MPI.
856 * \param size The number of random bytes to generate.
857 * \param f_rng The RNG function to use. This must not be \c NULL.
858 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
859 * \c NULL if \p f_rng doesn't need a context argument.
861 * \return \c 0 if successful.
862 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
863 * \return Another negative error code on failure.
865 * \note The bytes obtained from the RNG are interpreted
866 * as a big-endian representation of an MPI; this can
867 * be relevant in applications like deterministic ECDSA.
869 int mbedtls_mpi_fill_random(mbedtls_mpi
*X
, size_t size
,
870 int (*f_rng
)(void *, unsigned char *, size_t),
874 * \brief Compute the greatest common divisor: G = gcd(A, B)
876 * \param G The destination MPI. This must point to an initialized MPI.
877 * \param A The first operand. This must point to an initialized MPI.
878 * \param B The second operand. This must point to an initialized MPI.
880 * \return \c 0 if successful.
881 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
882 * \return Another negative error code on different kinds of failure.
884 int mbedtls_mpi_gcd(mbedtls_mpi
*G
, const mbedtls_mpi
*A
,
885 const mbedtls_mpi
*B
);
888 * \brief Compute the modular inverse: X = A^-1 mod N
890 * \param X The destination MPI. This must point to an initialized MPI.
891 * \param A The MPI to calculate the modular inverse of. This must point
892 * to an initialized MPI.
893 * \param N The base of the modular inversion. This must point to an
896 * \return \c 0 if successful.
897 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
898 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p N is less than
900 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p has no modular inverse
901 * with respect to \p N.
903 int mbedtls_mpi_inv_mod(mbedtls_mpi
*X
, const mbedtls_mpi
*A
,
904 const mbedtls_mpi
*N
);
906 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
907 #if defined(MBEDTLS_DEPRECATED_WARNING)
908 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
910 #define MBEDTLS_DEPRECATED
913 * \brief Perform a Miller-Rabin primality test with error
914 * probability of 2<sup>-80</sup>.
916 * \deprecated Superseded by mbedtls_mpi_is_prime_ext() which allows
917 * specifying the number of Miller-Rabin rounds.
919 * \param X The MPI to check for primality.
920 * This must point to an initialized MPI.
921 * \param f_rng The RNG function to use. This must not be \c NULL.
922 * \param p_rng The RNG parameter to be passed to \p f_rng.
923 * This may be \c NULL if \p f_rng doesn't use a
926 * \return \c 0 if successful, i.e. \p X is probably prime.
927 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
928 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p X is not prime.
929 * \return Another negative error code on other kinds of failure.
931 MBEDTLS_DEPRECATED
int mbedtls_mpi_is_prime(const mbedtls_mpi
*X
,
932 int (*f_rng
)(void *, unsigned char *, size_t),
934 #undef MBEDTLS_DEPRECATED
935 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
938 * \brief Miller-Rabin primality test.
940 * \warning If \p X is potentially generated by an adversary, for example
941 * when validating cryptographic parameters that you didn't
942 * generate yourself and that are supposed to be prime, then
943 * \p rounds should be at least the half of the security
944 * strength of the cryptographic algorithm. On the other hand,
945 * if \p X is chosen uniformly or non-adversially (as is the
946 * case when mbedtls_mpi_gen_prime calls this function), then
947 * \p rounds can be much lower.
949 * \param X The MPI to check for primality.
950 * This must point to an initialized MPI.
951 * \param rounds The number of bases to perform the Miller-Rabin primality
952 * test for. The probability of returning 0 on a composite is
953 * at most 2<sup>-2*\p rounds</sup>.
954 * \param f_rng The RNG function to use. This must not be \c NULL.
955 * \param p_rng The RNG parameter to be passed to \p f_rng.
956 * This may be \c NULL if \p f_rng doesn't use
957 * a context parameter.
959 * \return \c 0 if successful, i.e. \p X is probably prime.
960 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
961 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p X is not prime.
962 * \return Another negative error code on other kinds of failure.
964 int mbedtls_mpi_is_prime_ext(const mbedtls_mpi
*X
, int rounds
,
965 int (*f_rng
)(void *, unsigned char *, size_t),
968 * \brief Flags for mbedtls_mpi_gen_prime()
970 * Each of these flags is a constraint on the result X returned by
971 * mbedtls_mpi_gen_prime().
974 MBEDTLS_MPI_GEN_PRIME_FLAG_DH
= 0x0001, /**< (X-1)/2 is prime too */
975 MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR
= 0x0002, /**< lower error rate from 2<sup>-80</sup> to 2<sup>-128</sup> */
976 } mbedtls_mpi_gen_prime_flag_t
;
979 * \brief Generate a prime number.
981 * \param X The destination MPI to store the generated prime in.
982 * This must point to an initialized MPi.
983 * \param nbits The required size of the destination MPI in bits.
984 * This must be between \c 3 and #MBEDTLS_MPI_MAX_BITS.
985 * \param flags A mask of flags of type #mbedtls_mpi_gen_prime_flag_t.
986 * \param f_rng The RNG function to use. This must not be \c NULL.
987 * \param p_rng The RNG parameter to be passed to \p f_rng.
988 * This may be \c NULL if \p f_rng doesn't use
989 * a context parameter.
991 * \return \c 0 if successful, in which case \p X holds a
992 * probably prime number.
993 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
994 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if `nbits` is not between
995 * \c 3 and #MBEDTLS_MPI_MAX_BITS.
997 int mbedtls_mpi_gen_prime(mbedtls_mpi
*X
, size_t nbits
, int flags
,
998 int (*f_rng
)(void *, unsigned char *, size_t),
1001 #if defined(MBEDTLS_SELF_TEST)
1004 * \brief Checkup routine
1006 * \return 0 if successful, or 1 if the test failed
1008 int mbedtls_mpi_self_test(int verbose
);
1010 #endif /* MBEDTLS_SELF_TEST */
1016 #endif /* bignum.h */