PolarSSL v1.3.2
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 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
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 INT64 int64_t;
46 typedef UINT32 uint32_t;
47 typedef UINT64 uint64_t;
48 #else
49 #include <inttypes.h>
50 #endif
51 
52 #define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002
53 #define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004
54 #define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006
55 #define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008
56 #define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A
57 #define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C
58 #define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E
59 #define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010
61 #define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
62 
63 /*
64  * Maximum size MPIs are allowed to grow to in number of limbs.
65  */
66 #define POLARSSL_MPI_MAX_LIMBS 10000
67 
68 #if !defined(POLARSSL_CONFIG_OPTIONS)
69 /*
70  * Maximum window size used for modular exponentiation. Default: 6
71  * Minimum value: 1. Maximum value: 6.
72  *
73  * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
74  * for the sliding window calculation. (So 64 by default)
75  *
76  * Reduction in size, reduces speed.
77  */
78 #define POLARSSL_MPI_WINDOW_SIZE 6
80 /*
81  * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
82  * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
83  *
84  * Note: Calculations can results temporarily in larger MPIs. So the number
85  * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
86  */
87 #define POLARSSL_MPI_MAX_SIZE 512
89 #endif /* !POLARSSL_CONFIG_OPTIONS */
90 
91 #define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE )
93 /*
94  * When reading from files with mpi_read_file() and writing to files with
95  * mpi_write_file() the buffer should have space
96  * for a (short) label, the MPI (in the provided radix), the newline
97  * characters and the '\0'.
98  *
99  * By default we assume at least a 10 char label, a minimum radix of 10
100  * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
101  * Autosized at compile time for at least a 10 char label, a minimum radix
102  * of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size.
103  *
104  * This used to be statically sized to 1250 for a maximum of 4096 bit
105  * numbers (1234 decimal chars).
106  *
107  * Calculate using the formula:
108  * POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) +
109  * LabelSize + 6
110  */
111 #define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
112 #define LN_2_DIV_LN_10_SCALE100 332
113 #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 )
114 
115 /*
116  * Define the base integer type, architecture-wise
117  */
118 #if defined(POLARSSL_HAVE_INT8)
119 typedef signed char t_sint;
120 typedef unsigned char t_uint;
121 typedef uint16_t t_udbl;
122 #define POLARSSL_HAVE_UDBL
123 #else
124 #if defined(POLARSSL_HAVE_INT16)
125 typedef int16_t t_sint;
126 typedef uint16_t t_uint;
127 typedef uint32_t t_udbl;
128 #define POLARSSL_HAVE_UDBL
129 #else
130  #if ( defined(_MSC_VER) && defined(_M_AMD64) )
131  #define POLARSSL_HAVE_INT64
132  typedef int64_t t_sint;
133  typedef uint64_t t_uint;
134  #else
135  #if ( defined(__GNUC__) && ( \
136  defined(__amd64__) || defined(__x86_64__) || \
137  defined(__ppc64__) || defined(__powerpc64__) || \
138  defined(__ia64__) || defined(__alpha__) || \
139  (defined(__sparc__) && defined(__arch64__)) || \
140  defined(__s390x__) ) )
141  #define POLARSSL_HAVE_INT64
142  typedef int64_t t_sint;
143  typedef uint64_t t_uint;
144  typedef unsigned int t_udbl __attribute__((mode(TI)));
145  #define POLARSSL_HAVE_UDBL
146  #else
147  #define POLARSSL_HAVE_INT32
148  typedef int32_t t_sint;
149  typedef uint32_t t_uint;
150  #if ( defined(_MSC_VER) && defined(_M_IX86) )
151  typedef uint64_t t_udbl;
152  #define POLARSSL_HAVE_UDBL
153  #else
154  #if defined( POLARSSL_HAVE_LONGLONG )
155  typedef unsigned long long t_udbl;
156  #define POLARSSL_HAVE_UDBL
157  #endif
158  #endif
159  #endif
160  #endif
161 #endif /* POLARSSL_HAVE_INT16 */
162 #endif /* POLARSSL_HAVE_INT8 */
163 
164 #ifdef __cplusplus
165 extern "C" {
166 #endif
167 
171 typedef struct
172 {
173  int s;
174  size_t n;
175  t_uint *p;
176 }
177 mpi;
178 
184 void mpi_init( mpi *X );
185 
191 void mpi_free( mpi *X );
192 
202 int mpi_grow( mpi *X, size_t nblimbs );
203 
213 int mpi_copy( mpi *X, const mpi *Y );
214 
221 void mpi_swap( mpi *X, mpi *Y );
222 
232 int mpi_lset( mpi *X, t_sint z );
233 
242 int mpi_get_bit( const mpi *X, size_t pos );
243 
258 int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
259 
268 size_t mpi_lsb( const mpi *X );
269 
278 size_t mpi_msb( const mpi *X );
279 
285 size_t mpi_size( const mpi *X );
286 
296 int mpi_read_string( mpi *X, int radix, const char *s );
297 
313 int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
314 
315 #if defined(POLARSSL_FS_IO)
316 
327 int mpi_read_file( mpi *X, int radix, FILE *fin );
328 
341 int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
342 #endif /* POLARSSL_FS_IO */
343 
354 int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
355 
366 int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
367 
377 int mpi_shift_l( mpi *X, size_t count );
378 
388 int mpi_shift_r( mpi *X, size_t count );
389 
400 int mpi_cmp_abs( const mpi *X, const mpi *Y );
401 
412 int mpi_cmp_mpi( const mpi *X, const mpi *Y );
413 
424 int mpi_cmp_int( const mpi *X, t_sint z );
425 
436 int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
437 
448 int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
449 
460 int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
461 
472 int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
473 
484 int mpi_add_int( mpi *X, const mpi *A, t_sint b );
485 
496 int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
497 
508 int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
509 
522 int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
523 
538 int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
539 
554 int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
555 
568 int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
569 
582 int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
583 
602 int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
603 
615 int mpi_fill_random( mpi *X, size_t size,
616  int (*f_rng)(void *, unsigned char *, size_t),
617  void *p_rng );
618 
629 int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
630 
643 int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
644 
656 int mpi_is_prime( mpi *X,
657  int (*f_rng)(void *, unsigned char *, size_t),
658  void *p_rng );
659 
673 int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
674  int (*f_rng)(void *, unsigned char *, size_t),
675  void *p_rng );
676 
682 int mpi_self_test( int verbose );
683 
684 #ifdef __cplusplus
685 }
686 #endif
687 
688 #endif /* bignum.h */
int mpi_cmp_int(const mpi *X, t_sint z)
Compare signed values.
void mpi_swap(mpi *X, mpi *Y)
Swap the contents of X and Y.
uint32_t t_uint
Definition: bignum.h:149
int mpi_div_int(mpi *Q, mpi *R, const mpi *A, t_sint b)
Division by int: A = Q * b + R.
int mpi_gcd(mpi *G, const mpi *A, const mpi *B)
Greatest common divisor: G = gcd(A, B)
int s
Definition: bignum.h:173
int mpi_fill_random(mpi *X, size_t size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Fill an MPI X with size bytes of random.
int mpi_sub_abs(mpi *X, const mpi *A, const mpi *B)
Unsigned subtraction: X = |A| - |B|.
int mpi_cmp_abs(const mpi *X, const mpi *Y)
Compare unsigned values.
Configuration options (set of defines)
int mpi_add_int(mpi *X, const mpi *A, t_sint b)
Signed addition: X = A + b.
int mpi_read_file(mpi *X, int radix, FILE *fin)
Read X from an opened file.
int mpi_div_mpi(mpi *Q, mpi *R, const mpi *A, const mpi *B)
Division by mpi: A = Q * B + R.
int mpi_lset(mpi *X, t_sint z)
Set value from integer.
int mpi_is_prime(mpi *X, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Miller-Rabin primality test.
MPI structure.
Definition: bignum.h:171
int mpi_write_file(const char *p, const mpi *X, int radix, FILE *fout)
Write X into an opened file, or stdout if fout is NULL.
void mpi_init(mpi *X)
Initialize one MPI.
int mpi_cmp_mpi(const mpi *X, const mpi *Y)
Compare signed values.
unsigned long long t_udbl
Definition: bignum.h:155
int mpi_shift_r(mpi *X, size_t count)
Right-shift: X &gt;&gt;= count.
int mpi_add_mpi(mpi *X, const mpi *A, const mpi *B)
Signed addition: X = A + B.
asn1_buf val
The named value.
Definition: asn1.h:151
int mpi_write_string(const mpi *X, int radix, char *s, size_t *slen)
Export into an ASCII string.
int32_t t_sint
Definition: bignum.h:148
size_t mpi_lsb(const mpi *X)
Return the number of zero-bits before the least significant &#39;1&#39; bit.
int mpi_inv_mod(mpi *X, const mpi *A, const mpi *N)
Modular inverse: X = A^-1 mod N.
void mpi_free(mpi *X)
Unallocate one MPI.
int mpi_mul_int(mpi *X, const mpi *A, t_sint b)
Baseline multiplication: X = A * b Note: b is an unsigned integer type, thus Negative values of b are...
int mpi_grow(mpi *X, size_t nblimbs)
Enlarge to the specified number of limbs.
int mpi_mod_int(t_uint *r, const mpi *A, t_sint b)
Modulo: r = A mod b.
int mpi_exp_mod(mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR)
Sliding-window exponentiation: X = A^E mod N.
int mpi_gen_prime(mpi *X, size_t nbits, int dh_flag, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Prime number generation.
size_t mpi_msb(const mpi *X)
Return the number of bits up to and including the most significant &#39;1&#39; bit&#39;.
int mpi_add_abs(mpi *X, const mpi *A, const mpi *B)
Unsigned addition: X = |A| + |B|.
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
t_uint * p
Definition: bignum.h:175
int mpi_read_binary(mpi *X, const unsigned char *buf, size_t buflen)
Import X from unsigned binary data, big endian.
int mpi_self_test(int verbose)
Checkup routine.
size_t mpi_size(const mpi *X)
Return the total size in bytes.
int mpi_copy(mpi *X, const mpi *Y)
Copy the contents of Y into X.
size_t n
Definition: bignum.h:174
int mpi_mod_mpi(mpi *R, const mpi *A, const mpi *B)
Modulo: R = A mod B.
int mpi_get_bit(const mpi *X, size_t pos)
Get a specific bit from X.
int mpi_write_binary(const mpi *X, unsigned char *buf, size_t buflen)
Export X into unsigned binary data, big endian.
int mpi_shift_l(mpi *X, size_t count)
Left-shift: X &lt;&lt;= count.
int mpi_mul_mpi(mpi *X, const mpi *A, const mpi *B)
Baseline multiplication: X = A * B.
int mpi_sub_mpi(mpi *X, const mpi *A, const mpi *B)
Signed subtraction: X = A - B.
int mpi_set_bit(mpi *X, size_t pos, unsigned char val)
Set a bit of X to a specific value of 0 or 1.
int mpi_sub_int(mpi *X, const mpi *A, t_sint b)
Signed subtraction: X = A - b.