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/cipher.h> 70 #include <openssl/digest.h> 71 #include <openssl/obj.h> 72 73 #if defined(__cplusplus) 74 extern "C" { 75 #endif 76 77 78 /* EVP abstracts over public/private key algorithms. */ 79 80 81 /* Public key objects. */ 82 83 /* EVP_PKEY_new creates a new, empty public-key object and returns it or NULL 84 * on allocation failure. */ 85 OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_new(void); 86 87 /* EVP_PKEY_free frees all data referenced by |pkey| and then frees |pkey| 88 * itself. */ 89 OPENSSL_EXPORT void EVP_PKEY_free(EVP_PKEY *pkey); 90 91 /* EVP_PKEY_up_ref increments the reference count of |pkey| and returns it. */ 92 OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_up_ref(EVP_PKEY *pkey); 93 94 /* EVP_PKEY_is_opaque returns one if |pkey| is opaque. Opaque keys are backed by 95 * custom implementations which do not expose key material and parameters. It is 96 * an error to attempt to duplicate, export, or compare an opaque key. */ 97 OPENSSL_EXPORT int EVP_PKEY_is_opaque(const EVP_PKEY *pkey); 98 99 /* EVP_PKEY_supports_digest returns one if |pkey| supports digests of 100 * type |md|. This is intended for use with EVP_PKEYs backing custom 101 * implementations which can't sign all digests. */ 102 OPENSSL_EXPORT int EVP_PKEY_supports_digest(const EVP_PKEY *pkey, 103 const EVP_MD *md); 104 105 /* EVP_PKEY_cmp compares |a| and |b| and returns one if they are equal, zero if 106 * not and a negative number on error. 107 * 108 * WARNING: this differs from the traditional return value of a "cmp" 109 * function. */ 110 OPENSSL_EXPORT int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b); 111 112 /* EVP_PKEY_copy_parameters sets the parameters of |to| to equal the parameters 113 * of |from|. It returns one on success and zero on error. */ 114 OPENSSL_EXPORT int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from); 115 116 /* EVP_PKEY_missing_parameters returns one if |pkey| is missing needed 117 * parameters or zero if not, or if the algorithm doesn't take parameters. */ 118 OPENSSL_EXPORT int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey); 119 120 /* EVP_PKEY_size returns the maximum size, in bytes, of a signature signed by 121 * |pkey|. For an RSA key, this returns the number of bytes needed to represent 122 * the modulus. For an EC key, this returns the maximum size of a DER-encoded 123 * ECDSA signature. */ 124 OPENSSL_EXPORT int EVP_PKEY_size(const EVP_PKEY *pkey); 125 126 /* EVP_PKEY_bits returns the "size", in bits, of |pkey|. For an RSA key, this 127 * returns the bit length of the modulus. For an EC key, this returns the bit 128 * length of the group order. */ 129 OPENSSL_EXPORT int EVP_PKEY_bits(EVP_PKEY *pkey); 130 131 /* EVP_PKEY_id returns the type of |pkey|, which is one of the |EVP_PKEY_*| 132 * values. */ 133 OPENSSL_EXPORT int EVP_PKEY_id(const EVP_PKEY *pkey); 134 135 /* EVP_PKEY_type returns a canonicalised form of |NID|. For example, 136 * |EVP_PKEY_RSA2| will be turned into |EVP_PKEY_RSA|. */ 137 OPENSSL_EXPORT int EVP_PKEY_type(int nid); 138 139 /* Deprecated: EVP_PKEY_new_mac_key allocates a fresh |EVP_PKEY| of the given 140 * type (e.g. |EVP_PKEY_HMAC|), sets |mac_key| as the MAC key and "generates" a 141 * new key, suitable for signing. It returns the fresh |EVP_PKEY|, or NULL on 142 * error. Use |HMAC_CTX| directly instead. */ 143 OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *engine, 144 const uint8_t *mac_key, 145 size_t mac_key_len); 146 147 148 /* Getting and setting concrete public key types. 149 * 150 * The following functions get and set the underlying public key in an 151 * |EVP_PKEY| object. The |set1| functions take an additional reference to the 152 * underlying key and return one on success or zero on error. The |assign| 153 * functions adopt the caller's reference. The getters return a fresh reference 154 * to the underlying object. */ 155 156 OPENSSL_EXPORT int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key); 157 OPENSSL_EXPORT int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key); 158 OPENSSL_EXPORT RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey); 159 160 OPENSSL_EXPORT int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, struct dsa_st *key); 161 OPENSSL_EXPORT int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key); 162 OPENSSL_EXPORT struct dsa_st *EVP_PKEY_get1_DSA(EVP_PKEY *pkey); 163 164 OPENSSL_EXPORT int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, struct ec_key_st *key); 165 OPENSSL_EXPORT int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key); 166 OPENSSL_EXPORT struct ec_key_st *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey); 167 168 OPENSSL_EXPORT int EVP_PKEY_set1_DH(EVP_PKEY *pkey, struct dh_st *key); 169 OPENSSL_EXPORT int EVP_PKEY_assign_DH(EVP_PKEY *pkey, DH *key); 170 OPENSSL_EXPORT struct dh_st *EVP_PKEY_get1_DH(EVP_PKEY *pkey); 171 172 #define EVP_PKEY_NONE NID_undef 173 #define EVP_PKEY_RSA NID_rsaEncryption 174 #define EVP_PKEY_RSA2 NID_rsa 175 #define EVP_PKEY_DSA NID_dsa 176 #define EVP_PKEY_DH NID_dhKeyAgreement 177 #define EVP_PKEY_DHX NID_dhpublicnumber 178 #define EVP_PKEY_EC NID_X9_62_id_ecPublicKey 179 180 /* Deprecated: Use |HMAC_CTX| directly instead. */ 181 #define EVP_PKEY_HMAC NID_hmac 182 183 /* EVP_PKEY_assign sets the underlying key of |pkey| to |key|, which must be of 184 * the given type. The |type| argument should be one of the |EVP_PKEY_*| 185 * values. */ 186 OPENSSL_EXPORT int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key); 187 188 /* EVP_PKEY_set_type sets the type of |pkey| to |type|, which should be one of 189 * the |EVP_PKEY_*| values. It returns one if sucessful or zero otherwise. If 190 * |pkey| is NULL, it simply reports whether the type is known. */ 191 OPENSSL_EXPORT int EVP_PKEY_set_type(EVP_PKEY *pkey, int type); 192 193 /* EVP_PKEY_cmp_parameters compares the parameters of |a| and |b|. It returns 194 * one if they match, zero if not, or a negative number of on error. 195 * 196 * WARNING: the return value differs from the usual return value convention. */ 197 OPENSSL_EXPORT int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, 198 const EVP_PKEY *b); 199 200 201 /* ASN.1 functions */ 202 203 /* d2i_PrivateKey parses an ASN.1, DER-encoded, private key from |len| bytes at 204 * |*inp|. If |out| is not NULL then, on exit, a pointer to the result is in 205 * |*out|. If |*out| is already non-NULL on entry then the result is written 206 * directly into |*out|, otherwise a fresh |EVP_PKEY| is allocated. On 207 * successful exit, |*inp| is advanced past the DER structure. It returns the 208 * result or NULL on error. */ 209 OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **out, 210 const uint8_t **inp, long len); 211 212 /* d2i_AutoPrivateKey acts the same as |d2i_PrivateKey|, but detects the type 213 * of the private key. */ 214 OPENSSL_EXPORT EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **out, const uint8_t **inp, 215 long len); 216 217 /* i2d_PrivateKey marshals a private key from |key| to an ASN.1, DER 218 * structure. If |outp| is not NULL then the result is written to |*outp| and 219 * |*outp| is advanced just past the output. It returns the number of bytes in 220 * the result, whether written or not, or a negative value on error. */ 221 OPENSSL_EXPORT int i2d_PrivateKey(const EVP_PKEY *key, uint8_t **outp); 222 223 /* i2d_PublicKey marshals a public key from |key| to an ASN.1, DER 224 * structure. If |outp| is not NULL then the result is written to |*outp| and 225 * |*outp| is advanced just past the output. It returns the number of bytes in 226 * the result, whether written or not, or a negative value on error. */ 227 OPENSSL_EXPORT int i2d_PublicKey(EVP_PKEY *key, uint8_t **outp); 228 229 230 /* Signing */ 231 232 /* EVP_DigestSignInit sets up |ctx| for a signing operation with |type| and 233 * |pkey|. The |ctx| argument must have been initialised with 234 * |EVP_MD_CTX_init|. If |pctx| is not NULL, the |EVP_PKEY_CTX| of the signing 235 * operation will be written to |*pctx|; this can be used to set alternative 236 * signing options. 237 * 238 * It returns one on success, or zero on error. */ 239 OPENSSL_EXPORT int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, 240 const EVP_MD *type, ENGINE *e, 241 EVP_PKEY *pkey); 242 243 /* EVP_DigestSignUpdate appends |len| bytes from |data| to the data which will 244 * be signed in |EVP_DigestSignFinal|. It returns one. */ 245 OPENSSL_EXPORT int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, 246 size_t len); 247 248 /* EVP_DigestSignFinal signs the data that has been included by one or more 249 * calls to |EVP_DigestSignUpdate|. If |out_sig| is NULL then |*out_sig_len| is 250 * set to the maximum number of output bytes. Otherwise, on entry, 251 * |*out_sig_len| must contain the length of the |out_sig| buffer. If the call 252 * is successful, the signature is written to |out_sig| and |*out_sig_len| is 253 * set to its length. 254 * 255 * It returns one on success, or zero on error. */ 256 OPENSSL_EXPORT int EVP_DigestSignFinal(EVP_MD_CTX *ctx, uint8_t *out_sig, 257 size_t *out_sig_len); 258 259 /* EVP_DigestSignAlgorithm encodes the signing parameters of |ctx| as an 260 * AlgorithmIdentifer and saves the result in |algor|. 261 * 262 * It returns one on success, or zero on error. 263 * 264 * TODO(davidben): This API should eventually lose the dependency on 265 * crypto/asn1/. */ 266 OPENSSL_EXPORT int EVP_DigestSignAlgorithm(EVP_MD_CTX *ctx, X509_ALGOR *algor); 267 268 269 /* Verifying */ 270 271 /* EVP_DigestVerifyInit sets up |ctx| for a signature verification operation 272 * with |type| and |pkey|. The |ctx| argument must have been initialised with 273 * |EVP_MD_CTX_init|. If |pctx| is not NULL, the |EVP_PKEY_CTX| of the signing 274 * operation will be written to |*pctx|; this can be used to set alternative 275 * signing options. 276 * 277 * It returns one on success, or zero on error. */ 278 OPENSSL_EXPORT int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, 279 const EVP_MD *type, ENGINE *e, 280 EVP_PKEY *pkey); 281 282 /* EVP_DigestVerifyInitFromAlgorithm sets up |ctx| for a signature verification 283 * operation with public key |pkey| and parameters from |algor|. The |ctx| 284 * argument must have been initialised with |EVP_MD_CTX_init|. 285 * 286 * It returns one on success, or zero on error. 287 * 288 * TODO(davidben): This API should eventually lose the dependency on 289 * crypto/asn1/. */ 290 OPENSSL_EXPORT int EVP_DigestVerifyInitFromAlgorithm(EVP_MD_CTX *ctx, 291 X509_ALGOR *algor, 292 EVP_PKEY *pkey); 293 294 /* EVP_DigestVerifyUpdate appends |len| bytes from |data| to the data which 295 * will be verified by |EVP_DigestVerifyFinal|. It returns one. */ 296 OPENSSL_EXPORT int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, 297 size_t len); 298 299 /* EVP_DigestVerifyFinal verifies that |sig_len| bytes of |sig| are a valid 300 * signature for the data that has been included by one or more calls to 301 * |EVP_DigestVerifyUpdate|. It returns one on success and zero otherwise. */ 302 OPENSSL_EXPORT int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig, 303 size_t sig_len); 304 305 306 /* Signing (old functions) */ 307 308 /* EVP_SignInit_ex configures |ctx|, which must already have been initialised, 309 * for a fresh signing operation using the hash function |type|. It returns one 310 * on success and zero otherwise. 311 * 312 * (In order to initialise |ctx|, either obtain it initialised with 313 * |EVP_MD_CTX_create|, or use |EVP_MD_CTX_init|.) */ 314 OPENSSL_EXPORT int EVP_SignInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, 315 ENGINE *impl); 316 317 /* EVP_SignInit is a deprecated version of |EVP_SignInit_ex|. 318 * 319 * TODO(fork): remove. */ 320 OPENSSL_EXPORT int EVP_SignInit(EVP_MD_CTX *ctx, const EVP_MD *type); 321 322 /* EVP_SignUpdate appends |len| bytes from |data| to the data which will be 323 * signed in |EVP_SignFinal|. */ 324 OPENSSL_EXPORT int EVP_SignUpdate(EVP_MD_CTX *ctx, const void *data, 325 size_t len); 326 327 /* EVP_SignFinal signs the data that has been included by one or more calls to 328 * |EVP_SignUpdate|, using the key |pkey|, and writes it to |sig|. On entry, 329 * |sig| must point to at least |EVP_PKEY_size(pkey)| bytes of space. The 330 * actual size of the signature is written to |*out_sig_len|. 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. */ 336 OPENSSL_EXPORT int EVP_SignFinal(const EVP_MD_CTX *ctx, uint8_t *sig, 337 unsigned int *out_sig_len, EVP_PKEY *pkey); 338 339 340 /* Verifying (old functions) */ 341 342 /* EVP_VerifyInit_ex configures |ctx|, which must already have been 343 * initialised, for a fresh signature verification operation using the hash 344 * function |type|. It returns one on success and zero otherwise. 345 * 346 * (In order to initialise |ctx|, either obtain it initialised with 347 * |EVP_MD_CTX_create|, or use |EVP_MD_CTX_init|.) */ 348 OPENSSL_EXPORT int EVP_VerifyInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, 349 ENGINE *impl); 350 351 /* EVP_VerifyInit is a deprecated version of |EVP_VerifyInit_ex|. 352 * 353 * TODO(fork): remove. */ 354 OPENSSL_EXPORT int EVP_VerifyInit(EVP_MD_CTX *ctx, const EVP_MD *type); 355 356 /* EVP_VerifyUpdate appends |len| bytes from |data| to the data which will be 357 * signed in |EVP_VerifyFinal|. */ 358 OPENSSL_EXPORT int EVP_VerifyUpdate(EVP_MD_CTX *ctx, const void *data, 359 size_t len); 360 361 /* EVP_VerifyFinal verifies that |sig_len| bytes of |sig| are a valid 362 * signature, by |pkey|, for the data that has been included by one or more 363 * calls to |EVP_VerifyUpdate|. 364 * 365 * It returns one on success and zero otherwise. 366 * 367 * It does not modify |ctx|, thus it's possible to continue to use |ctx| in 368 * order to sign a longer message. */ 369 OPENSSL_EXPORT int EVP_VerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig, 370 size_t sig_len, EVP_PKEY *pkey); 371 372 373 /* Printing */ 374 375 /* EVP_PKEY_print_public prints a textual representation of the public key in 376 * |pkey| to |out|. Returns one on success or zero otherwise. */ 377 OPENSSL_EXPORT int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, 378 int indent, ASN1_PCTX *pctx); 379 380 /* EVP_PKEY_print_public prints a textual representation of the private key in 381 * |pkey| to |out|. Returns one on success or zero otherwise. */ 382 OPENSSL_EXPORT int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, 383 int indent, ASN1_PCTX *pctx); 384 385 /* EVP_PKEY_print_public prints a textual representation of the parameters in 386 * |pkey| to |out|. Returns one on success or zero otherwise. */ 387 OPENSSL_EXPORT int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, 388 int indent, ASN1_PCTX *pctx); 389 390 391 /* Password stretching. 392 * 393 * Password stretching functions take a low-entropy password and apply a slow 394 * function that results in a key suitable for use in symmetric 395 * cryptography. */ 396 397 /* PKCS5_PBKDF2_HMAC computes |iterations| iterations of PBKDF2 of |password| 398 * and |salt|, using |digest|, and outputs |key_len| bytes to |out_key|. It 399 * returns one on success and zero on error. */ 400 OPENSSL_EXPORT int PKCS5_PBKDF2_HMAC(const char *password, size_t password_len, 401 const uint8_t *salt, size_t salt_len, 402 unsigned iterations, const EVP_MD *digest, 403 size_t key_len, uint8_t *out_key); 404 405 /* PKCS5_PBKDF2_HMAC_SHA1 is the same as PKCS5_PBKDF2_HMAC, but with |digest| 406 * fixed to |EVP_sha1|. */ 407 OPENSSL_EXPORT int PKCS5_PBKDF2_HMAC_SHA1(const char *password, 408 size_t password_len, const uint8_t *salt, 409 size_t salt_len, unsigned iterations, 410 size_t key_len, uint8_t *out_key); 411 412 413 /* Public key contexts. 414 * 415 * |EVP_PKEY_CTX| objects hold the context of an operation (e.g. signing or 416 * encrypting) that uses a public key. */ 417 418 /* EVP_PKEY_CTX_new allocates a fresh |EVP_PKEY_CTX| for use with |pkey|. It 419 * returns the context or NULL on error. */ 420 OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e); 421 422 /* EVP_PKEY_CTX_new allocates a fresh |EVP_PKEY_CTX| for a key of type |id| 423 * (e.g. |EVP_PKEY_HMAC|). This can be used for key generation where 424 * |EVP_PKEY_CTX_new| can't be used because there isn't an |EVP_PKEY| to pass 425 * it. It returns the context or NULL on error. */ 426 OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e); 427 428 /* EVP_KEY_CTX_free frees |ctx| and the data it owns. */ 429 OPENSSL_EXPORT void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx); 430 431 /* EVP_PKEY_CTX_dup allocates a fresh |EVP_PKEY_CTX| and sets it equal to the 432 * state of |ctx|. It returns the fresh |EVP_PKEY_CTX| or NULL on error. */ 433 OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *ctx); 434 435 /* EVP_PKEY_CTX_get0_pkey returns the |EVP_PKEY| associated with |ctx|. */ 436 OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx); 437 438 /* EVP_PKEY_CTX_set_app_data sets an opaque pointer on |ctx|. */ 439 OPENSSL_EXPORT void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data); 440 441 /* EVP_PKEY_CTX_get_app_data returns the opaque pointer from |ctx| that was 442 * previously set with |EVP_PKEY_CTX_set_app_data|, or NULL if none has been 443 * set. */ 444 OPENSSL_EXPORT void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx); 445 446 /* EVP_PKEY_sign_init initialises an |EVP_PKEY_CTX| for a signing operation. It 447 * should be called before |EVP_PKEY_sign|. 448 * 449 * It returns one on success or zero on error. */ 450 OPENSSL_EXPORT int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx); 451 452 /* EVP_PKEY_sign signs |data_len| bytes from |data| using |ctx|. If |sig| is 453 * NULL, the maximum size of the signature is written to 454 * |out_sig_len|. Otherwise, |*sig_len| must contain the number of bytes of 455 * space available at |sig|. If sufficient, the signature will be written to 456 * |sig| and |*sig_len| updated with the true length. 457 * 458 * WARNING: Setting |sig| to NULL only gives the maximum size of the 459 * signature. The actual signature may be smaller. 460 * 461 * It returns one on success or zero on error. (Note: this differs from 462 * OpenSSL, which can also return negative values to indicate an error. ) */ 463 OPENSSL_EXPORT int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, 464 size_t *sig_len, const uint8_t *data, 465 size_t data_len); 466 467 /* EVP_PKEY_verify_init initialises an |EVP_PKEY_CTX| for a signature 468 * verification operation. It should be called before |EVP_PKEY_verify|. 469 * 470 * It returns one on success or zero on error. */ 471 OPENSSL_EXPORT int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx); 472 473 /* EVP_PKEY_verify verifies that |sig_len| bytes from |sig| are a valid signature 474 * for |data|. 475 * 476 * It returns one on success or zero on error. */ 477 OPENSSL_EXPORT int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, 478 size_t sig_len, const uint8_t *data, 479 size_t data_len); 480 481 /* EVP_PKEY_encrypt_init initialises an |EVP_PKEY_CTX| for an encryption 482 * operation. It should be called before |EVP_PKEY_encrypt|. 483 * 484 * It returns one on success or zero on error. */ 485 OPENSSL_EXPORT int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx); 486 487 /* EVP_PKEY_encrypt encrypts |in_len| bytes from |in|. If |out| is NULL, the 488 * maximum size of the ciphertext is written to |out_len|. Otherwise, |*out_len| 489 * must contain the number of bytes of space available at |out|. If sufficient, 490 * the ciphertext will be written to |out| and |*out_len| updated with the true 491 * length. 492 * 493 * WARNING: Setting |out| to NULL only gives the maximum size of the 494 * ciphertext. The actual ciphertext may be smaller. 495 * 496 * It returns one on success or zero on error. */ 497 OPENSSL_EXPORT int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, 498 size_t *out_len, const uint8_t *in, 499 size_t in_len); 500 501 /* EVP_PKEY_decrypt_init initialises an |EVP_PKEY_CTX| for a decryption 502 * operation. It should be called before |EVP_PKEY_decrypt|. 503 * 504 * It returns one on success or zero on error. */ 505 OPENSSL_EXPORT int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx); 506 507 /* EVP_PKEY_decrypt decrypts |in_len| bytes from |in|. If |out| is NULL, the 508 * maximum size of the plaintext is written to |out_len|. Otherwise, |*out_len| 509 * must contain the number of bytes of space available at |out|. If sufficient, 510 * the ciphertext will be written to |out| and |*out_len| updated with the true 511 * length. 512 * 513 * WARNING: Setting |out| to NULL only gives the maximum size of the 514 * plaintext. The actual plaintext may be smaller. 515 * 516 * It returns one on success or zero on error. */ 517 OPENSSL_EXPORT int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out, 518 size_t *out_len, const uint8_t *in, 519 size_t in_len); 520 521 /* EVP_PKEY_derive_init initialises an |EVP_PKEY_CTX| for a key derivation 522 * operation. It should be called before |EVP_PKEY_derive_set_peer| and 523 * |EVP_PKEY_derive|. 524 * 525 * It returns one on success or zero on error. */ 526 OPENSSL_EXPORT int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx); 527 528 /* EVP_PKEY_derive_set_peer sets the peer's key to be used for key derivation 529 * by |ctx| to |peer|. It should be called after |EVP_PKEY_derive_init|. (For 530 * example, this is used to set the peer's key in (EC)DH.) It returns one on 531 * success and zero on error. */ 532 OPENSSL_EXPORT int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer); 533 534 /* EVP_PKEY_derive derives a shared key between the two keys configured in 535 * |ctx|. If |key| is non-NULL then, on entry, |out_key_len| must contain the 536 * amount of space at |key|. If sufficient then the shared key will be written 537 * to |key| and |*out_key_len| will be set to the length. If |key| is NULL then 538 * |out_key_len| will be set to the maximum length. 539 * 540 * WARNING: Setting |out| to NULL only gives the maximum size of the key. The 541 * actual key may be smaller. 542 * 543 * It returns one on success and zero on error. */ 544 OPENSSL_EXPORT int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, uint8_t *key, 545 size_t *out_key_len); 546 547 /* EVP_PKEY_keygen_init initialises an |EVP_PKEY_CTX| for a key generation 548 * operation. It should be called before |EVP_PKEY_keygen|. 549 * 550 * It returns one on success or zero on error. */ 551 OPENSSL_EXPORT int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx); 552 553 /* EVP_PKEY_keygen performs a key generation operation using the values from 554 * |ctx| and sets |*ppkey| to a fresh |EVP_PKEY| containing the resulting key. 555 * It returns one on success or zero on error. */ 556 OPENSSL_EXPORT int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey); 557 558 559 /* Generic control functions. */ 560 561 /* EVP_PKEY_CTX_set_signature_md sets |md| as the digest to be used in a 562 * signature operation. It returns one on success or zero on error. */ 563 OPENSSL_EXPORT int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, 564 const EVP_MD *md); 565 566 /* EVP_PKEY_CTX_get_signature_md sets |*out_md| to the digest to be used in a 567 * signature operation. It returns one on success or zero on error. */ 568 OPENSSL_EXPORT int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, 569 const EVP_MD **out_md); 570 571 572 /* RSA specific control functions. */ 573 574 /* EVP_PKEY_CTX_set_rsa_padding sets the padding type to use. It should be one 575 * of the |RSA_*_PADDING| values. Returns one on success or zero on error. */ 576 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int padding); 577 578 /* EVP_PKEY_CTX_get_rsa_padding sets |*out_padding| to the current padding 579 * value, which is one of the |RSA_*_PADDING| values. Returns one on success or 580 * zero on error. */ 581 OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, 582 int *out_padding); 583 584 /* EVP_PKEY_CTX_set_rsa_pss_saltlen sets the length of the salt in a PSS-padded 585 * signature. A value of -1 cause the salt to be the same length as the digest 586 * in the signature. A value of -2 causes the salt to be the maximum length 587 * that will fit. Otherwise the value gives the size of the salt in bytes. 588 * 589 * Returns one on success or zero on error. */ 590 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, 591 int salt_len); 592 593 /* EVP_PKEY_CTX_get_rsa_pss_saltlen sets |*out_salt_len| to the salt length of 594 * a PSS-padded signature. See the documentation for 595 * |EVP_PKEY_CTX_set_rsa_pss_saltlen| for details of the special values that it 596 * can take. 597 * 598 * Returns one on success or zero on error. */ 599 OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, 600 int *out_salt_len); 601 602 /* EVP_PKEY_CTX_set_rsa_keygen_bits sets the size of the desired RSA modulus, 603 * in bits, for key generation. Returns one on success or zero on 604 * error. */ 605 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, 606 int bits); 607 608 /* EVP_PKEY_CTX_set_rsa_keygen_pubexp sets |e| as the public exponent for key 609 * generation. Returns one on success or zero on error. */ 610 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, 611 BIGNUM *e); 612 613 /* EVP_PKEY_CTX_set_rsa_oaep_md sets |md| as the digest used in OAEP padding. 614 * Returns one on success or zero on error. */ 615 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, 616 const EVP_MD *md); 617 618 /* EVP_PKEY_CTX_get_rsa_oaep_md sets |*out_md| to the digest function used in 619 * OAEP padding. Returns one on success or zero on error. */ 620 OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, 621 const EVP_MD **out_md); 622 623 /* EVP_PKEY_CTX_set_rsa_mgf1_md sets |md| as the digest used in MGF1. Returns 624 * one on success or zero on error. */ 625 OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, 626 const EVP_MD *md); 627 628 /* EVP_PKEY_CTX_get_rsa_mgf1_md sets |*out_md| to the digest function used in 629 * MGF1. Returns one on success or zero on error. */ 630 OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, 631 const EVP_MD **out_md); 632 633 /* EVP_PKEY_CTX_set0_rsa_oaep_label sets |label_len| bytes from |label| as the 634 * label used in OAEP. DANGER: On success, this call takes ownership of |label| 635 * and will call |OPENSSL_free| on it when |ctx| is destroyed. 636 * 637 * Returns one on success or zero on error. */ 638 OPENSSL_EXPORT int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, 639 const uint8_t *label, 640 size_t label_len); 641 642 /* EVP_PKEY_CTX_get0_rsa_oaep_label sets |*out_label| to point to the internal 643 * buffer containing the OAEP label (which may be NULL) and returns the length 644 * of the label or a negative value on error. 645 * 646 * WARNING: the return value differs from the usual return value convention. */ 647 OPENSSL_EXPORT int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, 648 const uint8_t **out_label); 649 650 651 /* Deprecated functions. */ 652 653 /* EVP_PKEY_dup adds one to the reference count of |pkey| and returns 654 * |pkey|. 655 * 656 * WARNING: this is a |_dup| function that doesn't actually duplicate! Use 657 * |EVP_PKEY_up_ref| if you want to increment the reference count without 658 * confusion. */ 659 OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey); 660 661 662 /* Private functions */ 663 664 /* OpenSSL_add_all_algorithms does nothing. */ 665 OPENSSL_EXPORT void OpenSSL_add_all_algorithms(void); 666 667 /* OpenSSL_add_all_ciphers does nothing. */ 668 OPENSSL_EXPORT void OpenSSL_add_all_ciphers(void); 669 670 /* OpenSSL_add_all_digests does nothing. */ 671 OPENSSL_EXPORT void OpenSSL_add_all_digests(void); 672 673 /* EVP_cleanup does nothing. */ 674 OPENSSL_EXPORT void EVP_cleanup(void); 675 676 /* EVP_PKEY_asn1_find returns the ASN.1 method table for the given |nid|, which 677 * should be one of the |EVP_PKEY_*| values. It returns NULL if |nid| is 678 * unknown. */ 679 OPENSSL_EXPORT const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pengine, 680 int nid); 681 682 /* TODO(fork): move to PEM? */ 683 OPENSSL_EXPORT const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str( 684 ENGINE **pengine, const char *name, size_t len); 685 686 struct evp_pkey_st { 687 CRYPTO_refcount_t references; 688 689 /* type contains one of the EVP_PKEY_* values or NID_undef and determines 690 * which element (if any) of the |pkey| union is valid. */ 691 int type; 692 693 union { 694 char *ptr; 695 struct rsa_st *rsa; /* RSA */ 696 struct dsa_st *dsa; /* DSA */ 697 struct dh_st *dh; /* DH */ 698 struct ec_key_st *ec; /* ECC */ 699 } pkey; 700 701 /* ameth contains a pointer to a method table that contains many ASN.1 702 * methods for the key type. */ 703 const EVP_PKEY_ASN1_METHOD *ameth; 704 } /* EVP_PKEY */; 705 706 707 #if defined(__cplusplus) 708 } /* extern C */ 709 #endif 710 711 #define EVP_F_EVP_PKEY_derive_init 108 712 #define EVP_F_EVP_PKEY_encrypt 110 713 #define EVP_F_EVP_PKEY_encrypt_init 111 714 #define EVP_F_EVP_PKEY_get1_DH 112 715 #define EVP_F_EVP_PKEY_get1_EC_KEY 114 716 #define EVP_F_EVP_PKEY_get1_RSA 115 717 #define EVP_F_EVP_PKEY_keygen 116 718 #define EVP_F_EVP_PKEY_sign 120 719 #define EVP_F_EVP_PKEY_sign_init 121 720 #define EVP_F_EVP_PKEY_verify 122 721 #define EVP_F_EVP_PKEY_verify_init 123 722 #define EVP_F_d2i_AutoPrivateKey 125 723 #define EVP_F_d2i_PrivateKey 126 724 #define EVP_F_do_EC_KEY_print 127 725 #define EVP_F_do_sigver_init 129 726 #define EVP_F_eckey_param2type 130 727 #define EVP_F_eckey_param_decode 131 728 #define EVP_F_eckey_priv_decode 132 729 #define EVP_F_eckey_priv_encode 133 730 #define EVP_F_eckey_pub_decode 134 731 #define EVP_F_eckey_pub_encode 135 732 #define EVP_F_eckey_type2param 136 733 #define EVP_F_evp_pkey_ctx_new 137 734 #define EVP_F_hmac_signctx 138 735 #define EVP_F_i2d_PublicKey 139 736 #define EVP_F_old_ec_priv_decode 140 737 #define EVP_F_old_rsa_priv_decode 141 738 #define EVP_F_pkey_ec_ctrl 142 739 #define EVP_F_pkey_ec_derive 143 740 #define EVP_F_pkey_ec_keygen 144 741 #define EVP_F_pkey_ec_paramgen 145 742 #define EVP_F_pkey_ec_sign 146 743 #define EVP_F_pkey_rsa_ctrl 147 744 #define EVP_F_pkey_rsa_decrypt 148 745 #define EVP_F_pkey_rsa_encrypt 149 746 #define EVP_F_pkey_rsa_sign 150 747 #define EVP_F_rsa_algor_to_md 151 748 #define EVP_F_rsa_digest_verify_init_from_algorithm 152 749 #define EVP_F_rsa_mgf1_to_md 153 750 #define EVP_F_rsa_priv_decode 154 751 #define EVP_F_rsa_priv_encode 155 752 #define EVP_F_rsa_pss_to_ctx 156 753 #define EVP_F_rsa_pub_decode 157 754 #define EVP_F_pkey_hmac_ctrl 158 755 #define EVP_F_EVP_PKEY_CTX_get0_rsa_oaep_label 159 756 #define EVP_F_EVP_DigestSignAlgorithm 160 757 #define EVP_F_EVP_DigestVerifyInitFromAlgorithm 161 758 #define EVP_F_EVP_PKEY_CTX_ctrl 162 759 #define EVP_F_EVP_PKEY_CTX_dup 163 760 #define EVP_F_EVP_PKEY_copy_parameters 164 761 #define EVP_F_EVP_PKEY_decrypt 165 762 #define EVP_F_EVP_PKEY_decrypt_init 166 763 #define EVP_F_EVP_PKEY_derive 167 764 #define EVP_F_EVP_PKEY_derive_set_peer 168 765 #define EVP_F_EVP_PKEY_get1_DSA 169 766 #define EVP_F_EVP_PKEY_keygen_init 170 767 #define EVP_F_EVP_PKEY_new 171 768 #define EVP_F_EVP_PKEY_set_type 172 769 #define EVP_F_check_padding_md 173 770 #define EVP_F_do_dsa_print 174 771 #define EVP_F_do_rsa_print 175 772 #define EVP_F_dsa_param_decode 176 773 #define EVP_F_dsa_priv_decode 177 774 #define EVP_F_dsa_priv_encode 178 775 #define EVP_F_dsa_pub_decode 179 776 #define EVP_F_dsa_pub_encode 180 777 #define EVP_F_dsa_sig_print 181 778 #define EVP_F_old_dsa_priv_decode 182 779 #define EVP_R_BUFFER_TOO_SMALL 100 780 #define EVP_R_COMMAND_NOT_SUPPORTED 101 781 #define EVP_R_DIFFERENT_KEY_TYPES 104 782 #define EVP_R_DIFFERENT_PARAMETERS 105 783 #define EVP_R_EXPECTING_AN_EC_KEY_KEY 107 784 #define EVP_R_EXPECTING_A_DH_KEY 109 785 #define EVP_R_EXPECTING_A_DSA_KEY 110 786 #define EVP_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE 111 787 #define EVP_R_INVALID_CURVE 112 788 #define EVP_R_INVALID_DIGEST_LENGTH 113 789 #define EVP_R_INVALID_DIGEST_TYPE 114 790 #define EVP_R_INVALID_KEYBITS 115 791 #define EVP_R_INVALID_MGF1_MD 116 792 #define EVP_R_INVALID_PADDING_MODE 118 793 #define EVP_R_INVALID_PSS_PARAMETERS 119 794 #define EVP_R_INVALID_SALT_LENGTH 121 795 #define EVP_R_INVALID_TRAILER 122 796 #define EVP_R_KEYS_NOT_SET 123 797 #define EVP_R_MISSING_PARAMETERS 124 798 #define EVP_R_NO_DEFAULT_DIGEST 125 799 #define EVP_R_NO_KEY_SET 126 800 #define EVP_R_NO_MDC2_SUPPORT 127 801 #define EVP_R_NO_NID_FOR_CURVE 128 802 #define EVP_R_NO_OPERATION_SET 129 803 #define EVP_R_NO_PARAMETERS_SET 130 804 #define EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE 131 805 #define EVP_R_OPERATON_NOT_INITIALIZED 132 806 #define EVP_R_UNKNOWN_DIGEST 133 807 #define EVP_R_UNKNOWN_MASK_DIGEST 134 808 #define EVP_R_UNSUPPORTED_ALGORITHM 138 809 #define EVP_R_UNSUPPORTED_MASK_ALGORITHM 139 810 #define EVP_R_UNSUPPORTED_MASK_PARAMETER 140 811 #define EVP_R_EXPECTING_AN_RSA_KEY 141 812 #define EVP_R_INVALID_OPERATION 142 813 #define EVP_R_DECODE_ERROR 143 814 #define EVP_R_INVALID_PSS_SALTLEN 144 815 #define EVP_R_UNKNOWN_PUBLIC_KEY_TYPE 145 816 #define EVP_R_CONTEXT_NOT_INITIALISED 146 817 #define EVP_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED 147 818 #define EVP_R_WRONG_PUBLIC_KEY_TYPE 148 819 #define EVP_R_UNKNOWN_SIGNATURE_ALGORITHM 149 820 #define EVP_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM 150 821 #define EVP_R_BN_DECODE_ERROR 151 822 #define EVP_R_PARAMETER_ENCODING_ERROR 152 823 #define EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE 153 824 #define EVP_R_UNSUPPORTED_SIGNATURE_TYPE 154 825 826 #endif /* OPENSSL_HEADER_EVP_H */ 827