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.
 
 
 
 
 
 

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