1EVP_PKEY_NEW(3ossl) OpenSSL EVP_PKEY_NEW(3ossl)
2
3
4
6 EVP_PKEY, EVP_PKEY_new, EVP_PKEY_up_ref, EVP_PKEY_dup, EVP_PKEY_free,
7 EVP_PKEY_new_raw_private_key_ex, EVP_PKEY_new_raw_private_key,
8 EVP_PKEY_new_raw_public_key_ex, EVP_PKEY_new_raw_public_key,
9 EVP_PKEY_new_CMAC_key, EVP_PKEY_new_mac_key,
10 EVP_PKEY_get_raw_private_key, EVP_PKEY_get_raw_public_key -
11 public/private key allocation and raw key handling functions
12
14 #include <openssl/evp.h>
15
16 typedef evp_pkey_st EVP_PKEY;
17
18 EVP_PKEY *EVP_PKEY_new(void);
19 int EVP_PKEY_up_ref(EVP_PKEY *key);
20 EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *key);
21 void EVP_PKEY_free(EVP_PKEY *key);
22
23 EVP_PKEY *EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX *libctx,
24 const char *keytype,
25 const char *propq,
26 const unsigned char *key,
27 size_t keylen);
28 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
29 const unsigned char *key, size_t keylen);
30 EVP_PKEY *EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX *libctx,
31 const char *keytype,
32 const char *propq,
33 const unsigned char *key,
34 size_t keylen);
35 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
36 const unsigned char *key, size_t keylen);
37 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, const unsigned char *key,
38 int keylen);
39
40 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
41 size_t *len);
42 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
43 size_t *len);
44
45 The following function has been deprecated since OpenSSL 3.0, and can
46 be hidden entirely by defining OPENSSL_API_COMPAT with a suitable
47 version value, see openssl_user_macros(7):
48
49 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
50 size_t len, const EVP_CIPHER *cipher);
51
53 EVP_PKEY is a generic structure to hold diverse types of asymmetric
54 keys (also known as "key pairs"), and can be used for diverse
55 operations, like signing, verifying signatures, key derivation, etc.
56 The asymmetric keys themselves are often refered to as the "internal
57 key", and are handled by backends, such as providers (through
58 EVP_KEYMGMT(3)) or ENGINEs.
59
60 Conceptually, an EVP_PKEY internal key may hold a private key, a public
61 key, or both (a keypair), and along with those, key parameters if the
62 key type requires them. The presence of these components determine
63 what operations can be made; for example, signing normally requires the
64 presence of a private key, and verifying normally requires the presence
65 of a public key.
66
67 EVP_PKEY has also been used for MAC algorithm that were conceived as
68 producing signatures, although not being public key algorithms;
69 "POLY1305", "SIPHASH", "HMAC", "CMAC". This usage is considered legacy
70 and is discouraged in favor of the EVP_MAC(3) API.
71
72 The EVP_PKEY_new() function allocates an empty EVP_PKEY structure which
73 is used by OpenSSL to store public and private keys. The reference
74 count is set to 1.
75
76 EVP_PKEY_up_ref() increments the reference count of key.
77
78 EVP_PKEY_dup() duplicates the key. The key must not be ENGINE based or
79 a raw key, otherwise the duplication will fail.
80
81 EVP_PKEY_free() decrements the reference count of key and, if the
82 reference count is zero, frees it up. If key is NULL, nothing is done.
83
84 EVP_PKEY_new_raw_private_key_ex() allocates a new EVP_PKEY. Unless an
85 engine should be used for the key type, a provider for the key is found
86 using the library context libctx and the property query string propq.
87 The keytype argument indicates what kind of key this is. The value
88 should be a string for a public key algorithm that supports raw private
89 keys, i.e one of "X25519", "ED25519", "X448" or "ED448". key points to
90 the raw private key data for this EVP_PKEY which should be of length
91 keylen. The length should be appropriate for the type of the key. The
92 public key data will be automatically derived from the given private
93 key data (if appropriate for the algorithm type).
94
95 EVP_PKEY_new_raw_private_key() does the same as
96 EVP_PKEY_new_raw_private_key_ex() except that the default library
97 context and default property query are used instead. If e is non-NULL
98 then the new EVP_PKEY structure is associated with the engine e. The
99 type argument indicates what kind of key this is. The value should be a
100 NID for a public key algorithm that supports raw private keys, i.e. one
101 of EVP_PKEY_X25519, EVP_PKEY_ED25519, EVP_PKEY_X448 or EVP_PKEY_ED448.
102
103 EVP_PKEY_new_raw_private_key_ex() and EVP_PKEY_new_raw_private_key()
104 may also be used with most MACs implemented as public key algorithms,
105 so key types such as "HMAC", "POLY1305", "SIPHASH", or their NID form
106 EVP_PKEY_POLY1305, EVP_PKEY_SIPHASH, EVP_PKEY_HMAC are also accepted.
107 This usage is, as mentioned above, discouraged in favor of the
108 EVP_MAC(3) API.
109
110 EVP_PKEY_new_raw_public_key_ex() works in the same way as
111 EVP_PKEY_new_raw_private_key_ex() except that key points to the raw
112 public key data. The EVP_PKEY structure will be initialised without any
113 private key information. Algorithm types that support raw public keys
114 are "X25519", "ED25519", "X448" or "ED448".
115
116 EVP_PKEY_new_raw_public_key() works in the same way as
117 EVP_PKEY_new_raw_private_key() except that key points to the raw public
118 key data. The EVP_PKEY structure will be initialised without any
119 private key information. Algorithm types that support raw public keys
120 are EVP_PKEY_X25519, EVP_PKEY_ED25519, EVP_PKEY_X448 or EVP_PKEY_ED448.
121
122 EVP_PKEY_new_mac_key() works in the same way as
123 EVP_PKEY_new_raw_private_key(). New applications should use
124 EVP_PKEY_new_raw_private_key() instead.
125
126 EVP_PKEY_get_raw_private_key() fills the buffer provided by priv with
127 raw private key data. The size of the priv buffer should be in *len on
128 entry to the function, and on exit *len is updated with the number of
129 bytes actually written. If the buffer priv is NULL then *len is
130 populated with the number of bytes required to hold the key. The
131 calling application is responsible for ensuring that the buffer is
132 large enough to receive the private key data. This function only works
133 for algorithms that support raw private keys. Currently this is:
134 EVP_PKEY_HMAC, EVP_PKEY_POLY1305, EVP_PKEY_SIPHASH, EVP_PKEY_X25519,
135 EVP_PKEY_ED25519, EVP_PKEY_X448 or EVP_PKEY_ED448.
136
137 EVP_PKEY_get_raw_public_key() fills the buffer provided by pub with raw
138 public key data. The size of the pub buffer should be in *len on entry
139 to the function, and on exit *len is updated with the number of bytes
140 actually written. If the buffer pub is NULL then *len is populated with
141 the number of bytes required to hold the key. The calling application
142 is responsible for ensuring that the buffer is large enough to receive
143 the public key data. This function only works for algorithms that
144 support raw public keys. Currently this is: EVP_PKEY_X25519,
145 EVP_PKEY_ED25519, EVP_PKEY_X448 or EVP_PKEY_ED448.
146
147 EVP_PKEY_new_CMAC_key() works in the same way as
148 EVP_PKEY_new_raw_private_key() except it is only for the EVP_PKEY_CMAC
149 algorithm type. In addition to the raw private key data, it also takes
150 a cipher algorithm to be used during creation of a CMAC in the cipher
151 argument. The cipher should be a standard encryption-only cipher. For
152 example AEAD and XTS ciphers should not be used.
153
154 Applications should use the EVP_MAC(3) API instead and set the
155 OSSL_MAC_PARAM_CIPHER parameter on the EVP_MAC_CTX object with the name
156 of the cipher being used.
157
159 The EVP_PKEY structure is used by various OpenSSL functions which
160 require a general private key without reference to any particular
161 algorithm.
162
163 The structure returned by EVP_PKEY_new() is empty. To add a private or
164 public key to this empty structure use the appropriate functions
165 described in EVP_PKEY_set1_RSA(3), EVP_PKEY_set1_DSA(3),
166 EVP_PKEY_set1_DH(3) or EVP_PKEY_set1_EC_KEY(3).
167
169 EVP_PKEY_new(), EVP_PKEY_new_raw_private_key(),
170 EVP_PKEY_new_raw_public_key(), EVP_PKEY_new_CMAC_key() and
171 EVP_PKEY_new_mac_key() return either the newly allocated EVP_PKEY
172 structure or NULL if an error occurred.
173
174 EVP_PKEY_dup() returns the key duplicate or NULL if an error occurred.
175
176 EVP_PKEY_up_ref(), EVP_PKEY_get_raw_private_key() and
177 EVP_PKEY_get_raw_public_key() return 1 for success and 0 for failure.
178
180 EVP_PKEY_set1_RSA(3), EVP_PKEY_set1_DSA(3), EVP_PKEY_set1_DH(3) or
181 EVP_PKEY_set1_EC_KEY(3)
182
184 The EVP_PKEY_new() and EVP_PKEY_free() functions exist in all versions
185 of OpenSSL.
186
187 The EVP_PKEY_up_ref() function was added in OpenSSL 1.1.0.
188
189 The EVP_PKEY_new_raw_private_key(), EVP_PKEY_new_raw_public_key(),
190 EVP_PKEY_new_CMAC_key(), EVP_PKEY_new_raw_private_key() and
191 EVP_PKEY_get_raw_public_key() functions were added in OpenSSL 1.1.1.
192
193 The EVP_PKEY_dup(), EVP_PKEY_new_raw_private_key_ex(), and
194 EVP_PKEY_new_raw_public_key_ex() functions were added in OpenSSL 3.0.
195
196 The EVP_PKEY_new_CMAC_key() was deprecated in OpenSSL 3.0.
197
198 The documentation of EVP_PKEY was amended in OpenSSL 3.0 to allow there
199 to be the private part of the keypair without the public part, where
200 this was previously implied to be disallowed.
201
203 Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
204
205 Licensed under the Apache License 2.0 (the "License"). You may not use
206 this file except in compliance with the License. You can obtain a copy
207 in the file LICENSE in the source distribution or at
208 <https://www.openssl.org/source/license.html>.
209
210
211
2123.0.5 2022-11-01 EVP_PKEY_NEW(3ossl)