evp.h revision 6f79a50fbad5817b8fc7a07c5657d8249c57a0f7
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/thread.h> 63 64/* 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> 69#include <openssl/base64.h> 70#include <openssl/cipher.h> 71#include <openssl/digest.h> 72#include <openssl/nid.h> 73 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 92/* EVP_PKEY_up_ref increments the reference count of |pkey| and returns one. */ 93OPENSSL_EXPORT int EVP_PKEY_up_ref(EVP_PKEY *pkey); 94 95/* 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 100/* 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 107/* 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 115/* 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. */ 119OPENSSL_EXPORT int EVP_PKEY_size(const EVP_PKEY *pkey); 120 121/* 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. */ 124OPENSSL_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 130/* EVP_PKEY_type returns |nid| if |nid| is a known key type and |NID_undef| 131 * otherwise. */ 132OPENSSL_EXPORT int EVP_PKEY_type(int nid); 133 134 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| 140 * 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. */ 144 145OPENSSL_EXPORT int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key); 146OPENSSL_EXPORT int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key); 147OPENSSL_EXPORT RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey); 148OPENSSL_EXPORT RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey); 149 150OPENSSL_EXPORT int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key); 151OPENSSL_EXPORT int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key); 152OPENSSL_EXPORT DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey); 153OPENSSL_EXPORT DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey); 154 155OPENSSL_EXPORT int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key); 156OPENSSL_EXPORT int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key); 157OPENSSL_EXPORT EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey); 158OPENSSL_EXPORT EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey); 159 160#define EVP_PKEY_NONE NID_undef 161#define EVP_PKEY_RSA NID_rsaEncryption 162#define EVP_PKEY_DSA NID_dsa 163#define EVP_PKEY_EC NID_X9_62_id_ecPublicKey 164 165/* EVP_PKEY_assign sets the underlying key of |pkey| to |key|, which must be of 166 * the given type. The |type| argument should be one of the |EVP_PKEY_*| 167 * values. */ 168OPENSSL_EXPORT int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key); 169 170/* EVP_PKEY_set_type sets the type of |pkey| to |type|, which should be one of 171 * the |EVP_PKEY_*| values. It returns one if successful or zero otherwise. If 172 * |pkey| is NULL, it simply reports whether the type is known. */ 173OPENSSL_EXPORT int EVP_PKEY_set_type(EVP_PKEY *pkey, int type); 174 175/* EVP_PKEY_cmp_parameters compares the parameters of |a| and |b|. It returns 176 * one if they match, zero if not, or a negative number of on error. 177 * 178 * WARNING: the return value differs from the usual return value convention. */ 179OPENSSL_EXPORT int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, 180 const EVP_PKEY *b); 181 182 183/* ASN.1 functions */ 184 185/* EVP_parse_public_key decodes a DER-encoded SubjectPublicKeyInfo structure 186 * (RFC 5280) from |cbs| and advances |cbs|. It returns a newly-allocated 187 * |EVP_PKEY| or NULL on error. 188 * 189 * The caller must check the type of the parsed public key to ensure it is 190 * suitable and validate other desired key properties such as RSA modulus size 191 * or EC curve. */ 192OPENSSL_EXPORT EVP_PKEY *EVP_parse_public_key(CBS *cbs); 193 194/* EVP_marshal_public_key marshals |key| as a DER-encoded SubjectPublicKeyInfo 195 * structure (RFC 5280) and appends the result to |cbb|. It returns one on 196 * success and zero on error. */ 197OPENSSL_EXPORT int EVP_marshal_public_key(CBB *cbb, const EVP_PKEY *key); 198 199/* EVP_parse_private_key decodes a DER-encoded PrivateKeyInfo structure (RFC 200 * 5208) from |cbs| and advances |cbs|. It returns a newly-allocated |EVP_PKEY| 201 * or NULL on error. 202 * 203 * The caller must check the type of the parsed private key to ensure it is 204 * suitable and validate other desired key properties such as RSA modulus size 205 * or EC curve. 206 * 207 * A PrivateKeyInfo ends with an optional set of attributes. These are not 208 * processed and so this function will silently ignore any trailing data in the 209 * structure. */ 210OPENSSL_EXPORT EVP_PKEY *EVP_parse_private_key(CBS *cbs); 211 212/* EVP_marshal_private_key marshals |key| as a DER-encoded PrivateKeyInfo 213 * structure (RFC 5208) and appends the result to |cbb|. It returns one on 214 * success and zero on error. */ 215OPENSSL_EXPORT int EVP_marshal_private_key(CBB *cbb, const EVP_PKEY *key); 216 217 218/* Signing */ 219 220/* EVP_DigestSignInit sets up |ctx| for a signing operation with |type| and 221 * |pkey|. The |ctx| argument must have been initialised with 222 * |EVP_MD_CTX_init|. If |pctx| is not NULL, the |EVP_PKEY_CTX| of the signing 223 * operation will be written to |*pctx|; this can be used to set alternative 224 * signing options. 225 * 226 * It returns one on success, or zero on error. */ 227OPENSSL_EXPORT int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, 228 const EVP_MD *type, ENGINE *e, 229 EVP_PKEY *pkey); 230 231/* EVP_DigestSignUpdate appends |len| bytes from |data| to the data which will 232 * be signed in |EVP_DigestSignFinal|. It returns one. */ 233OPENSSL_EXPORT int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, 234 size_t len); 235 236/* EVP_DigestSignFinal signs the data that has been included by one or more 237 * calls to |EVP_DigestSignUpdate|. If |out_sig| is NULL then |*out_sig_len| is 238 * set to the maximum number of output bytes. Otherwise, on entry, 239 * |*out_sig_len| must contain the length of the |out_sig| buffer. If the call 240 * is successful, the signature is written to |out_sig| and |*out_sig_len| is 241 * set to its length. 242 * 243 * It returns one on success, or zero on error. */ 244OPENSSL_EXPORT int EVP_DigestSignFinal(EVP_MD_CTX *ctx, uint8_t *out_sig, 245 size_t *out_sig_len); 246 247 248/* Verifying */ 249 250/* EVP_DigestVerifyInit sets up |ctx| for a signature verification operation 251 * with |type| and |pkey|. The |ctx| argument must have been initialised with 252 * |EVP_MD_CTX_init|. If |pctx| is not NULL, the |EVP_PKEY_CTX| of the signing 253 * operation will be written to |*pctx|; this can be used to set alternative 254 * signing options. 255 * 256 * It returns one on success, or zero on error. */ 257OPENSSL_EXPORT int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, 258 const EVP_MD *type, ENGINE *e, 259 EVP_PKEY *pkey); 260 261/* EVP_DigestVerifyUpdate appends |len| bytes from |data| to the data which 262 * will be verified by |EVP_DigestVerifyFinal|. It returns one. */ 263OPENSSL_EXPORT int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, 264 size_t len); 265 266/* EVP_DigestVerifyFinal verifies that |sig_len| bytes of |sig| are a valid 267 * signature for the data that has been included by one or more calls to 268 * |EVP_DigestVerifyUpdate|. It returns one on success and zero otherwise. */ 269OPENSSL_EXPORT int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig, 270 size_t sig_len); 271 272 273/* Signing (old functions) */ 274 275/* EVP_SignInit_ex configures |ctx|, which must already have been initialised, 276 * for a fresh signing operation using the hash function |type|. It returns one 277 * on success and zero otherwise. 278 * 279 * (In order to initialise |ctx|, either obtain it initialised with 280 * |EVP_MD_CTX_create|, or use |EVP_MD_CTX_init|.) */ 281OPENSSL_EXPORT int EVP_SignInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, 282 ENGINE *impl); 283 284/* EVP_SignInit is a deprecated version of |EVP_SignInit_ex|. 285 * 286 * TODO(fork): remove. */ 287OPENSSL_EXPORT int EVP_SignInit(EVP_MD_CTX *ctx, const EVP_MD *type); 288 289/* EVP_SignUpdate appends |len| bytes from |data| to the data which will be 290 * signed in |EVP_SignFinal|. */ 291OPENSSL_EXPORT int EVP_SignUpdate(EVP_MD_CTX *ctx, const void *data, 292 size_t len); 293 294/* EVP_SignFinal signs the data that has been included by one or more calls to 295 * |EVP_SignUpdate|, using the key |pkey|, and writes it to |sig|. On entry, 296 * |sig| must point to at least |EVP_PKEY_size(pkey)| bytes of space. The 297 * actual size of the signature is written to |*out_sig_len|. 298 * 299 * It returns one on success and zero otherwise. 300 * 301 * It does not modify |ctx|, thus it's possible to continue to use |ctx| in 302 * order to sign a longer message. */ 303OPENSSL_EXPORT int EVP_SignFinal(const EVP_MD_CTX *ctx, uint8_t *sig, 304 unsigned int *out_sig_len, EVP_PKEY *pkey); 305 306 307/* Verifying (old functions) */ 308 309/* EVP_VerifyInit_ex configures |ctx|, which must already have been 310 * initialised, for a fresh signature verification operation using the hash 311 * function |type|. It returns one on success and zero otherwise. 312 * 313 * (In order to initialise |ctx|, either obtain it initialised with 314 * |EVP_MD_CTX_create|, or use |EVP_MD_CTX_init|.) */ 315OPENSSL_EXPORT int EVP_VerifyInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, 316 ENGINE *impl); 317 318/* EVP_VerifyInit is a deprecated version of |EVP_VerifyInit_ex|. 319 * 320 * TODO(fork): remove. */ 321OPENSSL_EXPORT int EVP_VerifyInit(EVP_MD_CTX *ctx, const EVP_MD *type); 322 323/* EVP_VerifyUpdate appends |len| bytes from |data| to the data which will be 324 * signed in |EVP_VerifyFinal|. */ 325OPENSSL_EXPORT int EVP_VerifyUpdate(EVP_MD_CTX *ctx, const void *data, 326 size_t len); 327 328/* EVP_VerifyFinal verifies that |sig_len| bytes of |sig| are a valid 329 * signature, by |pkey|, for the data that has been included by one or more 330 * calls to |EVP_VerifyUpdate|. 331 * 332 * It returns one on success and zero otherwise. 333 * 334 * It does not modify |ctx|, thus it's possible to continue to use |ctx| in 335 * order to sign a longer message. */ 336OPENSSL_EXPORT int EVP_VerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig, 337 size_t sig_len, EVP_PKEY *pkey); 338 339 340/* Printing */ 341 342/* EVP_PKEY_print_public prints a textual representation of the public key in 343 * |pkey| to |out|. Returns one on success or zero otherwise. */ 344OPENSSL_EXPORT int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, 345 int indent, ASN1_PCTX *pctx); 346 347/* EVP_PKEY_print_private prints a textual representation of the private key in 348 * |pkey| to |out|. Returns one on success or zero otherwise. */ 349OPENSSL_EXPORT int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, 350 int indent, ASN1_PCTX *pctx); 351 352/* EVP_PKEY_print_params prints a textual representation of the parameters in 353 * |pkey| to |out|. Returns one on success or zero otherwise. */ 354OPENSSL_EXPORT int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, 355 int indent, ASN1_PCTX *pctx); 356 357 358/* Password stretching. 359 * 360 * Password stretching functions take a low-entropy password and apply a slow 361 * function that results in a key suitable for use in symmetric 362 * cryptography. */ 363 364/* PKCS5_PBKDF2_HMAC computes |iterations| iterations of PBKDF2 of |password| 365 * and |salt|, using |digest|, and outputs |key_len| bytes to |out_key|. It 366 * returns one on success and zero on error. */ 367OPENSSL_EXPORT int PKCS5_PBKDF2_HMAC(const char *password, size_t password_len, 368 const uint8_t *salt, size_t salt_len, 369 unsigned iterations, const EVP_MD *digest, 370 size_t key_len, uint8_t *out_key); 371 372/* PKCS5_PBKDF2_HMAC_SHA1 is the same as PKCS5_PBKDF2_HMAC, but with |digest| 373 * fixed to |EVP_sha1|. */ 374OPENSSL_EXPORT int PKCS5_PBKDF2_HMAC_SHA1(const char *password, 375 size_t password_len, 376 const uint8_t *salt, size_t salt_len, 377 unsigned iterations, size_t key_len, 378 uint8_t *out_key); 379 380 381/* Public key contexts. 382 * 383 * |EVP_PKEY_CTX| objects hold the context of an operation (e.g. signing or 384 * encrypting) that uses a public key. */ 385 386/* EVP_PKEY_CTX_new allocates a fresh |EVP_PKEY_CTX| for use with |pkey|. It 387 * returns the context or NULL on error. */ 388OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e); 389 390/* EVP_PKEY_CTX_new_id allocates a fresh |EVP_PKEY_CTX| for a key of type |id| 391 * (e.g. |EVP_PKEY_HMAC|). This can be used for key generation where 392 * |EVP_PKEY_CTX_new| can't be used because there isn't an |EVP_PKEY| to pass 393 * it. It returns the context or NULL on error. */ 394OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e); 395 396/* EVP_PKEY_CTX_free frees |ctx| and the data it owns. */ 397OPENSSL_EXPORT void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx); 398 399/* EVP_PKEY_CTX_dup allocates a fresh |EVP_PKEY_CTX| and sets it equal to the 400 * state of |ctx|. It returns the fresh |EVP_PKEY_CTX| or NULL on error. */ 401OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *ctx); 402 403/* EVP_PKEY_CTX_get0_pkey returns the |EVP_PKEY| associated with |ctx|. */ 404OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx); 405 406/* EVP_PKEY_sign_init initialises an |EVP_PKEY_CTX| for a signing operation. It 407 * should be called before |EVP_PKEY_sign|. 408 * 409 * It returns one on success or zero on error. */ 410OPENSSL_EXPORT int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx); 411 412/* EVP_PKEY_sign signs |data_len| bytes from |data| using |ctx|. If |sig| is 413 * NULL, the maximum size of the signature is written to 414 * |out_sig_len|. Otherwise, |*sig_len| must contain the number of bytes of 415 * space available at |sig|. If sufficient, the signature will be written to 416 * |sig| and |*sig_len| updated with the true length. 417 * 418 * WARNING: Setting |sig| to NULL only gives the maximum size of the 419 * signature. The actual signature may be smaller. 420 * 421 * It returns one on success or zero on error. (Note: this differs from 422 * OpenSSL, which can also return negative values to indicate an error. ) */ 423OPENSSL_EXPORT int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, 424 size_t *sig_len, const uint8_t *data, 425 size_t data_len); 426 427/* EVP_PKEY_verify_init initialises an |EVP_PKEY_CTX| for a signature 428 * verification operation. It should be called before |EVP_PKEY_verify|. 429 * 430 * It returns one on success or zero on error. */ 431OPENSSL_EXPORT int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx); 432 433/* EVP_PKEY_verify verifies that |sig_len| bytes from |sig| are a valid 434 * signature for |data|. 435 * 436 * It returns one on success or zero on error. */ 437OPENSSL_EXPORT int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, 438 size_t sig_len, const uint8_t *data, 439 size_t data_len); 440 441/* EVP_PKEY_encrypt_init initialises an |EVP_PKEY_CTX| for an encryption 442 * operation. It should be called before |EVP_PKEY_encrypt|. 443 * 444 * It returns one on success or zero on error. */ 445OPENSSL_EXPORT int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx); 446 447/* EVP_PKEY_encrypt encrypts |in_len| bytes from |in|. If |out| is NULL, the 448 * maximum size of the ciphertext is written to |out_len|. Otherwise, |*out_len| 449 * must contain the number of bytes of space available at |out|. If sufficient, 450 * the ciphertext will be written to |out| and |*out_len| updated with the true 451 * length. 452 * 453 * WARNING: Setting |out| to NULL only gives the maximum size of the 454 * ciphertext. The actual ciphertext may be smaller. 455 * 456 * It returns one on success or zero on error. */ 457OPENSSL_EXPORT int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, 458 size_t *out_len, const uint8_t *in, 459 size_t in_len); 460 461/* EVP_PKEY_decrypt_init initialises an |EVP_PKEY_CTX| for a decryption 462 * operation. It should be called before |EVP_PKEY_decrypt|. 463 * 464 * It returns one on success or zero on error. */ 465OPENSSL_EXPORT int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx); 466 467/* EVP_PKEY_decrypt decrypts |in_len| bytes from |in|. If |out| is NULL, the 468 * maximum size of the plaintext is written to |out_len|. Otherwise, |*out_len| 469 * must contain the number of bytes of space available at |out|. If sufficient, 470 * the ciphertext will be written to |out| and |*out_len| updated with the true 471 * length. 472 * 473 * WARNING: Setting |out| to NULL only gives the maximum size of the 474 * plaintext. The actual plaintext may be smaller. 475 * 476 * It returns one on success or zero on error. */ 477OPENSSL_EXPORT int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out, 478 size_t *out_len, const uint8_t *in, 479 size_t in_len); 480 481/* EVP_PKEY_verify_recover_init initialises an |EVP_PKEY_CTX| for a public-key 482 * decryption operation. It should be called before |EVP_PKEY_verify_recover|. 483 * 484 * Public-key decryption is a very obscure operation that is only implemented 485 * by RSA keys. It is effectively a signature verification operation that 486 * returns the signed message directly. It is almost certainly not what you 487 * want. 488 * 489 * It returns one on success or zero on error. */ 490OPENSSL_EXPORT int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx); 491 492/* EVP_PKEY_verify_recover decrypts |sig_len| bytes from |sig|. If |out| is 493 * NULL, the maximum size of the plaintext is written to |out_len|. Otherwise, 494 * |*out_len| must contain the number of bytes of space available at |out|. If 495 * sufficient, the ciphertext will be written to |out| and |*out_len| updated 496 * with the true length. 497 * 498 * WARNING: Setting |out| to NULL only gives the maximum size of the 499 * plaintext. The actual plaintext may be smaller. 500 * 501 * See the warning about this operation in |EVP_PKEY_verify_recover_init|. It 502 * is probably not what you want. 503 * 504 * It returns one on success or zero on error. */ 505OPENSSL_EXPORT int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out, 506 size_t *out_len, const uint8_t *sig, 507 size_t siglen); 508 509/* EVP_PKEY_derive_init initialises an |EVP_PKEY_CTX| for a key derivation 510 * operation. It should be called before |EVP_PKEY_derive_set_peer| and 511 * |EVP_PKEY_derive|. 512 * 513 * It returns one on success or zero on error. */ 514OPENSSL_EXPORT int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx); 515 516/* EVP_PKEY_derive_set_peer sets the peer's key to be used for key derivation 517 * by |ctx| to |peer|. It should be called after |EVP_PKEY_derive_init|. (For 518 * example, this is used to set the peer's key in (EC)DH.) It returns one on 519 * success and zero on error. */ 520OPENSSL_EXPORT int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer); 521 522/* EVP_PKEY_derive derives a shared key between the two keys configured in 523 * |ctx|. If |key| is non-NULL then, on entry, |out_key_len| must contain the 524 * amount of space at |key|. If sufficient then the shared key will be written 525 * to |key| and |*out_key_len| will be set to the length. If |key| is NULL then 526 * |out_key_len| will be set to the maximum length. 527 * 528 * WARNING: Setting |out| to NULL only gives the maximum size of the key. The 529 * actual key may be smaller. 530 * 531 * It returns one on success and zero on error. */ 532OPENSSL_EXPORT int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, uint8_t *key, 533 size_t *out_key_len); 534 535/* EVP_PKEY_keygen_init initialises an |EVP_PKEY_CTX| for a key generation 536 * operation. It should be called before |EVP_PKEY_keygen|. 537 * 538 * It returns one on success or zero on error. */ 539OPENSSL_EXPORT int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx); 540 541/* EVP_PKEY_keygen performs a key generation operation using the values from 542 * |ctx| and sets |*ppkey| to a fresh |EVP_PKEY| containing the resulting key. 543 * It returns one on success or zero on error. */ 544OPENSSL_EXPORT int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey); 545 546 547/* Generic control functions. */ 548 549/* EVP_PKEY_CTX_set_signature_md sets |md| as the digest to be used in a 550 * signature operation. It returns one on success or zero on error. */ 551OPENSSL_EXPORT int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, 552 const EVP_MD *md); 553 554/* EVP_PKEY_CTX_get_signature_md sets |*out_md| to the digest to be used in a 555 * signature operation. It returns one on success or zero on error. */ 556OPENSSL_EXPORT int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, 557 const EVP_MD **out_md); 558 559 560/* RSA specific control functions. */ 561 562/* EVP_PKEY_CTX_set_rsa_padding sets the padding type to use. It should be one 563 * of the |RSA_*_PADDING| values. Returns one on success or zero on error. */ 564OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int padding); 565 566/* EVP_PKEY_CTX_get_rsa_padding sets |*out_padding| to the current padding 567 * value, which is one of the |RSA_*_PADDING| values. Returns one on success or 568 * zero on error. */ 569OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, 570 int *out_padding); 571 572/* EVP_PKEY_CTX_set_rsa_pss_saltlen sets the length of the salt in a PSS-padded 573 * signature. A value of -1 cause the salt to be the same length as the digest 574 * in the signature. A value of -2 causes the salt to be the maximum length 575 * that will fit when signing and recovered from the signature when verifying. 576 * Otherwise the value gives the size of the salt in bytes. 577 * 578 * If unsure, use -1. 579 * 580 * Returns one on success or zero on error. */ 581OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, 582 int salt_len); 583 584/* EVP_PKEY_CTX_get_rsa_pss_saltlen sets |*out_salt_len| to the salt length of 585 * a PSS-padded signature. See the documentation for 586 * |EVP_PKEY_CTX_set_rsa_pss_saltlen| for details of the special values that it 587 * can take. 588 * 589 * Returns one on success or zero on error. */ 590OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, 591 int *out_salt_len); 592 593/* EVP_PKEY_CTX_set_rsa_keygen_bits sets the size of the desired RSA modulus, 594 * in bits, for key generation. Returns one on success or zero on 595 * error. */ 596OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, 597 int bits); 598 599/* EVP_PKEY_CTX_set_rsa_keygen_pubexp sets |e| as the public exponent for key 600 * generation. Returns one on success or zero on error. */ 601OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, 602 BIGNUM *e); 603 604/* EVP_PKEY_CTX_set_rsa_oaep_md sets |md| as the digest used in OAEP padding. 605 * Returns one on success or zero on error. */ 606OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, 607 const EVP_MD *md); 608 609/* EVP_PKEY_CTX_get_rsa_oaep_md sets |*out_md| to the digest function used in 610 * OAEP padding. Returns one on success or zero on error. */ 611OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, 612 const EVP_MD **out_md); 613 614/* EVP_PKEY_CTX_set_rsa_mgf1_md sets |md| as the digest used in MGF1. Returns 615 * one on success or zero on error. */ 616OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, 617 const EVP_MD *md); 618 619/* EVP_PKEY_CTX_get_rsa_mgf1_md sets |*out_md| to the digest function used in 620 * MGF1. Returns one on success or zero on error. */ 621OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, 622 const EVP_MD **out_md); 623 624/* EVP_PKEY_CTX_set0_rsa_oaep_label sets |label_len| bytes from |label| as the 625 * label used in OAEP. DANGER: On success, this call takes ownership of |label| 626 * and will call |OPENSSL_free| on it when |ctx| is destroyed. 627 * 628 * Returns one on success or zero on error. */ 629OPENSSL_EXPORT int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, 630 uint8_t *label, 631 size_t label_len); 632 633/* EVP_PKEY_CTX_get0_rsa_oaep_label sets |*out_label| to point to the internal 634 * buffer containing the OAEP label (which may be NULL) and returns the length 635 * of the label or a negative value on error. 636 * 637 * WARNING: the return value differs from the usual return value convention. */ 638OPENSSL_EXPORT int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, 639 const uint8_t **out_label); 640 641 642/* Deprecated functions. */ 643 644/* EVP_PKEY_DH is defined for compatibility, but it is impossible to create an 645 * |EVP_PKEY| of that type. */ 646#define EVP_PKEY_DH NID_dhKeyAgreement 647 648/* EVP_PKEY_RSA2 was historically an alternate form for RSA public keys (OID 649 * 2.5.8.1.1), but is no longer accepted. */ 650#define EVP_PKEY_RSA2 NID_rsa 651 652/* OpenSSL_add_all_algorithms does nothing. */ 653OPENSSL_EXPORT void OpenSSL_add_all_algorithms(void); 654 655/* OPENSSL_add_all_algorithms_conf does nothing. */ 656OPENSSL_EXPORT void OPENSSL_add_all_algorithms_conf(void); 657 658/* OpenSSL_add_all_ciphers does nothing. */ 659OPENSSL_EXPORT void OpenSSL_add_all_ciphers(void); 660 661/* OpenSSL_add_all_digests does nothing. */ 662OPENSSL_EXPORT void OpenSSL_add_all_digests(void); 663 664/* EVP_cleanup does nothing. */ 665OPENSSL_EXPORT void EVP_cleanup(void); 666 667OPENSSL_EXPORT void EVP_CIPHER_do_all_sorted( 668 void (*callback)(const EVP_CIPHER *cipher, const char *name, 669 const char *unused, void *arg), 670 void *arg); 671 672OPENSSL_EXPORT void EVP_MD_do_all_sorted(void (*callback)(const EVP_MD *cipher, 673 const char *name, 674 const char *unused, 675 void *arg), 676 void *arg); 677 678/* i2d_PrivateKey marshals a private key from |key| to an ASN.1, DER 679 * structure. If |outp| is not NULL then the result is written to |*outp| and 680 * |*outp| is advanced just past the output. It returns the number of bytes in 681 * the result, whether written or not, or a negative value on error. 682 * 683 * RSA keys are serialized as a DER-encoded RSAPublicKey (RFC 3447) structure. 684 * EC keys are serialized as a DER-encoded ECPrivateKey (RFC 5915) structure. 685 * 686 * Use |RSA_marshal_private_key| or |EC_marshal_private_key| instead. */ 687OPENSSL_EXPORT int i2d_PrivateKey(const EVP_PKEY *key, uint8_t **outp); 688 689/* i2d_PublicKey marshals a public key from |key| to a type-specific format. 690 * If |outp| is not NULL then the result is written to |*outp| and 691 * |*outp| is advanced just past the output. It returns the number of bytes in 692 * the result, whether written or not, or a negative value on error. 693 * 694 * RSA keys are serialized as a DER-encoded RSAPublicKey (RFC 3447) structure. 695 * EC keys are serialized as an EC point per SEC 1. 696 * 697 * Use |RSA_marshal_public_key| or |EC_POINT_point2cbb| instead. */ 698OPENSSL_EXPORT int i2d_PublicKey(EVP_PKEY *key, uint8_t **outp); 699 700/* d2i_PrivateKey parses an ASN.1, DER-encoded, private key from |len| bytes at 701 * |*inp|. If |out| is not NULL then, on exit, a pointer to the result is in 702 * |*out|. Note that, even if |*out| is already non-NULL on entry, it will not 703 * be written to. Rather, a fresh |EVP_PKEY| is allocated and the previous one 704 * is freed. On successful exit, |*inp| is advanced past the DER structure. It 705 * returns the result or NULL on error. 706 * 707 * This function tries to detect one of several formats. Instead, use 708 * |EVP_parse_private_key| for a PrivateKeyInfo, |RSA_parse_private_key| for an 709 * RSAPrivateKey, and |EC_parse_private_key| for an ECPrivateKey. */ 710OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **out, 711 const uint8_t **inp, long len); 712 713/* d2i_AutoPrivateKey acts the same as |d2i_PrivateKey|, but detects the type 714 * of the private key. 715 * 716 * This function tries to detect one of several formats. Instead, use 717 * |EVP_parse_private_key| for a PrivateKeyInfo, |RSA_parse_private_key| for an 718 * RSAPrivateKey, and |EC_parse_private_key| for an ECPrivateKey. */ 719OPENSSL_EXPORT EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **out, const uint8_t **inp, 720 long len); 721 722/* EVP_PKEY_get0_DH returns NULL. */ 723OPENSSL_EXPORT DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey); 724 725 726/* Private structures. */ 727 728struct evp_pkey_st { 729 CRYPTO_refcount_t references; 730 731 /* type contains one of the EVP_PKEY_* values or NID_undef and determines 732 * which element (if any) of the |pkey| union is valid. */ 733 int type; 734 735 union { 736 char *ptr; 737 RSA *rsa; 738 DSA *dsa; 739 DH *dh; 740 EC_KEY *ec; 741 } pkey; 742 743 /* ameth contains a pointer to a method table that contains many ASN.1 744 * methods for the key type. */ 745 const EVP_PKEY_ASN1_METHOD *ameth; 746} /* EVP_PKEY */; 747 748 749#if defined(__cplusplus) 750} /* extern C */ 751 752extern "C++" { 753namespace bssl { 754 755BORINGSSL_MAKE_DELETER(EVP_PKEY, EVP_PKEY_free) 756BORINGSSL_MAKE_DELETER(EVP_PKEY_CTX, EVP_PKEY_CTX_free) 757 758} // namespace bssl 759 760} /* extern C++ */ 761 762#endif 763 764#define EVP_R_BUFFER_TOO_SMALL 100 765#define EVP_R_COMMAND_NOT_SUPPORTED 101 766#define EVP_R_DECODE_ERROR 102 767#define EVP_R_DIFFERENT_KEY_TYPES 103 768#define EVP_R_DIFFERENT_PARAMETERS 104 769#define EVP_R_ENCODE_ERROR 105 770#define EVP_R_EXPECTING_AN_EC_KEY_KEY 106 771#define EVP_R_EXPECTING_AN_RSA_KEY 107 772#define EVP_R_EXPECTING_A_DSA_KEY 108 773#define EVP_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE 109 774#define EVP_R_INVALID_DIGEST_LENGTH 110 775#define EVP_R_INVALID_DIGEST_TYPE 111 776#define EVP_R_INVALID_KEYBITS 112 777#define EVP_R_INVALID_MGF1_MD 113 778#define EVP_R_INVALID_OPERATION 114 779#define EVP_R_INVALID_PADDING_MODE 115 780#define EVP_R_INVALID_PSS_SALTLEN 116 781#define EVP_R_KEYS_NOT_SET 117 782#define EVP_R_MISSING_PARAMETERS 118 783#define EVP_R_NO_DEFAULT_DIGEST 119 784#define EVP_R_NO_KEY_SET 120 785#define EVP_R_NO_MDC2_SUPPORT 121 786#define EVP_R_NO_NID_FOR_CURVE 122 787#define EVP_R_NO_OPERATION_SET 123 788#define EVP_R_NO_PARAMETERS_SET 124 789#define EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE 125 790#define EVP_R_OPERATON_NOT_INITIALIZED 126 791#define EVP_R_UNKNOWN_PUBLIC_KEY_TYPE 127 792#define EVP_R_UNSUPPORTED_ALGORITHM 128 793#define EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE 129 794 795#endif /* OPENSSL_HEADER_EVP_H */ 796