PolarSSL v1.2.7
Main Page
Modules
Data Structures
Files
File List
Globals
include
polarssl
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
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
/*
69
* Maximum window size used for modular exponentiation. Default: 6
70
* Minimum value: 1. Maximum value: 6.
71
*
72
* Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
73
* for the sliding window calculation. (So 64 by default)
74
*
75
* Reduction in size, reduces speed.
76
*/
77
#define POLARSSL_MPI_WINDOW_SIZE 6
79
/*
80
* Maximum size of MPIs allowed in bits and bytes for user-MPIs.
81
* ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
82
*
83
* Note: Calculations can results temporarily in larger MPIs. So the number
84
* of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
85
*/
86
#define POLARSSL_MPI_MAX_SIZE 512
87
#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE )
89
/*
90
* When reading from files with mpi_read_file() and writing to files with
91
* mpi_write_file() the buffer should have space
92
* for a (short) label, the MPI (in the provided radix), the newline
93
* characters and the '\0'.
94
*
95
* By default we assume at least a 10 char label, a minimum radix of 10
96
* (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
97
* Autosized at compile time for at least a 10 char label, a minimum radix
98
* of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size.
99
*
100
* This used to be statically sized to 1250 for a maximum of 4096 bit
101
* numbers (1234 decimal chars).
102
*
103
* Calculate using the formula:
104
* POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) +
105
* LabelSize + 6
106
*/
107
#define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
108
#define LN_2_DIV_LN_10_SCALE100 332
109
#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 )
110
111
/*
112
* Define the base integer type, architecture-wise
113
*/
114
#if defined(POLARSSL_HAVE_INT8)
115
typedef
signed
char
t_sint
;
116
typedef
unsigned
char
t_uint
;
117
typedef
uint16_t
t_udbl
;
118
#define POLARSSL_HAVE_UDBL
119
#else
120
#if defined(POLARSSL_HAVE_INT16)
121
typedef
int16_t
t_sint
;
122
typedef
uint16_t
t_uint
;
123
typedef
uint32_t
t_udbl
;
124
#define POLARSSL_HAVE_UDBL
125
#else
126
#if ( defined(_MSC_VER) && defined(_M_AMD64) )
127
typedef
int64_t
t_sint
;
128
typedef
uint64_t
t_uint
;
129
#else
130
#if ( defined(__GNUC__) && ( \
131
defined(__amd64__) || defined(__x86_64__) || \
132
defined(__ppc64__) || defined(__powerpc64__) || \
133
defined(__ia64__) || defined(__alpha__) || \
134
(defined(__sparc__) && defined(__arch64__)) || \
135
defined(__s390x__) ) )
136
typedef
int64_t
t_sint
;
137
typedef
uint64_t
t_uint
;
138
typedef
unsigned
int
t_udbl
__attribute__((mode(TI)));
139
#define POLARSSL_HAVE_UDBL
140
#else
141
typedef
int32_t
t_sint
;
142
typedef
uint32_t
t_uint
;
143
#if ( defined(_MSC_VER) && defined(_M_IX86) )
144
typedef
uint64_t
t_udbl
;
145
#define POLARSSL_HAVE_UDBL
146
#else
147
#if defined( POLARSSL_HAVE_LONGLONG )
148
typedef
unsigned
long
long
t_udbl
;
149
#define POLARSSL_HAVE_UDBL
150
#endif
151
#endif
152
#endif
153
#endif
154
#endif
/* POLARSSL_HAVE_INT16 */
155
#endif
/* POLARSSL_HAVE_INT8 */
156
160
typedef
struct
161
{
162
int
s
;
163
size_t
n
;
164
t_uint *
p
;
165
}
166
mpi
;
167
168
#ifdef __cplusplus
169
extern
"C"
{
170
#endif
171
177
void
mpi_init
(
mpi
*X );
178
184
void
mpi_free
(
mpi
*X );
185
195
int
mpi_grow
(
mpi
*X,
size_t
nblimbs );
196
206
int
mpi_copy
(
mpi
*X,
const
mpi
*Y );
207
214
void
mpi_swap
(
mpi
*X,
mpi
*Y );
215
225
int
mpi_lset
(
mpi
*X, t_sint z );
226
235
int
mpi_get_bit
(
const
mpi
*X,
size_t
pos );
236
251
int
mpi_set_bit
(
mpi
*X,
size_t
pos,
unsigned
char
val );
252
261
size_t
mpi_lsb
(
const
mpi
*X );
262
271
size_t
mpi_msb
(
const
mpi
*X );
272
278
size_t
mpi_size
(
const
mpi
*X );
279
289
int
mpi_read_string
(
mpi
*X,
int
radix,
const
char
*s );
290
306
int
mpi_write_string
(
const
mpi
*X,
int
radix,
char
*s,
size_t
*slen );
307
308
#if defined(POLARSSL_FS_IO)
309
320
int
mpi_read_file
(
mpi
*X,
int
radix, FILE *fin );
321
334
int
mpi_write_file
(
const
char
*p,
const
mpi
*X,
int
radix, FILE *fout );
335
#endif
/* POLARSSL_FS_IO */
336
347
int
mpi_read_binary
(
mpi
*X,
const
unsigned
char
*
buf
,
size_t
buflen );
348
359
int
mpi_write_binary
(
const
mpi
*X,
unsigned
char
*buf,
size_t
buflen );
360
370
int
mpi_shift_l
(
mpi
*X,
size_t
count );
371
381
int
mpi_shift_r
(
mpi
*X,
size_t
count );
382
393
int
mpi_cmp_abs
(
const
mpi
*X,
const
mpi
*Y );
394
405
int
mpi_cmp_mpi
(
const
mpi
*X,
const
mpi
*Y );
406
417
int
mpi_cmp_int
(
const
mpi
*X, t_sint z );
418
429
int
mpi_add_abs
(
mpi
*X,
const
mpi
*A,
const
mpi
*B );
430
441
int
mpi_sub_abs
(
mpi
*X,
const
mpi
*A,
const
mpi
*B );
442
453
int
mpi_add_mpi
(
mpi
*X,
const
mpi
*A,
const
mpi
*B );
454
465
int
mpi_sub_mpi
(
mpi
*X,
const
mpi
*A,
const
mpi
*B );
466
477
int
mpi_add_int
(
mpi
*X,
const
mpi
*A, t_sint b );
478
489
int
mpi_sub_int
(
mpi
*X,
const
mpi
*A, t_sint b );
490
501
int
mpi_mul_mpi
(
mpi
*X,
const
mpi
*A,
const
mpi
*B );
502
515
int
mpi_mul_int
(
mpi
*X,
const
mpi
*A, t_sint b );
516
531
int
mpi_div_mpi
(
mpi
*Q,
mpi
*R,
const
mpi
*A,
const
mpi
*B );
532
547
int
mpi_div_int
(
mpi
*Q,
mpi
*R,
const
mpi
*A, t_sint b );
548
561
int
mpi_mod_mpi
(
mpi
*R,
const
mpi
*A,
const
mpi
*B );
562
575
int
mpi_mod_int
( t_uint *r,
const
mpi
*A, t_sint b );
576
595
int
mpi_exp_mod
(
mpi
*X,
const
mpi
*A,
const
mpi
*E,
const
mpi
*N,
mpi
*_RR );
596
608
int
mpi_fill_random
(
mpi
*X,
size_t
size,
609
int
(*f_rng)(
void
*,
unsigned
char
*,
size_t
),
610
void
*p_rng );
611
622
int
mpi_gcd
(
mpi
*G,
const
mpi
*A,
const
mpi
*B );
623
636
int
mpi_inv_mod
(
mpi
*X,
const
mpi
*A,
const
mpi
*N );
637
649
int
mpi_is_prime
(
mpi
*X,
650
int
(*f_rng)(
void
*,
unsigned
char
*,
size_t
),
651
void
*p_rng );
652
666
int
mpi_gen_prime
(
mpi
*X,
size_t
nbits,
int
dh_flag,
667
int
(*f_rng)(
void
*,
unsigned
char
*,
size_t
),
668
void
*p_rng );
669
675
int
mpi_self_test
(
int
verbose );
676
677
#ifdef __cplusplus
678
}
679
#endif
680
681
#endif
/* bignum.h */
Generated on Wed Apr 17 2013 15:56:18 for PolarSSL v1.2.7 by
1.8.3.1