76c5ba335841aea50748ac3e4905ba0e7bd74456
[openssl.git] / doc / crypto / EC_KEY_new.pod
1 =pod
2
3 =head1 NAME
4
5 EC_KEY_new, EC_KEY_get_flags, EC_KEY_set_flags, EC_KEY_clear_flags,
6 EC_KEY_new_by_curve_name, EC_KEY_free, EC_KEY_copy, EC_KEY_dup, EC_KEY_up_ref,
7 EC_KEY_get0_group, EC_KEY_set_group, EC_KEY_get0_private_key,
8 EC_KEY_set_private_key, EC_KEY_get0_public_key, EC_KEY_set_public_key,
9 EC_KEY_get_enc_flags, EC_KEY_set_enc_flags, EC_KEY_get_conv_form,
10 EC_KEY_set_conv_form, EC_KEY_set_asn1_flag, EC_KEY_precompute_mult,
11 EC_KEY_generate_key, EC_KEY_check_key, EC_KEY_set_public_key_affine_coordinates,
12 EC_KEY_oct2key, EC_KEY_key2buf, EC_KEY_oct2priv, EC_KEY_priv2oct,
13 EC_KEY_priv2buf - Functions for creating, destroying and manipulating
14 EC_KEY objects
15
16 =head1 SYNOPSIS
17
18  #include <openssl/ec.h>
19
20  EC_KEY *EC_KEY_new(void);
21  int EC_KEY_get_flags(const EC_KEY *key);
22  void EC_KEY_set_flags(EC_KEY *key, int flags);
23  void EC_KEY_clear_flags(EC_KEY *key, int flags);
24  EC_KEY *EC_KEY_new_by_curve_name(int nid);
25  void EC_KEY_free(EC_KEY *key);
26  EC_KEY *EC_KEY_copy(EC_KEY *dst, const EC_KEY *src);
27  EC_KEY *EC_KEY_dup(const EC_KEY *src);
28  int EC_KEY_up_ref(EC_KEY *key);
29  const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key);
30  int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group);
31  const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key);
32  int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *prv);
33  const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key);
34  int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub);
35  point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key);
36  void EC_KEY_set_conv_form(EC_KEY *eckey, point_conversion_form_t cform);
37  void EC_KEY_set_asn1_flag(EC_KEY *eckey, int asn1_flag);
38  int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx);
39  int EC_KEY_generate_key(EC_KEY *key);
40  int EC_KEY_check_key(const EC_KEY *key);
41  int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key,
42                                               BIGNUM *x, BIGNUM *y);
43  const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key);
44  int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth);
45
46  int EC_KEY_oct2key(EC_KEY *eckey, const unsigned char *buf, size_t len,
47                     BN_CTX *ctx);
48  size_t EC_KEY_key2buf(const EC_KEY *eckey, point_conversion_form_t form,
49                        unsigned char **pbuf, BN_CTX *ctx);
50
51  int EC_KEY_oct2priv(EC_KEY *eckey, unsigned char *buf, size_t len);
52  size_t EC_KEY_priv2oct(const EC_KEY *eckey, unsigned char *buf, size_t len);
53
54  size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf);
55
56 =head1 DESCRIPTION
57
58 An EC_KEY represents a public key and, optionally, the associated private
59 key. A new EC_KEY with no associated curve can be constructed by calling
60 EC_KEY_new(). The reference count for the newly created EC_KEY is initially
61 set to 1. A curve can be associated with the EC_KEY by calling
62 EC_KEY_set_group().
63
64 Alternatively a new EC_KEY can be constructed by calling
65 EC_KEY_new_by_curve_name() and supplying the nid of the associated curve. See
66 L<EC_GROUP_new(3)> for a description of curve names. This function simply
67 wraps calls to EC_KEY_new() and EC_GROUP_new_by_curve_name().
68
69 Calling EC_KEY_free() decrements the reference count for the EC_KEY object,
70 and if it has dropped to zero then frees the memory associated with it.  If
71 B<key> is NULL nothing is done.
72
73 EC_KEY_copy() copies the contents of the EC_KEY in B<src> into B<dest>.
74
75 EC_KEY_dup() creates a new EC_KEY object and copies B<ec_key> into it.
76
77 EC_KEY_up_ref() increments the reference count associated with the EC_KEY
78 object.
79
80 EC_KEY_generate_key() generates a new public and private key for the supplied
81 B<eckey> object. B<eckey> must have an EC_GROUP object associated with it
82 before calling this function. The private key is a random integer (0 < priv_key
83 < order, where I<order> is the order of the EC_GROUP object). The public key is
84 an EC_POINT on the curve calculated by multiplying the generator for the
85 curve by the private key.
86
87 EC_KEY_check_key() performs various sanity checks on the EC_KEY object to
88 confirm that it is valid.
89
90 EC_KEY_set_public_key_affine_coordinates() sets the public key for B<key> based
91 on its affine co-ordinates; i.e., it constructs an EC_POINT object based on
92 the supplied B<x> and B<y> values and sets the public key to be this
93 EC_POINT. It also performs certain sanity checks on the key to confirm
94 that it is valid.
95
96 The functions EC_KEY_get0_group(), EC_KEY_set_group(),
97 EC_KEY_get0_private_key(), EC_KEY_set_private_key(), EC_KEY_get0_public_key(),
98 and EC_KEY_set_public_key() get and set the EC_GROUP object, the private key,
99 and the EC_POINT public key for the B<key> respectively.
100
101 The functions EC_KEY_get_conv_form() and EC_KEY_set_conv_form() get and set the
102 point_conversion_form for the B<key>. For a description of
103 point_conversion_forms please see L<EC_POINT_new(3)>.
104
105 EC_KEY_set_flags() sets the flags in the B<flags> parameter on the EC_KEY
106 object. Any flags that are already set are left set. The flags currently
107 defined are EC_FLAG_NON_FIPS_ALLOW and EC_FLAG_FIPS_CHECKED. In
108 addition there is the flag EC_FLAG_COFACTOR_ECDH which is specific to ECDH.
109 EC_KEY_get_flags() returns the current flags that are set for this EC_KEY.
110 EC_KEY_clear_flags() clears the flags indicated by the B<flags> parameter; all
111 other flags are left in their existing state.
112
113 EC_KEY_set_asn1_flag() sets the asn1_flag on the underlying EC_GROUP object
114 (if set). Refer to L<EC_GROUP_copy(3)> for further information on the
115 asn1_flag.
116
117 EC_KEY_precompute_mult() stores multiples of the underlying EC_GROUP generator
118 for faster point multiplication. See also L<EC_POINT_add(3)>.
119
120 EC_KEY_oct2key() and EC_KEY_key2buf() are identical to the functions
121 EC_POINT_oct2point() and EC_KEY_point2buf() except they use the public key
122 EC_POINT in B<eckey>.
123
124 EC_KEY_oct2priv() and EC_KEY_priv2oct() convert between the private key
125 component of B<eckey> and octet form. The octet form consists of the content
126 octets of the B<privateKey> OCTET STRING in an B<ECPrivateKey> ASN.1 structure.
127
128 The function EC_KEY_priv2oct() must be supplied with a buffer long enough to
129 store the octet form. The return value provides the number of octets stored.
130 Calling the function with a NULL buffer will not perform the conversion but
131 will just return the required buffer length.
132
133 The function EC_KEY_priv2buf() allocates a buffer of suitable length and writes
134 an EC_KEY to it in octet format. The allocated buffer is written to B<*pbuf>
135 and its length is returned. The caller must free up the allocated buffer with a
136 call to OPENSSL_free(). Since the allocated buffer value is written to B<*pbuf>
137 the B<pbuf> parameter B<MUST NOT> be B<NULL>.
138
139 EC_KEY_priv2buf() converts an EC_KEY private key into an allocated buffer.
140
141 =head1 RETURN VALUES
142
143 EC_KEY_new(), EC_KEY_new_by_curve_name() and EC_KEY_dup() return a pointer to
144 the newly created EC_KEY object, or NULL on error.
145
146 EC_KEY_get_flags() returns the flags associated with the EC_KEY object as an
147 integer.
148
149 EC_KEY_copy() returns a pointer to the destination key, or NULL on error.
150
151 EC_KEY_up_ref(), EC_KEY_set_group(), EC_KEY_set_private_key(),
152 EC_KEY_set_public_key(), EC_KEY_precompute_mult(), EC_KEY_generate_key(),
153 EC_KEY_check_key(), EC_KEY_set_public_key_affine_coordinates(),
154 EC_KEY_oct2key() and EC_KEY_oct2priv() return 1 on success or 0 on error.
155
156 EC_KEY_get0_group() returns the EC_GROUP associated with the EC_KEY.
157
158 EC_KEY_get0_private_key() returns the private key associated with the EC_KEY.
159
160 EC_KEY_get_conv_form() return the point_conversion_form for the EC_KEY.
161
162 EC_KEY_key2buf(), EC_KEY_priv2oct() and EC_KEY_priv2buf() return the length
163 of the buffer or 0 on error.
164
165 =head1 SEE ALSO
166
167 L<crypto(3)>, L<ec(3)>, L<EC_GROUP_new(3)>,
168 L<EC_GROUP_copy(3)>, L<EC_POINT_new(3)>,
169 L<EC_POINT_add(3)>,
170 L<EC_GFp_simple_method(3)>,
171 L<d2i_ECPKParameters(3)>
172
173 =head1 COPYRIGHT
174
175 Copyright 2013-2016 The OpenSSL Project Authors. All Rights Reserved.
176
177 Licensed under the OpenSSL license (the "License").  You may not use
178 this file except in compliance with the License.  You can obtain a copy
179 in the file LICENSE in the source distribution or at
180 L<https://www.openssl.org/source/license.html>.
181
182 =cut