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