don't let BN_CTX_free(NULL) segfault
[openssl.git] / crypto / bn / bn_lcl.h
1 /* crypto/bn/bn_lcl.h */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 /* ====================================================================
59  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer. 
67  *
68  * 2. Redistributions in binary form must reproduce the above copyright
69  *    notice, this list of conditions and the following disclaimer in
70  *    the documentation and/or other materials provided with the
71  *    distribution.
72  *
73  * 3. All advertising materials mentioning features or use of this
74  *    software must display the following acknowledgment:
75  *    "This product includes software developed by the OpenSSL Project
76  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77  *
78  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79  *    endorse or promote products derived from this software without
80  *    prior written permission. For written permission, please contact
81  *    openssl-core@openssl.org.
82  *
83  * 5. Products derived from this software may not be called "OpenSSL"
84  *    nor may "OpenSSL" appear in their names without prior written
85  *    permission of the OpenSSL Project.
86  *
87  * 6. Redistributions of any form whatsoever must retain the following
88  *    acknowledgment:
89  *    "This product includes software developed by the OpenSSL Project
90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103  * OF THE POSSIBILITY OF SUCH DAMAGE.
104  * ====================================================================
105  *
106  * This product includes cryptographic software written by Eric Young
107  * (eay@cryptsoft.com).  This product includes software written by Tim
108  * Hudson (tjh@cryptsoft.com).
109  *
110  */
111
112 #ifndef HEADER_BN_LCL_H
113 #define HEADER_BN_LCL_H
114
115 #include <openssl/bn.h>
116
117 #ifdef  __cplusplus
118 extern "C" {
119 #endif
120
121
122 /*
123  * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions
124  *
125  *
126  * For window size 'w' (w >= 2) and a random 'b' bits exponent,
127  * the number of multiplications is a constant plus on average
128  *
129  *    2^(w-1) + (b-w)/(w+1);
130  *
131  * here  2^(w-1)  is for precomputing the table (we actually need
132  * entries only for windows that have the lowest bit set), and
133  * (b-w)/(w+1)  is an approximation for the expected number of
134  * w-bit windows, not counting the first one.
135  *
136  * Thus we should use
137  *
138  *    w >= 6  if        b > 671
139  *     w = 5  if  671 > b > 239
140  *     w = 4  if  239 > b >  79
141  *     w = 3  if   79 > b >  23
142  *    w <= 2  if   23 > b
143  *
144  * (with draws in between).  Very small exponents are often selected
145  * with low Hamming weight, so we use  w = 1  for b <= 23.
146  */
147 #if 1
148 #define BN_window_bits_for_exponent_size(b) \
149                 ((b) > 671 ? 6 : \
150                  (b) > 239 ? 5 : \
151                  (b) >  79 ? 4 : \
152                  (b) >  23 ? 3 : 1)
153 #else
154 /* Old SSLeay/OpenSSL table.
155  * Maximum window size was 5, so this table differs for b==1024;
156  * but it coincides for other interesting values (b==160, b==512).
157  */
158 #define BN_window_bits_for_exponent_size(b) \
159                 ((b) > 255 ? 5 : \
160                  (b) > 127 ? 4 : \
161                  (b) >  17 ? 3 : 1)
162 #endif   
163
164
165
166 /* Pentium pro 16,16,16,32,64 */
167 /* Alpha       16,16,16,16.64 */
168 #define BN_MULL_SIZE_NORMAL                     (16) /* 32 */
169 #define BN_MUL_RECURSIVE_SIZE_NORMAL            (16) /* 32 less than */
170 #define BN_SQR_RECURSIVE_SIZE_NORMAL            (16) /* 32 */
171 #define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL        (32) /* 32 */
172 #define BN_MONT_CTX_SET_SIZE_WORD               (64) /* 32 */
173
174 #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
175 /*
176  * BN_UMULT_HIGH section.
177  *
178  * No, I'm not trying to overwhelm you when stating that the
179  * product of N-bit numbers is 2*N bits wide:-) No, I don't expect
180  * you to be impressed when I say that if the compiler doesn't
181  * support 2*N integer type, then you have to replace every N*N
182  * multiplication with 4 (N/2)*(N/2) accompanied by some shifts
183  * and additions which unavoidably results in severe performance
184  * penalties. Of course provided that the hardware is capable of
185  * producing 2*N result... That's when you normally start
186  * considering assembler implementation. However! It should be
187  * pointed out that some CPUs (most notably Alpha, PowerPC and
188  * upcoming IA-64 family:-) provide *separate* instruction
189  * calculating the upper half of the product placing the result
190  * into a general purpose register. Now *if* the compiler supports
191  * inline assembler, then it's not impossible to implement the
192  * "bignum" routines (and have the compiler optimize 'em)
193  * exhibiting "native" performance in C. That's what BN_UMULT_HIGH
194  * macro is about:-)
195  *
196  *                                      <appro@fy.chalmers.se>
197  */
198 # if defined(__alpha) && (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT))
199 #  if defined(__DECC)
200 #   include <c_asm.h>
201 #   define BN_UMULT_HIGH(a,b)   (BN_ULONG)asm("umulh %a0,%a1,%v0",(a),(b))
202 #  elif defined(__GNUC__)
203 #   define BN_UMULT_HIGH(a,b)   ({      \
204         register BN_ULONG ret;          \
205         asm ("umulh     %1,%2,%0"       \
206              : "=r"(ret)                \
207              : "r"(a), "r"(b));         \
208         ret;                    })
209 #  endif        /* compiler */
210 # elif defined(_ARCH_PPC) && defined(__64BIT__) && defined(SIXTY_FOUR_BIT_LONG)
211 #  if defined(__GNUC__)
212 #   define BN_UMULT_HIGH(a,b)   ({      \
213         register BN_ULONG ret;          \
214         asm ("mulhdu    %0,%1,%2"       \
215              : "=r"(ret)                \
216              : "r"(a), "r"(b));         \
217         ret;                    })
218 #  endif        /* compiler */
219 # elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG)
220 #  if defined(__GNUC__)
221 #   define BN_UMULT_HIGH(a,b)   ({      \
222         register BN_ULONG ret,discard;  \
223         asm ("mulq      %3"             \
224              : "=a"(discard),"=d"(ret)  \
225              : "a"(a), "g"(b)           \
226              : "cc");                   \
227         ret;                    })
228 #   define BN_UMULT_LOHI(low,high,a,b)  \
229         asm ("mulq      %3"             \
230                 : "=a"(low),"=d"(high)  \
231                 : "a"(a),"g"(b)         \
232                 : "cc");
233 #  endif
234 # endif         /* cpu */
235 #endif          /* OPENSSL_NO_ASM */
236
237 /*************************************************************
238  * Using the long long type
239  */
240 #define Lw(t)    (((BN_ULONG)(t))&BN_MASK2)
241 #define Hw(t)    (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2)
242
243 #ifdef BN_DEBUG_RAND
244 #define bn_clear_top2max(a) \
245         { \
246         int      ind = (a)->dmax - (a)->top; \
247         BN_ULONG *ftl = &(a)->d[(a)->top-1]; \
248         for (; ind != 0; ind--) \
249                 *(++ftl) = 0x0; \
250         }
251 #else
252 #define bn_clear_top2max(a)
253 #endif
254
255 #ifdef BN_LLONG
256 #define mul_add(r,a,w,c) { \
257         BN_ULLONG t; \
258         t=(BN_ULLONG)w * (a) + (r) + (c); \
259         (r)= Lw(t); \
260         (c)= Hw(t); \
261         }
262
263 #define mul(r,a,w,c) { \
264         BN_ULLONG t; \
265         t=(BN_ULLONG)w * (a) + (c); \
266         (r)= Lw(t); \
267         (c)= Hw(t); \
268         }
269
270 #define sqr(r0,r1,a) { \
271         BN_ULLONG t; \
272         t=(BN_ULLONG)(a)*(a); \
273         (r0)=Lw(t); \
274         (r1)=Hw(t); \
275         }
276
277 #elif defined(BN_UMULT_HIGH)
278 #define mul_add(r,a,w,c) {              \
279         BN_ULONG high,low,ret,tmp=(a);  \
280         ret =  (r);                     \
281         high=  BN_UMULT_HIGH(w,tmp);    \
282         ret += (c);                     \
283         low =  (w) * tmp;               \
284         (c) =  (ret<(c))?1:0;           \
285         (c) += high;                    \
286         ret += low;                     \
287         (c) += (ret<low)?1:0;           \
288         (r) =  ret;                     \
289         }
290
291 #define mul(r,a,w,c)    {               \
292         BN_ULONG high,low,ret,ta=(a);   \
293         low =  (w) * ta;                \
294         high=  BN_UMULT_HIGH(w,ta);     \
295         ret =  low + (c);               \
296         (c) =  high;                    \
297         (c) += (ret<low)?1:0;           \
298         (r) =  ret;                     \
299         }
300
301 #define sqr(r0,r1,a)    {               \
302         BN_ULONG tmp=(a);               \
303         (r0) = tmp * tmp;               \
304         (r1) = BN_UMULT_HIGH(tmp,tmp);  \
305         }
306
307 #else
308 /*************************************************************
309  * No long long type
310  */
311
312 #define LBITS(a)        ((a)&BN_MASK2l)
313 #define HBITS(a)        (((a)>>BN_BITS4)&BN_MASK2l)
314 #define L2HBITS(a)      (((a)<<BN_BITS4)&BN_MASK2)
315
316 #define LLBITS(a)       ((a)&BN_MASKl)
317 #define LHBITS(a)       (((a)>>BN_BITS2)&BN_MASKl)
318 #define LL2HBITS(a)     ((BN_ULLONG)((a)&BN_MASKl)<<BN_BITS2)
319
320 #define mul64(l,h,bl,bh) \
321         { \
322         BN_ULONG m,m1,lt,ht; \
323  \
324         lt=l; \
325         ht=h; \
326         m =(bh)*(lt); \
327         lt=(bl)*(lt); \
328         m1=(bl)*(ht); \
329         ht =(bh)*(ht); \
330         m=(m+m1)&BN_MASK2; if (m < m1) ht+=L2HBITS((BN_ULONG)1); \
331         ht+=HBITS(m); \
332         m1=L2HBITS(m); \
333         lt=(lt+m1)&BN_MASK2; if (lt < m1) ht++; \
334         (l)=lt; \
335         (h)=ht; \
336         }
337
338 #define sqr64(lo,ho,in) \
339         { \
340         BN_ULONG l,h,m; \
341  \
342         h=(in); \
343         l=LBITS(h); \
344         h=HBITS(h); \
345         m =(l)*(h); \
346         l*=l; \
347         h*=h; \
348         h+=(m&BN_MASK2h1)>>(BN_BITS4-1); \
349         m =(m&BN_MASK2l)<<(BN_BITS4+1); \
350         l=(l+m)&BN_MASK2; if (l < m) h++; \
351         (lo)=l; \
352         (ho)=h; \
353         }
354
355 #define mul_add(r,a,bl,bh,c) { \
356         BN_ULONG l,h; \
357  \
358         h= (a); \
359         l=LBITS(h); \
360         h=HBITS(h); \
361         mul64(l,h,(bl),(bh)); \
362  \
363         /* non-multiply part */ \
364         l=(l+(c))&BN_MASK2; if (l < (c)) h++; \
365         (c)=(r); \
366         l=(l+(c))&BN_MASK2; if (l < (c)) h++; \
367         (c)=h&BN_MASK2; \
368         (r)=l; \
369         }
370
371 #define mul(r,a,bl,bh,c) { \
372         BN_ULONG l,h; \
373  \
374         h= (a); \
375         l=LBITS(h); \
376         h=HBITS(h); \
377         mul64(l,h,(bl),(bh)); \
378  \
379         /* non-multiply part */ \
380         l+=(c); if ((l&BN_MASK2) < (c)) h++; \
381         (c)=h&BN_MASK2; \
382         (r)=l&BN_MASK2; \
383         }
384 #endif /* !BN_LLONG */
385
386 void bn_mul_normal(BN_ULONG *r,BN_ULONG *a,int na,BN_ULONG *b,int nb);
387 void bn_mul_comba8(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b);
388 void bn_mul_comba4(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b);
389 void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp);
390 void bn_sqr_comba8(BN_ULONG *r,const BN_ULONG *a);
391 void bn_sqr_comba4(BN_ULONG *r,const BN_ULONG *a);
392 int bn_cmp_words(const BN_ULONG *a,const BN_ULONG *b,int n);
393 int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b,
394         int cl, int dl);
395 void bn_mul_recursive(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,int n2,
396         int dna,int dnb,BN_ULONG *t);
397 void bn_mul_part_recursive(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,
398         int n,int tna,int tnb,BN_ULONG *t);
399 void bn_sqr_recursive(BN_ULONG *r,const BN_ULONG *a, int n2, BN_ULONG *t);
400 void bn_mul_low_normal(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b, int n);
401 void bn_mul_low_recursive(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,int n2,
402         BN_ULONG *t);
403 void bn_mul_high(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,BN_ULONG *l,int n2,
404         BN_ULONG *t);
405 BN_ULONG bn_add_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
406         int cl, int dl);
407 BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
408         int cl, int dl);
409
410 #ifdef  __cplusplus
411 }
412 #endif
413
414 #endif