1 /* mpi.h - Multi Precision Integers
2 * Copyright (C) 1994, 1996, 1998, 1999,
3 * 2000, 2001 Free Software Foundation, Inc.
5 * This file is part of GNUPG.
7 * GNUPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GNUPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 * Note: This code is heavily based on the GNU MP Library.
23 * Actually it's the same code with only minor changes in the
24 * way the data is stored; this is to support the abstraction
25 * of an optional secure memory allocation which may be used
26 * to avoid revealing of sensitive data due to paging etc.
27 * The GNU MP Library itself is published under the LGPL;
28 * however I decided to publish this code under the plain GPL.
34 #error this file should not be used anymore
47 #if BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_INT
48 typedef unsigned int mpi_limb_t
;
49 typedef signed int mpi_limb_signed_t
;
50 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG
51 typedef unsigned long int mpi_limb_t
;
52 typedef signed long int mpi_limb_signed_t
;
53 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG_LONG
54 typedef unsigned long long int mpi_limb_t
;
55 typedef signed long long int mpi_limb_signed_t
;
56 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_SHORT
57 typedef unsigned short int mpi_limb_t
;
58 typedef signed short int mpi_limb_signed_t
;
60 #error BYTES_PER_MPI_LIMB does not match any C type
62 #define BITS_PER_MPI_LIMB (8*BYTES_PER_MPI_LIMB)
64 #ifndef EXTERN_UNLESS_MAIN_MODULE
65 #if defined (__riscos__) && !defined (INCLUDED_BY_MAIN_MODULE)
66 #define EXTERN_UNLESS_MAIN_MODULE extern
68 #define EXTERN_UNLESS_MAIN_MODULE
72 #define DBG_MPI mpi_debug_mode
73 EXTERN_UNLESS_MAIN_MODULE
int mpi_debug_mode
;
77 int alloced
; /* array size (# of allocated limbs) */
78 int nlimbs
; /* number of valid limbs */
79 int nbits
; /* the real number of valid bits (info only) */
80 int sign
; /* indicates a negative number */
81 unsigned flags
; /* bit 0: array must be allocated in secure memory space */
83 /* bit 2: the limb is a pointer to some m_alloced data */
84 mpi_limb_t
*d
; /* array with the limbs */
87 typedef struct gcry_mpi
*MPI
;
91 #define mpi_get_nlimbs(a) ((a)->nlimbs)
92 #define mpi_is_neg(a) ((a)->sign)
97 #define mpi_alloc(n) mpi_debug_alloc((n), M_DBGINFO( __LINE__ ) )
98 #define mpi_alloc_secure(n) mpi_debug_alloc_secure((n), M_DBGINFO( __LINE__ ) )
99 #define mpi_alloc_like(n) mpi_debug_alloc_like((n), M_DBGINFO( __LINE__ ) )
100 #define mpi_free(a) mpi_debug_free((a), M_DBGINFO(__LINE__) )
101 #define mpi_resize(a,b) mpi_debug_resize((a),(b), M_DBGINFO(__LINE__) )
102 #define mpi_copy(a) mpi_debug_copy((a), M_DBGINFO(__LINE__) )
103 MPI
mpi_debug_alloc( unsigned nlimbs
, const char *info
);
104 MPI
mpi_debug_alloc_secure( unsigned nlimbs
, const char *info
);
105 MPI
mpi_debug_alloc_like( MPI a
, const char *info
);
106 void mpi_debug_free( MPI a
, const char *info
);
107 void mpi_debug_resize( MPI a
, unsigned nlimbs
, const char *info
);
108 MPI
mpi_debug_copy( MPI a
, const char *info
);
110 MPI
mpi_alloc( unsigned nlimbs
);
111 MPI
mpi_alloc_secure( unsigned nlimbs
);
112 MPI
mpi_alloc_like( MPI a
);
113 void mpi_free( MPI a
);
114 void mpi_resize( MPI a
, unsigned nlimbs
);
115 MPI
mpi_copy( MPI a
);
117 #define mpi_is_opaque(a) ((a) && ((a)->flags&4))
118 MPI
mpi_set_opaque( MPI a
, void *p
, int len
);
119 void *mpi_get_opaque( MPI a
, int *len
);
120 #define mpi_is_secure(a) ((a) && ((a)->flags&1))
121 void mpi_set_secure( MPI a
);
122 void mpi_clear( MPI a
);
123 void mpi_set( MPI w
, MPI u
);
124 void mpi_set_ui( MPI w
, ulong u
);
125 MPI
mpi_alloc_set_ui( unsigned long u
);
126 void mpi_m_check( MPI a
);
127 void mpi_swap( MPI a
, MPI b
);
130 int mpi_write( IOBUF out
, MPI a
);
132 #define mpi_read(a,b,c) mpi_debug_read((a),(b),(c), M_DBGINFO( __LINE__ ) )
133 MPI
mpi_debug_read(IOBUF inp
, unsigned *nread
, int secure
, const char *info
);
135 MPI
mpi_read(IOBUF inp
, unsigned *nread
, int secure
);
137 MPI
mpi_read_from_buffer(byte
*buffer
, unsigned *ret_nread
, int secure
);
138 int mpi_fromstr(MPI val
, const char *str
);
139 int mpi_print( FILE *fp
, MPI a
, int mode
);
140 void g10_log_mpidump( const char *text
, MPI a
);
141 u32
mpi_get_keyid( MPI a
, u32
*keyid
);
142 byte
*mpi_get_buffer( MPI a
, unsigned *nbytes
, int *sign
);
143 byte
*mpi_get_secure_buffer( MPI a
, unsigned *nbytes
, int *sign
);
144 void mpi_set_buffer( MPI a
, const byte
*buffer
, unsigned nbytes
, int sign
);
146 #define log_mpidump g10_log_mpidump
149 void mpi_add_ui(MPI w
, MPI u
, ulong v
);
150 void mpi_add(MPI w
, MPI u
, MPI v
);
151 void mpi_addm(MPI w
, MPI u
, MPI v
, MPI m
);
152 void mpi_sub_ui(MPI w
, MPI u
, ulong v
);
153 void mpi_sub( MPI w
, MPI u
, MPI v
);
154 void mpi_subm( MPI w
, MPI u
, MPI v
, MPI m
);
157 void mpi_mul_ui(MPI w
, MPI u
, ulong v
);
158 void mpi_mul_2exp( MPI w
, MPI u
, ulong cnt
);
159 void mpi_mul( MPI w
, MPI u
, MPI v
);
160 void mpi_mulm( MPI w
, MPI u
, MPI v
, MPI m
);
163 ulong
mpi_fdiv_r_ui( MPI rem
, MPI dividend
, ulong divisor
);
164 void mpi_fdiv_r( MPI rem
, MPI dividend
, MPI divisor
);
165 void mpi_fdiv_q( MPI quot
, MPI dividend
, MPI divisor
);
166 void mpi_fdiv_qr( MPI quot
, MPI rem
, MPI dividend
, MPI divisor
);
167 void mpi_tdiv_r( MPI rem
, MPI num
, MPI den
);
168 void mpi_tdiv_qr( MPI quot
, MPI rem
, MPI num
, MPI den
);
169 void mpi_tdiv_q_2exp( MPI w
, MPI u
, unsigned count
);
170 int mpi_divisible_ui(MPI dividend
, ulong divisor
);
173 int mpi_gcd( MPI g
, MPI a
, MPI b
);
176 void mpi_pow( MPI w
, MPI u
, MPI v
);
177 void mpi_powm( MPI res
, MPI base
, MPI exp
, MPI mod
);
180 void mpi_mulpowm( MPI res
, MPI
*basearray
, MPI
*exparray
, MPI mod
);
183 int mpi_cmp_ui( MPI u
, ulong v
);
184 int mpi_cmp( MPI u
, MPI v
);
187 int mpi_getbyte( MPI a
, unsigned idx
);
188 void mpi_putbyte( MPI a
, unsigned idx
, int value
);
189 unsigned mpi_trailing_zeros( MPI a
);
192 void mpi_normalize( MPI a
);
193 unsigned mpi_get_nbits( MPI a
);
194 int mpi_test_bit( MPI a
, unsigned n
);
195 void mpi_set_bit( MPI a
, unsigned n
);
196 void mpi_set_highbit( MPI a
, unsigned n
);
197 void mpi_clear_highbit( MPI a
, unsigned n
);
198 void mpi_clear_bit( MPI a
, unsigned n
);
199 void mpi_rshift( MPI x
, MPI a
, unsigned n
);
202 void mpi_invm( MPI x
, MPI u
, MPI v
);