1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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)"
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
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.] */
57 #ifndef OPENSSL_HEADER_EVP_H
58 #define OPENSSL_HEADER_EVP_H
60 #include <openssl/base.h>
61 #include <openssl/stack.h>
63 /* OpenSSL included digest and cipher functions in this header so we include
64 * them for users that still expect that.
66 * TODO(fork): clean up callers so that they include what they use. */
67 #include <openssl/aead.h>
68 #include <openssl/cipher.h>
69 #include <openssl/digest.h>
70 #include <openssl/mem.h>
71 #include <openssl/obj.h>
72 #include <openssl/thread.h>
74 #if defined(__cplusplus)
79 /* EVP abstracts over public/private key algorithms. */
82 /* Public key objects. */
84 /* EVP_PKEY_new creates a new, empty public-key object and returns it or NULL
85 * on allocation failure. */
86 OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_new(void);
88 /* EVP_PKEY_free frees all data referenced by |pkey| and then frees |pkey|
90 OPENSSL_EXPORT void EVP_PKEY_free(EVP_PKEY *pkey);
92 /* EVP_PKEY_is_opaque returns one if |pkey| is opaque. Opaque keys are backed by
93 * custom implementations which do not expose key material and parameters. It is
94 * an error to attempt to duplicate, export, or compare an opaque key. */
95 OPENSSL_EXPORT int EVP_PKEY_is_opaque(const EVP_PKEY *pkey);
97 /* EVP_PKEY_cmp compares |a| and |b| and returns one if they are equal, zero if
98 * not and a negative number on error.
100 * WARNING: this differs from the traditional return value of a "cmp"
102 OPENSSL_EXPORT int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b);
104 /* EVP_PKEY_dup adds one to the reference count of |pkey| and returns
106 OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey);
108 /* EVP_PKEY_copy_parameters sets the parameters of |to| to equal the parameters
109 * of |from|. It returns one on success and zero on error. */
110 OPENSSL_EXPORT int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from);
112 /* EVP_PKEY_missing_parameters returns one if |pkey| is missing needed
113 * parameters or zero if not, or if the algorithm doesn't take parameters. */
114 OPENSSL_EXPORT int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey);
116 /* EVP_PKEY_size returns the "size", in bytes, of |pkey|. For example, for an
117 * RSA key this returns the number of bytes needed to represent the modulus. */
118 OPENSSL_EXPORT int EVP_PKEY_size(const EVP_PKEY *pkey);
120 /* EVP_PKEY_bits returns the "size", in bits, of |pkey|. For example, for an
121 * RSA key, this returns the bit length of the modulus. */
122 OPENSSL_EXPORT int EVP_PKEY_bits(EVP_PKEY *pkey);
124 /* EVP_PKEY_id returns the type of |pkey|, which is one of the |EVP_PKEY_*|
126 OPENSSL_EXPORT int EVP_PKEY_id(const EVP_PKEY *pkey);
128 /* EVP_PKEY_type returns a canonicalised form of |NID|. For example,
129 * |EVP_PKEY_RSA2| will be turned into |EVP_PKEY_RSA|. */
130 OPENSSL_EXPORT int EVP_PKEY_type(int nid);
132 /* EVP_PKEY_new_mac_key allocates a fresh |EVP_PKEY| of the given type (e.g.
133 * |EVP_PKEY_HMAC|), sets |mac_key| as the MAC key and "generates" a new key,
134 * suitable for signing. It returns the fresh |EVP_PKEY|, or NULL on error. */
135 OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *engine,
136 const uint8_t *mac_key,
140 /* Getting and setting concrete public key types.
142 * The following functions get and set the underlying public key in an
143 * |EVP_PKEY| object. The |set1| functions take an additional reference to the
144 * underlying key and return one on success or zero on error. The |assign|
145 * functions adopt the caller's reference. The getters return a fresh reference
146 * to the underlying object. */
148 OPENSSL_EXPORT int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key);
149 OPENSSL_EXPORT int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key);
150 OPENSSL_EXPORT RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey);
152 OPENSSL_EXPORT int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, struct dsa_st *key);
153 OPENSSL_EXPORT int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key);
154 OPENSSL_EXPORT struct dsa_st *EVP_PKEY_get1_DSA(EVP_PKEY *pkey);
156 OPENSSL_EXPORT int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, struct ec_key_st *key);
157 OPENSSL_EXPORT int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key);
158 OPENSSL_EXPORT struct ec_key_st *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey);
160 OPENSSL_EXPORT int EVP_PKEY_set1_DH(EVP_PKEY *pkey, struct dh_st *key);
161 OPENSSL_EXPORT int EVP_PKEY_assign_DH(EVP_PKEY *pkey, DH *key);
162 OPENSSL_EXPORT struct dh_st *EVP_PKEY_get1_DH(EVP_PKEY *pkey);
164 #define EVP_PKEY_NONE NID_undef
165 #define EVP_PKEY_RSA NID_rsaEncryption
166 #define EVP_PKEY_RSA2 NID_rsa
167 #define EVP_PKEY_DSA NID_dsa
168 #define EVP_PKEY_DH NID_dhKeyAgreement
169 #define EVP_PKEY_DHX NID_dhpublicnumber
170 #define EVP_PKEY_EC NID_X9_62_id_ecPublicKey
171 #define EVP_PKEY_HMAC NID_hmac
173 /* EVP_PKEY_assign sets the underlying key of |pkey| to |key|, which must be of
174 * the given type. The |type| argument should be one of the |EVP_PKEY_*|
176 OPENSSL_EXPORT int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key);
178 /* EVP_PKEY_set_type sets the type of |pkey| to |type|, which should be one of
179 * the |EVP_PKEY_*| values. It returns one if sucessful or zero otherwise. If
180 * |pkey| is NULL, it simply reports whether the type is known. */
181 OPENSSL_EXPORT int EVP_PKEY_set_type(EVP_PKEY *pkey, int type);
183 /* EVP_PKEY_cmp_parameters compares the parameters of |a| and |b|. It returns
184 * one if they match, zero if not, or a negative number of on error.
186 * WARNING: the return value differs from the usual return value convention. */
187 OPENSSL_EXPORT int EVP_PKEY_cmp_parameters(const EVP_PKEY *a,
191 /* ASN.1 functions */
193 /* d2i_PrivateKey parses an ASN.1, DER-encoded, private key from |len| bytes at
194 * |*inp|. If |out| is not NULL then, on exit, a pointer to the result is in
195 * |*out|. If |*out| is already non-NULL on entry then the result is written
196 * directly into |*out|, otherwise a fresh |EVP_PKEY| is allocated. On
197 * successful exit, |*inp| is advanced past the DER structure. It returns the
198 * result or NULL on error. */
199 OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **out,
200 const uint8_t **inp, long len);
202 /* d2i_AutoPrivateKey acts the same as |d2i_PrivateKey|, but detects the type
203 * of the private key. */
204 OPENSSL_EXPORT EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **out, const uint8_t **inp,
207 /* i2d_PrivateKey marshals a private key from |key| to an ASN.1, DER
208 * structure. If |outp| is not NULL then the result is written to |*outp| and
209 * |*outp| is advanced just past the output. It returns the number of bytes in
210 * the result, whether written or not, or a negative value on error. */
211 OPENSSL_EXPORT int i2d_PrivateKey(const EVP_PKEY *key, uint8_t **outp);
213 /* i2d_PublicKey marshals a public key from |key| to an ASN.1, DER
214 * structure. If |outp| is not NULL then the result is written to |*outp| and
215 * |*outp| is advanced just past the output. It returns the number of bytes in
216 * the result, whether written or not, or a negative value on error. */
217 OPENSSL_EXPORT int i2d_PublicKey(EVP_PKEY *key, uint8_t **outp);
222 /* EVP_DigestSignInit sets up |ctx| for a signing operation with |type| and
223 * |pkey|. The |ctx| argument must have been initialised with
224 * |EVP_MD_CTX_init|. If |pctx| is not NULL, the |EVP_PKEY_CTX| of the signing
225 * operation will be written to |*pctx|; this can be used to set alternative
228 * It returns one on success, or zero on error. */
229 OPENSSL_EXPORT int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
230 const EVP_MD *type, ENGINE *e,
233 /* EVP_DigestSignUpdate appends |len| bytes from |data| to the data which will
234 * be signed in |EVP_DigestSignFinal|. It returns one on success and zero
236 OPENSSL_EXPORT int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data,
239 /* EVP_DigestSignFinal signs the data that has been included by one or more
240 * calls to |EVP_DigestSignUpdate|. If |out_sig| is NULL then |*out_sig_len| is
241 * set to the maximum number of output bytes. Otherwise, on entry,
242 * |*out_sig_len| must contain the length of the |out_sig| buffer. If the call
243 * is successful, the signature is written to |out_sig| and |*out_sig_len| is
246 * It returns one on success, or zero on error. */
247 OPENSSL_EXPORT int EVP_DigestSignFinal(EVP_MD_CTX *ctx, uint8_t *out_sig,
248 size_t *out_sig_len);
250 /* EVP_DigestSignAlgorithm encodes the signing parameters of |ctx| as an
251 * AlgorithmIdentifer and saves the result in |algor|.
253 * It returns one on success, or zero on error.
255 * TODO(davidben): This API should eventually lose the dependency on
257 OPENSSL_EXPORT int EVP_DigestSignAlgorithm(EVP_MD_CTX *ctx, X509_ALGOR *algor);
262 /* EVP_DigestVerifyInit sets up |ctx| for a signature verification operation
263 * with |type| and |pkey|. The |ctx| argument must have been initialised with
264 * |EVP_MD_CTX_init|. If |pctx| is not NULL, the |EVP_PKEY_CTX| of the signing
265 * operation will be written to |*pctx|; this can be used to set alternative
268 * It returns one on success, or zero on error. */
269 OPENSSL_EXPORT int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
270 const EVP_MD *type, ENGINE *e,
273 /* EVP_DigestVerifyInitFromAlgorithm sets up |ctx| for a signature verification
274 * operation with public key |pkey| and parameters from |algor|. The |ctx|
275 * argument must have been initialised with |EVP_MD_CTX_init|.
277 * It returns one on success, or zero on error.
279 * TODO(davidben): This API should eventually lose the dependency on
281 OPENSSL_EXPORT int EVP_DigestVerifyInitFromAlgorithm(EVP_MD_CTX *ctx,
285 /* EVP_DigestVerifyUpdate appends |len| bytes from |data| to the data which
286 * will be verified by |EVP_DigestVerifyFinal|. It returns one on success and
288 OPENSSL_EXPORT int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data,
291 /* EVP_DigestVerifyFinal verifies that |sig_len| bytes of |sig| are a valid
292 * signature for the data that has been included by one or more calls to
293 * |EVP_DigestVerifyUpdate|. It returns one on success and zero otherwise. */
294 OPENSSL_EXPORT int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig,
298 /* Signing (old functions) */
300 /* EVP_SignInit_ex configures |ctx|, which must already have been initialised,
301 * for a fresh signing operation using the hash function |type|. It returns one
302 * on success and zero otherwise.
304 * (In order to initialise |ctx|, either obtain it initialised with
305 * |EVP_MD_CTX_create|, or use |EVP_MD_CTX_init|.) */
306 OPENSSL_EXPORT int EVP_SignInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type,
309 /* EVP_SignInit is a deprecated version of |EVP_SignInit_ex|.
311 * TODO(fork): remove. */
312 OPENSSL_EXPORT int EVP_SignInit(EVP_MD_CTX *ctx, const EVP_MD *type);
314 /* EVP_SignUpdate appends |len| bytes from |data| to the data which will be
315 * signed in |EVP_SignFinal|. */
316 OPENSSL_EXPORT int EVP_SignUpdate(EVP_MD_CTX *ctx, const void *data,
319 /* EVP_SignFinal signs the data that has been included by one or more calls to
320 * |EVP_SignUpdate|, using the key |pkey|, and writes it to |sig|. On entry,
321 * |sig| must point to at least |EVP_PKEY_size(pkey)| bytes of space. The
322 * actual size of the signature is written to |*out_sig_len|.
324 * It returns one on success and zero otherwise.
326 * It does not modify |ctx|, thus it's possible to continue to use |ctx| in
327 * order to sign a longer message. */
328 OPENSSL_EXPORT int EVP_SignFinal(const EVP_MD_CTX *ctx, uint8_t *sig,
329 unsigned int *out_sig_len, EVP_PKEY *pkey);
332 /* Verifying (old functions) */
334 /* EVP_VerifyInit_ex configures |ctx|, which must already have been
335 * initialised, for a fresh signature verification operation using the hash
336 * function |type|. It returns one on success and zero otherwise.
338 * (In order to initialise |ctx|, either obtain it initialised with
339 * |EVP_MD_CTX_create|, or use |EVP_MD_CTX_init|.) */
340 OPENSSL_EXPORT int EVP_VerifyInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type,
343 /* EVP_VerifyInit is a deprecated version of |EVP_VerifyInit_ex|.
345 * TODO(fork): remove. */
346 OPENSSL_EXPORT int EVP_VerifyInit(EVP_MD_CTX *ctx, const EVP_MD *type);
348 /* EVP_VerifyUpdate appends |len| bytes from |data| to the data which will be
349 * signed in |EVP_VerifyFinal|. */
350 OPENSSL_EXPORT int EVP_VerifyUpdate(EVP_MD_CTX *ctx, const void *data,
353 /* EVP_VerifyFinal verifies that |sig_len| bytes of |sig| are a valid
354 * signature, by |pkey|, for the data that has been included by one or more
355 * calls to |EVP_VerifyUpdate|.
357 * It returns one on success and zero otherwise.
359 * It does not modify |ctx|, thus it's possible to continue to use |ctx| in
360 * order to sign a longer message. */
361 OPENSSL_EXPORT int EVP_VerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig,
362 size_t sig_len, EVP_PKEY *pkey);
367 /* EVP_PKEY_print_public prints a textual representation of the public key in
368 * |pkey| to |out|. Returns one on success or zero otherwise. */
369 OPENSSL_EXPORT int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
370 int indent, ASN1_PCTX *pctx);
372 /* EVP_PKEY_print_public prints a textual representation of the private key in
373 * |pkey| to |out|. Returns one on success or zero otherwise. */
374 OPENSSL_EXPORT int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
375 int indent, ASN1_PCTX *pctx);
377 /* EVP_PKEY_print_public prints a textual representation of the parameters in
378 * |pkey| to |out|. Returns one on success or zero otherwise. */
379 OPENSSL_EXPORT int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
380 int indent, ASN1_PCTX *pctx);
383 /* Password stretching.
385 * Password stretching functions take a low-entropy password and apply a slow
386 * function that results in a key suitable for use in symmetric
389 /* PKCS5_PBKDF2_HMAC computes |iterations| iterations of PBKDF2 of |password|
390 * and |salt|, using |digest|, and outputs |key_len| bytes to |out_key|. It
391 * returns one on success and zero on error. */
392 OPENSSL_EXPORT int PKCS5_PBKDF2_HMAC(const char *password, int password_len,
393 const uint8_t *salt, size_t salt_len,
394 unsigned iterations, const EVP_MD *digest,
395 size_t key_len, uint8_t *out_key);
397 /* PKCS5_PBKDF2_HMAC_SHA1 is the same as PKCS5_PBKDF2_HMAC, but with |digest|
398 * fixed to |EVP_sha1|. */
399 OPENSSL_EXPORT int PKCS5_PBKDF2_HMAC_SHA1(const char *password,
400 int password_len, const uint8_t *salt,
401 size_t salt_len, unsigned iterations,
402 size_t key_len, uint8_t *out_key);
405 /* Public key contexts.
407 * |EVP_PKEY_CTX| objects hold the context of an operation (e.g. signing or
408 * encrypting) that uses a public key. */
410 /* EVP_PKEY_CTX_new allocates a fresh |EVP_PKEY_CTX| for use with |pkey|. It
411 * returns the context or NULL on error. */
412 OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e);
414 /* EVP_PKEY_CTX_new allocates a fresh |EVP_PKEY_CTX| for a key of type |id|
415 * (e.g. |EVP_PKEY_HMAC|). This can be used for key generation where
416 * |EVP_PKEY_CTX_new| can't be used because there isn't an |EVP_PKEY| to pass
417 * it. It returns the context or NULL on error. */
418 OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e);
420 /* EVP_KEY_CTX_free frees |ctx| and the data it owns. */
421 OPENSSL_EXPORT void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx);
423 /* EVP_PKEY_CTX_dup allocates a fresh |EVP_PKEY_CTX| and sets it equal to the
424 * state of |ctx|. It returns the fresh |EVP_PKEY_CTX| or NULL on error. */
425 OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *ctx);
427 /* EVP_PKEY_CTX_get0_pkey returns the |EVP_PKEY| associated with |ctx|. */
428 OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx);
430 /* EVP_PKEY_CTX_set_app_data sets an opaque pointer on |ctx|. */
431 OPENSSL_EXPORT void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data);
433 /* EVP_PKEY_CTX_get_app_data returns the opaque pointer from |ctx| that was
434 * previously set with |EVP_PKEY_CTX_set_app_data|, or NULL if none has been
436 OPENSSL_EXPORT void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx);
438 /* EVP_PKEY_CTX_ctrl performs |cmd| on |ctx|. The |keytype| and |optype|
439 * arguments can be -1 to specify that any type and operation are acceptable,
440 * otherwise |keytype| must match the type of |ctx| and the bits of |optype|
441 * must intersect the operation flags set on |ctx|.
443 * The |p1| and |p2| arguments depend on the value of |cmd|.
445 * It returns -2 if |cmd| is not recognised, -1 on error or a |cmd| specific
446 * value otherwise. */
447 OPENSSL_EXPORT int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
448 int cmd, int p1, void *p2);
450 /* EVP_PKEY_sign_init initialises an |EVP_PKEY_CTX| for a signing operation. It
451 * should be called before |EVP_PKEY_sign|.
453 * It returns one on success or zero on error. */
454 OPENSSL_EXPORT int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx);
456 /* EVP_PKEY_sign signs |data_len| bytes from |data| using |ctx|. If |sig| is
457 * NULL, the maximum size of the signature is written to
458 * |out_sig_len|. Otherwise, |*sig_len| must contain the number of bytes of
459 * space available at |sig|. If sufficient, the signature will be written to
460 * |sig| and |*sig_len| updated with the true length.
462 * WARNING: Setting |sig| to NULL only gives the maximum size of the
463 * signature. The actual signature may be smaller.
465 * It returns one on success or zero on error. (Note: this differs from
466 * OpenSSL, which can also return negative values to indicate an error. ) */
467 OPENSSL_EXPORT int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, uint8_t *sig,
468 size_t *sig_len, const uint8_t *data,
471 /* EVP_PKEY_verify_init initialises an |EVP_PKEY_CTX| for a signature
472 * verification operation. It should be called before |EVP_PKEY_verify|.
474 * It returns one on success or zero on error. */
475 OPENSSL_EXPORT int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx);
477 /* EVP_PKEY_verify verifies that |sig_len| bytes from |sig| are a valid signature
480 * It returns one on success or zero on error. */
481 OPENSSL_EXPORT int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig,
482 size_t sig_len, const uint8_t *data,
485 /* EVP_PKEY_encrypt_init initialises an |EVP_PKEY_CTX| for an encryption
486 * operation. It should be called before |EVP_PKEY_encrypt|.
488 * It returns one on success or zero on error. */
489 OPENSSL_EXPORT int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx);
491 /* EVP_PKEY_encrypt encrypts |in_len| bytes from |in|. If |out| is NULL, the
492 * maximum size of the ciphertext is written to |out_len|. Otherwise, |*out_len|
493 * must contain the number of bytes of space available at |out|. If sufficient,
494 * the ciphertext will be written to |out| and |*out_len| updated with the true
497 * WARNING: Setting |out| to NULL only gives the maximum size of the
498 * ciphertext. The actual ciphertext may be smaller.
500 * It returns one on success or zero on error. */
501 OPENSSL_EXPORT int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out,
502 size_t *out_len, const uint8_t *in,
505 /* EVP_PKEY_decrypt_init initialises an |EVP_PKEY_CTX| for a decryption
506 * operation. It should be called before |EVP_PKEY_decrypt|.
508 * It returns one on success or zero on error. */
509 OPENSSL_EXPORT int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx);
511 /* EVP_PKEY_decrypt decrypts |in_len| bytes from |in|. If |out| is NULL, the
512 * maximum size of the plaintext is written to |out_len|. Otherwise, |*out_len|
513 * must contain the number of bytes of space available at |out|. If sufficient,
514 * the ciphertext will be written to |out| and |*out_len| updated with the true
517 * WARNING: Setting |out| to NULL only gives the maximum size of the
518 * plaintext. The actual plaintext may be smaller.
520 * It returns one on success or zero on error. */
521 OPENSSL_EXPORT int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out,
522 size_t *out_len, const uint8_t *in,
525 /* EVP_PKEY_derive_init initialises an |EVP_PKEY_CTX| for a key derivation
526 * operation. It should be called before |EVP_PKEY_derive_set_peer| and
529 * It returns one on success or zero on error. */
530 OPENSSL_EXPORT int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx);
532 /* EVP_PKEY_derive_set_peer sets the peer's key to be used for key derivation
533 * by |ctx| to |peer|. It should be called after |EVP_PKEY_derive_init|. (For
534 * example, this is used to set the peer's key in (EC)DH.) It returns one on
535 * success and zero on error. */
536 OPENSSL_EXPORT int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer);
538 /* EVP_PKEY_derive derives a shared key between the two keys configured in
539 * |ctx|. If |key| is non-NULL then, on entry, |out_key_len| must contain the
540 * amount of space at |key|. If sufficient then the shared key will be written
541 * to |key| and |*out_key_len| will be set to the length. If |key| is NULL then
542 * |out_key_len| will be set to the maximum length.
544 * WARNING: Setting |out| to NULL only gives the maximum size of the key. The
545 * actual key may be smaller.
547 * It returns one on success and zero on error. */
548 OPENSSL_EXPORT int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, uint8_t *key,
549 size_t *out_key_len);
551 /* EVP_PKEY_keygen_init initialises an |EVP_PKEY_CTX| for a key generation
552 * operation. It should be called before |EVP_PKEY_keygen|.
554 * It returns one on success or zero on error. */
555 OPENSSL_EXPORT int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx);
557 /* EVP_PKEY_keygen performs a key generation operation using the values from
558 * |ctx| and sets |*ppkey| to a fresh |EVP_PKEY| containing the resulting key.
559 * It returns one on success or zero on error. */
560 OPENSSL_EXPORT int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
563 /* EVP_PKEY_CTX_ctrl operations.
565 * These values are passed as the |cmd| argument to
566 * EVP_PKEY_CTX_ctrl */
570 /* EVP_PKEY_CTX_set_signature_md sets |md| as the digest to be used in a
571 * signature operation. It returns one on success or otherwise on error. See
572 * the return values of |EVP_PKEY_CTX_ctrl| for details. */
573 OPENSSL_EXPORT int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx,
576 /* EVP_PKEY_CTX_get_signature_md sets |*out_md| to the digest to be used in a
577 * signature operation. It returns one on success or otherwise on error. See
578 * the return values of |EVP_PKEY_CTX_ctrl| for details. */
579 OPENSSL_EXPORT int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx,
580 const EVP_MD **out_md);
582 /* EVP_PKEY_CTRL_DIGESTINIT is an internal value. It's called by
583 * EVP_DigestInit_ex to signal the |EVP_PKEY| that a digest operation is
585 #define EVP_PKEY_CTRL_DIGESTINIT 3
587 /* EVP_PKEY_CTRL_PEER_KEY is called with different values of |p1|:
588 * 0: Is called from |EVP_PKEY_derive_set_peer| and |p2| contains a peer key.
589 * If the return value is <= 0, the key is rejected.
590 * 1: Is called at the end of |EVP_PKEY_derive_set_peer| and |p2| contains a
591 * peer key. If the return value is <= 0, the key is rejected.
592 * 2: Is called with |p2| == NULL to test whether the peer's key was used.
593 * (EC)DH always return one in this case.
594 * 3: Is called with |p2| == NULL to set whether the peer's key was used.
595 * (EC)DH always return one in this case. This was only used for GOST. */
596 #define EVP_PKEY_CTRL_PEER_KEY 4
598 /* EVP_PKEY_CTRL_SET_MAC_KEY sets a MAC key. For example, this can be done an
599 * |EVP_PKEY_CTX| prior to calling |EVP_PKEY_keygen| in order to generate an
600 * HMAC |EVP_PKEY| with the given key. It returns one on success and zero on
602 #define EVP_PKEY_CTRL_SET_MAC_KEY 5
604 /* EVP_PKEY_ALG_CTRL is the base value from which key-type specific ctrl
605 * commands are numbered. */
606 #define EVP_PKEY_ALG_CTRL 0x1000
609 /* RSA specific control functions. */
611 /* EVP_PKEY_CTX_set_rsa_padding sets the padding type to use. It should be one
612 * of the |RSA_*_PADDING| values. Returns one on success or another value on
613 * error. See |EVP_PKEY_CTX_ctrl| for the other return values, which are
615 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int padding);
617 /* EVP_PKEY_CTX_get_rsa_padding sets |*out_padding| to the current padding
618 * value, which is one of the |RSA_*_PADDING| values. Returns one on success or
619 * another value on error. See |EVP_PKEY_CTX_ctrl| for the other return values,
620 * which are non-standard. */
621 OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx,
624 /* EVP_PKEY_CTX_set_rsa_pss_saltlen sets the length of the salt in a PSS-padded
625 * signature. A value of -1 cause the salt to be the same length as the digest
626 * in the signature. A value of -2 causes the salt to be the maximum length
627 * that will fit. Otherwise the value gives the size of the salt in bytes.
629 * Returns one on success or another value on error. See |EVP_PKEY_CTX_ctrl|
630 * for the other return values, which are non-standard. */
631 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx,
634 /* EVP_PKEY_CTX_get_rsa_pss_saltlen sets |*out_salt_len| to the salt length of
635 * a PSS-padded signature. See the documentation for
636 * |EVP_PKEY_CTX_set_rsa_pss_saltlen| for details of the special values that it
639 * Returns one on success or another value on error. See |EVP_PKEY_CTX_ctrl|
640 * for the other return values, which are non-standard. */
641 OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx,
644 /* EVP_PKEY_CTX_set_rsa_keygen_bits sets the size of the desired RSA modulus,
645 * in bits, for key generation. Returns one on success or another value on
646 * error. See |EVP_PKEY_CTX_ctrl| for the other return values, which are
648 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx,
651 /* EVP_PKEY_CTX_set_rsa_keygen_pubexp sets |e| as the public exponent for key
652 * generation. Returns one on success or another value on error. See
653 * |EVP_PKEY_CTX_ctrl| for the other return values, which are non-standard. */
654 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx,
657 /* EVP_PKEY_CTX_set_rsa_oaep_md sets |md| as the digest used in OAEP padding.
658 * Returns one on success or another value on error. See |EVP_PKEY_CTX_ctrl|
659 * for the other return values, which are non-standard. */
660 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx,
663 /* EVP_PKEY_CTX_get_rsa_oaep_md sets |*out_md| to the digest function used in
664 * OAEP padding. Returns one on success or another value on error. See
665 * |EVP_PKEY_CTX_ctrl| for the other return values, which are non-standard. */
666 OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx,
667 const EVP_MD **out_md);
669 /* EVP_PKEY_CTX_set_rsa_mgf1_md sets |md| as the digest used in MGF1. Returns
670 * one on success or another value on error. See |EVP_PKEY_CTX_ctrl| for the
671 * other return values, which are non-standard. */
672 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx,
675 /* EVP_PKEY_CTX_get_rsa_mgf1_md sets |*out_md| to the digest function used in
676 * MGF1. Returns one on success or another value on error. See
677 * |EVP_PKEY_CTX_ctrl| for the other return values, which are non-standard. */
678 OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx,
679 const EVP_MD **out_md);
681 /* EVP_PKEY_CTX_set0_rsa_oaep_label sets |label_len| bytes from |label| as the
682 * label used in OAEP. DANGER: this call takes ownership of |label| and will
683 * call |free| on it when |ctx| is destroyed.
685 * Returns one on success or another value on error. See |EVP_PKEY_CTX_ctrl|
686 * for the other return values, which are non-standard. */
687 OPENSSL_EXPORT int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx,
688 const uint8_t *label,
691 /* EVP_PKEY_CTX_get0_rsa_oaep_label sets |*out_label| to point to the internal
692 * buffer containing the OAEP label (which may be NULL) and returns the length
693 * of the label or a negative value on error. */
694 OPENSSL_EXPORT int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx,
695 const uint8_t **out_label);
700 #define EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID (EVP_PKEY_ALG_CTRL + 1)
701 #define EVP_PKEY_CTRL_EC_PARAM_ENC (EVP_PKEY_ALG_CTRL + 2)
702 #define EVP_PKEY_CTRL_EC_ECDH_COFACTOR (EVP_PKEY_ALG_CTRL + 3)
703 #define EVP_PKEY_CTRL_EC_KDF_TYPE (EVP_PKEY_ALG_CTRL + 4)
704 #define EVP_PKEY_CTRL_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 5)
705 #define EVP_PKEY_CTRL_GET_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 6)
706 #define EVP_PKEY_CTRL_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 7)
707 #define EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 8)
708 #define EVP_PKEY_CTRL_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 9)
709 #define EVP_PKEY_CTRL_GET_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 10)
711 #define EVP_PKEY_ECDH_KDF_NONE 1
712 #define EVP_PKEY_ECDH_KDF_X9_62 2
715 /* PKEY ctrl commands.
717 * These values are passed as the |op| argument to
718 * EVP_PKEY_ASN1_METHOD.pkey_ctrl. */
720 /* ASN1_PKEY_CTRL_DEFAULT_MD_NID expects |arg2| to be an |int*| and sets the
721 * pointed at int to be the NID of the default hash function used in
723 #define ASN1_PKEY_CTRL_DEFAULT_MD_NID 0x3
726 /* Private functions */
728 /* OpenSSL_add_all_algorithms does nothing. */
729 OPENSSL_EXPORT void OpenSSL_add_all_algorithms(void);
731 /* EVP_cleanup does nothing. */
732 OPENSSL_EXPORT void EVP_cleanup(void);
734 /* EVP_PKEY_asn1_find returns the ASN.1 method table for the given |nid|, which
735 * should be one of the |EVP_PKEY_*| values. It returns NULL if |nid| is
737 OPENSSL_EXPORT const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pengine,
740 /* TODO(fork): move to PEM? */
741 OPENSSL_EXPORT const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(
742 ENGINE **pengine, const char *name, size_t len);
747 /* type contains one of the EVP_PKEY_* values or NID_undef and determines
748 * which element (if any) of the |pkey| union is valid. */
751 /* TODO(fork): document */
756 struct rsa_st *rsa; /* RSA */
757 struct dsa_st *dsa; /* DSA */
758 struct dh_st *dh; /* DH */
759 struct ec_key_st *ec; /* ECC */
764 /* TODO(fork): document */
766 /* ameth contains a pointer to a method table that contains many ASN.1
767 * methods for the key type. */
768 const EVP_PKEY_ASN1_METHOD *ameth;
770 /* TODO(fork): document; */
771 STACK_OF(X509_ATTRIBUTE) * attributes; /* [ 0 ] */
775 #if defined(__cplusplus)
779 #define EVP_F_rsa_item_verify 100
780 #define EVP_F_do_sigver_init 101
781 #define EVP_F_eckey_priv_decode 102
782 #define EVP_F_pkey_ec_sign 103
783 #define EVP_F_EVP_PKEY_sign_init 104
784 #define EVP_F_d2i_PrivateKey 105
785 #define EVP_F_rsa_priv_encode 106
786 #define EVP_F_rsa_mgf1_to_md 107
787 #define EVP_F_EVP_PKEY_get1_DH 108
788 #define EVP_F_EVP_PKEY_sign 109
789 #define EVP_F_old_ec_priv_decode 110
790 #define EVP_F_EVP_PKEY_get1_RSA 111
791 #define EVP_F_pkey_ec_ctrl 112
792 #define EVP_F_evp_pkey_ctx_new 113
793 #define EVP_F_EVP_PKEY_verify 114
794 #define EVP_F_EVP_PKEY_encrypt 115
795 #define EVP_F_EVP_PKEY_keygen 116
796 #define EVP_F_eckey_type2param 117
797 #define EVP_F_eckey_priv_encode 118
798 #define EVP_F_do_EC_KEY_print 119
799 #define EVP_F_pkey_ec_keygen 120
800 #define EVP_F_EVP_PKEY_encrypt_init 121
801 #define EVP_F_pkey_rsa_ctrl 122
802 #define EVP_F_rsa_priv_decode 123
803 #define EVP_F_rsa_pss_to_ctx 124
804 #define EVP_F_EVP_PKEY_get1_EC_KEY 125
805 #define EVP_F_EVP_PKEY_verify_init 126
806 #define EVP_F_EVP_PKEY_derive_init 127
807 #define EVP_F_eckey_param2type 128
808 #define EVP_F_eckey_pub_decode 129
809 #define EVP_F_d2i_AutoPrivateKey 130
810 #define EVP_F_eckey_param_decode 131
811 #define EVP_F_EVP_PKEY_new 132
812 #define EVP_F_pkey_ec_derive 133
813 #define EVP_F_pkey_ec_paramgen 134
814 #define EVP_F_EVP_PKEY_CTX_ctrl 135
815 #define EVP_F_EVP_PKEY_decrypt_init 136
816 #define EVP_F_EVP_PKEY_decrypt 137
817 #define EVP_F_EVP_PKEY_copy_parameters 138
818 #define EVP_F_EVP_PKEY_set_type 139
819 #define EVP_F_EVP_PKEY_derive 140
820 #define EVP_F_EVP_PKEY_keygen_init 141
821 #define EVP_F_do_rsa_print 142
822 #define EVP_F_old_rsa_priv_decode 143
823 #define EVP_F_rsa_algor_to_md 144
824 #define EVP_F_eckey_pub_encode 145
825 #define EVP_F_EVP_PKEY_derive_set_peer 146
826 #define EVP_F_pkey_rsa_sign 147
827 #define EVP_F_check_padding_md 148
828 #define EVP_F_i2d_PublicKey 149
829 #define EVP_F_rsa_pub_decode 150
830 #define EVP_F_EVP_PKEY_get1_DSA 151
831 #define EVP_F_pkey_rsa_encrypt 152
832 #define EVP_F_pkey_rsa_decrypt 153
833 #define EVP_F_hmac_signctx 154
834 #define EVP_F_EVP_DigestVerifyInitFromAlgorithm 155
835 #define EVP_F_EVP_DigestSignAlgorithm 156
836 #define EVP_F_rsa_digest_verify_init_from_algorithm 157
837 #define EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE 100
838 #define EVP_R_UNSUPPORTED_SIGNATURE_TYPE 101
839 #define EVP_R_INVALID_DIGEST_TYPE 102
840 #define EVP_R_EXPECTING_A_DH_KEY 103
841 #define EVP_R_OPERATON_NOT_INITIALIZED 104
842 #define EVP_R_MISSING_PARAMETERS 105
843 #define EVP_R_NO_DEFAULT_DIGEST 106
844 #define EVP_R_UNKNOWN_DIGEST 107
845 #define EVP_R_KEYS_NOT_SET 108
846 #define EVP_R_X931_UNSUPPORTED 109
847 #define EVP_R_DIGEST_DOES_NOT_MATCH 110
848 #define EVP_R_DIFFERENT_PARAMETERS 111
849 #define EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE 112
850 #define EVP_R_DIFFERENT_KEY_TYPES 113
851 #define EVP_R_NO_PARAMETERS_SET 114
852 #define EVP_R_NO_NID_FOR_CURVE 115
853 #define EVP_R_NO_OPERATION_SET 116
854 #define EVP_R_UNSUPPORTED_ALGORITHM 117
855 #define EVP_R_EXPECTING_AN_DSA_KEY 118
856 #define EVP_R_UNKNOWN_MASK_DIGEST 119
857 #define EVP_R_INVALID_SALT_LENGTH 120
858 #define EVP_R_BUFFER_TOO_SMALL 121
859 #define EVP_R_INVALID_PADDING_MODE 122
860 #define EVP_R_INVALID_MGF1_MD 123
861 #define EVP_R_SHARED_INFO_ERROR 124
862 #define EVP_R_INVALID_KEYBITS 125
863 #define EVP_R_PEER_KEY_ERROR 126
864 #define EVP_R_EXPECTING_A_DSA_KEY 127
865 #define EVP_R_UNSUPPORTED_MASK_ALGORITHM 128
866 #define EVP_R_EXPECTING_AN_EC_KEY_KEY 129
867 #define EVP_R_INVALID_TRAILER 130
868 #define EVP_R_INVALID_DIGEST_LENGTH 131
869 #define EVP_R_COMMAND_NOT_SUPPORTED 132
870 #define EVP_R_EXPLICIT_EC_PARAMETERS_NOT_SUPPORTED 133
871 #define EVP_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE 134
872 #define EVP_R_NO_MDC2_SUPPORT 135
873 #define EVP_R_INVALID_CURVE 136
874 #define EVP_R_NO_KEY_SET 137
875 #define EVP_R_INVALID_PSS_PARAMETERS 138
876 #define EVP_R_KDF_PARAMETER_ERROR 139
877 #define EVP_R_UNSUPPORTED_MASK_PARAMETER 140
878 #define EVP_R_EXPECTING_AN_RSA_KEY 141
879 #define EVP_R_INVALID_OPERATION 142
880 #define EVP_R_DECODE_ERROR 143
881 #define EVP_R_INVALID_PSS_SALTLEN 144
882 #define EVP_R_UNKNOWN_PUBLIC_KEY_TYPE 145
883 #define EVP_R_CONTEXT_NOT_INITIALISED 146
884 #define EVP_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED 147
885 #define EVP_R_WRONG_PUBLIC_KEY_TYPE 148
886 #define EVP_R_UNKNOWN_SIGNATURE_ALGORITHM 149
887 #define EVP_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM 150
889 #endif /* OPENSSL_HEADER_EVP_H */