blob: 31097d43f3af9252a44cb093ef0471e2b5675650 [file] [log] [blame]
Kenny Roote99801b2015-11-06 15:31:15 -08001/* Copyright (c) 2015, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15/* A 64-bit implementation of the NIST P-224 elliptic curve point multiplication
16 *
17 * Inspired by Daniel J. Bernstein's public domain nistp224 implementation
18 * and Adam Langley's public domain 64-bit C implementation of curve25519. */
19
20#include <openssl/base.h>
21
22#if defined(OPENSSL_64_BIT) && !defined(OPENSSL_WINDOWS) && \
23 !defined(OPENSSL_SMALL)
24
25#include <openssl/bn.h>
26#include <openssl/ec.h>
27#include <openssl/err.h>
28#include <openssl/mem.h>
Kenny Roote99801b2015-11-06 15:31:15 -080029
30#include <string.h>
31
32#include "internal.h"
Robert Sloan8ff03552017-06-14 12:40:58 -070033#include "../delocate.h"
34#include "../../internal.h"
Kenny Roote99801b2015-11-06 15:31:15 -080035
36
Kenny Roote99801b2015-11-06 15:31:15 -080037/* Field elements are represented as a_0 + 2^56*a_1 + 2^112*a_2 + 2^168*a_3
38 * using 64-bit coefficients called 'limbs', and sometimes (for multiplication
39 * results) as b_0 + 2^56*b_1 + 2^112*b_2 + 2^168*b_3 + 2^224*b_4 + 2^280*b_5 +
Robert Sloan8ff03552017-06-14 12:40:58 -070040 * 2^336*b_6 using 128-bit coefficients called 'widelimbs'. A 4-p224_limb
41 * representation is an 'p224_felem'; a 7-p224_widelimb representation is a
42 * 'p224_widefelem'. Even within felems, bits of adjacent limbs overlap, and we
43 * don't always reduce the representations: we ensure that inputs to each
44 * p224_felem multiplication satisfy a_i < 2^60, so outputs satisfy b_i <
45 * 4*2^60*2^60, and fit into a 128-bit word without overflow. The coefficients
46 * are then again partially reduced to obtain an p224_felem satisfying a_i <
47 * 2^57. We only reduce to the unique minimal representation at the end of the
48 * computation. */
Kenny Roote99801b2015-11-06 15:31:15 -080049
Robert Sloan8ff03552017-06-14 12:40:58 -070050typedef uint64_t p224_limb;
51typedef uint128_t p224_widelimb;
Kenny Roote99801b2015-11-06 15:31:15 -080052
Robert Sloan8ff03552017-06-14 12:40:58 -070053typedef p224_limb p224_felem[4];
54typedef p224_widelimb p224_widefelem[7];
Kenny Roote99801b2015-11-06 15:31:15 -080055
56/* Field element represented as a byte arrary. 28*8 = 224 bits is also the
57 * group order size for the elliptic curve, and we also use this type for
58 * scalars for point multiplication. */
Robert Sloan8ff03552017-06-14 12:40:58 -070059typedef uint8_t p224_felem_bytearray[28];
Kenny Roote99801b2015-11-06 15:31:15 -080060
Kenny Roote99801b2015-11-06 15:31:15 -080061/* Precomputed multiples of the standard generator
62 * Points are given in coordinates (X, Y, Z) where Z normally is 1
63 * (0 for the point at infinity).
64 * For each field element, slice a_0 is word 0, etc.
65 *
66 * The table has 2 * 16 elements, starting with the following:
67 * index | bits | point
68 * ------+---------+------------------------------
69 * 0 | 0 0 0 0 | 0G
70 * 1 | 0 0 0 1 | 1G
71 * 2 | 0 0 1 0 | 2^56G
72 * 3 | 0 0 1 1 | (2^56 + 1)G
73 * 4 | 0 1 0 0 | 2^112G
74 * 5 | 0 1 0 1 | (2^112 + 1)G
75 * 6 | 0 1 1 0 | (2^112 + 2^56)G
76 * 7 | 0 1 1 1 | (2^112 + 2^56 + 1)G
77 * 8 | 1 0 0 0 | 2^168G
78 * 9 | 1 0 0 1 | (2^168 + 1)G
79 * 10 | 1 0 1 0 | (2^168 + 2^56)G
80 * 11 | 1 0 1 1 | (2^168 + 2^56 + 1)G
81 * 12 | 1 1 0 0 | (2^168 + 2^112)G
82 * 13 | 1 1 0 1 | (2^168 + 2^112 + 1)G
83 * 14 | 1 1 1 0 | (2^168 + 2^112 + 2^56)G
84 * 15 | 1 1 1 1 | (2^168 + 2^112 + 2^56 + 1)G
85 * followed by a copy of this with each element multiplied by 2^28.
86 *
87 * The reason for this is so that we can clock bits into four different
88 * locations when doing simple scalar multiplies against the base point,
89 * and then another four locations using the second 16 elements. */
Robert Sloan8ff03552017-06-14 12:40:58 -070090static const p224_felem g_p224_pre_comp[2][16][3] = {
Kenny Roote99801b2015-11-06 15:31:15 -080091 {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
92 {{0x3280d6115c1d21, 0xc1d356c2112234, 0x7f321390b94a03, 0xb70e0cbd6bb4bf},
93 {0xd5819985007e34, 0x75a05a07476444, 0xfb4c22dfe6cd43, 0xbd376388b5f723},
94 {1, 0, 0, 0}},
95 {{0xfd9675666ebbe9, 0xbca7664d40ce5e, 0x2242df8d8a2a43, 0x1f49bbb0f99bc5},
96 {0x29e0b892dc9c43, 0xece8608436e662, 0xdc858f185310d0, 0x9812dd4eb8d321},
97 {1, 0, 0, 0}},
98 {{0x6d3e678d5d8eb8, 0x559eed1cb362f1, 0x16e9a3bbce8a3f, 0xeedcccd8c2a748},
99 {0xf19f90ed50266d, 0xabf2b4bf65f9df, 0x313865468fafec, 0x5cb379ba910a17},
100 {1, 0, 0, 0}},
101 {{0x0641966cab26e3, 0x91fb2991fab0a0, 0xefec27a4e13a0b, 0x0499aa8a5f8ebe},
102 {0x7510407766af5d, 0x84d929610d5450, 0x81d77aae82f706, 0x6916f6d4338c5b},
103 {1, 0, 0, 0}},
104 {{0xea95ac3b1f15c6, 0x086000905e82d4, 0xdd323ae4d1c8b1, 0x932b56be7685a3},
105 {0x9ef93dea25dbbf, 0x41665960f390f0, 0xfdec76dbe2a8a7, 0x523e80f019062a},
106 {1, 0, 0, 0}},
107 {{0x822fdd26732c73, 0xa01c83531b5d0f, 0x363f37347c1ba4, 0xc391b45c84725c},
108 {0xbbd5e1b2d6ad24, 0xddfbcde19dfaec, 0xc393da7e222a7f, 0x1efb7890ede244},
109 {1, 0, 0, 0}},
110 {{0x4c9e90ca217da1, 0xd11beca79159bb, 0xff8d33c2c98b7c, 0x2610b39409f849},
111 {0x44d1352ac64da0, 0xcdbb7b2c46b4fb, 0x966c079b753c89, 0xfe67e4e820b112},
112 {1, 0, 0, 0}},
113 {{0xe28cae2df5312d, 0xc71b61d16f5c6e, 0x79b7619a3e7c4c, 0x05c73240899b47},
114 {0x9f7f6382c73e3a, 0x18615165c56bda, 0x641fab2116fd56, 0x72855882b08394},
115 {1, 0, 0, 0}},
116 {{0x0469182f161c09, 0x74a98ca8d00fb5, 0xb89da93489a3e0, 0x41c98768fb0c1d},
117 {0xe5ea05fb32da81, 0x3dce9ffbca6855, 0x1cfe2d3fbf59e6, 0x0e5e03408738a7},
118 {1, 0, 0, 0}},
119 {{0xdab22b2333e87f, 0x4430137a5dd2f6, 0xe03ab9f738beb8, 0xcb0c5d0dc34f24},
120 {0x764a7df0c8fda5, 0x185ba5c3fa2044, 0x9281d688bcbe50, 0xc40331df893881},
121 {1, 0, 0, 0}},
122 {{0xb89530796f0f60, 0xade92bd26909a3, 0x1a0c83fb4884da, 0x1765bf22a5a984},
123 {0x772a9ee75db09e, 0x23bc6c67cec16f, 0x4c1edba8b14e2f, 0xe2a215d9611369},
124 {1, 0, 0, 0}},
125 {{0x571e509fb5efb3, 0xade88696410552, 0xc8ae85fada74fe, 0x6c7e4be83bbde3},
126 {0xff9f51160f4652, 0xb47ce2495a6539, 0xa2946c53b582f4, 0x286d2db3ee9a60},
127 {1, 0, 0, 0}},
128 {{0x40bbd5081a44af, 0x0995183b13926c, 0xbcefba6f47f6d0, 0x215619e9cc0057},
129 {0x8bc94d3b0df45e, 0xf11c54a3694f6f, 0x8631b93cdfe8b5, 0xe7e3f4b0982db9},
130 {1, 0, 0, 0}},
131 {{0xb17048ab3e1c7b, 0xac38f36ff8a1d8, 0x1c29819435d2c6, 0xc813132f4c07e9},
132 {0x2891425503b11f, 0x08781030579fea, 0xf5426ba5cc9674, 0x1e28ebf18562bc},
133 {1, 0, 0, 0}},
134 {{0x9f31997cc864eb, 0x06cd91d28b5e4c, 0xff17036691a973, 0xf1aef351497c58},
135 {0xdd1f2d600564ff, 0xdead073b1402db, 0x74a684435bd693, 0xeea7471f962558},
136 {1, 0, 0, 0}}},
137 {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
138 {{0x9665266dddf554, 0x9613d78b60ef2d, 0xce27a34cdba417, 0xd35ab74d6afc31},
139 {0x85ccdd22deb15e, 0x2137e5783a6aab, 0xa141cffd8c93c6, 0x355a1830e90f2d},
140 {1, 0, 0, 0}},
141 {{0x1a494eadaade65, 0xd6da4da77fe53c, 0xe7992996abec86, 0x65c3553c6090e3},
142 {0xfa610b1fb09346, 0xf1c6540b8a4aaf, 0xc51a13ccd3cbab, 0x02995b1b18c28a},
143 {1, 0, 0, 0}},
144 {{0x7874568e7295ef, 0x86b419fbe38d04, 0xdc0690a7550d9a, 0xd3966a44beac33},
145 {0x2b7280ec29132f, 0xbeaa3b6a032df3, 0xdc7dd88ae41200, 0xd25e2513e3a100},
146 {1, 0, 0, 0}},
147 {{0x924857eb2efafd, 0xac2bce41223190, 0x8edaa1445553fc, 0x825800fd3562d5},
148 {0x8d79148ea96621, 0x23a01c3dd9ed8d, 0xaf8b219f9416b5, 0xd8db0cc277daea},
149 {1, 0, 0, 0}},
150 {{0x76a9c3b1a700f0, 0xe9acd29bc7e691, 0x69212d1a6b0327, 0x6322e97fe154be},
151 {0x469fc5465d62aa, 0x8d41ed18883b05, 0x1f8eae66c52b88, 0xe4fcbe9325be51},
152 {1, 0, 0, 0}},
153 {{0x825fdf583cac16, 0x020b857c7b023a, 0x683c17744b0165, 0x14ffd0a2daf2f1},
154 {0x323b36184218f9, 0x4944ec4e3b47d4, 0xc15b3080841acf, 0x0bced4b01a28bb},
155 {1, 0, 0, 0}},
156 {{0x92ac22230df5c4, 0x52f33b4063eda8, 0xcb3f19870c0c93, 0x40064f2ba65233},
157 {0xfe16f0924f8992, 0x012da25af5b517, 0x1a57bb24f723a6, 0x06f8bc76760def},
158 {1, 0, 0, 0}},
159 {{0x4a7084f7817cb9, 0xbcab0738ee9a78, 0x3ec11e11d9c326, 0xdc0fe90e0f1aae},
160 {0xcf639ea5f98390, 0x5c350aa22ffb74, 0x9afae98a4047b7, 0x956ec2d617fc45},
161 {1, 0, 0, 0}},
162 {{0x4306d648c1be6a, 0x9247cd8bc9a462, 0xf5595e377d2f2e, 0xbd1c3caff1a52e},
163 {0x045e14472409d0, 0x29f3e17078f773, 0x745a602b2d4f7d, 0x191837685cdfbb},
164 {1, 0, 0, 0}},
165 {{0x5b6ee254a8cb79, 0x4953433f5e7026, 0xe21faeb1d1def4, 0xc4c225785c09de},
166 {0x307ce7bba1e518, 0x31b125b1036db8, 0x47e91868839e8f, 0xc765866e33b9f3},
167 {1, 0, 0, 0}},
168 {{0x3bfece24f96906, 0x4794da641e5093, 0xde5df64f95db26, 0x297ecd89714b05},
169 {0x701bd3ebb2c3aa, 0x7073b4f53cb1d5, 0x13c5665658af16, 0x9895089d66fe58},
170 {1, 0, 0, 0}},
171 {{0x0fef05f78c4790, 0x2d773633b05d2e, 0x94229c3a951c94, 0xbbbd70df4911bb},
172 {0xb2c6963d2c1168, 0x105f47a72b0d73, 0x9fdf6111614080, 0x7b7e94b39e67b0},
173 {1, 0, 0, 0}},
174 {{0xad1a7d6efbe2b3, 0xf012482c0da69d, 0x6b3bdf12438345, 0x40d7558d7aa4d9},
175 {0x8a09fffb5c6d3d, 0x9a356e5d9ffd38, 0x5973f15f4f9b1c, 0xdcd5f59f63c3ea},
176 {1, 0, 0, 0}},
177 {{0xacf39f4c5ca7ab, 0x4c8071cc5fd737, 0xc64e3602cd1184, 0x0acd4644c9abba},
178 {0x6c011a36d8bf6e, 0xfecd87ba24e32a, 0x19f6f56574fad8, 0x050b204ced9405},
179 {1, 0, 0, 0}},
180 {{0xed4f1cae7d9a96, 0x5ceef7ad94c40a, 0x778e4a3bf3ef9b, 0x7405783dc3b55e},
181 {0x32477c61b6e8c6, 0xb46a97570f018b, 0x91176d0a7e95d1, 0x3df90fbc4c7d0e},
182 {1, 0, 0, 0}}}};
183
184/* Helper functions to convert field elements to/from internal representation */
Robert Sloan8ff03552017-06-14 12:40:58 -0700185static void p224_bin28_to_felem(p224_felem out, const uint8_t in[28]) {
Kenny Roote99801b2015-11-06 15:31:15 -0800186 out[0] = *((const uint64_t *)(in)) & 0x00ffffffffffffff;
187 out[1] = (*((const uint64_t *)(in + 7))) & 0x00ffffffffffffff;
188 out[2] = (*((const uint64_t *)(in + 14))) & 0x00ffffffffffffff;
189 out[3] = (*((const uint64_t *)(in + 20))) >> 8;
190}
191
Robert Sloan8ff03552017-06-14 12:40:58 -0700192static void p224_felem_to_bin28(uint8_t out[28], const p224_felem in) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400193 for (size_t i = 0; i < 7; ++i) {
Kenny Roote99801b2015-11-06 15:31:15 -0800194 out[i] = in[0] >> (8 * i);
195 out[i + 7] = in[1] >> (8 * i);
196 out[i + 14] = in[2] >> (8 * i);
197 out[i + 21] = in[3] >> (8 * i);
198 }
199}
200
201/* To preserve endianness when using BN_bn2bin and BN_bin2bn */
Robert Sloan8ff03552017-06-14 12:40:58 -0700202static void p224_flip_endian(uint8_t *out, const uint8_t *in, size_t len) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400203 for (size_t i = 0; i < len; ++i) {
Kenny Roote99801b2015-11-06 15:31:15 -0800204 out[i] = in[len - 1 - i];
205 }
206}
207
208/* From OpenSSL BIGNUM to internal representation */
Robert Sloan8ff03552017-06-14 12:40:58 -0700209static int p224_BN_to_felem(p224_felem out, const BIGNUM *bn) {
Kenny Roote99801b2015-11-06 15:31:15 -0800210 /* BN_bn2bin eats leading zeroes */
Robert Sloan8ff03552017-06-14 12:40:58 -0700211 p224_felem_bytearray b_out;
Robert Sloan69939df2017-01-09 10:53:07 -0800212 OPENSSL_memset(b_out, 0, sizeof(b_out));
David Benjamin4969cc92016-04-22 15:02:23 -0400213 size_t num_bytes = BN_num_bytes(bn);
Kenny Roote99801b2015-11-06 15:31:15 -0800214 if (num_bytes > sizeof(b_out) ||
215 BN_is_negative(bn)) {
216 OPENSSL_PUT_ERROR(EC, EC_R_BIGNUM_OUT_OF_RANGE);
217 return 0;
218 }
219
Robert Sloan8ff03552017-06-14 12:40:58 -0700220 p224_felem_bytearray b_in;
Kenny Roote99801b2015-11-06 15:31:15 -0800221 num_bytes = BN_bn2bin(bn, b_in);
Robert Sloan8ff03552017-06-14 12:40:58 -0700222 p224_flip_endian(b_out, b_in, num_bytes);
223 p224_bin28_to_felem(out, b_out);
Kenny Roote99801b2015-11-06 15:31:15 -0800224 return 1;
225}
226
227/* From internal representation to OpenSSL BIGNUM */
Robert Sloan8ff03552017-06-14 12:40:58 -0700228static BIGNUM *p224_felem_to_BN(BIGNUM *out, const p224_felem in) {
229 p224_felem_bytearray b_in, b_out;
230 p224_felem_to_bin28(b_in, in);
231 p224_flip_endian(b_out, b_in, sizeof(b_out));
Kenny Roote99801b2015-11-06 15:31:15 -0800232 return BN_bin2bn(b_out, sizeof(b_out), out);
233}
234
235/* Field operations, using the internal representation of field elements.
236 * NB! These operations are specific to our point multiplication and cannot be
237 * expected to be correct in general - e.g., multiplication with a large scalar
238 * will cause an overflow. */
239
Robert Sloan8ff03552017-06-14 12:40:58 -0700240static void p224_felem_assign(p224_felem out, const p224_felem in) {
Kenny Roote99801b2015-11-06 15:31:15 -0800241 out[0] = in[0];
242 out[1] = in[1];
243 out[2] = in[2];
244 out[3] = in[3];
245}
246
247/* Sum two field elements: out += in */
Robert Sloan8ff03552017-06-14 12:40:58 -0700248static void p224_felem_sum(p224_felem out, const p224_felem in) {
Kenny Roote99801b2015-11-06 15:31:15 -0800249 out[0] += in[0];
250 out[1] += in[1];
251 out[2] += in[2];
252 out[3] += in[3];
253}
254
255/* Get negative value: out = -in */
256/* Assumes in[i] < 2^57 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700257static void p224_felem_neg(p224_felem out, const p224_felem in) {
258 static const p224_limb two58p2 =
259 (((p224_limb)1) << 58) + (((p224_limb)1) << 2);
260 static const p224_limb two58m2 =
261 (((p224_limb)1) << 58) - (((p224_limb)1) << 2);
262 static const p224_limb two58m42m2 =
263 (((p224_limb)1) << 58) - (((p224_limb)1) << 42) - (((p224_limb)1) << 2);
Kenny Roote99801b2015-11-06 15:31:15 -0800264
265 /* Set to 0 mod 2^224-2^96+1 to ensure out > in */
266 out[0] = two58p2 - in[0];
267 out[1] = two58m42m2 - in[1];
268 out[2] = two58m2 - in[2];
269 out[3] = two58m2 - in[3];
270}
271
272/* Subtract field elements: out -= in */
273/* Assumes in[i] < 2^57 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700274static void p224_felem_diff(p224_felem out, const p224_felem in) {
275 static const p224_limb two58p2 =
276 (((p224_limb)1) << 58) + (((p224_limb)1) << 2);
277 static const p224_limb two58m2 =
278 (((p224_limb)1) << 58) - (((p224_limb)1) << 2);
279 static const p224_limb two58m42m2 =
280 (((p224_limb)1) << 58) - (((p224_limb)1) << 42) - (((p224_limb)1) << 2);
Kenny Roote99801b2015-11-06 15:31:15 -0800281
282 /* Add 0 mod 2^224-2^96+1 to ensure out > in */
283 out[0] += two58p2;
284 out[1] += two58m42m2;
285 out[2] += two58m2;
286 out[3] += two58m2;
287
288 out[0] -= in[0];
289 out[1] -= in[1];
290 out[2] -= in[2];
291 out[3] -= in[3];
292}
293
294/* Subtract in unreduced 128-bit mode: out -= in */
295/* Assumes in[i] < 2^119 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700296static void p224_widefelem_diff(p224_widefelem out, const p224_widefelem in) {
297 static const p224_widelimb two120 = ((p224_widelimb)1) << 120;
298 static const p224_widelimb two120m64 =
299 (((p224_widelimb)1) << 120) - (((p224_widelimb)1) << 64);
300 static const p224_widelimb two120m104m64 = (((p224_widelimb)1) << 120) -
301 (((p224_widelimb)1) << 104) -
302 (((p224_widelimb)1) << 64);
Kenny Roote99801b2015-11-06 15:31:15 -0800303
304 /* Add 0 mod 2^224-2^96+1 to ensure out > in */
305 out[0] += two120;
306 out[1] += two120m64;
307 out[2] += two120m64;
308 out[3] += two120;
309 out[4] += two120m104m64;
310 out[5] += two120m64;
311 out[6] += two120m64;
312
313 out[0] -= in[0];
314 out[1] -= in[1];
315 out[2] -= in[2];
316 out[3] -= in[3];
317 out[4] -= in[4];
318 out[5] -= in[5];
319 out[6] -= in[6];
320}
321
322/* Subtract in mixed mode: out128 -= in64 */
323/* in[i] < 2^63 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700324static void p224_felem_diff_128_64(p224_widefelem out, const p224_felem in) {
325 static const p224_widelimb two64p8 =
326 (((p224_widelimb)1) << 64) + (((p224_widelimb)1) << 8);
327 static const p224_widelimb two64m8 =
328 (((p224_widelimb)1) << 64) - (((p224_widelimb)1) << 8);
329 static const p224_widelimb two64m48m8 = (((p224_widelimb)1) << 64) -
330 (((p224_widelimb)1) << 48) -
331 (((p224_widelimb)1) << 8);
Kenny Roote99801b2015-11-06 15:31:15 -0800332
333 /* Add 0 mod 2^224-2^96+1 to ensure out > in */
334 out[0] += two64p8;
335 out[1] += two64m48m8;
336 out[2] += two64m8;
337 out[3] += two64m8;
338
339 out[0] -= in[0];
340 out[1] -= in[1];
341 out[2] -= in[2];
342 out[3] -= in[3];
343}
344
345/* Multiply a field element by a scalar: out = out * scalar
346 * The scalars we actually use are small, so results fit without overflow */
Robert Sloan8ff03552017-06-14 12:40:58 -0700347static void p224_felem_scalar(p224_felem out, const p224_limb scalar) {
Kenny Roote99801b2015-11-06 15:31:15 -0800348 out[0] *= scalar;
349 out[1] *= scalar;
350 out[2] *= scalar;
351 out[3] *= scalar;
352}
353
354/* Multiply an unreduced field element by a scalar: out = out * scalar
355 * The scalars we actually use are small, so results fit without overflow */
Robert Sloan8ff03552017-06-14 12:40:58 -0700356static void p224_widefelem_scalar(p224_widefelem out,
357 const p224_widelimb scalar) {
Kenny Roote99801b2015-11-06 15:31:15 -0800358 out[0] *= scalar;
359 out[1] *= scalar;
360 out[2] *= scalar;
361 out[3] *= scalar;
362 out[4] *= scalar;
363 out[5] *= scalar;
364 out[6] *= scalar;
365}
366
367/* Square a field element: out = in^2 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700368static void p224_felem_square(p224_widefelem out, const p224_felem in) {
369 p224_limb tmp0, tmp1, tmp2;
Kenny Roote99801b2015-11-06 15:31:15 -0800370 tmp0 = 2 * in[0];
371 tmp1 = 2 * in[1];
372 tmp2 = 2 * in[2];
Robert Sloan8ff03552017-06-14 12:40:58 -0700373 out[0] = ((p224_widelimb)in[0]) * in[0];
374 out[1] = ((p224_widelimb)in[0]) * tmp1;
375 out[2] = ((p224_widelimb)in[0]) * tmp2 + ((p224_widelimb)in[1]) * in[1];
376 out[3] = ((p224_widelimb)in[3]) * tmp0 + ((p224_widelimb)in[1]) * tmp2;
377 out[4] = ((p224_widelimb)in[3]) * tmp1 + ((p224_widelimb)in[2]) * in[2];
378 out[5] = ((p224_widelimb)in[3]) * tmp2;
379 out[6] = ((p224_widelimb)in[3]) * in[3];
Kenny Roote99801b2015-11-06 15:31:15 -0800380}
381
382/* Multiply two field elements: out = in1 * in2 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700383static void p224_felem_mul(p224_widefelem out, const p224_felem in1,
384 const p224_felem in2) {
385 out[0] = ((p224_widelimb)in1[0]) * in2[0];
386 out[1] = ((p224_widelimb)in1[0]) * in2[1] + ((p224_widelimb)in1[1]) * in2[0];
387 out[2] = ((p224_widelimb)in1[0]) * in2[2] + ((p224_widelimb)in1[1]) * in2[1] +
388 ((p224_widelimb)in1[2]) * in2[0];
389 out[3] = ((p224_widelimb)in1[0]) * in2[3] + ((p224_widelimb)in1[1]) * in2[2] +
390 ((p224_widelimb)in1[2]) * in2[1] + ((p224_widelimb)in1[3]) * in2[0];
391 out[4] = ((p224_widelimb)in1[1]) * in2[3] + ((p224_widelimb)in1[2]) * in2[2] +
392 ((p224_widelimb)in1[3]) * in2[1];
393 out[5] = ((p224_widelimb)in1[2]) * in2[3] + ((p224_widelimb)in1[3]) * in2[2];
394 out[6] = ((p224_widelimb)in1[3]) * in2[3];
Kenny Roote99801b2015-11-06 15:31:15 -0800395}
396
397/* Reduce seven 128-bit coefficients to four 64-bit coefficients.
398 * Requires in[i] < 2^126,
399 * ensures out[0] < 2^56, out[1] < 2^56, out[2] < 2^56, out[3] <= 2^56 + 2^16 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700400static void p224_felem_reduce(p224_felem out, const p224_widefelem in) {
401 static const p224_widelimb two127p15 =
402 (((p224_widelimb)1) << 127) + (((p224_widelimb)1) << 15);
403 static const p224_widelimb two127m71 =
404 (((p224_widelimb)1) << 127) - (((p224_widelimb)1) << 71);
405 static const p224_widelimb two127m71m55 = (((p224_widelimb)1) << 127) -
406 (((p224_widelimb)1) << 71) -
407 (((p224_widelimb)1) << 55);
408 p224_widelimb output[5];
Kenny Roote99801b2015-11-06 15:31:15 -0800409
410 /* Add 0 mod 2^224-2^96+1 to ensure all differences are positive */
411 output[0] = in[0] + two127p15;
412 output[1] = in[1] + two127m71m55;
413 output[2] = in[2] + two127m71;
414 output[3] = in[3];
415 output[4] = in[4];
416
417 /* Eliminate in[4], in[5], in[6] */
418 output[4] += in[6] >> 16;
419 output[3] += (in[6] & 0xffff) << 40;
420 output[2] -= in[6];
421
422 output[3] += in[5] >> 16;
423 output[2] += (in[5] & 0xffff) << 40;
424 output[1] -= in[5];
425
426 output[2] += output[4] >> 16;
427 output[1] += (output[4] & 0xffff) << 40;
428 output[0] -= output[4];
429
430 /* Carry 2 -> 3 -> 4 */
431 output[3] += output[2] >> 56;
432 output[2] &= 0x00ffffffffffffff;
433
434 output[4] = output[3] >> 56;
435 output[3] &= 0x00ffffffffffffff;
436
437 /* Now output[2] < 2^56, output[3] < 2^56, output[4] < 2^72 */
438
439 /* Eliminate output[4] */
440 output[2] += output[4] >> 16;
441 /* output[2] < 2^56 + 2^56 = 2^57 */
442 output[1] += (output[4] & 0xffff) << 40;
443 output[0] -= output[4];
444
445 /* Carry 0 -> 1 -> 2 -> 3 */
446 output[1] += output[0] >> 56;
447 out[0] = output[0] & 0x00ffffffffffffff;
448
449 output[2] += output[1] >> 56;
450 /* output[2] < 2^57 + 2^72 */
451 out[1] = output[1] & 0x00ffffffffffffff;
452 output[3] += output[2] >> 56;
453 /* output[3] <= 2^56 + 2^16 */
454 out[2] = output[2] & 0x00ffffffffffffff;
455
456 /* out[0] < 2^56, out[1] < 2^56, out[2] < 2^56,
457 * out[3] <= 2^56 + 2^16 (due to final carry),
458 * so out < 2*p */
459 out[3] = output[3];
460}
461
Kenny Roote99801b2015-11-06 15:31:15 -0800462/* Reduce to unique minimal representation.
Robert Sloan8ff03552017-06-14 12:40:58 -0700463 * Requires 0 <= in < 2*p (always call p224_felem_reduce first) */
464static void p224_felem_contract(p224_felem out, const p224_felem in) {
465 static const int64_t two56 = ((p224_limb)1) << 56;
Kenny Roote99801b2015-11-06 15:31:15 -0800466 /* 0 <= in < 2*p, p = 2^224 - 2^96 + 1 */
467 /* if in > p , reduce in = in - 2^224 + 2^96 - 1 */
468 int64_t tmp[4], a;
469 tmp[0] = in[0];
470 tmp[1] = in[1];
471 tmp[2] = in[2];
472 tmp[3] = in[3];
473 /* Case 1: a = 1 iff in >= 2^224 */
474 a = (in[3] >> 56);
475 tmp[0] -= a;
476 tmp[1] += a << 40;
477 tmp[3] &= 0x00ffffffffffffff;
478 /* Case 2: a = 0 iff p <= in < 2^224, i.e., the high 128 bits are all 1 and
479 * the lower part is non-zero */
480 a = ((in[3] & in[2] & (in[1] | 0x000000ffffffffff)) + 1) |
481 (((int64_t)(in[0] + (in[1] & 0x000000ffffffffff)) - 1) >> 63);
482 a &= 0x00ffffffffffffff;
483 /* turn a into an all-one mask (if a = 0) or an all-zero mask */
484 a = (a - 1) >> 63;
485 /* subtract 2^224 - 2^96 + 1 if a is all-one */
486 tmp[3] &= a ^ 0xffffffffffffffff;
487 tmp[2] &= a ^ 0xffffffffffffffff;
488 tmp[1] &= (a ^ 0xffffffffffffffff) | 0x000000ffffffffff;
489 tmp[0] -= 1 & a;
490
491 /* eliminate negative coefficients: if tmp[0] is negative, tmp[1] must
492 * be non-zero, so we only need one step */
493 a = tmp[0] >> 63;
494 tmp[0] += two56 & a;
495 tmp[1] -= 1 & a;
496
497 /* carry 1 -> 2 -> 3 */
498 tmp[2] += tmp[1] >> 56;
499 tmp[1] &= 0x00ffffffffffffff;
500
501 tmp[3] += tmp[2] >> 56;
502 tmp[2] &= 0x00ffffffffffffff;
503
504 /* Now 0 <= out < p */
505 out[0] = tmp[0];
506 out[1] = tmp[1];
507 out[2] = tmp[2];
508 out[3] = tmp[3];
509}
510
511/* Zero-check: returns 1 if input is 0, and 0 otherwise. We know that field
512 * elements are reduced to in < 2^225, so we only need to check three cases: 0,
513 * 2^224 - 2^96 + 1, and 2^225 - 2^97 + 2 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700514static p224_limb p224_felem_is_zero(const p224_felem in) {
515 p224_limb zero = in[0] | in[1] | in[2] | in[3];
Kenny Roote99801b2015-11-06 15:31:15 -0800516 zero = (((int64_t)(zero)-1) >> 63) & 1;
517
Robert Sloan8ff03552017-06-14 12:40:58 -0700518 p224_limb two224m96p1 = (in[0] ^ 1) | (in[1] ^ 0x00ffff0000000000) |
Kenny Roote99801b2015-11-06 15:31:15 -0800519 (in[2] ^ 0x00ffffffffffffff) |
520 (in[3] ^ 0x00ffffffffffffff);
521 two224m96p1 = (((int64_t)(two224m96p1)-1) >> 63) & 1;
Robert Sloan8ff03552017-06-14 12:40:58 -0700522 p224_limb two225m97p2 = (in[0] ^ 2) | (in[1] ^ 0x00fffe0000000000) |
Kenny Roote99801b2015-11-06 15:31:15 -0800523 (in[2] ^ 0x00ffffffffffffff) |
524 (in[3] ^ 0x01ffffffffffffff);
525 two225m97p2 = (((int64_t)(two225m97p2)-1) >> 63) & 1;
526 return (zero | two224m96p1 | two225m97p2);
527}
528
Kenny Roote99801b2015-11-06 15:31:15 -0800529/* Invert a field element */
530/* Computation chain copied from djb's code */
Robert Sloan8ff03552017-06-14 12:40:58 -0700531static void p224_felem_inv(p224_felem out, const p224_felem in) {
532 p224_felem ftmp, ftmp2, ftmp3, ftmp4;
533 p224_widefelem tmp;
Kenny Roote99801b2015-11-06 15:31:15 -0800534
Robert Sloan8ff03552017-06-14 12:40:58 -0700535 p224_felem_square(tmp, in);
536 p224_felem_reduce(ftmp, tmp); /* 2 */
537 p224_felem_mul(tmp, in, ftmp);
538 p224_felem_reduce(ftmp, tmp); /* 2^2 - 1 */
539 p224_felem_square(tmp, ftmp);
540 p224_felem_reduce(ftmp, tmp); /* 2^3 - 2 */
541 p224_felem_mul(tmp, in, ftmp);
542 p224_felem_reduce(ftmp, tmp); /* 2^3 - 1 */
543 p224_felem_square(tmp, ftmp);
544 p224_felem_reduce(ftmp2, tmp); /* 2^4 - 2 */
545 p224_felem_square(tmp, ftmp2);
546 p224_felem_reduce(ftmp2, tmp); /* 2^5 - 4 */
547 p224_felem_square(tmp, ftmp2);
548 p224_felem_reduce(ftmp2, tmp); /* 2^6 - 8 */
549 p224_felem_mul(tmp, ftmp2, ftmp);
550 p224_felem_reduce(ftmp, tmp); /* 2^6 - 1 */
551 p224_felem_square(tmp, ftmp);
552 p224_felem_reduce(ftmp2, tmp); /* 2^7 - 2 */
David Benjamin7c0d06c2016-08-11 13:26:41 -0400553 for (size_t i = 0; i < 5; ++i) { /* 2^12 - 2^6 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700554 p224_felem_square(tmp, ftmp2);
555 p224_felem_reduce(ftmp2, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800556 }
Robert Sloan8ff03552017-06-14 12:40:58 -0700557 p224_felem_mul(tmp, ftmp2, ftmp);
558 p224_felem_reduce(ftmp2, tmp); /* 2^12 - 1 */
559 p224_felem_square(tmp, ftmp2);
560 p224_felem_reduce(ftmp3, tmp); /* 2^13 - 2 */
David Benjamin7c0d06c2016-08-11 13:26:41 -0400561 for (size_t i = 0; i < 11; ++i) {/* 2^24 - 2^12 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700562 p224_felem_square(tmp, ftmp3);
563 p224_felem_reduce(ftmp3, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800564 }
Robert Sloan8ff03552017-06-14 12:40:58 -0700565 p224_felem_mul(tmp, ftmp3, ftmp2);
566 p224_felem_reduce(ftmp2, tmp); /* 2^24 - 1 */
567 p224_felem_square(tmp, ftmp2);
568 p224_felem_reduce(ftmp3, tmp); /* 2^25 - 2 */
David Benjamin7c0d06c2016-08-11 13:26:41 -0400569 for (size_t i = 0; i < 23; ++i) {/* 2^48 - 2^24 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700570 p224_felem_square(tmp, ftmp3);
571 p224_felem_reduce(ftmp3, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800572 }
Robert Sloan8ff03552017-06-14 12:40:58 -0700573 p224_felem_mul(tmp, ftmp3, ftmp2);
574 p224_felem_reduce(ftmp3, tmp); /* 2^48 - 1 */
575 p224_felem_square(tmp, ftmp3);
576 p224_felem_reduce(ftmp4, tmp); /* 2^49 - 2 */
David Benjamin7c0d06c2016-08-11 13:26:41 -0400577 for (size_t i = 0; i < 47; ++i) {/* 2^96 - 2^48 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700578 p224_felem_square(tmp, ftmp4);
579 p224_felem_reduce(ftmp4, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800580 }
Robert Sloan8ff03552017-06-14 12:40:58 -0700581 p224_felem_mul(tmp, ftmp3, ftmp4);
582 p224_felem_reduce(ftmp3, tmp); /* 2^96 - 1 */
583 p224_felem_square(tmp, ftmp3);
584 p224_felem_reduce(ftmp4, tmp); /* 2^97 - 2 */
David Benjamin7c0d06c2016-08-11 13:26:41 -0400585 for (size_t i = 0; i < 23; ++i) {/* 2^120 - 2^24 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700586 p224_felem_square(tmp, ftmp4);
587 p224_felem_reduce(ftmp4, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800588 }
Robert Sloan8ff03552017-06-14 12:40:58 -0700589 p224_felem_mul(tmp, ftmp2, ftmp4);
590 p224_felem_reduce(ftmp2, tmp); /* 2^120 - 1 */
David Benjamin7c0d06c2016-08-11 13:26:41 -0400591 for (size_t i = 0; i < 6; ++i) { /* 2^126 - 2^6 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700592 p224_felem_square(tmp, ftmp2);
593 p224_felem_reduce(ftmp2, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800594 }
Robert Sloan8ff03552017-06-14 12:40:58 -0700595 p224_felem_mul(tmp, ftmp2, ftmp);
596 p224_felem_reduce(ftmp, tmp); /* 2^126 - 1 */
597 p224_felem_square(tmp, ftmp);
598 p224_felem_reduce(ftmp, tmp); /* 2^127 - 2 */
599 p224_felem_mul(tmp, ftmp, in);
600 p224_felem_reduce(ftmp, tmp); /* 2^127 - 1 */
David Benjamin7c0d06c2016-08-11 13:26:41 -0400601 for (size_t i = 0; i < 97; ++i) {/* 2^224 - 2^97 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700602 p224_felem_square(tmp, ftmp);
603 p224_felem_reduce(ftmp, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800604 }
Robert Sloan8ff03552017-06-14 12:40:58 -0700605 p224_felem_mul(tmp, ftmp, ftmp3);
606 p224_felem_reduce(out, tmp); /* 2^224 - 2^96 - 1 */
Kenny Roote99801b2015-11-06 15:31:15 -0800607}
608
609/* Copy in constant time:
610 * if icopy == 1, copy in to out,
611 * if icopy == 0, copy out to itself. */
Robert Sloan8ff03552017-06-14 12:40:58 -0700612static void p224_copy_conditional(p224_felem out, const p224_felem in,
613 p224_limb icopy) {
Kenny Roote99801b2015-11-06 15:31:15 -0800614 /* icopy is a (64-bit) 0 or 1, so copy is either all-zero or all-one */
Robert Sloan8ff03552017-06-14 12:40:58 -0700615 const p224_limb copy = -icopy;
David Benjamin7c0d06c2016-08-11 13:26:41 -0400616 for (size_t i = 0; i < 4; ++i) {
Robert Sloan8ff03552017-06-14 12:40:58 -0700617 const p224_limb tmp = copy & (in[i] ^ out[i]);
Kenny Roote99801b2015-11-06 15:31:15 -0800618 out[i] ^= tmp;
619 }
620}
621
622/* ELLIPTIC CURVE POINT OPERATIONS
623 *
624 * Points are represented in Jacobian projective coordinates:
625 * (X, Y, Z) corresponds to the affine point (X/Z^2, Y/Z^3),
626 * or to the point at infinity if Z == 0. */
627
628/* Double an elliptic curve point:
629 * (X', Y', Z') = 2 * (X, Y, Z), where
630 * X' = (3 * (X - Z^2) * (X + Z^2))^2 - 8 * X * Y^2
631 * Y' = 3 * (X - Z^2) * (X + Z^2) * (4 * X * Y^2 - X') - 8 * Y^2
632 * Z' = (Y + Z)^2 - Y^2 - Z^2 = 2 * Y * Z
633 * Outputs can equal corresponding inputs, i.e., x_out == x_in is allowed,
634 * while x_out == y_in is not (maybe this works, but it's not tested). */
Robert Sloan8ff03552017-06-14 12:40:58 -0700635static void p224_point_double(p224_felem x_out, p224_felem y_out,
636 p224_felem z_out, const p224_felem x_in,
637 const p224_felem y_in, const p224_felem z_in) {
638 p224_widefelem tmp, tmp2;
639 p224_felem delta, gamma, beta, alpha, ftmp, ftmp2;
Kenny Roote99801b2015-11-06 15:31:15 -0800640
Robert Sloan8ff03552017-06-14 12:40:58 -0700641 p224_felem_assign(ftmp, x_in);
642 p224_felem_assign(ftmp2, x_in);
Kenny Roote99801b2015-11-06 15:31:15 -0800643
644 /* delta = z^2 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700645 p224_felem_square(tmp, z_in);
646 p224_felem_reduce(delta, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800647
648 /* gamma = y^2 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700649 p224_felem_square(tmp, y_in);
650 p224_felem_reduce(gamma, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800651
652 /* beta = x*gamma */
Robert Sloan8ff03552017-06-14 12:40:58 -0700653 p224_felem_mul(tmp, x_in, gamma);
654 p224_felem_reduce(beta, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800655
656 /* alpha = 3*(x-delta)*(x+delta) */
Robert Sloan8ff03552017-06-14 12:40:58 -0700657 p224_felem_diff(ftmp, delta);
Kenny Roote99801b2015-11-06 15:31:15 -0800658 /* ftmp[i] < 2^57 + 2^58 + 2 < 2^59 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700659 p224_felem_sum(ftmp2, delta);
Kenny Roote99801b2015-11-06 15:31:15 -0800660 /* ftmp2[i] < 2^57 + 2^57 = 2^58 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700661 p224_felem_scalar(ftmp2, 3);
Kenny Roote99801b2015-11-06 15:31:15 -0800662 /* ftmp2[i] < 3 * 2^58 < 2^60 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700663 p224_felem_mul(tmp, ftmp, ftmp2);
Kenny Roote99801b2015-11-06 15:31:15 -0800664 /* tmp[i] < 2^60 * 2^59 * 4 = 2^121 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700665 p224_felem_reduce(alpha, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800666
667 /* x' = alpha^2 - 8*beta */
Robert Sloan8ff03552017-06-14 12:40:58 -0700668 p224_felem_square(tmp, alpha);
Kenny Roote99801b2015-11-06 15:31:15 -0800669 /* tmp[i] < 4 * 2^57 * 2^57 = 2^116 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700670 p224_felem_assign(ftmp, beta);
671 p224_felem_scalar(ftmp, 8);
Kenny Roote99801b2015-11-06 15:31:15 -0800672 /* ftmp[i] < 8 * 2^57 = 2^60 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700673 p224_felem_diff_128_64(tmp, ftmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800674 /* tmp[i] < 2^116 + 2^64 + 8 < 2^117 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700675 p224_felem_reduce(x_out, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800676
677 /* z' = (y + z)^2 - gamma - delta */
Robert Sloan8ff03552017-06-14 12:40:58 -0700678 p224_felem_sum(delta, gamma);
Kenny Roote99801b2015-11-06 15:31:15 -0800679 /* delta[i] < 2^57 + 2^57 = 2^58 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700680 p224_felem_assign(ftmp, y_in);
681 p224_felem_sum(ftmp, z_in);
Kenny Roote99801b2015-11-06 15:31:15 -0800682 /* ftmp[i] < 2^57 + 2^57 = 2^58 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700683 p224_felem_square(tmp, ftmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800684 /* tmp[i] < 4 * 2^58 * 2^58 = 2^118 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700685 p224_felem_diff_128_64(tmp, delta);
Kenny Roote99801b2015-11-06 15:31:15 -0800686 /* tmp[i] < 2^118 + 2^64 + 8 < 2^119 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700687 p224_felem_reduce(z_out, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800688
689 /* y' = alpha*(4*beta - x') - 8*gamma^2 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700690 p224_felem_scalar(beta, 4);
Kenny Roote99801b2015-11-06 15:31:15 -0800691 /* beta[i] < 4 * 2^57 = 2^59 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700692 p224_felem_diff(beta, x_out);
Kenny Roote99801b2015-11-06 15:31:15 -0800693 /* beta[i] < 2^59 + 2^58 + 2 < 2^60 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700694 p224_felem_mul(tmp, alpha, beta);
Kenny Roote99801b2015-11-06 15:31:15 -0800695 /* tmp[i] < 4 * 2^57 * 2^60 = 2^119 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700696 p224_felem_square(tmp2, gamma);
Kenny Roote99801b2015-11-06 15:31:15 -0800697 /* tmp2[i] < 4 * 2^57 * 2^57 = 2^116 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700698 p224_widefelem_scalar(tmp2, 8);
Kenny Roote99801b2015-11-06 15:31:15 -0800699 /* tmp2[i] < 8 * 2^116 = 2^119 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700700 p224_widefelem_diff(tmp, tmp2);
Kenny Roote99801b2015-11-06 15:31:15 -0800701 /* tmp[i] < 2^119 + 2^120 < 2^121 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700702 p224_felem_reduce(y_out, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800703}
704
705/* Add two elliptic curve points:
706 * (X_1, Y_1, Z_1) + (X_2, Y_2, Z_2) = (X_3, Y_3, Z_3), where
707 * X_3 = (Z_1^3 * Y_2 - Z_2^3 * Y_1)^2 - (Z_1^2 * X_2 - Z_2^2 * X_1)^3 -
708 * 2 * Z_2^2 * X_1 * (Z_1^2 * X_2 - Z_2^2 * X_1)^2
709 * 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 *
710 * X_1)^2 - X_3) -
711 * Z_2^3 * Y_1 * (Z_1^2 * X_2 - Z_2^2 * X_1)^3
712 * Z_3 = (Z_1^2 * X_2 - Z_2^2 * X_1) * (Z_1 * Z_2)
713 *
714 * This runs faster if 'mixed' is set, which requires Z_2 = 1 or Z_2 = 0. */
715
716/* This function is not entirely constant-time: it includes a branch for
717 * checking whether the two input points are equal, (while not equal to the
718 * point at infinity). This case never happens during single point
719 * multiplication, so there is no timing leak for ECDH or ECDSA signing. */
Robert Sloan8ff03552017-06-14 12:40:58 -0700720static void p224_point_add(p224_felem x3, p224_felem y3, p224_felem z3,
721 const p224_felem x1, const p224_felem y1,
722 const p224_felem z1, const int mixed,
723 const p224_felem x2, const p224_felem y2,
724 const p224_felem z2) {
725 p224_felem ftmp, ftmp2, ftmp3, ftmp4, ftmp5, x_out, y_out, z_out;
726 p224_widefelem tmp, tmp2;
727 p224_limb z1_is_zero, z2_is_zero, x_equal, y_equal;
Kenny Roote99801b2015-11-06 15:31:15 -0800728
729 if (!mixed) {
730 /* ftmp2 = z2^2 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700731 p224_felem_square(tmp, z2);
732 p224_felem_reduce(ftmp2, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800733
734 /* ftmp4 = z2^3 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700735 p224_felem_mul(tmp, ftmp2, z2);
736 p224_felem_reduce(ftmp4, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800737
738 /* ftmp4 = z2^3*y1 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700739 p224_felem_mul(tmp2, ftmp4, y1);
740 p224_felem_reduce(ftmp4, tmp2);
Kenny Roote99801b2015-11-06 15:31:15 -0800741
742 /* ftmp2 = z2^2*x1 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700743 p224_felem_mul(tmp2, ftmp2, x1);
744 p224_felem_reduce(ftmp2, tmp2);
Kenny Roote99801b2015-11-06 15:31:15 -0800745 } else {
746 /* We'll assume z2 = 1 (special case z2 = 0 is handled later) */
747
748 /* ftmp4 = z2^3*y1 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700749 p224_felem_assign(ftmp4, y1);
Kenny Roote99801b2015-11-06 15:31:15 -0800750
751 /* ftmp2 = z2^2*x1 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700752 p224_felem_assign(ftmp2, x1);
Kenny Roote99801b2015-11-06 15:31:15 -0800753 }
754
755 /* ftmp = z1^2 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700756 p224_felem_square(tmp, z1);
757 p224_felem_reduce(ftmp, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800758
759 /* ftmp3 = z1^3 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700760 p224_felem_mul(tmp, ftmp, z1);
761 p224_felem_reduce(ftmp3, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800762
763 /* tmp = z1^3*y2 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700764 p224_felem_mul(tmp, ftmp3, y2);
Kenny Roote99801b2015-11-06 15:31:15 -0800765 /* tmp[i] < 4 * 2^57 * 2^57 = 2^116 */
766
767 /* ftmp3 = z1^3*y2 - z2^3*y1 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700768 p224_felem_diff_128_64(tmp, ftmp4);
Kenny Roote99801b2015-11-06 15:31:15 -0800769 /* tmp[i] < 2^116 + 2^64 + 8 < 2^117 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700770 p224_felem_reduce(ftmp3, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800771
772 /* tmp = z1^2*x2 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700773 p224_felem_mul(tmp, ftmp, x2);
Kenny Roote99801b2015-11-06 15:31:15 -0800774 /* tmp[i] < 4 * 2^57 * 2^57 = 2^116 */
775
776 /* ftmp = z1^2*x2 - z2^2*x1 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700777 p224_felem_diff_128_64(tmp, ftmp2);
Kenny Roote99801b2015-11-06 15:31:15 -0800778 /* tmp[i] < 2^116 + 2^64 + 8 < 2^117 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700779 p224_felem_reduce(ftmp, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800780
781 /* the formulae are incorrect if the points are equal
782 * so we check for this and do doubling if this happens */
Robert Sloan8ff03552017-06-14 12:40:58 -0700783 x_equal = p224_felem_is_zero(ftmp);
784 y_equal = p224_felem_is_zero(ftmp3);
785 z1_is_zero = p224_felem_is_zero(z1);
786 z2_is_zero = p224_felem_is_zero(z2);
Kenny Roote99801b2015-11-06 15:31:15 -0800787 /* In affine coordinates, (X_1, Y_1) == (X_2, Y_2) */
788 if (x_equal && y_equal && !z1_is_zero && !z2_is_zero) {
Robert Sloan8ff03552017-06-14 12:40:58 -0700789 p224_point_double(x3, y3, z3, x1, y1, z1);
Kenny Roote99801b2015-11-06 15:31:15 -0800790 return;
791 }
792
793 /* ftmp5 = z1*z2 */
794 if (!mixed) {
Robert Sloan8ff03552017-06-14 12:40:58 -0700795 p224_felem_mul(tmp, z1, z2);
796 p224_felem_reduce(ftmp5, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800797 } else {
798 /* special case z2 = 0 is handled later */
Robert Sloan8ff03552017-06-14 12:40:58 -0700799 p224_felem_assign(ftmp5, z1);
Kenny Roote99801b2015-11-06 15:31:15 -0800800 }
801
802 /* z_out = (z1^2*x2 - z2^2*x1)*(z1*z2) */
Robert Sloan8ff03552017-06-14 12:40:58 -0700803 p224_felem_mul(tmp, ftmp, ftmp5);
804 p224_felem_reduce(z_out, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800805
806 /* ftmp = (z1^2*x2 - z2^2*x1)^2 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700807 p224_felem_assign(ftmp5, ftmp);
808 p224_felem_square(tmp, ftmp);
809 p224_felem_reduce(ftmp, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800810
811 /* ftmp5 = (z1^2*x2 - z2^2*x1)^3 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700812 p224_felem_mul(tmp, ftmp, ftmp5);
813 p224_felem_reduce(ftmp5, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800814
815 /* ftmp2 = z2^2*x1*(z1^2*x2 - z2^2*x1)^2 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700816 p224_felem_mul(tmp, ftmp2, ftmp);
817 p224_felem_reduce(ftmp2, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800818
819 /* tmp = z2^3*y1*(z1^2*x2 - z2^2*x1)^3 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700820 p224_felem_mul(tmp, ftmp4, ftmp5);
Kenny Roote99801b2015-11-06 15:31:15 -0800821 /* tmp[i] < 4 * 2^57 * 2^57 = 2^116 */
822
823 /* tmp2 = (z1^3*y2 - z2^3*y1)^2 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700824 p224_felem_square(tmp2, ftmp3);
Kenny Roote99801b2015-11-06 15:31:15 -0800825 /* tmp2[i] < 4 * 2^57 * 2^57 < 2^116 */
826
827 /* tmp2 = (z1^3*y2 - z2^3*y1)^2 - (z1^2*x2 - z2^2*x1)^3 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700828 p224_felem_diff_128_64(tmp2, ftmp5);
Kenny Roote99801b2015-11-06 15:31:15 -0800829 /* tmp2[i] < 2^116 + 2^64 + 8 < 2^117 */
830
831 /* ftmp5 = 2*z2^2*x1*(z1^2*x2 - z2^2*x1)^2 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700832 p224_felem_assign(ftmp5, ftmp2);
833 p224_felem_scalar(ftmp5, 2);
Kenny Roote99801b2015-11-06 15:31:15 -0800834 /* ftmp5[i] < 2 * 2^57 = 2^58 */
835
836 /* x_out = (z1^3*y2 - z2^3*y1)^2 - (z1^2*x2 - z2^2*x1)^3 -
837 2*z2^2*x1*(z1^2*x2 - z2^2*x1)^2 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700838 p224_felem_diff_128_64(tmp2, ftmp5);
Kenny Roote99801b2015-11-06 15:31:15 -0800839 /* tmp2[i] < 2^117 + 2^64 + 8 < 2^118 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700840 p224_felem_reduce(x_out, tmp2);
Kenny Roote99801b2015-11-06 15:31:15 -0800841
842 /* ftmp2 = z2^2*x1*(z1^2*x2 - z2^2*x1)^2 - x_out */
Robert Sloan8ff03552017-06-14 12:40:58 -0700843 p224_felem_diff(ftmp2, x_out);
Kenny Roote99801b2015-11-06 15:31:15 -0800844 /* ftmp2[i] < 2^57 + 2^58 + 2 < 2^59 */
845
846 /* tmp2 = (z1^3*y2 - z2^3*y1)*(z2^2*x1*(z1^2*x2 - z2^2*x1)^2 - x_out) */
Robert Sloan8ff03552017-06-14 12:40:58 -0700847 p224_felem_mul(tmp2, ftmp3, ftmp2);
Kenny Roote99801b2015-11-06 15:31:15 -0800848 /* tmp2[i] < 4 * 2^57 * 2^59 = 2^118 */
849
850 /* y_out = (z1^3*y2 - z2^3*y1)*(z2^2*x1*(z1^2*x2 - z2^2*x1)^2 - x_out) -
851 z2^3*y1*(z1^2*x2 - z2^2*x1)^3 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700852 p224_widefelem_diff(tmp2, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800853 /* tmp2[i] < 2^118 + 2^120 < 2^121 */
Robert Sloan8ff03552017-06-14 12:40:58 -0700854 p224_felem_reduce(y_out, tmp2);
Kenny Roote99801b2015-11-06 15:31:15 -0800855
856 /* the result (x_out, y_out, z_out) is incorrect if one of the inputs is
857 * the point at infinity, so we need to check for this separately */
858
859 /* if point 1 is at infinity, copy point 2 to output, and vice versa */
Robert Sloan8ff03552017-06-14 12:40:58 -0700860 p224_copy_conditional(x_out, x2, z1_is_zero);
861 p224_copy_conditional(x_out, x1, z2_is_zero);
862 p224_copy_conditional(y_out, y2, z1_is_zero);
863 p224_copy_conditional(y_out, y1, z2_is_zero);
864 p224_copy_conditional(z_out, z2, z1_is_zero);
865 p224_copy_conditional(z_out, z1, z2_is_zero);
866 p224_felem_assign(x3, x_out);
867 p224_felem_assign(y3, y_out);
868 p224_felem_assign(z3, z_out);
Kenny Roote99801b2015-11-06 15:31:15 -0800869}
870
Robert Sloan8ff03552017-06-14 12:40:58 -0700871/* p224_select_point selects the |idx|th point from a precomputation table and
Kenny Roote99801b2015-11-06 15:31:15 -0800872 * copies it to out. */
Robert Sloan8ff03552017-06-14 12:40:58 -0700873static void p224_select_point(const uint64_t idx, size_t size,
874 const p224_felem pre_comp[/*size*/][3],
875 p224_felem out[3]) {
876 p224_limb *outlimbs = &out[0][0];
877 OPENSSL_memset(outlimbs, 0, 3 * sizeof(p224_felem));
Kenny Roote99801b2015-11-06 15:31:15 -0800878
David Benjamin7c0d06c2016-08-11 13:26:41 -0400879 for (size_t i = 0; i < size; i++) {
Robert Sloan8ff03552017-06-14 12:40:58 -0700880 const p224_limb *inlimbs = &pre_comp[i][0][0];
881 uint64_t mask = i ^ idx;
Kenny Roote99801b2015-11-06 15:31:15 -0800882 mask |= mask >> 4;
883 mask |= mask >> 2;
884 mask |= mask >> 1;
885 mask &= 1;
886 mask--;
David Benjamin7c0d06c2016-08-11 13:26:41 -0400887 for (size_t j = 0; j < 4 * 3; j++) {
Kenny Roote99801b2015-11-06 15:31:15 -0800888 outlimbs[j] |= inlimbs[j] & mask;
889 }
890 }
891}
892
Robert Sloan8ff03552017-06-14 12:40:58 -0700893/* p224_get_bit returns the |i|th bit in |in| */
894static char p224_get_bit(const p224_felem_bytearray in, size_t i) {
Kenny Roote99801b2015-11-06 15:31:15 -0800895 if (i >= 224) {
896 return 0;
897 }
898 return (in[i >> 3] >> (i & 7)) & 1;
899}
900
901/* Interleaved point multiplication using precomputed point multiples:
Robert Sloan69939df2017-01-09 10:53:07 -0800902 * The small point multiples 0*P, 1*P, ..., 16*P are in p_pre_comp, the scalars
903 * in p_scalar, if non-NULL. If g_scalar is non-NULL, we also add this multiple
Robert Sloan8ff03552017-06-14 12:40:58 -0700904 * of the generator, using certain (large) precomputed multiples in
905 * g_p224_pre_comp. Output point (X, Y, Z) is stored in x_out, y_out, z_out */
906static void p224_batch_mul(p224_felem x_out, p224_felem y_out, p224_felem z_out,
907 const uint8_t *p_scalar, const uint8_t *g_scalar,
908 const p224_felem p_pre_comp[17][3]) {
909 p224_felem nq[3], tmp[4];
910 uint64_t bits;
911 uint8_t sign, digit;
Kenny Roote99801b2015-11-06 15:31:15 -0800912
913 /* set nq to the point at infinity */
Robert Sloan8ff03552017-06-14 12:40:58 -0700914 OPENSSL_memset(nq, 0, 3 * sizeof(p224_felem));
Kenny Roote99801b2015-11-06 15:31:15 -0800915
Robert Sloan69939df2017-01-09 10:53:07 -0800916 /* Loop over both scalars msb-to-lsb, interleaving additions of multiples of
917 * the generator (two in each of the last 28 rounds) and additions of p (every
918 * 5th round). */
David Benjamin4969cc92016-04-22 15:02:23 -0400919 int skip = 1; /* save two point operations in the first round */
Robert Sloan69939df2017-01-09 10:53:07 -0800920 size_t i = p_scalar != NULL ? 220 : 27;
David Benjamin4969cc92016-04-22 15:02:23 -0400921 for (;;) {
Kenny Roote99801b2015-11-06 15:31:15 -0800922 /* double */
923 if (!skip) {
Robert Sloan8ff03552017-06-14 12:40:58 -0700924 p224_point_double(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2]);
Kenny Roote99801b2015-11-06 15:31:15 -0800925 }
926
927 /* add multiples of the generator */
David Benjamin4969cc92016-04-22 15:02:23 -0400928 if (g_scalar != NULL && i <= 27) {
Kenny Roote99801b2015-11-06 15:31:15 -0800929 /* first, look 28 bits upwards */
Robert Sloan8ff03552017-06-14 12:40:58 -0700930 bits = p224_get_bit(g_scalar, i + 196) << 3;
931 bits |= p224_get_bit(g_scalar, i + 140) << 2;
932 bits |= p224_get_bit(g_scalar, i + 84) << 1;
933 bits |= p224_get_bit(g_scalar, i + 28);
Kenny Roote99801b2015-11-06 15:31:15 -0800934 /* select the point to add, in constant time */
Robert Sloan8ff03552017-06-14 12:40:58 -0700935 p224_select_point(bits, 16, g_p224_pre_comp[1], tmp);
Kenny Roote99801b2015-11-06 15:31:15 -0800936
937 if (!skip) {
Robert Sloan8ff03552017-06-14 12:40:58 -0700938 p224_point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 1 /* mixed */,
Kenny Roote99801b2015-11-06 15:31:15 -0800939 tmp[0], tmp[1], tmp[2]);
940 } else {
Robert Sloan8ff03552017-06-14 12:40:58 -0700941 OPENSSL_memcpy(nq, tmp, 3 * sizeof(p224_felem));
Kenny Roote99801b2015-11-06 15:31:15 -0800942 skip = 0;
943 }
944
945 /* second, look at the current position */
Robert Sloan8ff03552017-06-14 12:40:58 -0700946 bits = p224_get_bit(g_scalar, i + 168) << 3;
947 bits |= p224_get_bit(g_scalar, i + 112) << 2;
948 bits |= p224_get_bit(g_scalar, i + 56) << 1;
949 bits |= p224_get_bit(g_scalar, i);
Kenny Roote99801b2015-11-06 15:31:15 -0800950 /* select the point to add, in constant time */
Robert Sloan8ff03552017-06-14 12:40:58 -0700951 p224_select_point(bits, 16, g_p224_pre_comp[0], tmp);
952 p224_point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 1 /* mixed */,
953 tmp[0], tmp[1], tmp[2]);
Kenny Roote99801b2015-11-06 15:31:15 -0800954 }
955
956 /* do other additions every 5 doublings */
Robert Sloan69939df2017-01-09 10:53:07 -0800957 if (p_scalar != NULL && i % 5 == 0) {
Robert Sloan8ff03552017-06-14 12:40:58 -0700958 bits = p224_get_bit(p_scalar, i + 4) << 5;
959 bits |= p224_get_bit(p_scalar, i + 3) << 4;
960 bits |= p224_get_bit(p_scalar, i + 2) << 3;
961 bits |= p224_get_bit(p_scalar, i + 1) << 2;
962 bits |= p224_get_bit(p_scalar, i) << 1;
963 bits |= p224_get_bit(p_scalar, i - 1);
Robert Sloan69939df2017-01-09 10:53:07 -0800964 ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
Kenny Roote99801b2015-11-06 15:31:15 -0800965
Robert Sloan69939df2017-01-09 10:53:07 -0800966 /* select the point to add or subtract */
Robert Sloan8ff03552017-06-14 12:40:58 -0700967 p224_select_point(digit, 17, p_pre_comp, tmp);
968 p224_felem_neg(tmp[3], tmp[1]); /* (X, -Y, Z) is the negative point */
969 p224_copy_conditional(tmp[1], tmp[3], sign);
Kenny Roote99801b2015-11-06 15:31:15 -0800970
Robert Sloan69939df2017-01-09 10:53:07 -0800971 if (!skip) {
Robert Sloan8ff03552017-06-14 12:40:58 -0700972 p224_point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 0 /* mixed */,
Robert Sloan69939df2017-01-09 10:53:07 -0800973 tmp[0], tmp[1], tmp[2]);
974 } else {
Robert Sloan8ff03552017-06-14 12:40:58 -0700975 OPENSSL_memcpy(nq, tmp, 3 * sizeof(p224_felem));
Robert Sloan69939df2017-01-09 10:53:07 -0800976 skip = 0;
Kenny Roote99801b2015-11-06 15:31:15 -0800977 }
978 }
David Benjamin4969cc92016-04-22 15:02:23 -0400979
980 if (i == 0) {
981 break;
982 }
983 --i;
Kenny Roote99801b2015-11-06 15:31:15 -0800984 }
Robert Sloan8ff03552017-06-14 12:40:58 -0700985 p224_felem_assign(x_out, nq[0]);
986 p224_felem_assign(y_out, nq[1]);
987 p224_felem_assign(z_out, nq[2]);
Kenny Roote99801b2015-11-06 15:31:15 -0800988}
989
Kenny Roote99801b2015-11-06 15:31:15 -0800990/* Takes the Jacobian coordinates (X, Y, Z) of a point and returns
991 * (X', Y') = (X/Z^2, Y/Z^3) */
David Benjamin4969cc92016-04-22 15:02:23 -0400992static int ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
993 const EC_POINT *point,
994 BIGNUM *x, BIGNUM *y,
995 BN_CTX *ctx) {
Robert Sloan8ff03552017-06-14 12:40:58 -0700996 p224_felem z1, z2, x_in, y_in, x_out, y_out;
997 p224_widefelem tmp;
Kenny Roote99801b2015-11-06 15:31:15 -0800998
999 if (EC_POINT_is_at_infinity(group, point)) {
1000 OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
1001 return 0;
1002 }
1003
Robert Sloan8ff03552017-06-14 12:40:58 -07001004 if (!p224_BN_to_felem(x_in, &point->X) ||
1005 !p224_BN_to_felem(y_in, &point->Y) ||
1006 !p224_BN_to_felem(z1, &point->Z)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001007 return 0;
1008 }
1009
Robert Sloan8ff03552017-06-14 12:40:58 -07001010 p224_felem_inv(z2, z1);
1011 p224_felem_square(tmp, z2);
1012 p224_felem_reduce(z1, tmp);
1013 p224_felem_mul(tmp, x_in, z1);
1014 p224_felem_reduce(x_in, tmp);
1015 p224_felem_contract(x_out, x_in);
1016 if (x != NULL && !p224_felem_to_BN(x, x_out)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001017 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
1018 return 0;
1019 }
1020
Robert Sloan8ff03552017-06-14 12:40:58 -07001021 p224_felem_mul(tmp, z1, z2);
1022 p224_felem_reduce(z1, tmp);
1023 p224_felem_mul(tmp, y_in, z1);
1024 p224_felem_reduce(y_in, tmp);
1025 p224_felem_contract(y_out, y_in);
1026 if (y != NULL && !p224_felem_to_BN(y, y_out)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001027 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
1028 return 0;
1029 }
1030
1031 return 1;
1032}
1033
Robert Sloan69939df2017-01-09 10:53:07 -08001034static int ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
1035 const BIGNUM *g_scalar, const EC_POINT *p,
1036 const BIGNUM *p_scalar, BN_CTX *ctx) {
Kenny Roote99801b2015-11-06 15:31:15 -08001037 int ret = 0;
Kenny Roote99801b2015-11-06 15:31:15 -08001038 BN_CTX *new_ctx = NULL;
1039 BIGNUM *x, *y, *z, *tmp_scalar;
Robert Sloan8ff03552017-06-14 12:40:58 -07001040 p224_felem_bytearray g_secret, p_secret;
1041 p224_felem p_pre_comp[17][3];
1042 p224_felem_bytearray tmp;
1043 p224_felem x_in, y_in, z_in, x_out, y_out, z_out;
Kenny Roote99801b2015-11-06 15:31:15 -08001044
1045 if (ctx == NULL) {
1046 ctx = BN_CTX_new();
1047 new_ctx = ctx;
1048 if (ctx == NULL) {
1049 return 0;
1050 }
1051 }
1052
1053 BN_CTX_start(ctx);
1054 if ((x = BN_CTX_get(ctx)) == NULL ||
1055 (y = BN_CTX_get(ctx)) == NULL ||
1056 (z = BN_CTX_get(ctx)) == NULL ||
1057 (tmp_scalar = BN_CTX_get(ctx)) == NULL) {
1058 goto err;
1059 }
1060
Robert Sloan69939df2017-01-09 10:53:07 -08001061 if (p != NULL && p_scalar != NULL) {
1062 /* We treat NULL scalars as 0, and NULL points as points at infinity, i.e.,
1063 * they contribute nothing to the linear combination. */
1064 OPENSSL_memset(&p_secret, 0, sizeof(p_secret));
1065 OPENSSL_memset(&p_pre_comp, 0, sizeof(p_pre_comp));
1066 size_t num_bytes;
1067 /* reduce g_scalar to 0 <= g_scalar < 2^224 */
1068 if (BN_num_bits(p_scalar) > 224 || BN_is_negative(p_scalar)) {
1069 /* this is an unusual input, and we don't guarantee
1070 * constant-timeness */
1071 if (!BN_nnmod(tmp_scalar, p_scalar, &group->order, ctx)) {
1072 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
1073 goto err;
1074 }
1075 num_bytes = BN_bn2bin(tmp_scalar, tmp);
1076 } else {
1077 num_bytes = BN_bn2bin(p_scalar, tmp);
1078 }
1079
Robert Sloan8ff03552017-06-14 12:40:58 -07001080 p224_flip_endian(p_secret, tmp, num_bytes);
Robert Sloan69939df2017-01-09 10:53:07 -08001081 /* precompute multiples */
Robert Sloan8ff03552017-06-14 12:40:58 -07001082 if (!p224_BN_to_felem(x_out, &p->X) ||
1083 !p224_BN_to_felem(y_out, &p->Y) ||
1084 !p224_BN_to_felem(z_out, &p->Z)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001085 goto err;
1086 }
1087
Robert Sloan8ff03552017-06-14 12:40:58 -07001088 p224_felem_assign(p_pre_comp[1][0], x_out);
1089 p224_felem_assign(p_pre_comp[1][1], y_out);
1090 p224_felem_assign(p_pre_comp[1][2], z_out);
Robert Sloan69939df2017-01-09 10:53:07 -08001091
1092 for (size_t j = 2; j <= 16; ++j) {
1093 if (j & 1) {
Robert Sloan8ff03552017-06-14 12:40:58 -07001094 p224_point_add(p_pre_comp[j][0], p_pre_comp[j][1], p_pre_comp[j][2],
Robert Sloan69939df2017-01-09 10:53:07 -08001095 p_pre_comp[1][0], p_pre_comp[1][1], p_pre_comp[1][2],
1096 0, p_pre_comp[j - 1][0], p_pre_comp[j - 1][1],
1097 p_pre_comp[j - 1][2]);
Kenny Roote99801b2015-11-06 15:31:15 -08001098 } else {
Robert Sloan8ff03552017-06-14 12:40:58 -07001099 p224_point_double(p_pre_comp[j][0], p_pre_comp[j][1],
Robert Sloan69939df2017-01-09 10:53:07 -08001100 p_pre_comp[j][2], p_pre_comp[j / 2][0],
1101 p_pre_comp[j / 2][1], p_pre_comp[j / 2][2]);
Kenny Roote99801b2015-11-06 15:31:15 -08001102 }
1103 }
Kenny Roote99801b2015-11-06 15:31:15 -08001104 }
1105
Adam Langley4139edb2016-01-13 15:00:54 -08001106 if (g_scalar != NULL) {
Robert Sloan69939df2017-01-09 10:53:07 -08001107 OPENSSL_memset(g_secret, 0, sizeof(g_secret));
David Benjamin4969cc92016-04-22 15:02:23 -04001108 size_t num_bytes;
Adam Langley4139edb2016-01-13 15:00:54 -08001109 /* reduce g_scalar to 0 <= g_scalar < 2^224 */
1110 if (BN_num_bits(g_scalar) > 224 || BN_is_negative(g_scalar)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001111 /* this is an unusual input, and we don't guarantee constant-timeness */
Adam Langley4139edb2016-01-13 15:00:54 -08001112 if (!BN_nnmod(tmp_scalar, g_scalar, &group->order, ctx)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001113 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
1114 goto err;
1115 }
1116 num_bytes = BN_bn2bin(tmp_scalar, tmp);
1117 } else {
Adam Langley4139edb2016-01-13 15:00:54 -08001118 num_bytes = BN_bn2bin(g_scalar, tmp);
Kenny Roote99801b2015-11-06 15:31:15 -08001119 }
1120
Robert Sloan8ff03552017-06-14 12:40:58 -07001121 p224_flip_endian(g_secret, tmp, num_bytes);
Kenny Roote99801b2015-11-06 15:31:15 -08001122 }
Robert Sloan8ff03552017-06-14 12:40:58 -07001123 p224_batch_mul(
1124 x_out, y_out, z_out, (p != NULL && p_scalar != NULL) ? p_secret : NULL,
1125 g_scalar != NULL ? g_secret : NULL, (const p224_felem(*)[3])p_pre_comp);
Kenny Roote99801b2015-11-06 15:31:15 -08001126
1127 /* reduce the output to its unique minimal representation */
Robert Sloan8ff03552017-06-14 12:40:58 -07001128 p224_felem_contract(x_in, x_out);
1129 p224_felem_contract(y_in, y_out);
1130 p224_felem_contract(z_in, z_out);
1131 if (!p224_felem_to_BN(x, x_in) ||
1132 !p224_felem_to_BN(y, y_in) ||
1133 !p224_felem_to_BN(z, z_in)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001134 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
1135 goto err;
1136 }
1137 ret = ec_point_set_Jprojective_coordinates_GFp(group, r, x, y, z, ctx);
1138
1139err:
1140 BN_CTX_end(ctx);
Kenny Roote99801b2015-11-06 15:31:15 -08001141 BN_CTX_free(new_ctx);
Kenny Roote99801b2015-11-06 15:31:15 -08001142 return ret;
1143}
1144
Robert Sloan8ff03552017-06-14 12:40:58 -07001145DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_nistp224_method) {
1146 out->group_init = ec_GFp_simple_group_init;
1147 out->group_finish = ec_GFp_simple_group_finish;
1148 out->group_copy = ec_GFp_simple_group_copy;
1149 out->group_set_curve = ec_GFp_simple_group_set_curve;
1150 out->point_get_affine_coordinates =
1151 ec_GFp_nistp224_point_get_affine_coordinates;
1152 out->mul = ec_GFp_nistp224_points_mul;
1153 out->field_mul = ec_GFp_simple_field_mul;
1154 out->field_sqr = ec_GFp_simple_field_sqr;
1155 out->field_encode = NULL;
1156 out->field_decode = NULL;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001157};
Kenny Roote99801b2015-11-06 15:31:15 -08001158
1159#endif /* 64_BIT && !WINDOWS && !SMALL */