gnunet-android

GNUnet for Android
Log | Files | Refs | README

evp.h (48495B)


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