This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / crypto / mpi / mpi-internal.h
1 /* mpi-internal.h  -  Internal to the Multi Precision Integers
2  *      Copyright (C) 1994, 1996 Free Software Foundation, Inc.
3  *      Copyright (C) 1998, 2000 Free Software Foundation, Inc.
4  *
5  * This file is part of GnuPG.
6  *
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.
11  *
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.
16  *
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
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  *       The GNU MP Library itself is published under the LGPL;
27  *       however I decided to publish this code under the plain GPL.
28  */
29
30 #ifndef G10_MPI_INTERNAL_H
31 #define G10_MPI_INTERNAL_H
32
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/string.h>
36 #include <linux/crypto/mpi.h>
37 #include <asm/errno.h>
38
39 #define log_debug printk
40 #define log_bug printk
41
42 #define assert(x) do { \
43                if (!x) log_bug("failed assertion\n"); \
44                } while(0);
45
46 /* If KARATSUBA_THRESHOLD is not already defined, define it to a
47  * value which is good on most machines.  */
48
49 /* tested 4, 16, 32 and 64, where 16 gave the best performance when
50  * checking a 768 and a 1024 bit ElGamal signature.
51  * (wk 22.12.97) */
52 #ifndef KARATSUBA_THRESHOLD
53     #define KARATSUBA_THRESHOLD 16
54 #endif
55
56 /* The code can't handle KARATSUBA_THRESHOLD smaller than 2.  */
57 #if KARATSUBA_THRESHOLD < 2
58     #undef KARATSUBA_THRESHOLD
59     #define KARATSUBA_THRESHOLD 2
60 #endif
61
62
63 typedef mpi_limb_t *mpi_ptr_t; /* pointer to a limb */
64 typedef int mpi_size_t;        /* (must be a signed type) */
65
66 #define ABS(x) (x >= 0 ? x : -x)
67 #define MIN(l,o) ((l) < (o) ? (l) : (o))
68 #define MAX(h,i) ((h) > (i) ? (h) : (i))
69
70 static inline int RESIZE_IF_NEEDED(MPI a, unsigned b)
71 {
72         if (a->alloced < b)
73                 return mpi_resize(a,b);
74         return 0;
75 }
76
77 /* Copy N limbs from S to D.  */
78 #define MPN_COPY( d, s, n) \
79     do {                                \
80         mpi_size_t _i;                  \
81         for( _i = 0; _i < (n); _i++ )   \
82             (d)[_i] = (s)[_i];          \
83     } while(0)
84
85 #define MPN_COPY_INCR( d, s, n)         \
86     do {                                \
87         mpi_size_t _i;                  \
88         for( _i = 0; _i < (n); _i++ )   \
89             (d)[_i] = (d)[_i];          \
90     } while (0)
91
92 #define MPN_COPY_DECR( d, s, n ) \
93     do {                                \
94         mpi_size_t _i;                  \
95         for( _i = (n)-1; _i >= 0; _i--) \
96            (d)[_i] = (s)[_i];           \
97     } while(0)
98
99 /* Zero N limbs at D */
100 #define MPN_ZERO(d, n) \
101     do {                                  \
102         int  _i;                          \
103         for( _i = 0; _i < (n); _i++ )  \
104             (d)[_i] = 0;                    \
105     } while (0)
106
107 #define MPN_NORMALIZE(d, n)  \
108     do {                       \
109         while( (n) > 0 ) {     \
110             if( (d)[(n)-1] ) \
111                 break;         \
112             (n)--;             \
113         }                      \
114     } while(0)
115
116 #define MPN_NORMALIZE_NOT_ZERO(d, n) \
117     do {                                    \
118         for(;;) {                           \
119             if( (d)[(n)-1] )                \
120                 break;                      \
121             (n)--;                          \
122         }                                   \
123     } while(0)
124
125 #define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
126     do {                                                \
127         if( (size) < KARATSUBA_THRESHOLD )              \
128             mul_n_basecase (prodp, up, vp, size);       \
129         else                                            \
130             mul_n (prodp, up, vp, size, tspace);        \
131     } while (0);
132
133
134 /* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
135  * limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
136  * If this would yield overflow, DI should be the largest possible number
137  * (i.e., only ones).  For correct operation, the most significant bit of D
138  * has to be set.  Put the quotient in Q and the remainder in R.
139  */
140 #define UDIV_QRNND_PREINV(q, r, nh, nl, d, di) \
141     do {                                                            \
142         mpi_limb_t _q, _ql, _r;                                     \
143         mpi_limb_t _xh, _xl;                                        \
144         umul_ppmm (_q, _ql, (nh), (di));                            \
145         _q += (nh);     /* DI is 2**BITS_PER_MPI_LIMB too small */  \
146         umul_ppmm (_xh, _xl, _q, (d));                              \
147         sub_ddmmss (_xh, _r, (nh), (nl), _xh, _xl);                 \
148         if( _xh ) {                                                 \
149             sub_ddmmss (_xh, _r, _xh, _r, 0, (d));                  \
150             _q++;                                                   \
151             if( _xh) {                                              \
152                 sub_ddmmss (_xh, _r, _xh, _r, 0, (d));              \
153                 _q++;                                               \
154             }                                                       \
155         }                                                           \
156         if( _r >= (d) ) {                                           \
157             _r -= (d);                                              \
158             _q++;                                                   \
159         }                                                           \
160         (r) = _r;                                                   \
161         (q) = _q;                                                   \
162     } while (0)
163
164
165 /*-- mpiutil.c --*/
166 mpi_ptr_t mpi_alloc_limb_space( unsigned nlimbs );
167 void mpi_free_limb_space( mpi_ptr_t a );
168 void mpi_assign_limb_space( MPI a, mpi_ptr_t ap, unsigned nlimbs );
169
170 /*-- mpi-bit.c --*/
171 void mpi_rshift_limbs( MPI a, unsigned int count );
172 int mpi_lshift_limbs( MPI a, unsigned int count );
173
174
175 /*-- mpihelp-add.c --*/
176 mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr,  mpi_ptr_t s1_ptr,
177                          mpi_size_t s1_size, mpi_limb_t s2_limb );
178 mpi_limb_t mpihelp_add_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
179                           mpi_ptr_t s2_ptr,  mpi_size_t size);
180 mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
181                        mpi_ptr_t s2_ptr, mpi_size_t s2_size);
182
183 /*-- mpihelp-sub.c --*/
184 mpi_limb_t mpihelp_sub_1( mpi_ptr_t res_ptr,  mpi_ptr_t s1_ptr,
185                           mpi_size_t s1_size, mpi_limb_t s2_limb );
186 mpi_limb_t mpihelp_sub_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
187                           mpi_ptr_t s2_ptr, mpi_size_t size);
188 mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
189                        mpi_ptr_t s2_ptr, mpi_size_t s2_size);
190
191 /*-- mpihelp-cmp.c --*/
192 int mpihelp_cmp( mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size );
193
194 /*-- mpihelp-mul.c --*/
195
196 struct karatsuba_ctx {
197     struct karatsuba_ctx *next;
198     mpi_ptr_t tspace;
199     mpi_size_t tspace_size;
200     mpi_ptr_t tp;
201     mpi_size_t tp_size;
202 };
203
204 void mpihelp_release_karatsuba_ctx( struct karatsuba_ctx *ctx );
205
206 mpi_limb_t mpihelp_addmul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
207                              mpi_size_t s1_size, mpi_limb_t s2_limb);
208 mpi_limb_t mpihelp_submul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
209                              mpi_size_t s1_size, mpi_limb_t s2_limb);
210 int mpihelp_mul_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp,
211                                                    mpi_size_t size);
212 int mpihelp_mul( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
213                  mpi_ptr_t vp, mpi_size_t vsize, mpi_limb_t *_result);
214 void mpih_sqr_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size );
215 void mpih_sqr_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
216                                                 mpi_ptr_t tspace);
217
218 int mpihelp_mul_karatsuba_case( mpi_ptr_t prodp,
219                                  mpi_ptr_t up, mpi_size_t usize,
220                                  mpi_ptr_t vp, mpi_size_t vsize,
221                                  struct karatsuba_ctx *ctx );
222
223
224 /*-- mpihelp-mul_1.c (or xxx/cpu/ *.S) --*/
225 mpi_limb_t mpihelp_mul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
226                           mpi_size_t s1_size, mpi_limb_t s2_limb);
227
228 /*-- mpihelp-div.c --*/
229 mpi_limb_t mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
230                                                  mpi_limb_t divisor_limb);
231 mpi_limb_t mpihelp_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
232                            mpi_ptr_t np, mpi_size_t nsize,
233                            mpi_ptr_t dp, mpi_size_t dsize);
234 mpi_limb_t mpihelp_divmod_1( mpi_ptr_t quot_ptr,
235                              mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
236                              mpi_limb_t divisor_limb);
237
238 /*-- mpihelp-shift.c --*/
239 mpi_limb_t mpihelp_lshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
240                                                            unsigned cnt);
241 mpi_limb_t mpihelp_rshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
242                                                            unsigned cnt);
243
244
245 /* Define stuff for longlong.h.  */
246 #define W_TYPE_SIZE BITS_PER_MPI_LIMB
247   typedef mpi_limb_t   UWtype;
248   typedef unsigned int UHWtype;
249 #if defined (__GNUC__)
250   typedef unsigned int UQItype    __attribute__ ((mode (QI)));
251   typedef          int SItype     __attribute__ ((mode (SI)));
252   typedef unsigned int USItype    __attribute__ ((mode (SI)));
253   typedef          int DItype     __attribute__ ((mode (DI)));
254   typedef unsigned int UDItype    __attribute__ ((mode (DI)));
255 #else
256   typedef unsigned char UQItype;
257   typedef          long SItype;
258   typedef unsigned long USItype;
259 #endif
260
261 #ifdef __GNUC__
262   #include "mpi-inline.h"
263 #endif
264
265 #endif /*G10_MPI_INTERNAL_H*/