raw
ch1_mpi                 1 /* mpi-internal.h  -  Internal to the Multi Precision Integers
ch1_mpi 2 * Modified by No Such Labs. (C) 2015. See README.
eucrypt_mpi_fix_c... 3 * Modified by S.MG, 2017: fixing broken MPN_COPY_INCR macro. See EuCrypt patches.
ch1_mpi 4 *
ch1_mpi 5 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
ch1_mpi 6 * SHA256(gnupg-1.4.10.tar.gz):
ch1_mpi 7 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
ch1_mpi 8 * (C) 1994-2005 Free Software Foundation, Inc.
ch1_mpi 9 *
ch1_mpi 10 * This program is free software: you can redistribute it and/or modify
ch1_mpi 11 * it under the terms of the GNU General Public License as published by
ch1_mpi 12 * the Free Software Foundation, either version 3 of the License, or
ch1_mpi 13 * (at your option) any later version.
ch1_mpi 14 *
ch1_mpi 15 * This program is distributed in the hope that it will be useful,
ch1_mpi 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ch1_mpi 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ch1_mpi 18 * GNU General Public License for more details.
ch1_mpi 19 *
ch1_mpi 20 * You should have received a copy of the GNU General Public License
ch1_mpi 21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
ch1_mpi 22 */
ch1_mpi 23
ch1_mpi 24 #ifndef G10_MPI_INTERNAL_H
ch1_mpi 25 #define G10_MPI_INTERNAL_H
ch1_mpi 26
ch1_mpi 27 #include "mpi.h"
ch1_mpi 28
ch1_mpi 29 /* from the old mpi-asm-defs.h */
ch1_mpi 30 #define BYTES_PER_MPI_LIMB (SIZEOF_UNSIGNED_LONG)
ch1_mpi 31
ch1_mpi 32 #if BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_INT
ch1_mpi 33 typedef unsigned int mpi_limb_t;
ch1_mpi 34 typedef signed int mpi_limb_signed_t;
ch1_mpi 35 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG
ch1_mpi 36 typedef unsigned long int mpi_limb_t;
ch1_mpi 37 typedef signed long int mpi_limb_signed_t;
ch1_mpi 38 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG_LONG
ch1_mpi 39 typedef unsigned long long int mpi_limb_t;
ch1_mpi 40 typedef signed long long int mpi_limb_signed_t;
ch1_mpi 41 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_SHORT
ch1_mpi 42 typedef unsigned short int mpi_limb_t;
ch1_mpi 43 typedef signed short int mpi_limb_signed_t;
ch1_mpi 44 #else
ch1_mpi 45 #error BYTES_PER_MPI_LIMB does not match any C type
ch1_mpi 46 #endif
ch1_mpi 47 #define BITS_PER_MPI_LIMB (8*BYTES_PER_MPI_LIMB)
ch1_mpi 48
ch1_mpi 49
ch1_mpi 50 struct gcry_mpi {
ch1_mpi 51 int alloced; /* array size (# of allocated limbs) */
ch1_mpi 52 int nlimbs; /* number of valid limbs */
ch1_mpi 53 unsigned int nbits; /* the real number of valid bits (info only) */
ch1_mpi 54 int sign; /* indicates a negative number */
ch1_mpi 55 unsigned flags; /* bit 0: array must be allocated in secure memory space */
ch1_mpi 56 /* bit 1: not used */
ch1_mpi 57 /* bit 2: the limb is a pointer to some xmalloced data */
ch1_mpi 58 mpi_limb_t *d; /* array with the limbs */
ch1_mpi 59 };
ch1_mpi 60
ch1_mpi 61
ch1_mpi 62
ch1_mpi 63 /* If KARATSUBA_THRESHOLD is not already defined, define it to a
ch1_mpi 64 * value which is good on most machines. */
ch1_mpi 65
ch1_mpi 66 /* tested 4, 16, 32 and 64, where 16 gave the best performance when
ch1_mpi 67 * checking a 768 and a 1024 bit ElGamal signature.
ch1_mpi 68 * (wk 22.12.97) */
ch1_mpi 69 #ifndef KARATSUBA_THRESHOLD
ch1_mpi 70 #define KARATSUBA_THRESHOLD 16
ch1_mpi 71 #endif
ch1_mpi 72
ch1_mpi 73 /* The code can't handle KARATSUBA_THRESHOLD smaller than 2. */
ch1_mpi 74 #if KARATSUBA_THRESHOLD < 2
ch1_mpi 75 #undef KARATSUBA_THRESHOLD
ch1_mpi 76 #define KARATSUBA_THRESHOLD 2
ch1_mpi 77 #endif
ch1_mpi 78
ch1_mpi 79
ch1_mpi 80 typedef mpi_limb_t *mpi_ptr_t; /* pointer to a limb */
ch1_mpi 81 typedef int mpi_size_t; /* (must be a signed type) */
ch1_mpi 82
ch1_mpi 83 #define ABS(x) (x >= 0 ? x : -x)
ch1_mpi 84 #define MIN(l,o) ((l) < (o) ? (l) : (o))
ch1_mpi 85 #define MAX(h,i) ((h) > (i) ? (h) : (i))
ch1_mpi 86 #define RESIZE_IF_NEEDED(a,b) \
ch1_mpi 87 do { \
ch1_mpi 88 if( (a)->alloced < (b) ) \
ch1_mpi 89 mpi_resize((a), (b)); \
ch1_mpi 90 } while(0)
ch1_mpi 91
ch1_mpi 92 /* Copy N limbs from S to D. */
ch1_mpi 93 #define MPN_COPY( d, s, n) \
ch1_mpi 94 do { \
ch1_mpi 95 mpi_size_t _i; \
ch1_mpi 96 for( _i = 0; _i < (n); _i++ ) \
ch1_mpi 97 (d)[_i] = (s)[_i]; \
ch1_mpi 98 } while(0)
ch1_mpi 99
ch1_mpi 100 #define MPN_COPY_INCR( d, s, n) \
ch1_mpi 101 do { \
ch1_mpi 102 mpi_size_t _i; \
ch1_mpi 103 for( _i = 0; _i < (n); _i++ ) \
eucrypt_mpi_fix_c... 104 (d)[_i] = (s)[_i]; \
ch1_mpi 105 } while (0)
ch1_mpi 106
ch1_mpi 107 #define MPN_COPY_DECR( d, s, n ) \
ch1_mpi 108 do { \
ch1_mpi 109 mpi_size_t _i; \
ch1_mpi 110 for( _i = (n)-1; _i >= 0; _i--) \
ch1_mpi 111 (d)[_i] = (s)[_i]; \
ch1_mpi 112 } while(0)
ch1_mpi 113
ch1_mpi 114 /* Zero N limbs at D */
ch1_mpi 115 #define MPN_ZERO(d, n) \
ch1_mpi 116 do { \
ch1_mpi 117 int _i; \
ch1_mpi 118 for( _i = 0; _i < (n); _i++ ) \
ch1_mpi 119 (d)[_i] = 0; \
ch1_mpi 120 } while (0)
ch1_mpi 121
ch1_mpi 122 #define MPN_NORMALIZE(d, n) \
ch1_mpi 123 do { \
ch1_mpi 124 while( (n) > 0 ) { \
ch1_mpi 125 if( (d)[(n)-1] ) \
ch1_mpi 126 break; \
ch1_mpi 127 (n)--; \
ch1_mpi 128 } \
ch1_mpi 129 } while(0)
ch1_mpi 130
ch1_mpi 131 #define MPN_NORMALIZE_NOT_ZERO(d, n) \
ch1_mpi 132 do { \
ch1_mpi 133 for(;;) { \
ch1_mpi 134 if( (d)[(n)-1] ) \
ch1_mpi 135 break; \
ch1_mpi 136 (n)--; \
ch1_mpi 137 } \
ch1_mpi 138 } while(0)
ch1_mpi 139
ch1_mpi 140 #define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
ch1_mpi 141 do { \
ch1_mpi 142 if( (size) < KARATSUBA_THRESHOLD ) \
ch1_mpi 143 mul_n_basecase (prodp, up, vp, size); \
ch1_mpi 144 else \
ch1_mpi 145 mul_n (prodp, up, vp, size, tspace); \
ch1_mpi 146 } while (0);
ch1_mpi 147
ch1_mpi 148
ch1_mpi 149 /* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
ch1_mpi 150 * limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
ch1_mpi 151 * If this would yield overflow, DI should be the largest possible number
ch1_mpi 152 * (i.e., only ones). For correct operation, the most significant bit of D
ch1_mpi 153 * has to be set. Put the quotient in Q and the remainder in R.
ch1_mpi 154 */
ch1_mpi 155 #define UDIV_QRNND_PREINV(q, r, nh, nl, d, di) \
ch1_mpi 156 do { \
ch1_mpi 157 mpi_limb_t _q, _ql, _r; \
ch1_mpi 158 mpi_limb_t _xh, _xl; \
ch1_mpi 159 umul_ppmm (_q, _ql, (nh), (di)); \
ch1_mpi 160 _q += (nh); /* DI is 2**BITS_PER_MPI_LIMB too small */ \
ch1_mpi 161 umul_ppmm (_xh, _xl, _q, (d)); \
ch1_mpi 162 sub_ddmmss (_xh, _r, (nh), (nl), _xh, _xl); \
ch1_mpi 163 if( _xh ) { \
ch1_mpi 164 sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
ch1_mpi 165 _q++; \
ch1_mpi 166 if( _xh) { \
ch1_mpi 167 sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
ch1_mpi 168 _q++; \
ch1_mpi 169 } \
ch1_mpi 170 } \
ch1_mpi 171 if( _r >= (d) ) { \
ch1_mpi 172 _r -= (d); \
ch1_mpi 173 _q++; \
ch1_mpi 174 } \
ch1_mpi 175 (r) = _r; \
ch1_mpi 176 (q) = _q; \
ch1_mpi 177 } while (0)
ch1_mpi 178
ch1_mpi 179
ch1_mpi 180 /*-- mpiutil.c --*/
ch1_mpi 181 #ifdef M_DEBUG
ch1_mpi 182 #define mpi_alloc_limb_space(n,f) mpi_debug_alloc_limb_space((n),(f), M_DBGINFO( __LINE__ ) )
ch1_mpi 183 #define mpi_free_limb_space(n) mpi_debug_free_limb_space((n), M_DBGINFO( __LINE__ ) )
ch1_mpi 184 mpi_ptr_t mpi_debug_alloc_limb_space( unsigned nlimbs, int sec, const char *info );
ch1_mpi 185 void mpi_debug_free_limb_space( mpi_ptr_t a, const char *info );
ch1_mpi 186 #else
ch1_mpi 187 mpi_ptr_t mpi_alloc_limb_space( unsigned nlimbs, int sec );
ch1_mpi 188 void mpi_free_limb_space( mpi_ptr_t a );
ch1_mpi 189 #endif
ch1_mpi 190 void mpi_assign_limb_space( MPI a, mpi_ptr_t ap, unsigned nlimbs );
ch1_mpi 191
ch1_mpi 192 /*-- mpi-bit.c --*/
ch1_mpi 193 void mpi_rshift_limbs( MPI a, unsigned int count );
ch1_mpi 194 void mpi_lshift_limbs( MPI a, unsigned int count );
ch1_mpi 195
ch1_mpi 196
ch1_mpi 197 /*-- mpihelp-add.c --*/
ch1_mpi 198 mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
ch1_mpi 199 mpi_size_t s1_size, mpi_limb_t s2_limb );
ch1_mpi 200 mpi_limb_t mpihelp_add_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
ch1_mpi 201 mpi_ptr_t s2_ptr, mpi_size_t size);
ch1_mpi 202 mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
ch1_mpi 203 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
ch1_mpi 204
ch1_mpi 205 /*-- mpihelp-sub.c --*/
ch1_mpi 206 mpi_limb_t mpihelp_sub_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
ch1_mpi 207 mpi_size_t s1_size, mpi_limb_t s2_limb );
ch1_mpi 208 mpi_limb_t mpihelp_sub_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
ch1_mpi 209 mpi_ptr_t s2_ptr, mpi_size_t size);
ch1_mpi 210 mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
ch1_mpi 211 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
ch1_mpi 212
ch1_mpi 213 /*-- mpihelp-cmp.c --*/
ch1_mpi 214 int mpihelp_cmp( mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size );
ch1_mpi 215
ch1_mpi 216 /*-- mpihelp-mul.c --*/
ch1_mpi 217
ch1_mpi 218 struct karatsuba_ctx {
ch1_mpi 219 struct karatsuba_ctx *next;
ch1_mpi 220 mpi_ptr_t tspace;
ch1_mpi 221 mpi_size_t tspace_size;
ch1_mpi 222 mpi_ptr_t tp;
ch1_mpi 223 mpi_size_t tp_size;
ch1_mpi 224 };
ch1_mpi 225
ch1_mpi 226 void mpihelp_release_karatsuba_ctx( struct karatsuba_ctx *ctx );
ch1_mpi 227
ch1_mpi 228 mpi_limb_t mpihelp_addmul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
ch1_mpi 229 mpi_size_t s1_size, mpi_limb_t s2_limb);
ch1_mpi 230 mpi_limb_t mpihelp_submul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
ch1_mpi 231 mpi_size_t s1_size, mpi_limb_t s2_limb);
ch1_mpi 232 void mpihelp_mul_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp,
ch1_mpi 233 mpi_size_t size);
ch1_mpi 234 mpi_limb_t mpihelp_mul( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
ch1_mpi 235 mpi_ptr_t vp, mpi_size_t vsize);
ch1_mpi 236 void mpih_sqr_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size );
ch1_mpi 237 void mpih_sqr_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
ch1_mpi 238 mpi_ptr_t tspace);
ch1_mpi 239
ch1_mpi 240 void mpihelp_mul_karatsuba_case( mpi_ptr_t prodp,
ch1_mpi 241 mpi_ptr_t up, mpi_size_t usize,
ch1_mpi 242 mpi_ptr_t vp, mpi_size_t vsize,
ch1_mpi 243 struct karatsuba_ctx *ctx );
ch1_mpi 244
ch1_mpi 245
ch1_mpi 246 /*-- mpihelp-mul_1.c (or xxx/cpu/ *.S) --*/
ch1_mpi 247 mpi_limb_t mpihelp_mul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
ch1_mpi 248 mpi_size_t s1_size, mpi_limb_t s2_limb);
ch1_mpi 249
ch1_mpi 250 /*-- mpihelp-div.c --*/
ch1_mpi 251 mpi_limb_t mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
ch1_mpi 252 mpi_limb_t divisor_limb);
ch1_mpi 253 mpi_limb_t mpihelp_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
ch1_mpi 254 mpi_ptr_t np, mpi_size_t nsize,
ch1_mpi 255 mpi_ptr_t dp, mpi_size_t dsize);
ch1_mpi 256 mpi_limb_t mpihelp_divmod_1( mpi_ptr_t quot_ptr,
ch1_mpi 257 mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
ch1_mpi 258 mpi_limb_t divisor_limb);
ch1_mpi 259
ch1_mpi 260 /*-- mpihelp-shift.c --*/
ch1_mpi 261 mpi_limb_t mpihelp_lshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
ch1_mpi 262 unsigned cnt);
ch1_mpi 263 mpi_limb_t mpihelp_rshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
ch1_mpi 264 unsigned cnt);
ch1_mpi 265
ch1_mpi 266
ch1_mpi 267 /* Define stuff for longlong.h. */
ch1_mpi 268 #define W_TYPE_SIZE BITS_PER_MPI_LIMB
ch1_mpi 269 typedef mpi_limb_t UWtype;
ch1_mpi 270 typedef unsigned int UHWtype;
ch1_mpi 271 #if defined (__GNUC__)
ch1_mpi 272 typedef unsigned int UQItype __attribute__ ((mode (QI)));
ch1_mpi 273 typedef int SItype __attribute__ ((mode (SI)));
ch1_mpi 274 typedef unsigned int USItype __attribute__ ((mode (SI)));
ch1_mpi 275 typedef int DItype __attribute__ ((mode (DI)));
ch1_mpi 276 typedef unsigned int UDItype __attribute__ ((mode (DI)));
ch1_mpi 277 #else
ch1_mpi 278 typedef unsigned char UQItype;
ch1_mpi 279 typedef long SItype;
ch1_mpi 280 typedef unsigned long USItype;
ch1_mpi 281 #endif
ch1_mpi 282
ch1_mpi 283 #ifdef __GNUC__
ch1_mpi 284 #include "mpi-inline.h"
ch1_mpi 285 #endif
ch1_mpi 286
ch1_mpi 287 #endif /*G10_MPI_INTERNAL_H*/