00001
00027 #ifndef POLARSSL_BIGNUM_H
00028 #define POLARSSL_BIGNUM_H
00029
00030 #include <stdio.h>
00031 #include <string.h>
00032
00033 #include "config.h"
00034
00035 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
00036 #include <basetsd.h>
00037 #if (_MSC_VER <= 1200)
00038 typedef signed short int16_t;
00039 typedef unsigned short uint16_t;
00040 #else
00041 typedef INT16 int16_t;
00042 typedef UINT16 uint16_t;
00043 #endif
00044 typedef INT32 int32_t;
00045 typedef INT64 int64_t;
00046 typedef UINT32 uint32_t;
00047 typedef UINT64 uint64_t;
00048 #else
00049 #include <inttypes.h>
00050 #endif
00051
00052 #define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002
00053 #define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004
00054 #define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006
00055 #define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008
00056 #define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A
00057 #define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C
00058 #define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E
00059 #define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010
00061 #define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
00062
00063
00064
00065
00066 #define POLARSSL_MPI_MAX_LIMBS 10000
00067
00068 #if !defined(POLARSSL_CONFIG_OPTIONS)
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 #define POLARSSL_MPI_WINDOW_SIZE 6
00080
00081
00082
00083
00084
00085
00086
00087 #define POLARSSL_MPI_MAX_SIZE 512
00089 #endif
00090
00091 #define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE )
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 #define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
00112 #define LN_2_DIV_LN_10_SCALE100 332
00113 #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 )
00114
00115
00116
00117
00118 #if defined(POLARSSL_HAVE_INT8)
00119 typedef signed char t_sint;
00120 typedef unsigned char t_uint;
00121 typedef uint16_t t_udbl;
00122 #define POLARSSL_HAVE_UDBL
00123 #else
00124 #if defined(POLARSSL_HAVE_INT16)
00125 typedef int16_t t_sint;
00126 typedef uint16_t t_uint;
00127 typedef uint32_t t_udbl;
00128 #define POLARSSL_HAVE_UDBL
00129 #else
00130 #if ( defined(_MSC_VER) && defined(_M_AMD64) )
00131 #define POLARSSL_HAVE_INT64
00132 typedef int64_t t_sint;
00133 typedef uint64_t t_uint;
00134 #else
00135 #if ( defined(__GNUC__) && ( \
00136 defined(__amd64__) || defined(__x86_64__) || \
00137 defined(__ppc64__) || defined(__powerpc64__) || \
00138 defined(__ia64__) || defined(__alpha__) || \
00139 (defined(__sparc__) && defined(__arch64__)) || \
00140 defined(__s390x__) ) )
00141 #define POLARSSL_HAVE_INT64
00142 typedef int64_t t_sint;
00143 typedef uint64_t t_uint;
00144 typedef unsigned int t_udbl __attribute__((mode(TI)));
00145 #define POLARSSL_HAVE_UDBL
00146 #else
00147 #define POLARSSL_HAVE_INT32
00148 typedef int32_t t_sint;
00149 typedef uint32_t t_uint;
00150 #if ( defined(_MSC_VER) && defined(_M_IX86) )
00151 typedef uint64_t t_udbl;
00152 #define POLARSSL_HAVE_UDBL
00153 #else
00154 #if defined( POLARSSL_HAVE_LONGLONG )
00155 typedef unsigned long long t_udbl;
00156 #define POLARSSL_HAVE_UDBL
00157 #endif
00158 #endif
00159 #endif
00160 #endif
00161 #endif
00162 #endif
00163
00164 #ifdef __cplusplus
00165 extern "C" {
00166 #endif
00167
00171 typedef struct
00172 {
00173 int s;
00174 size_t n;
00175 t_uint *p;
00176 }
00177 mpi;
00178
00184 void mpi_init( mpi *X );
00185
00191 void mpi_free( mpi *X );
00192
00202 int mpi_grow( mpi *X, size_t nblimbs );
00203
00213 int mpi_copy( mpi *X, const mpi *Y );
00214
00221 void mpi_swap( mpi *X, mpi *Y );
00222
00232 int mpi_lset( mpi *X, t_sint z );
00233
00242 int mpi_get_bit( const mpi *X, size_t pos );
00243
00258 int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
00259
00268 size_t mpi_lsb( const mpi *X );
00269
00278 size_t mpi_msb( const mpi *X );
00279
00285 size_t mpi_size( const mpi *X );
00286
00296 int mpi_read_string( mpi *X, int radix, const char *s );
00297
00313 int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
00314
00315 #if defined(POLARSSL_FS_IO)
00316
00327 int mpi_read_file( mpi *X, int radix, FILE *fin );
00328
00341 int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
00342 #endif
00343
00354 int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
00355
00366 int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
00367
00377 int mpi_shift_l( mpi *X, size_t count );
00378
00388 int mpi_shift_r( mpi *X, size_t count );
00389
00400 int mpi_cmp_abs( const mpi *X, const mpi *Y );
00401
00412 int mpi_cmp_mpi( const mpi *X, const mpi *Y );
00413
00424 int mpi_cmp_int( const mpi *X, t_sint z );
00425
00436 int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
00437
00448 int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
00449
00460 int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
00461
00472 int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
00473
00484 int mpi_add_int( mpi *X, const mpi *A, t_sint b );
00485
00496 int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
00497
00508 int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
00509
00522 int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
00523
00538 int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
00539
00554 int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
00555
00568 int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
00569
00582 int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
00583
00602 int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
00603
00615 int mpi_fill_random( mpi *X, size_t size,
00616 int (*f_rng)(void *, unsigned char *, size_t),
00617 void *p_rng );
00618
00629 int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
00630
00643 int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
00644
00656 int mpi_is_prime( mpi *X,
00657 int (*f_rng)(void *, unsigned char *, size_t),
00658 void *p_rng );
00659
00673 int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
00674 int (*f_rng)(void *, unsigned char *, size_t),
00675 void *p_rng );
00676
00682 int mpi_self_test( int verbose );
00683
00684 #ifdef __cplusplus
00685 }
00686 #endif
00687
00688 #endif