EVP_DigestInit.pod 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. =pod
  2. =head1 NAME
  3. EVP_MD_CTX_new, EVP_MD_CTX_reset, EVP_MD_CTX_free, EVP_MD_CTX_copy_ex,
  4. EVP_MD_CTX_ctrl, EVP_MD_CTX_set_flags, EVP_MD_CTX_clear_flags,
  5. EVP_MD_CTX_test_flags, EVP_DigestInit_ex, EVP_DigestInit, EVP_DigestUpdate,
  6. EVP_DigestFinal_ex, EVP_DigestFinalXOF, EVP_DigestFinal,
  7. EVP_MD_CTX_copy, EVP_MD_type, EVP_MD_pkey_type, EVP_MD_size,
  8. EVP_MD_block_size, EVP_MD_CTX_md, EVP_MD_CTX_size,
  9. EVP_MD_CTX_block_size, EVP_MD_CTX_type, EVP_MD_CTX_md_data,
  10. EVP_md_null,
  11. EVP_get_digestbyname, EVP_get_digestbynid,
  12. EVP_get_digestbyobj,
  13. 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. void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
  21. void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags);
  22. int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags);
  23. int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
  24. int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
  25. int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
  26. int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t len);
  27. int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
  28. int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
  29. int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
  30. int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
  31. int EVP_MD_type(const EVP_MD *md);
  32. int EVP_MD_pkey_type(const EVP_MD *md);
  33. int EVP_MD_size(const EVP_MD *md);
  34. int EVP_MD_block_size(const EVP_MD *md);
  35. const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
  36. int EVP_MD_CTX_size(const EVP_MD *ctx);
  37. int EVP_MD_CTX_block_size(const EVP_MD *ctx);
  38. int EVP_MD_CTX_type(const EVP_MD *ctx);
  39. void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx);
  40. const EVP_MD *EVP_md_null(void);
  41. const EVP_MD *EVP_get_digestbyname(const char *name);
  42. const EVP_MD *EVP_get_digestbynid(int type);
  43. const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
  44. void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx);
  45. =head1 DESCRIPTION
  46. The EVP digest routines are a high level interface to message digests,
  47. and should be used instead of the cipher-specific functions.
  48. =over 4
  49. =item EVP_MD_CTX_new()
  50. Allocates and returns a digest context.
  51. =item EVP_MD_CTX_reset()
  52. Resets the digest context B<ctx>. This can be used to reuse an already
  53. existing context.
  54. =item EVP_MD_CTX_free()
  55. Cleans up digest context B<ctx> and frees up the space allocated to it.
  56. =item EVP_MD_CTX_ctrl()
  57. Performs digest-specific control actions on context B<ctx>.
  58. =item EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags(), EVP_MD_CTX_test_flags()
  59. Sets, clears and tests B<ctx> flags. See L</FLAGS> below for more information.
  60. =item EVP_DigestInit_ex()
  61. Sets up digest context B<ctx> to use a digest B<type> from ENGINE B<impl>.
  62. B<type> will typically be supplied by a function such as EVP_sha1(). If
  63. B<impl> is NULL then the default implementation of digest B<type> is used.
  64. =item EVP_DigestUpdate()
  65. Hashes B<cnt> bytes of data at B<d> into the digest context B<ctx>. This
  66. function can be called several times on the same B<ctx> to hash additional
  67. data.
  68. =item EVP_DigestFinal_ex()
  69. Retrieves the digest value from B<ctx> and places it in B<md>. If the B<s>
  70. parameter is not NULL then the number of bytes of data written (i.e. the
  71. length of the digest) will be written to the integer at B<s>, at most
  72. B<EVP_MAX_MD_SIZE> bytes will be written. After calling EVP_DigestFinal_ex()
  73. no additional calls to EVP_DigestUpdate() can be made, but
  74. EVP_DigestInit_ex() can be called to initialize a new digest operation.
  75. =item EVP_DigestFinalXOF()
  76. Interfaces to extendable-output functions, XOFs, such as SHAKE128 and SHAKE256.
  77. It retrieves the digest value from B<ctx> and places it in B<len>-sized <B>md.
  78. After calling this function no additional calls to EVP_DigestUpdate() can be
  79. made, but EVP_DigestInit_ex() can be called to initialize a new operation.
  80. =item EVP_MD_CTX_copy_ex()
  81. Can be used to copy the message digest state from B<in> to B<out>. This is
  82. useful if large amounts of data are to be hashed which only differ in the last
  83. few bytes.
  84. =item EVP_DigestInit()
  85. Behaves in the same way as EVP_DigestInit_ex() except it always uses the
  86. default digest implementation.
  87. =item EVP_DigestFinal()
  88. Similar to EVP_DigestFinal_ex() except the digest context B<ctx> is
  89. automatically cleaned up.
  90. =item EVP_MD_CTX_copy()
  91. Similar to EVP_MD_CTX_copy_ex() except the destination B<out> does not have to
  92. be initialized.
  93. =item EVP_MD_size(),
  94. EVP_MD_CTX_size()
  95. Return the size of the message digest when passed an B<EVP_MD> or an
  96. B<EVP_MD_CTX> structure, i.e. the size of the hash.
  97. =item EVP_MD_block_size(),
  98. EVP_MD_CTX_block_size()
  99. Return the block size of the message digest when passed an B<EVP_MD> or an
  100. B<EVP_MD_CTX> structure.
  101. =item EVP_MD_type(),
  102. EVP_MD_CTX_type()
  103. Return the NID of the OBJECT IDENTIFIER representing the given message digest
  104. when passed an B<EVP_MD> structure. For example, C<EVP_MD_type(EVP_sha1())>
  105. returns B<NID_sha1>. This function is normally used when setting ASN1 OIDs.
  106. =item EVP_MD_CTX_md_data()
  107. Return the digest method private data for the passed B<EVP_MD_CTX>.
  108. The space is allocated by OpenSSL and has the size originally set with
  109. EVP_MD_meth_set_app_datasize().
  110. =item EVP_MD_CTX_md()
  111. Returns the B<EVP_MD> structure corresponding to the passed B<EVP_MD_CTX>.
  112. =item EVP_MD_pkey_type()
  113. Returns the NID of the public key signing algorithm associated with this
  114. digest. For example EVP_sha1() is associated with RSA so this will return
  115. B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms are no
  116. longer linked this function is only retained for compatibility reasons.
  117. =item EVP_md_null()
  118. A "null" message digest that does nothing: i.e. the hash it returns is of zero
  119. length.
  120. =item EVP_get_digestbyname(),
  121. EVP_get_digestbynid(),
  122. EVP_get_digestbyobj()
  123. Returns an B<EVP_MD> structure when passed a digest name, a digest B<NID> or an
  124. B<ASN1_OBJECT> structure respectively.
  125. =item EVP_MD_CTX_set_pkey_ctx()
  126. Assigns an B<EVP_PKEY_CTX> to B<EVP_MD_CTX>. This is usually used to provide
  127. a customzied B<EVP_PKEY_CTX> to L<EVP_DigestSignInit(3)> or
  128. L<EVP_DigestVerifyInit(3)>. The B<pctx> passed to this function should be freed
  129. by the caller. A NULL B<pctx> pointer is also allowed to clear the B<EVP_PKEY_CTX>
  130. assigned to B<ctx>. In such case, freeing the cleared B<EVP_PKEY_CTX> or not
  131. depends on how the B<EVP_PKEY_CTX> is created.
  132. =back
  133. =head1 FLAGS
  134. EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags() and EVP_MD_CTX_test_flags()
  135. can be used the manipulate and test these B<EVP_MD_CTX> flags:
  136. =over 4
  137. =item EVP_MD_CTX_FLAG_ONESHOT
  138. This flag instructs the digest to optimize for one update only, if possible.
  139. =for comment EVP_MD_CTX_FLAG_CLEANED is internal, don't mention it
  140. =for comment EVP_MD_CTX_FLAG_REUSE is internal, don't mention it
  141. =for comment We currently avoid documenting flags that are only bit holder:
  142. EVP_MD_CTX_FLAG_NON_FIPS_ALLOW, EVP_MD_CTX_FLAGS_PAD_*
  143. =item EVP_MD_CTX_FLAG_NO_INIT
  144. This flag instructs EVP_DigestInit() and similar not to initialise the
  145. implementation specific data.
  146. =item EVP_MD_CTX_FLAG_FINALISE
  147. Some functions such as EVP_DigestSign only finalise copies of internal
  148. contexts so additional data can be included after the finalisation call.
  149. This is inefficient if this functionality is not required, and can be
  150. disabled with this flag.
  151. =back
  152. =head1 RETURN VALUES
  153. =over 4
  154. =item EVP_DigestInit_ex(),
  155. EVP_DigestUpdate(),
  156. EVP_DigestFinal_ex()
  157. Returns 1 for
  158. success and 0 for failure.
  159. =item EVP_MD_CTX_ctrl()
  160. Returns 1 if successful or 0 for failure.
  161. =item EVP_MD_CTX_copy_ex()
  162. Returns 1 if successful or 0 for failure.
  163. =item EVP_MD_type(),
  164. EVP_MD_pkey_type(),
  165. EVP_MD_type()
  166. Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef if none
  167. exists.
  168. =item EVP_MD_size(),
  169. EVP_MD_block_size(),
  170. EVP_MD_CTX_size(),
  171. EVP_MD_CTX_block_size()
  172. Returns the digest or block size in bytes.
  173. =item EVP_md_null()
  174. Returns a pointer to the B<EVP_MD> structure of the "null" message digest.
  175. =item EVP_get_digestbyname(),
  176. EVP_get_digestbynid(),
  177. EVP_get_digestbyobj()
  178. Returns either an B<EVP_MD> structure or NULL if an error occurs.
  179. =item EVP_MD_CTX_set_pkey_ctx()
  180. This function has no return value.
  181. =back
  182. =head1 NOTES
  183. The B<EVP> interface to message digests should almost always be used in
  184. preference to the low level interfaces. This is because the code then becomes
  185. transparent to the digest used and much more flexible.
  186. New applications should use the SHA-2 (such as L<EVP_sha256(3)>) or the SHA-3
  187. digest algorithms (such as L<EVP_sha3_512(3)>). The other digest algorithms
  188. are still in common use.
  189. For most applications the B<impl> parameter to EVP_DigestInit_ex() will be
  190. set to NULL to use the default digest implementation.
  191. The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are
  192. obsolete but are retained to maintain compatibility with existing code. New
  193. applications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and
  194. EVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
  195. instead of initializing and cleaning it up on each call and allow non default
  196. implementations of digests to be specified.
  197. If digest contexts are not cleaned up after use,
  198. memory leaks will occur.
  199. EVP_MD_CTX_size(), EVP_MD_CTX_block_size(), EVP_MD_CTX_type(),
  200. EVP_get_digestbynid() and EVP_get_digestbyobj() are defined as
  201. macros.
  202. EVP_MD_CTX_ctrl() sends commands to message digests for additional configuration
  203. or control.
  204. =head1 EXAMPLE
  205. This example digests the data "Test Message\n" and "Hello World\n", using the
  206. digest name passed on the command line.
  207. #include <stdio.h>
  208. #include <string.h>
  209. #include <openssl/evp.h>
  210. int main(int argc, char *argv[])
  211. {
  212. EVP_MD_CTX *mdctx;
  213. const EVP_MD *md;
  214. char mess1[] = "Test Message\n";
  215. char mess2[] = "Hello World\n";
  216. unsigned char md_value[EVP_MAX_MD_SIZE];
  217. unsigned int md_len, i;
  218. if (argv[1] == NULL) {
  219. printf("Usage: mdtest digestname\n");
  220. exit(1);
  221. }
  222. md = EVP_get_digestbyname(argv[1]);
  223. if (md == NULL) {
  224. printf("Unknown message digest %s\n", argv[1]);
  225. exit(1);
  226. }
  227. mdctx = EVP_MD_CTX_new();
  228. EVP_DigestInit_ex(mdctx, md, NULL);
  229. EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
  230. EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
  231. EVP_DigestFinal_ex(mdctx, md_value, &md_len);
  232. EVP_MD_CTX_free(mdctx);
  233. printf("Digest is: ");
  234. for (i = 0; i < md_len; i++)
  235. printf("%02x", md_value[i]);
  236. printf("\n");
  237. exit(0);
  238. }
  239. =head1 SEE ALSO
  240. L<dgst(1)>,
  241. L<evp(7)>
  242. The full list of digest algorithms are provided below.
  243. L<EVP_blake2b512(3)>,
  244. L<EVP_md2(3)>,
  245. L<EVP_md4(3)>,
  246. L<EVP_md5(3)>,
  247. L<EVP_mdc2(3)>,
  248. L<EVP_ripemd160(3)>,
  249. L<EVP_sha1(3)>,
  250. L<EVP_sha224(3)>,
  251. L<EVP_sha3_224(3)>,
  252. L<EVP_sm3(3)>,
  253. L<EVP_whirlpool(3)>
  254. =head1 HISTORY
  255. EVP_MD_CTX_create() and EVP_MD_CTX_destroy() were renamed to
  256. EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0.
  257. The link between digests and signing algorithms was fixed in OpenSSL 1.0 and
  258. later, so now EVP_sha1() can be used with RSA and DSA.
  259. EVP_dss1() was removed in OpenSSL 1.1.0.
  260. EVP_MD_CTX_set_pkey_ctx() was added in 1.1.1.
  261. =head1 COPYRIGHT
  262. Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
  263. Licensed under the Apache License 2.0 (the "License"). You may not use
  264. this file except in compliance with the License. You can obtain a copy
  265. in the file LICENSE in the source distribution or at
  266. L<https://www.openssl.org/source/license.html>.
  267. =cut