PolarSSL v1.2.5
bignum.h
Go to the documentation of this file.
1 
27 #ifndef POLARSSL_BIGNUM_H
28 #define POLARSSL_BIGNUM_H
29 
30 #include <stdio.h>
31 #include <string.h>
32 
33 #include "config.h"
34 
35 #ifdef _MSC_VER
36 #include <basetsd.h>
37 #if (_MSC_VER <= 1200)
38 typedef signed short int16_t;
39 typedef unsigned short uint16_t;
40 #else
41 typedef INT16 int16_t;
42 typedef UINT16 uint16_t;
43 #endif
44 typedef INT32 int32_t;
45 typedef UINT32 uint32_t;
46 typedef UINT64 uint64_t;
47 #else
48 #include <inttypes.h>
49 #endif
50 
51 #define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002
52 #define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004
53 #define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006
54 #define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008
55 #define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A
56 #define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C
57 #define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E
58 #define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010
60 #define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
61 
62 /*
63  * Maximum size MPIs are allowed to grow to in number of limbs.
64  */
65 #define POLARSSL_MPI_MAX_LIMBS 10000
66 
67 /*
68  * Maximum window size used for modular exponentiation. Default: 6
69  * Minimum value: 1. Maximum value: 6.
70  *
71  * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
72  * for the sliding window calculation. (So 64 by default)
73  *
74  * Reduction in size, reduces speed.
75  */
76 #define POLARSSL_MPI_WINDOW_SIZE 6
78 /*
79  * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
80  * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
81  *
82  * Note: Calculations can results temporarily in larger MPIs. So the number
83  * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
84  */
85 #define POLARSSL_MPI_MAX_SIZE 512
86 #define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE )
88 /*
89  * When reading from files with mpi_read_file() and writing to files with
90  * mpi_write_file() the buffer should have space
91  * for a (short) label, the MPI (in the provided radix), the newline
92  * characters and the '\0'.
93  *
94  * By default we assume at least a 10 char label, a minimum radix of 10
95  * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
96  * Autosized at compile time for at least a 10 char label, a minimum radix
97  * of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size.
98  *
99  * This used to be statically sized to 1250 for a maximum of 4096 bit
100  * numbers (1234 decimal chars).
101  *
102  * Calculate using the formula:
103  * POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) +
104  * LabelSize + 6
105  */
106 #define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
107 #define LN_2_DIV_LN_10_SCALE100 332
108 #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 )
109 
110 /*
111  * Define the base integer type, architecture-wise
112  */
113 #if defined(POLARSSL_HAVE_INT8)
114 typedef signed char t_sint;
115 typedef unsigned char t_uint;
116 typedef uint16_t t_udbl;
117 #define POLARSSL_HAVE_UDBL
118 #else
119 #if defined(POLARSSL_HAVE_INT16)
120 typedef int16_t t_sint;
121 typedef uint16_t t_uint;
122 typedef uint32_t t_udbl;
123 #define POLARSSL_HAVE_UDBL
124 #else
125  #if ( defined(__MSC_VER) && defined(_M_AMD64) )
126  typedef int64_t t_sint;
127  typedef uint64_t t_uint;
128  #else
129  #if ( defined(__GNUC__) && ( \
130  defined(__amd64__) || defined(__x86_64__) || \
131  defined(__ppc64__) || defined(__powerpc64__) || \
132  defined(__ia64__) || defined(__alpha__) || \
133  (defined(__sparc__) && defined(__arch64__)) || \
134  defined(__s390x__) ) )
135  typedef int64_t t_sint;
136  typedef uint64_t t_uint;
137  typedef unsigned int t_udbl __attribute__((mode(TI)));
138  #define POLARSSL_HAVE_UDBL
139  #else
140  typedef int32_t t_sint;
141  typedef uint32_t t_uint;
142  #if ( defined(_MSC_VER) && defined(_M_IX86) )
143  typedef uint64_t t_udbl;
144  #define POLARSSL_HAVE_UDBL
145  #else
146  #if defined( POLARSSL_HAVE_LONGLONG )
147  typedef unsigned long long t_udbl;
148  #define POLARSSL_HAVE_UDBL
149  #endif
150  #endif
151  #endif
152  #endif
153 #endif /* POLARSSL_HAVE_INT16 */
154 #endif /* POLARSSL_HAVE_INT8 */
155 
159 typedef struct
160 {
161  int s;
162  size_t n;
163  t_uint *p;
164 }
165 mpi;
166 
167 #ifdef __cplusplus
168 extern "C" {
169 #endif
170 
176 void mpi_init( mpi *X );
177 
183 void mpi_free( mpi *X );
184 
194 int mpi_grow( mpi *X, size_t nblimbs );
195 
205 int mpi_copy( mpi *X, const mpi *Y );
206 
213 void mpi_swap( mpi *X, mpi *Y );
214 
224 int mpi_lset( mpi *X, t_sint z );
225 
234 int mpi_get_bit( const mpi *X, size_t pos );
235 
250 int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
251 
260 size_t mpi_lsb( const mpi *X );
261 
270 size_t mpi_msb( const mpi *X );
271 
277 size_t mpi_size( const mpi *X );
278 
288 int mpi_read_string( mpi *X, int radix, const char *s );
289 
305 int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
306 
307 #if defined(POLARSSL_FS_IO)
308 
319 int mpi_read_file( mpi *X, int radix, FILE *fin );
320 
333 int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
334 #endif /* POLARSSL_FS_IO */
335 
346 int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
347 
358 int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
359 
369 int mpi_shift_l( mpi *X, size_t count );
370 
380 int mpi_shift_r( mpi *X, size_t count );
381 
392 int mpi_cmp_abs( const mpi *X, const mpi *Y );
393 
404 int mpi_cmp_mpi( const mpi *X, const mpi *Y );
405 
416 int mpi_cmp_int( const mpi *X, t_sint z );
417 
428 int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
429 
440 int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
441 
452 int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
453 
464 int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
465 
476 int mpi_add_int( mpi *X, const mpi *A, t_sint b );
477 
488 int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
489 
500 int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
501 
514 int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
515 
530 int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
531 
546 int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
547 
560 int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
561 
574 int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
575 
594 int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
595 
607 int mpi_fill_random( mpi *X, size_t size,
608  int (*f_rng)(void *, unsigned char *, size_t),
609  void *p_rng );
610 
621 int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
622 
635 int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
636 
648 int mpi_is_prime( mpi *X,
649  int (*f_rng)(void *, unsigned char *, size_t),
650  void *p_rng );
651 
665 int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
666  int (*f_rng)(void *, unsigned char *, size_t),
667  void *p_rng );
668 
674 int mpi_self_test( int verbose );
675 
676 #ifdef __cplusplus
677 }
678 #endif
679 
680 #endif /* bignum.h */