27 #ifndef POLARSSL_BIGNUM_H
28 #define POLARSSL_BIGNUM_H
37 #if (_MSC_VER <= 1200)
38 typedef signed short int16_t;
39 typedef unsigned short uint16_t;
41 typedef INT16 int16_t;
42 typedef UINT16 uint16_t;
44 typedef INT32 int32_t;
45 typedef INT64 int64_t;
46 typedef UINT32 uint32_t;
47 typedef UINT64 uint64_t;
52 #define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002
53 #define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004
54 #define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006
55 #define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008
56 #define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A
57 #define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C
58 #define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E
59 #define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010
61 #define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
66 #define POLARSSL_MPI_MAX_LIMBS 10000
68 #if !defined(POLARSSL_CONFIG_OPTIONS)
78 #define POLARSSL_MPI_WINDOW_SIZE 6
87 #define POLARSSL_MPI_MAX_SIZE 512
91 #define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE )
111 #define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
112 #define LN_2_DIV_LN_10_SCALE100 332
113 #define POLARSSL_MPI_RW_BUFFER_SIZE ( ((POLARSSL_MPI_MAX_BITS_SCALE100 + LN_2_DIV_LN_10_SCALE100 - 1) / LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
118 #if defined(POLARSSL_HAVE_INT8)
119 typedef signed char t_sint;
120 typedef unsigned char t_uint;
122 #define POLARSSL_HAVE_UDBL
124 #if defined(POLARSSL_HAVE_INT16)
128 #define POLARSSL_HAVE_UDBL
130 #if ( defined(_MSC_VER) && defined(_M_AMD64) )
134 #if ( defined(__GNUC__) && ( \
135 defined(__amd64__) || defined(__x86_64__) || \
136 defined(__ppc64__) || defined(__powerpc64__) || \
137 defined(__ia64__) || defined(__alpha__) || \
138 (defined(__sparc__) && defined(__arch64__)) || \
139 defined(__s390x__) ) )
142 typedef unsigned int t_udbl __attribute__((mode(TI)));
143 #define POLARSSL_HAVE_UDBL
147 #if ( defined(_MSC_VER) && defined(_M_IX86) )
149 #define POLARSSL_HAVE_UDBL
151 #if defined( POLARSSL_HAVE_LONGLONG )
153 #define POLARSSL_HAVE_UDBL
312 #if defined(POLARSSL_FS_IO)
616 int (*f_rng)(
void *,
unsigned char *,
size_t),
657 int (*f_rng)(
void *,
unsigned char *,
size_t),
674 int (*f_rng)(
void *,
unsigned char *,
size_t),
int mpi_cmp_int(const mpi *X, t_sint z)
Compare signed values.
void mpi_swap(mpi *X, mpi *Y)
Swap the contents of X and Y.
int mpi_div_int(mpi *Q, mpi *R, const mpi *A, t_sint b)
Division by int: A = Q * b + R.
asn1_buf buf
Buffer containing the given ASN.1 item.
int mpi_gcd(mpi *G, const mpi *A, const mpi *B)
Greatest common divisor: G = gcd(A, B)
int mpi_fill_random(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 mpi_sub_abs(mpi *X, const mpi *A, const mpi *B)
Unsigned substraction: X = |A| - |B|.
int mpi_cmp_abs(const mpi *X, const mpi *Y)
Compare unsigned values.
Configuration options (set of defines)
int mpi_add_int(mpi *X, const mpi *A, t_sint b)
Signed addition: X = A + b.
int mpi_read_file(mpi *X, int radix, FILE *fin)
Read X from an opened file.
int mpi_div_mpi(mpi *Q, mpi *R, const mpi *A, const mpi *B)
Division by mpi: A = Q * B + R.
int mpi_lset(mpi *X, t_sint z)
Set value from integer.
int mpi_is_prime(mpi *X, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Miller-Rabin primality test.
int mpi_write_file(const char *p, const mpi *X, int radix, FILE *fout)
Write X into an opened file, or stdout if fout is NULL.
void mpi_init(mpi *X)
Initialize one MPI.
int mpi_cmp_mpi(const mpi *X, const mpi *Y)
Compare signed values.
unsigned long long t_udbl
int mpi_shift_r(mpi *X, size_t count)
Right-shift: X >>= count.
int mpi_add_mpi(mpi *X, const mpi *A, const mpi *B)
Signed addition: X = A + B.
int mpi_write_string(const mpi *X, int radix, char *s, size_t *slen)
Export into an ASCII string.
size_t mpi_lsb(const mpi *X)
Return the number of zero-bits before the least significant '1' bit.
int mpi_inv_mod(mpi *X, const mpi *A, const mpi *N)
Modular inverse: X = A^-1 mod N.
void mpi_free(mpi *X)
Unallocate one MPI.
int mpi_mul_int(mpi *X, const mpi *A, t_sint b)
Baseline multiplication: X = A * b Note: despite the functon signature, b is treated as a t_uint...
int mpi_grow(mpi *X, size_t nblimbs)
Enlarge to the specified number of limbs.
int mpi_mod_int(t_uint *r, const mpi *A, t_sint b)
Modulo: r = A mod b.
int mpi_exp_mod(mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR)
Sliding-window exponentiation: X = A^E mod N.
int mpi_gen_prime(mpi *X, size_t nbits, int dh_flag, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Prime number generation.
size_t mpi_msb(const mpi *X)
Return the number of bits up to and including the most significant '1' bit'.
int mpi_add_abs(mpi *X, const mpi *A, const mpi *B)
Unsigned addition: X = |A| + |B|.
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
int mpi_read_binary(mpi *X, const unsigned char *buf, size_t buflen)
Import X from unsigned binary data, big endian.
int mpi_self_test(int verbose)
Checkup routine.
size_t mpi_size(const mpi *X)
Return the total size in bytes.
int mpi_copy(mpi *X, const mpi *Y)
Copy the contents of Y into X.
int mpi_mod_mpi(mpi *R, const mpi *A, const mpi *B)
Modulo: R = A mod B.
int mpi_get_bit(const mpi *X, size_t pos)
Get a specific bit from X.
int mpi_write_binary(const mpi *X, unsigned char *buf, size_t buflen)
Export X into unsigned binary data, big endian.
int mpi_shift_l(mpi *X, size_t count)
Left-shift: X <<= count.
int mpi_mul_mpi(mpi *X, const mpi *A, const mpi *B)
Baseline multiplication: X = A * B.
int mpi_sub_mpi(mpi *X, const mpi *A, const mpi *B)
Signed substraction: X = A - B.
int mpi_set_bit(mpi *X, size_t pos, unsigned char val)
Set a bit of X to a specific value of 0 or 1.
int mpi_sub_int(mpi *X, const mpi *A, t_sint b)
Signed substraction: X = A - b.