evp.h 49 KB

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