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