1.\" $OpenBSD: EC_GROUP_copy.3,v 1.7 2016/12/11 14:22:43 schwarze Exp $ 2.\" OpenSSL aafbe1cc Jun 12 23:42:08 2013 +0100 3.\" 4.\" This file was written by Matt Caswell <matt@openssl.org>. 5.\" Copyright (c) 2013, 2015 The OpenSSL Project. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in 16.\" the documentation and/or other materials provided with the 17.\" distribution. 18.\" 19.\" 3. All advertising materials mentioning features or use of this 20.\" software must display the following acknowledgment: 21.\" "This product includes software developed by the OpenSSL Project 22.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 23.\" 24.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 25.\" endorse or promote products derived from this software without 26.\" prior written permission. For written permission, please contact 27.\" openssl-core@openssl.org. 28.\" 29.\" 5. Products derived from this software may not be called "OpenSSL" 30.\" nor may "OpenSSL" appear in their names without prior written 31.\" permission of the OpenSSL Project. 32.\" 33.\" 6. Redistributions of any form whatsoever must retain the following 34.\" acknowledgment: 35.\" "This product includes software developed by the OpenSSL Project 36.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 37.\" 38.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 39.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 40.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 41.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 42.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 43.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 44.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 47.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 48.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 49.\" OF THE POSSIBILITY OF SUCH DAMAGE. 50.\" 51.Dd $Mdocdate: December 11 2016 $ 52.Dt EC_GROUP_COPY 3 53.Os 54.Sh NAME 55.Nm EC_GROUP_copy , 56.Nm EC_GROUP_dup , 57.Nm EC_GROUP_method_of , 58.Nm EC_GROUP_set_generator , 59.Nm EC_GROUP_get0_generator , 60.Nm EC_GROUP_get_order , 61.Nm EC_GROUP_get_cofactor , 62.Nm EC_GROUP_set_curve_name , 63.Nm EC_GROUP_get_curve_name , 64.Nm EC_GROUP_set_asn1_flag , 65.Nm EC_GROUP_get_asn1_flag , 66.Nm EC_GROUP_set_point_conversion_form , 67.Nm EC_GROUP_get_point_conversion_form , 68.Nm EC_GROUP_get0_seed , 69.Nm EC_GROUP_get_seed_len , 70.Nm EC_GROUP_set_seed , 71.Nm EC_GROUP_get_degree , 72.Nm EC_GROUP_check , 73.Nm EC_GROUP_check_discriminant , 74.Nm EC_GROUP_cmp , 75.Nm EC_GROUP_get_basis_type , 76.Nm EC_GROUP_get_trinomial_basis , 77.Nm EC_GROUP_get_pentanomial_basis 78.Nd manipulate EC_GROUP objects 79.Sh SYNOPSIS 80.In openssl/ec.h 81.In openssl/bn.h 82.Ft int 83.Fo EC_GROUP_copy 84.Fa "EC_GROUP *dst" 85.Fa "const EC_GROUP *src" 86.Fc 87.Ft EC_GROUP * 88.Fo EC_GROUP_dup 89.Fa "const EC_GROUP *src" 90.Fc 91.Ft const EC_METHOD * 92.Fo EC_GROUP_method_of 93.Fa "const EC_GROUP *group" 94.Fc 95.Ft int 96.Fo EC_GROUP_set_generator 97.Fa "EC_GROUP *group" 98.Fa "const EC_POINT *generator" 99.Fa "const BIGNUM *order" 100.Fa "const BIGNUM *cofactor" 101.Fc 102.Ft const EC_POINT * 103.Fo EC_GROUP_get0_generator 104.Fa "const EC_GROUP *group" 105.Fc 106.Ft int 107.Fo EC_GROUP_get_order 108.Fa "const EC_GROUP *group" 109.Fa "BIGNUM *order" 110.Fa "BN_CTX *ctx" 111.Fc 112.Ft int 113.Fo EC_GROUP_get_cofactor 114.Fa "const EC_GROUP *group" 115.Fa "BIGNUM *cofactor" 116.Fa "BN_CTX *ctx" 117.Fc 118.Ft void 119.Fo EC_GROUP_set_curve_name 120.Fa "EC_GROUP *group" 121.Fa "int nid" 122.Fc 123.Ft int 124.Fo EC_GROUP_get_curve_name 125.Fa "const EC_GROUP *group" 126.Fc 127.Ft void 128.Fo EC_GROUP_set_asn1_flag 129.Fa "EC_GROUP *group" 130.Fa "int flag" 131.Fc 132.Ft int 133.Fo EC_GROUP_get_asn1_flag 134.Fa "const EC_GROUP *group" 135.Fc 136.Ft void 137.Fo EC_GROUP_set_point_conversion_form 138.Fa "EC_GROUP *group" 139.Fa "point_conversion_form_t form" 140.Fc 141.Ft point_conversion_form_t 142.Fo EC_GROUP_get_point_conversion_form 143.Fa "const EC_GROUP *" 144.Fc 145.Ft unsigned char * 146.Fo EC_GROUP_get0_seed 147.Fa "const EC_GROUP *x" 148.Fc 149.Ft size_t 150.Fo EC_GROUP_get_seed_len 151.Fa "const EC_GROUP *" 152.Fc 153.Ft size_t 154.Fo EC_GROUP_set_seed 155.Fa "EC_GROUP *" 156.Fa "const unsigned char *" 157.Fa "size_t len" 158.Fc 159.Ft int 160.Fo EC_GROUP_get_degree 161.Fa "const EC_GROUP *group" 162.Fc 163.Ft int 164.Fo EC_GROUP_check 165.Fa "const EC_GROUP *group" 166.Fa "BN_CTX *ctx" 167.Fc 168.Ft int 169.Fo EC_GROUP_check_discriminant 170.Fa "const EC_GROUP *group" 171.Fa "BN_CTX *ctx" 172.Fc 173.Ft int 174.Fo EC_GROUP_cmp 175.Fa "const EC_GROUP *a" 176.Fa "const EC_GROUP *b" 177.Fa "BN_CTX *ctx" 178.Fc 179.Ft int 180.Fo EC_GROUP_get_basis_type 181.Fa "const EC_GROUP *" 182.Fc 183.Ft int 184.Fo EC_GROUP_get_trinomial_basis 185.Fa "const EC_GROUP *" 186.Fa "unsigned int *k" 187.Fc 188.Ft int 189.Fo EC_GROUP_get_pentanomial_basis 190.Fa "const EC_GROUP *" 191.Fa "unsigned int *k1" 192.Fa "unsigned int *k2" 193.Fa "unsigned int *k3" 194.Fc 195.Sh DESCRIPTION 196These functions operate on 197.Vt EC_GROUP 198objects created by the functions described in 199.Xr EC_GROUP_new 3 . 200.Pp 201.Fn EC_GROUP_copy 202copies the curve 203.Fa src 204into 205.Fa dst . 206Both 207.Fa src 208and 209.Fa dst 210must use the same 211.Vt EC_METHOD . 212.Pp 213.Fn EC_GROUP_dup 214creates a new 215.Vt EC_GROUP 216object and copies the content from 217.Fa src 218to the newly created 219.Vt EC_GROUP 220object. 221.Pp 222.Fn EC_GROUP_method_of 223obtains the 224.Vt EC_METHOD 225of 226.Fa group . 227.Pp 228.Fn EC_GROUP_set_generator 229sets curve parameters that must be agreed by all participants using 230the curve. 231These parameters include the 232.Fa generator , 233the 234.Fa order 235and the 236.Fa cofactor . 237The 238.Fa generator 239is a well defined point on the curve chosen for cryptographic 240operations. 241Integers used for point multiplications will be between 0 and 242.Fa order No - 1 . 243The 244.Fa order 245multiplied by the 246.Fa cofactor 247gives the number of points on the curve. 248.Pp 249.Fn EC_GROUP_get0_generator 250returns the generator for the identified 251.Fa group . 252.Pp 253The functions 254.Fn EC_GROUP_get_order 255and 256.Fn EC_GROUP_get_cofactor 257populate the provided 258.Fa order 259and 260.Fa cofactor 261parameters with the respective order and cofactors for the 262.Fa group . 263.Pp 264The functions 265.Fn EC_GROUP_set_curve_name 266and 267.Fn EC_GROUP_get_curve_name 268set and get the NID for the curve, respectively (see 269.Xr EC_GROUP_new 3 ) . 270If a curve does not have a NID associated with it, then 271.Fn EC_GROUP_get_curve_name 272will return 0. 273.Pp 274The asn1_flag value on a curve is used to determine whether there is a 275specific ASN.1 OID to describe the curve or not. 276If the asn1_flag is 1 then this is a named curve with an associated ASN.1 OID. 277If not then asn1_flag is 0. 278The functions 279.Fn EC_GROUP_get_asn1_flag 280and 281.Fn EC_GROUP_set_asn1_flag 282get and set the status of the asn1_flag for the curve. 283If set, then the curve_name must also be set. 284.Pp 285The point_conversion_form for a curve controls how 286.Vt EC_POINT 287data is encoded as ASN.1 as defined in X9.62 (ECDSA). 288.Vt point_conversion_form_t 289is an enum defined as follows: 290.Bd -literal 291typedef enum { 292 /** the point is encoded as z||x, where the octet z specifies 293 * which solution of the quadratic equation y is */ 294 POINT_CONVERSION_COMPRESSED = 2, 295 /** the point is encoded as z||x||y, where z is the octet 0x02 */ 296 POINT_CONVERSION_UNCOMPRESSED = 4, 297 /** the point is encoded as z||x||y, where the octet z specifies 298 * which solution of the quadratic equation y is */ 299 POINT_CONVERSION_HYBRID = 6 300} point_conversion_form_t; 301.Ed 302.Pp 303For 304.Dv POINT_CONVERSION_UNCOMPRESSED 305the point is encoded as an octet signifying the UNCOMPRESSED form 306has been used followed by the octets for x, followed by the octets 307for y. 308.Pp 309For any given x coordinate for a point on a curve it is possible to 310derive two possible y values. 311For 312.Dv POINT_CONVERSION_COMPRESSED 313the point is encoded as an octet signifying that the COMPRESSED 314form has been used AND which of the two possible solutions for y 315has been used, followed by the octets for x. 316.Pp 317For 318.Dv POINT_CONVERSION_HYBRID 319the point is encoded as an octet signifying the HYBRID form has 320been used AND which of the two possible solutions for y has been 321used, followed by the octets for x, followed by the octets for y. 322.Pp 323The functions 324.Fn EC_GROUP_set_point_conversion_form 325and 326.Fn EC_GROUP_get_point_conversion_form 327set and get the point_conversion_form for the curve, respectively. 328.Pp 329ANSI X9.62 (ECDSA standard) defines a method of generating the curve 330parameter b from a random number. 331This provides advantages in that a parameter obtained in this way is 332highly unlikely to be susceptible to special purpose attacks, or have 333any trapdoors in it. 334If the seed is present for a curve then the b parameter was generated in 335a verifiable fashion using that seed. 336The OpenSSL EC library does not use this seed value but does enable you 337to inspect it using 338.Fn EC_GROUP_get0_seed . 339This returns a pointer to a memory block containing the seed that was 340used. 341The length of the memory block can be obtained using 342.Fn EC_GROUP_get_seed_len . 343A number of the builtin curves within the library provide seed values 344that can be obtained. 345It is also possible to set a custom seed using 346.Fn EC_GROUP_set_seed 347and passing a pointer to a memory block, along with the length of 348the seed. 349Again, the EC library will not use this seed value, although it will be 350preserved in any ASN.1 based communications. 351.Pp 352.Fn EC_GROUP_get_degree 353gets the degree of the field. 354For Fp fields this will be the number of bits in p. 355For F2^m fields this will be the value m. 356.Pp 357The function 358.Fn EC_GROUP_check_discriminant 359calculates the discriminant for the curve and verifies that it is 360valid. 361For a curve defined over Fp the discriminant is given by the formula 3624*a^3 + 27*b^2 whilst for F2^m curves the discriminant is simply b. 363In either case for the curve to be valid the discriminant must be 364non-zero. 365.Pp 366The function 367.Fn EC_GROUP_check 368performs a number of checks on a curve to verify that it is valid. 369Checks performed include verifying that the discriminant is non-zero; 370that a generator has been defined; that the generator is on the curve 371and has the correct order. 372.Pp 373.Fn EC_GROUP_cmp 374compares 375.Fa a 376and 377.Fa b 378to determine whether they represent the same curve or not. 379.Pp 380The functions 381.Fn EC_GROUP_get_basis_type , 382.Fn EC_GROUP_get_trinomial_basis , 383and 384.Fn EC_GROUP_get_pentanomial_basis 385should only be called for curves defined over an F2^m field. 386Addition and multiplication operations within an F2^m field are 387performed using an irreducible polynomial function f(x). 388This function is either a trinomial of the form: 389.Pp 390.Dl f(x) = x^m + x^k + 1 with m > k >= 1 391.Pp 392or a pentanomial of the form: 393.Pp 394.Dl f(x) = x^m + x^k3 + x^k2 + x^k1 + 1 with m > k3 > k2 > k1 >= 1 395.Pp 396The function 397.Fn EC_GROUP_get_basis_type 398returns a NID identifying whether a trinomial or pentanomial is in 399use for the field. 400The function 401.Fn EC_GROUP_get_trinomial_basis 402must only be called where f(x) is of the trinomial form, and returns 403the value of 404.Fa k . 405Similarly, the function 406.Fn EC_GROUP_get_pentanomial_basis 407must only be called where f(x) is of the pentanomial form, and 408returns the values of 409.Fa k1 , 410.Fa k2 , 411and 412.Fa k3 . 413.Sh RETURN VALUES 414The following functions return 1 on success or 0 on error: 415.Fn EC_GROUP_copy , 416.Fn EC_GROUP_set_generator , 417.Fn EC_GROUP_check , 418.Fn EC_GROUP_check_discriminant , 419.Fn EC_GROUP_get_trinomial_basis , 420and 421.Fn EC_GROUP_get_pentanomial_basis . 422.Pp 423.Fn EC_GROUP_dup 424returns a pointer to the duplicated curve or 425.Dv NULL 426on error. 427.Pp 428.Fn EC_GROUP_method_of 429returns the 430.Vt EC_METHOD 431implementation in use for the given curve or 432.Dv NULL 433on error. 434.Pp 435.Fn EC_GROUP_get0_generator 436returns the generator for the given curve or 437.Dv NULL 438on error. 439.Pp 440.Fn EC_GROUP_get_order , 441.Fn EC_GROUP_get_cofactor , 442.Fn EC_GROUP_get_curve_name , 443.Fn EC_GROUP_get_asn1_flag , 444.Fn EC_GROUP_get_point_conversion_form , 445and 446.Fn EC_GROUP_get_degree 447return the order, cofactor, curve name (NID), ASN.1 flag, 448point_conversion_form and degree for the specified curve, respectively. 449If there is no curve name associated with a curve then 450.Fn EC_GROUP_get_curve_name 451returns 0. 452.Pp 453.Fn EC_GROUP_get0_seed 454returns a pointer to the seed that was used to generate the parameter 455b, or 456.Dv NULL 457if the seed is not specified. 458.Fn EC_GROUP_get_seed_len 459returns the length of the seed or 0 if the seed is not specified. 460.Pp 461.Fn EC_GROUP_set_seed 462returns the length of the seed that has been set. 463If the supplied seed is 464.Dv NULL 465or the supplied seed length is 0, the return value will be 1. 466On error 0 is returned. 467.Pp 468.Fn EC_GROUP_cmp 469returns 0 if the curves are equal, 1 if they are not equal, 470or -1 on error. 471.Pp 472.Fn EC_GROUP_get_basis_type 473returns the values 474.Dv NID_X9_62_tpBasis 475or 476.Dv NID_X9_62_ppBasis 477as defined in 478.In openssl/obj_mac.h 479for a trinomial or pentanomial, respectively. 480Alternatively in the event of an error a 0 is returned. 481.Sh SEE ALSO 482.Xr d2i_ECPKParameters 3 , 483.Xr EC_GFp_simple_method 3 , 484.Xr EC_GROUP_new 3 , 485.Xr EC_KEY_new 3 , 486.Xr EC_POINT_add 3 , 487.Xr EC_POINT_new 3 488