EVP_DigestInit.pod 28 KB

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