Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

373 строки
16 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. /* Use default functions for poin2oct, oct2point and compressed coordinates */
  77. #define EC_FLAGS_DEFAULT_OCT 0x1
  78. struct ec_method_st {
  79. /* Various method flags */
  80. int flags;
  81. /* used by EC_GROUP_new, EC_GROUP_free, EC_GROUP_clear_free, EC_GROUP_copy: */
  82. int (*group_init)(EC_GROUP *);
  83. void (*group_finish)(EC_GROUP *);
  84. void (*group_clear_finish)(EC_GROUP *);
  85. int (*group_copy)(EC_GROUP *, const EC_GROUP *);
  86. /* used by EC_GROUP_set_curve_GFp, EC_GROUP_get_curve_GFp, */
  87. /* EC_GROUP_set_curve_GF2m, and EC_GROUP_get_curve_GF2m: */
  88. int (*group_set_curve)(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
  89. const BIGNUM *b, BN_CTX *);
  90. int (*group_get_curve)(const EC_GROUP *, BIGNUM *p, BIGNUM *a, BIGNUM *b,
  91. BN_CTX *);
  92. /* used by EC_GROUP_get_degree: */
  93. int (*group_get_degree)(const EC_GROUP *);
  94. /* used by EC_GROUP_check: */
  95. int (*group_check_discriminant)(const EC_GROUP *, BN_CTX *);
  96. /* used by EC_POINT_new, EC_POINT_free, EC_POINT_clear_free, EC_POINT_copy: */
  97. int (*point_init)(EC_POINT *);
  98. void (*point_finish)(EC_POINT *);
  99. void (*point_clear_finish)(EC_POINT *);
  100. int (*point_copy)(EC_POINT *, const EC_POINT *);
  101. /* used by EC_POINT_set_to_infinity,
  102. * EC_POINT_set_Jprojective_coordinates_GFp,
  103. * EC_POINT_get_Jprojective_coordinates_GFp,
  104. * EC_POINT_set_affine_coordinates_GFp, ..._GF2m,
  105. * EC_POINT_get_affine_coordinates_GFp, ..._GF2m,
  106. * EC_POINT_set_compressed_coordinates_GFp, ..._GF2m:
  107. */
  108. int (*point_set_to_infinity)(const EC_GROUP *, EC_POINT *);
  109. int (*point_set_Jprojective_coordinates_GFp)(const EC_GROUP *, EC_POINT *,
  110. const BIGNUM *x, const BIGNUM *y,
  111. const BIGNUM *z, BN_CTX *);
  112. int (*point_get_Jprojective_coordinates_GFp)(const EC_GROUP *,
  113. const EC_POINT *, BIGNUM *x,
  114. BIGNUM *y, BIGNUM *z, BN_CTX *);
  115. int (*point_set_affine_coordinates)(const EC_GROUP *, EC_POINT *,
  116. const BIGNUM *x, const BIGNUM *y,
  117. BN_CTX *);
  118. int (*point_get_affine_coordinates)(const EC_GROUP *, const EC_POINT *,
  119. BIGNUM *x, BIGNUM *y, BN_CTX *);
  120. int (*point_set_compressed_coordinates)(const EC_GROUP *, EC_POINT *,
  121. const BIGNUM *x, int y_bit, BN_CTX *);
  122. /* used by EC_POINT_point2oct, EC_POINT_oct2point: */
  123. size_t (*point2oct)(const EC_GROUP *, const EC_POINT *,
  124. point_conversion_form_t form, unsigned char *buf,
  125. size_t len, BN_CTX *);
  126. int (*oct2point)(const EC_GROUP *, EC_POINT *, const unsigned char *buf,
  127. size_t len, BN_CTX *);
  128. /* used by EC_POINT_add, EC_POINT_dbl, ECP_POINT_invert: */
  129. int (*add)(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  130. const EC_POINT *b, BN_CTX *);
  131. int (*dbl)(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *);
  132. int (*invert)(const EC_GROUP *, EC_POINT *, BN_CTX *);
  133. /* used by EC_POINT_is_at_infinity, EC_POINT_is_on_curve, EC_POINT_cmp: */
  134. int (*is_at_infinity)(const EC_GROUP *, const EC_POINT *);
  135. int (*is_on_curve)(const EC_GROUP *, const EC_POINT *, BN_CTX *);
  136. int (*point_cmp)(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
  137. BN_CTX *);
  138. /* used by EC_POINT_make_affine, EC_POINTs_make_affine: */
  139. int (*make_affine)(const EC_GROUP *, EC_POINT *, BN_CTX *);
  140. int (*points_make_affine)(const EC_GROUP *, size_t num, EC_POINT * [],
  141. BN_CTX *);
  142. /* used by EC_POINTs_mul, EC_POINT_mul, EC_POINT_precompute_mult,
  143. * EC_POINT_have_precompute_mult
  144. * (default implementations are used if the 'mul' pointer is 0): */
  145. int (*mul)(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  146. size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
  147. BN_CTX *);
  148. int (*precompute_mult)(EC_GROUP *group, BN_CTX *);
  149. int (*have_precompute_mult)(const EC_GROUP *group);
  150. /* internal functions */
  151. /* 'field_mul', 'field_sqr', and 'field_div' can be used by 'add' and 'dbl'
  152. * so that the same implementations of point operations can be used with
  153. * different optimized implementations of expensive field operations: */
  154. int (*field_mul)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  155. const BIGNUM *b, BN_CTX *);
  156. int (*field_sqr)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
  157. int (*field_div)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  158. const BIGNUM *b, BN_CTX *);
  159. int (*field_encode)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  160. BN_CTX *); /* e.g. to Montgomery */
  161. int (*field_decode)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  162. BN_CTX *); /* e.g. from Montgomery */
  163. int (*field_set_to_one)(const EC_GROUP *, BIGNUM *r, BN_CTX *);
  164. } /* EC_METHOD */;
  165. const EC_METHOD* EC_GFp_mont_method(void);
  166. struct ec_pre_comp_st;
  167. void ec_pre_comp_free(struct ec_pre_comp_st *pre_comp);
  168. void *ec_pre_comp_dup(struct ec_pre_comp_st *pre_comp);
  169. struct ec_group_st {
  170. const EC_METHOD *meth;
  171. EC_POINT *generator; /* optional */
  172. BIGNUM order, cofactor;
  173. int curve_name; /* optional NID for named curve */
  174. struct ec_pre_comp_st *pre_comp;
  175. /* The following members are handled by the method functions,
  176. * even if they appear generic */
  177. BIGNUM field; /* For curves over GF(p), this is the modulus. */
  178. BIGNUM a, b; /* Curve coefficients. */
  179. int a_is_minus3; /* enable optimized point arithmetics for special case */
  180. BN_MONT_CTX *mont; /* Montgomery structure. */
  181. BIGNUM *one; /* The value one */
  182. } /* EC_GROUP */;
  183. struct ec_point_st {
  184. const EC_METHOD *meth;
  185. /* All members except 'meth' are handled by the method functions,
  186. * even if they appear generic */
  187. BIGNUM X;
  188. BIGNUM Y;
  189. BIGNUM Z; /* Jacobian projective coordinates:
  190. * (X, Y, Z) represents (X/Z^2, Y/Z^3) if Z != 0 */
  191. int Z_is_one; /* enable optimized point arithmetics for special case */
  192. } /* EC_POINT */;
  193. EC_GROUP *ec_group_new(const EC_METHOD *meth);
  194. int ec_group_copy(EC_GROUP *dest, const EC_GROUP *src);
  195. int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  196. size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
  197. BN_CTX *);
  198. int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *);
  199. int ec_wNAF_have_precompute_mult(const EC_GROUP *group);
  200. /* method functions in simple.c */
  201. int ec_GFp_simple_group_init(EC_GROUP *);
  202. void ec_GFp_simple_group_finish(EC_GROUP *);
  203. void ec_GFp_simple_group_clear_finish(EC_GROUP *);
  204. int ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
  205. int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
  206. const BIGNUM *b, BN_CTX *);
  207. int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
  208. BIGNUM *b, BN_CTX *);
  209. int ec_GFp_simple_group_get_degree(const EC_GROUP *);
  210. int ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
  211. int ec_GFp_simple_point_init(EC_POINT *);
  212. void ec_GFp_simple_point_finish(EC_POINT *);
  213. void ec_GFp_simple_point_clear_finish(EC_POINT *);
  214. int ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
  215. int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
  216. int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *, EC_POINT *,
  217. const BIGNUM *x,
  218. const BIGNUM *y,
  219. const BIGNUM *z, BN_CTX *);
  220. int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *,
  221. const EC_POINT *, BIGNUM *x,
  222. BIGNUM *y, BIGNUM *z,
  223. BN_CTX *);
  224. int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
  225. const BIGNUM *x, const BIGNUM *y,
  226. BN_CTX *);
  227. int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *,
  228. const EC_POINT *, BIGNUM *x,
  229. BIGNUM *y, BN_CTX *);
  230. int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
  231. const BIGNUM *x, int y_bit,
  232. BN_CTX *);
  233. int ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  234. const EC_POINT *b, BN_CTX *);
  235. int ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  236. BN_CTX *);
  237. int ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
  238. int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
  239. int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
  240. int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
  241. BN_CTX *);
  242. int ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
  243. int ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
  244. EC_POINT * [], BN_CTX *);
  245. int ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  246. const BIGNUM *b, BN_CTX *);
  247. int ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  248. BN_CTX *);
  249. /* method functions in montgomery.c */
  250. int ec_GFp_mont_group_init(EC_GROUP *);
  251. int ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
  252. const BIGNUM *b, BN_CTX *);
  253. void ec_GFp_mont_group_finish(EC_GROUP *);
  254. void ec_GFp_mont_group_clear_finish(EC_GROUP *);
  255. int ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
  256. int ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  257. const BIGNUM *b, BN_CTX *);
  258. int ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  259. BN_CTX *);
  260. int ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  261. BN_CTX *);
  262. int ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  263. BN_CTX *);
  264. int ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
  265. int ec_point_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
  266. EC_POINT *point, const BIGNUM *x,
  267. const BIGNUM *y, const BIGNUM *z,
  268. BN_CTX *ctx);
  269. void ec_GFp_nistp_points_make_affine_internal(
  270. size_t num, void *point_array, size_t felem_size, void *tmp_felems,
  271. void (*felem_one)(void *out), int (*felem_is_zero)(const void *in),
  272. void (*felem_assign)(void *out, const void *in),
  273. void (*felem_square)(void *out, const void *in),
  274. void (*felem_mul)(void *out, const void *in1, const void *in2),
  275. void (*felem_inv)(void *out, const void *in),
  276. void (*felem_contract)(void *out, const void *in));
  277. void ec_GFp_nistp_recode_scalar_bits(uint8_t *sign, uint8_t *digit, uint8_t in);
  278. const EC_METHOD *EC_GFp_nistp256_method(void);
  279. struct ec_key_st {
  280. int version;
  281. EC_GROUP *group;
  282. EC_POINT *pub_key;
  283. BIGNUM *priv_key;
  284. unsigned int enc_flag;
  285. point_conversion_form_t conv_form;
  286. CRYPTO_refcount_t references;
  287. int flags;
  288. ECDSA_METHOD *ecdsa_meth;
  289. CRYPTO_EX_DATA ex_data;
  290. } /* EC_KEY */;
  291. /* curve_data contains data about a built-in elliptic curve. */
  292. struct curve_data {
  293. /* comment is a human-readable string describing the curve. */
  294. const char *comment;
  295. /* param_len is the number of bytes needed to store a field element. */
  296. uint8_t param_len;
  297. /* cofactor is the cofactor of the group (i.e. the number of elements in the
  298. * group divided by the size of the main subgroup. */
  299. uint8_t cofactor; /* promoted to BN_ULONG */
  300. /* data points to an array of 6*|param_len| bytes which hold the field
  301. * elements of the following (in big-endian order): prime, a, b, generator x,
  302. * generator y, order. */
  303. const uint8_t data[];
  304. };
  305. struct built_in_curve {
  306. int nid;
  307. const struct curve_data *data;
  308. const EC_METHOD *(*method)(void);
  309. };
  310. /* OPENSSL_built_in_curves is terminated with an entry where |nid| is
  311. * |NID_undef|. */
  312. extern const struct built_in_curve OPENSSL_built_in_curves[];
  313. #if defined(__cplusplus)
  314. } /* extern C */
  315. #endif
  316. #endif /* OPENSSL_HEADER_EC_INTERNAL_H */