EVP_DigestInit.pod 16 KB

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