~ubuntu-branches/ubuntu/lucid/seamonkey/lucid-security

« back to all changes in this revision

Viewing changes to security/nss-fips/lib/freebl/mpi/mpi-priv.h

  • Committer: Bazaar Package Importer
  • Author(s): Fabien Tassin
  • Date: 2008-07-29 21:29:02 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080729212902-spm9kpvchp9udwbw
Tags: 1.1.11+nobinonly-0ubuntu1
* New security upstream release: 1.1.11 (LP: #218534)
  Fixes USN-602-1, USN-619-1, USN-623-1 and USN-629-1
* Refresh diverged patch:
  - update debian/patches/80_security_build.patch
* Fix FTBFS with missing -lfontconfig
  - add debian/patches/11_fix_ftbfs_with_fontconfig.patch
  - update debian/patches/series
* Build with default gcc (hardy: 4.2, intrepid: 4.3)
  - update debian/rules
  - update debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  mpi-priv.h  - Private header file for MPI 
 
3
 *  Arbitrary precision integer arithmetic library
 
4
 *
 
5
 *  NOTE WELL: the content of this header file is NOT part of the "public"
 
6
 *  API for the MPI library, and may change at any time.  
 
7
 *  Application programs that use libmpi should NOT include this header file.
 
8
 *
 
9
 * ***** BEGIN LICENSE BLOCK *****
 
10
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
11
 *
 
12
 * The contents of this file are subject to the Mozilla Public License Version
 
13
 * 1.1 (the "License"); you may not use this file except in compliance with
 
14
 * the License. You may obtain a copy of the License at
 
15
 * http://www.mozilla.org/MPL/
 
16
 *
 
17
 * Software distributed under the License is distributed on an "AS IS" basis,
 
18
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
19
 * for the specific language governing rights and limitations under the
 
20
 * License.
 
21
 *
 
22
 * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
 
23
 *
 
24
 * The Initial Developer of the Original Code is
 
25
 * Michael J. Fromberger.
 
26
 * Portions created by the Initial Developer are Copyright (C) 1998
 
27
 * the Initial Developer. All Rights Reserved.
 
28
 *
 
29
 * Contributor(s):
 
30
 *   Netscape Communications Corporation
 
31
 *
 
32
 * Alternatively, the contents of this file may be used under the terms of
 
33
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
34
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
35
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
36
 * of those above. If you wish to allow use of your version of this file only
 
37
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
38
 * use your version of this file under the terms of the MPL, indicate your
 
39
 * decision by deleting the provisions above and replace them with the notice
 
40
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
41
 * the provisions above, a recipient may use your version of this file under
 
42
 * the terms of any one of the MPL, the GPL or the LGPL.
 
43
 *
 
44
 * ***** END LICENSE BLOCK ***** */
 
45
/* $Id: mpi-priv.h,v 1.20 2005/11/22 07:16:43 relyea%netscape.com Exp $ */
 
46
#ifndef _MPI_PRIV_H_
 
47
#define _MPI_PRIV_H_ 1
 
48
 
 
49
#include "mpi.h"
 
50
#include <stdlib.h>
 
51
#include <string.h>
 
52
#include <ctype.h>
 
53
 
 
54
#if MP_DEBUG
 
55
#include <stdio.h>
 
56
 
 
57
#define DIAG(T,V) {fprintf(stderr,T);mp_print(V,stderr);fputc('\n',stderr);}
 
58
#else
 
59
#define DIAG(T,V)
 
60
#endif
 
61
 
 
62
/* If we aren't using a wired-in logarithm table, we need to include
 
63
   the math library to get the log() function
 
64
 */
 
65
 
 
66
/* {{{ s_logv_2[] - log table for 2 in various bases */
 
67
 
 
68
#if MP_LOGTAB
 
69
/*
 
70
  A table of the logs of 2 for various bases (the 0 and 1 entries of
 
71
  this table are meaningless and should not be referenced).  
 
72
 
 
73
  This table is used to compute output lengths for the mp_toradix()
 
74
  function.  Since a number n in radix r takes up about log_r(n)
 
75
  digits, we estimate the output size by taking the least integer
 
76
  greater than log_r(n), where:
 
77
 
 
78
  log_r(n) = log_2(n) * log_r(2)
 
79
 
 
80
  This table, therefore, is a table of log_r(2) for 2 <= r <= 36,
 
81
  which are the output bases supported.  
 
82
 */
 
83
 
 
84
extern const float s_logv_2[];
 
85
#define LOG_V_2(R)  s_logv_2[(R)]
 
86
 
 
87
#else
 
88
 
 
89
/* 
 
90
   If MP_LOGTAB is not defined, use the math library to compute the
 
91
   logarithms on the fly.  Otherwise, use the table.
 
92
   Pick which works best for your system.
 
93
 */
 
94
 
 
95
#include <math.h>
 
96
#define LOG_V_2(R)  (log(2.0)/log(R))
 
97
 
 
98
#endif /* if MP_LOGTAB */
 
99
 
 
100
/* }}} */
 
101
 
 
102
/* {{{ Digit arithmetic macros */
 
103
 
 
104
/*
 
105
  When adding and multiplying digits, the results can be larger than
 
106
  can be contained in an mp_digit.  Thus, an mp_word is used.  These
 
107
  macros mask off the upper and lower digits of the mp_word (the
 
108
  mp_word may be more than 2 mp_digits wide, but we only concern
 
109
  ourselves with the low-order 2 mp_digits)
 
110
 */
 
111
 
 
112
#define  CARRYOUT(W)  (mp_digit)((W)>>DIGIT_BIT)
 
113
#define  ACCUM(W)     (mp_digit)(W)
 
114
 
 
115
#define MP_MIN(a,b)   (((a) < (b)) ? (a) : (b))
 
116
#define MP_MAX(a,b)   (((a) > (b)) ? (a) : (b))
 
117
#define MP_HOWMANY(a,b) (((a) + (b) - 1)/(b))
 
118
#define MP_ROUNDUP(a,b) (MP_HOWMANY(a,b) * (b))
 
119
 
 
120
/* }}} */
 
121
 
 
122
/* {{{ Comparison constants */
 
123
 
 
124
#define  MP_LT       -1
 
125
#define  MP_EQ        0
 
126
#define  MP_GT        1
 
127
 
 
128
/* }}} */
 
129
 
 
130
/* {{{ private function declarations */
 
131
 
 
132
/* 
 
133
   If MP_MACRO is false, these will be defined as actual functions;
 
134
   otherwise, suitable macro definitions will be used.  This works
 
135
   around the fact that ANSI C89 doesn't support an 'inline' keyword
 
136
   (although I hear C9x will ... about bloody time).  At present, the
 
137
   macro definitions are identical to the function bodies, but they'll
 
138
   expand in place, instead of generating a function call.
 
139
 
 
140
   I chose these particular functions to be made into macros because
 
141
   some profiling showed they are called a lot on a typical workload,
 
142
   and yet they are primarily housekeeping.
 
143
 */
 
144
#if MP_MACRO == 0
 
145
 void     s_mp_setz(mp_digit *dp, mp_size count); /* zero digits           */
 
146
 void     s_mp_copy(const mp_digit *sp, mp_digit *dp, mp_size count); /* copy */
 
147
 void    *s_mp_alloc(size_t nb, size_t ni);       /* general allocator     */
 
148
 void     s_mp_free(void *ptr);                   /* general free function */
 
149
extern unsigned long mp_allocs;
 
150
extern unsigned long mp_frees;
 
151
extern unsigned long mp_copies;
 
152
#else
 
153
 
 
154
 /* Even if these are defined as macros, we need to respect the settings
 
155
    of the MP_MEMSET and MP_MEMCPY configuration options...
 
156
  */
 
157
 #if MP_MEMSET == 0
 
158
  #define  s_mp_setz(dp, count) \
 
159
       {int ix;for(ix=0;ix<(count);ix++)(dp)[ix]=0;}
 
160
 #else
 
161
  #define  s_mp_setz(dp, count) memset(dp, 0, (count) * sizeof(mp_digit))
 
162
 #endif /* MP_MEMSET */
 
163
 
 
164
 #if MP_MEMCPY == 0
 
165
  #define  s_mp_copy(sp, dp, count) \
 
166
       {int ix;for(ix=0;ix<(count);ix++)(dp)[ix]=(sp)[ix];}
 
167
 #else
 
168
  #define  s_mp_copy(sp, dp, count) memcpy(dp, sp, (count) * sizeof(mp_digit))
 
169
 #endif /* MP_MEMCPY */
 
170
 
 
171
 #define  s_mp_alloc(nb, ni)  calloc(nb, ni)
 
172
 #define  s_mp_free(ptr) {if(ptr) free(ptr);}
 
173
#endif /* MP_MACRO */
 
174
 
 
175
mp_err   s_mp_grow(mp_int *mp, mp_size min);   /* increase allocated size */
 
176
mp_err   s_mp_pad(mp_int *mp, mp_size min);    /* left pad with zeroes    */
 
177
 
 
178
#if MP_MACRO == 0
 
179
 void     s_mp_clamp(mp_int *mp);               /* clip leading zeroes     */
 
180
#else
 
181
 #define  s_mp_clamp(mp)\
 
182
  { mp_size used = MP_USED(mp); \
 
183
    while (used > 1 && DIGIT(mp, used - 1) == 0) --used; \
 
184
    MP_USED(mp) = used; \
 
185
  } 
 
186
#endif /* MP_MACRO */
 
187
 
 
188
void     s_mp_exch(mp_int *a, mp_int *b);      /* swap a and b in place   */
 
189
 
 
190
mp_err   s_mp_lshd(mp_int *mp, mp_size p);     /* left-shift by p digits  */
 
191
void     s_mp_rshd(mp_int *mp, mp_size p);     /* right-shift by p digits */
 
192
mp_err   s_mp_mul_2d(mp_int *mp, mp_digit d);  /* multiply by 2^d in place */
 
193
void     s_mp_div_2d(mp_int *mp, mp_digit d);  /* divide by 2^d in place  */
 
194
void     s_mp_mod_2d(mp_int *mp, mp_digit d);  /* modulo 2^d in place     */
 
195
void     s_mp_div_2(mp_int *mp);               /* divide by 2 in place    */
 
196
mp_err   s_mp_mul_2(mp_int *mp);               /* multiply by 2 in place  */
 
197
mp_err   s_mp_norm(mp_int *a, mp_int *b, mp_digit *pd); 
 
198
                                               /* normalize for division  */
 
199
mp_err   s_mp_add_d(mp_int *mp, mp_digit d);   /* unsigned digit addition */
 
200
mp_err   s_mp_sub_d(mp_int *mp, mp_digit d);   /* unsigned digit subtract */
 
201
mp_err   s_mp_mul_d(mp_int *mp, mp_digit d);   /* unsigned digit multiply */
 
202
mp_err   s_mp_div_d(mp_int *mp, mp_digit d, mp_digit *r);
 
203
                                               /* unsigned digit divide   */
 
204
mp_err   s_mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu);
 
205
                                               /* Barrett reduction       */
 
206
mp_err   s_mp_add(mp_int *a, const mp_int *b); /* magnitude addition      */
 
207
mp_err   s_mp_add_3arg(const mp_int *a, const mp_int *b, mp_int *c);
 
208
mp_err   s_mp_sub(mp_int *a, const mp_int *b); /* magnitude subtract      */
 
209
mp_err   s_mp_sub_3arg(const mp_int *a, const mp_int *b, mp_int *c);
 
210
mp_err   s_mp_add_offset(mp_int *a, mp_int *b, mp_size offset);
 
211
                                               /* a += b * RADIX^offset   */
 
212
mp_err   s_mp_mul(mp_int *a, const mp_int *b); /* magnitude multiply      */
 
213
#if MP_SQUARE
 
214
mp_err   s_mp_sqr(mp_int *a);                  /* magnitude square        */
 
215
#else
 
216
#define  s_mp_sqr(a) s_mp_mul(a, a)
 
217
#endif
 
218
mp_err   s_mp_div(mp_int *rem, mp_int *div, mp_int *quot); /* magnitude div */
 
219
mp_err   s_mp_exptmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c);
 
220
mp_err   s_mp_2expt(mp_int *a, mp_digit k);    /* a = 2^k                 */
 
221
int      s_mp_cmp(const mp_int *a, const mp_int *b); /* magnitude comparison */
 
222
int      s_mp_cmp_d(const mp_int *a, mp_digit d); /* magnitude digit compare */
 
223
int      s_mp_ispow2(const mp_int *v);         /* is v a power of 2?      */
 
224
int      s_mp_ispow2d(mp_digit d);             /* is d a power of 2?      */
 
225
 
 
226
int      s_mp_tovalue(char ch, int r);          /* convert ch to value    */
 
227
char     s_mp_todigit(mp_digit val, int r, int low); /* convert val to digit */
 
228
int      s_mp_outlen(int bits, int r);          /* output length in bytes */
 
229
mp_digit s_mp_invmod_radix(mp_digit P);   /* returns (P ** -1) mod RADIX */
 
230
mp_err   s_mp_invmod_odd_m( const mp_int *a, const mp_int *m, mp_int *c);
 
231
mp_err   s_mp_invmod_2d(    const mp_int *a, mp_size k,       mp_int *c);
 
232
mp_err   s_mp_invmod_even_m(const mp_int *a, const mp_int *m, mp_int *c);
 
233
 
 
234
#ifdef NSS_USE_COMBA
 
235
 
 
236
#define IS_POWER_OF_2(a) ((a) && !((a) & ((a)-1)))
 
237
 
 
238
void s_mp_mul_comba_4(const mp_int *A, const mp_int *B, mp_int *C);
 
239
void s_mp_mul_comba_8(const mp_int *A, const mp_int *B, mp_int *C);
 
240
void s_mp_mul_comba_16(const mp_int *A, const mp_int *B, mp_int *C);
 
241
void s_mp_mul_comba_32(const mp_int *A, const mp_int *B, mp_int *C);
 
242
 
 
243
void s_mp_sqr_comba_4(const mp_int *A, mp_int *B);
 
244
void s_mp_sqr_comba_8(const mp_int *A, mp_int *B);
 
245
void s_mp_sqr_comba_16(const mp_int *A, mp_int *B);
 
246
void s_mp_sqr_comba_32(const mp_int *A, mp_int *B);
 
247
 
 
248
#endif /* end NSS_USE_COMBA */
 
249
 
 
250
/* ------ mpv functions, operate on arrays of digits, not on mp_int's ------ */
 
251
#if defined (__OS2__) && defined (__IBMC__)
 
252
#define MPI_ASM_DECL __cdecl
 
253
#else
 
254
#define MPI_ASM_DECL
 
255
#endif
 
256
 
 
257
#ifdef MPI_AMD64
 
258
 
 
259
mp_digit MPI_ASM_DECL s_mpv_mul_set_vec64(mp_digit*, mp_digit *, mp_size, mp_digit);
 
260
mp_digit MPI_ASM_DECL s_mpv_mul_add_vec64(mp_digit*, const mp_digit*, mp_size, mp_digit);
 
261
 
 
262
/* c = a * b */
 
263
#define s_mpv_mul_d(a, a_len, b, c) \
 
264
        ((unsigned long*)c)[a_len] = s_mpv_mul_set_vec64(c, a, a_len, b)
 
265
 
 
266
/* c += a * b */
 
267
#define s_mpv_mul_d_add(a, a_len, b, c) \
 
268
        ((unsigned long*)c)[a_len] = s_mpv_mul_add_vec64(c, a, a_len, b)
 
269
 
 
270
#else
 
271
 
 
272
void     MPI_ASM_DECL s_mpv_mul_d(const mp_digit *a, mp_size a_len,
 
273
                                        mp_digit b, mp_digit *c);
 
274
void     MPI_ASM_DECL s_mpv_mul_d_add(const mp_digit *a, mp_size a_len,
 
275
                                            mp_digit b, mp_digit *c);
 
276
 
 
277
#endif
 
278
 
 
279
void     MPI_ASM_DECL s_mpv_mul_d_add_prop(const mp_digit *a,
 
280
                                                mp_size a_len, mp_digit b, 
 
281
                                                mp_digit *c);
 
282
void     MPI_ASM_DECL s_mpv_sqr_add_prop(const mp_digit *a,
 
283
                                                mp_size a_len,
 
284
                                                mp_digit *sqrs);
 
285
 
 
286
mp_err   MPI_ASM_DECL s_mpv_div_2dx1d(mp_digit Nhi, mp_digit Nlo,
 
287
                            mp_digit divisor, mp_digit *quot, mp_digit *rem);
 
288
 
 
289
/* c += a * b * (MP_RADIX ** offset);  */
 
290
#define s_mp_mul_d_add_offset(a, b, c, off) \
 
291
(s_mpv_mul_d_add_prop(MP_DIGITS(a), MP_USED(a), b, MP_DIGITS(c) + off), MP_OKAY)
 
292
 
 
293
typedef struct {
 
294
  mp_int       N;       /* modulus N */
 
295
  mp_digit     n0prime; /* n0' = - (n0 ** -1) mod MP_RADIX */
 
296
  mp_size      b;       /* R == 2 ** b,  also b = # significant bits in N */
 
297
} mp_mont_modulus;
 
298
 
 
299
mp_err s_mp_mul_mont(const mp_int *a, const mp_int *b, mp_int *c, 
 
300
                       mp_mont_modulus *mmm);
 
301
mp_err s_mp_redc(mp_int *T, mp_mont_modulus *mmm);
 
302
 
 
303
/*
 
304
 * s_mpi_getProcessorLineSize() returns the size in bytes of the cache line
 
305
 * if a cache exists, or zero if there is no cache. If more than one
 
306
 * cache line exists, it should return the smallest line size (which is
 
307
 * usually the L1 cache).
 
308
 *
 
309
 * mp_modexp uses this information to make sure that private key information
 
310
 * isn't being leaked through the cache.
 
311
 *
 
312
 * see mpcpucache.c for the implementation.
 
313
 */
 
314
unsigned long s_mpi_getProcessorLineSize();
 
315
 
 
316
/* }}} */
 
317
#endif
 
318