• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #ifndef OPENSSL_HEADER_EVP_H
58 #define OPENSSL_HEADER_EVP_H
59 
60 #include <openssl/base.h>
61 
62 #include <openssl/evp_errors.h>
63 #include <openssl/thread.h>
64 
65 // OpenSSL included digest and cipher functions in this header so we include
66 // them for users that still expect that.
67 //
68 // TODO(fork): clean up callers so that they include what they use.
69 #include <openssl/aead.h>
70 #include <openssl/base64.h>
71 #include <openssl/cipher.h>
72 #include <openssl/digest.h>
73 #include <openssl/nid.h>
74 
75 #if defined(__cplusplus)
76 extern "C" {
77 #endif
78 
79 
80 // EVP abstracts over public/private key algorithms.
81 
82 
83 // Public key objects.
84 //
85 // An |EVP_PKEY| object represents a public or private key. A given object may
86 // be used concurrently on multiple threads by non-mutating functions, provided
87 // no other thread is concurrently calling a mutating function. Unless otherwise
88 // documented, functions which take a |const| pointer are non-mutating and
89 // functions which take a non-|const| pointer are mutating.
90 
91 // EVP_PKEY_new creates a new, empty public-key object and returns it or NULL
92 // on allocation failure.
93 OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_new(void);
94 
95 // EVP_PKEY_free frees all data referenced by |pkey| and then frees |pkey|
96 // itself.
97 OPENSSL_EXPORT void EVP_PKEY_free(EVP_PKEY *pkey);
98 
99 // EVP_PKEY_up_ref increments the reference count of |pkey| and returns one. It
100 // does not mutate |pkey| for thread-safety purposes and may be used
101 // concurrently.
102 OPENSSL_EXPORT int EVP_PKEY_up_ref(EVP_PKEY *pkey);
103 
104 // EVP_PKEY_is_opaque returns one if |pkey| is opaque. Opaque keys are backed by
105 // custom implementations which do not expose key material and parameters. It is
106 // an error to attempt to duplicate, export, or compare an opaque key.
107 OPENSSL_EXPORT int EVP_PKEY_is_opaque(const EVP_PKEY *pkey);
108 
109 // EVP_PKEY_cmp compares |a| and |b| and returns one if they are equal, zero if
110 // not and a negative number on error.
111 //
112 // WARNING: this differs from the traditional return value of a "cmp"
113 // function.
114 OPENSSL_EXPORT int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b);
115 
116 // EVP_PKEY_copy_parameters sets the parameters of |to| to equal the parameters
117 // of |from|. It returns one on success and zero on error.
118 OPENSSL_EXPORT int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from);
119 
120 // EVP_PKEY_missing_parameters returns one if |pkey| is missing needed
121 // parameters or zero if not, or if the algorithm doesn't take parameters.
122 OPENSSL_EXPORT int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey);
123 
124 // EVP_PKEY_size returns the maximum size, in bytes, of a signature signed by
125 // |pkey|. For an RSA key, this returns the number of bytes needed to represent
126 // the modulus. For an EC key, this returns the maximum size of a DER-encoded
127 // ECDSA signature.
128 OPENSSL_EXPORT int EVP_PKEY_size(const EVP_PKEY *pkey);
129 
130 // EVP_PKEY_bits returns the "size", in bits, of |pkey|. For an RSA key, this
131 // returns the bit length of the modulus. For an EC key, this returns the bit
132 // length of the group order.
133 OPENSSL_EXPORT int EVP_PKEY_bits(const EVP_PKEY *pkey);
134 
135 // EVP_PKEY_id returns the type of |pkey|, which is one of the |EVP_PKEY_*|
136 // values.
137 OPENSSL_EXPORT int EVP_PKEY_id(const EVP_PKEY *pkey);
138 
139 // EVP_PKEY_type returns |nid| if |nid| is a known key type and |NID_undef|
140 // otherwise.
141 OPENSSL_EXPORT int EVP_PKEY_type(int nid);
142 
143 
144 // Getting and setting concrete public key types.
145 //
146 // The following functions get and set the underlying public key in an
147 // |EVP_PKEY| object. The |set1| functions take an additional reference to the
148 // underlying key and return one on success or zero if |key| is NULL. The
149 // |assign| functions adopt the caller's reference and return one on success or
150 // zero if |key| is NULL. The |get1| functions return a fresh reference to the
151 // underlying object or NULL if |pkey| is not of the correct type. The |get0|
152 // functions behave the same but return a non-owning pointer.
153 //
154 // The |get0| and |get1| functions take |const| pointers and are thus
155 // non-mutating for thread-safety purposes, but mutating functions on the
156 // returned lower-level objects are considered to also mutate the |EVP_PKEY| and
157 // may not be called concurrently with other operations on the |EVP_PKEY|.
158 
159 OPENSSL_EXPORT int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key);
160 OPENSSL_EXPORT int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key);
161 OPENSSL_EXPORT RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey);
162 OPENSSL_EXPORT RSA *EVP_PKEY_get1_RSA(const EVP_PKEY *pkey);
163 
164 OPENSSL_EXPORT int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key);
165 OPENSSL_EXPORT int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key);
166 OPENSSL_EXPORT DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey);
167 OPENSSL_EXPORT DSA *EVP_PKEY_get1_DSA(const EVP_PKEY *pkey);
168 
169 OPENSSL_EXPORT int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key);
170 OPENSSL_EXPORT int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key);
171 OPENSSL_EXPORT EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey);
172 OPENSSL_EXPORT EC_KEY *EVP_PKEY_get1_EC_KEY(const EVP_PKEY *pkey);
173 
174 #define EVP_PKEY_NONE NID_undef
175 #define EVP_PKEY_RSA NID_rsaEncryption
176 #define EVP_PKEY_RSA_PSS NID_rsassaPss
177 #define EVP_PKEY_DSA NID_dsa
178 #define EVP_PKEY_EC NID_X9_62_id_ecPublicKey
179 #define EVP_PKEY_ED25519 NID_ED25519
180 #define EVP_PKEY_X25519 NID_X25519
181 #define EVP_PKEY_HKDF NID_hkdf
182 
183 // EVP_PKEY_assign sets the underlying key of |pkey| to |key|, which must be of
184 // the given type. It returns one if successful or zero if the |type| argument
185 // is not one of the |EVP_PKEY_*| values or if |key| is NULL.
186 OPENSSL_EXPORT int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key);
187 
188 // EVP_PKEY_set_type sets the type of |pkey| to |type|. It returns one if
189 // successful or zero if the |type| argument is not one of the |EVP_PKEY_*|
190 // values. If |pkey| is NULL, it simply reports whether the type is known.
191 OPENSSL_EXPORT int EVP_PKEY_set_type(EVP_PKEY *pkey, int type);
192 
193 // EVP_PKEY_cmp_parameters compares the parameters of |a| and |b|. It returns
194 // one if they match, zero if not, or a negative number of on error.
195 //
196 // WARNING: the return value differs from the usual return value convention.
197 OPENSSL_EXPORT int EVP_PKEY_cmp_parameters(const EVP_PKEY *a,
198                                            const EVP_PKEY *b);
199 
200 
201 // ASN.1 functions
202 
203 // EVP_parse_public_key decodes a DER-encoded SubjectPublicKeyInfo structure
204 // (RFC 5280) from |cbs| and advances |cbs|. It returns a newly-allocated
205 // |EVP_PKEY| or NULL on error. If the key is an EC key, the curve is guaranteed
206 // to be set.
207 //
208 // The caller must check the type of the parsed public key to ensure it is
209 // suitable and validate other desired key properties such as RSA modulus size
210 // or EC curve.
211 OPENSSL_EXPORT EVP_PKEY *EVP_parse_public_key(CBS *cbs);
212 
213 // EVP_marshal_public_key marshals |key| as a DER-encoded SubjectPublicKeyInfo
214 // structure (RFC 5280) and appends the result to |cbb|. It returns one on
215 // success and zero on error.
216 OPENSSL_EXPORT int EVP_marshal_public_key(CBB *cbb, const EVP_PKEY *key);
217 
218 // EVP_parse_private_key decodes a DER-encoded PrivateKeyInfo structure (RFC
219 // 5208) from |cbs| and advances |cbs|. It returns a newly-allocated |EVP_PKEY|
220 // or NULL on error.
221 //
222 // The caller must check the type of the parsed private key to ensure it is
223 // suitable and validate other desired key properties such as RSA modulus size
224 // or EC curve. In particular, RSA private key operations scale cubicly, so
225 // applications accepting RSA private keys from external sources may need to
226 // bound key sizes (use |EVP_PKEY_bits| or |RSA_bits|) to avoid a DoS vector.
227 //
228 // A PrivateKeyInfo ends with an optional set of attributes. These are not
229 // processed and so this function will silently ignore any trailing data in the
230 // structure.
231 OPENSSL_EXPORT EVP_PKEY *EVP_parse_private_key(CBS *cbs);
232 
233 // EVP_marshal_private_key marshals |key| as a DER-encoded PrivateKeyInfo
234 // structure (RFC 5208) and appends the result to |cbb|. It returns one on
235 // success and zero on error.
236 OPENSSL_EXPORT int EVP_marshal_private_key(CBB *cbb, const EVP_PKEY *key);
237 
238 
239 // Raw keys
240 //
241 // Some keys types support a "raw" serialization. Currently the only supported
242 // raw format is Ed25519, where the public key and private key formats are those
243 // specified in RFC 8032. Note the RFC 8032 private key format is the 32-byte
244 // prefix of |ED25519_sign|'s 64-byte private key.
245 
246 // EVP_PKEY_new_raw_private_key returns a newly allocated |EVP_PKEY| wrapping a
247 // private key of the specified type. It returns one on success and zero on
248 // error.
249 OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *unused,
250                                                       const uint8_t *in,
251                                                       size_t len);
252 
253 // EVP_PKEY_new_raw_public_key returns a newly allocated |EVP_PKEY| wrapping a
254 // public key of the specified type. It returns one on success and zero on
255 // error.
256 OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *unused,
257                                                      const uint8_t *in,
258                                                      size_t len);
259 
260 // EVP_PKEY_get_raw_private_key outputs the private key for |pkey| in raw form.
261 // If |out| is NULL, it sets |*out_len| to the size of the raw private key.
262 // Otherwise, it writes at most |*out_len| bytes to |out| and sets |*out_len| to
263 // the number of bytes written.
264 //
265 // It returns one on success and zero if |pkey| has no private key, the key
266 // type does not support a raw format, or the buffer is too small.
267 OPENSSL_EXPORT int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey,
268                                                 uint8_t *out, size_t *out_len);
269 
270 // EVP_PKEY_get_raw_public_key outputs the public key for |pkey| in raw form.
271 // If |out| is NULL, it sets |*out_len| to the size of the raw public key.
272 // Otherwise, it writes at most |*out_len| bytes to |out| and sets |*out_len| to
273 // the number of bytes written.
274 //
275 // It returns one on success and zero if |pkey| has no public key, the key
276 // type does not support a raw format, or the buffer is too small.
277 OPENSSL_EXPORT int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey,
278                                                uint8_t *out, size_t *out_len);
279 
280 
281 // Signing
282 
283 // EVP_DigestSignInit sets up |ctx| for a signing operation with |type| and
284 // |pkey|. The |ctx| argument must have been initialised with
285 // |EVP_MD_CTX_init|. If |pctx| is not NULL, the |EVP_PKEY_CTX| of the signing
286 // operation will be written to |*pctx|; this can be used to set alternative
287 // signing options.
288 //
289 // For single-shot signing algorithms which do not use a pre-hash, such as
290 // Ed25519, |type| should be NULL. The |EVP_MD_CTX| itself is unused but is
291 // present so the API is uniform. See |EVP_DigestSign|.
292 //
293 // This function does not mutate |pkey| for thread-safety purposes and may be
294 // used concurrently with other non-mutating functions on |pkey|.
295 //
296 // It returns one on success, or zero on error.
297 OPENSSL_EXPORT int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
298                                       const EVP_MD *type, ENGINE *e,
299                                       EVP_PKEY *pkey);
300 
301 // EVP_DigestSignUpdate appends |len| bytes from |data| to the data which will
302 // be signed in |EVP_DigestSignFinal|. It returns one.
303 //
304 // This function performs a streaming signing operation and will fail for
305 // signature algorithms which do not support this. Use |EVP_DigestSign| for a
306 // single-shot operation.
307 OPENSSL_EXPORT int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data,
308                                         size_t len);
309 
310 // EVP_DigestSignFinal signs the data that has been included by one or more
311 // calls to |EVP_DigestSignUpdate|. If |out_sig| is NULL then |*out_sig_len| is
312 // set to the maximum number of output bytes. Otherwise, on entry,
313 // |*out_sig_len| must contain the length of the |out_sig| buffer. If the call
314 // is successful, the signature is written to |out_sig| and |*out_sig_len| is
315 // set to its length.
316 //
317 // This function performs a streaming signing operation and will fail for
318 // signature algorithms which do not support this. Use |EVP_DigestSign| for a
319 // single-shot operation.
320 //
321 // It returns one on success, or zero on error.
322 OPENSSL_EXPORT int EVP_DigestSignFinal(EVP_MD_CTX *ctx, uint8_t *out_sig,
323                                        size_t *out_sig_len);
324 
325 // EVP_DigestSign signs |data_len| bytes from |data| using |ctx|. If |out_sig|
326 // is NULL then |*out_sig_len| is set to the maximum number of output
327 // bytes. Otherwise, on entry, |*out_sig_len| must contain the length of the
328 // |out_sig| buffer. If the call is successful, the signature is written to
329 // |out_sig| and |*out_sig_len| is set to its length.
330 //
331 // It returns one on success and zero on error.
332 OPENSSL_EXPORT int EVP_DigestSign(EVP_MD_CTX *ctx, uint8_t *out_sig,
333                                   size_t *out_sig_len, const uint8_t *data,
334                                   size_t data_len);
335 
336 
337 // Verifying
338 
339 // EVP_DigestVerifyInit sets up |ctx| for a signature verification operation
340 // with |type| and |pkey|. The |ctx| argument must have been initialised with
341 // |EVP_MD_CTX_init|. If |pctx| is not NULL, the |EVP_PKEY_CTX| of the signing
342 // operation will be written to |*pctx|; this can be used to set alternative
343 // signing options.
344 //
345 // For single-shot signing algorithms which do not use a pre-hash, such as
346 // Ed25519, |type| should be NULL. The |EVP_MD_CTX| itself is unused but is
347 // present so the API is uniform. See |EVP_DigestVerify|.
348 //
349 // This function does not mutate |pkey| for thread-safety purposes and may be
350 // used concurrently with other non-mutating functions on |pkey|.
351 //
352 // It returns one on success, or zero on error.
353 OPENSSL_EXPORT int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
354                                         const EVP_MD *type, ENGINE *e,
355                                         EVP_PKEY *pkey);
356 
357 // EVP_DigestVerifyUpdate appends |len| bytes from |data| to the data which
358 // will be verified by |EVP_DigestVerifyFinal|. It returns one.
359 //
360 // This function performs streaming signature verification and will fail for
361 // signature algorithms which do not support this. Use |EVP_PKEY_verify_message|
362 // for a single-shot verification.
363 OPENSSL_EXPORT int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data,
364                                           size_t len);
365 
366 // EVP_DigestVerifyFinal verifies that |sig_len| bytes of |sig| are a valid
367 // signature for the data that has been included by one or more calls to
368 // |EVP_DigestVerifyUpdate|. It returns one on success and zero otherwise.
369 //
370 // This function performs streaming signature verification and will fail for
371 // signature algorithms which do not support this. Use |EVP_PKEY_verify_message|
372 // for a single-shot verification.
373 OPENSSL_EXPORT int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig,
374                                          size_t sig_len);
375 
376 // EVP_DigestVerify verifies that |sig_len| bytes from |sig| are a valid
377 // signature for |data|. It returns one on success or zero on error.
378 OPENSSL_EXPORT int EVP_DigestVerify(EVP_MD_CTX *ctx, const uint8_t *sig,
379                                     size_t sig_len, const uint8_t *data,
380                                     size_t len);
381 
382 
383 // Signing (old functions)
384 
385 // EVP_SignInit_ex configures |ctx|, which must already have been initialised,
386 // for a fresh signing operation using the hash function |type|. It returns one
387 // on success and zero otherwise.
388 //
389 // (In order to initialise |ctx|, either obtain it initialised with
390 // |EVP_MD_CTX_create|, or use |EVP_MD_CTX_init|.)
391 OPENSSL_EXPORT int EVP_SignInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type,
392                                    ENGINE *impl);
393 
394 // EVP_SignInit is a deprecated version of |EVP_SignInit_ex|.
395 //
396 // TODO(fork): remove.
397 OPENSSL_EXPORT int EVP_SignInit(EVP_MD_CTX *ctx, const EVP_MD *type);
398 
399 // EVP_SignUpdate appends |len| bytes from |data| to the data which will be
400 // signed in |EVP_SignFinal|.
401 OPENSSL_EXPORT int EVP_SignUpdate(EVP_MD_CTX *ctx, const void *data,
402                                   size_t len);
403 
404 // EVP_SignFinal signs the data that has been included by one or more calls to
405 // |EVP_SignUpdate|, using the key |pkey|, and writes it to |sig|. On entry,
406 // |sig| must point to at least |EVP_PKEY_size(pkey)| bytes of space. The
407 // actual size of the signature is written to |*out_sig_len|.
408 //
409 // It returns one on success and zero otherwise.
410 //
411 // It does not modify |ctx|, thus it's possible to continue to use |ctx| in
412 // order to sign a longer message. It also does not mutate |pkey| for
413 // thread-safety purposes and may be used concurrently with other non-mutating
414 // functions on |pkey|.
415 OPENSSL_EXPORT int EVP_SignFinal(const EVP_MD_CTX *ctx, uint8_t *sig,
416                                  unsigned int *out_sig_len, EVP_PKEY *pkey);
417 
418 
419 // Verifying (old functions)
420 
421 // EVP_VerifyInit_ex configures |ctx|, which must already have been
422 // initialised, for a fresh signature verification operation using the hash
423 // function |type|. It returns one on success and zero otherwise.
424 //
425 // (In order to initialise |ctx|, either obtain it initialised with
426 // |EVP_MD_CTX_create|, or use |EVP_MD_CTX_init|.)
427 OPENSSL_EXPORT int EVP_VerifyInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type,
428                                      ENGINE *impl);
429 
430 // EVP_VerifyInit is a deprecated version of |EVP_VerifyInit_ex|.
431 //
432 // TODO(fork): remove.
433 OPENSSL_EXPORT int EVP_VerifyInit(EVP_MD_CTX *ctx, const EVP_MD *type);
434 
435 // EVP_VerifyUpdate appends |len| bytes from |data| to the data which will be
436 // signed in |EVP_VerifyFinal|.
437 OPENSSL_EXPORT int EVP_VerifyUpdate(EVP_MD_CTX *ctx, const void *data,
438                                     size_t len);
439 
440 // EVP_VerifyFinal verifies that |sig_len| bytes of |sig| are a valid
441 // signature, by |pkey|, for the data that has been included by one or more
442 // calls to |EVP_VerifyUpdate|.
443 //
444 // It returns one on success and zero otherwise.
445 //
446 // It does not modify |ctx|, thus it's possible to continue to use |ctx| in
447 // order to verify a longer message. It also does not mutate |pkey| for
448 // thread-safety purposes and may be used concurrently with other non-mutating
449 // functions on |pkey|.
450 OPENSSL_EXPORT int EVP_VerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig,
451                                    size_t sig_len, EVP_PKEY *pkey);
452 
453 
454 // Printing
455 
456 // EVP_PKEY_print_public prints a textual representation of the public key in
457 // |pkey| to |out|. Returns one on success or zero otherwise.
458 OPENSSL_EXPORT int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
459                                          int indent, ASN1_PCTX *pctx);
460 
461 // EVP_PKEY_print_private prints a textual representation of the private key in
462 // |pkey| to |out|. Returns one on success or zero otherwise.
463 OPENSSL_EXPORT int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
464                                           int indent, ASN1_PCTX *pctx);
465 
466 // EVP_PKEY_print_params prints a textual representation of the parameters in
467 // |pkey| to |out|. Returns one on success or zero otherwise.
468 OPENSSL_EXPORT int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
469                                          int indent, ASN1_PCTX *pctx);
470 
471 
472 // Password stretching.
473 //
474 // Password stretching functions take a low-entropy password and apply a slow
475 // function that results in a key suitable for use in symmetric
476 // cryptography.
477 
478 // PKCS5_PBKDF2_HMAC computes |iterations| iterations of PBKDF2 of |password|
479 // and |salt|, using |digest|, and outputs |key_len| bytes to |out_key|. It
480 // returns one on success and zero on allocation failure or if iterations is 0.
481 OPENSSL_EXPORT int PKCS5_PBKDF2_HMAC(const char *password, size_t password_len,
482                                      const uint8_t *salt, size_t salt_len,
483                                      unsigned iterations, const EVP_MD *digest,
484                                      size_t key_len, uint8_t *out_key);
485 
486 // PKCS5_PBKDF2_HMAC_SHA1 is the same as PKCS5_PBKDF2_HMAC, but with |digest|
487 // fixed to |EVP_sha1|.
488 OPENSSL_EXPORT int PKCS5_PBKDF2_HMAC_SHA1(const char *password,
489                                           size_t password_len,
490                                           const uint8_t *salt, size_t salt_len,
491                                           unsigned iterations, size_t key_len,
492                                           uint8_t *out_key);
493 
494 // EVP_PBE_scrypt expands |password| into a secret key of length |key_len| using
495 // scrypt, as described in RFC 7914, and writes the result to |out_key|. It
496 // returns one on success and zero on allocation failure, if the memory required
497 // for the operation exceeds |max_mem|, or if any of the parameters are invalid
498 // as described below.
499 //
500 // |N|, |r|, and |p| are as described in RFC 7914 section 6. They determine the
501 // cost of the operation. If |max_mem| is zero, a defult limit of 32MiB will be
502 // used.
503 //
504 // The parameters are considered invalid under any of the following conditions:
505 // - |r| or |p| are zero
506 // - |p| > (2^30 - 1) / |r|
507 // - |N| is not a power of two
508 // - |N| > 2^32
509 // - |N| > 2^(128 * |r| / 8)
510 OPENSSL_EXPORT int EVP_PBE_scrypt(const char *password, size_t password_len,
511                                   const uint8_t *salt, size_t salt_len,
512                                   uint64_t N, uint64_t r, uint64_t p,
513                                   size_t max_mem, uint8_t *out_key,
514                                   size_t key_len);
515 
516 
517 // Public key contexts.
518 //
519 // |EVP_PKEY_CTX| objects hold the context of an operation (e.g. signing or
520 // encrypting) that uses a public key.
521 
522 // EVP_PKEY_CTX_new allocates a fresh |EVP_PKEY_CTX| for use with |pkey|. It
523 // returns the context or NULL on error.
524 OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e);
525 
526 // EVP_PKEY_CTX_new_id allocates a fresh |EVP_PKEY_CTX| for a key of type |id|
527 // (e.g. |EVP_PKEY_HMAC|). This can be used for key generation where
528 // |EVP_PKEY_CTX_new| can't be used because there isn't an |EVP_PKEY| to pass
529 // it. It returns the context or NULL on error.
530 OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e);
531 
532 // EVP_PKEY_CTX_free frees |ctx| and the data it owns.
533 OPENSSL_EXPORT void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx);
534 
535 // EVP_PKEY_CTX_dup allocates a fresh |EVP_PKEY_CTX| and sets it equal to the
536 // state of |ctx|. It returns the fresh |EVP_PKEY_CTX| or NULL on error.
537 OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *ctx);
538 
539 // EVP_PKEY_CTX_get0_pkey returns the |EVP_PKEY| associated with |ctx|.
540 OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx);
541 
542 // EVP_PKEY_sign_init initialises an |EVP_PKEY_CTX| for a signing operation. It
543 // should be called before |EVP_PKEY_sign|.
544 //
545 // It returns one on success or zero on error.
546 OPENSSL_EXPORT int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx);
547 
548 // EVP_PKEY_sign signs |digest_len| bytes from |digest| using |ctx|. If |sig| is
549 // NULL, the maximum size of the signature is written to |out_sig_len|.
550 // Otherwise, |*sig_len| must contain the number of bytes of space available at
551 // |sig|. If sufficient, the signature will be written to |sig| and |*sig_len|
552 // updated with the true length. This function will fail for signature
553 // algorithms like Ed25519 that do not support signing pre-hashed inputs.
554 //
555 // WARNING: |digest| must be the output of some hash function on the data to be
556 // signed. Passing unhashed inputs will not result in a secure signature scheme.
557 // Use |EVP_DigestSignInit| to sign an unhashed input.
558 //
559 // WARNING: Setting |sig| to NULL only gives the maximum size of the
560 // signature. The actual signature may be smaller.
561 //
562 // It returns one on success or zero on error. (Note: this differs from
563 // OpenSSL, which can also return negative values to indicate an error. )
564 OPENSSL_EXPORT int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, uint8_t *sig,
565                                  size_t *sig_len, const uint8_t *digest,
566                                  size_t digest_len);
567 
568 // EVP_PKEY_verify_init initialises an |EVP_PKEY_CTX| for a signature
569 // verification operation. It should be called before |EVP_PKEY_verify|.
570 //
571 // It returns one on success or zero on error.
572 OPENSSL_EXPORT int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx);
573 
574 // EVP_PKEY_verify verifies that |sig_len| bytes from |sig| are a valid
575 // signature for |digest|. This function will fail for signature
576 // algorithms like Ed25519 that do not support signing pre-hashed inputs.
577 //
578 // WARNING: |digest| must be the output of some hash function on the data to be
579 // verified. Passing unhashed inputs will not result in a secure signature
580 // scheme. Use |EVP_DigestVerifyInit| to verify a signature given the unhashed
581 // input.
582 //
583 // It returns one on success or zero on error.
584 OPENSSL_EXPORT int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig,
585                                    size_t sig_len, const uint8_t *digest,
586                                    size_t digest_len);
587 
588 // EVP_PKEY_encrypt_init initialises an |EVP_PKEY_CTX| for an encryption
589 // operation. It should be called before |EVP_PKEY_encrypt|.
590 //
591 // It returns one on success or zero on error.
592 OPENSSL_EXPORT int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx);
593 
594 // EVP_PKEY_encrypt encrypts |in_len| bytes from |in|. If |out| is NULL, the
595 // maximum size of the ciphertext is written to |out_len|. Otherwise, |*out_len|
596 // must contain the number of bytes of space available at |out|. If sufficient,
597 // the ciphertext will be written to |out| and |*out_len| updated with the true
598 // length.
599 //
600 // WARNING: Setting |out| to NULL only gives the maximum size of the
601 // ciphertext. The actual ciphertext may be smaller.
602 //
603 // It returns one on success or zero on error.
604 OPENSSL_EXPORT int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out,
605                                     size_t *out_len, const uint8_t *in,
606                                     size_t in_len);
607 
608 // EVP_PKEY_decrypt_init initialises an |EVP_PKEY_CTX| for a decryption
609 // operation. It should be called before |EVP_PKEY_decrypt|.
610 //
611 // It returns one on success or zero on error.
612 OPENSSL_EXPORT int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx);
613 
614 // EVP_PKEY_decrypt decrypts |in_len| bytes from |in|. If |out| is NULL, the
615 // maximum size of the plaintext is written to |out_len|. Otherwise, |*out_len|
616 // must contain the number of bytes of space available at |out|. If sufficient,
617 // the ciphertext will be written to |out| and |*out_len| updated with the true
618 // length.
619 //
620 // WARNING: Setting |out| to NULL only gives the maximum size of the
621 // plaintext. The actual plaintext may be smaller.
622 //
623 // It returns one on success or zero on error.
624 OPENSSL_EXPORT int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out,
625                                     size_t *out_len, const uint8_t *in,
626                                     size_t in_len);
627 
628 // EVP_PKEY_verify_recover_init initialises an |EVP_PKEY_CTX| for a public-key
629 // decryption operation. It should be called before |EVP_PKEY_verify_recover|.
630 //
631 // Public-key decryption is a very obscure operation that is only implemented
632 // by RSA keys. It is effectively a signature verification operation that
633 // returns the signed message directly. It is almost certainly not what you
634 // want.
635 //
636 // It returns one on success or zero on error.
637 OPENSSL_EXPORT int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx);
638 
639 // EVP_PKEY_verify_recover decrypts |sig_len| bytes from |sig|. If |out| is
640 // NULL, the maximum size of the plaintext is written to |out_len|. Otherwise,
641 // |*out_len| must contain the number of bytes of space available at |out|. If
642 // sufficient, the ciphertext will be written to |out| and |*out_len| updated
643 // with the true length.
644 //
645 // WARNING: Setting |out| to NULL only gives the maximum size of the
646 // plaintext. The actual plaintext may be smaller.
647 //
648 // See the warning about this operation in |EVP_PKEY_verify_recover_init|. It
649 // is probably not what you want.
650 //
651 // It returns one on success or zero on error.
652 OPENSSL_EXPORT int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out,
653                                            size_t *out_len, const uint8_t *sig,
654                                            size_t siglen);
655 
656 // EVP_PKEY_derive_init initialises an |EVP_PKEY_CTX| for a key derivation
657 // operation. It should be called before |EVP_PKEY_derive_set_peer| and
658 // |EVP_PKEY_derive|.
659 //
660 // It returns one on success or zero on error.
661 OPENSSL_EXPORT int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx);
662 
663 // EVP_PKEY_derive_set_peer sets the peer's key to be used for key derivation
664 // by |ctx| to |peer|. It should be called after |EVP_PKEY_derive_init|. (For
665 // example, this is used to set the peer's key in (EC)DH.) It returns one on
666 // success and zero on error.
667 OPENSSL_EXPORT int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer);
668 
669 // EVP_PKEY_derive derives a shared key from |ctx|. If |key| is non-NULL then,
670 // on entry, |out_key_len| must contain the amount of space at |key|. If
671 // sufficient then the shared key will be written to |key| and |*out_key_len|
672 // will be set to the length. If |key| is NULL then |out_key_len| will be set to
673 // the maximum length.
674 //
675 // WARNING: Setting |out| to NULL only gives the maximum size of the key. The
676 // actual key may be smaller.
677 //
678 // It returns one on success and zero on error.
679 OPENSSL_EXPORT int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, uint8_t *key,
680                                    size_t *out_key_len);
681 
682 // EVP_PKEY_keygen_init initialises an |EVP_PKEY_CTX| for a key generation
683 // operation. It should be called before |EVP_PKEY_keygen|.
684 //
685 // It returns one on success or zero on error.
686 OPENSSL_EXPORT int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx);
687 
688 // EVP_PKEY_keygen performs a key generation operation using the values from
689 // |ctx|. If |*out_pkey| is non-NULL, it overwrites |*out_pkey| with the
690 // resulting key. Otherwise, it sets |*out_pkey| to a newly-allocated |EVP_PKEY|
691 // containing the result. It returns one on success or zero on error.
692 OPENSSL_EXPORT int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **out_pkey);
693 
694 // EVP_PKEY_paramgen_init initialises an |EVP_PKEY_CTX| for a parameter
695 // generation operation. It should be called before |EVP_PKEY_paramgen|.
696 //
697 // It returns one on success or zero on error.
698 OPENSSL_EXPORT int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx);
699 
700 // EVP_PKEY_paramgen performs a parameter generation using the values from
701 // |ctx|. If |*out_pkey| is non-NULL, it overwrites |*out_pkey| with the
702 // resulting parameters, but no key. Otherwise, it sets |*out_pkey| to a
703 // newly-allocated |EVP_PKEY| containing the result. It returns one on success
704 // or zero on error.
705 OPENSSL_EXPORT int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **out_pkey);
706 
707 
708 // Generic control functions.
709 
710 // EVP_PKEY_CTX_set_signature_md sets |md| as the digest to be used in a
711 // signature operation. It returns one on success or zero on error.
712 OPENSSL_EXPORT int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx,
713                                                  const EVP_MD *md);
714 
715 // EVP_PKEY_CTX_get_signature_md sets |*out_md| to the digest to be used in a
716 // signature operation. It returns one on success or zero on error.
717 OPENSSL_EXPORT int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx,
718                                                  const EVP_MD **out_md);
719 
720 
721 // RSA specific control functions.
722 
723 // EVP_PKEY_CTX_set_rsa_padding sets the padding type to use. It should be one
724 // of the |RSA_*_PADDING| values. Returns one on success or zero on error. By
725 // default, the padding is |RSA_PKCS1_PADDING|.
726 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int padding);
727 
728 // EVP_PKEY_CTX_get_rsa_padding sets |*out_padding| to the current padding
729 // value, which is one of the |RSA_*_PADDING| values. Returns one on success or
730 // zero on error.
731 OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx,
732                                                 int *out_padding);
733 
734 // EVP_PKEY_CTX_set_rsa_pss_saltlen sets the length of the salt in a PSS-padded
735 // signature. A value of -1 cause the salt to be the same length as the digest
736 // in the signature. A value of -2 causes the salt to be the maximum length
737 // that will fit when signing and recovered from the signature when verifying.
738 // Otherwise the value gives the size of the salt in bytes.
739 //
740 // If unsure, use -1.
741 //
742 // Returns one on success or zero on error.
743 //
744 // TODO(davidben): The default is currently -2. Switch it to -1.
745 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx,
746                                                     int salt_len);
747 
748 // EVP_PKEY_CTX_get_rsa_pss_saltlen sets |*out_salt_len| to the salt length of
749 // a PSS-padded signature. See the documentation for
750 // |EVP_PKEY_CTX_set_rsa_pss_saltlen| for details of the special values that it
751 // can take.
752 //
753 // Returns one on success or zero on error.
754 OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx,
755                                                     int *out_salt_len);
756 
757 // EVP_PKEY_CTX_set_rsa_keygen_bits sets the size of the desired RSA modulus,
758 // in bits, for key generation. Returns one on success or zero on
759 // error.
760 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx,
761                                                     int bits);
762 
763 // EVP_PKEY_CTX_set_rsa_keygen_pubexp sets |e| as the public exponent for key
764 // generation. Returns one on success or zero on error.
765 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx,
766                                                       BIGNUM *e);
767 
768 // EVP_PKEY_CTX_set_rsa_oaep_md sets |md| as the digest used in OAEP padding.
769 // Returns one on success or zero on error. If unset, the default is SHA-1.
770 // Callers are recommended to overwrite this default.
771 //
772 // TODO(davidben): Remove the default and require callers specify this.
773 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx,
774                                                 const EVP_MD *md);
775 
776 // EVP_PKEY_CTX_get_rsa_oaep_md sets |*out_md| to the digest function used in
777 // OAEP padding. Returns one on success or zero on error.
778 OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx,
779                                                 const EVP_MD **out_md);
780 
781 // EVP_PKEY_CTX_set_rsa_mgf1_md sets |md| as the digest used in MGF1. Returns
782 // one on success or zero on error.
783 //
784 // If unset, the default is the signing hash for |RSA_PKCS1_PSS_PADDING| and the
785 // OAEP hash for |RSA_PKCS1_OAEP_PADDING|. Callers are recommended to use this
786 // default and not call this function.
787 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx,
788                                                 const EVP_MD *md);
789 
790 // EVP_PKEY_CTX_get_rsa_mgf1_md sets |*out_md| to the digest function used in
791 // MGF1. Returns one on success or zero on error.
792 OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx,
793                                                 const EVP_MD **out_md);
794 
795 // EVP_PKEY_CTX_set0_rsa_oaep_label sets |label_len| bytes from |label| as the
796 // label used in OAEP. DANGER: On success, this call takes ownership of |label|
797 // and will call |OPENSSL_free| on it when |ctx| is destroyed.
798 //
799 // Returns one on success or zero on error.
800 OPENSSL_EXPORT int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx,
801                                                     uint8_t *label,
802                                                     size_t label_len);
803 
804 // EVP_PKEY_CTX_get0_rsa_oaep_label sets |*out_label| to point to the internal
805 // buffer containing the OAEP label (which may be NULL) and returns the length
806 // of the label or a negative value on error.
807 //
808 // WARNING: the return value differs from the usual return value convention.
809 OPENSSL_EXPORT int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx,
810                                                     const uint8_t **out_label);
811 
812 
813 // EC specific control functions.
814 
815 // EVP_PKEY_CTX_set_ec_paramgen_curve_nid sets the curve used for
816 // |EVP_PKEY_keygen| or |EVP_PKEY_paramgen| operations to |nid|. It returns one
817 // on success and zero on error.
818 OPENSSL_EXPORT int EVP_PKEY_CTX_set_ec_paramgen_curve_nid(EVP_PKEY_CTX *ctx,
819                                                           int nid);
820 
821 
822 // Deprecated functions.
823 
824 // EVP_PKEY_DH is defined for compatibility, but it is impossible to create an
825 // |EVP_PKEY| of that type.
826 #define EVP_PKEY_DH NID_dhKeyAgreement
827 
828 // EVP_PKEY_RSA2 was historically an alternate form for RSA public keys (OID
829 // 2.5.8.1.1), but is no longer accepted.
830 #define EVP_PKEY_RSA2 NID_rsa
831 
832 // EVP_PKEY_X448 is defined for OpenSSL compatibility, but we do not support
833 // X448 and attempts to create keys will fail.
834 #define EVP_PKEY_X448 NID_X448
835 
836 // EVP_PKEY_ED448 is defined for OpenSSL compatibility, but we do not support
837 // Ed448 and attempts to create keys will fail.
838 #define EVP_PKEY_ED448 NID_ED448
839 
840 // EVP_PKEY_get0 returns NULL. This function is provided for compatibility with
841 // OpenSSL but does not return anything. Use the typed |EVP_PKEY_get0_*|
842 // functions instead.
843 OPENSSL_EXPORT void *EVP_PKEY_get0(const EVP_PKEY *pkey);
844 
845 // OpenSSL_add_all_algorithms does nothing.
846 OPENSSL_EXPORT void OpenSSL_add_all_algorithms(void);
847 
848 // OPENSSL_add_all_algorithms_conf does nothing.
849 OPENSSL_EXPORT void OPENSSL_add_all_algorithms_conf(void);
850 
851 // OpenSSL_add_all_ciphers does nothing.
852 OPENSSL_EXPORT void OpenSSL_add_all_ciphers(void);
853 
854 // OpenSSL_add_all_digests does nothing.
855 OPENSSL_EXPORT void OpenSSL_add_all_digests(void);
856 
857 // EVP_cleanup does nothing.
858 OPENSSL_EXPORT void EVP_cleanup(void);
859 
860 OPENSSL_EXPORT void EVP_CIPHER_do_all_sorted(
861     void (*callback)(const EVP_CIPHER *cipher, const char *name,
862                      const char *unused, void *arg),
863     void *arg);
864 
865 OPENSSL_EXPORT void EVP_MD_do_all_sorted(void (*callback)(const EVP_MD *cipher,
866                                                           const char *name,
867                                                           const char *unused,
868                                                           void *arg),
869                                          void *arg);
870 
871 OPENSSL_EXPORT void EVP_MD_do_all(void (*callback)(const EVP_MD *cipher,
872                                                    const char *name,
873                                                    const char *unused,
874                                                    void *arg),
875                                   void *arg);
876 
877 // i2d_PrivateKey marshals a private key from |key| to type-specific format, as
878 // described in |i2d_SAMPLE|.
879 //
880 // RSA keys are serialized as a DER-encoded RSAPublicKey (RFC 8017) structure.
881 // EC keys are serialized as a DER-encoded ECPrivateKey (RFC 5915) structure.
882 //
883 // Use |RSA_marshal_private_key| or |EC_KEY_marshal_private_key| instead.
884 OPENSSL_EXPORT int i2d_PrivateKey(const EVP_PKEY *key, uint8_t **outp);
885 
886 // i2d_PublicKey marshals a public key from |key| to a type-specific format, as
887 // described in |i2d_SAMPLE|.
888 //
889 // RSA keys are serialized as a DER-encoded RSAPublicKey (RFC 8017) structure.
890 // EC keys are serialized as an EC point per SEC 1.
891 //
892 // Use |RSA_marshal_public_key| or |EC_POINT_point2cbb| instead.
893 OPENSSL_EXPORT int i2d_PublicKey(const EVP_PKEY *key, uint8_t **outp);
894 
895 // d2i_PrivateKey parses a DER-encoded private key from |len| bytes at |*inp|,
896 // as described in |d2i_SAMPLE|. The private key must have type |type|,
897 // otherwise it will be rejected.
898 //
899 // This function tries to detect one of several formats. Instead, use
900 // |EVP_parse_private_key| for a PrivateKeyInfo, |RSA_parse_private_key| for an
901 // RSAPrivateKey, and |EC_parse_private_key| for an ECPrivateKey.
902 OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **out,
903                                         const uint8_t **inp, long len);
904 
905 // d2i_AutoPrivateKey acts the same as |d2i_PrivateKey|, but detects the type
906 // of the private key.
907 //
908 // This function tries to detect one of several formats. Instead, use
909 // |EVP_parse_private_key| for a PrivateKeyInfo, |RSA_parse_private_key| for an
910 // RSAPrivateKey, and |EC_parse_private_key| for an ECPrivateKey.
911 OPENSSL_EXPORT EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **out, const uint8_t **inp,
912                                             long len);
913 
914 // d2i_PublicKey parses a public key from |len| bytes at |*inp| in a type-
915 // specific format specified by |type|, as described in |d2i_SAMPLE|.
916 //
917 // The only supported value for |type| is |EVP_PKEY_RSA|, which parses a
918 // DER-encoded RSAPublicKey (RFC 8017) structure. Parsing EC keys is not
919 // supported by this function.
920 //
921 // Use |RSA_parse_public_key| instead.
922 OPENSSL_EXPORT EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **out,
923                                        const uint8_t **inp, long len);
924 
925 // EVP_PKEY_get0_DH returns NULL.
926 OPENSSL_EXPORT DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey);
927 
928 // EVP_PKEY_get1_DH returns NULL.
929 OPENSSL_EXPORT DH *EVP_PKEY_get1_DH(const EVP_PKEY *pkey);
930 
931 // EVP_PKEY_CTX_set_ec_param_enc returns one if |encoding| is
932 // |OPENSSL_EC_NAMED_CURVE| or zero with an error otherwise.
933 OPENSSL_EXPORT int EVP_PKEY_CTX_set_ec_param_enc(EVP_PKEY_CTX *ctx,
934                                                  int encoding);
935 
936 // EVP_PKEY_set1_tls_encodedpoint replaces |pkey| with a public key encoded by
937 // |in|. It returns one on success and zero on error.
938 //
939 // If |pkey| is an EC key, the format is an X9.62 point and |pkey| must already
940 // have an EC group configured. If it is an X25519 key, it is the 32-byte X25519
941 // public key representation. This function is not supported for other key types
942 // and will fail.
943 OPENSSL_EXPORT int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
944                                                   const uint8_t *in,
945                                                   size_t len);
946 
947 // EVP_PKEY_get1_tls_encodedpoint sets |*out_ptr| to a newly-allocated buffer
948 // containing the raw encoded public key for |pkey|. The caller must call
949 // |OPENSSL_free| to release this buffer. The function returns the length of the
950 // buffer on success and zero on error.
951 //
952 // If |pkey| is an EC key, the format is an X9.62 point with uncompressed
953 // coordinates. If it is an X25519 key, it is the 32-byte X25519 public key
954 // representation. This function is not supported for other key types and will
955 // fail.
956 OPENSSL_EXPORT size_t EVP_PKEY_get1_tls_encodedpoint(const EVP_PKEY *pkey,
957                                                      uint8_t **out_ptr);
958 
959 // EVP_PKEY_base_id calls |EVP_PKEY_id|.
960 OPENSSL_EXPORT int EVP_PKEY_base_id(const EVP_PKEY *pkey);
961 
962 // EVP_PKEY_CTX_set_rsa_pss_keygen_md returns 0.
963 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_pss_keygen_md(EVP_PKEY_CTX *ctx,
964                                                       const EVP_MD *md);
965 
966 // EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen returns 0.
967 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(EVP_PKEY_CTX *ctx,
968                                                            int salt_len);
969 
970 // EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md returns 0.
971 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(EVP_PKEY_CTX *ctx,
972                                                            const EVP_MD *md);
973 
974 // i2d_PUBKEY marshals |pkey| as a DER-encoded SubjectPublicKeyInfo, as
975 // described in |i2d_SAMPLE|.
976 //
977 // Use |EVP_marshal_public_key| instead.
978 OPENSSL_EXPORT int i2d_PUBKEY(const EVP_PKEY *pkey, uint8_t **outp);
979 
980 // d2i_PUBKEY parses a DER-encoded SubjectPublicKeyInfo from |len| bytes at
981 // |*inp|, as described in |d2i_SAMPLE|.
982 //
983 // Use |EVP_parse_public_key| instead.
984 OPENSSL_EXPORT EVP_PKEY *d2i_PUBKEY(EVP_PKEY **out, const uint8_t **inp,
985                                     long len);
986 
987 // i2d_RSA_PUBKEY marshals |rsa| as a DER-encoded SubjectPublicKeyInfo
988 // structure, as described in |i2d_SAMPLE|.
989 //
990 // Use |EVP_marshal_public_key| instead.
991 OPENSSL_EXPORT int i2d_RSA_PUBKEY(const RSA *rsa, uint8_t **outp);
992 
993 // d2i_RSA_PUBKEY parses an RSA public key as a DER-encoded SubjectPublicKeyInfo
994 // from |len| bytes at |*inp|, as described in |d2i_SAMPLE|.
995 // SubjectPublicKeyInfo structures containing other key types are rejected.
996 //
997 // Use |EVP_parse_public_key| instead.
998 OPENSSL_EXPORT RSA *d2i_RSA_PUBKEY(RSA **out, const uint8_t **inp, long len);
999 
1000 // i2d_DSA_PUBKEY marshals |dsa| as a DER-encoded SubjectPublicKeyInfo, as
1001 // described in |i2d_SAMPLE|.
1002 //
1003 // Use |EVP_marshal_public_key| instead.
1004 OPENSSL_EXPORT int i2d_DSA_PUBKEY(const DSA *dsa, uint8_t **outp);
1005 
1006 // d2i_DSA_PUBKEY parses a DSA public key as a DER-encoded SubjectPublicKeyInfo
1007 // from |len| bytes at |*inp|, as described in |d2i_SAMPLE|.
1008 // SubjectPublicKeyInfo structures containing other key types are rejected.
1009 //
1010 // Use |EVP_parse_public_key| instead.
1011 OPENSSL_EXPORT DSA *d2i_DSA_PUBKEY(DSA **out, const uint8_t **inp, long len);
1012 
1013 // i2d_EC_PUBKEY marshals |ec_key| as a DER-encoded SubjectPublicKeyInfo, as
1014 // described in |i2d_SAMPLE|.
1015 //
1016 // Use |EVP_marshal_public_key| instead.
1017 OPENSSL_EXPORT int i2d_EC_PUBKEY(const EC_KEY *ec_key, uint8_t **outp);
1018 
1019 // d2i_EC_PUBKEY parses an EC public key as a DER-encoded SubjectPublicKeyInfo
1020 // from |len| bytes at |*inp|, as described in |d2i_SAMPLE|.
1021 // SubjectPublicKeyInfo structures containing other key types are rejected.
1022 //
1023 // Use |EVP_parse_public_key| instead.
1024 OPENSSL_EXPORT EC_KEY *d2i_EC_PUBKEY(EC_KEY **out, const uint8_t **inp,
1025                                      long len);
1026 
1027 // EVP_PKEY_CTX_set_dsa_paramgen_bits returns zero.
1028 OPENSSL_EXPORT int EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx,
1029                                                       int nbits);
1030 
1031 // EVP_PKEY_CTX_set_dsa_paramgen_q_bits returns zero.
1032 OPENSSL_EXPORT int EVP_PKEY_CTX_set_dsa_paramgen_q_bits(EVP_PKEY_CTX *ctx,
1033                                                         int qbits);
1034 
1035 
1036 // Preprocessor compatibility section (hidden).
1037 //
1038 // Historically, a number of APIs were implemented in OpenSSL as macros and
1039 // constants to 'ctrl' functions. To avoid breaking #ifdefs in consumers, this
1040 // section defines a number of legacy macros.
1041 
1042 // |BORINGSSL_PREFIX| already makes each of these symbols into macros, so there
1043 // is no need to define conflicting macros.
1044 #if !defined(BORINGSSL_PREFIX)
1045 #define EVP_PKEY_CTX_set_rsa_oaep_md EVP_PKEY_CTX_set_rsa_oaep_md
1046 #define EVP_PKEY_CTX_set0_rsa_oaep_label EVP_PKEY_CTX_set0_rsa_oaep_label
1047 #endif
1048 
1049 
1050 // Nodejs compatibility section (hidden).
1051 //
1052 // These defines exist for node.js, with the hope that we can eliminate the
1053 // need for them over time.
1054 
1055 #define EVPerr(function, reason) \
1056   ERR_put_error(ERR_LIB_EVP, 0, reason, __FILE__, __LINE__)
1057 
1058 
1059 #if defined(__cplusplus)
1060 }  // extern C
1061 
1062 extern "C++" {
1063 BSSL_NAMESPACE_BEGIN
1064 
1065 BORINGSSL_MAKE_DELETER(EVP_PKEY, EVP_PKEY_free)
1066 BORINGSSL_MAKE_UP_REF(EVP_PKEY, EVP_PKEY_up_ref)
1067 BORINGSSL_MAKE_DELETER(EVP_PKEY_CTX, EVP_PKEY_CTX_free)
1068 
1069 BSSL_NAMESPACE_END
1070 
1071 }  // extern C++
1072 
1073 #endif
1074 
1075 #endif  // OPENSSL_HEADER_EVP_H
1076