Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/ec/p256-nistz.c

raw
1 // Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
2 // Copyright (c) 2014, Intel Corporation. All Rights Reserved.
3 //
4 // Originally written by Shay Gueron (1, 2), and Vlad Krasnov (1)
5 // (1) Intel Corporation, Israel Development Center, Haifa, Israel
6 // (2) University of Haifa, Israel
7 //
8 // SPDX-License-Identifier: Apache-2.0
9 //
10 // Reference:
11 // S.Gueron and V.Krasnov, "Fast Prime Field Elliptic Curve Cryptography with
12 // 256 Bit Primes"
13
14 #include <openssl/ec.h>
15
16 #include <assert.h>
17 #include <stdint.h>
18 #include <string.h>
19
20 #include <openssl/bn.h>
21 #include <openssl/crypto.h>
22 #include <openssl/err.h>
23
24 #include "../bn/internal.h"
25 #include "../delocate.h"
26 #include "../../internal.h"
27 #include "internal.h"
28 #include "p256-nistz.h"
29 #include "ec_nistp.h"
30
31 #if defined(EC_NISTP_USE_S2N_BIGNUM)
32 #include "../../../third_party/s2n-bignum/s2n-bignum_aws-lc.h"
33 #endif
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
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
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.
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 //
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.
122 static void ecp_nistz256_mod_inverse_sqr_mont(BN_ULONG r[P256_LIMBS],
123 const BN_ULONG in[P256_LIMBS]) {
124 #if defined(EC_NISTP_USE_S2N_BIGNUM)
125 ec_nistp_felem_limb in_sqr[P256_LIMBS];
126 ecp_nistz256_sqr_mont(in_sqr, in);
127 bignum_montinv_p256(r, in_sqr);
128 #else
129 // This implements the addition chain described in
130 // https://briansmith.org/ecc-inversion-addition-chains-01#p256_field_inversion
131 BN_ULONG x2[P256_LIMBS], x3[P256_LIMBS], x6[P256_LIMBS], x12[P256_LIMBS],
132 x15[P256_LIMBS], x30[P256_LIMBS], x32[P256_LIMBS];
133 ecp_nistz256_sqr_mont(x2, in); // 2^2 - 2^1
134 ecp_nistz256_mul_mont(x2, x2, in); // 2^2 - 2^0
135
136 ecp_nistz256_sqr_mont(x3, x2); // 2^3 - 2^1
137 ecp_nistz256_mul_mont(x3, x3, in); // 2^3 - 2^0
138
139 ecp_nistz256_sqr_mont(x6, x3);
140 for (int i = 1; i < 3; i++) {
141 ecp_nistz256_sqr_mont(x6, x6);
142 } // 2^6 - 2^3
143 ecp_nistz256_mul_mont(x6, x6, x3); // 2^6 - 2^0
144
145 ecp_nistz256_sqr_mont(x12, x6);
146 for (int i = 1; i < 6; i++) {
147 ecp_nistz256_sqr_mont(x12, x12);
148 } // 2^12 - 2^6
149 ecp_nistz256_mul_mont(x12, x12, x6); // 2^12 - 2^0
150
151 ecp_nistz256_sqr_mont(x15, x12);
152 for (int i = 1; i < 3; i++) {
153 ecp_nistz256_sqr_mont(x15, x15);
154 } // 2^15 - 2^3
155 ecp_nistz256_mul_mont(x15, x15, x3); // 2^15 - 2^0
156
157 ecp_nistz256_sqr_mont(x30, x15);
158 for (int i = 1; i < 15; i++) {
159 ecp_nistz256_sqr_mont(x30, x30);
160 } // 2^30 - 2^15
161 ecp_nistz256_mul_mont(x30, x30, x15); // 2^30 - 2^0
162
163 ecp_nistz256_sqr_mont(x32, x30);
164 ecp_nistz256_sqr_mont(x32, x32); // 2^32 - 2^2
165 ecp_nistz256_mul_mont(x32, x32, x2); // 2^32 - 2^0
166
167 BN_ULONG ret[P256_LIMBS];
168 ecp_nistz256_sqr_mont(ret, x32);
169 for (int i = 1; i < 31 + 1; i++) {
170 ecp_nistz256_sqr_mont(ret, ret);
171 } // 2^64 - 2^32
172 ecp_nistz256_mul_mont(ret, ret, in); // 2^64 - 2^32 + 2^0
173
174 for (int i = 0; i < 96 + 32; i++) {
175 ecp_nistz256_sqr_mont(ret, ret);
176 } // 2^192 - 2^160 + 2^128
177 ecp_nistz256_mul_mont(ret, ret, x32); // 2^192 - 2^160 + 2^128 + 2^32 - 2^0
178
179 for (int i = 0; i < 32; i++) {
180 ecp_nistz256_sqr_mont(ret, ret);
181 } // 2^224 - 2^192 + 2^160 + 2^64 - 2^32
182 ecp_nistz256_mul_mont(ret, ret, x32); // 2^224 - 2^192 + 2^160 + 2^64 - 2^0
183
184 for (int i = 0; i < 30; i++) {
185 ecp_nistz256_sqr_mont(ret, ret);
186 } // 2^254 - 2^222 + 2^190 + 2^94 - 2^30
187 ecp_nistz256_mul_mont(ret, ret, x30); // 2^254 - 2^222 + 2^190 + 2^94 - 2^0
188
189 ecp_nistz256_sqr_mont(ret, ret);
190 ecp_nistz256_sqr_mont(r, ret); // 2^256 - 2^224 + 2^192 + 2^96 - 2^2
191 #endif
192 }
193
194 // r = p * p_scalar
195 static void ecp_nistz256_windowed_mul(const EC_GROUP *group, P256_POINT *r,
196 const EC_JACOBIAN *p,
197 const EC_SCALAR *p_scalar) {
198 assert(p != NULL);
199 assert(p_scalar != NULL);
200 assert(group->field.N.width == P256_LIMBS);
201
202 static const size_t kWindowSize = 5;
203 static const crypto_word_t kMask = (1 << (5 /* kWindowSize */ + 1)) - 1;
204
205 // A |P256_POINT| is (3 * 32) = 96 bytes, and the 64-byte alignment should
206 // add no more than 63 bytes of overhead. Thus, |table| should require
207 // ~1599 ((96 * 16) + 63) bytes of stack space.
208 stack_align_type table_buffer[64 + (sizeof(P256_POINT) * 16)];
209 P256_POINT *aligned_table = (P256_POINT *) align_pointer(table_buffer, 64);
210 uint8_t p_str[33];
211 OPENSSL_memcpy(p_str, p_scalar->words, 32);
212 p_str[32] = 0;
213
214 // table[0] is implicitly (0,0,0) (the point at infinity), therefore it is
215 // not stored. All other values are actually stored with an offset of -1 in
216 // table.
217 P256_POINT *row = aligned_table;
218 assert(group->field.N.width == P256_LIMBS);
219 OPENSSL_memcpy(row[1 - 1].X, p->X.words, P256_LIMBS * sizeof(BN_ULONG));
220 OPENSSL_memcpy(row[1 - 1].Y, p->Y.words, P256_LIMBS * sizeof(BN_ULONG));
221 OPENSSL_memcpy(row[1 - 1].Z, p->Z.words, P256_LIMBS * sizeof(BN_ULONG));
222
223 ecp_nistz256_point_double(&row[2 - 1], &row[1 - 1]);
224 ecp_nistz256_point_add(&row[3 - 1], &row[2 - 1], &row[1 - 1]);
225 ecp_nistz256_point_double(&row[4 - 1], &row[2 - 1]);
226 ecp_nistz256_point_double(&row[6 - 1], &row[3 - 1]);
227 ecp_nistz256_point_double(&row[8 - 1], &row[4 - 1]);
228 ecp_nistz256_point_double(&row[12 - 1], &row[6 - 1]);
229 ecp_nistz256_point_add(&row[5 - 1], &row[4 - 1], &row[1 - 1]);
230 ecp_nistz256_point_add(&row[7 - 1], &row[6 - 1], &row[1 - 1]);
231 ecp_nistz256_point_add(&row[9 - 1], &row[8 - 1], &row[1 - 1]);
232 ecp_nistz256_point_add(&row[13 - 1], &row[12 - 1], &row[1 - 1]);
233 ecp_nistz256_point_double(&row[14 - 1], &row[7 - 1]);
234 ecp_nistz256_point_double(&row[10 - 1], &row[5 - 1]);
235 ecp_nistz256_point_add(&row[15 - 1], &row[14 - 1], &row[1 - 1]);
236 ecp_nistz256_point_add(&row[11 - 1], &row[10 - 1], &row[1 - 1]);
237 ecp_nistz256_point_double(&row[16 - 1], &row[8 - 1]);
238
239 BN_ULONG tmp[P256_LIMBS];
240 stack_align_type buffer_h[32 + sizeof(P256_POINT)];
241 P256_POINT *aligned_h = (P256_POINT *) align_pointer(buffer_h, 32);
242 size_t index = 255;
243 crypto_word_t wvalue = p_str[(index - 1) / 8];
244 wvalue = (wvalue >> ((index - 1) % 8)) & kMask;
245
246 ecp_nistz256_select_w5(r, aligned_table, booth_recode_w5(wvalue) >> 1);
247
248 while (index >= 5) {
249 if (index != 255) {
250 size_t off = (index - 1) / 8;
251
252 wvalue = (crypto_word_t)p_str[off] | (crypto_word_t)p_str[off + 1] << 8;
253 wvalue = (wvalue >> ((index - 1) % 8)) & kMask;
254
255 wvalue = booth_recode_w5(wvalue);
256
257 ecp_nistz256_select_w5(aligned_h, aligned_table, wvalue >> 1);
258
259 ecp_nistz256_neg(tmp, aligned_h->Y);
260 copy_conditional(aligned_h->Y, tmp, (wvalue & 1));
261
262 ecp_nistz256_point_add(r, r, aligned_h);
263 }
264
265 index -= kWindowSize;
266
267 ecp_nistz256_point_double(r, r);
268 ecp_nistz256_point_double(r, r);
269 ecp_nistz256_point_double(r, r);
270 ecp_nistz256_point_double(r, r);
271 ecp_nistz256_point_double(r, r);
272 }
273
274 // Final window
275 wvalue = p_str[0];
276 wvalue = (wvalue << 1) & kMask;
277
278 wvalue = booth_recode_w5(wvalue);
279
280 ecp_nistz256_select_w5(aligned_h, aligned_table, wvalue >> 1);
281
282 ecp_nistz256_neg(tmp, aligned_h->Y);
283 copy_conditional(aligned_h->Y, tmp, wvalue & 1);
284
285 ecp_nistz256_point_add(r, r, aligned_h);
286 }
287
288 static crypto_word_t calc_first_wvalue(size_t *index, const uint8_t p_str[33]) {
289 static const size_t kWindowSize = 7;
290 static const crypto_word_t kMask = (1 << (7 /* kWindowSize */ + 1)) - 1;
291 *index = kWindowSize;
292
293 crypto_word_t wvalue = (p_str[0] << 1) & kMask;
294 return booth_recode_w7(wvalue);
295 }
296
297 static crypto_word_t calc_wvalue(size_t *index, const uint8_t p_str[33]) {
298 static const size_t kWindowSize = 7;
299 static const crypto_word_t kMask = (1 << (7 /* kWindowSize */ + 1)) - 1;
300
301 const size_t off = (*index - 1) / 8;
302 crypto_word_t wvalue =
303 (crypto_word_t)p_str[off] | (crypto_word_t)p_str[off + 1] << 8;
304 wvalue = (wvalue >> ((*index - 1) % 8)) & kMask;
305 *index += kWindowSize;
306
307 return booth_recode_w7(wvalue);
308 }
309
310 static void ecp_nistz256_point_mul(const EC_GROUP *group, EC_JACOBIAN *r,
311 const EC_JACOBIAN *p,
312 const EC_SCALAR *scalar) {
313 #if defined(EC_NISTP_USE_S2N_BIGNUM)
314 ec_nistp_felem_limb in[P256_LIMBS * 3];
315 ec_nistp_felem_limb out[P256_LIMBS * 3];
316 ec_nistp_coordinates_to_point(in, p->X.words, p->Y.words, p->Z.words, P256_LIMBS);
317 p256_montjscalarmul_selector(out, scalar->words, in);
318 ec_nistp_point_to_coordinates(r->X.words, r->Y.words, r->Z.words, out, P256_LIMBS);
319 #else
320 stack_align_type buffer_out[32 + sizeof(P256_POINT)];
321 P256_POINT *aligned_out = (P256_POINT *) align_pointer(buffer_out, 32);
322 ecp_nistz256_windowed_mul(group, aligned_out, p, scalar);
323
324 assert(group->field.N.width == P256_LIMBS);
325 OPENSSL_memcpy(r->X.words, aligned_out->X, P256_LIMBS * sizeof(BN_ULONG));
326 OPENSSL_memcpy(r->Y.words, aligned_out->Y, P256_LIMBS * sizeof(BN_ULONG));
327 OPENSSL_memcpy(r->Z.words, aligned_out->Z, P256_LIMBS * sizeof(BN_ULONG));
328 #endif
329 }
330
331 static void ecp_nistz256_point_mul_base(const EC_GROUP *group, EC_JACOBIAN *r,
332 const EC_SCALAR *scalar) {
333
334 stack_align_type buffer_t[32 + sizeof(P256_POINT_AFFINE)];
335 P256_POINT_AFFINE *aligned_t = (P256_POINT_AFFINE *) align_pointer(buffer_t, 32);
336 stack_align_type buffer_p[32 + sizeof(P256_POINT)];
337 P256_POINT *aligned_p = (P256_POINT *) align_pointer(buffer_p, 32);
338
339 uint8_t p_str[33];
340 OPENSSL_memcpy(p_str, scalar->words, 32);
341 p_str[32] = 0;
342
343 // First window
344 size_t index = 0;
345 crypto_word_t wvalue = calc_first_wvalue(&index, p_str);
346
347 ecp_nistz256_select_w7(aligned_t, ecp_nistz256_precomputed[0], wvalue >> 1);
348 ecp_nistz256_neg(aligned_p->Z, aligned_t->Y);
349 copy_conditional(aligned_t->Y, aligned_p->Z, wvalue & 1);
350
351 // Convert |t| from affine to Jacobian coordinates. We set Z to zero if |t|
352 // is infinity and |ONE| otherwise. |t| was computed from the table, so it
353 // is infinity iff |wvalue >> 1| is zero.
354 OPENSSL_memcpy(aligned_p->X, aligned_t->X, sizeof(aligned_p->X));
355 OPENSSL_memcpy(aligned_p->Y, aligned_t->Y, sizeof(aligned_p->Y));
356 OPENSSL_memset(aligned_p->Z, 0, sizeof(aligned_p->Z));
357 copy_conditional(aligned_p->Z, ONE, is_not_zero(wvalue >> 1));
358
359 for (int i = 1; i < 37; i++) {
360 wvalue = calc_wvalue(&index, p_str);
361
362 ecp_nistz256_select_w7(aligned_t, ecp_nistz256_precomputed[i], wvalue >> 1);
363
364 stack_align_type buffer_neg_y[32 + (sizeof(BN_ULONG) * P256_LIMBS)];
365 BN_ULONG *aligned_neg_y = (BN_ULONG *) align_pointer(buffer_neg_y, 32);
366 ecp_nistz256_neg(aligned_neg_y, aligned_t->Y);
367 copy_conditional(aligned_t->Y, aligned_neg_y, wvalue & 1);
368
369 // Note |ecp_nistz256_point_add_affine| does not work if |p.p| and |t.a|
370 // are the same non-infinity point.
371 ecp_nistz256_point_add_affine(aligned_p, aligned_p, aligned_t);
372 }
373
374 assert(group->field.N.width == P256_LIMBS);
375 OPENSSL_memcpy(r->X.words, aligned_p->X, P256_LIMBS * sizeof(BN_ULONG));
376 OPENSSL_memcpy(r->Y.words, aligned_p->Y, P256_LIMBS * sizeof(BN_ULONG));
377 OPENSSL_memcpy(r->Z.words, aligned_p->Z, P256_LIMBS * sizeof(BN_ULONG));
378 }
379
380 static void ecp_nistz256_points_mul_public(const EC_GROUP *group,
381 EC_JACOBIAN *r,
382 const EC_SCALAR *g_scalar,
383 const EC_JACOBIAN *p_,
384 const EC_SCALAR *p_scalar) {
385 assert(p_ != NULL && p_scalar != NULL && g_scalar != NULL);
386
387 stack_align_type buffer_p[32 + sizeof(P256_POINT)];
388 P256_POINT *aligned_p = (P256_POINT *) align_pointer(buffer_p, 32);
389
390 uint8_t p_str[33];
391 OPENSSL_memcpy(p_str, g_scalar->words, 32);
392 p_str[32] = 0;
393
394 // First window
395 size_t index = 0;
396 size_t wvalue = calc_first_wvalue(&index, p_str);
397
398 // Convert |p| from affine to Jacobian coordinates. We set Z to zero if |p|
399 // is infinity and |ONE| otherwise. |p| was computed from the table, so it
400 // is infinity iff |wvalue >> 1| is zero.
401 if ((wvalue >> 1) != 0) {
402 OPENSSL_memcpy(aligned_p->X, &ecp_nistz256_precomputed[0][(wvalue >> 1) - 1].X,
403 sizeof(aligned_p->X));
404 OPENSSL_memcpy(aligned_p->Y, &ecp_nistz256_precomputed[0][(wvalue >> 1) - 1].Y,
405 sizeof(aligned_p->Y));
406 OPENSSL_memcpy(aligned_p->Z, ONE, sizeof(aligned_p->Z));
407 } else {
408 OPENSSL_memset(aligned_p->X, 0, sizeof(aligned_p->X));
409 OPENSSL_memset(aligned_p->Y, 0, sizeof(aligned_p->X));
410 OPENSSL_memset(aligned_p->Z, 0, sizeof(aligned_p->Z));
411 }
412
413 if ((wvalue & 1) == 1) {
414 ecp_nistz256_neg(aligned_p->Y, aligned_p->Y);
415 }
416
417 for (int i = 1; i < 37; i++) {
418 wvalue = calc_wvalue(&index, p_str);
419 if ((wvalue >> 1) == 0) {
420 continue;
421 }
422
423 stack_align_type buffer_t[32 + sizeof(P256_POINT_AFFINE)];
424 P256_POINT_AFFINE *aligned_t = (P256_POINT_AFFINE *) align_pointer(buffer_t, 32);
425
426 OPENSSL_memcpy(aligned_t, &ecp_nistz256_precomputed[i][(wvalue >> 1) - 1],
427 sizeof(*aligned_t));
428
429 if ((wvalue & 1) == 1) {
430 ecp_nistz256_neg(aligned_t->Y, aligned_t->Y);
431 }
432
433 // Note |ecp_nistz256_point_add_affine| does not work if |p| and |t| are
434 // the same non-infinity point, so it is important that we compute the
435 // |g_scalar| term before the |p_scalar| term.
436 ecp_nistz256_point_add_affine(aligned_p, aligned_p, aligned_t);
437 }
438
439 stack_align_type buffer_tmp[32 + sizeof(P256_POINT)];
440 P256_POINT *aligned_tmp = (P256_POINT *) align_pointer(buffer_tmp, 32);
441
442 ecp_nistz256_windowed_mul(group, aligned_tmp, p_, p_scalar);
443 ecp_nistz256_point_add(aligned_p, aligned_p, aligned_tmp);
444
445 assert(group->field.N.width == P256_LIMBS);
446 OPENSSL_memcpy(r->X.words, aligned_p->X, P256_LIMBS * sizeof(BN_ULONG));
447 OPENSSL_memcpy(r->Y.words, aligned_p->Y, P256_LIMBS * sizeof(BN_ULONG));
448 OPENSSL_memcpy(r->Z.words, aligned_p->Z, P256_LIMBS * sizeof(BN_ULONG));
449 }
450
451 static int ecp_nistz256_get_affine(const EC_GROUP *group,
452 const EC_JACOBIAN *point, EC_FELEM *x,
453 EC_FELEM *y) {
454 if (constant_time_declassify_int(
455 ec_GFp_simple_is_at_infinity(group, point))) {
456 OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
457 return 0;
458 }
459
460 BN_ULONG z_inv2[P256_LIMBS];
461 assert(group->field.N.width == P256_LIMBS);
462 ecp_nistz256_mod_inverse_sqr_mont(z_inv2, point->Z.words);
463
464 if (x != NULL) {
465 ecp_nistz256_mul_mont(x->words, z_inv2, point->X.words);
466 }
467
468 if (y != NULL) {
469 ecp_nistz256_sqr_mont(z_inv2, z_inv2); // z^-4
470 ecp_nistz256_mul_mont(y->words, point->Y.words, point->Z.words); // y * z
471 ecp_nistz256_mul_mont(y->words, y->words, z_inv2); // y * z^-3
472 }
473
474 return 1;
475 }
476
477 static void ecp_nistz256_add(const EC_GROUP *group, EC_JACOBIAN *r,
478 const EC_JACOBIAN *a_, const EC_JACOBIAN *b_) {
479 P256_POINT a, b;
480 OPENSSL_memcpy(a.X, a_->X.words, P256_LIMBS * sizeof(BN_ULONG));
481 OPENSSL_memcpy(a.Y, a_->Y.words, P256_LIMBS * sizeof(BN_ULONG));
482 OPENSSL_memcpy(a.Z, a_->Z.words, P256_LIMBS * sizeof(BN_ULONG));
483 OPENSSL_memcpy(b.X, b_->X.words, P256_LIMBS * sizeof(BN_ULONG));
484 OPENSSL_memcpy(b.Y, b_->Y.words, P256_LIMBS * sizeof(BN_ULONG));
485 OPENSSL_memcpy(b.Z, b_->Z.words, P256_LIMBS * sizeof(BN_ULONG));
486 ecp_nistz256_point_add(&a, &a, &b);
487 OPENSSL_memcpy(r->X.words, a.X, P256_LIMBS * sizeof(BN_ULONG));
488 OPENSSL_memcpy(r->Y.words, a.Y, P256_LIMBS * sizeof(BN_ULONG));
489 OPENSSL_memcpy(r->Z.words, a.Z, P256_LIMBS * sizeof(BN_ULONG));
490 }
491
492 static void ecp_nistz256_dbl(const EC_GROUP *group, EC_JACOBIAN *r,
493 const EC_JACOBIAN *a_) {
494 P256_POINT a;
495 OPENSSL_memcpy(a.X, a_->X.words, P256_LIMBS * sizeof(BN_ULONG));
496 OPENSSL_memcpy(a.Y, a_->Y.words, P256_LIMBS * sizeof(BN_ULONG));
497 OPENSSL_memcpy(a.Z, a_->Z.words, P256_LIMBS * sizeof(BN_ULONG));
498 ecp_nistz256_point_double(&a, &a);
499 OPENSSL_memcpy(r->X.words, a.X, P256_LIMBS * sizeof(BN_ULONG));
500 OPENSSL_memcpy(r->Y.words, a.Y, P256_LIMBS * sizeof(BN_ULONG));
501 OPENSSL_memcpy(r->Z.words, a.Z, P256_LIMBS * sizeof(BN_ULONG));
502 }
503
504 static void ecp_nistz256_inv0_mod_ord(const EC_GROUP *group, EC_SCALAR *out,
505 const EC_SCALAR *in) {
506 // table[i] stores a power of |in| corresponding to the matching enum value.
507 enum {
508 // The following indices specify the power in binary.
509 i_1 = 0,
510 i_10,
511 i_11,
512 i_101,
513 i_111,
514 i_1010,
515 i_1111,
516 i_10101,
517 i_101010,
518 i_101111,
519 // The following indices specify 2^N-1, or N ones in a row.
520 i_x6,
521 i_x8,
522 i_x16,
523 i_x32
524 };
525 BN_ULONG table[15][P256_LIMBS];
526
527 // https://briansmith.org/ecc-inversion-addition-chains-01#p256_scalar_inversion
528 //
529 // Even though this code path spares 12 squarings, 4.5%, and 13
530 // multiplications, 25%, the overall sign operation is not that much faster,
531 // not more that 2%. Most of the performance of this function comes from the
532 // scalar operations.
533
534 // Pre-calculate powers.
535 OPENSSL_memcpy(table[i_1], in->words, P256_LIMBS * sizeof(BN_ULONG));
536
537 ecp_nistz256_ord_sqr_mont(table[i_10], table[i_1], 1);
538
539 ecp_nistz256_ord_mul_mont(table[i_11], table[i_1], table[i_10]);
540
541 ecp_nistz256_ord_mul_mont(table[i_101], table[i_11], table[i_10]);
542
543 ecp_nistz256_ord_mul_mont(table[i_111], table[i_101], table[i_10]);
544
545 ecp_nistz256_ord_sqr_mont(table[i_1010], table[i_101], 1);
546
547 ecp_nistz256_ord_mul_mont(table[i_1111], table[i_1010], table[i_101]);
548
549 ecp_nistz256_ord_sqr_mont(table[i_10101], table[i_1010], 1);
550 ecp_nistz256_ord_mul_mont(table[i_10101], table[i_10101], table[i_1]);
551
552 ecp_nistz256_ord_sqr_mont(table[i_101010], table[i_10101], 1);
553
554 ecp_nistz256_ord_mul_mont(table[i_101111], table[i_101010], table[i_101]);
555
556 ecp_nistz256_ord_mul_mont(table[i_x6], table[i_101010], table[i_10101]);
557
558 ecp_nistz256_ord_sqr_mont(table[i_x8], table[i_x6], 2);
559 ecp_nistz256_ord_mul_mont(table[i_x8], table[i_x8], table[i_11]);
560
561 ecp_nistz256_ord_sqr_mont(table[i_x16], table[i_x8], 8);
562 ecp_nistz256_ord_mul_mont(table[i_x16], table[i_x16], table[i_x8]);
563
564 ecp_nistz256_ord_sqr_mont(table[i_x32], table[i_x16], 16);
565 ecp_nistz256_ord_mul_mont(table[i_x32], table[i_x32], table[i_x16]);
566
567 // Compute |in| raised to the order-2.
568 ecp_nistz256_ord_sqr_mont(out->words, table[i_x32], 64);
569 ecp_nistz256_ord_mul_mont(out->words, out->words, table[i_x32]);
570 static const struct {
571 uint8_t p, i;
572 } kChain[27] = {{32, i_x32}, {6, i_101111}, {5, i_111}, {4, i_11},
573 {5, i_1111}, {5, i_10101}, {4, i_101}, {3, i_101},
574 {3, i_101}, {5, i_111}, {9, i_101111}, {6, i_1111},
575 {2, i_1}, {5, i_1}, {6, i_1111}, {5, i_111},
576 {4, i_111}, {5, i_111}, {5, i_101}, {3, i_11},
577 {10, i_101111}, {2, i_11}, {5, i_11}, {5, i_11},
578 {3, i_1}, {7, i_10101}, {6, i_1111}};
579 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kChain); i++) {
580 ecp_nistz256_ord_sqr_mont(out->words, out->words, kChain[i].p);
581 ecp_nistz256_ord_mul_mont(out->words, out->words, table[kChain[i].i]);
582 }
583 }
584
585 static int ecp_nistz256_scalar_to_montgomery_inv_vartime(const EC_GROUP *group,
586 EC_SCALAR *out,
587 const EC_SCALAR *in) {
588
589 #if defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_AVX)
590 return ec_simple_scalar_to_montgomery_inv_vartime(group, out, in);
591 #else
592
593 #if defined(OPENSSL_X86_64)
594 if (!CRYPTO_is_AVX_capable()) {
595 // No AVX support; fallback to generic code.
596 return ec_simple_scalar_to_montgomery_inv_vartime(group, out, in);
597 }
598 #endif
599
600 assert(group->order.N.width == P256_LIMBS);
601 if (!beeu_mod_inverse_vartime(out->words, in->words, group->order.N.d)) {
602 return 0;
603 }
604
605 // The result should be returned in the Montgomery domain.
606 ec_scalar_to_montgomery(group, out, out);
607 return 1;
608
609 #endif
610 }
611
612 static int ecp_nistz256_cmp_x_coordinate(const EC_GROUP *group,
613 const EC_JACOBIAN *p,
614 const EC_SCALAR *r) {
615 if (ec_GFp_simple_is_at_infinity(group, p)) {
616 return 0;
617 }
618
619 assert(group->order.N.width == P256_LIMBS);
620 assert(group->field.N.width == P256_LIMBS);
621
622 // We wish to compare X/Z^2 with r. This is equivalent to comparing X with
623 // r*Z^2. Note that X and Z are represented in Montgomery form, while r is
624 // not.
625 BN_ULONG r_Z2[P256_LIMBS], Z2_mont[P256_LIMBS], X[P256_LIMBS];
626 ecp_nistz256_mul_mont(Z2_mont, p->Z.words, p->Z.words);
627 ecp_nistz256_mul_mont(r_Z2, r->words, Z2_mont);
628 ecp_nistz256_from_mont(X, p->X.words);
629
630 if (OPENSSL_memcmp(r_Z2, X, sizeof(r_Z2)) == 0) {
631 return 1;
632 }
633
634 // During signing the x coefficient is reduced modulo the group order.
635 // Therefore there is a small possibility, less than 1/2^128, that group_order
636 // < p.x < P. in that case we need not only to compare against |r| but also to
637 // compare against r+group_order.
638 BN_ULONG carry = bn_add_words(r_Z2, r->words, group->order.N.d, P256_LIMBS);
639 if (carry == 0 && bn_less_than_words(r_Z2, group->field.N.d, P256_LIMBS)) {
640 // r + group_order < p, so compare (r + group_order) * Z^2 against X.
641 ecp_nistz256_mul_mont(r_Z2, r_Z2, Z2_mont);
642 if (OPENSSL_memcmp(r_Z2, X, sizeof(r_Z2)) == 0) {
643 return 1;
644 }
645 }
646
647 return 0;
648 }
649
650 DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_nistz256_method) {
651 out->point_get_affine_coordinates = ecp_nistz256_get_affine;
652 out->add = ecp_nistz256_add;
653 out->dbl = ecp_nistz256_dbl;
654 out->mul = ecp_nistz256_point_mul;
655 out->mul_base = ecp_nistz256_point_mul_base;
656 out->mul_public = ecp_nistz256_points_mul_public;
657 out->felem_mul = ec_GFp_mont_felem_mul;
658 out->felem_sqr = ec_GFp_mont_felem_sqr;
659 out->felem_to_bytes = ec_GFp_mont_felem_to_bytes;
660 out->felem_from_bytes = ec_GFp_mont_felem_from_bytes;
661 out->felem_reduce = ec_GFp_mont_felem_reduce;
662 // TODO(davidben): This should use the specialized field arithmetic
663 // implementation, rather than the generic one.
664 out->felem_exp = ec_GFp_mont_felem_exp;
665 out->scalar_inv0_montgomery = ecp_nistz256_inv0_mod_ord;
666 out->scalar_to_montgomery_inv_vartime =
667 ecp_nistz256_scalar_to_montgomery_inv_vartime;
668 out->cmp_x_coordinate = ecp_nistz256_cmp_x_coordinate;
669 }
670
671 #endif /* !defined(OPENSSL_NO_ASM) && \
672 (defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \
673 !defined(OPENSSL_SMALL) */