xref: /freebsd/crypto/openssl/doc/man3/EVP_PKEY_new.pod (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim=pod
2e71b7053SJung-uk Kim
3e71b7053SJung-uk Kim=head1 NAME
4e71b7053SJung-uk Kim
5*b077aed3SPierre ProncheryEVP_PKEY,
6e71b7053SJung-uk KimEVP_PKEY_new,
7e71b7053SJung-uk KimEVP_PKEY_up_ref,
8*b077aed3SPierre ProncheryEVP_PKEY_dup,
9e71b7053SJung-uk KimEVP_PKEY_free,
10*b077aed3SPierre ProncheryEVP_PKEY_new_raw_private_key_ex,
11e71b7053SJung-uk KimEVP_PKEY_new_raw_private_key,
12*b077aed3SPierre ProncheryEVP_PKEY_new_raw_public_key_ex,
13e71b7053SJung-uk KimEVP_PKEY_new_raw_public_key,
14e71b7053SJung-uk KimEVP_PKEY_new_CMAC_key,
15e71b7053SJung-uk KimEVP_PKEY_new_mac_key,
16e71b7053SJung-uk KimEVP_PKEY_get_raw_private_key,
17e71b7053SJung-uk KimEVP_PKEY_get_raw_public_key
18e71b7053SJung-uk Kim- public/private key allocation and raw key handling functions
19e71b7053SJung-uk Kim
20e71b7053SJung-uk Kim=head1 SYNOPSIS
21e71b7053SJung-uk Kim
22e71b7053SJung-uk Kim #include <openssl/evp.h>
23e71b7053SJung-uk Kim
24*b077aed3SPierre Pronchery typedef evp_pkey_st EVP_PKEY;
25*b077aed3SPierre Pronchery
26e71b7053SJung-uk Kim EVP_PKEY *EVP_PKEY_new(void);
27e71b7053SJung-uk Kim int EVP_PKEY_up_ref(EVP_PKEY *key);
28*b077aed3SPierre Pronchery EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *key);
29e71b7053SJung-uk Kim void EVP_PKEY_free(EVP_PKEY *key);
30e71b7053SJung-uk Kim
31*b077aed3SPierre Pronchery EVP_PKEY *EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX *libctx,
32*b077aed3SPierre Pronchery                                           const char *keytype,
33*b077aed3SPierre Pronchery                                           const char *propq,
34*b077aed3SPierre Pronchery                                           const unsigned char *key,
35*b077aed3SPierre Pronchery                                           size_t keylen);
36e71b7053SJung-uk Kim EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
37e71b7053SJung-uk Kim                                        const unsigned char *key, size_t keylen);
38*b077aed3SPierre Pronchery EVP_PKEY *EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX *libctx,
39*b077aed3SPierre Pronchery                                          const char *keytype,
40*b077aed3SPierre Pronchery                                          const char *propq,
41*b077aed3SPierre Pronchery                                          const unsigned char *key,
42*b077aed3SPierre Pronchery                                          size_t keylen);
43e71b7053SJung-uk Kim EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
44e71b7053SJung-uk Kim                                       const unsigned char *key, size_t keylen);
45e71b7053SJung-uk Kim EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, const unsigned char *key,
46e71b7053SJung-uk Kim                                int keylen);
47e71b7053SJung-uk Kim
48e71b7053SJung-uk Kim int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
49e71b7053SJung-uk Kim                                  size_t *len);
50e71b7053SJung-uk Kim int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
51e71b7053SJung-uk Kim                                 size_t *len);
52e71b7053SJung-uk Kim
53*b077aed3SPierre ProncheryThe following function has been deprecated since OpenSSL 3.0, and can be
54*b077aed3SPierre Proncheryhidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
55*b077aed3SPierre Proncherysee L<openssl_user_macros(7)>:
56*b077aed3SPierre Pronchery
57*b077aed3SPierre Pronchery EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
58*b077aed3SPierre Pronchery                                 size_t len, const EVP_CIPHER *cipher);
59*b077aed3SPierre Pronchery
60e71b7053SJung-uk Kim=head1 DESCRIPTION
61e71b7053SJung-uk Kim
62*b077aed3SPierre ProncheryB<EVP_PKEY> is a generic structure to hold diverse types of asymmetric keys
63*b077aed3SPierre Pronchery(also known as "key pairs"), and can be used for diverse operations, like
64*b077aed3SPierre Proncherysigning, verifying signatures, key derivation, etc.  The asymmetric keys
65*b077aed3SPierre Proncherythemselves are often refered to as the "internal key", and are handled by
66*b077aed3SPierre Proncherybackends, such as providers (through L<EVP_KEYMGMT(3)>) or B<ENGINE>s.
67*b077aed3SPierre Pronchery
68*b077aed3SPierre ProncheryConceptually, an B<EVP_PKEY> internal key may hold a private key, a public
69*b077aed3SPierre Proncherykey, or both (a keypair), and along with those, key parameters if the key type
70*b077aed3SPierre Proncheryrequires them.  The presence of these components determine what operations can
71*b077aed3SPierre Proncherybe made; for example, signing normally requires the presence of a private key,
72*b077aed3SPierre Proncheryand verifying normally requires the presence of a public key.
73*b077aed3SPierre Pronchery
74*b077aed3SPierre Pronchery=for comment ED signature require both the private and public key...
75*b077aed3SPierre Pronchery
76*b077aed3SPierre ProncheryB<EVP_PKEY> has also been used for MAC algorithm that were conceived as
77*b077aed3SPierre Proncheryproducing signatures, although not being public key algorithms; "POLY1305",
78*b077aed3SPierre Pronchery"SIPHASH", "HMAC", "CMAC".  This usage is considered legacy and is discouraged
79*b077aed3SPierre Proncheryin favor of the L<EVP_MAC(3)> API.
80*b077aed3SPierre Pronchery
81e71b7053SJung-uk KimThe EVP_PKEY_new() function allocates an empty B<EVP_PKEY> structure which is
82e71b7053SJung-uk Kimused by OpenSSL to store public and private keys. The reference count is set to
83e71b7053SJung-uk KimB<1>.
84e71b7053SJung-uk Kim
85*b077aed3SPierre ProncheryEVP_PKEY_up_ref() increments the reference count of I<key>.
86e71b7053SJung-uk Kim
87*b077aed3SPierre ProncheryEVP_PKEY_dup() duplicates the I<key>. The I<key> must not be ENGINE based or
88*b077aed3SPierre Proncherya raw key, otherwise the duplication will fail.
89e71b7053SJung-uk Kim
90*b077aed3SPierre ProncheryEVP_PKEY_free() decrements the reference count of I<key> and, if the reference
91*b077aed3SPierre Proncherycount is zero, frees it up. If I<key> is NULL, nothing is done.
92*b077aed3SPierre Pronchery
93*b077aed3SPierre ProncheryEVP_PKEY_new_raw_private_key_ex() allocates a new B<EVP_PKEY>. Unless an
94*b077aed3SPierre Proncheryengine should be used for the key type, a provider for the key is found using
95*b077aed3SPierre Proncherythe library context I<libctx> and the property query string I<propq>. The
96*b077aed3SPierre ProncheryI<keytype> argument indicates what kind of key this is. The value should be a
97*b077aed3SPierre Proncherystring for a public key algorithm that supports raw private keys, i.e one of
98*b077aed3SPierre Pronchery"X25519", "ED25519", "X448" or "ED448". I<key> points to the raw private key
99*b077aed3SPierre Proncherydata for this B<EVP_PKEY> which should be of length I<keylen>. The length
100*b077aed3SPierre Proncheryshould be appropriate for the type of the key. The public key data will be
101*b077aed3SPierre Proncheryautomatically derived from the given private key data (if appropriate for the
102*b077aed3SPierre Proncheryalgorithm type).
103*b077aed3SPierre Pronchery
104*b077aed3SPierre ProncheryEVP_PKEY_new_raw_private_key() does the same as
105*b077aed3SPierre ProncheryEVP_PKEY_new_raw_private_key_ex() except that the default library context and
106*b077aed3SPierre Proncherydefault property query are used instead. If I<e> is non-NULL then the new
107*b077aed3SPierre ProncheryB<EVP_PKEY> structure is associated with the engine I<e>. The I<type> argument
108*b077aed3SPierre Proncheryindicates what kind of key this is. The value should be a NID for a public key
109*b077aed3SPierre Proncheryalgorithm that supports raw private keys, i.e. one of B<EVP_PKEY_X25519>,
110*b077aed3SPierre ProncheryB<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or B<EVP_PKEY_ED448>.
111*b077aed3SPierre Pronchery
112*b077aed3SPierre ProncheryEVP_PKEY_new_raw_private_key_ex() and EVP_PKEY_new_raw_private_key() may also
113*b077aed3SPierre Proncherybe used with most MACs implemented as public key algorithms, so key types such
114*b077aed3SPierre Proncheryas "HMAC", "POLY1305", "SIPHASH", or their NID form B<EVP_PKEY_POLY1305>,
115*b077aed3SPierre ProncheryB<EVP_PKEY_SIPHASH>, B<EVP_PKEY_HMAC> are also accepted.  This usage is,
116*b077aed3SPierre Proncheryas mentioned above, discouraged in favor of the L<EVP_MAC(3)> API.
117*b077aed3SPierre Pronchery
118*b077aed3SPierre ProncheryEVP_PKEY_new_raw_public_key_ex() works in the same way as
119*b077aed3SPierre ProncheryEVP_PKEY_new_raw_private_key_ex() except that I<key> points to the raw
120*b077aed3SPierre Proncherypublic key data. The B<EVP_PKEY> structure will be initialised without any
121*b077aed3SPierre Proncheryprivate key information. Algorithm types that support raw public keys are
122*b077aed3SPierre Pronchery"X25519", "ED25519", "X448" or "ED448".
123e71b7053SJung-uk Kim
124e71b7053SJung-uk KimEVP_PKEY_new_raw_public_key() works in the same way as
125*b077aed3SPierre ProncheryEVP_PKEY_new_raw_private_key() except that I<key> points to the raw public key
126e71b7053SJung-uk Kimdata. The B<EVP_PKEY> structure will be initialised without any private key
127e71b7053SJung-uk Kiminformation. Algorithm types that support raw public keys are
128e71b7053SJung-uk KimB<EVP_PKEY_X25519>, B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or B<EVP_PKEY_ED448>.
129e71b7053SJung-uk Kim
130e71b7053SJung-uk KimEVP_PKEY_new_mac_key() works in the same way as EVP_PKEY_new_raw_private_key().
131e71b7053SJung-uk KimNew applications should use EVP_PKEY_new_raw_private_key() instead.
132e71b7053SJung-uk Kim
133*b077aed3SPierre ProncheryEVP_PKEY_get_raw_private_key() fills the buffer provided by I<priv> with raw
134*b077aed3SPierre Proncheryprivate key data. The size of the I<priv> buffer should be in I<*len> on entry
135*b077aed3SPierre Proncheryto the function, and on exit I<*len> is updated with the number of bytes
136*b077aed3SPierre Proncheryactually written. If the buffer I<priv> is NULL then I<*len> is populated with
13717f01e99SJung-uk Kimthe number of bytes required to hold the key. The calling application is
13817f01e99SJung-uk Kimresponsible for ensuring that the buffer is large enough to receive the private
13917f01e99SJung-uk Kimkey data. This function only works for algorithms that support raw private keys.
14017f01e99SJung-uk KimCurrently this is: B<EVP_PKEY_HMAC>, B<EVP_PKEY_POLY1305>, B<EVP_PKEY_SIPHASH>,
14117f01e99SJung-uk KimB<EVP_PKEY_X25519>, B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or B<EVP_PKEY_ED448>.
142e71b7053SJung-uk Kim
143*b077aed3SPierre ProncheryEVP_PKEY_get_raw_public_key() fills the buffer provided by I<pub> with raw
144*b077aed3SPierre Proncherypublic key data. The size of the I<pub> buffer should be in I<*len> on entry
145*b077aed3SPierre Proncheryto the function, and on exit I<*len> is updated with the number of bytes
146*b077aed3SPierre Proncheryactually written. If the buffer I<pub> is NULL then I<*len> is populated with
14717f01e99SJung-uk Kimthe number of bytes required to hold the key. The calling application is
14817f01e99SJung-uk Kimresponsible for ensuring that the buffer is large enough to receive the public
14917f01e99SJung-uk Kimkey data. This function only works for algorithms that support raw public  keys.
15017f01e99SJung-uk KimCurrently this is: B<EVP_PKEY_X25519>, B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or
15117f01e99SJung-uk KimB<EVP_PKEY_ED448>.
152e71b7053SJung-uk Kim
153*b077aed3SPierre ProncheryEVP_PKEY_new_CMAC_key() works in the same way as EVP_PKEY_new_raw_private_key()
154*b077aed3SPierre Proncheryexcept it is only for the B<EVP_PKEY_CMAC> algorithm type. In addition to the
155*b077aed3SPierre Proncheryraw private key data, it also takes a cipher algorithm to be used during
156*b077aed3SPierre Proncherycreation of a CMAC in the B<cipher> argument. The cipher should be a standard
157*b077aed3SPierre Proncheryencryption-only cipher. For example AEAD and XTS ciphers should not be used.
158*b077aed3SPierre Pronchery
159*b077aed3SPierre ProncheryApplications should use the L<EVP_MAC(3)> API instead
160*b077aed3SPierre Proncheryand set the B<OSSL_MAC_PARAM_CIPHER> parameter on the B<EVP_MAC_CTX> object
161*b077aed3SPierre Proncherywith the name of the cipher being used.
162*b077aed3SPierre Pronchery
163e71b7053SJung-uk Kim=head1 NOTES
164e71b7053SJung-uk Kim
165e71b7053SJung-uk KimThe B<EVP_PKEY> structure is used by various OpenSSL functions which require a
166e71b7053SJung-uk Kimgeneral private key without reference to any particular algorithm.
167e71b7053SJung-uk Kim
168e71b7053SJung-uk KimThe structure returned by EVP_PKEY_new() is empty. To add a private or public
169e71b7053SJung-uk Kimkey to this empty structure use the appropriate functions described in
170*b077aed3SPierre ProncheryL<EVP_PKEY_set1_RSA(3)>, L<EVP_PKEY_set1_DSA(3)>, L<EVP_PKEY_set1_DH(3)> or
171*b077aed3SPierre ProncheryL<EVP_PKEY_set1_EC_KEY(3)>.
172e71b7053SJung-uk Kim
173e71b7053SJung-uk Kim=head1 RETURN VALUES
174e71b7053SJung-uk Kim
175e71b7053SJung-uk KimEVP_PKEY_new(), EVP_PKEY_new_raw_private_key(), EVP_PKEY_new_raw_public_key(),
176e71b7053SJung-uk KimEVP_PKEY_new_CMAC_key() and EVP_PKEY_new_mac_key() return either the newly
177*b077aed3SPierre Proncheryallocated B<EVP_PKEY> structure or NULL if an error occurred.
178*b077aed3SPierre Pronchery
179*b077aed3SPierre ProncheryEVP_PKEY_dup() returns the key duplicate or NULL if an error occurred.
180e71b7053SJung-uk Kim
181e71b7053SJung-uk KimEVP_PKEY_up_ref(), EVP_PKEY_get_raw_private_key() and
182e71b7053SJung-uk KimEVP_PKEY_get_raw_public_key() return 1 for success and 0 for failure.
183e71b7053SJung-uk Kim
184e71b7053SJung-uk Kim=head1 SEE ALSO
185e71b7053SJung-uk Kim
186*b077aed3SPierre ProncheryL<EVP_PKEY_set1_RSA(3)>, L<EVP_PKEY_set1_DSA(3)>, L<EVP_PKEY_set1_DH(3)> or
187*b077aed3SPierre ProncheryL<EVP_PKEY_set1_EC_KEY(3)>
188e71b7053SJung-uk Kim
189e71b7053SJung-uk Kim=head1 HISTORY
190e71b7053SJung-uk Kim
1916935a639SJung-uk KimThe
1926935a639SJung-uk KimEVP_PKEY_new() and EVP_PKEY_free() functions exist in all versions of OpenSSL.
193e71b7053SJung-uk Kim
1946935a639SJung-uk KimThe EVP_PKEY_up_ref() function was added in OpenSSL 1.1.0.
1956935a639SJung-uk Kim
1966935a639SJung-uk KimThe
197e71b7053SJung-uk KimEVP_PKEY_new_raw_private_key(), EVP_PKEY_new_raw_public_key(),
198e71b7053SJung-uk KimEVP_PKEY_new_CMAC_key(), EVP_PKEY_new_raw_private_key() and
1996935a639SJung-uk KimEVP_PKEY_get_raw_public_key() functions were added in OpenSSL 1.1.1.
200e71b7053SJung-uk Kim
201*b077aed3SPierre ProncheryThe EVP_PKEY_dup(), EVP_PKEY_new_raw_private_key_ex(), and
202*b077aed3SPierre ProncheryEVP_PKEY_new_raw_public_key_ex()
203*b077aed3SPierre Proncheryfunctions were added in OpenSSL 3.0.
204*b077aed3SPierre Pronchery
205*b077aed3SPierre ProncheryThe EVP_PKEY_new_CMAC_key() was deprecated in OpenSSL 3.0.
206*b077aed3SPierre Pronchery
207*b077aed3SPierre ProncheryThe documentation of B<EVP_PKEY> was amended in OpenSSL 3.0 to allow there to
208*b077aed3SPierre Proncherybe the private part of the keypair without the public part, where this was
209*b077aed3SPierre Proncherypreviously implied to be disallowed.
210*b077aed3SPierre Pronchery
211e71b7053SJung-uk Kim=head1 COPYRIGHT
212e71b7053SJung-uk Kim
213*b077aed3SPierre ProncheryCopyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
214e71b7053SJung-uk Kim
215*b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
216e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
217e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
218e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
219e71b7053SJung-uk Kim
220e71b7053SJung-uk Kim=cut
221