23 #ifndef MBEDTLS_BIGNUM_H
24 #define MBEDTLS_BIGNUM_H
26 #if !defined(MBEDTLS_CONFIG_FILE)
29 #include MBEDTLS_CONFIG_FILE
35 #if defined(MBEDTLS_FS_IO)
39 #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
40 #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
41 #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
42 #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
43 #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
44 #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
45 #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
46 #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
48 #define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
53 #define MBEDTLS_MPI_MAX_LIMBS 10000
55 #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
65 #define MBEDTLS_MPI_WINDOW_SIZE 6
68 #if !defined(MBEDTLS_MPI_MAX_SIZE)
76 #define MBEDTLS_MPI_MAX_SIZE 1024
79 #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE )
99 #define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
100 #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
101 #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 )
109 #if ( ! defined(MBEDTLS_HAVE_INT32) && \
110 defined(_MSC_VER) && defined(_M_AMD64) )
111 #define MBEDTLS_HAVE_INT64
115 #if ( ! defined(MBEDTLS_HAVE_INT32) && \
116 defined(__GNUC__) && ( \
117 defined(__amd64__) || defined(__x86_64__) || \
118 defined(__ppc64__) || defined(__powerpc64__) || \
119 defined(__ia64__) || defined(__alpha__) || \
120 (defined(__sparc__) && defined(__arch64__)) || \
121 defined(__s390x__) || defined(__mips64) ) )
122 #define MBEDTLS_HAVE_INT64
126 #define MBEDTLS_HAVE_UDBL
128 #define MBEDTLS_HAVE_INT32
132 #define MBEDTLS_HAVE_UDBL
338 char *buf,
size_t buflen,
size_t *olen );
340 #if defined(MBEDTLS_FS_IO)
352 int mbedtls_mpi_read_file(
mbedtls_mpi *X,
int radix, FILE *fin );
366 int mbedtls_mpi_write_file(
const char *p,
const mbedtls_mpi *X,
int radix, FILE *fout );
643 int (*f_rng)(
void *,
unsigned char *,
size_t),
684 int (*f_rng)(
void *,
unsigned char *,
size_t),
702 int (*f_rng)(
void *,
unsigned char *,
size_t),
int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Division by mbedtls_mpi: A = Q * B + R.
void mbedtls_mpi_free(mbedtls_mpi *X)
Unallocate one MPI.
int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
Greatest common divisor: G = gcd(A, B)
int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Fill an MPI X with size bytes of random.
int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Signed subtraction: X = A - b.
size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
Return the number of zero-bits before the least significant '1' bit.
int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
Enlarge to the specified number of limbs.
int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign)
Safe conditional assignement X = Y if assign is 1.
int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Modulo: R = A mod B.
int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR)
Sliding-window exponentiation: X = A^E mod N.
size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
Return the number of bits up to and including the most significant '1' bit'.
int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Compare signed values.
size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Return the total size in bytes.
int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Signed subtraction: X = A - B.
int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Unsigned subtraction: X = |A| - |B|.
Compatibility names (set of defines)
int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
Import from an ASCII string.
int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
Set a bit of X to a specific value of 0 or 1.
int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
Set value from integer.
int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Division by int: A = Q * b + R.
int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Import X from unsigned binary data, big endian.
int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
Resize down, keeping at least the specified number of limbs.
int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Signed addition: X = A + b.
int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
Right-shift: X >>= count.
int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int dh_flag, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Prime number generation.
int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Modulo: r = A mod b.
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Left-shift: X <<= count.
mbedtls_asn1_buf val
The named value.
int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
Baseline multiplication: X = A * b.
int mbedtls_mpi_is_prime(const mbedtls_mpi *X, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Miller-Rabin primality test.
uint32_t mbedtls_mpi_uint
int mbedtls_mpi_self_test(int verbose)
Checkup routine.
int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign)
Safe conditional swap X <-> Y if swap is 1.
void mbedtls_mpi_init(mbedtls_mpi *X)
Initialize one MPI (make internal references valid) This just makes it ready to be set or freed...
void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
Swap the contents of X and Y.
int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Signed addition: X = A + B.
int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Modular inverse: X = A^-1 mod N.
int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Copy the contents of Y into X.
int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix, char *buf, size_t buflen, size_t *olen)
Export into an ASCII string.
int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Unsigned addition: X = |A| + |B|.
int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Compare unsigned values.
int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t buflen)
Export X into unsigned binary data, big endian.
int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Baseline multiplication: X = A * B.
int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
Compare signed values.
int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
Get a specific bit from X.