| 1 |
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
// Implementation of P-521 that uses Fiat-crypto for the field arithmetic |
| 5 |
// found in third_party/fiat, similarly to p256.c and p384.c |
| 6 |
|
| 7 |
#include <openssl/bn.h> |
| 8 |
#include <openssl/ec.h> |
| 9 |
#include <openssl/err.h> |
| 10 |
#include <openssl/mem.h> |
| 11 |
|
| 12 |
#include "../bn/internal.h" |
| 13 |
#include "../cpucap/internal.h" |
| 14 |
#include "../delocate.h" |
| 15 |
#include "internal.h" |
| 16 |
#include "ec_nistp.h" |
| 17 |
|
| 18 |
#if !defined(OPENSSL_SMALL) |
| 19 |
|
| 20 |
#if defined(EC_NISTP_USE_S2N_BIGNUM) |
| 21 |
# include "../../../third_party/s2n-bignum/s2n-bignum_aws-lc.h" |
| 22 |
#else |
| 23 |
# if defined(EC_NISTP_USE_64BIT_LIMB) |
| 24 |
# include "../../../third_party/fiat/p521_64.h" |
| 25 |
# else |
| 26 |
# include "../../../third_party/fiat/p521_32.h" |
| 27 |
# endif |
| 28 |
#endif |
| 29 |
|
| 30 |
#if defined(EC_NISTP_USE_S2N_BIGNUM) |
| 31 |
|
| 32 |
#define P521_NLIMBS (9) |
| 33 |
|
| 34 |
typedef uint64_t p521_limb_t; |
| 35 |
typedef uint64_t p521_felem[P521_NLIMBS]; // field element |
| 36 |
|
| 37 |
static const p521_limb_t p521_felem_one[P521_NLIMBS] = { |
| 38 |
0x0000000000000001, 0x0000000000000000, |
| 39 |
0x0000000000000000, 0x0000000000000000, |
| 40 |
0x0000000000000000, 0x0000000000000000, |
| 41 |
0x0000000000000000, 0x0000000000000000, |
| 42 |
0x0000000000000000}; |
| 43 |
|
| 44 |
// The field characteristic p. |
| 45 |
static const p521_limb_t p521_felem_p[P521_NLIMBS] = { |
| 46 |
0xffffffffffffffff, 0xffffffffffffffff, |
| 47 |
0xffffffffffffffff, 0xffffffffffffffff, |
| 48 |
0xffffffffffffffff, 0xffffffffffffffff, |
| 49 |
0xffffffffffffffff, 0xffffffffffffffff, |
| 50 |
0x1ff}; |
| 51 |
|
| 52 |
// s2n-bignum implementation of field arithmetic |
| 53 |
#define p521_felem_add(out, in0, in1) bignum_add_p521(out, in0, in1) |
| 54 |
#define p521_felem_sub(out, in0, in1) bignum_sub_p521(out, in0, in1) |
| 55 |
#define p521_felem_opp(out, in0) bignum_neg_p521(out, in0) |
| 56 |
#define p521_felem_to_bytes(out, in0) bignum_tolebytes_p521(out, in0) |
| 57 |
#define p521_felem_from_bytes(out, in0) bignum_fromlebytes_p521(out, in0) |
| 58 |
#define p521_felem_mul(out, in0, in1) bignum_mul_p521_selector(out, in0, in1) |
| 59 |
#define p521_felem_sqr(out, in0) bignum_sqr_p521_selector(out, in0) |
| 60 |
|
| 61 |
#else // EC_NISTP_USE_S2N_BIGNUM |
| 62 |
|
| 63 |
#if defined(EC_NISTP_USE_64BIT_LIMB) |
| 64 |
|
| 65 |
// In the 64-bit case Fiat-crypto represents a field element by 9 58-bit digits. |
| 66 |
#define P521_NLIMBS (9) |
| 67 |
|
| 68 |
typedef uint64_t p521_felem[P521_NLIMBS]; // field element |
| 69 |
typedef uint64_t p521_limb_t; |
| 70 |
|
| 71 |
// One in Fiat-crypto's representation (58-bit digits). |
| 72 |
static const p521_limb_t p521_felem_one[P521_NLIMBS] = { |
| 73 |
0x0000000000000001, 0x0000000000000000, |
| 74 |
0x0000000000000000, 0x0000000000000000, |
| 75 |
0x0000000000000000, 0x0000000000000000, |
| 76 |
0x0000000000000000, 0x0000000000000000, |
| 77 |
0x0000000000000000}; |
| 78 |
|
| 79 |
// The field characteristic p in Fiat-crypto's representation (58-bit digits). |
| 80 |
static const p521_limb_t p521_felem_p[P521_NLIMBS] = { |
| 81 |
0x03ffffffffffffff, 0x03ffffffffffffff, |
| 82 |
0x03ffffffffffffff, 0x03ffffffffffffff, |
| 83 |
0x03ffffffffffffff, 0x03ffffffffffffff, |
| 84 |
0x03ffffffffffffff, 0x03ffffffffffffff, |
| 85 |
0x01ffffffffffffff}; |
| 86 |
|
| 87 |
#else // 64BIT; else 32BIT |
| 88 |
|
| 89 |
// In the 32-bit case Fiat-crypto represents a field element by 19 digits |
| 90 |
// with the following bit sizes: |
| 91 |
// [28, 27, 28, 27, 28, 27, 27, 28, 27, 28, 27, 28, 27, 27, 28, 27, 28, 27, 27]. |
| 92 |
#define P521_NLIMBS (19) |
| 93 |
|
| 94 |
typedef uint32_t p521_felem[P521_NLIMBS]; // field element |
| 95 |
typedef uint32_t p521_limb_t; |
| 96 |
|
| 97 |
// One in Fiat-crypto's representation. |
| 98 |
static const p521_limb_t p521_felem_one[P521_NLIMBS] = { |
| 99 |
0x0000001, 0x0000000, 0x0000000, 0x0000000, |
| 100 |
0x0000000, 0x0000000, 0x0000000, 0x0000000, |
| 101 |
0x0000000, 0x0000000, 0x0000000, 0x0000000, |
| 102 |
0x0000000, 0x0000000, 0x0000000, 0x0000000, |
| 103 |
0x0000000, 0x0000000, 0x0000000}; |
| 104 |
|
| 105 |
// The field characteristic p in Fiat-crypto's representation. |
| 106 |
static const p521_limb_t p521_felem_p[P521_NLIMBS] = { |
| 107 |
0xfffffff, 0x7ffffff, 0xfffffff, 0x7ffffff, |
| 108 |
0xfffffff, 0x7ffffff, 0x7ffffff, 0xfffffff, |
| 109 |
0x7ffffff, 0xfffffff, 0x7ffffff, 0xfffffff, |
| 110 |
0x7ffffff, 0x7ffffff, 0xfffffff, 0x7ffffff, |
| 111 |
0xfffffff, 0x7ffffff, 0x7ffffff}; |
| 112 |
#endif // 64BIT |
| 113 |
|
| 114 |
// Fiat-crypto implementation of field arithmetic |
| 115 |
#define p521_felem_add(out, in0, in1) fiat_secp521r1_carry_add(out, in0, in1) |
| 116 |
#define p521_felem_sub(out, in0, in1) fiat_secp521r1_carry_sub(out, in0, in1) |
| 117 |
#define p521_felem_opp(out, in0) fiat_secp521r1_carry_opp(out, in0) |
| 118 |
#define p521_felem_mul(out, in0, in1) fiat_secp521r1_carry_mul(out, in0, in1) |
| 119 |
#define p521_felem_sqr(out, in0) fiat_secp521r1_carry_square(out, in0) |
| 120 |
#define p521_felem_to_bytes(out, in0) fiat_secp521r1_to_bytes(out, in0) |
| 121 |
#define p521_felem_from_bytes(out, in0) fiat_secp521r1_from_bytes(out, in0) |
| 122 |
|
| 123 |
#endif // EC_NISTP_USE_S2N_BIGNUM |
| 124 |
|
| 125 |
// The wrapper functions are needed for FIPS static build. |
| 126 |
// Otherwise, initializing ec_nistp_meth with pointers to s2n-bignum |
| 127 |
// functions directly generates :got: references that are also thought |
| 128 |
// to be local_target by the delocator. |
| 129 |
static inline void p521_felem_add_wrapper(ec_nistp_felem_limb *c, |
| 130 |
const ec_nistp_felem_limb *a, |
| 131 |
const ec_nistp_felem_limb *b) { |
| 132 |
p521_felem_add(c, a, b); |
| 133 |
} |
| 134 |
|
| 135 |
static inline void p521_felem_sub_wrapper(ec_nistp_felem_limb *c, |
| 136 |
const ec_nistp_felem_limb *a, |
| 137 |
const ec_nistp_felem_limb *b) { |
| 138 |
p521_felem_sub(c, a, b); |
| 139 |
} |
| 140 |
|
| 141 |
static inline void p521_felem_neg_wrapper(ec_nistp_felem_limb *c, |
| 142 |
const ec_nistp_felem_limb *a) { |
| 143 |
p521_felem_opp(c, a); |
| 144 |
} |
| 145 |
|
| 146 |
static p521_limb_t p521_felem_nz(const p521_limb_t in1[P521_NLIMBS]) { |
| 147 |
p521_limb_t is_not_zero = 0; |
| 148 |
for (int i = 0; i < P521_NLIMBS; i++) { |
| 149 |
is_not_zero |= in1[i]; |
| 150 |
} |
| 151 |
|
| 152 |
#if defined(EC_NISTP_USE_S2N_BIGNUM) |
| 153 |
return is_not_zero; |
| 154 |
#else |
| 155 |
// Fiat-crypto functions may return p (the field characteristic) |
| 156 |
// instead of 0 in some cases, so we also check for that. |
| 157 |
p521_limb_t is_not_p = 0; |
| 158 |
for (int i = 0; i < P521_NLIMBS; i++) { |
| 159 |
is_not_p |= (in1[i] ^ p521_felem_p[i]); |
| 160 |
} |
| 161 |
|
| 162 |
return ~(constant_time_is_zero_w(is_not_p) | |
| 163 |
constant_time_is_zero_w(is_not_zero)); |
| 164 |
#endif |
| 165 |
} |
| 166 |
|
| 167 |
// NOTE: the input and output are in little-endian representation. |
| 168 |
static void p521_from_generic(p521_felem out, const EC_FELEM *in) { |
| 169 |
#ifdef OPENSSL_BIG_ENDIAN |
| 170 |
uint8_t tmp[P521_EC_FELEM_BYTES]; |
| 171 |
bn_words_to_little_endian(tmp, P521_EC_FELEM_BYTES, in->words, P521_EC_FELEM_WORDS); |
| 172 |
p521_felem_from_bytes(out, tmp); |
| 173 |
#else |
| 174 |
p521_felem_from_bytes(out, (const uint8_t *)in->words); |
| 175 |
#endif |
| 176 |
} |
| 177 |
|
| 178 |
// NOTE: the input and output are in little-endian representation. |
| 179 |
static void p521_to_generic(EC_FELEM *out, const p521_felem in) { |
| 180 |
// |p521_felem_to_bytes| function will write the result to the first 66 bytes |
| 181 |
// of |out| which is exactly how many bytes are needed to represent a 521-bit |
| 182 |
// element. |
| 183 |
// The number of BN_ULONGs to represent a 521-bit value is 9 and 17, when |
| 184 |
// BN_ULONG is 64-bit and 32-bit, respectively. Nine 64-bit BN_ULONGs |
| 185 |
// translate to 72 bytes, which means that we have to make sure that the |
| 186 |
// extra 6 bytes are zeroed out. To avoid confusion over 32 vs. 64 bit |
| 187 |
// systems and Fiat's vs. ours representation we zero out the whole element. |
| 188 |
#ifdef OPENSSL_BIG_ENDIAN |
| 189 |
uint8_t tmp[P521_EC_FELEM_BYTES]; |
| 190 |
p521_felem_to_bytes(tmp, in); |
| 191 |
bn_little_endian_to_words(out->words, P521_EC_FELEM_WORDS, tmp, P521_EC_FELEM_BYTES); |
| 192 |
#else |
| 193 |
OPENSSL_memset((uint8_t*)out->words, 0, sizeof(out->words)); |
| 194 |
// Convert the element to bytes. |
| 195 |
p521_felem_to_bytes((uint8_t *)out->words, in); |
| 196 |
#endif |
| 197 |
} |
| 198 |
|
| 199 |
// Finite field inversion using Fermat Little Theorem. |
| 200 |
// The code is autogenerated by the ECCKiila project: |
| 201 |
// https://arxiv.org/abs/2007.11481 |
| 202 |
static void p521_felem_inv(p521_felem output, const p521_felem t1) { |
| 203 |
#if defined(EC_NISTP_USE_S2N_BIGNUM) |
| 204 |
bignum_inv_p521(output, t1); |
| 205 |
#else |
| 206 |
/* temporary variables */ |
| 207 |
p521_felem acc, t2, t4, t8, t16, t32, t64; |
| 208 |
p521_felem t128, t256, t512, t516, t518, t519; |
| 209 |
|
| 210 |
p521_felem_sqr(acc, t1); |
| 211 |
p521_felem_mul(t2, acc, t1); |
| 212 |
p521_felem_sqr(acc, t2); |
| 213 |
p521_felem_sqr(acc, acc); |
| 214 |
p521_felem_mul(t4, acc, t2); |
| 215 |
p521_felem_sqr(acc, t4); |
| 216 |
for (int i = 0; i < 3; i++) { |
| 217 |
p521_felem_sqr(acc, acc); |
| 218 |
} |
| 219 |
p521_felem_mul(t8, acc, t4); |
| 220 |
p521_felem_sqr(acc, t8); |
| 221 |
for (int i = 0; i < 7; i++) { |
| 222 |
p521_felem_sqr(acc, acc); |
| 223 |
} |
| 224 |
p521_felem_mul(t16, acc, t8); |
| 225 |
p521_felem_sqr(acc, t16); |
| 226 |
for (int i = 0; i < 15; i++) { |
| 227 |
p521_felem_sqr(acc, acc); |
| 228 |
} |
| 229 |
p521_felem_mul(t32, acc, t16); |
| 230 |
p521_felem_sqr(acc, t32); |
| 231 |
for (int i = 0; i < 31; i++) { |
| 232 |
p521_felem_sqr(acc, acc); |
| 233 |
} |
| 234 |
p521_felem_mul(t64, acc, t32); |
| 235 |
p521_felem_sqr(acc, t64); |
| 236 |
for (int i = 0; i < 63; i++) { |
| 237 |
p521_felem_sqr(acc, acc); |
| 238 |
} |
| 239 |
p521_felem_mul(t128, acc, t64); |
| 240 |
p521_felem_sqr(acc, t128); |
| 241 |
for (int i = 0; i < 127; i++) { |
| 242 |
p521_felem_sqr(acc, acc); |
| 243 |
} |
| 244 |
p521_felem_mul(t256, acc, t128); |
| 245 |
p521_felem_sqr(acc, t256); |
| 246 |
for (int i = 0; i < 255; i++) { |
| 247 |
p521_felem_sqr(acc, acc); |
| 248 |
} |
| 249 |
p521_felem_mul(t512, acc, t256); |
| 250 |
p521_felem_sqr(acc, t512); |
| 251 |
for (int i = 0; i < 3; i++) { |
| 252 |
p521_felem_sqr(acc, acc); |
| 253 |
} |
| 254 |
p521_felem_mul(t516, acc, t4); |
| 255 |
p521_felem_sqr(acc, t516); |
| 256 |
p521_felem_sqr(acc, acc); |
| 257 |
p521_felem_mul(t518, acc, t2); |
| 258 |
p521_felem_sqr(acc, t518); |
| 259 |
p521_felem_mul(t519, acc, t1); |
| 260 |
p521_felem_sqr(acc, t519); |
| 261 |
p521_felem_sqr(acc, acc); |
| 262 |
p521_felem_mul(output, acc, t1); |
| 263 |
#endif |
| 264 |
} |
| 265 |
|
| 266 |
static void p521_point_double(p521_felem x_out, |
| 267 |
p521_felem y_out, |
| 268 |
p521_felem z_out, |
| 269 |
const p521_felem x_in, |
| 270 |
const p521_felem y_in, |
| 271 |
const p521_felem z_in) { |
| 272 |
#if defined(EC_NISTP_USE_S2N_BIGNUM) |
| 273 |
ec_nistp_felem_limb in[P521_NLIMBS * 3]; |
| 274 |
ec_nistp_felem_limb out[P521_NLIMBS * 3]; |
| 275 |
ec_nistp_coordinates_to_point(in, x_in, y_in, z_in, P521_NLIMBS); |
| 276 |
p521_jdouble_selector(out, in); |
| 277 |
ec_nistp_point_to_coordinates(x_out, y_out, z_out, out, P521_NLIMBS); |
| 278 |
#else |
| 279 |
ec_nistp_point_double(p521_methods(), x_out, y_out, z_out, x_in, y_in, z_in); |
| 280 |
#endif |
| 281 |
} |
| 282 |
|
| 283 |
// p521_point_add calculates (x1, y1, z1) + (x2, y2, z2) |
| 284 |
// |
| 285 |
// The method is taken from: |
| 286 |
// http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#addition-add-2007-bl |
| 287 |
// adapted for mixed addition (z2 = 1, or z2 = 0 for the point at infinity). |
| 288 |
// |
| 289 |
// Coq transcription and correctness proof: |
| 290 |
// <https://github.com/davidben/fiat-crypto/blob/c7b95f62b2a54b559522573310e9b487327d219a/src/Curves/Weierstrass/Jacobian.v#L467> |
| 291 |
// <https://github.com/davidben/fiat-crypto/blob/c7b95f62b2a54b559522573310e9b487327d219a/src/Curves/Weierstrass/Jacobian.v#L544> |
| 292 |
static void p521_point_add(p521_felem x3, p521_felem y3, p521_felem z3, |
| 293 |
const p521_felem x1, |
| 294 |
const p521_felem y1, |
| 295 |
const p521_felem z1, |
| 296 |
const int mixed, |
| 297 |
const p521_felem x2, |
| 298 |
const p521_felem y2, |
| 299 |
const p521_felem z2) { |
| 300 |
ec_nistp_point_add(p521_methods(), x3, y3, z3, x1, y1, z1, mixed, x2, y2, z2); |
| 301 |
} |
| 302 |
|
| 303 |
#include "p521_table.h" |
| 304 |
|
| 305 |
#if defined(EC_NISTP_USE_S2N_BIGNUM) |
| 306 |
DEFINE_METHOD_FUNCTION(ec_nistp_meth, p521_methods) { |
| 307 |
out->felem_num_limbs = P521_NLIMBS; |
| 308 |
out->felem_num_bits = 521; |
| 309 |
out->felem_add = p521_felem_add_wrapper; |
| 310 |
out->felem_sub = p521_felem_sub_wrapper; |
| 311 |
out->felem_mul = bignum_mul_p521_selector; |
| 312 |
out->felem_sqr = bignum_sqr_p521_selector; |
| 313 |
out->felem_neg = p521_felem_neg_wrapper; |
| 314 |
out->felem_nz = p521_felem_nz; |
| 315 |
out->felem_one = p521_felem_one; |
| 316 |
out->point_dbl = p521_point_double; |
| 317 |
out->point_add = p521_point_add; |
| 318 |
out->scalar_mul_base_table = (const ec_nistp_felem_limb*) p521_g_pre_comp; |
| 319 |
} |
| 320 |
#else |
| 321 |
DEFINE_METHOD_FUNCTION(ec_nistp_meth, p521_methods) { |
| 322 |
out->felem_num_limbs = P521_NLIMBS; |
| 323 |
out->felem_num_bits = 521; |
| 324 |
out->felem_add = fiat_secp521r1_carry_add; |
| 325 |
out->felem_sub = fiat_secp521r1_carry_sub; |
| 326 |
out->felem_mul = fiat_secp521r1_carry_mul; |
| 327 |
out->felem_sqr = fiat_secp521r1_carry_square; |
| 328 |
out->felem_neg = fiat_secp521r1_carry_opp; |
| 329 |
out->felem_nz = p521_felem_nz; |
| 330 |
out->felem_one = p521_felem_one; |
| 331 |
out->point_dbl = p521_point_double; |
| 332 |
out->point_add = p521_point_add; |
| 333 |
out->scalar_mul_base_table = (const ec_nistp_felem_limb*) p521_g_pre_comp; |
| 334 |
} |
| 335 |
#endif |
| 336 |
|
| 337 |
// OPENSSL EC_METHOD FUNCTIONS |
| 338 |
|
| 339 |
// Takes the Jacobian coordinates (X, Y, Z) of a point and returns: |
| 340 |
// (X', Y') = (X/Z^2, Y/Z^3). |
| 341 |
static int ec_GFp_nistp521_point_get_affine_coordinates( |
| 342 |
const EC_GROUP *group, const EC_JACOBIAN *point, |
| 343 |
EC_FELEM *x_out, EC_FELEM *y_out) { |
| 344 |
|
| 345 |
if (constant_time_declassify_w(ec_GFp_simple_is_at_infinity(group, point))) { |
| 346 |
OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY); |
| 347 |
return 0; |
| 348 |
} |
| 349 |
|
| 350 |
p521_felem z1, z2; |
| 351 |
p521_from_generic(z1, &point->Z); |
| 352 |
p521_felem_inv(z2, z1); |
| 353 |
p521_felem_sqr(z2, z2); |
| 354 |
|
| 355 |
if (x_out != NULL) { |
| 356 |
p521_felem x; |
| 357 |
p521_from_generic(x, &point->X); |
| 358 |
p521_felem_mul(x, x, z2); |
| 359 |
p521_to_generic(x_out, x); |
| 360 |
} |
| 361 |
|
| 362 |
if (y_out != NULL) { |
| 363 |
p521_felem y; |
| 364 |
p521_from_generic(y, &point->Y); |
| 365 |
p521_felem_sqr(z2, z2); // z^-4 |
| 366 |
p521_felem_mul(y, y, z1); // y * z |
| 367 |
p521_felem_mul(y, y, z2); // y * z^-3 |
| 368 |
p521_to_generic(y_out, y); |
| 369 |
} |
| 370 |
|
| 371 |
return 1; |
| 372 |
} |
| 373 |
|
| 374 |
static void ec_GFp_nistp521_add(const EC_GROUP *group, EC_JACOBIAN *r, |
| 375 |
const EC_JACOBIAN *a, const EC_JACOBIAN *b) { |
| 376 |
p521_felem x1, y1, z1, x2, y2, z2; |
| 377 |
p521_from_generic(x1, &a->X); |
| 378 |
p521_from_generic(y1, &a->Y); |
| 379 |
p521_from_generic(z1, &a->Z); |
| 380 |
p521_from_generic(x2, &b->X); |
| 381 |
p521_from_generic(y2, &b->Y); |
| 382 |
p521_from_generic(z2, &b->Z); |
| 383 |
p521_point_add(x1, y1, z1, x1, y1, z1, 0 /* both Jacobian */, x2, y2, z2); |
| 384 |
p521_to_generic(&r->X, x1); |
| 385 |
p521_to_generic(&r->Y, y1); |
| 386 |
p521_to_generic(&r->Z, z1); |
| 387 |
} |
| 388 |
|
| 389 |
static void ec_GFp_nistp521_dbl(const EC_GROUP *group, EC_JACOBIAN *r, |
| 390 |
const EC_JACOBIAN *a) { |
| 391 |
p521_felem x, y, z; |
| 392 |
p521_from_generic(x, &a->X); |
| 393 |
p521_from_generic(y, &a->Y); |
| 394 |
p521_from_generic(z, &a->Z); |
| 395 |
p521_point_double(x, y, z, x, y, z); |
| 396 |
p521_to_generic(&r->X, x); |
| 397 |
p521_to_generic(&r->Y, y); |
| 398 |
p521_to_generic(&r->Z, z); |
| 399 |
} |
| 400 |
|
| 401 |
// Multiplication of an arbitrary point by a scalar, r = [scalar]P. |
| 402 |
static void ec_GFp_nistp521_point_mul(const EC_GROUP *group, EC_JACOBIAN *r, |
| 403 |
const EC_JACOBIAN *p, |
| 404 |
const EC_SCALAR *scalar) { |
| 405 |
|
| 406 |
p521_felem res[3] = {{0}, {0}, {0}}, tmp[3] = {{0}, {0}, {0}}; |
| 407 |
|
| 408 |
p521_from_generic(tmp[0], &p->X); |
| 409 |
p521_from_generic(tmp[1], &p->Y); |
| 410 |
p521_from_generic(tmp[2], &p->Z); |
| 411 |
|
| 412 |
#if defined(EC_NISTP_USE_S2N_BIGNUM) |
| 413 |
p521_jscalarmul_selector((uint64_t*)res, scalar->words, (uint64_t*)tmp); |
| 414 |
#else |
| 415 |
ec_nistp_scalar_mul(p521_methods(), res[0], res[1], res[2], tmp[0], tmp[1], tmp[2], scalar); |
| 416 |
#endif |
| 417 |
|
| 418 |
p521_to_generic(&r->X, res[0]); |
| 419 |
p521_to_generic(&r->Y, res[1]); |
| 420 |
p521_to_generic(&r->Z, res[2]); |
| 421 |
} |
| 422 |
|
| 423 |
// Multiplication of the base point G of P-521 curve with the given scalar. |
| 424 |
static void ec_GFp_nistp521_point_mul_base(const EC_GROUP *group, |
| 425 |
EC_JACOBIAN *r, |
| 426 |
const EC_SCALAR *scalar) { |
| 427 |
p521_felem res[3] = {{0}, {0}, {0}}; |
| 428 |
|
| 429 |
ec_nistp_scalar_mul_base(p521_methods(), res[0], res[1], res[2], scalar); |
| 430 |
|
| 431 |
p521_to_generic(&r->X, res[0]); |
| 432 |
p521_to_generic(&r->Y, res[1]); |
| 433 |
p521_to_generic(&r->Z, res[2]); |
| 434 |
} |
| 435 |
|
| 436 |
// Computes [g_scalar]G + [p_scalar]P, where G is the base point of the P-521 |
| 437 |
// curve, and P is the given point |p|. |
| 438 |
// Note: this function is NOT constant-time. |
| 439 |
static void ec_GFp_nistp521_point_mul_public(const EC_GROUP *group, |
| 440 |
EC_JACOBIAN *r, |
| 441 |
const EC_SCALAR *g_scalar, |
| 442 |
const EC_JACOBIAN *p, |
| 443 |
const EC_SCALAR *p_scalar) { |
| 444 |
|
| 445 |
p521_felem res[3] = {{0}, {0}, {0}}, tmp[3] = {{0}, {0}, {0}}; |
| 446 |
|
| 447 |
p521_from_generic(tmp[0], &p->X); |
| 448 |
p521_from_generic(tmp[1], &p->Y); |
| 449 |
p521_from_generic(tmp[2], &p->Z); |
| 450 |
|
| 451 |
ec_nistp_scalar_mul_public(p521_methods(), res[0], res[1], res[2], g_scalar, tmp[0], tmp[1], tmp[2], p_scalar); |
| 452 |
|
| 453 |
// Copy the result to the output. |
| 454 |
p521_to_generic(&r->X, res[0]); |
| 455 |
p521_to_generic(&r->Y, res[1]); |
| 456 |
p521_to_generic(&r->Z, res[2]); |
| 457 |
} |
| 458 |
|
| 459 |
static void ec_GFp_nistp521_felem_mul(const EC_GROUP *group, EC_FELEM *r, |
| 460 |
const EC_FELEM *a, const EC_FELEM *b) { |
| 461 |
p521_felem felem1, felem2, felem3; |
| 462 |
p521_from_generic(felem1, a); |
| 463 |
p521_from_generic(felem2, b); |
| 464 |
p521_felem_mul(felem3, felem1, felem2); |
| 465 |
p521_to_generic(r, felem3); |
| 466 |
} |
| 467 |
|
| 468 |
static void ec_GFp_nistp521_felem_sqr(const EC_GROUP *group, EC_FELEM *r, |
| 469 |
const EC_FELEM *a) { |
| 470 |
p521_felem felem1, felem2; |
| 471 |
p521_from_generic(felem1, a); |
| 472 |
p521_felem_sqr(felem2, felem1); |
| 473 |
p521_to_generic(r, felem2); |
| 474 |
} |
| 475 |
|
| 476 |
DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_nistp521_method) { |
| 477 |
out->point_get_affine_coordinates = |
| 478 |
ec_GFp_nistp521_point_get_affine_coordinates; |
| 479 |
out->add = ec_GFp_nistp521_add; |
| 480 |
out->dbl = ec_GFp_nistp521_dbl; |
| 481 |
out->mul = ec_GFp_nistp521_point_mul; |
| 482 |
out->mul_base = ec_GFp_nistp521_point_mul_base; |
| 483 |
out->mul_public = ec_GFp_nistp521_point_mul_public; |
| 484 |
out->felem_mul = ec_GFp_nistp521_felem_mul; |
| 485 |
out->felem_sqr = ec_GFp_nistp521_felem_sqr; |
| 486 |
out->felem_to_bytes = ec_GFp_simple_felem_to_bytes; |
| 487 |
out->felem_from_bytes = ec_GFp_simple_felem_from_bytes; |
| 488 |
out->scalar_inv0_montgomery = ec_simple_scalar_inv0_montgomery; |
| 489 |
out->scalar_to_montgomery_inv_vartime = |
| 490 |
ec_simple_scalar_to_montgomery_inv_vartime; |
| 491 |
out->cmp_x_coordinate = ec_GFp_simple_cmp_x_coordinate; |
| 492 |
} |
| 493 |
|
| 494 |
// ---------------------------------------------------------------------------- |
| 495 |
// Analysis of the doubling case occurrence in the Joye-Tunstall recoding: |
| 496 |
// see the analysis at the bottom of the |p384.c| file. |
| 497 |
#endif // !defined(OPENSSL_SMALL) |