00001
00004 #ifndef _BIGNUM_H
00005 #define _BIGNUM_H
00006
00007 #ifdef __cplusplus
00008 extern "C" {
00009 #endif
00010
00011 #include <stdio.h>
00012
00013 #include "ssl_conf.h"
00014
00015 #define ERR_MPI_FILE_IO_ERROR 0x0002
00016 #define ERR_MPI_INVALID_CHARACTER 0x0004
00017 #define ERR_MPI_INVALID_PARAMETER 0x0006
00018 #define ERR_MPI_BUFFER_TOO_SMALL 0x0008
00019 #define ERR_MPI_NEGATIVE_VALUE 0x000A
00020 #define ERR_MPI_DIVISION_BY_ZERO 0x000C
00021 #define ERR_MPI_NOT_ACCEPTABLE 0x000E
00022
00023 #define CHK(fc) if( ( ret = fc ) != 0 ) goto cleanup
00024
00025
00026
00027
00028 #if defined(HAVE_INT16)
00029 typedef unsigned int t_int;
00030 typedef unsigned long t_dbl;
00031 #else
00032 typedef unsigned long t_int;
00033 #if defined(_MSC_VER) && defined(_M_IX86)
00034 typedef unsigned __int64 t_dbl;
00035 #else
00036 #if defined(__amd64__) || defined(__x86_64__) || \
00037 defined(__ppc64__) || defined(__powerpc64__) || \
00038 defined(__ia64__) || defined(__alpha__)
00039 typedef unsigned int t_dbl __attribute__((mode(TI)));
00040 #else
00041 typedef unsigned long long t_dbl;
00042 #endif
00043 #endif
00044 #endif
00045
00046 #define ciL (int) sizeof(t_int)
00047 #define biL (ciL << 3)
00048 #define biH (ciL << 2)
00049
00053 typedef struct
00054 {
00055 int s;
00056 int n;
00057 t_int *p;
00058 }
00059 mpi;
00060
00064 void mpi_init( mpi *X, ... );
00065
00069 void mpi_free( mpi *X, ... );
00070
00077 int mpi_grow( mpi *X, int nblimbs );
00078
00085 int mpi_copy( mpi *X, mpi *Y );
00086
00090 void mpi_swap( mpi *X, mpi *Y );
00091
00098 int mpi_lset( mpi *X, int z );
00099
00109 int mpi_read_string( mpi *X, int radix, char *s );
00110
00124 int mpi_write_string( mpi *X, int radix, char *s, int *slen );
00125
00135 int mpi_read_file( mpi *X, int radix, FILE *fin );
00136
00149 int mpi_write_file( char *p, mpi *X, int radix, FILE *fout );
00150
00161 int mpi_read_binary( mpi *X, unsigned char *buf, int buflen );
00162
00176 int mpi_write_binary( mpi *X, unsigned char *buf, int *buflen );
00177
00181 int mpi_msb( mpi *X );
00182
00186 int mpi_lsb( mpi *X );
00187
00194 int mpi_shift_l( mpi *X, int count );
00195
00202 int mpi_shift_r( mpi *X, int count );
00203
00211 int mpi_cmp_abs( mpi *X, mpi *Y );
00212
00220 int mpi_cmp_mpi( mpi *X, mpi *Y );
00221
00229 int mpi_cmp_int( mpi *X, int z );
00230
00237 int mpi_add_abs( mpi *X, mpi *A, mpi *B );
00238
00245 int mpi_sub_abs( mpi *X, mpi *A, mpi *B );
00246
00253 int mpi_add_mpi( mpi *X, mpi *A, mpi *B );
00254
00261 int mpi_sub_mpi( mpi *X, mpi *A, mpi *B );
00262
00269 int mpi_add_int( mpi *X, mpi *A, int b );
00270
00277 int mpi_sub_int( mpi *X, mpi *A, int b );
00278
00285 int mpi_mul_mpi( mpi *X, mpi *A, mpi *B );
00286
00293 int mpi_mul_int( mpi *X, mpi *A, t_int b );
00294
00304 int mpi_div_mpi( mpi *Q, mpi *R, mpi *A, mpi *B );
00305
00315 int mpi_div_int( mpi *Q, mpi *R, mpi *A, int b );
00316
00324 int mpi_mod_mpi( mpi *R, mpi *A, mpi *B );
00325
00333 int mpi_mod_int( t_int *r, mpi *A, int b );
00334
00346 int mpi_exp_mod( mpi *X, mpi *A, mpi *E, mpi *N, mpi *_RR );
00347
00354 int mpi_gcd( mpi *G, mpi *A, mpi *B );
00355
00364 int mpi_inv_mod( mpi *X, mpi *A, mpi *N );
00365
00373 int mpi_is_prime( mpi *X );
00374
00388 int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
00389 int (*rng_f)(void *), void *rng_d );
00390
00396 int mpi_self_test( int verbose );
00397
00398 #ifdef __cplusplus
00399 }
00400 #endif
00401
00402 #endif