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