| 1 |
// Copyright (c) 2020, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
// An implementation of the NIST P-256 elliptic curve point multiplication. |
| 5 |
// 256-bit Montgomery form for 64 and 32-bit. Field operations are generated by |
| 6 |
// Fiat, which lives in //third_party/fiat. |
| 7 |
|
| 8 |
#include <openssl/base.h> |
| 9 |
|
| 10 |
#include <openssl/bn.h> |
| 11 |
#include <openssl/ec.h> |
| 12 |
#include <openssl/err.h> |
| 13 |
#include <openssl/mem.h> |
| 14 |
#include <openssl/type_check.h> |
| 15 |
|
| 16 |
#include <assert.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#include "../../internal.h" |
| 20 |
#include "../delocate.h" |
| 21 |
#include "./internal.h" |
| 22 |
#include "ec_nistp.h" |
| 23 |
|
| 24 |
#if defined(BORINGSSL_HAS_UINT128) |
| 25 |
#define BORINGSSL_NISTP256_64BIT 1 |
| 26 |
#include "../../../third_party/fiat/p256_64.h" |
| 27 |
#else |
| 28 |
#include "../../../third_party/fiat/p256_32.h" |
| 29 |
#endif |
| 30 |
|
| 31 |
|
| 32 |
// utility functions, handwritten |
| 33 |
|
| 34 |
#if defined(BORINGSSL_NISTP256_64BIT) |
| 35 |
#define FIAT_P256_NLIMBS 4 |
| 36 |
typedef uint64_t fiat_p256_limb_t; |
| 37 |
typedef uint64_t fiat_p256_felem[FIAT_P256_NLIMBS]; |
| 38 |
static const fiat_p256_felem fiat_p256_one = {0x1, 0xffffffff00000000, |
| 39 |
0xffffffffffffffff, 0xfffffffe}; |
| 40 |
#else // 64BIT; else 32BIT |
| 41 |
#define FIAT_P256_NLIMBS 8 |
| 42 |
typedef uint32_t fiat_p256_limb_t; |
| 43 |
typedef uint32_t fiat_p256_felem[FIAT_P256_NLIMBS]; |
| 44 |
static const fiat_p256_felem fiat_p256_one = { |
| 45 |
0x1, 0x0, 0x0, 0xffffffff, 0xffffffff, 0xffffffff, 0xfffffffe, 0x0}; |
| 46 |
#endif // 64BIT |
| 47 |
|
| 48 |
|
| 49 |
static fiat_p256_limb_t fiat_p256_nz( |
| 50 |
const fiat_p256_limb_t in1[FIAT_P256_NLIMBS]) { |
| 51 |
fiat_p256_limb_t ret; |
| 52 |
fiat_p256_nonzero(&ret, in1); |
| 53 |
return ret; |
| 54 |
} |
| 55 |
|
| 56 |
static void fiat_p256_from_words(fiat_p256_felem out, |
| 57 |
const BN_ULONG in[32 / sizeof(BN_ULONG)]) { |
| 58 |
// Typically, |BN_ULONG| and |fiat_p256_limb_t| will be the same type, but on |
| 59 |
// 64-bit platforms without |uint128_t|, they are different. However, on |
| 60 |
// little-endian systems, |uint64_t[4]| and |uint32_t[8]| have the same |
| 61 |
// layout. |
| 62 |
OPENSSL_memcpy(out, in, 32); |
| 63 |
} |
| 64 |
|
| 65 |
static void fiat_p256_from_generic(fiat_p256_felem out, const EC_FELEM *in) { |
| 66 |
fiat_p256_from_words(out, in->words); |
| 67 |
} |
| 68 |
|
| 69 |
static void fiat_p256_to_generic(EC_FELEM *out, const fiat_p256_felem in) { |
| 70 |
// See |fiat_p256_from_words|. |
| 71 |
OPENSSL_memcpy(out->words, in, 32); |
| 72 |
} |
| 73 |
|
| 74 |
// fiat_p256_inv_square calculates |out| = |in|^{-2} |
| 75 |
// |
| 76 |
// Based on Fermat's Little Theorem: |
| 77 |
// a^p = a (mod p) |
| 78 |
// a^{p-1} = 1 (mod p) |
| 79 |
// a^{p-3} = a^{-2} (mod p) |
| 80 |
static void fiat_p256_inv_square(fiat_p256_felem out, |
| 81 |
const fiat_p256_felem in) { |
| 82 |
// This implements the addition chain described in |
| 83 |
// https://briansmith.org/ecc-inversion-addition-chains-01#p256_field_inversion |
| 84 |
fiat_p256_felem x2, x3, x6, x12, x15, x30, x32; |
| 85 |
fiat_p256_square(x2, in); // 2^2 - 2^1 |
| 86 |
fiat_p256_mul(x2, x2, in); // 2^2 - 2^0 |
| 87 |
|
| 88 |
fiat_p256_square(x3, x2); // 2^3 - 2^1 |
| 89 |
fiat_p256_mul(x3, x3, in); // 2^3 - 2^0 |
| 90 |
|
| 91 |
fiat_p256_square(x6, x3); |
| 92 |
for (int i = 1; i < 3; i++) { |
| 93 |
fiat_p256_square(x6, x6); |
| 94 |
} // 2^6 - 2^3 |
| 95 |
fiat_p256_mul(x6, x6, x3); // 2^6 - 2^0 |
| 96 |
|
| 97 |
fiat_p256_square(x12, x6); |
| 98 |
for (int i = 1; i < 6; i++) { |
| 99 |
fiat_p256_square(x12, x12); |
| 100 |
} // 2^12 - 2^6 |
| 101 |
fiat_p256_mul(x12, x12, x6); // 2^12 - 2^0 |
| 102 |
|
| 103 |
fiat_p256_square(x15, x12); |
| 104 |
for (int i = 1; i < 3; i++) { |
| 105 |
fiat_p256_square(x15, x15); |
| 106 |
} // 2^15 - 2^3 |
| 107 |
fiat_p256_mul(x15, x15, x3); // 2^15 - 2^0 |
| 108 |
|
| 109 |
fiat_p256_square(x30, x15); |
| 110 |
for (int i = 1; i < 15; i++) { |
| 111 |
fiat_p256_square(x30, x30); |
| 112 |
} // 2^30 - 2^15 |
| 113 |
fiat_p256_mul(x30, x30, x15); // 2^30 - 2^0 |
| 114 |
|
| 115 |
fiat_p256_square(x32, x30); |
| 116 |
fiat_p256_square(x32, x32); // 2^32 - 2^2 |
| 117 |
fiat_p256_mul(x32, x32, x2); // 2^32 - 2^0 |
| 118 |
|
| 119 |
fiat_p256_felem ret; |
| 120 |
fiat_p256_square(ret, x32); |
| 121 |
for (int i = 1; i < 31 + 1; i++) { |
| 122 |
fiat_p256_square(ret, ret); |
| 123 |
} // 2^64 - 2^32 |
| 124 |
fiat_p256_mul(ret, ret, in); // 2^64 - 2^32 + 2^0 |
| 125 |
|
| 126 |
for (int i = 0; i < 96 + 32; i++) { |
| 127 |
fiat_p256_square(ret, ret); |
| 128 |
} // 2^192 - 2^160 + 2^128 |
| 129 |
fiat_p256_mul(ret, ret, x32); // 2^192 - 2^160 + 2^128 + 2^32 - 2^0 |
| 130 |
|
| 131 |
for (int i = 0; i < 32; i++) { |
| 132 |
fiat_p256_square(ret, ret); |
| 133 |
} // 2^224 - 2^192 + 2^160 + 2^64 - 2^32 |
| 134 |
fiat_p256_mul(ret, ret, x32); // 2^224 - 2^192 + 2^160 + 2^64 - 2^0 |
| 135 |
|
| 136 |
for (int i = 0; i < 30; i++) { |
| 137 |
fiat_p256_square(ret, ret); |
| 138 |
} // 2^254 - 2^222 + 2^190 + 2^94 - 2^30 |
| 139 |
fiat_p256_mul(ret, ret, x30); // 2^254 - 2^222 + 2^190 + 2^94 - 2^0 |
| 140 |
|
| 141 |
fiat_p256_square(ret, ret); |
| 142 |
fiat_p256_square(out, ret); // 2^256 - 2^224 + 2^192 + 2^96 - 2^2 |
| 143 |
} |
| 144 |
|
| 145 |
static void fiat_p256_point_double(fiat_p256_felem x_out, |
| 146 |
fiat_p256_felem y_out, |
| 147 |
fiat_p256_felem z_out, |
| 148 |
const fiat_p256_felem x_in, |
| 149 |
const fiat_p256_felem y_in, |
| 150 |
const fiat_p256_felem z_in) { |
| 151 |
ec_nistp_point_double(p256_methods(), x_out, y_out, z_out, x_in, y_in, z_in); |
| 152 |
} |
| 153 |
|
| 154 |
static void fiat_p256_point_add(fiat_p256_felem x3, fiat_p256_felem y3, |
| 155 |
fiat_p256_felem z3, const fiat_p256_felem x1, |
| 156 |
const fiat_p256_felem y1, |
| 157 |
const fiat_p256_felem z1, const int mixed, |
| 158 |
const fiat_p256_felem x2, |
| 159 |
const fiat_p256_felem y2, |
| 160 |
const fiat_p256_felem z2) { |
| 161 |
ec_nistp_point_add(p256_methods(), x3, y3, z3, x1, y1, z1, mixed, x2, y2, z2); |
| 162 |
} |
| 163 |
|
| 164 |
#include "./p256_table.h" |
| 165 |
|
| 166 |
DEFINE_METHOD_FUNCTION(ec_nistp_meth, p256_methods) { |
| 167 |
out->felem_num_limbs = FIAT_P256_NLIMBS; |
| 168 |
out->felem_num_bits = 256; |
| 169 |
out->felem_add = fiat_p256_add; |
| 170 |
out->felem_sub = fiat_p256_sub; |
| 171 |
out->felem_mul = fiat_p256_mul; |
| 172 |
out->felem_sqr = fiat_p256_square; |
| 173 |
out->felem_neg = fiat_p256_opp; |
| 174 |
out->felem_nz = fiat_p256_nz; |
| 175 |
out->felem_one = fiat_p256_one; |
| 176 |
out->point_dbl = fiat_p256_point_double; |
| 177 |
out->point_add = fiat_p256_point_add; |
| 178 |
out->scalar_mul_base_table = (const ec_nistp_felem_limb*) fiat_p256_g_pre_comp; |
| 179 |
} |
| 180 |
|
| 181 |
// OPENSSL EC_METHOD FUNCTIONS |
| 182 |
|
| 183 |
// Takes the Jacobian coordinates (X, Y, Z) of a point and returns (X', Y') = |
| 184 |
// (X/Z^2, Y/Z^3). |
| 185 |
static int ec_GFp_nistp256_point_get_affine_coordinates( |
| 186 |
const EC_GROUP *group, const EC_JACOBIAN *point, EC_FELEM *x_out, |
| 187 |
EC_FELEM *y_out) { |
| 188 |
if (constant_time_declassify_int( |
| 189 |
ec_GFp_simple_is_at_infinity(group, point))) { |
| 190 |
OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY); |
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
fiat_p256_felem z1, z2; |
| 195 |
fiat_p256_from_generic(z1, &point->Z); |
| 196 |
fiat_p256_inv_square(z2, z1); |
| 197 |
|
| 198 |
if (x_out != NULL) { |
| 199 |
fiat_p256_felem x; |
| 200 |
fiat_p256_from_generic(x, &point->X); |
| 201 |
fiat_p256_mul(x, x, z2); |
| 202 |
fiat_p256_to_generic(x_out, x); |
| 203 |
} |
| 204 |
|
| 205 |
if (y_out != NULL) { |
| 206 |
fiat_p256_felem y; |
| 207 |
fiat_p256_from_generic(y, &point->Y); |
| 208 |
fiat_p256_square(z2, z2); // z^-4 |
| 209 |
fiat_p256_mul(y, y, z1); // y * z |
| 210 |
fiat_p256_mul(y, y, z2); // y * z^-3 |
| 211 |
fiat_p256_to_generic(y_out, y); |
| 212 |
} |
| 213 |
|
| 214 |
return 1; |
| 215 |
} |
| 216 |
|
| 217 |
static void ec_GFp_nistp256_add(const EC_GROUP *group, EC_JACOBIAN *r, |
| 218 |
const EC_JACOBIAN *a, const EC_JACOBIAN *b) { |
| 219 |
fiat_p256_felem x1, y1, z1, x2, y2, z2; |
| 220 |
fiat_p256_from_generic(x1, &a->X); |
| 221 |
fiat_p256_from_generic(y1, &a->Y); |
| 222 |
fiat_p256_from_generic(z1, &a->Z); |
| 223 |
fiat_p256_from_generic(x2, &b->X); |
| 224 |
fiat_p256_from_generic(y2, &b->Y); |
| 225 |
fiat_p256_from_generic(z2, &b->Z); |
| 226 |
fiat_p256_point_add(x1, y1, z1, x1, y1, z1, 0 /* both Jacobian */, x2, y2, |
| 227 |
z2); |
| 228 |
fiat_p256_to_generic(&r->X, x1); |
| 229 |
fiat_p256_to_generic(&r->Y, y1); |
| 230 |
fiat_p256_to_generic(&r->Z, z1); |
| 231 |
} |
| 232 |
|
| 233 |
static void ec_GFp_nistp256_dbl(const EC_GROUP *group, EC_JACOBIAN *r, |
| 234 |
const EC_JACOBIAN *a) { |
| 235 |
fiat_p256_felem x, y, z; |
| 236 |
fiat_p256_from_generic(x, &a->X); |
| 237 |
fiat_p256_from_generic(y, &a->Y); |
| 238 |
fiat_p256_from_generic(z, &a->Z); |
| 239 |
fiat_p256_point_double(x, y, z, x, y, z); |
| 240 |
fiat_p256_to_generic(&r->X, x); |
| 241 |
fiat_p256_to_generic(&r->Y, y); |
| 242 |
fiat_p256_to_generic(&r->Z, z); |
| 243 |
} |
| 244 |
|
| 245 |
static void ec_GFp_nistp256_point_mul(const EC_GROUP *group, EC_JACOBIAN *r, |
| 246 |
const EC_JACOBIAN *p, |
| 247 |
const EC_SCALAR *scalar) { |
| 248 |
fiat_p256_felem res[3], tmp[3]; |
| 249 |
fiat_p256_from_generic(tmp[0], &p->X); |
| 250 |
fiat_p256_from_generic(tmp[1], &p->Y); |
| 251 |
fiat_p256_from_generic(tmp[2], &p->Z); |
| 252 |
|
| 253 |
ec_nistp_scalar_mul(p256_methods(), res[0], res[1], res[2], tmp[0], tmp[1], tmp[2], scalar); |
| 254 |
|
| 255 |
fiat_p256_to_generic(&r->X, res[0]); |
| 256 |
fiat_p256_to_generic(&r->Y, res[1]); |
| 257 |
fiat_p256_to_generic(&r->Z, res[2]); |
| 258 |
} |
| 259 |
|
| 260 |
static void ec_GFp_nistp256_point_mul_base(const EC_GROUP *group, |
| 261 |
EC_JACOBIAN *r, |
| 262 |
const EC_SCALAR *scalar) { |
| 263 |
fiat_p256_felem res[3]; |
| 264 |
|
| 265 |
ec_nistp_scalar_mul_base(p256_methods(), res[0], res[1], res[2], scalar); |
| 266 |
|
| 267 |
fiat_p256_to_generic(&r->X, res[0]); |
| 268 |
fiat_p256_to_generic(&r->Y, res[1]); |
| 269 |
fiat_p256_to_generic(&r->Z, res[2]); |
| 270 |
} |
| 271 |
|
| 272 |
static void ec_GFp_nistp256_point_mul_public(const EC_GROUP *group, |
| 273 |
EC_JACOBIAN *r, |
| 274 |
const EC_SCALAR *g_scalar, |
| 275 |
const EC_JACOBIAN *p, |
| 276 |
const EC_SCALAR *p_scalar) { |
| 277 |
fiat_p256_felem res[3], tmp[3]; |
| 278 |
fiat_p256_from_generic(tmp[0], &p->X); |
| 279 |
fiat_p256_from_generic(tmp[1], &p->Y); |
| 280 |
fiat_p256_from_generic(tmp[2], &p->Z); |
| 281 |
|
| 282 |
ec_nistp_scalar_mul_public(p256_methods(), res[0], res[1], res[2], g_scalar, tmp[0], tmp[1], tmp[2], p_scalar); |
| 283 |
|
| 284 |
fiat_p256_to_generic(&r->X, res[0]); |
| 285 |
fiat_p256_to_generic(&r->Y, res[1]); |
| 286 |
fiat_p256_to_generic(&r->Z, res[2]); |
| 287 |
} |
| 288 |
|
| 289 |
static int ec_GFp_nistp256_cmp_x_coordinate(const EC_GROUP *group, |
| 290 |
const EC_JACOBIAN *p, |
| 291 |
const EC_SCALAR *r) { |
| 292 |
if (ec_GFp_simple_is_at_infinity(group, p)) { |
| 293 |
return 0; |
| 294 |
} |
| 295 |
|
| 296 |
// We wish to compare X/Z^2 with r. This is equivalent to comparing X with |
| 297 |
// r*Z^2. Note that X and Z are represented in Montgomery form, while r is |
| 298 |
// not. |
| 299 |
fiat_p256_felem Z2_mont; |
| 300 |
fiat_p256_from_generic(Z2_mont, &p->Z); |
| 301 |
fiat_p256_mul(Z2_mont, Z2_mont, Z2_mont); |
| 302 |
|
| 303 |
fiat_p256_felem r_Z2; |
| 304 |
fiat_p256_from_words(r_Z2, r->words); // r < order < p, so this is valid. |
| 305 |
fiat_p256_mul(r_Z2, r_Z2, Z2_mont); |
| 306 |
|
| 307 |
fiat_p256_felem X; |
| 308 |
fiat_p256_from_generic(X, &p->X); |
| 309 |
fiat_p256_from_montgomery(X, X); |
| 310 |
|
| 311 |
if (OPENSSL_memcmp(&r_Z2, &X, sizeof(r_Z2)) == 0) { |
| 312 |
return 1; |
| 313 |
} |
| 314 |
|
| 315 |
// During signing the x coefficient is reduced modulo the group order. |
| 316 |
// Therefore there is a small possibility, less than 1/2^128, that group_order |
| 317 |
// < p.x < P. in that case we need not only to compare against |r| but also to |
| 318 |
// compare against r+group_order. |
| 319 |
assert(group->field.N.width == group->order.N.width); |
| 320 |
EC_FELEM tmp; |
| 321 |
BN_ULONG carry = |
| 322 |
bn_add_words(tmp.words, r->words, group->order.N.d, group->field.N.width); |
| 323 |
if (carry == 0 && |
| 324 |
bn_less_than_words(tmp.words, group->field.N.d, group->field.N.width)) { |
| 325 |
fiat_p256_from_generic(r_Z2, &tmp); |
| 326 |
fiat_p256_mul(r_Z2, r_Z2, Z2_mont); |
| 327 |
if (OPENSSL_memcmp(&r_Z2, &X, sizeof(r_Z2)) == 0) { |
| 328 |
return 1; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_nistp256_method) { |
| 336 |
out->point_get_affine_coordinates = |
| 337 |
ec_GFp_nistp256_point_get_affine_coordinates; |
| 338 |
out->add = ec_GFp_nistp256_add; |
| 339 |
out->dbl = ec_GFp_nistp256_dbl; |
| 340 |
out->mul = ec_GFp_nistp256_point_mul; |
| 341 |
out->mul_base = ec_GFp_nistp256_point_mul_base; |
| 342 |
out->mul_public = ec_GFp_nistp256_point_mul_public; |
| 343 |
out->felem_mul = ec_GFp_mont_felem_mul; |
| 344 |
out->felem_sqr = ec_GFp_mont_felem_sqr; |
| 345 |
out->felem_to_bytes = ec_GFp_mont_felem_to_bytes; |
| 346 |
out->felem_from_bytes = ec_GFp_mont_felem_from_bytes; |
| 347 |
out->felem_reduce = ec_GFp_mont_felem_reduce; |
| 348 |
// TODO(davidben): This should use the specialized field arithmetic |
| 349 |
// implementation, rather than the generic one. |
| 350 |
out->felem_exp = ec_GFp_mont_felem_exp; |
| 351 |
out->scalar_inv0_montgomery = ec_simple_scalar_inv0_montgomery; |
| 352 |
out->scalar_to_montgomery_inv_vartime = |
| 353 |
ec_simple_scalar_to_montgomery_inv_vartime; |
| 354 |
out->cmp_x_coordinate = ec_GFp_nistp256_cmp_x_coordinate; |
| 355 |
} |
| 356 |
|
| 357 |
#undef BORINGSSL_NISTP256_64BIT |