Coverage Report

Created: 2026-05-30 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/ec/ecp_nistz256.c
Line
Count
Source
1
/*
2
 * Copyright 2014-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2014, Intel Corporation. All Rights Reserved.
4
 * Copyright (c) 2015, CloudFlare, Inc.
5
 *
6
 * Licensed under the Apache License 2.0 (the "License").  You may not use
7
 * this file except in compliance with the License.  You can obtain a copy
8
 * in the file LICENSE in the source distribution or at
9
 * https://www.openssl.org/source/license.html
10
 *
11
 * Originally written by Shay Gueron (1, 2), and Vlad Krasnov (1, 3)
12
 * (1) Intel Corporation, Israel Development Center, Haifa, Israel
13
 * (2) University of Haifa, Israel
14
 * (3) CloudFlare, Inc.
15
 *
16
 * Reference:
17
 * S.Gueron and V.Krasnov, "Fast Prime Field Elliptic Curve Cryptography with
18
 *                          256 Bit Primes"
19
 */
20
21
/*
22
 * ECDSA low level APIs are deprecated for public use, but still ok for
23
 * internal use.
24
 */
25
#include "internal/deprecated.h"
26
27
#include <string.h>
28
29
#include "internal/cryptlib.h"
30
#include "crypto/bn.h"
31
#include "ec_local.h"
32
#include "internal/refcount.h"
33
34
#if BN_BITS2 != 64
35
#define TOBN(hi, lo) lo, hi
36
#else
37
0
#define TOBN(hi, lo) ((BN_ULONG)hi << 32 | lo)
38
#endif
39
40
0
#define P256_LIMBS (256 / BN_BITS2)
41
42
typedef struct {
43
    BN_ULONG X[P256_LIMBS];
44
    BN_ULONG Y[P256_LIMBS];
45
    BN_ULONG Z[P256_LIMBS];
46
} P256_POINT;
47
48
typedef struct {
49
    BN_ULONG X[P256_LIMBS];
50
    BN_ULONG Y[P256_LIMBS];
51
} P256_POINT_AFFINE;
52
53
typedef P256_POINT_AFFINE PRECOMP256_ROW[64];
54
55
/* structure for precomputed multiples of the generator */
56
struct nistz256_pre_comp_st {
57
    const EC_GROUP *group; /* Parent EC_GROUP object */
58
    size_t w; /* Window size */
59
    /*
60
     * Constant time access to the X and Y coordinates of the pre-computed,
61
     * generator multiplies, in the Montgomery domain. Pre-calculated
62
     * multiplies are stored in affine form.
63
     */
64
    PRECOMP256_ROW *precomp;
65
    void *precomp_storage;
66
    CRYPTO_REF_COUNT references;
67
};
68
69
/* Functions implemented in assembly */
70
/*
71
 * Most of below mentioned functions *preserve* the property of inputs
72
 * being fully reduced, i.e. being in [0, modulus) range. Simply put if
73
 * inputs are fully reduced, then output is too. Note that reverse is
74
 * not true, in sense that given partially reduced inputs output can be
75
 * either, not unlikely reduced. And "most" in first sentence refers to
76
 * the fact that given the calculations flow one can tolerate that
77
 * addition, 1st function below, produces partially reduced result *if*
78
 * multiplications by 2 and 3, which customarily use addition, fully
79
 * reduce it. This effectively gives two options: a) addition produces
80
 * fully reduced result [as long as inputs are, just like remaining
81
 * functions]; b) addition is allowed to produce partially reduced
82
 * result, but multiplications by 2 and 3 perform additional reduction
83
 * step. Choice between the two can be platform-specific, but it was a)
84
 * in all cases so far...
85
 */
86
/* Modular add: res = a+b mod P   */
87
void ecp_nistz256_add(BN_ULONG res[P256_LIMBS],
88
    const BN_ULONG a[P256_LIMBS],
89
    const BN_ULONG b[P256_LIMBS]);
90
/* Modular mul by 2: res = 2*a mod P */
91
void ecp_nistz256_mul_by_2(BN_ULONG res[P256_LIMBS],
92
    const BN_ULONG a[P256_LIMBS]);
93
/* Modular mul by 3: res = 3*a mod P */
94
void ecp_nistz256_mul_by_3(BN_ULONG res[P256_LIMBS],
95
    const BN_ULONG a[P256_LIMBS]);
96
97
/* Modular div by 2: res = a/2 mod P */
98
void ecp_nistz256_div_by_2(BN_ULONG res[P256_LIMBS],
99
    const BN_ULONG a[P256_LIMBS]);
100
/* Modular sub: res = a-b mod P   */
101
void ecp_nistz256_sub(BN_ULONG res[P256_LIMBS],
102
    const BN_ULONG a[P256_LIMBS],
103
    const BN_ULONG b[P256_LIMBS]);
104
/* Modular neg: res = -a mod P    */
105
void ecp_nistz256_neg(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS]);
106
/* Montgomery mul: res = a*b*2^-256 mod P */
107
void ecp_nistz256_mul_mont(BN_ULONG res[P256_LIMBS],
108
    const BN_ULONG a[P256_LIMBS],
109
    const BN_ULONG b[P256_LIMBS]);
110
/* Montgomery sqr: res = a*a*2^-256 mod P */
111
void ecp_nistz256_sqr_mont(BN_ULONG res[P256_LIMBS],
112
    const BN_ULONG a[P256_LIMBS]);
113
/* Convert a number from Montgomery domain, by multiplying with 1 */
114
void ecp_nistz256_from_mont(BN_ULONG res[P256_LIMBS],
115
    const BN_ULONG in[P256_LIMBS]);
116
/* Convert a number to Montgomery domain, by multiplying with 2^512 mod P*/
117
void ecp_nistz256_to_mont(BN_ULONG res[P256_LIMBS],
118
    const BN_ULONG in[P256_LIMBS]);
119
/* Functions that perform constant time access to the precomputed tables */
120
void ecp_nistz256_scatter_w5(P256_POINT *val,
121
    const P256_POINT *in_t, int idx);
122
void ecp_nistz256_gather_w5(P256_POINT *val,
123
    const P256_POINT *in_t, int idx);
124
void ecp_nistz256_scatter_w7(P256_POINT_AFFINE *val,
125
    const P256_POINT_AFFINE *in_t, int idx);
126
void ecp_nistz256_gather_w7(P256_POINT_AFFINE *val,
127
    const P256_POINT_AFFINE *in_t, int idx);
128
129
/* One converted into the Montgomery domain */
130
static const BN_ULONG ONE[P256_LIMBS] = {
131
    TOBN(0x00000000, 0x00000001), TOBN(0xffffffff, 0x00000000),
132
    TOBN(0xffffffff, 0xffffffff), TOBN(0x00000000, 0xfffffffe)
133
};
134
135
static NISTZ256_PRE_COMP *ecp_nistz256_pre_comp_new(const EC_GROUP *group);
136
137
/* Precomputed tables for the default generator */
138
extern const PRECOMP256_ROW ecp_nistz256_precomputed[37];
139
140
/* Recode window to a signed digit, see ecp_nistputil.c for details */
141
static unsigned int _booth_recode_w5(unsigned int in)
142
0
{
143
0
    unsigned int s, d;
144
145
0
    s = ~((in >> 5) - 1);
146
0
    d = (1 << 6) - in - 1;
147
0
    d = (d & s) | (in & ~s);
148
0
    d = (d >> 1) + (d & 1);
149
150
0
    return (d << 1) + (s & 1);
151
0
}
152
153
static unsigned int _booth_recode_w7(unsigned int in)
154
0
{
155
0
    unsigned int s, d;
156
157
0
    s = ~((in >> 7) - 1);
158
0
    d = (1 << 8) - in - 1;
159
0
    d = (d & s) | (in & ~s);
160
0
    d = (d >> 1) + (d & 1);
161
162
0
    return (d << 1) + (s & 1);
163
0
}
164
165
static void copy_conditional(BN_ULONG dst[P256_LIMBS],
166
    const BN_ULONG src[P256_LIMBS], BN_ULONG move)
167
0
{
168
0
    BN_ULONG mask1 = 0 - move;
169
0
    BN_ULONG mask2 = ~mask1;
170
171
0
    dst[0] = (src[0] & mask1) ^ (dst[0] & mask2);
172
0
    dst[1] = (src[1] & mask1) ^ (dst[1] & mask2);
173
0
    dst[2] = (src[2] & mask1) ^ (dst[2] & mask2);
174
0
    dst[3] = (src[3] & mask1) ^ (dst[3] & mask2);
175
0
    if (P256_LIMBS == 8) {
176
0
        dst[4] = (src[4] & mask1) ^ (dst[4] & mask2);
177
0
        dst[5] = (src[5] & mask1) ^ (dst[5] & mask2);
178
0
        dst[6] = (src[6] & mask1) ^ (dst[6] & mask2);
179
0
        dst[7] = (src[7] & mask1) ^ (dst[7] & mask2);
180
0
    }
181
0
}
182
183
static BN_ULONG is_zero(BN_ULONG in)
184
0
{
185
0
    in |= (0 - in);
186
0
    in = ~in;
187
0
    in >>= BN_BITS2 - 1;
188
0
    return in;
189
0
}
190
191
static BN_ULONG is_equal(const BN_ULONG a[P256_LIMBS],
192
    const BN_ULONG b[P256_LIMBS])
193
0
{
194
0
    BN_ULONG res;
195
196
0
    res = a[0] ^ b[0];
197
0
    res |= a[1] ^ b[1];
198
0
    res |= a[2] ^ b[2];
199
0
    res |= a[3] ^ b[3];
200
0
    if (P256_LIMBS == 8) {
201
0
        res |= a[4] ^ b[4];
202
0
        res |= a[5] ^ b[5];
203
0
        res |= a[6] ^ b[6];
204
0
        res |= a[7] ^ b[7];
205
0
    }
206
207
0
    return is_zero(res);
208
0
}
209
210
static BN_ULONG is_one(const BIGNUM *z)
211
0
{
212
0
    BN_ULONG res = 0;
213
0
    BN_ULONG *a = bn_get_words(z);
214
215
0
    if (bn_get_top(z) == (P256_LIMBS - P256_LIMBS / 8)) {
216
0
        res = a[0] ^ ONE[0];
217
0
        res |= a[1] ^ ONE[1];
218
0
        res |= a[2] ^ ONE[2];
219
0
        res |= a[3] ^ ONE[3];
220
0
        if (P256_LIMBS == 8) {
221
0
            res |= a[4] ^ ONE[4];
222
0
            res |= a[5] ^ ONE[5];
223
0
            res |= a[6] ^ ONE[6];
224
            /*
225
             * no check for a[7] (being zero) on 32-bit platforms,
226
             * because value of "one" takes only 7 limbs.
227
             */
228
0
        }
229
0
        res = is_zero(res);
230
0
    }
231
232
0
    return res;
233
0
}
234
235
/*
236
 * For reference, this macro is used only when new ecp_nistz256 assembly
237
 * module is being developed.  For example, configure with
238
 * -DECP_NISTZ256_REFERENCE_IMPLEMENTATION and implement only functions
239
 * performing simplest arithmetic operations on 256-bit vectors. Then
240
 * work on implementation of higher-level functions performing point
241
 * operations. Then remove ECP_NISTZ256_REFERENCE_IMPLEMENTATION
242
 * and never define it again. (The correct macro denoting presence of
243
 * ecp_nistz256 module is ECP_NISTZ256_ASM.)
244
 */
245
#ifndef ECP_NISTZ256_REFERENCE_IMPLEMENTATION
246
void ecp_nistz256_point_double(P256_POINT *r, const P256_POINT *a);
247
void ecp_nistz256_point_add(P256_POINT *r,
248
    const P256_POINT *a, const P256_POINT *b);
249
void ecp_nistz256_point_add_affine(P256_POINT *r,
250
    const P256_POINT *a,
251
    const P256_POINT_AFFINE *b);
252
#else
253
/* Point double: r = 2*a */
254
static void ecp_nistz256_point_double(P256_POINT *r, const P256_POINT *a)
255
{
256
    BN_ULONG S[P256_LIMBS];
257
    BN_ULONG M[P256_LIMBS];
258
    BN_ULONG Zsqr[P256_LIMBS];
259
    BN_ULONG tmp0[P256_LIMBS];
260
261
    const BN_ULONG *in_x = a->X;
262
    const BN_ULONG *in_y = a->Y;
263
    const BN_ULONG *in_z = a->Z;
264
265
    BN_ULONG *res_x = r->X;
266
    BN_ULONG *res_y = r->Y;
267
    BN_ULONG *res_z = r->Z;
268
269
    ecp_nistz256_mul_by_2(S, in_y);
270
271
    ecp_nistz256_sqr_mont(Zsqr, in_z);
272
273
    ecp_nistz256_sqr_mont(S, S);
274
275
    ecp_nistz256_mul_mont(res_z, in_z, in_y);
276
    ecp_nistz256_mul_by_2(res_z, res_z);
277
278
    ecp_nistz256_add(M, in_x, Zsqr);
279
    ecp_nistz256_sub(Zsqr, in_x, Zsqr);
280
281
    ecp_nistz256_sqr_mont(res_y, S);
282
    ecp_nistz256_div_by_2(res_y, res_y);
283
284
    ecp_nistz256_mul_mont(M, M, Zsqr);
285
    ecp_nistz256_mul_by_3(M, M);
286
287
    ecp_nistz256_mul_mont(S, S, in_x);
288
    ecp_nistz256_mul_by_2(tmp0, S);
289
290
    ecp_nistz256_sqr_mont(res_x, M);
291
292
    ecp_nistz256_sub(res_x, res_x, tmp0);
293
    ecp_nistz256_sub(S, S, res_x);
294
295
    ecp_nistz256_mul_mont(S, S, M);
296
    ecp_nistz256_sub(res_y, S, res_y);
297
}
298
299
/* Point addition: r = a+b */
300
static void ecp_nistz256_point_add(P256_POINT *r,
301
    const P256_POINT *a, const P256_POINT *b)
302
{
303
    BN_ULONG U2[P256_LIMBS], S2[P256_LIMBS];
304
    BN_ULONG U1[P256_LIMBS], S1[P256_LIMBS];
305
    BN_ULONG Z1sqr[P256_LIMBS];
306
    BN_ULONG Z2sqr[P256_LIMBS];
307
    BN_ULONG H[P256_LIMBS], R[P256_LIMBS];
308
    BN_ULONG Hsqr[P256_LIMBS];
309
    BN_ULONG Rsqr[P256_LIMBS];
310
    BN_ULONG Hcub[P256_LIMBS];
311
312
    BN_ULONG res_x[P256_LIMBS];
313
    BN_ULONG res_y[P256_LIMBS];
314
    BN_ULONG res_z[P256_LIMBS];
315
316
    BN_ULONG in1infty, in2infty;
317
318
    const BN_ULONG *in1_x = a->X;
319
    const BN_ULONG *in1_y = a->Y;
320
    const BN_ULONG *in1_z = a->Z;
321
322
    const BN_ULONG *in2_x = b->X;
323
    const BN_ULONG *in2_y = b->Y;
324
    const BN_ULONG *in2_z = b->Z;
325
326
    /*
327
     * Infinity in encoded as (,,0)
328
     */
329
    in1infty = (in1_z[0] | in1_z[1] | in1_z[2] | in1_z[3]);
330
    if (P256_LIMBS == 8)
331
        in1infty |= (in1_z[4] | in1_z[5] | in1_z[6] | in1_z[7]);
332
333
    in2infty = (in2_z[0] | in2_z[1] | in2_z[2] | in2_z[3]);
334
    if (P256_LIMBS == 8)
335
        in2infty |= (in2_z[4] | in2_z[5] | in2_z[6] | in2_z[7]);
336
337
    in1infty = is_zero(in1infty);
338
    in2infty = is_zero(in2infty);
339
340
    ecp_nistz256_sqr_mont(Z2sqr, in2_z); /* Z2^2 */
341
    ecp_nistz256_sqr_mont(Z1sqr, in1_z); /* Z1^2 */
342
343
    ecp_nistz256_mul_mont(S1, Z2sqr, in2_z); /* S1 = Z2^3 */
344
    ecp_nistz256_mul_mont(S2, Z1sqr, in1_z); /* S2 = Z1^3 */
345
346
    ecp_nistz256_mul_mont(S1, S1, in1_y); /* S1 = Y1*Z2^3 */
347
    ecp_nistz256_mul_mont(S2, S2, in2_y); /* S2 = Y2*Z1^3 */
348
    ecp_nistz256_sub(R, S2, S1); /* R = S2 - S1 */
349
350
    ecp_nistz256_mul_mont(U1, in1_x, Z2sqr); /* U1 = X1*Z2^2 */
351
    ecp_nistz256_mul_mont(U2, in2_x, Z1sqr); /* U2 = X2*Z1^2 */
352
    ecp_nistz256_sub(H, U2, U1); /* H = U2 - U1 */
353
354
    /*
355
     * The formulae are incorrect if the points are equal so we check for
356
     * this and do doubling if this happens.
357
     *
358
     * Points here are in Jacobian projective coordinates (Xi, Yi, Zi)
359
     * that are bound to the affine coordinates (xi, yi) by the following
360
     * equations:
361
     *     - xi = Xi / (Zi)^2
362
     *     - y1 = Yi / (Zi)^3
363
     *
364
     * For the sake of optimization, the algorithm operates over
365
     * intermediate variables U1, U2 and S1, S2 that are derived from
366
     * the projective coordinates:
367
     *     - U1 = X1 * (Z2)^2 ; U2 = X2 * (Z1)^2
368
     *     - S1 = Y1 * (Z2)^3 ; S2 = Y2 * (Z1)^3
369
     *
370
     * It is easy to prove that is_equal(U1, U2) implies that the affine
371
     * x-coordinates are equal, or either point is at infinity.
372
     * Likewise is_equal(S1, S2) implies that the affine y-coordinates are
373
     * equal, or either point is at infinity.
374
     *
375
     * The special case of either point being the point at infinity (Z1 or Z2
376
     * is zero), is handled separately later on in this function, so we avoid
377
     * jumping to point_double here in those special cases.
378
     *
379
     * When both points are inverse of each other, we know that the affine
380
     * x-coordinates are equal, and the y-coordinates have different sign.
381
     * Therefore since U1 = U2, we know H = 0, and therefore Z3 = H*Z1*Z2
382
     * will equal 0, thus the result is infinity, if we simply let this
383
     * function continue normally.
384
     *
385
     * We use bitwise operations to avoid potential side-channels introduced by
386
     * the short-circuiting behaviour of boolean operators.
387
     */
388
    if (is_equal(U1, U2) & ~in1infty & ~in2infty & is_equal(S1, S2)) {
389
        /*
390
         * This is obviously not constant-time but it should never happen during
391
         * single point multiplication, so there is no timing leak for ECDH or
392
         * ECDSA signing.
393
         */
394
        ecp_nistz256_point_double(r, a);
395
        return;
396
    }
397
398
    ecp_nistz256_sqr_mont(Rsqr, R); /* R^2 */
399
    ecp_nistz256_mul_mont(res_z, H, in1_z); /* Z3 = H*Z1*Z2 */
400
    ecp_nistz256_sqr_mont(Hsqr, H); /* H^2 */
401
    ecp_nistz256_mul_mont(res_z, res_z, in2_z); /* Z3 = H*Z1*Z2 */
402
    ecp_nistz256_mul_mont(Hcub, Hsqr, H); /* H^3 */
403
404
    ecp_nistz256_mul_mont(U2, U1, Hsqr); /* U1*H^2 */
405
    ecp_nistz256_mul_by_2(Hsqr, U2); /* 2*U1*H^2 */
406
407
    ecp_nistz256_sub(res_x, Rsqr, Hsqr);
408
    ecp_nistz256_sub(res_x, res_x, Hcub);
409
410
    ecp_nistz256_sub(res_y, U2, res_x);
411
412
    ecp_nistz256_mul_mont(S2, S1, Hcub);
413
    ecp_nistz256_mul_mont(res_y, R, res_y);
414
    ecp_nistz256_sub(res_y, res_y, S2);
415
416
    copy_conditional(res_x, in2_x, in1infty);
417
    copy_conditional(res_y, in2_y, in1infty);
418
    copy_conditional(res_z, in2_z, in1infty);
419
420
    copy_conditional(res_x, in1_x, in2infty);
421
    copy_conditional(res_y, in1_y, in2infty);
422
    copy_conditional(res_z, in1_z, in2infty);
423
424
    memcpy(r->X, res_x, sizeof(res_x));
425
    memcpy(r->Y, res_y, sizeof(res_y));
426
    memcpy(r->Z, res_z, sizeof(res_z));
427
}
428
429
/* Point addition when b is known to be affine: r = a+b */
430
static void ecp_nistz256_point_add_affine(P256_POINT *r,
431
    const P256_POINT *a,
432
    const P256_POINT_AFFINE *b)
433
{
434
    BN_ULONG U2[P256_LIMBS], S2[P256_LIMBS];
435
    BN_ULONG Z1sqr[P256_LIMBS];
436
    BN_ULONG H[P256_LIMBS], R[P256_LIMBS];
437
    BN_ULONG Hsqr[P256_LIMBS];
438
    BN_ULONG Rsqr[P256_LIMBS];
439
    BN_ULONG Hcub[P256_LIMBS];
440
441
    BN_ULONG res_x[P256_LIMBS];
442
    BN_ULONG res_y[P256_LIMBS];
443
    BN_ULONG res_z[P256_LIMBS];
444
445
    BN_ULONG in1infty, in2infty;
446
447
    const BN_ULONG *in1_x = a->X;
448
    const BN_ULONG *in1_y = a->Y;
449
    const BN_ULONG *in1_z = a->Z;
450
451
    const BN_ULONG *in2_x = b->X;
452
    const BN_ULONG *in2_y = b->Y;
453
454
    /*
455
     * Infinity in encoded as (,,0)
456
     */
457
    in1infty = (in1_z[0] | in1_z[1] | in1_z[2] | in1_z[3]);
458
    if (P256_LIMBS == 8)
459
        in1infty |= (in1_z[4] | in1_z[5] | in1_z[6] | in1_z[7]);
460
461
    /*
462
     * In affine representation we encode infinity as (0,0), which is
463
     * not on the curve, so it is OK
464
     */
465
    in2infty = (in2_x[0] | in2_x[1] | in2_x[2] | in2_x[3] | in2_y[0] | in2_y[1] | in2_y[2] | in2_y[3]);
466
    if (P256_LIMBS == 8)
467
        in2infty |= (in2_x[4] | in2_x[5] | in2_x[6] | in2_x[7] | in2_y[4] | in2_y[5] | in2_y[6] | in2_y[7]);
468
469
    in1infty = is_zero(in1infty);
470
    in2infty = is_zero(in2infty);
471
472
    ecp_nistz256_sqr_mont(Z1sqr, in1_z); /* Z1^2 */
473
474
    ecp_nistz256_mul_mont(U2, in2_x, Z1sqr); /* U2 = X2*Z1^2 */
475
    ecp_nistz256_sub(H, U2, in1_x); /* H = U2 - U1 */
476
477
    ecp_nistz256_mul_mont(S2, Z1sqr, in1_z); /* S2 = Z1^3 */
478
479
    ecp_nistz256_mul_mont(res_z, H, in1_z); /* Z3 = H*Z1*Z2 */
480
481
    ecp_nistz256_mul_mont(S2, S2, in2_y); /* S2 = Y2*Z1^3 */
482
    ecp_nistz256_sub(R, S2, in1_y); /* R = S2 - S1 */
483
484
    ecp_nistz256_sqr_mont(Hsqr, H); /* H^2 */
485
    ecp_nistz256_sqr_mont(Rsqr, R); /* R^2 */
486
    ecp_nistz256_mul_mont(Hcub, Hsqr, H); /* H^3 */
487
488
    ecp_nistz256_mul_mont(U2, in1_x, Hsqr); /* U1*H^2 */
489
    ecp_nistz256_mul_by_2(Hsqr, U2); /* 2*U1*H^2 */
490
491
    ecp_nistz256_sub(res_x, Rsqr, Hsqr);
492
    ecp_nistz256_sub(res_x, res_x, Hcub);
493
    ecp_nistz256_sub(H, U2, res_x);
494
495
    ecp_nistz256_mul_mont(S2, in1_y, Hcub);
496
    ecp_nistz256_mul_mont(H, H, R);
497
    ecp_nistz256_sub(res_y, H, S2);
498
499
    copy_conditional(res_x, in2_x, in1infty);
500
    copy_conditional(res_x, in1_x, in2infty);
501
502
    copy_conditional(res_y, in2_y, in1infty);
503
    copy_conditional(res_y, in1_y, in2infty);
504
505
    copy_conditional(res_z, ONE, in1infty);
506
    copy_conditional(res_z, in1_z, in2infty);
507
508
    memcpy(r->X, res_x, sizeof(res_x));
509
    memcpy(r->Y, res_y, sizeof(res_y));
510
    memcpy(r->Z, res_z, sizeof(res_z));
511
}
512
#endif
513
514
/* r = in^-1 mod p */
515
static void ecp_nistz256_mod_inverse(BN_ULONG r[P256_LIMBS],
516
    const BN_ULONG in[P256_LIMBS])
517
0
{
518
    /*
519
     * The poly is ffffffff 00000001 00000000 00000000 00000000 ffffffff
520
     * ffffffff ffffffff We use FLT and used poly-2 as exponent
521
     */
522
0
    BN_ULONG p2[P256_LIMBS];
523
0
    BN_ULONG p4[P256_LIMBS];
524
0
    BN_ULONG p8[P256_LIMBS];
525
0
    BN_ULONG p16[P256_LIMBS];
526
0
    BN_ULONG p32[P256_LIMBS];
527
0
    BN_ULONG res[P256_LIMBS];
528
0
    int i;
529
530
0
    ecp_nistz256_sqr_mont(res, in);
531
0
    ecp_nistz256_mul_mont(p2, res, in); /* 3*p */
532
533
0
    ecp_nistz256_sqr_mont(res, p2);
534
0
    ecp_nistz256_sqr_mont(res, res);
535
0
    ecp_nistz256_mul_mont(p4, res, p2); /* f*p */
536
537
0
    ecp_nistz256_sqr_mont(res, p4);
538
0
    ecp_nistz256_sqr_mont(res, res);
539
0
    ecp_nistz256_sqr_mont(res, res);
540
0
    ecp_nistz256_sqr_mont(res, res);
541
0
    ecp_nistz256_mul_mont(p8, res, p4); /* ff*p */
542
543
0
    ecp_nistz256_sqr_mont(res, p8);
544
0
    for (i = 0; i < 7; i++)
545
0
        ecp_nistz256_sqr_mont(res, res);
546
0
    ecp_nistz256_mul_mont(p16, res, p8); /* ffff*p */
547
548
0
    ecp_nistz256_sqr_mont(res, p16);
549
0
    for (i = 0; i < 15; i++)
550
0
        ecp_nistz256_sqr_mont(res, res);
551
0
    ecp_nistz256_mul_mont(p32, res, p16); /* ffffffff*p */
552
553
0
    ecp_nistz256_sqr_mont(res, p32);
554
0
    for (i = 0; i < 31; i++)
555
0
        ecp_nistz256_sqr_mont(res, res);
556
0
    ecp_nistz256_mul_mont(res, res, in);
557
558
0
    for (i = 0; i < 32 * 4; i++)
559
0
        ecp_nistz256_sqr_mont(res, res);
560
0
    ecp_nistz256_mul_mont(res, res, p32);
561
562
0
    for (i = 0; i < 32; i++)
563
0
        ecp_nistz256_sqr_mont(res, res);
564
0
    ecp_nistz256_mul_mont(res, res, p32);
565
566
0
    for (i = 0; i < 16; i++)
567
0
        ecp_nistz256_sqr_mont(res, res);
568
0
    ecp_nistz256_mul_mont(res, res, p16);
569
570
0
    for (i = 0; i < 8; i++)
571
0
        ecp_nistz256_sqr_mont(res, res);
572
0
    ecp_nistz256_mul_mont(res, res, p8);
573
574
0
    ecp_nistz256_sqr_mont(res, res);
575
0
    ecp_nistz256_sqr_mont(res, res);
576
0
    ecp_nistz256_sqr_mont(res, res);
577
0
    ecp_nistz256_sqr_mont(res, res);
578
0
    ecp_nistz256_mul_mont(res, res, p4);
579
580
0
    ecp_nistz256_sqr_mont(res, res);
581
0
    ecp_nistz256_sqr_mont(res, res);
582
0
    ecp_nistz256_mul_mont(res, res, p2);
583
584
0
    ecp_nistz256_sqr_mont(res, res);
585
0
    ecp_nistz256_sqr_mont(res, res);
586
0
    ecp_nistz256_mul_mont(res, res, in);
587
588
0
    memcpy(r, res, sizeof(res));
589
0
}
590
591
/*
592
 * ecp_nistz256_bignum_to_field_elem copies the contents of |in| to |out| and
593
 * returns one if it fits. Otherwise it returns zero.
594
 */
595
__owur static int ecp_nistz256_bignum_to_field_elem(BN_ULONG out[P256_LIMBS],
596
    const BIGNUM *in)
597
0
{
598
0
    return bn_copy_words(out, in, P256_LIMBS);
599
0
}
600
601
/* r = sum(scalar[i]*point[i]) */
602
__owur static int ecp_nistz256_windowed_mul(const EC_GROUP *group,
603
    P256_POINT *r,
604
    const BIGNUM **scalar,
605
    const EC_POINT **point,
606
    size_t num, BN_CTX *ctx)
607
0
{
608
0
    size_t i;
609
0
    int j, ret = 0;
610
0
    unsigned int idx;
611
0
    unsigned char (*p_str)[33] = NULL;
612
0
    const unsigned int window_size = 5;
613
0
    const unsigned int mask = (1 << (window_size + 1)) - 1;
614
0
    unsigned int wvalue;
615
0
    P256_POINT *temp; /* place for 5 temporary points */
616
0
    const BIGNUM **scalars = NULL;
617
0
    P256_POINT(*table)
618
0
    [16] = NULL;
619
0
    void *table_storage = NULL;
620
621
0
    if ((num * 16 + 6) > OPENSSL_MALLOC_MAX_NELEMS(P256_POINT)
622
0
        || (table = OPENSSL_aligned_alloc_array(num * 16 + 5, sizeof(P256_POINT), 64,
623
0
                &table_storage))
624
0
            == NULL
625
0
        || (p_str = OPENSSL_malloc_array(num, 33)) == NULL
626
0
        || (scalars = OPENSSL_malloc_array(num, sizeof(BIGNUM *))) == NULL)
627
0
        goto err;
628
629
0
    temp = (P256_POINT *)(table + num);
630
631
0
    for (i = 0; i < num; i++) {
632
0
        P256_POINT *row = table[i];
633
634
        /* This is an unusual input, we don't guarantee constant-timeness. */
635
0
        if ((BN_num_bits(scalar[i]) > 256) || BN_is_negative(scalar[i])) {
636
0
            BIGNUM *mod;
637
638
0
            if ((mod = BN_CTX_get(ctx)) == NULL)
639
0
                goto err;
640
0
            if (!BN_nnmod(mod, scalar[i], group->order, ctx)) {
641
0
                ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
642
0
                goto err;
643
0
            }
644
0
            scalars[i] = mod;
645
0
        } else
646
0
            scalars[i] = scalar[i];
647
648
0
        for (j = 0; j < bn_get_top(scalars[i]) * BN_BYTES; j += BN_BYTES) {
649
0
            BN_ULONG d = bn_get_words(scalars[i])[j / BN_BYTES];
650
651
0
            p_str[i][j + 0] = (unsigned char)d;
652
0
            p_str[i][j + 1] = (unsigned char)(d >> 8);
653
0
            p_str[i][j + 2] = (unsigned char)(d >> 16);
654
0
            p_str[i][j + 3] = (unsigned char)(d >>= 24);
655
0
            if (BN_BYTES == 8) {
656
0
                d >>= 8;
657
0
                p_str[i][j + 4] = (unsigned char)d;
658
0
                p_str[i][j + 5] = (unsigned char)(d >> 8);
659
0
                p_str[i][j + 6] = (unsigned char)(d >> 16);
660
0
                p_str[i][j + 7] = (unsigned char)(d >> 24);
661
0
            }
662
0
        }
663
0
        for (; j < 33; j++)
664
0
            p_str[i][j] = 0;
665
666
0
        if (!ecp_nistz256_bignum_to_field_elem(temp[0].X, point[i]->X)
667
0
            || !ecp_nistz256_bignum_to_field_elem(temp[0].Y, point[i]->Y)
668
0
            || !ecp_nistz256_bignum_to_field_elem(temp[0].Z, point[i]->Z)) {
669
0
            ERR_raise(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
670
0
            goto err;
671
0
        }
672
673
        /*
674
         * row[0] is implicitly (0,0,0) (the point at infinity), therefore it
675
         * is not stored. All other values are actually stored with an offset
676
         * of -1 in table.
677
         */
678
679
0
        ecp_nistz256_scatter_w5(row, &temp[0], 1);
680
0
        ecp_nistz256_point_double(&temp[1], &temp[0]); /*1+1=2  */
681
0
        ecp_nistz256_scatter_w5(row, &temp[1], 2);
682
0
        ecp_nistz256_point_add(&temp[2], &temp[1], &temp[0]); /*2+1=3  */
683
0
        ecp_nistz256_scatter_w5(row, &temp[2], 3);
684
0
        ecp_nistz256_point_double(&temp[1], &temp[1]); /*2*2=4  */
685
0
        ecp_nistz256_scatter_w5(row, &temp[1], 4);
686
0
        ecp_nistz256_point_double(&temp[2], &temp[2]); /*2*3=6  */
687
0
        ecp_nistz256_scatter_w5(row, &temp[2], 6);
688
0
        ecp_nistz256_point_add(&temp[3], &temp[1], &temp[0]); /*4+1=5  */
689
0
        ecp_nistz256_scatter_w5(row, &temp[3], 5);
690
0
        ecp_nistz256_point_add(&temp[4], &temp[2], &temp[0]); /*6+1=7  */
691
0
        ecp_nistz256_scatter_w5(row, &temp[4], 7);
692
0
        ecp_nistz256_point_double(&temp[1], &temp[1]); /*2*4=8  */
693
0
        ecp_nistz256_scatter_w5(row, &temp[1], 8);
694
0
        ecp_nistz256_point_double(&temp[2], &temp[2]); /*2*6=12 */
695
0
        ecp_nistz256_scatter_w5(row, &temp[2], 12);
696
0
        ecp_nistz256_point_double(&temp[3], &temp[3]); /*2*5=10 */
697
0
        ecp_nistz256_scatter_w5(row, &temp[3], 10);
698
0
        ecp_nistz256_point_double(&temp[4], &temp[4]); /*2*7=14 */
699
0
        ecp_nistz256_scatter_w5(row, &temp[4], 14);
700
0
        ecp_nistz256_point_add(&temp[2], &temp[2], &temp[0]); /*12+1=13*/
701
0
        ecp_nistz256_scatter_w5(row, &temp[2], 13);
702
0
        ecp_nistz256_point_add(&temp[3], &temp[3], &temp[0]); /*10+1=11*/
703
0
        ecp_nistz256_scatter_w5(row, &temp[3], 11);
704
0
        ecp_nistz256_point_add(&temp[4], &temp[4], &temp[0]); /*14+1=15*/
705
0
        ecp_nistz256_scatter_w5(row, &temp[4], 15);
706
0
        ecp_nistz256_point_add(&temp[2], &temp[1], &temp[0]); /*8+1=9  */
707
0
        ecp_nistz256_scatter_w5(row, &temp[2], 9);
708
0
        ecp_nistz256_point_double(&temp[1], &temp[1]); /*2*8=16 */
709
0
        ecp_nistz256_scatter_w5(row, &temp[1], 16);
710
0
    }
711
712
0
    idx = 255;
713
714
0
    wvalue = p_str[0][(idx - 1) / 8];
715
0
    wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
716
717
    /*
718
     * We gather to temp[0], because we know it's position relative
719
     * to table
720
     */
721
0
    ecp_nistz256_gather_w5(&temp[0], table[0], _booth_recode_w5(wvalue) >> 1);
722
0
    memcpy(r, &temp[0], sizeof(temp[0]));
723
724
0
    while (idx >= 5) {
725
0
        for (i = (idx == 255 ? 1 : 0); i < num; i++) {
726
0
            unsigned int off = (idx - 1) / 8;
727
728
0
            wvalue = p_str[i][off] | p_str[i][off + 1] << 8;
729
0
            wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
730
731
0
            wvalue = _booth_recode_w5(wvalue);
732
733
0
            ecp_nistz256_gather_w5(&temp[0], table[i], wvalue >> 1);
734
735
0
            ecp_nistz256_neg(temp[1].Y, temp[0].Y);
736
0
            copy_conditional(temp[0].Y, temp[1].Y, (wvalue & 1));
737
738
0
            ecp_nistz256_point_add(r, r, &temp[0]);
739
0
        }
740
741
0
        idx -= window_size;
742
743
0
        ecp_nistz256_point_double(r, r);
744
0
        ecp_nistz256_point_double(r, r);
745
0
        ecp_nistz256_point_double(r, r);
746
0
        ecp_nistz256_point_double(r, r);
747
0
        ecp_nistz256_point_double(r, r);
748
0
    }
749
750
    /* Final window */
751
0
    for (i = 0; i < num; i++) {
752
0
        wvalue = p_str[i][0];
753
0
        wvalue = (wvalue << 1) & mask;
754
755
0
        wvalue = _booth_recode_w5(wvalue);
756
757
0
        ecp_nistz256_gather_w5(&temp[0], table[i], wvalue >> 1);
758
759
0
        ecp_nistz256_neg(temp[1].Y, temp[0].Y);
760
0
        copy_conditional(temp[0].Y, temp[1].Y, wvalue & 1);
761
762
0
        ecp_nistz256_point_add(r, r, &temp[0]);
763
0
    }
764
765
0
    ret = 1;
766
0
err:
767
0
    OPENSSL_free(table_storage);
768
0
    OPENSSL_free(p_str);
769
0
    OPENSSL_free(scalars);
770
0
    return ret;
771
0
}
772
773
/* Coordinates of G, for which we have precomputed tables */
774
static const BN_ULONG def_xG[P256_LIMBS] = {
775
    TOBN(0x79e730d4, 0x18a9143c), TOBN(0x75ba95fc, 0x5fedb601),
776
    TOBN(0x79fb732b, 0x77622510), TOBN(0x18905f76, 0xa53755c6)
777
};
778
779
static const BN_ULONG def_yG[P256_LIMBS] = {
780
    TOBN(0xddf25357, 0xce95560a), TOBN(0x8b4ab8e4, 0xba19e45c),
781
    TOBN(0xd2e88688, 0xdd21f325), TOBN(0x8571ff18, 0x25885d85)
782
};
783
784
/*
785
 * ecp_nistz256_is_affine_G returns one if |generator| is the standard, P-256
786
 * generator.
787
 */
788
static int ecp_nistz256_is_affine_G(const EC_POINT *generator)
789
0
{
790
0
    return (bn_get_top(generator->X) == P256_LIMBS) && (bn_get_top(generator->Y) == P256_LIMBS) && is_equal(bn_get_words(generator->X), def_xG) && is_equal(bn_get_words(generator->Y), def_yG) && is_one(generator->Z);
791
0
}
792
793
__owur static int ecp_nistz256_mult_precompute(EC_GROUP *group, BN_CTX *ctx)
794
0
{
795
    /*
796
     * We precompute a table for a Booth encoded exponent (wNAF) based
797
     * computation. Each table holds 64 values for safe access, with an
798
     * implicit value of infinity at index zero. We use window of size 7, and
799
     * therefore require ceil(256/7) = 37 tables.
800
     */
801
0
    const BIGNUM *order;
802
0
    EC_POINT *P = NULL, *T = NULL;
803
0
    const EC_POINT *generator;
804
0
    NISTZ256_PRE_COMP *pre_comp;
805
0
    BN_CTX *new_ctx = NULL;
806
0
    int i, j, k, ret = 0;
807
0
    size_t w;
808
809
0
    PRECOMP256_ROW *preComputedTable = NULL;
810
0
    void *precomp_storage = NULL;
811
812
    /* if there is an old NISTZ256_PRE_COMP object, throw it away */
813
0
    EC_pre_comp_free(group);
814
0
    generator = EC_GROUP_get0_generator(group);
815
0
    if (generator == NULL) {
816
0
        ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR);
817
0
        return 0;
818
0
    }
819
820
0
    if (ecp_nistz256_is_affine_G(generator)) {
821
        /*
822
         * No need to calculate tables for the standard generator because we
823
         * have them statically.
824
         */
825
0
        return 1;
826
0
    }
827
828
0
    if ((pre_comp = ecp_nistz256_pre_comp_new(group)) == NULL)
829
0
        return 0;
830
831
0
    if (ctx == NULL) {
832
0
        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
833
0
        if (ctx == NULL)
834
0
            goto err;
835
0
    }
836
837
0
    BN_CTX_start(ctx);
838
839
0
    order = EC_GROUP_get0_order(group);
840
0
    if (order == NULL)
841
0
        goto err;
842
843
0
    if (BN_is_zero(order)) {
844
0
        ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_ORDER);
845
0
        goto err;
846
0
    }
847
848
0
    w = 7;
849
850
0
    if ((preComputedTable = OPENSSL_aligned_alloc_array(37 * 64, sizeof(P256_POINT_AFFINE), 64,
851
0
             &precomp_storage))
852
0
        == NULL)
853
0
        goto err;
854
855
0
    P = EC_POINT_new(group);
856
0
    T = EC_POINT_new(group);
857
0
    if (P == NULL || T == NULL)
858
0
        goto err;
859
860
    /*
861
     * The zero entry is implicitly infinity, and we skip it, storing other
862
     * values with -1 offset.
863
     */
864
0
    if (!EC_POINT_copy(T, generator))
865
0
        goto err;
866
867
0
    for (k = 0; k < 64; k++) {
868
0
        if (!EC_POINT_copy(P, T))
869
0
            goto err;
870
0
        for (j = 0; j < 37; j++) {
871
0
            P256_POINT_AFFINE temp;
872
            /*
873
             * It would be faster to use EC_POINTs_make_affine and
874
             * make multiple points affine at the same time.
875
             */
876
0
            if (group->meth->make_affine == NULL
877
0
                || !group->meth->make_affine(group, P, ctx))
878
0
                goto err;
879
0
            if (!ecp_nistz256_bignum_to_field_elem(temp.X, P->X) || !ecp_nistz256_bignum_to_field_elem(temp.Y, P->Y)) {
880
0
                ERR_raise(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
881
0
                goto err;
882
0
            }
883
0
            ecp_nistz256_scatter_w7(preComputedTable[j], &temp, k);
884
0
            for (i = 0; i < 7; i++) {
885
0
                if (!EC_POINT_dbl(group, P, P, ctx))
886
0
                    goto err;
887
0
            }
888
0
        }
889
0
        if (!EC_POINT_add(group, T, T, generator, ctx))
890
0
            goto err;
891
0
    }
892
893
0
    pre_comp->group = group;
894
0
    pre_comp->w = w;
895
0
    pre_comp->precomp = preComputedTable;
896
0
    pre_comp->precomp_storage = precomp_storage;
897
0
    precomp_storage = NULL;
898
0
    SETPRECOMP(group, nistz256, pre_comp);
899
0
    pre_comp = NULL;
900
0
    ret = 1;
901
902
0
err:
903
0
    BN_CTX_end(ctx);
904
0
    BN_CTX_free(new_ctx);
905
906
0
    EC_nistz256_pre_comp_free(pre_comp);
907
0
    OPENSSL_free(precomp_storage);
908
0
    EC_POINT_free(P);
909
0
    EC_POINT_free(T);
910
0
    return ret;
911
0
}
912
913
__owur static int ecp_nistz256_set_from_affine(EC_POINT *out, const EC_GROUP *group,
914
    const P256_POINT_AFFINE *in,
915
    BN_CTX *ctx)
916
0
{
917
0
    int ret = 0;
918
919
0
    if ((ret = bn_set_words(out->X, in->X, P256_LIMBS))
920
0
        && (ret = bn_set_words(out->Y, in->Y, P256_LIMBS))
921
0
        && (ret = bn_set_words(out->Z, ONE, P256_LIMBS)))
922
0
        out->Z_is_one = 1;
923
924
0
    return ret;
925
0
}
926
927
/* r = scalar*G + sum(scalars[i]*points[i]) */
928
__owur static int ecp_nistz256_points_mul(const EC_GROUP *group,
929
    EC_POINT *r,
930
    const BIGNUM *scalar,
931
    size_t num,
932
    const EC_POINT *points[],
933
    const BIGNUM *scalars[], BN_CTX *ctx)
934
0
{
935
0
    int i = 0, ret = 0, no_precomp_for_generator = 0, p_is_infinity = 0;
936
0
    unsigned char p_str[33] = { 0 };
937
0
    const PRECOMP256_ROW *preComputedTable = NULL;
938
0
    const NISTZ256_PRE_COMP *pre_comp = NULL;
939
0
    const EC_POINT *generator = NULL;
940
0
    const BIGNUM **new_scalars = NULL;
941
0
    const EC_POINT **new_points = NULL;
942
0
    unsigned int idx = 0;
943
0
    const unsigned int window_size = 7;
944
0
    const unsigned int mask = (1 << (window_size + 1)) - 1;
945
0
    unsigned int wvalue;
946
0
    ALIGN32 union {
947
0
        P256_POINT p;
948
0
        P256_POINT_AFFINE a;
949
0
    } t, p;
950
0
    BIGNUM *tmp_scalar;
951
952
0
    if ((num + 1) == 0 || (num + 1) > OPENSSL_MALLOC_MAX_NELEMS(void *)) {
953
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_INVALID_ARGUMENT);
954
0
        return 0;
955
0
    }
956
957
0
    memset(&p, 0, sizeof(p));
958
0
    BN_CTX_start(ctx);
959
960
0
    if (scalar) {
961
0
        generator = EC_GROUP_get0_generator(group);
962
0
        if (generator == NULL) {
963
0
            ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR);
964
0
            goto err;
965
0
        }
966
967
        /* look if we can use precomputed multiples of generator */
968
0
        pre_comp = group->pre_comp.nistz256;
969
970
0
        if (pre_comp) {
971
            /*
972
             * If there is a precomputed table for the generator, check that
973
             * it was generated with the same generator.
974
             */
975
0
            EC_POINT *pre_comp_generator = EC_POINT_new(group);
976
0
            if (pre_comp_generator == NULL)
977
0
                goto err;
978
979
0
            ecp_nistz256_gather_w7(&p.a, pre_comp->precomp[0], 1);
980
0
            if (!ecp_nistz256_set_from_affine(pre_comp_generator,
981
0
                    group, &p.a, ctx)) {
982
0
                EC_POINT_free(pre_comp_generator);
983
0
                goto err;
984
0
            }
985
986
0
            if (0 == EC_POINT_cmp(group, generator, pre_comp_generator, ctx))
987
0
                preComputedTable = (const PRECOMP256_ROW *)pre_comp->precomp;
988
989
0
            EC_POINT_free(pre_comp_generator);
990
0
        }
991
992
0
        if (preComputedTable == NULL && ecp_nistz256_is_affine_G(generator)) {
993
            /*
994
             * If there is no precomputed data, but the generator is the
995
             * default, a hardcoded table of precomputed data is used. This
996
             * is because applications, such as Apache, do not use
997
             * EC_KEY_precompute_mult.
998
             */
999
0
            preComputedTable = ecp_nistz256_precomputed;
1000
0
        }
1001
1002
0
        if (preComputedTable) {
1003
0
            BN_ULONG infty;
1004
1005
0
            if ((BN_num_bits(scalar) > 256)
1006
0
                || BN_is_negative(scalar)) {
1007
0
                if ((tmp_scalar = BN_CTX_get(ctx)) == NULL)
1008
0
                    goto err;
1009
1010
0
                if (!BN_nnmod(tmp_scalar, scalar, group->order, ctx)) {
1011
0
                    ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
1012
0
                    goto err;
1013
0
                }
1014
0
                scalar = tmp_scalar;
1015
0
            }
1016
1017
0
            for (i = 0; i < bn_get_top(scalar) * BN_BYTES; i += BN_BYTES) {
1018
0
                BN_ULONG d = bn_get_words(scalar)[i / BN_BYTES];
1019
1020
0
                p_str[i + 0] = (unsigned char)d;
1021
0
                p_str[i + 1] = (unsigned char)(d >> 8);
1022
0
                p_str[i + 2] = (unsigned char)(d >> 16);
1023
0
                p_str[i + 3] = (unsigned char)(d >>= 24);
1024
0
                if (BN_BYTES == 8) {
1025
0
                    d >>= 8;
1026
0
                    p_str[i + 4] = (unsigned char)d;
1027
0
                    p_str[i + 5] = (unsigned char)(d >> 8);
1028
0
                    p_str[i + 6] = (unsigned char)(d >> 16);
1029
0
                    p_str[i + 7] = (unsigned char)(d >> 24);
1030
0
                }
1031
0
            }
1032
1033
0
            for (; i < 33; i++)
1034
0
                p_str[i] = 0;
1035
1036
            /* First window */
1037
0
            wvalue = (p_str[0] << 1) & mask;
1038
0
            idx += window_size;
1039
1040
0
            wvalue = _booth_recode_w7(wvalue);
1041
1042
0
            ecp_nistz256_gather_w7(&p.a, preComputedTable[0],
1043
0
                wvalue >> 1);
1044
1045
0
            ecp_nistz256_neg(p.p.Z, p.p.Y);
1046
0
            copy_conditional(p.p.Y, p.p.Z, wvalue & 1);
1047
1048
            /*
1049
             * Since affine infinity is encoded as (0,0) and
1050
             * Jacobian is (,,0), we need to harmonize them
1051
             * by assigning "one" or zero to Z.
1052
             */
1053
0
            infty = (p.p.X[0] | p.p.X[1] | p.p.X[2] | p.p.X[3] | p.p.Y[0] | p.p.Y[1] | p.p.Y[2] | p.p.Y[3]);
1054
0
            if (P256_LIMBS == 8)
1055
0
                infty |= (p.p.X[4] | p.p.X[5] | p.p.X[6] | p.p.X[7] | p.p.Y[4] | p.p.Y[5] | p.p.Y[6] | p.p.Y[7]);
1056
1057
0
            infty = 0 - is_zero(infty);
1058
0
            infty = ~infty;
1059
1060
0
            p.p.Z[0] = ONE[0] & infty;
1061
0
            p.p.Z[1] = ONE[1] & infty;
1062
0
            p.p.Z[2] = ONE[2] & infty;
1063
0
            p.p.Z[3] = ONE[3] & infty;
1064
0
            if (P256_LIMBS == 8) {
1065
0
                p.p.Z[4] = ONE[4] & infty;
1066
0
                p.p.Z[5] = ONE[5] & infty;
1067
0
                p.p.Z[6] = ONE[6] & infty;
1068
0
                p.p.Z[7] = ONE[7] & infty;
1069
0
            }
1070
1071
0
            for (i = 1; i < 37; i++) {
1072
0
                unsigned int off = (idx - 1) / 8;
1073
0
                wvalue = p_str[off] | p_str[off + 1] << 8;
1074
0
                wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
1075
0
                idx += window_size;
1076
1077
0
                wvalue = _booth_recode_w7(wvalue);
1078
1079
0
                ecp_nistz256_gather_w7(&t.a,
1080
0
                    preComputedTable[i], wvalue >> 1);
1081
1082
0
                ecp_nistz256_neg(t.p.Z, t.a.Y);
1083
0
                copy_conditional(t.a.Y, t.p.Z, wvalue & 1);
1084
1085
0
                ecp_nistz256_point_add_affine(&p.p, &p.p, &t.a);
1086
0
            }
1087
0
        } else {
1088
0
            p_is_infinity = 1;
1089
0
            no_precomp_for_generator = 1;
1090
0
        }
1091
0
    } else
1092
0
        p_is_infinity = 1;
1093
1094
0
    if (no_precomp_for_generator) {
1095
        /*
1096
         * Without a precomputed table for the generator, it has to be
1097
         * handled like a normal point.
1098
         */
1099
0
        new_scalars = OPENSSL_malloc_array(num + 1, sizeof(BIGNUM *));
1100
0
        if (new_scalars == NULL)
1101
0
            goto err;
1102
1103
0
        new_points = OPENSSL_malloc_array(num + 1, sizeof(EC_POINT *));
1104
0
        if (new_points == NULL)
1105
0
            goto err;
1106
1107
0
        memcpy(new_scalars, scalars, num * sizeof(BIGNUM *));
1108
0
        new_scalars[num] = scalar;
1109
0
        memcpy(new_points, points, num * sizeof(EC_POINT *));
1110
0
        new_points[num] = generator;
1111
1112
0
        scalars = new_scalars;
1113
0
        points = new_points;
1114
0
        num++;
1115
0
    }
1116
1117
0
    if (num) {
1118
0
        P256_POINT *out = &t.p;
1119
0
        if (p_is_infinity)
1120
0
            out = &p.p;
1121
1122
0
        if (!ecp_nistz256_windowed_mul(group, out, scalars, points, num, ctx))
1123
0
            goto err;
1124
1125
0
        if (!p_is_infinity)
1126
0
            ecp_nistz256_point_add(&p.p, &p.p, out);
1127
0
    }
1128
1129
    /* Not constant-time, but we're only operating on the public output. */
1130
0
    if (!bn_set_words(r->X, p.p.X, P256_LIMBS) || !bn_set_words(r->Y, p.p.Y, P256_LIMBS) || !bn_set_words(r->Z, p.p.Z, P256_LIMBS)) {
1131
0
        goto err;
1132
0
    }
1133
0
    r->Z_is_one = is_one(r->Z) & 1;
1134
1135
0
    ret = 1;
1136
1137
0
err:
1138
0
    BN_CTX_end(ctx);
1139
0
    OPENSSL_free(new_points);
1140
0
    OPENSSL_free(new_scalars);
1141
0
    return ret;
1142
0
}
1143
1144
__owur static int ecp_nistz256_get_affine(const EC_GROUP *group,
1145
    const EC_POINT *point,
1146
    BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
1147
0
{
1148
0
    BN_ULONG z_inv2[P256_LIMBS];
1149
0
    BN_ULONG z_inv3[P256_LIMBS];
1150
0
    BN_ULONG x_aff[P256_LIMBS];
1151
0
    BN_ULONG y_aff[P256_LIMBS];
1152
0
    BN_ULONG point_x[P256_LIMBS], point_y[P256_LIMBS], point_z[P256_LIMBS];
1153
0
    BN_ULONG x_ret[P256_LIMBS], y_ret[P256_LIMBS];
1154
1155
0
    if (EC_POINT_is_at_infinity(group, point)) {
1156
0
        ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
1157
0
        return 0;
1158
0
    }
1159
1160
0
    if (!ecp_nistz256_bignum_to_field_elem(point_x, point->X) || !ecp_nistz256_bignum_to_field_elem(point_y, point->Y) || !ecp_nistz256_bignum_to_field_elem(point_z, point->Z)) {
1161
0
        ERR_raise(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
1162
0
        return 0;
1163
0
    }
1164
1165
0
    ecp_nistz256_mod_inverse(z_inv3, point_z);
1166
0
    ecp_nistz256_sqr_mont(z_inv2, z_inv3);
1167
0
    ecp_nistz256_mul_mont(x_aff, z_inv2, point_x);
1168
1169
0
    if (x != NULL) {
1170
0
        ecp_nistz256_from_mont(x_ret, x_aff);
1171
0
        if (!bn_set_words(x, x_ret, P256_LIMBS))
1172
0
            return 0;
1173
0
    }
1174
1175
0
    if (y != NULL) {
1176
0
        ecp_nistz256_mul_mont(z_inv3, z_inv3, z_inv2);
1177
0
        ecp_nistz256_mul_mont(y_aff, z_inv3, point_y);
1178
0
        ecp_nistz256_from_mont(y_ret, y_aff);
1179
0
        if (!bn_set_words(y, y_ret, P256_LIMBS))
1180
0
            return 0;
1181
0
    }
1182
1183
0
    return 1;
1184
0
}
1185
1186
static NISTZ256_PRE_COMP *ecp_nistz256_pre_comp_new(const EC_GROUP *group)
1187
0
{
1188
0
    NISTZ256_PRE_COMP *ret = NULL;
1189
1190
0
    if (!group)
1191
0
        return NULL;
1192
1193
0
    ret = OPENSSL_zalloc(sizeof(*ret));
1194
1195
0
    if (ret == NULL)
1196
0
        return ret;
1197
1198
0
    ret->group = group;
1199
0
    ret->w = 6; /* default */
1200
1201
0
    if (!CRYPTO_NEW_REF(&ret->references, 1)) {
1202
0
        OPENSSL_free(ret);
1203
0
        return NULL;
1204
0
    }
1205
0
    return ret;
1206
0
}
1207
1208
NISTZ256_PRE_COMP *EC_nistz256_pre_comp_dup(NISTZ256_PRE_COMP *p)
1209
0
{
1210
0
    int i;
1211
0
    if (p != NULL)
1212
0
        CRYPTO_UP_REF(&p->references, &i);
1213
0
    return p;
1214
0
}
1215
1216
void EC_nistz256_pre_comp_free(NISTZ256_PRE_COMP *pre)
1217
0
{
1218
0
    int i;
1219
1220
0
    if (pre == NULL)
1221
0
        return;
1222
1223
0
    CRYPTO_DOWN_REF(&pre->references, &i);
1224
0
    REF_PRINT_COUNT("EC_nistz256", i, pre);
1225
0
    if (i > 0)
1226
0
        return;
1227
0
    REF_ASSERT_ISNT(i < 0);
1228
1229
0
    OPENSSL_free(pre->precomp_storage);
1230
0
    CRYPTO_FREE_REF(&pre->references);
1231
0
    OPENSSL_free(pre);
1232
0
}
1233
1234
static int ecp_nistz256_window_have_precompute_mult(const EC_GROUP *group)
1235
0
{
1236
    /* There is a hard-coded table for the default generator. */
1237
0
    const EC_POINT *generator = EC_GROUP_get0_generator(group);
1238
1239
0
    if (generator != NULL && ecp_nistz256_is_affine_G(generator)) {
1240
        /* There is a hard-coded table for the default generator. */
1241
0
        return 1;
1242
0
    }
1243
1244
0
    return HAVEPRECOMP(group, nistz256);
1245
0
}
1246
1247
#if defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64) || defined(__powerpc64__) || defined(_ARCH_PP64) || defined(__aarch64__)
1248
/*
1249
 * Montgomery mul modulo Order(P): res = a*b*2^-256 mod Order(P)
1250
 */
1251
void ecp_nistz256_ord_mul_mont(BN_ULONG res[P256_LIMBS],
1252
    const BN_ULONG a[P256_LIMBS],
1253
    const BN_ULONG b[P256_LIMBS]);
1254
void ecp_nistz256_ord_sqr_mont(BN_ULONG res[P256_LIMBS],
1255
    const BN_ULONG a[P256_LIMBS],
1256
    BN_ULONG rep);
1257
1258
static int ecp_nistz256_inv_mod_ord(const EC_GROUP *group, BIGNUM *r,
1259
    const BIGNUM *x, BN_CTX *ctx)
1260
0
{
1261
    /* RR = 2^512 mod ord(p256) */
1262
0
    static const BN_ULONG RR[P256_LIMBS] = {
1263
0
        TOBN(0x83244c95, 0xbe79eea2), TOBN(0x4699799c, 0x49bd6fa6),
1264
0
        TOBN(0x2845b239, 0x2b6bec59), TOBN(0x66e12d94, 0xf3d95620)
1265
0
    };
1266
    /* The constant 1 (unlike ONE that is one in Montgomery representation) */
1267
0
    static const BN_ULONG one[P256_LIMBS] = {
1268
0
        TOBN(0, 1), TOBN(0, 0), TOBN(0, 0), TOBN(0, 0)
1269
0
    };
1270
    /*
1271
     * We don't use entry 0 in the table, so we omit it and address
1272
     * with -1 offset.
1273
     */
1274
0
    BN_ULONG table[15][P256_LIMBS];
1275
0
    BN_ULONG out[P256_LIMBS], t[P256_LIMBS];
1276
0
    int i, ret = 0;
1277
0
    enum {
1278
0
        i_1 = 0,
1279
0
        i_10,
1280
0
        i_11,
1281
0
        i_101,
1282
0
        i_111,
1283
0
        i_1010,
1284
0
        i_1111,
1285
0
        i_10101,
1286
0
        i_101010,
1287
0
        i_101111,
1288
0
        i_x6,
1289
0
        i_x8,
1290
0
        i_x16,
1291
0
        i_x32
1292
0
    };
1293
1294
    /*
1295
     * Catch allocation failure early.
1296
     */
1297
0
    if (bn_wexpand(r, P256_LIMBS) == NULL) {
1298
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
1299
0
        goto err;
1300
0
    }
1301
1302
0
    if ((BN_num_bits(x) > 256) || BN_is_negative(x)) {
1303
0
        BIGNUM *tmp;
1304
1305
0
        if ((tmp = BN_CTX_get(ctx)) == NULL
1306
0
            || !BN_nnmod(tmp, x, group->order, ctx)) {
1307
0
            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
1308
0
            goto err;
1309
0
        }
1310
0
        x = tmp;
1311
0
    }
1312
1313
0
    if (!ecp_nistz256_bignum_to_field_elem(t, x)) {
1314
0
        ERR_raise(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
1315
0
        goto err;
1316
0
    }
1317
1318
0
    ecp_nistz256_ord_mul_mont(table[0], t, RR);
1319
#if 0
1320
    /*
1321
     * Original sparse-then-fixed-window algorithm, retained for reference.
1322
     */
1323
    for (i = 2; i < 16; i += 2) {
1324
        ecp_nistz256_ord_sqr_mont(table[i-1], table[i/2-1], 1);
1325
        ecp_nistz256_ord_mul_mont(table[i], table[i-1], table[0]);
1326
    }
1327
1328
    /*
1329
     * The top 128bit of the exponent are highly redudndant, so we
1330
     * perform an optimized flow
1331
     */
1332
    ecp_nistz256_ord_sqr_mont(t, table[15-1], 4);   /* f0 */
1333
    ecp_nistz256_ord_mul_mont(t, t, table[15-1]);   /* ff */
1334
1335
    ecp_nistz256_ord_sqr_mont(out, t, 8);           /* ff00 */
1336
    ecp_nistz256_ord_mul_mont(out, out, t);         /* ffff */
1337
1338
    ecp_nistz256_ord_sqr_mont(t, out, 16);          /* ffff0000 */
1339
    ecp_nistz256_ord_mul_mont(t, t, out);           /* ffffffff */
1340
1341
    ecp_nistz256_ord_sqr_mont(out, t, 64);          /* ffffffff0000000000000000 */
1342
    ecp_nistz256_ord_mul_mont(out, out, t);         /* ffffffff00000000ffffffff */
1343
1344
    ecp_nistz256_ord_sqr_mont(out, out, 32);        /* ffffffff00000000ffffffff00000000 */
1345
    ecp_nistz256_ord_mul_mont(out, out, t);         /* ffffffff00000000ffffffffffffffff */
1346
1347
    /*
1348
     * The bottom 128 bit of the exponent are processed with fixed 4-bit window
1349
     */
1350
    for (i = 0; i < 32; i++) {
1351
        /* expLo - the low 128 bits of the exponent we use (ord(p256) - 2),
1352
         * split into nibbles */
1353
        static const unsigned char expLo[32]  = {
1354
            0xb,0xc,0xe,0x6,0xf,0xa,0xa,0xd,0xa,0x7,0x1,0x7,0x9,0xe,0x8,0x4,
1355
            0xf,0x3,0xb,0x9,0xc,0xa,0xc,0x2,0xf,0xc,0x6,0x3,0x2,0x5,0x4,0xf
1356
        };
1357
1358
        ecp_nistz256_ord_sqr_mont(out, out, 4);
1359
        /* The exponent is public, no need in constant-time access */
1360
        ecp_nistz256_ord_mul_mont(out, out, table[expLo[i]-1]);
1361
    }
1362
#else
1363
    /*
1364
     * https://briansmith.org/ecc-inversion-addition-chains-01#p256_scalar_inversion
1365
     *
1366
     * Even though this code path spares 12 squarings, 4.5%, and 13
1367
     * multiplications, 25%, on grand scale sign operation is not that
1368
     * much faster, not more that 2%...
1369
     */
1370
1371
    /* pre-calculate powers */
1372
0
    ecp_nistz256_ord_sqr_mont(table[i_10], table[i_1], 1);
1373
1374
0
    ecp_nistz256_ord_mul_mont(table[i_11], table[i_1], table[i_10]);
1375
1376
0
    ecp_nistz256_ord_mul_mont(table[i_101], table[i_11], table[i_10]);
1377
1378
0
    ecp_nistz256_ord_mul_mont(table[i_111], table[i_101], table[i_10]);
1379
1380
0
    ecp_nistz256_ord_sqr_mont(table[i_1010], table[i_101], 1);
1381
1382
0
    ecp_nistz256_ord_mul_mont(table[i_1111], table[i_1010], table[i_101]);
1383
1384
0
    ecp_nistz256_ord_sqr_mont(table[i_10101], table[i_1010], 1);
1385
0
    ecp_nistz256_ord_mul_mont(table[i_10101], table[i_10101], table[i_1]);
1386
1387
0
    ecp_nistz256_ord_sqr_mont(table[i_101010], table[i_10101], 1);
1388
1389
0
    ecp_nistz256_ord_mul_mont(table[i_101111], table[i_101010], table[i_101]);
1390
1391
0
    ecp_nistz256_ord_mul_mont(table[i_x6], table[i_101010], table[i_10101]);
1392
1393
0
    ecp_nistz256_ord_sqr_mont(table[i_x8], table[i_x6], 2);
1394
0
    ecp_nistz256_ord_mul_mont(table[i_x8], table[i_x8], table[i_11]);
1395
1396
0
    ecp_nistz256_ord_sqr_mont(table[i_x16], table[i_x8], 8);
1397
0
    ecp_nistz256_ord_mul_mont(table[i_x16], table[i_x16], table[i_x8]);
1398
1399
0
    ecp_nistz256_ord_sqr_mont(table[i_x32], table[i_x16], 16);
1400
0
    ecp_nistz256_ord_mul_mont(table[i_x32], table[i_x32], table[i_x16]);
1401
1402
    /* calculations */
1403
0
    ecp_nistz256_ord_sqr_mont(out, table[i_x32], 64);
1404
0
    ecp_nistz256_ord_mul_mont(out, out, table[i_x32]);
1405
1406
0
    for (i = 0; i < 27; i++) {
1407
0
        static const struct {
1408
0
            unsigned char p, i;
1409
0
        } chain[27] = {
1410
0
            { 32, i_x32 }, { 6, i_101111 }, { 5, i_111 },
1411
0
            { 4, i_11 }, { 5, i_1111 }, { 5, i_10101 },
1412
0
            { 4, i_101 }, { 3, i_101 }, { 3, i_101 },
1413
0
            { 5, i_111 }, { 9, i_101111 }, { 6, i_1111 },
1414
0
            { 2, i_1 }, { 5, i_1 }, { 6, i_1111 },
1415
0
            { 5, i_111 }, { 4, i_111 }, { 5, i_111 },
1416
0
            { 5, i_101 }, { 3, i_11 }, { 10, i_101111 },
1417
0
            { 2, i_11 }, { 5, i_11 }, { 5, i_11 },
1418
0
            { 3, i_1 }, { 7, i_10101 }, { 6, i_1111 }
1419
0
        };
1420
1421
0
        ecp_nistz256_ord_sqr_mont(out, out, chain[i].p);
1422
0
        ecp_nistz256_ord_mul_mont(out, out, table[chain[i].i]);
1423
0
    }
1424
0
#endif
1425
0
    ecp_nistz256_ord_mul_mont(out, out, one);
1426
1427
    /*
1428
     * Can't fail, but check return code to be consistent anyway.
1429
     */
1430
0
    if (!bn_set_words(r, out, P256_LIMBS))
1431
0
        goto err;
1432
1433
0
    ret = 1;
1434
0
err:
1435
0
    return ret;
1436
0
}
1437
#else
1438
#define ecp_nistz256_inv_mod_ord NULL
1439
#endif
1440
1441
static int ecp_nistz256group_full_init(EC_GROUP *group,
1442
    const unsigned char *params)
1443
0
{
1444
0
    BN_CTX *ctx = NULL;
1445
0
    BN_MONT_CTX *mont = NULL, *ordmont = NULL;
1446
0
    const int param_len = 32;
1447
0
    const int seed_len = 20;
1448
0
    int ok = 0;
1449
0
    uint32_t hi_order_n = 0xccd1c8aa;
1450
0
    uint32_t lo_order_n = 0xee00bc4f;
1451
0
    BIGNUM *p = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL, *one = NULL,
1452
0
           *order = NULL;
1453
0
    EC_POINT *P = NULL;
1454
1455
0
    if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL) {
1456
0
        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
1457
0
        return 0;
1458
0
    }
1459
1460
0
    if (!EC_GROUP_set_seed(group, params, seed_len)) {
1461
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1462
0
        goto err;
1463
0
    }
1464
0
    params += seed_len;
1465
1466
0
    if ((p = BN_bin2bn(params + 0 * param_len, param_len, NULL)) == NULL
1467
0
        || (a = BN_bin2bn(params + 1 * param_len, param_len, NULL)) == NULL
1468
0
        || (b = BN_bin2bn(params + 2 * param_len, param_len, NULL)) == NULL) {
1469
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
1470
0
        goto err;
1471
0
    }
1472
1473
    /*
1474
     * Set up curve params and montgomery for field
1475
     * Start by setting up montgomery and one
1476
     */
1477
0
    mont = BN_MONT_CTX_new();
1478
0
    if (mont == NULL)
1479
0
        goto err;
1480
1481
0
    if (!ossl_bn_mont_ctx_set(mont, p, 256, params + 6 * param_len, param_len,
1482
0
            1, 0))
1483
0
        goto err;
1484
1485
0
    one = BN_new();
1486
0
    if (one == NULL) {
1487
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
1488
0
        goto err;
1489
0
    }
1490
0
    if (!BN_to_montgomery(one, BN_value_one(), mont, ctx)) {
1491
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
1492
0
        goto err;
1493
0
    }
1494
0
    group->field_data1 = mont;
1495
0
    mont = NULL;
1496
0
    group->field_data2 = one;
1497
0
    one = NULL;
1498
1499
0
    if (!ossl_ec_GFp_simple_group_set_curve(group, p, a, b, ctx)) {
1500
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1501
0
        goto err;
1502
0
    }
1503
1504
0
    if ((P = EC_POINT_new(group)) == NULL) {
1505
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1506
0
        goto err;
1507
0
    }
1508
1509
0
    if ((x = BN_bin2bn(params + 3 * param_len, param_len, NULL)) == NULL
1510
0
        || (y = BN_bin2bn(params + 4 * param_len, param_len, NULL)) == NULL) {
1511
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
1512
0
        goto err;
1513
0
    }
1514
0
    if (!EC_POINT_set_affine_coordinates(group, P, x, y, ctx)) {
1515
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1516
0
        goto err;
1517
0
    }
1518
0
    if ((order = BN_bin2bn(params + 5 * param_len, param_len, NULL)) == NULL
1519
0
        || !BN_set_word(x, (BN_ULONG)1)) { /* cofactor is 1 */
1520
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
1521
0
        goto err;
1522
0
    }
1523
1524
    /*
1525
     * Set up generator and order and montgomery data
1526
     */
1527
0
    group->generator = EC_POINT_new(group);
1528
0
    if (group->generator == NULL) {
1529
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
1530
0
        goto err;
1531
0
    }
1532
0
    if (!EC_POINT_copy(group->generator, P))
1533
0
        goto err;
1534
0
    if (BN_copy(group->order, order) == NULL)
1535
0
        goto err;
1536
0
    if (!BN_set_word(group->cofactor, 1))
1537
0
        goto err;
1538
1539
0
    ordmont = BN_MONT_CTX_new();
1540
0
    if (ordmont == NULL)
1541
0
        goto err;
1542
0
    if (!ossl_bn_mont_ctx_set(ordmont, order, 256, params + 7 * param_len,
1543
0
            param_len, lo_order_n, hi_order_n))
1544
0
        goto err;
1545
1546
0
    group->mont_data = ordmont;
1547
0
    ordmont = NULL;
1548
1549
0
    ok = 1;
1550
1551
0
err:
1552
0
    EC_POINT_free(P);
1553
0
    BN_CTX_free(ctx);
1554
0
    BN_MONT_CTX_free(mont);
1555
0
    BN_MONT_CTX_free(ordmont);
1556
0
    BN_free(p);
1557
0
    BN_free(one);
1558
0
    BN_free(a);
1559
0
    BN_free(b);
1560
0
    BN_free(order);
1561
0
    BN_free(x);
1562
0
    BN_free(y);
1563
1564
0
    return ok;
1565
0
}
1566
1567
const EC_METHOD *EC_GFp_nistz256_method(void)
1568
0
{
1569
0
    static const EC_METHOD ret = {
1570
0
        EC_FLAGS_DEFAULT_OCT,
1571
0
        NID_X9_62_prime_field,
1572
0
        ossl_ec_GFp_mont_group_init,
1573
0
        ossl_ec_GFp_mont_group_finish,
1574
0
        ossl_ec_GFp_mont_group_clear_finish,
1575
0
        ossl_ec_GFp_mont_group_copy,
1576
0
        ossl_ec_GFp_mont_group_set_curve,
1577
0
        ossl_ec_GFp_simple_group_get_curve,
1578
0
        ossl_ec_GFp_simple_group_get_degree,
1579
0
        ossl_ec_group_simple_order_bits,
1580
0
        ossl_ec_GFp_simple_group_check_discriminant,
1581
0
        ossl_ec_GFp_simple_point_init,
1582
0
        ossl_ec_GFp_simple_point_finish,
1583
0
        ossl_ec_GFp_simple_point_clear_finish,
1584
0
        ossl_ec_GFp_simple_point_copy,
1585
0
        ossl_ec_GFp_simple_point_set_to_infinity,
1586
0
        ossl_ec_GFp_simple_point_set_affine_coordinates,
1587
0
        ecp_nistz256_get_affine,
1588
0
        0, 0, 0,
1589
0
        ossl_ec_GFp_simple_add,
1590
0
        ossl_ec_GFp_simple_dbl,
1591
0
        ossl_ec_GFp_simple_invert,
1592
0
        ossl_ec_GFp_simple_is_at_infinity,
1593
0
        ossl_ec_GFp_simple_is_on_curve,
1594
0
        ossl_ec_GFp_simple_cmp,
1595
0
        ossl_ec_GFp_simple_make_affine,
1596
0
        ossl_ec_GFp_simple_points_make_affine,
1597
0
        ecp_nistz256_points_mul, /* mul */
1598
0
        ecp_nistz256_mult_precompute, /* precompute_mult */
1599
0
        ecp_nistz256_window_have_precompute_mult, /* have_precompute_mult */
1600
0
        ossl_ec_GFp_mont_field_mul,
1601
0
        ossl_ec_GFp_mont_field_sqr,
1602
0
        0, /* field_div */
1603
0
        ossl_ec_GFp_mont_field_inv,
1604
0
        ossl_ec_GFp_mont_field_encode,
1605
0
        ossl_ec_GFp_mont_field_decode,
1606
0
        ossl_ec_GFp_mont_field_set_to_one,
1607
0
        ossl_ec_key_simple_priv2oct,
1608
0
        ossl_ec_key_simple_oct2priv,
1609
0
        0, /* set private */
1610
0
        ossl_ec_key_simple_generate_key,
1611
0
        ossl_ec_key_simple_check_key,
1612
0
        ossl_ec_key_simple_generate_public_key,
1613
0
        0, /* keycopy */
1614
0
        0, /* keyfinish */
1615
0
        ossl_ecdh_simple_compute_key,
1616
0
        ossl_ecdsa_simple_sign_setup,
1617
0
        ossl_ecdsa_simple_sign_sig,
1618
0
        ossl_ecdsa_simple_verify_sig,
1619
0
        ecp_nistz256_inv_mod_ord, /* can be #define-d NULL */
1620
0
        0, /* blind_coordinates */
1621
0
        0, /* ladder_pre */
1622
0
        0, /* ladder_step */
1623
0
        0, /* ladder_post */
1624
0
        ecp_nistz256group_full_init
1625
0
    };
1626
1627
0
    return &ret;
1628
0
}