EVP_DigestInit.pod 28 KB

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