Document the new raw private/public key functions
[openssl.git] / doc / man3 / EVP_PKEY_new.pod
1 =pod
2
3 =head1 NAME
4
5 EVP_PKEY_new,
6 EVP_PKEY_up_ref,
7 EVP_PKEY_free,
8 EVP_PKEY_new_raw_private_key_with_libctx,
9 EVP_PKEY_new_raw_private_key,
10 EVP_PKEY_new_raw_public_key_with_libctx,
11 EVP_PKEY_new_raw_public_key,
12 EVP_PKEY_new_CMAC_key,
13 EVP_PKEY_new_mac_key,
14 EVP_PKEY_get_raw_private_key,
15 EVP_PKEY_get_raw_public_key
16 - public/private key allocation and raw key handling functions
17
18 =head1 SYNOPSIS
19
20  #include <openssl/evp.h>
21
22  EVP_PKEY *EVP_PKEY_new(void);
23  int EVP_PKEY_up_ref(EVP_PKEY *key);
24  void EVP_PKEY_free(EVP_PKEY *key);
25
26  EVP_PKEY *EVP_PKEY_new_raw_private_key_with_libctx(OPENSSL_CTX *libctx,
27                                                     const char *keytype,
28                                                     const char *propq,
29                                                     const unsigned char *key,
30                                                     size_t keylen);
31  EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
32                                         const unsigned char *key, size_t keylen);
33  EVP_PKEY *EVP_PKEY_new_raw_public_key_with_libctx(OPENSSL_CTX *libctx,
34                                                    const char *keytype,
35                                                    const char *propq,
36                                                    const unsigned char *key,
37                                                    size_t keylen);
38  EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
39                                        const unsigned char *key, size_t keylen);
40  EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
41                                  size_t len, const EVP_CIPHER *cipher);
42  EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, const unsigned char *key,
43                                 int keylen);
44
45  int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
46                                   size_t *len);
47  int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
48                                  size_t *len);
49
50 =head1 DESCRIPTION
51
52 The EVP_PKEY_new() function allocates an empty B<EVP_PKEY> structure which is
53 used by OpenSSL to store public and private keys. The reference count is set to
54 B<1>.
55
56 EVP_PKEY_up_ref() increments the reference count of B<key>.
57
58 EVP_PKEY_free() decrements the reference count of B<key> and, if the reference
59 count is zero, frees it up. If B<key> is NULL, nothing is done.
60
61 EVP_PKEY_new_raw_private_key_with_libctx() allocates a new B<EVP_PKEY>. Unless
62 an engine should be used for the key type, a provider for the key is found using
63 the library context I<libctx> and the property query string I<propq>. The
64 I<keytype> argument indicates what kind of key this is. The value should be a
65 string for a public key algorithm that supports raw private keys, i.e one of
66 "POLY1305", "SIPHASH", "X25519", "ED25519", "X448" or "ED448". Note that you may
67 also use "HMAC" which is not a public key algorithm but is treated as such by
68 some OpenSSL APIs. You are encouraged to use the EVP_MAC APIs instead for HMAC
69 (see L<EVP_MAC(3)>). I<key> points to the raw private key data for this
70 B<EVP_PKEY> which should be of length I<keylen>. The length should be
71 appropriate for the type of the key. The public key data will be automatically
72 derived from the given private key data (if appropriate for the algorithm type).
73
74 EVP_PKEY_new_raw_private_key() does the same as
75 EVP_PKEY_new_raw_private_key_with_libctx() except that the default library
76 context and default property query are used instead. If B<e> is non-NULL then
77 the new B<EVP_PKEY> structure is associated with the engine B<e>. The B<type>
78 argument indicates what kind of key this is. The value should be a NID for a
79 public key algorithm that supports raw private keys, i.e. one of
80 B<EVP_PKEY_POLY1305>, B<EVP_PKEY_SIPHASH>, B<EVP_PKEY_X25519>,
81 B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or B<EVP_PKEY_ED448>. As for
82 EVP_PKEY_new_raw_private_key_with_libctx() you may also use B<EVP_PKEY_HMAC>.
83
84 EVP_PKEY_new_raw_public_key_with_libctx() works in the same way as
85 EVP_PKEY_new_raw_private_key_with_libctx() except that B<key> points to the raw
86 public key data. The B<EVP_PKEY> structure will be initialised without any
87 private key information. Algorithm types that support raw public keys are
88 "X25519", "ED25519", "X448" or "ED448".
89
90 EVP_PKEY_new_raw_public_key() works in the same way as
91 EVP_PKEY_new_raw_private_key() except that B<key> points to the raw public key
92 data. The B<EVP_PKEY> structure will be initialised without any private key
93 information. Algorithm types that support raw public keys are
94 B<EVP_PKEY_X25519>, B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or B<EVP_PKEY_ED448>.
95
96 EVP_PKEY_new_CMAC_key() works in the same way as EVP_PKEY_new_raw_private_key()
97 except it is only for the B<EVP_PKEY_CMAC> algorithm type. In addition to the
98 raw private key data, it also takes a cipher algorithm to be used during
99 creation of a CMAC in the B<cipher> argument.
100
101 EVP_PKEY_new_mac_key() works in the same way as EVP_PKEY_new_raw_private_key().
102 New applications should use EVP_PKEY_new_raw_private_key() instead.
103
104 EVP_PKEY_get_raw_private_key() fills the buffer provided by B<priv> with raw
105 private key data. The size of the B<priv> buffer should be in B<*len> on entry
106 to the function, and on exit B<*len> is updated with the number of bytes
107 actually written. If the buffer B<priv> is NULL then B<*len> is populated with
108 the number of bytes required to hold the key. The calling application is
109 responsible for ensuring that the buffer is large enough to receive the private
110 key data. This function only works for algorithms that support raw private keys.
111 Currently this is: B<EVP_PKEY_HMAC>, B<EVP_PKEY_POLY1305>, B<EVP_PKEY_SIPHASH>,
112 B<EVP_PKEY_X25519>, B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or B<EVP_PKEY_ED448>.
113
114 EVP_PKEY_get_raw_public_key() fills the buffer provided by B<pub> with raw
115 public key data. The size of the B<pub> buffer should be in B<*len> on entry
116 to the function, and on exit B<*len> is updated with the number of bytes
117 actually written. If the buffer B<pub> is NULL then B<*len> is populated with
118 the number of bytes required to hold the key. The calling application is
119 responsible for ensuring that the buffer is large enough to receive the public
120 key data. This function only works for algorithms that support raw public  keys.
121 Currently this is: B<EVP_PKEY_X25519>, B<EVP_PKEY_ED25519>, B<EVP_PKEY_X448> or
122 B<EVP_PKEY_ED448>.
123
124 =head1 NOTES
125
126 The B<EVP_PKEY> structure is used by various OpenSSL functions which require a
127 general private key without reference to any particular algorithm.
128
129 The structure returned by EVP_PKEY_new() is empty. To add a private or public
130 key to this empty structure use the appropriate functions described in
131 L<EVP_PKEY_set1_RSA(3)>, L<EVP_PKEY_set1_DSA(3)>, L<EVP_PKEY_set1_DH(3)> or
132 L<EVP_PKEY_set1_EC_KEY(3)>.
133
134 =head1 RETURN VALUES
135
136 EVP_PKEY_new(), EVP_PKEY_new_raw_private_key(), EVP_PKEY_new_raw_public_key(),
137 EVP_PKEY_new_CMAC_key() and EVP_PKEY_new_mac_key() return either the newly
138 allocated B<EVP_PKEY> structure or B<NULL> if an error occurred.
139
140 EVP_PKEY_up_ref(), EVP_PKEY_get_raw_private_key() and
141 EVP_PKEY_get_raw_public_key() return 1 for success and 0 for failure.
142
143 =head1 SEE ALSO
144
145 L<EVP_PKEY_set1_RSA(3)>, L<EVP_PKEY_set1_DSA(3)>, L<EVP_PKEY_set1_DH(3)> or
146 L<EVP_PKEY_set1_EC_KEY(3)>
147
148 =head1 HISTORY
149
150 The
151 EVP_PKEY_new() and EVP_PKEY_free() functions exist in all versions of OpenSSL.
152
153 The EVP_PKEY_up_ref() function was added in OpenSSL 1.1.0.
154
155 The
156 EVP_PKEY_new_raw_private_key(), EVP_PKEY_new_raw_public_key(),
157 EVP_PKEY_new_CMAC_key(), EVP_PKEY_new_raw_private_key() and
158 EVP_PKEY_get_raw_public_key() functions were added in OpenSSL 1.1.1.
159
160 The EVP_PKEY_new_raw_private_key_with_libctx and
161 EVP_PKEY_new_raw_public_key_with_libctx functions were added in OpenSSL 3.0.
162
163 =head1 COPYRIGHT
164
165 Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
166
167 Licensed under the Apache License 2.0 (the "License").  You may not use
168 this file except in compliance with the License.  You can obtain a copy
169 in the file LICENSE in the source distribution or at
170 L<https://www.openssl.org/source/license.html>.
171
172 =cut