| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 |
| 16 | #ifndef OPENSSL_HEADER_CRYPTO_FIPSMODULE_EC_INTERNAL_H |
| 17 | #define OPENSSL_HEADER_CRYPTO_FIPSMODULE_EC_INTERNAL_H |
| 18 |
| 19 | #include <openssl/base.h> |
| 20 |
| 21 | #include <assert.h> |
| 22 |
| 23 | #include <openssl/bn.h> |
| 24 | #include <openssl/ec.h> |
| 25 | #include <openssl/ex_data.h> |
| 26 |
| 27 | #include "../bn/internal.h" |
| 28 |
| 29 | #if defined(__cplusplus) |
| 30 | extern "C" { |
| 31 | #endif |
| 32 |
| 33 |
| 34 | // EC internals. |
| 35 |
| 36 |
| 37 | // Cap the size of all field elements and scalars, including custom curves, to |
| 38 | // 66 bytes, large enough to fit secp521r1 and brainpoolP512r1, which appear to |
| 39 | // be the largest fields anyone plausibly uses. |
| 40 | #define EC_MAX_BYTES 66 |
| 41 | #define EC_MAX_WORDS ((EC_MAX_BYTES + BN_BYTES - 1) / BN_BYTES) |
| 42 | #define EC_MAX_COMPRESSED (EC_MAX_BYTES + 1) |
| 43 | #define EC_MAX_UNCOMPRESSED (2 * EC_MAX_BYTES + 1) |
| 44 |
| 45 | static_assert(EC_MAX_WORDS <= BN_SMALL_MAX_WORDS, |
| 46 | "bn_*_small functions not usable"); |
| 47 |
| 48 |
| 49 | // Scalars. |
| 50 |
| 51 | // An EC_SCALAR is an integer fully reduced modulo the order. Only the first |
| 52 | // |order->width| words are used. An |EC_SCALAR| is specific to an |EC_GROUP| |
| 53 | // and must not be mixed between groups. |
| 54 | typedef struct { |
| 55 | BN_ULONG words[EC_MAX_WORDS]; |
| 56 | } EC_SCALAR; |
| 57 |
| 58 | // ec_bignum_to_scalar converts |in| to an |EC_SCALAR| and writes it to |
| 59 | // |*out|. It returns one on success and zero if |in| is out of range. |
| 60 | OPENSSL_EXPORT int ec_bignum_to_scalar(const EC_GROUP *group, EC_SCALAR *out, |
| 61 | const BIGNUM *in); |
| 62 |
| 63 | // ec_scalar_to_bytes serializes |in| as a big-endian bytestring to |out| and |
| 64 | // sets |*out_len| to the number of bytes written. The number of bytes written |
| 65 | // is |BN_num_bytes(&group->order)|, which is at most |EC_MAX_BYTES|. |
| 66 | OPENSSL_EXPORT void ec_scalar_to_bytes(const EC_GROUP *group, uint8_t *out, |
| 67 | size_t *out_len, const EC_SCALAR *in); |
| 68 |
| 69 | // ec_scalar_from_bytes deserializes |in| and stores the resulting scalar over |
| 70 | // group |group| to |out|. It returns one on success and zero if |in| is |
| 71 | // invalid. |
| 72 | OPENSSL_EXPORT int ec_scalar_from_bytes(const EC_GROUP *group, EC_SCALAR *out, |
| 73 | const uint8_t *in, size_t len); |
| 74 |
| 75 | // ec_scalar_reduce sets |out| to |words|, reduced modulo the group order. |
| 76 | // |words| must be less than order^2. |num| must be at most twice the width of |
| 77 | // group order. This function treats |words| as secret. |
| 78 | void ec_scalar_reduce(const EC_GROUP *group, EC_SCALAR *out, |
| 79 | const BN_ULONG *words, size_t num); |
| 80 |
| 81 | // ec_random_nonzero_scalar sets |out| to a uniformly selected random value from |
| 82 | // zero to |group->order| - 1. It returns one on success and zero on error. |
| 83 | int ec_random_scalar(const EC_GROUP *group, EC_SCALAR *out, |
| 84 | const uint8_t additional_data[32]); |
| 85 |
| 86 | // ec_random_nonzero_scalar sets |out| to a uniformly selected random value from |
| 87 | // 1 to |group->order| - 1. It returns one on success and zero on error. |
| 88 | int ec_random_nonzero_scalar(const EC_GROUP *group, EC_SCALAR *out, |
| 89 | const uint8_t additional_data[32]); |
| 90 |
| 91 | // ec_scalar_equal_vartime returns one if |a| and |b| are equal and zero |
| 92 | // otherwise. Both values are treated as public. |
| 93 | int ec_scalar_equal_vartime(const EC_GROUP *group, const EC_SCALAR *a, |
| 94 | const EC_SCALAR *b); |
| 95 |
| 96 | // ec_scalar_is_zero returns one if |a| is zero and zero otherwise. |
| 97 | int ec_scalar_is_zero(const EC_GROUP *group, const EC_SCALAR *a); |
| 98 |
| 99 | // ec_scalar_add sets |r| to |a| + |b|. |
| 100 | void ec_scalar_add(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a, |
| 101 | const EC_SCALAR *b); |
| 102 |
| 103 | // ec_scalar_sub sets |r| to |a| - |b|. |
| 104 | void ec_scalar_sub(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a, |
| 105 | const EC_SCALAR *b); |
| 106 |
| 107 | // ec_scalar_neg sets |r| to -|a|. |
| 108 | void ec_scalar_neg(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a); |
| 109 |
| 110 | // ec_scalar_to_montgomery sets |r| to |a| in Montgomery form. |
| 111 | void ec_scalar_to_montgomery(const EC_GROUP *group, EC_SCALAR *r, |
| 112 | const EC_SCALAR *a); |
| 113 |
| 114 | // ec_scalar_to_montgomery sets |r| to |a| converted from Montgomery form. |
| 115 | void ec_scalar_from_montgomery(const EC_GROUP *group, EC_SCALAR *r, |
| 116 | const EC_SCALAR *a); |
| 117 |
| 118 | // ec_scalar_mul_montgomery sets |r| to |a| * |b| where inputs and outputs are |
| 119 | // in Montgomery form. |
| 120 | void ec_scalar_mul_montgomery(const EC_GROUP *group, EC_SCALAR *r, |
| 121 | const EC_SCALAR *a, const EC_SCALAR *b); |
| 122 |
| 123 | // ec_scalar_inv0_montgomery sets |r| to |a|^-1 where inputs and outputs are in |
| 124 | // Montgomery form. If |a| is zero, |r| is set to zero. |
| 125 | void ec_scalar_inv0_montgomery(const EC_GROUP *group, EC_SCALAR *r, |
| 126 | const EC_SCALAR *a); |
| 127 |
| 128 | // ec_scalar_to_montgomery_inv_vartime sets |r| to |a|^-1 R. That is, it takes |
| 129 | // in |a| not in Montgomery form and computes the inverse in Montgomery form. It |
| 130 | // returns one on success and zero if |a| has no inverse. This function assumes |
| 131 | // |a| is public and may leak information about it via timing. |
| 132 | // |
| 133 | // Note this is not the same operation as |ec_scalar_inv0_montgomery|. |
| 134 | int ec_scalar_to_montgomery_inv_vartime(const EC_GROUP *group, EC_SCALAR *r, |
| 135 | const EC_SCALAR *a); |
| 136 |
| 137 | // ec_scalar_select, in constant time, sets |out| to |a| if |mask| is all ones |
| 138 | // and |b| if |mask| is all zeros. |
| 139 | void ec_scalar_select(const EC_GROUP *group, EC_SCALAR *out, BN_ULONG mask, |
| 140 | const EC_SCALAR *a, const EC_SCALAR *b); |
| 141 |
| 142 |
| 143 | // Field elements. |
| 144 |
| 145 | // An EC_FELEM represents a field element. Only the first |field->width| words |
| 146 | // are used. An |EC_FELEM| is specific to an |EC_GROUP| and must not be mixed |
| 147 | // between groups. Additionally, the representation (whether or not elements are |
| 148 | // represented in Montgomery-form) may vary between |EC_METHOD|s. |
| 149 | typedef struct { |
| 150 | BN_ULONG words[EC_MAX_WORDS]; |
| 151 | } EC_FELEM; |
| 152 |
| 153 | // ec_felem_one returns one in |group|'s field. |
| 154 | const EC_FELEM *ec_felem_one(const EC_GROUP *group); |
| 155 |
| 156 | // ec_bignum_to_felem converts |in| to an |EC_FELEM|. It returns one on success |
| 157 | // and zero if |in| is out of range. |
| 158 | int ec_bignum_to_felem(const EC_GROUP *group, EC_FELEM *out, const BIGNUM *in); |
| 159 |
| 160 | // ec_felem_to_bignum converts |in| to a |BIGNUM|. It returns one on success and |
| 161 | // zero on allocation failure. |
| 162 | int ec_felem_to_bignum(const EC_GROUP *group, BIGNUM *out, const EC_FELEM *in); |
| 163 |
| 164 | // ec_felem_to_bytes serializes |in| as a big-endian bytestring to |out| and |
| 165 | // sets |*out_len| to the number of bytes written. The number of bytes written |
| 166 | // is |BN_num_bytes(&group->order)|, which is at most |EC_MAX_BYTES|. |
| 167 | void ec_felem_to_bytes(const EC_GROUP *group, uint8_t *out, size_t *out_len, |
| 168 | const EC_FELEM *in); |
| 169 |
| 170 | // ec_felem_from_bytes deserializes |in| and stores the resulting field element |
| 171 | // to |out|. It returns one on success and zero if |in| is invalid. |
| 172 | int ec_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out, const uint8_t *in, |
| 173 | size_t len); |
| 174 |
| 175 | // ec_felem_neg sets |out| to -|a|. |
| 176 | void ec_felem_neg(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a); |
| 177 |
| 178 | // ec_felem_add sets |out| to |a| + |b|. |
| 179 | void ec_felem_add(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, |
| 180 | const EC_FELEM *b); |
| 181 |
| 182 | // ec_felem_add sets |out| to |a| - |b|. |
| 183 | void ec_felem_sub(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, |
| 184 | const EC_FELEM *b); |
| 185 |
| 186 | // ec_felem_non_zero_mask returns all ones if |a| is non-zero and all zeros |
| 187 | // otherwise. |
| 188 | BN_ULONG ec_felem_non_zero_mask(const EC_GROUP *group, const EC_FELEM *a); |
| 189 |
| 190 | // ec_felem_select, in constant time, sets |out| to |a| if |mask| is all ones |
| 191 | // and |b| if |mask| is all zeros. |
| 192 | void ec_felem_select(const EC_GROUP *group, EC_FELEM *out, BN_ULONG mask, |
| 193 | const EC_FELEM *a, const EC_FELEM *b); |
| 194 |
| 195 | // ec_felem_equal returns one if |a| and |b| are equal and zero otherwise. |
| 196 | int ec_felem_equal(const EC_GROUP *group, const EC_FELEM *a, const EC_FELEM *b); |
| 197 |
| 198 |
| 199 | // Points. |
| 200 | // |
| 201 | // Points may represented in affine coordinates as |EC_AFFINE| or Jacobian |
| 202 | // coordinates as |EC_JACOBIAN|. Affine coordinates directly represent a |
| 203 | // point on the curve, but point addition over affine coordinates requires |
| 204 | // costly field inversions, so arithmetic is done in Jacobian coordinates. |
| 205 | // Converting from affine to Jacobian is cheap, while converting from Jacobian |
| 206 | // to affine costs a field inversion. (Jacobian coordinates amortize the field |
| 207 | // inversions needed in a sequence of point operations.) |
| 208 |
| 209 | // An EC_JACOBIAN represents an elliptic curve point in Jacobian coordinates. |
| 210 | // Unlike |EC_POINT|, it is a plain struct which can be stack-allocated and |
| 211 | // needs no cleanup. It is specific to an |EC_GROUP| and must not be mixed |
| 212 | // between groups. |
| 213 | typedef struct { |
| 214 | // X, Y, and Z are Jacobian projective coordinates. They represent |
| 215 | // (X/Z^2, Y/Z^3) if Z != 0 and the point at infinity otherwise. |
| 216 | EC_FELEM X, Y, Z; |
| 217 | } EC_JACOBIAN; |
| 218 |
| 219 | // An EC_AFFINE represents an elliptic curve point in affine coordinates. |
| 220 | // coordinates. Note the point at infinity cannot be represented in affine |
| 221 | // coordinates. |
| 222 | typedef struct { |
| 223 | EC_FELEM X, Y; |
| 224 | } EC_AFFINE; |
| 225 |
| 226 | // ec_affine_to_jacobian converts |p| to Jacobian form and writes the result to |
| 227 | // |*out|. This operation is very cheap and only costs a few copies. |
| 228 | void ec_affine_to_jacobian(const EC_GROUP *group, EC_JACOBIAN *out, |
| 229 | const EC_AFFINE *p); |
| 230 |
| 231 | // ec_jacobian_to_affine converts |p| to affine form and writes the result to |
| 232 | // |*out|. It returns one on success and zero if |p| was the point at infinity. |
| 233 | // This operation performs a field inversion and should only be done once per |
| 234 | // point. |
| 235 | // |
| 236 | // If only extracting the x-coordinate, use |ec_get_x_coordinate_*| which is |
| 237 | // slightly faster. |
| 238 | OPENSSL_EXPORT int ec_jacobian_to_affine(const EC_GROUP *group, EC_AFFINE *out, |
| 239 | const EC_JACOBIAN *p); |
| 240 |
| 241 | // ec_jacobian_to_affine_batch converts |num| points in |in| from Jacobian |
| 242 | // coordinates to affine coordinates and writes the results to |out|. It returns |
| 243 | // one on success and zero if any of the input points were infinity. |
| 244 | // |
| 245 | // This function is not implemented for all curves. Add implementations as |
| 246 | // needed. |
| 247 | int ec_jacobian_to_affine_batch(const EC_GROUP *group, EC_AFFINE *out, |
| 248 | const EC_JACOBIAN *in, size_t num); |
| 249 |
| 250 | // ec_point_set_affine_coordinates sets |out|'s to a point with affine |
| 251 | // coordinates |x| and |y|. It returns one if the point is on the curve and |
| 252 | // zero otherwise. If the point is not on the curve, the value of |out| is |
| 253 | // undefined. |
| 254 | int ec_point_set_affine_coordinates(const EC_GROUP *group, EC_AFFINE *out, |
| 255 | const EC_FELEM *x, const EC_FELEM *y); |
| 256 |
| 257 | // ec_point_mul_no_self_test does the same as |EC_POINT_mul|, but doesn't try to |
| 258 | // run the self-test first. This is for use in the self tests themselves, to |
| 259 | // prevent an infinite loop. |
| 260 | int ec_point_mul_no_self_test(const EC_GROUP *group, EC_POINT *r, |
| 261 | const BIGNUM *g_scalar, const EC_POINT *p, |
| 262 | const BIGNUM *p_scalar, BN_CTX *ctx); |
| 263 |
| 264 | // ec_point_mul_scalar sets |r| to |p| * |scalar|. Both inputs are considered |
| 265 | // secret. |
| 266 | int ec_point_mul_scalar(const EC_GROUP *group, EC_JACOBIAN *r, |
| 267 | const EC_JACOBIAN *p, const EC_SCALAR *scalar); |
| 268 |
| 269 | // ec_point_mul_scalar_base sets |r| to generator * |scalar|. |scalar| is |
| 270 | // treated as secret. |
| 271 | int ec_point_mul_scalar_base(const EC_GROUP *group, EC_JACOBIAN *r, |
| 272 | const EC_SCALAR *scalar); |
| 273 |
| 274 | // ec_point_mul_scalar_batch sets |r| to |p0| * |scalar0| + |p1| * |scalar1| + |
| 275 | // |p2| * |scalar2|. |p2| may be NULL to skip that term. |
| 276 | // |
| 277 | // The inputs are treated as secret, however, this function leaks information |
| 278 | // about whether intermediate computations add a point to itself. Callers must |
| 279 | // ensure that discrete logs between |p0|, |p1|, and |p2| are uniformly |
| 280 | // distributed and independent of the scalars, which should be uniformly |
| 281 | // selected and not under the attackers control. This ensures the doubling case |
| 282 | // will occur with negligible probability. |
| 283 | // |
| 284 | // This function is not implemented for all curves. Add implementations as |
| 285 | // needed. |
| 286 | // |
| 287 | // TODO(davidben): This function does not use base point tables. For now, it is |
| 288 | // only used with the generic |EC_GFp_mont_method| implementation which has |
| 289 | // none. If generalizing to tuned curves, this may be useful. However, we still |
| 290 | // must double up to the least efficient input, so precomputed tables can only |
| 291 | // save table setup and allow a wider window size. |
| 292 | int ec_point_mul_scalar_batch(const EC_GROUP *group, EC_JACOBIAN *r, |
| 293 | const EC_JACOBIAN *p0, const EC_SCALAR *scalar0, |
| 294 | const EC_JACOBIAN *p1, const EC_SCALAR *scalar1, |
| 295 | const EC_JACOBIAN *p2, const EC_SCALAR *scalar2); |
| 296 |
| 297 | #define EC_MONT_PRECOMP_COMB_SIZE 5 |
| 298 |
| 299 | // An |EC_PRECOMP| stores precomputed information about a point, to optimize |
| 300 | // repeated multiplications involving it. It is a union so different |
| 301 | // |EC_METHOD|s can store different information in it. |
| 302 | typedef union { |
| 303 | EC_AFFINE comb[(1 << EC_MONT_PRECOMP_COMB_SIZE) - 1]; |
| 304 | } EC_PRECOMP; |
| 305 |
| 306 | // ec_init_precomp precomputes multiples of |p| and writes the result to |out|. |
| 307 | // It returns one on success and zero on error. The resulting table may be used |
| 308 | // with |ec_point_mul_scalar_precomp|. This function will fail if |p| is the |
| 309 | // point at infinity. |
| 310 | // |
| 311 | // This function is not implemented for all curves. Add implementations as |
| 312 | // needed. |
| 313 | int ec_init_precomp(const EC_GROUP *group, EC_PRECOMP *out, |
| 314 | const EC_JACOBIAN *p); |
| 315 |
| 316 | // ec_point_mul_scalar_precomp sets |r| to |p0| * |scalar0| + |p1| * |scalar1| + |
| 317 | // |p2| * |scalar2|. |p1| or |p2| may be NULL to skip the corresponding term. |
| 318 | // The points are represented as |EC_PRECOMP| and must be initialized with |
| 319 | // |ec_init_precomp|. This function runs faster than |ec_point_mul_scalar_batch| |
| 320 | // but requires setup work per input point, so it is only appropriate for points |
| 321 | // which are used frequently. |
| 322 | // |
| 323 | // The inputs are treated as secret, however, this function leaks information |
| 324 | // about whether intermediate computations add a point to itself. Callers must |
| 325 | // ensure that discrete logs between |p0|, |p1|, and |p2| are uniformly |
| 326 | // distributed and independent of the scalars, which should be uniformly |
| 327 | // selected and not under the attackers control. This ensures the doubling case |
| 328 | // will occur with negligible probability. |
| 329 | // |
| 330 | // This function is not implemented for all curves. Add implementations as |
| 331 | // needed. |
| 332 | // |
| 333 | // TODO(davidben): This function does not use base point tables. For now, it is |
| 334 | // only used with the generic |EC_GFp_mont_method| implementation which has |
| 335 | // none. If generalizing to tuned curves, we should add a parameter for the base |
| 336 | // point and arrange for the generic implementation to have base point tables |
| 337 | // available. |
| 338 | int ec_point_mul_scalar_precomp(const EC_GROUP *group, EC_JACOBIAN *r, |
| 339 | const EC_PRECOMP *p0, const EC_SCALAR *scalar0, |
| 340 | const EC_PRECOMP *p1, const EC_SCALAR *scalar1, |
| 341 | const EC_PRECOMP *p2, const EC_SCALAR *scalar2); |
| 342 |
| 343 | // ec_point_mul_scalar_public sets |r| to |
| 344 | // generator * |g_scalar| + |p| * |p_scalar|. It assumes that the inputs are |
| 345 | // public so there is no concern about leaking their values through timing. |
| 346 | OPENSSL_EXPORT int ec_point_mul_scalar_public(const EC_GROUP *group, |
| 347 | EC_JACOBIAN *r, |
| 348 | const EC_SCALAR *g_scalar, |
| 349 | const EC_JACOBIAN *p, |
| 350 | const EC_SCALAR *p_scalar); |
| 351 |
| 352 | // ec_point_mul_scalar_public_batch sets |r| to the sum of generator * |
| 353 | // |g_scalar| and |points[i]| * |scalars[i]| where |points| and |scalars| have |
| 354 | // |num| elements. It assumes that the inputs are public so there is no concern |
| 355 | // about leaking their values through timing. |g_scalar| may be NULL to skip |
| 356 | // that term. |
| 357 | // |
| 358 | // This function is not implemented for all curves. Add implementations as |
| 359 | // needed. |
| 360 | int ec_point_mul_scalar_public_batch(const EC_GROUP *group, EC_JACOBIAN *r, |
| 361 | const EC_SCALAR *g_scalar, |
| 362 | const EC_JACOBIAN *points, |
| 363 | const EC_SCALAR *scalars, size_t num); |
| 364 |
| 365 | // ec_point_select, in constant time, sets |out| to |a| if |mask| is all ones |
| 366 | // and |b| if |mask| is all zeros. |
| 367 | void ec_point_select(const EC_GROUP *group, EC_JACOBIAN *out, BN_ULONG mask, |
| 368 | const EC_JACOBIAN *a, const EC_JACOBIAN *b); |
| 369 |
| 370 | // ec_affine_select behaves like |ec_point_select| but acts on affine points. |
| 371 | void ec_affine_select(const EC_GROUP *group, EC_AFFINE *out, BN_ULONG mask, |
| 372 | const EC_AFFINE *a, const EC_AFFINE *b); |
| 373 |
| 374 | // ec_precomp_select behaves like |ec_point_select| but acts on |EC_PRECOMP|. |
| 375 | void ec_precomp_select(const EC_GROUP *group, EC_PRECOMP *out, BN_ULONG mask, |
| 376 | const EC_PRECOMP *a, const EC_PRECOMP *b); |
| 377 |
| 378 | // ec_cmp_x_coordinate compares the x (affine) coordinate of |p|, mod the group |
| 379 | // order, with |r|. It returns one if the values match and zero if |p| is the |
| 380 | // point at infinity of the values do not match. |p| is treated as public. |
| 381 | int ec_cmp_x_coordinate(const EC_GROUP *group, const EC_JACOBIAN *p, |
| 382 | const EC_SCALAR *r); |
| 383 |
| 384 | // ec_get_x_coordinate_as_scalar sets |*out| to |p|'s x-coordinate, modulo |
| 385 | // |group->order|. It returns one on success and zero if |p| is the point at |
| 386 | // infinity. |
| 387 | int ec_get_x_coordinate_as_scalar(const EC_GROUP *group, EC_SCALAR *out, |
| 388 | const EC_JACOBIAN *p); |
| 389 |
| 390 | // ec_get_x_coordinate_as_bytes writes |p|'s affine x-coordinate to |out|, which |
| 391 | // must have at must |max_out| bytes. It sets |*out_len| to the number of bytes |
| 392 | // written. The value is written big-endian and zero-padded to the size of the |
| 393 | // field. This function returns one on success and zero on failure. |
| 394 | int ec_get_x_coordinate_as_bytes(const EC_GROUP *group, uint8_t *out, |
| 395 | size_t *out_len, size_t max_out, |
| 396 | const EC_JACOBIAN *p); |
| 397 |
| 398 | // ec_point_byte_len returns the number of bytes in the byte representation of |
| 399 | // a non-infinity point in |group|, encoded according to |form|, or zero if |
| 400 | // |form| is invalid. |
| 401 | size_t ec_point_byte_len(const EC_GROUP *group, point_conversion_form_t form); |
| 402 |
| 403 | // ec_point_to_bytes encodes |point| according to |form| and writes the result |
| 404 | // |buf|. It returns the size of the output on success or zero on error. At most |
| 405 | // |max_out| bytes will be written. The buffer should be at least |
| 406 | // |ec_point_byte_len| long to guarantee success. |
| 407 | size_t ec_point_to_bytes(const EC_GROUP *group, const EC_AFFINE *point, |
| 408 | point_conversion_form_t form, uint8_t *buf, |
| 409 | size_t max_out); |
| 410 |
| 411 | // ec_point_from_uncompressed parses |in| as a point in uncompressed form and |
| 412 | // sets the result to |out|. It returns one on success and zero if the input was |
| 413 | // invalid. |
| 414 | int ec_point_from_uncompressed(const EC_GROUP *group, EC_AFFINE *out, |
| 415 | const uint8_t *in, size_t len); |
| 416 |
| 417 | // ec_set_to_safe_point sets |out| to an arbitrary point on |group|, either the |
| 418 | // generator or the point at infinity. This is used to guard against callers of |
| 419 | // external APIs not checking the return value. |
| 420 | void ec_set_to_safe_point(const EC_GROUP *group, EC_JACOBIAN *out); |
| 421 |
| 422 | // ec_affine_jacobian_equal returns one if |a| and |b| represent the same point |
| 423 | // and zero otherwise. It treats both inputs as secret. |
| 424 | int ec_affine_jacobian_equal(const EC_GROUP *group, const EC_AFFINE *a, |
| 425 | const EC_JACOBIAN *b); |
| 426 |
| 427 |
| 428 | // Implementation details. |
| 429 |
| 430 | struct ec_method_st { |
| 431 | // point_get_affine_coordinates sets |*x| and |*y| to the affine coordinates |
| 432 | // of |p|. Either |x| or |y| may be NULL to omit it. It returns one on success |
| 433 | // and zero if |p| is the point at infinity. It leaks whether |p| was the |
| 434 | // point at infinity, but otherwise treats |p| as secret. |
| 435 | int (*point_get_affine_coordinates)(const EC_GROUP *, const EC_JACOBIAN *p, |
| 436 | EC_FELEM *x, EC_FELEM *y); |
| 437 |
| 438 | // jacobian_to_affine_batch implements |ec_jacobian_to_affine_batch|. |
| 439 | int (*jacobian_to_affine_batch)(const EC_GROUP *group, EC_AFFINE *out, |
| 440 | const EC_JACOBIAN *in, size_t num); |
| 441 |
| 442 | // add sets |r| to |a| + |b|. |
| 443 | void (*add)(const EC_GROUP *group, EC_JACOBIAN *r, const EC_JACOBIAN *a, |
| 444 | const EC_JACOBIAN *b); |
| 445 | // dbl sets |r| to |a| + |a|. |
| 446 | void (*dbl)(const EC_GROUP *group, EC_JACOBIAN *r, const EC_JACOBIAN *a); |
| 447 |
| 448 | // mul sets |r| to |scalar|*|p|. |
| 449 | void (*mul)(const EC_GROUP *group, EC_JACOBIAN *r, const EC_JACOBIAN *p, |
| 450 | const EC_SCALAR *scalar); |
| 451 | // mul_base sets |r| to |scalar|*generator. |
| 452 | void (*mul_base)(const EC_GROUP *group, EC_JACOBIAN *r, |
| 453 | const EC_SCALAR *scalar); |
| 454 | // mul_batch implements |ec_mul_scalar_batch|. |
| 455 | void (*mul_batch)(const EC_GROUP *group, EC_JACOBIAN *r, |
| 456 | const EC_JACOBIAN *p0, const EC_SCALAR *scalar0, |
| 457 | const EC_JACOBIAN *p1, const EC_SCALAR *scalar1, |
| 458 | const EC_JACOBIAN *p2, const EC_SCALAR *scalar2); |
| 459 | // mul_public sets |r| to |g_scalar|*generator + |p_scalar|*|p|. It assumes |
| 460 | // that the inputs are public so there is no concern about leaking their |
| 461 | // values through timing. |
| 462 | // |
| 463 | // This function may be omitted if |mul_public_batch| is provided. |
| 464 | void (*mul_public)(const EC_GROUP *group, EC_JACOBIAN *r, |
| 465 | const EC_SCALAR *g_scalar, const EC_JACOBIAN *p, |
| 466 | const EC_SCALAR *p_scalar); |
| 467 | // mul_public_batch implements |ec_point_mul_scalar_public_batch|. |
| 468 | int (*mul_public_batch)(const EC_GROUP *group, EC_JACOBIAN *r, |
| 469 | const EC_SCALAR *g_scalar, const EC_JACOBIAN *points, |
| 470 | const EC_SCALAR *scalars, size_t num); |
| 471 |
| 472 | // init_precomp implements |ec_init_precomp|. |
| 473 | int (*init_precomp)(const EC_GROUP *group, EC_PRECOMP *out, |
| 474 | const EC_JACOBIAN *p); |
| 475 | // mul_precomp implements |ec_point_mul_scalar_precomp|. |
| 476 | void (*mul_precomp)(const EC_GROUP *group, EC_JACOBIAN *r, |
| 477 | const EC_PRECOMP *p0, const EC_SCALAR *scalar0, |
| 478 | const EC_PRECOMP *p1, const EC_SCALAR *scalar1, |
| 479 | const EC_PRECOMP *p2, const EC_SCALAR *scalar2); |
| 480 |
| 481 | // felem_mul and felem_sqr implement multiplication and squaring, |
| 482 | // respectively, so that the generic |EC_POINT_add| and |EC_POINT_dbl| |
| 483 | // implementations can work both with |EC_GFp_mont_method| and the tuned |
| 484 | // operations. |
| 485 | // |
| 486 | // TODO(davidben): This constrains |EC_FELEM|'s internal representation, adds |
| 487 | // many indirect calls in the middle of the generic code, and a bunch of |
| 488 | // conversions. If p224-64.c were easily convertable to Montgomery form, we |
| 489 | // could say |EC_FELEM| is always in Montgomery form. If we routed the rest of |
| 490 | // simple.c to |EC_METHOD|, we could give |EC_POINT| an |EC_METHOD|-specific |
| 491 | // representation and say |EC_FELEM| is purely a |EC_GFp_mont_method| type. |
| 492 | void (*felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a, |
| 493 | const EC_FELEM *b); |
| 494 | void (*felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a); |
| 495 |
| 496 | void (*felem_to_bytes)(const EC_GROUP *group, uint8_t *out, size_t *out_len, |
| 497 | const EC_FELEM *in); |
| 498 | int (*felem_from_bytes)(const EC_GROUP *group, EC_FELEM *out, |
| 499 | const uint8_t *in, size_t len); |
| 500 |
| 501 | // felem_reduce sets |out| to |words|, reduced modulo the field size, p. |
| 502 | // |words| must be less than p^2. |num| must be at most twice the width of p. |
| 503 | // This function treats |words| as secret. |
| 504 | // |
| 505 | // This function is only used in hash-to-curve and may be omitted in curves |
| 506 | // that do not support it. |
| 507 | void (*felem_reduce)(const EC_GROUP *group, EC_FELEM *out, |
| 508 | const BN_ULONG *words, size_t num); |
| 509 |
| 510 | // felem_exp sets |out| to |a|^|exp|. It treats |a| is secret but |exp| as |
| 511 | // public. |
| 512 | // |
| 513 | // This function is used in hash-to-curve and may be NULL in curves not used |
| 514 | // with hash-to-curve. |
| 515 | // |
| 516 | // TODO(https://crbug.com/boringssl/567): hash-to-curve uses this as part of |
| 517 | // computing a square root, which is what compressed coordinates ultimately |
| 518 | // needs to avoid |BIGNUM|. Can we unify this a bit? By generalizing to |
| 519 | // arbitrary exponentiation, we also miss an opportunity to use a specialized |
| 520 | // addition chain. |
| 521 | void (*felem_exp)(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a, |
| 522 | const BN_ULONG *exp, size_t num_exp); |
| 523 |
| 524 | // scalar_inv0_montgomery implements |ec_scalar_inv0_montgomery|. |
| 525 | void (*scalar_inv0_montgomery)(const EC_GROUP *group, EC_SCALAR *out, |
| 526 | const EC_SCALAR *in); |
| 527 |
| 528 | // scalar_to_montgomery_inv_vartime implements |
| 529 | // |ec_scalar_to_montgomery_inv_vartime|. |
| 530 | int (*scalar_to_montgomery_inv_vartime)(const EC_GROUP *group, EC_SCALAR *out, |
| 531 | const EC_SCALAR *in); |
| 532 |
| 533 | // cmp_x_coordinate compares the x (affine) coordinate of |p|, mod the group |
| 534 | // order, with |r|. It returns one if the values match and zero if |p| is the |
| 535 | // point at infinity of the values do not match. |
| 536 | int (*cmp_x_coordinate)(const EC_GROUP *group, const EC_JACOBIAN *p, |
| 537 | const EC_SCALAR *r); |
| 538 | } /* EC_METHOD */; |
| 539 |
| 540 | const EC_METHOD *EC_GFp_mont_method(void); |
| 541 |
| 542 | struct ec_point_st { |
| 543 | // group is an owning reference to |group|, unless this is |
| 544 | // |group->generator|. |
| 545 | EC_GROUP *group; |
| 546 | // raw is the group-specific point data. Functions that take |EC_POINT| |
| 547 | // typically check consistency with |EC_GROUP| while functions that take |
| 548 | // |EC_JACOBIAN| do not. Thus accesses to this field should be externally |
| 549 | // checked for consistency. |
| 550 | EC_JACOBIAN raw; |
| 551 | } /* EC_POINT */; |
| 552 |
| 553 | struct ec_group_st { |
| 554 | const EC_METHOD *meth; |
| 555 |
| 556 | // Unlike all other |EC_POINT|s, |generator| does not own |generator->group| |
| 557 | // to avoid a reference cycle. Additionally, Z is guaranteed to be one, so X |
| 558 | // and Y are suitable for use as an |EC_AFFINE|. Before |has_order| is set, Z |
| 559 | // is one, but X and Y are uninitialized. |
| 560 | EC_POINT generator; |
| 561 |
| 562 | BN_MONT_CTX order; |
| 563 | BN_MONT_CTX field; |
| 564 |
| 565 | EC_FELEM a, b; // Curve coefficients. |
| 566 |
| 567 | // comment is a human-readable string describing the curve. |
| 568 | const char *comment; |
| 569 |
| 570 | int curve_name; // optional NID for named curve |
| 571 | uint8_t oid[9]; |
| 572 | uint8_t oid_len; |
| 573 |
| 574 | // a_is_minus3 is one if |a| is -3 mod |field| and zero otherwise. Point |
| 575 | // arithmetic is optimized for -3. |
| 576 | int a_is_minus3; |
| 577 |
| 578 | // has_order is one if |generator| and |order| have been initialized. |
| 579 | int has_order; |
| 580 |
| 581 | // field_greater_than_order is one if |field| is greate than |order| and zero |
| 582 | // otherwise. |
| 583 | int field_greater_than_order; |
| 584 |
| 585 | CRYPTO_refcount_t references; |
| 586 | } /* EC_GROUP */; |
| 587 |
| 588 | EC_GROUP *ec_group_new(const EC_METHOD *meth, const BIGNUM *p, const BIGNUM *a, |
| 589 | const BIGNUM *b, BN_CTX *ctx); |
| 590 |
| 591 | void ec_GFp_mont_mul(const EC_GROUP *group, EC_JACOBIAN *r, |
| 592 | const EC_JACOBIAN *p, const EC_SCALAR *scalar); |
| 593 | void ec_GFp_mont_mul_base(const EC_GROUP *group, EC_JACOBIAN *r, |
| 594 | const EC_SCALAR *scalar); |
| 595 | void ec_GFp_mont_mul_batch(const EC_GROUP *group, EC_JACOBIAN *r, |
| 596 | const EC_JACOBIAN *p0, const EC_SCALAR *scalar0, |
| 597 | const EC_JACOBIAN *p1, const EC_SCALAR *scalar1, |
| 598 | const EC_JACOBIAN *p2, const EC_SCALAR *scalar2); |
| 599 | int ec_GFp_mont_init_precomp(const EC_GROUP *group, EC_PRECOMP *out, |
| 600 | const EC_JACOBIAN *p); |
| 601 | void ec_GFp_mont_mul_precomp(const EC_GROUP *group, EC_JACOBIAN *r, |
| 602 | const EC_PRECOMP *p0, const EC_SCALAR *scalar0, |
| 603 | const EC_PRECOMP *p1, const EC_SCALAR *scalar1, |
| 604 | const EC_PRECOMP *p2, const EC_SCALAR *scalar2); |
| 605 | void ec_GFp_mont_felem_reduce(const EC_GROUP *group, EC_FELEM *out, |
| 606 | const BN_ULONG *words, size_t num); |
| 607 | void ec_GFp_mont_felem_exp(const EC_GROUP *group, EC_FELEM *out, |
| 608 | const EC_FELEM *a, const BN_ULONG *exp, |
| 609 | size_t num_exp); |
| 610 |
| 611 | // ec_compute_wNAF writes the modified width-(w+1) Non-Adjacent Form (wNAF) of |
| 612 | // |scalar| to |out|. |out| must have room for |bits| + 1 elements, each of |
| 613 | // which will be either zero or odd with an absolute value less than 2^w |
| 614 | // satisfying |
| 615 | // scalar = \sum_j out[j]*2^j |
| 616 | // where at most one of any w+1 consecutive digits is non-zero |
| 617 | // with the exception that the most significant digit may be only |
| 618 | // w-1 zeros away from that next non-zero digit. |
| 619 | void ec_compute_wNAF(const EC_GROUP *group, int8_t *out, |
| 620 | const EC_SCALAR *scalar, size_t bits, int w); |
| 621 |
| 622 | int ec_GFp_mont_mul_public_batch(const EC_GROUP *group, EC_JACOBIAN *r, |
| 623 | const EC_SCALAR *g_scalar, |
| 624 | const EC_JACOBIAN *points, |
| 625 | const EC_SCALAR *scalars, size_t num); |
| 626 |
| 627 | // method functions in simple.c |
| 628 | int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a, |
| 629 | const BIGNUM *b, BN_CTX *); |
| 630 | int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a, |
| 631 | BIGNUM *b); |
| 632 | void ec_GFp_simple_point_init(EC_JACOBIAN *); |
| 633 | void ec_GFp_simple_point_copy(EC_JACOBIAN *, const EC_JACOBIAN *); |
| 634 | void ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_JACOBIAN *); |
| 635 | void ec_GFp_mont_add(const EC_GROUP *, EC_JACOBIAN *r, const EC_JACOBIAN *a, |
| 636 | const EC_JACOBIAN *b); |
| 637 | void ec_GFp_mont_dbl(const EC_GROUP *, EC_JACOBIAN *r, const EC_JACOBIAN *a); |
| 638 | void ec_GFp_simple_invert(const EC_GROUP *, EC_JACOBIAN *); |
| 639 | int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_JACOBIAN *); |
| 640 | int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_JACOBIAN *); |
| 641 | int ec_GFp_simple_points_equal(const EC_GROUP *, const EC_JACOBIAN *a, |
| 642 | const EC_JACOBIAN *b); |
| 643 | void ec_simple_scalar_inv0_montgomery(const EC_GROUP *group, EC_SCALAR *r, |
| 644 | const EC_SCALAR *a); |
| 645 |
| 646 | int ec_simple_scalar_to_montgomery_inv_vartime(const EC_GROUP *group, |
| 647 | EC_SCALAR *r, |
| 648 | const EC_SCALAR *a); |
| 649 |
| 650 | int ec_GFp_simple_cmp_x_coordinate(const EC_GROUP *group, const EC_JACOBIAN *p, |
| 651 | const EC_SCALAR *r); |
| 652 |
| 653 | void ec_GFp_simple_felem_to_bytes(const EC_GROUP *group, uint8_t *out, |
| 654 | size_t *out_len, const EC_FELEM *in); |
| 655 | int ec_GFp_simple_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out, |
| 656 | const uint8_t *in, size_t len); |
| 657 |
| 658 | // method functions in montgomery.c |
| 659 | void ec_GFp_mont_felem_mul(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a, |
| 660 | const EC_FELEM *b); |
| 661 | void ec_GFp_mont_felem_sqr(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a); |
| 662 |
| 663 | void ec_GFp_mont_felem_to_bytes(const EC_GROUP *group, uint8_t *out, |
| 664 | size_t *out_len, const EC_FELEM *in); |
| 665 | int ec_GFp_mont_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out, |
| 666 | const uint8_t *in, size_t len); |
| 667 |
| 668 | void ec_GFp_nistp_recode_scalar_bits(crypto_word_t *sign, crypto_word_t *digit, |
| 669 | crypto_word_t in); |
| 670 |
| 671 | const EC_METHOD *EC_GFp_nistp224_method(void); |
| 672 | const EC_METHOD *EC_GFp_nistp256_method(void); |
| 673 |
| 674 | // EC_GFp_nistz256_method is a GFp method using montgomery multiplication, with |
| 675 | // x86-64 optimized P256. See http://eprint.iacr.org/2013/816. |
| 676 | const EC_METHOD *EC_GFp_nistz256_method(void); |
| 677 |
| 678 | // An EC_WRAPPED_SCALAR is an |EC_SCALAR| with a parallel |BIGNUM| |
| 679 | // representation. It exists to support the |EC_KEY_get0_private_key| API. |
| 680 | typedef struct { |
| 681 | BIGNUM bignum; |
| 682 | EC_SCALAR scalar; |
| 683 | } EC_WRAPPED_SCALAR; |
| 684 |
| 685 | struct ec_key_st { |
| 686 | EC_GROUP *group; |
| 687 |
| 688 | // Ideally |pub_key| would be an |EC_AFFINE| so serializing it does not pay an |
| 689 | // inversion each time, but the |EC_KEY_get0_public_key| API implies public |
| 690 | // keys are stored in an |EC_POINT|-compatible form. |
| 691 | EC_POINT *pub_key; |
| 692 | EC_WRAPPED_SCALAR *priv_key; |
| 693 |
| 694 | unsigned int enc_flag; |
| 695 | point_conversion_form_t conv_form; |
| 696 |
| 697 | CRYPTO_refcount_t references; |
| 698 |
| 699 | ECDSA_METHOD *ecdsa_meth; |
| 700 |
| 701 | CRYPTO_EX_DATA ex_data; |
| 702 | } /* EC_KEY */; |
| 703 |
| 704 |
| 705 | #if defined(__cplusplus) |
| 706 | } // extern C |
| 707 | #endif |
| 708 |
| 709 | #endif // OPENSSL_HEADER_CRYPTO_FIPSMODULE_EC_INTERNAL_H |
| 710 |