1 /*
2 * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2014, Intel Corporation. All Rights Reserved.
4 *
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 *
10 * Originally written by Shay Gueron (1, 2), and Vlad Krasnov (1)
11 * (1) Intel Corporation, Israel Development Center, Haifa, Israel
12 * (2) University of Haifa, Israel
13 *
14 * Reference:
15 * S.Gueron and V.Krasnov, "Fast Prime Field Elliptic Curve Cryptography with
16 * 256 Bit Primes"
17 */
18
19 #include <openssl/ec.h>
20
21 #include <assert.h>
22 #include <stdint.h>
23 #include <string.h>
24
25 #include <openssl/bn.h>
26 #include <openssl/crypto.h>
27 #include <openssl/err.h>
28
29 #include "../bn/internal.h"
30 #include "../delocate.h"
31 #include "../../internal.h"
32 #include "internal.h"
33 #include "p256-nistz.h"
34
35 #if !defined(OPENSSL_NO_ASM) && \
36 (defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \
37 !defined(OPENSSL_SMALL)
38
39 typedef P256_POINT_AFFINE PRECOMP256_ROW[64];
40
41 // One converted into the Montgomery domain
42 static const BN_ULONG ONE[P256_LIMBS] = {
43 TOBN(0x00000000, 0x00000001), TOBN(0xffffffff, 0x00000000),
44 TOBN(0xffffffff, 0xffffffff), TOBN(0x00000000, 0xfffffffe),
45 };
46
47 // Precomputed tables for the default generator
48 #include "p256-nistz-table.h"
49
50 // Recode window to a signed digit, see |ec_GFp_nistp_recode_scalar_bits| in
51 // util.c for details
booth_recode_w5(crypto_word_t in)52 static crypto_word_t booth_recode_w5(crypto_word_t in) {
53 crypto_word_t s, d;
54
55 s = ~((in >> 5) - 1);
56 d = (1 << 6) - in - 1;
57 d = (d & s) | (in & ~s);
58 d = (d >> 1) + (d & 1);
59
60 return (d << 1) + (s & 1);
61 }
62
booth_recode_w7(crypto_word_t in)63 static crypto_word_t booth_recode_w7(crypto_word_t in) {
64 crypto_word_t s, d;
65
66 s = ~((in >> 7) - 1);
67 d = (1 << 8) - in - 1;
68 d = (d & s) | (in & ~s);
69 d = (d >> 1) + (d & 1);
70
71 return (d << 1) + (s & 1);
72 }
73
74 // copy_conditional copies |src| to |dst| if |move| is one and leaves it as-is
75 // if |move| is zero.
76 //
77 // WARNING: this breaks the usual convention of constant-time functions
78 // returning masks.
copy_conditional(BN_ULONG dst[P256_LIMBS],const BN_ULONG src[P256_LIMBS],BN_ULONG move)79 static void copy_conditional(BN_ULONG dst[P256_LIMBS],
80 const BN_ULONG src[P256_LIMBS], BN_ULONG move) {
81 BN_ULONG mask1 = ((BN_ULONG)0) - move;
82 BN_ULONG mask2 = ~mask1;
83
84 dst[0] = (src[0] & mask1) ^ (dst[0] & mask2);
85 dst[1] = (src[1] & mask1) ^ (dst[1] & mask2);
86 dst[2] = (src[2] & mask1) ^ (dst[2] & mask2);
87 dst[3] = (src[3] & mask1) ^ (dst[3] & mask2);
88 if (P256_LIMBS == 8) {
89 dst[4] = (src[4] & mask1) ^ (dst[4] & mask2);
90 dst[5] = (src[5] & mask1) ^ (dst[5] & mask2);
91 dst[6] = (src[6] & mask1) ^ (dst[6] & mask2);
92 dst[7] = (src[7] & mask1) ^ (dst[7] & mask2);
93 }
94 }
95
96 // is_not_zero returns one iff in != 0 and zero otherwise.
97 //
98 // WARNING: this breaks the usual convention of constant-time functions
99 // returning masks.
100 //
101 // (define-fun is_not_zero ((in (_ BitVec 64))) (_ BitVec 64)
102 // (bvlshr (bvor in (bvsub #x0000000000000000 in)) #x000000000000003f)
103 // )
104 //
105 // (declare-fun x () (_ BitVec 64))
106 //
107 // (assert (and (= x #x0000000000000000) (= (is_not_zero x) #x0000000000000001)))
108 // (check-sat)
109 //
110 // (assert (and (not (= x #x0000000000000000)) (= (is_not_zero x) #x0000000000000000)))
111 // (check-sat)
112 //
is_not_zero(BN_ULONG in)113 static BN_ULONG is_not_zero(BN_ULONG in) {
114 in |= (0 - in);
115 in >>= BN_BITS2 - 1;
116 return in;
117 }
118
119 // ecp_nistz256_mod_inverse_sqr_mont sets |r| to (|in| * 2^-256)^-2 * 2^256 mod
120 // p. That is, |r| is the modular inverse square of |in| for input and output in
121 // the Montgomery domain.
ecp_nistz256_mod_inverse_sqr_mont(BN_ULONG r[P256_LIMBS],const BN_ULONG in[P256_LIMBS])122 static void ecp_nistz256_mod_inverse_sqr_mont(BN_ULONG r[P256_LIMBS],
123 const BN_ULONG in[P256_LIMBS]) {
124 // This implements the addition chain described in
125 // https://briansmith.org/ecc-inversion-addition-chains-01#p256_field_inversion
126 BN_ULONG x2[P256_LIMBS], x3[P256_LIMBS], x6[P256_LIMBS], x12[P256_LIMBS],
127 x15[P256_LIMBS], x30[P256_LIMBS], x32[P256_LIMBS];
128 ecp_nistz256_sqr_mont(x2, in); // 2^2 - 2^1
129 ecp_nistz256_mul_mont(x2, x2, in); // 2^2 - 2^0
130
131 ecp_nistz256_sqr_mont(x3, x2); // 2^3 - 2^1
132 ecp_nistz256_mul_mont(x3, x3, in); // 2^3 - 2^0
133
134 ecp_nistz256_sqr_mont(x6, x3);
135 for (int i = 1; i < 3; i++) {
136 ecp_nistz256_sqr_mont(x6, x6);
137 } // 2^6 - 2^3
138 ecp_nistz256_mul_mont(x6, x6, x3); // 2^6 - 2^0
139
140 ecp_nistz256_sqr_mont(x12, x6);
141 for (int i = 1; i < 6; i++) {
142 ecp_nistz256_sqr_mont(x12, x12);
143 } // 2^12 - 2^6
144 ecp_nistz256_mul_mont(x12, x12, x6); // 2^12 - 2^0
145
146 ecp_nistz256_sqr_mont(x15, x12);
147 for (int i = 1; i < 3; i++) {
148 ecp_nistz256_sqr_mont(x15, x15);
149 } // 2^15 - 2^3
150 ecp_nistz256_mul_mont(x15, x15, x3); // 2^15 - 2^0
151
152 ecp_nistz256_sqr_mont(x30, x15);
153 for (int i = 1; i < 15; i++) {
154 ecp_nistz256_sqr_mont(x30, x30);
155 } // 2^30 - 2^15
156 ecp_nistz256_mul_mont(x30, x30, x15); // 2^30 - 2^0
157
158 ecp_nistz256_sqr_mont(x32, x30);
159 ecp_nistz256_sqr_mont(x32, x32); // 2^32 - 2^2
160 ecp_nistz256_mul_mont(x32, x32, x2); // 2^32 - 2^0
161
162 BN_ULONG ret[P256_LIMBS];
163 ecp_nistz256_sqr_mont(ret, x32);
164 for (int i = 1; i < 31 + 1; i++) {
165 ecp_nistz256_sqr_mont(ret, ret);
166 } // 2^64 - 2^32
167 ecp_nistz256_mul_mont(ret, ret, in); // 2^64 - 2^32 + 2^0
168
169 for (int i = 0; i < 96 + 32; i++) {
170 ecp_nistz256_sqr_mont(ret, ret);
171 } // 2^192 - 2^160 + 2^128
172 ecp_nistz256_mul_mont(ret, ret, x32); // 2^192 - 2^160 + 2^128 + 2^32 - 2^0
173
174 for (int i = 0; i < 32; i++) {
175 ecp_nistz256_sqr_mont(ret, ret);
176 } // 2^224 - 2^192 + 2^160 + 2^64 - 2^32
177 ecp_nistz256_mul_mont(ret, ret, x32); // 2^224 - 2^192 + 2^160 + 2^64 - 2^0
178
179 for (int i = 0; i < 30; i++) {
180 ecp_nistz256_sqr_mont(ret, ret);
181 } // 2^254 - 2^222 + 2^190 + 2^94 - 2^30
182 ecp_nistz256_mul_mont(ret, ret, x30); // 2^254 - 2^222 + 2^190 + 2^94 - 2^0
183
184 ecp_nistz256_sqr_mont(ret, ret);
185 ecp_nistz256_sqr_mont(r, ret); // 2^256 - 2^224 + 2^192 + 2^96 - 2^2
186 }
187
188 // r = p * p_scalar
ecp_nistz256_windowed_mul(const EC_GROUP * group,P256_POINT * r,const EC_RAW_POINT * p,const EC_SCALAR * p_scalar)189 static void ecp_nistz256_windowed_mul(const EC_GROUP *group, P256_POINT *r,
190 const EC_RAW_POINT *p,
191 const EC_SCALAR *p_scalar) {
192 assert(p != NULL);
193 assert(p_scalar != NULL);
194 assert(group->field.width == P256_LIMBS);
195
196 static const size_t kWindowSize = 5;
197 static const crypto_word_t kMask = (1 << (5 /* kWindowSize */ + 1)) - 1;
198
199 // A |P256_POINT| is (3 * 32) = 96 bytes, and the 64-byte alignment should
200 // add no more than 63 bytes of overhead. Thus, |table| should require
201 // ~1599 ((96 * 16) + 63) bytes of stack space.
202 alignas(64) P256_POINT table[16];
203 uint8_t p_str[33];
204 OPENSSL_memcpy(p_str, p_scalar->words, 32);
205 p_str[32] = 0;
206
207 // table[0] is implicitly (0,0,0) (the point at infinity), therefore it is
208 // not stored. All other values are actually stored with an offset of -1 in
209 // table.
210 P256_POINT *row = table;
211 assert(group->field.width == P256_LIMBS);
212 OPENSSL_memcpy(row[1 - 1].X, p->X.words, P256_LIMBS * sizeof(BN_ULONG));
213 OPENSSL_memcpy(row[1 - 1].Y, p->Y.words, P256_LIMBS * sizeof(BN_ULONG));
214 OPENSSL_memcpy(row[1 - 1].Z, p->Z.words, P256_LIMBS * sizeof(BN_ULONG));
215
216 ecp_nistz256_point_double(&row[2 - 1], &row[1 - 1]);
217 ecp_nistz256_point_add(&row[3 - 1], &row[2 - 1], &row[1 - 1]);
218 ecp_nistz256_point_double(&row[4 - 1], &row[2 - 1]);
219 ecp_nistz256_point_double(&row[6 - 1], &row[3 - 1]);
220 ecp_nistz256_point_double(&row[8 - 1], &row[4 - 1]);
221 ecp_nistz256_point_double(&row[12 - 1], &row[6 - 1]);
222 ecp_nistz256_point_add(&row[5 - 1], &row[4 - 1], &row[1 - 1]);
223 ecp_nistz256_point_add(&row[7 - 1], &row[6 - 1], &row[1 - 1]);
224 ecp_nistz256_point_add(&row[9 - 1], &row[8 - 1], &row[1 - 1]);
225 ecp_nistz256_point_add(&row[13 - 1], &row[12 - 1], &row[1 - 1]);
226 ecp_nistz256_point_double(&row[14 - 1], &row[7 - 1]);
227 ecp_nistz256_point_double(&row[10 - 1], &row[5 - 1]);
228 ecp_nistz256_point_add(&row[15 - 1], &row[14 - 1], &row[1 - 1]);
229 ecp_nistz256_point_add(&row[11 - 1], &row[10 - 1], &row[1 - 1]);
230 ecp_nistz256_point_double(&row[16 - 1], &row[8 - 1]);
231
232 BN_ULONG tmp[P256_LIMBS];
233 alignas(32) P256_POINT h;
234 size_t index = 255;
235 crypto_word_t wvalue = p_str[(index - 1) / 8];
236 wvalue = (wvalue >> ((index - 1) % 8)) & kMask;
237
238 ecp_nistz256_select_w5(r, table, booth_recode_w5(wvalue) >> 1);
239
240 while (index >= 5) {
241 if (index != 255) {
242 size_t off = (index - 1) / 8;
243
244 wvalue = (crypto_word_t)p_str[off] | (crypto_word_t)p_str[off + 1] << 8;
245 wvalue = (wvalue >> ((index - 1) % 8)) & kMask;
246
247 wvalue = booth_recode_w5(wvalue);
248
249 ecp_nistz256_select_w5(&h, table, wvalue >> 1);
250
251 ecp_nistz256_neg(tmp, h.Y);
252 copy_conditional(h.Y, tmp, (wvalue & 1));
253
254 ecp_nistz256_point_add(r, r, &h);
255 }
256
257 index -= kWindowSize;
258
259 ecp_nistz256_point_double(r, r);
260 ecp_nistz256_point_double(r, r);
261 ecp_nistz256_point_double(r, r);
262 ecp_nistz256_point_double(r, r);
263 ecp_nistz256_point_double(r, r);
264 }
265
266 // Final window
267 wvalue = p_str[0];
268 wvalue = (wvalue << 1) & kMask;
269
270 wvalue = booth_recode_w5(wvalue);
271
272 ecp_nistz256_select_w5(&h, table, wvalue >> 1);
273
274 ecp_nistz256_neg(tmp, h.Y);
275 copy_conditional(h.Y, tmp, wvalue & 1);
276
277 ecp_nistz256_point_add(r, r, &h);
278 }
279
calc_first_wvalue(size_t * index,const uint8_t p_str[33])280 static crypto_word_t calc_first_wvalue(size_t *index, const uint8_t p_str[33]) {
281 static const size_t kWindowSize = 7;
282 static const crypto_word_t kMask = (1 << (7 /* kWindowSize */ + 1)) - 1;
283 *index = kWindowSize;
284
285 crypto_word_t wvalue = (p_str[0] << 1) & kMask;
286 return booth_recode_w7(wvalue);
287 }
288
calc_wvalue(size_t * index,const uint8_t p_str[33])289 static crypto_word_t calc_wvalue(size_t *index, const uint8_t p_str[33]) {
290 static const size_t kWindowSize = 7;
291 static const crypto_word_t kMask = (1 << (7 /* kWindowSize */ + 1)) - 1;
292
293 const size_t off = (*index - 1) / 8;
294 crypto_word_t wvalue =
295 (crypto_word_t)p_str[off] | (crypto_word_t)p_str[off + 1] << 8;
296 wvalue = (wvalue >> ((*index - 1) % 8)) & kMask;
297 *index += kWindowSize;
298
299 return booth_recode_w7(wvalue);
300 }
301
ecp_nistz256_point_mul(const EC_GROUP * group,EC_RAW_POINT * r,const EC_RAW_POINT * p,const EC_SCALAR * scalar)302 static void ecp_nistz256_point_mul(const EC_GROUP *group, EC_RAW_POINT *r,
303 const EC_RAW_POINT *p,
304 const EC_SCALAR *scalar) {
305 alignas(32) P256_POINT out;
306 ecp_nistz256_windowed_mul(group, &out, p, scalar);
307
308 assert(group->field.width == P256_LIMBS);
309 OPENSSL_memcpy(r->X.words, out.X, P256_LIMBS * sizeof(BN_ULONG));
310 OPENSSL_memcpy(r->Y.words, out.Y, P256_LIMBS * sizeof(BN_ULONG));
311 OPENSSL_memcpy(r->Z.words, out.Z, P256_LIMBS * sizeof(BN_ULONG));
312 }
313
ecp_nistz256_point_mul_base(const EC_GROUP * group,EC_RAW_POINT * r,const EC_SCALAR * scalar)314 static void ecp_nistz256_point_mul_base(const EC_GROUP *group, EC_RAW_POINT *r,
315 const EC_SCALAR *scalar) {
316 uint8_t p_str[33];
317 OPENSSL_memcpy(p_str, scalar->words, 32);
318 p_str[32] = 0;
319
320 // First window
321 size_t index = 0;
322 crypto_word_t wvalue = calc_first_wvalue(&index, p_str);
323
324 alignas(32) P256_POINT_AFFINE t;
325 alignas(32) P256_POINT p;
326 ecp_nistz256_select_w7(&t, ecp_nistz256_precomputed[0], wvalue >> 1);
327 ecp_nistz256_neg(p.Z, t.Y);
328 copy_conditional(t.Y, p.Z, wvalue & 1);
329
330 // Convert |t| from affine to Jacobian coordinates. We set Z to zero if |t|
331 // is infinity and |ONE| otherwise. |t| was computed from the table, so it
332 // is infinity iff |wvalue >> 1| is zero.
333 OPENSSL_memcpy(p.X, t.X, sizeof(p.X));
334 OPENSSL_memcpy(p.Y, t.Y, sizeof(p.Y));
335 OPENSSL_memset(p.Z, 0, sizeof(p.Z));
336 copy_conditional(p.Z, ONE, is_not_zero(wvalue >> 1));
337
338 for (int i = 1; i < 37; i++) {
339 wvalue = calc_wvalue(&index, p_str);
340
341 ecp_nistz256_select_w7(&t, ecp_nistz256_precomputed[i], wvalue >> 1);
342
343 alignas(32) BN_ULONG neg_Y[P256_LIMBS];
344 ecp_nistz256_neg(neg_Y, t.Y);
345 copy_conditional(t.Y, neg_Y, wvalue & 1);
346
347 // Note |ecp_nistz256_point_add_affine| does not work if |p| and |t| are the
348 // same non-infinity point.
349 ecp_nistz256_point_add_affine(&p, &p, &t);
350 }
351
352 assert(group->field.width == P256_LIMBS);
353 OPENSSL_memcpy(r->X.words, p.X, P256_LIMBS * sizeof(BN_ULONG));
354 OPENSSL_memcpy(r->Y.words, p.Y, P256_LIMBS * sizeof(BN_ULONG));
355 OPENSSL_memcpy(r->Z.words, p.Z, P256_LIMBS * sizeof(BN_ULONG));
356 }
357
ecp_nistz256_points_mul_public(const EC_GROUP * group,EC_RAW_POINT * r,const EC_SCALAR * g_scalar,const EC_RAW_POINT * p_,const EC_SCALAR * p_scalar)358 static void ecp_nistz256_points_mul_public(const EC_GROUP *group,
359 EC_RAW_POINT *r,
360 const EC_SCALAR *g_scalar,
361 const EC_RAW_POINT *p_,
362 const EC_SCALAR *p_scalar) {
363 assert(p_ != NULL && p_scalar != NULL && g_scalar != NULL);
364
365 alignas(32) P256_POINT p;
366 uint8_t p_str[33];
367 OPENSSL_memcpy(p_str, g_scalar->words, 32);
368 p_str[32] = 0;
369
370 // First window
371 size_t index = 0;
372 size_t wvalue = calc_first_wvalue(&index, p_str);
373
374 // Convert |p| from affine to Jacobian coordinates. We set Z to zero if |p|
375 // is infinity and |ONE| otherwise. |p| was computed from the table, so it
376 // is infinity iff |wvalue >> 1| is zero.
377 if ((wvalue >> 1) != 0) {
378 OPENSSL_memcpy(p.X, &ecp_nistz256_precomputed[0][(wvalue >> 1) - 1].X,
379 sizeof(p.X));
380 OPENSSL_memcpy(p.Y, &ecp_nistz256_precomputed[0][(wvalue >> 1) - 1].Y,
381 sizeof(p.Y));
382 OPENSSL_memcpy(p.Z, ONE, sizeof(p.Z));
383 } else {
384 OPENSSL_memset(p.X, 0, sizeof(p.X));
385 OPENSSL_memset(p.Y, 0, sizeof(p.Y));
386 OPENSSL_memset(p.Z, 0, sizeof(p.Z));
387 }
388
389 if ((wvalue & 1) == 1) {
390 ecp_nistz256_neg(p.Y, p.Y);
391 }
392
393 for (int i = 1; i < 37; i++) {
394 wvalue = calc_wvalue(&index, p_str);
395 if ((wvalue >> 1) == 0) {
396 continue;
397 }
398
399 alignas(32) P256_POINT_AFFINE t;
400 OPENSSL_memcpy(&t, &ecp_nistz256_precomputed[i][(wvalue >> 1) - 1],
401 sizeof(t));
402 if ((wvalue & 1) == 1) {
403 ecp_nistz256_neg(t.Y, t.Y);
404 }
405
406 // Note |ecp_nistz256_point_add_affine| does not work if |p| and |t| are
407 // the same non-infinity point, so it is important that we compute the
408 // |g_scalar| term before the |p_scalar| term.
409 ecp_nistz256_point_add_affine(&p, &p, &t);
410 }
411
412 alignas(32) P256_POINT tmp;
413 ecp_nistz256_windowed_mul(group, &tmp, p_, p_scalar);
414 ecp_nistz256_point_add(&p, &p, &tmp);
415
416 assert(group->field.width == P256_LIMBS);
417 OPENSSL_memcpy(r->X.words, p.X, P256_LIMBS * sizeof(BN_ULONG));
418 OPENSSL_memcpy(r->Y.words, p.Y, P256_LIMBS * sizeof(BN_ULONG));
419 OPENSSL_memcpy(r->Z.words, p.Z, P256_LIMBS * sizeof(BN_ULONG));
420 }
421
ecp_nistz256_get_affine(const EC_GROUP * group,const EC_RAW_POINT * point,EC_FELEM * x,EC_FELEM * y)422 static int ecp_nistz256_get_affine(const EC_GROUP *group,
423 const EC_RAW_POINT *point, EC_FELEM *x,
424 EC_FELEM *y) {
425 if (ec_GFp_simple_is_at_infinity(group, point)) {
426 OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
427 return 0;
428 }
429
430 BN_ULONG z_inv2[P256_LIMBS];
431 assert(group->field.width == P256_LIMBS);
432 ecp_nistz256_mod_inverse_sqr_mont(z_inv2, point->Z.words);
433
434 if (x != NULL) {
435 ecp_nistz256_mul_mont(x->words, z_inv2, point->X.words);
436 }
437
438 if (y != NULL) {
439 ecp_nistz256_sqr_mont(z_inv2, z_inv2); // z^-4
440 ecp_nistz256_mul_mont(y->words, point->Y.words, point->Z.words); // y * z
441 ecp_nistz256_mul_mont(y->words, y->words, z_inv2); // y * z^-3
442 }
443
444 return 1;
445 }
446
ecp_nistz256_add(const EC_GROUP * group,EC_RAW_POINT * r,const EC_RAW_POINT * a_,const EC_RAW_POINT * b_)447 static void ecp_nistz256_add(const EC_GROUP *group, EC_RAW_POINT *r,
448 const EC_RAW_POINT *a_, const EC_RAW_POINT *b_) {
449 P256_POINT a, b;
450 OPENSSL_memcpy(a.X, a_->X.words, P256_LIMBS * sizeof(BN_ULONG));
451 OPENSSL_memcpy(a.Y, a_->Y.words, P256_LIMBS * sizeof(BN_ULONG));
452 OPENSSL_memcpy(a.Z, a_->Z.words, P256_LIMBS * sizeof(BN_ULONG));
453 OPENSSL_memcpy(b.X, b_->X.words, P256_LIMBS * sizeof(BN_ULONG));
454 OPENSSL_memcpy(b.Y, b_->Y.words, P256_LIMBS * sizeof(BN_ULONG));
455 OPENSSL_memcpy(b.Z, b_->Z.words, P256_LIMBS * sizeof(BN_ULONG));
456 ecp_nistz256_point_add(&a, &a, &b);
457 OPENSSL_memcpy(r->X.words, a.X, P256_LIMBS * sizeof(BN_ULONG));
458 OPENSSL_memcpy(r->Y.words, a.Y, P256_LIMBS * sizeof(BN_ULONG));
459 OPENSSL_memcpy(r->Z.words, a.Z, P256_LIMBS * sizeof(BN_ULONG));
460 }
461
ecp_nistz256_dbl(const EC_GROUP * group,EC_RAW_POINT * r,const EC_RAW_POINT * a_)462 static void ecp_nistz256_dbl(const EC_GROUP *group, EC_RAW_POINT *r,
463 const EC_RAW_POINT *a_) {
464 P256_POINT a;
465 OPENSSL_memcpy(a.X, a_->X.words, P256_LIMBS * sizeof(BN_ULONG));
466 OPENSSL_memcpy(a.Y, a_->Y.words, P256_LIMBS * sizeof(BN_ULONG));
467 OPENSSL_memcpy(a.Z, a_->Z.words, P256_LIMBS * sizeof(BN_ULONG));
468 ecp_nistz256_point_double(&a, &a);
469 OPENSSL_memcpy(r->X.words, a.X, P256_LIMBS * sizeof(BN_ULONG));
470 OPENSSL_memcpy(r->Y.words, a.Y, P256_LIMBS * sizeof(BN_ULONG));
471 OPENSSL_memcpy(r->Z.words, a.Z, P256_LIMBS * sizeof(BN_ULONG));
472 }
473
ecp_nistz256_inv0_mod_ord(const EC_GROUP * group,EC_SCALAR * out,const EC_SCALAR * in)474 static void ecp_nistz256_inv0_mod_ord(const EC_GROUP *group, EC_SCALAR *out,
475 const EC_SCALAR *in) {
476 // table[i] stores a power of |in| corresponding to the matching enum value.
477 enum {
478 // The following indices specify the power in binary.
479 i_1 = 0,
480 i_10,
481 i_11,
482 i_101,
483 i_111,
484 i_1010,
485 i_1111,
486 i_10101,
487 i_101010,
488 i_101111,
489 // The following indices specify 2^N-1, or N ones in a row.
490 i_x6,
491 i_x8,
492 i_x16,
493 i_x32
494 };
495 BN_ULONG table[15][P256_LIMBS];
496
497 // https://briansmith.org/ecc-inversion-addition-chains-01#p256_scalar_inversion
498 //
499 // Even though this code path spares 12 squarings, 4.5%, and 13
500 // multiplications, 25%, the overall sign operation is not that much faster,
501 // not more that 2%. Most of the performance of this function comes from the
502 // scalar operations.
503
504 // Pre-calculate powers.
505 OPENSSL_memcpy(table[i_1], in->words, P256_LIMBS * sizeof(BN_ULONG));
506
507 ecp_nistz256_ord_sqr_mont(table[i_10], table[i_1], 1);
508
509 ecp_nistz256_ord_mul_mont(table[i_11], table[i_1], table[i_10]);
510
511 ecp_nistz256_ord_mul_mont(table[i_101], table[i_11], table[i_10]);
512
513 ecp_nistz256_ord_mul_mont(table[i_111], table[i_101], table[i_10]);
514
515 ecp_nistz256_ord_sqr_mont(table[i_1010], table[i_101], 1);
516
517 ecp_nistz256_ord_mul_mont(table[i_1111], table[i_1010], table[i_101]);
518
519 ecp_nistz256_ord_sqr_mont(table[i_10101], table[i_1010], 1);
520 ecp_nistz256_ord_mul_mont(table[i_10101], table[i_10101], table[i_1]);
521
522 ecp_nistz256_ord_sqr_mont(table[i_101010], table[i_10101], 1);
523
524 ecp_nistz256_ord_mul_mont(table[i_101111], table[i_101010], table[i_101]);
525
526 ecp_nistz256_ord_mul_mont(table[i_x6], table[i_101010], table[i_10101]);
527
528 ecp_nistz256_ord_sqr_mont(table[i_x8], table[i_x6], 2);
529 ecp_nistz256_ord_mul_mont(table[i_x8], table[i_x8], table[i_11]);
530
531 ecp_nistz256_ord_sqr_mont(table[i_x16], table[i_x8], 8);
532 ecp_nistz256_ord_mul_mont(table[i_x16], table[i_x16], table[i_x8]);
533
534 ecp_nistz256_ord_sqr_mont(table[i_x32], table[i_x16], 16);
535 ecp_nistz256_ord_mul_mont(table[i_x32], table[i_x32], table[i_x16]);
536
537 // Compute |in| raised to the order-2.
538 ecp_nistz256_ord_sqr_mont(out->words, table[i_x32], 64);
539 ecp_nistz256_ord_mul_mont(out->words, out->words, table[i_x32]);
540 static const struct {
541 uint8_t p, i;
542 } kChain[27] = {{32, i_x32}, {6, i_101111}, {5, i_111}, {4, i_11},
543 {5, i_1111}, {5, i_10101}, {4, i_101}, {3, i_101},
544 {3, i_101}, {5, i_111}, {9, i_101111}, {6, i_1111},
545 {2, i_1}, {5, i_1}, {6, i_1111}, {5, i_111},
546 {4, i_111}, {5, i_111}, {5, i_101}, {3, i_11},
547 {10, i_101111}, {2, i_11}, {5, i_11}, {5, i_11},
548 {3, i_1}, {7, i_10101}, {6, i_1111}};
549 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kChain); i++) {
550 ecp_nistz256_ord_sqr_mont(out->words, out->words, kChain[i].p);
551 ecp_nistz256_ord_mul_mont(out->words, out->words, table[kChain[i].i]);
552 }
553 }
554
ecp_nistz256_scalar_to_montgomery_inv_vartime(const EC_GROUP * group,EC_SCALAR * out,const EC_SCALAR * in)555 static int ecp_nistz256_scalar_to_montgomery_inv_vartime(const EC_GROUP *group,
556 EC_SCALAR *out,
557 const EC_SCALAR *in) {
558 #if defined(OPENSSL_X86_64)
559 if (!CRYPTO_is_AVX_capable()) {
560 // No AVX support; fallback to generic code.
561 return ec_simple_scalar_to_montgomery_inv_vartime(group, out, in);
562 }
563 #endif
564
565 assert(group->order.width == P256_LIMBS);
566 if (!beeu_mod_inverse_vartime(out->words, in->words, group->order.d)) {
567 return 0;
568 }
569
570 // The result should be returned in the Montgomery domain.
571 ec_scalar_to_montgomery(group, out, out);
572 return 1;
573 }
574
ecp_nistz256_cmp_x_coordinate(const EC_GROUP * group,const EC_RAW_POINT * p,const EC_SCALAR * r)575 static int ecp_nistz256_cmp_x_coordinate(const EC_GROUP *group,
576 const EC_RAW_POINT *p,
577 const EC_SCALAR *r) {
578 if (ec_GFp_simple_is_at_infinity(group, p)) {
579 return 0;
580 }
581
582 assert(group->order.width == P256_LIMBS);
583 assert(group->field.width == P256_LIMBS);
584
585 // We wish to compare X/Z^2 with r. This is equivalent to comparing X with
586 // r*Z^2. Note that X and Z are represented in Montgomery form, while r is
587 // not.
588 BN_ULONG r_Z2[P256_LIMBS], Z2_mont[P256_LIMBS], X[P256_LIMBS];
589 ecp_nistz256_mul_mont(Z2_mont, p->Z.words, p->Z.words);
590 ecp_nistz256_mul_mont(r_Z2, r->words, Z2_mont);
591 ecp_nistz256_from_mont(X, p->X.words);
592
593 if (OPENSSL_memcmp(r_Z2, X, sizeof(r_Z2)) == 0) {
594 return 1;
595 }
596
597 // During signing the x coefficient is reduced modulo the group order.
598 // Therefore there is a small possibility, less than 1/2^128, that group_order
599 // < p.x < P. in that case we need not only to compare against |r| but also to
600 // compare against r+group_order.
601 if (bn_less_than_words(r->words, group->field_minus_order.words,
602 P256_LIMBS)) {
603 // We can ignore the carry because: r + group_order < p < 2^256.
604 bn_add_words(r_Z2, r->words, group->order.d, P256_LIMBS);
605 ecp_nistz256_mul_mont(r_Z2, r_Z2, Z2_mont);
606 if (OPENSSL_memcmp(r_Z2, X, sizeof(r_Z2)) == 0) {
607 return 1;
608 }
609 }
610
611 return 0;
612 }
613
DEFINE_METHOD_FUNCTION(EC_METHOD,EC_GFp_nistz256_method)614 DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_nistz256_method) {
615 out->group_init = ec_GFp_mont_group_init;
616 out->group_finish = ec_GFp_mont_group_finish;
617 out->group_set_curve = ec_GFp_mont_group_set_curve;
618 out->point_get_affine_coordinates = ecp_nistz256_get_affine;
619 out->add = ecp_nistz256_add;
620 out->dbl = ecp_nistz256_dbl;
621 out->mul = ecp_nistz256_point_mul;
622 out->mul_base = ecp_nistz256_point_mul_base;
623 out->mul_public = ecp_nistz256_points_mul_public;
624 out->felem_mul = ec_GFp_mont_felem_mul;
625 out->felem_sqr = ec_GFp_mont_felem_sqr;
626 out->felem_to_bytes = ec_GFp_mont_felem_to_bytes;
627 out->felem_from_bytes = ec_GFp_mont_felem_from_bytes;
628 out->felem_reduce = ec_GFp_mont_felem_reduce;
629 // TODO(davidben): This should use the specialized field arithmetic
630 // implementation, rather than the generic one.
631 out->felem_exp = ec_GFp_mont_felem_exp;
632 out->scalar_inv0_montgomery = ecp_nistz256_inv0_mod_ord;
633 out->scalar_to_montgomery_inv_vartime =
634 ecp_nistz256_scalar_to_montgomery_inv_vartime;
635 out->cmp_x_coordinate = ecp_nistz256_cmp_x_coordinate;
636 }
637
638 #endif /* !defined(OPENSSL_NO_ASM) && \
639 (defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \
640 !defined(OPENSSL_SMALL) */
641