1 /* Copyright (c) 2020, 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 // An implementation of the NIST P-256 elliptic curve point multiplication.
16 // 256-bit Montgomery form for 64 and 32-bit. Field operations are generated by
17 // Fiat, which lives in //third_party/fiat.
18
19 #include <ring-core/base.h>
20
21 #include "../../limbs/limbs.h"
22 #include "../../limbs/limbs.inl"
23
24 #include "p256_shared.h"
25
26 #include "../../internal.h"
27 #include "./util.h"
28
29 #if !defined(OPENSSL_USE_NISTZ256)
30
31 #if defined(_MSC_VER) && !defined(__clang__)
32 // '=': conversion from 'int64_t' to 'int32_t', possible loss of data
33 #pragma warning(disable: 4242)
34 // '=': conversion from 'int32_t' to 'uint8_t', possible loss of data
35 #pragma warning(disable: 4244)
36 // 'initializing': conversion from 'size_t' to 'fiat_p256_limb_t'
37 #pragma warning(disable: 4267)
38 #endif
39
40 #if defined(__GNUC__)
41 #pragma GCC diagnostic ignored "-Wconversion"
42 #pragma GCC diagnostic ignored "-Wsign-conversion"
43 #endif
44
45 // MSVC does not implement uint128_t, and crashes with intrinsics
46 #if defined(BORINGSSL_HAS_UINT128)
47 #if defined(__GNUC__)
48 #pragma GCC diagnostic ignored "-Wpedantic"
49 #endif
50 #define BORINGSSL_NISTP256_64BIT 1
51 #include "../../../third_party/fiat/p256_64.h"
52 #else
53 #include "../../../third_party/fiat/p256_32.h"
54 #endif
55
56
57 // utility functions, handwritten
58
59 #if defined(BORINGSSL_NISTP256_64BIT)
60 #define FIAT_P256_NLIMBS 4
61 typedef uint64_t fiat_p256_limb_t;
62 typedef uint64_t fiat_p256_felem[FIAT_P256_NLIMBS];
63 static const fiat_p256_felem fiat_p256_one = {0x1, 0xffffffff00000000,
64 0xffffffffffffffff, 0xfffffffe};
65 #else // 64BIT; else 32BIT
66 #define FIAT_P256_NLIMBS 8
67 typedef uint32_t fiat_p256_limb_t;
68 typedef uint32_t fiat_p256_felem[FIAT_P256_NLIMBS];
69 static const fiat_p256_felem fiat_p256_one = {
70 0x1, 0x0, 0x0, 0xffffffff, 0xffffffff, 0xffffffff, 0xfffffffe, 0x0};
71 #endif // 64BIT
72
73
fiat_p256_nz(const fiat_p256_limb_t in1[FIAT_P256_NLIMBS])74 static fiat_p256_limb_t fiat_p256_nz(
75 const fiat_p256_limb_t in1[FIAT_P256_NLIMBS]) {
76 fiat_p256_limb_t ret;
77 fiat_p256_nonzero(&ret, in1);
78 return ret;
79 }
80
fiat_p256_copy(fiat_p256_limb_t out[FIAT_P256_NLIMBS],const fiat_p256_limb_t in1[FIAT_P256_NLIMBS])81 static void fiat_p256_copy(fiat_p256_limb_t out[FIAT_P256_NLIMBS],
82 const fiat_p256_limb_t in1[FIAT_P256_NLIMBS]) {
83 for (size_t i = 0; i < FIAT_P256_NLIMBS; i++) {
84 out[i] = in1[i];
85 }
86 }
87
fiat_p256_cmovznz(fiat_p256_limb_t out[FIAT_P256_NLIMBS],fiat_p256_limb_t t,const fiat_p256_limb_t z[FIAT_P256_NLIMBS],const fiat_p256_limb_t nz[FIAT_P256_NLIMBS])88 static void fiat_p256_cmovznz(fiat_p256_limb_t out[FIAT_P256_NLIMBS],
89 fiat_p256_limb_t t,
90 const fiat_p256_limb_t z[FIAT_P256_NLIMBS],
91 const fiat_p256_limb_t nz[FIAT_P256_NLIMBS]) {
92 fiat_p256_selectznz(out, !!t, z, nz);
93 }
94
95 // Group operations
96 // ----------------
97 //
98 // Building on top of the field operations we have the operations on the
99 // elliptic curve group itself. Points on the curve are represented in Jacobian
100 // coordinates.
101 //
102 // Both operations were transcribed to Coq and proven to correspond to naive
103 // implementations using Affine coordinates, for all suitable fields. In the
104 // Coq proofs, issues of constant-time execution and memory layout (aliasing)
105 // conventions were not considered. Specification of affine coordinates:
106 // <https://github.com/mit-plv/fiat-crypto/blob/79f8b5f39ed609339f0233098dee1a3c4e6b3080/src/Spec/WeierstrassCurve.v#L28>
107 // As a sanity check, a proof that these points form a commutative group:
108 // <https://github.com/mit-plv/fiat-crypto/blob/79f8b5f39ed609339f0233098dee1a3c4e6b3080/src/Curves/Weierstrass/AffineProofs.v#L33>
109
110 // fiat_p256_point_double calculates 2*(x_in, y_in, z_in)
111 //
112 // The method is taken from:
113 // http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
114 //
115 // Coq transcription and correctness proof:
116 // <https://github.com/mit-plv/fiat-crypto/blob/79f8b5f39ed609339f0233098dee1a3c4e6b3080/src/Curves/Weierstrass/Jacobian.v#L93>
117 // <https://github.com/mit-plv/fiat-crypto/blob/79f8b5f39ed609339f0233098dee1a3c4e6b3080/src/Curves/Weierstrass/Jacobian.v#L201>
118 //
119 // Outputs can equal corresponding inputs, i.e., x_out == x_in is allowed.
120 // while x_out == y_in is not (maybe this works, but it's not tested).
fiat_p256_point_double(fiat_p256_felem x_out,fiat_p256_felem y_out,fiat_p256_felem z_out,const fiat_p256_felem x_in,const fiat_p256_felem y_in,const fiat_p256_felem z_in)121 static void fiat_p256_point_double(fiat_p256_felem x_out, fiat_p256_felem y_out,
122 fiat_p256_felem z_out,
123 const fiat_p256_felem x_in,
124 const fiat_p256_felem y_in,
125 const fiat_p256_felem z_in) {
126 fiat_p256_felem delta, gamma, beta, ftmp, ftmp2, tmptmp, alpha, fourbeta;
127 // delta = z^2
128 fiat_p256_square(delta, z_in);
129 // gamma = y^2
130 fiat_p256_square(gamma, y_in);
131 // beta = x*gamma
132 fiat_p256_mul(beta, x_in, gamma);
133
134 // alpha = 3*(x-delta)*(x+delta)
135 fiat_p256_sub(ftmp, x_in, delta);
136 fiat_p256_add(ftmp2, x_in, delta);
137
138 fiat_p256_add(tmptmp, ftmp2, ftmp2);
139 fiat_p256_add(ftmp2, ftmp2, tmptmp);
140 fiat_p256_mul(alpha, ftmp, ftmp2);
141
142 // x' = alpha^2 - 8*beta
143 fiat_p256_square(x_out, alpha);
144 fiat_p256_add(fourbeta, beta, beta);
145 fiat_p256_add(fourbeta, fourbeta, fourbeta);
146 fiat_p256_add(tmptmp, fourbeta, fourbeta);
147 fiat_p256_sub(x_out, x_out, tmptmp);
148
149 // z' = (y + z)^2 - gamma - delta
150 fiat_p256_add(delta, gamma, delta);
151 fiat_p256_add(ftmp, y_in, z_in);
152 fiat_p256_square(z_out, ftmp);
153 fiat_p256_sub(z_out, z_out, delta);
154
155 // y' = alpha*(4*beta - x') - 8*gamma^2
156 fiat_p256_sub(y_out, fourbeta, x_out);
157 fiat_p256_add(gamma, gamma, gamma);
158 fiat_p256_square(gamma, gamma);
159 fiat_p256_mul(y_out, alpha, y_out);
160 fiat_p256_add(gamma, gamma, gamma);
161 fiat_p256_sub(y_out, y_out, gamma);
162 }
163
164 // fiat_p256_point_add calculates (x1, y1, z1) + (x2, y2, z2)
165 //
166 // The method is taken from:
167 // http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl,
168 // adapted for mixed addition (z2 = 1, or z2 = 0 for the point at infinity).
169 //
170 // Coq transcription and correctness proof:
171 // <https://github.com/mit-plv/fiat-crypto/blob/79f8b5f39ed609339f0233098dee1a3c4e6b3080/src/Curves/Weierstrass/Jacobian.v#L135>
172 // <https://github.com/mit-plv/fiat-crypto/blob/79f8b5f39ed609339f0233098dee1a3c4e6b3080/src/Curves/Weierstrass/Jacobian.v#L205>
173 //
174 // This function includes a branch for checking whether the two input points
175 // are equal, (while not equal to the point at infinity). This case never
176 // happens during single point multiplication, so there is no timing leak for
177 // ECDH or ECDSA signing.
fiat_p256_point_add(fiat_p256_felem x3,fiat_p256_felem y3,fiat_p256_felem z3,const fiat_p256_felem x1,const fiat_p256_felem y1,const fiat_p256_felem z1,const int mixed,const fiat_p256_felem x2,const fiat_p256_felem y2,const fiat_p256_felem z2)178 static void fiat_p256_point_add(fiat_p256_felem x3, fiat_p256_felem y3,
179 fiat_p256_felem z3, const fiat_p256_felem x1,
180 const fiat_p256_felem y1,
181 const fiat_p256_felem z1, const int mixed,
182 const fiat_p256_felem x2,
183 const fiat_p256_felem y2,
184 const fiat_p256_felem z2) {
185 fiat_p256_felem x_out, y_out, z_out;
186 fiat_p256_limb_t z1nz = fiat_p256_nz(z1);
187 fiat_p256_limb_t z2nz = fiat_p256_nz(z2);
188
189 // z1z1 = z1z1 = z1**2
190 fiat_p256_felem z1z1;
191 fiat_p256_square(z1z1, z1);
192
193 fiat_p256_felem u1, s1, two_z1z2;
194 if (!mixed) {
195 // z2z2 = z2**2
196 fiat_p256_felem z2z2;
197 fiat_p256_square(z2z2, z2);
198
199 // u1 = x1*z2z2
200 fiat_p256_mul(u1, x1, z2z2);
201
202 // two_z1z2 = (z1 + z2)**2 - (z1z1 + z2z2) = 2z1z2
203 fiat_p256_add(two_z1z2, z1, z2);
204 fiat_p256_square(two_z1z2, two_z1z2);
205 fiat_p256_sub(two_z1z2, two_z1z2, z1z1);
206 fiat_p256_sub(two_z1z2, two_z1z2, z2z2);
207
208 // s1 = y1 * z2**3
209 fiat_p256_mul(s1, z2, z2z2);
210 fiat_p256_mul(s1, s1, y1);
211 } else {
212 // We'll assume z2 = 1 (special case z2 = 0 is handled later).
213
214 // u1 = x1*z2z2
215 fiat_p256_copy(u1, x1);
216 // two_z1z2 = 2z1z2
217 fiat_p256_add(two_z1z2, z1, z1);
218 // s1 = y1 * z2**3
219 fiat_p256_copy(s1, y1);
220 }
221
222 // u2 = x2*z1z1
223 fiat_p256_felem u2;
224 fiat_p256_mul(u2, x2, z1z1);
225
226 // h = u2 - u1
227 fiat_p256_felem h;
228 fiat_p256_sub(h, u2, u1);
229
230 fiat_p256_limb_t xneq = fiat_p256_nz(h);
231
232 // z_out = two_z1z2 * h
233 fiat_p256_mul(z_out, h, two_z1z2);
234
235 // z1z1z1 = z1 * z1z1
236 fiat_p256_felem z1z1z1;
237 fiat_p256_mul(z1z1z1, z1, z1z1);
238
239 // s2 = y2 * z1**3
240 fiat_p256_felem s2;
241 fiat_p256_mul(s2, y2, z1z1z1);
242
243 // r = (s2 - s1)*2
244 fiat_p256_felem r;
245 fiat_p256_sub(r, s2, s1);
246 fiat_p256_add(r, r, r);
247
248 fiat_p256_limb_t yneq = fiat_p256_nz(r);
249
250 fiat_p256_limb_t is_nontrivial_double = constant_time_is_zero_w(xneq | yneq) &
251 ~constant_time_is_zero_w(z1nz) &
252 ~constant_time_is_zero_w(z2nz);
253 if (is_nontrivial_double) {
254 fiat_p256_point_double(x3, y3, z3, x1, y1, z1);
255 return;
256 }
257
258 // I = (2h)**2
259 fiat_p256_felem i;
260 fiat_p256_add(i, h, h);
261 fiat_p256_square(i, i);
262
263 // J = h * I
264 fiat_p256_felem j;
265 fiat_p256_mul(j, h, i);
266
267 // V = U1 * I
268 fiat_p256_felem v;
269 fiat_p256_mul(v, u1, i);
270
271 // x_out = r**2 - J - 2V
272 fiat_p256_square(x_out, r);
273 fiat_p256_sub(x_out, x_out, j);
274 fiat_p256_sub(x_out, x_out, v);
275 fiat_p256_sub(x_out, x_out, v);
276
277 // y_out = r(V-x_out) - 2 * s1 * J
278 fiat_p256_sub(y_out, v, x_out);
279 fiat_p256_mul(y_out, y_out, r);
280 fiat_p256_felem s1j;
281 fiat_p256_mul(s1j, s1, j);
282 fiat_p256_sub(y_out, y_out, s1j);
283 fiat_p256_sub(y_out, y_out, s1j);
284
285 fiat_p256_cmovznz(x_out, z1nz, x2, x_out);
286 fiat_p256_cmovznz(x3, z2nz, x1, x_out);
287 fiat_p256_cmovznz(y_out, z1nz, y2, y_out);
288 fiat_p256_cmovznz(y3, z2nz, y1, y_out);
289 fiat_p256_cmovznz(z_out, z1nz, z2, z_out);
290 fiat_p256_cmovznz(z3, z2nz, z1, z_out);
291 }
292
293 #include "./p256_table.h"
294
295 // fiat_p256_select_point_affine selects the |idx-1|th point from a
296 // precomputation table and copies it to out. If |idx| is zero, the output is
297 // the point at infinity.
fiat_p256_select_point_affine(const fiat_p256_limb_t idx,size_t size,const fiat_p256_felem pre_comp[][2],fiat_p256_felem out[3])298 static void fiat_p256_select_point_affine(
299 const fiat_p256_limb_t idx, size_t size,
300 const fiat_p256_felem pre_comp[/*size*/][2], fiat_p256_felem out[3]) {
301 OPENSSL_memset(out, 0, sizeof(fiat_p256_felem) * 3);
302 for (size_t i = 0; i < size; i++) {
303 fiat_p256_limb_t mismatch = i ^ (idx - 1);
304 fiat_p256_cmovznz(out[0], mismatch, pre_comp[i][0], out[0]);
305 fiat_p256_cmovznz(out[1], mismatch, pre_comp[i][1], out[1]);
306 }
307 fiat_p256_cmovznz(out[2], idx, out[2], fiat_p256_one);
308 }
309
310 // fiat_p256_select_point selects the |idx|th point from a precomputation table
311 // and copies it to out.
fiat_p256_select_point(const fiat_p256_limb_t idx,size_t size,const fiat_p256_felem pre_comp[][3],fiat_p256_felem out[3])312 static void fiat_p256_select_point(const fiat_p256_limb_t idx, size_t size,
313 const fiat_p256_felem pre_comp[/*size*/][3],
314 fiat_p256_felem out[3]) {
315 OPENSSL_memset(out, 0, sizeof(fiat_p256_felem) * 3);
316 for (size_t i = 0; i < size; i++) {
317 fiat_p256_limb_t mismatch = i ^ idx;
318 fiat_p256_cmovznz(out[0], mismatch, pre_comp[i][0], out[0]);
319 fiat_p256_cmovznz(out[1], mismatch, pre_comp[i][1], out[1]);
320 fiat_p256_cmovznz(out[2], mismatch, pre_comp[i][2], out[2]);
321 }
322 }
323
324 // fiat_p256_get_bit returns the |i|th bit in |in|
fiat_p256_get_bit(const uint8_t * in,int i)325 static crypto_word fiat_p256_get_bit(const uint8_t *in, int i) {
326 if (i < 0 || i >= 256) {
327 return 0;
328 }
329 return (in[i >> 3] >> (i & 7)) & 1;
330 }
331
p256_point_mul(P256_POINT * r,const Limb scalar[P256_LIMBS],const Limb p_x[P256_LIMBS],const Limb p_y[P256_LIMBS])332 void p256_point_mul(P256_POINT *r, const Limb scalar[P256_LIMBS],
333 const Limb p_x[P256_LIMBS], const Limb p_y[P256_LIMBS]) {
334 debug_assert_nonsecret(r != NULL);
335 debug_assert_nonsecret(scalar != NULL);
336 debug_assert_nonsecret(p_x != NULL);
337 debug_assert_nonsecret(p_y != NULL);
338
339 P256_SCALAR_BYTES scalar_bytes;
340 p256_scalar_bytes_from_limbs(scalar_bytes, scalar);
341
342 fiat_p256_felem p_pre_comp[17][3];
343 OPENSSL_memset(&p_pre_comp, 0, sizeof(p_pre_comp));
344
345 // Precompute multiples.
346 limbs_copy(&p_pre_comp[1][0][0], p_x, P256_LIMBS);
347 limbs_copy(&p_pre_comp[1][1][0], p_y, P256_LIMBS);
348 limbs_copy(&p_pre_comp[1][2][0], fiat_p256_one, P256_LIMBS);
349
350 for (size_t j = 2; j <= 16; ++j) {
351 if (j & 1) {
352 fiat_p256_point_add(p_pre_comp[j][0], p_pre_comp[j][1], p_pre_comp[j][2],
353 p_pre_comp[1][0], p_pre_comp[1][1], p_pre_comp[1][2],
354 0, p_pre_comp[j - 1][0], p_pre_comp[j - 1][1],
355 p_pre_comp[j - 1][2]);
356 } else {
357 fiat_p256_point_double(p_pre_comp[j][0], p_pre_comp[j][1],
358 p_pre_comp[j][2], p_pre_comp[j / 2][0],
359 p_pre_comp[j / 2][1], p_pre_comp[j / 2][2]);
360 }
361 }
362
363 // Set nq to the point at infinity.
364 fiat_p256_felem nq[3] = {{0}, {0}, {0}}, ftmp, tmp[3];
365
366 // Loop over |scalar| msb-to-lsb, incorporating |p_pre_comp| every 5th round.
367 int skip = 1; // Save two point operations in the first round.
368 for (size_t i = 255; i < 256; i--) {
369 // double
370 if (!skip) {
371 fiat_p256_point_double(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2]);
372 }
373
374 // do other additions every 5 doublings
375 if (i % 5 == 0) {
376 crypto_word bits = fiat_p256_get_bit(scalar_bytes, i + 4) << 5;
377 bits |= fiat_p256_get_bit(scalar_bytes, i + 3) << 4;
378 bits |= fiat_p256_get_bit(scalar_bytes, i + 2) << 3;
379 bits |= fiat_p256_get_bit(scalar_bytes, i + 1) << 2;
380 bits |= fiat_p256_get_bit(scalar_bytes, i) << 1;
381 bits |= fiat_p256_get_bit(scalar_bytes, i - 1);
382 crypto_word sign, digit;
383 recode_scalar_bits(&sign, &digit, bits);
384
385 // select the point to add or subtract, in constant time.
386 fiat_p256_select_point(digit, 17,
387 RING_CORE_POINTLESS_ARRAY_CONST_CAST((const fiat_p256_felem(*)[3]))p_pre_comp,
388 tmp);
389 fiat_p256_opp(ftmp, tmp[1]); // (X, -Y, Z) is the negative point.
390 fiat_p256_cmovznz(tmp[1], sign, tmp[1], ftmp);
391
392 if (!skip) {
393 fiat_p256_point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2],
394 0 /* mixed */, tmp[0], tmp[1], tmp[2]);
395 } else {
396 fiat_p256_copy(nq[0], tmp[0]);
397 fiat_p256_copy(nq[1], tmp[1]);
398 fiat_p256_copy(nq[2], tmp[2]);
399 skip = 0;
400 }
401 }
402 }
403
404 limbs_copy(r->X, nq[0], P256_LIMBS);
405 limbs_copy(r->Y, nq[1], P256_LIMBS);
406 limbs_copy(r->Z, nq[2], P256_LIMBS);
407 }
408
p256_point_mul_base(P256_POINT * r,const Limb scalar[P256_LIMBS])409 void p256_point_mul_base(P256_POINT *r, const Limb scalar[P256_LIMBS]) {
410 P256_SCALAR_BYTES scalar_bytes;
411 p256_scalar_bytes_from_limbs(scalar_bytes, scalar);
412
413 // Set nq to the point at infinity.
414 fiat_p256_felem nq[3] = {{0}, {0}, {0}}, tmp[3];
415
416 int skip = 1; // Save two point operations in the first round.
417 for (size_t i = 31; i < 32; i--) {
418 if (!skip) {
419 fiat_p256_point_double(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2]);
420 }
421
422 // First, look 32 bits upwards.
423 crypto_word bits = fiat_p256_get_bit(scalar_bytes, i + 224) << 3;
424 bits |= fiat_p256_get_bit(scalar_bytes, i + 160) << 2;
425 bits |= fiat_p256_get_bit(scalar_bytes, i + 96) << 1;
426 bits |= fiat_p256_get_bit(scalar_bytes, i + 32);
427 // Select the point to add, in constant time.
428 fiat_p256_select_point_affine(bits, 15, fiat_p256_g_pre_comp[1], tmp);
429
430 if (!skip) {
431 fiat_p256_point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2],
432 1 /* mixed */, tmp[0], tmp[1], tmp[2]);
433 } else {
434 fiat_p256_copy(nq[0], tmp[0]);
435 fiat_p256_copy(nq[1], tmp[1]);
436 fiat_p256_copy(nq[2], tmp[2]);
437 skip = 0;
438 }
439
440 // Second, look at the current position.
441 bits = fiat_p256_get_bit(scalar_bytes, i + 192) << 3;
442 bits |= fiat_p256_get_bit(scalar_bytes, i + 128) << 2;
443 bits |= fiat_p256_get_bit(scalar_bytes, i + 64) << 1;
444 bits |= fiat_p256_get_bit(scalar_bytes, i);
445 // Select the point to add, in constant time.
446 fiat_p256_select_point_affine(bits, 15, fiat_p256_g_pre_comp[0], tmp);
447 fiat_p256_point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 1 /* mixed */,
448 tmp[0], tmp[1], tmp[2]);
449 }
450
451 limbs_copy(r->X, nq[0], P256_LIMBS);
452 limbs_copy(r->Y, nq[1], P256_LIMBS);
453 limbs_copy(r->Z, nq[2], P256_LIMBS);
454 }
455
p256_mul_mont(Limb r[P256_LIMBS],const Limb a[P256_LIMBS],const Limb b[P256_LIMBS])456 void p256_mul_mont(Limb r[P256_LIMBS], const Limb a[P256_LIMBS],
457 const Limb b[P256_LIMBS]) {
458 fiat_p256_mul(r, a, b);
459 }
460
p256_sqr_mont(Limb r[P256_LIMBS],const Limb a[P256_LIMBS])461 void p256_sqr_mont(Limb r[P256_LIMBS], const Limb a[P256_LIMBS]) {
462 fiat_p256_square(r, a);
463 }
464
p256_point_add(P256_POINT * r,const P256_POINT * a,const P256_POINT * b)465 void p256_point_add(P256_POINT *r, const P256_POINT *a, const P256_POINT *b) {
466 fiat_p256_point_add(r->X, r->Y, r->Z,
467 a->X, a->Y, a->Z,
468 0,
469 b->X, b->Y, b->Z);
470 }
471
p256_point_double(P256_POINT * r,const P256_POINT * a)472 void p256_point_double(P256_POINT *r, const P256_POINT *a) {
473 fiat_p256_point_double(r->X, r->Y, r->Z,
474 a->X, a->Y, a->Z);
475 }
476
477 // For testing only.
p256_point_add_affine(P256_POINT * r,const P256_POINT * a,const BN_ULONG b[P256_LIMBS * 2])478 void p256_point_add_affine(P256_POINT *r, const P256_POINT *a,
479 const BN_ULONG b[P256_LIMBS * 2]) {
480 const Limb *b_x = &b[0];
481 const Limb *b_y = &b[P256_LIMBS];
482 fiat_p256_felem b_z = {0};
483 crypto_word b_is_inf = constant_time_select_w(
484 LIMBS_are_zero(b_x, P256_LIMBS), LIMBS_are_zero(b_y, P256_LIMBS), 0);
485 fiat_p256_cmovznz(b_z, constant_time_is_zero_w(b_is_inf), b_z, fiat_p256_one);
486 fiat_p256_point_add(r->X, r->Y, r->Z,
487 a->X, a->Y, a->Z,
488 1,
489 b_x, b_y, b_z);
490 }
491
492 #undef BORINGSSL_NISTP256_64BIT
493
494 #endif /* !defined(OPENSSL_USE_NISTZ256) */
495