1 1.1 christos /* 2 1.1 christos * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved. 3 1.1 christos * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved 4 1.1 christos * 5 1.1 christos * Licensed under the OpenSSL license (the "License"). You may not use 6 1.1 christos * this file except in compliance with the License. You can obtain a copy 7 1.1 christos * in the file LICENSE in the source distribution or at 8 1.1 christos * https://www.openssl.org/source/license.html 9 1.1 christos */ 10 1.1 christos 11 1.1 christos #ifndef HEADER_EC_H 12 1.1 christos # define HEADER_EC_H 13 1.1 christos 14 1.1 christos # include <openssl/opensslconf.h> 15 1.1 christos 16 1.1 christos # ifndef OPENSSL_NO_EC 17 1.1 christos # include <openssl/asn1.h> 18 1.1 christos # include <openssl/symhacks.h> 19 1.1 christos # if OPENSSL_API_COMPAT < 0x10100000L 20 1.1 christos # include <openssl/bn.h> 21 1.1 christos # endif 22 1.1 christos # include <openssl/ecerr.h> 23 1.1 christos # ifdef __cplusplus 24 1.1 christos extern "C" { 25 1.1 christos # endif 26 1.1 christos 27 1.1 christos # ifndef OPENSSL_ECC_MAX_FIELD_BITS 28 1.1 christos # define OPENSSL_ECC_MAX_FIELD_BITS 661 29 1.1 christos # endif 30 1.1 christos 31 1.1 christos /** Enum for the point conversion form as defined in X9.62 (ECDSA) 32 1.1 christos * for the encoding of a elliptic curve point (x,y) */ 33 1.1 christos typedef enum { 34 1.1 christos /** the point is encoded as z||x, where the octet z specifies 35 1.1 christos * which solution of the quadratic equation y is */ 36 1.1 christos POINT_CONVERSION_COMPRESSED = 2, 37 1.1 christos /** the point is encoded as z||x||y, where z is the octet 0x04 */ 38 1.1 christos POINT_CONVERSION_UNCOMPRESSED = 4, 39 1.1 christos /** the point is encoded as z||x||y, where the octet z specifies 40 1.1 christos * which solution of the quadratic equation y is */ 41 1.1 christos POINT_CONVERSION_HYBRID = 6 42 1.1 christos } point_conversion_form_t; 43 1.1 christos 44 1.1 christos typedef struct ec_method_st EC_METHOD; 45 1.1 christos typedef struct ec_group_st EC_GROUP; 46 1.1 christos typedef struct ec_point_st EC_POINT; 47 1.1 christos typedef struct ecpk_parameters_st ECPKPARAMETERS; 48 1.1 christos typedef struct ec_parameters_st ECPARAMETERS; 49 1.1 christos 50 1.1 christos /********************************************************************/ 51 1.1 christos /* EC_METHODs for curves over GF(p) */ 52 1.1 christos /********************************************************************/ 53 1.1 christos 54 1.1 christos /** Returns the basic GFp ec methods which provides the basis for the 55 1.1 christos * optimized methods. 56 1.1 christos * \return EC_METHOD object 57 1.1 christos */ 58 1.1 christos const EC_METHOD *EC_GFp_simple_method(void); 59 1.1 christos 60 1.1 christos /** Returns GFp methods using montgomery multiplication. 61 1.1 christos * \return EC_METHOD object 62 1.1 christos */ 63 1.1 christos const EC_METHOD *EC_GFp_mont_method(void); 64 1.1 christos 65 1.1 christos /** Returns GFp methods using optimized methods for NIST recommended curves 66 1.1 christos * \return EC_METHOD object 67 1.1 christos */ 68 1.1 christos const EC_METHOD *EC_GFp_nist_method(void); 69 1.1 christos 70 1.1 christos # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 71 1.1 christos /** Returns 64-bit optimized methods for nistp224 72 1.1 christos * \return EC_METHOD object 73 1.1 christos */ 74 1.1 christos const EC_METHOD *EC_GFp_nistp224_method(void); 75 1.1 christos 76 1.1 christos /** Returns 64-bit optimized methods for nistp256 77 1.1 christos * \return EC_METHOD object 78 1.1 christos */ 79 1.1 christos const EC_METHOD *EC_GFp_nistp256_method(void); 80 1.1 christos 81 1.1 christos /** Returns 64-bit optimized methods for nistp521 82 1.1 christos * \return EC_METHOD object 83 1.1 christos */ 84 1.1 christos const EC_METHOD *EC_GFp_nistp521_method(void); 85 1.1 christos # endif 86 1.1 christos 87 1.1 christos # ifndef OPENSSL_NO_EC2M 88 1.1 christos /********************************************************************/ 89 1.1 christos /* EC_METHOD for curves over GF(2^m) */ 90 1.1 christos /********************************************************************/ 91 1.1 christos 92 1.1 christos /** Returns the basic GF2m ec method 93 1.1 christos * \return EC_METHOD object 94 1.1 christos */ 95 1.1 christos const EC_METHOD *EC_GF2m_simple_method(void); 96 1.1 christos 97 1.1 christos # endif 98 1.1 christos 99 1.1 christos /********************************************************************/ 100 1.1 christos /* EC_GROUP functions */ 101 1.1 christos /********************************************************************/ 102 1.1 christos 103 1.1 christos /** Creates a new EC_GROUP object 104 1.1 christos * \param meth EC_METHOD to use 105 1.1 christos * \return newly created EC_GROUP object or NULL in case of an error. 106 1.1 christos */ 107 1.1 christos EC_GROUP *EC_GROUP_new(const EC_METHOD *meth); 108 1.1 christos 109 1.1 christos /** Frees a EC_GROUP object 110 1.1 christos * \param group EC_GROUP object to be freed. 111 1.1 christos */ 112 1.1 christos void EC_GROUP_free(EC_GROUP *group); 113 1.1 christos 114 1.1 christos /** Clears and frees a EC_GROUP object 115 1.1 christos * \param group EC_GROUP object to be cleared and freed. 116 1.1 christos */ 117 1.1 christos void EC_GROUP_clear_free(EC_GROUP *group); 118 1.1 christos 119 1.1 christos /** Copies EC_GROUP objects. Note: both EC_GROUPs must use the same EC_METHOD. 120 1.1 christos * \param dst destination EC_GROUP object 121 1.1 christos * \param src source EC_GROUP object 122 1.1 christos * \return 1 on success and 0 if an error occurred. 123 1.1 christos */ 124 1.1 christos int EC_GROUP_copy(EC_GROUP *dst, const EC_GROUP *src); 125 1.1 christos 126 1.1 christos /** Creates a new EC_GROUP object and copies the copies the content 127 1.1 christos * form src to the newly created EC_KEY object 128 1.1 christos * \param src source EC_GROUP object 129 1.1 christos * \return newly created EC_GROUP object or NULL in case of an error. 130 1.1 christos */ 131 1.1 christos EC_GROUP *EC_GROUP_dup(const EC_GROUP *src); 132 1.1 christos 133 1.1 christos /** Returns the EC_METHOD of the EC_GROUP object. 134 1.1 christos * \param group EC_GROUP object 135 1.1 christos * \return EC_METHOD used in this EC_GROUP object. 136 1.1 christos */ 137 1.1 christos const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group); 138 1.1 christos 139 1.1 christos /** Returns the field type of the EC_METHOD. 140 1.1 christos * \param meth EC_METHOD object 141 1.1 christos * \return NID of the underlying field type OID. 142 1.1 christos */ 143 1.1 christos int EC_METHOD_get_field_type(const EC_METHOD *meth); 144 1.1 christos 145 1.1 christos /** Sets the generator and its order/cofactor of a EC_GROUP object. 146 1.1 christos * \param group EC_GROUP object 147 1.1 christos * \param generator EC_POINT object with the generator. 148 1.1 christos * \param order the order of the group generated by the generator. 149 1.1 christos * \param cofactor the index of the sub-group generated by the generator 150 1.1 christos * in the group of all points on the elliptic curve. 151 1.1 christos * \return 1 on success and 0 if an error occurred 152 1.1 christos */ 153 1.1 christos int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, 154 1.1 christos const BIGNUM *order, const BIGNUM *cofactor); 155 1.1 christos 156 1.1 christos /** Returns the generator of a EC_GROUP object. 157 1.1 christos * \param group EC_GROUP object 158 1.1 christos * \return the currently used generator (possibly NULL). 159 1.1 christos */ 160 1.1 christos const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group); 161 1.1 christos 162 1.1 christos /** Returns the montgomery data for order(Generator) 163 1.1 christos * \param group EC_GROUP object 164 1.1 christos * \return the currently used montgomery data (possibly NULL). 165 1.1 christos */ 166 1.1 christos BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group); 167 1.1 christos 168 1.1 christos /** Gets the order of a EC_GROUP 169 1.1 christos * \param group EC_GROUP object 170 1.1 christos * \param order BIGNUM to which the order is copied 171 1.1 christos * \param ctx unused 172 1.1 christos * \return 1 on success and 0 if an error occurred 173 1.1 christos */ 174 1.1 christos int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx); 175 1.1 christos 176 1.1 christos /** Gets the order of an EC_GROUP 177 1.1 christos * \param group EC_GROUP object 178 1.1 christos * \return the group order 179 1.1 christos */ 180 1.1 christos const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group); 181 1.1 christos 182 1.1 christos /** Gets the number of bits of the order of an EC_GROUP 183 1.1 christos * \param group EC_GROUP object 184 1.1 christos * \return number of bits of group order. 185 1.1 christos */ 186 1.1 christos int EC_GROUP_order_bits(const EC_GROUP *group); 187 1.1 christos 188 1.1 christos /** Gets the cofactor of a EC_GROUP 189 1.1 christos * \param group EC_GROUP object 190 1.1 christos * \param cofactor BIGNUM to which the cofactor is copied 191 1.1 christos * \param ctx unused 192 1.1 christos * \return 1 on success and 0 if an error occurred 193 1.1 christos */ 194 1.1 christos int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, 195 1.1 christos BN_CTX *ctx); 196 1.1 christos 197 1.1 christos /** Gets the cofactor of an EC_GROUP 198 1.1 christos * \param group EC_GROUP object 199 1.1 christos * \return the group cofactor 200 1.1 christos */ 201 1.1 christos const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group); 202 1.1 christos 203 1.1 christos /** Sets the name of a EC_GROUP object 204 1.1 christos * \param group EC_GROUP object 205 1.1 christos * \param nid NID of the curve name OID 206 1.1 christos */ 207 1.1 christos void EC_GROUP_set_curve_name(EC_GROUP *group, int nid); 208 1.1 christos 209 1.1 christos /** Returns the curve name of a EC_GROUP object 210 1.1 christos * \param group EC_GROUP object 211 1.1 christos * \return NID of the curve name OID or 0 if not set. 212 1.1 christos */ 213 1.1 christos int EC_GROUP_get_curve_name(const EC_GROUP *group); 214 1.1 christos 215 1.1 christos void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag); 216 1.1 christos int EC_GROUP_get_asn1_flag(const EC_GROUP *group); 217 1.1 christos 218 1.1 christos void EC_GROUP_set_point_conversion_form(EC_GROUP *group, 219 1.1 christos point_conversion_form_t form); 220 1.1 christos point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP *); 221 1.1 christos 222 1.1 christos unsigned char *EC_GROUP_get0_seed(const EC_GROUP *x); 223 1.1 christos size_t EC_GROUP_get_seed_len(const EC_GROUP *); 224 1.1 christos size_t EC_GROUP_set_seed(EC_GROUP *, const unsigned char *, size_t len); 225 1.1 christos 226 1.1 christos /** Sets the parameters of a ec curve defined by y^2 = x^3 + a*x + b (for GFp) 227 1.1 christos * or y^2 + x*y = x^3 + a*x^2 + b (for GF2m) 228 1.1 christos * \param group EC_GROUP object 229 1.1 christos * \param p BIGNUM with the prime number (GFp) or the polynomial 230 1.1 christos * defining the underlying field (GF2m) 231 1.1 christos * \param a BIGNUM with parameter a of the equation 232 1.1 christos * \param b BIGNUM with parameter b of the equation 233 1.1 christos * \param ctx BN_CTX object (optional) 234 1.1 christos * \return 1 on success and 0 if an error occurred 235 1.1 christos */ 236 1.1 christos int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, 237 1.1 christos const BIGNUM *b, BN_CTX *ctx); 238 1.1 christos 239 1.1 christos /** Gets the parameters of the ec curve defined by y^2 = x^3 + a*x + b (for GFp) 240 1.1 christos * or y^2 + x*y = x^3 + a*x^2 + b (for GF2m) 241 1.1 christos * \param group EC_GROUP object 242 1.1 christos * \param p BIGNUM with the prime number (GFp) or the polynomial 243 1.1 christos * defining the underlying field (GF2m) 244 1.1 christos * \param a BIGNUM for parameter a of the equation 245 1.1 christos * \param b BIGNUM for parameter b of the equation 246 1.1 christos * \param ctx BN_CTX object (optional) 247 1.1 christos * \return 1 on success and 0 if an error occurred 248 1.1 christos */ 249 1.1 christos int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, 250 1.1 christos BN_CTX *ctx); 251 1.1 christos 252 1.1 christos /** Sets the parameters of an ec curve. Synonym for EC_GROUP_set_curve 253 1.1 christos * \param group EC_GROUP object 254 1.1 christos * \param p BIGNUM with the prime number (GFp) or the polynomial 255 1.1 christos * defining the underlying field (GF2m) 256 1.1 christos * \param a BIGNUM with parameter a of the equation 257 1.1 christos * \param b BIGNUM with parameter b of the equation 258 1.1 christos * \param ctx BN_CTX object (optional) 259 1.1 christos * \return 1 on success and 0 if an error occurred 260 1.1 christos */ 261 1.1 christos DEPRECATEDIN_1_2_0(int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, 262 1.1 christos const BIGNUM *a, const BIGNUM *b, 263 1.1 christos BN_CTX *ctx)) 264 1.1 christos 265 1.1 christos /** Gets the parameters of an ec curve. Synonym for EC_GROUP_get_curve 266 1.1 christos * \param group EC_GROUP object 267 1.1 christos * \param p BIGNUM with the prime number (GFp) or the polynomial 268 1.1 christos * defining the underlying field (GF2m) 269 1.1 christos * \param a BIGNUM for parameter a of the equation 270 1.1 christos * \param b BIGNUM for parameter b of the equation 271 1.1 christos * \param ctx BN_CTX object (optional) 272 1.1 christos * \return 1 on success and 0 if an error occurred 273 1.1 christos */ 274 1.1 christos DEPRECATEDIN_1_2_0(int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, 275 1.1 christos BIGNUM *a, BIGNUM *b, 276 1.1 christos BN_CTX *ctx)) 277 1.1 christos 278 1.1 christos # ifndef OPENSSL_NO_EC2M 279 1.1 christos /** Sets the parameter of an ec curve. Synonym for EC_GROUP_set_curve 280 1.1 christos * \param group EC_GROUP object 281 1.1 christos * \param p BIGNUM with the prime number (GFp) or the polynomial 282 1.1 christos * defining the underlying field (GF2m) 283 1.1 christos * \param a BIGNUM with parameter a of the equation 284 1.1 christos * \param b BIGNUM with parameter b of the equation 285 1.1 christos * \param ctx BN_CTX object (optional) 286 1.1 christos * \return 1 on success and 0 if an error occurred 287 1.1 christos */ 288 1.1 christos DEPRECATEDIN_1_2_0(int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, 289 1.1 christos const BIGNUM *a, const BIGNUM *b, 290 1.1 christos BN_CTX *ctx)) 291 1.1 christos 292 1.1 christos /** Gets the parameters of an ec curve. Synonym for EC_GROUP_get_curve 293 1.1 christos * \param group EC_GROUP object 294 1.1 christos * \param p BIGNUM with the prime number (GFp) or the polynomial 295 1.1 christos * defining the underlying field (GF2m) 296 1.1 christos * \param a BIGNUM for parameter a of the equation 297 1.1 christos * \param b BIGNUM for parameter b of the equation 298 1.1 christos * \param ctx BN_CTX object (optional) 299 1.1 christos * \return 1 on success and 0 if an error occurred 300 1.1 christos */ 301 1.1 christos DEPRECATEDIN_1_2_0(int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, 302 1.1 christos BIGNUM *a, BIGNUM *b, 303 1.1 christos BN_CTX *ctx)) 304 1.1 christos # endif 305 1.1 christos /** Returns the number of bits needed to represent a field element 306 1.1 christos * \param group EC_GROUP object 307 1.1 christos * \return number of bits needed to represent a field element 308 1.1 christos */ 309 1.1 christos int EC_GROUP_get_degree(const EC_GROUP *group); 310 1.1 christos 311 1.1 christos /** Checks whether the parameter in the EC_GROUP define a valid ec group 312 1.1 christos * \param group EC_GROUP object 313 1.1 christos * \param ctx BN_CTX object (optional) 314 1.1 christos * \return 1 if group is a valid ec group and 0 otherwise 315 1.1 christos */ 316 1.1 christos int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx); 317 1.1 christos 318 1.1 christos /** Checks whether the discriminant of the elliptic curve is zero or not 319 1.1 christos * \param group EC_GROUP object 320 1.1 christos * \param ctx BN_CTX object (optional) 321 1.1 christos * \return 1 if the discriminant is not zero and 0 otherwise 322 1.1 christos */ 323 1.1 christos int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx); 324 1.1 christos 325 1.1 christos /** Compares two EC_GROUP objects 326 1.1 christos * \param a first EC_GROUP object 327 1.1 christos * \param b second EC_GROUP object 328 1.1 christos * \param ctx BN_CTX object (optional) 329 1.1 christos * \return 0 if the groups are equal, 1 if not, or -1 on error 330 1.1 christos */ 331 1.1 christos int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx); 332 1.1 christos 333 1.1 christos /* 334 1.1 christos * EC_GROUP_new_GF*() calls EC_GROUP_new() and EC_GROUP_set_GF*() after 335 1.1 christos * choosing an appropriate EC_METHOD 336 1.1 christos */ 337 1.1 christos 338 1.1 christos /** Creates a new EC_GROUP object with the specified parameters defined 339 1.1 christos * over GFp (defined by the equation y^2 = x^3 + a*x + b) 340 1.1 christos * \param p BIGNUM with the prime number 341 1.1 christos * \param a BIGNUM with the parameter a of the equation 342 1.1 christos * \param b BIGNUM with the parameter b of the equation 343 1.1 christos * \param ctx BN_CTX object (optional) 344 1.1 christos * \return newly created EC_GROUP object with the specified parameters 345 1.1 christos */ 346 1.1 christos EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a, 347 1.1 christos const BIGNUM *b, BN_CTX *ctx); 348 1.1 christos # ifndef OPENSSL_NO_EC2M 349 1.1 christos /** Creates a new EC_GROUP object with the specified parameters defined 350 1.1 christos * over GF2m (defined by the equation y^2 + x*y = x^3 + a*x^2 + b) 351 1.1 christos * \param p BIGNUM with the polynomial defining the underlying field 352 1.1 christos * \param a BIGNUM with the parameter a of the equation 353 1.1 christos * \param b BIGNUM with the parameter b of the equation 354 1.1 christos * \param ctx BN_CTX object (optional) 355 1.1 christos * \return newly created EC_GROUP object with the specified parameters 356 1.1 christos */ 357 1.1 christos EC_GROUP *EC_GROUP_new_curve_GF2m(const BIGNUM *p, const BIGNUM *a, 358 1.1 christos const BIGNUM *b, BN_CTX *ctx); 359 1.1 christos # endif 360 1.1 christos 361 1.1 christos /** Creates a EC_GROUP object with a curve specified by a NID 362 1.1 christos * \param nid NID of the OID of the curve name 363 1.1 christos * \return newly created EC_GROUP object with specified curve or NULL 364 1.1 christos * if an error occurred 365 1.1 christos */ 366 1.1 christos EC_GROUP *EC_GROUP_new_by_curve_name(int nid); 367 1.1 christos 368 1.1 christos /** Creates a new EC_GROUP object from an ECPARAMETERS object 369 1.1 christos * \param params pointer to the ECPARAMETERS object 370 1.1 christos * \return newly created EC_GROUP object with specified curve or NULL 371 1.1 christos * if an error occurred 372 1.1 christos */ 373 1.1 christos EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params); 374 1.1 christos 375 1.1 christos /** Creates an ECPARAMETERS object for the given EC_GROUP object. 376 1.1 christos * \param group pointer to the EC_GROUP object 377 1.1 christos * \param params pointer to an existing ECPARAMETERS object or NULL 378 1.1 christos * \return pointer to the new ECPARAMETERS object or NULL 379 1.1 christos * if an error occurred. 380 1.1 christos */ 381 1.1 christos ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group, 382 1.1 christos ECPARAMETERS *params); 383 1.1 christos 384 1.1 christos /** Creates a new EC_GROUP object from an ECPKPARAMETERS object 385 1.1 christos * \param params pointer to an existing ECPKPARAMETERS object, or NULL 386 1.1 christos * \return newly created EC_GROUP object with specified curve, or NULL 387 1.1 christos * if an error occurred 388 1.1 christos */ 389 1.1 christos EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params); 390 1.1 christos 391 1.1 christos /** Creates an ECPKPARAMETERS object for the given EC_GROUP object. 392 1.1 christos * \param group pointer to the EC_GROUP object 393 1.1 christos * \param params pointer to an existing ECPKPARAMETERS object or NULL 394 1.1 christos * \return pointer to the new ECPKPARAMETERS object or NULL 395 1.1 christos * if an error occurred. 396 1.1 christos */ 397 1.1 christos ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group, 398 1.1 christos ECPKPARAMETERS *params); 399 1.1 christos 400 1.1 christos /********************************************************************/ 401 1.1 christos /* handling of internal curves */ 402 1.1 christos /********************************************************************/ 403 1.1 christos 404 1.1 christos typedef struct { 405 1.1 christos int nid; 406 1.1 christos const char *comment; 407 1.1 christos } EC_builtin_curve; 408 1.1 christos 409 1.1 christos /* 410 1.1 christos * EC_builtin_curves(EC_builtin_curve *r, size_t size) returns number of all 411 1.1 christos * available curves or zero if a error occurred. In case r is not zero, 412 1.1 christos * nitems EC_builtin_curve structures are filled with the data of the first 413 1.1 christos * nitems internal groups 414 1.1 christos */ 415 1.1 christos size_t EC_get_builtin_curves(EC_builtin_curve *r, size_t nitems); 416 1.1 christos 417 1.1 christos const char *EC_curve_nid2nist(int nid); 418 1.1 christos int EC_curve_nist2nid(const char *name); 419 1.1 christos 420 1.1 christos /********************************************************************/ 421 1.1 christos /* EC_POINT functions */ 422 1.1 christos /********************************************************************/ 423 1.1 christos 424 1.1 christos /** Creates a new EC_POINT object for the specified EC_GROUP 425 1.1 christos * \param group EC_GROUP the underlying EC_GROUP object 426 1.1 christos * \return newly created EC_POINT object or NULL if an error occurred 427 1.1 christos */ 428 1.1 christos EC_POINT *EC_POINT_new(const EC_GROUP *group); 429 1.1 christos 430 1.1 christos /** Frees a EC_POINT object 431 1.1 christos * \param point EC_POINT object to be freed 432 1.1 christos */ 433 1.1 christos void EC_POINT_free(EC_POINT *point); 434 1.1 christos 435 1.1 christos /** Clears and frees a EC_POINT object 436 1.1 christos * \param point EC_POINT object to be cleared and freed 437 1.1 christos */ 438 1.1 christos void EC_POINT_clear_free(EC_POINT *point); 439 1.1 christos 440 1.1 christos /** Copies EC_POINT object 441 1.1 christos * \param dst destination EC_POINT object 442 1.1 christos * \param src source EC_POINT object 443 1.1 christos * \return 1 on success and 0 if an error occurred 444 1.1 christos */ 445 1.1 christos int EC_POINT_copy(EC_POINT *dst, const EC_POINT *src); 446 1.1 christos 447 1.1 christos /** Creates a new EC_POINT object and copies the content of the supplied 448 1.1 christos * EC_POINT 449 1.1 christos * \param src source EC_POINT object 450 1.1 christos * \param group underlying the EC_GROUP object 451 1.1 christos * \return newly created EC_POINT object or NULL if an error occurred 452 1.1 christos */ 453 1.1 christos EC_POINT *EC_POINT_dup(const EC_POINT *src, const EC_GROUP *group); 454 1.1 christos 455 1.1 christos /** Returns the EC_METHOD used in EC_POINT object 456 1.1 christos * \param point EC_POINT object 457 1.1 christos * \return the EC_METHOD used 458 1.1 christos */ 459 1.1 christos const EC_METHOD *EC_POINT_method_of(const EC_POINT *point); 460 1.1 christos 461 1.1 christos /** Sets a point to infinity (neutral element) 462 1.1 christos * \param group underlying EC_GROUP object 463 1.1 christos * \param point EC_POINT to set to infinity 464 1.1 christos * \return 1 on success and 0 if an error occurred 465 1.1 christos */ 466 1.1 christos int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point); 467 1.1 christos 468 1.1 christos /** Sets the jacobian projective coordinates of a EC_POINT over GFp 469 1.1 christos * \param group underlying EC_GROUP object 470 1.1 christos * \param p EC_POINT object 471 1.1 christos * \param x BIGNUM with the x-coordinate 472 1.1 christos * \param y BIGNUM with the y-coordinate 473 1.1 christos * \param z BIGNUM with the z-coordinate 474 1.1 christos * \param ctx BN_CTX object (optional) 475 1.1 christos * \return 1 on success and 0 if an error occurred 476 1.1 christos */ 477 1.1 christos int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, 478 1.1 christos EC_POINT *p, const BIGNUM *x, 479 1.1 christos const BIGNUM *y, const BIGNUM *z, 480 1.1 christos BN_CTX *ctx); 481 1.1 christos 482 1.1 christos /** Gets the jacobian projective coordinates of a EC_POINT over GFp 483 1.1 christos * \param group underlying EC_GROUP object 484 1.1 christos * \param p EC_POINT object 485 1.1 christos * \param x BIGNUM for the x-coordinate 486 1.1 christos * \param y BIGNUM for the y-coordinate 487 1.1 christos * \param z BIGNUM for the z-coordinate 488 1.1 christos * \param ctx BN_CTX object (optional) 489 1.1 christos * \return 1 on success and 0 if an error occurred 490 1.1 christos */ 491 1.1 christos int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group, 492 1.1 christos const EC_POINT *p, BIGNUM *x, 493 1.1 christos BIGNUM *y, BIGNUM *z, 494 1.1 christos BN_CTX *ctx); 495 1.1 christos 496 1.1 christos /** Sets the affine coordinates of an EC_POINT 497 1.1 christos * \param group underlying EC_GROUP object 498 1.1 christos * \param p EC_POINT object 499 1.1 christos * \param x BIGNUM with the x-coordinate 500 1.1 christos * \param y BIGNUM with the y-coordinate 501 1.1 christos * \param ctx BN_CTX object (optional) 502 1.1 christos * \return 1 on success and 0 if an error occurred 503 1.1 christos */ 504 1.1 christos int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *p, 505 1.1 christos const BIGNUM *x, const BIGNUM *y, 506 1.1 christos BN_CTX *ctx); 507 1.1 christos 508 1.1 christos /** Gets the affine coordinates of an EC_POINT. 509 1.1 christos * \param group underlying EC_GROUP object 510 1.1 christos * \param p EC_POINT object 511 1.1 christos * \param x BIGNUM for the x-coordinate 512 1.1 christos * \param y BIGNUM for the y-coordinate 513 1.1 christos * \param ctx BN_CTX object (optional) 514 1.1 christos * \return 1 on success and 0 if an error occurred 515 1.1 christos */ 516 1.1 christos int EC_POINT_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *p, 517 1.1 christos BIGNUM *x, BIGNUM *y, BN_CTX *ctx); 518 1.1 christos 519 1.1 christos /** Sets the affine coordinates of an EC_POINT. A synonym of 520 1.1 christos * EC_POINT_set_affine_coordinates 521 1.1 christos * \param group underlying EC_GROUP object 522 1.1 christos * \param p EC_POINT object 523 1.1 christos * \param x BIGNUM with the x-coordinate 524 1.1 christos * \param y BIGNUM with the y-coordinate 525 1.1 christos * \param ctx BN_CTX object (optional) 526 1.1 christos * \return 1 on success and 0 if an error occurred 527 1.1 christos */ 528 1.1 christos DEPRECATEDIN_1_2_0(int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, 529 1.1 christos EC_POINT *p, 530 1.1 christos const BIGNUM *x, 531 1.1 christos const BIGNUM *y, 532 1.1 christos BN_CTX *ctx)) 533 1.1 christos 534 1.1 christos /** Gets the affine coordinates of an EC_POINT. A synonym of 535 1.1 christos * EC_POINT_get_affine_coordinates 536 1.1 christos * \param group underlying EC_GROUP object 537 1.1 christos * \param p EC_POINT object 538 1.1 christos * \param x BIGNUM for the x-coordinate 539 1.1 christos * \param y BIGNUM for the y-coordinate 540 1.1 christos * \param ctx BN_CTX object (optional) 541 1.1 christos * \return 1 on success and 0 if an error occurred 542 1.1 christos */ 543 1.1 christos DEPRECATEDIN_1_2_0(int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, 544 1.1 christos const EC_POINT *p, 545 1.1 christos BIGNUM *x, 546 1.1 christos BIGNUM *y, 547 1.1 christos BN_CTX *ctx)) 548 1.1 christos 549 1.1 christos /** Sets the x9.62 compressed coordinates of a EC_POINT 550 1.1 christos * \param group underlying EC_GROUP object 551 1.1 christos * \param p EC_POINT object 552 1.1 christos * \param x BIGNUM with x-coordinate 553 1.1 christos * \param y_bit integer with the y-Bit (either 0 or 1) 554 1.1 christos * \param ctx BN_CTX object (optional) 555 1.1 christos * \return 1 on success and 0 if an error occurred 556 1.1 christos */ 557 1.1 christos int EC_POINT_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *p, 558 1.1 christos const BIGNUM *x, int y_bit, 559 1.1 christos BN_CTX *ctx); 560 1.1 christos 561 1.1 christos /** Sets the x9.62 compressed coordinates of a EC_POINT. A synonym of 562 1.1 christos * EC_POINT_set_compressed_coordinates 563 1.1 christos * \param group underlying EC_GROUP object 564 1.1 christos * \param p EC_POINT object 565 1.1 christos * \param x BIGNUM with x-coordinate 566 1.1 christos * \param y_bit integer with the y-Bit (either 0 or 1) 567 1.1 christos * \param ctx BN_CTX object (optional) 568 1.1 christos * \return 1 on success and 0 if an error occurred 569 1.1 christos */ 570 1.1 christos DEPRECATEDIN_1_2_0(int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, 571 1.1 christos EC_POINT *p, 572 1.1 christos const BIGNUM *x, 573 1.1 christos int y_bit, 574 1.1 christos BN_CTX *ctx)) 575 1.1 christos # ifndef OPENSSL_NO_EC2M 576 1.1 christos /** Sets the affine coordinates of an EC_POINT. A synonym of 577 1.1 christos * EC_POINT_set_affine_coordinates 578 1.1 christos * \param group underlying EC_GROUP object 579 1.1 christos * \param p EC_POINT object 580 1.1 christos * \param x BIGNUM with the x-coordinate 581 1.1 christos * \param y BIGNUM with the y-coordinate 582 1.1 christos * \param ctx BN_CTX object (optional) 583 1.1 christos * \return 1 on success and 0 if an error occurred 584 1.1 christos */ 585 1.1 christos DEPRECATEDIN_1_2_0(int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, 586 1.1 christos EC_POINT *p, 587 1.1 christos const BIGNUM *x, 588 1.1 christos const BIGNUM *y, 589 1.1 christos BN_CTX *ctx)) 590 1.1 christos 591 1.1 christos /** Gets the affine coordinates of an EC_POINT. A synonym of 592 1.1 christos * EC_POINT_get_affine_coordinates 593 1.1 christos * \param group underlying EC_GROUP object 594 1.1 christos * \param p EC_POINT object 595 1.1 christos * \param x BIGNUM for the x-coordinate 596 1.1 christos * \param y BIGNUM for the y-coordinate 597 1.1 christos * \param ctx BN_CTX object (optional) 598 1.1 christos * \return 1 on success and 0 if an error occurred 599 1.1 christos */ 600 1.1 christos DEPRECATEDIN_1_2_0(int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, 601 1.1 christos const EC_POINT *p, 602 1.1 christos BIGNUM *x, 603 1.1 christos BIGNUM *y, 604 1.1 christos BN_CTX *ctx)) 605 1.1 christos 606 1.1 christos /** Sets the x9.62 compressed coordinates of a EC_POINT. A synonym of 607 1.1 christos * EC_POINT_set_compressed_coordinates 608 1.1 christos * \param group underlying EC_GROUP object 609 1.1 christos * \param p EC_POINT object 610 1.1 christos * \param x BIGNUM with x-coordinate 611 1.1 christos * \param y_bit integer with the y-Bit (either 0 or 1) 612 1.1 christos * \param ctx BN_CTX object (optional) 613 1.1 christos * \return 1 on success and 0 if an error occurred 614 1.1 christos */ 615 1.1 christos DEPRECATEDIN_1_2_0(int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group, 616 1.1 christos EC_POINT *p, 617 1.1 christos const BIGNUM *x, 618 1.1 christos int y_bit, 619 1.1 christos BN_CTX *ctx)) 620 1.1 christos # endif 621 1.1 christos /** Encodes a EC_POINT object to a octet string 622 1.1 christos * \param group underlying EC_GROUP object 623 1.1 christos * \param p EC_POINT object 624 1.1 christos * \param form point conversion form 625 1.1 christos * \param buf memory buffer for the result. If NULL the function returns 626 1.1 christos * required buffer size. 627 1.1 christos * \param len length of the memory buffer 628 1.1 christos * \param ctx BN_CTX object (optional) 629 1.1 christos * \return the length of the encoded octet string or 0 if an error occurred 630 1.1 christos */ 631 1.1 christos size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *p, 632 1.1 christos point_conversion_form_t form, 633 1.1 christos unsigned char *buf, size_t len, BN_CTX *ctx); 634 1.1 christos 635 1.1 christos /** Decodes a EC_POINT from a octet string 636 1.1 christos * \param group underlying EC_GROUP object 637 1.1 christos * \param p EC_POINT object 638 1.1 christos * \param buf memory buffer with the encoded ec point 639 1.1 christos * \param len length of the encoded ec point 640 1.1 christos * \param ctx BN_CTX object (optional) 641 1.1 christos * \return 1 on success and 0 if an error occurred 642 1.1 christos */ 643 1.1 christos int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *p, 644 1.1 christos const unsigned char *buf, size_t len, BN_CTX *ctx); 645 1.1 christos 646 1.1 christos /** Encodes an EC_POINT object to an allocated octet string 647 1.1 christos * \param group underlying EC_GROUP object 648 1.1 christos * \param point EC_POINT object 649 1.1 christos * \param form point conversion form 650 1.1 christos * \param pbuf returns pointer to allocated buffer 651 1.1 christos * \param ctx BN_CTX object (optional) 652 1.1 christos * \return the length of the encoded octet string or 0 if an error occurred 653 1.1 christos */ 654 1.1 christos size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point, 655 1.1 christos point_conversion_form_t form, 656 1.1 christos unsigned char **pbuf, BN_CTX *ctx); 657 1.1 christos 658 1.1 christos /* other interfaces to point2oct/oct2point: */ 659 1.1 christos BIGNUM *EC_POINT_point2bn(const EC_GROUP *, const EC_POINT *, 660 1.1 christos point_conversion_form_t form, BIGNUM *, BN_CTX *); 661 1.1 christos EC_POINT *EC_POINT_bn2point(const EC_GROUP *, const BIGNUM *, 662 1.1 christos EC_POINT *, BN_CTX *); 663 1.1 christos char *EC_POINT_point2hex(const EC_GROUP *, const EC_POINT *, 664 1.1 christos point_conversion_form_t form, BN_CTX *); 665 1.1 christos EC_POINT *EC_POINT_hex2point(const EC_GROUP *, const char *, 666 1.1 christos EC_POINT *, BN_CTX *); 667 1.1 christos 668 1.1 christos /********************************************************************/ 669 1.1 christos /* functions for doing EC_POINT arithmetic */ 670 1.1 christos /********************************************************************/ 671 1.1 christos 672 1.1 christos /** Computes the sum of two EC_POINT 673 1.1 christos * \param group underlying EC_GROUP object 674 1.1 christos * \param r EC_POINT object for the result (r = a + b) 675 1.1 christos * \param a EC_POINT object with the first summand 676 1.1 christos * \param b EC_POINT object with the second summand 677 1.1 christos * \param ctx BN_CTX object (optional) 678 1.1 christos * \return 1 on success and 0 if an error occurred 679 1.1 christos */ 680 1.1 christos int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, 681 1.1 christos const EC_POINT *b, BN_CTX *ctx); 682 1.1 christos 683 1.1 christos /** Computes the double of a EC_POINT 684 1.1 christos * \param group underlying EC_GROUP object 685 1.1 christos * \param r EC_POINT object for the result (r = 2 * a) 686 1.1 christos * \param a EC_POINT object 687 1.1 christos * \param ctx BN_CTX object (optional) 688 1.1 christos * \return 1 on success and 0 if an error occurred 689 1.1 christos */ 690 1.1 christos int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, 691 1.1 christos BN_CTX *ctx); 692 1.1 christos 693 1.1 christos /** Computes the inverse of a EC_POINT 694 1.1 christos * \param group underlying EC_GROUP object 695 1.1 christos * \param a EC_POINT object to be inverted (it's used for the result as well) 696 1.1 christos * \param ctx BN_CTX object (optional) 697 1.1 christos * \return 1 on success and 0 if an error occurred 698 1.1 christos */ 699 1.1 christos int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx); 700 1.1 christos 701 1.1 christos /** Checks whether the point is the neutral element of the group 702 1.1 christos * \param group the underlying EC_GROUP object 703 1.1 christos * \param p EC_POINT object 704 1.1 christos * \return 1 if the point is the neutral element and 0 otherwise 705 1.1 christos */ 706 1.1 christos int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *p); 707 1.1 christos 708 1.1 christos /** Checks whether the point is on the curve 709 1.1 christos * \param group underlying EC_GROUP object 710 1.1 christos * \param point EC_POINT object to check 711 1.1 christos * \param ctx BN_CTX object (optional) 712 1.1 christos * \return 1 if the point is on the curve, 0 if not, or -1 on error 713 1.1 christos */ 714 1.1 christos int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point, 715 1.1 christos BN_CTX *ctx); 716 1.1 christos 717 1.1 christos /** Compares two EC_POINTs 718 1.1 christos * \param group underlying EC_GROUP object 719 1.1 christos * \param a first EC_POINT object 720 1.1 christos * \param b second EC_POINT object 721 1.1 christos * \param ctx BN_CTX object (optional) 722 1.1 christos * \return 1 if the points are not equal, 0 if they are, or -1 on error 723 1.1 christos */ 724 1.1 christos int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, 725 1.1 christos BN_CTX *ctx); 726 1.1 christos 727 1.1 christos int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx); 728 1.1 christos int EC_POINTs_make_affine(const EC_GROUP *group, size_t num, 729 1.1 christos EC_POINT *points[], BN_CTX *ctx); 730 1.1 christos 731 1.1 christos /** Computes r = generator * n + sum_{i=0}^{num-1} p[i] * m[i] 732 1.1 christos * \param group underlying EC_GROUP object 733 1.1 christos * \param r EC_POINT object for the result 734 1.1 christos * \param n BIGNUM with the multiplier for the group generator (optional) 735 1.1 christos * \param num number further summands 736 1.1 christos * \param p array of size num of EC_POINT objects 737 1.1 christos * \param m array of size num of BIGNUM objects 738 1.1 christos * \param ctx BN_CTX object (optional) 739 1.1 christos * \return 1 on success and 0 if an error occurred 740 1.1 christos */ 741 1.1 christos int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n, 742 1.1 christos size_t num, const EC_POINT *p[], const BIGNUM *m[], 743 1.1 christos BN_CTX *ctx); 744 1.1 christos 745 1.1 christos /** Computes r = generator * n + q * m 746 1.1 christos * \param group underlying EC_GROUP object 747 1.1 christos * \param r EC_POINT object for the result 748 1.1 christos * \param n BIGNUM with the multiplier for the group generator (optional) 749 1.1 christos * \param q EC_POINT object with the first factor of the second summand 750 1.1 christos * \param m BIGNUM with the second factor of the second summand 751 1.1 christos * \param ctx BN_CTX object (optional) 752 1.1 christos * \return 1 on success and 0 if an error occurred 753 1.1 christos */ 754 1.1 christos int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n, 755 1.1 christos const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx); 756 1.1 christos 757 1.1 christos /** Stores multiples of generator for faster point multiplication 758 1.1 christos * \param group EC_GROUP object 759 1.1 christos * \param ctx BN_CTX object (optional) 760 1.1 christos * \return 1 on success and 0 if an error occurred 761 1.1 christos */ 762 1.1 christos int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx); 763 1.1 christos 764 1.1 christos /** Reports whether a precomputation has been done 765 1.1 christos * \param group EC_GROUP object 766 1.1 christos * \return 1 if a pre-computation has been done and 0 otherwise 767 1.1 christos */ 768 1.1 christos int EC_GROUP_have_precompute_mult(const EC_GROUP *group); 769 1.1 christos 770 1.1 christos /********************************************************************/ 771 1.1 christos /* ASN1 stuff */ 772 1.1 christos /********************************************************************/ 773 1.1 christos 774 1.1 christos DECLARE_ASN1_ITEM(ECPKPARAMETERS) 775 1.1 christos DECLARE_ASN1_ALLOC_FUNCTIONS(ECPKPARAMETERS) 776 1.1 christos DECLARE_ASN1_ITEM(ECPARAMETERS) 777 1.1 christos DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS) 778 1.1 christos 779 1.1 christos /* 780 1.1 christos * EC_GROUP_get_basis_type() returns the NID of the basis type used to 781 1.1 christos * represent the field elements 782 1.1 christos */ 783 1.1 christos int EC_GROUP_get_basis_type(const EC_GROUP *); 784 1.1 christos # ifndef OPENSSL_NO_EC2M 785 1.1 christos int EC_GROUP_get_trinomial_basis(const EC_GROUP *, unsigned int *k); 786 1.1 christos int EC_GROUP_get_pentanomial_basis(const EC_GROUP *, unsigned int *k1, 787 1.1 christos unsigned int *k2, unsigned int *k3); 788 1.1 christos # endif 789 1.1 christos 790 1.1 christos # define OPENSSL_EC_EXPLICIT_CURVE 0x000 791 1.1 christos # define OPENSSL_EC_NAMED_CURVE 0x001 792 1.1 christos 793 1.1 christos EC_GROUP *d2i_ECPKParameters(EC_GROUP **, const unsigned char **in, long len); 794 1.1 christos int i2d_ECPKParameters(const EC_GROUP *, unsigned char **out); 795 1.1 christos 796 1.1 christos # define d2i_ECPKParameters_bio(bp,x) \ 797 1.1 christos ASN1_d2i_bio_of(EC_GROUP, NULL, d2i_ECPKParameters, bp, x) 798 1.1 christos # define i2d_ECPKParameters_bio(bp,x) \ 799 1.1 christos ASN1_i2d_bio_of_const(EC_GROUP, i2d_ECPKParameters, bp, x) 800 1.1 christos # define d2i_ECPKParameters_fp(fp,x) \ 801 1.1 christos (EC_GROUP *)ASN1_d2i_fp(NULL, (d2i_of_void *)d2i_ECPKParameters, (fp), \ 802 1.1 christos (void **)(x)) 803 1.1 christos # define i2d_ECPKParameters_fp(fp,x) \ 804 1.1 christos ASN1_i2d_fp((i2d_of_void *)i2d_ECPKParameters, (fp), (void *)(x)) 805 1.1 christos 806 1.1 christos int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off); 807 1.1 christos # ifndef OPENSSL_NO_STDIO 808 1.1 christos int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off); 809 1.1 christos # endif 810 1.1 christos 811 1.1 christos /********************************************************************/ 812 1.1 christos /* EC_KEY functions */ 813 1.1 christos /********************************************************************/ 814 1.1 christos 815 1.1 christos /* some values for the encoding_flag */ 816 1.1 christos # define EC_PKEY_NO_PARAMETERS 0x001 817 1.1 christos # define EC_PKEY_NO_PUBKEY 0x002 818 1.1 christos 819 1.1 christos /* some values for the flags field */ 820 1.1 christos # define EC_FLAG_NON_FIPS_ALLOW 0x1 821 1.1 christos # define EC_FLAG_FIPS_CHECKED 0x2 822 1.1 christos # define EC_FLAG_COFACTOR_ECDH 0x1000 823 1.1 christos 824 1.1 christos /** Creates a new EC_KEY object. 825 1.1 christos * \return EC_KEY object or NULL if an error occurred. 826 1.1 christos */ 827 1.1 christos EC_KEY *EC_KEY_new(void); 828 1.1 christos 829 1.1 christos int EC_KEY_get_flags(const EC_KEY *key); 830 1.1 christos 831 1.1 christos void EC_KEY_set_flags(EC_KEY *key, int flags); 832 1.1 christos 833 1.1 christos void EC_KEY_clear_flags(EC_KEY *key, int flags); 834 1.1 christos 835 1.1 christos int EC_KEY_decoded_from_explicit_params(const EC_KEY *key); 836 1.1 christos 837 1.1 christos /** Creates a new EC_KEY object using a named curve as underlying 838 1.1 christos * EC_GROUP object. 839 1.1 christos * \param nid NID of the named curve. 840 1.1 christos * \return EC_KEY object or NULL if an error occurred. 841 1.1 christos */ 842 1.1 christos EC_KEY *EC_KEY_new_by_curve_name(int nid); 843 1.1 christos 844 1.1 christos /** Frees a EC_KEY object. 845 1.1 christos * \param key EC_KEY object to be freed. 846 1.1 christos */ 847 1.1 christos void EC_KEY_free(EC_KEY *key); 848 1.1 christos 849 1.1 christos /** Copies a EC_KEY object. 850 1.1 christos * \param dst destination EC_KEY object 851 1.1 christos * \param src src EC_KEY object 852 1.1 christos * \return dst or NULL if an error occurred. 853 1.1 christos */ 854 1.1 christos EC_KEY *EC_KEY_copy(EC_KEY *dst, const EC_KEY *src); 855 1.1 christos 856 1.1 christos /** Creates a new EC_KEY object and copies the content from src to it. 857 1.1 christos * \param src the source EC_KEY object 858 1.1 christos * \return newly created EC_KEY object or NULL if an error occurred. 859 1.1 christos */ 860 1.1 christos EC_KEY *EC_KEY_dup(const EC_KEY *src); 861 1.1 christos 862 1.1 christos /** Increases the internal reference count of a EC_KEY object. 863 1.1 christos * \param key EC_KEY object 864 1.1 christos * \return 1 on success and 0 if an error occurred. 865 1.1 christos */ 866 1.1 christos int EC_KEY_up_ref(EC_KEY *key); 867 1.1 christos 868 1.1 christos /** Returns the ENGINE object of a EC_KEY object 869 1.1 christos * \param eckey EC_KEY object 870 1.1 christos * \return the ENGINE object (possibly NULL). 871 1.1 christos */ 872 1.1 christos ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey); 873 1.1 christos 874 1.1 christos /** Returns the EC_GROUP object of a EC_KEY object 875 1.1 christos * \param key EC_KEY object 876 1.1 christos * \return the EC_GROUP object (possibly NULL). 877 1.1 christos */ 878 1.1 christos const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key); 879 1.1 christos 880 1.1 christos /** Sets the EC_GROUP of a EC_KEY object. 881 1.1 christos * \param key EC_KEY object 882 1.1 christos * \param group EC_GROUP to use in the EC_KEY object (note: the EC_KEY 883 1.1 christos * object will use an own copy of the EC_GROUP). 884 1.1 christos * \return 1 on success and 0 if an error occurred. 885 1.1 christos */ 886 1.1 christos int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group); 887 1.1 christos 888 1.1 christos /** Returns the private key of a EC_KEY object. 889 1.1 christos * \param key EC_KEY object 890 1.1 christos * \return a BIGNUM with the private key (possibly NULL). 891 1.1 christos */ 892 1.1 christos const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key); 893 1.1 christos 894 1.1 christos /** Sets the private key of a EC_KEY object. 895 1.1 christos * \param key EC_KEY object 896 1.1 christos * \param prv BIGNUM with the private key (note: the EC_KEY object 897 1.1 christos * will use an own copy of the BIGNUM). 898 1.1 christos * \return 1 on success and 0 if an error occurred. 899 1.1 christos */ 900 1.1 christos int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *prv); 901 1.1 christos 902 1.1 christos /** Returns the public key of a EC_KEY object. 903 1.1 christos * \param key the EC_KEY object 904 1.1 christos * \return a EC_POINT object with the public key (possibly NULL) 905 1.1 christos */ 906 1.1 christos const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key); 907 1.1 christos 908 1.1 christos /** Sets the public key of a EC_KEY object. 909 1.1 christos * \param key EC_KEY object 910 1.1 christos * \param pub EC_POINT object with the public key (note: the EC_KEY object 911 1.1 christos * will use an own copy of the EC_POINT object). 912 1.1 christos * \return 1 on success and 0 if an error occurred. 913 1.1 christos */ 914 1.1 christos int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub); 915 1.1 christos 916 1.1 christos unsigned EC_KEY_get_enc_flags(const EC_KEY *key); 917 1.1 christos void EC_KEY_set_enc_flags(EC_KEY *eckey, unsigned int flags); 918 1.1 christos point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key); 919 1.1 christos void EC_KEY_set_conv_form(EC_KEY *eckey, point_conversion_form_t cform); 920 1.1 christos 921 1.1 christos #define EC_KEY_get_ex_new_index(l, p, newf, dupf, freef) \ 922 1.1 christos CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_EC_KEY, l, p, newf, dupf, freef) 923 1.1 christos int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg); 924 1.1 christos void *EC_KEY_get_ex_data(const EC_KEY *key, int idx); 925 1.1 christos 926 1.1 christos /* wrapper functions for the underlying EC_GROUP object */ 927 1.1 christos void EC_KEY_set_asn1_flag(EC_KEY *eckey, int asn1_flag); 928 1.1 christos 929 1.1 christos /** Creates a table of pre-computed multiples of the generator to 930 1.1 christos * accelerate further EC_KEY operations. 931 1.1 christos * \param key EC_KEY object 932 1.1 christos * \param ctx BN_CTX object (optional) 933 1.1 christos * \return 1 on success and 0 if an error occurred. 934 1.1 christos */ 935 1.1 christos int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx); 936 1.1 christos 937 1.1 christos /** Creates a new ec private (and optional a new public) key. 938 1.1 christos * \param key EC_KEY object 939 1.1 christos * \return 1 on success and 0 if an error occurred. 940 1.1 christos */ 941 1.1 christos int EC_KEY_generate_key(EC_KEY *key); 942 1.1 christos 943 1.1 christos /** Verifies that a private and/or public key is valid. 944 1.1 christos * \param key the EC_KEY object 945 1.1 christos * \return 1 on success and 0 otherwise. 946 1.1 christos */ 947 1.1 christos int EC_KEY_check_key(const EC_KEY *key); 948 1.1 christos 949 1.1 christos /** Indicates if an EC_KEY can be used for signing. 950 1.1 christos * \param eckey the EC_KEY object 951 1.1 christos * \return 1 if can can sign and 0 otherwise. 952 1.1 christos */ 953 1.1 christos int EC_KEY_can_sign(const EC_KEY *eckey); 954 1.1 christos 955 1.1 christos /** Sets a public key from affine coordinates performing 956 1.1 christos * necessary NIST PKV tests. 957 1.1 christos * \param key the EC_KEY object 958 1.1 christos * \param x public key x coordinate 959 1.1 christos * \param y public key y coordinate 960 1.1 christos * \return 1 on success and 0 otherwise. 961 1.1 christos */ 962 1.1 christos int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x, 963 1.1 christos BIGNUM *y); 964 1.1 christos 965 1.1 christos /** Encodes an EC_KEY public key to an allocated octet string 966 1.1 christos * \param key key to encode 967 1.1 christos * \param form point conversion form 968 1.1 christos * \param pbuf returns pointer to allocated buffer 969 1.1 christos * \param ctx BN_CTX object (optional) 970 1.1 christos * \return the length of the encoded octet string or 0 if an error occurred 971 1.1 christos */ 972 1.1 christos size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form, 973 1.1 christos unsigned char **pbuf, BN_CTX *ctx); 974 1.1 christos 975 1.1 christos /** Decodes a EC_KEY public key from a octet string 976 1.1 christos * \param key key to decode 977 1.1 christos * \param buf memory buffer with the encoded ec point 978 1.1 christos * \param len length of the encoded ec point 979 1.1 christos * \param ctx BN_CTX object (optional) 980 1.1 christos * \return 1 on success and 0 if an error occurred 981 1.1 christos */ 982 1.1 christos 983 1.1 christos int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len, 984 1.1 christos BN_CTX *ctx); 985 1.1 christos 986 1.1 christos /** Decodes an EC_KEY private key from an octet string 987 1.1 christos * \param key key to decode 988 1.1 christos * \param buf memory buffer with the encoded private key 989 1.1 christos * \param len length of the encoded key 990 1.1 christos * \return 1 on success and 0 if an error occurred 991 1.1 christos */ 992 1.1 christos 993 1.1 christos int EC_KEY_oct2priv(EC_KEY *key, const unsigned char *buf, size_t len); 994 1.1 christos 995 1.1 christos /** Encodes a EC_KEY private key to an octet string 996 1.1 christos * \param key key to encode 997 1.1 christos * \param buf memory buffer for the result. If NULL the function returns 998 1.1 christos * required buffer size. 999 1.1 christos * \param len length of the memory buffer 1000 1.1 christos * \return the length of the encoded octet string or 0 if an error occurred 1001 1.1 christos */ 1002 1.1 christos 1003 1.1 christos size_t EC_KEY_priv2oct(const EC_KEY *key, unsigned char *buf, size_t len); 1004 1.1 christos 1005 1.1 christos /** Encodes an EC_KEY private key to an allocated octet string 1006 1.1 christos * \param eckey key to encode 1007 1.1 christos * \param pbuf returns pointer to allocated buffer 1008 1.1 christos * \return the length of the encoded octet string or 0 if an error occurred 1009 1.1 christos */ 1010 1.1 christos size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf); 1011 1.1 christos 1012 1.1 christos /********************************************************************/ 1013 1.1 christos /* de- and encoding functions for SEC1 ECPrivateKey */ 1014 1.1 christos /********************************************************************/ 1015 1.1 christos 1016 1.1 christos /** Decodes a private key from a memory buffer. 1017 1.1 christos * \param key a pointer to a EC_KEY object which should be used (or NULL) 1018 1.1 christos * \param in pointer to memory with the DER encoded private key 1019 1.1 christos * \param len length of the DER encoded private key 1020 1.1 christos * \return the decoded private key or NULL if an error occurred. 1021 1.1 christos */ 1022 1.1 christos EC_KEY *d2i_ECPrivateKey(EC_KEY **key, const unsigned char **in, long len); 1023 1.1 christos 1024 1.1 christos /** Encodes a private key object and stores the result in a buffer. 1025 1.1 christos * \param key the EC_KEY object to encode 1026 1.1 christos * \param out the buffer for the result (if NULL the function returns number 1027 1.1 christos * of bytes needed). 1028 1.1 christos * \return 1 on success and 0 if an error occurred. 1029 1.1 christos */ 1030 1.1 christos int i2d_ECPrivateKey(EC_KEY *key, unsigned char **out); 1031 1.1 christos 1032 1.1 christos /********************************************************************/ 1033 1.1 christos /* de- and encoding functions for EC parameters */ 1034 1.1 christos /********************************************************************/ 1035 1.1 christos 1036 1.1 christos /** Decodes ec parameter from a memory buffer. 1037 1.1 christos * \param key a pointer to a EC_KEY object which should be used (or NULL) 1038 1.1 christos * \param in pointer to memory with the DER encoded ec parameters 1039 1.1 christos * \param len length of the DER encoded ec parameters 1040 1.1 christos * \return a EC_KEY object with the decoded parameters or NULL if an error 1041 1.1 christos * occurred. 1042 1.1 christos */ 1043 1.1 christos EC_KEY *d2i_ECParameters(EC_KEY **key, const unsigned char **in, long len); 1044 1.1 christos 1045 1.1 christos /** Encodes ec parameter and stores the result in a buffer. 1046 1.1 christos * \param key the EC_KEY object with ec parameters to encode 1047 1.1 christos * \param out the buffer for the result (if NULL the function returns number 1048 1.1 christos * of bytes needed). 1049 1.1 christos * \return 1 on success and 0 if an error occurred. 1050 1.1 christos */ 1051 1.1 christos int i2d_ECParameters(EC_KEY *key, unsigned char **out); 1052 1.1 christos 1053 1.1 christos /********************************************************************/ 1054 1.1 christos /* de- and encoding functions for EC public key */ 1055 1.1 christos /* (octet string, not DER -- hence 'o2i' and 'i2o') */ 1056 1.1 christos /********************************************************************/ 1057 1.1 christos 1058 1.1 christos /** Decodes a ec public key from a octet string. 1059 1.1 christos * \param key a pointer to a EC_KEY object which should be used 1060 1.1 christos * \param in memory buffer with the encoded public key 1061 1.1 christos * \param len length of the encoded public key 1062 1.1 christos * \return EC_KEY object with decoded public key or NULL if an error 1063 1.1 christos * occurred. 1064 1.1 christos */ 1065 1.1 christos EC_KEY *o2i_ECPublicKey(EC_KEY **key, const unsigned char **in, long len); 1066 1.1 christos 1067 1.1 christos /** Encodes a ec public key in an octet string. 1068 1.1 christos * \param key the EC_KEY object with the public key 1069 1.1 christos * \param out the buffer for the result (if NULL the function returns number 1070 1.1 christos * of bytes needed). 1071 1.1 christos * \return 1 on success and 0 if an error occurred 1072 1.1 christos */ 1073 1.1 christos int i2o_ECPublicKey(const EC_KEY *key, unsigned char **out); 1074 1.1 christos 1075 1.1 christos /** Prints out the ec parameters on human readable form. 1076 1.1 christos * \param bp BIO object to which the information is printed 1077 1.1 christos * \param key EC_KEY object 1078 1.1 christos * \return 1 on success and 0 if an error occurred 1079 1.1 christos */ 1080 1.1 christos int ECParameters_print(BIO *bp, const EC_KEY *key); 1081 1.1 christos 1082 1.1 christos /** Prints out the contents of a EC_KEY object 1083 1.1 christos * \param bp BIO object to which the information is printed 1084 1.1 christos * \param key EC_KEY object 1085 1.1 christos * \param off line offset 1086 1.1 christos * \return 1 on success and 0 if an error occurred 1087 1.1 christos */ 1088 1.1 christos int EC_KEY_print(BIO *bp, const EC_KEY *key, int off); 1089 1.1 christos 1090 1.1 christos # ifndef OPENSSL_NO_STDIO 1091 1.1 christos /** Prints out the ec parameters on human readable form. 1092 1.1 christos * \param fp file descriptor to which the information is printed 1093 1.1 christos * \param key EC_KEY object 1094 1.1 christos * \return 1 on success and 0 if an error occurred 1095 1.1 christos */ 1096 1.1 christos int ECParameters_print_fp(FILE *fp, const EC_KEY *key); 1097 1.1 christos 1098 1.1 christos /** Prints out the contents of a EC_KEY object 1099 1.1 christos * \param fp file descriptor to which the information is printed 1100 1.1 christos * \param key EC_KEY object 1101 1.1 christos * \param off line offset 1102 1.1 christos * \return 1 on success and 0 if an error occurred 1103 1.1 christos */ 1104 1.1 christos int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off); 1105 1.1 christos 1106 1.1 christos # endif 1107 1.1 christos 1108 1.1 christos const EC_KEY_METHOD *EC_KEY_OpenSSL(void); 1109 1.1 christos const EC_KEY_METHOD *EC_KEY_get_default_method(void); 1110 1.1 christos void EC_KEY_set_default_method(const EC_KEY_METHOD *meth); 1111 1.1 christos const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key); 1112 1.1 christos int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth); 1113 1.1 christos EC_KEY *EC_KEY_new_method(ENGINE *engine); 1114 1.1 christos 1115 1.1 christos /** The old name for ecdh_KDF_X9_63 1116 1.1 christos * The ECDH KDF specification has been mistakingly attributed to ANSI X9.62, 1117 1.1 christos * it is actually specified in ANSI X9.63. 1118 1.1 christos * This identifier is retained for backwards compatibility 1119 1.1 christos */ 1120 1.1 christos int ECDH_KDF_X9_62(unsigned char *out, size_t outlen, 1121 1.1 christos const unsigned char *Z, size_t Zlen, 1122 1.1 christos const unsigned char *sinfo, size_t sinfolen, 1123 1.1 christos const EVP_MD *md); 1124 1.1 christos 1125 1.1 christos int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key, 1126 1.1 christos const EC_KEY *ecdh, 1127 1.1 christos void *(*KDF) (const void *in, size_t inlen, 1128 1.1 christos void *out, size_t *outlen)); 1129 1.1 christos 1130 1.1 christos typedef struct ECDSA_SIG_st ECDSA_SIG; 1131 1.1 christos 1132 1.1 christos /** Allocates and initialize a ECDSA_SIG structure 1133 1.1 christos * \return pointer to a ECDSA_SIG structure or NULL if an error occurred 1134 1.1 christos */ 1135 1.1 christos ECDSA_SIG *ECDSA_SIG_new(void); 1136 1.1 christos 1137 1.1 christos /** frees a ECDSA_SIG structure 1138 1.1 christos * \param sig pointer to the ECDSA_SIG structure 1139 1.1 christos */ 1140 1.1 christos void ECDSA_SIG_free(ECDSA_SIG *sig); 1141 1.1 christos 1142 1.1 christos /** DER encode content of ECDSA_SIG object (note: this function modifies *pp 1143 1.1 christos * (*pp += length of the DER encoded signature)). 1144 1.1 christos * \param sig pointer to the ECDSA_SIG object 1145 1.1 christos * \param pp pointer to a unsigned char pointer for the output or NULL 1146 1.1 christos * \return the length of the DER encoded ECDSA_SIG object or a negative value 1147 1.1 christos * on error 1148 1.1 christos */ 1149 1.1 christos int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp); 1150 1.1 christos 1151 1.1 christos /** Decodes a DER encoded ECDSA signature (note: this function changes *pp 1152 1.1 christos * (*pp += len)). 1153 1.1 christos * \param sig pointer to ECDSA_SIG pointer (may be NULL) 1154 1.1 christos * \param pp memory buffer with the DER encoded signature 1155 1.1 christos * \param len length of the buffer 1156 1.1 christos * \return pointer to the decoded ECDSA_SIG structure (or NULL) 1157 1.1 christos */ 1158 1.1 christos ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, long len); 1159 1.1 christos 1160 1.1 christos /** Accessor for r and s fields of ECDSA_SIG 1161 1.1 christos * \param sig pointer to ECDSA_SIG structure 1162 1.1 christos * \param pr pointer to BIGNUM pointer for r (may be NULL) 1163 1.1 christos * \param ps pointer to BIGNUM pointer for s (may be NULL) 1164 1.1 christos */ 1165 1.1 christos void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps); 1166 1.1 christos 1167 1.1 christos /** Accessor for r field of ECDSA_SIG 1168 1.1 christos * \param sig pointer to ECDSA_SIG structure 1169 1.1 christos */ 1170 1.1 christos const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig); 1171 1.1 christos 1172 1.1 christos /** Accessor for s field of ECDSA_SIG 1173 1.1 christos * \param sig pointer to ECDSA_SIG structure 1174 1.1 christos */ 1175 1.1 christos const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig); 1176 1.1 christos 1177 1.1 christos /** Setter for r and s fields of ECDSA_SIG 1178 1.1 christos * \param sig pointer to ECDSA_SIG structure 1179 1.1 christos * \param r pointer to BIGNUM for r (may be NULL) 1180 1.1 christos * \param s pointer to BIGNUM for s (may be NULL) 1181 1.1 christos */ 1182 1.1 christos int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s); 1183 1.1 christos 1184 1.1 christos /** Computes the ECDSA signature of the given hash value using 1185 1.1 christos * the supplied private key and returns the created signature. 1186 1.1 christos * \param dgst pointer to the hash value 1187 1.1 christos * \param dgst_len length of the hash value 1188 1.1 christos * \param eckey EC_KEY object containing a private EC key 1189 1.1 christos * \return pointer to a ECDSA_SIG structure or NULL if an error occurred 1190 1.1 christos */ 1191 1.1 christos ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dgst_len, 1192 1.1 christos EC_KEY *eckey); 1193 1.1 christos 1194 1.1 christos /** Computes ECDSA signature of a given hash value using the supplied 1195 1.1 christos * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). 1196 1.1 christos * \param dgst pointer to the hash value to sign 1197 1.1 christos * \param dgstlen length of the hash value 1198 1.1 christos * \param kinv BIGNUM with a pre-computed inverse k (optional) 1199 1.1 christos * \param rp BIGNUM with a pre-computed rp value (optional), 1200 1.1 christos * see ECDSA_sign_setup 1201 1.1 christos * \param eckey EC_KEY object containing a private EC key 1202 1.1 christos * \return pointer to a ECDSA_SIG structure or NULL if an error occurred 1203 1.1 christos */ 1204 1.1 christos ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen, 1205 1.1 christos const BIGNUM *kinv, const BIGNUM *rp, 1206 1.1 christos EC_KEY *eckey); 1207 1.1 christos 1208 1.1 christos /** Verifies that the supplied signature is a valid ECDSA 1209 1.1 christos * signature of the supplied hash value using the supplied public key. 1210 1.1 christos * \param dgst pointer to the hash value 1211 1.1 christos * \param dgst_len length of the hash value 1212 1.1 christos * \param sig ECDSA_SIG structure 1213 1.1 christos * \param eckey EC_KEY object containing a public EC key 1214 1.1 christos * \return 1 if the signature is valid, 0 if the signature is invalid 1215 1.1 christos * and -1 on error 1216 1.1 christos */ 1217 1.1 christos int ECDSA_do_verify(const unsigned char *dgst, int dgst_len, 1218 1.1 christos const ECDSA_SIG *sig, EC_KEY *eckey); 1219 1.1 christos 1220 1.1 christos /** Precompute parts of the signing operation 1221 1.1 christos * \param eckey EC_KEY object containing a private EC key 1222 1.1 christos * \param ctx BN_CTX object (optional) 1223 1.1 christos * \param kinv BIGNUM pointer for the inverse of k 1224 1.1 christos * \param rp BIGNUM pointer for x coordinate of k * generator 1225 1.1 christos * \return 1 on success and 0 otherwise 1226 1.1 christos */ 1227 1.1 christos int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp); 1228 1.1 christos 1229 1.1 christos /** Computes ECDSA signature of a given hash value using the supplied 1230 1.1 christos * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). 1231 1.1 christos * \param type this parameter is ignored 1232 1.1 christos * \param dgst pointer to the hash value to sign 1233 1.1 christos * \param dgstlen length of the hash value 1234 1.1 christos * \param sig memory for the DER encoded created signature 1235 1.1 christos * \param siglen pointer to the length of the returned signature 1236 1.1 christos * \param eckey EC_KEY object containing a private EC key 1237 1.1 christos * \return 1 on success and 0 otherwise 1238 1.1 christos */ 1239 1.1 christos int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen, 1240 1.1 christos unsigned char *sig, unsigned int *siglen, EC_KEY *eckey); 1241 1.1 christos 1242 1.1 christos /** Computes ECDSA signature of a given hash value using the supplied 1243 1.1 christos * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). 1244 1.1 christos * \param type this parameter is ignored 1245 1.1 christos * \param dgst pointer to the hash value to sign 1246 1.1 christos * \param dgstlen length of the hash value 1247 1.1 christos * \param sig buffer to hold the DER encoded signature 1248 1.1 christos * \param siglen pointer to the length of the returned signature 1249 1.1 christos * \param kinv BIGNUM with a pre-computed inverse k (optional) 1250 1.1 christos * \param rp BIGNUM with a pre-computed rp value (optional), 1251 1.1 christos * see ECDSA_sign_setup 1252 1.1 christos * \param eckey EC_KEY object containing a private EC key 1253 1.1 christos * \return 1 on success and 0 otherwise 1254 1.1 christos */ 1255 1.1 christos int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen, 1256 1.1 christos unsigned char *sig, unsigned int *siglen, 1257 1.1 christos const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey); 1258 1.1 christos 1259 1.1 christos /** Verifies that the given signature is valid ECDSA signature 1260 1.1 christos * of the supplied hash value using the specified public key. 1261 1.1 christos * \param type this parameter is ignored 1262 1.1 christos * \param dgst pointer to the hash value 1263 1.1 christos * \param dgstlen length of the hash value 1264 1.1 christos * \param sig pointer to the DER encoded signature 1265 1.1 christos * \param siglen length of the DER encoded signature 1266 1.1 christos * \param eckey EC_KEY object containing a public EC key 1267 1.1 christos * \return 1 if the signature is valid, 0 if the signature is invalid 1268 1.1 christos * and -1 on error 1269 1.1 christos */ 1270 1.1 christos int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen, 1271 1.1 christos const unsigned char *sig, int siglen, EC_KEY *eckey); 1272 1.1 christos 1273 1.1 christos /** Returns the maximum length of the DER encoded signature 1274 1.1 christos * \param eckey EC_KEY object 1275 1.1 christos * \return numbers of bytes required for the DER encoded signature 1276 1.1 christos */ 1277 1.1 christos int ECDSA_size(const EC_KEY *eckey); 1278 1.1 christos 1279 1.1 christos /********************************************************************/ 1280 1.1 christos /* EC_KEY_METHOD constructors, destructors, writers and accessors */ 1281 1.1 christos /********************************************************************/ 1282 1.1 christos 1283 1.1 christos EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth); 1284 1.1 christos void EC_KEY_METHOD_free(EC_KEY_METHOD *meth); 1285 1.1 christos void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth, 1286 1.1 christos int (*init)(EC_KEY *key), 1287 1.1 christos void (*finish)(EC_KEY *key), 1288 1.1 christos int (*copy)(EC_KEY *dest, const EC_KEY *src), 1289 1.1 christos int (*set_group)(EC_KEY *key, const EC_GROUP *grp), 1290 1.1 christos int (*set_private)(EC_KEY *key, 1291 1.1 christos const BIGNUM *priv_key), 1292 1.1 christos int (*set_public)(EC_KEY *key, 1293 1.1 christos const EC_POINT *pub_key)); 1294 1.1 christos 1295 1.1 christos void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth, 1296 1.1 christos int (*keygen)(EC_KEY *key)); 1297 1.1 christos 1298 1.1 christos void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth, 1299 1.1 christos int (*ckey)(unsigned char **psec, 1300 1.1 christos size_t *pseclen, 1301 1.1 christos const EC_POINT *pub_key, 1302 1.1 christos const EC_KEY *ecdh)); 1303 1.1 christos 1304 1.1 christos void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth, 1305 1.1 christos int (*sign)(int type, const unsigned char *dgst, 1306 1.1 christos int dlen, unsigned char *sig, 1307 1.1 christos unsigned int *siglen, 1308 1.1 christos const BIGNUM *kinv, const BIGNUM *r, 1309 1.1 christos EC_KEY *eckey), 1310 1.1 christos int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, 1311 1.1 christos BIGNUM **kinvp, BIGNUM **rp), 1312 1.1 christos ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, 1313 1.1 christos int dgst_len, 1314 1.1 christos const BIGNUM *in_kinv, 1315 1.1 christos const BIGNUM *in_r, 1316 1.1 christos EC_KEY *eckey)); 1317 1.1 christos 1318 1.1 christos void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth, 1319 1.1 christos int (*verify)(int type, const unsigned 1320 1.1 christos char *dgst, int dgst_len, 1321 1.1 christos const unsigned char *sigbuf, 1322 1.1 christos int sig_len, EC_KEY *eckey), 1323 1.1 christos int (*verify_sig)(const unsigned char *dgst, 1324 1.1 christos int dgst_len, 1325 1.1 christos const ECDSA_SIG *sig, 1326 1.1 christos EC_KEY *eckey)); 1327 1.1 christos 1328 1.1 christos void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth, 1329 1.1 christos int (**pinit)(EC_KEY *key), 1330 1.1 christos void (**pfinish)(EC_KEY *key), 1331 1.1 christos int (**pcopy)(EC_KEY *dest, const EC_KEY *src), 1332 1.1 christos int (**pset_group)(EC_KEY *key, 1333 1.1 christos const EC_GROUP *grp), 1334 1.1 christos int (**pset_private)(EC_KEY *key, 1335 1.1 christos const BIGNUM *priv_key), 1336 1.1 christos int (**pset_public)(EC_KEY *key, 1337 1.1 christos const EC_POINT *pub_key)); 1338 1.1 christos 1339 1.1 christos void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth, 1340 1.1 christos int (**pkeygen)(EC_KEY *key)); 1341 1.1 christos 1342 1.1 christos void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth, 1343 1.1 christos int (**pck)(unsigned char **psec, 1344 1.1 christos size_t *pseclen, 1345 1.1 christos const EC_POINT *pub_key, 1346 1.1 christos const EC_KEY *ecdh)); 1347 1.1 christos 1348 1.1 christos void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth, 1349 1.1 christos int (**psign)(int type, const unsigned char *dgst, 1350 1.1 christos int dlen, unsigned char *sig, 1351 1.1 christos unsigned int *siglen, 1352 1.1 christos const BIGNUM *kinv, const BIGNUM *r, 1353 1.1 christos EC_KEY *eckey), 1354 1.1 christos int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, 1355 1.1 christos BIGNUM **kinvp, BIGNUM **rp), 1356 1.1 christos ECDSA_SIG *(**psign_sig)(const unsigned char *dgst, 1357 1.1 christos int dgst_len, 1358 1.1 christos const BIGNUM *in_kinv, 1359 1.1 christos const BIGNUM *in_r, 1360 1.1 christos EC_KEY *eckey)); 1361 1.1 christos 1362 1.1 christos void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth, 1363 1.1 christos int (**pverify)(int type, const unsigned 1364 1.1 christos char *dgst, int dgst_len, 1365 1.1 christos const unsigned char *sigbuf, 1366 1.1 christos int sig_len, EC_KEY *eckey), 1367 1.1 christos int (**pverify_sig)(const unsigned char *dgst, 1368 1.1 christos int dgst_len, 1369 1.1 christos const ECDSA_SIG *sig, 1370 1.1 christos EC_KEY *eckey)); 1371 1.1 christos 1372 1.1 christos # define ECParameters_dup(x) ASN1_dup_of(EC_KEY,i2d_ECParameters,d2i_ECParameters,x) 1373 1.1 christos 1374 1.1 christos # ifndef __cplusplus 1375 1.1 christos # if defined(__SUNPRO_C) 1376 1.1 christos # if __SUNPRO_C >= 0x520 1377 1.1 christos # pragma error_messages (default,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE) 1378 1.1 christos # endif 1379 1.1 christos # endif 1380 1.1 christos # endif 1381 1.1 christos 1382 1.1 christos # define EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid) \ 1383 1.1 christos EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1384 1.1 christos EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \ 1385 1.1 christos EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, nid, NULL) 1386 1.1 christos 1387 1.1 christos # define EVP_PKEY_CTX_set_ec_param_enc(ctx, flag) \ 1388 1.1 christos EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1389 1.1 christos EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \ 1390 1.1 christos EVP_PKEY_CTRL_EC_PARAM_ENC, flag, NULL) 1391 1.1 christos 1392 1.1 christos # define EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, flag) \ 1393 1.1 christos EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1394 1.1 christos EVP_PKEY_OP_DERIVE, \ 1395 1.1 christos EVP_PKEY_CTRL_EC_ECDH_COFACTOR, flag, NULL) 1396 1.1 christos 1397 1.1 christos # define EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx) \ 1398 1.1 christos EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1399 1.1 christos EVP_PKEY_OP_DERIVE, \ 1400 1.1 christos EVP_PKEY_CTRL_EC_ECDH_COFACTOR, -2, NULL) 1401 1.1 christos 1402 1.1 christos # define EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, kdf) \ 1403 1.1 christos EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1404 1.1 christos EVP_PKEY_OP_DERIVE, \ 1405 1.1 christos EVP_PKEY_CTRL_EC_KDF_TYPE, kdf, NULL) 1406 1.1 christos 1407 1.1 christos # define EVP_PKEY_CTX_get_ecdh_kdf_type(ctx) \ 1408 1.1 christos EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1409 1.1 christos EVP_PKEY_OP_DERIVE, \ 1410 1.1 christos EVP_PKEY_CTRL_EC_KDF_TYPE, -2, NULL) 1411 1.1 christos 1412 1.1 christos # define EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md) \ 1413 1.1 christos EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1414 1.1 christos EVP_PKEY_OP_DERIVE, \ 1415 1.1 christos EVP_PKEY_CTRL_EC_KDF_MD, 0, (void *)(md)) 1416 1.1 christos 1417 1.1 christos # define EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, pmd) \ 1418 1.1 christos EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1419 1.1 christos EVP_PKEY_OP_DERIVE, \ 1420 1.1 christos EVP_PKEY_CTRL_GET_EC_KDF_MD, 0, (void *)(pmd)) 1421 1.1 christos 1422 1.1 christos # define EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, len) \ 1423 1.1 christos EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1424 1.1 christos EVP_PKEY_OP_DERIVE, \ 1425 1.1 christos EVP_PKEY_CTRL_EC_KDF_OUTLEN, len, NULL) 1426 1.1 christos 1427 1.1 christos # define EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, plen) \ 1428 1.1 christos EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1429 1.1 christos EVP_PKEY_OP_DERIVE, \ 1430 1.1 christos EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, 0, \ 1431 1.1 christos (void *)(plen)) 1432 1.1 christos 1433 1.1 christos # define EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p, plen) \ 1434 1.1 christos EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1435 1.1 christos EVP_PKEY_OP_DERIVE, \ 1436 1.1 christos EVP_PKEY_CTRL_EC_KDF_UKM, plen, (void *)(p)) 1437 1.1 christos 1438 1.1 christos # define EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p) \ 1439 1.1 christos EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1440 1.1 christos EVP_PKEY_OP_DERIVE, \ 1441 1.1 christos EVP_PKEY_CTRL_GET_EC_KDF_UKM, 0, (void *)(p)) 1442 1.1 christos 1443 1.1 christos /* SM2 will skip the operation check so no need to pass operation here */ 1444 1.1 christos # define EVP_PKEY_CTX_set1_id(ctx, id, id_len) \ 1445 1.1 christos EVP_PKEY_CTX_ctrl(ctx, -1, -1, \ 1446 1.1 christos EVP_PKEY_CTRL_SET1_ID, (int)id_len, (void*)(id)) 1447 1.1 christos 1448 1.1 christos # define EVP_PKEY_CTX_get1_id(ctx, id) \ 1449 1.1 christos EVP_PKEY_CTX_ctrl(ctx, -1, -1, \ 1450 1.1 christos EVP_PKEY_CTRL_GET1_ID, 0, (void*)(id)) 1451 1.1 christos 1452 1.1 christos # define EVP_PKEY_CTX_get1_id_len(ctx, id_len) \ 1453 1.1 christos EVP_PKEY_CTX_ctrl(ctx, -1, -1, \ 1454 1.1 christos EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)(id_len)) 1455 1.1 christos 1456 1.1 christos # define EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID (EVP_PKEY_ALG_CTRL + 1) 1457 1.1 christos # define EVP_PKEY_CTRL_EC_PARAM_ENC (EVP_PKEY_ALG_CTRL + 2) 1458 1.1 christos # define EVP_PKEY_CTRL_EC_ECDH_COFACTOR (EVP_PKEY_ALG_CTRL + 3) 1459 1.1 christos # define EVP_PKEY_CTRL_EC_KDF_TYPE (EVP_PKEY_ALG_CTRL + 4) 1460 1.1 christos # define EVP_PKEY_CTRL_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 5) 1461 1.1 christos # define EVP_PKEY_CTRL_GET_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 6) 1462 1.1 christos # define EVP_PKEY_CTRL_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 7) 1463 1.1 christos # define EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 8) 1464 1.1 christos # define EVP_PKEY_CTRL_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 9) 1465 1.1 christos # define EVP_PKEY_CTRL_GET_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 10) 1466 1.1 christos # define EVP_PKEY_CTRL_SET1_ID (EVP_PKEY_ALG_CTRL + 11) 1467 1.1 christos # define EVP_PKEY_CTRL_GET1_ID (EVP_PKEY_ALG_CTRL + 12) 1468 1.1 christos # define EVP_PKEY_CTRL_GET1_ID_LEN (EVP_PKEY_ALG_CTRL + 13) 1469 1.1 christos /* KDF types */ 1470 1.1 christos # define EVP_PKEY_ECDH_KDF_NONE 1 1471 1.1 christos # define EVP_PKEY_ECDH_KDF_X9_63 2 1472 1.1 christos /** The old name for EVP_PKEY_ECDH_KDF_X9_63 1473 1.1 christos * The ECDH KDF specification has been mistakingly attributed to ANSI X9.62, 1474 1.1 christos * it is actually specified in ANSI X9.63. 1475 1.1 christos * This identifier is retained for backwards compatibility 1476 1.1 christos */ 1477 1.1 christos # define EVP_PKEY_ECDH_KDF_X9_62 EVP_PKEY_ECDH_KDF_X9_63 1478 1.1 christos 1479 1.1 christos 1480 1.1 christos # ifdef __cplusplus 1481 1.1 christos } 1482 1.1 christos # endif 1483 1.1 christos # endif 1484 1.1 christos #endif 1485