blob: cb2731e523490b1840db1ccac1522aa0ad829fc5 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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>
Adam Langleyd9e397b2015-01-22 14:27:53 -080061
Adam Langleyf4e42722015-06-04 17:45:09 -070062#include <openssl/thread.h>
63
Adam Langleyd9e397b2015-01-22 14:27:53 -080064/* OpenSSL included digest and cipher functions in this header so we include
65 * them for users that still expect that.
66 *
67 * TODO(fork): clean up callers so that they include what they use. */
68#include <openssl/aead.h>
Kenny Rootb8494592015-09-25 02:29:14 +000069#include <openssl/base64.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080070#include <openssl/cipher.h>
71#include <openssl/digest.h>
David Benjamin4969cc92016-04-22 15:02:23 -040072#include <openssl/nid.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080073
74#if defined(__cplusplus)
75extern "C" {
76#endif
77
78
79/* EVP abstracts over public/private key algorithms. */
80
81
82/* Public key objects. */
83
84/* EVP_PKEY_new creates a new, empty public-key object and returns it or NULL
85 * on allocation failure. */
86OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_new(void);
87
88/* EVP_PKEY_free frees all data referenced by |pkey| and then frees |pkey|
89 * itself. */
90OPENSSL_EXPORT void EVP_PKEY_free(EVP_PKEY *pkey);
91
David Benjaminc895d6b2016-08-11 13:26:41 -040092/* EVP_PKEY_up_ref increments the reference count of |pkey| and returns one. */
93OPENSSL_EXPORT int EVP_PKEY_up_ref(EVP_PKEY *pkey);
Adam Langleye9ada862015-05-11 17:20:37 -070094
Adam Langleyd9e397b2015-01-22 14:27:53 -080095/* EVP_PKEY_is_opaque returns one if |pkey| is opaque. Opaque keys are backed by
96 * custom implementations which do not expose key material and parameters. It is
97 * an error to attempt to duplicate, export, or compare an opaque key. */
98OPENSSL_EXPORT int EVP_PKEY_is_opaque(const EVP_PKEY *pkey);
99
Adam Langleyd9e397b2015-01-22 14:27:53 -0800100/* EVP_PKEY_cmp compares |a| and |b| and returns one if they are equal, zero if
101 * not and a negative number on error.
102 *
103 * WARNING: this differs from the traditional return value of a "cmp"
104 * function. */
105OPENSSL_EXPORT int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b);
106
Adam Langleyd9e397b2015-01-22 14:27:53 -0800107/* EVP_PKEY_copy_parameters sets the parameters of |to| to equal the parameters
108 * of |from|. It returns one on success and zero on error. */
109OPENSSL_EXPORT int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from);
110
111/* EVP_PKEY_missing_parameters returns one if |pkey| is missing needed
112 * parameters or zero if not, or if the algorithm doesn't take parameters. */
113OPENSSL_EXPORT int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey);
114
Adam Langleye9ada862015-05-11 17:20:37 -0700115/* EVP_PKEY_size returns the maximum size, in bytes, of a signature signed by
116 * |pkey|. For an RSA key, this returns the number of bytes needed to represent
117 * the modulus. For an EC key, this returns the maximum size of a DER-encoded
118 * ECDSA signature. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800119OPENSSL_EXPORT int EVP_PKEY_size(const EVP_PKEY *pkey);
120
Adam Langleye9ada862015-05-11 17:20:37 -0700121/* EVP_PKEY_bits returns the "size", in bits, of |pkey|. For an RSA key, this
122 * returns the bit length of the modulus. For an EC key, this returns the bit
123 * length of the group order. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800124OPENSSL_EXPORT int EVP_PKEY_bits(EVP_PKEY *pkey);
125
126/* EVP_PKEY_id returns the type of |pkey|, which is one of the |EVP_PKEY_*|
127 * values. */
128OPENSSL_EXPORT int EVP_PKEY_id(const EVP_PKEY *pkey);
129
David Benjamin4969cc92016-04-22 15:02:23 -0400130/* EVP_PKEY_type returns |nid| if |nid| is a known key type and |NID_undef|
131 * otherwise. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800132OPENSSL_EXPORT int EVP_PKEY_type(int nid);
133
Adam Langleyd9e397b2015-01-22 14:27:53 -0800134
135/* Getting and setting concrete public key types.
136 *
137 * The following functions get and set the underlying public key in an
138 * |EVP_PKEY| object. The |set1| functions take an additional reference to the
139 * underlying key and return one on success or zero on error. The |assign|
Adam Langley4139edb2016-01-13 15:00:54 -0800140 * functions adopt the caller's reference. The |get1| functions return a fresh
141 * reference to the underlying object or NULL if |pkey| is not of the correct
142 * type. The |get0| functions behave the same but return a non-owning
143 * pointer. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800144
145OPENSSL_EXPORT int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key);
146OPENSSL_EXPORT int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key);
Adam Langley4139edb2016-01-13 15:00:54 -0800147OPENSSL_EXPORT RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800148OPENSSL_EXPORT RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey);
149
Adam Langleyfad63272015-11-12 12:15:39 -0800150OPENSSL_EXPORT int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800151OPENSSL_EXPORT int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key);
Adam Langley4139edb2016-01-13 15:00:54 -0800152OPENSSL_EXPORT DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey);
Adam Langleyfad63272015-11-12 12:15:39 -0800153OPENSSL_EXPORT DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800154
Adam Langleyfad63272015-11-12 12:15:39 -0800155OPENSSL_EXPORT int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800156OPENSSL_EXPORT int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key);
Adam Langley4139edb2016-01-13 15:00:54 -0800157OPENSSL_EXPORT EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey);
Adam Langleyfad63272015-11-12 12:15:39 -0800158OPENSSL_EXPORT EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800159
Robert Sloan572a4e22017-04-17 10:52:19 -0700160/* EVP_PKEY_new_ed25519_public returns a newly allocated |EVP_PKEY| wrapping an
161 * Ed25519 public key, or NULL on allocation error. */
162OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_new_ed25519_public(
163 const uint8_t public_key[32]);
164
165/* EVP_PKEY_new_ed25519_private returns a newly allocated |EVP_PKEY| wrapping an
166 * Ed25519 private key, or NULL on allocation error. */
167OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_new_ed25519_private(
168 const uint8_t private_key[64]);
169
Adam Langleyd9e397b2015-01-22 14:27:53 -0800170#define EVP_PKEY_NONE NID_undef
171#define EVP_PKEY_RSA NID_rsaEncryption
Adam Langleyd9e397b2015-01-22 14:27:53 -0800172#define EVP_PKEY_DSA NID_dsa
Adam Langleyd9e397b2015-01-22 14:27:53 -0800173#define EVP_PKEY_EC NID_X9_62_id_ecPublicKey
Robert Sloan572a4e22017-04-17 10:52:19 -0700174#define EVP_PKEY_ED25519 NID_Ed25519
Adam Langleyd9e397b2015-01-22 14:27:53 -0800175
Adam Langleyd9e397b2015-01-22 14:27:53 -0800176/* EVP_PKEY_assign sets the underlying key of |pkey| to |key|, which must be of
177 * the given type. The |type| argument should be one of the |EVP_PKEY_*|
178 * values. */
179OPENSSL_EXPORT int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key);
180
181/* EVP_PKEY_set_type sets the type of |pkey| to |type|, which should be one of
Steven Valdezb0b45c62017-01-17 16:23:54 -0500182 * the |EVP_PKEY_*| values. It returns one if successful or zero otherwise. If
Adam Langleyd9e397b2015-01-22 14:27:53 -0800183 * |pkey| is NULL, it simply reports whether the type is known. */
184OPENSSL_EXPORT int EVP_PKEY_set_type(EVP_PKEY *pkey, int type);
185
186/* EVP_PKEY_cmp_parameters compares the parameters of |a| and |b|. It returns
187 * one if they match, zero if not, or a negative number of on error.
188 *
189 * WARNING: the return value differs from the usual return value convention. */
190OPENSSL_EXPORT int EVP_PKEY_cmp_parameters(const EVP_PKEY *a,
191 const EVP_PKEY *b);
192
193
194/* ASN.1 functions */
195
David Benjamin4969cc92016-04-22 15:02:23 -0400196/* EVP_parse_public_key decodes a DER-encoded SubjectPublicKeyInfo structure
197 * (RFC 5280) from |cbs| and advances |cbs|. It returns a newly-allocated
198 * |EVP_PKEY| or NULL on error.
Kenny Rootb8494592015-09-25 02:29:14 +0000199 *
David Benjamin4969cc92016-04-22 15:02:23 -0400200 * The caller must check the type of the parsed public key to ensure it is
201 * suitable and validate other desired key properties such as RSA modulus size
202 * or EC curve. */
203OPENSSL_EXPORT EVP_PKEY *EVP_parse_public_key(CBS *cbs);
204
205/* EVP_marshal_public_key marshals |key| as a DER-encoded SubjectPublicKeyInfo
206 * structure (RFC 5280) and appends the result to |cbb|. It returns one on
207 * success and zero on error. */
208OPENSSL_EXPORT int EVP_marshal_public_key(CBB *cbb, const EVP_PKEY *key);
209
210/* EVP_parse_private_key decodes a DER-encoded PrivateKeyInfo structure (RFC
211 * 5208) from |cbs| and advances |cbs|. It returns a newly-allocated |EVP_PKEY|
212 * or NULL on error.
213 *
214 * The caller must check the type of the parsed private key to ensure it is
215 * suitable and validate other desired key properties such as RSA modulus size
216 * or EC curve.
217 *
218 * A PrivateKeyInfo ends with an optional set of attributes. These are not
219 * processed and so this function will silently ignore any trailing data in the
220 * structure. */
221OPENSSL_EXPORT EVP_PKEY *EVP_parse_private_key(CBS *cbs);
222
223/* EVP_marshal_private_key marshals |key| as a DER-encoded PrivateKeyInfo
224 * structure (RFC 5208) and appends the result to |cbb|. It returns one on
225 * success and zero on error. */
226OPENSSL_EXPORT int EVP_marshal_private_key(CBB *cbb, const EVP_PKEY *key);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800227
228
229/* Signing */
230
231/* EVP_DigestSignInit sets up |ctx| for a signing operation with |type| and
232 * |pkey|. The |ctx| argument must have been initialised with
233 * |EVP_MD_CTX_init|. If |pctx| is not NULL, the |EVP_PKEY_CTX| of the signing
234 * operation will be written to |*pctx|; this can be used to set alternative
235 * signing options.
236 *
Robert Sloan572a4e22017-04-17 10:52:19 -0700237 * This function performs a streaming signing operation and will fail for
238 * signature algorithms which do not support this. Use |EVP_PKEY_sign_message|
239 * for a single-shot operation.
240 *
Adam Langleyd9e397b2015-01-22 14:27:53 -0800241 * It returns one on success, or zero on error. */
242OPENSSL_EXPORT int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
243 const EVP_MD *type, ENGINE *e,
244 EVP_PKEY *pkey);
245
246/* EVP_DigestSignUpdate appends |len| bytes from |data| to the data which will
Adam Langleyf4e42722015-06-04 17:45:09 -0700247 * be signed in |EVP_DigestSignFinal|. It returns one. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800248OPENSSL_EXPORT int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data,
249 size_t len);
250
251/* EVP_DigestSignFinal signs the data that has been included by one or more
252 * calls to |EVP_DigestSignUpdate|. If |out_sig| is NULL then |*out_sig_len| is
253 * set to the maximum number of output bytes. Otherwise, on entry,
254 * |*out_sig_len| must contain the length of the |out_sig| buffer. If the call
255 * is successful, the signature is written to |out_sig| and |*out_sig_len| is
256 * set to its length.
257 *
258 * It returns one on success, or zero on error. */
259OPENSSL_EXPORT int EVP_DigestSignFinal(EVP_MD_CTX *ctx, uint8_t *out_sig,
260 size_t *out_sig_len);
261
Adam Langleyd9e397b2015-01-22 14:27:53 -0800262
263/* Verifying */
264
265/* EVP_DigestVerifyInit sets up |ctx| for a signature verification operation
266 * with |type| and |pkey|. The |ctx| argument must have been initialised with
267 * |EVP_MD_CTX_init|. If |pctx| is not NULL, the |EVP_PKEY_CTX| of the signing
268 * operation will be written to |*pctx|; this can be used to set alternative
269 * signing options.
270 *
Robert Sloan572a4e22017-04-17 10:52:19 -0700271 * This function performs streaming signature verification and will fail for
272 * signature algorithms which do not support this. Use |EVP_PKEY_verify_message|
273 * for a single-shot verification.
274 *
Adam Langleyd9e397b2015-01-22 14:27:53 -0800275 * It returns one on success, or zero on error. */
276OPENSSL_EXPORT int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
277 const EVP_MD *type, ENGINE *e,
278 EVP_PKEY *pkey);
279
Adam Langleyd9e397b2015-01-22 14:27:53 -0800280/* EVP_DigestVerifyUpdate appends |len| bytes from |data| to the data which
Adam Langleyf4e42722015-06-04 17:45:09 -0700281 * will be verified by |EVP_DigestVerifyFinal|. It returns one. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800282OPENSSL_EXPORT int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data,
283 size_t len);
284
285/* EVP_DigestVerifyFinal verifies that |sig_len| bytes of |sig| are a valid
286 * signature for the data that has been included by one or more calls to
287 * |EVP_DigestVerifyUpdate|. It returns one on success and zero otherwise. */
288OPENSSL_EXPORT int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig,
289 size_t sig_len);
290
291
292/* Signing (old functions) */
293
294/* EVP_SignInit_ex configures |ctx|, which must already have been initialised,
295 * for a fresh signing operation using the hash function |type|. It returns one
296 * on success and zero otherwise.
297 *
298 * (In order to initialise |ctx|, either obtain it initialised with
299 * |EVP_MD_CTX_create|, or use |EVP_MD_CTX_init|.) */
300OPENSSL_EXPORT int EVP_SignInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type,
301 ENGINE *impl);
302
303/* EVP_SignInit is a deprecated version of |EVP_SignInit_ex|.
304 *
305 * TODO(fork): remove. */
306OPENSSL_EXPORT int EVP_SignInit(EVP_MD_CTX *ctx, const EVP_MD *type);
307
308/* EVP_SignUpdate appends |len| bytes from |data| to the data which will be
309 * signed in |EVP_SignFinal|. */
310OPENSSL_EXPORT int EVP_SignUpdate(EVP_MD_CTX *ctx, const void *data,
311 size_t len);
312
313/* EVP_SignFinal signs the data that has been included by one or more calls to
314 * |EVP_SignUpdate|, using the key |pkey|, and writes it to |sig|. On entry,
315 * |sig| must point to at least |EVP_PKEY_size(pkey)| bytes of space. The
316 * actual size of the signature is written to |*out_sig_len|.
317 *
318 * It returns one on success and zero otherwise.
319 *
320 * It does not modify |ctx|, thus it's possible to continue to use |ctx| in
321 * order to sign a longer message. */
322OPENSSL_EXPORT int EVP_SignFinal(const EVP_MD_CTX *ctx, uint8_t *sig,
323 unsigned int *out_sig_len, EVP_PKEY *pkey);
324
325
326/* Verifying (old functions) */
327
328/* EVP_VerifyInit_ex configures |ctx|, which must already have been
329 * initialised, for a fresh signature verification operation using the hash
330 * function |type|. It returns one on success and zero otherwise.
331 *
332 * (In order to initialise |ctx|, either obtain it initialised with
333 * |EVP_MD_CTX_create|, or use |EVP_MD_CTX_init|.) */
334OPENSSL_EXPORT int EVP_VerifyInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type,
335 ENGINE *impl);
336
337/* EVP_VerifyInit is a deprecated version of |EVP_VerifyInit_ex|.
338 *
339 * TODO(fork): remove. */
340OPENSSL_EXPORT int EVP_VerifyInit(EVP_MD_CTX *ctx, const EVP_MD *type);
341
342/* EVP_VerifyUpdate appends |len| bytes from |data| to the data which will be
343 * signed in |EVP_VerifyFinal|. */
344OPENSSL_EXPORT int EVP_VerifyUpdate(EVP_MD_CTX *ctx, const void *data,
345 size_t len);
346
347/* EVP_VerifyFinal verifies that |sig_len| bytes of |sig| are a valid
348 * signature, by |pkey|, for the data that has been included by one or more
349 * calls to |EVP_VerifyUpdate|.
350 *
351 * It returns one on success and zero otherwise.
352 *
353 * It does not modify |ctx|, thus it's possible to continue to use |ctx| in
354 * order to sign a longer message. */
355OPENSSL_EXPORT int EVP_VerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig,
356 size_t sig_len, EVP_PKEY *pkey);
357
358
359/* Printing */
360
361/* EVP_PKEY_print_public prints a textual representation of the public key in
362 * |pkey| to |out|. Returns one on success or zero otherwise. */
363OPENSSL_EXPORT int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
364 int indent, ASN1_PCTX *pctx);
365
Kenny Rootb8494592015-09-25 02:29:14 +0000366/* EVP_PKEY_print_private prints a textual representation of the private key in
Adam Langleyd9e397b2015-01-22 14:27:53 -0800367 * |pkey| to |out|. Returns one on success or zero otherwise. */
368OPENSSL_EXPORT int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
369 int indent, ASN1_PCTX *pctx);
370
Kenny Rootb8494592015-09-25 02:29:14 +0000371/* EVP_PKEY_print_params prints a textual representation of the parameters in
Adam Langleyd9e397b2015-01-22 14:27:53 -0800372 * |pkey| to |out|. Returns one on success or zero otherwise. */
373OPENSSL_EXPORT int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
374 int indent, ASN1_PCTX *pctx);
375
376
377/* Password stretching.
378 *
379 * Password stretching functions take a low-entropy password and apply a slow
380 * function that results in a key suitable for use in symmetric
381 * cryptography. */
382
383/* PKCS5_PBKDF2_HMAC computes |iterations| iterations of PBKDF2 of |password|
384 * and |salt|, using |digest|, and outputs |key_len| bytes to |out_key|. It
385 * returns one on success and zero on error. */
386OPENSSL_EXPORT int PKCS5_PBKDF2_HMAC(const char *password, size_t password_len,
387 const uint8_t *salt, size_t salt_len,
388 unsigned iterations, const EVP_MD *digest,
389 size_t key_len, uint8_t *out_key);
390
391/* PKCS5_PBKDF2_HMAC_SHA1 is the same as PKCS5_PBKDF2_HMAC, but with |digest|
392 * fixed to |EVP_sha1|. */
393OPENSSL_EXPORT int PKCS5_PBKDF2_HMAC_SHA1(const char *password,
Steven Valdezb0b45c62017-01-17 16:23:54 -0500394 size_t password_len,
395 const uint8_t *salt, size_t salt_len,
396 unsigned iterations, size_t key_len,
397 uint8_t *out_key);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800398
399
400/* Public key contexts.
401 *
402 * |EVP_PKEY_CTX| objects hold the context of an operation (e.g. signing or
403 * encrypting) that uses a public key. */
404
405/* EVP_PKEY_CTX_new allocates a fresh |EVP_PKEY_CTX| for use with |pkey|. It
406 * returns the context or NULL on error. */
407OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e);
408
Kenny Rootb8494592015-09-25 02:29:14 +0000409/* EVP_PKEY_CTX_new_id allocates a fresh |EVP_PKEY_CTX| for a key of type |id|
Adam Langleyd9e397b2015-01-22 14:27:53 -0800410 * (e.g. |EVP_PKEY_HMAC|). This can be used for key generation where
411 * |EVP_PKEY_CTX_new| can't be used because there isn't an |EVP_PKEY| to pass
412 * it. It returns the context or NULL on error. */
413OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e);
414
Kenny Rootb8494592015-09-25 02:29:14 +0000415/* EVP_PKEY_CTX_free frees |ctx| and the data it owns. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800416OPENSSL_EXPORT void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx);
417
418/* EVP_PKEY_CTX_dup allocates a fresh |EVP_PKEY_CTX| and sets it equal to the
419 * state of |ctx|. It returns the fresh |EVP_PKEY_CTX| or NULL on error. */
420OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *ctx);
421
422/* EVP_PKEY_CTX_get0_pkey returns the |EVP_PKEY| associated with |ctx|. */
423OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx);
424
Adam Langleyd9e397b2015-01-22 14:27:53 -0800425/* EVP_PKEY_sign_init initialises an |EVP_PKEY_CTX| for a signing operation. It
426 * should be called before |EVP_PKEY_sign|.
427 *
428 * It returns one on success or zero on error. */
429OPENSSL_EXPORT int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx);
430
Robert Sloan572a4e22017-04-17 10:52:19 -0700431/* EVP_PKEY_sign signs |digest_len| bytes from |digest| using |ctx|. If |sig| is
Adam Langleyd9e397b2015-01-22 14:27:53 -0800432 * NULL, the maximum size of the signature is written to
433 * |out_sig_len|. Otherwise, |*sig_len| must contain the number of bytes of
434 * space available at |sig|. If sufficient, the signature will be written to
435 * |sig| and |*sig_len| updated with the true length.
436 *
Robert Sloan572a4e22017-04-17 10:52:19 -0700437 * This function expects a pre-hashed input and will fail for signature
438 * algorithms which do not support this. Use |EVP_PKEY_sign_message| or
439 * |EVP_DigestSignInit| to sign an unhashed input.
440 *
Adam Langleyd9e397b2015-01-22 14:27:53 -0800441 * WARNING: Setting |sig| to NULL only gives the maximum size of the
442 * signature. The actual signature may be smaller.
443 *
444 * It returns one on success or zero on error. (Note: this differs from
445 * OpenSSL, which can also return negative values to indicate an error. ) */
446OPENSSL_EXPORT int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, uint8_t *sig,
Robert Sloan572a4e22017-04-17 10:52:19 -0700447 size_t *sig_len, const uint8_t *digest,
448 size_t digest_len);
449
450/* EVP_PKEY_sign_message signs |data_len| bytes from |data| using |ctx|. If
451 * |sig| is NULL, the maximum size of the signature is written to |out_sig_len|.
452 * Otherwise, |*sig_len| must contain the number of bytes of space available at
453 * |sig|. If sufficient, the signature will be written to |sig| and |*sig_len|
454 * updated with the true length.
455 *
456 * WARNING: Setting |sig| to NULL only gives the maximum size of the
457 * signature. The actual signature may be smaller.
458 *
459 * It returns one on success or zero on error. (Note: this differs from
460 * OpenSSL, which can also return negative values to indicate an error. ) */
461OPENSSL_EXPORT int EVP_PKEY_sign_message(EVP_PKEY_CTX *ctx, uint8_t *sig,
462 size_t *sig_len, const uint8_t *data,
463 size_t data_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800464
465/* EVP_PKEY_verify_init initialises an |EVP_PKEY_CTX| for a signature
466 * verification operation. It should be called before |EVP_PKEY_verify|.
467 *
468 * It returns one on success or zero on error. */
469OPENSSL_EXPORT int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx);
470
Steven Valdezb0b45c62017-01-17 16:23:54 -0500471/* EVP_PKEY_verify verifies that |sig_len| bytes from |sig| are a valid
Robert Sloan572a4e22017-04-17 10:52:19 -0700472 * signature for |digest|.
473 *
474 * This function expects a pre-hashed input and will fail for signature
475 * algorithms which do not support this. Use |EVP_PKEY_verify_message| or
476 * |EVP_DigestVerifyInit| to verify a signature given the unhashed input.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800477 *
478 * It returns one on success or zero on error. */
479OPENSSL_EXPORT int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig,
Robert Sloan572a4e22017-04-17 10:52:19 -0700480 size_t sig_len, const uint8_t *digest,
481 size_t digest_len);
482
483/* EVP_PKEY_verify_message verifies that |sig_len| bytes from |sig| are a valid
484 * signature for |data|. It returns one on success or zero on error. */
485OPENSSL_EXPORT int EVP_PKEY_verify_message(EVP_PKEY_CTX *ctx,
486 const uint8_t *sig, size_t sig_len,
487 const uint8_t *data,
488 size_t data_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800489
490/* EVP_PKEY_encrypt_init initialises an |EVP_PKEY_CTX| for an encryption
491 * operation. It should be called before |EVP_PKEY_encrypt|.
492 *
493 * It returns one on success or zero on error. */
494OPENSSL_EXPORT int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx);
495
496/* EVP_PKEY_encrypt encrypts |in_len| bytes from |in|. If |out| is NULL, the
497 * maximum size of the ciphertext is written to |out_len|. Otherwise, |*out_len|
498 * must contain the number of bytes of space available at |out|. If sufficient,
499 * the ciphertext will be written to |out| and |*out_len| updated with the true
500 * length.
501 *
502 * WARNING: Setting |out| to NULL only gives the maximum size of the
503 * ciphertext. The actual ciphertext may be smaller.
504 *
505 * It returns one on success or zero on error. */
506OPENSSL_EXPORT int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out,
507 size_t *out_len, const uint8_t *in,
508 size_t in_len);
509
510/* EVP_PKEY_decrypt_init initialises an |EVP_PKEY_CTX| for a decryption
511 * operation. It should be called before |EVP_PKEY_decrypt|.
512 *
513 * It returns one on success or zero on error. */
514OPENSSL_EXPORT int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx);
515
516/* EVP_PKEY_decrypt decrypts |in_len| bytes from |in|. If |out| is NULL, the
517 * maximum size of the plaintext is written to |out_len|. Otherwise, |*out_len|
518 * must contain the number of bytes of space available at |out|. If sufficient,
519 * the ciphertext will be written to |out| and |*out_len| updated with the true
520 * length.
521 *
522 * WARNING: Setting |out| to NULL only gives the maximum size of the
523 * plaintext. The actual plaintext may be smaller.
524 *
525 * It returns one on success or zero on error. */
526OPENSSL_EXPORT int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out,
527 size_t *out_len, const uint8_t *in,
528 size_t in_len);
529
David Benjamin4969cc92016-04-22 15:02:23 -0400530/* EVP_PKEY_verify_recover_init initialises an |EVP_PKEY_CTX| for a public-key
531 * decryption operation. It should be called before |EVP_PKEY_verify_recover|.
532 *
533 * Public-key decryption is a very obscure operation that is only implemented
534 * by RSA keys. It is effectively a signature verification operation that
535 * returns the signed message directly. It is almost certainly not what you
536 * want.
537 *
538 * It returns one on success or zero on error. */
539OPENSSL_EXPORT int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx);
540
541/* EVP_PKEY_verify_recover decrypts |sig_len| bytes from |sig|. If |out| is
542 * NULL, the maximum size of the plaintext is written to |out_len|. Otherwise,
543 * |*out_len| must contain the number of bytes of space available at |out|. If
544 * sufficient, the ciphertext will be written to |out| and |*out_len| updated
545 * with the true length.
546 *
547 * WARNING: Setting |out| to NULL only gives the maximum size of the
548 * plaintext. The actual plaintext may be smaller.
549 *
550 * See the warning about this operation in |EVP_PKEY_verify_recover_init|. It
551 * is probably not what you want.
552 *
553 * It returns one on success or zero on error. */
554OPENSSL_EXPORT int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out,
555 size_t *out_len, const uint8_t *sig,
556 size_t siglen);
557
Adam Langleyd9e397b2015-01-22 14:27:53 -0800558/* EVP_PKEY_derive_init initialises an |EVP_PKEY_CTX| for a key derivation
559 * operation. It should be called before |EVP_PKEY_derive_set_peer| and
560 * |EVP_PKEY_derive|.
561 *
562 * It returns one on success or zero on error. */
563OPENSSL_EXPORT int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx);
564
565/* EVP_PKEY_derive_set_peer sets the peer's key to be used for key derivation
566 * by |ctx| to |peer|. It should be called after |EVP_PKEY_derive_init|. (For
567 * example, this is used to set the peer's key in (EC)DH.) It returns one on
568 * success and zero on error. */
569OPENSSL_EXPORT int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer);
570
571/* EVP_PKEY_derive derives a shared key between the two keys configured in
572 * |ctx|. If |key| is non-NULL then, on entry, |out_key_len| must contain the
573 * amount of space at |key|. If sufficient then the shared key will be written
574 * to |key| and |*out_key_len| will be set to the length. If |key| is NULL then
575 * |out_key_len| will be set to the maximum length.
576 *
577 * WARNING: Setting |out| to NULL only gives the maximum size of the key. The
578 * actual key may be smaller.
579 *
580 * It returns one on success and zero on error. */
581OPENSSL_EXPORT int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, uint8_t *key,
582 size_t *out_key_len);
583
584/* EVP_PKEY_keygen_init initialises an |EVP_PKEY_CTX| for a key generation
585 * operation. It should be called before |EVP_PKEY_keygen|.
586 *
587 * It returns one on success or zero on error. */
588OPENSSL_EXPORT int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx);
589
590/* EVP_PKEY_keygen performs a key generation operation using the values from
591 * |ctx| and sets |*ppkey| to a fresh |EVP_PKEY| containing the resulting key.
592 * It returns one on success or zero on error. */
593OPENSSL_EXPORT int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
594
595
Adam Langleye9ada862015-05-11 17:20:37 -0700596/* Generic control functions. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800597
598/* EVP_PKEY_CTX_set_signature_md sets |md| as the digest to be used in a
Adam Langleye9ada862015-05-11 17:20:37 -0700599 * signature operation. It returns one on success or zero on error. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800600OPENSSL_EXPORT int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx,
601 const EVP_MD *md);
602
603/* EVP_PKEY_CTX_get_signature_md sets |*out_md| to the digest to be used in a
Adam Langleye9ada862015-05-11 17:20:37 -0700604 * signature operation. It returns one on success or zero on error. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800605OPENSSL_EXPORT int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx,
606 const EVP_MD **out_md);
607
Adam Langleyd9e397b2015-01-22 14:27:53 -0800608
609/* RSA specific control functions. */
610
611/* EVP_PKEY_CTX_set_rsa_padding sets the padding type to use. It should be one
Adam Langleye9ada862015-05-11 17:20:37 -0700612 * of the |RSA_*_PADDING| values. Returns one on success or zero on error. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800613OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int padding);
614
615/* EVP_PKEY_CTX_get_rsa_padding sets |*out_padding| to the current padding
616 * value, which is one of the |RSA_*_PADDING| values. Returns one on success or
Adam Langleye9ada862015-05-11 17:20:37 -0700617 * zero on error. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800618OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx,
619 int *out_padding);
620
621/* EVP_PKEY_CTX_set_rsa_pss_saltlen sets the length of the salt in a PSS-padded
622 * signature. A value of -1 cause the salt to be the same length as the digest
623 * in the signature. A value of -2 causes the salt to be the maximum length
Robert Sloan69939df2017-01-09 10:53:07 -0800624 * that will fit when signing and recovered from the signature when verifying.
625 * Otherwise the value gives the size of the salt in bytes.
626 *
627 * If unsure, use -1.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800628 *
Adam Langleye9ada862015-05-11 17:20:37 -0700629 * Returns one on success or zero on error. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800630OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx,
631 int salt_len);
632
633/* EVP_PKEY_CTX_get_rsa_pss_saltlen sets |*out_salt_len| to the salt length of
634 * a PSS-padded signature. See the documentation for
635 * |EVP_PKEY_CTX_set_rsa_pss_saltlen| for details of the special values that it
636 * can take.
637 *
Adam Langleye9ada862015-05-11 17:20:37 -0700638 * Returns one on success or zero on error. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800639OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx,
640 int *out_salt_len);
641
642/* EVP_PKEY_CTX_set_rsa_keygen_bits sets the size of the desired RSA modulus,
Adam Langleye9ada862015-05-11 17:20:37 -0700643 * in bits, for key generation. Returns one on success or zero on
644 * error. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800645OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx,
646 int bits);
647
648/* EVP_PKEY_CTX_set_rsa_keygen_pubexp sets |e| as the public exponent for key
Adam Langleye9ada862015-05-11 17:20:37 -0700649 * generation. Returns one on success or zero on error. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800650OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx,
651 BIGNUM *e);
652
653/* EVP_PKEY_CTX_set_rsa_oaep_md sets |md| as the digest used in OAEP padding.
Adam Langleye9ada862015-05-11 17:20:37 -0700654 * Returns one on success or zero on error. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800655OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx,
656 const EVP_MD *md);
657
658/* EVP_PKEY_CTX_get_rsa_oaep_md sets |*out_md| to the digest function used in
Adam Langleye9ada862015-05-11 17:20:37 -0700659 * OAEP padding. Returns one on success or zero on error. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800660OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx,
661 const EVP_MD **out_md);
662
663/* EVP_PKEY_CTX_set_rsa_mgf1_md sets |md| as the digest used in MGF1. Returns
Adam Langleye9ada862015-05-11 17:20:37 -0700664 * one on success or zero on error. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800665OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx,
666 const EVP_MD *md);
667
668/* EVP_PKEY_CTX_get_rsa_mgf1_md sets |*out_md| to the digest function used in
Adam Langleye9ada862015-05-11 17:20:37 -0700669 * MGF1. Returns one on success or zero on error. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800670OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx,
671 const EVP_MD **out_md);
672
673/* EVP_PKEY_CTX_set0_rsa_oaep_label sets |label_len| bytes from |label| as the
Adam Langleye9ada862015-05-11 17:20:37 -0700674 * label used in OAEP. DANGER: On success, this call takes ownership of |label|
675 * and will call |OPENSSL_free| on it when |ctx| is destroyed.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800676 *
Adam Langleye9ada862015-05-11 17:20:37 -0700677 * Returns one on success or zero on error. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800678OPENSSL_EXPORT int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx,
David Benjamin4969cc92016-04-22 15:02:23 -0400679 uint8_t *label,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800680 size_t label_len);
681
682/* EVP_PKEY_CTX_get0_rsa_oaep_label sets |*out_label| to point to the internal
683 * buffer containing the OAEP label (which may be NULL) and returns the length
Adam Langleye9ada862015-05-11 17:20:37 -0700684 * of the label or a negative value on error.
685 *
686 * WARNING: the return value differs from the usual return value convention. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800687OPENSSL_EXPORT int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx,
688 const uint8_t **out_label);
689
690
Adam Langleye9ada862015-05-11 17:20:37 -0700691/* Deprecated functions. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800692
Adam Langley4139edb2016-01-13 15:00:54 -0800693/* EVP_PKEY_DH is defined for compatibility, but it is impossible to create an
694 * |EVP_PKEY| of that type. */
695#define EVP_PKEY_DH NID_dhKeyAgreement
696
David Benjamin4969cc92016-04-22 15:02:23 -0400697/* EVP_PKEY_RSA2 was historically an alternate form for RSA public keys (OID
698 * 2.5.8.1.1), but is no longer accepted. */
699#define EVP_PKEY_RSA2 NID_rsa
700
Adam Langleyd9e397b2015-01-22 14:27:53 -0800701/* OpenSSL_add_all_algorithms does nothing. */
702OPENSSL_EXPORT void OpenSSL_add_all_algorithms(void);
703
David Benjaminc895d6b2016-08-11 13:26:41 -0400704/* OPENSSL_add_all_algorithms_conf does nothing. */
705OPENSSL_EXPORT void OPENSSL_add_all_algorithms_conf(void);
David Benjamin6e899c72016-06-09 18:02:18 -0400706
Adam Langleyf4e42722015-06-04 17:45:09 -0700707/* OpenSSL_add_all_ciphers does nothing. */
708OPENSSL_EXPORT void OpenSSL_add_all_ciphers(void);
709
710/* OpenSSL_add_all_digests does nothing. */
711OPENSSL_EXPORT void OpenSSL_add_all_digests(void);
712
Adam Langleyd9e397b2015-01-22 14:27:53 -0800713/* EVP_cleanup does nothing. */
714OPENSSL_EXPORT void EVP_cleanup(void);
715
David Benjamin4969cc92016-04-22 15:02:23 -0400716OPENSSL_EXPORT void EVP_CIPHER_do_all_sorted(
717 void (*callback)(const EVP_CIPHER *cipher, const char *name,
718 const char *unused, void *arg),
719 void *arg);
Kenny Rootb8494592015-09-25 02:29:14 +0000720
David Benjamin4969cc92016-04-22 15:02:23 -0400721OPENSSL_EXPORT void EVP_MD_do_all_sorted(void (*callback)(const EVP_MD *cipher,
722 const char *name,
723 const char *unused,
724 void *arg),
725 void *arg);
Kenny Rootb8494592015-09-25 02:29:14 +0000726
David Benjamin4969cc92016-04-22 15:02:23 -0400727/* i2d_PrivateKey marshals a private key from |key| to an ASN.1, DER
728 * structure. If |outp| is not NULL then the result is written to |*outp| and
729 * |*outp| is advanced just past the output. It returns the number of bytes in
730 * the result, whether written or not, or a negative value on error.
Adam Langleyfad63272015-11-12 12:15:39 -0800731 *
David Benjamin4969cc92016-04-22 15:02:23 -0400732 * RSA keys are serialized as a DER-encoded RSAPublicKey (RFC 3447) structure.
733 * EC keys are serialized as a DER-encoded ECPrivateKey (RFC 5915) structure.
734 *
735 * Use |RSA_marshal_private_key| or |EC_marshal_private_key| instead. */
736OPENSSL_EXPORT int i2d_PrivateKey(const EVP_PKEY *key, uint8_t **outp);
737
738/* i2d_PublicKey marshals a public key from |key| to a type-specific format.
739 * If |outp| is not NULL then the result is written to |*outp| and
740 * |*outp| is advanced just past the output. It returns the number of bytes in
741 * the result, whether written or not, or a negative value on error.
742 *
743 * RSA keys are serialized as a DER-encoded RSAPublicKey (RFC 3447) structure.
744 * EC keys are serialized as an EC point per SEC 1.
745 *
746 * Use |RSA_marshal_public_key| or |EC_POINT_point2cbb| instead. */
747OPENSSL_EXPORT int i2d_PublicKey(EVP_PKEY *key, uint8_t **outp);
748
749/* d2i_PrivateKey parses an ASN.1, DER-encoded, private key from |len| bytes at
750 * |*inp|. If |out| is not NULL then, on exit, a pointer to the result is in
751 * |*out|. Note that, even if |*out| is already non-NULL on entry, it will not
752 * be written to. Rather, a fresh |EVP_PKEY| is allocated and the previous one
753 * is freed. On successful exit, |*inp| is advanced past the DER structure. It
754 * returns the result or NULL on error.
755 *
756 * This function tries to detect one of several formats. Instead, use
757 * |EVP_parse_private_key| for a PrivateKeyInfo, |RSA_parse_private_key| for an
758 * RSAPrivateKey, and |EC_parse_private_key| for an ECPrivateKey. */
759OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **out,
760 const uint8_t **inp, long len);
761
762/* d2i_AutoPrivateKey acts the same as |d2i_PrivateKey|, but detects the type
763 * of the private key.
764 *
765 * This function tries to detect one of several formats. Instead, use
766 * |EVP_parse_private_key| for a PrivateKeyInfo, |RSA_parse_private_key| for an
767 * RSAPrivateKey, and |EC_parse_private_key| for an ECPrivateKey. */
768OPENSSL_EXPORT EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **out, const uint8_t **inp,
769 long len);
770
David Benjaminc895d6b2016-08-11 13:26:41 -0400771/* EVP_PKEY_get0_DH returns NULL. */
772OPENSSL_EXPORT DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey);
773
David Benjamin4969cc92016-04-22 15:02:23 -0400774
775/* Private structures. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800776
777struct evp_pkey_st {
Adam Langleyf4e42722015-06-04 17:45:09 -0700778 CRYPTO_refcount_t references;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800779
780 /* type contains one of the EVP_PKEY_* values or NID_undef and determines
781 * which element (if any) of the |pkey| union is valid. */
782 int type;
783
Adam Langleyd9e397b2015-01-22 14:27:53 -0800784 union {
Robert Sloan572a4e22017-04-17 10:52:19 -0700785 void *ptr;
Kenny Rootb8494592015-09-25 02:29:14 +0000786 RSA *rsa;
787 DSA *dsa;
788 DH *dh;
789 EC_KEY *ec;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800790 } pkey;
791
Adam Langleyd9e397b2015-01-22 14:27:53 -0800792 /* ameth contains a pointer to a method table that contains many ASN.1
793 * methods for the key type. */
794 const EVP_PKEY_ASN1_METHOD *ameth;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800795} /* EVP_PKEY */;
796
797
798#if defined(__cplusplus)
799} /* extern C */
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400800
801extern "C++" {
802namespace bssl {
803
804BORINGSSL_MAKE_DELETER(EVP_PKEY, EVP_PKEY_free)
805BORINGSSL_MAKE_DELETER(EVP_PKEY_CTX, EVP_PKEY_CTX_free)
806
807} // namespace bssl
808
809} /* extern C++ */
810
Adam Langleyd9e397b2015-01-22 14:27:53 -0800811#endif
812
Adam Langleye9ada862015-05-11 17:20:37 -0700813#define EVP_R_BUFFER_TOO_SMALL 100
814#define EVP_R_COMMAND_NOT_SUPPORTED 101
David Benjamin4969cc92016-04-22 15:02:23 -0400815#define EVP_R_DECODE_ERROR 102
816#define EVP_R_DIFFERENT_KEY_TYPES 103
817#define EVP_R_DIFFERENT_PARAMETERS 104
818#define EVP_R_ENCODE_ERROR 105
819#define EVP_R_EXPECTING_AN_EC_KEY_KEY 106
820#define EVP_R_EXPECTING_AN_RSA_KEY 107
821#define EVP_R_EXPECTING_A_DSA_KEY 108
822#define EVP_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE 109
823#define EVP_R_INVALID_DIGEST_LENGTH 110
824#define EVP_R_INVALID_DIGEST_TYPE 111
825#define EVP_R_INVALID_KEYBITS 112
826#define EVP_R_INVALID_MGF1_MD 113
827#define EVP_R_INVALID_OPERATION 114
828#define EVP_R_INVALID_PADDING_MODE 115
829#define EVP_R_INVALID_PSS_SALTLEN 116
830#define EVP_R_KEYS_NOT_SET 117
831#define EVP_R_MISSING_PARAMETERS 118
832#define EVP_R_NO_DEFAULT_DIGEST 119
833#define EVP_R_NO_KEY_SET 120
834#define EVP_R_NO_MDC2_SUPPORT 121
835#define EVP_R_NO_NID_FOR_CURVE 122
836#define EVP_R_NO_OPERATION_SET 123
837#define EVP_R_NO_PARAMETERS_SET 124
838#define EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE 125
839#define EVP_R_OPERATON_NOT_INITIALIZED 126
840#define EVP_R_UNKNOWN_PUBLIC_KEY_TYPE 127
841#define EVP_R_UNSUPPORTED_ALGORITHM 128
842#define EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE 129
Robert Sloan572a4e22017-04-17 10:52:19 -0700843#define EVP_R_NOT_A_PRIVATE_KEY 130
844#define EVP_R_INVALID_SIGNATURE 131
Adam Langleyd9e397b2015-01-22 14:27:53 -0800845
846#endif /* OPENSSL_HEADER_EVP_H */