| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088 |
- /**
- * \file bignum.h
- *
- * \brief Multi-precision integer library
- */
- /*
- * Copyright The Mbed TLS Contributors
- * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
- */
- #ifndef MBEDTLS_BIGNUM_H
- #define MBEDTLS_BIGNUM_H
- #include "mbedtls/private_access.h"
- #include "mbedtls/build_info.h"
- #include "mbedtls/platform_util.h"
- #include <stddef.h>
- #include <stdint.h>
- #if defined(MBEDTLS_FS_IO)
- #include <stdio.h>
- #endif
- /** An error occurred while reading from or writing to a file. */
- #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
- /** Bad input parameters to function. */
- #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
- /** There is an invalid character in the digit string. */
- #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
- /** The buffer is too small to write to. */
- #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
- /** The input arguments are negative or result in illegal output. */
- #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
- /** The input argument for division is zero, which is not allowed. */
- #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
- /** The input arguments are not acceptable. */
- #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
- /** Memory allocation failed. */
- #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
- #define MBEDTLS_MPI_CHK(f) \
- do \
- { \
- if ((ret = (f)) != 0) \
- goto cleanup; \
- } while (0)
- /*
- * Maximum size MPIs are allowed to grow to in number of limbs.
- */
- #define MBEDTLS_MPI_MAX_LIMBS 10000
- #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
- /*
- * Maximum window size used for modular exponentiation. Default: 3
- * Minimum value: 1. Maximum value: 6.
- *
- * Result is an array of ( 2 ** MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
- * for the sliding window calculation. (So 8 by default)
- *
- * Reduction in size, reduces speed.
- */
- #define MBEDTLS_MPI_WINDOW_SIZE 3 /**< Maximum window size used. */
- #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
- #if !defined(MBEDTLS_MPI_MAX_SIZE)
- /*
- * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
- * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
- *
- * Note: Calculations can temporarily result in larger MPIs. So the number
- * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
- */
- #define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
- #endif /* !MBEDTLS_MPI_MAX_SIZE */
- #define MBEDTLS_MPI_MAX_BITS (8 * MBEDTLS_MPI_MAX_SIZE) /**< Maximum number of bits for usable MPIs. */
- /*
- * When reading from files with mbedtls_mpi_read_file() and writing to files with
- * mbedtls_mpi_write_file() the buffer should have space
- * for a (short) label, the MPI (in the provided radix), the newline
- * characters and the '\0'.
- *
- * By default we assume at least a 10 char label, a minimum radix of 10
- * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
- * Autosized at compile time for at least a 10 char label, a minimum radix
- * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
- *
- * This used to be statically sized to 1250 for a maximum of 4096 bit
- * numbers (1234 decimal chars).
- *
- * Calculate using the formula:
- * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
- * LabelSize + 6
- */
- #define MBEDTLS_MPI_MAX_BITS_SCALE100 (100 * MBEDTLS_MPI_MAX_BITS)
- #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
- #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)
- /*
- * Define the base integer type, architecture-wise.
- *
- * 32 or 64-bit integer types can be forced regardless of the underlying
- * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
- * respectively and undefining MBEDTLS_HAVE_ASM.
- *
- * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
- * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
- */
- #if !defined(MBEDTLS_HAVE_INT32)
- #if defined(_MSC_VER) && defined(_M_AMD64)
- /* Always choose 64-bit when using MSC */
- #if !defined(MBEDTLS_HAVE_INT64)
- #define MBEDTLS_HAVE_INT64
- #endif /* !MBEDTLS_HAVE_INT64 */
- typedef int64_t mbedtls_mpi_sint;
- typedef uint64_t mbedtls_mpi_uint;
- #define MBEDTLS_MPI_UINT_MAX UINT64_MAX
- #elif defined(__GNUC__) && ( \
- defined(__amd64__) || defined(__x86_64__) || \
- defined(__ppc64__) || defined(__powerpc64__) || \
- defined(__ia64__) || defined(__alpha__) || \
- (defined(__sparc__) && defined(__arch64__)) || \
- defined(__s390x__) || defined(__mips64) || \
- defined(__aarch64__))
- #if !defined(MBEDTLS_HAVE_INT64)
- #define MBEDTLS_HAVE_INT64
- #endif /* MBEDTLS_HAVE_INT64 */
- typedef int64_t mbedtls_mpi_sint;
- typedef uint64_t mbedtls_mpi_uint;
- #define MBEDTLS_MPI_UINT_MAX UINT64_MAX
- #if !defined(MBEDTLS_NO_UDBL_DIVISION)
- /* mbedtls_t_udbl defined as 128-bit unsigned int */
- typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
- #define MBEDTLS_HAVE_UDBL
- #endif /* !MBEDTLS_NO_UDBL_DIVISION */
- #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
- /*
- * __ARMCC_VERSION is defined for both armcc and armclang and
- * __aarch64__ is only defined by armclang when compiling 64-bit code
- */
- #if !defined(MBEDTLS_HAVE_INT64)
- #define MBEDTLS_HAVE_INT64
- #endif /* !MBEDTLS_HAVE_INT64 */
- typedef int64_t mbedtls_mpi_sint;
- typedef uint64_t mbedtls_mpi_uint;
- #define MBEDTLS_MPI_UINT_MAX UINT64_MAX
- #if !defined(MBEDTLS_NO_UDBL_DIVISION)
- /* mbedtls_t_udbl defined as 128-bit unsigned int */
- typedef __uint128_t mbedtls_t_udbl;
- #define MBEDTLS_HAVE_UDBL
- #endif /* !MBEDTLS_NO_UDBL_DIVISION */
- #elif defined(MBEDTLS_HAVE_INT64)
- /* Force 64-bit integers with unknown compiler */
- typedef int64_t mbedtls_mpi_sint;
- typedef uint64_t mbedtls_mpi_uint;
- #define MBEDTLS_MPI_UINT_MAX UINT64_MAX
- #endif
- #endif /* !MBEDTLS_HAVE_INT32 */
- #if !defined(MBEDTLS_HAVE_INT64)
- /* Default to 32-bit compilation */
- #if !defined(MBEDTLS_HAVE_INT32)
- #define MBEDTLS_HAVE_INT32
- #endif /* !MBEDTLS_HAVE_INT32 */
- typedef int32_t mbedtls_mpi_sint;
- typedef uint32_t mbedtls_mpi_uint;
- #define MBEDTLS_MPI_UINT_MAX UINT32_MAX
- #if !defined(MBEDTLS_NO_UDBL_DIVISION)
- typedef uint64_t mbedtls_t_udbl;
- #define MBEDTLS_HAVE_UDBL
- #endif /* !MBEDTLS_NO_UDBL_DIVISION */
- #endif /* !MBEDTLS_HAVE_INT64 */
- /*
- * Sanity check that exactly one of MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64 is defined,
- * so that code elsewhere doesn't have to check.
- */
- #if (!(defined(MBEDTLS_HAVE_INT32) || defined(MBEDTLS_HAVE_INT64))) || \
- (defined(MBEDTLS_HAVE_INT32) && defined(MBEDTLS_HAVE_INT64))
- #error "Only 32-bit or 64-bit limbs are supported in bignum"
- #endif
- /** \typedef mbedtls_mpi_uint
- * \brief The type of machine digits in a bignum, called _limbs_.
- *
- * This is always an unsigned integer type with no padding bits. The size
- * is platform-dependent.
- */
- /** \typedef mbedtls_mpi_sint
- * \brief The signed type corresponding to #mbedtls_mpi_uint.
- *
- * This is always an signed integer type with no padding bits. The size
- * is platform-dependent.
- */
- #ifdef __cplusplus
- extern "C" {
- #endif
- /**
- * \brief MPI structure
- */
- typedef struct mbedtls_mpi {
- /** Pointer to limbs.
- *
- * This may be \c NULL if \c n is 0.
- */
- mbedtls_mpi_uint *MBEDTLS_PRIVATE(p);
- /** Sign: -1 if the mpi is negative, 1 otherwise.
- *
- * The number 0 must be represented with `s = +1`. Although many library
- * functions treat all-limbs-zero as equivalent to a valid representation
- * of 0 regardless of the sign bit, there are exceptions, so bignum
- * functions and external callers must always set \c s to +1 for the
- * number zero.
- *
- * Note that this implies that calloc() or `... = {0}` does not create
- * a valid MPI representation. You must call mbedtls_mpi_init().
- */
- signed short MBEDTLS_PRIVATE(s);
- /** Total number of limbs in \c p. */
- unsigned short MBEDTLS_PRIVATE(n);
- /* Make sure that MBEDTLS_MPI_MAX_LIMBS fits in n.
- * Use the same limit value on all platforms so that we don't have to
- * think about different behavior on the rare platforms where
- * unsigned short can store values larger than the minimum required by
- * the C language, which is 65535.
- */
- #if MBEDTLS_MPI_MAX_LIMBS > 65535
- #error "MBEDTLS_MPI_MAX_LIMBS > 65535 is not supported"
- #endif
- }
- mbedtls_mpi;
- /**
- * \brief Initialize an MPI context.
- *
- * This makes the MPI ready to be set or freed,
- * but does not define a value for the MPI.
- *
- * \param X The MPI context to initialize. This must not be \c NULL.
- */
- void mbedtls_mpi_init(mbedtls_mpi *X);
- /**
- * \brief This function frees the components of an MPI context.
- *
- * \param X The MPI context to be cleared. This may be \c NULL,
- * in which case this function is a no-op. If it is
- * not \c NULL, it must point to an initialized MPI.
- */
- void mbedtls_mpi_free(mbedtls_mpi *X);
- /**
- * \brief Enlarge an MPI to the specified number of limbs.
- *
- * \note This function does nothing if the MPI is
- * already large enough.
- *
- * \param X The MPI to grow. It must be initialized.
- * \param nblimbs The target number of limbs.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
- * \return Another negative error code on other kinds of failure.
- */
- int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs);
- /**
- * \brief This function resizes an MPI downwards, keeping at least the
- * specified number of limbs.
- *
- * If \c X is smaller than \c nblimbs, it is resized up
- * instead.
- *
- * \param X The MPI to shrink. This must point to an initialized MPI.
- * \param nblimbs The minimum number of limbs to keep.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
- * (this can only happen when resizing up).
- * \return Another negative error code on other kinds of failure.
- */
- int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs);
- /**
- * \brief Make a copy of an MPI.
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * \param Y The source MPI. This must point to an initialized MPI.
- *
- * \note The limb-buffer in the destination MPI is enlarged
- * if necessary to hold the value in the source MPI.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
- * \return Another negative error code on other kinds of failure.
- */
- int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y);
- /**
- * \brief Swap the contents of two MPIs.
- *
- * \param X The first MPI. It must be initialized.
- * \param Y The second MPI. It must be initialized.
- */
- void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y);
- /**
- * \brief Perform a safe conditional copy of MPI which doesn't
- * reveal whether the condition was true or not.
- *
- * \param X The MPI to conditionally assign to. This must point
- * to an initialized MPI.
- * \param Y The MPI to be assigned from. This must point to an
- * initialized MPI.
- * \param assign The condition deciding whether to perform the
- * assignment or not. Must be either 0 or 1:
- * * \c 1: Perform the assignment `X = Y`.
- * * \c 0: Keep the original value of \p X.
- *
- * \note This function is equivalent to
- * `if( assign ) mbedtls_mpi_copy( X, Y );`
- * except that it avoids leaking any information about whether
- * the assignment was done or not (the above code may leak
- * information through branch prediction and/or memory access
- * patterns analysis).
- *
- * \warning If \p assign is neither 0 nor 1, the result of this function
- * is indeterminate, and the resulting value in \p X might be
- * neither its original value nor the value in \p Y.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
- * \return Another negative error code on other kinds of failure.
- */
- int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign);
- /**
- * \brief Perform a safe conditional swap which doesn't
- * reveal whether the condition was true or not.
- *
- * \param X The first MPI. This must be initialized.
- * \param Y The second MPI. This must be initialized.
- * \param swap The condition deciding whether to perform
- * the swap or not. Must be either 0 or 1:
- * * \c 1: Swap the values of \p X and \p Y.
- * * \c 0: Keep the original values of \p X and \p Y.
- *
- * \note This function is equivalent to
- * if( swap ) mbedtls_mpi_swap( X, Y );
- * except that it avoids leaking any information about whether
- * the swap was done or not (the above code may leak
- * information through branch prediction and/or memory access
- * patterns analysis).
- *
- * \warning If \p swap is neither 0 nor 1, the result of this function
- * is indeterminate, and both \p X and \p Y might end up with
- * values different to either of the original ones.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
- * \return Another negative error code on other kinds of failure.
- *
- */
- int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap);
- /**
- * \brief Store integer value in MPI.
- *
- * \param X The MPI to set. This must be initialized.
- * \param z The value to use.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
- * \return Another negative error code on other kinds of failure.
- */
- int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z);
- /**
- * \brief Get a specific bit from an MPI.
- *
- * \param X The MPI to query. This must be initialized.
- * \param pos Zero-based index of the bit to query.
- *
- * \return \c 0 or \c 1 on success, depending on whether bit \c pos
- * of \c X is unset or set.
- * \return A negative error code on failure.
- */
- int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos);
- /**
- * \brief Modify a specific bit in an MPI.
- *
- * \note This function will grow the target MPI if necessary to set a
- * bit to \c 1 in a not yet existing limb. It will not grow if
- * the bit should be set to \c 0.
- *
- * \param X The MPI to modify. This must be initialized.
- * \param pos Zero-based index of the bit to modify.
- * \param val The desired value of bit \c pos: \c 0 or \c 1.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
- * \return Another negative error code on other kinds of failure.
- */
- int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val);
- /**
- * \brief Return the number of bits of value \c 0 before the
- * least significant bit of value \c 1.
- *
- * \note This is the same as the zero-based index of
- * the least significant bit of value \c 1.
- *
- * \param X The MPI to query.
- *
- * \return The number of bits of value \c 0 before the least significant
- * bit of value \c 1 in \p X.
- */
- size_t mbedtls_mpi_lsb(const mbedtls_mpi *X);
- /**
- * \brief Return the number of bits up to and including the most
- * significant bit of value \c 1.
- *
- * * \note This is same as the one-based index of the most
- * significant bit of value \c 1.
- *
- * \param X The MPI to query. This must point to an initialized MPI.
- *
- * \return The number of bits up to and including the most
- * significant bit of value \c 1.
- */
- size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X);
- /**
- * \brief Return the total size of an MPI value in bytes.
- *
- * \param X The MPI to use. This must point to an initialized MPI.
- *
- * \note The value returned by this function may be less than
- * the number of bytes used to store \p X internally.
- * This happens if and only if there are trailing bytes
- * of value zero.
- *
- * \return The least number of bytes capable of storing
- * the absolute value of \p X.
- */
- size_t mbedtls_mpi_size(const mbedtls_mpi *X);
- /**
- * \brief Import an MPI from an ASCII string.
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * \param radix The numeric base of the input string.
- * \param s Null-terminated string buffer.
- *
- * \return \c 0 if successful.
- * \return A negative error code on failure.
- */
- int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s);
- /**
- * \brief Export an MPI to an ASCII string.
- *
- * \param X The source MPI. This must point to an initialized MPI.
- * \param radix The numeric base of the output string.
- * \param buf The buffer to write the string to. This must be writable
- * buffer of length \p buflen Bytes.
- * \param buflen The available size in Bytes of \p buf.
- * \param olen The address at which to store the length of the string
- * written, including the final \c NULL byte. This must
- * not be \c NULL.
- *
- * \note You can call this function with `buflen == 0` to obtain the
- * minimum required buffer size in `*olen`.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the target buffer \p buf
- * is too small to hold the value of \p X in the desired base.
- * In this case, `*olen` is nonetheless updated to contain the
- * size of \p buf required for a successful call.
- * \return Another negative error code on different kinds of failure.
- */
- int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
- char *buf, size_t buflen, size_t *olen);
- #if defined(MBEDTLS_FS_IO)
- /**
- * \brief Read an MPI from a line in an opened file.
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * \param radix The numeric base of the string representation used
- * in the source line.
- * \param fin The input file handle to use. This must not be \c NULL.
- *
- * \note On success, this function advances the file stream
- * to the end of the current line or to EOF.
- *
- * The function returns \c 0 on an empty line.
- *
- * Leading whitespaces are ignored, as is a
- * '0x' prefix for radix \c 16.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the file read buffer
- * is too small.
- * \return Another negative error code on failure.
- */
- int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin);
- /**
- * \brief Export an MPI into an opened file.
- *
- * \param p A string prefix to emit prior to the MPI data.
- * For example, this might be a label, or "0x" when
- * printing in base \c 16. This may be \c NULL if no prefix
- * is needed.
- * \param X The source MPI. This must point to an initialized MPI.
- * \param radix The numeric base to be used in the emitted string.
- * \param fout The output file handle. This may be \c NULL, in which case
- * the output is written to \c stdout.
- *
- * \return \c 0 if successful.
- * \return A negative error code on failure.
- */
- int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X,
- int radix, FILE *fout);
- #endif /* MBEDTLS_FS_IO */
- /**
- * \brief Import an MPI from unsigned big endian binary data.
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * \param buf The input buffer. This must be a readable buffer of length
- * \p buflen Bytes.
- * \param buflen The length of the input buffer \p buf in Bytes.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
- * \return Another negative error code on different kinds of failure.
- */
- int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf,
- size_t buflen);
- /**
- * \brief Import X from unsigned binary data, little endian
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * \param buf The input buffer. This must be a readable buffer of length
- * \p buflen Bytes.
- * \param buflen The length of the input buffer \p buf in Bytes.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
- * \return Another negative error code on different kinds of failure.
- */
- int mbedtls_mpi_read_binary_le(mbedtls_mpi *X,
- const unsigned char *buf, size_t buflen);
- /**
- * \brief Export X into unsigned binary data, big endian.
- * Always fills the whole buffer, which will start with zeros
- * if the number is smaller.
- *
- * \param X The source MPI. This must point to an initialized MPI.
- * \param buf The output buffer. This must be a writable buffer of length
- * \p buflen Bytes.
- * \param buflen The size of the output buffer \p buf in Bytes.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
- * large enough to hold the value of \p X.
- * \return Another negative error code on different kinds of failure.
- */
- int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf,
- size_t buflen);
- /**
- * \brief Export X into unsigned binary data, little endian.
- * Always fills the whole buffer, which will end with zeros
- * if the number is smaller.
- *
- * \param X The source MPI. This must point to an initialized MPI.
- * \param buf The output buffer. This must be a writable buffer of length
- * \p buflen Bytes.
- * \param buflen The size of the output buffer \p buf in Bytes.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
- * large enough to hold the value of \p X.
- * \return Another negative error code on different kinds of failure.
- */
- int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X,
- unsigned char *buf, size_t buflen);
- /**
- * \brief Perform a left-shift on an MPI: X <<= count
- *
- * \param X The MPI to shift. This must point to an initialized MPI.
- * The MPI pointed by \p X may be resized to fit
- * the resulting number.
- * \param count The number of bits to shift by.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return Another negative error code on different kinds of failure.
- */
- int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count);
- /**
- * \brief Perform a right-shift on an MPI: X >>= count
- *
- * \param X The MPI to shift. This must point to an initialized MPI.
- * \param count The number of bits to shift by.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return Another negative error code on different kinds of failure.
- */
- int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count);
- /**
- * \brief Compare the absolute values of two MPIs.
- *
- * \param X The left-hand MPI. This must point to an initialized MPI.
- * \param Y The right-hand MPI. This must point to an initialized MPI.
- *
- * \return \c 1 if `|X|` is greater than `|Y|`.
- * \return \c -1 if `|X|` is lesser than `|Y|`.
- * \return \c 0 if `|X|` is equal to `|Y|`.
- */
- int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y);
- /**
- * \brief Compare two MPIs.
- *
- * \param X The left-hand MPI. This must point to an initialized MPI.
- * \param Y The right-hand MPI. This must point to an initialized MPI.
- *
- * \return \c 1 if \p X is greater than \p Y.
- * \return \c -1 if \p X is lesser than \p Y.
- * \return \c 0 if \p X is equal to \p Y.
- */
- int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y);
- /**
- * \brief Check if an MPI is less than the other in constant time.
- *
- * \param X The left-hand MPI. This must point to an initialized MPI
- * with the same allocated length as Y.
- * \param Y The right-hand MPI. This must point to an initialized MPI
- * with the same allocated length as X.
- * \param ret The result of the comparison:
- * \c 1 if \p X is less than \p Y.
- * \c 0 if \p X is greater than or equal to \p Y.
- *
- * \return 0 on success.
- * \return MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the allocated length of
- * the two input MPIs is not the same.
- */
- int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X, const mbedtls_mpi *Y,
- unsigned *ret);
- /**
- * \brief Compare an MPI with an integer.
- *
- * \param X The left-hand MPI. This must point to an initialized MPI.
- * \param z The integer value to compare \p X to.
- *
- * \return \c 1 if \p X is greater than \p z.
- * \return \c -1 if \p X is lesser than \p z.
- * \return \c 0 if \p X is equal to \p z.
- */
- int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z);
- /**
- * \brief Perform an unsigned addition of MPIs: X = |A| + |B|
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * \param A The first summand. This must point to an initialized MPI.
- * \param B The second summand. This must point to an initialized MPI.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return Another negative error code on different kinds of failure.
- */
- int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A,
- const mbedtls_mpi *B);
- /**
- * \brief Perform an unsigned subtraction of MPIs: X = |A| - |B|
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * \param A The minuend. This must point to an initialized MPI.
- * \param B The subtrahend. This must point to an initialized MPI.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is greater than \p A.
- * \return Another negative error code on different kinds of failure.
- *
- */
- int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A,
- const mbedtls_mpi *B);
- /**
- * \brief Perform a signed addition of MPIs: X = A + B
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * \param A The first summand. This must point to an initialized MPI.
- * \param B The second summand. This must point to an initialized MPI.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return Another negative error code on different kinds of failure.
- */
- int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A,
- const mbedtls_mpi *B);
- /**
- * \brief Perform a signed subtraction of MPIs: X = A - B
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * \param A The minuend. This must point to an initialized MPI.
- * \param B The subtrahend. This must point to an initialized MPI.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return Another negative error code on different kinds of failure.
- */
- int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A,
- const mbedtls_mpi *B);
- /**
- * \brief Perform a signed addition of an MPI and an integer: X = A + b
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * \param A The first summand. This must point to an initialized MPI.
- * \param b The second summand.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return Another negative error code on different kinds of failure.
- */
- int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A,
- mbedtls_mpi_sint b);
- /**
- * \brief Perform a signed subtraction of an MPI and an integer:
- * X = A - b
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * \param A The minuend. This must point to an initialized MPI.
- * \param b The subtrahend.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return Another negative error code on different kinds of failure.
- */
- int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A,
- mbedtls_mpi_sint b);
- /**
- * \brief Perform a multiplication of two MPIs: X = A * B
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * \param A The first factor. This must point to an initialized MPI.
- * \param B The second factor. This must point to an initialized MPI.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return Another negative error code on different kinds of failure.
- *
- */
- int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A,
- const mbedtls_mpi *B);
- /**
- * \brief Perform a multiplication of an MPI with an unsigned integer:
- * X = A * b
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * \param A The first factor. This must point to an initialized MPI.
- * \param b The second factor.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return Another negative error code on different kinds of failure.
- *
- */
- int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A,
- mbedtls_mpi_uint b);
- /**
- * \brief Perform a division with remainder of two MPIs:
- * A = Q * B + R
- *
- * \param Q The destination MPI for the quotient.
- * This may be \c NULL if the value of the
- * quotient is not needed. This must not alias A or B.
- * \param R The destination MPI for the remainder value.
- * This may be \c NULL if the value of the
- * remainder is not needed. This must not alias A or B.
- * \param A The dividend. This must point to an initialized MPI.
- * \param B The divisor. This must point to an initialized MPI.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
- * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero.
- * \return Another negative error code on different kinds of failure.
- */
- int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
- const mbedtls_mpi *B);
- /**
- * \brief Perform a division with remainder of an MPI by an integer:
- * A = Q * b + R
- *
- * \param Q The destination MPI for the quotient.
- * This may be \c NULL if the value of the
- * quotient is not needed. This must not alias A.
- * \param R The destination MPI for the remainder value.
- * This may be \c NULL if the value of the
- * remainder is not needed. This must not alias A.
- * \param A The dividend. This must point to an initialized MPi.
- * \param b The divisor.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
- * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero.
- * \return Another negative error code on different kinds of failure.
- */
- int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
- mbedtls_mpi_sint b);
- /**
- * \brief Perform a modular reduction. R = A mod B
- *
- * \param R The destination MPI for the residue value.
- * This must point to an initialized MPI.
- * \param A The MPI to compute the residue of.
- * This must point to an initialized MPI.
- * \param B The base of the modular reduction.
- * This must point to an initialized MPI.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero.
- * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is negative.
- * \return Another negative error code on different kinds of failure.
- *
- */
- int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A,
- const mbedtls_mpi *B);
- /**
- * \brief Perform a modular reduction with respect to an integer.
- * r = A mod b
- *
- * \param r The address at which to store the residue.
- * This must not be \c NULL.
- * \param A The MPI to compute the residue of.
- * This must point to an initialized MPi.
- * \param b The integer base of the modular reduction.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero.
- * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p b is negative.
- * \return Another negative error code on different kinds of failure.
- */
- int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A,
- mbedtls_mpi_sint b);
- /**
- * \brief Perform a modular exponentiation: X = A^E mod N
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * This must not alias E or N.
- * \param A The base of the exponentiation.
- * This must point to an initialized MPI.
- * \param E The exponent MPI. This must point to an initialized MPI.
- * \param N The base for the modular reduction. This must point to an
- * initialized MPI.
- * \param prec_RR A helper MPI depending solely on \p N which can be used to
- * speed-up multiple modular exponentiations for the same value
- * of \p N. This may be \c NULL. If it is not \c NULL, it must
- * point to an initialized MPI. If it hasn't been used after
- * the call to mbedtls_mpi_init(), this function will compute
- * the helper value and store it in \p prec_RR for reuse on
- * subsequent calls to this function. Otherwise, the function
- * will assume that \p prec_RR holds the helper value set by a
- * previous call to mbedtls_mpi_exp_mod(), and reuse it.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \c N is negative or
- * even, or if \c E is negative.
- * \return Another negative error code on different kinds of failures.
- *
- */
- int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
- const mbedtls_mpi *E, const mbedtls_mpi *N,
- mbedtls_mpi *prec_RR);
- /**
- * \brief Fill an MPI with a number of random bytes.
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * \param size The number of random bytes to generate.
- * \param f_rng The RNG function to use. This must not be \c NULL.
- * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
- * \c NULL if \p f_rng doesn't need a context argument.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return Another negative error code on failure.
- *
- * \note The bytes obtained from the RNG are interpreted
- * as a big-endian representation of an MPI; this can
- * be relevant in applications like deterministic ECDSA.
- */
- int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
- mbedtls_f_rng_t *f_rng,
- void *p_rng);
- /** Generate a random number uniformly in a range.
- *
- * This function generates a random number between \p min inclusive and
- * \p N exclusive.
- *
- * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
- * when the RNG is a suitably parametrized instance of HMAC_DRBG
- * and \p min is \c 1.
- *
- * \note There are `N - min` possible outputs. The lower bound
- * \p min can be reached, but the upper bound \p N cannot.
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * \param min The minimum value to return.
- * It must be nonnegative.
- * \param N The upper bound of the range, exclusive.
- * In other words, this is one plus the maximum value to return.
- * \p N must be strictly larger than \p min.
- * \param f_rng The RNG function to use. This must not be \c NULL.
- * \param p_rng The RNG parameter to be passed to \p f_rng.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p min or \p N is invalid
- * or if they are incompatible.
- * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
- * unable to find a suitable value within a limited number
- * of attempts. This has a negligible probability if \p N
- * is significantly larger than \p min, which is the case
- * for all usual cryptographic applications.
- * \return Another negative error code on failure.
- */
- int mbedtls_mpi_random(mbedtls_mpi *X,
- mbedtls_mpi_sint min,
- const mbedtls_mpi *N,
- mbedtls_f_rng_t *f_rng,
- void *p_rng);
- /**
- * \brief Compute the greatest common divisor: G = gcd(A, B)
- *
- * \param G The destination MPI. This must point to an initialized MPI.
- * This will always be positive or 0.
- * \param A The first operand. This must point to an initialized MPI.
- * \param B The second operand. This must point to an initialized MPI.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return Another negative error code on different kinds of failure.
- */
- int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A,
- const mbedtls_mpi *B);
- /**
- * \brief Compute the modular inverse: X = A^-1 mod N
- *
- * \param X The destination MPI. This must point to an initialized MPI.
- * The value returned on success will be between [1, N-1].
- * \param A The MPI to calculate the modular inverse of. This must point
- * to an initialized MPI. This value can be negative, in which
- * case a positive answer will still be returned in \p X.
- * \param N The base of the modular inversion. This must point to an
- * initialized MPI and be greater than one.
- *
- * \return \c 0 if successful.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p N is less than
- * or equal to one.
- * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p A has no modular
- * inverse with respect to \p N.
- */
- int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
- const mbedtls_mpi *N);
- /**
- * \brief Miller-Rabin primality test.
- *
- * \warning If \p X is potentially generated by an adversary, for example
- * when validating cryptographic parameters that you didn't
- * generate yourself and that are supposed to be prime, then
- * \p rounds should be at least the half of the security
- * strength of the cryptographic algorithm. On the other hand,
- * if \p X is chosen uniformly or non-adversarially (as is the
- * case when mbedtls_mpi_gen_prime calls this function), then
- * \p rounds can be much lower.
- *
- * \param X The MPI to check for primality.
- * This must point to an initialized MPI.
- * \param rounds The number of bases to perform the Miller-Rabin primality
- * test for. The probability of returning 0 on a composite is
- * at most 2<sup>-2*\p rounds </sup>.
- * \param f_rng The RNG function to use. This must not be \c NULL.
- * \param p_rng The RNG parameter to be passed to \p f_rng.
- * This may be \c NULL if \p f_rng doesn't use
- * a context parameter.
- *
- * \return \c 0 if successful, i.e. \p X is probably prime.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p X is not prime.
- * \return Another negative error code on other kinds of failure.
- */
- int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
- mbedtls_f_rng_t *f_rng,
- void *p_rng);
- /**
- * \brief Flags for mbedtls_mpi_gen_prime()
- *
- * Each of these flags is a constraint on the result X returned by
- * mbedtls_mpi_gen_prime().
- */
- typedef enum {
- MBEDTLS_MPI_GEN_PRIME_FLAG_DH = 0x0001, /**< (X-1)/2 is prime too */
- MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR = 0x0002, /**< lower error rate from 2<sup>-80</sup> to 2<sup>-128</sup> */
- } mbedtls_mpi_gen_prime_flag_t;
- /**
- * \brief Generate a prime number.
- *
- * \param X The destination MPI to store the generated prime in.
- * This must point to an initialized MPi.
- * \param nbits The required size of the destination MPI in bits.
- * This must be between \c 3 and #MBEDTLS_MPI_MAX_BITS.
- * \param flags A mask of flags of type #mbedtls_mpi_gen_prime_flag_t.
- * \param f_rng The RNG function to use. This must not be \c NULL.
- * \param p_rng The RNG parameter to be passed to \p f_rng.
- * This may be \c NULL if \p f_rng doesn't use
- * a context parameter.
- *
- * \return \c 0 if successful, in which case \p X holds a
- * probably prime number.
- * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
- * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if `nbits` is not between
- * \c 3 and #MBEDTLS_MPI_MAX_BITS.
- */
- int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
- mbedtls_f_rng_t *f_rng,
- void *p_rng);
- #if defined(MBEDTLS_SELF_TEST)
- /**
- * \brief Checkup routine
- *
- * \return 0 if successful, or 1 if the test failed
- */
- int mbedtls_mpi_self_test(int verbose);
- #endif /* MBEDTLS_SELF_TEST */
- #ifdef __cplusplus
- }
- #endif
- #endif /* bignum.h */
|