~ubuntu-branches/ubuntu/trusty/xulrunner/trusty

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2008-08-25 13:04:18 UTC
  • mfrom: (1.1.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20080825130418-ck1i2ms384tzb9m0
Tags: 1.8.1.16+nobinonly-0ubuntu1
* New upstream release (taken from upstream CVS), LP: #254618.
* Fix MFSA 2008-35, MFSA 2008-34, MFSA 2008-33, MFSA 2008-32, MFSA 2008-31,
  MFSA 2008-30, MFSA 2008-29, MFSA 2008-28, MFSA 2008-27, MFSA 2008-25,
  MFSA 2008-24, MFSA 2008-23, MFSA 2008-22, MFSA 2008-21, MFSA 2008-26 also
  known as CVE-2008-2933, CVE-2008-2785, CVE-2008-2811, CVE-2008-2810,
  CVE-2008-2809, CVE-2008-2808, CVE-2008-2807, CVE-2008-2806, CVE-2008-2805,
  CVE-2008-2803, CVE-2008-2802, CVE-2008-2801, CVE-2008-2800, CVE-2008-2798.
* Drop 89_bz419350_attachment_306066 patch, merged upstream.
* Bump Standards-Version to 3.8.0.

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 prime 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
 *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
 
24
 *
 
25
 * Alternatively, the contents of this file may be used under the terms of
 
26
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
27
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
28
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
29
 * of those above. If you wish to allow use of your version of this file only
 
30
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
31
 * use your version of this file under the terms of the MPL, indicate your
 
32
 * decision by deleting the provisions above and replace them with the notice
 
33
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
34
 * the provisions above, a recipient may use your version of this file under
 
35
 * the terms of any one of the MPL, the GPL or the LGPL.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
#include "ecp.h"
 
40
#include "mpi.h"
 
41
#include "mplogic.h"
 
42
#include "mpi-priv.h"
 
43
#include <stdlib.h>
 
44
 
 
45
#define ECP224_DIGITS ECL_CURVE_DIGITS(224)
 
46
 
 
47
/* Fast modular reduction for p224 = 2^224 - 2^96 + 1.  a can be r. Uses
 
48
 * algorithm 7 from Brown, Hankerson, Lopez, Menezes. Software
 
49
 * Implementation of the NIST Elliptic Curves over Prime Fields. */
 
50
mp_err
 
51
ec_GFp_nistp224_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
 
52
{
 
53
        mp_err res = MP_OKAY;
 
54
        mp_size a_used = MP_USED(a);
 
55
 
 
56
        int    r3b;
 
57
        mp_digit carry;
 
58
#ifdef ECL_THIRTY_TWO_BIT
 
59
        mp_digit a6a = 0, a6b = 0,
 
60
                a5a = 0, a5b = 0, a4a = 0, a4b = 0, a3a = 0, a3b = 0;
 
61
        mp_digit r0a, r0b, r1a, r1b, r2a, r2b, r3a;
 
62
#else
 
63
        mp_digit a6 = 0, a5 = 0, a4 = 0, a3b = 0, a5a = 0;
 
64
        mp_digit a6b = 0, a6a_a5b = 0, a5b = 0, a5a_a4b = 0, a4a_a3b = 0;
 
65
        mp_digit r0, r1, r2, r3;
 
66
#endif
 
67
 
 
68
        /* reduction not needed if a is not larger than field size */
 
69
        if (a_used < ECP224_DIGITS) {
 
70
                if (a == r) return MP_OKAY;
 
71
                return mp_copy(a, r);
 
72
        }
 
73
        /* for polynomials larger than twice the field size, use regular
 
74
         * reduction */
 
75
        if (a_used > ECL_CURVE_DIGITS(224*2)) {
 
76
                MP_CHECKOK(mp_mod(a, &meth->irr, r));
 
77
        } else {
 
78
#ifdef ECL_THIRTY_TWO_BIT
 
79
                /* copy out upper words of a */
 
80
                switch (a_used) {
 
81
                case 14:
 
82
                        a6b = MP_DIGIT(a, 13);
 
83
                case 13:
 
84
                        a6a = MP_DIGIT(a, 12);
 
85
                case 12:
 
86
                        a5b = MP_DIGIT(a, 11);
 
87
                case 11:
 
88
                        a5a = MP_DIGIT(a, 10);
 
89
                case 10:
 
90
                        a4b = MP_DIGIT(a, 9);
 
91
                case 9:
 
92
                        a4a = MP_DIGIT(a, 8);
 
93
                case 8:
 
94
                        a3b = MP_DIGIT(a, 7);
 
95
                }
 
96
                r3a = MP_DIGIT(a, 6);
 
97
                r2b= MP_DIGIT(a, 5);
 
98
                r2a= MP_DIGIT(a, 4);
 
99
                r1b = MP_DIGIT(a, 3);
 
100
                r1a = MP_DIGIT(a, 2);
 
101
                r0b = MP_DIGIT(a, 1);
 
102
                r0a = MP_DIGIT(a, 0);
 
103
 
 
104
 
 
105
                /* implement r = (a3a,a2,a1,a0)
 
106
                        +(a5a, a4,a3b,  0)
 
107
                        +(  0, a6,a5b,  0)
 
108
                        -(  0    0,    0|a6b, a6a|a5b )
 
109
                        -(  a6b, a6a|a5b, a5a|a4b, a4a|a3b ) */
 
110
                MP_ADD_CARRY (r1b, a3b, r1b, 0,     carry);
 
111
                MP_ADD_CARRY (r2a, a4a, r2a, carry, carry);
 
112
                MP_ADD_CARRY (r2b, a4b, r2b, carry, carry);
 
113
                MP_ADD_CARRY (r3a, a5a, r3a, carry, carry);
 
114
                r3b = carry;
 
115
                MP_ADD_CARRY (r1b, a5b, r1b, 0,     carry);
 
116
                MP_ADD_CARRY (r2a, a6a, r2a, carry, carry);
 
117
                MP_ADD_CARRY (r2b, a6b, r2b, carry, carry);
 
118
                MP_ADD_CARRY (r3a,   0, r3a, carry, carry);
 
119
                r3b += carry;
 
120
                MP_SUB_BORROW(r0a, a3b, r0a, 0,     carry);
 
121
                MP_SUB_BORROW(r0b, a4a, r0b, carry, carry);
 
122
                MP_SUB_BORROW(r1a, a4b, r1a, carry, carry);
 
123
                MP_SUB_BORROW(r1b, a5a, r1b, carry, carry);
 
124
                MP_SUB_BORROW(r2a, a5b, r2a, carry, carry);
 
125
                MP_SUB_BORROW(r2b, a6a, r2b, carry, carry);
 
126
                MP_SUB_BORROW(r3a, a6b, r3a, carry, carry);
 
127
                r3b -= carry;
 
128
                MP_SUB_BORROW(r0a, a5b, r0a, 0,     carry);
 
129
                MP_SUB_BORROW(r0b, a6a, r0b, carry, carry);
 
130
                MP_SUB_BORROW(r1a, a6b, r1a, carry, carry);
 
131
                if (carry) {
 
132
                        MP_SUB_BORROW(r1b, 0, r1b, carry, carry);
 
133
                        MP_SUB_BORROW(r2a, 0, r2a, carry, carry);
 
134
                        MP_SUB_BORROW(r2b, 0, r2b, carry, carry);
 
135
                        MP_SUB_BORROW(r3a, 0, r3a, carry, carry);
 
136
                        r3b -= carry;
 
137
                }
 
138
 
 
139
                while (r3b > 0) {
 
140
                        int tmp;
 
141
                        MP_ADD_CARRY(r1b, r3b, r1b, 0,     carry);
 
142
                        if (carry) {
 
143
                                MP_ADD_CARRY(r2a,  0, r2a, carry, carry);
 
144
                                MP_ADD_CARRY(r2b,  0, r2b, carry, carry);
 
145
                                MP_ADD_CARRY(r3a,  0, r3a, carry, carry);
 
146
                        }
 
147
                        tmp = carry;
 
148
                        MP_SUB_BORROW(r0a, r3b, r0a, 0,     carry);
 
149
                        if (carry) {
 
150
                                MP_SUB_BORROW(r0b, 0, r0b, carry, carry);
 
151
                                MP_SUB_BORROW(r1a, 0, r1a, carry, carry);
 
152
                                MP_SUB_BORROW(r1b, 0, r1b, carry, carry);
 
153
                                MP_SUB_BORROW(r2a, 0, r2a, carry, carry);
 
154
                                MP_SUB_BORROW(r2b, 0, r2b, carry, carry);
 
155
                                MP_SUB_BORROW(r3a, 0, r3a, carry, carry);
 
156
                                tmp -= carry;
 
157
                        }
 
158
                        r3b = tmp;
 
159
                }
 
160
 
 
161
                while (r3b < 0) {
 
162
                        mp_digit maxInt = MP_DIGIT_MAX;
 
163
                        MP_ADD_CARRY (r0a, 1, r0a, 0,     carry);
 
164
                        MP_ADD_CARRY (r0b, 0, r0b, carry, carry);
 
165
                        MP_ADD_CARRY (r1a, 0, r1a, carry, carry);
 
166
                        MP_ADD_CARRY (r1b, maxInt, r1b, carry, carry);
 
167
                        MP_ADD_CARRY (r2a, maxInt, r2a, carry, carry);
 
168
                        MP_ADD_CARRY (r2b, maxInt, r2b, carry, carry);
 
169
                        MP_ADD_CARRY (r3a, maxInt, r3a, carry, carry);
 
170
                        r3b += carry;
 
171
                }
 
172
                /* check for final reduction */
 
173
                /* now the only way we are over is if the top 4 words are all ones */
 
174
                if ((r3a == MP_DIGIT_MAX) && (r2b == MP_DIGIT_MAX)
 
175
                        && (r2a == MP_DIGIT_MAX) && (r1b == MP_DIGIT_MAX) &&
 
176
                         ((r1a != 0) || (r0b != 0) || (r0a != 0)) ) {
 
177
                        /* one last subraction */
 
178
                        MP_SUB_BORROW(r0a, 1, r0a, 0,     carry);
 
179
                        MP_SUB_BORROW(r0b, 0, r0b, carry, carry);
 
180
                        MP_SUB_BORROW(r1a, 0, r1a, carry, carry);
 
181
                        r1b = r2a = r2b = r3a = 0;
 
182
                }
 
183
 
 
184
 
 
185
                if (a != r) {
 
186
                        MP_CHECKOK(s_mp_pad(r, 7));
 
187
                }
 
188
                /* set the lower words of r */
 
189
                MP_SIGN(r) = MP_ZPOS;
 
190
                MP_USED(r) = 7;
 
191
                MP_DIGIT(r, 6) = r3a;
 
192
                MP_DIGIT(r, 5) = r2b;
 
193
                MP_DIGIT(r, 4) = r2a;
 
194
                MP_DIGIT(r, 3) = r1b;
 
195
                MP_DIGIT(r, 2) = r1a;
 
196
                MP_DIGIT(r, 1) = r0b;
 
197
                MP_DIGIT(r, 0) = r0a;
 
198
#else
 
199
                /* copy out upper words of a */
 
200
                switch (a_used) {
 
201
                case 7:
 
202
                        a6 = MP_DIGIT(a, 6);
 
203
                        a6b = a6 >> 32;
 
204
                        a6a_a5b = a6 << 32;
 
205
                case 6:
 
206
                        a5 = MP_DIGIT(a, 5);
 
207
                        a5b = a5 >> 32;
 
208
                        a6a_a5b |= a5b;
 
209
                        a5b = a5b << 32;
 
210
                        a5a_a4b = a5 << 32;
 
211
                        a5a = a5 & 0xffffffff;
 
212
                case 5:
 
213
                        a4 = MP_DIGIT(a, 4);
 
214
                        a5a_a4b |= a4 >> 32;
 
215
                        a4a_a3b = a4 << 32;
 
216
                case 4:
 
217
                        a3b = MP_DIGIT(a, 3) >> 32;
 
218
                        a4a_a3b |= a3b;
 
219
                        a3b = a3b << 32;
 
220
                }
 
221
 
 
222
                r3 = MP_DIGIT(a, 3) & 0xffffffff;
 
223
                r2 = MP_DIGIT(a, 2);
 
224
                r1 = MP_DIGIT(a, 1);
 
225
                r0 = MP_DIGIT(a, 0);
 
226
 
 
227
                /* implement r = (a3a,a2,a1,a0)
 
228
                        +(a5a, a4,a3b,  0)
 
229
                        +(  0, a6,a5b,  0)
 
230
                        -(  0    0,    0|a6b, a6a|a5b )
 
231
                        -(  a6b, a6a|a5b, a5a|a4b, a4a|a3b ) */
 
232
                MP_ADD_CARRY (r1, a3b, r1, 0,     carry);
 
233
                MP_ADD_CARRY (r2, a4 , r2, carry, carry);
 
234
                MP_ADD_CARRY (r3, a5a, r3, carry, carry);
 
235
                MP_ADD_CARRY (r1, a5b, r1, 0,     carry);
 
236
                MP_ADD_CARRY (r2, a6 , r2, carry, carry);
 
237
                MP_ADD_CARRY (r3,   0, r3, carry, carry);
 
238
 
 
239
                MP_SUB_BORROW(r0, a4a_a3b, r0, 0,     carry);
 
240
                MP_SUB_BORROW(r1, a5a_a4b, r1, carry, carry);
 
241
                MP_SUB_BORROW(r2, a6a_a5b, r2, carry, carry);
 
242
                MP_SUB_BORROW(r3, a6b    , r3, carry, carry);
 
243
                MP_SUB_BORROW(r0, a6a_a5b, r0, 0,     carry);
 
244
                MP_SUB_BORROW(r1, a6b    , r1, carry, carry);
 
245
                if (carry) {
 
246
                        MP_SUB_BORROW(r2, 0, r2, carry, carry);
 
247
                        MP_SUB_BORROW(r3, 0, r3, carry, carry);
 
248
                }
 
249
 
 
250
 
 
251
                /* if the value is negative, r3 has a 2's complement 
 
252
                 * high value */
 
253
                r3b = (int)(r3 >>32);
 
254
                while (r3b > 0) {
 
255
                        r3 &= 0xffffffff;
 
256
                        MP_ADD_CARRY(r1,((mp_digit)r3b) << 32, r1, 0, carry);
 
257
                        if (carry) {
 
258
                                MP_ADD_CARRY(r2,  0, r2, carry, carry);
 
259
                                MP_ADD_CARRY(r3,  0, r3, carry, carry);
 
260
                        }
 
261
                        MP_SUB_BORROW(r0, r3b, r0, 0, carry);
 
262
                        if (carry) {
 
263
                                MP_SUB_BORROW(r1, 0, r1, carry, carry);
 
264
                                MP_SUB_BORROW(r2, 0, r2, carry, carry);
 
265
                                MP_SUB_BORROW(r3, 0, r3, carry, carry);
 
266
                        }
 
267
                        r3b = (int)(r3 >>32);
 
268
                }
 
269
 
 
270
                while (r3b < 0) {
 
271
                        MP_ADD_CARRY (r0, 1, r0, 0,     carry);
 
272
                        MP_ADD_CARRY (r1, MP_DIGIT_MAX <<32, r1, carry, carry);
 
273
                        MP_ADD_CARRY (r2, MP_DIGIT_MAX, r2, carry, carry);
 
274
                        MP_ADD_CARRY (r3, MP_DIGIT_MAX >> 32, r3, carry, carry);
 
275
                        r3b = (int)(r3 >>32);
 
276
                }
 
277
                /* check for final reduction */
 
278
                /* now the only way we are over is if the top 4 words are all ones */
 
279
                if ((r3 == (MP_DIGIT_MAX >> 32)) && (r2 == MP_DIGIT_MAX)
 
280
                        && ((r1 & MP_DIGIT_MAX << 32)== MP_DIGIT_MAX << 32) &&
 
281
                         ((r1 != MP_DIGIT_MAX << 32 ) || (r0 != 0)) ) {
 
282
                        /* one last subraction */
 
283
                        MP_SUB_BORROW(r0, 1, r0, 0,     carry);
 
284
                        MP_SUB_BORROW(r1, 0, r1, carry, carry);
 
285
                        r2 = r3 = 0;
 
286
                }
 
287
 
 
288
 
 
289
                if (a != r) {
 
290
                        MP_CHECKOK(s_mp_pad(r, 4));
 
291
                }
 
292
                /* set the lower words of r */
 
293
                MP_SIGN(r) = MP_ZPOS;
 
294
                MP_USED(r) = 4;
 
295
                MP_DIGIT(r, 3) = r3;
 
296
                MP_DIGIT(r, 2) = r2;
 
297
                MP_DIGIT(r, 1) = r1;
 
298
                MP_DIGIT(r, 0) = r0;
 
299
#endif
 
300
        }
 
301
 
 
302
  CLEANUP:
 
303
        return res;
 
304
}
 
305
 
 
306
/* Compute the square of polynomial a, reduce modulo p224. Store the
 
307
 * result in r.  r could be a.  Uses optimized modular reduction for p224. 
 
308
 */
 
309
mp_err
 
310
ec_GFp_nistp224_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
 
311
{
 
312
        mp_err res = MP_OKAY;
 
313
 
 
314
        MP_CHECKOK(mp_sqr(a, r));
 
315
        MP_CHECKOK(ec_GFp_nistp224_mod(r, r, meth));
 
316
  CLEANUP:
 
317
        return res;
 
318
}
 
319
 
 
320
/* Compute the product of two polynomials a and b, reduce modulo p224.
 
321
 * Store the result in r.  r could be a or b; a could be b.  Uses
 
322
 * optimized modular reduction for p224. */
 
323
mp_err
 
324
ec_GFp_nistp224_mul(const mp_int *a, const mp_int *b, mp_int *r,
 
325
                                        const GFMethod *meth)
 
326
{
 
327
        mp_err res = MP_OKAY;
 
328
 
 
329
        MP_CHECKOK(mp_mul(a, b, r));
 
330
        MP_CHECKOK(ec_GFp_nistp224_mod(r, r, meth));
 
331
  CLEANUP:
 
332
        return res;
 
333
}
 
334
 
 
335
/* Divides two field elements. If a is NULL, then returns the inverse of
 
336
 * b. */
 
337
mp_err
 
338
ec_GFp_nistp224_div(const mp_int *a, const mp_int *b, mp_int *r,
 
339
                   const GFMethod *meth)
 
340
{
 
341
        mp_err res = MP_OKAY;
 
342
        mp_int t;
 
343
 
 
344
        /* If a is NULL, then return the inverse of b, otherwise return a/b. */
 
345
        if (a == NULL) {
 
346
                return  mp_invmod(b, &meth->irr, r);
 
347
        } else {
 
348
                /* MPI doesn't support divmod, so we implement it using invmod and 
 
349
                 * mulmod. */
 
350
                MP_CHECKOK(mp_init(&t));
 
351
                MP_CHECKOK(mp_invmod(b, &meth->irr, &t));
 
352
                MP_CHECKOK(mp_mul(a, &t, r));
 
353
                MP_CHECKOK(ec_GFp_nistp224_mod(r, r, meth));
 
354
          CLEANUP:
 
355
                mp_clear(&t);
 
356
                return res;
 
357
        }
 
358
}
 
359
 
 
360
/* Wire in fast field arithmetic and precomputation of base point for
 
361
 * named curves. */
 
362
mp_err
 
363
ec_group_set_gfp224(ECGroup *group, ECCurveName name)
 
364
{
 
365
        if (name == ECCurve_NIST_P224) {
 
366
                group->meth->field_mod = &ec_GFp_nistp224_mod;
 
367
                group->meth->field_mul = &ec_GFp_nistp224_mul;
 
368
                group->meth->field_sqr = &ec_GFp_nistp224_sqr;
 
369
                group->meth->field_div = &ec_GFp_nistp224_div;
 
370
        }
 
371
        return MP_OKAY;
 
372
}