No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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