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

« back to all changes in this revision

Viewing changes to security/nss-fips/lib/freebl/ecl/ec2_233.c

  • 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
 * ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Mozilla Public License Version
 
6
 * 1.1 (the "License"); you may not use this file except in compliance with
 
7
 * the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/MPL/
 
9
 *
 
10
 * Software distributed under the License is distributed on an "AS IS" basis,
 
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
12
 * for the specific language governing rights and limitations under the
 
13
 * License.
 
14
 *
 
15
 * The Original Code is the elliptic curve math library for binary polynomial field curves.
 
16
 *
 
17
 * The Initial Developer of the Original Code is
 
18
 * Sun Microsystems, Inc.
 
19
 * Portions created by the Initial Developer are Copyright (C) 2003
 
20
 * the Initial Developer. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *   Sheueling Chang-Shantz <sheueling.chang@sun.com>,
 
24
 *   Stephen Fung <fungstep@hotmail.com>, and
 
25
 *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories.
 
26
 *
 
27
 * Alternatively, the contents of this file may be used under the terms of
 
28
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
29
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
30
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
31
 * of those above. If you wish to allow use of your version of this file only
 
32
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
33
 * use your version of this file under the terms of the MPL, indicate your
 
34
 * decision by deleting the provisions above and replace them with the notice
 
35
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
36
 * the provisions above, a recipient may use your version of this file under
 
37
 * the terms of any one of the MPL, the GPL or the LGPL.
 
38
 *
 
39
 * ***** END LICENSE BLOCK ***** */
 
40
 
 
41
#include "ec2.h"
 
42
#include "mp_gf2m.h"
 
43
#include "mp_gf2m-priv.h"
 
44
#include "mpi.h"
 
45
#include "mpi-priv.h"
 
46
#include <stdlib.h>
 
47
 
 
48
/* Fast reduction for polynomials over a 233-bit curve. Assumes reduction
 
49
 * polynomial with terms {233, 74, 0}. */
 
50
mp_err
 
51
ec_GF2m_233_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
 
52
{
 
53
        mp_err res = MP_OKAY;
 
54
        mp_digit *u, z;
 
55
 
 
56
        if (a != r) {
 
57
                MP_CHECKOK(mp_copy(a, r));
 
58
        }
 
59
#ifdef ECL_SIXTY_FOUR_BIT
 
60
        if (MP_USED(r) < 8) {
 
61
                MP_CHECKOK(s_mp_pad(r, 8));
 
62
        }
 
63
        u = MP_DIGITS(r);
 
64
        MP_USED(r) = 8;
 
65
 
 
66
        /* u[7] only has 18 significant bits */
 
67
        z = u[7];
 
68
        u[4] ^= (z << 33) ^ (z >> 41);
 
69
        u[3] ^= (z << 23);
 
70
        z = u[6];
 
71
        u[4] ^= (z >> 31);
 
72
        u[3] ^= (z << 33) ^ (z >> 41);
 
73
        u[2] ^= (z << 23);
 
74
        z = u[5];
 
75
        u[3] ^= (z >> 31);
 
76
        u[2] ^= (z << 33) ^ (z >> 41);
 
77
        u[1] ^= (z << 23);
 
78
        z = u[4];
 
79
        u[2] ^= (z >> 31);
 
80
        u[1] ^= (z << 33) ^ (z >> 41);
 
81
        u[0] ^= (z << 23);
 
82
        z = u[3] >> 41;                         /* z only has 23 significant bits */
 
83
        u[1] ^= (z << 10);
 
84
        u[0] ^= z;
 
85
        /* clear bits above 233 */
 
86
        u[7] = u[6] = u[5] = u[4] = 0;
 
87
        u[3] ^= z << 41;
 
88
#else
 
89
        if (MP_USED(r) < 15) {
 
90
                MP_CHECKOK(s_mp_pad(r, 15));
 
91
        }
 
92
        u = MP_DIGITS(r);
 
93
        MP_USED(r) = 15;
 
94
 
 
95
        /* u[14] only has 18 significant bits */
 
96
        z = u[14];
 
97
        u[9] ^= (z << 1);
 
98
        u[7] ^= (z >> 9);
 
99
        u[6] ^= (z << 23);
 
100
        z = u[13];
 
101
        u[9] ^= (z >> 31);
 
102
        u[8] ^= (z << 1);
 
103
        u[6] ^= (z >> 9);
 
104
        u[5] ^= (z << 23);
 
105
        z = u[12];
 
106
        u[8] ^= (z >> 31);
 
107
        u[7] ^= (z << 1);
 
108
        u[5] ^= (z >> 9);
 
109
        u[4] ^= (z << 23);
 
110
        z = u[11];
 
111
        u[7] ^= (z >> 31);
 
112
        u[6] ^= (z << 1);
 
113
        u[4] ^= (z >> 9);
 
114
        u[3] ^= (z << 23);
 
115
        z = u[10];
 
116
        u[6] ^= (z >> 31);
 
117
        u[5] ^= (z << 1);
 
118
        u[3] ^= (z >> 9);
 
119
        u[2] ^= (z << 23);
 
120
        z = u[9];
 
121
        u[5] ^= (z >> 31);
 
122
        u[4] ^= (z << 1);
 
123
        u[2] ^= (z >> 9);
 
124
        u[1] ^= (z << 23);
 
125
        z = u[8];
 
126
        u[4] ^= (z >> 31);
 
127
        u[3] ^= (z << 1);
 
128
        u[1] ^= (z >> 9);
 
129
        u[0] ^= (z << 23);
 
130
        z = u[7] >> 9;                          /* z only has 23 significant bits */
 
131
        u[3] ^= (z >> 22);
 
132
        u[2] ^= (z << 10);
 
133
        u[0] ^= z;
 
134
        /* clear bits above 233 */
 
135
        u[14] = u[13] = u[12] = u[11] = u[10] = u[9] = u[8] = 0;
 
136
        u[7] ^= z << 9;
 
137
#endif
 
138
        s_mp_clamp(r);
 
139
 
 
140
  CLEANUP:
 
141
        return res;
 
142
}
 
143
 
 
144
/* Fast squaring for polynomials over a 233-bit curve. Assumes reduction
 
145
 * polynomial with terms {233, 74, 0}. */
 
146
mp_err
 
147
ec_GF2m_233_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
 
148
{
 
149
        mp_err res = MP_OKAY;
 
150
        mp_digit *u, *v;
 
151
 
 
152
        v = MP_DIGITS(a);
 
153
 
 
154
#ifdef ECL_SIXTY_FOUR_BIT
 
155
        if (MP_USED(a) < 4) {
 
156
                return mp_bsqrmod(a, meth->irr_arr, r);
 
157
        }
 
158
        if (MP_USED(r) < 8) {
 
159
                MP_CHECKOK(s_mp_pad(r, 8));
 
160
        }
 
161
        MP_USED(r) = 8;
 
162
#else
 
163
        if (MP_USED(a) < 8) {
 
164
                return mp_bsqrmod(a, meth->irr_arr, r);
 
165
        }
 
166
        if (MP_USED(r) < 15) {
 
167
                MP_CHECKOK(s_mp_pad(r, 15));
 
168
        }
 
169
        MP_USED(r) = 15;
 
170
#endif
 
171
        u = MP_DIGITS(r);
 
172
 
 
173
#ifdef ECL_THIRTY_TWO_BIT
 
174
        u[14] = gf2m_SQR0(v[7]);
 
175
        u[13] = gf2m_SQR1(v[6]);
 
176
        u[12] = gf2m_SQR0(v[6]);
 
177
        u[11] = gf2m_SQR1(v[5]);
 
178
        u[10] = gf2m_SQR0(v[5]);
 
179
        u[9] = gf2m_SQR1(v[4]);
 
180
        u[8] = gf2m_SQR0(v[4]);
 
181
#endif
 
182
        u[7] = gf2m_SQR1(v[3]);
 
183
        u[6] = gf2m_SQR0(v[3]);
 
184
        u[5] = gf2m_SQR1(v[2]);
 
185
        u[4] = gf2m_SQR0(v[2]);
 
186
        u[3] = gf2m_SQR1(v[1]);
 
187
        u[2] = gf2m_SQR0(v[1]);
 
188
        u[1] = gf2m_SQR1(v[0]);
 
189
        u[0] = gf2m_SQR0(v[0]);
 
190
        return ec_GF2m_233_mod(r, r, meth);
 
191
 
 
192
  CLEANUP:
 
193
        return res;
 
194
}
 
195
 
 
196
/* Fast multiplication for polynomials over a 233-bit curve. Assumes
 
197
 * reduction polynomial with terms {233, 74, 0}. */
 
198
mp_err
 
199
ec_GF2m_233_mul(const mp_int *a, const mp_int *b, mp_int *r,
 
200
                                const GFMethod *meth)
 
201
{
 
202
        mp_err res = MP_OKAY;
 
203
        mp_digit a3 = 0, a2 = 0, a1 = 0, a0, b3 = 0, b2 = 0, b1 = 0, b0;
 
204
 
 
205
#ifdef ECL_THIRTY_TWO_BIT
 
206
        mp_digit a7 = 0, a6 = 0, a5 = 0, a4 = 0, b7 = 0, b6 = 0, b5 = 0, b4 =
 
207
                0;
 
208
        mp_digit rm[8];
 
209
#endif
 
210
 
 
211
        if (a == b) {
 
212
                return ec_GF2m_233_sqr(a, r, meth);
 
213
        } else {
 
214
                switch (MP_USED(a)) {
 
215
#ifdef ECL_THIRTY_TWO_BIT
 
216
                case 8:
 
217
                        a7 = MP_DIGIT(a, 7);
 
218
                case 7:
 
219
                        a6 = MP_DIGIT(a, 6);
 
220
                case 6:
 
221
                        a5 = MP_DIGIT(a, 5);
 
222
                case 5:
 
223
                        a4 = MP_DIGIT(a, 4);
 
224
#endif
 
225
                case 4:
 
226
                        a3 = MP_DIGIT(a, 3);
 
227
                case 3:
 
228
                        a2 = MP_DIGIT(a, 2);
 
229
                case 2:
 
230
                        a1 = MP_DIGIT(a, 1);
 
231
                default:
 
232
                        a0 = MP_DIGIT(a, 0);
 
233
                }
 
234
                switch (MP_USED(b)) {
 
235
#ifdef ECL_THIRTY_TWO_BIT
 
236
                case 8:
 
237
                        b7 = MP_DIGIT(b, 7);
 
238
                case 7:
 
239
                        b6 = MP_DIGIT(b, 6);
 
240
                case 6:
 
241
                        b5 = MP_DIGIT(b, 5);
 
242
                case 5:
 
243
                        b4 = MP_DIGIT(b, 4);
 
244
#endif
 
245
                case 4:
 
246
                        b3 = MP_DIGIT(b, 3);
 
247
                case 3:
 
248
                        b2 = MP_DIGIT(b, 2);
 
249
                case 2:
 
250
                        b1 = MP_DIGIT(b, 1);
 
251
                default:
 
252
                        b0 = MP_DIGIT(b, 0);
 
253
                }
 
254
#ifdef ECL_SIXTY_FOUR_BIT
 
255
                MP_CHECKOK(s_mp_pad(r, 8));
 
256
                s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0);
 
257
                MP_USED(r) = 8;
 
258
                s_mp_clamp(r);
 
259
#else
 
260
                MP_CHECKOK(s_mp_pad(r, 16));
 
261
                s_bmul_4x4(MP_DIGITS(r) + 8, a7, a6, a5, a4, b7, b6, b5, b4);
 
262
                s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0);
 
263
                s_bmul_4x4(rm, a7 ^ a3, a6 ^ a2, a5 ^ a1, a4 ^ a0, b7 ^ b3,
 
264
                                   b6 ^ b2, b5 ^ b1, b4 ^ b0);
 
265
                rm[7] ^= MP_DIGIT(r, 7) ^ MP_DIGIT(r, 15);
 
266
                rm[6] ^= MP_DIGIT(r, 6) ^ MP_DIGIT(r, 14);
 
267
                rm[5] ^= MP_DIGIT(r, 5) ^ MP_DIGIT(r, 13);
 
268
                rm[4] ^= MP_DIGIT(r, 4) ^ MP_DIGIT(r, 12);
 
269
                rm[3] ^= MP_DIGIT(r, 3) ^ MP_DIGIT(r, 11);
 
270
                rm[2] ^= MP_DIGIT(r, 2) ^ MP_DIGIT(r, 10);
 
271
                rm[1] ^= MP_DIGIT(r, 1) ^ MP_DIGIT(r, 9);
 
272
                rm[0] ^= MP_DIGIT(r, 0) ^ MP_DIGIT(r, 8);
 
273
                MP_DIGIT(r, 11) ^= rm[7];
 
274
                MP_DIGIT(r, 10) ^= rm[6];
 
275
                MP_DIGIT(r, 9) ^= rm[5];
 
276
                MP_DIGIT(r, 8) ^= rm[4];
 
277
                MP_DIGIT(r, 7) ^= rm[3];
 
278
                MP_DIGIT(r, 6) ^= rm[2];
 
279
                MP_DIGIT(r, 5) ^= rm[1];
 
280
                MP_DIGIT(r, 4) ^= rm[0];
 
281
                MP_USED(r) = 16;
 
282
                s_mp_clamp(r);
 
283
#endif
 
284
                return ec_GF2m_233_mod(r, r, meth);
 
285
        }
 
286
 
 
287
  CLEANUP:
 
288
        return res;
 
289
}
 
290
 
 
291
/* Wire in fast field arithmetic for 233-bit curves. */
 
292
mp_err
 
293
ec_group_set_gf2m233(ECGroup *group, ECCurveName name)
 
294
{
 
295
        group->meth->field_mod = &ec_GF2m_233_mod;
 
296
        group->meth->field_mul = &ec_GF2m_233_mul;
 
297
        group->meth->field_sqr = &ec_GF2m_233_sqr;
 
298
        return MP_OKAY;
 
299
}