You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

277 regels
11 KiB

  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. #if defined(__cplusplus)
  74. extern "C" {
  75. #endif
  76. struct ec_method_st {
  77. int (*group_init)(EC_GROUP *);
  78. void (*group_finish)(EC_GROUP *);
  79. int (*group_copy)(EC_GROUP *, const EC_GROUP *);
  80. int (*group_set_curve)(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
  81. const BIGNUM *b, BN_CTX *);
  82. int (*point_get_affine_coordinates)(const EC_GROUP *, const EC_POINT *,
  83. BIGNUM *x, BIGNUM *y, BN_CTX *);
  84. /* Computes |r = g_scalar*generator + p_scalar*p| if |g_scalar| and |p_scalar|
  85. * are both non-null. Computes |r = g_scalar*generator| if |p_scalar| is null.
  86. * Computes |r = p_scalar*p| if g_scalar is null. At least one of |g_scalar|
  87. * and |p_scalar| must be non-null, and |p| must be non-null if |p_scalar| is
  88. * non-null. */
  89. int (*mul)(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
  90. const EC_POINT *p, const BIGNUM *p_scalar, BN_CTX *ctx);
  91. /* 'field_mul' and 'field_sqr' can be used by 'add' and 'dbl' so that the
  92. * same implementations of point operations can be used with different
  93. * optimized implementations of expensive field operations: */
  94. int (*field_mul)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  95. const BIGNUM *b, BN_CTX *);
  96. int (*field_sqr)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
  97. int (*field_encode)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  98. BN_CTX *); /* e.g. to Montgomery */
  99. int (*field_decode)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  100. BN_CTX *); /* e.g. from Montgomery */
  101. } /* EC_METHOD */;
  102. extern const EC_METHOD EC_GFp_mont_method;
  103. struct ec_group_st {
  104. const EC_METHOD *meth;
  105. EC_POINT *generator;
  106. BIGNUM order;
  107. int curve_name; /* optional NID for named curve */
  108. const BN_MONT_CTX *mont_data; /* data for ECDSA inverse */
  109. /* The following members are handled by the method functions,
  110. * even if they appear generic */
  111. BIGNUM field; /* For curves over GF(p), this is the modulus. */
  112. BIGNUM a, b; /* Curve coefficients. */
  113. int a_is_minus3; /* enable optimized point arithmetics for special case */
  114. BN_MONT_CTX *mont; /* Montgomery structure. */
  115. BIGNUM one; /* The value one. */
  116. } /* EC_GROUP */;
  117. struct ec_point_st {
  118. const EC_METHOD *meth;
  119. BIGNUM X;
  120. BIGNUM Y;
  121. BIGNUM Z; /* Jacobian projective coordinates:
  122. * (X, Y, Z) represents (X/Z^2, Y/Z^3) if Z != 0 */
  123. } /* EC_POINT */;
  124. EC_GROUP *ec_group_new(const EC_METHOD *meth);
  125. int ec_group_copy(EC_GROUP *dest, const EC_GROUP *src);
  126. /* ec_group_get_mont_data returns a Montgomery context for operations in the
  127. * scalar field of |group|. It may return NULL in the case that |group| is not
  128. * a built-in group. */
  129. const BN_MONT_CTX *ec_group_get_mont_data(const EC_GROUP *group);
  130. int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
  131. const EC_POINT *p, const BIGNUM *p_scalar, BN_CTX *ctx);
  132. /* method functions in simple.c */
  133. int ec_GFp_simple_group_init(EC_GROUP *);
  134. void ec_GFp_simple_group_finish(EC_GROUP *);
  135. int ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
  136. int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
  137. const BIGNUM *b, BN_CTX *);
  138. int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
  139. BIGNUM *b, BN_CTX *);
  140. unsigned ec_GFp_simple_group_get_degree(const EC_GROUP *);
  141. int ec_GFp_simple_point_init(EC_POINT *);
  142. void ec_GFp_simple_point_finish(EC_POINT *);
  143. void ec_GFp_simple_point_clear_finish(EC_POINT *);
  144. int ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
  145. int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
  146. int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *, EC_POINT *,
  147. const BIGNUM *x,
  148. const BIGNUM *y,
  149. const BIGNUM *z, BN_CTX *);
  150. int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *,
  151. const EC_POINT *, BIGNUM *x,
  152. BIGNUM *y, BIGNUM *z,
  153. BN_CTX *);
  154. int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
  155. const BIGNUM *x, const BIGNUM *y,
  156. BN_CTX *);
  157. int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
  158. const BIGNUM *x, int y_bit,
  159. BN_CTX *);
  160. int ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  161. const EC_POINT *b, BN_CTX *);
  162. int ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  163. BN_CTX *);
  164. int ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
  165. int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
  166. int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
  167. int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
  168. BN_CTX *);
  169. int ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
  170. int ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
  171. EC_POINT * [], BN_CTX *);
  172. int ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  173. const BIGNUM *b, BN_CTX *);
  174. int ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  175. BN_CTX *);
  176. /* method functions in montgomery.c */
  177. int ec_GFp_mont_group_init(EC_GROUP *);
  178. int ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
  179. const BIGNUM *b, BN_CTX *);
  180. void ec_GFp_mont_group_finish(EC_GROUP *);
  181. int ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
  182. int ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  183. const BIGNUM *b, BN_CTX *);
  184. int ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  185. BN_CTX *);
  186. int ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  187. BN_CTX *);
  188. int ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  189. BN_CTX *);
  190. int ec_point_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
  191. EC_POINT *point, const BIGNUM *x,
  192. const BIGNUM *y, const BIGNUM *z,
  193. BN_CTX *ctx);
  194. void ec_GFp_nistp_recode_scalar_bits(uint8_t *sign, uint8_t *digit, uint8_t in);
  195. extern const EC_METHOD EC_GFp_nistp224_method;
  196. extern const EC_METHOD EC_GFp_nistp256_method;
  197. /* EC_GFp_nistz256_method is a GFp method using montgomery multiplication, with
  198. * x86-64 optimized P256. See http://eprint.iacr.org/2013/816. */
  199. extern const EC_METHOD EC_GFp_nistz256_method;
  200. struct ec_key_st {
  201. EC_GROUP *group;
  202. EC_POINT *pub_key;
  203. BIGNUM *priv_key;
  204. unsigned int enc_flag;
  205. point_conversion_form_t conv_form;
  206. CRYPTO_refcount_t references;
  207. ECDSA_METHOD *ecdsa_meth;
  208. CRYPTO_EX_DATA ex_data;
  209. } /* EC_KEY */;
  210. /* curve_data contains data about a built-in elliptic curve. */
  211. struct curve_data {
  212. /* comment is a human-readable string describing the curve. */
  213. const char *comment;
  214. /* param_len is the number of bytes needed to store a field element. */
  215. uint8_t param_len;
  216. /* data points to an array of 6*|param_len| bytes which hold the field
  217. * elements of the following (in big-endian order): prime, a, b, generator x,
  218. * generator y, order. */
  219. const uint8_t data[];
  220. };
  221. struct built_in_curve {
  222. int nid;
  223. uint8_t oid[8];
  224. uint8_t oid_len;
  225. const struct curve_data *data;
  226. const EC_METHOD *method;
  227. };
  228. /* OPENSSL_built_in_curves is terminated with an entry where |nid| is
  229. * |NID_undef|. */
  230. extern const struct built_in_curve OPENSSL_built_in_curves[];
  231. #if defined(__cplusplus)
  232. } /* extern C */
  233. #endif
  234. #endif /* OPENSSL_HEADER_EC_INTERNAL_H */