Debian Codegraph @

mpi/mpi-internal.h

raw
1 /* mpi-internal.h - Internal to the Multi Precision Integers
2 * Copyright (C) 1994, 1996, 1998, 2000, 2002,
3 * 2003 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt 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 Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 *
21 * Note: This code is heavily based on the GNU MP Library.
22 * Actually it's the same code with only minor changes in the
23 * way the data is stored; this is to support the abstraction
24 * of an optional secure memory allocation which may be used
25 * to avoid revealing of sensitive data due to paging etc.
26 */
27
28 #ifndef G10_MPI_INTERNAL_H
29 #define G10_MPI_INTERNAL_H
30
31 #include "mpi-asm-defs.h"
32
33 #ifndef BITS_PER_MPI_LIMB
34 #if BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_INT
35 typedef unsigned int mpi_limb_t;
36 typedef signed int mpi_limb_signed_t;
37 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG
38 typedef unsigned long int mpi_limb_t;
39 typedef signed long int mpi_limb_signed_t;
40 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG_LONG
41 typedef unsigned long long int mpi_limb_t;
42 typedef signed long long int mpi_limb_signed_t;
43 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_SHORT
44 typedef unsigned short int mpi_limb_t;
45 typedef signed short int mpi_limb_signed_t;
46 #else
47 #error BYTES_PER_MPI_LIMB does not match any C type
48 #endif
49 #define BITS_PER_MPI_LIMB (8*BYTES_PER_MPI_LIMB)
50 #endif /*BITS_PER_MPI_LIMB*/
51
52 #include "mpi.h"
53 #include "const-time.h"
54
55 /* If KARATSUBA_THRESHOLD is not already defined, define it to a
56 * value which is good on most machines. */
57
58 /* tested 4, 16, 32 and 64, where 16 gave the best performance when
59 * checking a 768 and a 1024 bit ElGamal signature.
60 * (wk 22.12.97) */
61 #ifndef KARATSUBA_THRESHOLD
62 #define KARATSUBA_THRESHOLD 16
63 #endif
64
65 /* The code can't handle KARATSUBA_THRESHOLD smaller than 2. */
66 #if KARATSUBA_THRESHOLD < 2
67 #undef KARATSUBA_THRESHOLD
68 #define KARATSUBA_THRESHOLD 2
69 #endif
70
71
72 #define ABS(x) (x >= 0 ? x : -x)
73 #define MIN(l,o) ((l) < (o) ? (l) : (o))
74 #define MAX(h,i) ((h) > (i) ? (h) : (i))
75 #define RESIZE_IF_NEEDED(a,b) \
76 do { \
77 if( (a)->alloced < (b) ) \
78 mpi_resize((a), (b)); \
79 } while(0)
80 #define RESIZE_AND_CLEAR_IF_NEEDED(a,b) \
81 do { \
82 if( (a)->nlimbs < (b) ) \
83 mpi_resize((a), (b)); \
84 } while(0)
85
86 /* Copy N limbs from S to D. */
87 #define MPN_COPY( d, s, n) \
88 do { \
89 mpi_size_t _i; \
90 for( _i = 0; _i < (n); _i++ ) \
91 (d)[_i] = (s)[_i]; \
92 } while(0)
93
94 #define MPN_COPY_INCR( d, s, n) \
95 do { \
96 mpi_size_t _i; \
97 for( _i = 0; _i < (n); _i++ ) \
98 (d)[_i] = (s)[_i]; \
99 } while (0)
100
101 #define MPN_COPY_DECR( d, s, n ) \
102 do { \
103 mpi_size_t _i; \
104 for( _i = (n)-1; _i >= 0; _i--) \
105 (d)[_i] = (s)[_i]; \
106 } while(0)
107
108 /* Zero N limbs at D */
109 #define MPN_ZERO(d, n) \
110 do { \
111 int _i; \
112 for( _i = 0; _i < (n); _i++ ) \
113 (d)[_i] = 0; \
114 } while (0)
115
116 #define MPN_NORMALIZE(d, n) \
117 do { \
118 while( (n) > 0 ) { \
119 if( (d)[(n)-1] ) \
120 break; \
121 (n)--; \
122 } \
123 } while(0)
124
125 #define MPN_NORMALIZE_NOT_ZERO(d, n) \
126 do { \
127 for(;;) { \
128 if( (d)[(n)-1] ) \
129 break; \
130 (n)--; \
131 } \
132 } while(0)
133
134 #define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
135 do { \
136 if( (size) < KARATSUBA_THRESHOLD ) \
137 mul_n_basecase (prodp, up, vp, size); \
138 else \
139 mul_n (prodp, up, vp, size, tspace); \
140 } while (0)
141
142
143 /* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
144 * limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
145 * If this would yield overflow, DI should be the largest possible number
146 * (i.e., only ones). For correct operation, the most significant bit of D
147 * has to be set. Put the quotient in Q and the remainder in R.
148 */
149 #define UDIV_QRNND_PREINV(q, r, nh, nl, d, di) \
150 do { \
151 mpi_limb_t _ql GCC_ATTR_UNUSED; \
152 mpi_limb_t _q, _r; \
153 mpi_limb_t _xh, _xl; \
154 umul_ppmm (_q, _ql, (nh), (di)); \
155 _q += (nh); /* DI is 2**BITS_PER_MPI_LIMB too small */ \
156 umul_ppmm (_xh, _xl, _q, (d)); \
157 sub_ddmmss (_xh, _r, (nh), (nl), _xh, _xl); \
158 if( _xh ) { \
159 sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
160 _q++; \
161 if( _xh) { \
162 sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
163 _q++; \
164 } \
165 } \
166 if( _r >= (d) ) { \
167 _r -= (d); \
168 _q++; \
169 } \
170 (r) = _r; \
171 (q) = _q; \
172 } while (0)
173
174
175 /*-- mpiutil.c --*/
176 #define mpi_alloc_limb_space(n,f) _gcry_mpi_alloc_limb_space((n),(f))
177 mpi_ptr_t _gcry_mpi_alloc_limb_space( unsigned nlimbs, int sec );
178 void _gcry_mpi_free_limb_space( mpi_ptr_t a, unsigned int nlimbs );
179 void _gcry_mpi_assign_limb_space( gcry_mpi_t a, mpi_ptr_t ap, unsigned nlimbs );
180
181 /*-- mpi-bit.c --*/
182 #define mpi_rshift_limbs(a,n) _gcry_mpi_rshift_limbs ((a), (n))
183 #define mpi_lshift_limbs(a,n) _gcry_mpi_lshift_limbs ((a), (n))
184
185 void _gcry_mpi_rshift_limbs( gcry_mpi_t a, unsigned int count );
186 void _gcry_mpi_lshift_limbs( gcry_mpi_t a, unsigned int count );
187
188
189 /*-- mpih-add.c --*/
190 mpi_limb_t _gcry_mpih_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
191 mpi_size_t s1_size, mpi_limb_t s2_limb );
192 mpi_limb_t _gcry_mpih_add_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
193 mpi_ptr_t s2_ptr, mpi_size_t size);
194 mpi_limb_t _gcry_mpih_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
195 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
196
197 /*-- mpih-sub.c --*/
198 mpi_limb_t _gcry_mpih_sub_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
199 mpi_size_t s1_size, mpi_limb_t s2_limb );
200 mpi_limb_t _gcry_mpih_sub_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
201 mpi_ptr_t s2_ptr, mpi_size_t size);
202 mpi_limb_t _gcry_mpih_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
203 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
204
205 /*-- mpih-cmp.c --*/
206 int _gcry_mpih_cmp( mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size );
207
208 /*-- mpih-mul.c --*/
209
210 struct karatsuba_ctx {
211 struct karatsuba_ctx *next;
212 mpi_ptr_t tspace;
213 unsigned int tspace_nlimbs;
214 mpi_size_t tspace_size;
215 mpi_ptr_t tp;
216 unsigned int tp_nlimbs;
217 mpi_size_t tp_size;
218 };
219
220 void _gcry_mpih_release_karatsuba_ctx( struct karatsuba_ctx *ctx );
221
222 mpi_limb_t _gcry_mpih_addmul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
223 mpi_size_t s1_size, mpi_limb_t s2_limb);
224 mpi_limb_t _gcry_mpih_submul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
225 mpi_size_t s1_size, mpi_limb_t s2_limb);
226 void _gcry_mpih_mul_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp,
227 mpi_size_t size);
228 mpi_limb_t _gcry_mpih_mul( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
229 mpi_ptr_t vp, mpi_size_t vsize);
230 mpi_limb_t _gcry_mpih_mul_lli(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
231 mpi_ptr_t vp, mpi_size_t vsize);
232 void _gcry_mpih_sqr_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size );
233 void _gcry_mpih_sqr_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
234 mpi_ptr_t tspace);
235
236 void _gcry_mpih_mul_karatsuba_case( mpi_ptr_t prodp,
237 mpi_ptr_t up, mpi_size_t usize,
238 mpi_ptr_t vp, mpi_size_t vsize,
239 struct karatsuba_ctx *ctx );
240
241
242 /*-- mpih-mul_1.c (or xxx/cpu/ *.S) --*/
243 mpi_limb_t _gcry_mpih_mul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
244 mpi_size_t s1_size, mpi_limb_t s2_limb);
245
246 /*-- mpih-div.c --*/
247 mpi_limb_t _gcry_mpih_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
248 mpi_limb_t divisor_limb);
249 mpi_limb_t _gcry_mpih_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
250 mpi_ptr_t np, mpi_size_t nsize,
251 mpi_ptr_t dp, mpi_size_t dsize);
252 mpi_limb_t _gcry_mpih_divmod_1( mpi_ptr_t quot_ptr,
253 mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
254 mpi_limb_t divisor_limb);
255
256 /*-- mpih-shift.c --*/
257 mpi_limb_t _gcry_mpih_lshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
258 unsigned cnt);
259 mpi_limb_t _gcry_mpih_rshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
260 unsigned cnt);
261 /*-- mpih-pow.c --*/
262 void _gcry_mpih_powm_lli (mpi_ptr_t rp, mpi_ptr_t bp, mpi_ptr_t mp,
263 mpi_size_t n, mpi_ptr_t ep, mpi_size_t en);
264
265 /*-- mpih-const-time.c --*/
266 #define mpih_set_cond(w,u,s,o) _gcry_mpih_set_cond ((w),(u),(s),(o))
267 #define mpih_add_n_cond(w,u,v,s,o) _gcry_mpih_add_n_cond ((w),(u),(v),(s),(o))
268 #define mpih_sub_n_cond(w,u,v,s,o) _gcry_mpih_sub_n_cond ((w),(u),(v),(s),(o))
269 #define mpih_swap_cond(u,v,s,o) _gcry_mpih_swap_cond ((u),(v),(s),(o))
270 #define mpih_abs_cond(w,u,s,o) _gcry_mpih_abs_cond ((w),(u),(s),(o))
271 #define mpih_mod_lli(v,vs,u,us) _gcry_mpih_mod_lli ((v),(vs),(u),(us))
272
273 DEFINE_CT_TYPE_GEN_MASK(limb, mpi_limb_t)
274 DEFINE_CT_TYPE_GEN_INV_MASK(limb, mpi_limb_t)
275 DEFINE_CT_TYPE_SELECT_FUNC(limb, mpi_limb_t)
276
277 static inline int
278 mpih_limb_is_zero (mpi_limb_t a)
279 {
280 /* Sign bit set if A == 0. */
281 a = ~a & ~(-a);
282
283 return a >> (BITS_PER_MPI_LIMB - 1);
284 }
285
286 static inline int
287 mpih_limb_is_not_zero (mpi_limb_t a)
288 {
289 /* Sign bit set if A != 0. */
290 a = a | (-a);
291
292 return a >> (BITS_PER_MPI_LIMB - 1);
293 }
294
295 void _gcry_mpih_set_cond (mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
296 unsigned long op_enable);
297 mpi_limb_t _gcry_mpih_add_n_cond (mpi_ptr_t wp, mpi_ptr_t up, mpi_ptr_t vp,
298 mpi_size_t usize, unsigned long op_enable);
299 mpi_limb_t _gcry_mpih_sub_n_cond (mpi_ptr_t wp, mpi_ptr_t up, mpi_ptr_t vp,
300 mpi_size_t usize, unsigned long op_enable);
301 void _gcry_mpih_swap_cond (mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t usize,
302 unsigned long op_enable);
303 void _gcry_mpih_abs_cond (mpi_ptr_t wp, mpi_ptr_t up,
304 mpi_size_t usize, unsigned long op_enable);
305 void _gcry_mpih_lookup_lli (mpi_ptr_t rp, const mpi_limb_t *table,
306 mpi_size_t n, mpi_size_t nents, mpi_size_t idx);
307 mpi_ptr_t _gcry_mpih_mod_lli (mpi_ptr_t vp, mpi_size_t vsize,
308 mpi_ptr_t up, mpi_size_t usize);
309
310
311 /* Define stuff for longlong.h. */
312 #define W_TYPE_SIZE BITS_PER_MPI_LIMB
313 typedef mpi_limb_t UWtype;
314 typedef unsigned int UHWtype;
315 #if defined (__GNUC__)
316 typedef unsigned int UQItype __attribute__ ((mode (QI)));
317 typedef int SItype __attribute__ ((mode (SI)));
318 typedef unsigned int USItype __attribute__ ((mode (SI)));
319 typedef int DItype __attribute__ ((mode (DI)));
320 typedef unsigned int UDItype __attribute__ ((mode (DI)));
321 #else
322 typedef unsigned char UQItype;
323 typedef long SItype;
324 typedef unsigned long USItype;
325 #endif
326
327 #ifdef __GNUC__
328 #include "mpi-inline.h"
329 #endif
330
331 #endif /*G10_MPI_INTERNAL_H*/