25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

382 satır
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. typedef struct ec_method_st EC_METHOD;
  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();
  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. point_conversion_form_t asn1_form;
  175. struct ec_pre_comp_st *pre_comp;
  176. /* The following members are handled by the method functions,
  177. * even if they appear generic */
  178. BIGNUM field; /* Field specification.
  179. * For curves over GF(p), this is the modulus;
  180. * for curves over GF(2^m), this is the
  181. * irreducible polynomial defining the field. */
  182. int poly[6]; /* Field specification for curves over GF(2^m).
  183. * The irreducible f(t) is then of the form:
  184. * t^poly[0] + t^poly[1] + ... + t^poly[k]
  185. * where m = poly[0] > poly[1] > ... > poly[k] = 0.
  186. * The array is terminated with poly[k+1]=-1.
  187. * All elliptic curve irreducibles have at most 5
  188. * non-zero terms. */
  189. BIGNUM a, b; /* Curve coefficients.
  190. * (Here the assumption is that BIGNUMs can be used
  191. * or abused for all kinds of fields, not just GF(p).)
  192. * For characteristic > 3, the curve is defined
  193. * by a Weierstrass equation of the form
  194. * y^2 = x^3 + a*x + b.
  195. * For characteristic 2, the curve is defined by
  196. * an equation of the form
  197. * y^2 + x*y = x^3 + a*x^2 + b. */
  198. int a_is_minus3; /* enable optimized point arithmetics for special case */
  199. void *field_data1; /* method-specific (e.g., Montgomery structure) */
  200. void *field_data2; /* method-specific */
  201. int (*field_mod_func)(BIGNUM *, const BIGNUM *, const BIGNUM *,
  202. BN_CTX *); /* method-specific */
  203. } /* EC_GROUP */;
  204. struct ec_point_st {
  205. const EC_METHOD *meth;
  206. /* All members except 'meth' are handled by the method functions,
  207. * even if they appear generic */
  208. BIGNUM X;
  209. BIGNUM Y;
  210. BIGNUM Z; /* Jacobian projective coordinates:
  211. * (X, Y, Z) represents (X/Z^2, Y/Z^3) if Z != 0 */
  212. int Z_is_one; /* enable optimized point arithmetics for special case */
  213. } /* EC_POINT */;
  214. EC_GROUP *ec_group_new(const EC_METHOD *meth);
  215. int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  216. size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
  217. BN_CTX *);
  218. int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *);
  219. int ec_wNAF_have_precompute_mult(const EC_GROUP *group);
  220. /* method functions in simple.c */
  221. int ec_GFp_simple_group_init(EC_GROUP *);
  222. void ec_GFp_simple_group_finish(EC_GROUP *);
  223. void ec_GFp_simple_group_clear_finish(EC_GROUP *);
  224. int ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
  225. int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
  226. const BIGNUM *b, BN_CTX *);
  227. int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
  228. BIGNUM *b, BN_CTX *);
  229. int ec_GFp_simple_group_get_degree(const EC_GROUP *);
  230. int ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
  231. int ec_GFp_simple_point_init(EC_POINT *);
  232. void ec_GFp_simple_point_finish(EC_POINT *);
  233. void ec_GFp_simple_point_clear_finish(EC_POINT *);
  234. int ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
  235. int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
  236. int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *, EC_POINT *,
  237. const BIGNUM *x,
  238. const BIGNUM *y,
  239. const BIGNUM *z, BN_CTX *);
  240. int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *,
  241. const EC_POINT *, BIGNUM *x,
  242. BIGNUM *y, BIGNUM *z,
  243. BN_CTX *);
  244. int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
  245. const BIGNUM *x, const BIGNUM *y,
  246. BN_CTX *);
  247. int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *,
  248. const EC_POINT *, BIGNUM *x,
  249. BIGNUM *y, BN_CTX *);
  250. int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
  251. const BIGNUM *x, int y_bit,
  252. BN_CTX *);
  253. int ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  254. const EC_POINT *b, BN_CTX *);
  255. int ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
  256. BN_CTX *);
  257. int ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
  258. int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
  259. int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
  260. int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
  261. BN_CTX *);
  262. int ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
  263. int ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
  264. EC_POINT * [], BN_CTX *);
  265. int ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  266. const BIGNUM *b, BN_CTX *);
  267. int ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  268. BN_CTX *);
  269. /* method functions in montgomery.c */
  270. int ec_GFp_mont_group_init(EC_GROUP *);
  271. int ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
  272. const BIGNUM *b, BN_CTX *);
  273. void ec_GFp_mont_group_finish(EC_GROUP *);
  274. void ec_GFp_mont_group_clear_finish(EC_GROUP *);
  275. int ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
  276. int ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  277. const BIGNUM *b, BN_CTX *);
  278. int ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  279. BN_CTX *);
  280. int ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  281. BN_CTX *);
  282. int ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
  283. BN_CTX *);
  284. int ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
  285. int ec_point_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
  286. EC_POINT *point, const BIGNUM *x,
  287. const BIGNUM *y, const BIGNUM *z,
  288. BN_CTX *ctx);
  289. struct ec_key_st {
  290. int version;
  291. EC_GROUP *group;
  292. EC_POINT *pub_key;
  293. BIGNUM *priv_key;
  294. unsigned int enc_flag;
  295. point_conversion_form_t conv_form;
  296. int references;
  297. int flags;
  298. ECDSA_METHOD *ecdsa_meth;
  299. CRYPTO_EX_DATA ex_data;
  300. } /* EC_KEY */;
  301. /* curve_data contains data about a built-in elliptic curve. */
  302. struct curve_data {
  303. /* comment is a human-readable string describing the curve. */
  304. const char *comment;
  305. /* param_len is the number of bytes needed to store a field element. */
  306. uint8_t param_len;
  307. /* cofactor is the cofactor of the group (i.e. the number of elements in the
  308. * group divided by the size of the main subgroup. */
  309. uint8_t cofactor; /* promoted to BN_ULONG */
  310. /* data points to an array of 6*|param_len| bytes which hold the field
  311. * elements of the following (in big-endian order): prime, a, b, generator x,
  312. * generator y, order. */
  313. const uint8_t data[];
  314. };
  315. struct built_in_curve {
  316. int nid;
  317. const struct curve_data *data;
  318. const EC_METHOD *(*method)(void);
  319. };
  320. /* OPENSSL_built_in_curves is terminated with an entry where |nid| is
  321. * |NID_undef|. */
  322. extern const struct built_in_curve OPENSSL_built_in_curves[];
  323. #if defined(__cplusplus)
  324. } /* extern C */
  325. #endif
  326. #endif /* OPENSSL_HEADER_EC_INTERNAL_H */