summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/freebl/ecl/ecp_256.c
blob: ad4e630c170b3a89306a1edc5e3ba4808370b06a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ecp.h"
#include "mpi.h"
#include "mplogic.h"
#include "mpi-priv.h"

/* Fast modular reduction for p256 = 2^256 - 2^224 + 2^192+ 2^96 - 1.  a can be r.
 * Uses algorithm 2.29 from Hankerson, Menezes, Vanstone. Guide to
 * Elliptic Curve Cryptography. */
static mp_err
ec_GFp_nistp256_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
{
    mp_err res = MP_OKAY;
    mp_size a_used = MP_USED(a);
    int a_bits = mpl_significant_bits(a);
    mp_digit carry;

#ifdef ECL_THIRTY_TWO_BIT
    mp_digit a8 = 0, a9 = 0, a10 = 0, a11 = 0, a12 = 0, a13 = 0, a14 = 0, a15 = 0;
    mp_digit r0, r1, r2, r3, r4, r5, r6, r7;
    int r8; /* must be a signed value ! */
#else
    mp_digit a4 = 0, a5 = 0, a6 = 0, a7 = 0;
    mp_digit a4h, a4l, a5h, a5l, a6h, a6l, a7h, a7l;
    mp_digit r0, r1, r2, r3;
    int r4; /* must be a signed value ! */
#endif
    /* for polynomials larger than twice the field size
     * use regular reduction */
    if (a_bits < 256) {
        if (a == r)
            return MP_OKAY;
        return mp_copy(a, r);
    }
    if (a_bits > 512) {
        MP_CHECKOK(mp_mod(a, &meth->irr, r));
    } else {

#ifdef ECL_THIRTY_TWO_BIT
        switch (a_used) {
            case 16:
                a15 = MP_DIGIT(a, 15);
            case 15:
                a14 = MP_DIGIT(a, 14);
            case 14:
                a13 = MP_DIGIT(a, 13);
            case 13:
                a12 = MP_DIGIT(a, 12);
            case 12:
                a11 = MP_DIGIT(a, 11);
            case 11:
                a10 = MP_DIGIT(a, 10);
            case 10:
                a9 = MP_DIGIT(a, 9);
            case 9:
                a8 = MP_DIGIT(a, 8);
        }

        r0 = MP_DIGIT(a, 0);
        r1 = MP_DIGIT(a, 1);
        r2 = MP_DIGIT(a, 2);
        r3 = MP_DIGIT(a, 3);
        r4 = MP_DIGIT(a, 4);
        r5 = MP_DIGIT(a, 5);
        r6 = MP_DIGIT(a, 6);
        r7 = MP_DIGIT(a, 7);

        /* sum 1 */
        carry = 0;
        MP_ADD_CARRY(r3, a11, r3, carry);
        MP_ADD_CARRY(r4, a12, r4, carry);
        MP_ADD_CARRY(r5, a13, r5, carry);
        MP_ADD_CARRY(r6, a14, r6, carry);
        MP_ADD_CARRY(r7, a15, r7, carry);
        r8 = carry;
        carry = 0;
        MP_ADD_CARRY(r3, a11, r3, carry);
        MP_ADD_CARRY(r4, a12, r4, carry);
        MP_ADD_CARRY(r5, a13, r5, carry);
        MP_ADD_CARRY(r6, a14, r6, carry);
        MP_ADD_CARRY(r7, a15, r7, carry);
        r8 += carry;
        carry = 0;
        /* sum 2 */
        MP_ADD_CARRY(r3, a12, r3, carry);
        MP_ADD_CARRY(r4, a13, r4, carry);
        MP_ADD_CARRY(r5, a14, r5, carry);
        MP_ADD_CARRY(r6, a15, r6, carry);
        MP_ADD_CARRY(r7, 0, r7, carry);
        r8 += carry;
        carry = 0;
        /* combine last bottom of sum 3 with second sum 2 */
        MP_ADD_CARRY(r0, a8, r0, carry);
        MP_ADD_CARRY(r1, a9, r1, carry);
        MP_ADD_CARRY(r2, a10, r2, carry);
        MP_ADD_CARRY(r3, a12, r3, carry);
        MP_ADD_CARRY(r4, a13, r4, carry);
        MP_ADD_CARRY(r5, a14, r5, carry);
        MP_ADD_CARRY(r6, a15, r6, carry);
        MP_ADD_CARRY(r7, a15, r7, carry); /* from sum 3 */
        r8 += carry;
        carry = 0;
        /* sum 3 (rest of it)*/
        MP_ADD_CARRY(r6, a14, r6, carry);
        MP_ADD_CARRY(r7, 0, r7, carry);
        r8 += carry;
        carry = 0;
        /* sum 4 (rest of it)*/
        MP_ADD_CARRY(r0, a9, r0, carry);
        MP_ADD_CARRY(r1, a10, r1, carry);
        MP_ADD_CARRY(r2, a11, r2, carry);
        MP_ADD_CARRY(r3, a13, r3, carry);
        MP_ADD_CARRY(r4, a14, r4, carry);
        MP_ADD_CARRY(r5, a15, r5, carry);
        MP_ADD_CARRY(r6, a13, r6, carry);
        MP_ADD_CARRY(r7, a8, r7, carry);
        r8 += carry;
        carry = 0;
        /* diff 5 */
        MP_SUB_BORROW(r0, a11, r0, carry);
        MP_SUB_BORROW(r1, a12, r1, carry);
        MP_SUB_BORROW(r2, a13, r2, carry);
        MP_SUB_BORROW(r3, 0, r3, carry);
        MP_SUB_BORROW(r4, 0, r4, carry);
        MP_SUB_BORROW(r5, 0, r5, carry);
        MP_SUB_BORROW(r6, a8, r6, carry);
        MP_SUB_BORROW(r7, a10, r7, carry);
        r8 -= carry;
        carry = 0;
        /* diff 6 */
        MP_SUB_BORROW(r0, a12, r0, carry);
        MP_SUB_BORROW(r1, a13, r1, carry);
        MP_SUB_BORROW(r2, a14, r2, carry);
        MP_SUB_BORROW(r3, a15, r3, carry);
        MP_SUB_BORROW(r4, 0, r4, carry);
        MP_SUB_BORROW(r5, 0, r5, carry);
        MP_SUB_BORROW(r6, a9, r6, carry);
        MP_SUB_BORROW(r7, a11, r7, carry);
        r8 -= carry;
        carry = 0;
        /* diff 7 */
        MP_SUB_BORROW(r0, a13, r0, carry);
        MP_SUB_BORROW(r1, a14, r1, carry);
        MP_SUB_BORROW(r2, a15, r2, carry);
        MP_SUB_BORROW(r3, a8, r3, carry);
        MP_SUB_BORROW(r4, a9, r4, carry);
        MP_SUB_BORROW(r5, a10, r5, carry);
        MP_SUB_BORROW(r6, 0, r6, carry);
        MP_SUB_BORROW(r7, a12, r7, carry);
        r8 -= carry;
        carry = 0;
        /* diff 8 */
        MP_SUB_BORROW(r0, a14, r0, carry);
        MP_SUB_BORROW(r1, a15, r1, carry);
        MP_SUB_BORROW(r2, 0, r2, carry);
        MP_SUB_BORROW(r3, a9, r3, carry);
        MP_SUB_BORROW(r4, a10, r4, carry);
        MP_SUB_BORROW(r5, a11, r5, carry);
        MP_SUB_BORROW(r6, 0, r6, carry);
        MP_SUB_BORROW(r7, a13, r7, carry);
        r8 -= carry;

        /* reduce the overflows */
        while (r8 > 0) {
            mp_digit r8_d = r8;
            carry = 0;
            MP_ADD_CARRY(r0, r8_d, r0, carry);
            MP_ADD_CARRY(r1, 0, r1, carry);
            MP_ADD_CARRY(r2, 0, r2, carry);
            MP_ADD_CARRY(r3, 0 - r8_d, r3, carry);
            MP_ADD_CARRY(r4, MP_DIGIT_MAX, r4, carry);
            MP_ADD_CARRY(r5, MP_DIGIT_MAX, r5, carry);
            MP_ADD_CARRY(r6, 0 - (r8_d + 1), r6, carry);
            MP_ADD_CARRY(r7, (r8_d - 1), r7, carry);
            r8 = carry;
        }

        /* reduce the underflows */
        while (r8 < 0) {
            mp_digit r8_d = -r8;
            carry = 0;
            MP_SUB_BORROW(r0, r8_d, r0, carry);
            MP_SUB_BORROW(r1, 0, r1, carry);
            MP_SUB_BORROW(r2, 0, r2, carry);
            MP_SUB_BORROW(r3, 0 - r8_d, r3, carry);
            MP_SUB_BORROW(r4, MP_DIGIT_MAX, r4, carry);
            MP_SUB_BORROW(r5, MP_DIGIT_MAX, r5, carry);
            MP_SUB_BORROW(r6, 0 - (r8_d + 1), r6, carry);
            MP_SUB_BORROW(r7, (r8_d - 1), r7, carry);
            r8 = 0 - carry;
        }
        if (a != r) {
            MP_CHECKOK(s_mp_pad(r, 8));
        }
        MP_SIGN(r) = MP_ZPOS;
        MP_USED(r) = 8;

        MP_DIGIT(r, 7) = r7;
        MP_DIGIT(r, 6) = r6;
        MP_DIGIT(r, 5) = r5;
        MP_DIGIT(r, 4) = r4;
        MP_DIGIT(r, 3) = r3;
        MP_DIGIT(r, 2) = r2;
        MP_DIGIT(r, 1) = r1;
        MP_DIGIT(r, 0) = r0;

        /* final reduction if necessary */
        if ((r7 == MP_DIGIT_MAX) &&
            ((r6 > 1) || ((r6 == 1) &&
                          (r5 || r4 || r3 ||
                           ((r2 == MP_DIGIT_MAX) && (r1 == MP_DIGIT_MAX) && (r0 == MP_DIGIT_MAX)))))) {
            MP_CHECKOK(mp_sub(r, &meth->irr, r));
        }

        s_mp_clamp(r);
#else
        switch (a_used) {
            case 8:
                a7 = MP_DIGIT(a, 7);
            case 7:
                a6 = MP_DIGIT(a, 6);
            case 6:
                a5 = MP_DIGIT(a, 5);
            case 5:
                a4 = MP_DIGIT(a, 4);
        }
        a7l = a7 << 32;
        a7h = a7 >> 32;
        a6l = a6 << 32;
        a6h = a6 >> 32;
        a5l = a5 << 32;
        a5h = a5 >> 32;
        a4l = a4 << 32;
        a4h = a4 >> 32;
        r3 = MP_DIGIT(a, 3);
        r2 = MP_DIGIT(a, 2);
        r1 = MP_DIGIT(a, 1);
        r0 = MP_DIGIT(a, 0);

        /* sum 1 */
        carry = 0;
        MP_ADD_CARRY(r1, a5h << 32, r1, carry);
        MP_ADD_CARRY(r2, a6, r2, carry);
        MP_ADD_CARRY(r3, a7, r3, carry);
        r4 = carry;
        carry = 0;
        MP_ADD_CARRY(r1, a5h << 32, r1, carry);
        MP_ADD_CARRY(r2, a6, r2, carry);
        MP_ADD_CARRY(r3, a7, r3, carry);
        r4 += carry;
        /* sum 2 */
        carry = 0;
        MP_ADD_CARRY(r1, a6l, r1, carry);
        MP_ADD_CARRY(r2, a6h | a7l, r2, carry);
        MP_ADD_CARRY(r3, a7h, r3, carry);
        r4 += carry;
        carry = 0;
        MP_ADD_CARRY(r1, a6l, r1, carry);
        MP_ADD_CARRY(r2, a6h | a7l, r2, carry);
        MP_ADD_CARRY(r3, a7h, r3, carry);
        r4 += carry;

        /* sum 3 */
        carry = 0;
        MP_ADD_CARRY(r0, a4, r0, carry);
        MP_ADD_CARRY(r1, a5l >> 32, r1, carry);
        MP_ADD_CARRY(r2, 0, r2, carry);
        MP_ADD_CARRY(r3, a7, r3, carry);
        r4 += carry;
        /* sum 4 */
        carry = 0;
        MP_ADD_CARRY(r0, a4h | a5l, r0, carry);
        MP_ADD_CARRY(r1, a5h | (a6h << 32), r1, carry);
        MP_ADD_CARRY(r2, a7, r2, carry);
        MP_ADD_CARRY(r3, a6h | a4l, r3, carry);
        r4 += carry;
        /* diff 5 */
        carry = 0;
        MP_SUB_BORROW(r0, a5h | a6l, r0, carry);
        MP_SUB_BORROW(r1, a6h, r1, carry);
        MP_SUB_BORROW(r2, 0, r2, carry);
        MP_SUB_BORROW(r3, (a4l >> 32) | a5l, r3, carry);
        r4 -= carry;
        /* diff 6 */
        carry = 0;
        MP_SUB_BORROW(r0, a6, r0, carry);
        MP_SUB_BORROW(r1, a7, r1, carry);
        MP_SUB_BORROW(r2, 0, r2, carry);
        MP_SUB_BORROW(r3, a4h | (a5h << 32), r3, carry);
        r4 -= carry;
        /* diff 7 */
        carry = 0;
        MP_SUB_BORROW(r0, a6h | a7l, r0, carry);
        MP_SUB_BORROW(r1, a7h | a4l, r1, carry);
        MP_SUB_BORROW(r2, a4h | a5l, r2, carry);
        MP_SUB_BORROW(r3, a6l, r3, carry);
        r4 -= carry;
        /* diff 8 */
        carry = 0;
        MP_SUB_BORROW(r0, a7, r0, carry);
        MP_SUB_BORROW(r1, a4h << 32, r1, carry);
        MP_SUB_BORROW(r2, a5, r2, carry);
        MP_SUB_BORROW(r3, a6h << 32, r3, carry);
        r4 -= carry;

        /* reduce the overflows */
        while (r4 > 0) {
            mp_digit r4_long = r4;
            mp_digit r4l = (r4_long << 32);
            carry = 0;
            MP_ADD_CARRY(r0, r4_long, r0, carry);
            MP_ADD_CARRY(r1, 0 - r4l, r1, carry);
            MP_ADD_CARRY(r2, MP_DIGIT_MAX, r2, carry);
            MP_ADD_CARRY(r3, r4l - r4_long - 1, r3, carry);
            r4 = carry;
        }

        /* reduce the underflows */
        while (r4 < 0) {
            mp_digit r4_long = -r4;
            mp_digit r4l = (r4_long << 32);
            carry = 0;
            MP_SUB_BORROW(r0, r4_long, r0, carry);
            MP_SUB_BORROW(r1, 0 - r4l, r1, carry);
            MP_SUB_BORROW(r2, MP_DIGIT_MAX, r2, carry);
            MP_SUB_BORROW(r3, r4l - r4_long - 1, r3, carry);
            r4 = 0 - carry;
        }

        if (a != r) {
            MP_CHECKOK(s_mp_pad(r, 4));
        }
        MP_SIGN(r) = MP_ZPOS;
        MP_USED(r) = 4;

        MP_DIGIT(r, 3) = r3;
        MP_DIGIT(r, 2) = r2;
        MP_DIGIT(r, 1) = r1;
        MP_DIGIT(r, 0) = r0;

        /* final reduction if necessary */
        if ((r3 > 0xFFFFFFFF00000001ULL) ||
            ((r3 == 0xFFFFFFFF00000001ULL) &&
             (r2 || (r1 >> 32) ||
              (r1 == 0xFFFFFFFFULL && r0 == MP_DIGIT_MAX)))) {
            /* very rare, just use mp_sub */
            MP_CHECKOK(mp_sub(r, &meth->irr, r));
        }

        s_mp_clamp(r);
#endif
    }

CLEANUP:
    return res;
}

/* Compute the square of polynomial a, reduce modulo p256. Store the
 * result in r.  r could be a.  Uses optimized modular reduction for p256.
 */
static mp_err
ec_GFp_nistp256_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
{
    mp_err res = MP_OKAY;

    MP_CHECKOK(mp_sqr(a, r));
    MP_CHECKOK(ec_GFp_nistp256_mod(r, r, meth));
CLEANUP:
    return res;
}

/* Compute the product of two polynomials a and b, reduce modulo p256.
 * Store the result in r.  r could be a or b; a could be b.  Uses
 * optimized modular reduction for p256. */
static mp_err
ec_GFp_nistp256_mul(const mp_int *a, const mp_int *b, mp_int *r,
                    const GFMethod *meth)
{
    mp_err res = MP_OKAY;

    MP_CHECKOK(mp_mul(a, b, r));
    MP_CHECKOK(ec_GFp_nistp256_mod(r, r, meth));
CLEANUP:
    return res;
}

/* Wire in fast field arithmetic and precomputation of base point for
 * named curves. */
mp_err
ec_group_set_gfp256(ECGroup *group, ECCurveName name)
{
    if (name == ECCurve_NIST_P256) {
        group->meth->field_mod = &ec_GFp_nistp256_mod;
        group->meth->field_mul = &ec_GFp_nistp256_mul;
        group->meth->field_sqr = &ec_GFp_nistp256_sqr;
    }
    return MP_OKAY;
}