PolarSSL v1.1.4
bignum.h
Go to the documentation of this file.
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 #define POLARSSL_ERR_MPI_FILE_IO_ERROR                     -0x0002  
00036 #define POLARSSL_ERR_MPI_BAD_INPUT_DATA                    -0x0004  
00037 #define POLARSSL_ERR_MPI_INVALID_CHARACTER                 -0x0006  
00038 #define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL                  -0x0008  
00039 #define POLARSSL_ERR_MPI_NEGATIVE_VALUE                    -0x000A  
00040 #define POLARSSL_ERR_MPI_DIVISION_BY_ZERO                  -0x000C  
00041 #define POLARSSL_ERR_MPI_NOT_ACCEPTABLE                    -0x000E  
00042 #define POLARSSL_ERR_MPI_MALLOC_FAILED                     -0x0010  
00044 #define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
00045 
00046 /*
00047  * Maximum size MPIs are allowed to grow to in number of limbs.
00048  */
00049 #define POLARSSL_MPI_MAX_LIMBS                             10000
00050 
00051 /*
00052  * Maximum window size used for modular exponentiation. Default: 6
00053  * Minimum value: 1. Maximum value: 6.
00054  *
00055  * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
00056  * for the sliding window calculation. (So 64 by default)
00057  *
00058  * Reduction in size, reduces speed.
00059  */
00060 #define POLARSSL_MPI_WINDOW_SIZE                           6        
00062 /*
00063  * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
00064  * ( Default: 512 bytes => 4096 bits )
00065  *
00066  * Note: Calculations can results temporarily in larger MPIs. So the number
00067  * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
00068  */
00069 #define POLARSSL_MPI_MAX_SIZE                              512      
00070 #define POLARSSL_MPI_MAX_BITS                              ( 8 * POLARSSL_MPI_MAX_SIZE )    
00072 /*
00073  * When reading from files with mpi_read_file() the buffer should have space
00074  * for a (short) label, the MPI (in the provided radix), the newline
00075  * characters and the '\0'.
00076  *
00077  * By default we assume at least a 10 char label, a minimum radix of 10
00078  * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
00079  */
00080 #define POLARSSL_MPI_READ_BUFFER_SIZE                       1250   
00081 
00082 /*
00083  * Define the base integer type, architecture-wise
00084  */
00085 #if defined(POLARSSL_HAVE_INT8)
00086 typedef   signed char  t_sint;
00087 typedef unsigned char  t_uint;
00088 typedef unsigned short t_udbl;
00089 #else
00090 #if defined(POLARSSL_HAVE_INT16)
00091 typedef   signed short t_sint;
00092 typedef unsigned short t_uint;
00093 typedef unsigned long  t_udbl;
00094 #else
00095   typedef   signed long t_sint;
00096   typedef unsigned long t_uint;
00097   #if defined(_MSC_VER) && defined(_M_IX86)
00098   typedef unsigned __int64 t_udbl;
00099   #else
00100     #if defined(__GNUC__) && (                          \
00101         defined(__amd64__) || defined(__x86_64__)    || \
00102         defined(__ppc64__) || defined(__powerpc64__) || \
00103         defined(__ia64__)  || defined(__alpha__)     || \
00104         (defined(__sparc__) && defined(__arch64__))  || \
00105         defined(__s390x__) )
00106     typedef unsigned int t_udbl __attribute__((mode(TI)));
00107     #define POLARSSL_HAVE_LONGLONG
00108     #else
00109       #if defined(POLARSSL_HAVE_LONGLONG)
00110       typedef unsigned long long t_udbl;
00111       #endif
00112     #endif
00113   #endif
00114 #endif
00115 #endif
00116 
00120 typedef struct
00121 {
00122     int s;              
00123     size_t n;           
00124     t_uint *p;          
00125 }
00126 mpi;
00127 
00128 #ifdef __cplusplus
00129 extern "C" {
00130 #endif
00131 
00137 void mpi_init( mpi *X );
00138 
00144 void mpi_free( mpi *X );
00145 
00155 int mpi_grow( mpi *X, size_t nblimbs );
00156 
00166 int mpi_copy( mpi *X, const mpi *Y );
00167 
00174 void mpi_swap( mpi *X, mpi *Y );
00175 
00185 int mpi_lset( mpi *X, t_sint z );
00186 
00187 /*
00188  * \brief          Get a specific bit from X
00189  *
00190  * \param X        MPI to use
00191  * \param pos      Zero-based index of the bit in X
00192  *
00193  * \return         Either a 0 or a 1
00194  */
00195 int mpi_get_bit( mpi *X, size_t pos );
00196 
00197 /*
00198  * \brief          Set a bit of X to a specific value of 0 or 1
00199  *
00200  * \note           Will grow X if necessary to set a bit to 1 in a not yet
00201  *                 existing limb. Will not grow if bit should be set to 0
00202  *
00203  * \param X        MPI to use
00204  * \param pos      Zero-based index of the bit in X
00205  * \param val      The value to set the bit to (0 or 1)
00206  *
00207  * \return         0 if successful,
00208  *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
00209  *                 POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
00210  */
00211 int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
00212 
00218 size_t mpi_lsb( const mpi *X );
00219 
00225 size_t mpi_msb( const mpi *X );
00226 
00232 size_t mpi_size( const mpi *X );
00233 
00243 int mpi_read_string( mpi *X, int radix, const char *s );
00244 
00260 int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
00261 
00273 int mpi_read_file( mpi *X, int radix, FILE *fin );
00274 
00287 int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
00288 
00299 int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
00300 
00311 int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
00312 
00322 int mpi_shift_l( mpi *X, size_t count );
00323 
00333 int mpi_shift_r( mpi *X, size_t count );
00334 
00345 int mpi_cmp_abs( const mpi *X, const mpi *Y );
00346 
00357 int mpi_cmp_mpi( const mpi *X, const mpi *Y );
00358 
00369 int mpi_cmp_int( const mpi *X, t_sint z );
00370 
00381 int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
00382 
00393 int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
00394 
00405 int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
00406 
00417 int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
00418 
00429 int mpi_add_int( mpi *X, const mpi *A, t_sint b );
00430 
00441 int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
00442 
00453 int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
00454 
00467 int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
00468 
00483 int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
00484 
00499 int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
00500 
00513 int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
00514 
00527 int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
00528 
00546 int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
00547 
00559 int mpi_fill_random( mpi *X, size_t size,
00560                      int (*f_rng)(void *, unsigned char *, size_t),
00561                      void *p_rng );
00562 
00573 int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
00574 
00587 int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
00588 
00600 int mpi_is_prime( mpi *X,
00601                   int (*f_rng)(void *, unsigned char *, size_t),
00602                   void *p_rng );
00603 
00617 int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
00618                    int (*f_rng)(void *, unsigned char *, size_t),
00619                    void *p_rng );
00620 
00626 int mpi_self_test( int verbose );
00627 
00628 #ifdef __cplusplus
00629 }
00630 #endif
00631 
00632 #endif /* bignum.h */