Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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