EVP_DigestInit.pod 9.2 KB

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