EVP_DigestInit.pod 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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_copy,
  6. 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_Digest, EVP_DigestInit_ex, EVP_DigestInit, EVP_DigestUpdate,
  12. EVP_DigestFinal_ex, EVP_DigestFinalXOF, EVP_DigestFinal,
  13. EVP_MD_is_a, EVP_MD_name, EVP_MD_number, EVP_MD_names_do_all, EVP_MD_provider,
  14. EVP_MD_type, EVP_MD_pkey_type, EVP_MD_size, EVP_MD_block_size, EVP_MD_flags,
  15. EVP_MD_CTX_name,
  16. EVP_MD_CTX_md, EVP_MD_CTX_type, EVP_MD_CTX_size, EVP_MD_CTX_block_size,
  17. EVP_MD_CTX_md_data, EVP_MD_CTX_update_fn, EVP_MD_CTX_set_update_fn,
  18. EVP_md_null,
  19. EVP_get_digestbyname, EVP_get_digestbynid, EVP_get_digestbyobj,
  20. EVP_MD_CTX_pkey_ctx, EVP_MD_CTX_set_pkey_ctx,
  21. EVP_MD_do_all_provided
  22. - EVP digest routines
  23. =head1 SYNOPSIS
  24. #include <openssl/evp.h>
  25. EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
  26. const char *properties);
  27. int EVP_MD_up_ref(EVP_MD *md);
  28. void EVP_MD_free(EVP_MD *md);
  29. int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[]);
  30. const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest);
  31. EVP_MD_CTX *EVP_MD_CTX_new(void);
  32. int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
  33. void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
  34. void EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void* p2);
  35. int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[]);
  36. int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[]);
  37. const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md);
  38. const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md);
  39. const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx);
  40. const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx);
  41. void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
  42. void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags);
  43. int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags);
  44. int EVP_Digest(const void *data, size_t count, unsigned char *md,
  45. unsigned int *size, const EVP_MD *type, ENGINE *impl);
  46. int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
  47. int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
  48. int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
  49. int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t len);
  50. int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
  51. int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
  52. int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
  53. int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
  54. const char *EVP_MD_name(const EVP_MD *md);
  55. int EVP_MD_number(const EVP_MD *md);
  56. int EVP_MD_is_a(const EVP_MD *md, const char *name);
  57. void EVP_MD_names_do_all(const EVP_MD *md,
  58. void (*fn)(const char *name, void *data),
  59. void *data);
  60. const OSSL_PROVIDER *EVP_MD_provider(const EVP_MD *md);
  61. int EVP_MD_type(const EVP_MD *md);
  62. int EVP_MD_pkey_type(const EVP_MD *md);
  63. int EVP_MD_size(const EVP_MD *md);
  64. int EVP_MD_block_size(const EVP_MD *md);
  65. unsigned long EVP_MD_flags(const EVP_MD *md);
  66. const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
  67. const char *EVP_MD_CTX_name(const EVP_MD_CTX *ctx);
  68. int EVP_MD_CTX_size(const EVP_MD_CTX *ctx);
  69. int EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx);
  70. int EVP_MD_CTX_type(const EVP_MD_CTX *ctx);
  71. void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx);
  72. int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
  73. const void *data, size_t count);
  74. void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
  75. int (*update)(EVP_MD_CTX *ctx,
  76. const void *data, size_t count));
  77. const EVP_MD *EVP_md_null(void);
  78. const EVP_MD *EVP_get_digestbyname(const char *name);
  79. const EVP_MD *EVP_get_digestbynid(int type);
  80. const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
  81. EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx);
  82. void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx);
  83. void EVP_MD_do_all_provided(OPENSSL_CTX *libctx,
  84. void (*fn)(EVP_MD *mac, void *arg),
  85. void *arg);
  86. =head1 DESCRIPTION
  87. The EVP digest routines are a high level interface to message digests,
  88. and should be used instead of the digest-specific functions.
  89. The B<EVP_MD> type is a structure for digest method implementation.
  90. =over 4
  91. =item EVP_MD_fetch()
  92. Fetches the digest implementation for the given B<algorithm> from any
  93. provider offering it, within the criteria given by the B<properties>.
  94. See L<provider(7)/Fetching algorithms> for further information.
  95. The returned value must eventually be freed with EVP_MD_free().
  96. Fetched B<EVP_MD> structures are reference counted.
  97. =item EVP_MD_up_ref()
  98. Increments the reference count for an B<EVP_MD> structure.
  99. =item EVP_MD_free()
  100. Decrements the reference count for the fetched B<EVP_MD> structure.
  101. If the reference count drops to 0 then the structure is freed.
  102. =item EVP_MD_CTX_new()
  103. Allocates and returns a digest context.
  104. =item EVP_MD_CTX_reset()
  105. Resets the digest context B<ctx>. This can be used to reuse an already
  106. existing context.
  107. =item EVP_MD_CTX_free()
  108. Cleans up digest context B<ctx> and frees up the space allocated to it.
  109. =item EVP_MD_CTX_ctrl()
  110. This is a legacy method. EVP_MD_CTX_set_params() and EVP_MD_CTX_get_params()
  111. is the mechanism that should be used to set and get parameters that are used by
  112. providers.
  113. Performs digest-specific control actions on context B<ctx>. The control command
  114. is indicated in B<cmd> and any additional arguments in B<p1> and B<p2>.
  115. EVP_MD_CTX_ctrl() must be called after EVP_DigestInit_ex(). Other restrictions
  116. may apply depending on the control type and digest implementation.
  117. See L</CONTROLS> below for more information.
  118. =item EVP_MD_get_params()
  119. Retrieves the requested list of B<params> from a MD B<md>.
  120. See L</PARAMETERS> below for more information.
  121. =item EVP_MD_CTX_get_params()
  122. Retrieves the requested list of B<params> from a MD context B<ctx>.
  123. See L</PARAMETERS> below for more information.
  124. =item EVP_MD_CTX_set_params()
  125. Sets the list of B<params> into a MD context B<ctx>.
  126. See L</PARAMETERS> below for more information.
  127. =item EVP_MD_gettable_params(), EVP_MD_gettable_ctx_params(),
  128. EVP_MD_settable_ctx_params(), EVP_MD_CTX_gettable_params(),
  129. EVP_MD_CTX_settable_params()
  130. Get a B<OSSL_PARAM> array that describes the retrievable and settable
  131. parameters. EVP_MD_gettable_params() returns parameters that can be used with
  132. EVP_MD_get_params(). EVP_MD_gettable_ctx_params() and
  133. EVP_MD_CTX_gettable_params() return parameters that can be used with
  134. EVP_MD_CTX_get_params(). EVP_MD_settable_ctx_params() and
  135. EVP_MD_CTX_settable_params() return parameters that can be used with
  136. EVP_MD_CTX_set_params().
  137. See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as parameter descriptor.
  138. =item EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags(), EVP_MD_CTX_test_flags()
  139. Sets, clears and tests B<ctx> flags. See L</FLAGS> below for more information.
  140. =item EVP_Digest()
  141. A wrapper around the Digest Init_ex, Update and Final_ex functions.
  142. Hashes B<count> bytes of data at B<data> using a digest B<type> from ENGINE
  143. B<impl>. The digest value is placed in B<md> and its length is written at B<size>
  144. if the pointer is not NULL. At most B<EVP_MAX_MD_SIZE> bytes will be written.
  145. If B<impl> is NULL the default implementation of digest B<type> is used.
  146. =item EVP_DigestInit_ex()
  147. Sets up digest context B<ctx> to use a digest B<type>.
  148. B<type> is typically supplied by a function such as EVP_sha1(), or a
  149. value explicitly fetched with EVP_MD_fetch().
  150. If B<impl> is non-NULL, its implementation of the digest B<type> is used if
  151. there is one, and if not, the default implementation is used.
  152. =item EVP_DigestUpdate()
  153. Hashes B<cnt> bytes of data at B<d> into the digest context B<ctx>. This
  154. function can be called several times on the same B<ctx> to hash additional
  155. data.
  156. =item EVP_DigestFinal_ex()
  157. Retrieves the digest value from B<ctx> and places it in B<md>. If the B<s>
  158. parameter is not NULL then the number of bytes of data written (i.e. the
  159. length of the digest) will be written to the integer at B<s>, at most
  160. B<EVP_MAX_MD_SIZE> bytes will be written. After calling EVP_DigestFinal_ex()
  161. no additional calls to EVP_DigestUpdate() can be made, but
  162. EVP_DigestInit_ex() can be called to initialize a new digest operation.
  163. =item EVP_DigestFinalXOF()
  164. Interfaces to extendable-output functions, XOFs, such as SHAKE128 and SHAKE256.
  165. It retrieves the digest value from B<ctx> and places it in B<len>-sized <B>md.
  166. After calling this function no additional calls to EVP_DigestUpdate() can be
  167. made, but EVP_DigestInit_ex() can be called to initialize a new operation.
  168. =item EVP_MD_CTX_copy_ex()
  169. Can be used to copy the message digest state from B<in> to B<out>. This is
  170. useful if large amounts of data are to be hashed which only differ in the last
  171. few bytes.
  172. =item EVP_DigestInit()
  173. Behaves in the same way as EVP_DigestInit_ex() except it always uses the
  174. default digest implementation and calls EVP_MD_CTX_reset().
  175. =item EVP_DigestFinal()
  176. Similar to EVP_DigestFinal_ex() except the digest context B<ctx> is
  177. automatically cleaned up.
  178. =item EVP_MD_CTX_copy()
  179. Similar to EVP_MD_CTX_copy_ex() except the destination B<out> does not have to
  180. be initialized.
  181. =item EVP_MD_is_a()
  182. Returns 1 if I<md> is an implementation of an algorithm that's
  183. identifiable with I<name>, otherwise 0.
  184. =item EVP_MD_number()
  185. Returns the internal dynamic number assigned to the I<md>. This is
  186. only useful with fetched B<EVP_MD>s.
  187. =item EVP_MD_name(),
  188. EVP_MD_CTX_name()
  189. Return the name of the given message digest. For fetched message
  190. digests with multiple names, only one of them is returned; it's
  191. recommended to use EVP_MD_names_do_all() instead.
  192. =item EVP_MD_names_do_all()
  193. Traverses all names for the I<md>, and calls I<fn> with each name and
  194. I<data>. This is only useful with fetched B<EVP_MD>s.
  195. =item EVP_MD_provider()
  196. Returns an B<OSSL_PROVIDER> pointer to the provider that implements the given
  197. B<EVP_MD>.
  198. =item EVP_MD_size(),
  199. EVP_MD_CTX_size()
  200. Return the size of the message digest when passed an B<EVP_MD> or an
  201. B<EVP_MD_CTX> structure, i.e. the size of the hash.
  202. =item EVP_MD_block_size(),
  203. EVP_MD_CTX_block_size()
  204. Return the block size of the message digest when passed an B<EVP_MD> or an
  205. B<EVP_MD_CTX> structure.
  206. =item EVP_MD_type(),
  207. EVP_MD_CTX_type()
  208. Return the NID of the OBJECT IDENTIFIER representing the given message digest
  209. when passed an B<EVP_MD> structure. For example, C<EVP_MD_type(EVP_sha1())>
  210. returns B<NID_sha1>. This function is normally used when setting ASN1 OIDs.
  211. =item EVP_MD_CTX_md_data()
  212. Return the digest method private data for the passed B<EVP_MD_CTX>.
  213. The space is allocated by OpenSSL and has the size originally set with
  214. EVP_MD_meth_set_app_datasize().
  215. =item EVP_MD_CTX_md()
  216. Returns the B<EVP_MD> structure corresponding to the passed B<EVP_MD_CTX>. This
  217. will be the same B<EVP_MD> object originally passed to EVP_DigestInit_ex() (or
  218. other similar function) when the EVP_MD_CTX was first initialised. Note that
  219. where explicit fetch is in use (see L<EVP_MD_fetch(3)>) the value returned from
  220. this function will not have its reference count incremented and therefore it
  221. should not be used after the EVP_MD_CTX is freed.
  222. =item EVP_MD_CTX_set_update_fn()
  223. Sets the update function for B<ctx> to B<update>.
  224. This is the function that is called by EVP_DigestUpdate. If not set, the
  225. update function from the B<EVP_MD> type specified at initialization is used.
  226. =item EVP_MD_CTX_update_fn()
  227. Returns the update function for B<ctx>.
  228. =item EVP_MD_flags()
  229. Returns the B<md> flags. Note that these are different from the B<EVP_MD_CTX>
  230. ones. See L<EVP_MD_meth_set_flags(3)> for more information.
  231. =item EVP_MD_pkey_type()
  232. Returns the NID of the public key signing algorithm associated with this
  233. digest. For example EVP_sha1() is associated with RSA so this will return
  234. B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms are no
  235. longer linked this function is only retained for compatibility reasons.
  236. =item EVP_md_null()
  237. A "null" message digest that does nothing: i.e. the hash it returns is of zero
  238. length.
  239. =item EVP_get_digestbyname(),
  240. EVP_get_digestbynid(),
  241. EVP_get_digestbyobj()
  242. Returns an B<EVP_MD> structure when passed a digest name, a digest B<NID> or an
  243. B<ASN1_OBJECT> structure respectively.
  244. =item EVP_MD_CTX_pkey_ctx()
  245. Returns the B<EVP_PKEY_CTX> assigned to B<ctx>. The returned pointer should not
  246. be freed by the caller.
  247. =item EVP_MD_CTX_set_pkey_ctx()
  248. Assigns an B<EVP_PKEY_CTX> to B<EVP_MD_CTX>. This is usually used to provide
  249. a customized B<EVP_PKEY_CTX> to L<EVP_DigestSignInit(3)> or
  250. L<EVP_DigestVerifyInit(3)>. The B<pctx> passed to this function should be freed
  251. by the caller. A NULL B<pctx> pointer is also allowed to clear the B<EVP_PKEY_CTX>
  252. assigned to B<ctx>. In such case, freeing the cleared B<EVP_PKEY_CTX> or not
  253. depends on how the B<EVP_PKEY_CTX> is created.
  254. =item EVP_MD_do_all_provided()
  255. Traverses all messages digests implemented by all activated providers
  256. in the given library context I<libctx>, and for each of the implementations,
  257. calls the given function I<fn> with the implementation method and the given
  258. I<arg> as argument.
  259. =back
  260. =head1 PARAMETERS
  261. See L<OSSL_PARAM(3)> for information about passing parameters.
  262. EVP_MD_CTX_set_params() can be used with the following OSSL_PARAM keys:
  263. =over 4
  264. =item "xoflen" (B<OSSL_PARAM_DIGEST_KEY_XOFLEN>) <unsigned integer>
  265. Sets the digest length for extendable output functions.
  266. It is used by the SHAKE algorithm and should not exceed what can be given
  267. using a B<size_t>.
  268. =item "pad_type" (B<OSSL_PARAM_DIGEST_KEY_PAD_TYPE>) <integer>
  269. Sets the pad type.
  270. It is used by the MDC2 algorithm.
  271. =back
  272. EVP_MD_CTX_get_params() can be used with the following OSSL_PARAM keys:
  273. =over 4
  274. =item "micalg" (B<OSSL_PARAM_DIGEST_KEY_MICALG>) <UTF8 string>.
  275. Gets the digest Message Integrity Check algorithm string. This is used when
  276. creating S/MIME multipart/signed messages, as specified in RFC 3851.
  277. It may be used by external engines or providers.
  278. =back
  279. =head1 CONTROLS
  280. EVP_MD_CTX_ctrl() can be used to send the following standard controls:
  281. =over 4
  282. =item EVP_MD_CTRL_MICALG
  283. Gets the digest Message Integrity Check algorithm string. This is used when
  284. creating S/MIME multipart/signed messages, as specified in RFC 3851.
  285. The string value is written to B<p2>.
  286. =item EVP_MD_CTRL_XOF_LEN
  287. This control sets the digest length for extendable output functions to B<p1>.
  288. Sending this control directly should not be necessary, the use of
  289. C<EVP_DigestFinalXOF()> is preferred.
  290. Currently used by SHAKE.
  291. =back
  292. =head1 FLAGS
  293. EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags() and EVP_MD_CTX_test_flags()
  294. can be used the manipulate and test these B<EVP_MD_CTX> flags:
  295. =over 4
  296. =item EVP_MD_CTX_FLAG_ONESHOT
  297. This flag instructs the digest to optimize for one update only, if possible.
  298. =for comment EVP_MD_CTX_FLAG_CLEANED is internal, don't mention it
  299. =for comment EVP_MD_CTX_FLAG_REUSE is internal, don't mention it
  300. =for comment We currently avoid documenting flags that are only bit holder:
  301. EVP_MD_CTX_FLAG_NON_FIPS_ALLOW, EVP_MD_CTX_FLAGS_PAD_*
  302. =item EVP_MD_CTX_FLAG_NO_INIT
  303. This flag instructs EVP_DigestInit() and similar not to initialise the
  304. implementation specific data.
  305. =item EVP_MD_CTX_FLAG_FINALISE
  306. Some functions such as EVP_DigestSign only finalise copies of internal
  307. contexts so additional data can be included after the finalisation call.
  308. This is inefficient if this functionality is not required, and can be
  309. disabled with this flag.
  310. =back
  311. =head1 RETURN VALUES
  312. =over 4
  313. =item EVP_MD_fetch()
  314. Returns a pointer to a B<EVP_MD> for success or NULL for failure.
  315. =item EVP_MD_up_ref()
  316. Returns 1 for success or 0 for failure.
  317. =item EVP_DigestInit_ex(),
  318. EVP_DigestUpdate(),
  319. EVP_DigestFinal_ex()
  320. Returns 1 for
  321. success and 0 for failure.
  322. =item EVP_MD_CTX_ctrl()
  323. Returns 1 if successful or 0 for failure.
  324. =item EVP_MD_CTX_set_params(),
  325. EVP_MD_CTX_get_params()
  326. Returns 1 if successful or 0 for failure.
  327. =item EVP_MD_CTX_settable_params(),
  328. EVP_MD_CTX_gettable_params()
  329. Return an array of constant B<OSSL_PARAM>s, or NULL if there is none
  330. to get.
  331. =item EVP_MD_CTX_copy_ex()
  332. Returns 1 if successful or 0 for failure.
  333. =item EVP_MD_type(),
  334. EVP_MD_pkey_type()
  335. Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef if none
  336. exists.
  337. =item EVP_MD_size(),
  338. EVP_MD_block_size(),
  339. EVP_MD_CTX_size(),
  340. EVP_MD_CTX_block_size()
  341. Returns the digest or block size in bytes.
  342. =item EVP_md_null()
  343. Returns a pointer to the B<EVP_MD> structure of the "null" message digest.
  344. =item EVP_get_digestbyname(),
  345. EVP_get_digestbynid(),
  346. EVP_get_digestbyobj()
  347. Returns either an B<EVP_MD> structure or NULL if an error occurs.
  348. =item EVP_MD_CTX_set_pkey_ctx()
  349. This function has no return value.
  350. =back
  351. =head1 NOTES
  352. The B<EVP> interface to message digests should almost always be used in
  353. preference to the low level interfaces. This is because the code then becomes
  354. transparent to the digest used and much more flexible.
  355. New applications should use the SHA-2 (such as L<EVP_sha256(3)>) or the SHA-3
  356. digest algorithms (such as L<EVP_sha3_512(3)>). The other digest algorithms
  357. are still in common use.
  358. For most applications the B<impl> parameter to EVP_DigestInit_ex() will be
  359. set to NULL to use the default digest implementation.
  360. The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are
  361. obsolete but are retained to maintain compatibility with existing code. New
  362. applications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and
  363. EVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
  364. instead of initializing and cleaning it up on each call and allow non default
  365. implementations of digests to be specified.
  366. If digest contexts are not cleaned up after use,
  367. memory leaks will occur.
  368. EVP_MD_CTX_name(), EVP_MD_CTX_size(), EVP_MD_CTX_block_size(),
  369. EVP_MD_CTX_type(), EVP_get_digestbynid() and EVP_get_digestbyobj() are defined
  370. as macros.
  371. EVP_MD_CTX_ctrl() sends commands to message digests for additional configuration
  372. or control.
  373. =head1 EXAMPLES
  374. This example digests the data "Test Message\n" and "Hello World\n", using the
  375. digest name passed on the command line.
  376. #include <stdio.h>
  377. #include <string.h>
  378. #include <openssl/evp.h>
  379. int main(int argc, char *argv[])
  380. {
  381. EVP_MD_CTX *mdctx;
  382. const EVP_MD *md;
  383. char mess1[] = "Test Message\n";
  384. char mess2[] = "Hello World\n";
  385. unsigned char md_value[EVP_MAX_MD_SIZE];
  386. unsigned int md_len, i;
  387. if (argv[1] == NULL) {
  388. printf("Usage: mdtest digestname\n");
  389. exit(1);
  390. }
  391. md = EVP_get_digestbyname(argv[1]);
  392. if (md == NULL) {
  393. printf("Unknown message digest %s\n", argv[1]);
  394. exit(1);
  395. }
  396. mdctx = EVP_MD_CTX_new();
  397. EVP_DigestInit_ex(mdctx, md, NULL);
  398. EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
  399. EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
  400. EVP_DigestFinal_ex(mdctx, md_value, &md_len);
  401. EVP_MD_CTX_free(mdctx);
  402. printf("Digest is: ");
  403. for (i = 0; i < md_len; i++)
  404. printf("%02x", md_value[i]);
  405. printf("\n");
  406. exit(0);
  407. }
  408. =head1 SEE ALSO
  409. L<EVP_MD_meth_new(3)>,
  410. L<openssl-dgst(1)>,
  411. L<evp(7)>,
  412. L<OSSL_PROVIDER(3)>,
  413. L<OSSL_PARAM(3)>
  414. The full list of digest algorithms are provided below.
  415. L<EVP_blake2b512(3)>,
  416. L<EVP_md2(3)>,
  417. L<EVP_md4(3)>,
  418. L<EVP_md5(3)>,
  419. L<EVP_mdc2(3)>,
  420. L<EVP_ripemd160(3)>,
  421. L<EVP_sha1(3)>,
  422. L<EVP_sha224(3)>,
  423. L<EVP_sha3_224(3)>,
  424. L<EVP_sm3(3)>,
  425. L<EVP_whirlpool(3)>
  426. L<provider(7)/Fetching algorithms>
  427. =head1 HISTORY
  428. The EVP_MD_CTX_create() and EVP_MD_CTX_destroy() functions were renamed to
  429. EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0, respectively.
  430. The link between digests and signing algorithms was fixed in OpenSSL 1.0 and
  431. later, so now EVP_sha1() can be used with RSA and DSA.
  432. The EVP_dss1() function was removed in OpenSSL 1.1.0.
  433. The EVP_MD_CTX_set_pkey_ctx() function was added in 1.1.1.
  434. The EVP_MD_fetch(), EVP_MD_free(), EVP_MD_up_ref(), EVP_MD_CTX_set_params()
  435. and EVP_MD_CTX_get_params() functions were added in 3.0.
  436. =head1 COPYRIGHT
  437. Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
  438. Licensed under the Apache License 2.0 (the "License"). You may not use
  439. this file except in compliance with the License. You can obtain a copy
  440. in the file LICENSE in the source distribution or at
  441. L<https://www.openssl.org/source/license.html>.
  442. =cut