EVP_DigestInit.pod 27 KB

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