PolarSSL
polarssl/bignum.h
Go to the documentation of this file.
00001 
00025 #ifndef POLARSSL_BIGNUM_H
00026 #define POLARSSL_BIGNUM_H
00027 
00028 #include <stdio.h>
00029 
00030 #define POLARSSL_ERR_MPI_FILE_IO_ERROR                     0x0002
00031 #define POLARSSL_ERR_MPI_BAD_INPUT_DATA                    0x0004
00032 #define POLARSSL_ERR_MPI_INVALID_CHARACTER                 0x0006
00033 #define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL                  0x0008
00034 #define POLARSSL_ERR_MPI_NEGATIVE_VALUE                    0x000A
00035 #define POLARSSL_ERR_MPI_DIVISION_BY_ZERO                  0x000C
00036 #define POLARSSL_ERR_MPI_NOT_ACCEPTABLE                    0x000E
00037 
00038 #define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
00039 
00040 /*
00041  * Define the base integer type, architecture-wise
00042  */
00043 #if defined(POLARSSL_HAVE_INT8)
00044 typedef unsigned char  t_int;
00045 typedef unsigned short t_dbl;
00046 #else
00047 #if defined(POLARSSL_HAVE_INT16)
00048 typedef unsigned short t_int;
00049 typedef unsigned long  t_dbl;
00050 #else
00051   typedef unsigned long t_int;
00052   #if defined(_MSC_VER) && defined(_M_IX86)
00053   typedef unsigned __int64 t_dbl;
00054   #else
00055     #if defined(__amd64__) || defined(__x86_64__)    || \
00056         defined(__ppc64__) || defined(__powerpc64__) || \
00057         defined(__ia64__)  || defined(__alpha__)
00058     typedef unsigned int t_dbl __attribute__((mode(TI)));
00059     #else
00060       #if defined(POLARSSL_HAVE_LONGLONG)
00061       typedef unsigned long long t_dbl;
00062       #endif
00063     #endif
00064   #endif
00065 #endif
00066 #endif
00067 
00071 typedef struct
00072 {
00073     int s;              
00074     int n;              
00075     t_int *p;           
00076 }
00077 mpi;
00078 
00079 #ifdef __cplusplus
00080 extern "C" {
00081 #endif
00082 
00086 void mpi_init( mpi *X, ... );
00087 
00091 void mpi_free( mpi *X, ... );
00092 
00102 int mpi_grow( mpi *X, int nblimbs );
00103 
00113 int mpi_copy( mpi *X, const mpi *Y );
00114 
00121 void mpi_swap( mpi *X, mpi *Y );
00122 
00132 int mpi_lset( mpi *X, int z );
00133 
00139 int mpi_lsb( const mpi *X );
00140 
00146 int mpi_msb( const mpi *X );
00147 
00153 int mpi_size( const mpi *X );
00154 
00164 int mpi_read_string( mpi *X, int radix, const char *s );
00165 
00181 int mpi_write_string( const mpi *X, int radix, char *s, int *slen );
00182 
00192 int mpi_read_file( mpi *X, int radix, FILE *fin );
00193 
00206 int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
00207 
00218 int mpi_read_binary( mpi *X, const unsigned char *buf, int buflen );
00219 
00230 int mpi_write_binary( const mpi *X, unsigned char *buf, int buflen );
00231 
00241 int mpi_shift_l( mpi *X, int count );
00242 
00252 int mpi_shift_r( mpi *X, int count );
00253 
00264 int mpi_cmp_abs( const mpi *X, const mpi *Y );
00265 
00276 int mpi_cmp_mpi( const mpi *X, const mpi *Y );
00277 
00288 int mpi_cmp_int( const mpi *X, int z );
00289 
00300 int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
00301 
00312 int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
00313 
00324 int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
00325 
00336 int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
00337 
00348 int mpi_add_int( mpi *X, const mpi *A, int b );
00349 
00360 int mpi_sub_int( mpi *X, const mpi *A, int b );
00361 
00372 int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
00373 
00386 int mpi_mul_int( mpi *X, const mpi *A, t_int b );
00387 
00402 int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
00403 
00418 int mpi_div_int( mpi *Q, mpi *R, const mpi *A, int b );
00419 
00432 int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
00433 
00446 int mpi_mod_int( t_int *r, const mpi *A, int b );
00447 
00465 int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
00466 
00477 int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
00478 
00491 int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
00492 
00504 int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
00505 
00519 int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
00520                    int (*f_rng)(void *), void *p_rng );
00521 
00527 int mpi_self_test( int verbose );
00528 
00529 #ifdef __cplusplus
00530 }
00531 #endif
00532 
00533 #endif /* bignum.h */
 All Classes Files Functions Variables Defines