PolarSSL v1.1.4
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
#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002
36
#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004
37
#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006
38
#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008
39
#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A
40
#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C
41
#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E
42
#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010
44
#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
45
46
/*
47
* Maximum size MPIs are allowed to grow to in number of limbs.
48
*/
49
#define POLARSSL_MPI_MAX_LIMBS 10000
50
51
/*
52
* Maximum window size used for modular exponentiation. Default: 6
53
* Minimum value: 1. Maximum value: 6.
54
*
55
* Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
56
* for the sliding window calculation. (So 64 by default)
57
*
58
* Reduction in size, reduces speed.
59
*/
60
#define POLARSSL_MPI_WINDOW_SIZE 6
62
/*
63
* Maximum size of MPIs allowed in bits and bytes for user-MPIs.
64
* ( Default: 512 bytes => 4096 bits )
65
*
66
* Note: Calculations can results temporarily in larger MPIs. So the number
67
* of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
68
*/
69
#define POLARSSL_MPI_MAX_SIZE 512
70
#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE )
72
/*
73
* When reading from files with mpi_read_file() the buffer should have space
74
* for a (short) label, the MPI (in the provided radix), the newline
75
* characters and the '\0'.
76
*
77
* By default we assume at least a 10 char label, a minimum radix of 10
78
* (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
79
*/
80
#define POLARSSL_MPI_READ_BUFFER_SIZE 1250
81
82
/*
83
* Define the base integer type, architecture-wise
84
*/
85
#if defined(POLARSSL_HAVE_INT8)
86
typedef
signed
char
t_sint
;
87
typedef
unsigned
char
t_uint
;
88
typedef
unsigned
short
t_udbl;
89
#else
90
#if defined(POLARSSL_HAVE_INT16)
91
typedef
signed
short
t_sint
;
92
typedef
unsigned
short
t_uint
;
93
typedef
unsigned
long
t_udbl;
94
#else
95
typedef
signed
long
t_sint
;
96
typedef
unsigned
long
t_uint
;
97
#if defined(_MSC_VER) && defined(_M_IX86)
98
typedef
unsigned
__int64 t_udbl;
99
#else
100
#if defined(__GNUC__) && ( \
101
defined(__amd64__) || defined(__x86_64__) || \
102
defined(__ppc64__) || defined(__powerpc64__) || \
103
defined(__ia64__) || defined(__alpha__) || \
104
(defined(__sparc__) && defined(__arch64__)) || \
105
defined(__s390x__) )
106
typedef
unsigned
int
t_udbl __attribute__((mode(TI)));
107
#define POLARSSL_HAVE_LONGLONG
108
#else
109
#if defined(POLARSSL_HAVE_LONGLONG)
110
typedef
unsigned
long
long
t_udbl;
111
#endif
112
#endif
113
#endif
114
#endif
115
#endif
116
120
typedef
struct
121
{
122
int
s
;
123
size_t
n
;
124
t_uint
*
p
;
125
}
126
mpi
;
127
128
#ifdef __cplusplus
129
extern
"C"
{
130
#endif
131
137
void
mpi_init
(
mpi
*X );
138
144
void
mpi_free
(
mpi
*X );
145
155
int
mpi_grow
(
mpi
*X,
size_t
nblimbs );
156
166
int
mpi_copy
(
mpi
*X,
const
mpi
*Y );
167
174
void
mpi_swap
(
mpi
*X,
mpi
*Y );
175
185
int
mpi_lset
(
mpi
*X,
t_sint
z );
186
187
/*
188
* \brief Get a specific bit from X
189
*
190
* \param X MPI to use
191
* \param pos Zero-based index of the bit in X
192
*
193
* \return Either a 0 or a 1
194
*/
195
int
mpi_get_bit
(
mpi
*X,
size_t
pos );
196
197
/*
198
* \brief Set a bit of X to a specific value of 0 or 1
199
*
200
* \note Will grow X if necessary to set a bit to 1 in a not yet
201
* existing limb. Will not grow if bit should be set to 0
202
*
203
* \param X MPI to use
204
* \param pos Zero-based index of the bit in X
205
* \param val The value to set the bit to (0 or 1)
206
*
207
* \return 0 if successful,
208
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
209
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
210
*/
211
int
mpi_set_bit
(
mpi
*X,
size_t
pos,
unsigned
char
val );
212
218
size_t
mpi_lsb
(
const
mpi
*X );
219
225
size_t
mpi_msb
(
const
mpi
*X );
226
232
size_t
mpi_size
(
const
mpi
*X );
233
243
int
mpi_read_string
(
mpi
*X,
int
radix,
const
char
*s );
244
260
int
mpi_write_string
(
const
mpi
*X,
int
radix,
char
*s,
size_t
*slen );
261
273
int
mpi_read_file
(
mpi
*X,
int
radix, FILE *fin );
274
287
int
mpi_write_file
(
const
char
*p,
const
mpi
*X,
int
radix, FILE *fout );
288
299
int
mpi_read_binary
(
mpi
*X,
const
unsigned
char
*
buf
,
size_t
buflen );
300
311
int
mpi_write_binary
(
const
mpi
*X,
unsigned
char
*buf,
size_t
buflen );
312
322
int
mpi_shift_l
(
mpi
*X,
size_t
count );
323
333
int
mpi_shift_r
(
mpi
*X,
size_t
count );
334
345
int
mpi_cmp_abs
(
const
mpi
*X,
const
mpi
*Y );
346
357
int
mpi_cmp_mpi
(
const
mpi
*X,
const
mpi
*Y );
358
369
int
mpi_cmp_int
(
const
mpi
*X,
t_sint
z );
370
381
int
mpi_add_abs
(
mpi
*X,
const
mpi
*A,
const
mpi
*B );
382
393
int
mpi_sub_abs
(
mpi
*X,
const
mpi
*A,
const
mpi
*B );
394
405
int
mpi_add_mpi
(
mpi
*X,
const
mpi
*A,
const
mpi
*B );
406
417
int
mpi_sub_mpi
(
mpi
*X,
const
mpi
*A,
const
mpi
*B );
418
429
int
mpi_add_int
(
mpi
*X,
const
mpi
*A,
t_sint
b );
430
441
int
mpi_sub_int
(
mpi
*X,
const
mpi
*A,
t_sint
b );
442
453
int
mpi_mul_mpi
(
mpi
*X,
const
mpi
*A,
const
mpi
*B );
454
467
int
mpi_mul_int
(
mpi
*X,
const
mpi
*A,
t_sint
b );
468
483
int
mpi_div_mpi
(
mpi
*Q,
mpi
*R,
const
mpi
*A,
const
mpi
*B );
484
499
int
mpi_div_int
(
mpi
*Q,
mpi
*R,
const
mpi
*A,
t_sint
b );
500
513
int
mpi_mod_mpi
(
mpi
*R,
const
mpi
*A,
const
mpi
*B );
514
527
int
mpi_mod_int
(
t_uint
*r,
const
mpi
*A,
t_sint
b );
528
546
int
mpi_exp_mod
(
mpi
*X,
const
mpi
*A,
const
mpi
*E,
const
mpi
*N,
mpi
*_RR );
547
559
int
mpi_fill_random
(
mpi
*X,
size_t
size,
560
int
(*f_rng)(
void
*,
unsigned
char
*,
size_t
),
561
void
*p_rng );
562
573
int
mpi_gcd
(
mpi
*G,
const
mpi
*A,
const
mpi
*B );
574
587
int
mpi_inv_mod
(
mpi
*X,
const
mpi
*A,
const
mpi
*N );
588
600
int
mpi_is_prime
(
mpi
*X,
601
int
(*f_rng)(
void
*,
unsigned
char
*,
size_t
),
602
void
*p_rng );
603
617
int
mpi_gen_prime
(
mpi
*X,
size_t
nbits,
int
dh_flag,
618
int
(*f_rng)(
void
*,
unsigned
char
*,
size_t
),
619
void
*p_rng );
620
626
int
mpi_self_test
(
int
verbose );
627
628
#ifdef __cplusplus
629
}
630
#endif
631
632
#endif
/* bignum.h */
Generated on Wed Aug 8 2012 22:20:28 for PolarSSL v1.1.4 by
1.8.1.2