~ubuntu-branches/ubuntu/trusty/grub2/trusty

« back to all changes in this revision

Viewing changes to grub-core/lib/libgcrypt-grub/mpi/mpi-internal.h

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2014-01-16 15:18:04 UTC
  • mfrom: (17.6.38 experimental)
  • Revision ID: package-import@ubuntu.com-20140116151804-3foouk7fpqcq3sxx
Tags: 2.02~beta2-2
* Convert patch handling to git-dpm.
* Add bi-endian support to ELF parser (Tomohiro B Berry).
* Adjust restore_mkdevicemap.patch to mark get_kfreebsd_version as static,
  to appease "gcc -Werror=missing-prototypes".
* Cherry-pick from upstream:
  - Change grub-macbless' manual page section to 8.
* Install grub-glue-efi, grub-macbless, grub-render-label, and
  grub-syslinux2cfg.
* grub-shell: Pass -no-pad to xorriso when building floppy images.

Show diffs side-by-side

added added

removed removed

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