24 #ifndef POLARSSL_BIGNUM_H
25 #define POLARSSL_BIGNUM_H
27 #if !defined(POLARSSL_CONFIG_FILE)
30 #include POLARSSL_CONFIG_FILE
35 #if defined(POLARSSL_FS_IO)
39 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
41 #if (_MSC_VER <= 1200)
42 typedef signed short int16_t;
43 typedef unsigned short uint16_t;
45 typedef INT16 int16_t;
46 typedef UINT16 uint16_t;
48 typedef INT32 int32_t;
49 typedef INT64 int64_t;
50 typedef UINT32 uint32_t;
51 typedef UINT64 uint64_t;
56 #define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002
57 #define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004
58 #define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006
59 #define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008
60 #define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A
61 #define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C
62 #define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E
63 #define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010
65 #define MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
70 #define POLARSSL_MPI_MAX_LIMBS 10000
72 #if !defined(POLARSSL_MPI_WINDOW_SIZE)
82 #define POLARSSL_MPI_WINDOW_SIZE 6
85 #if !defined(POLARSSL_MPI_MAX_SIZE)
93 #define POLARSSL_MPI_MAX_SIZE 1024
96 #define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE )
116 #define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
117 #define LN_2_DIV_LN_10_SCALE100 332
118 #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 )
123 #if defined(POLARSSL_HAVE_INT8)
124 typedef signed char t_sint;
125 typedef unsigned char t_uint;
127 #define POLARSSL_HAVE_UDBL
129 #if defined(POLARSSL_HAVE_INT16)
133 #define POLARSSL_HAVE_UDBL
139 #if ( ! defined(POLARSSL_HAVE_INT32) && \
140 defined(_MSC_VER) && defined(_M_AMD64) )
141 #define POLARSSL_HAVE_INT64
145 #if ( ! defined(POLARSSL_HAVE_INT32) && \
146 defined(__GNUC__) && ( \
147 defined(__amd64__) || defined(__x86_64__) || \
148 defined(__ppc64__) || defined(__powerpc64__) || \
149 defined(__ia64__) || defined(__alpha__) || \
150 (defined(__sparc__) && defined(__arch64__)) || \
151 defined(__s390x__) || defined(__mips64) ) )
152 #define POLARSSL_HAVE_INT64
155 typedef unsigned int t_udbl __attribute__((mode(TI)));
156 #define POLARSSL_HAVE_UDBL
158 #define POLARSSL_HAVE_INT32
161 #if ( defined(_MSC_VER) && defined(_M_IX86) )
163 #define POLARSSL_HAVE_UDBL
165 #if defined( POLARSSL_HAVE_LONGLONG )
167 #define POLARSSL_HAVE_UDBL
377 #if defined(POLARSSL_FS_IO)
681 int (*f_rng)(
void *,
unsigned char *,
size_t),
722 int (*f_rng)(
void *,
unsigned char *,
size_t),
740 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_shrink(mpi *X, size_t nblimbs)
Resize down, keeping at least the specified number of limbs.
int mpi_safe_cond_assign(mpi *X, const mpi *Y, unsigned char assign)
Safe conditional assignement X = Y if assign is 1.
int mpi_div_int(mpi *Q, mpi *R, const mpi *A, t_sint b)
Division by int: A = Q * b + R.
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 subtraction: 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 (make internal references valid) This just makes it ready to be set or freed...
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.
asn1_buf val
The named value.
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_safe_cond_swap(mpi *X, mpi *Y, unsigned char assign)
Safe conditional swap X <-> Y if swap is 1.
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 subtraction: 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 subtraction: X = A - b.