e0ca2dca17118609c43c4e291d955f145c397903
[openssl.git] / crypto / ec / ecp_nistp224.c
1 /* crypto/ec/ecp_nistp224.c */
2 /*
3  * Written by Emilia Kasper (Google) for the OpenSSL project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2000-2010 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 /*
60  * A 64-bit implementation of the NIST P-224 elliptic curve point multiplication
61  *
62  * Inspired by Daniel J. Bernstein's public domain nistp224 implementation
63  * and Adam Langley's public domain 64-bit C implementation of curve25519
64  */
65 #ifdef EC_NISTP224_64_GCC_128
66 #include <stdint.h>
67 #include <string.h>
68 #include <openssl/err.h>
69 #include "ec_lcl.h"
70
71 #if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
72   /* even with gcc, the typedef won't work for 32-bit platforms */
73   typedef __uint128_t uint128_t; /* nonstandard; implemented by gcc on 64-bit platforms */
74 #else
75   #error "Need GCC 3.1 or later to define type uint128_t"
76 #endif
77
78 typedef uint8_t u8;
79
80
81 /******************************************************************************/
82 /*                  INTERNAL REPRESENTATION OF FIELD ELEMENTS
83  *
84  * Field elements are represented as a_0 + 2^56*a_1 + 2^112*a_2 + 2^168*a_3
85  * where each slice a_i is a 64-bit word, i.e., a field element is an fslice
86  * array a with 4 elements, where a[i] = a_i.
87  * Outputs from multiplications are represented as unreduced polynomials
88  * b_0 + 2^56*b_1 + 2^112*b_2 + 2^168*b_3 + 2^224*b_4 + 2^280*b_5 + 2^336*b_6
89  * where each b_i is a 128-bit word. We ensure that inputs to each field
90  * multiplication satisfy a_i < 2^60, so outputs satisfy b_i < 4*2^60*2^60,
91  * and fit into a 128-bit word without overflow. The coefficients are then
92  * again partially reduced to a_i < 2^57. We only reduce to the unique minimal
93  * representation at the end of the computation.
94  *
95  */
96
97 typedef uint64_t fslice;
98
99 /* Field element represented as a byte arrary.
100  * 28*8 = 224 bits is also the group order size for the elliptic curve.  */
101 typedef u8 felem_bytearray[28];
102
103 static const felem_bytearray nistp224_curve_params[5] = {
104         {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,    /* p */
105          0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
106          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01},
107         {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,    /* a */
108          0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFF,0xFF,
109          0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE},
110         {0xB4,0x05,0x0A,0x85,0x0C,0x04,0xB3,0xAB,0xF5,0x41,    /* b */
111          0x32,0x56,0x50,0x44,0xB0,0xB7,0xD7,0xBF,0xD8,0xBA,
112          0x27,0x0B,0x39,0x43,0x23,0x55,0xFF,0xB4},
113         {0xB7,0x0E,0x0C,0xBD,0x6B,0xB4,0xBF,0x7F,0x32,0x13,    /* x */
114          0x90,0xB9,0x4A,0x03,0xC1,0xD3,0x56,0xC2,0x11,0x22,
115          0x34,0x32,0x80,0xD6,0x11,0x5C,0x1D,0x21},
116         {0xbd,0x37,0x63,0x88,0xb5,0xf7,0x23,0xfb,0x4c,0x22,    /* y */
117          0xdf,0xe6,0xcd,0x43,0x75,0xa0,0x5a,0x07,0x47,0x64,
118          0x44,0xd5,0x81,0x99,0x85,0x00,0x7e,0x34}
119 };
120
121 /* Precomputed multiples of the standard generator
122  * b_0*G + b_1*2^56*G + b_2*2^112*G + b_3*2^168*G for
123  * (b_3, b_2, b_1, b_0) in [0,15], i.e., gmul[0] = point_at_infinity,
124  * gmul[1] = G, gmul[2] = 2^56*G, gmul[3] = 2^56*G + G, etc.
125  * Points are given in Jacobian projective coordinates: words 0-3 represent the
126  * X-coordinate (slice a_0 is word 0, etc.), words 4-7 represent the
127  * Y-coordinate and words 8-11 represent the Z-coordinate. */
128 static const fslice gmul[16][3][4] = {
129         {{0x00000000000000, 0x00000000000000, 0x00000000000000, 0x00000000000000},
130          {0x00000000000000, 0x00000000000000, 0x00000000000000, 0x00000000000000},
131          {0x00000000000000, 0x00000000000000, 0x00000000000000, 0x00000000000000}},
132         {{0x3280d6115c1d21, 0xc1d356c2112234, 0x7f321390b94a03, 0xb70e0cbd6bb4bf},
133          {0xd5819985007e34, 0x75a05a07476444, 0xfb4c22dfe6cd43, 0xbd376388b5f723},
134          {0x00000000000001, 0x00000000000000, 0x00000000000000, 0x00000000000000}},
135         {{0xfd9675666ebbe9, 0xbca7664d40ce5e, 0x2242df8d8a2a43, 0x1f49bbb0f99bc5},
136          {0x29e0b892dc9c43, 0xece8608436e662, 0xdc858f185310d0, 0x9812dd4eb8d321},
137          {0x00000000000001, 0x00000000000000, 0x00000000000000, 0x00000000000000}},
138         {{0x6d3e678d5d8eb8, 0x559eed1cb362f1, 0x16e9a3bbce8a3f, 0xeedcccd8c2a748},
139          {0xf19f90ed50266d, 0xabf2b4bf65f9df, 0x313865468fafec, 0x5cb379ba910a17},
140          {0x00000000000001, 0x00000000000000, 0x00000000000000, 0x00000000000000}},
141         {{0x0641966cab26e3, 0x91fb2991fab0a0, 0xefec27a4e13a0b, 0x0499aa8a5f8ebe},
142          {0x7510407766af5d, 0x84d929610d5450, 0x81d77aae82f706, 0x6916f6d4338c5b},
143          {0x00000000000001, 0x00000000000000, 0x00000000000000, 0x00000000000000}},
144         {{0xea95ac3b1f15c6, 0x086000905e82d4, 0xdd323ae4d1c8b1, 0x932b56be7685a3},
145          {0x9ef93dea25dbbf, 0x41665960f390f0, 0xfdec76dbe2a8a7, 0x523e80f019062a},
146          {0x00000000000001, 0x00000000000000, 0x00000000000000, 0x00000000000000}},
147         {{0x822fdd26732c73, 0xa01c83531b5d0f, 0x363f37347c1ba4, 0xc391b45c84725c},
148          {0xbbd5e1b2d6ad24, 0xddfbcde19dfaec, 0xc393da7e222a7f, 0x1efb7890ede244},
149          {0x00000000000001, 0x00000000000000, 0x00000000000000, 0x00000000000000}},
150         {{0x4c9e90ca217da1, 0xd11beca79159bb, 0xff8d33c2c98b7c, 0x2610b39409f849},
151          {0x44d1352ac64da0, 0xcdbb7b2c46b4fb, 0x966c079b753c89, 0xfe67e4e820b112},
152          {0x00000000000001, 0x00000000000000, 0x00000000000000, 0x00000000000000}},
153         {{0xe28cae2df5312d, 0xc71b61d16f5c6e, 0x79b7619a3e7c4c, 0x05c73240899b47},
154          {0x9f7f6382c73e3a, 0x18615165c56bda, 0x641fab2116fd56, 0x72855882b08394},
155          {0x00000000000001, 0x00000000000000, 0x00000000000000, 0x00000000000000}},
156         {{0x0469182f161c09, 0x74a98ca8d00fb5, 0xb89da93489a3e0, 0x41c98768fb0c1d},
157          {0xe5ea05fb32da81, 0x3dce9ffbca6855, 0x1cfe2d3fbf59e6, 0x0e5e03408738a7},
158          {0x00000000000001, 0x00000000000000, 0x00000000000000, 0x00000000000000}},
159         {{0xdab22b2333e87f, 0x4430137a5dd2f6, 0xe03ab9f738beb8, 0xcb0c5d0dc34f24},
160          {0x764a7df0c8fda5, 0x185ba5c3fa2044, 0x9281d688bcbe50, 0xc40331df893881},
161          {0x00000000000001, 0x00000000000000, 0x00000000000000, 0x00000000000000}},
162         {{0xb89530796f0f60, 0xade92bd26909a3, 0x1a0c83fb4884da, 0x1765bf22a5a984},
163          {0x772a9ee75db09e, 0x23bc6c67cec16f, 0x4c1edba8b14e2f, 0xe2a215d9611369},
164          {0x00000000000001, 0x00000000000000, 0x00000000000000, 0x00000000000000}},
165         {{0x571e509fb5efb3, 0xade88696410552, 0xc8ae85fada74fe, 0x6c7e4be83bbde3},
166          {0xff9f51160f4652, 0xb47ce2495a6539, 0xa2946c53b582f4, 0x286d2db3ee9a60},
167          {0x00000000000001, 0x00000000000000, 0x00000000000000, 0x00000000000000}},
168         {{0x40bbd5081a44af, 0x0995183b13926c, 0xbcefba6f47f6d0, 0x215619e9cc0057},
169          {0x8bc94d3b0df45e, 0xf11c54a3694f6f, 0x8631b93cdfe8b5, 0xe7e3f4b0982db9},
170          {0x00000000000001, 0x00000000000000, 0x00000000000000, 0x00000000000000}},
171         {{0xb17048ab3e1c7b, 0xac38f36ff8a1d8, 0x1c29819435d2c6, 0xc813132f4c07e9},
172          {0x2891425503b11f, 0x08781030579fea, 0xf5426ba5cc9674, 0x1e28ebf18562bc},
173          {0x00000000000001, 0x00000000000000, 0x00000000000000, 0x00000000000000}},
174         {{0x9f31997cc864eb, 0x06cd91d28b5e4c, 0xff17036691a973, 0xf1aef351497c58},
175          {0xdd1f2d600564ff, 0xdead073b1402db, 0x74a684435bd693, 0xeea7471f962558},
176          {0x00000000000001, 0x00000000000000, 0x00000000000000, 0x00000000000000}}
177 };
178
179 /* Precomputation for the group generator. */
180 typedef struct {
181         fslice g_pre_comp[16][3][4];
182         int references;
183 } NISTP224_PRE_COMP;
184
185 const EC_METHOD *EC_GFp_nistp224_method(void)
186         {
187         static const EC_METHOD ret = {
188                 NID_X9_62_prime_field,
189                 ec_GFp_nistp224_group_init,
190                 ec_GFp_simple_group_finish,
191                 ec_GFp_simple_group_clear_finish,
192                 ec_GFp_nist_group_copy,
193                 ec_GFp_nistp224_group_set_curve,
194                 ec_GFp_simple_group_get_curve,
195                 ec_GFp_simple_group_get_degree,
196                 ec_GFp_simple_group_check_discriminant,
197                 ec_GFp_simple_point_init,
198                 ec_GFp_simple_point_finish,
199                 ec_GFp_simple_point_clear_finish,
200                 ec_GFp_simple_point_copy,
201                 ec_GFp_simple_point_set_to_infinity,
202                 ec_GFp_simple_set_Jprojective_coordinates_GFp,
203                 ec_GFp_simple_get_Jprojective_coordinates_GFp,
204                 ec_GFp_simple_point_set_affine_coordinates,
205                 ec_GFp_nistp224_point_get_affine_coordinates,
206                 ec_GFp_simple_set_compressed_coordinates,
207                 ec_GFp_simple_point2oct,
208                 ec_GFp_simple_oct2point,
209                 ec_GFp_simple_add,
210                 ec_GFp_simple_dbl,
211                 ec_GFp_simple_invert,
212                 ec_GFp_simple_is_at_infinity,
213                 ec_GFp_simple_is_on_curve,
214                 ec_GFp_simple_cmp,
215                 ec_GFp_simple_make_affine,
216                 ec_GFp_simple_points_make_affine,
217                 ec_GFp_nistp224_points_mul,
218                 ec_GFp_nistp224_precompute_mult,
219                 ec_GFp_nistp224_have_precompute_mult,
220                 ec_GFp_nist_field_mul,
221                 ec_GFp_nist_field_sqr,
222                 0 /* field_div */,
223                 0 /* field_encode */,
224                 0 /* field_decode */,
225                 0 /* field_set_to_one */ };
226
227         return &ret;
228         }
229
230 /* Helper functions to convert field elements to/from internal representation */
231 static void bin28_to_felem(fslice out[4], const u8 in[28])
232         {
233         out[0] = *((const uint64_t *)(in)) & 0x00ffffffffffffff;
234         out[1] = (*((const uint64_t *)(in+7))) & 0x00ffffffffffffff;
235         out[2] = (*((const uint64_t *)(in+14))) & 0x00ffffffffffffff;
236         out[3] = (*((const uint64_t *)(in+21))) & 0x00ffffffffffffff;
237         }
238
239 static void felem_to_bin28(u8 out[28], const fslice in[4])
240         {
241         unsigned i;
242         for (i = 0; i < 7; ++i)
243                 {
244                 out[i]    = in[0]>>(8*i);
245                 out[i+7]  = in[1]>>(8*i);
246                 out[i+14] = in[2]>>(8*i);
247                 out[i+21] = in[3]>>(8*i);
248                 }
249         }
250
251 /* To preserve endianness when using BN_bn2bin and BN_bin2bn */
252 static void flip_endian(u8 *out, const u8 *in, unsigned len)
253         {
254         unsigned i;
255         for (i = 0; i < len; ++i)
256                 out[i] = in[len-1-i];
257         }
258
259 /* From OpenSSL BIGNUM to internal representation */
260 static int BN_to_felem(fslice out[4], const BIGNUM *bn)
261         {
262         felem_bytearray b_in;
263         felem_bytearray b_out;
264         unsigned num_bytes;
265
266         /* BN_bn2bin eats leading zeroes */
267         memset(b_out, 0, sizeof b_out);
268         num_bytes = BN_num_bytes(bn);
269         if (num_bytes > sizeof b_out)
270                 {
271                 ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
272                 return 0;
273                 }
274         if (BN_is_negative(bn))
275                 {
276                 ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
277                 return 0;
278                 }
279         num_bytes = BN_bn2bin(bn, b_in);
280         flip_endian(b_out, b_in, num_bytes);
281         bin28_to_felem(out, b_out);
282         return 1;
283         }
284
285 /* From internal representation to OpenSSL BIGNUM */
286 static BIGNUM *felem_to_BN(BIGNUM *out, const fslice in[4])
287         {
288         felem_bytearray b_in, b_out;
289         felem_to_bin28(b_in, in);
290         flip_endian(b_out, b_in, sizeof b_out);
291         return BN_bin2bn(b_out, sizeof b_out, out);
292         }
293
294 /******************************************************************************/
295 /*                              FIELD OPERATIONS
296  *
297  * Field operations, using the internal representation of field elements.
298  * NB! These operations are specific to our point multiplication and cannot be
299  * expected to be correct in general - e.g., multiplication with a large scalar
300  * will cause an overflow.
301  *
302  */
303
304 /* Sum two field elements: out += in */
305 static void felem_sum64(fslice out[4], const fslice in[4])
306         {
307         out[0] += in[0];
308         out[1] += in[1];
309         out[2] += in[2];
310         out[3] += in[3];
311         }
312
313 /* Subtract field elements: out -= in */
314 /* Assumes in[i] < 2^57 */
315 static void felem_diff64(fslice out[4], const fslice in[4])
316         {
317         static const uint64_t two58p2 = (((uint64_t) 1) << 58) + (((uint64_t) 1) << 2);
318         static const uint64_t two58m2 = (((uint64_t) 1) << 58) - (((uint64_t) 1) << 2);
319         static const uint64_t two58m42m2 = (((uint64_t) 1) << 58) -
320             (((uint64_t) 1) << 42) - (((uint64_t) 1) << 2);
321
322         /* Add 0 mod 2^224-2^96+1 to ensure out > in */
323         out[0] += two58p2;
324         out[1] += two58m42m2;
325         out[2] += two58m2;
326         out[3] += two58m2;
327
328         out[0] -= in[0];
329         out[1] -= in[1];
330         out[2] -= in[2];
331         out[3] -= in[3];
332         }
333
334 /* Subtract in unreduced 128-bit mode: out128 -= in128 */
335 /* Assumes in[i] < 2^119 */
336 static void felem_diff128(uint128_t out[7], const uint128_t in[4])
337         {
338         static const uint128_t two120 = ((uint128_t) 1) << 120;
339         static const uint128_t two120m64 = (((uint128_t) 1) << 120) -
340                 (((uint128_t) 1) << 64);
341         static const uint128_t two120m104m64 = (((uint128_t) 1) << 120) -
342                 (((uint128_t) 1) << 104) - (((uint128_t) 1) << 64);
343
344         /* Add 0 mod 2^224-2^96+1 to ensure out > in */
345         out[0] += two120;
346         out[1] += two120m64;
347         out[2] += two120m64;
348         out[3] += two120;
349         out[4] += two120m104m64;
350         out[5] += two120m64;
351         out[6] += two120m64;
352
353         out[0] -= in[0];
354         out[1] -= in[1];
355         out[2] -= in[2];
356         out[3] -= in[3];
357         out[4] -= in[4];
358         out[5] -= in[5];
359         out[6] -= in[6];
360         }
361
362 /* Subtract in mixed mode: out128 -= in64 */
363 /* in[i] < 2^63 */
364 static void felem_diff_128_64(uint128_t out[7], const fslice in[4])
365         {
366         static const uint128_t two64p8 = (((uint128_t) 1) << 64) +
367                 (((uint128_t) 1) << 8);
368         static const uint128_t two64m8 = (((uint128_t) 1) << 64) -
369                 (((uint128_t) 1) << 8);
370         static const uint128_t two64m48m8 = (((uint128_t) 1) << 64) -
371                 (((uint128_t) 1) << 48) - (((uint128_t) 1) << 8);
372
373         /* Add 0 mod 2^224-2^96+1 to ensure out > in */
374         out[0] += two64p8;
375         out[1] += two64m48m8;
376         out[2] += two64m8;
377         out[3] += two64m8;
378
379         out[0] -= in[0];
380         out[1] -= in[1];
381         out[2] -= in[2];
382         out[3] -= in[3];
383         }
384
385 /* Multiply a field element by a scalar: out64 = out64 * scalar
386  * The scalars we actually use are small, so results fit without overflow */
387 static void felem_scalar64(fslice out[4], const fslice scalar)
388         {
389         out[0] *= scalar;
390         out[1] *= scalar;
391         out[2] *= scalar;
392         out[3] *= scalar;
393         }
394
395 /* Multiply an unreduced field element by a scalar: out128 = out128 * scalar
396  * The scalars we actually use are small, so results fit without overflow */
397 static void felem_scalar128(uint128_t out[7], const uint128_t scalar)
398         {
399         out[0] *= scalar;
400         out[1] *= scalar;
401         out[2] *= scalar;
402         out[3] *= scalar;
403         out[4] *= scalar;
404         out[5] *= scalar;
405         out[6] *= scalar;
406         }
407
408 /* Square a field element: out = in^2 */
409 static void felem_square(uint128_t out[7], const fslice in[4])
410         {
411         out[0] = ((uint128_t) in[0]) * in[0];
412         out[1] = ((uint128_t) in[0]) * in[1] * 2;
413         out[2] = ((uint128_t) in[0]) * in[2] * 2 + ((uint128_t) in[1]) * in[1];
414         out[3] = ((uint128_t) in[0]) * in[3] * 2 +
415                 ((uint128_t) in[1]) * in[2] * 2;
416         out[4] = ((uint128_t) in[1]) * in[3] * 2 + ((uint128_t) in[2]) * in[2];
417         out[5] = ((uint128_t) in[2]) * in[3] * 2;
418         out[6] = ((uint128_t) in[3]) * in[3];
419         }
420
421 /* Multiply two field elements: out = in1 * in2 */
422 static void felem_mul(uint128_t out[7], const fslice in1[4], const fslice in2[4])
423         {
424         out[0] = ((uint128_t) in1[0]) * in2[0];
425         out[1] = ((uint128_t) in1[0]) * in2[1] + ((uint128_t) in1[1]) * in2[0];
426         out[2] = ((uint128_t) in1[0]) * in2[2] + ((uint128_t) in1[1]) * in2[1] +
427                 ((uint128_t) in1[2]) * in2[0];
428         out[3] = ((uint128_t) in1[0]) * in2[3] + ((uint128_t) in1[1]) * in2[2] +
429                 ((uint128_t) in1[2]) * in2[1] + ((uint128_t) in1[3]) * in2[0];
430         out[4] = ((uint128_t) in1[1]) * in2[3] + ((uint128_t) in1[2]) * in2[2] +
431                 ((uint128_t) in1[3]) * in2[1];
432         out[5] = ((uint128_t) in1[2]) * in2[3] + ((uint128_t) in1[3]) * in2[2];
433         out[6] = ((uint128_t) in1[3]) * in2[3];
434         }
435
436 /* Reduce 128-bit coefficients to 64-bit coefficients. Requires in[i] < 2^126,
437  * ensures out[0] < 2^56, out[1] < 2^56, out[2] < 2^56, out[3] < 2^57 */
438 static void felem_reduce(fslice out[4], const uint128_t in[7])
439         {
440         static const uint128_t two127p15 = (((uint128_t) 1) << 127) +
441                 (((uint128_t) 1) << 15);
442         static const uint128_t two127m71 = (((uint128_t) 1) << 127) -
443                 (((uint128_t) 1) << 71);
444         static const uint128_t two127m71m55 = (((uint128_t) 1) << 127) -
445                 (((uint128_t) 1) << 71) - (((uint128_t) 1) << 55);
446         uint128_t output[5];
447
448         /* Add 0 mod 2^224-2^96+1 to ensure all differences are positive */
449         output[0] = in[0] + two127p15;
450         output[1] = in[1] + two127m71m55;
451         output[2] = in[2] + two127m71;
452         output[3] = in[3];
453         output[4] = in[4];
454
455         /* Eliminate in[4], in[5], in[6] */
456         output[4] += in[6] >> 16;
457         output[3] += (in[6]&0xffff) << 40;
458         output[2] -= in[6];
459
460         output[3] += in[5] >> 16;
461         output[2] += (in[5]&0xffff) << 40;
462         output[1] -= in[5];
463
464         output[2] += output[4] >> 16;
465         output[1] += (output[4]&0xffff) << 40;
466         output[0] -= output[4];
467         output[4] = 0;
468
469         /* Carry 2 -> 3 -> 4 */
470         output[3] += output[2] >> 56;
471         output[2] &= 0x00ffffffffffffff;
472
473         output[4] += output[3] >> 56;
474         output[3] &= 0x00ffffffffffffff;
475
476         /* Now output[2] < 2^56, output[3] < 2^56 */
477
478         /* Eliminate output[4] */
479         output[2] += output[4] >> 16;
480         output[1] += (output[4]&0xffff) << 40;
481         output[0] -= output[4];
482
483         /* Carry 0 -> 1 -> 2 -> 3 */
484         output[1] += output[0] >> 56;
485         out[0] = output[0] & 0x00ffffffffffffff;
486
487         output[2] += output[1] >> 56;
488         out[1] = output[1] & 0x00ffffffffffffff;
489         output[3] += output[2] >> 56;
490         out[2] = output[2] & 0x00ffffffffffffff;
491
492         /* out[0] < 2^56, out[1] < 2^56, out[2] < 2^56,
493          * out[3] < 2^57 (due to final carry) */
494         out[3] = output[3];
495         }
496
497 /* Reduce to unique minimal representation */
498 static void felem_contract(fslice out[4], const fslice in[4])
499         {
500         static const int64_t two56 = ((uint64_t) 1) << 56;
501         /* 0 <= in < 2^225 */
502         /* if in > 2^224 , reduce in = in - 2^224 + 2^96 - 1 */
503         int64_t tmp[4], a;
504         tmp[0] = (int64_t) in[0] - (in[3] >> 56);
505         tmp[1] = (int64_t) in[1] + ((in[3] >> 16) & 0x0000010000000000);
506         tmp[2] = (int64_t) in[2];
507         tmp[3] = (int64_t) in[3] & 0x00ffffffffffffff;
508
509         /* eliminate negative coefficients */
510         a = tmp[0] >> 63;
511         tmp[0] += two56 & a;
512         tmp[1] -= 1 & a;
513
514         a = tmp[1] >> 63;
515         tmp[1] += two56 & a;
516         tmp[2] -= 1 & a;
517
518         a = tmp[2] >> 63;
519         tmp[2] += two56 & a;
520         tmp[3] -= 1 & a;
521
522         a = tmp[3] >> 63;
523         tmp[3] += two56 & a;
524         tmp[0] += 1 & a;
525         tmp[1] -= (1 & a) << 40;
526
527         /* carry 1 -> 2 -> 3 */
528         tmp[2] += tmp[1] >> 56;
529         tmp[1] &= 0x00ffffffffffffff;
530
531         tmp[3] += tmp[2] >> 56;
532         tmp[2] &= 0x00ffffffffffffff;
533
534         /* 0 <= in < 2^224 + 2^96 - 1 */
535         /* if in > 2^224 , reduce in = in - 2^224 + 2^96 - 1 */
536         tmp[0] -= (tmp[3] >> 56);
537         tmp[1] += ((tmp[3] >> 16) & 0x0000010000000000);
538         tmp[3] &= 0x00ffffffffffffff;
539
540         /* eliminate negative coefficients */
541         a = tmp[0] >> 63;
542         tmp[0] += two56 & a;
543         tmp[1] -= 1 & a;
544
545         a = tmp[1] >> 63;
546         tmp[1] += two56 & a;
547         tmp[2] -= 1 & a;
548
549         a = tmp[2] >> 63;
550         tmp[2] += two56 & a;
551         tmp[3] -= 1 & a;
552
553         a = tmp[3] >> 63;
554         tmp[3] += two56 & a;
555         tmp[0] += 1 & a;
556         tmp[1] -= (1 & a) << 40;
557
558         /* carry 1 -> 2 -> 3 */
559         tmp[2] += tmp[1] >> 56;
560         tmp[1] &= 0x00ffffffffffffff;
561
562         tmp[3] += tmp[2] >> 56;
563         tmp[2] &= 0x00ffffffffffffff;
564
565         /* Now 0 <= in < 2^224 */
566
567         /* if in > 2^224 - 2^96, reduce */
568         /* a = 0 iff in > 2^224 - 2^96, i.e.,
569          * the high 128 bits are all 1 and the lower part is non-zero */
570         a = (tmp[3] + 1) | (tmp[2] + 1) |
571                 ((tmp[1] | 0x000000ffffffffff) + 1) |
572                 ((((tmp[1] & 0xffff) - 1) >> 63) & ((tmp[0] - 1) >> 63));
573         /* turn a into an all-one mask (if a = 0) or an all-zero mask */
574         a = ((a & 0x00ffffffffffffff) - 1) >> 63;
575         /* subtract 2^224 - 2^96 + 1 if a is all-one*/
576         tmp[3] &= a ^ 0xffffffffffffffff;
577         tmp[2] &= a ^ 0xffffffffffffffff;
578         tmp[1] &= (a ^ 0xffffffffffffffff) | 0x000000ffffffffff;
579         tmp[0] -= 1 & a;
580         /* eliminate negative coefficients: if tmp[0] is negative, tmp[1] must be
581          * non-zero, so we only need one step */
582         a = tmp[0] >> 63;
583         tmp[0] += two56 & a;
584         tmp[1] -= 1 & a;
585
586         out[0] = tmp[0];
587         out[1] = tmp[1];
588         out[2] = tmp[2];
589         out[3] = tmp[3];
590         }
591
592 /* Zero-check: returns 1 if input is 0, and 0 otherwise.
593  * We know that field elements are reduced to in < 2^225,
594  * so we only need to check three cases: 0, 2^224 - 2^96 + 1,
595  * and 2^225 - 2^97 + 2 */
596 static fslice felem_is_zero(const fslice in[4])
597         {
598         fslice zero, two224m96p1, two225m97p2;
599
600         zero = in[0] | in[1] | in[2] | in[3];
601         zero = (((int64_t)(zero) - 1) >> 63) & 1;
602         two224m96p1 = (in[0] ^ 1) | (in[1] ^ 0x00ffff0000000000)
603                 | (in[2] ^ 0x00ffffffffffffff) | (in[3] ^ 0x00ffffffffffffff);
604         two224m96p1 = (((int64_t)(two224m96p1) - 1) >> 63) & 1;
605         two225m97p2 = (in[0] ^ 2) | (in[1] ^ 0x00fffe0000000000)
606                 | (in[2] ^ 0x00ffffffffffffff) | (in[3] ^ 0x01ffffffffffffff);
607         two225m97p2 = (((int64_t)(two225m97p2) - 1) >> 63) & 1;
608         return (zero | two224m96p1 | two225m97p2);
609         }
610
611 /* Invert a field element */
612 /* Computation chain copied from djb's code */
613 static void felem_inv(fslice out[4], const fslice in[4])
614         {
615         fslice ftmp[4], ftmp2[4], ftmp3[4], ftmp4[4];
616         uint128_t tmp[7];
617         unsigned i;
618
619         felem_square(tmp, in); felem_reduce(ftmp, tmp);         /* 2 */
620         felem_mul(tmp, in, ftmp); felem_reduce(ftmp, tmp);      /* 2^2 - 1 */
621         felem_square(tmp, ftmp); felem_reduce(ftmp, tmp);       /* 2^3 - 2 */
622         felem_mul(tmp, in, ftmp); felem_reduce(ftmp, tmp);      /* 2^3 - 1 */
623         felem_square(tmp, ftmp); felem_reduce(ftmp2, tmp);      /* 2^4 - 2 */
624         felem_square(tmp, ftmp2); felem_reduce(ftmp2, tmp);     /* 2^5 - 4 */
625         felem_square(tmp, ftmp2); felem_reduce(ftmp2, tmp);     /* 2^6 - 8 */
626         felem_mul(tmp, ftmp2, ftmp); felem_reduce(ftmp, tmp);   /* 2^6 - 1 */
627         felem_square(tmp, ftmp); felem_reduce(ftmp2, tmp);      /* 2^7 - 2 */
628         for (i = 0; i < 5; ++i)                                 /* 2^12 - 2^6 */
629                 {
630                 felem_square(tmp, ftmp2); felem_reduce(ftmp2, tmp);
631                 }
632         felem_mul(tmp, ftmp2, ftmp); felem_reduce(ftmp2, tmp);  /* 2^12 - 1 */
633         felem_square(tmp, ftmp2); felem_reduce(ftmp3, tmp);     /* 2^13 - 2 */
634         for (i = 0; i < 11; ++i)                                /* 2^24 - 2^12 */
635                 {
636                 felem_square(tmp, ftmp3); felem_reduce(ftmp3, tmp);
637                 }
638         felem_mul(tmp, ftmp3, ftmp2); felem_reduce(ftmp2, tmp); /* 2^24 - 1 */
639         felem_square(tmp, ftmp2); felem_reduce(ftmp3, tmp);     /* 2^25 - 2 */
640         for (i = 0; i < 23; ++i)                                /* 2^48 - 2^24 */
641                 {
642                 felem_square(tmp, ftmp3); felem_reduce(ftmp3, tmp);
643                 }
644         felem_mul(tmp, ftmp3, ftmp2); felem_reduce(ftmp3, tmp); /* 2^48 - 1 */
645         felem_square(tmp, ftmp3); felem_reduce(ftmp4, tmp);     /* 2^49 - 2 */
646         for (i = 0; i < 47; ++i)                                /* 2^96 - 2^48 */
647                 {
648                 felem_square(tmp, ftmp4); felem_reduce(ftmp4, tmp);
649                 }
650         felem_mul(tmp, ftmp3, ftmp4); felem_reduce(ftmp3, tmp); /* 2^96 - 1 */
651         felem_square(tmp, ftmp3); felem_reduce(ftmp4, tmp);     /* 2^97 - 2 */
652         for (i = 0; i < 23; ++i)                                /* 2^120 - 2^24 */
653                 {
654                 felem_square(tmp, ftmp4); felem_reduce(ftmp4, tmp);
655                 }
656         felem_mul(tmp, ftmp2, ftmp4); felem_reduce(ftmp2, tmp); /* 2^120 - 1 */
657         for (i = 0; i < 6; ++i)                                 /* 2^126 - 2^6 */
658                 {
659                 felem_square(tmp, ftmp2); felem_reduce(ftmp2, tmp);
660                 }
661         felem_mul(tmp, ftmp2, ftmp); felem_reduce(ftmp, tmp);   /* 2^126 - 1 */
662         felem_square(tmp, ftmp); felem_reduce(ftmp, tmp);       /* 2^127 - 2 */
663         felem_mul(tmp, ftmp, in); felem_reduce(ftmp, tmp);      /* 2^127 - 1 */
664         for (i = 0; i < 97; ++i)                                /* 2^224 - 2^97 */
665                 {
666                 felem_square(tmp, ftmp); felem_reduce(ftmp, tmp);
667                 }
668         felem_mul(tmp, ftmp, ftmp3); felem_reduce(out, tmp);    /* 2^224 - 2^96 - 1 */
669         }
670
671 /* Copy in constant time:
672  * if icopy == 1, copy in to out,
673  * if icopy == 0, copy out to itself. */
674 static void
675 copy_conditional(fslice *out, const fslice *in, unsigned len, fslice icopy)
676         {
677         unsigned i;
678         /* icopy is a (64-bit) 0 or 1, so copy is either all-zero or all-one */
679         const fslice copy = -icopy;
680         for (i = 0; i < len; ++i)
681                 {
682                 const fslice tmp = copy & (in[i] ^ out[i]);
683                 out[i] ^= tmp;
684                 }
685         }
686
687 /* Copy in constant time:
688  * if isel == 1, copy in2 to out,
689  * if isel == 0, copy in1 to out. */
690 static void select_conditional(fslice *out, const fslice *in1, const fslice *in2,
691         unsigned len, fslice isel)
692         {
693         unsigned i;
694         /* isel is a (64-bit) 0 or 1, so sel is either all-zero or all-one */
695         const fslice sel = -isel;
696         for (i = 0; i < len; ++i)
697                 {
698                 const fslice tmp = sel & (in1[i] ^ in2[i]);
699                 out[i] = in1[i] ^ tmp;
700                 }
701 }
702
703 /******************************************************************************/
704 /*                       ELLIPTIC CURVE POINT OPERATIONS
705  *
706  * Points are represented in Jacobian projective coordinates:
707  * (X, Y, Z) corresponds to the affine point (X/Z^2, Y/Z^3),
708  * or to the point at infinity if Z == 0.
709  *
710  */
711
712 /* Double an elliptic curve point:
713  * (X', Y', Z') = 2 * (X, Y, Z), where
714  * X' = (3 * (X - Z^2) * (X + Z^2))^2 - 8 * X * Y^2
715  * Y' = 3 * (X - Z^2) * (X + Z^2) * (4 * X * Y^2 - X') - 8 * Y^2
716  * Z' = (Y + Z)^2 - Y^2 - Z^2 = 2 * Y * Z
717  * Outputs can equal corresponding inputs, i.e., x_out == x_in is allowed,
718  * while x_out == y_in is not (maybe this works, but it's not tested). */
719 static void
720 point_double(fslice x_out[4], fslice y_out[4], fslice z_out[4],
721              const fslice x_in[4], const fslice y_in[4], const fslice z_in[4])
722         {
723         uint128_t tmp[7], tmp2[7];
724         fslice delta[4];
725         fslice gamma[4];
726         fslice beta[4];
727         fslice alpha[4];
728         fslice ftmp[4], ftmp2[4];
729         memcpy(ftmp, x_in, 4 * sizeof(fslice));
730         memcpy(ftmp2, x_in, 4 * sizeof(fslice));
731
732         /* delta = z^2 */
733         felem_square(tmp, z_in);
734         felem_reduce(delta, tmp);
735
736         /* gamma = y^2 */
737         felem_square(tmp, y_in);
738         felem_reduce(gamma, tmp);
739
740         /* beta = x*gamma */
741         felem_mul(tmp, x_in, gamma);
742         felem_reduce(beta, tmp);
743
744         /* alpha = 3*(x-delta)*(x+delta) */
745         felem_diff64(ftmp, delta);
746         /* ftmp[i] < 2^57 + 2^58 + 2 < 2^59 */
747         felem_sum64(ftmp2, delta);
748         /* ftmp2[i] < 2^57 + 2^57 = 2^58 */
749         felem_scalar64(ftmp2, 3);
750         /* ftmp2[i] < 3 * 2^58 < 2^60 */
751         felem_mul(tmp, ftmp, ftmp2);
752         /* tmp[i] < 2^60 * 2^59 * 4 = 2^121 */
753         felem_reduce(alpha, tmp);
754
755         /* x' = alpha^2 - 8*beta */
756         felem_square(tmp, alpha);
757         /* tmp[i] < 4 * 2^57 * 2^57 = 2^116 */
758         memcpy(ftmp, beta, 4 * sizeof(fslice));
759         felem_scalar64(ftmp, 8);
760         /* ftmp[i] < 8 * 2^57 = 2^60 */
761         felem_diff_128_64(tmp, ftmp);
762         /* tmp[i] < 2^116 + 2^64 + 8 < 2^117 */
763         felem_reduce(x_out, tmp);
764
765         /* z' = (y + z)^2 - gamma - delta */
766         felem_sum64(delta, gamma);
767         /* delta[i] < 2^57 + 2^57 = 2^58 */
768         memcpy(ftmp, y_in, 4 * sizeof(fslice));
769         felem_sum64(ftmp, z_in);
770         /* ftmp[i] < 2^57 + 2^57 = 2^58 */
771         felem_square(tmp, ftmp);
772         /* tmp[i] < 4 * 2^58 * 2^58 = 2^118 */
773         felem_diff_128_64(tmp, delta);
774         /* tmp[i] < 2^118 + 2^64 + 8 < 2^119 */
775         felem_reduce(z_out, tmp);
776
777         /* y' = alpha*(4*beta - x') - 8*gamma^2 */
778         felem_scalar64(beta, 4);
779         /* beta[i] < 4 * 2^57 = 2^59 */
780         felem_diff64(beta, x_out);
781         /* beta[i] < 2^59 + 2^58 + 2 < 2^60 */
782         felem_mul(tmp, alpha, beta);
783         /* tmp[i] < 4 * 2^57 * 2^60 = 2^119 */
784         felem_square(tmp2, gamma);
785         /* tmp2[i] < 4 * 2^57 * 2^57 = 2^116 */
786         felem_scalar128(tmp2, 8);
787         /* tmp2[i] < 8 * 2^116 = 2^119 */
788         felem_diff128(tmp, tmp2);
789         /* tmp[i] < 2^119 + 2^120 < 2^121 */
790         felem_reduce(y_out, tmp);
791         }
792
793 /* Add two elliptic curve points:
794  * (X_1, Y_1, Z_1) + (X_2, Y_2, Z_2) = (X_3, Y_3, Z_3), where
795  * X_3 = (Z_1^3 * Y_2 - Z_2^3 * Y_1)^2 - (Z_1^2 * X_2 - Z_2^2 * X_1)^3 -
796  * 2 * Z_2^2 * X_1 * (Z_1^2 * X_2 - Z_2^2 * X_1)^2
797  * Y_3 = (Z_1^3 * Y_2 - Z_2^3 * Y_1) * (Z_2^2 * X_1 * (Z_1^2 * X_2 - Z_2^2 * X_1)^2 - X_3) -
798  *        Z_2^3 * Y_1 * (Z_1^2 * X_2 - Z_2^2 * X_1)^3
799  * Z_3 = (Z_1^2 * X_2 - Z_2^2 * X_1) * (Z_1 * Z_2) */
800
801 /* This function is not entirely constant-time:
802  * it includes a branch for checking whether the two input points are equal,
803  * (while not equal to the point at infinity).
804  * This case never happens during single point multiplication,
805  * so there is no timing leak for ECDH or ECDSA signing. */
806 static void point_add(fslice x3[4], fslice y3[4], fslice z3[4],
807         const fslice x1[4], const fslice y1[4], const fslice z1[4],
808         const fslice x2[4], const fslice y2[4], const fslice z2[4])
809         {
810         fslice ftmp[4], ftmp2[4], ftmp3[4], ftmp4[4], ftmp5[4];
811         uint128_t tmp[7], tmp2[7];
812         fslice z1_is_zero, z2_is_zero, x_equal, y_equal;
813
814         /* ftmp = z1^2 */
815         felem_square(tmp, z1);
816         felem_reduce(ftmp, tmp);
817
818         /* ftmp2 = z2^2 */
819         felem_square(tmp, z2);
820         felem_reduce(ftmp2, tmp);
821
822         /* ftmp3 = z1^3 */
823         felem_mul(tmp, ftmp, z1);
824         felem_reduce(ftmp3, tmp);
825
826         /* ftmp4 = z2^3 */
827         felem_mul(tmp, ftmp2, z2);
828         felem_reduce(ftmp4, tmp);
829
830         /* ftmp3 = z1^3*y2 */
831         felem_mul(tmp, ftmp3, y2);
832         /* tmp[i] < 4 * 2^57 * 2^57 = 2^116 */
833
834         /* ftmp4 = z2^3*y1 */
835         felem_mul(tmp2, ftmp4, y1);
836         felem_reduce(ftmp4, tmp2);
837
838         /* ftmp3 = z1^3*y2 - z2^3*y1 */
839         felem_diff_128_64(tmp, ftmp4);
840         /* tmp[i] < 2^116 + 2^64 + 8 < 2^117 */
841         felem_reduce(ftmp3, tmp);
842
843         /* ftmp = z1^2*x2 */
844         felem_mul(tmp, ftmp, x2);
845         /* tmp[i] < 4 * 2^57 * 2^57 = 2^116 */
846
847         /* ftmp2 =z2^2*x1 */
848         felem_mul(tmp2, ftmp2, x1);
849         felem_reduce(ftmp2, tmp2);
850
851         /* ftmp = z1^2*x2 - z2^2*x1 */
852         felem_diff128(tmp, tmp2);
853         /* tmp[i] < 2^116 + 2^64 + 8 < 2^117 */
854         felem_reduce(ftmp, tmp);
855
856         /* the formulae are incorrect if the points are equal
857          * so we check for this and do doubling if this happens */
858         x_equal = felem_is_zero(ftmp);
859         y_equal = felem_is_zero(ftmp3);
860         z1_is_zero = felem_is_zero(z1);
861         z2_is_zero = felem_is_zero(z2);
862         /* In affine coordinates, (X_1, Y_1) == (X_2, Y_2) */
863         if (x_equal && y_equal && !z1_is_zero && !z2_is_zero)
864                 {
865                 point_double(x3, y3, z3, x1, y1, z1);
866                 return;
867                 }
868
869         /* ftmp5 = z1*z2 */
870         felem_mul(tmp, z1, z2);
871         felem_reduce(ftmp5, tmp);
872
873         /* z3 = (z1^2*x2 - z2^2*x1)*(z1*z2) */
874         felem_mul(tmp, ftmp, ftmp5);
875         felem_reduce(z3, tmp);
876
877         /* ftmp = (z1^2*x2 - z2^2*x1)^2 */
878         memcpy(ftmp5, ftmp, 4 * sizeof(fslice));
879         felem_square(tmp, ftmp);
880         felem_reduce(ftmp, tmp);
881
882         /* ftmp5 = (z1^2*x2 - z2^2*x1)^3 */
883         felem_mul(tmp, ftmp, ftmp5);
884         felem_reduce(ftmp5, tmp);
885
886         /* ftmp2 = z2^2*x1*(z1^2*x2 - z2^2*x1)^2 */
887         felem_mul(tmp, ftmp2, ftmp);
888         felem_reduce(ftmp2, tmp);
889
890         /* ftmp4 = z2^3*y1*(z1^2*x2 - z2^2*x1)^3 */
891         felem_mul(tmp, ftmp4, ftmp5);
892         /* tmp[i] < 4 * 2^57 * 2^57 = 2^116 */
893
894         /* tmp2 = (z1^3*y2 - z2^3*y1)^2 */
895         felem_square(tmp2, ftmp3);
896         /* tmp2[i] < 4 * 2^57 * 2^57 < 2^116 */
897
898         /* tmp2 = (z1^3*y2 - z2^3*y1)^2 - (z1^2*x2 - z2^2*x1)^3 */
899         felem_diff_128_64(tmp2, ftmp5);
900         /* tmp2[i] < 2^116 + 2^64 + 8 < 2^117 */
901
902         /* ftmp5 = 2*z2^2*x1*(z1^2*x2 - z2^2*x1)^2 */
903         memcpy(ftmp5, ftmp2, 4 * sizeof(fslice));
904         felem_scalar64(ftmp5, 2);
905         /* ftmp5[i] < 2 * 2^57 = 2^58 */
906
907         /* x3 = (z1^3*y2 - z2^3*y1)^2 - (z1^2*x2 - z2^2*x1)^3 -
908            2*z2^2*x1*(z1^2*x2 - z2^2*x1)^2 */
909         felem_diff_128_64(tmp2, ftmp5);
910         /* tmp2[i] < 2^117 + 2^64 + 8 < 2^118 */
911         felem_reduce(x3, tmp2);
912
913         /* ftmp2 = z2^2*x1*(z1^2*x2 - z2^2*x1)^2 - x3 */
914         felem_diff64(ftmp2, x3);
915         /* ftmp2[i] < 2^57 + 2^58 + 2 < 2^59 */
916
917         /* tmp2 = (z1^3*y2 - z2^3*y1)*(z2^2*x1*(z1^2*x2 - z2^2*x1)^2 - x3) */
918         felem_mul(tmp2, ftmp3, ftmp2);
919         /* tmp2[i] < 4 * 2^57 * 2^59 = 2^118 */
920
921         /* y3 = (z1^3*y2 - z2^3*y1)*(z2^2*x1*(z1^2*x2 - z2^2*x1)^2 - x3) -
922            z2^3*y1*(z1^2*x2 - z2^2*x1)^3 */
923         felem_diff128(tmp2, tmp);
924         /* tmp2[i] < 2^118 + 2^120 < 2^121 */
925         felem_reduce(y3, tmp2);
926
927         /* the result (x3, y3, z3) is incorrect if one of the inputs is the
928          * point at infinity, so we need to check for this separately */
929
930         /* if point 1 is at infinity, copy point 2 to output, and vice versa */
931         copy_conditional(x3, x2, 4, z1_is_zero);
932         copy_conditional(x3, x1, 4, z2_is_zero);
933         copy_conditional(y3, y2, 4, z1_is_zero);
934         copy_conditional(y3, y1, 4, z2_is_zero);
935         copy_conditional(z3, z2, 4, z1_is_zero);
936         copy_conditional(z3, z1, 4, z2_is_zero);
937         }
938
939 /* Select a point from an array of 16 precomputed point multiples,
940  * in constant time: for bits = {b_0, b_1, b_2, b_3}, return the point
941  * pre_comp[8*b_3 + 4*b_2 + 2*b_1 + b_0] */
942 static void select_point(const fslice bits[4], const fslice pre_comp[16][3][4],
943         fslice out[12])
944         {
945         fslice tmp[5][12];
946         select_conditional(tmp[0], pre_comp[7][0], pre_comp[15][0], 12, bits[3]);
947         select_conditional(tmp[1], pre_comp[3][0], pre_comp[11][0], 12, bits[3]);
948         select_conditional(tmp[2], tmp[1], tmp[0], 12, bits[2]);
949         select_conditional(tmp[0], pre_comp[5][0], pre_comp[13][0], 12, bits[3]);
950         select_conditional(tmp[1], pre_comp[1][0], pre_comp[9][0], 12, bits[3]);
951         select_conditional(tmp[3], tmp[1], tmp[0], 12, bits[2]);
952         select_conditional(tmp[4], tmp[3], tmp[2], 12, bits[1]);
953         select_conditional(tmp[0], pre_comp[6][0], pre_comp[14][0], 12, bits[3]);
954         select_conditional(tmp[1], pre_comp[2][0], pre_comp[10][0], 12, bits[3]);
955         select_conditional(tmp[2], tmp[1], tmp[0], 12, bits[2]);
956         select_conditional(tmp[0], pre_comp[4][0], pre_comp[12][0], 12, bits[3]);
957         select_conditional(tmp[1], pre_comp[0][0], pre_comp[8][0], 12, bits[3]);
958         select_conditional(tmp[3], tmp[1], tmp[0], 12, bits[2]);
959         select_conditional(tmp[1], tmp[3], tmp[2], 12, bits[1]);
960         select_conditional(out, tmp[1], tmp[4], 12, bits[0]);
961         }
962
963 /* Interleaved point multiplication using precomputed point multiples:
964  * The small point multiples 0*P, 1*P, ..., 15*P are in pre_comp[],
965  * the scalars in scalars[]. If g_scalar is non-NULL, we also add this multiple
966  * of the generator, using certain (large) precomputed multiples in g_pre_comp.
967  * Output point (X, Y, Z) is stored in x_out, y_out, z_out */
968 static void batch_mul(fslice x_out[4], fslice y_out[4], fslice z_out[4],
969         const felem_bytearray scalars[], const unsigned num_points, const u8 *g_scalar,
970         const fslice pre_comp[][16][3][4], const fslice g_pre_comp[16][3][4])
971         {
972         unsigned i, j, num;
973         unsigned gen_mul = (g_scalar != NULL);
974         fslice nq[12], nqt[12], tmp[12];
975         fslice bits[4];
976         u8 byte;
977
978         /* set nq to the point at infinity */
979         memset(nq, 0, 12 * sizeof(fslice));
980
981         /* Loop over all scalars msb-to-lsb, 4 bits at a time: for each nibble,
982          * double 4 times, then add the precomputed point multiples.
983          * If we are also adding multiples of the generator, then interleave
984          * these additions with the last 56 doublings. */
985         for (i = (num_points ? 28 : 7); i > 0; --i)
986                 {
987                 for (j = 0; j < 8; ++j)
988                         {
989                         /* double once */
990                         point_double(nq, nq+4, nq+8, nq, nq+4, nq+8);
991                         /* add multiples of the generator */
992                         if ((gen_mul) && (i <= 7))
993                                 {
994                                 bits[3] = (g_scalar[i+20] >> (7-j)) & 1;
995                                 bits[2] = (g_scalar[i+13] >> (7-j)) & 1;
996                                 bits[1] = (g_scalar[i+6] >> (7-j)) & 1;
997                                 bits[0] = (g_scalar[i-1] >> (7-j)) & 1;
998                                 /* select the point to add, in constant time */
999                                 select_point(bits, g_pre_comp, tmp);
1000                                 memcpy(nqt, nq, 12 * sizeof(fslice));
1001                                 point_add(nq, nq+4, nq+8, nqt, nqt+4, nqt+8,
1002                                         tmp, tmp+4, tmp+8);
1003                                 }
1004                         /* do an addition after every 4 doublings */
1005                         if (j % 4 == 3)
1006                                 {
1007                                 /* loop over all scalars */
1008                                 for (num = 0; num < num_points; ++num)
1009                                         {
1010                                         byte = scalars[num][i-1];
1011                                         bits[3] = (byte >> (10-j)) & 1;
1012                                         bits[2] = (byte >> (9-j)) & 1;
1013                                         bits[1] = (byte >> (8-j)) & 1;
1014                                         bits[0] = (byte >> (7-j)) & 1;
1015                                         /* select the point to add */
1016                                         select_point(bits,
1017                                                 pre_comp[num], tmp);
1018                                         memcpy(nqt, nq, 12 * sizeof(fslice));
1019                                         point_add(nq, nq+4, nq+8, nqt, nqt+4,
1020                                                 nqt+8, tmp, tmp+4, tmp+8);
1021                                         }
1022                                 }
1023                         }
1024                 }
1025         memcpy(x_out, nq, 4 * sizeof(fslice));
1026         memcpy(y_out, nq+4, 4 * sizeof(fslice));
1027         memcpy(z_out, nq+8, 4 * sizeof(fslice));
1028         }
1029
1030 /******************************************************************************/
1031 /*                     FUNCTIONS TO MANAGE PRECOMPUTATION
1032  */
1033
1034 static NISTP224_PRE_COMP *nistp224_pre_comp_new()
1035         {
1036         NISTP224_PRE_COMP *ret = NULL;
1037         ret = (NISTP224_PRE_COMP *)OPENSSL_malloc(sizeof(NISTP224_PRE_COMP));
1038         if (!ret)
1039                 {
1040                 ECerr(EC_F_NISTP224_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
1041                 return ret;
1042                 }
1043         memset(ret->g_pre_comp, 0, sizeof(ret->g_pre_comp));
1044         ret->references = 1;
1045         return ret;
1046         }
1047
1048 static void *nistp224_pre_comp_dup(void *src_)
1049         {
1050         NISTP224_PRE_COMP *src = src_;
1051
1052         /* no need to actually copy, these objects never change! */
1053         CRYPTO_add(&src->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
1054
1055         return src_;
1056         }
1057
1058 static void nistp224_pre_comp_free(void *pre_)
1059         {
1060         int i;
1061         NISTP224_PRE_COMP *pre = pre_;
1062
1063         if (!pre)
1064                 return;
1065
1066         i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
1067         if (i > 0)
1068                 return;
1069
1070         OPENSSL_free(pre);
1071         }
1072
1073 static void nistp224_pre_comp_clear_free(void *pre_)
1074         {
1075         int i;
1076         NISTP224_PRE_COMP *pre = pre_;
1077
1078         if (!pre)
1079                 return;
1080
1081         i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
1082         if (i > 0)
1083                 return;
1084
1085         OPENSSL_cleanse(pre, sizeof *pre);
1086         OPENSSL_free(pre);
1087         }
1088
1089 /******************************************************************************/
1090 /*                         OPENSSL EC_METHOD FUNCTIONS
1091  */
1092
1093 int ec_GFp_nistp224_group_init(EC_GROUP *group)
1094         {
1095         int ret;
1096         ret = ec_GFp_simple_group_init(group);
1097         group->a_is_minus3 = 1;
1098         return ret;
1099         }
1100
1101 int ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
1102         const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
1103         {
1104         int ret = 0;
1105         BN_CTX *new_ctx = NULL;
1106         BIGNUM *curve_p, *curve_a, *curve_b;
1107
1108         if (ctx == NULL)
1109                 if ((ctx = new_ctx = BN_CTX_new()) == NULL) return 0;
1110         BN_CTX_start(ctx);
1111         if (((curve_p = BN_CTX_get(ctx)) == NULL) ||
1112                 ((curve_a = BN_CTX_get(ctx)) == NULL) ||
1113                 ((curve_b = BN_CTX_get(ctx)) == NULL)) goto err;
1114         BN_bin2bn(nistp224_curve_params[0], sizeof(felem_bytearray), curve_p);
1115         BN_bin2bn(nistp224_curve_params[1], sizeof(felem_bytearray), curve_a);
1116         BN_bin2bn(nistp224_curve_params[2], sizeof(felem_bytearray), curve_b);
1117         if ((BN_cmp(curve_p, p)) || (BN_cmp(curve_a, a)) ||
1118                 (BN_cmp(curve_b, b)))
1119                 {
1120                 ECerr(EC_F_EC_GFP_NISTP224_GROUP_SET_CURVE,
1121                         EC_R_WRONG_CURVE_PARAMETERS);
1122                 goto err;
1123                 }
1124         group->field_mod_func = BN_nist_mod_224;
1125         ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
1126 err:
1127         BN_CTX_end(ctx);
1128         if (new_ctx != NULL)
1129                 BN_CTX_free(new_ctx);
1130         return ret;
1131         }
1132
1133 /* Takes the Jacobian coordinates (X, Y, Z) of a point and returns
1134  * (X', Y') = (X/Z^2, Y/Z^3) */
1135 int ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
1136         const EC_POINT *point, BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
1137         {
1138         fslice z1[4], z2[4], x_in[4], y_in[4], x_out[4], y_out[4];
1139         uint128_t tmp[7];
1140
1141         if (EC_POINT_is_at_infinity(group, point))
1142                 {
1143                 ECerr(EC_F_EC_GFP_NISTP224_POINT_GET_AFFINE_COORDINATES,
1144                         EC_R_POINT_AT_INFINITY);
1145                 return 0;
1146                 }
1147         if ((!BN_to_felem(x_in, &point->X)) || (!BN_to_felem(y_in, &point->Y)) ||
1148                 (!BN_to_felem(z1, &point->Z))) return 0;
1149         felem_inv(z2, z1);
1150         felem_square(tmp, z2); felem_reduce(z1, tmp);
1151         felem_mul(tmp, x_in, z1); felem_reduce(x_in, tmp);
1152         felem_contract(x_out, x_in);
1153         if (x != NULL)
1154                 {
1155                 if (!felem_to_BN(x, x_out)) {
1156                 ECerr(EC_F_EC_GFP_NISTP224_POINT_GET_AFFINE_COORDINATES,
1157                         ERR_R_BN_LIB);
1158                 return 0;
1159                 }
1160                 }
1161         felem_mul(tmp, z1, z2); felem_reduce(z1, tmp);
1162         felem_mul(tmp, y_in, z1); felem_reduce(y_in, tmp);
1163         felem_contract(y_out, y_in);
1164         if (y != NULL)
1165                 {
1166                 if (!felem_to_BN(y, y_out)) {
1167                 ECerr(EC_F_EC_GFP_NISTP224_POINT_GET_AFFINE_COORDINATES,
1168                         ERR_R_BN_LIB);
1169                 return 0;
1170                 }
1171                 }
1172         return 1;
1173         }
1174
1175 /* Computes scalar*generator + \sum scalars[i]*points[i], ignoring NULL values
1176  * Result is stored in r (r can equal one of the inputs). */
1177 int ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
1178         const BIGNUM *scalar, size_t num, const EC_POINT *points[],
1179         const BIGNUM *scalars[], BN_CTX *ctx)
1180         {
1181         int ret = 0;
1182         int i, j;
1183         BN_CTX *new_ctx = NULL;
1184         BIGNUM *x, *y, *z, *tmp_scalar;
1185         felem_bytearray g_secret;
1186         felem_bytearray *secrets = NULL;
1187         fslice (*pre_comp)[16][3][4] = NULL;
1188         felem_bytearray tmp;
1189         unsigned num_bytes;
1190         int have_pre_comp = 0;
1191         size_t num_points = num;
1192         fslice x_in[4], y_in[4], z_in[4], x_out[4], y_out[4], z_out[4];
1193         NISTP224_PRE_COMP *pre = NULL;
1194         fslice (*g_pre_comp)[3][4] = NULL;
1195         EC_POINT *generator = NULL;
1196         const EC_POINT *p = NULL;
1197         const BIGNUM *p_scalar = NULL;
1198
1199         if (ctx == NULL)
1200                 if ((ctx = new_ctx = BN_CTX_new()) == NULL) return 0;
1201         BN_CTX_start(ctx);
1202         if (((x = BN_CTX_get(ctx)) == NULL) ||
1203                 ((y = BN_CTX_get(ctx)) == NULL) ||
1204                 ((z = BN_CTX_get(ctx)) == NULL) ||
1205                 ((tmp_scalar = BN_CTX_get(ctx)) == NULL))
1206                 goto err;
1207
1208         if (scalar != NULL)
1209                 {
1210                 pre = EC_EX_DATA_get_data(group->extra_data,
1211                         nistp224_pre_comp_dup, nistp224_pre_comp_free,
1212                         nistp224_pre_comp_clear_free);
1213                 if (pre)
1214                         /* we have precomputation, try to use it */
1215                         g_pre_comp = pre->g_pre_comp;
1216                 else
1217                         /* try to use the standard precomputation */
1218                         g_pre_comp = (fslice (*)[3][4]) gmul;
1219                 generator = EC_POINT_new(group);
1220                 if (generator == NULL)
1221                         goto err;
1222                 /* get the generator from precomputation */
1223                 if (!felem_to_BN(x, g_pre_comp[1][0]) ||
1224                         !felem_to_BN(y, g_pre_comp[1][1]) ||
1225                         !felem_to_BN(z, g_pre_comp[1][2]))
1226                         {
1227                         ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
1228                         goto err;
1229                         }
1230                 if (!EC_POINT_set_Jprojective_coordinates_GFp(group,
1231                                 generator, x, y, z, ctx))
1232                         goto err;
1233                 if (0 == EC_POINT_cmp(group, generator, group->generator, ctx))
1234                         /* precomputation matches generator */
1235                         have_pre_comp = 1;
1236                 else
1237                         /* we don't have valid precomputation:
1238                          * treat the generator as a random point */
1239                         num_points = num_points + 1;
1240                 }
1241         secrets = OPENSSL_malloc(num_points * sizeof(felem_bytearray));
1242         pre_comp = OPENSSL_malloc(num_points * 16 * 3 * 4 * sizeof(fslice));
1243
1244         if ((num_points) && ((secrets == NULL) || (pre_comp == NULL)))
1245                 {
1246                 ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_MALLOC_FAILURE);
1247                 goto err;
1248                 }
1249
1250         /* we treat NULL scalars as 0, and NULL points as points at infinity,
1251          * i.e., they contribute nothing to the linear combination */
1252         memset(secrets, 0, num_points * sizeof(felem_bytearray));
1253         memset(pre_comp, 0, num_points * 16 * 3 * 4 * sizeof(fslice));
1254         for (i = 0; i < num_points; ++i)
1255                 {
1256                 if (i == num)
1257                         /* the generator */
1258                         {
1259                         p = EC_GROUP_get0_generator(group);
1260                         p_scalar = scalar;
1261                         }
1262                 else
1263                         /* the i^th point */
1264                         {
1265                         p = points[i];
1266                         p_scalar = scalars[i];
1267                         }
1268                 if ((p_scalar != NULL) && (p != NULL))
1269                         {
1270                         num_bytes = BN_num_bytes(p_scalar);
1271                         /* reduce scalar to 0 <= scalar < 2^224 */
1272                         if ((num_bytes > sizeof(felem_bytearray)) || (BN_is_negative(p_scalar)))
1273                                 {
1274                                 /* this is an unusual input, and we don't guarantee
1275                                  * constant-timeness */
1276                                 if (!BN_nnmod(tmp_scalar, p_scalar, &group->order, ctx))
1277                                         {
1278                                         ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
1279                                         goto err;
1280                                         }
1281                                 num_bytes = BN_bn2bin(tmp_scalar, tmp);
1282                                 }
1283                         else
1284                                 BN_bn2bin(p_scalar, tmp);
1285                         flip_endian(secrets[i], tmp, num_bytes);
1286                         /* precompute multiples */
1287                         if ((!BN_to_felem(x_out, &p->X)) ||
1288                                 (!BN_to_felem(y_out, &p->Y)) ||
1289                                 (!BN_to_felem(z_out, &p->Z))) goto err;
1290                         memcpy(pre_comp[i][1][0], x_out, 4 * sizeof(fslice));
1291                         memcpy(pre_comp[i][1][1], y_out, 4 * sizeof(fslice));
1292                         memcpy(pre_comp[i][1][2], z_out, 4 * sizeof(fslice));
1293                         for (j = 1; j < 8; ++j)
1294                                 {
1295                                 point_double(pre_comp[i][2*j][0],
1296                                         pre_comp[i][2*j][1],
1297                                         pre_comp[i][2*j][2],
1298                                         pre_comp[i][j][0],
1299                                         pre_comp[i][j][1],
1300                                         pre_comp[i][j][2]);
1301                                 point_add(pre_comp[i][2*j+1][0],
1302                                         pre_comp[i][2*j+1][1],
1303                                         pre_comp[i][2*j+1][2],
1304                                         pre_comp[i][1][0],
1305                                         pre_comp[i][1][1],
1306                                         pre_comp[i][1][2],
1307                                         pre_comp[i][2*j][0],
1308                                         pre_comp[i][2*j][1],
1309                                         pre_comp[i][2*j][2]);
1310                                 }
1311                         }
1312                 }
1313
1314         /* the scalar for the generator */
1315         if ((scalar != NULL) && (have_pre_comp))
1316                 {
1317                 memset(g_secret, 0, sizeof g_secret);
1318                 num_bytes = BN_num_bytes(scalar);
1319                 /* reduce scalar to 0 <= scalar < 2^224 */
1320                 if ((num_bytes > sizeof(felem_bytearray)) || (BN_is_negative(scalar)))
1321                         {
1322                         /* this is an unusual input, and we don't guarantee
1323                          * constant-timeness */
1324                         if (!BN_nnmod(tmp_scalar, scalar, &group->order, ctx))
1325                                 {
1326                                 ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
1327                                 goto err;
1328                                 }
1329                         num_bytes = BN_bn2bin(tmp_scalar, tmp);
1330                         }
1331                 else
1332                         BN_bn2bin(scalar, tmp);
1333                 flip_endian(g_secret, tmp, num_bytes);
1334                 /* do the multiplication with generator precomputation*/
1335                 batch_mul(x_out, y_out, z_out,
1336                         (const felem_bytearray (*)) secrets, num_points,
1337                         g_secret, (const fslice (*)[16][3][4]) pre_comp,
1338                         (const fslice (*)[3][4]) g_pre_comp);
1339                 }
1340         else
1341                 /* do the multiplication without generator precomputation */
1342                 batch_mul(x_out, y_out, z_out,
1343                         (const felem_bytearray (*)) secrets, num_points,
1344                         NULL, (const fslice (*)[16][3][4]) pre_comp, NULL);
1345         /* reduce the output to its unique minimal representation */
1346         felem_contract(x_in, x_out);
1347         felem_contract(y_in, y_out);
1348         felem_contract(z_in, z_out);
1349         if ((!felem_to_BN(x, x_in)) || (!felem_to_BN(y, y_in)) ||
1350                 (!felem_to_BN(z, z_in)))
1351                 {
1352                 ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
1353                 goto err;
1354                 }
1355         ret = EC_POINT_set_Jprojective_coordinates_GFp(group, r, x, y, z, ctx);
1356
1357 err:
1358         BN_CTX_end(ctx);
1359         if (generator != NULL)
1360                 EC_POINT_free(generator);
1361         if (new_ctx != NULL)
1362                 BN_CTX_free(new_ctx);
1363         if (secrets != NULL)
1364                 OPENSSL_free(secrets);
1365         if (pre_comp != NULL)
1366                 OPENSSL_free(pre_comp);
1367         return ret;
1368         }
1369
1370 int ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1371         {
1372         int ret = 0;
1373         NISTP224_PRE_COMP *pre = NULL;
1374         int i, j;
1375         BN_CTX *new_ctx = NULL;
1376         BIGNUM *x, *y;
1377         EC_POINT *generator = NULL;
1378
1379         /* throw away old precomputation */
1380         EC_EX_DATA_free_data(&group->extra_data, nistp224_pre_comp_dup,
1381                 nistp224_pre_comp_free, nistp224_pre_comp_clear_free);
1382         if (ctx == NULL)
1383                 if ((ctx = new_ctx = BN_CTX_new()) == NULL) return 0;
1384         BN_CTX_start(ctx);
1385         if (((x = BN_CTX_get(ctx)) == NULL) ||
1386                 ((y = BN_CTX_get(ctx)) == NULL))
1387                 goto err;
1388         /* get the generator */
1389         if (group->generator == NULL) goto err;
1390         generator = EC_POINT_new(group);
1391         if (generator == NULL)
1392                 goto err;
1393         BN_bin2bn(nistp224_curve_params[3], sizeof (felem_bytearray), x);
1394         BN_bin2bn(nistp224_curve_params[4], sizeof (felem_bytearray), y);
1395         if (!EC_POINT_set_affine_coordinates_GFp(group, generator, x, y, ctx))
1396                 goto err;
1397         if ((pre = nistp224_pre_comp_new()) == NULL)
1398                 goto err;
1399         /* if the generator is the standard one, use built-in precomputation */
1400         if (0 == EC_POINT_cmp(group, generator, group->generator, ctx))
1401                 {
1402                 memcpy(pre->g_pre_comp, gmul, sizeof(pre->g_pre_comp));
1403                 ret = 1;
1404                 goto err;
1405                 }
1406         if ((!BN_to_felem(pre->g_pre_comp[1][0], &group->generator->X)) ||
1407                 (!BN_to_felem(pre->g_pre_comp[1][1], &group->generator->Y)) ||
1408                 (!BN_to_felem(pre->g_pre_comp[1][2], &group->generator->Z)))
1409                 goto err;
1410         /* compute 2^56*G, 2^112*G, 2^168*G */
1411         for (i = 1; i < 5; ++i)
1412                 {
1413                 point_double(pre->g_pre_comp[2*i][0], pre->g_pre_comp[2*i][1],
1414                         pre->g_pre_comp[2*i][2], pre->g_pre_comp[i][0],
1415                         pre->g_pre_comp[i][1], pre->g_pre_comp[i][2]);
1416                 for (j = 0; j < 55; ++j)
1417                         {
1418                         point_double(pre->g_pre_comp[2*i][0],
1419                                 pre->g_pre_comp[2*i][1],
1420                                 pre->g_pre_comp[2*i][2],
1421                                 pre->g_pre_comp[2*i][0],
1422                                 pre->g_pre_comp[2*i][1],
1423                                 pre->g_pre_comp[2*i][2]);
1424                         }
1425                 }
1426         /* g_pre_comp[0] is the point at infinity */
1427         memset(pre->g_pre_comp[0], 0, sizeof(pre->g_pre_comp[0]));
1428         /* the remaining multiples */
1429         /* 2^56*G + 2^112*G */
1430         point_add(pre->g_pre_comp[6][0], pre->g_pre_comp[6][1],
1431                 pre->g_pre_comp[6][2], pre->g_pre_comp[4][0],
1432                 pre->g_pre_comp[4][1], pre->g_pre_comp[4][2],
1433                 pre->g_pre_comp[2][0], pre->g_pre_comp[2][1],
1434                 pre->g_pre_comp[2][2]);
1435         /* 2^56*G + 2^168*G */
1436         point_add(pre->g_pre_comp[10][0], pre->g_pre_comp[10][1],
1437                 pre->g_pre_comp[10][2], pre->g_pre_comp[8][0],
1438                 pre->g_pre_comp[8][1], pre->g_pre_comp[8][2],
1439                 pre->g_pre_comp[2][0], pre->g_pre_comp[2][1],
1440                 pre->g_pre_comp[2][2]);
1441         /* 2^112*G + 2^168*G */
1442         point_add(pre->g_pre_comp[12][0], pre->g_pre_comp[12][1],
1443                 pre->g_pre_comp[12][2], pre->g_pre_comp[8][0],
1444                 pre->g_pre_comp[8][1], pre->g_pre_comp[8][2],
1445                 pre->g_pre_comp[4][0], pre->g_pre_comp[4][1],
1446                 pre->g_pre_comp[4][2]);
1447         /* 2^56*G + 2^112*G + 2^168*G */
1448         point_add(pre->g_pre_comp[14][0], pre->g_pre_comp[14][1],
1449                 pre->g_pre_comp[14][2], pre->g_pre_comp[12][0],
1450                 pre->g_pre_comp[12][1], pre->g_pre_comp[12][2],
1451                 pre->g_pre_comp[2][0], pre->g_pre_comp[2][1],
1452                 pre->g_pre_comp[2][2]);
1453         for (i = 1; i < 8; ++i)
1454                 {
1455                 /* odd multiples: add G */
1456                 point_add(pre->g_pre_comp[2*i+1][0], pre->g_pre_comp[2*i+1][1],
1457                         pre->g_pre_comp[2*i+1][2], pre->g_pre_comp[2*i][0],
1458                         pre->g_pre_comp[2*i][1], pre->g_pre_comp[2*i][2],
1459                         pre->g_pre_comp[1][0], pre->g_pre_comp[1][1],
1460                         pre->g_pre_comp[1][2]);
1461                 }
1462
1463         if (!EC_EX_DATA_set_data(&group->extra_data, pre, nistp224_pre_comp_dup,
1464                         nistp224_pre_comp_free, nistp224_pre_comp_clear_free))
1465                 goto err;
1466         ret = 1;
1467         pre = NULL;
1468  err:
1469         BN_CTX_end(ctx);
1470         if (generator != NULL)
1471                 EC_POINT_free(generator);
1472         if (new_ctx != NULL)
1473                 BN_CTX_free(new_ctx);
1474         if (pre)
1475                 nistp224_pre_comp_free(pre);
1476         return ret;
1477         }
1478
1479 int ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group)
1480         {
1481         if (EC_EX_DATA_get_data(group->extra_data, nistp224_pre_comp_dup,
1482                         nistp224_pre_comp_free, nistp224_pre_comp_clear_free)
1483                 != NULL)
1484                 return 1;
1485         else
1486                 return 0;
1487         }
1488
1489 #else
1490 static void *dummy=&dummy;
1491 #endif