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