internal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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@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@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@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. #ifndef OPENSSL_HEADER_EC_INTERNAL_H
  68. #define OPENSSL_HEADER_EC_INTERNAL_H
  69. #include <openssl/base.h>
  70. #include <openssl/bn.h>
  71. #include <openssl/ex_data.h>
  72. #include <openssl/thread.h>
  73. #include <openssl/type_check.h>
  74. #include "../bn/internal.h"
  75. #if defined(__cplusplus)
  76. extern "C" {
  77. #endif
  78. // Cap the size of all field elements and scalars, including custom curves, to
  79. // 66 bytes, large enough to fit secp521r1 and brainpoolP512r1, which appear to
  80. // be the largest fields anyone plausibly uses.
  81. #define EC_MAX_SCALAR_BYTES 66
  82. #define EC_MAX_SCALAR_WORDS ((66 + BN_BYTES - 1) / BN_BYTES)
  83. OPENSSL_COMPILE_ASSERT(EC_MAX_SCALAR_WORDS <= BN_SMALL_MAX_WORDS,
  84. bn_small_functions_applicable);
  85. // An EC_SCALAR is a |BN_num_bits(order)|-bit integer. Only the first
  86. // |order->top| words are used. An |EC_SCALAR| is specific to an |EC_GROUP| and
  87. // must not be mixed between groups. Unless otherwise specified, it is fully
  88. // reduced modulo the |order|.
  89. typedef union {
  90. // bytes is the representation of the scalar in little-endian order.
  91. uint8_t bytes[EC_MAX_SCALAR_BYTES];
  92. BN_ULONG words[EC_MAX_SCALAR_WORDS];
  93. } EC_SCALAR;
  94. struct ec_method_st {
  95. int (*group_init)(EC_GROUP *);
  96. void (*group_finish)(EC_GROUP *);
  97. int (*group_set_curve)(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
  98. const BIGNUM *b, BN_CTX *);
  99. int (*point_get_affine_coordinates)(const EC_GROUP *, const EC_POINT *,
  100. BIGNUM *x, BIGNUM *y, BN_CTX *);
  101. // Computes |r = g_scalar*generator + p_scalar*p| if |g_scalar| and |p_scalar|
  102. // are both non-null. Computes |r = g_scalar*generator| if |p_scalar| is null.
  103. // Computes |r = p_scalar*p| if g_scalar is null. At least one of |g_scalar|
  104. // and |p_scalar| must be non-null, and |p| must be non-null if |p_scalar| is
  105. // non-null.
  106. int (*mul)(const EC_GROUP *group, EC_POINT *r, const EC_SCALAR *g_scalar,
  107. const EC_POINT *p, const EC_SCALAR *p_scalar, BN_CTX *ctx);
  108. // 'field_mul' and 'field_sqr' can be used by 'add' and 'dbl' so that the
  109. // same implementations of point operations can be used with different
  110. // optimized implementations of expensive field operations:
  111. int (*field_mul)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  112. const BIGNUM *b, BN_CTX *);
  113. int (*field_sqr)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
  114. int (*field_encode)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  115. BN_CTX *); // e.g. to Montgomery
  116. int (*field_decode)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  117. BN_CTX *); // e.g. from Montgomery
  118. } /* EC_METHOD */;
  119. const EC_METHOD *EC_GFp_mont_method(void);
  120. struct ec_group_st {
  121. const EC_METHOD *meth;
  122. // Unlike all other |EC_POINT|s, |generator| does not own |generator->group|
  123. // to avoid a reference cycle.
  124. EC_POINT *generator;
  125. BIGNUM order;
  126. int curve_name; // optional NID for named curve
  127. BN_MONT_CTX *order_mont; // data for ECDSA inverse
  128. // The following members are handled by the method functions,
  129. // even if they appear generic
  130. BIGNUM field; // For curves over GF(p), this is the modulus.
  131. BIGNUM a, b; // Curve coefficients.
  132. int a_is_minus3; // enable optimized point arithmetics for special case
  133. CRYPTO_refcount_t references;
  134. BN_MONT_CTX *mont; // Montgomery structure.
  135. BIGNUM one; // The value one.
  136. } /* EC_GROUP */;
  137. struct ec_point_st {
  138. // group is an owning reference to |group|, unless this is
  139. // |group->generator|.
  140. EC_GROUP *group;
  141. BIGNUM X;
  142. BIGNUM Y;
  143. BIGNUM Z; // Jacobian projective coordinates:
  144. // (X, Y, Z) represents (X/Z^2, Y/Z^3) if Z != 0
  145. } /* EC_POINT */;
  146. EC_GROUP *ec_group_new(const EC_METHOD *meth);
  147. // ec_bignum_to_scalar converts |in| to an |EC_SCALAR| and writes it to |*out|.
  148. // |in| must be non-negative and have at most |BN_num_bits(&group->order)| bits.
  149. // It returns one on success and zero on error. It does not ensure |in| is fully
  150. // reduced.
  151. int ec_bignum_to_scalar(const EC_GROUP *group, EC_SCALAR *out,
  152. const BIGNUM *in);
  153. // ec_random_nonzero_scalar sets |out| to a uniformly selected random value from
  154. // 1 to |group->order| - 1. It returns one on success and zero on error.
  155. int ec_random_nonzero_scalar(const EC_GROUP *group, EC_SCALAR *out,
  156. const uint8_t additional_data[32]);
  157. // ec_point_mul_scalar sets |r| to generator * |g_scalar| + |p| *
  158. // |p_scalar|. Unlike other functions which take |EC_SCALAR|, |g_scalar| and
  159. // |p_scalar| need not be fully reduced. They need only contain as many bits as
  160. // the order.
  161. int ec_point_mul_scalar(const EC_GROUP *group, EC_POINT *r,
  162. const EC_SCALAR *g_scalar, const EC_POINT *p,
  163. const EC_SCALAR *p_scalar, BN_CTX *ctx);
  164. int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const EC_SCALAR *g_scalar,
  165. const EC_POINT *p, const EC_SCALAR *p_scalar, BN_CTX *ctx);
  166. // method functions in simple.c
  167. int ec_GFp_simple_group_init(EC_GROUP *);
  168. void ec_GFp_simple_group_finish(EC_GROUP *);
  169. int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
  170. const BIGNUM *b, BN_CTX *);
  171. int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
  172. BIGNUM *b, BN_CTX *);
  173. unsigned ec_GFp_simple_group_get_degree(const EC_GROUP *);
  174. int ec_GFp_simple_point_init(EC_POINT *);
  175. void ec_GFp_simple_point_finish(EC_POINT *);
  176. int ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
  177. int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
  178. int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *, EC_POINT *,
  179. const BIGNUM *x,
  180. const BIGNUM *y,
  181. const BIGNUM *z, BN_CTX *);
  182. int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
  183. const BIGNUM *x, const BIGNUM *y,
  184. BN_CTX *);
  185. int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
  186. const BIGNUM *x, int y_bit,
  187. BN_CTX *);
  188. int ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  189. const EC_POINT *b, BN_CTX *);
  190. int ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  191. BN_CTX *);
  192. int ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
  193. int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
  194. int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
  195. int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
  196. BN_CTX *);
  197. int ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
  198. int ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
  199. EC_POINT * [], BN_CTX *);
  200. int ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  201. const BIGNUM *b, BN_CTX *);
  202. int ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  203. BN_CTX *);
  204. // method functions in montgomery.c
  205. int ec_GFp_mont_group_init(EC_GROUP *);
  206. int ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
  207. const BIGNUM *b, BN_CTX *);
  208. void ec_GFp_mont_group_finish(EC_GROUP *);
  209. int ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  210. const BIGNUM *b, BN_CTX *);
  211. int ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  212. BN_CTX *);
  213. int ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  214. BN_CTX *);
  215. int ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  216. BN_CTX *);
  217. int ec_point_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
  218. EC_POINT *point, const BIGNUM *x,
  219. const BIGNUM *y, const BIGNUM *z,
  220. BN_CTX *ctx);
  221. void ec_GFp_nistp_recode_scalar_bits(uint8_t *sign, uint8_t *digit, uint8_t in);
  222. const EC_METHOD *EC_GFp_nistp224_method(void);
  223. const EC_METHOD *EC_GFp_nistp256_method(void);
  224. // EC_GFp_nistz256_method is a GFp method using montgomery multiplication, with
  225. // x86-64 optimized P256. See http://eprint.iacr.org/2013/816.
  226. const EC_METHOD *EC_GFp_nistz256_method(void);
  227. struct ec_key_st {
  228. EC_GROUP *group;
  229. EC_POINT *pub_key;
  230. BIGNUM *priv_key;
  231. // fixed_k may contain a specific value of 'k', to be used in ECDSA signing.
  232. // This is only for the FIPS power-on tests.
  233. BIGNUM *fixed_k;
  234. unsigned int enc_flag;
  235. point_conversion_form_t conv_form;
  236. CRYPTO_refcount_t references;
  237. ECDSA_METHOD *ecdsa_meth;
  238. CRYPTO_EX_DATA ex_data;
  239. } /* EC_KEY */;
  240. struct built_in_curve {
  241. int nid;
  242. const uint8_t *oid;
  243. uint8_t oid_len;
  244. // comment is a human-readable string describing the curve.
  245. const char *comment;
  246. // param_len is the number of bytes needed to store a field element.
  247. uint8_t param_len;
  248. // params points to an array of 6*|param_len| bytes which hold the field
  249. // elements of the following (in big-endian order): prime, a, b, generator x,
  250. // generator y, order.
  251. const uint8_t *params;
  252. const EC_METHOD *method;
  253. };
  254. #define OPENSSL_NUM_BUILT_IN_CURVES 4
  255. struct built_in_curves {
  256. struct built_in_curve curves[OPENSSL_NUM_BUILT_IN_CURVES];
  257. };
  258. // OPENSSL_built_in_curves returns a pointer to static information about
  259. // standard curves. The array is terminated with an entry where |nid| is
  260. // |NID_undef|.
  261. const struct built_in_curves *OPENSSL_built_in_curves(void);
  262. #if defined(__cplusplus)
  263. } // extern C
  264. #endif
  265. #endif // OPENSSL_HEADER_EC_INTERNAL_H