Home | History | Annotate | Download | only in ec
      1 /* Originally written by Bodo Moeller for the OpenSSL project.
      2  * ====================================================================
      3  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  *
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in
     14  *    the documentation and/or other materials provided with the
     15  *    distribution.
     16  *
     17  * 3. All advertising materials mentioning features or use of this
     18  *    software must display the following acknowledgment:
     19  *    "This product includes software developed by the OpenSSL Project
     20  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
     21  *
     22  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
     23  *    endorse or promote products derived from this software without
     24  *    prior written permission. For written permission, please contact
     25  *    openssl-core (at) openssl.org.
     26  *
     27  * 5. Products derived from this software may not be called "OpenSSL"
     28  *    nor may "OpenSSL" appear in their names without prior written
     29  *    permission of the OpenSSL Project.
     30  *
     31  * 6. Redistributions of any form whatsoever must retain the following
     32  *    acknowledgment:
     33  *    "This product includes software developed by the OpenSSL Project
     34  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
     35  *
     36  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
     37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     39  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
     40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
     47  * OF THE POSSIBILITY OF SUCH DAMAGE.
     48  * ====================================================================
     49  *
     50  * This product includes cryptographic software written by Eric Young
     51  * (eay (at) cryptsoft.com).  This product includes software written by Tim
     52  * Hudson (tjh (at) cryptsoft.com).
     53  *
     54  */
     55 /* ====================================================================
     56  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
     57  *
     58  * Portions of the attached software ("Contribution") are developed by
     59  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
     60  *
     61  * The Contribution is licensed pursuant to the OpenSSL open source
     62  * license provided above.
     63  *
     64  * The elliptic curve binary polynomial software is originally written by
     65  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
     66  * Laboratories. */
     67 
     68 #ifndef OPENSSL_HEADER_EC_INTERNAL_H
     69 #define OPENSSL_HEADER_EC_INTERNAL_H
     70 
     71 #include <openssl/base.h>
     72 
     73 #include <openssl/bn.h>
     74 #include <openssl/ex_data.h>
     75 #include <openssl/thread.h>
     76 #include <openssl/type_check.h>
     77 
     78 #include "../bn/internal.h"
     79 
     80 #if defined(__cplusplus)
     81 extern "C" {
     82 #endif
     83 
     84 
     85 // Cap the size of all field elements and scalars, including custom curves, to
     86 // 66 bytes, large enough to fit secp521r1 and brainpoolP512r1, which appear to
     87 // be the largest fields anyone plausibly uses.
     88 #define EC_MAX_BYTES 66
     89 #define EC_MAX_WORDS ((EC_MAX_BYTES + BN_BYTES - 1) / BN_BYTES)
     90 
     91 OPENSSL_STATIC_ASSERT(EC_MAX_WORDS <= BN_SMALL_MAX_WORDS,
     92                       "bn_*_small functions not usable");
     93 
     94 // An EC_SCALAR is an integer fully reduced modulo the order. Only the first
     95 // |order->width| words are used. An |EC_SCALAR| is specific to an |EC_GROUP|
     96 // and must not be mixed between groups.
     97 typedef union {
     98   // bytes is the representation of the scalar in little-endian order.
     99   uint8_t bytes[EC_MAX_BYTES];
    100   BN_ULONG words[EC_MAX_WORDS];
    101 } EC_SCALAR;
    102 
    103 // An EC_FELEM represents a field element. Only the first |field->width| words
    104 // are used. An |EC_FELEM| is specific to an |EC_GROUP| and must not be mixed
    105 // between groups. Additionally, the representation (whether or not elements are
    106 // represented in Montgomery-form) may vary between |EC_METHOD|s.
    107 typedef union {
    108   // bytes is the representation of the field element in little-endian order.
    109   uint8_t bytes[EC_MAX_BYTES];
    110   BN_ULONG words[EC_MAX_WORDS];
    111 } EC_FELEM;
    112 
    113 // An EC_RAW_POINT represents an elliptic curve point. Unlike |EC_POINT|, it is
    114 // a plain struct which can be stack-allocated and needs no cleanup. It is
    115 // specific to an |EC_GROUP| and must not be mixed between groups.
    116 typedef struct {
    117   EC_FELEM X, Y, Z;
    118   // X, Y, and Z are Jacobian projective coordinates. They represent
    119   // (X/Z^2, Y/Z^3) if Z != 0 and the point at infinity otherwise.
    120 } EC_RAW_POINT;
    121 
    122 struct ec_method_st {
    123   int (*group_init)(EC_GROUP *);
    124   void (*group_finish)(EC_GROUP *);
    125   int (*group_set_curve)(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
    126                          const BIGNUM *b, BN_CTX *);
    127 
    128   // point_get_affine_coordinates sets |*x| and |*y| to the affine coordinates
    129   // of |p|. Either |x| or |y| may be NULL to omit it. It returns one on success
    130   // and zero if |p| is the point at infinity.
    131   //
    132   // Note: unlike |EC_FELEM|s used as intermediate values internal to the
    133   // |EC_METHOD|, |*x| and |*y| are not encoded in Montgomery form.
    134   int (*point_get_affine_coordinates)(const EC_GROUP *, const EC_RAW_POINT *p,
    135                                       EC_FELEM *x, EC_FELEM *y);
    136 
    137   // add sets |r| to |a| + |b|.
    138   void (*add)(const EC_GROUP *group, EC_RAW_POINT *r, const EC_RAW_POINT *a,
    139               const EC_RAW_POINT *b);
    140   // dbl sets |r| to |a| + |a|.
    141   void (*dbl)(const EC_GROUP *group, EC_RAW_POINT *r, const EC_RAW_POINT *a);
    142 
    143   // Computes |r = g_scalar*generator + p_scalar*p| if |g_scalar| and |p_scalar|
    144   // are both non-null. Computes |r = g_scalar*generator| if |p_scalar| is null.
    145   // Computes |r = p_scalar*p| if g_scalar is null. At least one of |g_scalar|
    146   // and |p_scalar| must be non-null, and |p| must be non-null if |p_scalar| is
    147   // non-null.
    148   void (*mul)(const EC_GROUP *group, EC_RAW_POINT *r, const EC_SCALAR *g_scalar,
    149               const EC_RAW_POINT *p, const EC_SCALAR *p_scalar);
    150   // mul_public performs the same computation as mul. It further assumes that
    151   // the inputs are public so there is no concern about leaking their values
    152   // through timing.
    153   void (*mul_public)(const EC_GROUP *group, EC_RAW_POINT *r,
    154                      const EC_SCALAR *g_scalar, const EC_RAW_POINT *p,
    155                      const EC_SCALAR *p_scalar);
    156 
    157   // felem_mul and felem_sqr implement multiplication and squaring,
    158   // respectively, so that the generic |EC_POINT_add| and |EC_POINT_dbl|
    159   // implementations can work both with |EC_GFp_mont_method| and the tuned
    160   // operations.
    161   //
    162   // TODO(davidben): This constrains |EC_FELEM|'s internal representation, adds
    163   // many indirect calls in the middle of the generic code, and a bunch of
    164   // conversions. If p224-64.c were easily convertable to Montgomery form, we
    165   // could say |EC_FELEM| is always in Montgomery form. If we routed the rest of
    166   // simple.c to |EC_METHOD|, we could give |EC_POINT| an |EC_METHOD|-specific
    167   // representation and say |EC_FELEM| is purely a |EC_GFp_mont_method| type.
    168   void (*felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
    169                     const EC_FELEM *b);
    170   void (*felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a);
    171 
    172   int (*bignum_to_felem)(const EC_GROUP *group, EC_FELEM *out,
    173                          const BIGNUM *in);
    174   int (*felem_to_bignum)(const EC_GROUP *group, BIGNUM *out,
    175                          const EC_FELEM *in);
    176 
    177   // scalar_inv_montgomery sets |out| to |in|^-1, where both input and output
    178   // are in Montgomery form.
    179   void (*scalar_inv_montgomery)(const EC_GROUP *group, EC_SCALAR *out,
    180                                 const EC_SCALAR *in);
    181 
    182   // scalar_inv_montgomery_vartime performs the same computation as
    183   // |scalar_inv_montgomery|. It further assumes that the inputs are public so
    184   // there is no concern about leaking their values through timing.
    185   int (*scalar_inv_montgomery_vartime)(const EC_GROUP *group, EC_SCALAR *out,
    186                                        const EC_SCALAR *in);
    187 
    188   // cmp_x_coordinate compares the x (affine) coordinate of |p|, mod the group
    189   // order, with |r|. It returns one if the values match and zero if |p| is the
    190   // point at infinity of the values do not match.
    191   int (*cmp_x_coordinate)(const EC_GROUP *group, const EC_RAW_POINT *p,
    192                           const EC_SCALAR *r);
    193 } /* EC_METHOD */;
    194 
    195 const EC_METHOD *EC_GFp_mont_method(void);
    196 
    197 struct ec_group_st {
    198   const EC_METHOD *meth;
    199 
    200   // Unlike all other |EC_POINT|s, |generator| does not own |generator->group|
    201   // to avoid a reference cycle.
    202   EC_POINT *generator;
    203   BIGNUM order;
    204 
    205   int curve_name;  // optional NID for named curve
    206 
    207   BN_MONT_CTX *order_mont;  // data for ECDSA inverse
    208 
    209   // The following members are handled by the method functions,
    210   // even if they appear generic
    211 
    212   BIGNUM field;  // For curves over GF(p), this is the modulus.
    213 
    214   EC_FELEM a, b;  // Curve coefficients.
    215 
    216   // a_is_minus3 is one if |a| is -3 mod |field| and zero otherwise. Point
    217   // arithmetic is optimized for -3.
    218   int a_is_minus3;
    219 
    220   // field_greater_than_order is one if |field| is greate than |order| and zero
    221   // otherwise.
    222   int field_greater_than_order;
    223 
    224   // field_minus_order, if |field_greater_than_order| is true, is |field| minus
    225   // |order| represented as an |EC_FELEM|. Otherwise, it is zero.
    226   //
    227   // Note: unlike |EC_FELEM|s used as intermediate values internal to the
    228   // |EC_METHOD|, this value is not encoded in Montgomery form.
    229   EC_FELEM field_minus_order;
    230 
    231   CRYPTO_refcount_t references;
    232 
    233   BN_MONT_CTX *mont;  // Montgomery structure.
    234 
    235   EC_FELEM one;  // The value one.
    236 } /* EC_GROUP */;
    237 
    238 struct ec_point_st {
    239   // group is an owning reference to |group|, unless this is
    240   // |group->generator|.
    241   EC_GROUP *group;
    242   // raw is the group-specific point data. Functions that take |EC_POINT|
    243   // typically check consistency with |EC_GROUP| while functions that take
    244   // |EC_RAW_POINT| do not. Thus accesses to this field should be externally
    245   // checked for consistency.
    246   EC_RAW_POINT raw;
    247 } /* EC_POINT */;
    248 
    249 EC_GROUP *ec_group_new(const EC_METHOD *meth);
    250 
    251 // ec_bignum_to_felem converts |in| to an |EC_FELEM|. It returns one on success
    252 // and zero if |in| is out of range.
    253 int ec_bignum_to_felem(const EC_GROUP *group, EC_FELEM *out, const BIGNUM *in);
    254 
    255 // ec_felem_to_bignum converts |in| to a |BIGNUM|. It returns one on success and
    256 // zero on allocation failure.
    257 int ec_felem_to_bignum(const EC_GROUP *group, BIGNUM *out, const EC_FELEM *in);
    258 
    259 // ec_felem_neg sets |out| to -|a|.
    260 void ec_felem_neg(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a);
    261 
    262 // ec_felem_add sets |out| to |a| + |b|.
    263 void ec_felem_add(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a,
    264                   const EC_FELEM *b);
    265 
    266 // ec_felem_add sets |out| to |a| - |b|.
    267 void ec_felem_sub(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a,
    268                   const EC_FELEM *b);
    269 
    270 // ec_felem_non_zero_mask returns all ones if |a| is non-zero and all zeros
    271 // otherwise.
    272 BN_ULONG ec_felem_non_zero_mask(const EC_GROUP *group, const EC_FELEM *a);
    273 
    274 // ec_felem_select, in constant time, sets |out| to |a| if |mask| is all ones
    275 // and |b| if |mask| is all zeros.
    276 void ec_felem_select(const EC_GROUP *group, EC_FELEM *out, BN_ULONG mask,
    277                      const EC_FELEM *a, const EC_FELEM *b);
    278 
    279 // ec_felem_equal returns one if |a| and |b| are equal and zero otherwise. It
    280 // treats |a| and |b| as public and does *not* run in constant time.
    281 int ec_felem_equal(const EC_GROUP *group, const EC_FELEM *a, const EC_FELEM *b);
    282 
    283 // ec_bignum_to_scalar converts |in| to an |EC_SCALAR| and writes it to
    284 // |*out|. It returns one on success and zero if |in| is out of range.
    285 OPENSSL_EXPORT int ec_bignum_to_scalar(const EC_GROUP *group, EC_SCALAR *out,
    286                                        const BIGNUM *in);
    287 
    288 // ec_random_nonzero_scalar sets |out| to a uniformly selected random value from
    289 // 1 to |group->order| - 1. It returns one on success and zero on error.
    290 int ec_random_nonzero_scalar(const EC_GROUP *group, EC_SCALAR *out,
    291                              const uint8_t additional_data[32]);
    292 
    293 // ec_scalar_equal_vartime returns one if |a| and |b| are equal and zero
    294 // otherwise. Both values are treated as public.
    295 int ec_scalar_equal_vartime(const EC_GROUP *group, const EC_SCALAR *a,
    296                             const EC_SCALAR *b);
    297 
    298 // ec_scalar_is_zero returns one if |a| is zero and zero otherwise.
    299 int ec_scalar_is_zero(const EC_GROUP *group, const EC_SCALAR *a);
    300 
    301 // ec_scalar_add sets |r| to |a| + |b|.
    302 void ec_scalar_add(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a,
    303                    const EC_SCALAR *b);
    304 
    305 // ec_scalar_to_montgomery sets |r| to |a| in Montgomery form.
    306 void ec_scalar_to_montgomery(const EC_GROUP *group, EC_SCALAR *r,
    307                              const EC_SCALAR *a);
    308 
    309 // ec_scalar_to_montgomery sets |r| to |a| converted from Montgomery form.
    310 void ec_scalar_from_montgomery(const EC_GROUP *group, EC_SCALAR *r,
    311                                const EC_SCALAR *a);
    312 
    313 // ec_scalar_mul_montgomery sets |r| to |a| * |b| where inputs and outputs are
    314 // in Montgomery form.
    315 void ec_scalar_mul_montgomery(const EC_GROUP *group, EC_SCALAR *r,
    316                               const EC_SCALAR *a, const EC_SCALAR *b);
    317 
    318 // ec_scalar_mul_montgomery sets |r| to |a|^-1 where inputs and outputs are in
    319 // Montgomery form.
    320 void ec_scalar_inv_montgomery(const EC_GROUP *group, EC_SCALAR *r,
    321                               const EC_SCALAR *a);
    322 
    323 // ec_scalar_inv_montgomery_vartime performs the same actions as
    324 // |ec_scalar_inv_montgomery|, but in variable time.
    325 int ec_scalar_inv_montgomery_vartime(const EC_GROUP *group, EC_SCALAR *r,
    326                                      const EC_SCALAR *a);
    327 
    328 // ec_point_mul_scalar sets |r| to generator * |g_scalar| + |p| *
    329 // |p_scalar|. Unlike other functions which take |EC_SCALAR|, |g_scalar| and
    330 // |p_scalar| need not be fully reduced. They need only contain as many bits as
    331 // the order.
    332 int ec_point_mul_scalar(const EC_GROUP *group, EC_RAW_POINT *r,
    333                         const EC_SCALAR *g_scalar, const EC_RAW_POINT *p,
    334                         const EC_SCALAR *p_scalar);
    335 
    336 // ec_point_mul_scalar_public performs the same computation as
    337 // ec_point_mul_scalar.  It further assumes that the inputs are public so
    338 // there is no concern about leaking their values through timing.
    339 OPENSSL_EXPORT int ec_point_mul_scalar_public(const EC_GROUP *group,
    340                                               EC_RAW_POINT *r,
    341                                               const EC_SCALAR *g_scalar,
    342                                               const EC_RAW_POINT *p,
    343                                               const EC_SCALAR *p_scalar);
    344 
    345 // ec_cmp_x_coordinate compares the x (affine) coordinate of |p|, mod the group
    346 // order, with |r|. It returns one if the values match and zero if |p| is the
    347 // point at infinity of the values do not match.
    348 int ec_cmp_x_coordinate(const EC_GROUP *group, const EC_RAW_POINT *p,
    349                         const EC_SCALAR *r);
    350 
    351 // ec_get_x_coordinate_as_scalar sets |*out| to |p|'s x-coordinate, modulo
    352 // |group->order|. It returns one on success and zero if |p| is the point at
    353 // infinity.
    354 int ec_get_x_coordinate_as_scalar(const EC_GROUP *group, EC_SCALAR *out,
    355                                   const EC_RAW_POINT *p);
    356 
    357 // ec_point_get_affine_coordinate_bytes writes |p|'s affine coordinates to
    358 // |out_x| and |out_y|, each of which must have at must |max_out| bytes. It sets
    359 // |*out_len| to the number of bytes written in each buffer. Coordinates are
    360 // written big-endian and zero-padded to the size of the field.
    361 //
    362 // Either of |out_x| or |out_y| may be NULL to omit that coordinate. This
    363 // function returns one on success and zero on failure.
    364 int ec_point_get_affine_coordinate_bytes(const EC_GROUP *group, uint8_t *out_x,
    365                                          uint8_t *out_y, size_t *out_len,
    366                                          size_t max_out, const EC_RAW_POINT *p);
    367 
    368 // ec_field_element_to_scalar reduces |r| modulo |group->order|. |r| must
    369 // previously have been reduced modulo |group->field|.
    370 int ec_field_element_to_scalar(const EC_GROUP *group, BIGNUM *r);
    371 
    372 void ec_GFp_mont_mul(const EC_GROUP *group, EC_RAW_POINT *r,
    373                      const EC_SCALAR *g_scalar, const EC_RAW_POINT *p,
    374                      const EC_SCALAR *p_scalar);
    375 
    376 // ec_compute_wNAF writes the modified width-(w+1) Non-Adjacent Form (wNAF) of
    377 // |scalar| to |out|. |out| must have room for |bits| + 1 elements, each of
    378 // which will be either zero or odd with an absolute value less than  2^w
    379 // satisfying
    380 //     scalar = \sum_j out[j]*2^j
    381 // where at most one of any  w+1  consecutive digits is non-zero
    382 // with the exception that the most significant digit may be only
    383 // w-1 zeros away from that next non-zero digit.
    384 void ec_compute_wNAF(const EC_GROUP *group, int8_t *out,
    385                      const EC_SCALAR *scalar, size_t bits, int w);
    386 
    387 void ec_GFp_mont_mul_public(const EC_GROUP *group, EC_RAW_POINT *r,
    388                             const EC_SCALAR *g_scalar, const EC_RAW_POINT *p,
    389                             const EC_SCALAR *p_scalar);
    390 
    391 // method functions in simple.c
    392 int ec_GFp_simple_group_init(EC_GROUP *);
    393 void ec_GFp_simple_group_finish(EC_GROUP *);
    394 int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
    395                                   const BIGNUM *b, BN_CTX *);
    396 int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
    397                                   BIGNUM *b);
    398 void ec_GFp_simple_point_init(EC_RAW_POINT *);
    399 void ec_GFp_simple_point_copy(EC_RAW_POINT *, const EC_RAW_POINT *);
    400 void ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_RAW_POINT *);
    401 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_RAW_POINT *,
    402                                                const BIGNUM *x,
    403                                                const BIGNUM *y);
    404 void ec_GFp_mont_add(const EC_GROUP *, EC_RAW_POINT *r, const EC_RAW_POINT *a,
    405                      const EC_RAW_POINT *b);
    406 void ec_GFp_mont_dbl(const EC_GROUP *, EC_RAW_POINT *r, const EC_RAW_POINT *a);
    407 void ec_GFp_simple_invert(const EC_GROUP *, EC_RAW_POINT *);
    408 int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_RAW_POINT *);
    409 int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_RAW_POINT *);
    410 int ec_GFp_simple_cmp(const EC_GROUP *, const EC_RAW_POINT *a,
    411                       const EC_RAW_POINT *b);
    412 void ec_simple_scalar_inv_montgomery(const EC_GROUP *group, EC_SCALAR *r,
    413                                      const EC_SCALAR *a);
    414 
    415 int ec_GFp_simple_mont_inv_mod_ord_vartime(const EC_GROUP *group, EC_SCALAR *r,
    416                                            const EC_SCALAR *a);
    417 
    418 int ec_GFp_simple_cmp_x_coordinate(const EC_GROUP *group, const EC_RAW_POINT *p,
    419                                    const EC_SCALAR *r);
    420 
    421 // method functions in montgomery.c
    422 int ec_GFp_mont_group_init(EC_GROUP *);
    423 int ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
    424                                 const BIGNUM *b, BN_CTX *);
    425 void ec_GFp_mont_group_finish(EC_GROUP *);
    426 void ec_GFp_mont_felem_mul(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
    427                            const EC_FELEM *b);
    428 void ec_GFp_mont_felem_sqr(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a);
    429 
    430 int ec_GFp_mont_bignum_to_felem(const EC_GROUP *group, EC_FELEM *out,
    431                                 const BIGNUM *in);
    432 int ec_GFp_mont_felem_to_bignum(const EC_GROUP *group, BIGNUM *out,
    433                                 const EC_FELEM *in);
    434 
    435 void ec_GFp_nistp_recode_scalar_bits(uint8_t *sign, uint8_t *digit, uint8_t in);
    436 
    437 const EC_METHOD *EC_GFp_nistp224_method(void);
    438 const EC_METHOD *EC_GFp_nistp256_method(void);
    439 
    440 // EC_GFp_nistz256_method is a GFp method using montgomery multiplication, with
    441 // x86-64 optimized P256. See http://eprint.iacr.org/2013/816.
    442 const EC_METHOD *EC_GFp_nistz256_method(void);
    443 
    444 // An EC_WRAPPED_SCALAR is an |EC_SCALAR| with a parallel |BIGNUM|
    445 // representation. It exists to support the |EC_KEY_get0_private_key| API.
    446 typedef struct {
    447   BIGNUM bignum;
    448   EC_SCALAR scalar;
    449 } EC_WRAPPED_SCALAR;
    450 
    451 struct ec_key_st {
    452   EC_GROUP *group;
    453 
    454   EC_POINT *pub_key;
    455   EC_WRAPPED_SCALAR *priv_key;
    456 
    457   // fixed_k may contain a specific value of 'k', to be used in ECDSA signing.
    458   // This is only for the FIPS power-on tests.
    459   BIGNUM *fixed_k;
    460 
    461   unsigned int enc_flag;
    462   point_conversion_form_t conv_form;
    463 
    464   CRYPTO_refcount_t references;
    465 
    466   ECDSA_METHOD *ecdsa_meth;
    467 
    468   CRYPTO_EX_DATA ex_data;
    469 } /* EC_KEY */;
    470 
    471 struct built_in_curve {
    472   int nid;
    473   const uint8_t *oid;
    474   uint8_t oid_len;
    475   // comment is a human-readable string describing the curve.
    476   const char *comment;
    477   // param_len is the number of bytes needed to store a field element.
    478   uint8_t param_len;
    479   // params points to an array of 6*|param_len| bytes which hold the field
    480   // elements of the following (in big-endian order): prime, a, b, generator x,
    481   // generator y, order.
    482   const uint8_t *params;
    483   const EC_METHOD *method;
    484 };
    485 
    486 #define OPENSSL_NUM_BUILT_IN_CURVES 4
    487 
    488 struct built_in_curves {
    489   struct built_in_curve curves[OPENSSL_NUM_BUILT_IN_CURVES];
    490 };
    491 
    492 // OPENSSL_built_in_curves returns a pointer to static information about
    493 // standard curves. The array is terminated with an entry where |nid| is
    494 // |NID_undef|.
    495 const struct built_in_curves *OPENSSL_built_in_curves(void);
    496 
    497 #if defined(__cplusplus)
    498 }  // extern C
    499 #endif
    500 
    501 #endif  // OPENSSL_HEADER_EC_INTERNAL_H
    502