xref: /freebsd/crypto/openssl/doc/man3/EVP_PKEY_new.pod (revision aa7957345732816fb0ba8308798d2f79f45597f9)
1e71b7053SJung-uk Kim=pod
2e71b7053SJung-uk Kim
3e71b7053SJung-uk Kim=head1 NAME
4e71b7053SJung-uk Kim
5b077aed3SPierre ProncheryEVP_PKEY,
6e71b7053SJung-uk KimEVP_PKEY_new,
7e71b7053SJung-uk KimEVP_PKEY_up_ref,
8b077aed3SPierre ProncheryEVP_PKEY_dup,
9e71b7053SJung-uk KimEVP_PKEY_free,
10b077aed3SPierre ProncheryEVP_PKEY_new_raw_private_key_ex,
11e71b7053SJung-uk KimEVP_PKEY_new_raw_private_key,
12b077aed3SPierre 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
24b077aed3SPierre Pronchery typedef evp_pkey_st EVP_PKEY;
25b077aed3SPierre Pronchery
26e71b7053SJung-uk Kim EVP_PKEY *EVP_PKEY_new(void);
27e71b7053SJung-uk Kim int EVP_PKEY_up_ref(EVP_PKEY *key);
28b077aed3SPierre Pronchery EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *key);
29e71b7053SJung-uk Kim void EVP_PKEY_free(EVP_PKEY *key);
30e71b7053SJung-uk Kim
31b077aed3SPierre Pronchery EVP_PKEY *EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX *libctx,
32b077aed3SPierre Pronchery                                           const char *keytype,
33b077aed3SPierre Pronchery                                           const char *propq,
34b077aed3SPierre Pronchery                                           const unsigned char *key,
35b077aed3SPierre 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);
38b077aed3SPierre Pronchery EVP_PKEY *EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX *libctx,
39b077aed3SPierre Pronchery                                          const char *keytype,
40b077aed3SPierre Pronchery                                          const char *propq,
41b077aed3SPierre Pronchery                                          const unsigned char *key,
42b077aed3SPierre 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
53b077aed3SPierre ProncheryThe following function has been deprecated since OpenSSL 3.0, and can be
54b077aed3SPierre Proncheryhidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
55b077aed3SPierre Proncherysee L<openssl_user_macros(7)>:
56b077aed3SPierre Pronchery
57b077aed3SPierre Pronchery EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
58b077aed3SPierre Pronchery                                 size_t len, const EVP_CIPHER *cipher);
59b077aed3SPierre Pronchery
60e71b7053SJung-uk Kim=head1 DESCRIPTION
61e71b7053SJung-uk Kim
62b077aed3SPierre ProncheryB<EVP_PKEY> is a generic structure to hold diverse types of asymmetric keys
63b077aed3SPierre Pronchery(also known as "key pairs"), and can be used for diverse operations, like
64b077aed3SPierre Proncherysigning, verifying signatures, key derivation, etc.  The asymmetric keys
65*aa795734SPierre Proncherythemselves are often referred to as the "internal key", and are handled by
66b077aed3SPierre Proncherybackends, such as providers (through L<EVP_KEYMGMT(3)>) or B<ENGINE>s.
67b077aed3SPierre Pronchery
68b077aed3SPierre ProncheryConceptually, an B<EVP_PKEY> internal key may hold a private key, a public
69b077aed3SPierre Proncherykey, or both (a keypair), and along with those, key parameters if the key type
70b077aed3SPierre Proncheryrequires them.  The presence of these components determine what operations can
71b077aed3SPierre Proncherybe made; for example, signing normally requires the presence of a private key,
72b077aed3SPierre Proncheryand verifying normally requires the presence of a public key.
73b077aed3SPierre Pronchery
74b077aed3SPierre Pronchery=for comment ED signature require both the private and public key...
75b077aed3SPierre Pronchery
76b077aed3SPierre ProncheryB<EVP_PKEY> has also been used for MAC algorithm that were conceived as
77b077aed3SPierre Proncheryproducing signatures, although not being public key algorithms; "POLY1305",
78b077aed3SPierre Pronchery"SIPHASH", "HMAC", "CMAC".  This usage is considered legacy and is discouraged
79b077aed3SPierre Proncheryin favor of the L<EVP_MAC(3)> API.
80b077aed3SPierre 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
85b077aed3SPierre ProncheryEVP_PKEY_up_ref() increments the reference count of I<key>.
86e71b7053SJung-uk Kim
87b077aed3SPierre ProncheryEVP_PKEY_dup() duplicates the I<key>. The I<key> must not be ENGINE based or
88b077aed3SPierre Proncherya raw key, otherwise the duplication will fail.
89e71b7053SJung-uk Kim
90b077aed3SPierre ProncheryEVP_PKEY_free() decrements the reference count of I<key> and, if the reference
91b077aed3SPierre Proncherycount is zero, frees it up. If I<key> is NULL, nothing is done.
92b077aed3SPierre Pronchery
93b077aed3SPierre ProncheryEVP_PKEY_new_raw_private_key_ex() allocates a new B<EVP_PKEY>. Unless an
94b077aed3SPierre Proncheryengine should be used for the key type, a provider for the key is found using
95b077aed3SPierre Proncherythe library context I<libctx> and the property query string I<propq>. The
96b077aed3SPierre ProncheryI<keytype> argument indicates what kind of key this is. The value should be a
97b077aed3SPierre Proncherystring for a public key algorithm that supports raw private keys, i.e one of
98b077aed3SPierre Pronchery"X25519", "ED25519", "X448" or "ED448". I<key> points to the raw private key
99b077aed3SPierre Proncherydata for this B<EVP_PKEY> which should be of length I<keylen>. The length
100b077aed3SPierre Proncheryshould be appropriate for the type of the key. The public key data will be
101b077aed3SPierre Proncheryautomatically derived from the given private key data (if appropriate for the
102b077aed3SPierre Proncheryalgorithm type).
103b077aed3SPierre Pronchery
104b077aed3SPierre ProncheryEVP_PKEY_new_raw_private_key() does the same as
105b077aed3SPierre ProncheryEVP_PKEY_new_raw_private_key_ex() except that the default library context and
106b077aed3SPierre Proncherydefault property query are used instead. If I<e> is non-NULL then the new
107b077aed3SPierre ProncheryB<EVP_PKEY> structure is associated with the engine I<e>. The I<type> argument
108b077aed3SPierre Proncheryindicates what kind of key this is. The value should be a NID for a public key
109b077aed3SPierre Proncheryalgorithm that supports raw private keys, i.e. one of B<EVP_PKEY_X25519>,
110b077aed3SPierre ProncheryB<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or B<EVP_PKEY_ED448>.
111b077aed3SPierre Pronchery
112b077aed3SPierre ProncheryEVP_PKEY_new_raw_private_key_ex() and EVP_PKEY_new_raw_private_key() may also
113b077aed3SPierre Proncherybe used with most MACs implemented as public key algorithms, so key types such
114b077aed3SPierre Proncheryas "HMAC", "POLY1305", "SIPHASH", or their NID form B<EVP_PKEY_POLY1305>,
115b077aed3SPierre ProncheryB<EVP_PKEY_SIPHASH>, B<EVP_PKEY_HMAC> are also accepted.  This usage is,
116b077aed3SPierre Proncheryas mentioned above, discouraged in favor of the L<EVP_MAC(3)> API.
117b077aed3SPierre Pronchery
118b077aed3SPierre ProncheryEVP_PKEY_new_raw_public_key_ex() works in the same way as
119b077aed3SPierre ProncheryEVP_PKEY_new_raw_private_key_ex() except that I<key> points to the raw
120b077aed3SPierre Proncherypublic key data. The B<EVP_PKEY> structure will be initialised without any
121b077aed3SPierre Proncheryprivate key information. Algorithm types that support raw public keys are
122b077aed3SPierre Pronchery"X25519", "ED25519", "X448" or "ED448".
123e71b7053SJung-uk Kim
124e71b7053SJung-uk KimEVP_PKEY_new_raw_public_key() works in the same way as
125b077aed3SPierre 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
133b077aed3SPierre ProncheryEVP_PKEY_get_raw_private_key() fills the buffer provided by I<priv> with raw
134b077aed3SPierre Proncheryprivate key data. The size of the I<priv> buffer should be in I<*len> on entry
135b077aed3SPierre Proncheryto the function, and on exit I<*len> is updated with the number of bytes
136b077aed3SPierre 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
143b077aed3SPierre ProncheryEVP_PKEY_get_raw_public_key() fills the buffer provided by I<pub> with raw
144b077aed3SPierre Proncherypublic key data. The size of the I<pub> buffer should be in I<*len> on entry
145b077aed3SPierre Proncheryto the function, and on exit I<*len> is updated with the number of bytes
146b077aed3SPierre 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
153b077aed3SPierre ProncheryEVP_PKEY_new_CMAC_key() works in the same way as EVP_PKEY_new_raw_private_key()
154b077aed3SPierre Proncheryexcept it is only for the B<EVP_PKEY_CMAC> algorithm type. In addition to the
155b077aed3SPierre Proncheryraw private key data, it also takes a cipher algorithm to be used during
156b077aed3SPierre Proncherycreation of a CMAC in the B<cipher> argument. The cipher should be a standard
157b077aed3SPierre Proncheryencryption-only cipher. For example AEAD and XTS ciphers should not be used.
158b077aed3SPierre Pronchery
159b077aed3SPierre ProncheryApplications should use the L<EVP_MAC(3)> API instead
160b077aed3SPierre Proncheryand set the B<OSSL_MAC_PARAM_CIPHER> parameter on the B<EVP_MAC_CTX> object
161b077aed3SPierre Proncherywith the name of the cipher being used.
162b077aed3SPierre 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
170b077aed3SPierre ProncheryL<EVP_PKEY_set1_RSA(3)>, L<EVP_PKEY_set1_DSA(3)>, L<EVP_PKEY_set1_DH(3)> or
171b077aed3SPierre 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
177b077aed3SPierre Proncheryallocated B<EVP_PKEY> structure or NULL if an error occurred.
178b077aed3SPierre Pronchery
179b077aed3SPierre 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
186b077aed3SPierre ProncheryL<EVP_PKEY_set1_RSA(3)>, L<EVP_PKEY_set1_DSA(3)>, L<EVP_PKEY_set1_DH(3)> or
187b077aed3SPierre 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
201b077aed3SPierre ProncheryThe EVP_PKEY_dup(), EVP_PKEY_new_raw_private_key_ex(), and
202b077aed3SPierre ProncheryEVP_PKEY_new_raw_public_key_ex()
203b077aed3SPierre Proncheryfunctions were added in OpenSSL 3.0.
204b077aed3SPierre Pronchery
205b077aed3SPierre ProncheryThe EVP_PKEY_new_CMAC_key() was deprecated in OpenSSL 3.0.
206b077aed3SPierre Pronchery
207b077aed3SPierre ProncheryThe documentation of B<EVP_PKEY> was amended in OpenSSL 3.0 to allow there to
208b077aed3SPierre Proncherybe the private part of the keypair without the public part, where this was
209b077aed3SPierre Proncherypreviously implied to be disallowed.
210b077aed3SPierre Pronchery
211e71b7053SJung-uk Kim=head1 COPYRIGHT
212e71b7053SJung-uk Kim
213*aa795734SPierre ProncheryCopyright 2002-2023 The OpenSSL Project Authors. All Rights Reserved.
214e71b7053SJung-uk Kim
215b077aed3SPierre 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