EVP_DigestInit.pod 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. =pod
  2. =head1 NAME
  3. EVP_MD_fetch, EVP_MD_up_ref, EVP_MD_free,
  4. EVP_MD_get_params, EVP_MD_gettable_params,
  5. EVP_MD_CTX_new, EVP_MD_CTX_reset, EVP_MD_CTX_free, EVP_MD_CTX_dup,
  6. EVP_MD_CTX_copy, EVP_MD_CTX_copy_ex, EVP_MD_CTX_ctrl,
  7. EVP_MD_CTX_set_params, EVP_MD_CTX_get_params,
  8. EVP_MD_settable_ctx_params, EVP_MD_gettable_ctx_params,
  9. EVP_MD_CTX_settable_params, EVP_MD_CTX_gettable_params,
  10. EVP_MD_CTX_set_flags, EVP_MD_CTX_clear_flags, EVP_MD_CTX_test_flags,
  11. EVP_Q_digest, EVP_Digest, EVP_DigestInit_ex2, EVP_DigestInit_ex, EVP_DigestInit,
  12. EVP_DigestUpdate, EVP_DigestFinal_ex, EVP_DigestFinalXOF, EVP_DigestFinal,
  13. EVP_MD_is_a, EVP_MD_get0_name, EVP_MD_get0_description,
  14. EVP_MD_names_do_all, EVP_MD_get0_provider, EVP_MD_get_type,
  15. EVP_MD_get_pkey_type, EVP_MD_get_size, EVP_MD_get_block_size, EVP_MD_get_flags,
  16. EVP_MD_CTX_get0_name, EVP_MD_CTX_md, EVP_MD_CTX_get0_md, EVP_MD_CTX_get1_md,
  17. EVP_MD_CTX_get_type, EVP_MD_CTX_get_size, EVP_MD_CTX_get_block_size,
  18. EVP_MD_CTX_get0_md_data, EVP_MD_CTX_update_fn, EVP_MD_CTX_set_update_fn,
  19. EVP_md_null,
  20. EVP_get_digestbyname, EVP_get_digestbynid, EVP_get_digestbyobj,
  21. EVP_MD_CTX_get_pkey_ctx, EVP_MD_CTX_set_pkey_ctx,
  22. EVP_MD_do_all_provided,
  23. EVP_MD_type, EVP_MD_nid, EVP_MD_name, EVP_MD_pkey_type, EVP_MD_size,
  24. EVP_MD_block_size, EVP_MD_flags, EVP_MD_CTX_size, EVP_MD_CTX_block_size,
  25. EVP_MD_CTX_type, EVP_MD_CTX_pkey_ctx, EVP_MD_CTX_md_data
  26. - EVP digest routines
  27. =head1 SYNOPSIS
  28. #include <openssl/evp.h>
  29. EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
  30. const char *properties);
  31. int EVP_MD_up_ref(EVP_MD *md);
  32. void EVP_MD_free(EVP_MD *md);
  33. int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[]);
  34. const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest);
  35. EVP_MD_CTX *EVP_MD_CTX_new(void);
  36. int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
  37. void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
  38. void EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void* p2);
  39. int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[]);
  40. int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[]);
  41. const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md);
  42. const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md);
  43. const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx);
  44. const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx);
  45. void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
  46. void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags);
  47. int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags);
  48. int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
  49. const void *data, size_t datalen,
  50. unsigned char *md, size_t *mdlen);
  51. int EVP_Digest(const void *data, size_t count, unsigned char *md,
  52. unsigned int *size, const EVP_MD *type, ENGINE *impl);
  53. int EVP_DigestInit_ex2(EVP_MD_CTX *ctx, const EVP_MD *type,
  54. const OSSL_PARAM params[]);
  55. int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
  56. int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
  57. int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
  58. int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t len);
  59. EVP_MD_CTX *EVP_MD_CTX_dup(const EVP_MD_CTX *in);
  60. int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
  61. int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
  62. int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
  63. int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
  64. const char *EVP_MD_get0_name(const EVP_MD *md);
  65. const char *EVP_MD_get0_description(const EVP_MD *md);
  66. int EVP_MD_is_a(const EVP_MD *md, const char *name);
  67. int EVP_MD_names_do_all(const EVP_MD *md,
  68. void (*fn)(const char *name, void *data),
  69. void *data);
  70. const OSSL_PROVIDER *EVP_MD_get0_provider(const EVP_MD *md);
  71. int EVP_MD_get_type(const EVP_MD *md);
  72. int EVP_MD_get_pkey_type(const EVP_MD *md);
  73. int EVP_MD_get_size(const EVP_MD *md);
  74. int EVP_MD_get_block_size(const EVP_MD *md);
  75. unsigned long EVP_MD_get_flags(const EVP_MD *md);
  76. const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx);
  77. EVP_MD *EVP_MD_CTX_get1_md(EVP_MD_CTX *ctx);
  78. const char *EVP_MD_CTX_get0_name(const EVP_MD_CTX *ctx);
  79. int EVP_MD_CTX_get_size(const EVP_MD_CTX *ctx);
  80. int EVP_MD_CTX_get_block_size(const EVP_MD_CTX *ctx);
  81. int EVP_MD_CTX_get_type(const EVP_MD_CTX *ctx);
  82. void *EVP_MD_CTX_get0_md_data(const EVP_MD_CTX *ctx);
  83. const EVP_MD *EVP_md_null(void);
  84. const EVP_MD *EVP_get_digestbyname(const char *name);
  85. const EVP_MD *EVP_get_digestbynid(int type);
  86. const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
  87. EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx);
  88. void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx);
  89. void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
  90. void (*fn)(EVP_MD *mac, void *arg),
  91. void *arg);
  92. #define EVP_MD_type EVP_MD_get_type
  93. #define EVP_MD_nid EVP_MD_get_type
  94. #define EVP_MD_name EVP_MD_get0_name
  95. #define EVP_MD_pkey_type EVP_MD_get_pkey_type
  96. #define EVP_MD_size EVP_MD_get_size
  97. #define EVP_MD_block_size EVP_MD_get_block_size
  98. #define EVP_MD_flags EVP_MD_get_flags
  99. #define EVP_MD_CTX_size EVP_MD_CTX_get_size
  100. #define EVP_MD_CTX_block_size EVP_MD_CTX_get_block_size
  101. #define EVP_MD_CTX_type EVP_MD_CTX_get_type
  102. #define EVP_MD_CTX_pkey_ctx EVP_MD_CTX_get_pkey_ctx
  103. #define EVP_MD_CTX_md_data EVP_MD_CTX_get0_md_data
  104. The following functions have been deprecated since OpenSSL 3.0, and can be
  105. hidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
  106. see L<openssl_user_macros(7)>:
  107. const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
  108. int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
  109. const void *data, size_t count);
  110. void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
  111. int (*update)(EVP_MD_CTX *ctx,
  112. const void *data, size_t count));
  113. =head1 DESCRIPTION
  114. The EVP digest routines are a high-level interface to message digests,
  115. and should be used instead of the digest-specific functions.
  116. The B<EVP_MD> type is a structure for digest method implementation.
  117. =over 4
  118. =item EVP_MD_fetch()
  119. Fetches the digest implementation for the given I<algorithm> from any
  120. provider offering it, within the criteria given by the I<properties>.
  121. See L<crypto(7)/ALGORITHM FETCHING> for further information.
  122. The returned value must eventually be freed with EVP_MD_free().
  123. Fetched B<EVP_MD> structures are reference counted.
  124. =item EVP_MD_up_ref()
  125. Increments the reference count for an B<EVP_MD> structure.
  126. =item EVP_MD_free()
  127. Decrements the reference count for the fetched B<EVP_MD> structure.
  128. If the reference count drops to 0 then the structure is freed.
  129. =item EVP_MD_CTX_new()
  130. Allocates and returns a digest context.
  131. =item EVP_MD_CTX_reset()
  132. Resets the digest context I<ctx>. This can be used to reuse an already
  133. existing context.
  134. =item EVP_MD_CTX_free()
  135. Cleans up digest context I<ctx> and frees up the space allocated to it.
  136. =item EVP_MD_CTX_ctrl()
  137. I<This is a legacy method. EVP_MD_CTX_set_params() and EVP_MD_CTX_get_params()
  138. is the mechanism that should be used to set and get parameters that are used by
  139. providers.>
  140. Performs digest-specific control actions on context I<ctx>. The control command
  141. is indicated in I<cmd> and any additional arguments in I<p1> and I<p2>.
  142. EVP_MD_CTX_ctrl() must be called after EVP_DigestInit_ex2(). Other restrictions
  143. may apply depending on the control type and digest implementation.
  144. If this function happens to be used with a fetched B<EVP_MD>, it will
  145. translate the controls that are known to OpenSSL into L<OSSL_PARAM(3)>
  146. parameters with keys defined by OpenSSL and call EVP_MD_CTX_get_params() or
  147. EVP_MD_CTX_set_params() as is appropriate for each control command.
  148. See L</CONTROLS> below for more information, including what translations are
  149. being done.
  150. =item EVP_MD_get_params()
  151. Retrieves the requested list of I<params> from a MD I<md>.
  152. See L</PARAMETERS> below for more information.
  153. =item EVP_MD_CTX_get_params()
  154. Retrieves the requested list of I<params> from a MD context I<ctx>.
  155. See L</PARAMETERS> below for more information.
  156. =item EVP_MD_CTX_set_params()
  157. Sets the list of I<params> into a MD context I<ctx>.
  158. See L</PARAMETERS> below for more information.
  159. =item EVP_MD_gettable_params()
  160. Get a constant L<OSSL_PARAM(3)> array that describes the retrievable parameters
  161. that can be used with EVP_MD_get_params().
  162. =item EVP_MD_gettable_ctx_params(), EVP_MD_CTX_gettable_params()
  163. Get a constant L<OSSL_PARAM(3)> array that describes the retrievable parameters
  164. that can be used with EVP_MD_CTX_get_params(). EVP_MD_gettable_ctx_params()
  165. returns the parameters that can be retrieved from the algorithm, whereas
  166. EVP_MD_CTX_gettable_params() returns the parameters that can be retrieved
  167. in the context's current state.
  168. =item EVP_MD_settable_ctx_params(), EVP_MD_CTX_settable_params()
  169. Get a constant L<OSSL_PARAM(3)> array that describes the settable parameters
  170. that can be used with EVP_MD_CTX_set_params(). EVP_MD_settable_ctx_params()
  171. returns the parameters that can be set from the algorithm, whereas
  172. EVP_MD_CTX_settable_params() returns the parameters that can be set in the
  173. context's current state.
  174. =item EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags(), EVP_MD_CTX_test_flags()
  175. Sets, clears and tests I<ctx> flags. See L</FLAGS> below for more information.
  176. =item EVP_Q_digest() is a quick one-shot digest function.
  177. It hashes I<datalen> bytes of data at I<data> using the digest algorithm
  178. I<name>, which is fetched using the optional I<libctx> and I<propq> parameters.
  179. The digest value is placed in I<md> and its length is written at I<mdlen>
  180. if the pointer is not NULL. At most B<EVP_MAX_MD_SIZE> bytes will be written.
  181. =item EVP_Digest()
  182. A wrapper around the Digest Init_ex, Update and Final_ex functions.
  183. Hashes I<count> bytes of data at I<data> using a digest I<type> from ENGINE
  184. I<impl>. The digest value is placed in I<md> and its length is written at I<size>
  185. if the pointer is not NULL. At most B<EVP_MAX_MD_SIZE> bytes will be written.
  186. If I<impl> is NULL the default implementation of digest I<type> is used.
  187. =item EVP_DigestInit_ex2()
  188. Sets up digest context I<ctx> to use a digest I<type>.
  189. I<type> is typically supplied by a function such as EVP_sha1(), or a
  190. value explicitly fetched with EVP_MD_fetch().
  191. The parameters B<params> are set on the context after initialisation.
  192. The I<type> parameter can be NULL if I<ctx> has been already initialized
  193. with another EVP_DigestInit_ex() call and has not been reset with
  194. EVP_MD_CTX_reset().
  195. =item EVP_DigestInit_ex()
  196. Sets up digest context I<ctx> to use a digest I<type>.
  197. I<type> is typically supplied by a function such as EVP_sha1(), or a
  198. value explicitly fetched with EVP_MD_fetch().
  199. If I<impl> is non-NULL, its implementation of the digest I<type> is used if
  200. there is one, and if not, the default implementation is used.
  201. The I<type> parameter can be NULL if I<ctx> has been already initialized
  202. with another EVP_DigestInit_ex() call and has not been reset with
  203. EVP_MD_CTX_reset().
  204. =item EVP_DigestUpdate()
  205. Hashes I<cnt> bytes of data at I<d> into the digest context I<ctx>. This
  206. function can be called several times on the same I<ctx> to hash additional
  207. data.
  208. =item EVP_DigestFinal_ex()
  209. Retrieves the digest value from I<ctx> and places it in I<md>. If the I<s>
  210. parameter is not NULL then the number of bytes of data written (i.e. the
  211. length of the digest) will be written to the integer at I<s>, at most
  212. B<EVP_MAX_MD_SIZE> bytes will be written. After calling EVP_DigestFinal_ex()
  213. no additional calls to EVP_DigestUpdate() can be made, but
  214. EVP_DigestInit_ex2() can be called to initialize a new digest operation.
  215. =item EVP_DigestFinalXOF()
  216. Interfaces to extendable-output functions, XOFs, such as SHAKE128 and SHAKE256.
  217. It retrieves the digest value from I<ctx> and places it in I<len>-sized I<md>.
  218. After calling this function no additional calls to EVP_DigestUpdate() can be
  219. made, but EVP_DigestInit_ex2() can be called to initialize a new operation.
  220. =item EVP_MD_CTX_dup()
  221. Can be used to duplicate the message digest state from I<in>. This is useful
  222. to avoid multiple EVP_MD_fetch() calls or if large amounts of data are to be
  223. hashed which only differ in the last few bytes.
  224. =item EVP_MD_CTX_copy_ex()
  225. Can be used to copy the message digest state from I<in> to I<out>. This is
  226. useful if large amounts of data are to be hashed which only differ in the last
  227. few bytes.
  228. =item EVP_DigestInit()
  229. Behaves in the same way as EVP_DigestInit_ex2() except it doesn't set any
  230. parameters and calls EVP_MD_CTX_reset() so it cannot be used with an I<type>
  231. of NULL.
  232. =item EVP_DigestFinal()
  233. Similar to EVP_DigestFinal_ex() except after computing the digest
  234. the digest context I<ctx> is automatically cleaned up with EVP_MD_CTX_reset().
  235. =item EVP_MD_CTX_copy()
  236. Similar to EVP_MD_CTX_copy_ex() except the destination I<out> does not have to
  237. be initialized.
  238. =item EVP_MD_is_a()
  239. Returns 1 if I<md> is an implementation of an algorithm that's
  240. identifiable with I<name>, otherwise 0.
  241. If I<md> is a legacy digest (it's the return value from the likes of
  242. EVP_sha256() rather than the result of an EVP_MD_fetch()), only cipher
  243. names registered with the default library context (see
  244. L<OSSL_LIB_CTX(3)>) will be considered.
  245. =item EVP_MD_get0_name(),
  246. EVP_MD_CTX_get0_name()
  247. Return the name of the given message digest. For fetched message
  248. digests with multiple names, only one of them is returned; it's
  249. recommended to use EVP_MD_names_do_all() instead.
  250. =item EVP_MD_names_do_all()
  251. Traverses all names for the I<md>, and calls I<fn> with each name and
  252. I<data>. This is only useful with fetched B<EVP_MD>s.
  253. =item EVP_MD_get0_description()
  254. Returns a description of the digest, meant for display and human consumption.
  255. The description is at the discretion of the digest implementation.
  256. =item EVP_MD_get0_provider()
  257. Returns an B<OSSL_PROVIDER> pointer to the provider that implements the given
  258. B<EVP_MD>.
  259. =item EVP_MD_get_size(),
  260. EVP_MD_CTX_get_size()
  261. Return the size of the message digest when passed an B<EVP_MD> or an
  262. B<EVP_MD_CTX> structure, i.e. the size of the hash.
  263. =item EVP_MD_get_block_size(),
  264. EVP_MD_CTX_get_block_size()
  265. Return the block size of the message digest when passed an B<EVP_MD> or an
  266. B<EVP_MD_CTX> structure.
  267. =item EVP_MD_get_type(),
  268. EVP_MD_CTX_get_type()
  269. Return the NID of the OBJECT IDENTIFIER representing the given message digest
  270. when passed an B<EVP_MD> structure. For example, C<EVP_MD_get_type(EVP_sha1())>
  271. returns B<NID_sha1>. This function is normally used when setting ASN1 OIDs.
  272. =item EVP_MD_CTX_get0_md_data()
  273. Return the digest method private data for the passed B<EVP_MD_CTX>.
  274. The space is allocated by OpenSSL and has the size originally set with
  275. EVP_MD_meth_set_app_datasize().
  276. =item EVP_MD_CTX_get0_md(), EVP_MD_CTX_get1_md()
  277. EVP_MD_CTX_get0_md() returns
  278. the B<EVP_MD> structure corresponding to the passed B<EVP_MD_CTX>. This
  279. will be the same B<EVP_MD> object originally passed to EVP_DigestInit_ex2() (or
  280. other similar function) when the EVP_MD_CTX was first initialised. Note that
  281. where explicit fetch is in use (see L<EVP_MD_fetch(3)>) the value returned from
  282. this function will not have its reference count incremented and therefore it
  283. should not be used after the EVP_MD_CTX is freed.
  284. EVP_MD_CTX_get1_md() is the same except the ownership is passed to the
  285. caller and is from the passed B<EVP_MD_CTX>.
  286. =item EVP_MD_CTX_set_update_fn()
  287. Sets the update function for I<ctx> to I<update>.
  288. This is the function that is called by EVP_DigestUpdate(). If not set, the
  289. update function from the B<EVP_MD> type specified at initialization is used.
  290. =item EVP_MD_CTX_update_fn()
  291. Returns the update function for I<ctx>.
  292. =item EVP_MD_get_flags()
  293. Returns the I<md> flags. Note that these are different from the B<EVP_MD_CTX>
  294. ones. See L<EVP_MD_meth_set_flags(3)> for more information.
  295. =item EVP_MD_get_pkey_type()
  296. Returns the NID of the public key signing algorithm associated with this
  297. digest. For example EVP_sha1() is associated with RSA so this will return
  298. B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms are no
  299. longer linked this function is only retained for compatibility reasons.
  300. =item EVP_md_null()
  301. A "null" message digest that does nothing: i.e. the hash it returns is of zero
  302. length.
  303. =item EVP_get_digestbyname(),
  304. EVP_get_digestbynid(),
  305. EVP_get_digestbyobj()
  306. Returns an B<EVP_MD> structure when passed a digest name, a digest B<NID> or an
  307. B<ASN1_OBJECT> structure respectively.
  308. The EVP_get_digestbyname() function is present for backwards compatibility with
  309. OpenSSL prior to version 3 and is different to the EVP_MD_fetch() function
  310. since it does not attempt to "fetch" an implementation of the cipher.
  311. Additionally, it only knows about digests that are built-in to OpenSSL and have
  312. an associated NID. Similarly EVP_get_digestbynid() and EVP_get_digestbyobj()
  313. also return objects without an associated implementation.
  314. When the digest objects returned by these functions are used (such as in a call
  315. to EVP_DigestInit_ex()) an implementation of the digest will be implicitly
  316. fetched from the loaded providers. This fetch could fail if no suitable
  317. implementation is available. Use EVP_MD_fetch() instead to explicitly fetch
  318. the algorithm and an associated implementation from a provider.
  319. See L<crypto(7)/ALGORITHM FETCHING> for more information about fetching.
  320. The digest objects returned from these functions do not need to be freed with
  321. EVP_MD_free().
  322. =item EVP_MD_CTX_get_pkey_ctx()
  323. Returns the B<EVP_PKEY_CTX> assigned to I<ctx>. The returned pointer should not
  324. be freed by the caller.
  325. =item EVP_MD_CTX_set_pkey_ctx()
  326. Assigns an B<EVP_PKEY_CTX> to B<EVP_MD_CTX>. This is usually used to provide
  327. a customized B<EVP_PKEY_CTX> to L<EVP_DigestSignInit(3)> or
  328. L<EVP_DigestVerifyInit(3)>. The I<pctx> passed to this function should be freed
  329. by the caller. A NULL I<pctx> pointer is also allowed to clear the B<EVP_PKEY_CTX>
  330. assigned to I<ctx>. In such case, freeing the cleared B<EVP_PKEY_CTX> or not
  331. depends on how the B<EVP_PKEY_CTX> is created.
  332. =item EVP_MD_do_all_provided()
  333. Traverses all messages digests implemented by all activated providers
  334. in the given library context I<libctx>, and for each of the implementations,
  335. calls the given function I<fn> with the implementation method and the given
  336. I<arg> as argument.
  337. =back
  338. =head1 PARAMETERS
  339. See L<OSSL_PARAM(3)> for information about passing parameters.
  340. EVP_MD_CTX_set_params() can be used with the following OSSL_PARAM keys:
  341. =over 4
  342. =item "xoflen" (B<OSSL_DIGEST_PARAM_XOFLEN>) <unsigned integer>
  343. Sets the digest length for extendable output functions.
  344. It is used by the SHAKE algorithm and should not exceed what can be given
  345. using a B<size_t>.
  346. =item "pad-type" (B<OSSL_DIGEST_PARAM_PAD_TYPE>) <unsigned integer>
  347. Sets the padding type.
  348. It is used by the MDC2 algorithm.
  349. =back
  350. EVP_MD_CTX_get_params() can be used with the following OSSL_PARAM keys:
  351. =over 4
  352. =item "micalg" (B<OSSL_PARAM_DIGEST_KEY_MICALG>) <UTF8 string>.
  353. Gets the digest Message Integrity Check algorithm string. This is used when
  354. creating S/MIME multipart/signed messages, as specified in RFC 3851.
  355. It may be used by external engines or providers.
  356. =back
  357. =head1 CONTROLS
  358. EVP_MD_CTX_ctrl() can be used to send the following standard controls:
  359. =over 4
  360. =item EVP_MD_CTRL_MICALG
  361. Gets the digest Message Integrity Check algorithm string. This is used when
  362. creating S/MIME multipart/signed messages, as specified in RFC 3851.
  363. The string value is written to I<p2>.
  364. When used with a fetched B<EVP_MD>, EVP_MD_CTX_get_params() gets called with
  365. an L<OSSL_PARAM(3)> item with the key "micalg" (B<OSSL_DIGEST_PARAM_MICALG>).
  366. =item EVP_MD_CTRL_XOF_LEN
  367. This control sets the digest length for extendable output functions to I<p1>.
  368. Sending this control directly should not be necessary, the use of
  369. EVP_DigestFinalXOF() is preferred.
  370. Currently used by SHAKE.
  371. When used with a fetched B<EVP_MD>, EVP_MD_CTX_get_params() gets called with
  372. an L<OSSL_PARAM(3)> item with the key "xoflen" (B<OSSL_DIGEST_PARAM_XOFLEN>).
  373. =back
  374. =head1 FLAGS
  375. EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags() and EVP_MD_CTX_test_flags()
  376. can be used the manipulate and test these B<EVP_MD_CTX> flags:
  377. =over 4
  378. =item EVP_MD_CTX_FLAG_ONESHOT
  379. This flag instructs the digest to optimize for one update only, if possible.
  380. =for comment EVP_MD_CTX_FLAG_CLEANED is internal, don't mention it
  381. =for comment EVP_MD_CTX_FLAG_REUSE is internal, don't mention it
  382. =for comment We currently avoid documenting flags that are only bit holder:
  383. EVP_MD_CTX_FLAG_NON_FIPS_ALLOW, EVP_MD_CTX_FLAGS_PAD_*
  384. =item EVP_MD_CTX_FLAG_NO_INIT
  385. This flag instructs EVP_DigestInit() and similar not to initialise the
  386. implementation specific data.
  387. =item EVP_MD_CTX_FLAG_FINALISE
  388. Some functions such as EVP_DigestSign only finalise copies of internal
  389. contexts so additional data can be included after the finalisation call.
  390. This is inefficient if this functionality is not required, and can be
  391. disabled with this flag.
  392. =back
  393. =head1 RETURN VALUES
  394. =over 4
  395. =item EVP_MD_fetch()
  396. Returns a pointer to a B<EVP_MD> for success or NULL for failure.
  397. =item EVP_MD_up_ref()
  398. Returns 1 for success or 0 for failure.
  399. =item EVP_Q_digest(),
  400. EVP_Digest(),
  401. EVP_DigestInit_ex2(),
  402. EVP_DigestInit_ex(),
  403. EVP_DigestInit(),
  404. EVP_DigestUpdate(),
  405. EVP_DigestFinal_ex(),
  406. EVP_DigestFinalXOF(), and
  407. EVP_DigestFinal()
  408. return 1 for
  409. success and 0 for failure.
  410. =item EVP_MD_CTX_ctrl()
  411. Returns 1 if successful or 0 for failure.
  412. =item EVP_MD_CTX_set_params(),
  413. EVP_MD_CTX_get_params()
  414. Returns 1 if successful or 0 for failure.
  415. =item EVP_MD_CTX_settable_params(),
  416. EVP_MD_CTX_gettable_params()
  417. Return an array of constant L<OSSL_PARAM(3)>s, or NULL if there is none
  418. to get.
  419. =item EVP_MD_CTX_dup()
  420. Returns a new EVP_MD_CTX if successful or NULL on failure.
  421. =item EVP_MD_CTX_copy_ex()
  422. Returns 1 if successful or 0 for failure.
  423. =item EVP_MD_get_type(),
  424. EVP_MD_get_pkey_type()
  425. Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef if none
  426. exists.
  427. =item EVP_MD_get_size(),
  428. EVP_MD_get_block_size(),
  429. EVP_MD_CTX_get_size(),
  430. EVP_MD_CTX_get_block_size()
  431. Returns the digest or block size in bytes or -1 for failure.
  432. =item EVP_md_null()
  433. Returns a pointer to the B<EVP_MD> structure of the "null" message digest.
  434. =item EVP_get_digestbyname(),
  435. EVP_get_digestbynid(),
  436. EVP_get_digestbyobj()
  437. Returns either an B<EVP_MD> structure or NULL if an error occurs.
  438. =item EVP_MD_CTX_set_pkey_ctx()
  439. This function has no return value.
  440. =item EVP_MD_names_do_all()
  441. Returns 1 if the callback was called for all names. A return value of 0 means
  442. that the callback was not called for any names.
  443. =back
  444. =head1 NOTES
  445. The B<EVP> interface to message digests should almost always be used in
  446. preference to the low-level interfaces. This is because the code then becomes
  447. transparent to the digest used and much more flexible.
  448. New applications should use the SHA-2 (such as L<EVP_sha256(3)>) or the SHA-3
  449. digest algorithms (such as L<EVP_sha3_512(3)>). The other digest algorithms
  450. are still in common use.
  451. For most applications the I<impl> parameter to EVP_DigestInit_ex() will be
  452. set to NULL to use the default digest implementation.
  453. Ignoring failure returns of EVP_DigestInit_ex(), EVP_DigestInit_ex2(), or
  454. EVP_DigestInit() can lead to undefined behavior on subsequent calls
  455. updating or finalizing the B<EVP_MD_CTX> such as the EVP_DigestUpdate() or
  456. EVP_DigestFinal() functions. The only valid calls on the B<EVP_MD_CTX>
  457. when initialization fails are calls that attempt another initialization of
  458. the context or release the context.
  459. The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are
  460. obsolete but are retained to maintain compatibility with existing code. New
  461. applications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and
  462. EVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
  463. instead of initializing and cleaning it up on each call and allow non default
  464. implementations of digests to be specified.
  465. If digest contexts are not cleaned up after use,
  466. memory leaks will occur.
  467. EVP_MD_CTX_get0_name(), EVP_MD_CTX_get_size(), EVP_MD_CTX_get_block_size(),
  468. EVP_MD_CTX_get_type(), EVP_get_digestbynid() and EVP_get_digestbyobj() are
  469. defined as macros.
  470. EVP_MD_CTX_ctrl() sends commands to message digests for additional configuration
  471. or control.
  472. =head1 EXAMPLES
  473. This example digests the data "Test Message\n" and "Hello World\n", using the
  474. digest name passed on the command line.
  475. #include <stdio.h>
  476. #include <string.h>
  477. #include <openssl/evp.h>
  478. int main(int argc, char *argv[])
  479. {
  480. EVP_MD_CTX *mdctx;
  481. const EVP_MD *md;
  482. char mess1[] = "Test Message\n";
  483. char mess2[] = "Hello World\n";
  484. unsigned char md_value[EVP_MAX_MD_SIZE];
  485. unsigned int md_len, i;
  486. if (argv[1] == NULL) {
  487. printf("Usage: mdtest digestname\n");
  488. exit(1);
  489. }
  490. md = EVP_get_digestbyname(argv[1]);
  491. if (md == NULL) {
  492. printf("Unknown message digest %s\n", argv[1]);
  493. exit(1);
  494. }
  495. mdctx = EVP_MD_CTX_new();
  496. if (!EVP_DigestInit_ex2(mdctx, md, NULL)) {
  497. printf("Message digest initialization failed.\n");
  498. EVP_MD_CTX_free(mdctx);
  499. exit(1);
  500. }
  501. if (!EVP_DigestUpdate(mdctx, mess1, strlen(mess1))) {
  502. printf("Message digest update failed.\n");
  503. EVP_MD_CTX_free(mdctx);
  504. exit(1);
  505. }
  506. if (!EVP_DigestUpdate(mdctx, mess2, strlen(mess2))) {
  507. printf("Message digest update failed.\n");
  508. EVP_MD_CTX_free(mdctx);
  509. exit(1);
  510. }
  511. if (!EVP_DigestFinal_ex(mdctx, md_value, &md_len)) {
  512. printf("Message digest finalization failed.\n");
  513. EVP_MD_CTX_free(mdctx);
  514. exit(1);
  515. }
  516. EVP_MD_CTX_free(mdctx);
  517. printf("Digest is: ");
  518. for (i = 0; i < md_len; i++)
  519. printf("%02x", md_value[i]);
  520. printf("\n");
  521. exit(0);
  522. }
  523. =head1 SEE ALSO
  524. L<EVP_MD_meth_new(3)>,
  525. L<openssl-dgst(1)>,
  526. L<evp(7)>,
  527. L<OSSL_PROVIDER(3)>,
  528. L<OSSL_PARAM(3)>,
  529. L<property(7)>,
  530. L<crypto(7)/ALGORITHM FETCHING>,
  531. L<provider-digest(7)>,
  532. L<life_cycle-digest(7)>
  533. The full list of digest algorithms are provided below.
  534. L<EVP_blake2b512(3)>,
  535. L<EVP_md2(3)>,
  536. L<EVP_md4(3)>,
  537. L<EVP_md5(3)>,
  538. L<EVP_mdc2(3)>,
  539. L<EVP_ripemd160(3)>,
  540. L<EVP_sha1(3)>,
  541. L<EVP_sha224(3)>,
  542. L<EVP_sha3_224(3)>,
  543. L<EVP_sm3(3)>,
  544. L<EVP_whirlpool(3)>
  545. =head1 HISTORY
  546. The EVP_MD_CTX_create() and EVP_MD_CTX_destroy() functions were renamed to
  547. EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0, respectively.
  548. The link between digests and signing algorithms was fixed in OpenSSL 1.0 and
  549. later, so now EVP_sha1() can be used with RSA and DSA.
  550. The EVP_dss1() function was removed in OpenSSL 1.1.0.
  551. The EVP_MD_CTX_set_pkey_ctx() function was added in OpenSSL 1.1.1.
  552. The EVP_Q_digest(), EVP_DigestInit_ex2(),
  553. EVP_MD_fetch(), EVP_MD_free(), EVP_MD_up_ref(),
  554. EVP_MD_get_params(), EVP_MD_CTX_set_params(), EVP_MD_CTX_get_params(),
  555. EVP_MD_gettable_params(), EVP_MD_gettable_ctx_params(),
  556. EVP_MD_settable_ctx_params(), EVP_MD_CTX_settable_params() and
  557. EVP_MD_CTX_gettable_params() functions were added in OpenSSL 3.0.
  558. The EVP_MD_type(), EVP_MD_nid(), EVP_MD_name(), EVP_MD_pkey_type(),
  559. EVP_MD_size(), EVP_MD_block_size(), EVP_MD_flags(), EVP_MD_CTX_size(),
  560. EVP_MD_CTX_block_size(), EVP_MD_CTX_type(), and EVP_MD_CTX_md_data()
  561. functions were renamed to include C<get> or C<get0> in their names in
  562. OpenSSL 3.0, respectively. The old names are kept as non-deprecated
  563. alias macros.
  564. The EVP_MD_CTX_md() function was deprecated in OpenSSL 3.0; use
  565. EVP_MD_CTX_get0_md() instead.
  566. EVP_MD_CTX_update_fn() and EVP_MD_CTX_set_update_fn() were deprecated
  567. in OpenSSL 3.0.
  568. EVP_MD_CTX_dup() was added in OpenSSL 3.1.
  569. =head1 COPYRIGHT
  570. Copyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved.
  571. Licensed under the Apache License 2.0 (the "License"). You may not use
  572. this file except in compliance with the License. You can obtain a copy
  573. in the file LICENSE in the source distribution or at
  574. L<https://www.openssl.org/source/license.html>.
  575. =cut