1 1.1 christos =pod 2 1.1 christos 3 1.1 christos =head1 NAME 4 1.1 christos 5 1.1.1.4 christos EVP_EC_gen, 6 1.1.1.4 christos EC_KEY_get_method, EC_KEY_set_method, EC_KEY_new_ex, 7 1.1 christos EC_KEY_new, EC_KEY_get_flags, EC_KEY_set_flags, EC_KEY_clear_flags, 8 1.1.1.4 christos EC_KEY_new_by_curve_name_ex, EC_KEY_new_by_curve_name, EC_KEY_free, 9 1.1.1.4 christos EC_KEY_copy, EC_KEY_dup, EC_KEY_up_ref, EC_KEY_get0_engine, 10 1.1 christos EC_KEY_get0_group, EC_KEY_set_group, EC_KEY_get0_private_key, 11 1.1 christos EC_KEY_set_private_key, EC_KEY_get0_public_key, EC_KEY_set_public_key, 12 1.1 christos EC_KEY_get_conv_form, 13 1.1.1.2 christos EC_KEY_set_conv_form, EC_KEY_set_asn1_flag, 14 1.1.1.2 christos EC_KEY_decoded_from_explicit_params, EC_KEY_precompute_mult, 15 1.1 christos EC_KEY_generate_key, EC_KEY_check_key, EC_KEY_set_public_key_affine_coordinates, 16 1.1 christos EC_KEY_oct2key, EC_KEY_key2buf, EC_KEY_oct2priv, EC_KEY_priv2oct, 17 1.1 christos EC_KEY_priv2buf - Functions for creating, destroying and manipulating 18 1.1 christos EC_KEY objects 19 1.1 christos 20 1.1 christos =head1 SYNOPSIS 21 1.1 christos 22 1.1 christos #include <openssl/ec.h> 23 1.1 christos 24 1.1.1.4 christos EVP_PKEY *EVP_EC_gen(const char *curve); 25 1.1.1.4 christos 26 1.1.1.4 christos The following functions have been deprecated since OpenSSL 3.0, and can be 27 1.1.1.4 christos hidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value, 28 1.1.1.4 christos see L<openssl_user_macros(7)>: 29 1.1.1.4 christos 30 1.1.1.4 christos EC_KEY *EC_KEY_new_ex(OSSL_LIB_CTX *ctx, const char *propq); 31 1.1 christos EC_KEY *EC_KEY_new(void); 32 1.1 christos int EC_KEY_get_flags(const EC_KEY *key); 33 1.1 christos void EC_KEY_set_flags(EC_KEY *key, int flags); 34 1.1 christos void EC_KEY_clear_flags(EC_KEY *key, int flags); 35 1.1.1.4 christos EC_KEY *EC_KEY_new_by_curve_name_ex(OSSL_LIB_CTX *ctx, const char *propq, 36 1.1.1.4 christos int nid); 37 1.1 christos EC_KEY *EC_KEY_new_by_curve_name(int nid); 38 1.1 christos void EC_KEY_free(EC_KEY *key); 39 1.1 christos EC_KEY *EC_KEY_copy(EC_KEY *dst, const EC_KEY *src); 40 1.1 christos EC_KEY *EC_KEY_dup(const EC_KEY *src); 41 1.1 christos int EC_KEY_up_ref(EC_KEY *key); 42 1.1 christos ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey); 43 1.1 christos const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key); 44 1.1 christos int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group); 45 1.1 christos const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key); 46 1.1.1.3 christos int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key); 47 1.1 christos const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key); 48 1.1 christos int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub); 49 1.1 christos point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key); 50 1.1 christos void EC_KEY_set_conv_form(EC_KEY *eckey, point_conversion_form_t cform); 51 1.1 christos void EC_KEY_set_asn1_flag(EC_KEY *eckey, int asn1_flag); 52 1.1.1.2 christos int EC_KEY_decoded_from_explicit_params(const EC_KEY *key); 53 1.1 christos int EC_KEY_generate_key(EC_KEY *key); 54 1.1 christos int EC_KEY_check_key(const EC_KEY *key); 55 1.1 christos int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x, BIGNUM *y); 56 1.1 christos const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key); 57 1.1 christos int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth); 58 1.1 christos 59 1.1 christos int EC_KEY_oct2key(EC_KEY *eckey, const unsigned char *buf, size_t len, BN_CTX *ctx); 60 1.1 christos size_t EC_KEY_key2buf(const EC_KEY *eckey, point_conversion_form_t form, 61 1.1 christos unsigned char **pbuf, BN_CTX *ctx); 62 1.1 christos 63 1.1 christos int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len); 64 1.1 christos size_t EC_KEY_priv2oct(const EC_KEY *eckey, unsigned char *buf, size_t len); 65 1.1 christos 66 1.1 christos size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf); 67 1.1.1.4 christos int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx); 68 1.1 christos 69 1.1 christos =head1 DESCRIPTION 70 1.1 christos 71 1.1.1.4 christos EVP_EC_gen() generates a new EC key pair on the given I<curve>. 72 1.1.1.4 christos 73 1.1.1.4 christos All of the functions described below are deprecated. 74 1.1.1.4 christos Applications should instead use EVP_EC_gen(), L<EVP_PKEY_Q_keygen(3)>, or 75 1.1.1.4 christos L<EVP_PKEY_keygen_init(3)> and L<EVP_PKEY_keygen(3)>. 76 1.1.1.4 christos 77 1.1 christos An EC_KEY represents a public key and, optionally, the associated private 78 1.1.1.4 christos key. 79 1.1.1.4 christos A new EC_KEY with no associated curve can be constructed by calling 80 1.1.1.4 christos EC_KEY_new_ex() and specifying the associated library context in I<ctx> 81 1.1.1.4 christos (see L<OSSL_LIB_CTX(3)>) and property query string I<propq>. 82 1.1.1.4 christos The I<ctx> parameter may be NULL in which case the default library context is 83 1.1.1.4 christos used. 84 1.1.1.4 christos The reference count for the newly created EC_KEY is initially 85 1.1.1.4 christos set to 1. 86 1.1.1.4 christos A curve can be associated with the EC_KEY by calling 87 1.1 christos EC_KEY_set_group(). 88 1.1 christos 89 1.1.1.4 christos EC_KEY_new() is the same as EC_KEY_new_ex() except that the default library 90 1.1.1.4 christos context is always used. 91 1.1.1.4 christos 92 1.1 christos Alternatively a new EC_KEY can be constructed by calling 93 1.1.1.4 christos EC_KEY_new_by_curve_name_ex() and supplying the nid of the associated 94 1.1.1.4 christos curve, the library context to be used I<ctx> (see L<OSSL_LIB_CTX(3)>) and any 95 1.1.1.4 christos property query string I<propq>. 96 1.1.1.4 christos The I<ctx> parameter may be NULL in which case the default library context is 97 1.1.1.4 christos used. The I<propq> value may also be NULL. 98 1.1.1.4 christos See L<EC_GROUP_new(3)> for a description of curve names. 99 1.1.1.4 christos This function simply wraps calls to EC_KEY_new_ex() and 100 1.1.1.4 christos EC_GROUP_new_by_curve_name_ex(). 101 1.1.1.4 christos 102 1.1.1.4 christos EC_KEY_new_by_curve_name() is the same as EC_KEY_new_by_curve_name_ex() 103 1.1.1.4 christos except that the default library context is always used and a NULL property query 104 1.1.1.4 christos string. 105 1.1 christos 106 1.1 christos Calling EC_KEY_free() decrements the reference count for the EC_KEY object, 107 1.1 christos and if it has dropped to zero then frees the memory associated with it. If 108 1.1.1.4 christos I<key> is NULL nothing is done. 109 1.1 christos 110 1.1.1.4 christos EC_KEY_copy() copies the contents of the EC_KEY in I<src> into I<dest>. 111 1.1 christos 112 1.1.1.4 christos EC_KEY_dup() creates a new EC_KEY object and copies I<ec_key> into it. 113 1.1 christos 114 1.1 christos EC_KEY_up_ref() increments the reference count associated with the EC_KEY 115 1.1 christos object. 116 1.1 christos 117 1.1 christos EC_KEY_get0_engine() returns a handle to the ENGINE that has been set for 118 1.1 christos this EC_KEY object. 119 1.1 christos 120 1.1 christos EC_KEY_generate_key() generates a new public and private key for the supplied 121 1.1.1.4 christos I<eckey> object. I<eckey> must have an EC_GROUP object associated with it 122 1.1 christos before calling this function. The private key is a random integer (0 < priv_key 123 1.1 christos < order, where I<order> is the order of the EC_GROUP object). The public key is 124 1.1 christos an EC_POINT on the curve calculated by multiplying the generator for the 125 1.1 christos curve by the private key. 126 1.1 christos 127 1.1 christos EC_KEY_check_key() performs various sanity checks on the EC_KEY object to 128 1.1 christos confirm that it is valid. 129 1.1 christos 130 1.1.1.4 christos EC_KEY_set_public_key_affine_coordinates() sets the public key for I<key> based 131 1.1.1.5 christos on its affine coordinates; i.e., it constructs an EC_POINT object based on 132 1.1.1.4 christos the supplied I<x> and I<y> values and sets the public key to be this 133 1.1 christos EC_POINT. It also performs certain sanity checks on the key to confirm 134 1.1 christos that it is valid. 135 1.1 christos 136 1.1 christos The functions EC_KEY_get0_group(), EC_KEY_set_group(), 137 1.1 christos EC_KEY_get0_private_key(), EC_KEY_set_private_key(), EC_KEY_get0_public_key(), 138 1.1 christos and EC_KEY_set_public_key() get and set the EC_GROUP object, the private key, 139 1.1.1.3 christos and the EC_POINT public key for the B<key> respectively. The function 140 1.1.1.3 christos EC_KEY_set_private_key() accepts NULL as the priv_key argument to securely clear 141 1.1.1.3 christos the private key component from the EC_KEY. 142 1.1 christos 143 1.1 christos The functions EC_KEY_get_conv_form() and EC_KEY_set_conv_form() get and set the 144 1.1.1.4 christos point_conversion_form for the I<key>. For a description of 145 1.1 christos point_conversion_forms please see L<EC_POINT_new(3)>. 146 1.1 christos 147 1.1.1.4 christos EC_KEY_set_flags() sets the flags in the I<flags> parameter on the EC_KEY 148 1.1 christos object. Any flags that are already set are left set. The flags currently 149 1.1 christos defined are EC_FLAG_NON_FIPS_ALLOW and EC_FLAG_FIPS_CHECKED. In 150 1.1 christos addition there is the flag EC_FLAG_COFACTOR_ECDH which is specific to ECDH. 151 1.1 christos EC_KEY_get_flags() returns the current flags that are set for this EC_KEY. 152 1.1.1.4 christos EC_KEY_clear_flags() clears the flags indicated by the I<flags> parameter; all 153 1.1 christos other flags are left in their existing state. 154 1.1 christos 155 1.1 christos EC_KEY_set_asn1_flag() sets the asn1_flag on the underlying EC_GROUP object 156 1.1 christos (if set). Refer to L<EC_GROUP_copy(3)> for further information on the 157 1.1 christos asn1_flag. 158 1.1 christos 159 1.1.1.2 christos EC_KEY_decoded_from_explicit_params() returns 1 if the group of the I<key> was 160 1.1.1.2 christos decoded from data with explicitly encoded group parameters, -1 if the I<key> 161 1.1.1.2 christos is NULL or the group parameters are missing, and 0 otherwise. 162 1.1.1.2 christos 163 1.1 christos EC_KEY_precompute_mult() stores multiples of the underlying EC_GROUP generator 164 1.1 christos for faster point multiplication. See also L<EC_POINT_add(3)>. 165 1.1.1.4 christos Modern versions should instead switch to named curves which OpenSSL has 166 1.1.1.4 christos hardcoded lookup tables for. 167 1.1 christos 168 1.1 christos EC_KEY_oct2key() and EC_KEY_key2buf() are identical to the functions 169 1.1.1.2 christos EC_POINT_oct2point() and EC_POINT_point2buf() except they use the public key 170 1.1.1.4 christos EC_POINT in I<eckey>. 171 1.1 christos 172 1.1 christos EC_KEY_oct2priv() and EC_KEY_priv2oct() convert between the private key 173 1.1.1.4 christos component of I<eckey> and octet form. The octet form consists of the content 174 1.1.1.4 christos octets of the I<privateKey> OCTET STRING in an I<ECPrivateKey> ASN.1 structure. 175 1.1 christos 176 1.1 christos The function EC_KEY_priv2oct() must be supplied with a buffer long enough to 177 1.1 christos store the octet form. The return value provides the number of octets stored. 178 1.1 christos Calling the function with a NULL buffer will not perform the conversion but 179 1.1 christos will just return the required buffer length. 180 1.1 christos 181 1.1 christos The function EC_KEY_priv2buf() allocates a buffer of suitable length and writes 182 1.1.1.4 christos an EC_KEY to it in octet format. The allocated buffer is written to I<*pbuf> 183 1.1 christos and its length is returned. The caller must free up the allocated buffer with a 184 1.1.1.4 christos call to OPENSSL_free(). Since the allocated buffer value is written to I<*pbuf> 185 1.1.1.4 christos the I<pbuf> parameter B<MUST NOT> be B<NULL>. 186 1.1 christos 187 1.1 christos EC_KEY_priv2buf() converts an EC_KEY private key into an allocated buffer. 188 1.1 christos 189 1.1 christos =head1 RETURN VALUES 190 1.1 christos 191 1.1.1.4 christos EC_KEY_new_ex(), EC_KEY_new(), EC_KEY_new_by_curve_name_ex(), 192 1.1.1.4 christos EC_KEY_new_by_curve_name() and EC_KEY_dup() return a pointer to the newly 193 1.1.1.4 christos created EC_KEY object, or NULL on error. 194 1.1 christos 195 1.1 christos EC_KEY_get_flags() returns the flags associated with the EC_KEY object as an 196 1.1 christos integer. 197 1.1 christos 198 1.1 christos EC_KEY_copy() returns a pointer to the destination key, or NULL on error. 199 1.1 christos 200 1.1 christos EC_KEY_get0_engine() returns a pointer to an ENGINE, or NULL if it wasn't set. 201 1.1 christos 202 1.1.1.3 christos EC_KEY_up_ref(), EC_KEY_set_group(), EC_KEY_set_public_key(), 203 1.1.1.3 christos EC_KEY_precompute_mult(), EC_KEY_generate_key(), EC_KEY_check_key(), 204 1.1.1.3 christos EC_KEY_set_public_key_affine_coordinates(), EC_KEY_oct2key() and 205 1.1.1.3 christos EC_KEY_oct2priv() return 1 on success or 0 on error. 206 1.1.1.3 christos 207 1.1.1.3 christos EC_KEY_set_private_key() returns 1 on success or 0 on error except when the 208 1.1.1.3 christos priv_key argument is NULL, in that case it returns 0, for legacy compatibility, 209 1.1.1.3 christos and should not be treated as an error. 210 1.1 christos 211 1.1 christos EC_KEY_get0_group() returns the EC_GROUP associated with the EC_KEY. 212 1.1 christos 213 1.1 christos EC_KEY_get0_private_key() returns the private key associated with the EC_KEY. 214 1.1 christos 215 1.1 christos EC_KEY_get_conv_form() return the point_conversion_form for the EC_KEY. 216 1.1 christos 217 1.1 christos EC_KEY_key2buf(), EC_KEY_priv2oct() and EC_KEY_priv2buf() return the length 218 1.1 christos of the buffer or 0 on error. 219 1.1 christos 220 1.1 christos =head1 SEE ALSO 221 1.1 christos 222 1.1.1.4 christos L<EVP_PKEY_Q_keygen(3)> 223 1.1 christos L<crypto(7)>, L<EC_GROUP_new(3)>, 224 1.1 christos L<EC_GROUP_copy(3)>, L<EC_POINT_new(3)>, 225 1.1 christos L<EC_POINT_add(3)>, 226 1.1 christos L<EC_GFp_simple_method(3)>, 227 1.1.1.4 christos L<d2i_ECPKParameters(3)>, 228 1.1.1.4 christos L<OSSL_LIB_CTX(3)> 229 1.1.1.4 christos 230 1.1.1.4 christos =head1 HISTORY 231 1.1.1.4 christos 232 1.1.1.4 christos EVP_EC_gen() was added in OpenSSL 3.0. 233 1.1.1.4 christos All other functions described here were deprecated in OpenSSL 3.0. 234 1.1.1.4 christos For replacement see L<EVP_PKEY-EC(7)>. 235 1.1 christos 236 1.1 christos =head1 COPYRIGHT 237 1.1 christos 238 1.1.1.5 christos Copyright 2013-2023 The OpenSSL Project Authors. All Rights Reserved. 239 1.1 christos 240 1.1.1.4 christos Licensed under the Apache License 2.0 (the "License"). You may not use 241 1.1 christos this file except in compliance with the License. You can obtain a copy 242 1.1 christos in the file LICENSE in the source distribution or at 243 1.1 christos L<https://www.openssl.org/source/license.html>. 244 1.1 christos 245 1.1 christos =cut 246