3 * Release $Name: MATRIXSSL_1_8_8_OPEN $
5 * multiple-precision integer library
8 * Copyright (c) PeerSec Networks, 2002-2009. All Rights Reserved.
9 * The latest version of this code is available at http://www.matrixssl.org
11 * This software is open source; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This General Public License does NOT permit incorporating this software
17 * into proprietary programs. If you are unable to comply with the GPL, a
18 * commercial license for this software may be purchased from PeerSec Networks
19 * at http://www.peersec.com
21 * This program is distributed in WITHOUT ANY WARRANTY; without even the
22 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 * See the GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * http://www.gnu.org/copyleft/gpl.html
30 /******************************************************************************/
43 #define MIN(x,y) ((x)<(y)?(x):(y))
45 #define MAX(x,y) ((x)>(y)?(x):(y))
52 C++ compilers don't like assigning void * to mp_digit *
54 #define OPT_CAST(x) (x *)
59 C on the other hand doesn't care
63 #endif /* __cplusplus */
65 /******************************************************************************/
67 some default configurations.
69 A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
70 A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
72 At the very least a mp_digit must be able to hold 7 bits
73 [any size beyond that is ok provided it doesn't overflow the data type]
76 typedef unsigned char mp_digit
;
77 typedef unsigned short mp_word
;
78 #elif defined(MP_16BIT)
79 typedef unsigned short mp_digit
;
80 typedef unsigned long mp_word
;
81 #elif defined(MP_64BIT)
83 for GCC only on supported platforms
86 typedef unsigned long long ulong64
;
87 typedef signed long long long64
;
90 typedef ulong64 mp_digit
;
91 typedef unsigned long mp_word
__attribute__ ((mode(TI
)));
96 this is the default case, 28-bit digits
99 #if defined(_MSC_VER) || defined(__BORLANDC__)
100 typedef unsigned __int64 ulong64
;
101 typedef signed __int64 long64
;
103 typedef unsigned long long ulong64
;
104 typedef signed long long long64
;
108 typedef unsigned long mp_digit
;
109 typedef ulong64 mp_word
;
113 this is an extension that uses 31-bit digits
118 default case is 28-bit digits, defines MP_28BIT as a handy macro to test
122 #endif /* MP_31BIT */
126 otherwise the bits per digit is calculated automatically from the size of
130 #define DIGIT_BIT ((int32)((CHAR_BIT * sizeof(mp_digit) - 1))) /* bits per digit */
131 #endif /* DIGIT_BIT */
133 #define MP_DIGIT_BIT DIGIT_BIT
134 #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
135 #define MP_DIGIT_MAX MP_MASK
137 /******************************************************************************/
141 #define MP_LT -1 /* less than */
142 #define MP_EQ 0 /* equal to */
143 #define MP_GT 1 /* greater than */
145 #define MP_ZPOS 0 /* positive integer */
146 #define MP_NEG 1 /* negative */
148 #define MP_OKAY 0 /* ok result */
149 #define MP_MEM -2 /* out of mem */
150 #define MP_VAL -3 /* invalid input */
151 #define MP_RANGE MP_VAL
153 #define MP_YES 1 /* yes response */
154 #define MP_NO 0 /* no response */
156 typedef int32 mp_err
;
158 /******************************************************************************/
160 various build options
162 #define MP_PREC 64 /* default digits of precision */
165 define this to use lower memory usage routines (exptmods mostly)
170 size of comba arrays, should be at least
171 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2)
173 #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
176 int32 used
, alloc
, sign
;
180 #define USED(m) ((m)->used)
181 #define DIGIT(m,k) ((m)->dp[(k)])
182 #define SIGN(m) ((m)->sign)
184 /******************************************************************************/
186 init and deinit bignum functions
192 extern int32
mp_init(psPool_t
*pool
, mp_int
*a
);
197 extern void mp_clear(mp_int
*a
);
200 init a series of arguments
202 extern int32
_mp_init_multi(psPool_t
*pool
, mp_int
*mp0
, mp_int
*mp1
, mp_int
*mp2
,
203 mp_int
*mp3
, mp_int
*mp4
, mp_int
*mp5
, mp_int
*mp6
,
207 clear a series of arguments
209 extern void _mp_clear_multi(mp_int
*mp0
, mp_int
*mp1
, mp_int
*mp2
, mp_int
*mp3
,
210 mp_int
*mp4
, mp_int
*mp5
, mp_int
*mp6
, mp_int
*mp7
);
215 extern void mp_exch(mp_int
*a
, mp_int
*b
);
218 shrink ram required for a bignum
220 extern int32
mp_shrink(mp_int
*a
);
223 grow an int32 to a given size
225 extern int32
mp_grow(mp_int
*a
, int32 size
);
228 init to a given number of digits
230 extern int32
mp_init_size(psPool_t
*pool
, mp_int
*a
, int32 size
);
232 /******************************************************************************/
236 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
237 #define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
238 #define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
240 extern int32
mp_add_d (mp_int
* a
, mp_digit b
, mp_int
* c
);
241 extern int32
mp_sub_d (mp_int
* a
, mp_digit b
, mp_int
* c
);
245 extern void mp_zero(mp_int
*a
);
250 extern void mp_set(mp_int
*a
, mp_digit b
);
255 extern int32
mp_copy(mp_int
*a
, mp_int
*b
);
258 inits and copies, a = b
260 extern int32
mp_init_copy(psPool_t
*pool
, mp_int
*a
, mp_int
*b
);
265 extern void mp_clamp(mp_int
*a
);
267 /******************************************************************************/
273 right shift by "b" digits
275 extern void mp_rshd(mp_int
*a
, int32 b
);
278 left shift by "b" digits
280 extern int32
mp_lshd(mp_int
*a
, int32 b
);
285 extern int32
mp_div_2d(psPool_t
*pool
, mp_int
*a
, int32 b
, mp_int
*c
, mp_int
*d
);
290 extern int32
mp_div_2(mp_int
*a
, mp_int
*b
);
295 extern int32
mp_mul_2d(mp_int
*a
, int32 b
, mp_int
*c
);
300 extern int32
mp_mod_2d(mp_int
*a
, int32 b
, mp_int
*c
);
305 extern int32
mp_2expt(mp_int
*a
, int32 b
);
307 /******************************************************************************/
315 extern int32
mp_abs(mp_int
*a
, mp_int
*b
);
320 extern int32
mp_cmp(mp_int
*a
, mp_int
*b
);
325 extern int32
mp_cmp_mag(mp_int
*a
, mp_int
*b
);
330 extern int32
mp_add(mp_int
*a
, mp_int
*b
, mp_int
*c
);
335 extern int32
mp_sub(mp_int
*a
, mp_int
*b
, mp_int
*c
);
341 /* moved mp_mul out of SLOW case */
342 extern int32
mp_mul(psPool_t
*pool
, mp_int
*a
, mp_int
*b
, mp_int
*c
);
343 #ifdef USE_SMALL_WORD
344 extern int32
mp_sqr(psPool_t
*pool
, mp_int
*a
, mp_int
*b
);
350 extern int32
mp_div(psPool_t
*pool
, mp_int
*a
, mp_int
*b
, mp_int
*c
, mp_int
*d
);
353 c = a mod b, 0 <= c < b
355 extern int32
mp_mod(psPool_t
*pool
, mp_int
*a
, mp_int
*b
, mp_int
*c
);
357 /******************************************************************************/
359 single digit functions
363 compare against a single digit
365 extern int32
mp_cmp_d(mp_int
*a
, mp_digit b
);
370 extern int32
mp_mul_d(mp_int
*a
, mp_digit b
, mp_int
*c
);
372 /******************************************************************************/
380 extern int32
mp_addmod(psPool_t
*pool
, mp_int
*a
, mp_int
*b
, mp_int
*c
, mp_int
*d
);
385 extern int32
mp_mulmod(psPool_t
*pool
, mp_int
*a
, mp_int
*b
, mp_int
*c
, mp_int
*d
);
390 extern int32
mp_invmodSSH(psPool_t
*pool
, mp_int
*a
, mp_int
*b
, mp_int
*c
);
393 setups the montgomery reduction
395 extern int32
mp_montgomery_setup(mp_int
*a
, mp_digit
*mp
);
398 computes a = B**n mod b without division or multiplication useful for
399 normalizing numbers in a Montgomery system.
401 extern int32
mp_montgomery_calc_normalization(mp_int
*a
, mp_int
*b
);
404 computes x/R == x (mod N) via Montgomery Reduction
406 #ifdef USE_SMALL_WORD
407 extern int32
mp_montgomery_reduce(mp_int
*a
, mp_int
*m
, mp_digit mp
);
413 extern int32
mp_exptmod(psPool_t
*pool
, mp_int
*a
, mp_int
*b
, mp_int
*c
, mp_int
*d
);
415 /******************************************************************************/
417 If we're using 1024 or 2048 bit keys and 28 bit digits, we only need the
418 fast_ versions of these functions, removing the others to save space.
419 Otherwise, we include the slow versions as well and which version to use
422 #ifdef USE_SMALL_WORD
423 extern int32
s_mp_mul_digs(psPool_t
*pool
, mp_int
*a
, mp_int
*b
, mp_int
*c
,
425 extern int32
s_mp_sqr(psPool_t
*pool
, mp_int
*a
, mp_int
*b
);
428 #define mp_montgomery_reduce fast_mp_montgomery_reduce
429 #define mp_sqr fast_s_mp_sqr
430 #define s_mp_mul_digs fast_s_mp_mul_digs
431 #define mp_invmod fast_mp_invmod
434 /******************************************************************************/
438 extern int32
mp_count_bits(mp_int
*a
);
440 extern int32
mp_unsigned_bin_size(mp_int
*a
);
441 extern int32
mp_read_unsigned_bin(mp_int
*a
, unsigned char *b
, int32 c
);
442 extern int32
mp_to_unsigned_bin(psPool_t
*pool
, mp_int
*a
, unsigned char *b
);
444 extern int32
mp_signed_bin_size(mp_int
*a
);
447 lowlevel functions, do not call!
449 /* define this in all cases for now FUTURE*/
450 #define s_mp_mul(P, A, B, C) s_mp_mul_digs(P, A, B, C, (A)->used + (B)->used + 1)
456 extern int32
mp_mul_2(mp_int
*a
, mp_int
*b
);
458 extern int32
s_mp_add(mp_int
*a
, mp_int
*b
, mp_int
*c
);
459 extern int32
s_mp_sub(mp_int
*a
, mp_int
*b
, mp_int
*c
);
461 extern int32
fast_s_mp_mul_digs(psPool_t
*pool
, mp_int
*a
, mp_int
*b
, mp_int
*c
,
463 extern int32
fast_s_mp_sqr(psPool_t
*pool
, mp_int
*a
, mp_int
*b
);
465 extern int32
fast_mp_invmod(psPool_t
*pool
, mp_int
*a
, mp_int
*b
, mp_int
*c
);
466 extern int32
fast_mp_montgomery_reduce(mp_int
*a
, mp_int
*m
, mp_digit mp
);
468 extern void bn_reverse(unsigned char *s
, int32 len
);
473 #endif /* __cplusplus */