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