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

« back to all changes in this revision

Viewing changes to security/nss-fips/lib/freebl/ecl/ec2_193.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 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 193-bit curve. Assumes reduction
 
49
 * polynomial with terms {193, 15, 0}. */
 
50
mp_err
 
51
ec_GF2m_193_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) < 7) {
 
61
                MP_CHECKOK(s_mp_pad(r, 7));
 
62
        }
 
63
        u = MP_DIGITS(r);
 
64
        MP_USED(r) = 7;
 
65
 
 
66
        /* u[6] only has 2 significant bits */
 
67
        z = u[6];
 
68
        u[3] ^= (z << 14) ^ (z >> 1);
 
69
        u[2] ^= (z << 63);
 
70
        z = u[5];
 
71
        u[3] ^= (z >> 50);
 
72
        u[2] ^= (z << 14) ^ (z >> 1);
 
73
        u[1] ^= (z << 63);
 
74
        z = u[4];
 
75
        u[2] ^= (z >> 50);
 
76
        u[1] ^= (z << 14) ^ (z >> 1);
 
77
        u[0] ^= (z << 63);
 
78
        z = u[3] >> 1;                          /* z only has 63 significant bits */
 
79
        u[1] ^= (z >> 49);
 
80
        u[0] ^= (z << 15) ^ z;
 
81
        /* clear bits above 193 */
 
82
        u[6] = u[5] = u[4] = 0;
 
83
        u[3] ^= z << 1;
 
84
#else
 
85
        if (MP_USED(r) < 13) {
 
86
                MP_CHECKOK(s_mp_pad(r, 13));
 
87
        }
 
88
        u = MP_DIGITS(r);
 
89
        MP_USED(r) = 13;
 
90
 
 
91
        /* u[12] only has 2 significant bits */
 
92
        z = u[12];
 
93
        u[6] ^= (z << 14) ^ (z >> 1);
 
94
        u[5] ^= (z << 31);
 
95
        z = u[11];
 
96
        u[6] ^= (z >> 18);
 
97
        u[5] ^= (z << 14) ^ (z >> 1);
 
98
        u[4] ^= (z << 31);
 
99
        z = u[10];
 
100
        u[5] ^= (z >> 18);
 
101
        u[4] ^= (z << 14) ^ (z >> 1);
 
102
        u[3] ^= (z << 31);
 
103
        z = u[9];
 
104
        u[4] ^= (z >> 18);
 
105
        u[3] ^= (z << 14) ^ (z >> 1);
 
106
        u[2] ^= (z << 31);
 
107
        z = u[8];
 
108
        u[3] ^= (z >> 18);
 
109
        u[2] ^= (z << 14) ^ (z >> 1);
 
110
        u[1] ^= (z << 31);
 
111
        z = u[7];
 
112
        u[2] ^= (z >> 18);
 
113
        u[1] ^= (z << 14) ^ (z >> 1);
 
114
        u[0] ^= (z << 31);
 
115
        z = u[6] >> 1;                          /* z only has 31 significant bits */
 
116
        u[1] ^= (z >> 17);
 
117
        u[0] ^= (z << 15) ^ z;
 
118
        /* clear bits above 193 */
 
119
        u[12] = u[11] = u[10] = u[9] = u[8] = u[7] = 0;
 
120
        u[6] ^= z << 1;
 
121
#endif
 
122
        s_mp_clamp(r);
 
123
 
 
124
  CLEANUP:
 
125
        return res;
 
126
}
 
127
 
 
128
/* Fast squaring for polynomials over a 193-bit curve. Assumes reduction
 
129
 * polynomial with terms {193, 15, 0}. */
 
130
mp_err
 
131
ec_GF2m_193_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
 
132
{
 
133
        mp_err res = MP_OKAY;
 
134
        mp_digit *u, *v;
 
135
 
 
136
        v = MP_DIGITS(a);
 
137
 
 
138
#ifdef ECL_SIXTY_FOUR_BIT
 
139
        if (MP_USED(a) < 4) {
 
140
                return mp_bsqrmod(a, meth->irr_arr, r);
 
141
        }
 
142
        if (MP_USED(r) < 7) {
 
143
                MP_CHECKOK(s_mp_pad(r, 7));
 
144
        }
 
145
        MP_USED(r) = 7;
 
146
#else
 
147
        if (MP_USED(a) < 7) {
 
148
                return mp_bsqrmod(a, meth->irr_arr, r);
 
149
        }
 
150
        if (MP_USED(r) < 13) {
 
151
                MP_CHECKOK(s_mp_pad(r, 13));
 
152
        }
 
153
        MP_USED(r) = 13;
 
154
#endif
 
155
        u = MP_DIGITS(r);
 
156
 
 
157
#ifdef ECL_THIRTY_TWO_BIT
 
158
        u[12] = gf2m_SQR0(v[6]);
 
159
        u[11] = gf2m_SQR1(v[5]);
 
160
        u[10] = gf2m_SQR0(v[5]);
 
161
        u[9] = gf2m_SQR1(v[4]);
 
162
        u[8] = gf2m_SQR0(v[4]);
 
163
        u[7] = gf2m_SQR1(v[3]);
 
164
#endif
 
165
        u[6] = gf2m_SQR0(v[3]);
 
166
        u[5] = gf2m_SQR1(v[2]);
 
167
        u[4] = gf2m_SQR0(v[2]);
 
168
        u[3] = gf2m_SQR1(v[1]);
 
169
        u[2] = gf2m_SQR0(v[1]);
 
170
        u[1] = gf2m_SQR1(v[0]);
 
171
        u[0] = gf2m_SQR0(v[0]);
 
172
        return ec_GF2m_193_mod(r, r, meth);
 
173
 
 
174
  CLEANUP:
 
175
        return res;
 
176
}
 
177
 
 
178
/* Fast multiplication for polynomials over a 193-bit curve. Assumes
 
179
 * reduction polynomial with terms {193, 15, 0}. */
 
180
mp_err
 
181
ec_GF2m_193_mul(const mp_int *a, const mp_int *b, mp_int *r,
 
182
                                const GFMethod *meth)
 
183
{
 
184
        mp_err res = MP_OKAY;
 
185
        mp_digit a3 = 0, a2 = 0, a1 = 0, a0, b3 = 0, b2 = 0, b1 = 0, b0;
 
186
 
 
187
#ifdef ECL_THIRTY_TWO_BIT
 
188
        mp_digit a6 = 0, a5 = 0, a4 = 0, b6 = 0, b5 = 0, b4 = 0;
 
189
        mp_digit rm[8];
 
190
#endif
 
191
 
 
192
        if (a == b) {
 
193
                return ec_GF2m_193_sqr(a, r, meth);
 
194
        } else {
 
195
                switch (MP_USED(a)) {
 
196
#ifdef ECL_THIRTY_TWO_BIT
 
197
                case 7:
 
198
                        a6 = MP_DIGIT(a, 6);
 
199
                case 6:
 
200
                        a5 = MP_DIGIT(a, 5);
 
201
                case 5:
 
202
                        a4 = MP_DIGIT(a, 4);
 
203
#endif
 
204
                case 4:
 
205
                        a3 = MP_DIGIT(a, 3);
 
206
                case 3:
 
207
                        a2 = MP_DIGIT(a, 2);
 
208
                case 2:
 
209
                        a1 = MP_DIGIT(a, 1);
 
210
                default:
 
211
                        a0 = MP_DIGIT(a, 0);
 
212
                }
 
213
                switch (MP_USED(b)) {
 
214
#ifdef ECL_THIRTY_TWO_BIT
 
215
                case 7:
 
216
                        b6 = MP_DIGIT(b, 6);
 
217
                case 6:
 
218
                        b5 = MP_DIGIT(b, 5);
 
219
                case 5:
 
220
                        b4 = MP_DIGIT(b, 4);
 
221
#endif
 
222
                case 4:
 
223
                        b3 = MP_DIGIT(b, 3);
 
224
                case 3:
 
225
                        b2 = MP_DIGIT(b, 2);
 
226
                case 2:
 
227
                        b1 = MP_DIGIT(b, 1);
 
228
                default:
 
229
                        b0 = MP_DIGIT(b, 0);
 
230
                }
 
231
#ifdef ECL_SIXTY_FOUR_BIT
 
232
                MP_CHECKOK(s_mp_pad(r, 8));
 
233
                s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0);
 
234
                MP_USED(r) = 8;
 
235
                s_mp_clamp(r);
 
236
#else
 
237
                MP_CHECKOK(s_mp_pad(r, 14));
 
238
                s_bmul_3x3(MP_DIGITS(r) + 8, a6, a5, a4, b6, b5, b4);
 
239
                s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0);
 
240
                s_bmul_4x4(rm, a3, a6 ^ a2, a5 ^ a1, a4 ^ a0, b3, b6 ^ b2, b5 ^ b1,
 
241
                                   b4 ^ b0);
 
242
                rm[7] ^= MP_DIGIT(r, 7);
 
243
                rm[6] ^= MP_DIGIT(r, 6);
 
244
                rm[5] ^= MP_DIGIT(r, 5) ^ MP_DIGIT(r, 13);
 
245
                rm[4] ^= MP_DIGIT(r, 4) ^ MP_DIGIT(r, 12);
 
246
                rm[3] ^= MP_DIGIT(r, 3) ^ MP_DIGIT(r, 11);
 
247
                rm[2] ^= MP_DIGIT(r, 2) ^ MP_DIGIT(r, 10);
 
248
                rm[1] ^= MP_DIGIT(r, 1) ^ MP_DIGIT(r, 9);
 
249
                rm[0] ^= MP_DIGIT(r, 0) ^ MP_DIGIT(r, 8);
 
250
                MP_DIGIT(r, 11) ^= rm[7];
 
251
                MP_DIGIT(r, 10) ^= rm[6];
 
252
                MP_DIGIT(r, 9) ^= rm[5];
 
253
                MP_DIGIT(r, 8) ^= rm[4];
 
254
                MP_DIGIT(r, 7) ^= rm[3];
 
255
                MP_DIGIT(r, 6) ^= rm[2];
 
256
                MP_DIGIT(r, 5) ^= rm[1];
 
257
                MP_DIGIT(r, 4) ^= rm[0];
 
258
                MP_USED(r) = 14;
 
259
                s_mp_clamp(r);
 
260
#endif
 
261
                return ec_GF2m_193_mod(r, r, meth);
 
262
        }
 
263
 
 
264
  CLEANUP:
 
265
        return res;
 
266
}
 
267
 
 
268
/* Wire in fast field arithmetic for 193-bit curves. */
 
269
mp_err
 
270
ec_group_set_gf2m193(ECGroup *group, ECCurveName name)
 
271
{
 
272
        group->meth->field_mod = &ec_GF2m_193_mod;
 
273
        group->meth->field_mul = &ec_GF2m_193_mul;
 
274
        group->meth->field_sqr = &ec_GF2m_193_sqr;
 
275
        return MP_OKAY;
 
276
}