Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

874 linhas
39 KiB

  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. #ifndef OPENSSL_HEADER_EVP_H
  57. #define OPENSSL_HEADER_EVP_H
  58. #include <openssl/base.h>
  59. #include <openssl/thread.h>
  60. // OpenSSL included digest and cipher functions in this header so we include
  61. // them for users that still expect that.
  62. //
  63. // TODO(fork): clean up callers so that they include what they use.
  64. #include <openssl/aead.h>
  65. #include <openssl/base64.h>
  66. #include <openssl/cipher.h>
  67. #include <openssl/digest.h>
  68. #include <openssl/nid.h>
  69. #if defined(__cplusplus)
  70. extern "C" {
  71. #endif
  72. // EVP abstracts over public/private key algorithms.
  73. // Public key objects.
  74. // EVP_PKEY_new creates a new, empty public-key object and returns it or NULL
  75. // on allocation failure.
  76. OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_new(void);
  77. // EVP_PKEY_free frees all data referenced by |pkey| and then frees |pkey|
  78. // itself.
  79. OPENSSL_EXPORT void EVP_PKEY_free(EVP_PKEY *pkey);
  80. // EVP_PKEY_up_ref increments the reference count of |pkey| and returns one.
  81. OPENSSL_EXPORT int EVP_PKEY_up_ref(EVP_PKEY *pkey);
  82. // EVP_PKEY_is_opaque returns one if |pkey| is opaque. Opaque keys are backed by
  83. // custom implementations which do not expose key material and parameters. It is
  84. // an error to attempt to duplicate, export, or compare an opaque key.
  85. OPENSSL_EXPORT int EVP_PKEY_is_opaque(const EVP_PKEY *pkey);
  86. // EVP_PKEY_cmp compares |a| and |b| and returns one if they are equal, zero if
  87. // not and a negative number on error.
  88. //
  89. // WARNING: this differs from the traditional return value of a "cmp"
  90. // function.
  91. OPENSSL_EXPORT int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b);
  92. // EVP_PKEY_copy_parameters sets the parameters of |to| to equal the parameters
  93. // of |from|. It returns one on success and zero on error.
  94. OPENSSL_EXPORT int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from);
  95. // EVP_PKEY_missing_parameters returns one if |pkey| is missing needed
  96. // parameters or zero if not, or if the algorithm doesn't take parameters.
  97. OPENSSL_EXPORT int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey);
  98. // EVP_PKEY_size returns the maximum size, in bytes, of a signature signed by
  99. // |pkey|. For an RSA key, this returns the number of bytes needed to represent
  100. // the modulus. For an EC key, this returns the maximum size of a DER-encoded
  101. // ECDSA signature.
  102. OPENSSL_EXPORT int EVP_PKEY_size(const EVP_PKEY *pkey);
  103. // EVP_PKEY_bits returns the "size", in bits, of |pkey|. For an RSA key, this
  104. // returns the bit length of the modulus. For an EC key, this returns the bit
  105. // length of the group order.
  106. OPENSSL_EXPORT int EVP_PKEY_bits(EVP_PKEY *pkey);
  107. // EVP_PKEY_id returns the type of |pkey|, which is one of the |EVP_PKEY_*|
  108. // values.
  109. OPENSSL_EXPORT int EVP_PKEY_id(const EVP_PKEY *pkey);
  110. // EVP_PKEY_type returns |nid| if |nid| is a known key type and |NID_undef|
  111. // otherwise.
  112. OPENSSL_EXPORT int EVP_PKEY_type(int nid);
  113. // Getting and setting concrete public key types.
  114. //
  115. // The following functions get and set the underlying public key in an
  116. // |EVP_PKEY| object. The |set1| functions take an additional reference to the
  117. // underlying key and return one on success or zero on error. The |assign|
  118. // functions adopt the caller's reference. The |get1| functions return a fresh
  119. // reference to the underlying object or NULL if |pkey| is not of the correct
  120. // type. The |get0| functions behave the same but return a non-owning
  121. // pointer.
  122. OPENSSL_EXPORT int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key);
  123. OPENSSL_EXPORT int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key);
  124. OPENSSL_EXPORT RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey);
  125. OPENSSL_EXPORT RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey);
  126. OPENSSL_EXPORT int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key);
  127. OPENSSL_EXPORT int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key);
  128. OPENSSL_EXPORT DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey);
  129. OPENSSL_EXPORT DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey);
  130. OPENSSL_EXPORT int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key);
  131. OPENSSL_EXPORT int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key);
  132. OPENSSL_EXPORT EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey);
  133. OPENSSL_EXPORT EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey);
  134. // EVP_PKEY_new_ed25519_public returns a newly allocated |EVP_PKEY| wrapping an
  135. // Ed25519 public key, or NULL on allocation error.
  136. OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_new_ed25519_public(
  137. const uint8_t public_key[32]);
  138. // EVP_PKEY_new_ed25519_private returns a newly allocated |EVP_PKEY| wrapping an
  139. // Ed25519 private key, or NULL on allocation error.
  140. OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_new_ed25519_private(
  141. const uint8_t private_key[64]);
  142. #define EVP_PKEY_NONE NID_undef
  143. #define EVP_PKEY_RSA NID_rsaEncryption
  144. #define EVP_PKEY_DSA NID_dsa
  145. #define EVP_PKEY_EC NID_X9_62_id_ecPublicKey
  146. #define EVP_PKEY_ED25519 NID_ED25519
  147. // EVP_PKEY_assign sets the underlying key of |pkey| to |key|, which must be of
  148. // the given type. The |type| argument should be one of the |EVP_PKEY_*|
  149. // values.
  150. OPENSSL_EXPORT int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key);
  151. // EVP_PKEY_set_type sets the type of |pkey| to |type|, which should be one of
  152. // the |EVP_PKEY_*| values. It returns one if successful or zero otherwise. If
  153. // |pkey| is NULL, it simply reports whether the type is known.
  154. OPENSSL_EXPORT int EVP_PKEY_set_type(EVP_PKEY *pkey, int type);
  155. // EVP_PKEY_cmp_parameters compares the parameters of |a| and |b|. It returns
  156. // one if they match, zero if not, or a negative number of on error.
  157. //
  158. // WARNING: the return value differs from the usual return value convention.
  159. OPENSSL_EXPORT int EVP_PKEY_cmp_parameters(const EVP_PKEY *a,
  160. const EVP_PKEY *b);
  161. // ASN.1 functions
  162. // EVP_parse_public_key decodes a DER-encoded SubjectPublicKeyInfo structure
  163. // (RFC 5280) from |cbs| and advances |cbs|. It returns a newly-allocated
  164. // |EVP_PKEY| or NULL on error.
  165. //
  166. // The caller must check the type of the parsed public key to ensure it is
  167. // suitable and validate other desired key properties such as RSA modulus size
  168. // or EC curve.
  169. OPENSSL_EXPORT EVP_PKEY *EVP_parse_public_key(CBS *cbs);
  170. // EVP_marshal_public_key marshals |key| as a DER-encoded SubjectPublicKeyInfo
  171. // structure (RFC 5280) and appends the result to |cbb|. It returns one on
  172. // success and zero on error.
  173. OPENSSL_EXPORT int EVP_marshal_public_key(CBB *cbb, const EVP_PKEY *key);
  174. // EVP_parse_private_key decodes a DER-encoded PrivateKeyInfo structure (RFC
  175. // 5208) from |cbs| and advances |cbs|. It returns a newly-allocated |EVP_PKEY|
  176. // or NULL on error.
  177. //
  178. // The caller must check the type of the parsed private key to ensure it is
  179. // suitable and validate other desired key properties such as RSA modulus size
  180. // or EC curve.
  181. //
  182. // A PrivateKeyInfo ends with an optional set of attributes. These are not
  183. // processed and so this function will silently ignore any trailing data in the
  184. // structure.
  185. OPENSSL_EXPORT EVP_PKEY *EVP_parse_private_key(CBS *cbs);
  186. // EVP_marshal_private_key marshals |key| as a DER-encoded PrivateKeyInfo
  187. // structure (RFC 5208) and appends the result to |cbb|. It returns one on
  188. // success and zero on error.
  189. OPENSSL_EXPORT int EVP_marshal_private_key(CBB *cbb, const EVP_PKEY *key);
  190. // Signing
  191. // EVP_DigestSignInit sets up |ctx| for a signing operation with |type| and
  192. // |pkey|. The |ctx| argument must have been initialised with
  193. // |EVP_MD_CTX_init|. If |pctx| is not NULL, the |EVP_PKEY_CTX| of the signing
  194. // operation will be written to |*pctx|; this can be used to set alternative
  195. // signing options.
  196. //
  197. // For single-shot signing algorithms which do not use a pre-hash, such as
  198. // Ed25519, |type| should be NULL. The |EVP_MD_CTX| itself is unused but is
  199. // present so the API is uniform. See |EVP_DigestSign|.
  200. //
  201. // It returns one on success, or zero on error.
  202. OPENSSL_EXPORT int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  203. const EVP_MD *type, ENGINE *e,
  204. EVP_PKEY *pkey);
  205. // EVP_DigestSignUpdate appends |len| bytes from |data| to the data which will
  206. // be signed in |EVP_DigestSignFinal|. It returns one.
  207. //
  208. // This function performs a streaming signing operation and will fail for
  209. // signature algorithms which do not support this. Use |EVP_DigestSign| for a
  210. // single-shot operation.
  211. OPENSSL_EXPORT int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data,
  212. size_t len);
  213. // EVP_DigestSignFinal signs the data that has been included by one or more
  214. // calls to |EVP_DigestSignUpdate|. If |out_sig| is NULL then |*out_sig_len| is
  215. // set to the maximum number of output bytes. Otherwise, on entry,
  216. // |*out_sig_len| must contain the length of the |out_sig| buffer. If the call
  217. // is successful, the signature is written to |out_sig| and |*out_sig_len| is
  218. // set to its length.
  219. //
  220. // This function performs a streaming signing operation and will fail for
  221. // signature algorithms which do not support this. Use |EVP_DigestSign| for a
  222. // single-shot operation.
  223. //
  224. // It returns one on success, or zero on error.
  225. OPENSSL_EXPORT int EVP_DigestSignFinal(EVP_MD_CTX *ctx, uint8_t *out_sig,
  226. size_t *out_sig_len);
  227. // EVP_DigestSign signs |data_len| bytes from |data| using |ctx|. If |out_sig|
  228. // is NULL then |*out_sig_len| is set to the maximum number of output
  229. // bytes. Otherwise, on entry, |*out_sig_len| must contain the length of the
  230. // |out_sig| buffer. If the call is successful, the signature is written to
  231. // |out_sig| and |*out_sig_len| is set to its length.
  232. //
  233. // It returns one on success and zero on error.
  234. OPENSSL_EXPORT int EVP_DigestSign(EVP_MD_CTX *ctx, uint8_t *out_sig,
  235. size_t *out_sig_len, const uint8_t *data,
  236. size_t data_len);
  237. // Verifying
  238. // EVP_DigestVerifyInit sets up |ctx| for a signature verification operation
  239. // with |type| and |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_DigestVerify|.
  247. //
  248. // It returns one on success, or zero on error.
  249. OPENSSL_EXPORT int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  250. const EVP_MD *type, ENGINE *e,
  251. EVP_PKEY *pkey);
  252. // EVP_DigestVerifyUpdate appends |len| bytes from |data| to the data which
  253. // will be verified by |EVP_DigestVerifyFinal|. It returns one.
  254. //
  255. // This function performs streaming signature verification and will fail for
  256. // signature algorithms which do not support this. Use |EVP_PKEY_verify_message|
  257. // for a single-shot verification.
  258. OPENSSL_EXPORT int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data,
  259. size_t len);
  260. // EVP_DigestVerifyFinal verifies that |sig_len| bytes of |sig| are a valid
  261. // signature for the data that has been included by one or more calls to
  262. // |EVP_DigestVerifyUpdate|. It returns one on success and zero otherwise.
  263. //
  264. // This function performs streaming signature verification and will fail for
  265. // signature algorithms which do not support this. Use |EVP_PKEY_verify_message|
  266. // for a single-shot verification.
  267. OPENSSL_EXPORT int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig,
  268. size_t sig_len);
  269. // EVP_DigestVerify verifies that |sig_len| bytes from |sig| are a valid
  270. // signature for |data|. It returns one on success or zero on error.
  271. OPENSSL_EXPORT int EVP_DigestVerify(EVP_MD_CTX *ctx, const uint8_t *sig,
  272. size_t sig_len, const uint8_t *data,
  273. size_t len);
  274. // Signing (old functions)
  275. // EVP_SignInit_ex configures |ctx|, which must already have been initialised,
  276. // for a fresh signing operation using the hash function |type|. It returns one
  277. // on success and zero otherwise.
  278. //
  279. // (In order to initialise |ctx|, either obtain it initialised with
  280. // |EVP_MD_CTX_create|, or use |EVP_MD_CTX_init|.)
  281. OPENSSL_EXPORT int EVP_SignInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type,
  282. ENGINE *impl);
  283. // EVP_SignInit is a deprecated version of |EVP_SignInit_ex|.
  284. //
  285. // TODO(fork): remove.
  286. OPENSSL_EXPORT int EVP_SignInit(EVP_MD_CTX *ctx, const EVP_MD *type);
  287. // EVP_SignUpdate appends |len| bytes from |data| to the data which will be
  288. // signed in |EVP_SignFinal|.
  289. OPENSSL_EXPORT int EVP_SignUpdate(EVP_MD_CTX *ctx, const void *data,
  290. size_t len);
  291. // EVP_SignFinal signs the data that has been included by one or more calls to
  292. // |EVP_SignUpdate|, using the key |pkey|, and writes it to |sig|. On entry,
  293. // |sig| must point to at least |EVP_PKEY_size(pkey)| bytes of space. The
  294. // actual size of the signature is written to |*out_sig_len|.
  295. //
  296. // It returns one on success and zero otherwise.
  297. //
  298. // It does not modify |ctx|, thus it's possible to continue to use |ctx| in
  299. // order to sign a longer message.
  300. OPENSSL_EXPORT int EVP_SignFinal(const EVP_MD_CTX *ctx, uint8_t *sig,
  301. unsigned int *out_sig_len, EVP_PKEY *pkey);
  302. // Verifying (old functions)
  303. // EVP_VerifyInit_ex configures |ctx|, which must already have been
  304. // initialised, for a fresh signature verification operation using the hash
  305. // function |type|. It returns one on success and zero otherwise.
  306. //
  307. // (In order to initialise |ctx|, either obtain it initialised with
  308. // |EVP_MD_CTX_create|, or use |EVP_MD_CTX_init|.)
  309. OPENSSL_EXPORT int EVP_VerifyInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type,
  310. ENGINE *impl);
  311. // EVP_VerifyInit is a deprecated version of |EVP_VerifyInit_ex|.
  312. //
  313. // TODO(fork): remove.
  314. OPENSSL_EXPORT int EVP_VerifyInit(EVP_MD_CTX *ctx, const EVP_MD *type);
  315. // EVP_VerifyUpdate appends |len| bytes from |data| to the data which will be
  316. // signed in |EVP_VerifyFinal|.
  317. OPENSSL_EXPORT int EVP_VerifyUpdate(EVP_MD_CTX *ctx, const void *data,
  318. size_t len);
  319. // EVP_VerifyFinal verifies that |sig_len| bytes of |sig| are a valid
  320. // signature, by |pkey|, for the data that has been included by one or more
  321. // calls to |EVP_VerifyUpdate|.
  322. //
  323. // It returns one on success and zero otherwise.
  324. //
  325. // It does not modify |ctx|, thus it's possible to continue to use |ctx| in
  326. // order to sign a longer message.
  327. OPENSSL_EXPORT int EVP_VerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig,
  328. size_t sig_len, EVP_PKEY *pkey);
  329. // Printing
  330. // EVP_PKEY_print_public prints a textual representation of the public key in
  331. // |pkey| to |out|. Returns one on success or zero otherwise.
  332. OPENSSL_EXPORT int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
  333. int indent, ASN1_PCTX *pctx);
  334. // EVP_PKEY_print_private prints a textual representation of the private key in
  335. // |pkey| to |out|. Returns one on success or zero otherwise.
  336. OPENSSL_EXPORT int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
  337. int indent, ASN1_PCTX *pctx);
  338. // EVP_PKEY_print_params prints a textual representation of the parameters in
  339. // |pkey| to |out|. Returns one on success or zero otherwise.
  340. OPENSSL_EXPORT int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
  341. int indent, ASN1_PCTX *pctx);
  342. // Password stretching.
  343. //
  344. // Password stretching functions take a low-entropy password and apply a slow
  345. // function that results in a key suitable for use in symmetric
  346. // cryptography.
  347. // PKCS5_PBKDF2_HMAC computes |iterations| iterations of PBKDF2 of |password|
  348. // and |salt|, using |digest|, and outputs |key_len| bytes to |out_key|. It
  349. // returns one on success and zero on error.
  350. OPENSSL_EXPORT int PKCS5_PBKDF2_HMAC(const char *password, size_t password_len,
  351. const uint8_t *salt, size_t salt_len,
  352. unsigned iterations, const EVP_MD *digest,
  353. size_t key_len, uint8_t *out_key);
  354. // PKCS5_PBKDF2_HMAC_SHA1 is the same as PKCS5_PBKDF2_HMAC, but with |digest|
  355. // fixed to |EVP_sha1|.
  356. OPENSSL_EXPORT int PKCS5_PBKDF2_HMAC_SHA1(const char *password,
  357. size_t password_len,
  358. const uint8_t *salt, size_t salt_len,
  359. unsigned iterations, size_t key_len,
  360. uint8_t *out_key);
  361. // EVP_PBE_scrypt expands |password| into a secret key of length |key_len| using
  362. // scrypt, as described in RFC 7914, and writes the result to |out_key|. It
  363. // returns one on success and zero on error.
  364. //
  365. // |N|, |r|, and |p| are as described in RFC 7914 section 6. They determine the
  366. // cost of the operation. If the memory required exceeds |max_mem|, the
  367. // operation will fail instead. If |max_mem| is zero, a defult limit of 32MiB
  368. // will be used.
  369. OPENSSL_EXPORT int EVP_PBE_scrypt(const char *password, size_t password_len,
  370. const uint8_t *salt, size_t salt_len,
  371. uint64_t N, uint64_t r, uint64_t p,
  372. size_t max_mem, uint8_t *out_key,
  373. size_t key_len);
  374. // Public key contexts.
  375. //
  376. // |EVP_PKEY_CTX| objects hold the context of an operation (e.g. signing or
  377. // encrypting) that uses a public key.
  378. // EVP_PKEY_CTX_new allocates a fresh |EVP_PKEY_CTX| for use with |pkey|. It
  379. // returns the context or NULL on error.
  380. OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e);
  381. // EVP_PKEY_CTX_new_id allocates a fresh |EVP_PKEY_CTX| for a key of type |id|
  382. // (e.g. |EVP_PKEY_HMAC|). This can be used for key generation where
  383. // |EVP_PKEY_CTX_new| can't be used because there isn't an |EVP_PKEY| to pass
  384. // it. It returns the context or NULL on error.
  385. OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e);
  386. // EVP_PKEY_CTX_free frees |ctx| and the data it owns.
  387. OPENSSL_EXPORT void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx);
  388. // EVP_PKEY_CTX_dup allocates a fresh |EVP_PKEY_CTX| and sets it equal to the
  389. // state of |ctx|. It returns the fresh |EVP_PKEY_CTX| or NULL on error.
  390. OPENSSL_EXPORT EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *ctx);
  391. // EVP_PKEY_CTX_get0_pkey returns the |EVP_PKEY| associated with |ctx|.
  392. OPENSSL_EXPORT EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx);
  393. // EVP_PKEY_sign_init initialises an |EVP_PKEY_CTX| for a signing operation. It
  394. // should be called before |EVP_PKEY_sign|.
  395. //
  396. // It returns one on success or zero on error.
  397. OPENSSL_EXPORT int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx);
  398. // EVP_PKEY_sign signs |digest_len| bytes from |digest| using |ctx|. If |sig| is
  399. // NULL, the maximum size of the signature is written to
  400. // |out_sig_len|. Otherwise, |*sig_len| must contain the number of bytes of
  401. // space available at |sig|. If sufficient, the signature will be written to
  402. // |sig| and |*sig_len| updated with the true length.
  403. //
  404. // This function expects a pre-hashed input and will fail for signature
  405. // algorithms which do not support this. Use |EVP_DigestSignInit| to sign an
  406. // unhashed input.
  407. //
  408. // WARNING: Setting |sig| to NULL only gives the maximum size of the
  409. // signature. The actual signature may be smaller.
  410. //
  411. // It returns one on success or zero on error. (Note: this differs from
  412. // OpenSSL, which can also return negative values to indicate an error. )
  413. OPENSSL_EXPORT int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, uint8_t *sig,
  414. size_t *sig_len, const uint8_t *digest,
  415. size_t digest_len);
  416. // EVP_PKEY_verify_init initialises an |EVP_PKEY_CTX| for a signature
  417. // verification operation. It should be called before |EVP_PKEY_verify|.
  418. //
  419. // It returns one on success or zero on error.
  420. OPENSSL_EXPORT int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx);
  421. // EVP_PKEY_verify verifies that |sig_len| bytes from |sig| are a valid
  422. // signature for |digest|.
  423. //
  424. // This function expects a pre-hashed input and will fail for signature
  425. // algorithms which do not support this. Use |EVP_DigestVerifyInit| to verify a
  426. // signature given the unhashed input.
  427. //
  428. // It returns one on success or zero on error.
  429. OPENSSL_EXPORT int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig,
  430. size_t sig_len, const uint8_t *digest,
  431. size_t digest_len);
  432. // EVP_PKEY_encrypt_init initialises an |EVP_PKEY_CTX| for an encryption
  433. // operation. It should be called before |EVP_PKEY_encrypt|.
  434. //
  435. // It returns one on success or zero on error.
  436. OPENSSL_EXPORT int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx);
  437. // EVP_PKEY_encrypt encrypts |in_len| bytes from |in|. If |out| is NULL, the
  438. // maximum size of the ciphertext is written to |out_len|. Otherwise, |*out_len|
  439. // must contain the number of bytes of space available at |out|. If sufficient,
  440. // the ciphertext will be written to |out| and |*out_len| updated with the true
  441. // length.
  442. //
  443. // WARNING: Setting |out| to NULL only gives the maximum size of the
  444. // ciphertext. The actual ciphertext may be smaller.
  445. //
  446. // It returns one on success or zero on error.
  447. OPENSSL_EXPORT int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out,
  448. size_t *out_len, const uint8_t *in,
  449. size_t in_len);
  450. // EVP_PKEY_decrypt_init initialises an |EVP_PKEY_CTX| for a decryption
  451. // operation. It should be called before |EVP_PKEY_decrypt|.
  452. //
  453. // It returns one on success or zero on error.
  454. OPENSSL_EXPORT int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx);
  455. // EVP_PKEY_decrypt decrypts |in_len| bytes from |in|. If |out| is NULL, the
  456. // maximum size of the plaintext is written to |out_len|. Otherwise, |*out_len|
  457. // must contain the number of bytes of space available at |out|. If sufficient,
  458. // the ciphertext will be written to |out| and |*out_len| updated with the true
  459. // length.
  460. //
  461. // WARNING: Setting |out| to NULL only gives the maximum size of the
  462. // plaintext. The actual plaintext may be smaller.
  463. //
  464. // It returns one on success or zero on error.
  465. OPENSSL_EXPORT int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out,
  466. size_t *out_len, const uint8_t *in,
  467. size_t in_len);
  468. // EVP_PKEY_verify_recover_init initialises an |EVP_PKEY_CTX| for a public-key
  469. // decryption operation. It should be called before |EVP_PKEY_verify_recover|.
  470. //
  471. // Public-key decryption is a very obscure operation that is only implemented
  472. // by RSA keys. It is effectively a signature verification operation that
  473. // returns the signed message directly. It is almost certainly not what you
  474. // want.
  475. //
  476. // It returns one on success or zero on error.
  477. OPENSSL_EXPORT int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx);
  478. // EVP_PKEY_verify_recover decrypts |sig_len| bytes from |sig|. If |out| is
  479. // NULL, the maximum size of the plaintext is written to |out_len|. Otherwise,
  480. // |*out_len| must contain the number of bytes of space available at |out|. If
  481. // sufficient, the ciphertext will be written to |out| and |*out_len| updated
  482. // with the true length.
  483. //
  484. // WARNING: Setting |out| to NULL only gives the maximum size of the
  485. // plaintext. The actual plaintext may be smaller.
  486. //
  487. // See the warning about this operation in |EVP_PKEY_verify_recover_init|. It
  488. // is probably not what you want.
  489. //
  490. // It returns one on success or zero on error.
  491. OPENSSL_EXPORT int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out,
  492. size_t *out_len, const uint8_t *sig,
  493. size_t siglen);
  494. // EVP_PKEY_derive_init initialises an |EVP_PKEY_CTX| for a key derivation
  495. // operation. It should be called before |EVP_PKEY_derive_set_peer| and
  496. // |EVP_PKEY_derive|.
  497. //
  498. // It returns one on success or zero on error.
  499. OPENSSL_EXPORT int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx);
  500. // EVP_PKEY_derive_set_peer sets the peer's key to be used for key derivation
  501. // by |ctx| to |peer|. It should be called after |EVP_PKEY_derive_init|. (For
  502. // example, this is used to set the peer's key in (EC)DH.) It returns one on
  503. // success and zero on error.
  504. OPENSSL_EXPORT int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer);
  505. // EVP_PKEY_derive derives a shared key between the two keys configured in
  506. // |ctx|. If |key| is non-NULL then, on entry, |out_key_len| must contain the
  507. // amount of space at |key|. If sufficient then the shared key will be written
  508. // to |key| and |*out_key_len| will be set to the length. If |key| is NULL then
  509. // |out_key_len| will be set to the maximum length.
  510. //
  511. // WARNING: Setting |out| to NULL only gives the maximum size of the key. The
  512. // actual key may be smaller.
  513. //
  514. // It returns one on success and zero on error.
  515. OPENSSL_EXPORT int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, uint8_t *key,
  516. size_t *out_key_len);
  517. // EVP_PKEY_keygen_init initialises an |EVP_PKEY_CTX| for a key generation
  518. // operation. It should be called before |EVP_PKEY_keygen|.
  519. //
  520. // It returns one on success or zero on error.
  521. OPENSSL_EXPORT int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx);
  522. // EVP_PKEY_keygen performs a key generation operation using the values from
  523. // |ctx| and sets |*ppkey| to a fresh |EVP_PKEY| containing the resulting key.
  524. // It returns one on success or zero on error.
  525. OPENSSL_EXPORT int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
  526. // Generic control functions.
  527. // EVP_PKEY_CTX_set_signature_md sets |md| as the digest to be used in a
  528. // signature operation. It returns one on success or zero on error.
  529. OPENSSL_EXPORT int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx,
  530. const EVP_MD *md);
  531. // EVP_PKEY_CTX_get_signature_md sets |*out_md| to the digest to be used in a
  532. // signature operation. It returns one on success or zero on error.
  533. OPENSSL_EXPORT int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx,
  534. const EVP_MD **out_md);
  535. // RSA specific control functions.
  536. // EVP_PKEY_CTX_set_rsa_padding sets the padding type to use. It should be one
  537. // of the |RSA_*_PADDING| values. Returns one on success or zero on error.
  538. OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int padding);
  539. // EVP_PKEY_CTX_get_rsa_padding sets |*out_padding| to the current padding
  540. // value, which is one of the |RSA_*_PADDING| values. Returns one on success or
  541. // zero on error.
  542. OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx,
  543. int *out_padding);
  544. // EVP_PKEY_CTX_set_rsa_pss_saltlen sets the length of the salt in a PSS-padded
  545. // signature. A value of -1 cause the salt to be the same length as the digest
  546. // in the signature. A value of -2 causes the salt to be the maximum length
  547. // that will fit when signing and recovered from the signature when verifying.
  548. // Otherwise the value gives the size of the salt in bytes.
  549. //
  550. // If unsure, use -1.
  551. //
  552. // Returns one on success or zero on error.
  553. OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx,
  554. int salt_len);
  555. // EVP_PKEY_CTX_get_rsa_pss_saltlen sets |*out_salt_len| to the salt length of
  556. // a PSS-padded signature. See the documentation for
  557. // |EVP_PKEY_CTX_set_rsa_pss_saltlen| for details of the special values that it
  558. // can take.
  559. //
  560. // Returns one on success or zero on error.
  561. OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx,
  562. int *out_salt_len);
  563. // EVP_PKEY_CTX_set_rsa_keygen_bits sets the size of the desired RSA modulus,
  564. // in bits, for key generation. Returns one on success or zero on
  565. // error.
  566. OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx,
  567. int bits);
  568. // EVP_PKEY_CTX_set_rsa_keygen_pubexp sets |e| as the public exponent for key
  569. // generation. Returns one on success or zero on error.
  570. OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx,
  571. BIGNUM *e);
  572. // EVP_PKEY_CTX_set_rsa_oaep_md sets |md| as the digest used in OAEP padding.
  573. // Returns one on success or zero on error.
  574. OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx,
  575. const EVP_MD *md);
  576. // EVP_PKEY_CTX_get_rsa_oaep_md sets |*out_md| to the digest function used in
  577. // OAEP padding. Returns one on success or zero on error.
  578. OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx,
  579. const EVP_MD **out_md);
  580. // EVP_PKEY_CTX_set_rsa_mgf1_md sets |md| as the digest used in MGF1. Returns
  581. // one on success or zero on error.
  582. OPENSSL_EXPORT int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx,
  583. const EVP_MD *md);
  584. // EVP_PKEY_CTX_get_rsa_mgf1_md sets |*out_md| to the digest function used in
  585. // MGF1. Returns one on success or zero on error.
  586. OPENSSL_EXPORT int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx,
  587. const EVP_MD **out_md);
  588. // EVP_PKEY_CTX_set0_rsa_oaep_label sets |label_len| bytes from |label| as the
  589. // label used in OAEP. DANGER: On success, this call takes ownership of |label|
  590. // and will call |OPENSSL_free| on it when |ctx| is destroyed.
  591. //
  592. // Returns one on success or zero on error.
  593. OPENSSL_EXPORT int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx,
  594. uint8_t *label,
  595. size_t label_len);
  596. // EVP_PKEY_CTX_get0_rsa_oaep_label sets |*out_label| to point to the internal
  597. // buffer containing the OAEP label (which may be NULL) and returns the length
  598. // of the label or a negative value on error.
  599. //
  600. // WARNING: the return value differs from the usual return value convention.
  601. OPENSSL_EXPORT int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx,
  602. const uint8_t **out_label);
  603. // Deprecated functions.
  604. // EVP_PKEY_DH is defined for compatibility, but it is impossible to create an
  605. // |EVP_PKEY| of that type.
  606. #define EVP_PKEY_DH NID_dhKeyAgreement
  607. // EVP_PKEY_RSA2 was historically an alternate form for RSA public keys (OID
  608. // 2.5.8.1.1), but is no longer accepted.
  609. #define EVP_PKEY_RSA2 NID_rsa
  610. // OpenSSL_add_all_algorithms does nothing.
  611. OPENSSL_EXPORT void OpenSSL_add_all_algorithms(void);
  612. // OPENSSL_add_all_algorithms_conf does nothing.
  613. OPENSSL_EXPORT void OPENSSL_add_all_algorithms_conf(void);
  614. // OpenSSL_add_all_ciphers does nothing.
  615. OPENSSL_EXPORT void OpenSSL_add_all_ciphers(void);
  616. // OpenSSL_add_all_digests does nothing.
  617. OPENSSL_EXPORT void OpenSSL_add_all_digests(void);
  618. // EVP_cleanup does nothing.
  619. OPENSSL_EXPORT void EVP_cleanup(void);
  620. OPENSSL_EXPORT void EVP_CIPHER_do_all_sorted(
  621. void (*callback)(const EVP_CIPHER *cipher, const char *name,
  622. const char *unused, void *arg),
  623. void *arg);
  624. OPENSSL_EXPORT void EVP_MD_do_all_sorted(void (*callback)(const EVP_MD *cipher,
  625. const char *name,
  626. const char *unused,
  627. void *arg),
  628. void *arg);
  629. // i2d_PrivateKey marshals a private key from |key| to an ASN.1, DER
  630. // structure. If |outp| is not NULL then the result is written to |*outp| and
  631. // |*outp| is advanced just past the output. It returns the number of bytes in
  632. // the result, whether written or not, or a negative value on error.
  633. //
  634. // RSA keys are serialized as a DER-encoded RSAPublicKey (RFC 3447) structure.
  635. // EC keys are serialized as a DER-encoded ECPrivateKey (RFC 5915) structure.
  636. //
  637. // Use |RSA_marshal_private_key| or |EC_KEY_marshal_private_key| instead.
  638. OPENSSL_EXPORT int i2d_PrivateKey(const EVP_PKEY *key, uint8_t **outp);
  639. // i2d_PublicKey marshals a public key from |key| to a type-specific format.
  640. // If |outp| is not NULL then the result is written to |*outp| and
  641. // |*outp| is advanced just past the output. It returns the number of bytes in
  642. // the result, whether written or not, or a negative value on error.
  643. //
  644. // RSA keys are serialized as a DER-encoded RSAPublicKey (RFC 3447) structure.
  645. // EC keys are serialized as an EC point per SEC 1.
  646. //
  647. // Use |RSA_marshal_public_key| or |EC_POINT_point2cbb| instead.
  648. OPENSSL_EXPORT int i2d_PublicKey(EVP_PKEY *key, uint8_t **outp);
  649. // d2i_PrivateKey parses an ASN.1, DER-encoded, private key from |len| bytes at
  650. // |*inp|. If |out| is not NULL then, on exit, a pointer to the result is in
  651. // |*out|. Note that, even if |*out| is already non-NULL on entry, it will not
  652. // be written to. Rather, a fresh |EVP_PKEY| is allocated and the previous one
  653. // is freed. On successful exit, |*inp| is advanced past the DER structure. It
  654. // returns the result or NULL on error.
  655. //
  656. // This function tries to detect one of several formats. Instead, use
  657. // |EVP_parse_private_key| for a PrivateKeyInfo, |RSA_parse_private_key| for an
  658. // RSAPrivateKey, and |EC_parse_private_key| for an ECPrivateKey.
  659. OPENSSL_EXPORT EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **out,
  660. const uint8_t **inp, long len);
  661. // d2i_AutoPrivateKey acts the same as |d2i_PrivateKey|, but detects the type
  662. // of the private key.
  663. //
  664. // This function tries to detect one of several formats. Instead, use
  665. // |EVP_parse_private_key| for a PrivateKeyInfo, |RSA_parse_private_key| for an
  666. // RSAPrivateKey, and |EC_parse_private_key| for an ECPrivateKey.
  667. OPENSSL_EXPORT EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **out, const uint8_t **inp,
  668. long len);
  669. // EVP_PKEY_get0_DH returns NULL.
  670. OPENSSL_EXPORT DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey);
  671. // Private structures.
  672. struct evp_pkey_st {
  673. CRYPTO_refcount_t references;
  674. // type contains one of the EVP_PKEY_* values or NID_undef and determines
  675. // which element (if any) of the |pkey| union is valid.
  676. int type;
  677. union {
  678. void *ptr;
  679. RSA *rsa;
  680. DSA *dsa;
  681. DH *dh;
  682. EC_KEY *ec;
  683. } pkey;
  684. // ameth contains a pointer to a method table that contains many ASN.1
  685. // methods for the key type.
  686. const EVP_PKEY_ASN1_METHOD *ameth;
  687. } /* EVP_PKEY */;
  688. #if defined(__cplusplus)
  689. } // extern C
  690. extern "C++" {
  691. namespace bssl {
  692. BORINGSSL_MAKE_DELETER(EVP_PKEY, EVP_PKEY_free)
  693. BORINGSSL_MAKE_DELETER(EVP_PKEY_CTX, EVP_PKEY_CTX_free)
  694. } // namespace bssl
  695. } // extern C++
  696. #endif
  697. #define EVP_R_BUFFER_TOO_SMALL 100
  698. #define EVP_R_COMMAND_NOT_SUPPORTED 101
  699. #define EVP_R_DECODE_ERROR 102
  700. #define EVP_R_DIFFERENT_KEY_TYPES 103
  701. #define EVP_R_DIFFERENT_PARAMETERS 104
  702. #define EVP_R_ENCODE_ERROR 105
  703. #define EVP_R_EXPECTING_AN_EC_KEY_KEY 106
  704. #define EVP_R_EXPECTING_AN_RSA_KEY 107
  705. #define EVP_R_EXPECTING_A_DSA_KEY 108
  706. #define EVP_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE 109
  707. #define EVP_R_INVALID_DIGEST_LENGTH 110
  708. #define EVP_R_INVALID_DIGEST_TYPE 111
  709. #define EVP_R_INVALID_KEYBITS 112
  710. #define EVP_R_INVALID_MGF1_MD 113
  711. #define EVP_R_INVALID_OPERATION 114
  712. #define EVP_R_INVALID_PADDING_MODE 115
  713. #define EVP_R_INVALID_PSS_SALTLEN 116
  714. #define EVP_R_KEYS_NOT_SET 117
  715. #define EVP_R_MISSING_PARAMETERS 118
  716. #define EVP_R_NO_DEFAULT_DIGEST 119
  717. #define EVP_R_NO_KEY_SET 120
  718. #define EVP_R_NO_MDC2_SUPPORT 121
  719. #define EVP_R_NO_NID_FOR_CURVE 122
  720. #define EVP_R_NO_OPERATION_SET 123
  721. #define EVP_R_NO_PARAMETERS_SET 124
  722. #define EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE 125
  723. #define EVP_R_OPERATON_NOT_INITIALIZED 126
  724. #define EVP_R_UNKNOWN_PUBLIC_KEY_TYPE 127
  725. #define EVP_R_UNSUPPORTED_ALGORITHM 128
  726. #define EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE 129
  727. #define EVP_R_NOT_A_PRIVATE_KEY 130
  728. #define EVP_R_INVALID_SIGNATURE 131
  729. #define EVP_R_MEMORY_LIMIT_EXCEEDED 132
  730. #define EVP_R_INVALID_PARAMETERS 133
  731. #endif // OPENSSL_HEADER_EVP_H